dsh-agent-sdk-bridge 1.2.3 → 1.3.2
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 +44 -5
- package/FAQ.md +99 -2
- package/lib/index.js +572 -128
- package/lib/types/adapter.d.ts +10 -0
- package/lib/types/codebuddy-adapter.d.ts +9 -0
- package/lib/types/codebuddy.d.ts +37 -3
- package/lib/types/mcp-naming.d.ts +43 -0
- package/lib/types/minimax-adapter.d.ts +9 -0
- package/lib/types/minimax.d.ts +30 -4
- 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 +37 -6
- package/src/codebuddy.ts +108 -47
- package/src/mcp-naming.ts +68 -0
- package/src/minimax-adapter.ts +52 -21
- package/src/minimax.ts +2496 -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-adapter.ts
CHANGED
|
@@ -23,7 +23,8 @@ import type { ImageRequestReader } from './adapter.ts'
|
|
|
23
23
|
import type { Live } from './live.ts'
|
|
24
24
|
import { readLive, readLiveOptional } from './live.ts'
|
|
25
25
|
import type { Feed, ImagePathResolver } from './render.ts'
|
|
26
|
-
import {
|
|
26
|
+
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
|
|
27
|
+
import { feedImageRefs, feedToText, imageRefText, imageRefs, renderInitialFeed, renderSideChannelFeed, resolvedFeedParts } from './render.ts'
|
|
27
28
|
import type { ChannelContent } from './session.ts'
|
|
28
29
|
|
|
29
30
|
/** The CodeBuddy provider route. */
|
|
@@ -223,11 +224,35 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
223
224
|
feed: Feed,
|
|
224
225
|
vision: boolean,
|
|
225
226
|
signal?: AbortSignal,
|
|
227
|
+
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
228
|
+
return await this.resolveImages(feedImageRefs(feed), vision, signal)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Images carried by the messages the inner session has not seen yet.
|
|
233
|
+
*
|
|
234
|
+
* Tool results are included: `read_image` hands its pixels back through the
|
|
235
|
+
* MCP tool result, not through the prompt text, so this tail is the only
|
|
236
|
+
* place the bytes can come from.
|
|
237
|
+
*/
|
|
238
|
+
private async resolveTurnImages(
|
|
239
|
+
tail: readonly RequestMessage[],
|
|
240
|
+
vision: boolean,
|
|
241
|
+
signal?: AbortSignal,
|
|
242
|
+
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
243
|
+
const refs = tail.flatMap(message => message.role === 'assistant' ? [] : imageRefs(message.content))
|
|
244
|
+
return await this.resolveImages(refs, vision, signal)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private async resolveImages(
|
|
248
|
+
refs: readonly ImageAttachmentRef[],
|
|
249
|
+
vision: boolean,
|
|
250
|
+
signal?: AbortSignal,
|
|
226
251
|
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
227
252
|
const map = new Map<string, { data: string, mediaType: string }>()
|
|
228
253
|
const readImage = this.options.readImage
|
|
229
254
|
if (!vision || readImage === undefined) return map
|
|
230
|
-
for (const ref of
|
|
255
|
+
for (const ref of refs) {
|
|
231
256
|
const key = String(ref.attachmentId)
|
|
232
257
|
if (map.has(key)) continue
|
|
233
258
|
try {
|
|
@@ -375,8 +400,10 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
375
400
|
}
|
|
376
401
|
const model = resolveCodeBuddyModelId(options.model, this.aliases)
|
|
377
402
|
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
378
|
-
|
|
379
|
-
|
|
403
|
+
// A side-channel request (compaction, a session title) carries its
|
|
404
|
+
// instruction as the last user message, so it is rendered as the task to
|
|
405
|
+
// carry out rather than as a turn to continue. See renderSideChannelFeed.
|
|
406
|
+
const prompt = feedToText(renderSideChannelFeed(options.system, options.messages, this.options.imagePath), this.options.imagePath)
|
|
380
407
|
for (const chunk of await codeBuddyColdStream(region, prompt, model, options)) yield chunk
|
|
381
408
|
return
|
|
382
409
|
}
|
|
@@ -388,7 +415,12 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
388
415
|
return
|
|
389
416
|
}
|
|
390
417
|
const tail = options.messages.slice(session.fedMessages.length)
|
|
391
|
-
|
|
418
|
+
// Resolve images before the tool results are delivered: a tool result can
|
|
419
|
+
// carry pixels (`read_image`), and the MCP result is the only channel that
|
|
420
|
+
// reaches the inner model for this turn.
|
|
421
|
+
const vision = await this.modelVision(options.model)
|
|
422
|
+
const images = await this.resolveTurnImages(tail, vision, options.signal)
|
|
423
|
+
session.deliverToolResults(tail, images)
|
|
392
424
|
const plan = planContinuation(session.fedMessages, options.messages)
|
|
393
425
|
if (plan.rebuild) {
|
|
394
426
|
this.sessions.dispose(sessionId)
|
|
@@ -402,7 +434,6 @@ export class CodeBuddyAdapter extends LlmAdapter {
|
|
|
402
434
|
session.ensureTools(options.tools ?? [])
|
|
403
435
|
session.fedMessages = options.messages
|
|
404
436
|
session.fedSystem = options.system
|
|
405
|
-
const vision = await this.modelVision(options.model)
|
|
406
437
|
const feed = plan.feed === null
|
|
407
438
|
? null
|
|
408
439
|
: await this.buildFeed(plan.feed, vision, options.signal)
|
package/src/codebuddy.ts
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import { randomBytes, randomUUID } from 'node:crypto'
|
|
16
|
-
import {
|
|
16
|
+
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
|
|
17
|
+
import { EMPTY_RESPONSE_CODE, ToolCallId, textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
17
18
|
import type {
|
|
18
19
|
FinishReason, GenerateOptions, StreamChunk, TokenUsage,
|
|
19
20
|
} from '@deepseek-ai/dsh-llm'
|
|
@@ -26,8 +27,8 @@ import type { CodeBuddySavedUser } from './codebuddy-state.ts'
|
|
|
26
27
|
import { jsonSchemaToShape } from './jsonschema.ts'
|
|
27
28
|
import type { Live } from './live.ts'
|
|
28
29
|
import { readLive } from './live.ts'
|
|
30
|
+
import { stripMcpNamespace } from './mcp-naming.ts'
|
|
29
31
|
import { codeBuddyEnvironment, loadCodeBuddySdk } from './sdk.ts'
|
|
30
|
-
import type { ChannelContent, ToolResultPayload } from './session.ts'
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Literal characters in a channel payload; images contribute pixels, not text.
|
|
@@ -58,7 +59,8 @@ function toCodeBuddyUserMessage(
|
|
|
58
59
|
parent_tool_use_id: null,
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
|
-
import { MCP_SERVER_NAME, ToolResultPairing, TurnQueue } from './session.ts'
|
|
62
|
+
import { MCP_SERVER_NAME, HostToolRegistry, ToolResultPairing, TurnQueue } from './session.ts'
|
|
63
|
+
import type { ChannelContent, ResolvedImages, ToolResultPayload, ToolRegistryHost } from './session.ts'
|
|
62
64
|
import { TurnOutputAccounting, estimateTextTokens } from './tokens.ts'
|
|
63
65
|
|
|
64
66
|
/** Default context capacity assumed for a CodeBuddy model that declares none. */
|
|
@@ -77,28 +79,54 @@ const LOGOUT_TIMEOUT_MS = 20_000
|
|
|
77
79
|
/** The session object the SDK's createSession returns (not a named export). */
|
|
78
80
|
type SessionImpl = ReturnType<typeof import('@tencent-ai/agent-sdk').unstable_v2_createSession>
|
|
79
81
|
|
|
80
|
-
const MCP_TOOL_PREFIX = `mcp__${MCP_SERVER_NAME}__`
|
|
81
82
|
/**
|
|
82
|
-
* The
|
|
83
|
+
* The key this plugin's MCP server is registered under, in the CodeBuddy CLI.
|
|
83
84
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* `
|
|
87
|
-
*
|
|
88
|
-
*
|
|
85
|
+
* It is the CLI's own normalised spelling - `dsh_host`, not `dsh-host` - and
|
|
86
|
+
* that choice is the fix for the recurring "tool went missing": the CLI builds
|
|
87
|
+
* its call-time tool map from `mcp__<key>__<tool>` with the key verbatim,
|
|
88
|
+
* while several of its own code paths (`parseMcpToolName`, the plugin tool
|
|
89
|
+
* namer, the wire-format normaliser) rewrite every non-alphanumeric character
|
|
90
|
+
* to `_`. With a hyphenated key the list showed `mcp__dsh-host__read_files`
|
|
91
|
+
* while a model that emitted the normalised `mcp__dsh_host__read_files` - as
|
|
92
|
+
* one did, verbatim, in a failed session - found no entry, and the CLI refused
|
|
93
|
+
* the call with `Tool "..." does not exist in the current tool set.` before it
|
|
94
|
+
* ever reached this server. Registering under the normalised spelling makes
|
|
95
|
+
* list, lookup and normalisation one and the same name, so there is no second
|
|
96
|
+
* spelling to guess.
|
|
89
97
|
*/
|
|
90
|
-
const
|
|
98
|
+
const CODEBUDDY_MCP_SERVER_KEY = 'dsh_host'
|
|
99
|
+
|
|
100
|
+
const MCP_TOOL_PREFIX = `mcp__${CODEBUDDY_MCP_SERVER_KEY}__`
|
|
101
|
+
/**
|
|
102
|
+
* The hyphenated spelling, kept for names already in flight.
|
|
103
|
+
*
|
|
104
|
+
* The server used to be keyed `dsh-host`; older prefixes and doubled forms can
|
|
105
|
+
* still carry `mcp__dsh-host__<tool>`, and both spellings name the same
|
|
106
|
+
* server, so the matcher accepts them all. See {@link CODEBUDDY_MCP_SERVER_KEY}
|
|
107
|
+
* for why the registered key itself is now the normalised spelling.
|
|
108
|
+
*/
|
|
109
|
+
const MCP_TOOL_PREFIX_SANITIZED = `mcp__${MCP_SERVER_NAME}__`
|
|
110
|
+
/** Both spellings, in the order the shared stripper tries them. */
|
|
111
|
+
const HOST_TOOL_PREFIXES = [MCP_TOOL_PREFIX, MCP_TOOL_PREFIX_SANITIZED]
|
|
91
112
|
|
|
92
113
|
/** Whether a CLI tool name belongs to this plugin's MCP server. */
|
|
93
|
-
function isHostTool(toolName: string): boolean {
|
|
94
|
-
return toolName
|
|
114
|
+
export function isHostTool(toolName: string): boolean {
|
|
115
|
+
return stripMcpNamespace(toolName, HOST_TOOL_PREFIXES) !== null
|
|
95
116
|
}
|
|
96
117
|
|
|
97
|
-
/**
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
118
|
+
/**
|
|
119
|
+
* The host-side tool name, with every MCP namespace layer removed.
|
|
120
|
+
*
|
|
121
|
+
* CodeBuddy qualifies an already-qualified name again, so the handler can be
|
|
122
|
+
* handed `mcp__dsh_host__mcp__dsh_host__edit`. Stripping one layer produced
|
|
123
|
+
* `mcp__dsh_host__edit`, a name the host does not have (its tool is `edit`), and
|
|
124
|
+
* DSH answered `unknown tool "mcp__dsh_host__edit"`; the model retried the
|
|
125
|
+
* doubled spelling next round and the CLI rejected it with `does not exist in
|
|
126
|
+
* the current tool set`. See {@link stripMcpNamespace}.
|
|
127
|
+
*/
|
|
128
|
+
export function hostToolName(toolName: string): string {
|
|
129
|
+
return stripMcpNamespace(toolName, HOST_TOOL_PREFIXES) ?? toolName
|
|
102
130
|
}
|
|
103
131
|
|
|
104
132
|
/** One model as the settings page and the adapter see it. */
|
|
@@ -945,8 +973,18 @@ export class CodeBuddySession {
|
|
|
945
973
|
private session: SessionImpl | null = null
|
|
946
974
|
private mcp: ReturnType<typeof import('@tencent-ai/agent-sdk').createSdkMcpServer> | null = null
|
|
947
975
|
private readonly pairing = new ToolResultPairing()
|
|
948
|
-
private readonly registered = new Map<string, string>()
|
|
949
976
|
private toolSchemas: readonly { name: string, description: string, parameters?: unknown }[] = []
|
|
977
|
+
/**
|
|
978
|
+
* Reconciles the live host tool list into {@link mcp}.
|
|
979
|
+
*
|
|
980
|
+
* Created with the MCP server, because that is what it installs into. Owned
|
|
981
|
+
* here rather than inline so a tool that the host adds or withdraws during a
|
|
982
|
+
* conversation is actually added or withdrawn: the MCP SDK refuses a second
|
|
983
|
+
* `registerTool` for a name it already holds, so the previous
|
|
984
|
+
* name→hash replay could never replace a changed schema, and `Tool "…" does
|
|
985
|
+
* not exist in the current tool set.` was what the model got for its trouble.
|
|
986
|
+
*/
|
|
987
|
+
private registry: HostToolRegistry | null = null
|
|
950
988
|
private queue: TurnQueue | null = null
|
|
951
989
|
private disposed = false
|
|
952
990
|
private abortPending = false
|
|
@@ -1069,32 +1107,25 @@ export class CodeBuddySession {
|
|
|
1069
1107
|
this.registerTools()
|
|
1070
1108
|
}
|
|
1071
1109
|
|
|
1110
|
+
/**
|
|
1111
|
+
* Reconcile this session's MCP tool set with the host's live list.
|
|
1112
|
+
*
|
|
1113
|
+
* Add, update in place, and remove: the host withdraws tools when a mode
|
|
1114
|
+
* changes and adds them when `ops_discover_tools` resolves a deferred one, and
|
|
1115
|
+
* a tool the model can still name but the server no longer holds is answered
|
|
1116
|
+
* with `Tool "…" does not exist in the current tool set.` — the CLI never
|
|
1117
|
+
* forwards it to the host, so no tool result ever comes back.
|
|
1118
|
+
*/
|
|
1072
1119
|
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
|
-
}
|
|
1120
|
+
if (this.registry === null) return
|
|
1121
|
+
this.registry.reconcile(this.toolSchemas)
|
|
1094
1122
|
}
|
|
1095
1123
|
|
|
1096
1124
|
/** Hand the host's tool results to parked or buffered handlers. */
|
|
1097
|
-
deliverToolResults(
|
|
1125
|
+
deliverToolResults(
|
|
1126
|
+
tail: readonly { role: string, toolCallId?: unknown, content?: unknown, isError?: boolean }[],
|
|
1127
|
+
images?: ResolvedImages,
|
|
1128
|
+
): void {
|
|
1098
1129
|
let freshUserTurn = false
|
|
1099
1130
|
const delivered = new Set<string>()
|
|
1100
1131
|
for (const message of tail) {
|
|
@@ -1104,7 +1135,7 @@ export class CodeBuddySession {
|
|
|
1104
1135
|
}
|
|
1105
1136
|
if (message.role !== 'tool') continue
|
|
1106
1137
|
delivered.add(this.pairing.deliver(String(message.toolCallId), {
|
|
1107
|
-
content: renderCodeBuddyResult(message.content),
|
|
1138
|
+
content: renderCodeBuddyResult(message.content, images),
|
|
1108
1139
|
isError: message.isError === true,
|
|
1109
1140
|
}))
|
|
1110
1141
|
}
|
|
@@ -1172,14 +1203,35 @@ export class CodeBuddySession {
|
|
|
1172
1203
|
private async ensureStarted(): Promise<SessionImpl> {
|
|
1173
1204
|
if (this.session !== null) return this.session
|
|
1174
1205
|
const sdk = await loadCodeBuddySdk()
|
|
1175
|
-
this.mcp ??= sdk.createSdkMcpServer({ name:
|
|
1176
|
-
// Host tools must exist in the MCP server before the CLI lists them.
|
|
1206
|
+
this.mcp ??= sdk.createSdkMcpServer({ name: CODEBUDDY_MCP_SERVER_KEY, tools: [] })
|
|
1207
|
+
// Host tools must exist in the MCP server before the CLI lists them. The
|
|
1208
|
+
// registry is created here, with the server it installs into, and the
|
|
1209
|
+
// replay below is what puts the first turn's schemas in place.
|
|
1210
|
+
this.registry ??= new HostToolRegistry(
|
|
1211
|
+
this.mcp.instance as unknown as ToolRegistryHost,
|
|
1212
|
+
() => async () => {
|
|
1213
|
+
const result = await this.pairing.claim(this.toolResultTimeoutMs)
|
|
1214
|
+
return {
|
|
1215
|
+
content: result.content,
|
|
1216
|
+
...result.isError ? { isError: true } : {},
|
|
1217
|
+
}
|
|
1218
|
+
},
|
|
1219
|
+
// A tool the SDK refused is invisible to the model, which then calls it and
|
|
1220
|
+
// gets the CLI's `does not exist in the current tool set` instead of a host
|
|
1221
|
+
// result. Surface it instead of dropping it.
|
|
1222
|
+
message => console.warn(`[dsh-agent-sdk-bridge] ${message}`),
|
|
1223
|
+
)
|
|
1224
|
+
// Declare the tools capability before createSession connects this server. A
|
|
1225
|
+
// first turn that published no host tools would otherwise connect with no
|
|
1226
|
+
// tools capability at all, and the MCP SDK then rejects every later
|
|
1227
|
+
// registration — the tool silently never reaches `tools/list`.
|
|
1228
|
+
this.registry.prepare()
|
|
1177
1229
|
this.registerTools()
|
|
1178
1230
|
const session = sdk.unstable_v2_createSession({
|
|
1179
1231
|
environment: codeBuddyEnvironment(this.region),
|
|
1180
1232
|
sessionId: randomUUID(),
|
|
1181
1233
|
model: this.model,
|
|
1182
|
-
mcpServers: { [
|
|
1234
|
+
mcpServers: { [CODEBUDDY_MCP_SERVER_KEY]: this.mcp },
|
|
1183
1235
|
canUseTool: this.canUseTool as CanUseTool,
|
|
1184
1236
|
// Only the host's MCP tools may run: the CLI's own Bash/Read/Edit have no
|
|
1185
1237
|
// host-side counterpart, and the permission gate denies them anyway.
|
|
@@ -1462,12 +1514,21 @@ export class CodeBuddySession {
|
|
|
1462
1514
|
}
|
|
1463
1515
|
|
|
1464
1516
|
/** Render host tool-result blocks into the MCP content the inner model reads. */
|
|
1465
|
-
function renderCodeBuddyResult(blocks: unknown): ToolResultPayload['content'] {
|
|
1517
|
+
export function renderCodeBuddyResult(blocks: unknown, images?: ResolvedImages): ToolResultPayload['content'] {
|
|
1466
1518
|
if (!Array.isArray(blocks)) return [{ type: 'text', text: String(blocks ?? '') }]
|
|
1467
1519
|
const content: ToolResultPayload['content'] = []
|
|
1468
1520
|
for (const block of blocks as { type?: string, text?: string, attachment?: unknown }[]) {
|
|
1469
1521
|
if (block.type === 'text') content.push({ type: 'text', text: block.text ?? '' })
|
|
1470
|
-
else if (block.type === 'image'
|
|
1522
|
+
else if (block.type === 'image' && block.attachment !== null && typeof block.attachment === 'object') {
|
|
1523
|
+
// A tool can return pixels (`read_image` does). The MCP result shape
|
|
1524
|
+
// carries them as an image block; dropping them here was why the inner
|
|
1525
|
+
// model could read a path in the result text but never see the picture.
|
|
1526
|
+
const attachment = block.attachment as ImageAttachmentRef
|
|
1527
|
+
const resolved = images?.get(String(attachment.attachmentId))
|
|
1528
|
+
content.push(resolved === undefined
|
|
1529
|
+
? { type: 'text', text: textOnlyImageText(attachment) }
|
|
1530
|
+
: { type: 'image', data: resolved.data, mimeType: resolved.mediaType })
|
|
1531
|
+
}
|
|
1471
1532
|
else content.push({ type: 'text', text: JSON.stringify(block) })
|
|
1472
1533
|
}
|
|
1473
1534
|
return content
|
|
@@ -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,8 @@ 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, imageRefs, renderInitialFeed, renderSideChannelFeed, resolvedFeedParts } from './render.ts'
|
|
34
|
+
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
|
|
34
35
|
|
|
35
36
|
/** The MiniMax Code provider route. */
|
|
36
37
|
export const MINIMAX_PROVIDER = 'minimax-acp'
|
|
@@ -310,8 +311,10 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
310
311
|
}
|
|
311
312
|
const model = resolveMinimaxModelId(options.model, this.aliases)
|
|
312
313
|
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
313
|
-
|
|
314
|
-
|
|
314
|
+
// A side-channel request (compaction, a session title) carries its
|
|
315
|
+
// instruction as the last user message, so it is rendered as the task to
|
|
316
|
+
// carry out rather than as a turn to continue. See renderSideChannelFeed.
|
|
317
|
+
const prompt = feedToText(renderSideChannelFeed(options.system, options.messages, this.options.imagePath), this.options.imagePath)
|
|
315
318
|
for (const chunk of await minimaxColdStream(region, prompt, model, options)) yield chunk
|
|
316
319
|
return
|
|
317
320
|
}
|
|
@@ -323,7 +326,12 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
323
326
|
return
|
|
324
327
|
}
|
|
325
328
|
const tail = options.messages.slice(session.fedMessages.length)
|
|
326
|
-
|
|
329
|
+
// Resolve images before the tool results are delivered: a tool result can
|
|
330
|
+
// carry pixels (`read_image`), and this ACP tool result is the only channel
|
|
331
|
+
// that reaches the inner model for the round.
|
|
332
|
+
const vision = await this.modelVision(model, region)
|
|
333
|
+
const images = await this.resolveTurnImages(tail, vision, options.signal)
|
|
334
|
+
session.deliverToolResults(tail, images)
|
|
327
335
|
const plan = planContinuation(session.fedMessages, options.messages)
|
|
328
336
|
if (plan.rebuild) {
|
|
329
337
|
this.sessions.dispose(sessionId)
|
|
@@ -356,7 +364,6 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
356
364
|
session.ensureTools(options.tools ?? [])
|
|
357
365
|
session.fedMessages = options.messages
|
|
358
366
|
session.fedSystem = options.system
|
|
359
|
-
const vision = await this.modelVision(model, region)
|
|
360
367
|
const feed = plan.feed === null
|
|
361
368
|
? null
|
|
362
369
|
: await this.buildFeed(plan.feed, vision, options.signal)
|
|
@@ -416,22 +423,7 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
416
423
|
): Promise<string | MinimaxPromptBlock[]> {
|
|
417
424
|
if (typeof feed === 'string') return feed
|
|
418
425
|
if (!vision) return feedToText(feed, this.options.imagePath)
|
|
419
|
-
const images =
|
|
420
|
-
const readImage = this.options.readImage
|
|
421
|
-
if (readImage !== undefined) {
|
|
422
|
-
for (const ref of feedImageRefs(feed)) {
|
|
423
|
-
const key = String(ref.attachmentId)
|
|
424
|
-
if (images.has(key)) continue
|
|
425
|
-
try {
|
|
426
|
-
const image = await readImage(ref, signal)
|
|
427
|
-
if (image !== undefined) {
|
|
428
|
-
images.set(key, { data: Buffer.from(image.bytes).toString('base64'), mediaType: image.mediaType })
|
|
429
|
-
}
|
|
430
|
-
} catch {
|
|
431
|
-
// Degrade this attachment to its handle text.
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
}
|
|
426
|
+
const images = await this.resolveImages(feedImageRefs(feed), vision, signal)
|
|
435
427
|
const parts = resolvedFeedParts(feed, images)
|
|
436
428
|
if (!parts.some(part => part.type !== 'text')) {
|
|
437
429
|
return parts.map(part => part.type === 'text' ? part.text : '').join('\n\n')
|
|
@@ -458,6 +450,45 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
458
450
|
return content
|
|
459
451
|
}
|
|
460
452
|
|
|
453
|
+
/**
|
|
454
|
+
* Images carried by the messages the inner session has not seen yet.
|
|
455
|
+
*
|
|
456
|
+
* Tool results are included: `read_image` hands its pixels back through the
|
|
457
|
+
* ACP tool result, not through the prompt text, so this tail is the only
|
|
458
|
+
* place the bytes can come from.
|
|
459
|
+
*/
|
|
460
|
+
private async resolveTurnImages(
|
|
461
|
+
tail: readonly RequestMessage[],
|
|
462
|
+
vision: boolean,
|
|
463
|
+
signal?: AbortSignal,
|
|
464
|
+
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
465
|
+
const refs = tail.flatMap(message => message.role === 'assistant' ? [] : imageRefs(message.content))
|
|
466
|
+
return await this.resolveImages(refs, vision, signal)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
private async resolveImages(
|
|
470
|
+
refs: readonly ImageAttachmentRef[],
|
|
471
|
+
vision: boolean,
|
|
472
|
+
signal?: AbortSignal,
|
|
473
|
+
): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
|
|
474
|
+
const map = new Map<string, { data: string, mediaType: string }>()
|
|
475
|
+
const readImage = this.options.readImage
|
|
476
|
+
if (!vision || readImage === undefined) return map
|
|
477
|
+
for (const ref of refs) {
|
|
478
|
+
const key = String(ref.attachmentId)
|
|
479
|
+
if (map.has(key)) continue
|
|
480
|
+
try {
|
|
481
|
+
const image = await readImage(ref, signal)
|
|
482
|
+
if (image !== undefined) {
|
|
483
|
+
map.set(key, { data: Buffer.from(image.bytes).toString('base64'), mediaType: image.mediaType })
|
|
484
|
+
}
|
|
485
|
+
} catch {
|
|
486
|
+
// Degrade this attachment to its handle text.
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return map
|
|
490
|
+
}
|
|
491
|
+
|
|
461
492
|
private cwdOf(options: GenerateOptions): string | undefined {
|
|
462
493
|
return (options as GenerateOptions & { cwd?: string }).cwd
|
|
463
494
|
}
|