@jsonstudio/llms 0.6.2979 → 0.6.3238

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 (246) hide show
  1. package/dist/conversion/args-mapping.js +8 -0
  2. package/dist/conversion/{shared/bridge-actions.js → bridge-actions.js} +2 -1
  3. package/dist/conversion/{shared/bridge-id-utils.js → bridge-id-utils.js} +1 -1
  4. package/dist/conversion/{shared/bridge-instructions.js → bridge-instructions.js} +1 -1
  5. package/dist/conversion/{shared/bridge-message-utils.d.ts → bridge-message-utils.d.ts} +1 -1
  6. package/dist/conversion/{shared/bridge-message-utils.js → bridge-message-utils.js} +5 -149
  7. package/dist/conversion/{shared/bridge-metadata.js → bridge-metadata.js} +1 -1
  8. package/dist/conversion/{shared/bridge-policies.js → bridge-policies.js} +1 -1
  9. package/dist/conversion/codecs/gemini-openai-codec.js +27 -8
  10. package/dist/conversion/codecs/responses-openai-codec.js +1 -1
  11. package/dist/conversion/{shared/compaction-detect.d.ts → compaction-detect.d.ts} +1 -1
  12. package/dist/conversion/compaction-detect.js +4 -0
  13. package/dist/conversion/compat/actions/apply-patch-fixer.js +2 -2
  14. package/dist/conversion/compat/actions/deepseek-web-response.d.ts +0 -1
  15. package/dist/conversion/compat/actions/deepseek-web-response.js +15 -405
  16. package/dist/conversion/compat/actions/harvest-tool-calls-from-text.js +1 -1
  17. package/dist/conversion/compat/actions/lmstudio-responses-fc-ids.js +1 -1
  18. package/dist/conversion/compat/actions/qwen-transform.js +74 -2
  19. package/dist/conversion/compat/actions/snapshot.js +1 -1
  20. package/dist/conversion/compat/antigravity-session-signature.js +36 -0
  21. package/dist/conversion/compat/profiles/chat-deepseek-web.json +0 -22
  22. package/dist/conversion/compat/profiles/chat-glm.json +251 -72
  23. package/dist/conversion/compat/profiles/chat-iflow.json +174 -39
  24. package/dist/conversion/compat/profiles/chat-lmstudio.json +43 -14
  25. package/dist/conversion/hub/operation-table/operation-table-runner.js +2 -2
  26. package/dist/conversion/hub/operation-table/semantic-mappers/anthropic-mapper.js +1 -1
  27. package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.d.ts +8 -0
  28. package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.js +404 -0
  29. package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +5 -381
  30. package/dist/conversion/hub/operation-table/semantic-mappers/gemini-mapper.js +2 -2
  31. package/dist/conversion/hub/operation-table/semantic-mappers/responses-mapper.js +2 -8
  32. package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +1 -0
  33. package/dist/conversion/hub/pipeline/hub-pipeline.js +50 -3
  34. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/index.d.ts +1 -1
  35. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/index.js +62 -0
  36. package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage2_route_select/index.js +3 -1
  37. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage1_sse_decode/index.js +1 -1
  38. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/chat-process-semantics-bridge.d.ts +1 -1
  39. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage1_tool_governance/index.js +42 -29
  40. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage2_finalize/index.js +12 -0
  41. package/dist/conversion/hub/policy/protocol-spec.js +1 -1
  42. package/dist/conversion/hub/process/chat-process-clock-reminders.js +1 -1
  43. package/dist/conversion/hub/process/chat-process-clock-tools.js +1 -1
  44. package/dist/conversion/hub/process/chat-process-continue-execution.js +1 -1
  45. package/dist/conversion/hub/process/chat-process-servertool-orchestration.js +1 -1
  46. package/dist/conversion/hub/process/chat-process-web-search.js +1 -1
  47. package/dist/conversion/hub/response/provider-response.js +14 -5
  48. package/dist/conversion/hub/response/response-mappers.js +23 -1
  49. package/dist/conversion/hub/response/response-runtime.js +28 -5
  50. package/dist/conversion/hub/snapshot-recorder.js +3 -92
  51. package/dist/conversion/hub/tool-governance/engine.d.ts +8 -0
  52. package/dist/conversion/hub/tool-governance/engine.js +40 -193
  53. package/dist/conversion/hub/tool-governance/rules.js +73 -69
  54. package/dist/conversion/hub/tool-surface/tool-surface-engine.js +1 -1
  55. package/dist/conversion/index.d.ts +1 -2
  56. package/dist/conversion/index.js +1 -2
  57. package/dist/conversion/{shared/jsonish.js → jsonish.js} +1 -1
  58. package/dist/conversion/{shared/mcp-injection.js → mcp-injection.js} +1 -1
  59. package/dist/conversion/media.js +4 -0
  60. package/dist/conversion/{shared/metadata-passthrough.d.ts → metadata-passthrough.d.ts} +1 -1
  61. package/dist/conversion/{shared/metadata-passthrough.js → metadata-passthrough.js} +2 -2
  62. package/dist/conversion/payload-budget.js +47 -0
  63. package/dist/conversion/protocol-field-allowlists.d.ts +7 -0
  64. package/dist/conversion/protocol-field-allowlists.js +9 -0
  65. package/dist/conversion/{shared/protocol-state.d.ts → protocol-state.d.ts} +2 -2
  66. package/dist/conversion/{shared/protocol-state.js → protocol-state.js} +2 -2
  67. package/dist/conversion/{shared/errors.d.ts → provider-protocol-error.d.ts} +0 -3
  68. package/dist/conversion/provider-protocol-error.js +25 -0
  69. package/dist/conversion/responses/responses-openai-bridge/response-payload.js +8 -5
  70. package/dist/conversion/responses/responses-openai-bridge/types.d.ts +1 -1
  71. package/dist/conversion/responses/responses-openai-bridge.d.ts +1 -1
  72. package/dist/conversion/responses/responses-openai-bridge.js +43 -10
  73. package/dist/conversion/{shared/runtime-metadata.d.ts → runtime-metadata.d.ts} +1 -1
  74. package/dist/conversion/{shared/runtime-metadata.js → runtime-metadata.js} +2 -2
  75. package/dist/conversion/shared/anthropic-message-utils.js +19 -8
  76. package/dist/conversion/shared/chat-request-filters.d.ts +3 -4
  77. package/dist/conversion/shared/chat-request-filters.js +22 -78
  78. package/dist/conversion/shared/gemini-tool-utils.d.ts +1 -1
  79. package/dist/conversion/shared/openai-finalizer.js +1 -0
  80. package/dist/conversion/shared/openai-message-normalize.js +2 -2
  81. package/dist/conversion/shared/reasoning-normalizer.js +6 -0
  82. package/dist/conversion/shared/reasoning-utils.js +5 -2
  83. package/dist/conversion/shared/responses-conversation-store.js +1 -1
  84. package/dist/conversion/shared/responses-output-builder.js +55 -11
  85. package/dist/conversion/shared/responses-reasoning-registry.d.ts +14 -2
  86. package/dist/conversion/shared/responses-reasoning-registry.js +34 -6
  87. package/dist/conversion/shared/responses-response-utils.js +99 -9
  88. package/dist/conversion/shared/responses-tool-utils.js +1 -1
  89. package/dist/conversion/shared/text-markup-normalizer/normalize.d.ts +1 -1
  90. package/dist/conversion/shared/text-markup-normalizer.d.ts +2 -2
  91. package/dist/conversion/shared/text-markup-normalizer.js +1 -1
  92. package/dist/conversion/shared/tool-filter-pipeline.js +1 -1
  93. package/dist/conversion/shared/tool-governor.js +3 -3
  94. package/dist/conversion/shared/tool-mapping.d.ts +1 -1
  95. package/dist/conversion/{shared/snapshot-utils.d.ts → snapshot-utils.d.ts} +11 -0
  96. package/dist/conversion/{shared/snapshot-utils.js → snapshot-utils.js} +14 -23
  97. package/dist/conversion/types/text-markup-normalizer.d.ts +13 -0
  98. package/dist/conversion/types/text-markup-normalizer.js +1 -0
  99. package/dist/filters/special/request-tools-normalize.js +1 -1
  100. package/dist/filters/special/response-tool-text-canonicalize.js +2 -2
  101. package/dist/native/router_hotpath_napi.node +0 -0
  102. package/dist/quota/quota-manager.js +31 -59
  103. package/dist/quota/quota-state.js +14 -7
  104. package/dist/router/virtual-router/bootstrap/profile-builder.d.ts +1 -0
  105. package/dist/router/virtual-router/bootstrap/profile-builder.js +13 -0
  106. package/dist/router/virtual-router/bootstrap/provider-normalization.d.ts +2 -0
  107. package/dist/router/virtual-router/bootstrap/provider-normalization.js +4 -1
  108. package/dist/router/virtual-router/bootstrap/streaming-helpers.d.ts +7 -0
  109. package/dist/router/virtual-router/bootstrap/streaming-helpers.js +44 -0
  110. package/dist/router/virtual-router/bootstrap.js +2 -0
  111. package/dist/router/virtual-router/engine/routing-state/store.d.ts +1 -2
  112. package/dist/router/virtual-router/engine/routing-state/store.js +2 -2
  113. package/dist/router/virtual-router/engine-legacy/config.d.ts +11 -0
  114. package/dist/router/virtual-router/engine-legacy/config.js +108 -0
  115. package/dist/router/virtual-router/engine-legacy/direct-model.d.ts +10 -0
  116. package/dist/router/virtual-router/engine-legacy/direct-model.js +38 -0
  117. package/dist/router/virtual-router/engine-legacy/health.d.ts +13 -0
  118. package/dist/router/virtual-router/engine-legacy/health.js +104 -0
  119. package/dist/router/virtual-router/engine-legacy/helpers.d.ts +16 -0
  120. package/dist/router/virtual-router/engine-legacy/helpers.js +226 -0
  121. package/dist/router/virtual-router/engine-legacy/route-finalize.d.ts +9 -0
  122. package/dist/router/virtual-router/engine-legacy/route-finalize.js +84 -0
  123. package/dist/router/virtual-router/engine-legacy/route-selection.d.ts +17 -0
  124. package/dist/router/virtual-router/engine-legacy/route-selection.js +205 -0
  125. package/dist/router/virtual-router/engine-legacy/route-state-allowlist.d.ts +3 -0
  126. package/dist/router/virtual-router/engine-legacy/route-state-allowlist.js +36 -0
  127. package/dist/router/virtual-router/engine-legacy/route-state.d.ts +12 -0
  128. package/dist/router/virtual-router/engine-legacy/route-state.js +386 -0
  129. package/dist/router/virtual-router/engine-legacy/route-utils.d.ts +19 -0
  130. package/dist/router/virtual-router/engine-legacy/route-utils.js +212 -0
  131. package/dist/router/virtual-router/engine-legacy/routing.d.ts +8 -0
  132. package/dist/router/virtual-router/engine-legacy/routing.js +8 -0
  133. package/dist/router/virtual-router/engine-legacy/selection-core.d.ts +28 -0
  134. package/dist/router/virtual-router/engine-legacy/selection-core.js +112 -0
  135. package/dist/router/virtual-router/engine-legacy/selection-state.d.ts +16 -0
  136. package/dist/router/virtual-router/engine-legacy/selection-state.js +187 -0
  137. package/dist/router/virtual-router/engine-legacy/state-accessors.d.ts +21 -0
  138. package/dist/router/virtual-router/engine-legacy/state-accessors.js +118 -0
  139. package/dist/router/virtual-router/engine-legacy.d.ts +123 -0
  140. package/dist/router/virtual-router/engine-legacy.js +194 -0
  141. package/dist/router/virtual-router/engine-logging.d.ts +2 -0
  142. package/dist/router/virtual-router/engine-logging.js +7 -2
  143. package/dist/router/virtual-router/engine-selection/key-parsing.js +0 -3
  144. package/dist/router/virtual-router/engine-selection/native-chat-request-filter-semantics.d.ts +1 -0
  145. package/dist/router/virtual-router/engine-selection/native-chat-request-filter-semantics.js +54 -0
  146. package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.d.ts +10 -0
  147. package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.js +67 -0
  148. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.d.ts +30 -0
  149. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.js +202 -0
  150. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.d.ts +2 -0
  151. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.js +83 -0
  152. package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +43 -2
  153. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +75 -0
  154. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +205 -0
  155. package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.d.ts +3 -0
  156. package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.js +109 -0
  157. package/dist/router/virtual-router/engine-selection/native-virtual-router-engine-proxy.d.ts +16 -0
  158. package/dist/router/virtual-router/engine-selection/native-virtual-router-engine-proxy.js +14 -0
  159. package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.d.ts +2 -0
  160. package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.js +86 -0
  161. package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.js +100 -0
  162. package/dist/router/virtual-router/engine-selection/tier-selection-select.js +99 -0
  163. package/dist/router/virtual-router/engine.d.ts +22 -105
  164. package/dist/router/virtual-router/engine.js +274 -1641
  165. package/dist/router/virtual-router/load-balancer.d.ts +8 -0
  166. package/dist/router/virtual-router/load-balancer.js +65 -2
  167. package/dist/router/virtual-router/provider-registry.js +2 -0
  168. package/dist/router/virtual-router/routing-instructions/clean.d.ts +3 -0
  169. package/dist/router/virtual-router/routing-instructions/clean.js +34 -0
  170. package/dist/router/virtual-router/routing-instructions/parse.d.ts +18 -0
  171. package/dist/router/virtual-router/routing-instructions/parse.js +377 -0
  172. package/dist/router/virtual-router/routing-instructions/state.d.ts +4 -0
  173. package/dist/router/virtual-router/routing-instructions/state.js +245 -0
  174. package/dist/router/virtual-router/routing-instructions/types.d.ts +70 -0
  175. package/dist/router/virtual-router/routing-instructions/types.js +2 -0
  176. package/dist/router/virtual-router/routing-instructions.d.ts +5 -89
  177. package/dist/router/virtual-router/routing-instructions.js +4 -655
  178. package/dist/router/virtual-router/sticky-session-store.d.ts +4 -0
  179. package/dist/router/virtual-router/sticky-session-store.js +19 -81
  180. package/dist/router/virtual-router/tool-signals.js +21 -3
  181. package/dist/router/virtual-router/types.d.ts +4 -0
  182. package/dist/servertool/clock/session-scope.js +32 -1
  183. package/dist/servertool/engine.js +79 -8
  184. package/dist/servertool/handlers/antigravity-thought-signature-bootstrap.js +1 -1
  185. package/dist/servertool/handlers/clock-auto.js +1 -1
  186. package/dist/servertool/handlers/clock.js +1 -1
  187. package/dist/servertool/handlers/compaction-detect.d.ts +1 -1
  188. package/dist/servertool/handlers/compaction-detect.js +1 -1
  189. package/dist/servertool/handlers/gemini-empty-reply-continue.js +1 -1
  190. package/dist/servertool/handlers/iflow-model-error-retry.js +1 -1
  191. package/dist/servertool/handlers/recursive-detection-guard.js +1 -1
  192. package/dist/servertool/handlers/review.js +1 -1
  193. package/dist/servertool/handlers/stop-message-auto/iflow-followup.js +1 -1
  194. package/dist/servertool/handlers/stop-message-auto/runtime-utils.js +1 -1
  195. package/dist/servertool/handlers/stop-message-auto.js +1 -1
  196. package/dist/servertool/handlers/vision.js +1 -1
  197. package/dist/servertool/handlers/web-search.js +1 -1
  198. package/dist/servertool/reenter-backend.js +1 -1
  199. package/dist/servertool/server-side-tools.js +2 -2
  200. package/dist/servertool/stop-gateway-context.js +1 -1
  201. package/dist/servertool/stop-message-compare-context.js +1 -1
  202. package/dist/sse/json-to-sse/event-generators/responses.d.ts +4 -0
  203. package/dist/sse/json-to-sse/event-generators/responses.js +95 -1
  204. package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +6 -4
  205. package/dist/sse/sse-to-json/builders/response-builder.d.ts +8 -0
  206. package/dist/sse/sse-to-json/builders/response-builder.js +162 -4
  207. package/dist/sse/sse-to-json/responses-sse-to-json-converter.js +2 -0
  208. package/dist/sse/types/responses-types.d.ts +6 -2
  209. package/dist/tools/apply-patch/structured/coercion.js +5 -0
  210. package/dist/tools/args-json.js +29 -0
  211. package/package.json +8 -5
  212. package/dist/conversion/shared/args-mapping.js +0 -77
  213. package/dist/conversion/shared/compaction-detect.js +0 -4
  214. package/dist/conversion/shared/errors.js +0 -31
  215. package/dist/conversion/shared/media.js +0 -4
  216. package/dist/conversion/shared/payload-budget.js +0 -165
  217. package/dist/conversion/shared/protocol-field-allowlists.d.ts +0 -7
  218. package/dist/conversion/shared/protocol-field-allowlists.js +0 -149
  219. package/dist/conversion/shared/snapshot-hooks.d.ts +0 -11
  220. package/dist/conversion/shared/snapshot-hooks.js +0 -503
  221. package/dist/conversion/shared/text-markup-normalizer/extractors-apply-patch.d.ts +0 -2
  222. package/dist/conversion/shared/text-markup-normalizer/extractors-apply-patch.js +0 -129
  223. package/dist/conversion/shared/text-markup-normalizer/extractors-json.d.ts +0 -4
  224. package/dist/conversion/shared/text-markup-normalizer/extractors-json.js +0 -637
  225. package/dist/conversion/shared/text-markup-normalizer/extractors-shared.d.ts +0 -21
  226. package/dist/conversion/shared/text-markup-normalizer/extractors-shared.js +0 -177
  227. package/dist/conversion/shared/text-markup-normalizer/extractors-transcript.d.ts +0 -5
  228. package/dist/conversion/shared/text-markup-normalizer/extractors-transcript.js +0 -385
  229. package/dist/conversion/shared/text-markup-normalizer/extractors-xml.d.ts +0 -10
  230. package/dist/conversion/shared/text-markup-normalizer/extractors-xml.js +0 -602
  231. package/dist/conversion/shared/text-markup-normalizer/extractors.d.ts +0 -5
  232. package/dist/conversion/shared/text-markup-normalizer/extractors.js +0 -4
  233. package/dist/conversion/shared/tool-canonicalizer.d.ts +0 -2
  234. package/dist/conversion/shared/tool-canonicalizer.js +0 -38
  235. /package/dist/conversion/{shared/args-mapping.d.ts → args-mapping.d.ts} +0 -0
  236. /package/dist/conversion/{shared/bridge-actions.d.ts → bridge-actions.d.ts} +0 -0
  237. /package/dist/conversion/{shared/bridge-id-utils.d.ts → bridge-id-utils.d.ts} +0 -0
  238. /package/dist/conversion/{shared/bridge-instructions.d.ts → bridge-instructions.d.ts} +0 -0
  239. /package/dist/conversion/{shared/bridge-metadata.d.ts → bridge-metadata.d.ts} +0 -0
  240. /package/dist/conversion/{shared/bridge-policies.d.ts → bridge-policies.d.ts} +0 -0
  241. /package/dist/conversion/{shared/jsonish.d.ts → jsonish.d.ts} +0 -0
  242. /package/dist/conversion/{shared/mcp-injection.d.ts → mcp-injection.d.ts} +0 -0
  243. /package/dist/conversion/{shared/media.d.ts → media.d.ts} +0 -0
  244. /package/dist/conversion/{shared/payload-budget.d.ts → payload-budget.d.ts} +0 -0
  245. /package/dist/conversion/{shared → types}/bridge-message-types.d.ts +0 -0
  246. /package/dist/conversion/{shared → types}/bridge-message-types.js +0 -0
