mixdog 1.4.3 → 1.4.5

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.
Files changed (281) hide show
  1. package/package.json +1 -2
  2. package/src/cli.mjs +5 -1
  3. package/src/defaults/shipped-history.json +30 -2
  4. package/src/defaults/skills/browser-use/SKILL.md +148 -273
  5. package/src/defaults/skills/browser-use/references/devtools.md +14 -0
  6. package/src/defaults/skills/browser-use/references/page-flows.md +66 -0
  7. package/src/defaults/skills/browser-use/references/sign-in.md +70 -0
  8. package/src/defaults/skills/code-tidy/SKILL.md +60 -84
  9. package/src/defaults/skills/code-tidy/references/agent-cleanup.md +27 -2
  10. package/src/defaults/skills/computer-use/SKILL.md +145 -229
  11. package/src/defaults/skills/computer-use/references/flows.md +35 -0
  12. package/src/defaults/skills/computer-use/references/operations.md +22 -0
  13. package/src/defaults/skills/computer-use/references/platforms.md +12 -0
  14. package/src/defaults/skills/docx/SKILL.md +23 -31
  15. package/src/defaults/skills/docx/references/native-authoring.md +26 -3
  16. package/src/defaults/skills/docx/references/redlining.md +15 -0
  17. package/src/defaults/skills/image/SKILL.md +37 -77
  18. package/src/defaults/skills/image/references/prompt-forms.md +51 -0
  19. package/src/defaults/skills/image/references/text.md +6 -0
  20. package/src/defaults/skills/local-provider/SKILL.md +75 -105
  21. package/src/defaults/skills/local-provider/references/lifecycle.md +14 -0
  22. package/src/defaults/skills/local-provider/references/maintenance.md +19 -0
  23. package/src/defaults/skills/local-provider/references/recovery.md +30 -0
  24. package/src/defaults/skills/memory-management/SKILL.md +22 -21
  25. package/src/defaults/skills/pdf/SKILL.md +21 -23
  26. package/src/defaults/skills/pdf/references/blocks.md +20 -0
  27. package/src/defaults/skills/pdf/references/forms.md +12 -0
  28. package/src/defaults/skills/pdf/references/html.md +2 -2
  29. package/src/defaults/skills/pdf/references/ocr.md +7 -0
  30. package/src/defaults/skills/pdf/references/security.md +8 -0
  31. package/src/defaults/skills/pptx/SKILL.md +30 -109
  32. package/src/defaults/skills/pptx/references/audit.md +45 -0
  33. package/src/defaults/skills/pptx/references/brief-critique.md +47 -0
  34. package/src/defaults/skills/pptx/references/editing.md +24 -0
  35. package/src/defaults/skills/pptx/references/html.md +1 -1
  36. package/src/defaults/skills/pptx/references/script.md +33 -0
  37. package/src/defaults/skills/setup/SKILL.md +34 -102
  38. package/src/defaults/skills/setup/references/actions.md +73 -14
  39. package/src/defaults/skills/setup/references/surfaces.md +23 -8
  40. package/src/defaults/skills/setup-guide/SKILL.md +28 -115
  41. package/src/defaults/skills/setup-guide/references/features.md +36 -0
  42. package/src/defaults/skills/setup-guide/references/glossary.md +38 -0
  43. package/src/defaults/skills/setup-guide/references/workflow-models.md +62 -0
  44. package/src/defaults/skills/test-suite/SKILL.md +15 -127
  45. package/src/defaults/skills/test-suite/references/case-management.md +17 -9
  46. package/src/defaults/skills/test-suite/references/diagnose.md +23 -0
  47. package/src/defaults/skills/test-suite/references/prune.md +27 -0
  48. package/src/defaults/skills/test-suite/references/write.md +44 -0
  49. package/src/defaults/skills/video/SKILL.md +28 -42
  50. package/src/defaults/skills/video/references/prompting.md +31 -0
  51. package/src/defaults/skills/visualize/SKILL.md +73 -107
  52. package/src/defaults/skills/visualize/references/craft.md +1 -2
  53. package/src/defaults/skills/visualize/references/feedback.md +36 -0
  54. package/src/defaults/skills/visualize/references/showing.md +13 -0
  55. package/src/defaults/skills/xlsx/SKILL.md +23 -50
  56. package/src/defaults/skills/xlsx/references/editing.md +15 -0
  57. package/src/defaults/skills/xlsx/references/model-conventions.md +1 -1
  58. package/src/defaults/skills/xlsx/references/recalculation.md +37 -0
  59. package/src/defaults/skills/xlsx/references/report-design.md +18 -2
  60. package/src/lib/keychain-cjs.cjs +22 -1
  61. package/src/lib/rules-builder.cjs +1 -1
  62. package/src/rules/agent/AGENT.md +3 -0
  63. package/src/rules/agent-mode/MIXY.md +5 -3
  64. package/src/rules/lead/LEAD.md +3 -1
  65. package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
  66. package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
  67. package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
  68. package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
  69. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +4 -3
  70. package/src/runtime/agent/orchestrator/providers/anthropic-api-request.mjs +11 -1
  71. package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
  72. package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
  73. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
  74. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
  75. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-events/content-block-start.mjs +4 -0
  76. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-outcome.mjs +2 -0
  77. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
  78. package/src/runtime/agent/orchestrator/providers/anthropic-turn-result.mjs +2 -0
  79. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +58 -6
  80. package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
  81. package/src/runtime/agent/orchestrator/providers/custom-provider-config.mjs +121 -9
  82. package/src/runtime/agent/orchestrator/providers/custom-provider.mjs +4 -1
  83. package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
  84. package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
  85. package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
  86. package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
  87. package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
  88. package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
  89. package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
  90. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
  91. package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
  92. package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
  93. package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
  94. package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
  95. package/src/runtime/agent/orchestrator/providers/lib/generation-window.mjs +9 -0
  96. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-send.mjs +9 -1
  97. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-result.mjs +2 -0
  98. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-state.mjs +5 -0
  99. package/src/runtime/agent/orchestrator/providers/openai-compat-options.mjs +3 -0
  100. package/src/runtime/agent/orchestrator/providers/openai-compat-response-normalization.mjs +2 -0
  101. package/src/runtime/agent/orchestrator/providers/openai-compat-responses.mjs +10 -1
  102. package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
  103. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-events.mjs +3 -0
  104. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-outcome.mjs +2 -0
  105. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
  106. package/src/runtime/agent/orchestrator/providers/openai-ws-response-state.mjs +9 -2
  107. package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
  108. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
  109. package/src/runtime/agent/orchestrator/providers/openrouter-provider-routing.mjs +28 -0
  110. package/src/runtime/agent/orchestrator/providers/openrouter.mjs +4 -1
  111. package/src/runtime/agent/orchestrator/providers/registry.mjs +30 -25
  112. package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
  113. package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
  114. package/src/runtime/agent/orchestrator/runtime-core/ask-user-result.mjs +66 -0
  115. package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
  116. package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
  117. package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
  118. package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
  119. package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
  120. package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
  121. package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
  122. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
  123. package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
  124. package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
  125. package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
  126. package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +8 -1
  127. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
  128. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
  129. package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
  130. package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
  131. package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
  132. package/src/runtime/agent/orchestrator/session/store.mjs +12 -6
  133. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
  134. package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
  135. package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
  136. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-output-silence.mjs +64 -0
  137. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-task-output.mjs +20 -6
  138. package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
  139. package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
  140. package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
  141. package/src/runtime/agent/orchestrator/tools/builtin/task-tool.mjs +12 -1
  142. package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
  143. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
  144. package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
  145. package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
  146. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
  147. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
  148. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
  149. package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
  150. package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
  151. package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
  152. package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
  153. package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
  154. package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
  155. package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
  156. package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
  157. package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
  158. package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
  159. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
  160. package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
  161. package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
  162. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
  163. package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
  164. package/src/runtime/channels/lib/crash-log.mjs +21 -1
  165. package/src/runtime/channels/lib/routines-db.mjs +35 -0
  166. package/src/runtime/channels/lib/schedules-db.mjs +4 -0
  167. package/src/runtime/channels/lib/webhooks-db.mjs +4 -0
  168. package/src/runtime/channels/lib/worker-main.mjs +8 -25
  169. package/src/runtime/computer-bridge/action-schema.mjs +2 -2
  170. package/src/runtime/computer-bridge/core-actions.mjs +16 -2
  171. package/src/runtime/computer-bridge/result-canonical.mjs +6 -0
  172. package/src/runtime/media/jobs.mjs +5 -0
  173. package/src/runtime/media/store.mjs +146 -3
  174. package/src/runtime/memory/tool-defs.mjs +1 -1
  175. package/src/runtime/office/core/office-write-ledger.mjs +2 -2
  176. package/src/runtime/shared/agent-reap-state.mjs +19 -0
  177. package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
  178. package/src/runtime/shared/desktop-notice-host.mjs +87 -0
  179. package/src/runtime/shared/file-lock.mjs +2 -2
  180. package/src/runtime/shared/heap-cap.mjs +9 -11
  181. package/src/runtime/shared/llm/http-agent.mjs +11 -1
  182. package/src/runtime/shared/llm/system-proxy.mjs +100 -25
  183. package/src/runtime/shared/llm/usage-accounting.mjs +121 -19
  184. package/src/runtime/shared/llm/usage-context.mjs +3 -3
  185. package/src/runtime/shared/llm/usage-ledger-quota.mjs +2 -0
  186. package/src/runtime/shared/llm/usage-ledger-requests.mjs +190 -0
  187. package/src/runtime/shared/llm/usage-ledger-rollup.mjs +7 -3
  188. package/src/runtime/shared/llm/usage-ledger-worker.mjs +2 -0
  189. package/src/runtime/shared/llm/usage-ledger.mjs +69 -1
  190. package/src/runtime/shared/llm/usage-request-status.mjs +48 -0
  191. package/src/runtime/shared/provider-accounts.mjs +22 -0
  192. package/src/runtime/shared/provider-auth-binding.mjs +2 -2
  193. package/src/runtime/shared/resource-admission.mjs +2 -2
  194. package/src/runtime/shared/root-async-context.mjs +51 -0
  195. package/src/runtime/shared/run-child.mjs +18 -1
  196. package/src/runtime/shared/slow-span.mjs +2 -2
  197. package/src/runtime/shared/tool-execution-owner.mjs +2 -2
  198. package/src/runtime/shared/tool-result-summary.mjs +1 -1
  199. package/src/runtime/shared/tool-work-units.mjs +2 -0
  200. package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
  201. package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
  202. package/src/runtime/shared/user-cwd.mjs +2 -2
  203. package/src/runtime/shared/worker-requests.mjs +3 -1
  204. package/src/runtime/shared/worktrees/lock.mjs +2 -2
  205. package/src/runtime/visualize-bridge/client.mjs +26 -4
  206. package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
  207. package/src/session-runtime/agent-home-delivery.mjs +76 -8
  208. package/src/session-runtime/agent-task-sessions.mjs +63 -22
  209. package/src/session-runtime/boot/apis.mjs +16 -0
  210. package/src/session-runtime/boot/begin.mjs +2 -0
  211. package/src/session-runtime/boot/tools.mjs +8 -2
  212. package/src/session-runtime/internal-tool-executor/ask-user-tool.mjs +3 -58
  213. package/src/session-runtime/media-api.mjs +15 -0
  214. package/src/session-runtime/model-route/route-persist.mjs +12 -4
  215. package/src/session-runtime/provider-models/row-hydration.mjs +2 -0
  216. package/src/session-runtime/provider-readiness.mjs +10 -2
  217. package/src/session-runtime/routine-owner.mjs +9 -3
  218. package/src/session-runtime/runtime-core.mjs +35 -8
  219. package/src/session-runtime/runtime-review-api.mjs +3 -1
  220. package/src/session-runtime/services/agent-task-status.mjs +29 -1
  221. package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
  222. package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
  223. package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
  224. package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
  225. package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
  226. package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
  227. package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
  228. package/src/session-runtime/services/agent-tool/job-views/session-progress.mjs +3 -0
  229. package/src/session-runtime/services/agent-tool/render.mjs +8 -1
  230. package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
  231. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
  232. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
  233. package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
  234. package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
  235. package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
  236. package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
  237. package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
  238. package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
  239. package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
  240. package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
  241. package/src/session-runtime/services/channel-admin.mjs +44 -3
  242. package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
  243. package/src/session-runtime/services/plugin-admin.mjs +46 -7
  244. package/src/session-runtime/services/provider-admin.mjs +5 -0
  245. package/src/session-runtime/session-create.mjs +5 -0
  246. package/src/session-runtime/setup-tool/agent-settings-actions.mjs +1 -1
  247. package/src/session-runtime/setup-tool/executor.mjs +1 -1
  248. package/src/session-runtime/setup-tool/extended-actions.mjs +1 -1
  249. package/src/session-runtime/setup-tool/settings-contract.mjs +4 -2
  250. package/src/session-runtime/usage-stats-api.mjs +120 -1
  251. package/src/session-runtime/workflow-agents-api/agent-connectors.mjs +36 -29
  252. package/src/session-runtime/workflow-agents-api/agent-editor.mjs +183 -23
  253. package/src/session-runtime/workspace/admission-gate.mjs +25 -7
  254. package/src/session-runtime/workspace/transitions.mjs +4 -0
  255. package/src/standalone/daemon-log.mjs +28 -4
  256. package/src/standalone/daemon-owner.mjs +41 -7
  257. package/src/standalone/daemon-work-gate.mjs +139 -0
  258. package/src/standalone/daemon.mjs +80 -11
  259. package/src/standalone/desktop-service-registry.mjs +11 -4
  260. package/src/standalone/session-client.mjs +57 -5
  261. package/src/standalone/session-protocol.mjs +6 -0
  262. package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
  263. package/src/standalone/session-service/agent-tree.mjs +7 -0
  264. package/src/standalone/session-service/retention/busy.mjs +3 -2
  265. package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
  266. package/src/standalone/session-transport/call-route.mjs +10 -4
  267. package/src/standalone/session-transport/routes.mjs +21 -0
  268. package/src/standalone/session-transport/stack.mjs +3 -1
  269. package/src/standalone/session-transport.mjs +5 -0
  270. package/src/tui/app/model-picker/model-footer.mjs +1 -1
  271. package/src/tui/app/model-picker/provider-models-list.mjs +7 -3
  272. package/src/tui/app/model-picker/route-selection/fast-parameters.mjs +23 -3
  273. package/src/tui/app/model-picker/route-selection.mjs +1 -0
  274. package/src/tui/dist/index.mjs +536 -484
  275. package/src/tui/session/session-api/integrations.mjs +3 -0
  276. package/src/tui/session/session-api/media.mjs +5 -0
  277. package/src/tui/session/tool-approval-settle.mjs +24 -4
  278. package/src/tui/session/tool-approval.mjs +35 -10
  279. package/src/tui/session/turn.mjs +13 -0
  280. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
  281. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
