dsh-agent-sdk-bridge 1.1.6 → 1.1.8
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 +47 -9
- package/FAQ.md +34 -8
- package/client.js +115 -50
- package/lib/index.js +370 -71
- package/lib/types/adapter.d.ts +80 -14
- package/lib/types/codebuddy-adapter.d.ts +20 -0
- package/lib/types/codebuddy.d.ts +24 -1
- package/lib/types/index.d.ts +10 -0
- package/lib/types/live.d.ts +2 -2
- package/lib/types/minimax-adapter.d.ts +8 -0
- package/lib/types/minimax.d.ts +7 -0
- package/lib/types/model-cache.d.ts +27 -0
- package/lib/types/models.d.ts +10 -0
- package/lib/types/render.d.ts +17 -4
- package/package.json +1 -1
- package/src/adapter.ts +188 -33
- package/src/api.ts +3 -0
- package/src/codebuddy-adapter.ts +75 -19
- package/src/codebuddy.ts +45 -4
- package/src/config.ts +1 -1
- package/src/index.ts +33 -0
- package/src/live.ts +2 -2
- package/src/minimax-adapter.ts +42 -15
- package/src/minimax.ts +10 -0
- package/src/model-cache.ts +39 -0
- package/src/models.ts +14 -0
- package/src/render.ts +42 -12
package/src/codebuddy.ts
CHANGED
|
@@ -352,8 +352,8 @@ export class CodeBuddyCatalog {
|
|
|
352
352
|
* Callers that must answer synchronously (deciding whether a model exposes a
|
|
353
353
|
* context-budget choice) use this instead of awaiting a refresh.
|
|
354
354
|
*/
|
|
355
|
-
snapshot(requested?: CodeBuddyRegion): readonly CodeBuddyModel[] {
|
|
356
|
-
return this.snapshots.get(requested ?? readLive(this.region, 'global'))?.models
|
|
355
|
+
snapshot(requested?: CodeBuddyRegion): readonly CodeBuddyModel[] | undefined {
|
|
356
|
+
return this.snapshots.get(requested ?? readLive(this.region, 'global'))?.models
|
|
357
357
|
}
|
|
358
358
|
|
|
359
359
|
/** Models for one region; a failed refresh keeps the last good list. */
|
|
@@ -933,6 +933,10 @@ export class CodeBuddySession {
|
|
|
933
933
|
private effort: string | undefined
|
|
934
934
|
private systemPrompt: string | undefined
|
|
935
935
|
private cwd: string | undefined
|
|
936
|
+
/** Context budget the host shows for this session, when a model offers one. */
|
|
937
|
+
private contextWindow: number | undefined
|
|
938
|
+
/** Budget already pushed to the CLI, keyed by model and value. */
|
|
939
|
+
private appliedWindow: string | undefined
|
|
936
940
|
/** Host messages already fed to the inner session, for continuation planning. */
|
|
937
941
|
fedMessages: readonly import('@deepseek-ai/dsh-llm').RequestMessage[] | undefined
|
|
938
942
|
/** Host system prompt last applied; informational for the adapter. */
|
|
@@ -949,12 +953,47 @@ export class CodeBuddySession {
|
|
|
949
953
|
}
|
|
950
954
|
|
|
951
955
|
/** Point the session at a region, model and optional system prompt. */
|
|
952
|
-
configure(
|
|
956
|
+
configure(
|
|
957
|
+
region: CodeBuddyRegion,
|
|
958
|
+
model: string,
|
|
959
|
+
options: { effort?: string, system?: string, cwd?: string, contextWindow?: number } = {},
|
|
960
|
+
): void {
|
|
953
961
|
this.region = region
|
|
954
962
|
this.model = model
|
|
955
963
|
this.effort = options.effort
|
|
956
964
|
this.systemPrompt = options.system
|
|
957
965
|
if (options.cwd !== undefined) this.cwd = options.cwd
|
|
966
|
+
if (options.contextWindow !== undefined) this.contextWindow = options.contextWindow
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Push the host-visible context window into the inner session.
|
|
971
|
+
*
|
|
972
|
+
* The CLI resolves its own budget (`override > model default > physical
|
|
973
|
+
* limit`) and both its compaction trigger and the upstream gateway enforce
|
|
974
|
+
* that budget. A global-catalog model such as `deepseek-v4.1-flash` declares
|
|
975
|
+
* `supportedLengths [300K, 1M]` with a 300K default, so a session left alone
|
|
976
|
+
* compacts (and the gateway rejects) anything past 276K — while the host,
|
|
977
|
+
* told 1M, keeps showing and feeding a larger history. Replaying that history
|
|
978
|
+
* then collapses to a summary. Applying the same number the host divides by
|
|
979
|
+
* keeps the two in step; models that expose no choice reject it and keep
|
|
980
|
+
* their default.
|
|
981
|
+
*
|
|
982
|
+
* The CLI refuses to clear the override (`contextWindow must be a positive
|
|
983
|
+
* safe integer`), so the adapter sends the model's default instead when the
|
|
984
|
+
* page returns to "default".
|
|
985
|
+
*/
|
|
986
|
+
private async applyContextWindow(session: SessionImpl): Promise<void> {
|
|
987
|
+
const window = this.contextWindow
|
|
988
|
+
if (window === undefined) return
|
|
989
|
+
const key = `${this.model}:${window}`
|
|
990
|
+
if (this.appliedWindow === key) return
|
|
991
|
+
this.appliedWindow = key
|
|
992
|
+
try {
|
|
993
|
+
await session.setConfig({ contextWindow: window })
|
|
994
|
+
} catch {
|
|
995
|
+
// `Allowed: <none>` models have one fixed window; their default stands.
|
|
996
|
+
}
|
|
958
997
|
}
|
|
959
998
|
|
|
960
999
|
/**
|
|
@@ -1031,8 +1070,10 @@ export class CodeBuddySession {
|
|
|
1031
1070
|
signal?.addEventListener('abort', onAbort, { once: true })
|
|
1032
1071
|
try {
|
|
1033
1072
|
// A model switch on a warm session is a control request; waiting for it
|
|
1034
|
-
// keeps the next prompt on the model the host just resolved.
|
|
1073
|
+
// keeps the next prompt on the model the host just resolved. The window
|
|
1074
|
+
// follows it: the CLI validates the value against the current model.
|
|
1035
1075
|
await session.setModel(this.model)
|
|
1076
|
+
await this.applyContextWindow(session)
|
|
1036
1077
|
if (feed !== null && feed.length > 0) {
|
|
1037
1078
|
const content = typeof feed === 'string'
|
|
1038
1079
|
? feed
|
package/src/config.ts
CHANGED
|
@@ -247,7 +247,7 @@ export const Config = z.object({
|
|
|
247
247
|
maxSessions: z.number().step(1).min(1).max(64).default(8).description('同时保持的内层会话数上限,超出后按最久未使用关闭。每个会话是一个子进程。').volatile(),
|
|
248
248
|
modelCacheTtlSeconds: z.number().step(1).min(10).max(86_400).default(300).description('模型列表缓存时长(秒)。调小可更快看到账号侧变化。').volatile(),
|
|
249
249
|
modelAliases: z.dict(z.string()).default({}).description('额外的模型别名映射:DSH 侧名称 → Qoder 模型 ID。内置 deepseek-v4-* 别名仍然生效。').volatile(),
|
|
250
|
-
defaultReasoningEffort: z.string().default('').description('
|
|
250
|
+
defaultReasoningEffort: z.string().default('').description('默认选用的思考档位;留空则用该模型支持的最强档位。').volatile(),
|
|
251
251
|
defaultMaxTokens: z.number().step(1).min(1).default(32_000).description('模型未声明输出上限时使用的输出上限。').volatile(),
|
|
252
252
|
toolResultTimeoutSeconds: z.number().step(1).min(5).max(3_600).default(120).description('仅用于未配对 handler 的兜底时限(秒):正常工具调用会等宿主下一次请求携带结果,不按此超时。').volatile(),
|
|
253
253
|
catalogFetchTimeoutSeconds: z.number().step(1).min(1).max(300).default(20).description('拉取模型列表的时限(秒),超时后回退到内置列表。').volatile(),
|
package/src/index.ts
CHANGED
|
@@ -54,10 +54,21 @@ interface WebServerService {
|
|
|
54
54
|
}): () => void
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* The mounted filesystem provider, used only to translate a host path into
|
|
59
|
+
* the execution world the host tools run in. Declared minimally so the plugin
|
|
60
|
+
* keeps only the peer dependencies it actually needs.
|
|
61
|
+
*/
|
|
62
|
+
interface FileSystemService {
|
|
63
|
+
processPathFromHostPath?(hostPath: string): string | undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
57
66
|
declare module '@deepseek-ai/cordis' {
|
|
58
67
|
interface Context {
|
|
59
68
|
/** Present only in web compositions; reached through a nested inject. */
|
|
60
69
|
webServer?: WebServerService
|
|
70
|
+
/** Present when a filesystem provider is mounted; reached by inject. */
|
|
71
|
+
fs?: FileSystemService
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
interface Events {
|
|
@@ -208,6 +219,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
208
219
|
// plugin's answer for the rest of the process lifetime.
|
|
209
220
|
const live = liveConfig(config)
|
|
210
221
|
let attachments: AttachmentStore | undefined
|
|
222
|
+
let filesystem: FileSystemService | undefined
|
|
211
223
|
|
|
212
224
|
/**
|
|
213
225
|
* Everything both editions share.
|
|
@@ -235,6 +247,20 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
235
247
|
return store.readImageRequest(ref, { width: ref.width, height: ref.height, maxBytes: REQUEST_IMAGE_MAX_BYTES }, signal)
|
|
236
248
|
.then(image => ({ bytes: image.data, mediaType: image.mediaType }))
|
|
237
249
|
},
|
|
250
|
+
/**
|
|
251
|
+
* Where the model can find the image on disk. Pixels let it describe the
|
|
252
|
+
* picture; this path lets a "modify and save it" turn act on the file
|
|
253
|
+
* instead of searching the workspace for something that was never there.
|
|
254
|
+
*/
|
|
255
|
+
imagePath: (ref: ImageAttachmentRef): string | undefined => {
|
|
256
|
+
const store = attachments
|
|
257
|
+
if (store === undefined) return undefined
|
|
258
|
+
const hostPath = store.imageHostPath(ref)
|
|
259
|
+
if (hostPath === undefined) return undefined
|
|
260
|
+
// Prefer the mounted tools' view of the same file; the local provider
|
|
261
|
+
// maps identically, a sandboxed one may not.
|
|
262
|
+
return filesystem?.processPathFromHostPath?.(hostPath) ?? hostPath
|
|
263
|
+
},
|
|
238
264
|
}
|
|
239
265
|
|
|
240
266
|
/**
|
|
@@ -279,6 +305,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
279
305
|
: live.codebuddyGlobalModelContextWindows(),
|
|
280
306
|
modelAliases: live.codebuddyModelAliases,
|
|
281
307
|
readImage: shared.readImage,
|
|
308
|
+
imagePath: shared.imagePath,
|
|
282
309
|
}),
|
|
283
310
|
minimax: new MinimaxAdapter({
|
|
284
311
|
maxSessions: live.maxSessions,
|
|
@@ -298,6 +325,7 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
298
325
|
: live.minimaxGlobalModelContextWindows(),
|
|
299
326
|
modelAliases: live.minimaxModelAliases,
|
|
300
327
|
readImage: shared.readImage,
|
|
328
|
+
imagePath: shared.imagePath,
|
|
301
329
|
}),
|
|
302
330
|
}
|
|
303
331
|
|
|
@@ -306,6 +334,11 @@ export function apply(ctx: Context, config: ParsedConfig): void {
|
|
|
306
334
|
scope.effect(() => () => { attachments = undefined }, 'llm-qoder-cn.attachments')
|
|
307
335
|
})
|
|
308
336
|
|
|
337
|
+
ctx.inject(['fs'], (scope) => {
|
|
338
|
+
filesystem = scope.fs
|
|
339
|
+
scope.effect(() => () => { filesystem = undefined }, 'llm-qoder-cn.fs')
|
|
340
|
+
})
|
|
341
|
+
|
|
309
342
|
// Routes and the provider directory are the two shapes a live value cannot
|
|
310
343
|
// reach on its own: which provider ids exist, and the labels the selection
|
|
311
344
|
// surfaces render. A disabled provider must own neither, so both are kept in
|
package/src/live.ts
CHANGED
|
@@ -47,8 +47,8 @@ export function readLive<T>(value: Live<T> | undefined, fallback: T): T {
|
|
|
47
47
|
* Resolve one {@link Live} option that may legitimately be undefined.
|
|
48
48
|
*
|
|
49
49
|
* Separate from {@link readLive} because an option whose "unset" state is
|
|
50
|
-
* itself meaningful (`defaultReasoningEffort`: empty means "
|
|
51
|
-
* has no fallback to apply.
|
|
50
|
+
* itself meaningful (`defaultReasoningEffort`: empty means "use the model's
|
|
51
|
+
* strongest level") has no fallback to apply.
|
|
52
52
|
*
|
|
53
53
|
* @param value - the plain value or thunk, possibly undefined.
|
|
54
54
|
* @returns the option's current value, or undefined when it is unset.
|
package/src/minimax-adapter.ts
CHANGED
|
@@ -22,15 +22,15 @@ import {
|
|
|
22
22
|
minimaxColdStream, minimaxRegionBlockMessage, minimaxRegionBlocked,
|
|
23
23
|
minimaxRegionBlockedCached, minimaxSignedIn, minimaxUsage,
|
|
24
24
|
} from './minimax.ts'
|
|
25
|
-
import { textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
26
25
|
import type { MinimaxModel, MinimaxPromptBlock, MinimaxUsageSnapshot } from './minimax.ts'
|
|
27
26
|
import { resolveMinimaxCli } from './minimax-cli.ts'
|
|
28
27
|
import { reasoningInfo, planContinuation } from './adapter.ts'
|
|
28
|
+
import { cachedModels } from './model-cache.ts'
|
|
29
29
|
import type { ImageRequestReader } from './adapter.ts'
|
|
30
30
|
import type { Live } from './live.ts'
|
|
31
31
|
import { readLive, readLiveOptional } from './live.ts'
|
|
32
|
-
import type { Feed } from './render.ts'
|
|
33
|
-
import { feedImageRefs, feedToText, renderInitialFeed, resolvedFeedParts } from './render.ts'
|
|
32
|
+
import type { Feed, ImagePathResolver } from './render.ts'
|
|
33
|
+
import { feedImageRefs, feedToText, imageRefText, renderInitialFeed, resolvedFeedParts } from './render.ts'
|
|
34
34
|
|
|
35
35
|
/** The MiniMax Code provider route. */
|
|
36
36
|
export const MINIMAX_PROVIDER = 'minimax-acp'
|
|
@@ -56,6 +56,11 @@ export interface MinimaxAdapterOptions {
|
|
|
56
56
|
* store) → images degrade to their handle text instead of failing the turn.
|
|
57
57
|
*/
|
|
58
58
|
readImage?: ImageRequestReader
|
|
59
|
+
/**
|
|
60
|
+
* The tool-world path of one image, for the handle text that lets the model
|
|
61
|
+
* edit it. Absent → an unreadable image keeps the harness placeholder.
|
|
62
|
+
*/
|
|
63
|
+
imagePath?: ImagePathResolver
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
const NO_MODELS: readonly string[] = []
|
|
@@ -90,6 +95,8 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
90
95
|
private readonly auth = new MinimaxAuth()
|
|
91
96
|
private readonly mcpServer = new MinimaxMcpServer()
|
|
92
97
|
private readonly disabledCache = new Map<MinimaxRegion, { key: string, set: ReadonlySet<string> }>()
|
|
98
|
+
/** Last verified image capability, keyed `${region}:${model}`. */
|
|
99
|
+
private readonly knownVision = new Map<string, boolean>()
|
|
93
100
|
|
|
94
101
|
constructor(options: MinimaxAdapterOptions = {}) {
|
|
95
102
|
super()
|
|
@@ -136,7 +143,9 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
136
143
|
async describeCatalog(region: MinimaxRegion = this.region): Promise<readonly MinimaxModel[]> {
|
|
137
144
|
if (!this.isSignedIn(region)) return []
|
|
138
145
|
if (await minimaxRegionBlocked(region)) return []
|
|
139
|
-
|
|
146
|
+
const models = await this.catalog.models(region)
|
|
147
|
+
for (const model of models) this.knownVision.set(`${region}:${model.id}`, model.vision)
|
|
148
|
+
return models
|
|
140
149
|
}
|
|
141
150
|
|
|
142
151
|
/** Whether the region's endpoints are refusing this machine's network. */
|
|
@@ -246,9 +255,13 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
246
255
|
override async resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
|
|
247
256
|
const region = this.region
|
|
248
257
|
const target = resolveMinimaxModelId(model, this.aliases)
|
|
249
|
-
const
|
|
258
|
+
const key = `${region}:${target}`
|
|
259
|
+
// Cache first: resolving a route may not wait on an ACP probe.
|
|
260
|
+
const live = (await cachedModels(this.catalog.snapshot(region), () => this.catalog.models(region)))
|
|
261
|
+
.find(entry => entry.id === target)
|
|
250
262
|
if (live !== undefined) {
|
|
251
|
-
|
|
263
|
+
this.knownVision.set(key, live.vision)
|
|
264
|
+
const reasoning = reasoningInfo(live.reasoningEfforts, readLiveOptional(this.options.defaultReasoningEffort), live.reasoning)
|
|
252
265
|
return {
|
|
253
266
|
provider,
|
|
254
267
|
id: model,
|
|
@@ -269,7 +282,7 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
269
282
|
provider,
|
|
270
283
|
id: model,
|
|
271
284
|
name: model,
|
|
272
|
-
inputModalities: ['text'],
|
|
285
|
+
inputModalities: this.knownVision.get(key) === true ? ['text', 'image'] : ['text'],
|
|
273
286
|
context: { contextWindow: this.contextWindowFor(target, region) ?? MINIMAX_DEFAULT_CONTEXT_WINDOW },
|
|
274
287
|
defaultMaxTokens: readLive(this.options.defaultMaxTokens, MINIMAX_DEFAULT_MAX_TOKENS),
|
|
275
288
|
...reasoning === undefined ? {} : { reasoning },
|
|
@@ -297,7 +310,7 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
297
310
|
}
|
|
298
311
|
const model = resolveMinimaxModelId(options.model, this.aliases)
|
|
299
312
|
if (options.sessionId === undefined || options.purpose !== undefined) {
|
|
300
|
-
const prompt = feedToText(renderInitialFeed(options.system, options.messages))
|
|
313
|
+
const prompt = feedToText(renderInitialFeed(options.system, options.messages, this.options.imagePath), this.options.imagePath)
|
|
301
314
|
+ '\n(这是一次性旁路请求,直接输出下一条助手回复。)'
|
|
302
315
|
for (const chunk of await minimaxColdStream(region, prompt, model, options)) yield chunk
|
|
303
316
|
return
|
|
@@ -367,7 +380,7 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
367
380
|
session.fedSystem = options.system
|
|
368
381
|
const vision = await this.modelVision(model, region)
|
|
369
382
|
const feed = await this.buildFeed(
|
|
370
|
-
renderInitialFeed(undefined, options.messages),
|
|
383
|
+
renderInitialFeed(undefined, options.messages, this.options.imagePath),
|
|
371
384
|
vision,
|
|
372
385
|
options.signal,
|
|
373
386
|
)
|
|
@@ -377,8 +390,15 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
377
390
|
/** Whether the resolved model accepts images (drives whether pixels are sent). */
|
|
378
391
|
private async modelVision(model: string, region: MinimaxRegion): Promise<boolean> {
|
|
379
392
|
const target = resolveMinimaxModelId(model, this.aliases)
|
|
393
|
+
const key = `${region}:${target}`
|
|
380
394
|
const live = (await this.catalog.models(region).catch(() => [])).find(entry => entry.id === target)
|
|
381
|
-
|
|
395
|
+
if (live !== undefined) {
|
|
396
|
+
this.knownVision.set(key, live.vision)
|
|
397
|
+
return live.vision
|
|
398
|
+
}
|
|
399
|
+
// Keep a verified vision capability across a missed catalog read; otherwise
|
|
400
|
+
// DSH strips the pixels before this adapter ever runs.
|
|
401
|
+
return this.knownVision.get(key) === true
|
|
382
402
|
}
|
|
383
403
|
|
|
384
404
|
/**
|
|
@@ -395,7 +415,7 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
395
415
|
signal?: AbortSignal,
|
|
396
416
|
): Promise<string | MinimaxPromptBlock[]> {
|
|
397
417
|
if (typeof feed === 'string') return feed
|
|
398
|
-
if (!vision) return feedToText(feed)
|
|
418
|
+
if (!vision) return feedToText(feed, this.options.imagePath)
|
|
399
419
|
const images = new Map<string, { data: string, mediaType: string }>()
|
|
400
420
|
const readImage = this.options.readImage
|
|
401
421
|
if (readImage !== undefined) {
|
|
@@ -422,10 +442,17 @@ export class MinimaxAdapter extends LlmAdapter {
|
|
|
422
442
|
content.push({ type: 'text', text: part.text })
|
|
423
443
|
continue
|
|
424
444
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
445
|
+
// The pixels let the model read the image; this handle lets it find and
|
|
446
|
+
// edit the source file. Without it a "modify this image" turn searches
|
|
447
|
+
// the workspace for a file that was never there.
|
|
448
|
+
const path = this.options.imagePath?.(part.attachment)
|
|
449
|
+
content.push({
|
|
450
|
+
type: 'text',
|
|
451
|
+
text: part.type === 'image-unreadable' && path === undefined
|
|
452
|
+
? `[图片附件 ${String(part.attachment.attachmentId)} 本次未能读取,请基于文字内容继续]`
|
|
453
|
+
: imageRefText(part.attachment, path),
|
|
454
|
+
})
|
|
455
|
+
if (part.type === 'image-unreadable') continue
|
|
429
456
|
content.push({ type: 'image', data: part.data, mimeType: part.mediaType })
|
|
430
457
|
}
|
|
431
458
|
return content
|
package/src/minimax.ts
CHANGED
|
@@ -1527,6 +1527,16 @@ export class MinimaxCatalog {
|
|
|
1527
1527
|
}
|
|
1528
1528
|
}
|
|
1529
1529
|
|
|
1530
|
+
/**
|
|
1531
|
+
* The last successful list for a region, without probing.
|
|
1532
|
+
*
|
|
1533
|
+
* The picker resolves a route on every switch; awaiting an ACP probe there
|
|
1534
|
+
* cost a process round-trip whenever the TTL had just expired.
|
|
1535
|
+
*/
|
|
1536
|
+
snapshot(requested?: MinimaxRegion): readonly MinimaxModel[] | undefined {
|
|
1537
|
+
return this.snapshots.get(requested ?? readLive(this.region, 'cn'))?.models
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1530
1540
|
/** Why the last refresh failed, or undefined when it succeeded. */
|
|
1531
1541
|
error(requested?: MinimaxRegion): string | undefined {
|
|
1532
1542
|
return this.failures.get(requested ?? readLive(this.region, 'cn'))
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache-first catalogue reads for the model picker.
|
|
3
|
+
*
|
|
4
|
+
* The harness resolves a route every time the user switches model or reasoning
|
|
5
|
+
* level, and each of the three providers answers that resolution from a
|
|
6
|
+
* catalogue whose fetch is expensive: Qoder cold-starts its CLI (up to the
|
|
7
|
+
* fetch timeout), CodeBuddy boots a process, MiniMax probes over ACP. Awaiting
|
|
8
|
+
* that fetch inside a resolution made the picker freeze for seconds whenever
|
|
9
|
+
* the cache had just expired.
|
|
10
|
+
*
|
|
11
|
+
* So a resolution reads the last successful snapshot when there is one and lets
|
|
12
|
+
* the catalogue refresh behind it; only the very first resolution, where there
|
|
13
|
+
* is nothing to show, waits. A snapshot that is merely stale still describes
|
|
14
|
+
* the same account's models, so answering from it is no less correct than the
|
|
15
|
+
* previous behaviour of answering from a failed fetch's fallback.
|
|
16
|
+
*
|
|
17
|
+
* @module dsh-agent-sdk-bridge/model-cache
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The cached list when one exists, otherwise the awaited fetch.
|
|
22
|
+
*
|
|
23
|
+
* @param snapshot - last successful catalogue, or undefined before the first.
|
|
24
|
+
* @param fetch - the catalogue's normal (TTL-aware, in-flight-sharing) read.
|
|
25
|
+
* @returns the entries to resolve against, never empty-by-surprise when a
|
|
26
|
+
* snapshot exists.
|
|
27
|
+
*/
|
|
28
|
+
export async function cachedModels<T>(
|
|
29
|
+
snapshot: readonly T[] | undefined,
|
|
30
|
+
fetch: () => Promise<readonly T[]>,
|
|
31
|
+
): Promise<readonly T[]> {
|
|
32
|
+
if (snapshot !== undefined && snapshot.length > 0) {
|
|
33
|
+
// Kick the refresh but do not wait for it: the caller needs an answer now,
|
|
34
|
+
// and a failure here changes nothing because the snapshot stays published.
|
|
35
|
+
void fetch().catch(() => undefined)
|
|
36
|
+
return snapshot
|
|
37
|
+
}
|
|
38
|
+
return fetch()
|
|
39
|
+
}
|
package/src/models.ts
CHANGED
|
@@ -101,6 +101,20 @@ export class QoderModelCatalog {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The last successful live list, without fetching.
|
|
106
|
+
*
|
|
107
|
+
* The model picker resolves a route on every switch, and a switch must not
|
|
108
|
+
* wait on a CLI cold start (up to the fetch timeout). Callers that need an
|
|
109
|
+
* immediate answer read this and let {@link liveModels} refresh behind them;
|
|
110
|
+
* `undefined` means nothing has been read yet, which only the first switch
|
|
111
|
+
* has to wait for.
|
|
112
|
+
*/
|
|
113
|
+
snapshot(): readonly ModelInfo[] | undefined {
|
|
114
|
+
const cached = this.cached ?? sharedSnapshots.get(this.brand)
|
|
115
|
+
return cached === undefined ? undefined : officialOnly(cached.models)
|
|
116
|
+
}
|
|
117
|
+
|
|
104
118
|
/** Why the last refresh failed, or undefined when it succeeded. */
|
|
105
119
|
get error(): string | undefined {
|
|
106
120
|
return this.failure
|
package/src/render.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @module dsh-llm-qoder/render
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
import { fileHandleText, offloadedImageText, textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
16
|
+
import { fileHandleText, offloadedImageText, requestImageHandleText, textOnlyImageText } from '@deepseek-ai/dsh-llm'
|
|
17
17
|
import type { ContentBlock, RequestMessage } from '@deepseek-ai/dsh-llm'
|
|
18
18
|
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
|
|
19
19
|
import { countWideChars } from './tokens.ts'
|
|
@@ -23,6 +23,25 @@ export type FeedPart =
|
|
|
23
23
|
| { type: 'text', text: string }
|
|
24
24
|
| { type: 'image', attachment: ImageAttachmentRef }
|
|
25
25
|
|
|
26
|
+
/** Resolve the tool-world path of one durable image attachment. */
|
|
27
|
+
export type ImagePathResolver = (ref: ImageAttachmentRef) => string | undefined
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Model-facing handle for one image.
|
|
31
|
+
*
|
|
32
|
+
* The host's native adapters use the same text: it names the attachment, its
|
|
33
|
+
* pixel size and media type, and the read-only normalized copy on disk, and it
|
|
34
|
+
* tells the model to copy that file to a writable path before editing. Pixels
|
|
35
|
+
* alone let a model describe an image but not process it — without this path
|
|
36
|
+
* the model goes looking through the workspace for a file that was never there.
|
|
37
|
+
*/
|
|
38
|
+
export function imageRefText(ref: ImageAttachmentRef, path: string | undefined): string {
|
|
39
|
+
if (path === undefined) {
|
|
40
|
+
return `[图片附件 ${String(ref.attachmentId)}(${ref.width}×${ref.height}px,${ref.mediaType})]`
|
|
41
|
+
}
|
|
42
|
+
return requestImageHandleText(ref, { width: ref.width, height: ref.height }, { readonlyPath: path })
|
|
43
|
+
}
|
|
44
|
+
|
|
26
45
|
/** What one inner turn receives: text (the historical shape) or text + images. */
|
|
27
46
|
export type Feed = string | FeedPart[]
|
|
28
47
|
|
|
@@ -40,12 +59,13 @@ export function imageRefs(blocks: readonly ContentBlock[]): ImageAttachmentRef[]
|
|
|
40
59
|
}
|
|
41
60
|
|
|
42
61
|
/** Render one content-block list as plain text; non-text blocks get handles. */
|
|
43
|
-
export function renderBlocks(blocks: readonly ContentBlock[]): string {
|
|
62
|
+
export function renderBlocks(blocks: readonly ContentBlock[], imagePath?: ImagePathResolver): string {
|
|
63
|
+
const parts: string[] = []
|
|
44
64
|
for (const block of blocks) {
|
|
45
65
|
switch (block.type) {
|
|
46
66
|
case 'text': parts.push(block.text); break
|
|
47
67
|
case 'reasoning': break
|
|
48
|
-
case 'image': parts.push(block.offloaded === true ? offloadedImageText(block.attachment) :
|
|
68
|
+
case 'image': parts.push(block.offloaded === true ? offloadedImageText(block.attachment) : imageRefText(block.attachment, imagePath?.(block.attachment))); break
|
|
49
69
|
case 'file': parts.push(fileHandleText(block.attachment, undefined)); break
|
|
50
70
|
case 'tool-call': parts.push(`[调用了工具 ${block.name}(${block.arguments})]`); break
|
|
51
71
|
case 'tool-addition': parts.push(`[工具启用 ${block.toolName}]`); break
|
|
@@ -71,8 +91,8 @@ function roleTag(message: RequestMessage): string {
|
|
|
71
91
|
* Render one request message with its role tag. A request-only user input has
|
|
72
92
|
* no durable identity or source, so it reads exactly like a user message.
|
|
73
93
|
*/
|
|
74
|
-
export function renderMessage(message: RequestMessage): string {
|
|
75
|
-
return `[${roleTag(message)}] ${renderBlocks(message.content)}`
|
|
94
|
+
export function renderMessage(message: RequestMessage, imagePath?: ImagePathResolver): string {
|
|
95
|
+
return `[${roleTag(message)}] ${renderBlocks(message.content, imagePath)}`
|
|
76
96
|
}
|
|
77
97
|
|
|
78
98
|
/** Preamble establishing the inner model's role as this agent's LLM backend. */
|
|
@@ -158,10 +178,16 @@ export function feedImageRefs(feed: Feed): ImageAttachmentRef[] {
|
|
|
158
178
|
}
|
|
159
179
|
|
|
160
180
|
/** Flatten a feed back to text, replacing images with the harness handle text. */
|
|
161
|
-
export function feedToText(feed: Feed): string {
|
|
181
|
+
export function feedToText(feed: Feed, imagePath?: ImagePathResolver): string {
|
|
162
182
|
if (typeof feed === 'string') return feed
|
|
163
183
|
return feed
|
|
164
|
-
.map(part =>
|
|
184
|
+
.map(part => {
|
|
185
|
+
if (part.type === 'text') return part.text
|
|
186
|
+
const path = imagePath?.(part.attachment)
|
|
187
|
+
// This function is used only where pixels are not sent, so the harness's
|
|
188
|
+
// "text only" wording is accurate when no path is available.
|
|
189
|
+
return path === undefined ? textOnlyImageText(part.attachment) : imageRefText(part.attachment, path)
|
|
190
|
+
})
|
|
165
191
|
.filter(text => text.length > 0)
|
|
166
192
|
.join('\n\n')
|
|
167
193
|
}
|
|
@@ -169,7 +195,7 @@ export function feedToText(feed: Feed): string {
|
|
|
169
195
|
/** One feed element after image bytes have been resolved (base64). */
|
|
170
196
|
export type ResolvedFeedPart =
|
|
171
197
|
| { type: 'text', text: string }
|
|
172
|
-
| { type: 'image', data: string, mediaType: string }
|
|
198
|
+
| { type: 'image', attachment: ImageAttachmentRef, data: string, mediaType: string }
|
|
173
199
|
| { type: 'image-unreadable', attachment: ImageAttachmentRef }
|
|
174
200
|
|
|
175
201
|
/**
|
|
@@ -195,7 +221,7 @@ export function resolvedFeedParts(feed: Feed, images: ReadonlyMap<string, { data
|
|
|
195
221
|
const resolved = images.get(String(part.attachment.attachmentId))
|
|
196
222
|
parts.push(resolved === undefined
|
|
197
223
|
? { type: 'image-unreadable', attachment: part.attachment }
|
|
198
|
-
: { type: 'image', data: resolved.data, mediaType: resolved.mediaType })
|
|
224
|
+
: { type: 'image', attachment: part.attachment, data: resolved.data, mediaType: resolved.mediaType })
|
|
199
225
|
}
|
|
200
226
|
return parts
|
|
201
227
|
}
|
|
@@ -222,14 +248,18 @@ export function feedWideCharCount(feed: Feed): number {
|
|
|
222
248
|
* prompt, and the existing conversation as compact context. Messages of the
|
|
223
249
|
* current turn keep their image references; earlier ones are handles.
|
|
224
250
|
*/
|
|
225
|
-
export function renderInitialFeed(
|
|
251
|
+
export function renderInitialFeed(
|
|
252
|
+
system: string | undefined,
|
|
253
|
+
messages: readonly RequestMessage[],
|
|
254
|
+
imagePath?: ImagePathResolver,
|
|
255
|
+
): Feed {
|
|
226
256
|
const from = currentTurnStart(messages)
|
|
227
257
|
const history: Feed[] = []
|
|
228
258
|
for (const [index, message] of messages.entries()) {
|
|
229
259
|
const entry: Feed = index >= from && imageRefs(message.content).length > 0
|
|
230
260
|
? label(`[${roleTag(message)}] `, blockParts(message.content))
|
|
231
|
-
: renderMessage(message)
|
|
232
|
-
if (feedToText(entry).length > 0) history.push(entry)
|
|
261
|
+
: renderMessage(message, imagePath)
|
|
262
|
+
if (feedToText(entry, imagePath).length > 0) history.push(entry)
|
|
233
263
|
}
|
|
234
264
|
const sections: Feed[] = [BACKEND_ROLE]
|
|
235
265
|
if (system !== undefined && system.length > 0) {
|