dsh-agent-sdk-bridge 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEVELOPMENT.md +8 -0
- package/FAQ.md +6 -0
- package/lib/index.js +44 -6
- package/lib/types/codebuddy.d.ts +12 -1
- package/package.json +1 -1
- package/src/codebuddy.ts +55 -4
package/DEVELOPMENT.md
CHANGED
|
@@ -26,6 +26,14 @@
|
|
|
26
26
|
切区域时页面先渲染本地快照,再用 `fresh=1` 在后台校验。
|
|
27
27
|
浏览器由 CLI 的登录流程自己打开;`onAuthUrl` 只记录 URL 供卡片显示「手动打开登录页」,
|
|
28
28
|
宿主不再调用系统打开方式,否则每次登录会出现两个相同标签页。
|
|
29
|
+
- **CodeBuddy 的宿主工具**:宿主工具通过进程内 MCP server 挂入,`CodeBuddySession` 必须
|
|
30
|
+
在 CLI 启动前完成注册——`ensureTools()` 会保存 schema,`ensureStarted()` 创建 MCP server
|
|
31
|
+
后立即重放,然后才 `createSession`(MCP SDK 拒绝连接后再注册)。早返回会让首个 turn
|
|
32
|
+
根本没有宿主工具,模型退回调用 CLI 内置的 `Bash`,宿主只能报 unknown tool。
|
|
33
|
+
会话同时传 `tools: []` 禁用 CLI 内置工具(`--tools ""`),MCP 工具不受影响;`handle()`
|
|
34
|
+
仍会丢弃任何非宿主前缀的 tool_use,防止模型臆造工具名。CodeBuddy 会把 MCP server 名里的
|
|
35
|
+
非字母数字字符规范成 `_`(`dsh-host` → `mcp__dsh_host__<tool>`),Qoder CLI 保留连字符,
|
|
36
|
+
因此 `isHostTool()` / `hostToolName()` 同时接受两种拼写。
|
|
29
37
|
- **CodeBuddy 模型选择按区域**:`codebuddyGlobalDisabledModels` /
|
|
30
38
|
`codebuddyCnDisabledModels` 与 `codebuddyGlobalModelContextWindows` /
|
|
31
39
|
`codebuddyCnModelContextWindows` 是四个独立字段,`CodeBuddyAdapter` 按 region 读取,
|
package/FAQ.md
CHANGED
|
@@ -38,6 +38,12 @@ bearer token,只留在本机,请不要分享或提交到仓库。从旧版
|
|
|
38
38
|
> 在**国内版**页面对应的模型设置里点一次「全不选」,再勾选你要用的模型即可;之后两个区域
|
|
39
39
|
> 互不影响。
|
|
40
40
|
|
|
41
|
+
**CodeBuddy 的模型不调用工具,或者报「unknown tool Bash / ToolSearch」?** 已修复。原因是
|
|
42
|
+
宿主工具没有注册进会话(首个 turn 在 MCP server 创建前就被跳过了),模型只能看到 CLI 自带的
|
|
43
|
+
`Bash`/`ToolSearch`,而宿主没有这些工具。现在宿主工具会在 CLI 启动前注册,同时用
|
|
44
|
+
`tools: []` 禁用 CLI 内置工具,只保留宿主通过 MCP 挂入的工具;CodeBuddy 把 MCP server 名
|
|
45
|
+
规范成 `dsh_host` 的写法也已兼容。升级后重启 DSH Web 即可。
|
|
46
|
+
|
|
41
47
|
**点了「登录」却没有弹出浏览器,或者弹出了两个?** CodeBuddy 的 CLI 在登录流程里会自己打开
|
|
42
48
|
浏览器,插件只把登录链接显示在卡片上作为兜底,不再额外打开,所以一次登录只会有一个标签页。
|
|
43
49
|
每次点击还会先清除该区域在 CLI 中缓存的旧凭据,再启动全新的授权流程;SDK 的 `authenticate()`
|
package/lib/index.js
CHANGED
|
@@ -1338,7 +1338,7 @@ function meteredUsage(real, reasoning) {
|
|
|
1338
1338
|
* The host tool runtime dispatches by the bare host name; the inner model
|
|
1339
1339
|
* only ever sees the namespaced MCP form, so strip the prefix on the way out.
|
|
1340
1340
|
*/
|
|
1341
|
-
function hostToolName(name) {
|
|
1341
|
+
function hostToolName$1(name) {
|
|
1342
1342
|
return name.startsWith(MCP_TOOL_PREFIX$1) ? name.slice(MCP_TOOL_PREFIX$1.length) : name;
|
|
1343
1343
|
}
|
|
1344
1344
|
/** Deny native tools, allow this adapter's MCP tools. */
|
|
@@ -2168,7 +2168,7 @@ var QoderSession = class {
|
|
|
2168
2168
|
this.openTool = {
|
|
2169
2169
|
chunkIndex,
|
|
2170
2170
|
callId,
|
|
2171
|
-
name: hostToolName(block.name ?? ""),
|
|
2171
|
+
name: hostToolName$1(block.name ?? ""),
|
|
2172
2172
|
arguments: initialArguments
|
|
2173
2173
|
};
|
|
2174
2174
|
this.countOutput(this.openTool.name);
|
|
@@ -3286,6 +3286,26 @@ const LOGIN_TIMEOUT_MS = 3e5;
|
|
|
3286
3286
|
const LOGOUT_TIMEOUT_MS = 2e4;
|
|
3287
3287
|
const MCP_TOOL_PREFIX = `mcp__${MCP_SERVER_NAME}__`;
|
|
3288
3288
|
/**
|
|
3289
|
+
* The same namespace after the CLI normalises the server name.
|
|
3290
|
+
*
|
|
3291
|
+
* CodeBuddy rewrites every non-alphanumeric character in an MCP server name to
|
|
3292
|
+
* `_`, so a server registered as `dsh-host` is exposed to the model as
|
|
3293
|
+
* `mcp__dsh_host__<tool>`. Matching only the registered spelling rejected the
|
|
3294
|
+
* host's own tools as "built-ins", which is exactly why the model fell back to
|
|
3295
|
+
* `Bash`. The Qoder CLI keeps the hyphen, so both spellings are accepted.
|
|
3296
|
+
*/
|
|
3297
|
+
const MCP_TOOL_PREFIX_SANITIZED = `mcp__${MCP_SERVER_NAME.replace(/[^a-zA-Z0-9]/g, "_")}__`;
|
|
3298
|
+
/** Whether a CLI tool name belongs to this plugin's MCP server. */
|
|
3299
|
+
function isHostTool(toolName) {
|
|
3300
|
+
return toolName.startsWith(MCP_TOOL_PREFIX) || toolName.startsWith(MCP_TOOL_PREFIX_SANITIZED);
|
|
3301
|
+
}
|
|
3302
|
+
/** The host-side tool name, with the MCP namespace removed. */
|
|
3303
|
+
function hostToolName(toolName) {
|
|
3304
|
+
if (toolName.startsWith(MCP_TOOL_PREFIX)) return toolName.slice(MCP_TOOL_PREFIX.length);
|
|
3305
|
+
if (toolName.startsWith(MCP_TOOL_PREFIX_SANITIZED)) return toolName.slice(MCP_TOOL_PREFIX_SANITIZED.length);
|
|
3306
|
+
return toolName;
|
|
3307
|
+
}
|
|
3308
|
+
/**
|
|
3289
3309
|
* Whether the CLI answered as the anonymous fallback.
|
|
3290
3310
|
*
|
|
3291
3311
|
* With an invalid `CODEBUDDY_AUTH_TOKEN` the CLI still reports an account
|
|
@@ -3893,6 +3913,7 @@ var CodeBuddySession = class {
|
|
|
3893
3913
|
mcp = null;
|
|
3894
3914
|
pairing = new ToolResultPairing();
|
|
3895
3915
|
registered = /* @__PURE__ */ new Map();
|
|
3916
|
+
toolSchemas = [];
|
|
3896
3917
|
queue = null;
|
|
3897
3918
|
disposed = false;
|
|
3898
3919
|
abortPending = false;
|
|
@@ -3933,10 +3954,23 @@ var CodeBuddySession = class {
|
|
|
3933
3954
|
this.systemPrompt = options.system;
|
|
3934
3955
|
if (options.cwd !== void 0) this.cwd = options.cwd;
|
|
3935
3956
|
}
|
|
3936
|
-
/**
|
|
3957
|
+
/**
|
|
3958
|
+
* Remember the host tools and register them with this session's MCP server.
|
|
3959
|
+
*
|
|
3960
|
+
* Registration must happen before the CLI connects: the MCP SDK refuses a
|
|
3961
|
+
* tool added afterwards. The first turn calls this before the session — and
|
|
3962
|
+
* therefore its MCP server — exists, so the schemas are kept and replayed by
|
|
3963
|
+
* {@link ensureStarted}. Returning early there left the model with no host
|
|
3964
|
+
* tools at all, and it fell back to CodeBuddy's built-in `Bash`, which the
|
|
3965
|
+
* host could only answer with "unknown tool".
|
|
3966
|
+
*/
|
|
3937
3967
|
ensureTools(tools) {
|
|
3968
|
+
this.toolSchemas = tools;
|
|
3969
|
+
this.registerTools();
|
|
3970
|
+
}
|
|
3971
|
+
registerTools() {
|
|
3938
3972
|
if (this.mcp === null) return;
|
|
3939
|
-
for (const schema of
|
|
3973
|
+
for (const schema of this.toolSchemas) {
|
|
3940
3974
|
const hash = JSON.stringify(schema.parameters ?? {});
|
|
3941
3975
|
if (this.registered.get(schema.name) === hash) continue;
|
|
3942
3976
|
const shape = jsonSchemaToShape(schema.parameters ?? {});
|
|
@@ -4040,12 +4074,14 @@ var CodeBuddySession = class {
|
|
|
4040
4074
|
name: MCP_SERVER_NAME,
|
|
4041
4075
|
tools: []
|
|
4042
4076
|
});
|
|
4077
|
+
this.registerTools();
|
|
4043
4078
|
const session = sdk.unstable_v2_createSession({
|
|
4044
4079
|
environment: codeBuddyEnvironment(this.region),
|
|
4045
4080
|
sessionId: randomUUID(),
|
|
4046
4081
|
model: this.model,
|
|
4047
4082
|
mcpServers: { [MCP_SERVER_NAME]: this.mcp },
|
|
4048
4083
|
canUseTool: this.canUseTool,
|
|
4084
|
+
tools: [],
|
|
4049
4085
|
...cliEnv(this.region),
|
|
4050
4086
|
settingSources: [],
|
|
4051
4087
|
includePartialMessages: true,
|
|
@@ -4058,7 +4094,7 @@ var CodeBuddySession = class {
|
|
|
4058
4094
|
return session;
|
|
4059
4095
|
}
|
|
4060
4096
|
canUseTool = (toolName, _input, options) => {
|
|
4061
|
-
if (toolName
|
|
4097
|
+
if (isHostTool(toolName)) {
|
|
4062
4098
|
if (options.toolUseID !== void 0) this.pairing.announce(options.toolUseID);
|
|
4063
4099
|
return Promise.resolve({
|
|
4064
4100
|
behavior: "allow",
|
|
@@ -4105,6 +4141,7 @@ var CodeBuddySession = class {
|
|
|
4105
4141
|
if (event.type === "content_block_start") {
|
|
4106
4142
|
const block = event.content_block;
|
|
4107
4143
|
if (block?.type === "tool_use") {
|
|
4144
|
+
if (!isHostTool(block.name ?? "")) return;
|
|
4108
4145
|
const callId = `cb-${this.callNonce}-${++this.callCounter}`;
|
|
4109
4146
|
if (typeof block.id === "string" && block.id.length > 0) this.pairing.mapHostCall(callId, block.id);
|
|
4110
4147
|
const chunkIndex = this.blockIndex++;
|
|
@@ -4112,7 +4149,7 @@ var CodeBuddySession = class {
|
|
|
4112
4149
|
this.openTool = {
|
|
4113
4150
|
chunkIndex,
|
|
4114
4151
|
callId,
|
|
4115
|
-
name: (block.name ?? "")
|
|
4152
|
+
name: hostToolName(block.name ?? ""),
|
|
4116
4153
|
arguments: initial
|
|
4117
4154
|
};
|
|
4118
4155
|
this.countOutput(this.openTool.name);
|
|
@@ -4435,6 +4472,7 @@ async function codeBuddyColdStream(region, prompt, model, options) {
|
|
|
4435
4472
|
sessionId: randomUUID(),
|
|
4436
4473
|
...model === void 0 ? {} : { model },
|
|
4437
4474
|
...cliEnv(region),
|
|
4475
|
+
tools: [],
|
|
4438
4476
|
settingSources: [],
|
|
4439
4477
|
persistSession: false
|
|
4440
4478
|
});
|
package/lib/types/codebuddy.d.ts
CHANGED
|
@@ -194,6 +194,7 @@ export declare class CodeBuddySession {
|
|
|
194
194
|
private mcp;
|
|
195
195
|
private readonly pairing;
|
|
196
196
|
private readonly registered;
|
|
197
|
+
private toolSchemas;
|
|
197
198
|
private queue;
|
|
198
199
|
private disposed;
|
|
199
200
|
private abortPending;
|
|
@@ -230,12 +231,22 @@ export declare class CodeBuddySession {
|
|
|
230
231
|
system?: string;
|
|
231
232
|
cwd?: string;
|
|
232
233
|
}): void;
|
|
233
|
-
/**
|
|
234
|
+
/**
|
|
235
|
+
* Remember the host tools and register them with this session's MCP server.
|
|
236
|
+
*
|
|
237
|
+
* Registration must happen before the CLI connects: the MCP SDK refuses a
|
|
238
|
+
* tool added afterwards. The first turn calls this before the session — and
|
|
239
|
+
* therefore its MCP server — exists, so the schemas are kept and replayed by
|
|
240
|
+
* {@link ensureStarted}. Returning early there left the model with no host
|
|
241
|
+
* tools at all, and it fell back to CodeBuddy's built-in `Bash`, which the
|
|
242
|
+
* host could only answer with "unknown tool".
|
|
243
|
+
*/
|
|
234
244
|
ensureTools(tools: readonly {
|
|
235
245
|
name: string;
|
|
236
246
|
description: string;
|
|
237
247
|
parameters?: unknown;
|
|
238
248
|
}[]): void;
|
|
249
|
+
private registerTools;
|
|
239
250
|
/** Hand the host's tool results to parked or buffered handlers. */
|
|
240
251
|
deliverToolResults(tail: readonly {
|
|
241
252
|
role: string;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-agent-sdk-bridge",
|
|
3
3
|
"displayName": "Agent SDK Bridge",
|
|
4
4
|
"description": "DeepSeek Harness LLM adapter bridge for Qoder CN, Qoder International and Tencent CodeBuddy subscriptions. It reuses each provider's own SDK and CLI login, shows the Qoder account quota, exposes per-provider settings pages, and supports CodeBuddy's international and mainland login regions.",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/codebuddy.ts
CHANGED
|
@@ -48,6 +48,28 @@ const LOGOUT_TIMEOUT_MS = 20_000
|
|
|
48
48
|
type SessionImpl = ReturnType<typeof import('@tencent-ai/agent-sdk').unstable_v2_createSession>
|
|
49
49
|
|
|
50
50
|
const MCP_TOOL_PREFIX = `mcp__${MCP_SERVER_NAME}__`
|
|
51
|
+
/**
|
|
52
|
+
* The same namespace after the CLI normalises the server name.
|
|
53
|
+
*
|
|
54
|
+
* CodeBuddy rewrites every non-alphanumeric character in an MCP server name to
|
|
55
|
+
* `_`, so a server registered as `dsh-host` is exposed to the model as
|
|
56
|
+
* `mcp__dsh_host__<tool>`. Matching only the registered spelling rejected the
|
|
57
|
+
* host's own tools as "built-ins", which is exactly why the model fell back to
|
|
58
|
+
* `Bash`. The Qoder CLI keeps the hyphen, so both spellings are accepted.
|
|
59
|
+
*/
|
|
60
|
+
const MCP_TOOL_PREFIX_SANITIZED = `mcp__${MCP_SERVER_NAME.replace(/[^a-zA-Z0-9]/g, '_')}__`
|
|
61
|
+
|
|
62
|
+
/** Whether a CLI tool name belongs to this plugin's MCP server. */
|
|
63
|
+
function isHostTool(toolName: string): boolean {
|
|
64
|
+
return toolName.startsWith(MCP_TOOL_PREFIX) || toolName.startsWith(MCP_TOOL_PREFIX_SANITIZED)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The host-side tool name, with the MCP namespace removed. */
|
|
68
|
+
function hostToolName(toolName: string): string {
|
|
69
|
+
if (toolName.startsWith(MCP_TOOL_PREFIX)) return toolName.slice(MCP_TOOL_PREFIX.length)
|
|
70
|
+
if (toolName.startsWith(MCP_TOOL_PREFIX_SANITIZED)) return toolName.slice(MCP_TOOL_PREFIX_SANITIZED.length)
|
|
71
|
+
return toolName
|
|
72
|
+
}
|
|
51
73
|
|
|
52
74
|
/** One model as the settings page and the adapter see it. */
|
|
53
75
|
export interface CodeBuddyModel {
|
|
@@ -785,6 +807,7 @@ export class CodeBuddySession {
|
|
|
785
807
|
private mcp: ReturnType<typeof import('@tencent-ai/agent-sdk').createSdkMcpServer> | null = null
|
|
786
808
|
private readonly pairing = new ToolResultPairing()
|
|
787
809
|
private readonly registered = new Map<string, string>()
|
|
810
|
+
private toolSchemas: readonly { name: string, description: string, parameters?: unknown }[] = []
|
|
788
811
|
private queue: TurnQueue | null = null
|
|
789
812
|
private disposed = false
|
|
790
813
|
private abortPending = false
|
|
@@ -831,10 +854,24 @@ export class CodeBuddySession {
|
|
|
831
854
|
if (options.cwd !== undefined) this.cwd = options.cwd
|
|
832
855
|
}
|
|
833
856
|
|
|
834
|
-
/**
|
|
857
|
+
/**
|
|
858
|
+
* Remember the host tools and register them with this session's MCP server.
|
|
859
|
+
*
|
|
860
|
+
* Registration must happen before the CLI connects: the MCP SDK refuses a
|
|
861
|
+
* tool added afterwards. The first turn calls this before the session — and
|
|
862
|
+
* therefore its MCP server — exists, so the schemas are kept and replayed by
|
|
863
|
+
* {@link ensureStarted}. Returning early there left the model with no host
|
|
864
|
+
* tools at all, and it fell back to CodeBuddy's built-in `Bash`, which the
|
|
865
|
+
* host could only answer with "unknown tool".
|
|
866
|
+
*/
|
|
835
867
|
ensureTools(tools: readonly { name: string, description: string, parameters?: unknown }[]): void {
|
|
868
|
+
this.toolSchemas = tools
|
|
869
|
+
this.registerTools()
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
private registerTools(): void {
|
|
836
873
|
if (this.mcp === null) return
|
|
837
|
-
for (const schema of
|
|
874
|
+
for (const schema of this.toolSchemas) {
|
|
838
875
|
const hash = JSON.stringify(schema.parameters ?? {})
|
|
839
876
|
if (this.registered.get(schema.name) === hash) continue
|
|
840
877
|
const shape = jsonSchemaToShape((schema.parameters ?? {}) as Record<string, unknown>)
|
|
@@ -929,12 +966,17 @@ export class CodeBuddySession {
|
|
|
929
966
|
if (this.session !== null) return this.session
|
|
930
967
|
const sdk = await loadCodeBuddySdk()
|
|
931
968
|
this.mcp ??= sdk.createSdkMcpServer({ name: MCP_SERVER_NAME, tools: [] })
|
|
969
|
+
// Host tools must exist in the MCP server before the CLI lists them.
|
|
970
|
+
this.registerTools()
|
|
932
971
|
const session = sdk.unstable_v2_createSession({
|
|
933
972
|
environment: codeBuddyEnvironment(this.region),
|
|
934
973
|
sessionId: randomUUID(),
|
|
935
974
|
model: this.model,
|
|
936
975
|
mcpServers: { [MCP_SERVER_NAME]: this.mcp },
|
|
937
976
|
canUseTool: this.canUseTool as CanUseTool,
|
|
977
|
+
// Only the host's MCP tools may run: the CLI's own Bash/Read/Edit have no
|
|
978
|
+
// host-side counterpart, and the permission gate denies them anyway.
|
|
979
|
+
tools: [],
|
|
938
980
|
...cliEnv(this.region),
|
|
939
981
|
settingSources: [],
|
|
940
982
|
includePartialMessages: true,
|
|
@@ -948,7 +990,7 @@ export class CodeBuddySession {
|
|
|
948
990
|
}
|
|
949
991
|
|
|
950
992
|
private canUseTool = (toolName: string, _input: Record<string, unknown>, options: { toolUseID?: string }) => {
|
|
951
|
-
if (toolName
|
|
993
|
+
if (isHostTool(toolName)) {
|
|
952
994
|
if (options.toolUseID !== undefined) this.pairing.announce(options.toolUseID)
|
|
953
995
|
return Promise.resolve({ behavior: 'allow', ...options.toolUseID === undefined ? {} : { toolUseID: options.toolUseID } })
|
|
954
996
|
}
|
|
@@ -985,13 +1027,20 @@ export class CodeBuddySession {
|
|
|
985
1027
|
if (event.type === 'content_block_start') {
|
|
986
1028
|
const block = event.content_block as { type?: string, id?: string, name?: string, input?: unknown }
|
|
987
1029
|
if (block?.type === 'tool_use') {
|
|
1030
|
+
// A built-in CLI tool (Bash, Read, ToolSearch…) has no host
|
|
1031
|
+
// counterpart; forwarding it made the host answer "unknown tool" and
|
|
1032
|
+
// stop the turn. `tools: []` stops them being offered at all, and
|
|
1033
|
+
// this guard covers anything the CLI still emits. The permission
|
|
1034
|
+
// gate denies the call, so the inner model sees the denial and can
|
|
1035
|
+
// retry with a host tool.
|
|
1036
|
+
if (!isHostTool(block.name ?? '')) return
|
|
988
1037
|
const callId = `cb-${this.callNonce}-${++this.callCounter}`
|
|
989
1038
|
if (typeof block.id === 'string' && block.id.length > 0) this.pairing.mapHostCall(callId, block.id)
|
|
990
1039
|
const chunkIndex = this.blockIndex++
|
|
991
1040
|
const initial = block.input !== undefined && block.input !== null && Object.keys(block.input as object).length > 0
|
|
992
1041
|
? JSON.stringify(block.input)
|
|
993
1042
|
: ''
|
|
994
|
-
this.openTool = { chunkIndex, callId, name: (block.name ?? '')
|
|
1043
|
+
this.openTool = { chunkIndex, callId, name: hostToolName(block.name ?? ''), arguments: initial }
|
|
995
1044
|
this.countOutput(this.openTool.name)
|
|
996
1045
|
this.countOutput(initial)
|
|
997
1046
|
this.emit({ type: 'block-start', index: chunkIndex, blockType: 'tool-call' })
|
|
@@ -1229,6 +1278,8 @@ export async function codeBuddyColdStream(
|
|
|
1229
1278
|
sessionId: randomUUID(),
|
|
1230
1279
|
...model === undefined ? {} : { model },
|
|
1231
1280
|
...cliEnv(region),
|
|
1281
|
+
// Titles and compaction must never run a tool.
|
|
1282
|
+
tools: [],
|
|
1232
1283
|
settingSources: [],
|
|
1233
1284
|
persistSession: false,
|
|
1234
1285
|
})
|