feat: 接入内建 weixin channel(同 #301 重构版本) (#303)

* feat: 接入 weixin 服务层与命令入口

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* feat: 注册内建 weixin channel 插件

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix: 修正 channel permission relay 路由与能力判定

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix: 修复 builtin channel 的 ChannelsNotice 误报

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* docs: 补充内建 weixin channel 使用说明

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* docs: 更新微信 channel 接入计划状态

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix: 延迟加载 weixin 登录二维码依赖

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix: 改用 qrcode 生成 weixin 登录二维码

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix: 修正 vite 构建的 Windows 路径解析

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* chore: 删除临时规划文档 wx_channel.md 并还原 package.json 排序

wx_channel.md 内容已整合到 docs/features/channels.md,不再需要。
package.json 中 @ant/model-provider 位置从原始位置被无意移动,还原。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* 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>

* fix: 修正 weixin barrel export 中 interface 的导出方式

ChannelPermissionRequestParams 是纯类型,必须用 export type 导出,
否则 Bun 运行时会报 "export not found" 错误。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: 将 server.ts 迁入 packages/weixin,彻底移除 src/services/weixin/

通过依赖注入(WeixinServerDeps)解耦 src/ 依赖(analytics、config、
MCP channel schema),server.ts 完全移入包内。cli.tsx 入口处一次性
注入所有依赖。

src/services/weixin/ 目录已完全删除。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: 修复 markdownToPlainText 中代码块正则的 ReDoS 风险

用非正则的线性扫描替代 \`\`\`[\s\S]*?\n([\s\S]*?)\`\`\` 匹配,
避免在含有大量重复 \`\`\` 序列的输入上触发多项式回溯。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: 1111 <11111@asd.c>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-19 21:33:27 +08:00
committed by GitHub
parent b83c3008d0
commit 494eab7204
39 changed files with 2616 additions and 19 deletions

View File

@@ -11,6 +11,7 @@ import {
getAllowedChannels,
getHasDevChannels,
} from '../../bootstrap/state.js'
import { getBuiltinPlugins } from '../../plugins/builtinPlugins.js'
import { Box, Text } from '@anthropic/ink'
import { getMcpConfigsByScope } from '../../services/mcp/config.js'
import { loadInstalledPluginsV2 } from '../../utils/plugins/installedPluginsManager.js'
@@ -75,25 +76,39 @@ function formatEntry(c: ChannelEntry): string {
type Unmatched = { entry: ChannelEntry; why: string }
function findUnmatched(
type FindUnmatchedDeps = {
configuredServerNames?: ReadonlySet<string>
installedPluginIds?: ReadonlySet<string>
}
export function findUnmatched(
entries: readonly ChannelEntry[],
deps?: FindUnmatchedDeps,
): Unmatched[] {
// Server-kind: build one Set from all scopes up front. getMcpConfigsByScope
// is not cached (project scope walks the dir tree); getMcpConfigByName would
// redo that walk per entry.
const scopes = ['enterprise', 'user', 'project', 'local'] as const
const configured = new Set<string>()
for (const scope of scopes) {
for (const name of Object.keys(getMcpConfigsByScope(scope).servers)) {
configured.add(name)
const configured = deps?.configuredServerNames ?? (() => {
const scopes = ['enterprise', 'user', 'project', 'local'] as const
const names = new Set<string>()
for (const scope of scopes) {
for (const name of Object.keys(getMcpConfigsByScope(scope).servers)) {
names.add(name)
}
}
}
return names
})()
// Plugin-kind installed check: installed_plugins.json keys are
// `name@marketplace`. loadInstalledPluginsV2 is cached.
const installedPluginIds = new Set(
Object.keys(loadInstalledPluginsV2().plugins),
)
const installedPluginIds = deps?.installedPluginIds ?? (() => {
const ids = new Set(Object.keys(loadInstalledPluginsV2().plugins))
const builtinPlugins = getBuiltinPlugins()
for (const plugin of [...builtinPlugins.enabled, ...builtinPlugins.disabled]) {
ids.add(plugin.source)
}
return ids
})()
const out: Unmatched[] = []
for (const entry of entries) {

View File

@@ -0,0 +1,17 @@
import { describe, expect, test } from 'bun:test'
import { findUnmatched } from '../ChannelsNotice.js'
describe('findUnmatched', () => {
test('does not flag builtin weixin as plugin not installed', () => {
expect(
findUnmatched(
[{ kind: 'plugin', name: 'weixin', marketplace: 'builtin' }],
{
configuredServerNames: new Set(),
installedPluginIds: new Set(['weixin@builtin']),
},
),
).toEqual([])
})
})

View File

@@ -140,6 +140,31 @@ async function main(): Promise<void> {
return
}
if (args[0] === 'weixin') {
profileCheckpoint('cli_weixin_path')
const { handleWeixinCli } = await import('@claude-code-best/weixin')
const { enableConfigs } = await import('../utils/config.js')
const { initializeAnalyticsSink } = await import('../services/analytics/sink.js')
const { shutdownDatadog } = await import('../services/analytics/datadog.js')
const { shutdown1PEventLogging } = await import('../services/analytics/firstPartyEventLogger.js')
const { logForDebugging } = await import('../utils/debug.js')
const { ChannelPermissionRequestNotificationSchema } = await import('../services/mcp/channelNotification.js')
await handleWeixinCli(args.slice(1), {
enableConfigs,
initializeAnalyticsSink,
shutdownDatadog,
shutdown1PEventLogging,
logForDebugging,
registerPermissionHandler(server, handler) {
server.setNotificationHandler(
ChannelPermissionRequestNotificationSchema(),
async notification => handler(notification.params),
)
},
}, MACRO.VERSION)
return
}
// Fast-path for `--daemon-worker=<kind>` (internal — supervisor spawns this).
// Must come before the daemon subcommand check: spawned per-worker, so
// perf-sensitive. No enableConfigs(), no analytics sinks at this layer —

View File

@@ -0,0 +1,38 @@
import { describe, expect, test } from 'bun:test'
import { getLatestChannelContextHint } from '../interactiveHandler.js'
describe('getLatestChannelContextHint', () => {
test('extracts source server and chat id from latest channel user message', () => {
expect(
getLatestChannelContextHint([
{
type: 'user',
origin: { kind: 'channel', server: 'plugin:weixin:weixin' },
message: {
content: [
{
type: 'text',
text: '<channel source="plugin:weixin:weixin" chat_id="user-1" sender_id="user-1">\nhello\n</channel>',
},
],
},
},
]),
).toEqual({
sourceServer: 'plugin:weixin:weixin',
chatId: 'user-1',
})
})
test('returns null when there is no channel-origin user message', () => {
expect(
getLatestChannelContextHint([
{
type: 'user',
origin: { kind: 'manual' },
message: { content: [{ type: 'text', text: 'hello' }] },
},
]),
).toBeNull()
})
})

View File

@@ -1,6 +1,7 @@
import { feature } from 'bun:bundle'
import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages.mjs'
import { randomUUID } from 'crypto'
import { CHANNEL_TAG } from 'src/constants/xml.js'
import { logForDebugging } from 'src/utils/debug.js'
import { getAllowedChannels } from '../../../bootstrap/state.js'
import type { BridgePermissionCallbacks } from '../../../bridge/bridgePermissionCallbacks.js'
@@ -46,6 +47,76 @@ type InteractivePermissionParams = {
channelCallbacks?: ChannelPermissionCallbacks
}
type ChannelContextHint = {
sourceServer?: string
chatId?: string
}
function getTextBlocksText(content: unknown): string {
if (typeof content === 'string') {
return content
}
if (!Array.isArray(content)) {
return ''
}
return content
.filter(
(block): block is { type: 'text'; text: string } =>
typeof block === 'object' &&
block !== null &&
(block as { type?: unknown }).type === 'text' &&
typeof (block as { text?: unknown }).text === 'string',
)
.map(block => block.text)
.join('\n')
}
function parseChannelContextHintFromText(text: string): ChannelContextHint | null {
const tagMatch = text.match(new RegExp(`<${CHANNEL_TAG}\\b([^>]*)>`))
if (!tagMatch?.[1]) {
return null
}
const attrs = tagMatch[1]
const sourceServer = attrs.match(/\bsource="([^"]+)"/)?.[1]
const chatId = attrs.match(/\bchat_id="([^"]+)"/)?.[1]
if (!sourceServer && !chatId) {
return null
}
return { sourceServer, chatId }
}
export function getLatestChannelContextHint(messages: readonly unknown[]): ChannelContextHint | null {
for (let index = messages.length - 1; index >= 0; index--) {
const message = messages[index] as {
type?: unknown
origin?: { kind?: unknown; server?: unknown }
message?: { content?: unknown }
}
if (message?.type !== 'user' || message?.origin?.kind !== 'channel') {
continue
}
const text = getTextBlocksText(message.message?.content)
const parsed = parseChannelContextHintFromText(text)
if (parsed) {
return {
sourceServer:
parsed.sourceServer ||
(typeof message.origin.server === 'string'
? message.origin.server
: undefined),
chatId: parsed.chatId,
}
}
}
return null
}
/**
* Handles the interactive (main-agent) permission flow.
*
@@ -420,6 +491,17 @@ function handleInteractivePermission(
description,
input_preview: truncateForPreview(displayInput),
}
const channelContext = getLatestChannelContextHint(
ctx.toolUseContext.messages,
)
if (channelContext?.sourceServer || channelContext?.chatId) {
params.channel_context = {
...(channelContext.sourceServer && {
source_server: channelContext.sourceServer,
}),
...(channelContext.chatId && { chat_id: channelContext.chatId }),
}
}
for (const client of channelClients) {
if (client.type !== 'connected') continue // refine for TS

View File

@@ -14,10 +14,11 @@
* 2. Call registerBuiltinPlugin() with the plugin definition here
*/
import { registerWeixinBuiltinPlugin } from './weixin.js'
/**
* Initialize built-in plugins. Called during CLI startup.
*/
export function initBuiltinPlugins(): void {
// No built-in plugins registered yet — this is the scaffolding for
// migrating bundled skills that should be user-toggleable.
registerWeixinBuiltinPlugin()
}

View File

@@ -0,0 +1,21 @@
import { registerBuiltinPlugin } from '../builtinPlugins.js'
import { buildCliLaunch } from '../../utils/cliLaunch.js'
export function registerWeixinBuiltinPlugin(): void {
const launch = buildCliLaunch(['weixin', 'serve'])
registerBuiltinPlugin({
name: 'weixin',
description:
'WeChat channel integration. Enables inbound WeChat messages via channels and provides reply/send_typing MCP tools. Configure with `ccb weixin login` and enable for a session with `--channels plugin:weixin@builtin`.',
version: MACRO.VERSION,
defaultEnabled: true,
mcpServers: {
weixin: {
type: 'stdio',
command: launch.execPath,
args: launch.args,
},
},
})
}

View File

@@ -0,0 +1,17 @@
import { describe, expect, mock, test } from 'bun:test'
mock.module('../../analytics/growthbook.js', () => ({
getFeatureValue_CACHED_MAY_BE_STALE: () => [],
}))
import { isChannelAllowlisted } from '../channelAllowlist.js'
describe('isChannelAllowlisted', () => {
test('allows builtin weixin plugin', () => {
expect(isChannelAllowlisted('weixin@builtin')).toBe(true)
})
test('rejects undefined plugin source', () => {
expect(isChannelAllowlisted(undefined)).toBe(false)
})
})

View File

@@ -5,6 +5,7 @@ mock.module("src/services/analytics/growthbook.js", () => ({
}));
const {
filterPermissionRelayClients,
shortRequestId,
truncateForPreview,
PERMISSION_REPLY_RE,
@@ -160,3 +161,34 @@ describe("createChannelPermissionCallbacks", () => {
expect(received?.behavior).toBe("deny");
});
});
describe("filterPermissionRelayClients", () => {
test("requires truthy permission capability", () => {
const clients = [
{
type: "connected",
name: "plugin:weixin:weixin",
capabilities: {
experimental: {
"claude/channel": {},
"claude/channel/permission": false,
},
},
},
{
type: "connected",
name: "plugin:telegram:telegram",
capabilities: {
experimental: {
"claude/channel": {},
"claude/channel/permission": {},
},
},
},
];
expect(
filterPermissionRelayClients(clients, () => true).map(client => client.name),
).toEqual(["plugin:telegram:telegram"]);
});
});

View File

@@ -16,6 +16,7 @@
*/
import { z } from 'zod/v4'
import { BUILTIN_MARKETPLACE_NAME } from '../../plugins/builtinPlugins.js'
import { lazySchema } from '../../utils/lazySchema.js'
import { parsePluginIdentifier } from '../../utils/plugins/pluginIdentifier.js'
import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js'
@@ -68,6 +69,9 @@ export function isChannelAllowlisted(
if (!pluginSource) return false
const { name, marketplace } = parsePluginIdentifier(pluginSource)
if (!marketplace) return false
if (marketplace === BUILTIN_MARKETPLACE_NAME && name === 'weixin') {
return true
}
return getChannelAllowlist().some(
e => e.plugin === name && e.marketplace === marketplace,
)

View File

@@ -91,8 +91,33 @@ export type ChannelPermissionRequestParams = {
* input is in the local terminal dialog; this is a phone-sized
* preview. Server decides whether/how to show it. */
input_preview: string
/** Optional source-channel routing hint for servers that support
* multi-chat routing. Backwards compatible: servers that don't care can
* ignore it and keep their existing fallback behavior. */
channel_context?: {
source_server?: string
chat_id?: string
}
}
export const ChannelPermissionRequestNotificationSchema = lazySchema(() =>
z.object({
method: z.literal(CHANNEL_PERMISSION_REQUEST_METHOD),
params: z.object({
request_id: z.string(),
tool_name: z.string(),
description: z.string(),
input_preview: z.string(),
channel_context: z
.object({
source_server: z.string().optional(),
chat_id: z.string().optional(),
})
.optional(),
}),
}),
)
/**
* Meta keys become XML attribute NAMES — a crafted key like
* `x="" injected="y` would break out of the attribute structure. Only

View File

@@ -34,7 +34,7 @@ import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js'
* don't apply until restart.
*/
export function isChannelPermissionRelayEnabled(): boolean {
return getFeatureValue_CACHED_MAY_BE_STALE('tengu_harbor_permissions', false)
return getFeatureValue_CACHED_MAY_BE_STALE('tengu_harbor_permissions', true)
}
export type ChannelPermissionResponse = {
@@ -188,8 +188,8 @@ export function filterPermissionRelayClients<
(c): c is T & { type: 'connected' } =>
c.type === 'connected' &&
isInAllowlist(c.name) &&
c.capabilities?.experimental?.['claude/channel'] !== undefined &&
c.capabilities?.experimental?.['claude/channel/permission'] !== undefined,
Boolean(c.capabilities?.experimental?.['claude/channel']) &&
Boolean(c.capabilities?.experimental?.['claude/channel/permission']),
)
}

View File

@@ -538,7 +538,7 @@ export function useManageMCPConnections(
if (
client.capabilities?.experimental?.[
'claude/channel/permission'
] !== undefined
]
) {
client.client.setNotificationHandler(
ChannelPermissionNotificationSchema(),