dsh-agent-sdk-bridge 1.2.3 → 1.3.0
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 +38 -5
- package/FAQ.md +78 -2
- package/lib/index.js +443 -88
- package/lib/types/adapter.d.ts +10 -0
- package/lib/types/codebuddy.d.ts +33 -1
- package/lib/types/mcp-naming.d.ts +43 -0
- package/lib/types/minimax.d.ts +8 -1
- package/lib/types/models.d.ts +37 -3
- package/lib/types/render.d.ts +11 -0
- package/lib/types/sdk.d.ts +16 -0
- package/lib/types/session.d.ts +143 -3
- package/lib/types/usage.d.ts +7 -1
- package/package.json +1 -1
- package/src/adapter.ts +22 -3
- package/src/api.ts +6 -6
- package/src/codebuddy-adapter.ts +5 -3
- package/src/codebuddy.ts +63 -32
- package/src/mcp-naming.ts +68 -0
- package/src/minimax-adapter.ts +5 -3
- package/src/minimax.ts +2463 -2458
- package/src/models.ts +85 -8
- package/src/render.ts +51 -0
- package/src/sdk.ts +25 -0
- package/src/session.ts +232 -27
- package/src/usage.ts +24 -5
package/src/codebuddy.ts
CHANGED
|
@@ -26,8 +26,8 @@ import type { CodeBuddySavedUser } from './codebuddy-state.ts'
|
|
|
26
26
|
import { jsonSchemaToShape } from './jsonschema.ts'
|
|
27
27
|
import type { Live } from './live.ts'
|
|
28
28
|
import { readLive } from './live.ts'
|
|
29
|
+
import { stripMcpNamespace } from './mcp-naming.ts'
|
|
29
30
|
import { codeBuddyEnvironment, loadCodeBuddySdk } from './sdk.ts'
|
|
30
|
-
import type { ChannelContent, ToolResultPayload } from './session.ts'
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Literal characters in a channel payload; images contribute pixels, not text.
|
|
@@ -58,7 +58,8 @@ function toCodeBuddyUserMessage(
|
|
|
58
58
|
parent_tool_use_id: null,
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
import { MCP_SERVER_NAME, ToolResultPairing, TurnQueue } from './session.ts'
|
|
61
|
+
import { MCP_SERVER_NAME, HostToolRegistry, ToolResultPairing, TurnQueue } from './session.ts'
|
|
62
|
+
import type { ChannelContent, ToolResultPayload, ToolRegistryHost } from './session.ts'
|
|
62
63
|
import { TurnOutputAccounting, estimateTextTokens } from './tokens.ts'
|
|
63
64
|
|
|
64
65
|
/** Default context capacity assumed for a CodeBuddy model that declares none. */
|
|
@@ -88,17 +89,26 @@ const MCP_TOOL_PREFIX = `mcp__${MCP_SERVER_NAME}__`
|
|
|
88
89
|
* `Bash`. The Qoder CLI keeps the hyphen, so both spellings are accepted.
|
|
89
90
|
*/
|
|
90
91
|
const MCP_TOOL_PREFIX_SANITIZED = `mcp__${MCP_SERVER_NAME.replace(/[^a-zA-Z0-9]/g, '_')}__`
|
|
92
|
+
/** Both spellings, in the order the shared stripper tries them. */
|
|
93
|
+
const HOST_TOOL_PREFIXES = [MCP_TOOL_PREFIX, MCP_TOOL_PREFIX_SANITIZED]
|
|
91
94
|
|
|
92
95
|
/** Whether a CLI tool name belongs to this plugin's MCP server. */
|
|
93
|
-
function isHostTool(toolName: string): boolean {
|
|
94
|
-
return toolName
|
|
96
|
+
export function isHostTool(toolName: string): boolean {
|
|
97
|
+
return stripMcpNamespace(toolName, HOST_TOOL_PREFIXES) !== null
|
|
95
98
|
}
|
|
96
99
|
|
|
97
|
-
/**
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
/**
|
|
101
|
+
* The host-side tool name, with every MCP namespace layer removed.
|
|
102
|
+
*
|
|
103
|
+
* CodeBuddy qualifies an already-qualified name again, so the handler can be
|
|
104
|
+
* handed `mcp__dsh_host__mcp__dsh_host__edit`. Stripping one layer produced
|
|
105
|
+
* `mcp__dsh_host__edit`, a name the host does not have (its tool is `edit`), and
|
|
106
|
+
* DSH answered `unknown tool "mcp__dsh_host__edit"`; the model retried the
|
|
107
|
+
* doubled spelling next round and the CLI rejected it with `does not exist in
|
|
108
|
+
* the current tool set`. See {@link stripMcpNamespace}.
|
|
109
|
+
*/
|
|
110
|
+
export function hostToolName(toolName: string): string {
|
|
111
|
+
return stripMcpNamespace(toolName, HOST_TOOL_PREFIXES) ?? toolName
|
|
102
112
|
}
|
|
103
113
|
|
|
104
114
|
/** One model as the settings page and the adapter see it. */
|
|
@@ -945,8 +955,18 @@ export class CodeBuddySession {
|
|
|
945
955
|
private session: SessionImpl | null = null
|
|
946
956
|
private mcp: ReturnType<typeof import('@tencent-ai/agent-sdk').createSdkMcpServer> | null = null
|
|
947
957
|
private readonly pairing = new ToolResultPairing()
|
|
948
|
-
private readonly registered = new Map<string, string>()
|
|
949
958
|
private toolSchemas: readonly { name: string, description: string, parameters?: unknown }[] = []
|
|
959
|
+
/**
|
|
960
|
+
* Reconciles the live host tool list into {@link mcp}.
|
|
961
|
+
*
|
|
962
|
+
* Created with the MCP server, because that is what it installs into. Owned
|
|
963
|
+
* here rather than inline so a tool that the host adds or withdraws during a
|
|
964
|
+
* conversation is actually added or withdrawn: the MCP SDK refuses a second
|
|
965
|
+
* `registerTool` for a name it already holds, so the previous
|
|
966
|
+
* name→hash replay could never replace a changed schema, and `Tool "…" does
|
|
967
|
+
* not exist in the current tool set.` was what the model got for its trouble.
|
|
968
|
+
*/
|
|
969
|
+
private registry: HostToolRegistry | null = null
|
|
950
970
|
private queue: TurnQueue | null = null
|
|
951
971
|
private disposed = false
|
|
952
972
|
private abortPending = false
|
|
@@ -1069,28 +1089,18 @@ export class CodeBuddySession {
|
|
|
1069
1089
|
this.registerTools()
|
|
1070
1090
|
}
|
|
1071
1091
|
|
|
1092
|
+
/**
|
|
1093
|
+
* Reconcile this session's MCP tool set with the host's live list.
|
|
1094
|
+
*
|
|
1095
|
+
* Add, update in place, and remove: the host withdraws tools when a mode
|
|
1096
|
+
* changes and adds them when `ops_discover_tools` resolves a deferred one, and
|
|
1097
|
+
* a tool the model can still name but the server no longer holds is answered
|
|
1098
|
+
* with `Tool "…" does not exist in the current tool set.` — the CLI never
|
|
1099
|
+
* forwards it to the host, so no tool result ever comes back.
|
|
1100
|
+
*/
|
|
1072
1101
|
private registerTools(): void {
|
|
1073
|
-
if (this.
|
|
1074
|
-
|
|
1075
|
-
const hash = JSON.stringify(schema.parameters ?? {})
|
|
1076
|
-
if (this.registered.get(schema.name) === hash) continue
|
|
1077
|
-
const shape = jsonSchemaToShape((schema.parameters ?? {}) as Record<string, unknown>)
|
|
1078
|
-
try {
|
|
1079
|
-
this.mcp.instance.registerTool(schema.name, {
|
|
1080
|
-
description: schema.description.length > 0 ? schema.description : schema.name,
|
|
1081
|
-
inputSchema: shape,
|
|
1082
|
-
}, async () => {
|
|
1083
|
-
const result = await this.pairing.claim(this.toolResultTimeoutMs)
|
|
1084
|
-
return {
|
|
1085
|
-
content: result.content,
|
|
1086
|
-
...result.isError ? { isError: true } : {},
|
|
1087
|
-
}
|
|
1088
|
-
})
|
|
1089
|
-
} catch {
|
|
1090
|
-
continue
|
|
1091
|
-
}
|
|
1092
|
-
this.registered.set(schema.name, hash)
|
|
1093
|
-
}
|
|
1102
|
+
if (this.registry === null) return
|
|
1103
|
+
this.registry.reconcile(this.toolSchemas)
|
|
1094
1104
|
}
|
|
1095
1105
|
|
|
1096
1106
|
/** Hand the host's tool results to parked or buffered handlers. */
|
|
@@ -1173,7 +1183,28 @@ export class CodeBuddySession {
|
|
|
1173
1183
|
if (this.session !== null) return this.session
|
|
1174
1184
|
const sdk = await loadCodeBuddySdk()
|
|
1175
1185
|
this.mcp ??= sdk.createSdkMcpServer({ name: MCP_SERVER_NAME, tools: [] })
|
|
1176
|
-
// Host tools must exist in the MCP server before the CLI lists them.
|
|
1186
|
+
// Host tools must exist in the MCP server before the CLI lists them. The
|
|
1187
|
+
// registry is created here, with the server it installs into, and the
|
|
1188
|
+
// replay below is what puts the first turn's schemas in place.
|
|
1189
|
+
this.registry ??= new HostToolRegistry(
|
|
1190
|
+
this.mcp.instance as unknown as ToolRegistryHost,
|
|
1191
|
+
() => async () => {
|
|
1192
|
+
const result = await this.pairing.claim(this.toolResultTimeoutMs)
|
|
1193
|
+
return {
|
|
1194
|
+
content: result.content,
|
|
1195
|
+
...result.isError ? { isError: true } : {},
|
|
1196
|
+
}
|
|
1197
|
+
},
|
|
1198
|
+
// A tool the SDK refused is invisible to the model, which then calls it and
|
|
1199
|
+
// gets the CLI's `does not exist in the current tool set` instead of a host
|
|
1200
|
+
// result. Surface it instead of dropping it.
|
|
1201
|
+
message => console.warn(`[dsh-agent-sdk-bridge] ${message}`),
|
|
1202
|
+
)
|
|
1203
|
+
// Declare the tools capability before createSession connects this server. A
|
|
1204
|
+
// first turn that published no host tools would otherwise connect with no
|
|
1205
|
+
// tools capability at all, and the MCP SDK then rejects every later
|
|
1206
|
+
// registration — the tool silently never reaches `tools/list`.
|
|
1207
|
+
this.registry.prepare()
|
|
1177
1208
|
this.registerTools()
|
|
1178
1209
|
const session = sdk.unstable_v2_createSession({
|
|
1179
1210
|
environment: codeBuddyEnvironment(this.region),
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tool-name namespacing, shared by every CLI backend this bridge drives.
|
|
3
|
+
*
|
|
4
|
+
* Each backend reaches the host's tools over an in-process MCP server, so the
|
|
5
|
+
* name a model emits is namespaced (`mcp__dsh-host__ops_bash`) while the host
|
|
6
|
+
* dispatches on the bare name (`ops_bash`). Two things make that translation
|
|
7
|
+
* less trivial than it looks, and both are the same bug in different clothes:
|
|
8
|
+
*
|
|
9
|
+
* - **Spelling.** CodeBuddy rewrites a server name's non-alphanumeric characters
|
|
10
|
+
* to `_`, so `dsh-host` is exposed as `mcp__dsh_host__…`; the Qoder CLI keeps
|
|
11
|
+
* the hyphen. A handler must accept both.
|
|
12
|
+
* - **Depth.** A model occasionally emits an already-qualified name, and the CLI
|
|
13
|
+
* then qualifies it again. Peeling exactly one layer handed the host
|
|
14
|
+
* `mcp__dsh_host__edit` — a name it does not have — so DSH answered
|
|
15
|
+
* `unknown tool "mcp__dsh_host__edit"`; the model then retried the doubled
|
|
16
|
+
* spelling and the CLI refused it with its own
|
|
17
|
+
* `does not exist in the current tool set.` Two error messages, one cause:
|
|
18
|
+
* a prefix peeled once instead of exhaustively.
|
|
19
|
+
*
|
|
20
|
+
* This module exists so all three backends peel the same way, rather than each
|
|
21
|
+
* carrying its own one-layer `slice`.
|
|
22
|
+
*
|
|
23
|
+
* @module dsh-agent-sdk-bridge/mcp-naming
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** How many prefix layers a single name may carry before the walk gives up. */
|
|
27
|
+
const MAX_NAMESPACE_LAYERS = 8
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Peel every matching namespace layer off a tool name.
|
|
31
|
+
*
|
|
32
|
+
* @param name - the tool name as the CLI's `tool_use` block carried it.
|
|
33
|
+
* @param prefixes - the namespaced spellings that belong to one server, in
|
|
34
|
+
* either spelling the backends produce. Longest first is not required; the
|
|
35
|
+
* walk re-checks the whole list after each peel.
|
|
36
|
+
* @returns the bare host tool name, or null when no prefix matched at all (or
|
|
37
|
+
* the name was nothing but namespaces).
|
|
38
|
+
*/
|
|
39
|
+
export function stripMcpNamespace(name: string, prefixes: readonly string[]): string | null {
|
|
40
|
+
const usable = prefixes.filter(prefix => prefix.length > 0)
|
|
41
|
+
let current = name
|
|
42
|
+
let stripped = false
|
|
43
|
+
// Bounded, so a name made only of prefixes cannot spin here.
|
|
44
|
+
for (let layer = 0; layer < MAX_NAMESPACE_LAYERS; layer++) {
|
|
45
|
+
const found = usable.find(prefix => current.startsWith(prefix))
|
|
46
|
+
if (found === undefined) return stripped && current.length > 0 ? current : null
|
|
47
|
+
current = current.slice(found.length)
|
|
48
|
+
stripped = true
|
|
49
|
+
}
|
|
50
|
+
// Still a namespace after the cap: there is no tool name in here, and
|
|
51
|
+
// returning the remainder would hand the host a prefix as if it were a tool.
|
|
52
|
+
return usable.some(prefix => current.startsWith(prefix)) || current.length === 0
|
|
53
|
+
? null
|
|
54
|
+
: stripped ? current : null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The two spellings one MCP server name is exposed under by the backends.
|
|
59
|
+
*
|
|
60
|
+
* @param serverName - the name the server was registered with, e.g. `dsh-host`.
|
|
61
|
+
* @returns the registered spelling followed by the CLI-sanitised one; the two
|
|
62
|
+
* collapse to a single entry when the name is already alphanumeric.
|
|
63
|
+
*/
|
|
64
|
+
export function mcpToolPrefixes(serverName: string): string[] {
|
|
65
|
+
const registered = `mcp__${serverName}__`
|
|
66
|
+
const sanitized = `mcp__${serverName.replace(/[^a-zA-Z0-9]/g, '_')}__`
|
|
67
|
+
return sanitized === registered ? [registered] : [registered, sanitized]
|
|
68
|
+
}
|
package/src/minimax-adapter.ts
CHANGED
|
@@ -30,7 +30,7 @@ import type { ImageRequestReader } from './adapter.ts'
|
|
|
30
30
|
import type { Live } from './live.ts'
|
|
31
31
|
import { readLive, readLiveOptional } from './live.ts'
|
|
32
32
|
import type { Feed, ImagePathResolver } from './render.ts'
|
|
33
|
-
import { feedImageRefs, feedToText, imageRefText, renderInitialFeed, resolvedFeedParts } from './render.ts'
|
|
33
|
+
import { feedImageRefs, feedToText, imageRefText, renderInitialFeed, renderSideChannelFeed, resolvedFeedParts } from './render.ts'
|
|
34
34
|
|
|
35
35
|
/** The MiniMax Code provider route. */
|
|
36
36
|
export const MINIMAX_PROVIDER = 'minimax-acp'
|
|
@@ -310,8 +310,10 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
310
310
|
}
|
|
311
311
|
const model = resolveMinimaxModelId(options.model, this.aliases)
|
|
312
312
|
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
313
|
-
|
|
314
|
-
|
|
313
|
+
// A side-channel request (compaction, a session title) carries its
|
|
314
|
+
// instruction as the last user message, so it is rendered as the task to
|
|
315
|
+
// carry out rather than as a turn to continue. See renderSideChannelFeed.
|
|
316
|
+
const prompt = feedToText(renderSideChannelFeed(options.system, options.messages, this.options.imagePath), this.options.imagePath)
|
|
315
317
|
for (const chunk of await minimaxColdStream(region, prompt, model, options)) yield chunk
|
|
316
318
|
return
|
|
317
319
|
}
|