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/src/adapter.ts CHANGED
@@ -23,8 +23,9 @@ import type { ResolvedConfig } from './config.ts'
23
23
  import type { Live } from './live.ts'
24
24
  import { readLive, readLiveOptional } from './live.ts'
25
25
  import { DEFAULT_MODEL_CACHE_TTL_MS, DEFAULT_FETCH_TIMEOUT_MS, QoderModelCatalog } from './models.ts'
26
- import { feedImageRefs, feedToText, imageRefs, joinFeeds, renderInitialFeed, renderRefreshed, renderUserTurn } from './render.ts'
27
- import type { Feed } from './render.ts'
26
+ import { cachedModels } from './model-cache.ts'
27
+ import { feedImageRefs, feedToText, imageRefText, imageRefs, joinFeeds, renderInitialFeed, renderRefreshed, renderUserTurn } from './render.ts'
28
+ import type { Feed, ImagePathResolver } from './render.ts'
28
29
  import { QoderSession, QoderSessionManager, DEFAULT_MAX_SESSIONS } from './session.ts'
29
30
  import type { ChannelContent, ResolvedImages, SessionModelPolicy } from './session.ts'
30
31
  import type { UsageSnapshot } from './usage.ts'
@@ -56,6 +57,11 @@ export interface QoderAdapterOptions {
56
57
  modelCacheTtlMs?: Live<number>
57
58
  /** Request-image reader; absent → images are never forwarded as pixels. */
58
59
  readImage?: ImageRequestReader
60
+ /**
61
+ * The tool-world path of one image, for the handle text that lets the model
62
+ * edit it. Absent → an unreadable image keeps the harness placeholder.
63
+ */
64
+ imagePath?: ImagePathResolver
59
65
  /** How long an unpaired MCP tool handler waits before failing its call. */
60
66
  toolResultTimeoutMs?: Live<number>
61
67
  /** How long the inner process gets to acknowledge an abort. */
@@ -95,6 +101,13 @@ export const QODER_PROVIDER = 'qoder-cn'
95
101
  /** The international edition's route; a second adapter instance serves it. */
96
102
  export const QODER_INTL_PROVIDER = 'qoder-intl'
97
103
 
104
+ /** The window facts one live catalog entry carries. */
105
+ export type WindowFacts = {
106
+ maxInputTokens?: number
107
+ defaultContextWindow?: number
108
+ availableContextWindows?: readonly number[]
109
+ }
110
+
98
111
  /**
99
112
  * Decide the context window to report for one live model.
100
113
  *
@@ -103,6 +116,8 @@ export const QODER_INTL_PROVIDER = 'qoder-intl'
103
116
  * actually runs with. `maxInputTokens` is NOT a substitute — it is the ceiling
104
117
  * for a single request and is often lower than the session window (e.g. 180 000
105
118
  * against a 200 000 session), so falling back to it would understate capacity.
119
+ * Callers that accept a user override should validate it against
120
+ * `availableContextWindows` first — see {@link resolveDeclaredWindow}.
106
121
  *
107
122
  * @param live - the live catalog entry, when the CLI answered.
108
123
  * @param override - the user's chosen window for this model, if any.
@@ -110,7 +125,7 @@ export const QODER_INTL_PROVIDER = 'qoder-intl'
110
125
  * @returns the window in tokens.
111
126
  */
112
127
  export function pickContextWindow(
113
- live: { maxInputTokens?: number, defaultContextWindow?: number, availableContextWindows?: readonly number[] } | undefined,
128
+ live: WindowFacts | undefined,
114
129
  override: number | undefined,
115
130
  fallback: number,
116
131
  ): number {
@@ -144,16 +159,39 @@ function largestWindow(available: readonly number[] | undefined): number | undef
144
159
  * @param declared - the window this route publishes to the harness.
145
160
  * @returns the window the CLI runs with, or undefined when unknown.
146
161
  */
147
- export function cliContextWindow(
148
- live: { maxInputTokens?: number, defaultContextWindow?: number, availableContextWindows?: readonly number[] } | undefined,
149
- declared: number,
150
- ): number | undefined {
162
+ export function cliContextWindow(live: WindowFacts | undefined, declared: number): number | undefined {
151
163
  if (live === undefined) return undefined
152
164
  const available = live.availableContextWindows
153
165
  if (available !== undefined && available.includes(declared)) return declared
154
166
  return live.defaultContextWindow ?? largestWindow(available) ?? live.maxInputTokens
155
167
  }
156
168
 
169
+ /**
170
+ * The window to publish for one route, which is also the one the CLI is told
171
+ * to run with.
172
+ *
173
+ * A live entry is authoritative: the user's override counts only when the
174
+ * model advertises it. The CLI silently ignores anything else (a 1M choice on
175
+ * a 200K-only route), and dividing by it would understate every reading
176
+ * fivefold. When the catalog is momentarily unavailable, the last successful
177
+ * answer is reused instead of the static 200K default — that fallback used to
178
+ * change the denominator, and the compaction budget, mid-session: a 1M
179
+ * conversation read 1M one turn and 200K the next, and then compacted at what
180
+ * looked like 10%.
181
+ */
182
+ export function resolveDeclaredWindow(
183
+ live: WindowFacts | undefined,
184
+ override: number | undefined,
185
+ remembered: number | undefined,
186
+ fallback: number,
187
+ ): number {
188
+ if (live === undefined) return remembered ?? override ?? fallback
189
+ const available = live.availableContextWindows
190
+ const honored = override !== undefined
191
+ && (available === undefined || available.length === 0 || available.includes(override))
192
+ return pickContextWindow(live, honored ? override : undefined, remembered ?? fallback)
193
+ }
194
+
157
195
  function modelInfo(provider: string, entry: { id: string, name: string, description?: string, isVl?: boolean }): LlmModelInfo {
158
196
  return {
159
197
  provider,
@@ -179,31 +217,77 @@ const EFFORT_NAMES: Readonly<Record<string, string>> = {
179
217
  max: 'Max',
180
218
  }
181
219
 
220
+ /**
221
+ * Strength order used to pick a model's starting level.
222
+ *
223
+ * The three providers spell their levels differently (Qoder publishes
224
+ * `low|medium|high|max`, MiniMax `low|high`, CodeBuddy whatever its catalog
225
+ * declares), so the ranking is by name rather than by position in any one
226
+ * upstream list. An id outside this table ranks below every known one, which is
227
+ * what several sources agreeing on an unfamiliar spelling should do.
228
+ */
229
+ const EFFORT_ORDER: readonly string[] = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max']
230
+
231
+ /**
232
+ * The strongest level a model offers.
233
+ *
234
+ * Switching model or level must start at the most capable setting the route
235
+ * accepts, the same rule the sibling adapter plugin uses; the previous
236
+ * behaviour of following upstream's own default meant some models silently
237
+ * started one or two steps down (`CodeBuddy` advertises `high` as its default
238
+ * even when `max` is selectable).
239
+ *
240
+ * @param efforts - the levels the route accepts, in upstream order.
241
+ * @returns the strongest id, or undefined for a non-reasoning model.
242
+ */
243
+ export function strongestEffort(efforts: readonly string[]): string | undefined {
244
+ if (efforts.length === 0) return undefined
245
+ let best: string | undefined
246
+ let bestRank = -1
247
+ for (const effort of efforts) {
248
+ // First occurrence wins a tie, which keeps an upstream-ordered list stable
249
+ // when it repeats a level.
250
+ const rank = EFFORT_ORDER.indexOf(effort)
251
+ if (best === undefined || rank > bestRank) {
252
+ best = effort
253
+ bestRank = rank
254
+ }
255
+ }
256
+ return best
257
+ }
258
+
182
259
  /**
183
260
  * Build the `reasoning` metadata block for a resolved model, or undefined
184
261
  * when the model does not support reasoning.
262
+ *
263
+ * The starting level is the strongest one the model accepts, not the level
264
+ * upstream happens to name as its default: the user's explicit choice in the
265
+ * sibling plugin, applied here too. An explicit configuration value still wins
266
+ * when the model accepts it.
267
+ *
185
268
  * @param efforts - CLI-reported effort ids (absent for reasoning-less models).
186
- * @param defaultEffort - CLI-reported default effort id.
269
+ * @param configuredEffort - user-configured starting level, when any.
187
270
  * @param isReasoning - whether the model supports reasoning per the CLI.
188
271
  * @returns the harness reasoning metadata, or undefined to omit it.
189
272
  */
190
273
  export function reasoningInfo(
191
274
  efforts: readonly string[] | undefined,
192
- defaultEffort: string | undefined,
275
+ configuredEffort: string | undefined,
193
276
  isReasoning: boolean | undefined,
194
277
  ): LlmModelReasoningInfo | undefined {
195
278
  if (efforts === undefined && isReasoning === false) return undefined
196
279
  const ids = efforts !== undefined && efforts.length > 0
197
280
  ? efforts
198
281
  : DEFAULT_REASONING_EFFORTS
282
+ const defaultEffort = configuredEffort !== undefined && ids.includes(configuredEffort)
283
+ ? configuredEffort
284
+ : strongestEffort(ids)
199
285
  return {
200
286
  efforts: ids.map(id => ({
201
287
  id: ReasoningEffortId(id),
202
288
  name: EFFORT_NAMES[id] ?? id,
203
289
  })),
204
- ...defaultEffort !== undefined && ids.includes(defaultEffort)
205
- ? { defaultEffort: ReasoningEffortId(defaultEffort) }
206
- : {},
290
+ ...defaultEffort === undefined ? {} : { defaultEffort: ReasoningEffortId(defaultEffort) },
207
291
  }
208
292
  }
209
293
 
@@ -237,6 +321,7 @@ export class QoderAdapter extends LlmAdapter {
237
321
  private readonly catalog: QoderModelCatalog
238
322
  private readonly usage: QoderUsage
239
323
  private readonly readImage: ImageRequestReader | undefined
324
+ private readonly imagePath: ImagePathResolver | undefined
240
325
  private readonly brand: QoderBrand
241
326
  /**
242
327
  * Memoized `Set` view of the disabled-model list.
@@ -246,6 +331,24 @@ export class QoderAdapter extends LlmAdapter {
246
331
  * hash probe instead of rebuilding a set per call, with no staleness.
247
332
  */
248
333
  private disabledCache: { source: readonly string[], set: ReadonlySet<string> } | undefined
334
+ /**
335
+ * The last window each route resolved to, plus what the model advertised.
336
+ *
337
+ * Reused only when a live entry is missing. Without it a failed refresh fell
338
+ * back to the static 200K, so a 1M session suddenly declared 200K: the ring
339
+ * denominator changed and the compaction engine budgeted against 200K,
340
+ * compacting a history that was still only ~10% of the real window.
341
+ */
342
+ private readonly knownWindows = new Map<string, { window: number, available?: readonly number[] }>()
343
+ /**
344
+ * Last verified image capability per route.
345
+ *
346
+ * A missed catalog refresh used to fall back to text-only, and DSH then
347
+ * stripped the pixels before this adapter ran: a vision model silently lost
348
+ * the image for that turn. Remembering the last answer keeps the declared
349
+ * capability stable across a transient miss.
350
+ */
351
+ private readonly knownVision = new Map<string, boolean>()
249
352
 
250
353
  constructor(options: QoderAdapterOptions = {}) {
251
354
  super()
@@ -264,6 +367,7 @@ export class QoderAdapter extends LlmAdapter {
264
367
  )
265
368
  this.usage = new QoderUsage(this.brand, options.usageCacheTtlMs)
266
369
  this.readImage = options.readImage
370
+ this.imagePath = options.imagePath
267
371
  }
268
372
 
269
373
  private get defaultMaxTokens(): number {
@@ -338,7 +442,14 @@ export class QoderAdapter extends LlmAdapter {
338
442
  * @returns live catalog entries in upstream order.
339
443
  */
340
444
  async describeCatalog(): Promise<Awaited<ReturnType<QoderModelCatalog['liveModels']>>> {
341
- return this.catalog.liveModels()
445
+ const live = await this.catalog.liveModels()
446
+ // Seed the remembered windows and capabilities here too, so the settings
447
+ // page's rows and the request path validate against the same catalog.
448
+ for (const entry of live) {
449
+ this.declaredWindow(entry.value, entry)
450
+ this.knownVision.set(entry.value, entry.isVl === true)
451
+ }
452
+ return live
342
453
  }
343
454
 
344
455
  /** Forget the cached catalog; the next read asks the CLI again. */
@@ -375,9 +486,33 @@ export class QoderAdapter extends LlmAdapter {
375
486
  return [...this.disabledModels]
376
487
  }
377
488
 
378
- /** The user's window for one model, or undefined when following upstream. */
489
+ /**
490
+ * The user's window for one model, when the provider accepts it.
491
+ *
492
+ * A stored choice the model does not advertise is not a real choice: the CLI
493
+ * ignores it and keeps its own window, so publishing it would only make the
494
+ * meter divide by a capacity the route never runs with. Until the catalog
495
+ * has answered once the choice is trusted (there is nothing to check yet).
496
+ */
379
497
  contextWindowFor(id: string): number | undefined {
380
- return this.windowOverride(id)
498
+ const override = this.windowOverride(id)
499
+ if (override === undefined) return undefined
500
+ const target = resolveQoderModelId(id, this.modelAliases)
501
+ const available = this.knownWindows.get(target)?.available
502
+ if (available === undefined || available.length === 0) return override
503
+ return available.includes(override) ? override : undefined
504
+ }
505
+
506
+ /** The window this route publishes, remembering it for catalog misses. */
507
+ private declaredWindow(id: string, live: WindowFacts | undefined): number {
508
+ const window = resolveDeclaredWindow(
509
+ live,
510
+ this.windowOverride(id),
511
+ this.knownWindows.get(id)?.window,
512
+ DEFAULT_CONTEXT_WINDOW,
513
+ )
514
+ if (live !== undefined) this.knownWindows.set(id, { window, available: live.availableContextWindows })
515
+ return window
381
516
  }
382
517
 
383
518
  /** The stored per-model window map, for rewriting the field. */
@@ -404,9 +539,16 @@ export class QoderAdapter extends LlmAdapter {
404
539
  // address to find the metadata, but echo the requested id back: the seam
405
540
  // rejects an exact-model result whose id differs from the one asked for.
406
541
  const target = resolveQoderModelId(model, this.modelAliases)
407
- const live = (await this.catalog.liveModels()).find(entry => entry.value === target)
542
+ // The picker resolves on every switch and every level change. Awaiting the
543
+ // CLI catalogue here (a cold start, up to the fetch timeout) froze the menu
544
+ // whenever the cache had just expired, so a snapshot answers immediately and
545
+ // the refresh runs behind it.
546
+ const live = (await cachedModels(this.catalog.snapshot(), () => this.catalog.liveModels()))
547
+ .find(entry => entry.value === target)
408
548
  if (live !== undefined) {
409
- const reasoning = reasoningInfo(live.efforts, live.defaultEffort ?? this.defaultReasoningEffort, live.isReasoning)
549
+ const vision = live.isVl === true
550
+ this.knownVision.set(target, vision)
551
+ const reasoning = reasoningInfo(live.efforts, this.defaultReasoningEffort, live.isReasoning)
410
552
  return {
411
553
  provider,
412
554
  id: model,
@@ -416,7 +558,7 @@ export class QoderAdapter extends LlmAdapter {
416
558
  // projects images to handle text for a route that omits it, so a claim
417
559
  // the CLI did not make would silently drop pixels; the reverse error
418
560
  // would send pixels to a model that cannot read them.
419
- inputModalities: live.isVl === true ? ['text' as const, 'image' as const] : ['text' as const],
561
+ inputModalities: vision ? ['text' as const, 'image' as const] : ['text' as const],
420
562
  context: {
421
563
  // The compaction engine and context meter price against the window a
422
564
  // request actually uses. Upstream reports three different numbers
@@ -425,11 +567,7 @@ export class QoderAdapter extends LlmAdapter {
425
567
  // then upstream's own session window. Reporting a window larger than
426
568
  // the session really runs with would push the auto-compaction
427
569
  // threshold past what the provider accepts.
428
- contextWindow: pickContextWindow(
429
- live,
430
- this.windowOverride(target),
431
- DEFAULT_CONTEXT_WINDOW,
432
- ),
570
+ contextWindow: this.declaredWindow(target, live),
433
571
  },
434
572
  defaultMaxTokens: live.maxOutputTokens ?? this.defaultMaxTokens,
435
573
  ...reasoning === undefined ? {} : { reasoning },
@@ -441,12 +579,17 @@ export class QoderAdapter extends LlmAdapter {
441
579
  ? QODER_MODELS.find(entry => entry.id === target)
442
580
  : undefined
443
581
  const reasoning = reasoningInfo(undefined, this.defaultReasoningEffort, true)
582
+ // The live catalog is momentarily unavailable. Reuse the last verified
583
+ // capability rather than declaring text-only, which would make the harness
584
+ // drop pixels from a vision model for this turn.
585
+ const rememberedVision = this.knownVision.get(target) === true
444
586
  return Promise.resolve({
445
587
  ...configured === undefined
446
- ? { provider, id: model, name: model, inputModalities: ['text' as const] }
588
+ ? { provider, id: model, name: model }
447
589
  : { ...modelInfo(provider, configured), id: model },
590
+ inputModalities: rememberedVision ? ['text' as const, 'image' as const] : ['text' as const],
448
591
  context: {
449
- contextWindow: this.windowOverride(target) ?? DEFAULT_CONTEXT_WINDOW,
592
+ contextWindow: this.declaredWindow(target, undefined),
450
593
  },
451
594
  defaultMaxTokens: this.defaultMaxTokens,
452
595
  ...reasoning === undefined ? {} : { reasoning },
@@ -475,7 +618,10 @@ export class QoderAdapter extends LlmAdapter {
475
618
  private async capture(provider: string, model: string, signal?: AbortSignal): Promise<CallGeneration> {
476
619
  const info = await this.resolveModel(provider, model, signal)
477
620
  const target = resolveQoderModelId(model, this.modelAliases)
478
- const live = (await this.catalog.liveModels()).find(entry => entry.value === target)
621
+ // Same cache-first rule as the resolution above: a dispatch may not stall
622
+ // on a catalogue fetch to learn window facts a snapshot already carries.
623
+ const live = (await cachedModels(this.catalog.snapshot(), () => this.catalog.liveModels()))
624
+ .find(entry => entry.value === target)
479
625
  const declared = info.context?.contextWindow
480
626
  return {
481
627
  info,
@@ -488,7 +634,7 @@ export class QoderAdapter extends LlmAdapter {
488
634
  const model = resolveQoderModelId(options.model, this.modelAliases)
489
635
  if (options.sessionId === undefined || options.purpose !== undefined) {
490
636
  // Side channels summarise text; pixels would only cost them context.
491
- const prompt = feedToText(renderInitialFeed(options.system, options.messages))
637
+ const prompt = feedToText(renderInitialFeed(options.system, options.messages, this.imagePath), this.imagePath)
492
638
  + '\n(这是一次性旁路请求,直接输出下一条助手回复。)'
493
639
  // Side channels (titles, compaction summaries) reuse the main session's
494
640
  // model so dsh's recorded summarization target matches what qodercli
@@ -534,7 +680,7 @@ export class QoderAdapter extends LlmAdapter {
534
680
  session.fedMessages = options.messages
535
681
  session.fedSystem = options.system
536
682
  const feed = plan.feed === null ? null : await this.buildFeed(plan.feed, generation.vision, images)
537
- yield* session.stream(options, feed, plan.feed === null ? undefined : feedToText(plan.feed))
683
+ yield* session.stream(options, feed, plan.feed === null ? undefined : feedToText(plan.feed, this.imagePath))
538
684
  }
539
685
 
540
686
  private async *firstTurn(
@@ -554,9 +700,9 @@ export class QoderAdapter extends LlmAdapter {
554
700
  session.recordRequestInput(options.system, options.messages)
555
701
  session.fedMessages = options.messages
556
702
  session.fedSystem = options.system
557
- const feed = renderInitialFeed(options.system, options.messages)
703
+ const feed = renderInitialFeed(options.system, options.messages, this.imagePath)
558
704
  const images = await this.resolveImages(feedImageRefs(feed), vision, options.signal)
559
- yield* session.stream(options, await this.buildFeed(feed, vision, images), feedToText(feed))
705
+ yield* session.stream(options, await this.buildFeed(feed, vision, images), feedToText(feed, this.imagePath))
560
706
  }
561
707
 
562
708
  /** Image references carried by a message tail, skipping assistant content. */
@@ -604,7 +750,7 @@ export class QoderAdapter extends LlmAdapter {
604
750
  images: ResolvedImages,
605
751
  ): Promise<string | ChannelContent[]> {
606
752
  if (typeof feed === 'string') return feed
607
- if (!vision) return feedToText(feed)
753
+ if (!vision) return feedToText(feed, this.imagePath)
608
754
  const content: ChannelContent[] = []
609
755
  let pixels = 0
610
756
  for (const part of feed) {
@@ -612,11 +758,20 @@ export class QoderAdapter extends LlmAdapter {
612
758
  if (part.text.length > 0) content.push({ type: 'text', text: part.text })
613
759
  continue
614
760
  }
615
- const resolved = images.get(String(part.attachment.attachmentId))
761
+ const key = String(part.attachment.attachmentId)
762
+ const resolved = images.get(key)
763
+ const path = this.imagePath?.(part.attachment)
616
764
  if (resolved === undefined) {
617
- content.push({ type: 'text', text: `[图片附件 ${String(part.attachment.attachmentId)} 本次未能读取,请基于文字内容继续]` })
765
+ content.push({
766
+ type: 'text',
767
+ text: `[图片附件 ${key} 本次未能读取${path === undefined ? '' : `;本机文件:${path}`},请基于文字内容继续]`,
768
+ })
618
769
  continue
619
770
  }
771
+ // The pixels let the model read the image; this handle lets it find and
772
+ // edit the source file. Without it a "modify this image" turn searches
773
+ // the workspace for a file that was never there.
774
+ content.push({ type: 'text', text: imageRefText(part.attachment, path) })
620
775
  pixels += 1
621
776
  content.push({
622
777
  type: 'image',
package/src/api.ts CHANGED
@@ -94,6 +94,9 @@ export async function buildRows(adapter: QoderAdapter): Promise<ModelRow[]> {
94
94
  ...override === undefined ? {} : { override },
95
95
  ...priceFactor === undefined ? {} : { priceFactor },
96
96
  reasoning: entry.isReasoning !== false,
97
+ // One advertised window is a fixed window: the CLI ignores anything
98
+ // else, so the page should show the value instead of a dead selector.
99
+ contextWindowSelectable: (entry.availableContextWindows?.length ?? 0) > 1,
97
100
  }
98
101
  })
99
102
  }
@@ -14,16 +14,16 @@ import type {
14
14
  GenerateOptions, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo,
15
15
  PreparedAdapterCall, RequestMessage, StreamChunk,
16
16
  } from '@deepseek-ai/dsh-llm'
17
- import { textOnlyImageText } from '@deepseek-ai/dsh-llm'
18
17
  import type { CodeBuddyRegion } from './brand.ts'
19
18
  import { codeBuddyColdStream, CodeBuddyAccountReader, CodeBuddyAuth, CodeBuddyCatalog, CodeBuddySessionManager, CODEBUDDY_DEFAULT_CONTEXT_WINDOW, CODEBUDDY_DEFAULT_MAX_TOKENS, CODEBUDDY_CACHE_TTL_MS, CODEBUDDY_FETCH_TIMEOUT_MS } from './codebuddy.ts'
20
19
  import type { CodeBuddyModel, CodeBuddyUsageSnapshot } from './codebuddy.ts'
21
20
  import { reasoningInfo, planContinuation } from './adapter.ts'
21
+ import { cachedModels } from './model-cache.ts'
22
22
  import type { ImageRequestReader } from './adapter.ts'
23
23
  import type { Live } from './live.ts'
24
24
  import { readLive, readLiveOptional } from './live.ts'
25
- import type { Feed } from './render.ts'
26
- import { feedImageRefs, feedToText, renderInitialFeed, resolvedFeedParts } from './render.ts'
25
+ import type { Feed, ImagePathResolver } from './render.ts'
26
+ import { feedImageRefs, feedToText, imageRefText, renderInitialFeed, resolvedFeedParts } from './render.ts'
27
27
  import type { ChannelContent } from './session.ts'
28
28
 
29
29
  /** The CodeBuddy provider route. */
@@ -50,6 +50,11 @@ export interface CodeBuddyAdapterOptions {
50
50
  * store) → images degrade to their handle text instead of failing the turn.
51
51
  */
52
52
  readImage?: ImageRequestReader
53
+ /**
54
+ * The tool-world path of one image, for the handle text that lets the model
55
+ * edit it. Absent → an unreadable image keeps the harness placeholder.
56
+ */
57
+ imagePath?: ImagePathResolver
53
58
  }
54
59
 
55
60
  const NO_MODELS: readonly string[] = []
@@ -84,6 +89,8 @@ export class CodeBuddyAdapter extends LlmAdapter {
84
89
  private readonly accounts: CodeBuddyAccountReader
85
90
  private readonly auth = new CodeBuddyAuth()
86
91
  private readonly disabledCache = new Map<CodeBuddyRegion, { key: string, set: ReadonlySet<string> }>()
92
+ /** Last verified image capability, keyed `${region}:${model}`. */
93
+ private readonly knownVision = new Map<string, boolean>()
87
94
 
88
95
  constructor(options: CodeBuddyAdapterOptions = {}) {
89
96
  super()
@@ -134,7 +141,9 @@ export class CodeBuddyAdapter extends LlmAdapter {
134
141
  /** The live catalog, unfiltered (the page must show switched-off models). */
135
142
  async describeCatalog(region: CodeBuddyRegion = this.region): Promise<readonly CodeBuddyModel[]> {
136
143
  if (!this.isSignedIn(region)) return []
137
- return this.catalog.models(region)
144
+ const models = await this.catalog.models(region)
145
+ for (const model of models) this.knownVision.set(`${region}:${model.id}`, model.vision)
146
+ return models
138
147
  }
139
148
 
140
149
  /** Whether the selected (or given) region has a stored login. */
@@ -190,8 +199,17 @@ export class CodeBuddyAdapter extends LlmAdapter {
190
199
  /** Whether the resolved model accepts images (drives whether pixels are sent). */
191
200
  private async modelVision(model: string): Promise<boolean> {
192
201
  const target = resolveCodeBuddyModelId(model, this.aliases)
193
- const live = (await this.catalog.models().catch(() => [])).find(entry => entry.id === target)
194
- return live?.vision === true
202
+ const key = `${this.region}:${target}`
203
+ const live = (await cachedModels(this.catalog.snapshot(), () => this.catalog.models()))
204
+ .find(entry => entry.id === target)
205
+ if (live !== undefined) {
206
+ this.knownVision.set(key, live.vision)
207
+ return live.vision
208
+ }
209
+ // A missed catalog read must not turn a vision model into a text-only one:
210
+ // DSH strips pixels before this adapter runs when the declared capability
211
+ // says text only.
212
+ return this.knownVision.get(key) === true
195
213
  }
196
214
 
197
215
  /**
@@ -237,7 +255,7 @@ export class CodeBuddyAdapter extends LlmAdapter {
237
255
  signal?: AbortSignal,
238
256
  ): Promise<string | ChannelContent[]> {
239
257
  if (typeof feed === 'string') return feed
240
- if (!vision) return feedToText(feed)
258
+ if (!vision) return feedToText(feed, this.options.imagePath)
241
259
  const images = await this.resolveFeedImages(feed, vision, signal)
242
260
  const parts = resolvedFeedParts(feed, images)
243
261
  if (!parts.some(part => part.type !== 'text')) return parts.map(part => part.type === 'text' ? part.text : '').join('\n\n')
@@ -247,10 +265,17 @@ export class CodeBuddyAdapter extends LlmAdapter {
247
265
  content.push({ type: 'text', text: part.text })
248
266
  continue
249
267
  }
250
- if (part.type === 'image-unreadable') {
251
- content.push({ type: 'text', text: textOnlyImageText(part.attachment) })
252
- continue
253
- }
268
+ // The pixels let the model read the image; this handle lets it find and
269
+ // edit the source file. Without it a "modify this image" turn searches
270
+ // the workspace for a file that was never there.
271
+ const path = this.options.imagePath?.(part.attachment)
272
+ content.push({
273
+ type: 'text',
274
+ text: part.type === 'image-unreadable' && path === undefined
275
+ ? `[图片附件 ${String(part.attachment.attachmentId)} 本次未能读取,请基于文字内容继续]`
276
+ : imageRefText(part.attachment, path),
277
+ })
278
+ if (part.type === 'image-unreadable') continue
254
279
  content.push({
255
280
  type: 'image',
256
281
  source: { type: 'base64', media_type: part.mediaType, data: part.data },
@@ -278,7 +303,7 @@ export class CodeBuddyAdapter extends LlmAdapter {
278
303
 
279
304
  /** The catalog snapshot the adapter last read (empty before the first read). */
280
305
  private models(region: CodeBuddyRegion): readonly CodeBuddyModel[] {
281
- return this.catalog.snapshot(region)
306
+ return this.catalog.snapshot(region) ?? []
282
307
  }
283
308
 
284
309
  /** Whether this model lets the user pick a context budget at all. */
@@ -297,9 +322,14 @@ export class CodeBuddyAdapter extends LlmAdapter {
297
322
 
298
323
  override async resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
299
324
  const target = resolveCodeBuddyModelId(model, this.aliases)
300
- const live = (await this.catalog.models()).find(entry => entry.id === target)
325
+ const key = `${this.region}:${target}`
326
+ // Cache first: a model switch must not wait on a catalogue fetch (which
327
+ // boots a CodeBuddy process) just because the TTL has expired.
328
+ const live = (await cachedModels(this.catalog.snapshot(), () => this.catalog.models()))
329
+ .find(entry => entry.id === target)
301
330
  if (live !== undefined) {
302
- const reasoning = reasoningInfo(live.reasoningEfforts, live.defaultEffort, (live.reasoningEfforts?.length ?? 0) > 0)
331
+ this.knownVision.set(key, live.vision)
332
+ const reasoning = reasoningInfo(live.reasoningEfforts, readLiveOptional(this.options.defaultReasoningEffort), (live.reasoningEfforts?.length ?? 0) > 0)
303
333
  return {
304
334
  provider,
305
335
  id: model,
@@ -320,7 +350,7 @@ export class CodeBuddyAdapter extends LlmAdapter {
320
350
  provider,
321
351
  id: model,
322
352
  name: model,
323
- inputModalities: ['text'],
353
+ inputModalities: this.knownVision.get(key) === true ? ['text', 'image'] : ['text'],
324
354
  context: { contextWindow: this.contextWindowFor(target) ?? CODEBUDDY_DEFAULT_CONTEXT_WINDOW },
325
355
  defaultMaxTokens: readLive(this.options.defaultMaxTokens, CODEBUDDY_DEFAULT_MAX_TOKENS),
326
356
  ...reasoning === undefined ? {} : { reasoning },
@@ -345,7 +375,7 @@ export class CodeBuddyAdapter extends LlmAdapter {
345
375
  }
346
376
  const model = resolveCodeBuddyModelId(options.model, this.aliases)
347
377
  if (options.sessionId === undefined || options.purpose !== undefined) {
348
- const prompt = feedToText(renderInitialFeed(options.system, options.messages))
378
+ const prompt = feedToText(renderInitialFeed(options.system, options.messages, this.options.imagePath), this.options.imagePath)
349
379
  + '\n(这是一次性旁路请求,直接输出下一条助手回复。)'
350
380
  for (const chunk of await codeBuddyColdStream(region, prompt, model, options)) yield chunk
351
381
  return
@@ -366,7 +396,9 @@ export class CodeBuddyAdapter extends LlmAdapter {
366
396
  yield* this.firstTurn(session, options, region, model, effort)
367
397
  return
368
398
  }
369
- session.configure(region, model, { effort, system: options.system, cwd: this.cwdOf(options) })
399
+ session.configure(region, model, {
400
+ effort, system: options.system, cwd: this.cwdOf(options), ...await this.windowOption(model),
401
+ })
370
402
  session.ensureTools(options.tools ?? [])
371
403
  session.fedMessages = options.messages
372
404
  session.fedSystem = options.system
@@ -384,19 +416,43 @@ export class CodeBuddyAdapter extends LlmAdapter {
384
416
  model: string,
385
417
  effort: string | undefined,
386
418
  ): AsyncGenerator<StreamChunk> {
387
- session.configure(region, model, { effort, system: options.system, cwd: this.cwdOf(options) })
419
+ session.configure(region, model, {
420
+ effort, system: options.system, cwd: this.cwdOf(options), ...await this.windowOption(model),
421
+ })
388
422
  session.ensureTools(options.tools ?? [])
389
423
  session.fedMessages = options.messages
390
424
  session.fedSystem = options.system
391
425
  const vision = await this.modelVision(options.model)
392
426
  const feed = await this.buildFeed(
393
- renderInitialFeed(undefined, options.messages),
427
+ renderInitialFeed(undefined, options.messages, this.options.imagePath),
394
428
  vision,
395
429
  options.signal,
396
430
  )
397
431
  yield* session.stream(options, feed)
398
432
  }
399
433
 
434
+ /**
435
+ * The context window the host and the inner session must agree on.
436
+ *
437
+ * Only a model that offers selectable budgets is configured: one fixed
438
+ * window rejects every value, so the CLI's own default already matches what
439
+ * the host shows. For a model with choices the adapter sends the picked
440
+ * value; returning to "default" sends the model's own default budget, since
441
+ * the CLI rejects a null clear (`contextWindow must be a positive safe
442
+ * integer`). The override lives for the life of the CLI process, so the
443
+ * default has to be sent explicitly to drop a previous choice.
444
+ */
445
+ private async windowOption(model: string): Promise<{ contextWindow?: number }> {
446
+ const live = (await this.catalog.models(this.region)).find(entry => entry.id === model)
447
+ if (live === undefined || live.contextWindows.length === 0) return {}
448
+ const chosen = this.options.modelContextWindows?.(this.region)[model]
449
+ return {
450
+ contextWindow: chosen !== undefined && live.contextWindows.includes(chosen)
451
+ ? chosen
452
+ : live.defaultContextWindow,
453
+ }
454
+ }
455
+
400
456
  private cwdOf(options: GenerateOptions): string | undefined {
401
457
  return (options as GenerateOptions & { cwd?: string }).cwd
402
458
  }