refactor: 将 weixin 模块从 src/ 迁移至 packages/weixin 工作区包

将 src/services/weixin/ 中的纯业务逻辑迁入 @claude-code-best/weixin
workspace 包,降低 src/ 耦合度。仅保留 server.ts 作为薄适配层。

- 迁移 7 个无修改的纯模块 (types/api/accounts/login/pairing/media/send)
- monitor.ts 内联 PERMISSION_REPLY_RE 正则,解除对 src/ 的依赖
- permissions.ts 本地定义 ChannelPermissionRequestParams 接口
- cli.ts 拆分:serve 子命令通过回调注入,login/access 保留在包内
- server.ts 重写为从 @claude-code-best/weixin 导入
- 新增 cli-serve.ts 作为 serve 入口薄壳

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-19 20:55:13 +08:00
parent 0201db55ac
commit 5ed0e63095
25 changed files with 183 additions and 14 deletions

View File

@@ -34,6 +34,7 @@
"@claude-code-best/agent-tools": "workspace:*",
"@claude-code-best/builtin-tools": "workspace:*",
"@claude-code-best/mcp-client": "workspace:*",
"@claude-code-best/weixin": "workspace:*",
"@commander-js/extra-typings": "^14.0.0",
"@growthbook/growthbook": "^1.6.5",
"@langfuse/otel": "^5.1.0",
@@ -320,6 +321,13 @@
"name": "url-handler-napi",
"version": "1.0.0",
},
"packages/weixin": {
"name": "@claude-code-best/weixin",
"version": "1.0.0",
"dependencies": {
"qrcode": "^1.5.4",
},
},
},
"packages": {
"@agentclientprotocol/sdk": ["@agentclientprotocol/sdk@0.19.0", "https://registry.npmmirror.com/@agentclientprotocol/sdk/-/sdk-0.19.0.tgz", { "peerDependencies": { "zod": "^3.25.0 || ^4.0.0" } }, "sha512-U9I8ws9WTOk6jCBAWpXefGSDgVXn14/kV6HFzwWGcstQ02mOQgClMAROHmoIn9GqZbDBDEOkdIbP4P4TEMQdug=="],
@@ -564,6 +572,8 @@
"@claude-code-best/mcp-client": ["@claude-code-best/mcp-client@workspace:packages/mcp-client"],
"@claude-code-best/weixin": ["@claude-code-best/weixin@workspace:packages/weixin"],
"@commander-js/extra-typings": ["@commander-js/extra-typings@14.0.0", "https://registry.npmmirror.com/@commander-js/extra-typings/-/extra-typings-14.0.0.tgz", { "peerDependencies": { "commander": "~14.0.0" } }, "sha512-hIn0ncNaJRLkZrxBIp5AsW/eXEHNKYQBh0aPdoUqNgD+Io3NIykQqpKFyKcuasZhicGaEZJX/JBSIkZ4e5x8Dg=="],
"@emnapi/core": ["@emnapi/core@1.9.2", "https://registry.npmmirror.com/@emnapi/core/-/core-1.9.2.tgz", { "dependencies": { "@emnapi/wasi-threads": "1.2.1", "tslib": "^2.4.0" } }, "sha512-UC+ZhH3XtczQYfOlu3lNEkdW/p4dsJ1r/bP7H8+rhao3TTTMO1ATq/4DdIi23XuGoFY+Cz0JmCbdVl0hz9jZcA=="],

View File

@@ -90,6 +90,7 @@
"@claude-code-best/agent-tools": "workspace:*",
"@claude-code-best/builtin-tools": "workspace:*",
"@claude-code-best/mcp-client": "workspace:*",
"@claude-code-best/weixin": "workspace:*",
"@commander-js/extra-typings": "^14.0.0",
"@growthbook/growthbook": "^1.6.5",
"@langfuse/otel": "^5.1.0",

View File

@@ -0,0 +1,11 @@
{
"name": "@claude-code-best/weixin",
"version": "1.0.0",
"private": true,
"type": "module",
"main": "./src/index.ts",
"types": "./src/index.ts",
"dependencies": {
"qrcode": "^1.5.4"
}
}

View File

@@ -1,7 +1,6 @@
import { clearAccount, DEFAULT_BASE_URL, loadAccount, saveAccount } from './accounts.js'
import { startLogin, waitForLogin } from './login.js'
import { confirmPairing } from './pairing.js'
import { runWeixinMcpServer } from './server.js'
function printUsage(): void {
process.stdout.write(
@@ -91,12 +90,20 @@ function runAccess(args: string[]): void {
process.stdout.write(`Paired successfully: ${userId}\n`)
}
export async function handleWeixinCli(args: string[]): Promise<void> {
export async function handleWeixinCli(
args: string[],
serveHandler?: () => Promise<void>,
): Promise<void> {
const [subcommand, ...rest] = args
switch (subcommand) {
case 'serve':
await runWeixinMcpServer()
if (serveHandler) {
await serveHandler()
} else {
process.stderr.write('[weixin] serve handler not available in this context.\n')
process.exit(1)
}
return
case 'login':
await runLogin(rest[0] === 'clear')

View File

@@ -0,0 +1,111 @@
// @claude-code-best/weixin — WeChat channel integration
// Types
export {
MessageType,
MessageItemType,
MessageState,
UploadMediaType,
TypingStatus,
} from './types.js'
export type {
BaseInfo,
CDNMedia,
TextItem,
ImageItem,
VoiceItem,
FileItem,
VideoItem,
RefMessage,
MessageItem,
WeixinMessage,
GetUpdatesReq,
GetUpdatesResp,
SendMessageReq,
GetUploadUrlReq,
GetUploadUrlResp,
GetConfigResp,
SendTypingReq,
SendTypingResp,
} from './types.js'
// API client
export {
getUpdates,
sendMessage,
getUploadUrl,
getConfig,
sendTyping,
} from './api.js'
// Account management
export {
DEFAULT_BASE_URL,
CDN_BASE_URL,
getStateDir,
loadAccount,
saveAccount,
clearAccount,
} from './accounts.js'
export type { AccountData } from './accounts.js'
// Login
export { startLogin, waitForLogin } from './login.js'
export type { QRCodeResult, LoginResult } from './login.js'
// Pairing / access control
export {
loadAccessConfig,
saveAccessConfig,
isAllowed,
addPendingPairing,
confirmPairing,
} from './pairing.js'
export type { AccessConfig } from './pairing.js'
// Media encryption / upload
export {
encryptAesEcb,
decryptAesEcb,
aesEcbPaddedSize,
buildCdnDownloadUrl,
buildCdnUploadUrl,
parseAesKey,
downloadAndDecrypt,
uploadFile,
guessMediaType,
downloadRemoteToTemp,
} from './media.js'
export type { UploadedFileInfo } from './media.js'
// Message sending
export { markdownToPlainText, sendText, sendMediaFile } from './send.js'
// Monitor (message polling)
export {
getContextToken,
extractPermissionReply,
startPollLoop,
} from './monitor.js'
export type {
ParsedMessage,
OnMessageCallback,
PermissionResponse,
OnPermissionResponseCallback,
} from './monitor.js'
// Permission state
export {
ChannelPermissionRequestParams,
setActivePermissionChat,
getActivePermissionChat,
savePendingPermission,
consumePendingPermission,
} from './permissions.js'
export type {
PendingPermissionRequest,
ActivePermissionChat,
} from './permissions.js'
// CLI
export { handleWeixinCli } from './cli.js'

View File

@@ -6,7 +6,8 @@ import {
} from 'node:fs'
import { tmpdir } from 'node:os'
import { basename, join } from 'node:path'
import { PERMISSION_REPLY_RE } from '../mcp/channelPermissions.js'
// Matches the canonical definition in src/services/mcp/channelPermissions.ts
const PERMISSION_REPLY_RE = /^\s*(y|yes|n|no)\s+([a-km-z]{5})\s*$/i
import { getUpdates } from './api.js'
import { getStateDir } from './accounts.js'
import { downloadAndDecrypt } from './media.js'

View File

@@ -1,4 +1,14 @@
import type { ChannelPermissionRequestParams } from '../mcp/channelNotification.js'
/** Mirrors ChannelPermissionRequestParams from src/services/mcp/channelNotification.ts */
export interface ChannelPermissionRequestParams {
request_id: string
tool_name: string
description: string
input_preview: string
channel_context?: {
source_server?: string
chat_id?: string
}
}
export type PendingPermissionRequest = ChannelPermissionRequestParams & {
chatId: string

View File

@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}

View File

@@ -142,8 +142,9 @@ async function main(): Promise<void> {
if (args[0] === 'weixin') {
profileCheckpoint('cli_weixin_path')
const { handleWeixinCli } = await import('../services/weixin/cli.js')
await handleWeixinCli(args.slice(1))
const { handleWeixinCli } = await import('@claude-code-best/weixin')
const { runWeixinMcpServer } = await import('../services/weixin/cli-serve.js')
await handleWeixinCli(args.slice(1), runWeixinMcpServer)
return
}

View File

@@ -0,0 +1 @@
export { runWeixinMcpServer } from './server.js'

View File

@@ -14,12 +14,21 @@ import { shutdownDatadog } from '../analytics/datadog.js'
import { shutdown1PEventLogging } from '../analytics/firstPartyEventLogger.js'
import { enableConfigs } from '../../utils/config.js'
import { logForDebugging } from '../../utils/debug.js'
import { CDN_BASE_URL, DEFAULT_BASE_URL, loadAccount } from './accounts.js'
import { getConfig, sendTyping } from './api.js'
import { getContextToken, startPollLoop, type ParsedMessage } from './monitor.js'
import { getActivePermissionChat, savePendingPermission } from './permissions.js'
import { sendMediaFile, sendText } from './send.js'
import { TypingStatus } from './types.js'
import {
CDN_BASE_URL,
DEFAULT_BASE_URL,
loadAccount,
getConfig,
sendTyping,
getContextToken,
startPollLoop,
getActivePermissionChat,
savePendingPermission,
sendMediaFile,
sendText,
TypingStatus,
} from '@claude-code-best/weixin'
import type { ParsedMessage } from '@claude-code-best/weixin'
function formatPermissionRequestMessage(
request: ChannelPermissionRequestParams,

View File

@@ -19,7 +19,9 @@
"@claude-code-best/mcp-client/*": ["./packages/mcp-client/src/*"],
"@claude-code-best/mcp-client": ["./packages/mcp-client/src/index.ts"],
"@claude-code-best/agent-tools/*": ["./packages/agent-tools/src/*"],
"@claude-code-best/agent-tools": ["./packages/agent-tools/src/index.ts"]
"@claude-code-best/agent-tools": ["./packages/agent-tools/src/index.ts"],
"@claude-code-best/weixin/*": ["./packages/weixin/src/*"],
"@claude-code-best/weixin": ["./packages/weixin/src/index.ts"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "packages/**/*.ts", "packages/**/*.tsx"],