dsh-agent-sdk-bridge 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api.ts CHANGED
@@ -22,10 +22,12 @@
22
22
  import type { IncomingMessage, ServerResponse } from 'node:http'
23
23
  import type { QoderAdapter } from './adapter.ts'
24
24
  import type { CodeBuddyAdapter } from './codebuddy-adapter.ts'
25
- import type { BrandId, CodeBuddyRegion } from './brand.ts'
25
+ import type { MinimaxAdapter } from './minimax-adapter.ts'
26
+ import type { BrandId, CodeBuddyRegion, MinimaxRegion } from './brand.ts'
26
27
  import { isBrandId, isCodeBuddyRegion } from './brand.ts'
27
28
  import { priceFactorOf } from './models.ts'
28
29
  import { isSetupMode, readSetupStatus, startSetup } from './setup.ts'
30
+ import { isMinimaxSetupMode, readMinimaxSetupStatus, startMinimaxSetup } from './minimax-cli.ts'
29
31
 
30
32
  /** Route prefix this plugin owns; the client resolves it relative to the page. */
31
33
  export const API_PREFIX = '/api/llm-qoder-cn'
@@ -46,7 +48,7 @@ export interface ModelRow {
46
48
  }
47
49
 
48
50
  /** The adapters the API can reach, one per provider route. */
49
- export type AdapterMap = Readonly<Record<BrandId, QoderAdapter | CodeBuddyAdapter>>
51
+ export type AdapterMap = Readonly<Record<BrandId, QoderAdapter | CodeBuddyAdapter | MinimaxAdapter>>
50
52
 
51
53
  /** Answer one request with JSON. */
52
54
  function send(res: ServerResponse, status: number, payload: unknown): void {
@@ -116,6 +118,32 @@ export async function buildCodeBuddyRows(
116
118
  })
117
119
  }
118
120
 
121
+ /** Build model rows for the MiniMax adapter, for one login region. */
122
+ export async function buildMinimaxRows(
123
+ adapter: MinimaxAdapter,
124
+ region?: MinimaxRegion,
125
+ ): Promise<ModelRow[]> {
126
+ const live = await adapter.describeCatalog(region)
127
+ return live.map(entry => {
128
+ const override = adapter.contextWindowFor(entry.id, region)
129
+ const choices = new Set<number>(entry.contextWindows)
130
+ choices.add(entry.defaultContextWindow)
131
+ if (override !== undefined) choices.add(override)
132
+ return {
133
+ id: entry.id,
134
+ name: entry.name,
135
+ ...entry.description === undefined ? {} : { description: entry.description },
136
+ enabled: !adapter.isModelDisabled(entry.id, region),
137
+ vision: entry.vision,
138
+ contextWindow: override ?? entry.defaultContextWindow,
139
+ contextWindows: [...choices].sort((a, b) => a - b),
140
+ defaultContextWindow: entry.defaultContextWindow,
141
+ ...override === undefined ? {} : { override },
142
+ reasoning: entry.reasoning,
143
+ }
144
+ })
145
+ }
146
+
119
147
  /** Largest request body this route accepts. */
120
148
  const MAX_BODY_BYTES = 2048
121
149
 