@@ -1,5 +1,5 @@
1
- import { GoogleGenerativeAI } from '@google/generative-ai';
2
1
  import { reloadProviderApiKey, resolveProviderApiKey } from '../../../shared/provider-api-key.mjs';
2
+ import { noteAbandonedUsage } from '../../../shared/llm/usage-context.mjs';
3
3
  import {
4
4
  canFallbackNonStreaming,
5
5
  emitProviderRetryStage,
@@ -29,16 +29,18 @@ import {
29
29
  geminiInvalidStreamReason,
30
30
  runGeminiStreamAttempts,
31
31
  } from './gemini-retry-policy.mjs';
32
- import { geminiTextLeakGuardFor, streamGeminiSdkAttempt } from './gemini-sdk-request.mjs';
33
- import { emitGeminiToolCalls } from './gemini-schema.mjs';
32
+ import { geminiTextLeakGuardFor } from './gemini-sdk-request.mjs';
33
+ import { emitGeminiToolCalls, parseGeminiTextPartMetadata } from './gemini-schema.mjs';
34
34
  import { createGeminiReasoningRelay } from './gemini-reasoning-relay.mjs';
35
- import { buildGeminiRequest, geminiCachedRestBody } from './gemini-request-body.mjs';
35
+ import { buildGeminiRequest, geminiCachedRestBody, geminiUncachedRestBody } from './gemini-request-body.mjs';
36
+ import { geminiRedirectTarget } from './gemini-model-redirect.mjs';
36
37
  import {
37
38
  geminiIncompleteError,
38
39
  geminiSendResult,
39
40
  parseGeminiCandidate,
40
41
  resolveGeminiUsage,
41
42
  geminiFailureUsage,
43
+ normalizeGeminiUsage,
42
44
  } from './gemini-response.mjs';
43
45
  import {
44
46
  _getGeminiGlobalCache,
@@ -79,6 +81,115 @@ import {
79
81
  // mirroring anthropic-oauth's _modelRefreshInFlight.
80
82
  let _modelRefreshInFlight = null;
81
83
  const GEMINI_AVAILABILITY_TIMEOUT_MS = 1_000;
84
+ const GEMINI_API_ORIGIN = 'https://generativelanguage.googleapis.com';
85
+ // Long decode: a streamed hop stops at 64K output tokens (32K non-streaming)
86
+ // with finishReason CONTINUATION. The 1M-token context bounds a turn to 16
87
+ // streamed hops (32 if every hop fell back to non-streaming); more is a loop.
88
+ const GEMINI_MAX_CONTINUATION_HOPS = 32;
89
+ // Redirected model ids already announced in this process.
90
+ const announcedRedirects = new Set();
91
+
92
+ // The key travels only in the documented header, never in a URL.
93
+ function geminiApiHeaders(apiKey, json = true) {
94
+ return { ...(json ? { 'Content-Type': 'application/json' } : {}), 'x-goog-api-key': apiKey };
95
+ }
96
+
97
+ const USAGE_COUNT_FIELDS = [
98
+ 'promptTokenCount',
99
+ 'cachedContentTokenCount',
100
+ 'candidatesTokenCount',
101
+ 'toolUsePromptTokenCount',
102
+ 'thoughtsTokenCount',
103
+ 'totalTokenCount',
104
+ ];
105
+
106
+ // Usage of a long-decode turn: every hop is billed, so every count is the sum
107
+ // over the hops; initialPromptTokenCount keeps the first hop's prompt (the
108
+ // context size, which the summed prompt count overstates). Context occupancy
109
+ // is that prompt plus the summed generated tokens.
110
+ function sumGeminiHopUsage(hops) {
111
+ const metas = hops.map((hop) => hop.usageMetadata).filter(Boolean);
112
+ if (!metas.length) return undefined;
113
+ const summed = { ...metas.at(-1) };
114
+ for (const field of USAGE_COUNT_FIELDS) {
115
+ if (metas.some((meta) => meta[field] !== undefined)) {
116
+ summed[field] = metas.reduce((total, meta) => total + (Number(meta[field]) || 0), 0);
117
+ }
118
+ }
119
+ summed.initialPromptTokenCount = Number(metas[0].promptTokenCount) || 0;
120
+ summed.initialCachedContentTokenCount = Number(metas[0].cachedContentTokenCount) || 0;
121
+ return summed;
122
+ }
123
+
124
+ // One response for a long-decode turn: the parts of every hop in order, the
125
+ // final hop's terminal state and the summed usage.
126
+ function mergeGeminiHops(hops) {
127
+ if (hops.length === 1) return hops[0];
128
+ const last = hops.at(-1);
129
+ const lastCandidate = last.candidates?.[0] || {};
130
+ const parts = hops.flatMap((hop) => hop.candidates?.[0]?.content?.parts || []);
131
+ const merged = {
132
+ ...last,
133
+ candidates: [{ ...lastCandidate, content: { role: lastCandidate.content?.role || 'model', parts } }],
134
+ };
135
+ const promptFeedback = hops.find((hop) => hop.promptFeedback)?.promptFeedback;
136
+ if (promptFeedback) merged.promptFeedback = promptFeedback;
137
+ const usageMetadata = sumGeminiHopUsage(hops);
138
+ if (usageMetadata) merged.usageMetadata = usageMetadata;
139
+ return merged;
140
+ }
141
+
142
+ // A long-decode turn that fails on a later hop is still one turn: its failure
143
+ // fields cover every finished hop before the failing one. `shownText` is what
144
+ // those hops relayed live; once anything was shown the turn is past its replay
145
+ // boundary even if the failing hop itself showed nothing.
146
+ function carryGeminiHopsIntoFailure(err, hops, shownText) {
147
+ if (!hops.length || !err || typeof err !== 'object') return err;
148
+ try {
149
+ const priorParts = hops.flatMap((hop) => hop.candidates?.[0]?.content?.parts || []);
150
+ const priorText = priorParts
151
+ .filter((part) => part?.thought !== true && typeof part?.text === 'string')
152
+ .map((part) => part.text)
153
+ .join('');
154
+ const shown = shownText.length > 0;
155
+ if (shown || typeof err.partialContent === 'string') {
156
+ err.partialContent = `${shown ? shownText : priorText}${typeof err.partialContent === 'string' ? err.partialContent : ''}`;
157
+ }
158
+ const prior = parseGeminiTextPartMetadata(priorParts)?.gemini || {};
159
+ const failing = err.providerMetadata?.gemini || {};
160
+ const thoughtParts = [...(prior.thoughtParts || []), ...(failing.thoughtParts || [])];
161
+ const textParts = [...(prior.textParts || []), ...(failing.textParts || [])];
162
+ if (thoughtParts.length || textParts.length) {
163
+ err.providerMetadata = {
164
+ ...err.providerMetadata,
165
+ gemini: {
166
+ ...failing,
167
+ ...(thoughtParts.length ? { thoughtParts } : {}),
168
+ ...(textParts.length ? { textParts } : {}),
169
+ },
170
+ };
171
+ }
172
+ if (shown) {
173
+ err.liveTextEmitted = true;
174
+ err.unsafeToRetry = true;
175
+ if (err.code === 'EGEMINITIMEOUT' || err.code === 'TRUNCATED_STREAM' || err.truncatedStream === true) {
176
+ err.streamStalled = true;
177
+ if (err.pendingToolUse === undefined) err.pendingToolUse = false;
178
+ }
179
+ }
180
+ } catch {
181
+ /* best-effort */
182
+ }
183
+ return err;
184
+ }
185
+
186
+ function continuationTokenOf(response) {
187
+ const candidate = response?.candidates?.[0];
188
+ if (geminiFinishReasonKind(candidate?.finishReason) !== 'continuation') return null;
189
+ return typeof candidate?.continuationToken === 'string' && candidate.continuationToken
190
+ ? candidate.continuationToken
191
+ : null;
192
+ }
82
193
 
83
194
  export function geminiRestError(res, text, label) {
84
195
  let payload = null;
@@ -143,6 +254,7 @@ function geminiSendCallbacks(opts) {
143
254
  onReasoningDelta: reasoning.onReasoningDelta,
144
255
  // Thinking already shown by a dead or discarded attempt.
145
256
  retractReasoning: reasoning.retract,
257
+ commitReasoning: reasoning.commit,
146
258
  };
147
259
  }
148
260
 
@@ -162,39 +274,29 @@ export class GeminiProvider {
162
274
  // The two-phase ladder (gemini-retry-policy.mjs) is the whole recovery:
163
275
  // a spent ladder surfaces instead of parking on loop recovery cycles.
164
276
  retryPolicy = Object.freeze({ recoveryCycles: false });
165
- genAI;
166
277
  config;
167
278
  _fetch;
168
279
  _preconnect;
169
- _createGenAI;
280
+ _now;
170
281
  _modelCache;
171
282
 
172
283
  constructor(config = {}) {
173
284
  this.config = config;
174
285
  this._fetch = typeof config.fetchFn === 'function' ? config.fetchFn : fetch;
175
286
  this._preconnect = typeof config.preconnectFn === 'function' ? config.preconnectFn : preconnect;
176
- this._createGenAI =
177
- typeof config.createGenAI === 'function' ? config.createGenAI : (apiKey) => new GoogleGenerativeAI(apiKey);
287
+ this._now = typeof config.nowFn === 'function' ? config.nowFn : Date.now;
178
288
  this._modelCache = config.modelCache || _modelCache;
179
- this._injectedGenAI = Boolean(config.genAI);
180
- this._genAIKey = resolveProviderApiKey('gemini', config);
181
- this.genAI = config.genAI || this._createGenAI(this._genAIKey);
182
289
  // Warm a kept-alive socket to the Gemini REST API so the first cache/
183
290
  // generateContent request skips the cold TLS handshake. Best-effort.
184
- this._preconnect('https://generativelanguage.googleapis.com');
291
+ this._preconnect(GEMINI_API_ORIGIN);
185
292
  }
186
293
 
187
294
  reloadApiKey() {
188
295
  try {
189
296
  const newKey = reloadProviderApiKey('gemini', this.config);
190
- if (newKey) {
191
- // Keep this.config in sync so REST/cache paths don't keep using the
192
- // stale key after a rotation; genAI alone is not enough.
193
- this.config = { ...(this.config || {}), apiKey: newKey };
194
- this._injectedGenAI = false;
195
- this._genAIKey = newKey;
196
- this.genAI = this._createGenAI(newKey);
197
- }
297
+ // Keep this.config in sync so REST/cache paths don't keep using the
298
+ // stale key after a rotation.
299
+ if (newKey) this.config = { ...(this.config || {}), apiKey: newKey };
198
300
  return newKey || '';
199
301
  } catch {
200
302
  /* best effort */
@@ -206,15 +308,34 @@ export class GeminiProvider {
206
308
  return resolveProviderApiKey('gemini', this.config);
207
309
  }
208
310
 
209
- // SDK client for the currently resolved key; rebuilt when the key changed
210
- // since construction (e.g. a keyless-constructed instance).
211
- _currentGenAI() {
212
- const key = this._getApiKey();
213
- if (!this._injectedGenAI && key !== this._genAIKey) {
214
- this._genAIKey = key;
215
- this.genAI = this._createGenAI(key);
311
+ // The direct Gemini API (not a gateway engine built on this adapter).
312
+ get _isDirectGeminiApi() {
313
+ return this.name === 'gemini';
314
+ }
315
+
316
+ // The id the Gemini API serves `model` as today. A redirected id resolves
317
+ // to its target for the whole request (wire id, settings, cache, cost),
318
+ // announced once per process.
319
+ _servedModel(model, opts) {
320
+ if (!this._isDirectGeminiApi) return model;
321
+ const target = geminiRedirectTarget(model, this._now());
322
+ if (!target) return model;
323
+ const requested = geminiBareModelId(model);
324
+ if (!announcedRedirects.has(requested)) {
325
+ announcedRedirects.add(requested);
326
+ const message = `${requested} is now served by Google as ${target} (redirect effective 2026-12-01; billed at ${target} rates)`;
327
+ try {
328
+ opts.onStageChange?.('notice', { tone: 'warn', message });
329
+ } catch {
330
+ /* display-only */
331
+ }
332
+ try {
333
+ process.stderr.write(`[gemini] ${message}\n`);
334
+ } catch {
335
+ /* best-effort */
336
+ }
216
337
  }
217
- return this.genAI;
338
+ return target;
218
339
  }
219
340
 
220
341
  /**
@@ -386,7 +507,7 @@ export class GeminiProvider {
386
507
  // cancel the cache every other session is waiting for — that is the
387
508
  // herd trigger. The 20s ceiling still bounds the preflight request,
388
509
  // and an aborting caller simply stops waiting (awaitSharedCreate).
389
- async _postGeminiCacheCreate(url, body, onFail) {
510
+ async _postGeminiCacheCreate(url, apiKey, body, onFail) {
390
511
  const createTotal = createTimeoutSignal(
391
512
  null,
392
513
  PROVIDER_CACHE_CREATE_TOTAL_TIMEOUT_MS,
@@ -397,7 +518,7 @@ export class GeminiProvider {
397
518
  async ({ signal: attemptSignal }) => {
398
519
  const res = await this._fetch(url, {
399
520
  method: 'POST',
400
- headers: { 'Content-Type': 'application/json' },
521
+ headers: geminiApiHeaders(apiKey),
401
522
  body: JSON.stringify(body),
402
523
  signal: attemptSignal,
403
524
  dispatcher: getLlmDispatcher(),
@@ -438,7 +559,7 @@ export class GeminiProvider {
438
559
  const contentsLen = Array.isArray(contents) ? contents.length : 0;
439
560
  try {
440
561
  const body = geminiCacheCreateBody({ model, ttlSeconds, ...request, cachePrefixContentCount });
441
- const url = `https://generativelanguage.googleapis.com/v1beta/cachedContents?key=${encodeURIComponent(apiKey)}`;
562
+ const url = `${GEMINI_API_ORIGIN}/v1beta/cachedContents`;
442
563
  const onFail = (res, text) =>
443
564
  traceGeminiCache(opts, currentIter, 'gemini_cache_create_fail', {
444
565
  status: res.status,
@@ -447,7 +568,7 @@ export class GeminiProvider {
447
568
  cachePrefixContentCount,
448
569
  canAttachState,
449
570
  });
450
- const data = await this._postGeminiCacheCreate(url, body, onFail);
571
+ const data = await this._postGeminiCacheCreate(url, apiKey, body, onFail);
451
572
  const cacheName = data?.name || null;
452
573
  if (!cacheName) return null;
453
574
  const cacheTokenSize = Number(data?.usageMetadata?.totalTokenCount || 0) || 0;
@@ -512,9 +633,10 @@ export class GeminiProvider {
512
633
  _scheduleGeminiCacheDelete(priorCacheName, apiKey) {
513
634
  setTimeout(() => {
514
635
  if (_geminiGlobalCacheNameIsLive(priorCacheName)) return;
515
- const delUrl = `https://generativelanguage.googleapis.com/v1beta/${priorCacheName}?key=${encodeURIComponent(apiKey)}`;
636
+ const delUrl = `${GEMINI_API_ORIGIN}/v1beta/${priorCacheName}`;
516
637
  this._fetch(delUrl, {
517
638
  method: 'DELETE',
639
+ headers: geminiApiHeaders(apiKey, false),
518
640
  signal: AbortSignal.timeout(10_000),
519
641
  dispatcher: getLlmDispatcher(),
520
642
  }).catch(() => {
@@ -526,7 +648,7 @@ export class GeminiProvider {
526
648
  async send(messages, model, tools, sendOpts) {
527
649
  // Re-warm a kept-alive socket before the turn (TTL-gated no-op while
528
650
  // hot) so a post-idle request skips the cold TLS handshake.
529
- this._preconnect('https://generativelanguage.googleapis.com');
651
+ this._preconnect(GEMINI_API_ORIGIN);
530
652
  try {
531
653
  return await this._doSend(messages, model, tools, sendOpts);
532
654
  } catch (err) {
@@ -571,8 +693,10 @@ export class GeminiProvider {
571
693
  const callbacks = geminiSendCallbacks(opts);
572
694
  throwIfGeminiAborted(signal);
573
695
 
574
- const useModel = model || (await ensureLatestGeminiModel(this));
575
- const request = buildGeminiRequest(messages, useModel, tools, opts, this._rosterOutputLimit(useModel));
696
+ const useModel = this._servedModel(model || (await ensureLatestGeminiModel(this)), opts);
697
+ const request = buildGeminiRequest(messages, useModel, tools, opts, this._rosterOutputLimit(useModel), {
698
+ omitIgnoredSampling: this._isDirectGeminiApi,
699
+ });
576
700
  signalRequesting(opts);
577
701
 
578
702
  // Explicit cachedContents (system + tools + prior-turn transcript).
@@ -592,14 +716,44 @@ export class GeminiProvider {
592
716
  });
593
717
  signalRequesting(opts);
594
718
 
595
- const stream = { ...request, opts, signal, useModel, tools, callbacks, cachedContent };
596
- const outcome = cachedContent
597
- ? await this._streamCachedViaRest(stream, internal)
598
- : await this._streamViaSdk(stream);
599
- if (outcome.retryUncached) {
600
- return await this._doSend(messages, model, tools, opts, { skipExplicitCache: true });
719
+ const stream = { ...request, opts, signal, useModel, tools, callbacks, cachedContent, continuationToken: null };
720
+ const hops = [];
721
+ let shownText = '';
722
+ let outcome;
723
+ try {
724
+ for (;;) {
725
+ outcome = cachedContent ? await this._streamViaRest(stream, internal) : await this._streamViaSdk(stream);
726
+ if (outcome.retryUncached) {
727
+ return await this._doSend(messages, model, tools, opts, { skipExplicitCache: true });
728
+ }
729
+ hops.push(outcome.response);
730
+ shownText += outcome.textLeakGuard?.getRelayedText?.() || '';
731
+ const token = this._isDirectGeminiApi ? continuationTokenOf(outcome.response) : null;
732
+ if (!token) break;
733
+ if (hops.length >= GEMINI_MAX_CONTINUATION_HOPS) {
734
+ throw Object.assign(
735
+ new Error(`Gemini long decode exceeded ${GEMINI_MAX_CONTINUATION_HOPS} continuation hops`),
736
+ { name: 'GeminiContinuationLimitError', code: 'CONTINUATION_LIMIT', continuationToken: token }
737
+ );
738
+ }
739
+ // Long decode: resend the same request with the hop's token; the
740
+ // shown hop is part of the turn, never retracted by a later retry.
741
+ throwIfGeminiAborted(signal);
742
+ callbacks.commitReasoning();
743
+ stream.continuationToken = token;
744
+ signalRequesting(opts);
745
+ }
746
+ } catch (err) {
747
+ // Finished hops were billed even though the turn failed.
748
+ for (const hop of hops) {
749
+ if (hop?.usageMetadata) {
750
+ noteAbandonedUsage(normalizeGeminiUsage(hop.usageMetadata, opts, cachedContent).resolvedUsage, useModel);
751
+ }
752
+ }
753
+ throw carryGeminiHopsIntoFailure(err, hops, shownText);
601
754
  }
602
- const { response, textLeakGuard } = outcome;
755
+ const response = mergeGeminiHops(hops);
756
+ const { textLeakGuard } = outcome;
603
757
  writeGeminiCacheTrace({
604
758
  opts,
605
759
  model: useModel,
@@ -637,13 +791,13 @@ export class GeminiProvider {
637
791
 
638
792
  // First byte bounded by GEMINI_FIRST_BYTE_TIMEOUT_MS; a non-OK status
639
793
  // becomes a typed REST error for withRetry's rules.
640
- async _openRestStream(url, body, attemptSignal) {
794
+ async _openRestStream(url, apiKey, body, attemptSignal) {
641
795
  const openFirstByte = createTimeoutSignal(attemptSignal, GEMINI_FIRST_BYTE_TIMEOUT_MS, 'Gemini REST first byte');
642
796
  let res;
643
797
  try {
644
798
  res = await this._fetch(url, {
645
799
  method: 'POST',
646
- headers: { 'Content-Type': 'application/json' },
800
+ headers: geminiApiHeaders(apiKey),
647
801
  body: JSON.stringify(body),
648
802
  signal: openFirstByte.signal,
649
803
  dispatcher: getLlmDispatcher(),
@@ -659,10 +813,10 @@ export class GeminiProvider {
659
813
  }
660
814
 
661
815
  async _restGenerateContent(useModel, apiKey, body, fallbackSignal) {
662
- const nonStreamUrl = `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(geminiBareModelId(useModel))}:generateContent?key=${encodeURIComponent(apiKey)}`;
816
+ const nonStreamUrl = `${GEMINI_API_ORIGIN}/v1beta/models/${encodeURIComponent(geminiBareModelId(useModel))}:generateContent`;
663
817
  const res = await this._fetch(nonStreamUrl, {
664
818
  method: 'POST',
665
- headers: { 'Content-Type': 'application/json' },
819
+ headers: geminiApiHeaders(apiKey),
666
820
  body: JSON.stringify(body),
667
821
  signal: fallbackSignal,
668
822
  dispatcher: getLlmDispatcher(),
@@ -674,21 +828,22 @@ export class GeminiProvider {
674
828
  return await res.json();
675
829
  }
676
830
 
677
- // With cachedContent attached we bypass @google/generative-ai
678
- // (deprecated; v1beta v1.x docs explicitly forbid re-sending tools or
679
- // systemInstruction once a cache carries them, but the bundled SDK can't
680
- // actually issue a tool-less generateContent call). REST direct sends the
681
- // v1beta payload Google's new genai client uses, so the cache owns
682
- // system/tools and the runtime gets a clean cache hit. Resolves
683
- // { retryUncached: true } when the cache itself was rejected and the send
684
- // must be replayed without it.
685
- async _streamCachedViaRest(stream, internal) {
686
- const { opts, signal, useModel, contents, generationConfig, safetySettings, cachedContent, callbacks } = stream;
831
+ // The one Gemini wire: streamGenerateContent over SSE. With a cachedContents
832
+ // prefix attached the body carries only the uncached tail (the cache owns
833
+ // system/tools/toolConfig, and the API rejects a request repeating them);
834
+ // without one it carries the whole request. A long-decode hop resends the
835
+ // same body plus its continuation token. Resolves { retryUncached: true }
836
+ // when the cache itself was rejected and the send must be replayed without
837
+ // it.
838
+ async _streamViaRest(stream, internal = {}) {
839
+ const { opts, signal, useModel, contents, cachedContent, callbacks, continuationToken } = stream;
687
840
  const apiKey = this._getApiKey();
688
- const genUrl = `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(geminiBareModelId(useModel))}:streamGenerateContent?alt=sse&key=${encodeURIComponent(apiKey)}`;
689
- const body = geminiCachedRestBody({ opts, contents, cachedContent, generationConfig, safetySettings });
690
- // cachedContent owns tools + toolConfig. The API rejects a
691
- // generateContent request that repeats either field.
841
+ const genUrl = `${GEMINI_API_ORIGIN}/v1beta/models/${encodeURIComponent(geminiBareModelId(useModel))}:streamGenerateContent?alt=sse`;
842
+ const body = {
843
+ ...(cachedContent ? geminiCachedRestBody(stream) : geminiUncachedRestBody(stream)),
844
+ ...(continuationToken ? { continuationToken } : {}),
845
+ };
846
+ const label = cachedContent ? '[gemini-rest]' : '[gemini]';
692
847
  // Option A (mirror anthropic-oauth): no absolute wall-clock cap on a
693
848
  // live streaming turn. A stream that keeps emitting SSE deltas must
694
849
  // not be killed by a fixed total-lifetime timer — that false-aborts
@@ -704,13 +859,13 @@ export class GeminiProvider {
704
859
  return await runGeminiStreamAttempts({
705
860
  signal: totalSignal,
706
861
  sleepFn: this.config?.sleepFn,
707
- onRetry: geminiRetryLogger(opts, '[gemini-rest]'),
862
+ onRetry: geminiRetryLogger(opts, label),
708
863
  afterToolResult,
709
864
  failureUsage: geminiFailureUsage(opts, cachedContent, useModel),
710
865
  run: async ({ signal: attemptSignal, connected }) => {
711
866
  callbacks.retractReasoning();
712
867
  signalRequesting(opts);
713
- const res = await this._openRestStream(genUrl, body, attemptSignal);
868
+ const res = await this._openRestStream(genUrl, apiKey, body, attemptSignal);
714
869
  connected();
715
870
  const textLeakGuard = geminiTextLeakGuardFor(stream);
716
871
  const response = await consumeGeminiRestStreamResponse(res, {
@@ -727,7 +882,9 @@ export class GeminiProvider {
727
882
  });
728
883
  } catch (err) {
729
884
  if (
885
+ cachedContent &&
730
886
  !internal.skipExplicitCache &&
887
+ !continuationToken &&
731
888
  err?.unsafeToRetry !== true &&
732
889
  isGeminiCachedContentError(err, cachedContent)
733
890
  ) {
@@ -750,60 +907,11 @@ export class GeminiProvider {
750
907
  }
751
908
  }
752
909
 
910
+ // Transport for a send without a cached prefix. Gateway engines built on
911
+ // this adapter (OpenCode Go) override it; the name predates the removal of
912
+ // the legacy SDK, which no longer exists on any path.
753
913
  async _streamViaSdk(stream) {
754
- const {
755
- opts,
756
- signal,
757
- useModel,
758
- systemInstruction,
759
- geminiTools,
760
- toolConfig,
761
- generationConfig,
762
- safetySettings,
763
- contents,
764
- } = stream;
765
- const genModel = this._currentGenAI().getGenerativeModel({
766
- model: useModel,
767
- systemInstruction,
768
- tools: geminiTools,
769
- ...(toolConfig ? { toolConfig } : {}),
770
- ...(generationConfig ? { generationConfig } : {}),
771
- ...(safetySettings ? { safetySettings } : {}),
772
- });
773
- // Option A (mirror anthropic-oauth): pure pass-through of the external
774
- // signal, no absolute streaming total cap. See the REST path.
775
- const sdkPassthrough = createPassthroughSignal(signal);
776
- const totalSignal = sdkPassthrough.signal;
777
- try {
778
- return await runGeminiStreamAttempts({
779
- signal: totalSignal,
780
- sleepFn: this.config?.sleepFn,
781
- onRetry: geminiRetryLogger(opts, '[gemini]'),
782
- afterToolResult: geminiEndsWithFunctionResponse(contents),
783
- failureUsage: geminiFailureUsage(opts, stream.cachedContent, useModel),
784
- run: async ({ signal: attemptSignal, connected }) => {
785
- stream.callbacks.retractReasoning();
786
- signalRequesting(opts);
787
- return await streamGeminiSdkAttempt(genModel, stream, attemptSignal, connected);
788
- },
789
- });
790
- } catch (err) {
791
- const recovered = await this._recoverGeminiNonStreaming({
792
- streamErr: err,
793
- signal: totalSignal,
794
- opts,
795
- model: useModel,
796
- generate: async (fallbackSignal) => {
797
- const result = await genModel.generateContent({ contents }, { signal: fallbackSignal });
798
- return result?.response ?? null;
799
- },
800
- retractReasoning: stream.callbacks.retractReasoning,
801
- });
802
- if (!recovered) throw err;
803
- return { response: recovered, textLeakGuard: null };
804
- } finally {
805
- sdkPassthrough.cleanup();
806
- }
914
+ return this._streamViaRest(stream);
807
915
  }
808
916
 
809
917
  async listModels() {
@@ -862,8 +970,8 @@ export class GeminiProvider {
862
970
  controller.abort(providerTimeoutError('Gemini availability probe', GEMINI_AVAILABILITY_TIMEOUT_MS));
863
971
  }, GEMINI_AVAILABILITY_TIMEOUT_MS);
864
972
  try {
865
- const model = this._currentGenAI().getGenerativeModel({ model: DEFAULT_MODEL });
866
- const generation = Promise.resolve(model.generateContent('hi', { signal: controller.signal }));
973
+ const body = { contents: [{ role: 'user', parts: [{ text: 'hi' }] }] };
974
+ const generation = this._restGenerateContent(DEFAULT_MODEL, this._getApiKey(), body, controller.signal);
867
975
  generation.catch(() => {});
868
976
  await Promise.race([
869
977
  generation,
@@ -341,9 +341,15 @@ export function toAnthropicMessages(transcript, availableTools, provider = '') {
341
341
  // Tool-result user messages a provider request ended with (see the
342
342
  // `sentRequestTail` meta finalizeInterruptedTurn stamps).
343
343
  const sent = new WeakSet();
344
+ // Set while the previous message was a dropped empty assistant final: the
345
+ // user message after it starts a new turn and is never folded into the
346
+ // tool_result the dropped turn ended on (it would read as tool output).
347
+ let afterDroppedFinal = false;
344
348
  for (let idx = 0; idx < messages.length; idx++) {
345
349
  const m = messages[idx];
346
350
  if (m.role === 'system') continue;
351
+ const turnBoundary = afterDroppedFinal;
352
+ afterDroppedFinal = false;
347
353
  const orderedReplay = m.role === 'assistant' ? providerReplayItems(m, 'anthropic') : undefined;
348
354
  if (
349
355
  m.role === 'assistant' &&
@@ -441,6 +447,7 @@ export function toAnthropicMessages(transcript, availableTools, provider = '') {
441
447
  if (
442
448
  m.role === 'user' &&
443
449
  !isSteering &&
450
+ !turnBoundary &&
444
451
  foldUserTextIntoToolResultTail(result, normalizeContentForAnthropic(m.content))
445
452
  ) {
446
453
  // A reminder folded before the request was sent ends that request.
@@ -450,7 +457,10 @@ export function toAnthropicMessages(transcript, availableTools, provider = '') {
450
457
  // An assistant turn with no text and no blocks (a refused or empty final
451
458
  // kept in the transcript for diagnosis) has nothing to replay; sending it
452
459
  // would be a blank text block the API rejects on every later request.
453
- if (m.role === 'assistant' && isBlankText(m.content)) continue;
460
+ if (m.role === 'assistant' && isBlankText(m.content)) {
461
+ afterDroppedFinal = true;
462
+ continue;
463
+ }
454
464
  const content = normalizeContentForAnthropic(m.content);
455
465
  result.push(m.role === 'assistant' ? assistantWireMessage(m, content) : { role: m.role, content });
456
466
  }
@@ -168,15 +168,27 @@ function refPointer(node, matches, ptr = '') {
168
168
  return undefined;
169
169
  }
170
170
 
171
- function assertValidDraft202012(schema) {
171
+ function newAjv() {
172
172
  const ajv = new Ajv2020({ strict: false, validateFormats: false, validateSchema: false });
173
173
  // Draft-04 `id` is an annotation providers ignore; Ajv would refuse it.
174
174
  ajv.removeKeyword('id');
175
175
  ajv.addKeyword('id');
176
- if (!ajv.validateSchema(schema)) {
177
- const [error] = ajv.errors;
176
+ return ajv;
177
+ }
178
+
179
+ // Compiling the 2020-12 meta-schema validator costs 10-25 ms per Ajv instance
180
+ // and runs on the event loop. validateSchema keeps no per-schema state, so one
181
+ // shared instance pays it once; each schema itself still compiles on a fresh
182
+ // instance (a shared one would reject a repeated or conflicting `$id`).
183
+ let metaSchemaAjv;
184
+
185
+ function assertValidDraft202012(schema) {
186
+ metaSchemaAjv ??= newAjv();
187
+ if (!metaSchemaAjv.validateSchema(schema)) {
188
+ const [error] = metaSchemaAjv.errors;
178
189
  throw new SchemaProblem(error.instancePath, `not a valid JSON Schema 2020-12 schema: ${error.message}`);
179
190
  }
191
+ const ajv = newAjv();
180
192
  try {
181
193
  ajv.compile(schema);
182
194
  } catch (error) {
@@ -29,10 +29,11 @@ export async function fetchGeminiModelPages(apiKey, fetchFn = fetch) {
29
29
  const items = [];
30
30
  let pageToken = '';
31
31
  do {
32
- const params = new URLSearchParams({ key: apiKey, pageSize: '1000' });
32
+ const params = new URLSearchParams({ pageSize: '1000' });
33
33
  if (pageToken) params.set('pageToken', pageToken);
34
34
  const url = `https://generativelanguage.googleapis.com/v1beta/models?${params}`;
35
35
  const res = await fetchFn(url, {
36
+ headers: { 'x-goog-api-key': apiKey },
36
37
  signal: AbortSignal.timeout(60_000),
37
38
  dispatcher: getLlmDispatcher(),
38
39
  });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A stream's success result carries its own generation window: from this
3
+ * stream's first output to its end. Usage accounting computes speed from it
4
+ * (usage-accounting.mjs), so a retried, reconnected or fallen-back send is
5
+ * timed by the stream that answered, never by an abandoned attempt or the
6
+ * backoff before it. A result without one (a non-streaming recovery, a
7
+ * stream that showed no output start) has no speed.
8
+ */
9
+ export const generationWindow = (firstOutputAt) => (firstOutputAt ? { generationMs: Date.now() - firstOutputAt } : {});
@@ -24,6 +24,7 @@ import { compatFirstByteWindowMs, compatRetryOptions, withCompatRetryProfile } f
24
24
  import { TIMEOUT_PHASE } from '../../../shared/provider-timeout-phase.mjs';
25
25
  import { preparePdfTextForProvider } from './media-normalization.mjs';
26
26
  import { customWireFormat, isCustomProviderName, resolveCustomReasoning } from './custom-reasoning.mjs';
27
+ import { applyCustomRequestBody, customRequestHeaders } from './custom-provider-config.mjs';
27
28
  import { geminiAcceptedTools } from './gemini-schema.mjs';
28
29
  import { acceptedWireTools } from './lib/claude-bridge-schema.mjs';
29
30
  import { isGeminiFamilyModel } from './gemini-family.mjs';
@@ -124,15 +125,22 @@ export async function sendCompatChat(provider, messages, useModel, tools, opts)
124
125
  });
125
126
  applyCompatProviderChatOptions(params, provider.name, opts, provider.config, modelInfo);
126
127
  profile?.applyParams?.(params, { opts, config: provider.config, modelInfo });
128
+ // A custom provider's own extra JSON goes over the reasoning fields; stream
129
+ // flags below and the app-owned keys stay the app's.
130
+ if (isCustom && provider.customConfig) applyCustomRequestBody(params, provider.customConfig, useModel);
127
131
  const firstByteTimeoutMs = profile?.firstByteTimeoutMs || PROVIDER_FIRST_BYTE_TIMEOUT_MS;
128
132
  const totalSignal = createPassthroughSignal(signal);
129
133
  const cacheRouting = provider.name === 'xai' ? xaiCacheRouting(opts, params, tools || [], useModel) : null;
130
134
  const cacheRoutingKey = cacheRouting?.key || null;
131
135
  // xAI Chat pins a conversation to the server holding its cached prefix with
132
136
  // the x-grok-conv-id header (the Responses equivalent is prompt_cache_key).
133
- const requestHeaders = cacheRoutingKey
137
+ const callerHeaders = cacheRoutingKey
134
138
  ? { 'x-grok-conv-id': cacheRoutingKey, ...(opts.requestHeaders || {}) }
135
139
  : opts.requestHeaders;
140
+ const requestHeaders =
141
+ isCustom && provider.customConfig
142
+ ? customRequestHeaders(provider.customConfig, useModel, callerHeaders)
143
+ : callerHeaders;
136
144
  params.stream = true;
137
145
  params.stream_options = { include_usage: true };
138
146
  let assembled;