dsh-opendesign-corouter 0.1.12 → 0.1.14

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/lib/index.js CHANGED
@@ -57,10 +57,12 @@ import { LlmError as LlmError13 } from "@deepseek-ai/dsh-llm";
57
57
  // src/adapter.ts
58
58
  import { createHmac, randomBytes } from "node:crypto";
59
59
  import {
60
+ IMAGE_OFFLOAD_REQUIRED_CODE,
60
61
  LlmAdapter,
61
62
  LlmError as LlmError11,
62
63
  ReasoningEffortId,
63
- assertUsableApiKey
64
+ assertUsableApiKey,
65
+ requiredImageOffload
64
66
  } from "@deepseek-ai/dsh-llm";
65
67
 
66
68
  // src/catalog.ts
@@ -75,7 +77,7 @@ var EFFORTS_GROK = [...EFFORTS_LEVELS, "xhigh"];
75
77
  var EFFORTS_MUSE = ["minimal", ...EFFORTS_REASONING];
76
78
  var MODELS = [
77
79
  // ── OpenAI(8) ─────────────────────────────────────────────
78
- { id: "gpt-6.1-sol", name: "GPT-6.1 Sol", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
80
+ { id: "gpt-6.1-sol", name: "GPT-6.1 Sol", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
79
81
  {
80
82
  id: "gpt-6.1-terra",
81
83
  name: "GPT-6.1 Terra",
@@ -85,7 +87,7 @@ var MODELS = [
85
87
  inputModalities: TEXT_AND_IMAGE,
86
88
  reasoningEfforts: EFFORTS_OPENAI
87
89
  },
88
- { id: "gpt-6-astra", name: "GPT-6 Astra", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
90
+ { id: "gpt-6-astra", name: "GPT-6 Astra", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
89
91
  {
90
92
  id: "gpt-6-luna",
91
93
  name: "GPT-6 Luna",
@@ -95,18 +97,18 @@ var MODELS = [
95
97
  inputModalities: TEXT_AND_IMAGE,
96
98
  reasoningEfforts: EFFORTS_OPENAI
97
99
  },
98
- { id: "gpt-6-sol", name: "GPT-6 Sol", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
99
- { id: "gpt-5.6-luna", name: "GPT-5.6 Luna", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
100
- { id: "gpt-5.6-sol", name: "GPT-5.6 Sol", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
101
- { id: "gpt-5.6-terra", name: "GPT-5.6 Terra", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
100
+ { id: "gpt-6-sol", name: "GPT-6 Sol", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
101
+ { id: "gpt-5.6-luna", name: "GPT-5.6 Luna", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
102
+ { id: "gpt-5.6-sol", name: "GPT-5.6 Sol", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
103
+ { id: "gpt-5.6-terra", name: "GPT-5.6 Terra", group: "paid", contextWindow: 105e4, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_OPENAI },
102
104
  // ── Anthropic(8) ──────────────────────────────────────────
103
- { id: "claude-fable-5.1", name: "Claude Fable 5.1", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
104
- { id: "claude-fable-5", name: "Claude Fable 5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
105
- { id: "claude-opus-5.5", name: "Claude Opus 5.5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
106
- { id: "claude-opus-5", name: "Claude Opus 5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
107
- { id: "claude-opus-4.8", name: "Claude Opus 4.8", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
108
- { id: "claude-sonnet-5.5", name: "Claude Sonnet 5.5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
109
- { id: "claude-sonnet-5", name: "Claude Sonnet 5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
105
+ { id: "claude-fable-5.1", name: "Claude Fable 5.1", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
106
+ { id: "claude-fable-5", name: "Claude Fable 5", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
107
+ { id: "claude-opus-5.5", name: "Claude Opus 5.5", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
108
+ { id: "claude-opus-5", name: "Claude Opus 5", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
109
+ { id: "claude-opus-4.8", name: "Claude Opus 4.8", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
110
+ { id: "claude-sonnet-5.5", name: "Claude Sonnet 5.5", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
111
+ { id: "claude-sonnet-5", name: "Claude Sonnet 5", group: "paid", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
110
112
  {
111
113
  id: "claude-haiku-5.5",
112
114
  name: "Claude Haiku 5.5",
@@ -171,7 +173,7 @@ var MODELS = [
171
173
  inputModalities: TEXT_AND_IMAGE,
172
174
  reasoningEfforts: EFFORTS_LOW_HIGH
173
175
  },
174
- { id: "glm-5.2", name: "GLM-5.2", group: "paid", inputModalities: TEXT_ONLY, reasoningEfforts: EFFORTS_GLM52 },
176
+ { id: "glm-5.2", name: "GLM-5.2", group: "paid", contextWindow: 1e6, inputModalities: TEXT_ONLY, reasoningEfforts: EFFORTS_GLM52 },
175
177
  // ── Moonshot(3) ───────────────────────────────────────────
176
178
  {
177
179
  id: "kimi-k2.7-code",
@@ -182,16 +184,16 @@ var MODELS = [
182
184
  inputModalities: TEXT_AND_IMAGE,
183
185
  reasoningEfforts: []
184
186
  },
185
- { id: "kimi-k3", name: "Kimi K3", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LOW_HIGH },
186
- { id: "kimi-k3.1-pro", name: "Kimi K3.1 Pro", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LOW_HIGH },
187
+ { id: "kimi-k3", name: "Kimi K3", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LOW_HIGH },
188
+ { id: "kimi-k3.1-pro", name: "Kimi K3.1 Pro", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LOW_HIGH },
187
189
  // ── Google(3) ─────────────────────────────────────────────
188
- { id: "gemini-4-argon", name: "Gemini 4 Argon", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
189
- { id: "gemini-3.8-flash", name: "Gemini 3.8 Flash", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
190
- { id: "gemini-3.7-flash", name: "Gemini 3.7 Flash", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
190
+ { id: "gemini-4-argon", name: "Gemini 4 Argon", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
191
+ { id: "gemini-3.8-flash", name: "Gemini 3.8 Flash", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
192
+ { id: "gemini-3.7-flash", name: "Gemini 3.7 Flash", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
191
193
  // ── xAI(3) ────────────────────────────────────────────────
192
- { id: "grok-4.7", name: "Grok 4.7", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_GROK },
193
- { id: "grok-4.6", name: "Grok 4.6", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_GROK },
194
- { id: "grok-4.5", name: "Grok 4.5", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
194
+ { id: "grok-4.7", name: "Grok 4.7", group: "paid", contextWindow: 5e5, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_GROK },
195
+ { id: "grok-4.6", name: "Grok 4.6", group: "paid", contextWindow: 5e5, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_GROK },
196
+ { id: "grok-4.5", name: "Grok 4.5", group: "paid", contextWindow: 5e5, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_LEVELS },
195
197
  // ── Xiaomi(2) ─────────────────────────────────────────────
196
198
  {
197
199
  id: "mimo-v2.6-pro",
@@ -222,11 +224,11 @@ var MODELS = [
222
224
  reasoningEfforts: ["none", "high"]
223
225
  },
224
226
  // ── Alibaba(1) ────────────────────────────────────────────
225
- { id: "qwen3.7-max", name: "Qwen3.7 Max", group: "paid", inputModalities: TEXT_ONLY, reasoningEfforts: [] },
227
+ { id: "qwen3.7-max", name: "Qwen3.7 Max", group: "paid", contextWindow: 1e6, inputModalities: TEXT_ONLY, reasoningEfforts: [] },
226
228
  // Official API additions; display labels for undocumented entries are local formatting.
227
- { id: "minimax-m3.1-flash", name: "MiniMax M3.1 Flash", group: "plan", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
228
- { id: "muse-spark-1.3", name: "Muse Spark 1.3", group: "paid", inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_MUSE },
229
- { id: "muse-wonder", name: "Muse Wonder", group: "paid" }
229
+ { id: "minimax-m3.1-flash", name: "MiniMax M3.1 Flash", group: "plan", contextWindow: 1e6, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_REASONING },
230
+ { id: "muse-spark-1.3", name: "Muse Spark 1.3", group: "paid", contextWindow: 1048576, inputModalities: TEXT_AND_IMAGE, reasoningEfforts: EFFORTS_MUSE },
231
+ { id: "muse-wonder", name: "Muse Wonder", group: "paid", contextWindow: 1048576 }
230
232
  ];
231
233
  var BY_ID = new Map(MODELS.map((model) => [model.id, model]));
232
234
  function findModel(id) {
@@ -322,6 +324,11 @@ function serializableTransform(callback) {
322
324
  });
323
325
  }
324
326
 
327
+ // src/image-budget.ts
328
+ var MIN_REQUEST_IMAGE_BYTES = 1024 * 1024;
329
+ var DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
330
+ var MAX_REQUEST_IMAGE_BYTES = 100 * 1024 * 1024;
331
+
325
332
  // src/config.ts
326
333
  var SETTINGS_NS = "llm-opendesign-corouter";
327
334
  var PLAN_PROVIDER = "opendesign-corouter-plan";
@@ -336,7 +343,8 @@ var Config = Schema.object({
336
343
  emptyTurnGuard: Schema.boolean().default(true).description("\u68C0\u6D4B DeepSeek V4.1 Flash \u5DE5\u5177\u5BF9\u8BDD\u7684\u7A7A\u56DE\u7B54\u548C\u5F02\u5E38\u7ED3\u675F\uFF1B\u6301\u7EED\u91CD\u8BD5\u76F4\u5230\u6210\u529F\u6216\u53D6\u6D88\uFF0C\u53EF\u80FD\u8BEF\u5224\u5E76\u989D\u5916\u6D88\u8017\u989D\u5EA6").volatile(),
337
344
  quotaRefreshSeconds: Schema.natural().min(5).max(3600).default(60).description("\u989D\u5EA6\u81EA\u52A8\u5237\u65B0\u95F4\u9694\uFF08\u79D2\uFF09").volatile(),
338
345
  requestTimeoutMs: Schema.natural().min(1e3).max(36e5).default(18e5).description("\u63A8\u7406\u8BF7\u6C42\u603B\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09").volatile(),
339
- streamIdleTimeoutMs: Schema.natural().min(1e3).max(18e5).default(3e5).description("\u6D41\u5F0F\u54CD\u5E94\u65E0\u6570\u636E\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09").volatile()
346
+ streamIdleTimeoutMs: Schema.natural().min(1e3).max(18e5).default(3e5).description("\u6D41\u5F0F\u54CD\u5E94\u65E0\u6570\u636E\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09").volatile(),
347
+ maxRequestImageBytes: Schema.natural().min(MIN_REQUEST_IMAGE_BYTES).max(MAX_REQUEST_IMAGE_BYTES).default(DEFAULT_MAX_REQUEST_IMAGE_BYTES).description("\u5355\u6B21\u8BF7\u6C42\u7684\u5185\u8054\u56FE\u7247\u9884\u7B97\uFF08\u5B57\u8282\uFF09").volatile()
340
348
  });
341
349
  function assertBaseURL(raw) {
342
350
  if (raw.length > 2048) throw new Error("\u63A8\u7406 Base URL \u6700\u591A\u5141\u8BB8 2048 \u5B57\u7B26");
@@ -362,7 +370,8 @@ function readConfig(config) {
362
370
  useDesktopCredential: config.useDesktopCredential.get(),
363
371
  emptyTurnGuard: config.emptyTurnGuard.get(),
364
372
  requestTimeoutMs: config.requestTimeoutMs.get(),
365
- streamIdleTimeoutMs: config.streamIdleTimeoutMs.get()
373
+ streamIdleTimeoutMs: config.streamIdleTimeoutMs.get(),
374
+ maxRequestImageBytes: config.maxRequestImageBytes.get()
366
375
  };
367
376
  }
368
377
 
@@ -516,6 +525,7 @@ var MESSAGES = {
516
525
  RATE_LIMIT: "OpenDesign\uFF1A\u8BF7\u6C42\u8FC7\u4E8E\u9891\u7E41\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",
517
526
  TIMEOUT: "OpenDesign\uFF1A\u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",
518
527
  SERVER: "OpenDesign\uFF1A\u4E0A\u6E38\u670D\u52A1\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",
528
+ REQUEST_TOO_LARGE: "OpenDesign\uFF1A\u8BF7\u6C42\u5185\u5BB9\u8D85\u8FC7\u4E0A\u6E38\u5927\u5C0F\u9650\u5236\uFF0C\u8BF7\u538B\u7F29\u5386\u53F2\u3001\u51CF\u5C11\u56FE\u7247\u6216\u7F29\u5C0F\u8F93\u5165\u540E\u91CD\u8BD5",
519
529
  INVALID_REQUEST: "OpenDesign\uFF1A\u8BF7\u6C42\u88AB\u4E0A\u6E38\u62D2\u7EDD",
520
530
  INVALID_RESPONSE: "OpenDesign\uFF1A\u4E0A\u6E38\u54CD\u5E94\u683C\u5F0F\u65E0\u6548"
521
531
  };
@@ -586,6 +596,7 @@ function classifyStatus(status) {
586
596
  if (status === 401) return "AUTH";
587
597
  if (status === 403) return "PERMISSION";
588
598
  if (status === 402) return QUOTA_EXCEEDED_CODE;
599
+ if (status === 413) return "REQUEST_TOO_LARGE";
589
600
  if (status === 429) return "RATE_LIMIT";
590
601
  if (status === 408 || status === 504) return "TIMEOUT";
591
602
  if (status >= 500) return "SERVER";
@@ -1950,7 +1961,7 @@ function metadata(provider, row, config) {
1950
1961
  id: row.id,
1951
1962
  name: row.name,
1952
1963
  ...row.inputModalities ? { inputModalities: row.inputModalities } : {},
1953
- ...row.contextWindow === void 0 ? {} : { context: { contextWindow: row.contextWindow - (reserve ?? 0) } },
1964
+ ...row.contextWindow === void 0 ? {} : { context: { contextWindow: row.contextWindow } },
1954
1965
  ...reserve === void 0 ? {} : { defaultMaxTokens: reserve },
1955
1966
  ...efforts?.length ? { reasoning: {
1956
1967
  efforts: efforts.map((id) => ({ id: ReasoningEffortId(id), name: { none: "\u5173\u95ED", minimal: "\u6700\u4F4E", low: "\u4F4E", medium: "\u4E2D", high: "\u9AD8", xhigh: "\u8F83\u9AD8", ultra: "\u6781\u9AD8", max: "\u6700\u9AD8" }[id] ?? id })),
@@ -2074,6 +2085,12 @@ var CoRouterAdapter = class extends LlmAdapter {
2074
2085
  unreadableImages.add(block.attachment.attachmentId);
2075
2086
  }
2076
2087
  }
2088
+ signal.throwIfAborted();
2089
+ const imageMessages = options.messages.filter((message) => message.role === "user" || message.role === "tool");
2090
+ const imageBytes = (block) => Buffer.byteLength(images.get(block.attachment.attachmentId) ?? "", "utf8");
2091
+ const offloadCount = (maxBytes) => requiredImageOffload(imageMessages, { representation: "raw", maxBytes }, imageBytes);
2092
+ const overBudget = offloadCount(config.maxRequestImageBytes ?? DEFAULT_MAX_REQUEST_IMAGE_BYTES);
2093
+ if (overBudget > 0) throw new LlmError11("OpenDesign\uFF1A\u56FE\u7247\u8BF7\u6C42\u8D85\u51FA\u5B57\u8282\u9884\u7B97\uFF0C\u9700\u8981\u9000\u8F7D\u8F83\u65E9\u56FE\u7247\u540E\u91CD\u8BD5", IMAGE_OFFLOAD_REQUIRED_CODE, { offloadImages: overBudget });
2077
2094
  const projectedOptions = unreadableImages.size === 0 ? options : {
2078
2095
  ...options,
2079
2096
  messages: options.messages.map((message) => message.role !== "user" && message.role !== "tool" ? message : {
@@ -2144,7 +2161,16 @@ var CoRouterAdapter = class extends LlmAdapter {
2144
2161
  reader.releaseLock();
2145
2162
  }
2146
2163
  }
2147
- throw gatewayError(response.status, error);
2164
+ signal.throwIfAborted();
2165
+ const failure = gatewayError(response.status, error);
2166
+ if (response.status === 413 && failure.code === "REQUEST_TOO_LARGE") {
2167
+ const totalImageBytes = imageMessages.reduce((total, message) => total + message.content.reduce((sum, block) => sum + (block.type === "image" && !block.offloaded ? imageBytes(block) : 0), 0), 0);
2168
+ if (totalImageBytes > 0) {
2169
+ const offloadImages = offloadCount(Math.max(0, totalImageBytes - 4 * 1024 * 1024));
2170
+ if (offloadImages > 0) throw new LlmError11("OpenDesign\uFF1A\u8BF7\u6C42\u4F53\u8FC7\u5927\uFF0C\u9700\u8981\u9000\u8F7D\u8F83\u65E9\u56FE\u7247\u540E\u91CD\u8BD5", IMAGE_OFFLOAD_REQUIRED_CODE, { status: 413, offloadImages });
2171
+ }
2172
+ }
2173
+ throw failure;
2148
2174
  }
2149
2175
  const monitor = row.group === "plan" && row.id === "deepseek-v4.1-flash" && config.emptyTurnGuard && Boolean(options.tools?.length);
2150
2176
  const reportEmptyTurn = () => {
@@ -2176,7 +2202,8 @@ var CoRouterAdapter = class extends LlmAdapter {
2176
2202
  } catch (error) {
2177
2203
  if (options.signal?.aborted || this.#lifetime.signal.aborted) signal.throwIfAborted();
2178
2204
  if (signal.aborted) throw new LlmError11("CoRouter \u8BF7\u6C42\u8D85\u8FC7\u603B\u8D85\u65F6", "TIMEOUT");
2179
- if (monitor && !tracker.sawToolCall && error instanceof PrematureEndError) throw new LlmError11(error.message, "TRANSPORT");
2205
+ const retryOpusMessages = protocolId === "messages" && row.group === "paid" && row.id === "claude-opus-5.5";
2206
+ if ((monitor || retryOpusMessages) && !tracker.sawToolCall && error instanceof PrematureEndError) throw new LlmError11(error.message, "TRANSPORT");
2180
2207
  if (monitor && !tracker.sawToolCall && error instanceof LlmError11 && error.code === "EMPTY_RESPONSE") {
2181
2208
  reportEmptyTurn();
2182
2209
  throw new LlmError11(error.message, OPENDESIGN_EOT);
@@ -2337,6 +2364,7 @@ var DISCOVERY_TIMEOUT_MS = 2e4;
2337
2364
  var MAX_DISCOVERY_BODY_BYTES = 2 * 1024 * 1024;
2338
2365
  var MAX_MODELS = 512;
2339
2366
  var MAX_MODEL_NAME_LENGTH = 128;
2367
+ var MAX_CONTEXT_WINDOW = 1e8;
2340
2368
  var MODEL_ID = /^[A-Za-z0-9._:/-]{1,128}$/u;
2341
2369
  var MEDIA_MODEL_ID = /^(?:gpt-image-|nano-banana-|seedream-|imagen-|veo-|doubao-seedance-|sora-)/u;
2342
2370
  var MESSAGES2 = {
@@ -2405,6 +2433,21 @@ async function boundedJson(response, signal, deadline) {
2405
2433
  throw new ModelDiscoveryError("response");
2406
2434
  }
2407
2435
  }
2436
+ function gatewayContext(value) {
2437
+ const metadata2 = record(value.metadata) ? value.metadata : {};
2438
+ const candidates = [
2439
+ metadata2.context_limit,
2440
+ metadata2.contextWindowTokens,
2441
+ metadata2.context_window_tokens,
2442
+ value.contextWindowTokens,
2443
+ value.context_window_tokens,
2444
+ value.contextLength,
2445
+ value.context_length,
2446
+ value.contextWindow,
2447
+ value.context_window
2448
+ ].filter((item) => typeof item === "number" && Number.isSafeInteger(item) && item > 0 && item <= MAX_CONTEXT_WINDOW);
2449
+ return candidates.length === 0 ? void 0 : Math.min(...candidates);
2450
+ }
2408
2451
  function normalize(body) {
2409
2452
  if (!record(body) || !Array.isArray(body.data)) throw new ModelDiscoveryError("response");
2410
2453
  if (body.data.length > MAX_MODELS) throw new ModelDiscoveryError("response");
@@ -2415,13 +2458,15 @@ function normalize(body) {
2415
2458
  if (MEDIA_MODEL_ID.test(value.id) || seen.has(value.id)) continue;
2416
2459
  seen.add(value.id);
2417
2460
  const known = findModel(value.id);
2461
+ const offeredContext = gatewayContext(value);
2418
2462
  if (known) {
2419
- rows.push({ ...known });
2463
+ const contextWindow = offeredContext === void 0 ? known.contextWindow : known.contextWindow === void 0 ? offeredContext : Math.min(known.contextWindow, offeredContext);
2464
+ rows.push({ ...known, ...contextWindow === void 0 ? {} : { contextWindow } });
2420
2465
  continue;
2421
2466
  }
2422
2467
  const offered = typeof value.name === "string" ? value.name.trim() : "";
2423
2468
  const name2 = offered !== "" && offered.length <= MAX_MODEL_NAME_LENGTH ? offered : value.id;
2424
- rows.push({ id: value.id, name: name2, group: "paid" });
2469
+ rows.push({ id: value.id, name: name2, group: "paid", ...offeredContext === void 0 ? {} : { contextWindow: offeredContext } });
2425
2470
  }
2426
2471
  return rows;
2427
2472
  }
@@ -2969,7 +3014,7 @@ import { randomUUID as randomUUID2 } from "node:crypto";
2969
3014
 
2970
3015
  // src/device-auth.ts
2971
3016
  var DEVICE_AUTH_ENDPOINT = "https://amr-api.open-design.ai/api/v1/cli/device-authorizations";
2972
- var DEVICE_AUTH_PLUGIN_VERSION = "0.1.12";
3017
+ var DEVICE_AUTH_PLUGIN_VERSION = "0.1.14";
2973
3018
  var DEVICE_REQUEST_TIMEOUT_MS = 2e4;
2974
3019
  var DEVICE_RESPONSE_MAX_BYTES = 65536;
2975
3020
  var DEVICE_POLL_MIN_MS = 1e3;
@@ -4088,7 +4133,8 @@ function parseStatusConfig(value) {
4088
4133
  ...value.modelSettings === void 0 ? {} : { modelSettings: parseModelSettingsMap(value.modelSettings) },
4089
4134
  ...value.requestTimeoutMs === void 0 ? {} : { requestTimeoutMs: positiveInteger(value.requestTimeoutMs, "\u8BF7\u6C42\u8D85\u65F6", 36e5, 1e3) },
4090
4135
  ...value.streamIdleTimeoutMs === void 0 ? {} : { streamIdleTimeoutMs: positiveInteger(value.streamIdleTimeoutMs, "\u6D41\u7A7A\u95F2\u8D85\u65F6", 18e5, 1e3) },
4091
- ...value.quotaRefreshSeconds === void 0 ? {} : { quotaRefreshSeconds: positiveInteger(value.quotaRefreshSeconds, "\u81EA\u52A8\u5237\u65B0\u95F4\u9694", 3600, 5) }
4136
+ ...value.quotaRefreshSeconds === void 0 ? {} : { quotaRefreshSeconds: positiveInteger(value.quotaRefreshSeconds, "\u81EA\u52A8\u5237\u65B0\u95F4\u9694", 3600, 5) },
4137
+ ...value.maxRequestImageBytes === void 0 ? {} : { maxRequestImageBytes: positiveInteger(value.maxRequestImageBytes, "\u8BF7\u6C42\u56FE\u7247\u9884\u7B97", MAX_REQUEST_IMAGE_BYTES, MIN_REQUEST_IMAGE_BYTES) }
4092
4138
  };
4093
4139
  }
4094
4140
  function parseStatus(value) {