@@ -1,1695 +1,328 @@
1
- import { ProviderHealthManager } from './health-manager.js';
1
+ import { VirtualRouterError, VirtualRouterErrorCode } from './types.js';
2
+ import { ROUTING_INSTRUCTION_MARKER_PATTERN } from './routing-instructions/types.js';
3
+ import { parseRoutingInstructions } from './routing-instructions/parse.js';
4
+ import { createVirtualRouterEngineProxy } from './engine-selection/native-virtual-router-engine-proxy.js';
5
+ import { cleanRoutingInstructionMarkersWithNative, parseRoutingInstructionKindsWithNative } from './engine-selection/native-virtual-router-routing-instructions-semantics.js';
6
+ import { getLatestUserTextFromResponsesContext, hasLatestUserRoutingInstructionMarker, hasRoutingInstructionMarkerInResponsesContext } from './engine-legacy/helpers.js';
2
7
  import { ProviderRegistry } from './provider-registry.js';
3
- import { RouteLoadBalancer } from './load-balancer.js';
4
- import { RoutingClassifier } from './classifier.js';
5
- import { buildRoutingFeatures } from './features.js';
6
- import { ContextAdvisor } from './context-advisor.js';
7
- import { DEFAULT_ROUTE, ROUTE_PRIORITY, VirtualRouterError, VirtualRouterErrorCode } from './types.js';
8
- import { getStatsCenter } from '../../telemetry/stats-center.js';
9
- import { parseRoutingInstructions, applyRoutingInstructions, cleanMessagesFromRoutingInstructions } from './routing-instructions.js';
10
- import { extractMessageText } from './message-utils.js';
11
- import { loadRoutingInstructionStateSync, saveRoutingInstructionStateAsync, saveRoutingInstructionStateSync } from './sticky-session-store.js';
12
- import { buildHitReason, createVirtualRouterHitRecord, formatVirtualRouterHit, toVirtualRouterHitEvent } from './engine-logging.js';
13
- import { selectDirectProviderModel, selectFromStickyPool, selectProviderImpl } from './engine/routing-pools/index.js';
14
- import { applyQuotaDepletedImpl, applyQuotaRecoveryImpl, applySeriesCooldownImpl, applyAntigravityRiskPolicyImpl, handleProviderFailureImpl, mapProviderErrorImpl } from './engine/health/index.js';
15
- import { hydrateAntigravityAliasLeaseStoreIfNeeded, recordAntigravitySessionLease, resolveAntigravityAliasReuseCooldownMs } from './engine/antigravity/alias-lease.js';
16
- import { buildMetadataInstructions, resolveRoutingMode } from './engine/routing-state/metadata.js';
17
- import { getRoutingInstructionState, persistRoutingInstructionState, resolveStopMessageScope } from './engine/routing-state/store.js';
18
- import { ensureStopMessageModeMaxRepeats } from './routing-stop-message-state-codec.js';
19
- import { extractKeyAlias, extractKeyIndex, extractProviderId, getProviderModelId } from './engine/provider-key/parse.js';
20
- import { resolveSessionScope as resolveSessionScopeImpl, resolveStickyKey as resolveStickyKeyImpl } from './engine/routing-state/keys.js';
21
- import { RouteAnalytics } from './engine/route-analytics.js';
22
- import { StickySessionManager } from './engine/sticky-session-manager.js';
23
- import { CooldownManager } from './engine/cooldown-manager.js';
24
- const DEFAULT_STOP_MESSAGE_MAX_REPEATS = 10;
25
- function isStopScopeTraceEnabled() {
26
- const raw = String(process.env.ROUTECODEX_STOP_SCOPE_TRACE ?? process.env.RCC_STOP_SCOPE_TRACE ?? '')
27
- .trim()
28
- .toLowerCase();
29
- return (raw === '1' ||
30
- raw === 'true' ||
31
- raw === 'on' ||
32
- raw === 'yes' ||
33
- raw === 'debug' ||
34
- raw === 'trace' ||
35
- raw === 'verbose');
36
- }
37
- function normalizeStopMessageStageMode(value) {
38
- if (typeof value !== 'string') {
39
- return undefined;
40
- }
41
- const normalized = value.trim().toLowerCase();
42
- if (normalized === 'on' || normalized === 'off' || normalized === 'auto') {
43
- return normalized;
44
- }
45
- return undefined;
46
- }
47
- function normalizeStopMessageAiMode(value) {
48
- if (typeof value !== 'string') {
49
- return undefined;
50
- }
51
- const normalized = value.trim().toLowerCase();
52
- if (normalized === 'on' || normalized === 'off') {
53
- return normalized;
54
- }
55
- return undefined;
56
- }
57
- function stripStopMessageFields(state) {
58
- return {
59
- ...state,
60
- stopMessageSource: undefined,
61
- stopMessageText: undefined,
62
- stopMessageMaxRepeats: undefined,
63
- stopMessageUsed: undefined,
64
- stopMessageUpdatedAt: undefined,
65
- stopMessageLastUsedAt: undefined,
66
- stopMessageStageMode: undefined,
67
- stopMessageAiMode: undefined,
68
- stopMessageAiSeedPrompt: undefined,
69
- stopMessageAiHistory: undefined
70
- };
71
- }
72
- function stripClientInjectScopedFields(state) {
73
- return {
74
- ...stripStopMessageFields(state),
75
- preCommandSource: undefined,
76
- preCommandScriptPath: undefined,
77
- preCommandUpdatedAt: undefined
78
- };
79
- }
80
- function hasClientInjectScopedFields(state) {
81
- if (!state) {
82
- return false;
83
- }
84
- return (typeof state.stopMessageText === 'string' ||
85
- typeof state.stopMessageMaxRepeats === 'number' ||
86
- typeof state.stopMessageUsed === 'number' ||
87
- typeof state.stopMessageStageMode === 'string' ||
88
- typeof state.stopMessageAiMode === 'string' ||
89
- typeof state.stopMessageAiSeedPrompt === 'string' ||
90
- Array.isArray(state.stopMessageAiHistory) ||
91
- typeof state.preCommandScriptPath === 'string' ||
92
- typeof state.preCommandUpdatedAt === 'number');
93
- }
94
- function hasRoutingInstructionMarker(messages) {
95
- for (const message of messages) {
96
- if (!message || message.role !== 'user') {
97
- continue;
98
- }
99
- const content = extractMessageText(message);
100
- if (!content) {
101
- continue;
102
- }
103
- if (/<\*\*[\s\S]*?\*\*>/.test(content)) {
104
- return true;
8
+ import { resolveStopMessageScope } from './engine/routing-state/store.js';
9
+ export class VirtualRouterEngine {
10
+ nativeProxy;
11
+ registry;
12
+ routingInstructionStateStore;
13
+ constructor(deps) {
14
+ this.nativeProxy = createVirtualRouterEngineProxy();
15
+ this.registry = new ProviderRegistry();
16
+ this.routingInstructionStateStore = new Map();
17
+ if (deps) {
18
+ this.nativeProxy.updateDeps(deps);
105
19
  }
106
20
  }
107
- return false;
108
- }
109
- function hasLatestUserRoutingInstructionMarker(messages) {
110
- for (let idx = messages.length - 1; idx >= 0; idx -= 1) {
111
- const message = messages[idx];
112
- if (!message) {
113
- continue;
114
- }
115
- if (message.role !== 'user') {
116
- return false;
117
- }
118
- const content = extractMessageText(message);
119
- if (!content) {
120
- return false;
121
- }
122
- return /<\*\*[\s\S]*?\*\*>/.test(content);
21
+ get antigravitySessionAliasStore() {
22
+ return this.nativeProxy.antigravitySessionAliasStore;
123
23
  }
124
- return false;
125
- }
126
- function isServerToolFollowupRequest(metadata) {
127
- if (!metadata || typeof metadata !== 'object') {
128
- return false;
24
+ get routingInstructionState() {
25
+ return this.routingInstructionStateStore;
129
26
  }
130
- const rt = metadata.__rt;
131
- if (!rt || typeof rt !== 'object' || Array.isArray(rt)) {
132
- return false;
27
+ get providerRegistry() {
28
+ return this.registry;
133
29
  }
134
- const flag = rt.serverToolFollowup;
135
- return (flag === true ||
136
- (typeof flag === 'string' && flag.trim().toLowerCase() === 'true'));
137
- }
138
- export class VirtualRouterEngine {
139
- routing = {};
140
- providerRegistry = new ProviderRegistry();
141
- healthManager = new ProviderHealthManager();
142
- get providerCooldowns() { return this.cooldownManager.getCooldownMap(); }
143
- loadBalancer = new RouteLoadBalancer();
144
- classifier = new RoutingClassifier({});
145
- contextAdvisor = new ContextAdvisor();
146
- contextRouting;
147
- routeAnalytics = new RouteAnalytics();
148
- stickySessionManager = new StickySessionManager();
149
- cooldownManager;
150
- antigravityLeasePersistence = { loadedOnce: false, loadedMtimeMs: null, flushTimer: null };
151
- debug = console; // thin hook; host may monkey-patch for colored logging
152
- healthConfig = null;
153
- statsCenter = getStatsCenter();
154
- // Derived flags from VirtualRouterConfig/routing used by process / response layers.
155
- webSearchForce = false;
156
- healthStore;
157
- routingStateStore = {
158
- loadSync: loadRoutingInstructionStateSync,
159
- saveAsync: saveRoutingInstructionStateAsync,
160
- saveSync: saveRoutingInstructionStateSync
161
- };
162
- routingInstructionState = new Map();
163
- quotaView;
164
- /**
165
- * Backward-compatible test/debug surface used by existing regression scripts.
166
- * Keep this as a read-only view over StickySessionManager storage.
167
- */
168
- get antigravitySessionAliasStore() {
169
- return this.stickySessionManager.getAllStores().sessionAliasStore;
170
- }
171
- constructor(deps) {
172
- this.cooldownManager = new CooldownManager({
173
- healthStore: deps?.healthStore,
174
- healthConfig: this.healthConfig,
175
- quotaView: deps?.quotaView
176
- });
177
- if (deps?.healthStore) {
178
- this.healthStore = deps.healthStore;
179
- }
180
- if (deps?.routingStateStore) {
181
- this.routingStateStore = deps.routingStateStore;
182
- }
183
- if (deps?.quotaView) {
184
- this.quotaView = deps.quotaView;
185
- }
30
+ initialize(config) {
31
+ this.nativeProxy.initialize(JSON.stringify(config));
32
+ this.registry.load(config.providers ?? {});
33
+ this.routingInstructionStateStore.clear();
186
34
  }
187
35
  updateDeps(deps) {
188
- if (!deps || typeof deps !== 'object') {
189
- return;
190
- }
191
- if ('healthStore' in deps) {
192
- this.healthStore = deps.healthStore ?? undefined;
193
- this.cooldownManager.updateDeps({ healthStore: this.healthStore });
194
- }
195
- if ('routingStateStore' in deps) {
196
- this.routingStateStore =
197
- deps.routingStateStore ??
198
- {
199
- loadSync: loadRoutingInstructionStateSync,
200
- saveAsync: saveRoutingInstructionStateAsync,
201
- saveSync: saveRoutingInstructionStateSync
202
- };
203
- // Routing state store changes require clearing in-memory cache to avoid stale reads.
204
- this.routingInstructionState.clear();
205
- }
206
- if ('quotaView' in deps) {
207
- const prevQuotaEnabled = Boolean(this.quotaView);
208
- this.quotaView = deps.quotaView ?? undefined;
209
- this.cooldownManager.updateDeps({ quotaView: this.quotaView });
210
- const nextQuotaEnabled = Boolean(this.quotaView);
211
- // When quotaView is enabled, health/cooldown decisions must be driven by quotaView only.
212
- // - Enabling quotaView: clear any legacy router-local cooldown TTLs immediately.
213
- // - Disabling quotaView: reload legacy cooldown state from health snapshots.
214
- if (!prevQuotaEnabled && nextQuotaEnabled) {
215
- this.cooldownManager.clearAllCooldowns();
216
- }
217
- else if (prevQuotaEnabled && !nextQuotaEnabled) {
218
- this.cooldownManager.clearAllCooldowns();
219
- this.restoreHealthFromStore();
220
- }
221
- }
36
+ this.nativeProxy.updateDeps(deps);
222
37
  }
223
- parseDirectProviderModel(model) {
224
- const raw = typeof model === 'string' ? model.trim() : '';
225
- if (!raw) {
226
- return null;
227
- }
228
- const firstDot = raw.indexOf('.');
229
- if (firstDot <= 0 || firstDot === raw.length - 1) {
230
- return null;
231
- }
232
- const providerId = raw.slice(0, firstDot).trim();
233
- const modelId = raw.slice(firstDot + 1).trim();
234
- if (!providerId || !modelId) {
235
- return null;
236
- }
237
- if (this.providerRegistry.listProviderKeys(providerId).length === 0) {
238
- return null;
239
- }
240
- return { providerId, modelId };
38
+ updateVirtualRouterConfig(config) {
39
+ this.nativeProxy.updateVirtualRouterConfig(JSON.stringify(config));
40
+ this.registry.load(config.providers ?? {});
241
41
  }
242
- shouldFallbackDirectModelForMedia(direct, features) {
243
- if (!features.hasImageAttachment) {
244
- return false;
245
- }
246
- const providerId = direct.providerId.trim().toLowerCase();
247
- const modelId = direct.modelId.trim().toLowerCase();
248
- if (providerId !== 'qwen') {
249
- return false;
250
- }
251
- const isQwen35Plus = modelId === 'qwen3.5-plus' || modelId === 'qwen3-5-plus' || modelId === 'qwen3_5-plus';
252
- if (!isQwen35Plus) {
253
- return false;
254
- }
255
- if (!(features.hasVideoAttachment === true && features.hasLocalVideoAttachment === true)) {
256
- return false;
257
- }
258
- return this.routeHasTargets(this.routing.vision);
42
+ markProviderCooldown(providerKey, cooldownMs) {
43
+ this.nativeProxy.markProviderCooldown(providerKey, cooldownMs);
259
44
  }
260
- initialize(config) {
261
- this.validateConfig(config);
262
- this.routing = config.routing;
263
- this.providerRegistry.load(config.providers);
264
- this.healthManager.configure(config.health);
265
- this.healthConfig = config.health ?? null;
266
- this.healthManager.registerProviders(Object.keys(config.providers));
267
- this.cooldownManager.clearAllCooldowns();
268
- this.restoreHealthFromStore();
269
- this.loadBalancer = new RouteLoadBalancer(config.loadBalancing);
270
- const aliasReuseCooldownMs = resolveAntigravityAliasReuseCooldownMs(config);
271
- this.stickySessionManager = new StickySessionManager(aliasReuseCooldownMs);
272
- hydrateAntigravityAliasLeaseStoreIfNeeded({
273
- force: true,
274
- leaseStore: this.stickySessionManager.getAllStores().aliasLeaseStore,
275
- persistence: this.antigravityLeasePersistence,
276
- aliasReuseCooldownMs: this.stickySessionManager.getAliasReuseCooldownMs()
277
- });
278
- this.classifier = new RoutingClassifier(config.classifier);
279
- this.contextRouting = config.contextRouting ?? { warnRatio: 0.9, hardLimit: false };
280
- this.contextAdvisor.configure(this.contextRouting);
281
- this.webSearchForce = config.webSearch?.force === true;
282
- this.routeAnalytics.getAllRouteStats().clear();
283
- for (const routeName of Object.keys(this.routing)) {
284
- this.routeAnalytics.getRouteStats(routeName) || this.routeAnalytics.incrementRouteStat(routeName, '', { timestampMs: Date.now(), stopMessage: { active: false } });
285
- }
45
+ clearProviderCooldown(providerKey) {
46
+ this.nativeProxy.clearProviderCooldown(providerKey);
286
47
  }
287
48
  route(request, metadata) {
288
- const stickyKey = this.resolveStickyKey(metadata);
289
- const sessionScope = this.resolveSessionScope(metadata);
290
- const stopMessageScope = resolveStopMessageScope(metadata);
291
- // Route sticky state remains session/request scoped for routing behavior,
292
- // but stopMessage state is tmux/clockd scoped and merged separately below.
293
- const stateKey = sessionScope || stickyKey || 'default';
294
- const baseState = getRoutingInstructionState(stateKey, this.routingInstructionState, this.routingStateStore);
295
- let routingState = stripStopMessageFields(baseState);
296
- const metadataInstructions = buildMetadataInstructions(metadata);
297
- if (metadataInstructions.length > 0) {
298
- routingState = applyRoutingInstructions(metadataInstructions, routingState);
299
- }
300
- const disableStickyRoutes = metadata &&
301
- typeof metadata === 'object' &&
302
- metadata.disableStickyRoutes === true;
303
- if (disableStickyRoutes && (routingState.stickyTarget || routingState.preferTarget)) {
304
- routingState = {
305
- ...routingState,
306
- stickyTarget: undefined,
307
- preferTarget: undefined
308
- };
309
- }
310
- if (stopMessageScope) {
311
- const sessionState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
312
- if (ensureStopMessageModeMaxRepeats(sessionState)) {
313
- this.routingInstructionState.set(stopMessageScope, sessionState);
314
- persistRoutingInstructionState(stopMessageScope, sessionState, this.routingStateStore);
315
- }
316
- if (typeof sessionState.stopMessageText === 'string' ||
317
- typeof sessionState.stopMessageMaxRepeats === 'number' ||
318
- typeof sessionState.stopMessageStageMode === 'string' ||
319
- typeof sessionState.stopMessageAiMode === 'string' ||
320
- typeof sessionState.stopMessageAiSeedPrompt === 'string' ||
321
- Array.isArray(sessionState.stopMessageAiHistory)) {
322
- routingState = {
323
- ...routingState,
324
- stopMessageText: sessionState.stopMessageText,
325
- stopMessageMaxRepeats: sessionState.stopMessageMaxRepeats,
326
- stopMessageUsed: sessionState.stopMessageUsed,
327
- stopMessageUpdatedAt: sessionState.stopMessageUpdatedAt,
328
- stopMessageLastUsedAt: sessionState.stopMessageLastUsedAt,
329
- stopMessageStageMode: sessionState.stopMessageStageMode,
330
- stopMessageAiMode: sessionState.stopMessageAiMode,
331
- stopMessageAiSeedPrompt: sessionState.stopMessageAiSeedPrompt,
332
- stopMessageAiHistory: sessionState.stopMessageAiHistory
333
- };
334
- }
335
- if (typeof sessionState.preCommandScriptPath === 'string' && sessionState.preCommandScriptPath.trim()) {
336
- routingState = {
337
- ...routingState,
338
- preCommandSource: sessionState.preCommandSource,
339
- preCommandScriptPath: sessionState.preCommandScriptPath,
340
- preCommandUpdatedAt: sessionState.preCommandUpdatedAt
341
- };
342
- }
343
- }
344
- const parsedInstructions = parseRoutingInstructions(request.messages);
345
- const markerDetected = hasRoutingInstructionMarker(request.messages);
346
- if (markerDetected && isStopScopeTraceEnabled()) {
347
- const parsedTypes = parsedInstructions.map((entry) => entry.type);
348
- this.debug?.log?.(`[virtual-router][instruction_parse] requestId=${metadata.requestId || 'n/a'} marker=detected parsed=${parsedTypes.join(',') || 'none'}`);
349
- }
350
- else if (isStopScopeTraceEnabled()) {
351
- const latestUserText = [...request.messages]
352
- .reverse()
353
- .find((message) => message?.role === 'user');
354
- const latestText = latestUserText ? extractMessageText(latestUserText).trim() : '';
355
- if (latestText && /stopmessage/i.test(latestText)) {
356
- const preview = latestText.replace(/\s+/g, ' ').slice(0, 120);
357
- this.debug?.log?.(`[virtual-router][instruction_parse] requestId=${metadata.requestId || 'n/a'} marker=missing contains_stopmessage=yes preview=${preview}`);
358
- }
359
- }
360
- const serverToolFollowup = isServerToolFollowupRequest(metadata);
361
- const latestUserHasMarker = hasLatestUserRoutingInstructionMarker(request.messages);
362
- let instructions = parsedInstructions;
363
- if (serverToolFollowup && instructions.length > 0) {
364
- instructions = instructions.filter((entry) => entry.type !== 'stopMessageSet' &&
365
- entry.type !== 'stopMessageMode' &&
366
- entry.type !== 'stopMessageClear' &&
367
- entry.type !== 'preCommandSet' &&
368
- entry.type !== 'preCommandClear');
369
- }
370
- if (stopMessageScope && parsedInstructions.length > 0) {
371
- const sessionState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
372
- const hasStaleStopMessageInstruction = !latestUserHasMarker &&
373
- parsedInstructions.some((entry) => entry.type === 'stopMessageSet' ||
374
- entry.type === 'stopMessageMode');
375
- if (hasStaleStopMessageInstruction) {
376
- const hasActiveStopState = typeof sessionState.stopMessageText === 'string' ||
377
- typeof sessionState.stopMessageMaxRepeats === 'number' ||
378
- typeof sessionState.stopMessageStageMode === 'string' ||
379
- typeof sessionState.stopMessageAiMode === 'string' ||
380
- typeof sessionState.stopMessageAiSeedPrompt === 'string' ||
381
- Array.isArray(sessionState.stopMessageAiHistory);
382
- const hasStopLifecycleStamp = (typeof sessionState.stopMessageUpdatedAt === 'number' && Number.isFinite(sessionState.stopMessageUpdatedAt)) ||
383
- (typeof sessionState.stopMessageLastUsedAt === 'number' && Number.isFinite(sessionState.stopMessageLastUsedAt));
384
- if (hasActiveStopState || hasStopLifecycleStamp) {
385
- instructions = instructions.filter((entry) => entry.type !== 'stopMessageSet' &&
386
- entry.type !== 'stopMessageMode');
387
- }
388
- }
389
- }
390
- // stopMessage/precommand require explicit client inject scope (tmux).
391
- // When scope is missing, drop only these instructions and keep request passthrough.
392
- if (instructions.length > 0 && !stopMessageScope) {
393
- const blockedInstructionTypes = instructions
394
- .filter((entry) => entry.type === 'stopMessageSet' ||
395
- entry.type === 'stopMessageMode' ||
396
- entry.type === 'stopMessageClear' ||
397
- entry.type === 'preCommandSet' ||
398
- entry.type === 'preCommandClear')
399
- .map((entry) => entry.type);
400
- if (blockedInstructionTypes.length > 0 && isStopScopeTraceEnabled()) {
401
- this.debug?.log?.(`[virtual-router][stop_scope] requestId=${metadata.requestId || 'n/a'} stage=drop reason=missing_tmux_scope instructions=${blockedInstructionTypes.join(',')}`);
402
- }
403
- instructions = instructions.filter((entry) => entry.type !== 'stopMessageSet' &&
404
- entry.type !== 'stopMessageMode' &&
405
- entry.type !== 'stopMessageClear' &&
406
- entry.type !== 'preCommandSet' &&
407
- entry.type !== 'preCommandClear');
408
- }
409
- if (hasRoutingInstructionMarker(request.messages)) {
410
- request.messages = cleanMessagesFromRoutingInstructions(request.messages);
411
- }
412
- let appliedRoutingState = routingState;
413
- if (instructions.length > 0) {
414
- appliedRoutingState = applyRoutingInstructions(instructions, routingState);
415
- const persistedBaseState = stopMessageScope
416
- ? stripClientInjectScopedFields(appliedRoutingState)
417
- : appliedRoutingState;
418
- routingState = persistedBaseState;
419
- this.routingInstructionState.set(stateKey, persistedBaseState);
420
- persistRoutingInstructionState(stateKey, persistedBaseState, this.routingStateStore);
421
- // Persist stopMessage under tmux/clockd scope so client injection and trigger matching
422
- // use the same scope and never fall back to generic session keys.
423
- if (stopMessageScope) {
424
- const hasStopMessageSet = instructions.some((entry) => entry.type === 'stopMessageSet');
425
- const hasStopMessageMode = instructions.some((entry) => entry.type === 'stopMessageMode');
426
- const hasGlobalClear = instructions.some((entry) => entry.type === 'clear');
427
- const hasStopMessageClear = hasGlobalClear || instructions.some((entry) => entry.type === 'stopMessageClear');
428
- if (hasStopMessageSet || hasStopMessageMode || hasStopMessageClear) {
429
- const activeInstructionTypes = instructions
430
- .filter((entry) => entry.type === 'stopMessageSet' ||
431
- entry.type === 'stopMessageMode' ||
432
- entry.type === 'stopMessageClear')
433
- .map((entry) => entry.type);
434
- if (isStopScopeTraceEnabled()) {
435
- this.debug?.log?.(`[virtual-router][stop_scope] requestId=${metadata.requestId || 'n/a'} stage=apply scope=${stopMessageScope} instructions=${activeInstructionTypes.join(',') || 'none'}`);
436
- }
437
- const sessionState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
438
- let nextSessionState = {
439
- ...sessionState
440
- };
441
- let shouldPersistSessionState = false;
442
- const hasStopMessageStateChanged = () => {
443
- return (nextSessionState.stopMessageText !== sessionState.stopMessageText ||
444
- nextSessionState.stopMessageMaxRepeats !== sessionState.stopMessageMaxRepeats ||
445
- nextSessionState.stopMessageUsed !== sessionState.stopMessageUsed ||
446
- nextSessionState.stopMessageUpdatedAt !== sessionState.stopMessageUpdatedAt ||
447
- nextSessionState.stopMessageLastUsedAt !== sessionState.stopMessageLastUsedAt ||
448
- nextSessionState.stopMessageSource !== sessionState.stopMessageSource ||
449
- nextSessionState.stopMessageStageMode !== sessionState.stopMessageStageMode ||
450
- nextSessionState.stopMessageAiMode !== sessionState.stopMessageAiMode ||
451
- nextSessionState.stopMessageAiSeedPrompt !== sessionState.stopMessageAiSeedPrompt ||
452
- JSON.stringify(nextSessionState.stopMessageAiHistory || []) !==
453
- JSON.stringify(sessionState.stopMessageAiHistory || []));
454
- };
455
- if (hasStopMessageClear) {
456
- if (hasGlobalClear) {
457
- // <**clear**> is a hard reset: clear all session-scoped routing state
458
- // and let persistRoutingInstructionState delete persistence markers.
459
- nextSessionState = {
460
- forcedTarget: undefined,
461
- stickyTarget: undefined,
462
- preferTarget: undefined,
463
- allowedProviders: new Set(),
464
- disabledProviders: new Set(),
465
- disabledKeys: new Map(),
466
- disabledModels: new Map(),
467
- stopMessageSource: undefined,
468
- stopMessageText: undefined,
469
- stopMessageMaxRepeats: undefined,
470
- stopMessageUsed: undefined,
471
- stopMessageUpdatedAt: undefined,
472
- stopMessageLastUsedAt: undefined,
473
- stopMessageStageMode: undefined,
474
- stopMessageAiMode: undefined,
475
- stopMessageAiSeedPrompt: undefined,
476
- stopMessageAiHistory: undefined,
477
- preCommandSource: undefined,
478
- preCommandScriptPath: undefined,
479
- preCommandUpdatedAt: undefined
480
- };
481
- shouldPersistSessionState = true;
482
- }
483
- else {
484
- nextSessionState.stopMessageText = undefined;
485
- nextSessionState.stopMessageMaxRepeats = undefined;
486
- nextSessionState.stopMessageUsed = undefined;
487
- nextSessionState.stopMessageUpdatedAt = undefined;
488
- nextSessionState.stopMessageLastUsedAt = undefined;
489
- nextSessionState.stopMessageSource = undefined;
490
- nextSessionState.stopMessageStageMode = undefined;
491
- nextSessionState.stopMessageAiMode = undefined;
492
- nextSessionState.stopMessageAiSeedPrompt = undefined;
493
- nextSessionState.stopMessageAiHistory = undefined;
494
- shouldPersistSessionState = true;
495
- }
496
- }
497
- else if (hasStopMessageSet || hasStopMessageMode) {
498
- nextSessionState.stopMessageText =
499
- typeof appliedRoutingState.stopMessageText === 'string' && appliedRoutingState.stopMessageText.trim()
500
- ? appliedRoutingState.stopMessageText
501
- : undefined;
502
- nextSessionState.stopMessageMaxRepeats =
503
- typeof appliedRoutingState.stopMessageMaxRepeats === 'number' && Number.isFinite(appliedRoutingState.stopMessageMaxRepeats)
504
- ? Math.floor(appliedRoutingState.stopMessageMaxRepeats)
505
- : undefined;
506
- nextSessionState.stopMessageUsed =
507
- typeof appliedRoutingState.stopMessageUsed === 'number' && Number.isFinite(appliedRoutingState.stopMessageUsed)
508
- ? Math.max(0, Math.floor(appliedRoutingState.stopMessageUsed))
509
- : undefined;
510
- nextSessionState.stopMessageUpdatedAt =
511
- typeof appliedRoutingState.stopMessageUpdatedAt === 'number' && Number.isFinite(appliedRoutingState.stopMessageUpdatedAt)
512
- ? appliedRoutingState.stopMessageUpdatedAt
513
- : undefined;
514
- nextSessionState.stopMessageLastUsedAt =
515
- typeof appliedRoutingState.stopMessageLastUsedAt === 'number' && Number.isFinite(appliedRoutingState.stopMessageLastUsedAt)
516
- ? appliedRoutingState.stopMessageLastUsedAt
517
- : undefined;
518
- nextSessionState.stopMessageSource =
519
- typeof appliedRoutingState.stopMessageSource === 'string' && appliedRoutingState.stopMessageSource.trim()
520
- ? appliedRoutingState.stopMessageSource.trim()
521
- : undefined;
522
- nextSessionState.stopMessageStageMode = normalizeStopMessageStageMode(appliedRoutingState.stopMessageStageMode);
523
- nextSessionState.stopMessageAiMode = normalizeStopMessageAiMode(appliedRoutingState.stopMessageAiMode);
524
- nextSessionState.stopMessageAiSeedPrompt =
525
- typeof appliedRoutingState.stopMessageAiSeedPrompt === 'string' &&
526
- appliedRoutingState.stopMessageAiSeedPrompt.trim()
527
- ? appliedRoutingState.stopMessageAiSeedPrompt.trim()
528
- : undefined;
529
- nextSessionState.stopMessageAiHistory = Array.isArray(appliedRoutingState.stopMessageAiHistory)
530
- ? appliedRoutingState.stopMessageAiHistory
531
- : undefined;
532
- shouldPersistSessionState = hasStopMessageStateChanged();
533
- }
534
- if (shouldPersistSessionState) {
535
- this.routingInstructionState.set(stopMessageScope, nextSessionState);
536
- persistRoutingInstructionState(stopMessageScope, nextSessionState, this.routingStateStore);
537
- }
538
- else {
539
- nextSessionState = sessionState;
540
- }
541
- // 日志展示使用 session scope 的 stopMessage 状态,避免每次解析重复刷新时间/次数。
542
- if (typeof nextSessionState.stopMessageText === 'string' ||
543
- typeof nextSessionState.stopMessageMaxRepeats === 'number' ||
544
- typeof nextSessionState.stopMessageStageMode === 'string' ||
545
- typeof nextSessionState.stopMessageAiMode === 'string' ||
546
- typeof nextSessionState.stopMessageAiSeedPrompt === 'string' ||
547
- Array.isArray(nextSessionState.stopMessageAiHistory)) {
548
- routingState.stopMessageText = nextSessionState.stopMessageText;
549
- routingState.stopMessageMaxRepeats = nextSessionState.stopMessageMaxRepeats;
550
- routingState.stopMessageUsed = nextSessionState.stopMessageUsed;
551
- routingState.stopMessageUpdatedAt = nextSessionState.stopMessageUpdatedAt;
552
- routingState.stopMessageLastUsedAt = nextSessionState.stopMessageLastUsedAt;
553
- routingState.stopMessageStageMode = nextSessionState.stopMessageStageMode;
554
- routingState.stopMessageAiMode = nextSessionState.stopMessageAiMode;
555
- routingState.stopMessageAiSeedPrompt = nextSessionState.stopMessageAiSeedPrompt;
556
- routingState.stopMessageAiHistory = nextSessionState.stopMessageAiHistory;
557
- }
558
- }
559
- }
560
- }
561
- if (instructions.length > 0 && stopMessageScope) {
562
- const hasPreCommandSet = instructions.some((entry) => entry.type === 'preCommandSet');
563
- const hasPreCommandClear = instructions.some((entry) => entry.type === 'preCommandClear');
564
- if (hasPreCommandSet || hasPreCommandClear) {
565
- const sessionState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
566
- const nextSessionState = {
567
- ...sessionState
568
- };
569
- let changed = false;
570
- if (hasPreCommandClear) {
571
- changed =
572
- typeof sessionState.preCommandScriptPath === 'string' ||
573
- typeof sessionState.preCommandSource === 'string' ||
574
- typeof sessionState.preCommandUpdatedAt === 'number';
575
- nextSessionState.preCommandScriptPath = undefined;
576
- nextSessionState.preCommandSource = undefined;
577
- nextSessionState.preCommandUpdatedAt = Date.now();
578
- }
579
- if (hasPreCommandSet) {
580
- const scriptPath = typeof appliedRoutingState.preCommandScriptPath === 'string' ? appliedRoutingState.preCommandScriptPath.trim() : '';
581
- if (scriptPath) {
582
- if (sessionState.preCommandScriptPath !== scriptPath) {
583
- changed = true;
584
- }
585
- nextSessionState.preCommandScriptPath = scriptPath;
586
- nextSessionState.preCommandSource = 'explicit';
587
- nextSessionState.preCommandUpdatedAt =
588
- typeof appliedRoutingState.preCommandUpdatedAt === 'number' && Number.isFinite(appliedRoutingState.preCommandUpdatedAt)
589
- ? appliedRoutingState.preCommandUpdatedAt
590
- : Date.now();
591
- }
592
- }
593
- if (changed) {
594
- this.routingInstructionState.set(stopMessageScope, nextSessionState);
595
- persistRoutingInstructionState(stopMessageScope, nextSessionState, this.routingStateStore);
596
- routingState.preCommandScriptPath = nextSessionState.preCommandScriptPath;
597
- routingState.preCommandSource = nextSessionState.preCommandSource;
598
- routingState.preCommandUpdatedAt = nextSessionState.preCommandUpdatedAt;
599
- }
600
- }
601
- }
602
- if (instructions.length === 0 && stopMessageScope) {
603
- const sessionState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
604
- if (typeof sessionState.stopMessageText === 'string' ||
605
- typeof sessionState.stopMessageMaxRepeats === 'number' ||
606
- typeof sessionState.stopMessageStageMode === 'string' ||
607
- typeof sessionState.stopMessageAiMode === 'string' ||
608
- typeof sessionState.stopMessageAiSeedPrompt === 'string' ||
609
- Array.isArray(sessionState.stopMessageAiHistory)) {
610
- routingState.stopMessageText = sessionState.stopMessageText;
611
- routingState.stopMessageMaxRepeats = sessionState.stopMessageMaxRepeats;
612
- routingState.stopMessageUsed = sessionState.stopMessageUsed;
613
- routingState.stopMessageUpdatedAt = sessionState.stopMessageUpdatedAt;
614
- routingState.stopMessageLastUsedAt = sessionState.stopMessageLastUsedAt;
615
- routingState.stopMessageStageMode = sessionState.stopMessageStageMode;
616
- routingState.stopMessageAiMode = sessionState.stopMessageAiMode;
617
- routingState.stopMessageAiSeedPrompt = sessionState.stopMessageAiSeedPrompt;
618
- routingState.stopMessageAiHistory = sessionState.stopMessageAiHistory;
619
- }
620
- if (typeof sessionState.preCommandScriptPath === 'string' && sessionState.preCommandScriptPath.trim()) {
621
- routingState.preCommandScriptPath = sessionState.preCommandScriptPath;
622
- routingState.preCommandSource = sessionState.preCommandSource;
623
- routingState.preCommandUpdatedAt = sessionState.preCommandUpdatedAt;
624
- }
625
- }
626
- // Guardrail: if a session is restricted to providers that do not exist in any routing pools,
627
- // we must not hard-fail the request loop. Auto-clear the allowlist and fall back to normal routing.
628
- if (routingState.allowedProviders.size > 0) {
629
- const providersInRouting = new Set();
630
- for (const pools of Object.values(this.routing)) {
631
- if (!Array.isArray(pools))
632
- continue;
633
- for (const pool of pools) {
634
- if (!pool || !Array.isArray(pool.targets))
635
- continue;
636
- for (const key of pool.targets) {
637
- if (typeof key !== 'string' || !key)
638
- continue;
639
- const providerId = extractProviderId(key);
640
- if (providerId) {
641
- providersInRouting.add(providerId);
642
- }
643
- }
644
- }
645
- }
646
- const allowed = Array.from(routingState.allowedProviders).filter((provider) => typeof provider === 'string');
647
- const hasIntersection = allowed.some((provider) => providersInRouting.has(provider));
648
- if (!hasIntersection) {
649
- routingState = {
650
- ...routingState,
651
- allowedProviders: new Set()
652
- };
653
- this.routingInstructionState.set(stateKey, routingState);
654
- persistRoutingInstructionState(stateKey, routingState, this.routingStateStore);
655
- }
656
- }
657
- const features = buildRoutingFeatures(request, metadata);
658
- const directProviderModel = this.parseDirectProviderModel(request?.model);
659
- let classification;
660
- let requestedRoute;
661
- let selection;
662
- const selectionDeps = {
663
- routing: this.routing,
664
- providerRegistry: this.providerRegistry,
665
- healthManager: this.healthManager,
666
- contextAdvisor: this.contextAdvisor,
667
- loadBalancer: this.loadBalancer,
668
- isProviderCoolingDown: (key) => this.isProviderCoolingDown(key),
669
- resolveStickyKey: (m) => this.resolveStickyKey(m),
670
- quotaView: this.quotaView
671
- };
672
- if (directProviderModel) {
673
- const forceMediaFallback = this.shouldFallbackDirectModelForMedia(directProviderModel, features);
674
- const providerKeys = this.providerRegistry.listProviderKeys(directProviderModel.providerId);
675
- let hasModel = false;
676
- for (const key of providerKeys) {
677
- try {
678
- const profile = this.providerRegistry.get(key);
679
- if (profile?.modelId === directProviderModel.modelId) {
680
- hasModel = true;
681
- break;
682
- }
683
- }
684
- catch {
685
- continue;
686
- }
687
- }
688
- if (!hasModel) {
689
- throw new VirtualRouterError(`Unknown model ${directProviderModel.modelId} for provider ${directProviderModel.providerId}`, VirtualRouterErrorCode.CONFIG_ERROR, { providerId: directProviderModel.providerId, modelId: directProviderModel.modelId });
690
- }
691
- if (!forceMediaFallback) {
692
- const directSelection = selectDirectProviderModel(directProviderModel.providerId, directProviderModel.modelId, metadata, features, routingState, selectionDeps);
693
- if (!directSelection) {
694
- throw new VirtualRouterError(`All providers unavailable for model ${directProviderModel.providerId}.${directProviderModel.modelId}`, VirtualRouterErrorCode.PROVIDER_NOT_AVAILABLE, { providerId: directProviderModel.providerId, modelId: directProviderModel.modelId });
695
- }
696
- classification = {
697
- routeName: 'direct',
698
- confidence: 1,
699
- reasoning: `direct_model:${directProviderModel.providerId}.${directProviderModel.modelId}`,
700
- fallback: false,
701
- candidates: ['direct']
702
- };
703
- requestedRoute = 'direct';
704
- selection = directSelection;
705
- }
706
- else {
707
- classification = this.classifier.classify(features);
708
- requestedRoute = this.normalizeRouteAlias(classification.routeName || DEFAULT_ROUTE);
709
- selection = this.selectProvider(requestedRoute, metadata, classification, features, routingState);
710
- }
711
- }
712
- else {
713
- // Prefer target (from "<**!provider.model**>") is evaluated before routing classification.
714
- const preferTarget = routingState.preferTarget;
715
- if (preferTarget && typeof preferTarget.provider === 'string' && preferTarget.provider.trim()) {
716
- const providerId = preferTarget.provider.trim();
717
- const keyAlias = typeof preferTarget.keyAlias === 'string' ? preferTarget.keyAlias.trim() : '';
718
- const modelId = typeof preferTarget.model === 'string' ? preferTarget.model.trim() : '';
719
- const keyIndex = typeof preferTarget.keyIndex === 'number' && Number.isFinite(preferTarget.keyIndex)
720
- ? Math.floor(preferTarget.keyIndex)
721
- : undefined;
722
- const candidateKeys = [];
723
- if (keyIndex !== undefined && keyIndex > 0) {
724
- const runtimeKey = this.providerRegistry.resolveRuntimeKeyByIndex(providerId, keyIndex);
725
- if (runtimeKey) {
726
- candidateKeys.push(runtimeKey);
727
- }
728
- }
729
- else if (modelId) {
730
- const allKeys = this.providerRegistry.listProviderKeys(providerId);
731
- for (const key of allKeys) {
732
- if (keyAlias) {
733
- const prefix = `${providerId}.${keyAlias}.`;
734
- if (!key.startsWith(prefix)) {
735
- continue;
736
- }
737
- }
738
- try {
739
- const profile = this.providerRegistry.get(key);
740
- if (profile?.modelId === modelId) {
741
- candidateKeys.push(key);
742
- }
743
- }
744
- catch {
745
- continue;
746
- }
747
- }
748
- }
749
- const allowAliasRotation = !keyAlias && keyIndex === undefined;
750
- const eligibleKeys = (() => {
751
- if (candidateKeys.length === 0) {
752
- return [];
753
- }
754
- const quotaView = selectionDeps.quotaView;
755
- const now = quotaView ? Date.now() : 0;
756
- return candidateKeys.filter((key) => {
757
- if (!quotaView) {
758
- if (this.isProviderCoolingDown(key)) {
759
- return false;
760
- }
761
- if (!this.healthManager.isAvailable(key)) {
762
- return false;
763
- }
764
- return true;
765
- }
766
- const entry = quotaView(key);
767
- if (!entry) {
768
- return true;
769
- }
770
- if (!entry.inPool) {
771
- return false;
772
- }
773
- if (entry.cooldownUntil && entry.cooldownUntil > now) {
774
- return false;
775
- }
776
- if (entry.blacklistUntil && entry.blacklistUntil > now) {
777
- return false;
778
- }
779
- return true;
780
- });
781
- })();
782
- const preferSelection = eligibleKeys.length > 0
783
- ? selectFromStickyPool(new Set(eligibleKeys), metadata, features, routingState, selectionDeps, {
784
- allowAliasRotation
785
- })
786
- : null;
787
- if (preferSelection) {
788
- classification = {
789
- routeName: 'prefer',
790
- confidence: 1,
791
- reasoning: keyIndex !== undefined ? `prefer_key:${providerId}.${keyIndex}` : `prefer_model:${providerId}.${modelId}`,
792
- fallback: false,
793
- candidates: ['prefer']
794
- };
795
- requestedRoute = 'prefer';
796
- selection = {
797
- ...preferSelection,
798
- routeUsed: 'prefer',
799
- poolId: 'prefer-primary'
800
- };
801
- }
802
- else if (routingState.preferTarget) {
803
- // Auto-clear only when the target becomes invalid or blocked by explicit routing instructions.
804
- // Do NOT clear for temporary unavailability (e.g. 429 cooldown, quota cooldown, transient health).
805
- const shouldAutoClear = (() => {
806
- if (candidateKeys.length === 0) {
807
- return true;
808
- }
809
- // Prefer selection failed despite eligible keys existing: treat as a hard block (e.g. routing rules).
810
- if (eligibleKeys.length > 0) {
811
- return true;
812
- }
813
- // If quota explicitly marks the preferred target as out-of-pool, clear the prefer instruction so
814
- // the router can fall back to other targets without repeatedly retrying an impossible preference.
815
- if (selectionDeps.quotaView) {
816
- for (const key of candidateKeys) {
817
- const entry = selectionDeps.quotaView(key);
818
- if (entry && entry.inPool === false) {
819
- return true;
820
- }
821
- }
822
- }
823
- return false;
824
- })();
825
- if (shouldAutoClear) {
826
- routingState = {
827
- ...routingState,
828
- preferTarget: undefined
829
- };
830
- this.routingInstructionState.set(stateKey, routingState);
831
- persistRoutingInstructionState(stateKey, routingState, this.routingStateStore);
832
- }
833
- }
834
- }
835
- if (!selection) {
836
- classification = metadata.routeHint && metadata.routeHint.trim()
837
- ? {
838
- routeName: metadata.routeHint.trim(),
839
- confidence: 1,
840
- reasoning: `route_hint:${metadata.routeHint.trim()}`,
841
- fallback: false,
842
- candidates: [metadata.routeHint.trim()]
843
- }
844
- : this.classifier.classify(features);
845
- requestedRoute = this.normalizeRouteAlias(classification.routeName || DEFAULT_ROUTE);
846
- selection = this.selectProvider(requestedRoute, metadata, classification, features, routingState);
847
- }
848
- }
849
- const baseTarget = this.providerRegistry.buildTarget(selection.providerKey);
850
- const forceVision = selection.routeUsed === 'vision' && this.routeHasForceFlag('vision');
851
- const target = {
852
- ...baseTarget,
853
- ...(this.webSearchForce ? { forceWebSearch: true } : {}),
854
- ...(forceVision ? { forceVision: true } : {})
855
- };
856
- const instructionProcessMode = this.resolveInstructionProcessModeForSelection(selection.providerKey, routingState);
857
- if (instructionProcessMode) {
858
- target.processMode = instructionProcessMode;
859
- }
860
- recordAntigravitySessionLease({
861
- metadata: features.metadata,
862
- providerKey: selection.providerKey,
863
- sessionKey: this.resolveSessionScope(features.metadata),
864
- providerRegistry: this.providerRegistry,
865
- leaseStore: this.stickySessionManager.getAllStores().aliasLeaseStore,
866
- sessionAliasStore: this.stickySessionManager.getAllStores().sessionAliasStore,
867
- persistence: this.antigravityLeasePersistence,
868
- aliasReuseCooldownMs: this.stickySessionManager.getAliasReuseCooldownMs(),
869
- commitSessionBinding: false,
870
- debug: this.debug
871
- });
872
- const routingMode = resolveRoutingMode([...metadataInstructions, ...instructions], routingState);
873
- const hitReason = buildHitReason(selection.routeUsed, selection.providerKey, classification, features, routingMode, { providerRegistry: this.providerRegistry, contextRouting: this.contextRouting });
874
- const stickyScope = routingMode !== 'none' ? this.resolveSessionScope(metadata) : undefined;
875
- const routeForLog = routingMode === 'sticky' ? 'sticky' : selection.routeUsed;
876
- const hitRecord = createVirtualRouterHitRecord({
877
- routeName: routeForLog,
878
- poolId: selection.poolId,
879
- providerKey: selection.providerKey,
880
- modelId: target.modelId || undefined,
881
- hitReason,
882
- stickyScope,
883
- routingState,
884
- requestTokens: features.estimatedTokens,
885
- selectionPenalty: this.resolveSelectionPenalty(selection.providerKey)
886
- });
887
- this.routeAnalytics.incrementRouteStat(selection.routeUsed, selection.providerKey, hitRecord);
49
+ const parseLog = buildRoutingInstructionParseLog(request, metadata);
50
+ const nativeMetadata = injectRuntimeNowMs(metadata);
51
+ let raw;
888
52
  try {
889
- this.statsCenter.recordVirtualRouterHit(toVirtualRouterHitEvent(hitRecord, {
890
- requestId: metadata.requestId,
891
- entryEndpoint: metadata.entryEndpoint || '/v1/chat/completions'
892
- }));
893
- }
894
- catch {
895
- // stats must never break routing
896
- }
897
- const formatted = formatVirtualRouterHit(hitRecord);
898
- if (formatted) {
899
- this.debug?.log?.(formatted);
900
- }
901
- else {
902
- this.debug?.log?.(formatVirtualRouterHit(hitRecord));
903
- }
904
- const didFallback = selection.routeUsed !== requestedRoute;
905
- return {
906
- target,
907
- decision: {
908
- routeName: selection.routeUsed,
909
- providerKey: selection.providerKey,
910
- pool: selection.pool,
911
- poolId: selection.poolId,
912
- confidence: classification.confidence,
913
- reasoning: classification.reasoning,
914
- fallback: didFallback
915
- },
916
- diagnostics: {
917
- routeName: selection.routeUsed,
918
- providerKey: selection.providerKey,
919
- pool: selection.pool,
920
- poolId: selection.poolId,
921
- reasoning: classification.reasoning,
922
- fallback: didFallback,
923
- confidence: classification.confidence
924
- }
925
- };
53
+ raw = this.nativeProxy.route(JSON.stringify(request), JSON.stringify(nativeMetadata));
54
+ }
55
+ catch (error) {
56
+ throw normalizeNativeVirtualRouterError(error);
57
+ }
58
+ if (typeof raw !== 'string') {
59
+ throw normalizeNativeVirtualRouterError(raw);
60
+ }
61
+ if (raw.startsWith('Error:') || raw.startsWith(VIRTUAL_ROUTER_ERROR_PREFIX)) {
62
+ throw normalizeNativeVirtualRouterError(raw);
63
+ }
64
+ const parsed = JSON.parse(raw);
65
+ emitRoutingInstructionParseLog(parseLog);
66
+ // Keep legacy observable behavior for callers/tests that inspect the request object
67
+ // after route(): instruction markers are stripped from forwarded payload structures.
68
+ cleanRoutingInstructionMarkersInPlace(request);
69
+ const stopScope = parseLog?.stopScope || resolveStopMessageScope(metadata);
70
+ const stopState = stopScope ? this.getStopMessageState(metadata) : null;
71
+ const forceStopStatusLabel = Boolean(parseLog?.stopMessageTypes.length ||
72
+ parseLog?.scopedTypes.some((type) => type === 'stopMessageSet' || type === 'stopMessageMode' || type === 'stopMessageClear'));
73
+ emitVirtualRouterHitLog(parsed, {
74
+ requestId: metadata.requestId,
75
+ stopScope,
76
+ stopState,
77
+ forceStopStatusLabel
78
+ });
79
+ return parsed;
926
80
  }
927
81
  getStopMessageState(metadata) {
928
- const hasArmedStopState = (candidate) => {
929
- if (!candidate) {
930
- return false;
931
- }
932
- const text = typeof candidate.stopMessageText === 'string' ? candidate.stopMessageText.trim() : '';
933
- const maxRepeats = typeof candidate.stopMessageMaxRepeats === 'number' && Number.isFinite(candidate.stopMessageMaxRepeats)
934
- ? Math.max(1, Math.floor(candidate.stopMessageMaxRepeats))
935
- : 0;
936
- const mode = typeof candidate.stopMessageStageMode === 'string'
937
- ? candidate.stopMessageStageMode.trim().toLowerCase()
938
- : '';
939
- if (mode === 'off') {
940
- return false;
941
- }
942
- return Boolean(text) && maxRepeats > 0;
943
- };
944
- const stopMessageScope = resolveStopMessageScope(metadata);
945
- if (!stopMessageScope) {
946
- const sessionScope = this.resolveSessionScope(metadata);
947
- if (sessionScope) {
948
- const legacyState = getRoutingInstructionState(sessionScope, this.routingInstructionState, this.routingStateStore);
949
- if (hasClientInjectScopedFields(legacyState)) {
950
- const cleared = stripClientInjectScopedFields(legacyState);
951
- this.routingInstructionState.set(sessionScope, cleared);
952
- persistRoutingInstructionState(sessionScope, cleared, this.routingStateStore);
953
- }
954
- }
955
- return null;
956
- }
957
- const effectiveState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
958
- if (!hasArmedStopState(effectiveState)) {
959
- return null;
960
- }
961
- const text = typeof effectiveState.stopMessageText === 'string' ? effectiveState.stopMessageText.trim() : '';
962
- const maxRepeats = typeof effectiveState.stopMessageMaxRepeats === 'number' &&
963
- Number.isFinite(effectiveState.stopMessageMaxRepeats)
964
- ? Math.max(1, Math.floor(effectiveState.stopMessageMaxRepeats))
965
- : 0;
966
- const stageModeRaw = typeof effectiveState.stopMessageStageMode === 'string'
967
- ? effectiveState.stopMessageStageMode.trim().toLowerCase()
968
- : '';
969
- const stageModeNormalized = stageModeRaw === 'on' || stageModeRaw === 'off' || stageModeRaw === 'auto'
970
- ? stageModeRaw
971
- : undefined;
972
- const aiModeRaw = typeof effectiveState.stopMessageAiMode === 'string'
973
- ? effectiveState.stopMessageAiMode.trim().toLowerCase()
974
- : '';
975
- const aiModeNormalized = aiModeRaw === 'on' || aiModeRaw === 'off' ? aiModeRaw : undefined;
976
- if (stageModeNormalized === 'off' || !text || maxRepeats <= 0) {
977
- return null;
978
- }
979
- return {
980
- ...(text ? { stopMessageText: text } : {}),
981
- stopMessageMaxRepeats: maxRepeats,
982
- ...(typeof effectiveState.stopMessageSource === 'string' && effectiveState.stopMessageSource.trim()
983
- ? { stopMessageSource: effectiveState.stopMessageSource.trim() }
984
- : {}),
985
- ...(typeof effectiveState.stopMessageUsed === 'number' && Number.isFinite(effectiveState.stopMessageUsed)
986
- ? { stopMessageUsed: Math.max(0, Math.floor(effectiveState.stopMessageUsed)) }
987
- : {}),
988
- ...(typeof effectiveState.stopMessageUpdatedAt === 'number' &&
989
- Number.isFinite(effectiveState.stopMessageUpdatedAt)
990
- ? { stopMessageUpdatedAt: effectiveState.stopMessageUpdatedAt }
991
- : {}),
992
- ...(typeof effectiveState.stopMessageLastUsedAt === 'number' &&
993
- Number.isFinite(effectiveState.stopMessageLastUsedAt)
994
- ? { stopMessageLastUsedAt: effectiveState.stopMessageLastUsedAt }
995
- : {}),
996
- ...(stageModeNormalized ? { stopMessageStageMode: stageModeNormalized } : {}),
997
- ...(aiModeNormalized ? { stopMessageAiMode: aiModeNormalized } : {}),
998
- ...(typeof effectiveState.stopMessageAiSeedPrompt === 'string' &&
999
- effectiveState.stopMessageAiSeedPrompt.trim()
1000
- ? { stopMessageAiSeedPrompt: effectiveState.stopMessageAiSeedPrompt.trim() }
1001
- : {}),
1002
- ...(Array.isArray(effectiveState.stopMessageAiHistory)
1003
- ? { stopMessageAiHistory: effectiveState.stopMessageAiHistory }
1004
- : {}),
1005
- };
82
+ const raw = this.nativeProxy.getStopMessageState(JSON.stringify(metadata));
83
+ return JSON.parse(raw);
1006
84
  }
1007
85
  getPreCommandState(metadata) {
1008
- const stopMessageScope = resolveStopMessageScope(metadata);
1009
- if (!stopMessageScope) {
1010
- return null;
1011
- }
1012
- const effectiveState = getRoutingInstructionState(stopMessageScope, this.routingInstructionState, this.routingStateStore);
1013
- if (!effectiveState) {
1014
- return null;
1015
- }
1016
- const scriptPath = typeof effectiveState.preCommandScriptPath === 'string' ? effectiveState.preCommandScriptPath.trim() : '';
1017
- if (!scriptPath) {
1018
- return null;
1019
- }
1020
- return {
1021
- preCommandScriptPath: scriptPath,
1022
- ...(typeof effectiveState.preCommandSource === 'string' && effectiveState.preCommandSource.trim()
1023
- ? { preCommandSource: effectiveState.preCommandSource.trim() }
1024
- : {}),
1025
- ...(typeof effectiveState.preCommandUpdatedAt === 'number' && Number.isFinite(effectiveState.preCommandUpdatedAt)
1026
- ? { preCommandUpdatedAt: effectiveState.preCommandUpdatedAt }
1027
- : {})
1028
- };
86
+ const raw = this.nativeProxy.getPreCommandState(JSON.stringify(metadata));
87
+ return JSON.parse(raw);
1029
88
  }
1030
89
  handleProviderFailure(event) {
1031
- handleProviderFailureImpl(event, this.healthManager, this.providerHealthConfig(), (key, ttl) => this.markProviderCooldown(key, ttl));
90
+ this.nativeProxy.handleProviderFailure(JSON.stringify(event));
1032
91
  }
1033
92
  handleProviderError(event) {
1034
- if (this.healthStore && typeof this.healthStore.recordProviderError === 'function') {
1035
- try {
1036
- this.healthStore.recordProviderError(event);
1037
- }
1038
- catch {
1039
- // ignore persistence errors
1040
- }
1041
- }
1042
- // Quota routing mode: health/cooldown must be driven by quotaView only (host/core quota center).
1043
- // VirtualRouter must not produce or persist its own cooldown state in this mode.
1044
- // 当 Host 注入 quotaView 时,VirtualRouter 的入池/优先级决策应以 quota 为准;
1045
- // 此时不再在 engine-health 内部进行 429/backoff/series cooldown 等健康决策,
1046
- // 以避免与 daemon/quota-center 的长期熔断策略重复维护并导致日志噪声。
1047
- if (this.quotaView) {
1048
- return;
1049
- }
1050
- // Antigravity account safety policy uses router-local cooldown TTLs; only applies when quota routing is disabled.
1051
- applyAntigravityRiskPolicyImpl(event, this.providerRegistry, this.healthManager, (key, ttl) => this.markProviderCooldown(key, ttl), this.debug);
1052
- // 配额恢复事件优先处理:一旦识别到 virtualRouterQuotaRecovery,
1053
- // 直接清理健康状态/冷却 TTL,避免继续走常规错误映射逻辑。
1054
- const handledByQuota = applyQuotaRecoveryImpl(event, this.healthManager, (key) => this.clearProviderCooldown(key), this.debug);
1055
- if (handledByQuota) {
1056
- return;
1057
- }
1058
- const handledByQuotaDepleted = applyQuotaDepletedImpl(event, this.healthManager, (key, ttl) => this.markProviderCooldown(key, ttl), this.debug);
1059
- if (handledByQuotaDepleted) {
1060
- return;
1061
- }
1062
- applySeriesCooldownImpl(event, this.providerRegistry, this.healthManager, (key, ttl) => this.markProviderCooldown(key, ttl), this.debug);
1063
- const derived = mapProviderErrorImpl(event, this.providerHealthConfig());
1064
- if (!derived) {
1065
- return;
1066
- }
1067
- this.handleProviderFailure(derived);
93
+ this.nativeProxy.handleProviderError(JSON.stringify(event));
1068
94
  }
1069
95
  handleProviderSuccess(event) {
1070
- if (!event || typeof event !== 'object') {
1071
- return;
1072
- }
1073
- const providerKey = event.runtime && typeof event.runtime.providerKey === 'string' ? event.runtime.providerKey.trim() : '';
1074
- if (!providerKey) {
1075
- return;
1076
- }
1077
- const metadata = event.metadata && typeof event.metadata === 'object' && !Array.isArray(event.metadata)
1078
- ? event.metadata
1079
- : null;
1080
- if (!metadata) {
1081
- return;
1082
- }
1083
- if (providerKey.toLowerCase().startsWith('antigravity.')) {
1084
- recordAntigravitySessionLease({
1085
- metadata: metadata,
1086
- providerKey,
1087
- sessionKey: this.resolveSessionScope(metadata),
1088
- providerRegistry: this.providerRegistry,
1089
- leaseStore: this.stickySessionManager.getAllStores().aliasLeaseStore,
1090
- sessionAliasStore: this.stickySessionManager.getAllStores().sessionAliasStore,
1091
- persistence: this.antigravityLeasePersistence,
1092
- aliasReuseCooldownMs: this.stickySessionManager.getAliasReuseCooldownMs(),
1093
- commitSessionBinding: true,
1094
- debug: this.debug
1095
- });
1096
- }
96
+ this.nativeProxy.handleProviderSuccess(JSON.stringify(event));
1097
97
  }
1098
98
  getStatus() {
1099
- const routes = {};
1100
- for (const [route, pools] of Object.entries(this.routing)) {
1101
- const stats = this.routeAnalytics.getRouteStats(route);
1102
- routes[route] = {
1103
- providers: this.flattenPoolTargets(pools),
1104
- hits: stats?.hits ?? 0,
1105
- lastUsedProvider: stats?.lastProvider,
1106
- ...(stats?.lastHit ? { lastHit: { ...stats.lastHit } } : {})
1107
- };
1108
- }
1109
- return {
1110
- routes,
1111
- health: this.healthManager.getSnapshot()
1112
- };
99
+ return JSON.parse(this.nativeProxy.getStatus());
1113
100
  }
1114
- /**
1115
- * 将分类器产生的逻辑路由名直接归一化为配置中的路由键。
1116
- * 不再维护 "websearch" 之类的别名,调用方应显式使用 "web_search" 或 "search" 等实际路由名。
1117
- */
1118
- normalizeRouteAlias(routeName) {
1119
- const base = routeName && routeName.trim() ? routeName.trim() : DEFAULT_ROUTE;
1120
- return base;
1121
- }
1122
- validateConfig(config) {
1123
- if (!config.routing || typeof config.routing !== 'object') {
1124
- throw new VirtualRouterError('routing configuration is required', VirtualRouterErrorCode.CONFIG_ERROR);
1125
- }
1126
- if (!config.providers || Object.keys(config.providers).length === 0) {
1127
- throw new VirtualRouterError('providers configuration is required', VirtualRouterErrorCode.CONFIG_ERROR);
1128
- }
1129
- const defaultPools = config.routing[DEFAULT_ROUTE];
1130
- if (!this.routeHasTargets(defaultPools)) {
1131
- throw new VirtualRouterError('default route must be configured with at least one provider', VirtualRouterErrorCode.CONFIG_ERROR);
1132
- }
1133
- if (!this.hasPrimaryPool(defaultPools)) {
1134
- throw new VirtualRouterError('default route must define at least one non-backup pool', VirtualRouterErrorCode.CONFIG_ERROR);
1135
- }
1136
- const providerKeys = new Set(Object.keys(config.providers));
1137
- for (const [routeName, pools] of Object.entries(config.routing)) {
1138
- if (!this.routeHasTargets(pools)) {
1139
- if (routeName === DEFAULT_ROUTE) {
1140
- throw new VirtualRouterError('default route cannot be empty', VirtualRouterErrorCode.CONFIG_ERROR);
1141
- }
1142
- continue;
1143
- }
1144
- for (const pool of pools) {
1145
- if (!Array.isArray(pool.targets) || !pool.targets.length) {
1146
- continue;
1147
- }
1148
- for (const providerKey of pool.targets) {
1149
- if (!providerKeys.has(providerKey)) {
1150
- throw new VirtualRouterError(`Route ${routeName} references unknown provider ${providerKey}`, VirtualRouterErrorCode.CONFIG_ERROR);
1151
- }
1152
- }
1153
- }
1154
- }
1155
- }
1156
- selectProvider(requestedRoute, metadata, classification, features, routingState) {
1157
- const activeState = routingState ||
1158
- getRoutingInstructionState(this.resolveStickyKey(metadata), this.routingInstructionState, this.routingStateStore);
1159
- return selectProviderImpl(requestedRoute, metadata, classification, features, activeState, {
1160
- routing: this.routing,
1161
- providerRegistry: this.providerRegistry,
1162
- healthManager: this.healthManager,
1163
- contextAdvisor: this.contextAdvisor,
1164
- loadBalancer: this.loadBalancer,
1165
- isProviderCoolingDown: (key) => this.isProviderCoolingDown(key),
1166
- getProviderCooldownRemainingMs: (key) => this.getProviderCooldownRemainingMs(key),
1167
- resolveStickyKey: (m) => this.resolveStickyKey(m),
1168
- quotaView: this.quotaView,
1169
- aliasQueueStore: this.stickySessionManager.getAllStores().aliasQueueStore,
1170
- antigravityAliasLeaseStore: this.stickySessionManager.getAllStores().aliasLeaseStore,
1171
- antigravitySessionAliasStore: this.stickySessionManager.getAllStores().sessionAliasStore,
1172
- antigravityAliasReuseCooldownMs: this.stickySessionManager.getAliasReuseCooldownMs()
1173
- }, { routingState });
1174
- }
1175
- resolveSelectionPenalty(providerKey) {
1176
- if (!this.quotaView) {
1177
- return undefined;
1178
- }
1179
- try {
1180
- const entry = this.quotaView(providerKey);
1181
- const raw = entry?.selectionPenalty;
1182
- if (typeof raw !== 'number' || !Number.isFinite(raw) || raw <= 0) {
1183
- return undefined;
1184
- }
1185
- return Math.floor(raw);
1186
- }
1187
- catch {
1188
- return undefined;
1189
- }
1190
- }
1191
- providerHealthConfig() {
1192
- return this.healthManager.getConfig();
1193
- }
1194
- resolveStickyKey(metadata) {
1195
- return resolveStickyKeyImpl(metadata);
1196
- }
1197
- resolveSessionScope(metadata) {
1198
- return resolveSessionScopeImpl(metadata);
101
+ }
102
+ const VIRTUAL_ROUTER_ERROR_PREFIX = 'VIRTUAL_ROUTER_ERROR:';
103
+ function normalizeNativeVirtualRouterError(error) {
104
+ if (error instanceof VirtualRouterError) {
105
+ return error;
1199
106
  }
1200
- resolveInstructionProcessModeForSelection(providerKey, routingState) {
1201
- const candidates = [
1202
- routingState.forcedTarget,
1203
- routingState.stickyTarget,
1204
- routingState.preferTarget
1205
- ];
1206
- for (const candidate of candidates) {
1207
- const processMode = candidate?.processMode;
1208
- if (!processMode) {
1209
- continue;
1210
- }
1211
- const resolved = this.resolveInstructionTarget(candidate);
1212
- if (!resolved) {
1213
- continue;
1214
- }
1215
- if (resolved.keys.includes(providerKey)) {
1216
- return processMode;
1217
- }
1218
- }
1219
- return undefined;
107
+ const message = extractNativeErrorMessage(error);
108
+ const parsed = parseVirtualRouterErrorMessage(message);
109
+ if (parsed) {
110
+ return new VirtualRouterError(parsed.message, parsed.code);
1220
111
  }
1221
- resolveInstructionTarget(target) {
1222
- if (!target || !target.provider) {
1223
- return null;
1224
- }
1225
- const providerId = target.provider;
1226
- const providerKeys = this.providerRegistry.listProviderKeys(providerId);
1227
- if (providerKeys.length === 0) {
1228
- return null;
1229
- }
1230
- const alias = typeof target.keyAlias === 'string' ? target.keyAlias.trim() : '';
1231
- const aliasExplicit = alias.length > 0 && target.pathLength === 3;
1232
- if (aliasExplicit) {
1233
- const runtimeKey = this.providerRegistry.resolveRuntimeKeyByAlias(providerId, alias);
1234
- if (runtimeKey) {
1235
- return { mode: 'exact', keys: [runtimeKey] };
1236
- }
1237
- }
1238
- if (typeof target.keyIndex === 'number' && target.keyIndex > 0) {
1239
- const runtimeKey = this.providerRegistry.resolveRuntimeKeyByIndex(providerId, target.keyIndex);
1240
- if (runtimeKey) {
1241
- return { mode: 'exact', keys: [runtimeKey] };
1242
- }
1243
- }
1244
- if (target.model && target.model.trim()) {
1245
- const normalizedModel = target.model.trim();
1246
- const matchingKeys = providerKeys.filter((key) => {
1247
- const modelId = getProviderModelId(key, this.providerRegistry);
1248
- return modelId === normalizedModel;
1249
- });
1250
- if (matchingKeys.length > 0) {
1251
- return { mode: 'filter', keys: matchingKeys };
1252
- }
1253
- }
1254
- if (alias && !aliasExplicit) {
1255
- const legacyKey = this.providerRegistry.resolveRuntimeKeyByAlias(providerId, alias);
1256
- if (legacyKey) {
1257
- return { mode: 'exact', keys: [legacyKey] };
1258
- }
1259
- }
1260
- return { mode: 'filter', keys: providerKeys };
112
+ if (isVirtualRouterErrorLike(error)) {
113
+ return new VirtualRouterError(typeof error.message === 'string' && error.message.trim() ? error.message : 'Virtual router error', error.code);
1261
114
  }
1262
- filterCandidatesByRoutingState(routes, state) {
1263
- // console.log('[filter] routes:', routes, 'state:', {
1264
- // allowed: Array.from(state.allowedProviders),
1265
- // disabled: Array.from(state.disabledProviders)
1266
- // });
1267
- if (state.allowedProviders.size === 0 &&
1268
- state.disabledProviders.size === 0 &&
1269
- state.disabledKeys.size === 0 &&
1270
- state.disabledModels.size === 0) {
1271
- return routes;
1272
- }
1273
- return routes.filter(routeName => {
1274
- const pools = this.routing[routeName];
1275
- if (!pools)
1276
- return false;
1277
- for (const pool of pools) {
1278
- if (!Array.isArray(pool.targets) || pool.targets.length === 0) {
1279
- continue;
1280
- }
1281
- for (const providerKey of pool.targets) {
1282
- const providerId = extractProviderId(providerKey);
1283
- // console.log('[filter] checking', providerKey, 'id=', providerId);
1284
- if (!providerId)
1285
- continue;
1286
- if (state.allowedProviders.size > 0 && !state.allowedProviders.has(providerId)) {
1287
- // console.log('[filter] dropped by allowed list');
1288
- continue;
1289
- }
1290
- if (state.disabledProviders.has(providerId)) {
1291
- continue;
1292
- }
1293
- const disabledKeys = state.disabledKeys.get(providerId);
1294
- if (disabledKeys && disabledKeys.size > 0) {
1295
- const keyAlias = extractKeyAlias(providerKey);
1296
- const keyIndex = extractKeyIndex(providerKey);
1297
- if (keyAlias && disabledKeys.has(keyAlias)) {
1298
- continue;
1299
- }
1300
- if (keyIndex !== undefined && disabledKeys.has(keyIndex + 1)) {
1301
- continue;
1302
- }
1303
- }
1304
- const disabledModels = state.disabledModels.get(providerId);
1305
- if (disabledModels && disabledModels.size > 0) {
1306
- const modelId = getProviderModelId(providerKey, this.providerRegistry);
1307
- if (modelId && disabledModels.has(modelId)) {
1308
- continue;
1309
- }
1310
- }
1311
- return true;
1312
- }
1313
- }
1314
- return false;
1315
- });
1316
- }
1317
- selectFromCandidates(routes, metadata, classification, features, state, requiredProviderKeys, allowAliasRotation) {
1318
- // legacy helper kept for backward compatibility; selection logic moved to engine-selection.ts
1319
- return selectProviderImpl(this.normalizeRouteAlias(classification.routeName || DEFAULT_ROUTE), metadata, classification, features, state, {
1320
- routing: this.routing,
1321
- providerRegistry: this.providerRegistry,
1322
- healthManager: this.healthManager,
1323
- contextAdvisor: this.contextAdvisor,
1324
- loadBalancer: this.loadBalancer,
1325
- isProviderCoolingDown: (key) => this.isProviderCoolingDown(key),
1326
- getProviderCooldownRemainingMs: (key) => this.getProviderCooldownRemainingMs(key),
1327
- resolveStickyKey: (m) => this.resolveStickyKey(m),
1328
- quotaView: this.quotaView,
1329
- aliasQueueStore: this.stickySessionManager.getAllStores().aliasQueueStore
1330
- }, { routingState: state });
115
+ return error instanceof Error ? error : new Error(message || 'Virtual router error');
116
+ }
117
+ function extractNativeErrorMessage(error) {
118
+ if (typeof error === 'string') {
119
+ return error;
1331
120
  }
1332
- /**
1333
- * 在已有候选路由集合上,筛选出真正挂载了 sticky 池内 providerKey 的路由,
1334
- * 并按 ROUTE_PRIORITY 进行排序;同时显式排除 tools 路由,保证一旦进入
1335
- * sticky 模式,就不会再命中独立的 tools 池(例如 glm/qwen 工具模型)。
1336
- * 若候选集合中完全没有挂载 sticky key 的路由,则尝试在 default 路由上兜底。
1337
- */
1338
- buildStickyRouteCandidatesFromFiltered(filteredCandidates, stickyKeySet) {
1339
- const routesWithSticky = [];
1340
- const candidateSet = new Set(filteredCandidates.filter((name) => name && name !== 'tools'));
1341
- for (const routeName of candidateSet) {
1342
- const pools = this.routing[routeName];
1343
- if (!this.routeHasTargets(pools)) {
1344
- continue;
1345
- }
1346
- const targets = this.flattenPoolTargets(pools);
1347
- if (!targets.some((key) => stickyKeySet.has(key))) {
1348
- continue;
1349
- }
1350
- routesWithSticky.push(routeName);
1351
- }
1352
- // 若当前候选路由中没有任何挂载 sticky key 的路由,尝试直接在 default 路由上兜底;
1353
- // 若 default 也不包含 sticky key,则视为 sticky 配置失效,由调用方回落到非 sticky 逻辑。
1354
- if (routesWithSticky.length === 0) {
1355
- const defaultPools = this.routing[DEFAULT_ROUTE];
1356
- if (this.routeHasTargets(defaultPools)) {
1357
- const targets = this.flattenPoolTargets(defaultPools);
1358
- if (targets.some((key) => stickyKeySet.has(key))) {
1359
- return [DEFAULT_ROUTE];
1360
- }
1361
- }
1362
- return [];
1363
- }
1364
- const ordered = this.sortByPriority(routesWithSticky);
1365
- const result = [];
1366
- let hasDefault = false;
1367
- for (const routeName of ordered) {
1368
- if (routeName === DEFAULT_ROUTE) {
1369
- hasDefault = true;
1370
- continue;
1371
- }
1372
- if (!result.includes(routeName)) {
1373
- result.push(routeName);
1374
- }
1375
- }
1376
- // default 路由若包含 sticky key,则始终放在候选列表最后,用于 sticky 模式兜底。
1377
- if (hasDefault && !result.includes(DEFAULT_ROUTE)) {
1378
- result.push(DEFAULT_ROUTE);
1379
- }
1380
- return result;
121
+ if (error instanceof Error) {
122
+ return error.message;
1381
123
  }
1382
- /**
1383
- * 在 sticky 模式下,仅在 sticky 池内选择 Provider:
1384
- * - stickyKeySet 表示已经解析并通过健康检查的 providerKey 集合;
1385
- * - 不再依赖 routing[*].targets 中是否挂载这些 key,避免「未初始化路由池」导致 sticky 池为空;
1386
- * - 仍然尊重 allowed/disabledProviders、disabledKeys、disabledModels 以及上下文长度。
1387
- */
1388
- selectFromStickyPool(stickyKeySet, metadata, features, state, allowAliasRotation) {
1389
- if (!stickyKeySet || stickyKeySet.size === 0) {
1390
- return null;
1391
- }
1392
- const allowedProviders = new Set(state.allowedProviders);
1393
- const disabledProviders = new Set(state.disabledProviders);
1394
- const disabledKeysMap = new Map(Array.from(state.disabledKeys.entries()).map(([provider, keys]) => [
1395
- provider,
1396
- new Set(Array.from(keys).map((k) => (typeof k === 'string' ? k : k + 1)))
1397
- ]));
1398
- const disabledModels = new Map(Array.from(state.disabledModels.entries()).map(([provider, models]) => [provider, new Set(models)]));
1399
- // 初始候选集合:sticky 池中的所有 key
1400
- // In quota routing mode, cooldown is controlled by quotaView only.
1401
- let candidates = Array.from(stickyKeySet).filter((key) => (this.quotaView ? true : !this.isProviderCoolingDown(key)));
1402
- // 应用 provider 白名单 / 黑名单
1403
- if (allowedProviders.size > 0) {
1404
- candidates = candidates.filter((key) => {
1405
- const providerId = extractProviderId(key);
1406
- return providerId && allowedProviders.has(providerId);
1407
- });
1408
- }
1409
- if (disabledProviders.size > 0) {
1410
- candidates = candidates.filter((key) => {
1411
- const providerId = extractProviderId(key);
1412
- return providerId && !disabledProviders.has(providerId);
1413
- });
1414
- }
1415
- // 应用 key / model 级别黑名单
1416
- if (disabledKeysMap.size > 0 || disabledModels.size > 0) {
1417
- candidates = candidates.filter((key) => {
1418
- const providerId = extractProviderId(key);
1419
- if (!providerId) {
1420
- return true;
1421
- }
1422
- const disabledKeys = disabledKeysMap.get(providerId);
1423
- if (disabledKeys && disabledKeys.size > 0) {
1424
- const keyAlias = extractKeyAlias(key);
1425
- const keyIndex = extractKeyIndex(key);
1426
- if (keyAlias && disabledKeys.has(keyAlias)) {
1427
- return false;
1428
- }
1429
- if (keyIndex !== undefined && disabledKeys.has(keyIndex + 1)) {
1430
- return false;
1431
- }
1432
- }
1433
- const disabledModelSet = disabledModels.get(providerId);
1434
- if (disabledModelSet && disabledModelSet.size > 0) {
1435
- const modelId = getProviderModelId(key, this.providerRegistry);
1436
- if (modelId && disabledModelSet.has(modelId)) {
1437
- return false;
1438
- }
1439
- }
1440
- return true;
1441
- });
1442
- }
1443
- if (!candidates.length) {
1444
- return null;
1445
- }
1446
- const stickyKey = allowAliasRotation ? undefined : this.resolveStickyKey(metadata);
1447
- const estimatedTokens = typeof features.estimatedTokens === 'number' && Number.isFinite(features.estimatedTokens)
1448
- ? Math.max(0, features.estimatedTokens)
1449
- : 0;
1450
- // delegate to selection module
124
+ return '';
125
+ }
126
+ function parseVirtualRouterErrorMessage(message) {
127
+ if (!message) {
1451
128
  return null;
1452
129
  }
1453
- // mapProviderError/applySeriesCooldown moved to engine-health.ts
1454
- extractExcludedProviderKeySet(metadata) {
1455
- return this.routeAnalytics.extractExcludedProviderKeySet(metadata);
1456
- }
1457
- buildRouteCandidates(requestedRoute, classificationCandidates, features) {
1458
- const forceVision = this.routeHasForceFlag('vision');
1459
- const hasMultimodalTargets = this.routeHasTargets(this.routing.multimodal);
1460
- const hasVisionTargets = this.routeHasTargets(this.routing.vision);
1461
- const hasLocalVideoAttachment = features.hasVideoAttachment === true && features.hasLocalVideoAttachment === true;
1462
- if (features.hasImageAttachment && hasLocalVideoAttachment && hasVisionTargets) {
1463
- return ['vision'];
1464
- }
1465
- const normalized = this.normalizeRouteAlias(requestedRoute || DEFAULT_ROUTE);
1466
- const baseList = [];
1467
- if (classificationCandidates && classificationCandidates.length) {
1468
- for (const candidate of classificationCandidates) {
1469
- baseList.push(this.normalizeRouteAlias(candidate));
1470
- }
1471
- }
1472
- else if (normalized) {
1473
- baseList.push(normalized);
1474
- }
1475
- if (features.hasImageAttachment) {
1476
- if (hasMultimodalTargets) {
1477
- if (!baseList.includes('multimodal')) {
1478
- baseList.unshift('multimodal');
1479
- }
1480
- }
1481
- if (hasVisionTargets && (!hasMultimodalTargets || forceVision)) {
1482
- if (!baseList.includes('vision')) {
1483
- baseList.push('vision');
1484
- }
1485
- }
1486
- if (!forceVision && hasMultimodalTargets) {
1487
- const visionAwareRoutes = [DEFAULT_ROUTE, 'thinking'];
1488
- for (const routeName of visionAwareRoutes) {
1489
- if (this.routeHasTargets(this.routing[routeName]) && !baseList.includes(routeName)) {
1490
- baseList.push(routeName);
1491
- }
1492
- }
1493
- }
1494
- }
1495
- let ordered = this.sortByPriority(baseList);
1496
- if (features.hasImageAttachment && !forceVision && hasMultimodalTargets) {
1497
- ordered = this.reorderForInlineVision(ordered);
1498
- }
1499
- if (features.hasImageAttachment && hasMultimodalTargets) {
1500
- ordered = this.reorderForPreferredModel(ordered, 'kimi-k2.5');
1501
- }
1502
- const deduped = [];
1503
- for (const routeName of ordered) {
1504
- if (routeName && !deduped.includes(routeName)) {
1505
- deduped.push(routeName);
1506
- }
1507
- }
1508
- if (!deduped.includes(DEFAULT_ROUTE)) {
1509
- deduped.push(DEFAULT_ROUTE);
1510
- }
1511
- const filtered = deduped.filter((routeName) => this.routeHasTargets(this.routing[routeName]));
1512
- if (!filtered.includes(DEFAULT_ROUTE) && this.routeHasTargets(this.routing[DEFAULT_ROUTE])) {
1513
- filtered.push(DEFAULT_ROUTE);
1514
- }
1515
- return filtered.length ? filtered : [DEFAULT_ROUTE];
1516
- }
1517
- reorderForInlineVision(routeNames) {
1518
- const unique = Array.from(new Set(routeNames.filter(Boolean)));
1519
- if (!unique.length) {
1520
- return unique;
1521
- }
1522
- // 仅当 default/thinking 中存在 Responses/Gemini 提供方时,才将其提前作为「一次完成」优先级。
1523
- const inlinePreferred = [];
1524
- const inlineRoutes = [DEFAULT_ROUTE, 'thinking'];
1525
- for (const routeName of inlineRoutes) {
1526
- if (this.routeSupportsInlineVision(routeName) && !inlinePreferred.includes(routeName)) {
1527
- inlinePreferred.push(routeName);
1528
- }
1529
- }
1530
- if (!inlinePreferred.length) {
1531
- return unique;
1532
- }
1533
- const remaining = [];
1534
- for (const routeName of unique) {
1535
- if (!inlinePreferred.includes(routeName)) {
1536
- remaining.push(routeName);
1537
- }
1538
- }
1539
- return [...inlinePreferred, ...remaining];
130
+ const normalized = message.startsWith('Error:') ? message.replace(/^Error:\s*/, '') : message;
131
+ if (!normalized.startsWith(VIRTUAL_ROUTER_ERROR_PREFIX)) {
132
+ return null;
1540
133
  }
1541
- reorderForPreferredModel(routeNames, modelId) {
1542
- const unique = Array.from(new Set(routeNames.filter(Boolean)));
1543
- if (!unique.length) {
1544
- return unique;
1545
- }
1546
- const preferred = unique.filter((routeName) => this.routeSupportsModel(routeName, modelId));
1547
- if (!preferred.length) {
1548
- return unique;
1549
- }
1550
- const remaining = unique.filter((routeName) => !preferred.includes(routeName));
1551
- return [...preferred, ...remaining];
134
+ const remainder = normalized.slice(VIRTUAL_ROUTER_ERROR_PREFIX.length);
135
+ const idx = remainder.indexOf(':');
136
+ if (idx <= 0) {
137
+ return null;
1552
138
  }
1553
- routeSupportsModel(routeName, modelId) {
1554
- const normalizedModel = modelId.trim().toLowerCase();
1555
- if (!normalizedModel) {
1556
- return false;
1557
- }
1558
- const pools = this.routing[routeName];
1559
- if (!Array.isArray(pools)) {
1560
- return false;
1561
- }
1562
- for (const pool of pools) {
1563
- if (!Array.isArray(pool.targets)) {
1564
- continue;
1565
- }
1566
- for (const providerKey of pool.targets) {
1567
- try {
1568
- const profile = this.providerRegistry.get(providerKey);
1569
- const candidate = typeof profile.modelId === 'string' ? profile.modelId.trim().toLowerCase() : '';
1570
- if (candidate === normalizedModel) {
1571
- return true;
1572
- }
1573
- }
1574
- catch {
1575
- // ignore unknown provider keys during capability probing
1576
- }
1577
- }
1578
- }
1579
- return false;
139
+ const code = remainder.slice(0, idx);
140
+ const detail = remainder.slice(idx + 1).trim();
141
+ if (!isVirtualRouterErrorCode(code)) {
142
+ return null;
1580
143
  }
1581
- routeSupportsInlineVision(routeName) {
1582
- const pools = this.routing[routeName];
1583
- if (!Array.isArray(pools)) {
1584
- return false;
1585
- }
1586
- for (const pool of pools) {
1587
- if (!Array.isArray(pool.targets)) {
1588
- continue;
1589
- }
1590
- for (const providerKey of pool.targets) {
1591
- try {
1592
- const profile = this.providerRegistry.get(providerKey);
1593
- if (profile.providerType === 'responses' || profile.providerType === 'gemini') {
1594
- return true;
1595
- }
1596
- }
1597
- catch {
1598
- // ignore unknown provider keys during capability probing
1599
- }
1600
- }
1601
- }
144
+ return { code, message: detail || 'Virtual router error' };
145
+ }
146
+ function isVirtualRouterErrorCode(value) {
147
+ return Object.values(VirtualRouterErrorCode).includes(value);
148
+ }
149
+ function isVirtualRouterErrorLike(error) {
150
+ if (!error || typeof error !== 'object') {
1602
151
  return false;
1603
152
  }
1604
- sortByPriority(routeNames) {
1605
- return [...routeNames].sort((a, b) => this.routeWeight(a) - this.routeWeight(b));
1606
- }
1607
- routeWeight(routeName) {
1608
- const idx = ROUTE_PRIORITY.indexOf(routeName);
1609
- return idx >= 0 ? idx : ROUTE_PRIORITY.length;
153
+ const record = error;
154
+ return typeof record.code === 'string' && isVirtualRouterErrorCode(record.code);
155
+ }
156
+ function injectRuntimeNowMs(metadata) {
157
+ const nowMs = Date.now();
158
+ const rt = metadata.__rt;
159
+ if (rt && typeof rt === 'object' && !Array.isArray(rt)) {
160
+ return { ...metadata, __rt: { ...rt, nowMs } };
1610
161
  }
1611
- routeHasForceFlag(routeName) {
1612
- const pools = this.routing[routeName];
1613
- if (!Array.isArray(pools)) {
1614
- return false;
1615
- }
1616
- return pools.some((pool) => pool.force);
162
+ return { ...metadata, __rt: { nowMs } };
163
+ }
164
+ function buildRoutingInstructionParseLog(request, metadata) {
165
+ const messages = Array.isArray(request.messages)
166
+ ? (request.messages ?? [])
167
+ : [];
168
+ if (!messages.length) {
169
+ return null;
1617
170
  }
1618
- routeHasTargets(pools) {
1619
- if (!Array.isArray(pools)) {
171
+ const latest = messages[messages.length - 1];
172
+ const latestText = typeof latest?.content === 'string' ? latest.content.trim() : '';
173
+ const parsedKinds = parseRoutingInstructionKindsWithNative(request);
174
+ const stopMessageTypes = parsedKinds.filter((type) => type === 'stopMessageSet' || type === 'stopMessageMode' || type === 'stopMessageClear');
175
+ const scopedTypes = parsedKinds.filter((type) => type === 'stopMessageSet' ||
176
+ type === 'stopMessageMode' ||
177
+ type === 'stopMessageClear' ||
178
+ type === 'preCommandSet' ||
179
+ type === 'preCommandClear');
180
+ const markerDetected = messages.some((message) => {
181
+ if (!message || typeof message !== 'object') {
1620
182
  return false;
1621
183
  }
1622
- return pools.some((pool) => Array.isArray(pool.targets) && pool.targets.length > 0);
184
+ const record = message;
185
+ return record.role === 'user' && typeof record.content === 'string' && /<\*\*[\s\S]*?\*\*>/.test(record.content);
186
+ });
187
+ const hasStopKeyword = /stopmessage/i.test(latestText);
188
+ if (!hasStopKeyword && stopMessageTypes.length === 0 && scopedTypes.length === 0) {
189
+ return null;
1623
190
  }
1624
- hasPrimaryPool(pools) {
1625
- if (!Array.isArray(pools)) {
1626
- return false;
191
+ return {
192
+ requestId: metadata.requestId || 'n/a',
193
+ markerDetected,
194
+ preview: latestText.replace(/\s+/g, ' ').slice(0, 120),
195
+ stopMessageTypes,
196
+ scopedTypes,
197
+ stopScope: resolveStopMessageScope(metadata)
198
+ };
199
+ }
200
+ function emitRoutingInstructionParseLog(log) {
201
+ if (!log) {
202
+ return;
203
+ }
204
+ const reset = '\x1b[0m';
205
+ const tagColor = '\x1b[38;5;39m';
206
+ const scopeColor = '\x1b[38;5;220m';
207
+ console.log(`${tagColor}[virtual-router][stop_message_parse]${reset} requestId=${log.requestId} marker=${log.markerDetected ? 'detected' : 'missing'} parsed=${log.stopMessageTypes.join(',') || 'none'} preview=${log.preview}`);
208
+ if (log.scopedTypes.length > 0) {
209
+ if (log.stopScope) {
210
+ console.log(`${scopeColor}[virtual-router][stop_scope]${reset} requestId=${log.requestId} stage=apply scope=${log.stopScope} instructions=${log.scopedTypes.join(',')}`);
1627
211
  }
1628
- return pools.some((pool) => !pool.backup && Array.isArray(pool.targets) && pool.targets.length > 0);
1629
- }
1630
- sortRoutePools(pools) {
1631
- if (!Array.isArray(pools)) {
1632
- return [];
212
+ else {
213
+ console.log(`${scopeColor}[virtual-router][stop_scope]${reset} requestId=${log.requestId} stage=drop reason=missing_tmux_scope instructions=${log.scopedTypes.join(',')}`);
1633
214
  }
1634
- return pools
1635
- .filter((pool) => Array.isArray(pool.targets) && pool.targets.length > 0)
1636
- .sort((a, b) => {
1637
- if (a.backup && !b.backup)
1638
- return 1;
1639
- if (!a.backup && b.backup)
1640
- return -1;
1641
- if (a.priority !== b.priority) {
1642
- return b.priority - a.priority;
1643
- }
1644
- return a.id.localeCompare(b.id);
1645
- });
1646
215
  }
1647
- flattenPoolTargets(pools) {
1648
- const flattened = [];
1649
- if (!Array.isArray(pools)) {
1650
- return flattened;
1651
- }
1652
- for (const pool of pools) {
1653
- if (!Array.isArray(pool.targets)) {
1654
- continue;
1655
- }
1656
- for (const target of pool.targets) {
1657
- if (typeof target === 'string' && target && !flattened.includes(target)) {
1658
- flattened.push(target);
1659
- }
216
+ }
217
+ function emitVirtualRouterHitLog(result, options) {
218
+ const reset = '\x1b[0m';
219
+ const prefixColor = '\x1b[38;5;208m';
220
+ const timeColor = '\x1b[90m';
221
+ const stopColor = '\x1b[38;5;214m';
222
+ const routeColorMap = {
223
+ multimodal: '\x1b[38;5;45m',
224
+ tools: '\x1b[38;5;214m',
225
+ thinking: '\x1b[34m',
226
+ coding: '\x1b[35m',
227
+ longcontext: '\x1b[38;5;141m',
228
+ web_search: '\x1b[32m',
229
+ search: '\x1b[38;5;34m',
230
+ vision: '\x1b[38;5;207m',
231
+ background: '\x1b[90m'
232
+ };
233
+ const now = new Date();
234
+ const timestamp = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
235
+ const routeLabel = result.decision.poolId
236
+ ? `${result.decision.routeName}/${result.decision.poolId}`
237
+ : result.decision.routeName;
238
+ const routeColor = routeColorMap[result.decision.routeName] ?? '\x1b[36m';
239
+ const providerKey = result.decision.providerKey || result.target.providerKey;
240
+ const modelSuffix = result.target.modelId ? `.${result.target.modelId}` : '';
241
+ const reason = result.decision.reasoning ? ` reason=${result.decision.reasoning}` : '';
242
+ const stopStatusLabel = formatStopMessageStatusLabel(options?.stopState ?? null, options?.stopScope, Boolean(options?.forceStopStatusLabel));
243
+ const requestId = typeof options?.requestId === 'string' ? options.requestId : '';
244
+ const requestLabel = requestId && !requestId.includes('unknown') ? ` req=${requestId}` : '';
245
+ console.log(`${prefixColor}[virtual-router-hit]${reset} ${timeColor}${timestamp}${reset}${requestLabel} ${routeColor}${routeLabel} -> ${providerKey}${modelSuffix}${reason}${reset}${stopStatusLabel ? ` ${stopColor}${stopStatusLabel}${reset}` : ''}`);
246
+ }
247
+ function formatStopMessageStatusLabel(snapshot, scope, forceShow) {
248
+ const scopeLabel = scope && scope.trim() ? scope.trim() : 'none';
249
+ if (!snapshot) {
250
+ if (!forceShow) {
251
+ return '';
252
+ }
253
+ return `[stopMessage:scope=${scopeLabel} active=no state=cleared]`;
254
+ }
255
+ const text = typeof snapshot.stopMessageText === 'string' ? snapshot.stopMessageText.trim() : '';
256
+ const safeText = text ? (text.length > 24 ? `${text.slice(0, 21)}...` : text) : '(mode-only)';
257
+ const mode = (snapshot.stopMessageStageMode || 'unset').toString().toLowerCase();
258
+ const maxRepeats = typeof snapshot.stopMessageMaxRepeats === 'number' && Number.isFinite(snapshot.stopMessageMaxRepeats)
259
+ ? Math.max(0, Math.floor(snapshot.stopMessageMaxRepeats))
260
+ : 0;
261
+ const used = typeof snapshot.stopMessageUsed === 'number' && Number.isFinite(snapshot.stopMessageUsed)
262
+ ? Math.max(0, Math.floor(snapshot.stopMessageUsed))
263
+ : 0;
264
+ const remaining = maxRepeats > 0 ? Math.max(0, maxRepeats - used) : -1;
265
+ const active = mode !== 'off' && Boolean(text) && maxRepeats > 0;
266
+ const rounds = maxRepeats > 0 ? `${used}/${maxRepeats}` : `${used}/-`;
267
+ const left = remaining >= 0 ? String(remaining) : 'n/a';
268
+ return `[stopMessage:scope=${scopeLabel} text="${safeText}" mode=${mode} round=${rounds} left=${left} active=${active ? 'yes' : 'no'}]`;
269
+ }
270
+ function cleanRoutingInstructionMarkersInPlace(request) {
271
+ const cleaned = cleanRoutingInstructionMarkersWithNative(request);
272
+ if (Array.isArray(cleaned.messages)) {
273
+ request.messages = cleaned.messages;
274
+ }
275
+ const cleanedSemantics = cleaned.semantics;
276
+ if (cleanedSemantics && typeof cleanedSemantics === 'object' && !Array.isArray(cleanedSemantics)) {
277
+ const cleanedResponses = cleanedSemantics.responses;
278
+ if (cleanedResponses && typeof cleanedResponses === 'object' && !Array.isArray(cleanedResponses)) {
279
+ const cleanedContext = cleanedResponses.context;
280
+ if (cleanedContext !== undefined) {
281
+ const semantics = request.semantics && typeof request.semantics === 'object' && !Array.isArray(request.semantics)
282
+ ? request.semantics
283
+ : {};
284
+ const responses = semantics.responses && typeof semantics.responses === 'object' && !Array.isArray(semantics.responses)
285
+ ? semantics.responses
286
+ : {};
287
+ responses.context = cleanedContext;
288
+ semantics.responses = responses;
289
+ request.semantics = semantics;
1660
290
  }
1661
291
  }
1662
- return flattened;
1663
- }
1664
- markProviderCooldown(providerKey, cooldownMs) {
1665
- this.cooldownManager.markProviderCooldown(providerKey, cooldownMs);
1666
- }
1667
- clearProviderCooldown(providerKey) {
1668
- this.cooldownManager.clearProviderCooldown(providerKey);
1669
292
  }
1670
- isProviderCoolingDown(providerKey) {
1671
- return this.cooldownManager.isProviderCoolingDown(providerKey);
1672
- }
1673
- getProviderCooldownRemainingMs(providerKey) {
1674
- if (!providerKey) {
1675
- return 0;
1676
- }
1677
- const expiry = this.providerCooldowns.get(providerKey);
1678
- if (!expiry || !Number.isFinite(expiry)) {
1679
- return 0;
293
+ }
294
+ function collectClientScopedInstructions(request) {
295
+ const messages = Array.isArray(request.messages)
296
+ ? request.messages
297
+ : [];
298
+ const responsesContext = request.semantics && typeof request.semantics === 'object'
299
+ ? request.semantics.responses?.context
300
+ : undefined;
301
+ const responsesHasMarker = hasRoutingInstructionMarkerInResponsesContext(responsesContext);
302
+ const latestUserHasMarker = hasLatestUserRoutingInstructionMarker(messages) || responsesHasMarker;
303
+ if (!latestUserHasMarker && !messages.some((message) => {
304
+ if (!message || typeof message !== 'object') {
305
+ return false;
1680
306
  }
1681
- const remaining = Math.floor(expiry - Date.now());
1682
- return remaining > 0 ? remaining : 0;
307
+ const record = message;
308
+ return record.role === 'user' && typeof record.content === 'string' && ROUTING_INSTRUCTION_MARKER_PATTERN.test(record.content);
309
+ })) {
310
+ return { instructions: [], latestUserHasMarker };
1683
311
  }
1684
- restoreHealthFromStore() {
1685
- this.cooldownManager.restoreHealthFromStore();
1686
- }
1687
- buildHealthSnapshot() {
1688
- const providers = this.healthManager.getSnapshot();
1689
- const cooldownSnapshot = this.cooldownManager.buildHealthSnapshot();
1690
- return { providers, cooldowns: cooldownSnapshot.cooldowns };
1691
- }
1692
- persistHealthSnapshot() {
1693
- this.cooldownManager.persistHealthSnapshot();
312
+ let instructions = parseRoutingInstructions(messages);
313
+ if (instructions.length === 0 && responsesHasMarker) {
314
+ const responsesLatestUserText = getLatestUserTextFromResponsesContext(responsesContext);
315
+ if (responsesLatestUserText) {
316
+ instructions = parseRoutingInstructions([{ role: 'user', content: responsesLatestUserText }]);
317
+ }
1694
318
  }
319
+ return { instructions, latestUserHasMarker };
320
+ }
321
+ function isClientScopedInstruction(instruction) {
322
+ return (instruction.type === 'stopMessageSet' ||
323
+ instruction.type === 'stopMessageMode' ||
324
+ instruction.type === 'stopMessageClear' ||
325
+ instruction.type === 'preCommandSet' ||
326
+ instruction.type === 'preCommandClear' ||
327
+ instruction.type === 'clear');
1695
328
  }