@@ -159,31 +187,38 @@ export async function handleApi(
159
187
  const queryBrand = brandFrom(url.searchParams.get('brand'))
160
188
 
161
189
  if (queryBrand === undefined) {
162
- send(res, 400, { error: 'brand 只接受 cn、global 或 codebuddy' })
190
+ send(res, 400, { error: 'brand 只接受 cn、global、codebuddy 或 minimax' })
163
191
  return
164
192
  }
165
193
  const adapter = adapters[queryBrand]
166
194
  const codebuddy = queryBrand === 'codebuddy' ? adapter as CodeBuddyAdapter : undefined
195
+ const minimax = queryBrand === 'minimax' ? adapter as MinimaxAdapter : undefined
196
+ const regional = codebuddy !== undefined || minimax !== undefined
167
197
 
168
198
  if (path === '/models' && method === 'GET') {
169
199
  const requestedRegion = url.searchParams.get('region')
170
200
  const region = requestedRegion === null || requestedRegion === ''
171
201
  ? undefined
172
202
  : regionFrom(requestedRegion)
173
- if (codebuddy !== undefined && region === undefined && requestedRegion !== null && requestedRegion !== '') {
203
+ if (regional && region === undefined && requestedRegion !== null && requestedRegion !== '') {
174
204
  send(res, 400, { error: 'region 只接受 global 或 cn' })
175
205
  return
176
206
  }
177
207
  if (url.searchParams.get('fresh') === '1') {
178
208
  if (codebuddy !== undefined) codebuddy.invalidateCatalog(region)
209
+ else if (minimax !== undefined) minimax.invalidateCatalog(region)
179
210
  else adapter.invalidateCatalog()
180
211
  }
181
- const signedIn = codebuddy?.isSignedIn(region)
212
+ const signedIn = codebuddy?.isSignedIn(region) ?? minimax?.isSignedIn(region)
182
213
  const rows = codebuddy !== undefined
183
214
  ? signedIn === false ? [] : await buildCodeBuddyRows(codebuddy, region)
184
- : await buildRows(adapter as QoderAdapter)
215
+ : minimax !== undefined
216
+ ? signedIn === false ? [] : await buildMinimaxRows(minimax, region)
217
+ : await buildRows(adapter as QoderAdapter)
185
218
  const notice = rows.length === 0
186
- ? codebuddy === undefined ? adapter.catalogError() : codebuddy.catalogError(region)
219
+ ? codebuddy !== undefined
220
+ ? codebuddy.catalogError(region)
221
+ : minimax !== undefined ? minimax.catalogError(region) : adapter.catalogError()
187
222
  : undefined
188
223
  send(res, 200, {
189
224
  brand: queryBrand,
@@ -192,6 +227,7 @@ export async function handleApi(
192
227
  // the other login: a harmless extra field for the Qoder brands.
193
228
  ...region === undefined ? {} : { region },
194
229
  ...signedIn === undefined ? {} : { signedIn },
230
+ ...minimax === undefined ? {} : { regionBlocked: minimax.regionBlocked(region) },
195
231
  ...notice === undefined ? {} : { notice },
196
232
  })
197
233
  return
@@ -200,25 +236,30 @@ export async function handleApi(
200
236
  if (path === '/usage' && method === 'GET') {
201
237
  const requested = url.searchParams.get('region')
202
238
  const region = requested === null || requested === '' ? undefined : regionFrom(requested)
203
- if (codebuddy !== undefined && region === undefined && requested !== null && requested !== '') {
239
+ if (regional && region === undefined && requested !== null && requested !== '') {
204
240
  send(res, 400, { error: 'region 只接受 global 或 cn' })
205
241
  return
206
242
  }
243
+ const fresh = url.searchParams.get('fresh') === '1'
207
244
  const snapshot = codebuddy !== undefined
208
- ? await codebuddy.usageInfo(url.searchParams.get('fresh') === '1', region)
209
- : await adapter.usageInfo(url.searchParams.get('fresh') === '1')
245
+ ? await codebuddy.usageInfo(fresh, region)
246
+ : minimax !== undefined
247
+ ? await minimax.usageInfo(fresh, region)
248
+ : await adapter.usageInfo(fresh)
210
249
  send(res, 200, {
211
250
  brand: queryBrand,
212
251
  ...snapshot,
213
- ...codebuddy === undefined ? {} : { signedIn: codebuddy.isSignedIn(region) },
252
+ ...codebuddy === undefined && minimax === undefined
253
+ ? {}
254
+ : { signedIn: (codebuddy ?? minimax)!.isSignedIn(region) },
214
255
  })
215
256
  return
216
257
  }
217
258
 
218
259
  if (path === '/auth') {
219
260
  if (method === 'GET') {
220
- if (codebuddy === undefined) {
221
- send(res, 404, { error: '该 provider 没有 CodeBuddy 登录流程' })
261
+ if (codebuddy === undefined && minimax === undefined) {
262
+ send(res, 404, { error: '该 provider 没有浏览器登录流程' })
222
263
  return
223
264
  }
224
265
  const region = regionFrom(url.searchParams.get('region'))
@@ -226,7 +267,9 @@ export async function handleApi(
226
267
  send(res, 400, { error: 'region 只接受 global 或 cn' })
227
268
  return
228
269
  }
229
- send(res, 200, { brand: 'codebuddy', ...codebuddy.authStatus(region) })
270
+ send(res, 200, codebuddy !== undefined
271
+ ? { brand: 'codebuddy', ...codebuddy.authStatus(region) }
272
+ : { brand: 'minimax', ...minimax!.authStatus(region) })
230
273
  return
231
274
  }
232
275
  if (method === 'POST') {
@@ -239,46 +282,64 @@ export async function handleApi(
239
282
  }
240
283
  // A POST selects its provider in the body: the page never puts the
241
284
  // brand in the URL for writes.
242
- if (brandFrom(body.brand) !== 'codebuddy') {
243
- send(res, 400, { error: '该 provider 没有 CodeBuddy 登录流程' })
285
+ const postBrand = brandFrom(body.brand)
286
+ if (postBrand !== 'codebuddy' && postBrand !== 'minimax') {
287
+ send(res, 400, { error: '该 provider 没有浏览器登录流程' })
244
288
  return
245
289
  }
246
- const cb = adapters.codebuddy as CodeBuddyAdapter
247
290
  const region = regionFrom(body.region)
248
291
  if (region === undefined) {
249
292
  send(res, 400, { error: 'region 只接受 global 或 cn' })
250
293
  return
251
294
  }
252
- if (body.action === 'logout') {
253
- const result = await cb.logout(region)
254
- if (!result.ok) {
255
- send(res, 500, { error: result.error })
295
+ if (body.action !== 'login' && body.action !== 'logout') {
296
+ send(res, 400, { error: 'action 只接受 login 或 logout' })
297
+ return
298
+ }
299
+ if (postBrand === 'codebuddy') {
300
+ const cb = adapters.codebuddy as CodeBuddyAdapter
301
+ if (body.action === 'logout') {
302
+ const result = await cb.logout(region)
303
+ if (!result.ok) {
304
+ send(res, 500, { error: result.error })
305
+ return
306
+ }
307
+ send(res, 200, { brand: 'codebuddy', ...cb.authStatus(region) })
308
+ return
309
+ }
310
+ const started = cb.startLogin(region)
311
+ if (started.started === false) {
312
+ send(res, 409, { error: started.error })
256
313
  return
257
314
  }
258
315
  send(res, 200, { brand: 'codebuddy', ...cb.authStatus(region) })
259
316
  return
260
317
  }
261
- if (body.action !== 'login') {
262
- send(res, 400, { error: 'action 只接受 login 或 logout' })
318
+ const mm = adapters.minimax as MinimaxAdapter
319
+ if (body.action === 'logout') {
320
+ send(res, 200, { brand: 'minimax', ...await mm.logout(region) })
263
321
  return
264
322
  }
265
- const started = cb.startLogin(region)
323
+ const started = mm.startLogin(region)
266
324
  if (started.started === false) {
267
325
  send(res, 409, { error: started.error })
268
326
  return
269
327
  }
270
- send(res, 200, { brand: 'codebuddy', ...cb.authStatus(region) })
328
+ send(res, 200, { brand: 'minimax', ...mm.authStatus(region) })
271
329
  return
272
330
  }
273
331
  }
274
332
 
275
333
  if (path === '/setup') {
276
- if (codebuddy !== undefined) {
277
- send(res, 400, { error: 'CodeBuddy 不需要安装 CLI,请在页面中登录' })
278
- return
279
- }
280
- const qoder = adapter as QoderAdapter
281
334
  if (method === 'GET') {
335
+ if (codebuddy !== undefined) {
336
+ send(res, 400, { error: 'CodeBuddy 不需要安装 CLI,请在页面中登录' })
337
+ return
338
+ }
339
+ if (minimax !== undefined) {
340
+ send(res, 200, readMinimaxSetupStatus())
341
+ return
342
+ }
282
343
  send(res, 200, readSetupStatus(queryBrand as 'cn' | 'global'))
283
344
  return
284
345
  }
@@ -290,15 +351,40 @@ export async function handleApi(
290
351
  send(res, 400, { error: `请求体无法解析:${error instanceof Error ? error.message : String(error)}` })
291
352
  return
292
353
  }
354
+ // A POST selects its provider in the body; the query string may name
355
+ // none at all, which is why this cannot reuse the GET's brand.
293
356
  const postBrand = brandFrom(parsed.brand)
294
- if (postBrand === undefined || postBrand === 'codebuddy') {
295
- send(res, 400, { error: '该 brand 没有 CLI 安装脚本' })
357
+ if (postBrand === undefined) {
358
+ send(res, 400, { error: 'brand 只接受 cn、global、codebuddy 或 minimax' })
359
+ return
360
+ }
361
+ if (postBrand === 'codebuddy') {
362
+ send(res, 400, { error: 'CodeBuddy 不需要安装 CLI,请在页面中登录' })
363
+ return
364
+ }
365
+ if (postBrand === 'minimax') {
366
+ if (!isMinimaxSetupMode(parsed.mode)) {
367
+ send(res, 400, { error: 'mode 只接受 install 或 uninstall' })
368
+ return
369
+ }
370
+ const mm = adapters.minimax as MinimaxAdapter
371
+ const result = startMinimaxSetup(parsed.mode, code => {
372
+ if (code !== 0) return
373
+ mm.invalidateCatalog()
374
+ mm.invalidateUsage()
375
+ })
376
+ if (!result.started) {
377
+ send(res, result.reason === 'busy' ? 409 : 501, { error: result.error })
378
+ return
379
+ }
380
+ send(res, 200, readMinimaxSetupStatus())
296
381
  return
297
382
  }
298
383
  if (!isSetupMode(parsed.mode)) {
299
384
  send(res, 400, { error: 'mode 只接受 configure、login 或 uninstall' })
300
385
  return
301
386
  }
387
+ const qoder = adapters[postBrand] as QoderAdapter
302
388
  const result = startSetup(postBrand, parsed.mode, code => {
303
389
  if (code === 0) {
304
390
  qoder.invalidateCatalog()
@@ -313,6 +399,8 @@ export async function handleApi(
313
399
  send(res, 200, readSetupStatus(postBrand))
314
400
  return
315
401
  }
402
+ send(res, 405, { error: '不支持的写入路径' })
403
+ return
316
404
  }
317
405
 
318
406
  send(res, method === 'GET' ? 404 : 405, { error: method === 'GET' ? 'not found' : '不支持的写入路径' })
package/src/brand.ts CHANGED
@@ -14,15 +14,20 @@
14
14
  export type QoderBrand = 'cn' | 'global'
15
15
  /** The CodeBuddy integration. Its login region is a config choice, not a brand. */
16
16
  export type CodeBuddyBrandId = 'codebuddy'
17
+ /** The MiniMax Code integration, spoken over ACP. */
18
+ export type MinimaxBrandId = 'minimax'
17
19
  /** Every provider route this plugin registers. */
18
- export type BrandId = QoderBrand | CodeBuddyBrandId
20
+ export type BrandId = QoderBrand | CodeBuddyBrandId | MinimaxBrandId
19
21
 
20
22
  /** Which SDK family a brand belongs to. */
21
- export type BrandFamily = 'qoder' | 'codebuddy'
23
+ export type BrandFamily = 'qoder' | 'codebuddy' | 'minimax'
22
24
 
23
25
  /** CodeBuddy's two public login regions. Enterprise domains are out of scope. */
24
26
  export type CodeBuddyRegion = 'global' | 'cn'
25
27
 
28
+ /** MiniMax Code's two public login regions. */
29
+ export type MinimaxRegion = 'global' | 'cn'
30
+
26
31
  /** Everything that differs between provider brands. */
27
32
  export interface BrandProfile {
28
33
  id: BrandId
@@ -32,7 +37,7 @@ export interface BrandProfile {
32
37
  /** Display name used when the config leaves it empty. */
33
38
  defaultDisplayName: string
34
39
  /** Config field that switches this brand on or off. */
35
- enabledKey: 'cnEnabled' | 'intlEnabled' | 'codebuddyEnabled'
40
+ enabledKey: 'cnEnabled' | 'intlEnabled' | 'codebuddyEnabled' | 'minimaxEnabled'
36
41
  /** SDK package that speaks this brand's protocol. */
37
42
  sdkPackage: string
38
43
  /** npm packages the CLI can be installed as, most specific first. */
@@ -105,8 +110,30 @@ export const CODEBUDDY_BRAND: BrandProfile = {
105
110
  staticCatalog: false,
106
111
  }
107
112
 
113
+ /**
114
+ * MiniMax Code, spoken over the Agent Client Protocol.
115
+ *
116
+ * `mcode acp` runs as a stdio ACP server. Unlike CodeBuddy, there is no SDK
117
+ * package to import: the plugin speaks the protocol directly and the CLI owns
118
+ * login, model selection and the upstream account. The login region is a
119
+ * config choice (`MAVIS_REGION` pins each process), not a brand.
120
+ */
121
+ export const MINIMAX_BRAND: BrandProfile = {
122
+ id: 'minimax',
123
+ family: 'minimax',
124
+ provider: 'minimax-acp',
125
+ defaultDisplayName: 'MiniMax Code',
126
+ enabledKey: 'minimaxEnabled',
127
+ sdkPackage: '@agentclientprotocol/sdk',
128
+ cliPackages: [{ scope: '@minimax-ai', name: 'code' }],
129
+ cliCommands: ['mcode'],
130
+ configDirName: '.minimax',
131
+ setupScript: null,
132
+ staticCatalog: false,
133
+ }
134
+
108
135
  /** Every brand, in the order the settings pages list them. */
109
- export const BRANDS: readonly BrandProfile[] = [CN_BRAND, GLOBAL_BRAND, CODEBUDDY_BRAND]
136
+ export const BRANDS: readonly BrandProfile[] = [CN_BRAND, GLOBAL_BRAND, CODEBUDDY_BRAND, MINIMAX_BRAND]
110
137
 
111
138
  /** Whether a value names a Qoder edition. */
112
139
  export function isQoderBrand(value: unknown): value is QoderBrand {
@@ -115,7 +142,7 @@ export function isQoderBrand(value: unknown): value is QoderBrand {
115
142
 
116
143
  /** Whether a value names one of the provider brands. */
117
144
  export function isBrandId(value: unknown): value is BrandId {
118
- return isQoderBrand(value) || value === 'codebuddy'
145
+ return isQoderBrand(value) || value === 'codebuddy' || value === 'minimax'
119
146
  }
120
147
 
121
148
  /** Whether a value names a CodeBuddy login region. */
@@ -123,12 +150,29 @@ export function isCodeBuddyRegion(value: unknown): value is CodeBuddyRegion {
123
150
  return value === 'global' || value === 'cn'
124
151
  }
125
152
 
153
+ /** Whether a value names a MiniMax Code login region. */
154
+ export function isMinimaxRegion(value: unknown): value is MinimaxRegion {
155
+ return value === 'global' || value === 'cn'
156
+ }
157
+
126
158
  /** The profile for one brand. */
127
159
  export function brandProfile(brand: BrandId): BrandProfile {
128
160
  if (brand === 'codebuddy') return CODEBUDDY_BRAND
161
+ if (brand === 'minimax') return MINIMAX_BRAND
129
162
  return brand === 'global' ? GLOBAL_BRAND : CN_BRAND
130
163
  }
131
164
 
165
+ /**
166
+ * The region name the MiniMax CLI uses internally.
167
+ *
168
+ * The CLI calls the mainland site `cn` and the international one `en`; its
169
+ * login command accepts `global` and maps it to `en`. Processes are pinned
170
+ * with `MAVIS_REGION`, which also expects `cn`/`en`.
171
+ */
172
+ export function minimaxCliRegion(region: MinimaxRegion): 'cn' | 'en' {
173
+ return region === 'cn' ? 'cn' : 'en'
174
+ }
175
+
132
176
  /** The SDK environment a CodeBuddy region logs into. */
133
177
  export function codeBuddyEnvironment(region: CodeBuddyRegion): 'external' | 'internal' {
134
178
  return region === 'cn' ? 'internal' : 'external'
@@ -14,13 +14,17 @@ 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'
17
18
  import type { CodeBuddyRegion } from './brand.ts'
18
19
  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'
19
20
  import type { CodeBuddyModel, CodeBuddyUsageSnapshot } from './codebuddy.ts'
20
21
  import { reasoningInfo, planContinuation } from './adapter.ts'
22
+ import type { ImageRequestReader } from './adapter.ts'
21
23
  import type { Live } from './live.ts'
22
24
  import { readLive, readLiveOptional } from './live.ts'
23
- import { feedToText, renderInitialFeed } from './render.ts'
25
+ import type { Feed } from './render.ts'
26
+ import { feedImageRefs, feedToText, renderInitialFeed, resolvedFeedParts } from './render.ts'
27
+ import type { ChannelContent } from './session.ts'
24
28
 
25
29
  /** The CodeBuddy provider route. */
26
30
  export const CODEBUDDY_PROVIDER = 'codebuddy'
@@ -41,6 +45,11 @@ export interface CodeBuddyAdapterOptions {
41
45
  /** Per-region context-window overrides, keyed by model id. */
42
46
  modelContextWindows?: (region: CodeBuddyRegion) => Readonly<Record<string, number>>
43
47
  modelAliases?: Live<Readonly<Record<string, string>>>
48
+ /**
49
+ * Reads image attachment bytes. Absent (a deployment with no attachment
50
+ * store) → images degrade to their handle text instead of failing the turn.
51
+ */
52
+ readImage?: ImageRequestReader
44
53
  }
45
54
 
46
55
  const NO_MODELS: readonly string[] = []
@@ -178,6 +187,78 @@ export class CodeBuddyAdapter extends LlmAdapter {
178
187
  return this.disabledFor(region).has(id)
179
188
  }
180
189
 
190
+ /** Whether the resolved model accepts images (drives whether pixels are sent). */
191
+ private async modelVision(model: string): Promise<boolean> {
192
+ const target = resolveCodeBuddyModelId(model, this.aliases)
193
+ const live = (await this.catalog.models().catch(() => [])).find(entry => entry.id === target)
194
+ return live?.vision === true
195
+ }
196
+
197
+ /**
198
+ * Resolve a feed's images into base64 for the CLI's vision blocks.
199
+ *
200
+ * Only references the host can actually read become pixels; the rest keep
201
+ * their handle text, so one unreadable attachment degrades that part of the
202
+ * prompt instead of failing the whole turn.
203
+ */
204
+ private async resolveFeedImages(
205
+ feed: Feed,
206
+ vision: boolean,
207
+ signal?: AbortSignal,
208
+ ): Promise<ReadonlyMap<string, { data: string, mediaType: string }>> {
209
+ const map = new Map<string, { data: string, mediaType: string }>()
210
+ const readImage = this.options.readImage
211
+ if (!vision || readImage === undefined) return map
212
+ for (const ref of feedImageRefs(feed)) {
213
+ const key = String(ref.attachmentId)
214
+ if (map.has(key)) continue
215
+ try {
216
+ const image = await readImage(ref, signal)
217
+ if (image !== undefined) {
218
+ map.set(key, { data: Buffer.from(image.bytes).toString('base64'), mediaType: image.mediaType })
219
+ }
220
+ } catch {
221
+ // Degrade this attachment to its handle text.
222
+ }
223
+ }
224
+ return map
225
+ }
226
+
227
+ /**
228
+ * Turn a feed into what one CLI turn receives.
229
+ *
230
+ * A text-only feed stays a plain string (the historical shape). A feed with
231
+ * readable images becomes the SDK's `ContentBlock[]`, because that is the
232
+ * only shape the CLI sends to the model as pixels.
233
+ */
234
+ private async buildFeed(
235
+ feed: Feed,
236
+ vision: boolean,
237
+ signal?: AbortSignal,
238
+ ): Promise<string | ChannelContent[]> {
239
+ if (typeof feed === 'string') return feed
240
+ if (!vision) return feedToText(feed)
241
+ const images = await this.resolveFeedImages(feed, vision, signal)
242
+ const parts = resolvedFeedParts(feed, images)
243
+ if (!parts.some(part => part.type !== 'text')) return parts.map(part => part.type === 'text' ? part.text : '').join('\n\n')
244
+ const content: ChannelContent[] = []
245
+ for (const part of parts) {
246
+ if (part.type === 'text') {
247
+ content.push({ type: 'text', text: part.text })
248
+ continue
249
+ }
250
+ if (part.type === 'image-unreadable') {
251
+ content.push({ type: 'text', text: textOnlyImageText(part.attachment) })
252
+ continue
253
+ }
254
+ content.push({
255
+ type: 'image',
256
+ source: { type: 'base64', media_type: part.mediaType, data: part.data },
257
+ })
258
+ }
259
+ return content
260
+ }
261
+
181
262
  contextWindowFor(id: string, region: CodeBuddyRegion = this.region): number | undefined {
182
263
  return this.options.modelContextWindows?.(region)[id]
183
264
  }
@@ -265,7 +346,10 @@ export class CodeBuddyAdapter extends LlmAdapter {
265
346
  session.ensureTools(options.tools ?? [])
266
347
  session.fedMessages = options.messages
267
348
  session.fedSystem = options.system
268
- const feed = plan.feed === null ? null : feedToText(plan.feed)
349
+ const vision = await this.modelVision(options.model)
350
+ const feed = plan.feed === null
351
+ ? null
352
+ : await this.buildFeed(plan.feed, vision, options.signal)
269
353
  yield* session.stream(options, feed)
270
354
  }
271
355
 
@@ -280,7 +364,12 @@ export class CodeBuddyAdapter extends LlmAdapter {
280
364
  session.ensureTools(options.tools ?? [])
281
365
  session.fedMessages = options.messages
282
366
  session.fedSystem = options.system
283
- const feed = feedToText(renderInitialFeed(undefined, options.messages))
367
+ const vision = await this.modelVision(options.model)
368
+ const feed = await this.buildFeed(
369
+ renderInitialFeed(undefined, options.messages),
370
+ vision,
371
+ options.signal,
372
+ )
284
373
  yield* session.stream(options, feed)
285
374
  }
286
375
 
package/src/codebuddy.ts CHANGED
@@ -27,7 +27,37 @@ import { jsonSchemaToShape } from './jsonschema.ts'
27
27
  import type { Live } from './live.ts'
28
28
  import { readLive } from './live.ts'
29
29
  import { codeBuddyEnvironment, loadCodeBuddySdk } from './sdk.ts'
30
- import type { ToolResultPayload } from './session.ts'
30
+ import type { ChannelContent, ToolResultPayload } from './session.ts'
31
+
32
+ /**
33
+ * Literal characters in a channel payload; images contribute pixels, not text.
34
+ *
35
+ * The per-turn input estimate charges text only, matching how the Qoder path
36
+ * counts a feed, so a picture cannot inflate the reported prompt size.
37
+ */
38
+ function contentChars(content: readonly ChannelContent[]): number {
39
+ return content.reduce((total, block) => total + (block.type === 'text' ? block.text.length : 0), 0)
40
+ }
41
+
42
+ /**
43
+ * Wrap one turn's content the way the SDK's `send()` expects it.
44
+ *
45
+ * A plain string keeps the historical shape; image turns are wrapped in a
46
+ * `UserMessage`, because a bare `ContentBlock[]` is not accepted and dropping
47
+ * the wrapper silently loses the picture.
48
+ */
49
+ function toCodeBuddyUserMessage(
50
+ sessionId: string,
51
+ content: string | ChannelContent[],
52
+ ): string | UserMessage {
53
+ if (typeof content === 'string') return content
54
+ return {
55
+ type: 'user',
56
+ session_id: sessionId,
57
+ message: { role: 'user', content: content as never },
58
+ parent_tool_use_id: null,
59
+ }
60
+ }
31
61
  import { MCP_SERVER_NAME, ToolResultPairing, TurnQueue } from './session.ts'
32
62
  import { TurnOutputAccounting, estimateTextTokens } from './tokens.ts'
33
63
 
@@ -911,8 +941,8 @@ export class CodeBuddySession {
911
941
  this.pairing.beginHostRequest(delivered, freshUserTurn)
912
942
  }
913
943
 
914
- /** Run one host turn; `feed` is new user text, or null for a tool continuation. */
915
- async *stream(options: GenerateOptions, feed: string | null): AsyncGenerator<StreamChunk> {
944
+ /** Run one host turn; `feed` is new user content, or null for a tool continuation. */
945
+ async *stream(options: GenerateOptions, feed: string | ChannelContent[] | null): AsyncGenerator<StreamChunk> {
916
946
  if (this.queue !== null) throw new Error(`CodeBuddy session ${this.sessionId} already has a turn in flight`)
917
947
  if (this.disposed) throw new Error(`CodeBuddy session ${this.sessionId} was disposed`)
918
948
  const session = await this.ensureStarted()
@@ -931,8 +961,13 @@ export class CodeBuddySession {
931
961
  // keeps the next prompt on the model the host just resolved.
932
962
  await session.setModel(this.model)
933
963
  if (feed !== null && feed.length > 0) {
934
- this.inputChars += feed.length
935
- await session.send(feed)
964
+ const content = typeof feed === 'string'
965
+ ? feed
966
+ : feed.filter(block => block.type !== 'text' || block.text.length > 0)
967
+ if (content.length > 0) {
968
+ this.inputChars += typeof content === 'string' ? content.length : contentChars(content)
969
+ await session.send(toCodeBuddyUserMessage(this.sessionId, content))
970
+ }
936
971
  }
937
972
  void this.consume(session)
938
973
  for await (const item of this.queue) {