@jsonstudio/llms 0.6.2172 → 0.6.2979

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 (362) hide show
  1. package/README.md +2 -0
  2. package/dist/conversion/compat/antigravity-session-signature.js +35 -1
  3. package/dist/conversion/compat/profiles/chat-gemini-cli.json +0 -7
  4. package/dist/conversion/compat/profiles/chat-gemini.json +0 -6
  5. package/dist/conversion/hub/core/detour-registry.d.ts +2 -0
  6. package/dist/conversion/hub/core/hub-context.d.ts +3 -1
  7. package/dist/conversion/hub/core/index.d.ts +1 -0
  8. package/dist/conversion/hub/core/stage-driver.js +2 -0
  9. package/dist/conversion/hub/format-adapters/anthropic-format-adapter.js +15 -4
  10. package/dist/conversion/hub/format-adapters/chat-format-adapter.js +15 -4
  11. package/dist/conversion/hub/format-adapters/gemini-format-adapter.js +15 -4
  12. package/dist/conversion/hub/format-adapters/responses-format-adapter.js +15 -4
  13. package/dist/conversion/hub/hub-feature.js +3 -2
  14. package/dist/conversion/hub/node-support.js +9 -4
  15. package/dist/conversion/hub/operation-table/operation-table-runner.js +10 -6
  16. package/dist/conversion/hub/operation-table/semantic-mappers/anthropic-mapper.js +2 -2
  17. package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +10 -10
  18. package/dist/conversion/hub/operation-table/semantic-mappers/gemini-mapper.js +4 -8
  19. package/dist/conversion/hub/operation-table/semantic-mappers/responses-mapper.js +19 -65
  20. package/dist/conversion/hub/ops/operations.js +2 -121
  21. package/dist/conversion/hub/pipeline/compat/compat-engine.js +6 -0
  22. package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.d.ts +1 -1
  23. package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +33 -1042
  24. package/dist/conversion/hub/pipeline/compat/compat-profile-resolver.js +2 -0
  25. package/dist/conversion/hub/pipeline/compat/compat-profile-store.js +2 -0
  26. package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +14 -17
  27. package/dist/conversion/hub/pipeline/compat/native-adapter-context.d.ts +3 -0
  28. package/dist/conversion/hub/pipeline/compat/native-adapter-context.js +39 -0
  29. package/dist/conversion/hub/pipeline/hub-pipeline.js +115 -262
  30. package/dist/conversion/hub/pipeline/session-identifiers.js +6 -196
  31. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage1_format_parse/index.d.ts +1 -2
  32. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage1_format_parse/index.js +37 -1
  33. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/index.js +12 -86
  34. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/semantic-lift.d.ts +14 -0
  35. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/semantic-lift.js +24 -0
  36. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/archive/shell-like-tool-call-normalization-fallback.d.ts +2 -0
  37. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/archive/shell-like-tool-call-normalization-fallback.js +157 -0
  38. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/context-capture-orchestration.d.ts +16 -0
  39. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/context-capture-orchestration.js +29 -0
  40. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/context-factories.js +3 -1
  41. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/index.d.ts +2 -15
  42. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/index.js +8 -595
  43. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/responses-context-snapshot.d.ts +8 -0
  44. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/responses-context-snapshot.js +28 -0
  45. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/tool-output-diagnostics.d.ts +2 -0
  46. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/tool-output-diagnostics.js +4 -0
  47. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/tool-output-snapshot.d.ts +10 -0
  48. package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/tool-output-snapshot.js +12 -0
  49. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage1_semantic_map/context-merge.d.ts +3 -0
  50. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage1_semantic_map/context-merge.js +30 -0
  51. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage1_semantic_map/index.js +9 -129
  52. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage2_format_build/index.d.ts +1 -4
  53. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage2_format_build/index.js +9 -26
  54. package/dist/conversion/hub/pipeline/stages/req_outbound/req_outbound_stage3_compat/index.js +32 -14
  55. package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage1_tool_governance/index.d.ts +2 -2
  56. package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage1_tool_governance/index.js +48 -8
  57. package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage2_route_select/index.js +18 -3
  58. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage1_sse_decode/index.js +10 -198
  59. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage1_sse_decode/stream-json-sniffer.d.ts +3 -0
  60. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage1_sse_decode/stream-json-sniffer.js +81 -0
  61. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage2_format_parse/index.d.ts +1 -2
  62. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage2_format_parse/index.js +36 -1
  63. package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage3_semantic_map/index.js +3 -1
  64. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/chat-process-semantics-bridge.d.ts +6 -0
  65. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/chat-process-semantics-bridge.js +17 -0
  66. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/client-remap-protocol-switch.d.ts +9 -0
  67. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/client-remap-protocol-switch.js +28 -0
  68. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/index.d.ts +1 -2
  69. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/index.js +14 -102
  70. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage2_sse_stream/index.js +3 -1
  71. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage1_tool_governance/index.js +12 -10
  72. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage2_finalize/index.js +6 -5
  73. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage3_servertool_orchestration/index.d.ts +11 -1
  74. package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage3_servertool_orchestration/index.js +10 -32
  75. package/dist/conversion/hub/pipeline/stages/utils.js +17 -1
  76. package/dist/conversion/hub/pipeline/target-utils.js +14 -91
  77. package/dist/conversion/hub/pipelines/inbound.js +3 -1
  78. package/dist/conversion/hub/pipelines/outbound.js +2 -0
  79. package/dist/conversion/hub/policy/policy-engine.js +9 -3
  80. package/dist/conversion/hub/policy/protocol-spec.js +20 -148
  81. package/dist/conversion/hub/process/chat-process-anthropic-alias.d.ts +2 -0
  82. package/dist/conversion/hub/process/chat-process-anthropic-alias.js +36 -0
  83. package/dist/conversion/hub/process/chat-process-clock-directive-parser.d.ts +5 -0
  84. package/dist/conversion/hub/process/chat-process-clock-directive-parser.js +48 -0
  85. package/dist/conversion/hub/process/chat-process-clock-directives.d.ts +24 -0
  86. package/dist/conversion/hub/process/chat-process-clock-directives.js +98 -0
  87. package/dist/conversion/hub/process/chat-process-clock-reminder-directives.d.ts +8 -0
  88. package/dist/conversion/hub/process/chat-process-clock-reminder-directives.js +42 -0
  89. package/dist/conversion/hub/process/chat-process-clock-reminder-finalize.d.ts +14 -0
  90. package/dist/conversion/hub/process/chat-process-clock-reminder-finalize.js +10 -0
  91. package/dist/conversion/hub/process/chat-process-clock-reminder-messages.d.ts +5 -0
  92. package/dist/conversion/hub/process/chat-process-clock-reminder-messages.js +10 -0
  93. package/dist/conversion/hub/process/chat-process-clock-reminder-orchestration.d.ts +30 -0
  94. package/dist/conversion/hub/process/chat-process-clock-reminder-orchestration.js +68 -0
  95. package/dist/conversion/hub/process/chat-process-clock-reminder-time-tag.d.ts +9 -0
  96. package/dist/conversion/hub/process/chat-process-clock-reminder-time-tag.js +18 -0
  97. package/dist/conversion/hub/process/chat-process-clock-reminders.d.ts +2 -0
  98. package/dist/conversion/hub/process/chat-process-clock-reminders.js +104 -0
  99. package/dist/conversion/hub/process/chat-process-clock-tool-schemas.d.ts +3 -0
  100. package/dist/conversion/hub/process/chat-process-clock-tool-schemas.js +233 -0
  101. package/dist/conversion/hub/process/chat-process-clock-tools.d.ts +6 -0
  102. package/dist/conversion/hub/process/chat-process-clock-tools.js +41 -0
  103. package/dist/conversion/hub/process/chat-process-continue-execution.d.ts +11 -0
  104. package/dist/conversion/hub/process/chat-process-continue-execution.js +82 -0
  105. package/dist/conversion/hub/process/chat-process-governance-context.d.ts +15 -0
  106. package/dist/conversion/hub/process/chat-process-governance-context.js +7 -0
  107. package/dist/conversion/hub/process/chat-process-governance-finalize.d.ts +16 -0
  108. package/dist/conversion/hub/process/chat-process-governance-finalize.js +11 -0
  109. package/dist/conversion/hub/process/chat-process-governance-orchestration.d.ts +9 -0
  110. package/dist/conversion/hub/process/chat-process-governance-orchestration.js +47 -0
  111. package/dist/conversion/hub/process/chat-process-governed-control-ops.d.ts +8 -0
  112. package/dist/conversion/hub/process/chat-process-governed-control-ops.js +5 -0
  113. package/dist/conversion/hub/process/chat-process-governed-filter-call.d.ts +12 -0
  114. package/dist/conversion/hub/process/chat-process-governed-filter-call.js +18 -0
  115. package/dist/conversion/hub/process/chat-process-governed-merge.d.ts +8 -0
  116. package/dist/conversion/hub/process/chat-process-governed-merge.js +11 -0
  117. package/dist/conversion/hub/process/chat-process-media.d.ts +3 -0
  118. package/dist/conversion/hub/process/chat-process-media.js +18 -0
  119. package/dist/conversion/hub/process/chat-process-node-result.d.ts +23 -0
  120. package/dist/conversion/hub/process/chat-process-node-result.js +24 -0
  121. package/dist/conversion/hub/process/chat-process-pending-tool-sync.d.ts +14 -0
  122. package/dist/conversion/hub/process/chat-process-pending-tool-sync.js +52 -0
  123. package/dist/conversion/hub/process/chat-process-post-governed-normalization.d.ts +8 -0
  124. package/dist/conversion/hub/process/chat-process-post-governed-normalization.js +16 -0
  125. package/dist/conversion/hub/process/chat-process-review.d.ts +2 -0
  126. package/dist/conversion/hub/process/chat-process-review.js +8 -0
  127. package/dist/conversion/hub/process/chat-process-servertool-orchestration.d.ts +8 -0
  128. package/dist/conversion/hub/process/chat-process-servertool-orchestration.js +22 -0
  129. package/dist/conversion/hub/process/chat-process-tool-normalization.d.ts +2 -0
  130. package/dist/conversion/hub/process/chat-process-tool-normalization.js +4 -0
  131. package/dist/conversion/hub/process/chat-process-web-search-intent.d.ts +12 -0
  132. package/dist/conversion/hub/process/chat-process-web-search-intent.js +13 -0
  133. package/dist/conversion/hub/process/chat-process-web-search-tool-schema.d.ts +3 -0
  134. package/dist/conversion/hub/process/chat-process-web-search-tool-schema.js +4 -0
  135. package/dist/conversion/hub/process/chat-process-web-search.d.ts +8 -0
  136. package/dist/conversion/hub/process/chat-process-web-search.js +26 -0
  137. package/dist/conversion/hub/process/chat-process.d.ts +2 -19
  138. package/dist/conversion/hub/process/chat-process.js +12 -1701
  139. package/dist/conversion/hub/process/client-inject-readiness.d.ts +1 -0
  140. package/dist/conversion/hub/process/client-inject-readiness.js +4 -0
  141. package/dist/conversion/hub/registry.js +5 -2
  142. package/dist/conversion/hub/response/chat-response-utils.js +5 -86
  143. package/dist/conversion/hub/response/provider-response.d.ts +9 -0
  144. package/dist/conversion/hub/response/provider-response.js +6 -21
  145. package/dist/conversion/hub/response/response-mappers.js +2 -26
  146. package/dist/conversion/hub/response/response-runtime.js +2 -93
  147. package/dist/conversion/hub/semantic-mappers/anthropic-mapper.d.ts +1 -0
  148. package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +1 -0
  149. package/dist/conversion/hub/semantic-mappers/chat-mapper.d.ts +1 -0
  150. package/dist/conversion/hub/semantic-mappers/chat-mapper.js +1 -0
  151. package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +1 -0
  152. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +1 -0
  153. package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +1 -0
  154. package/dist/conversion/hub/semantic-mappers/responses-mapper.js +1 -0
  155. package/dist/conversion/hub/snapshot-recorder.js +10 -3
  156. package/dist/conversion/hub/standardized-bridge.js +11 -288
  157. package/dist/conversion/hub/tool-governance/engine.js +5 -0
  158. package/dist/conversion/hub/tool-governance/rules.js +10 -10
  159. package/dist/conversion/hub/tool-session-compat.d.ts +2 -2
  160. package/dist/conversion/hub/tool-session-compat.js +17 -231
  161. package/dist/conversion/hub/tool-surface/tool-surface-engine.js +5 -3
  162. package/dist/conversion/responses/responses-host-policy.js +2 -12
  163. package/dist/conversion/responses/responses-openai-bridge/response-payload.js +6 -82
  164. package/dist/conversion/responses/responses-openai-bridge/types.d.ts +1 -0
  165. package/dist/conversion/responses/responses-openai-bridge.js +21 -54
  166. package/dist/conversion/shared/anthropic-message-utils.js +151 -13
  167. package/dist/conversion/shared/args-mapping.js +2 -146
  168. package/dist/conversion/shared/bridge-actions.js +203 -718
  169. package/dist/conversion/shared/bridge-id-utils.js +5 -71
  170. package/dist/conversion/shared/bridge-instructions.js +2 -1
  171. package/dist/conversion/shared/bridge-message-types.d.ts +2 -0
  172. package/dist/conversion/shared/bridge-message-utils.js +1 -2
  173. package/dist/conversion/shared/bridge-metadata.d.ts +1 -0
  174. package/dist/conversion/shared/bridge-metadata.js +4 -0
  175. package/dist/conversion/shared/bridge-policies.js +5 -189
  176. package/dist/conversion/shared/chat-envelope-validator.js +2 -126
  177. package/dist/conversion/shared/chat-output-normalizer.js +2 -54
  178. package/dist/conversion/shared/compaction-detect.js +2 -57
  179. package/dist/conversion/shared/gemini-tool-utils.js +9 -524
  180. package/dist/conversion/shared/jsonish.js +3 -160
  181. package/dist/conversion/shared/mcp-injection.js +3 -169
  182. package/dist/conversion/shared/media.js +2 -7
  183. package/dist/conversion/shared/metadata-passthrough.js +9 -46
  184. package/dist/conversion/shared/openai-finalizer.js +2 -1
  185. package/dist/conversion/shared/openai-message-normalize.js +11 -283
  186. package/dist/conversion/shared/output-content-normalizer.js +9 -112
  187. package/dist/conversion/shared/payload-budget.js +2 -85
  188. package/dist/conversion/shared/protocol-state.js +11 -7
  189. package/dist/conversion/shared/reasoning-mapping.js +2 -6
  190. package/dist/conversion/shared/reasoning-normalizer.js +4 -1
  191. package/dist/conversion/shared/reasoning-tool-normalizer.js +14 -126
  192. package/dist/conversion/shared/reasoning-tool-parser.js +4 -87
  193. package/dist/conversion/shared/reasoning-utils.js +2 -6
  194. package/dist/conversion/shared/responses-conversation-store.js +4 -82
  195. package/dist/conversion/shared/responses-output-builder.js +11 -47
  196. package/dist/conversion/shared/responses-reasoning-registry.js +7 -1
  197. package/dist/conversion/shared/responses-request-adapter.d.ts +7 -1
  198. package/dist/conversion/shared/responses-request-adapter.js +14 -1
  199. package/dist/conversion/shared/responses-response-utils.js +6 -7
  200. package/dist/conversion/shared/responses-tool-utils.d.ts +1 -0
  201. package/dist/conversion/shared/responses-tool-utils.js +90 -14
  202. package/dist/conversion/shared/runtime-metadata.js +13 -5
  203. package/dist/conversion/shared/streaming-text-extractor.js +2 -7
  204. package/dist/conversion/shared/text-markup-normalizer/normalize.d.ts +1 -1
  205. package/dist/conversion/shared/text-markup-normalizer/normalize.js +43 -17
  206. package/dist/conversion/shared/text-markup-normalizer.d.ts +1 -0
  207. package/dist/conversion/shared/text-markup-normalizer.js +1 -0
  208. package/dist/conversion/shared/thought-signature-validator.js +3 -2
  209. package/dist/conversion/shared/tool-argument-repairer.js +2 -2
  210. package/dist/conversion/shared/tool-call-id-manager.js +5 -7
  211. package/dist/conversion/shared/tool-call-utils.js +3 -45
  212. package/dist/conversion/shared/tool-canonicalizer.js +25 -29
  213. package/dist/conversion/shared/tool-filter-pipeline.js +4 -99
  214. package/dist/conversion/shared/tool-governor.d.ts +6 -0
  215. package/dist/conversion/shared/tool-governor.js +43 -125
  216. package/dist/conversion/shared/tool-harvester.js +2 -8
  217. package/dist/conversion/shared/tool-mapping.js +2 -5
  218. package/dist/conversion/shared/tooling.d.ts +0 -4
  219. package/dist/conversion/shared/tooling.js +18 -0
  220. package/dist/native/router_hotpath_napi.node +0 -0
  221. package/dist/router/virtual-router/engine/provider-key/parse.d.ts +1 -6
  222. package/dist/router/virtual-router/engine/provider-key/parse.js +1 -43
  223. package/dist/router/virtual-router/engine/routing-state/store.js +48 -12
  224. package/dist/router/virtual-router/engine-logging.js +4 -3
  225. package/dist/router/virtual-router/engine-selection/alias-selection.js +45 -83
  226. package/dist/router/virtual-router/engine-selection/key-parsing.js +9 -23
  227. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-directive-parser.d.ts +20 -0
  228. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-directive-parser.js +163 -0
  229. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-directives.d.ts +7 -0
  230. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-directives.js +103 -0
  231. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-orchestration-semantics.d.ts +10 -0
  232. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-orchestration-semantics.js +110 -0
  233. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-semantics.d.ts +8 -0
  234. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-semantics.js +281 -0
  235. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-time-tag-semantics.d.ts +1 -0
  236. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminder-time-tag-semantics.js +25 -0
  237. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminders-semantics.d.ts +4 -0
  238. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-reminders-semantics.js +44 -0
  239. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-tool-schema-semantics.d.ts +2 -0
  240. package/dist/router/virtual-router/engine-selection/native-chat-process-clock-tool-schema-semantics.js +62 -0
  241. package/dist/router/virtual-router/engine-selection/native-chat-process-governance-semantics.d.ts +40 -0
  242. package/dist/router/virtual-router/engine-selection/native-chat-process-governance-semantics.js +484 -0
  243. package/dist/router/virtual-router/engine-selection/native-chat-process-governed-filter-semantics.d.ts +9 -0
  244. package/dist/router/virtual-router/engine-selection/native-chat-process-governed-filter-semantics.js +64 -0
  245. package/dist/router/virtual-router/engine-selection/native-chat-process-node-result-semantics.d.ts +5 -0
  246. package/dist/router/virtual-router/engine-selection/native-chat-process-node-result-semantics.js +163 -0
  247. package/dist/router/virtual-router/engine-selection/native-chat-process-post-governed-normalization-semantics.d.ts +1 -0
  248. package/dist/router/virtual-router/engine-selection/native-chat-process-post-governed-normalization-semantics.js +49 -0
  249. package/dist/router/virtual-router/engine-selection/native-chat-process-servertool-orchestration-semantics.d.ts +30 -0
  250. package/dist/router/virtual-router/engine-selection/native-chat-process-servertool-orchestration-semantics.js +446 -0
  251. package/dist/router/virtual-router/engine-selection/native-chat-process-web-search-intent-semantics.d.ts +1 -0
  252. package/dist/router/virtual-router/engine-selection/native-chat-process-web-search-intent-semantics.js +49 -0
  253. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.d.ts +134 -0
  254. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.js +729 -0
  255. package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.d.ts +62 -0
  256. package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.js +338 -0
  257. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-edge-stage-semantics.d.ts +18 -0
  258. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-edge-stage-semantics.js +317 -0
  259. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-inbound-outbound-semantics.d.ts +22 -0
  260. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-inbound-outbound-semantics.js +426 -0
  261. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-orchestration-semantics.d.ts +57 -0
  262. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-orchestration-semantics.js +705 -0
  263. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.d.ts +46 -0
  264. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js +503 -0
  265. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.d.ts +146 -0
  266. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.js +570 -0
  267. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-process-semantics.d.ts +25 -0
  268. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-process-semantics.js +148 -0
  269. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-resp-semantics.d.ts +25 -0
  270. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-resp-semantics.js +637 -0
  271. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-session-identifiers-semantics.d.ts +11 -0
  272. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-session-identifiers-semantics.js +207 -0
  273. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-target-semantics.d.ts +3 -0
  274. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-target-semantics.js +128 -0
  275. package/dist/router/virtual-router/engine-selection/native-router-hotpath-analysis.d.ts +57 -0
  276. package/dist/router/virtual-router/engine-selection/native-router-hotpath-analysis.js +217 -0
  277. package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.d.ts +5 -0
  278. package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +284 -0
  279. package/dist/router/virtual-router/engine-selection/native-router-hotpath-policy.d.ts +5 -0
  280. package/dist/router/virtual-router/engine-selection/native-router-hotpath-policy.js +18 -0
  281. package/dist/router/virtual-router/engine-selection/native-router-hotpath-quota-buckets.d.ts +25 -0
  282. package/dist/router/virtual-router/engine-selection/native-router-hotpath-quota-buckets.js +85 -0
  283. package/dist/router/virtual-router/engine-selection/native-router-hotpath.d.ts +59 -0
  284. package/dist/router/virtual-router/engine-selection/native-router-hotpath.js +117 -0
  285. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +76 -0
  286. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +1166 -0
  287. package/dist/router/virtual-router/engine-selection/native-virtual-router-alias-selection-semantics.d.ts +16 -0
  288. package/dist/router/virtual-router/engine-selection/native-virtual-router-alias-selection-semantics.js +96 -0
  289. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-actions-semantics.d.ts +6 -0
  290. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-actions-semantics.js +85 -0
  291. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-semantics.d.ts +9 -0
  292. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-semantics.js +70 -0
  293. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-state-semantics.d.ts +2 -0
  294. package/dist/router/virtual-router/engine-selection/native-virtual-router-stop-message-state-semantics.js +76 -0
  295. package/dist/router/virtual-router/engine-selection/route-utils.js +1 -1
  296. package/dist/router/virtual-router/engine-selection/tier-selection-antigravity-session-lease.d.ts +10 -0
  297. package/dist/router/virtual-router/engine-selection/tier-selection-antigravity-session-lease.js +231 -0
  298. package/dist/router/virtual-router/engine-selection/tier-selection-antigravity-target-split.d.ts +4 -0
  299. package/dist/router/virtual-router/engine-selection/tier-selection-antigravity-target-split.js +43 -0
  300. package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.d.ts +27 -0
  301. package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.js +116 -0
  302. package/dist/router/virtual-router/engine-selection/tier-selection-select.d.ts +1 -1
  303. package/dist/router/virtual-router/engine-selection/tier-selection-select.js +29 -129
  304. package/dist/router/virtual-router/engine-selection/tier-selection.js +2 -265
  305. package/dist/router/virtual-router/engine.js +258 -249
  306. package/dist/router/virtual-router/features.js +2 -2
  307. package/dist/router/virtual-router/routing-instructions.d.ts +5 -7
  308. package/dist/router/virtual-router/routing-instructions.js +93 -66
  309. package/dist/router/virtual-router/routing-stop-message-actions.js +91 -112
  310. package/dist/router/virtual-router/routing-stop-message-parser.js +9 -132
  311. package/dist/router/virtual-router/routing-stop-message-state-codec.d.ts +1 -0
  312. package/dist/router/virtual-router/routing-stop-message-state-codec.js +58 -71
  313. package/dist/router/virtual-router/sticky-session-store.js +4 -2
  314. package/dist/router/virtual-router/stop-message-file-resolver.d.ts +1 -0
  315. package/dist/router/virtual-router/stop-message-file-resolver.js +10 -0
  316. package/dist/router/virtual-router/stop-message-state-sync.d.ts +1 -1
  317. package/dist/router/virtual-router/stop-message-state-sync.js +3 -7
  318. package/dist/router/virtual-router/token-counter.js +0 -9
  319. package/dist/router/virtual-router/types.d.ts +9 -7
  320. package/dist/servertool/clock/config.js +23 -51
  321. package/dist/servertool/clock/io.js +1 -0
  322. package/dist/servertool/clock/session-scope.d.ts +2 -2
  323. package/dist/servertool/clock/session-scope.js +5 -47
  324. package/dist/servertool/engine.d.ts +9 -0
  325. package/dist/servertool/engine.js +196 -79
  326. package/dist/servertool/handlers/antigravity-thought-signature-bootstrap.js +2 -2
  327. package/dist/servertool/handlers/clock.js +1 -1
  328. package/dist/servertool/handlers/continue-execution.js +8 -4
  329. package/dist/servertool/handlers/followup-request-builder.js +18 -1
  330. package/dist/servertool/handlers/gemini-empty-reply-continue.js +7 -1
  331. package/dist/servertool/handlers/review.js +180 -0
  332. package/dist/servertool/handlers/stop-message-auto/blocked-report.js +59 -1
  333. package/dist/servertool/handlers/stop-message-auto/iflow-followup.d.ts +23 -2
  334. package/dist/servertool/handlers/stop-message-auto/iflow-followup.js +397 -89
  335. package/dist/servertool/handlers/stop-message-auto/routing-state.d.ts +5 -15
  336. package/dist/servertool/handlers/stop-message-auto/routing-state.js +29 -55
  337. package/dist/servertool/handlers/stop-message-auto/runtime-utils.d.ts +6 -0
  338. package/dist/servertool/handlers/stop-message-auto/runtime-utils.js +35 -61
  339. package/dist/servertool/handlers/stop-message-auto.js +392 -76
  340. package/dist/servertool/server-side-tools.d.ts +1 -0
  341. package/dist/servertool/server-side-tools.js +90 -52
  342. package/dist/servertool/types.d.ts +17 -0
  343. package/dist/tools/apply-patch/patch-text/normalize.js +11 -0
  344. package/dist/tools/exec-command/validator.d.ts +4 -1
  345. package/dist/tools/exec-command/validator.js +87 -3
  346. package/dist/tools/tool-registry.d.ts +7 -1
  347. package/dist/tools/tool-registry.js +3 -2
  348. package/package.json +115 -7
  349. package/dist/servertool/handlers/stop-message-stage-policy/bd-runtime.d.ts +0 -18
  350. package/dist/servertool/handlers/stop-message-stage-policy/bd-runtime.js +0 -398
  351. package/dist/servertool/handlers/stop-message-stage-policy/decision.d.ts +0 -9
  352. package/dist/servertool/handlers/stop-message-stage-policy/decision.js +0 -127
  353. package/dist/servertool/handlers/stop-message-stage-policy/observation.d.ts +0 -2
  354. package/dist/servertool/handlers/stop-message-stage-policy/observation.js +0 -179
  355. package/dist/servertool/handlers/stop-message-stage-policy/templates.d.ts +0 -4
  356. package/dist/servertool/handlers/stop-message-stage-policy/templates.js +0 -96
  357. package/dist/servertool/handlers/stop-message-stage-policy/text-utils.d.ts +0 -9
  358. package/dist/servertool/handlers/stop-message-stage-policy/text-utils.js +0 -89
  359. package/dist/servertool/handlers/stop-message-stage-policy/types.d.ts +0 -59
  360. package/dist/servertool/handlers/stop-message-stage-policy.d.ts +0 -3
  361. package/dist/servertool/handlers/stop-message-stage-policy.js +0 -2
  362. /package/dist/servertool/handlers/{stop-message-stage-policy/types.js → review.d.ts} +0 -0
@@ -1,8 +1,4 @@
1
- import { normalizeMessageReasoningTools } from './reasoning-tool-normalizer.js';
2
- import { extractReasoningSegments } from './reasoning-utils.js';
3
- import { extractOutputSegments } from './output-content-normalizer.js';
4
- import { normalizeChatMessageContent } from './chat-output-normalizer.js';
5
- import { convertMessagesToBridgeInput } from './bridge-message-utils.js';
1
+ import { applyBridgeCaptureToolResultsWithNative, applyBridgeEnsureToolPlaceholdersWithNative, applyBridgeNormalizeHistoryWithNative, applyBridgeNormalizeToolIdentifiersWithNative, applyBridgeEnsureSystemInstructionWithNative, applyBridgeInjectSystemInstructionWithNative, applyBridgeMetadataActionWithNative, applyBridgeReasoningExtractWithNative, applyBridgeResponsesOutputReasoningWithNative, ensureBridgeOutputFieldsWithNative } from '../../router/virtual-router/engine-selection/native-hub-bridge-action-semantics.js';
6
2
  const registry = new Map();
7
3
  export function registerBridgeAction(name, action) {
8
4
  registry.set(name, action);
@@ -54,33 +50,6 @@ function ensureMessagesArray(state) {
54
50
  }
55
51
  return state.messages;
56
52
  }
57
- function flattenContentToString(value) {
58
- if (typeof value === 'string') {
59
- return value;
60
- }
61
- if (Array.isArray(value)) {
62
- const parts = value
63
- .map((entry) => flattenContentToString(entry))
64
- .filter((entry) => typeof entry === 'string' && entry.length > 0);
65
- if (parts.length) {
66
- return parts.join('\n');
67
- }
68
- return '';
69
- }
70
- if (value && typeof value === 'object') {
71
- const record = value;
72
- if (typeof record.text === 'string') {
73
- return record.text;
74
- }
75
- if (typeof record.content === 'string') {
76
- return record.content;
77
- }
78
- if (Array.isArray(record.content)) {
79
- return flattenContentToString(record.content);
80
- }
81
- }
82
- return undefined;
83
- }
84
53
  function ensureMetadataRecord(state) {
85
54
  if (!state.metadata || typeof state.metadata !== 'object') {
86
55
  state.metadata = {};
@@ -88,278 +57,45 @@ function ensureMetadataRecord(state) {
88
57
  return state.metadata;
89
58
  }
90
59
  const injectSystemInstructionAction = (ctx) => {
91
- if (ctx.stage !== 'request_inbound')
92
- return;
93
- const raw = ctx.state.rawRequest;
94
- if (!raw || typeof raw !== 'object')
95
- return;
96
- const field = typeof ctx.descriptor.options?.field === 'string' ? ctx.descriptor.options.field : 'instructions';
97
- const reasoningField = typeof ctx.descriptor.options?.reasoningField === 'string' ? ctx.descriptor.options.reasoningField : undefined;
98
- const instructions = readInstructionValue(raw, field);
99
- if (!instructions)
100
- return;
101
- const reasoningSegments = readInstructionReasoning(raw, reasoningField);
102
- const message = buildInstructionMessage('system', instructions, reasoningSegments);
103
- if (!message)
104
- return;
105
60
  const messages = ensureMessagesArray(ctx.state);
106
- messages.unshift(message);
107
- };
108
- function readInstructionValue(source, field) {
109
- if (!source || typeof source !== 'object')
110
- return undefined;
111
- const raw = source[field];
112
- if (typeof raw !== 'string')
113
- return undefined;
114
- const trimmed = raw.trim();
115
- return trimmed.length ? trimmed : undefined;
116
- }
117
- function readInstructionReasoning(source, field) {
118
- if (!field || !source || typeof source !== 'object')
119
- return [];
120
- const raw = source[field];
121
- if (!raw)
122
- return [];
123
- if (typeof raw === 'string') {
124
- const trimmed = raw.trim();
125
- return trimmed.length ? [trimmed] : [];
126
- }
127
- if (Array.isArray(raw)) {
128
- return raw
129
- .map((entry) => (typeof entry === 'string' ? entry.trim() : ''))
130
- .filter((entry) => entry.length > 0);
131
- }
132
- return [];
133
- }
134
- function buildInstructionMessage(role, text, reasoningSegments) {
135
- const normalized = normalizeChatMessageContent(text);
136
- const content = typeof normalized.contentText === 'string' ? normalized.contentText : '';
137
- const merged = [];
138
- if (typeof normalized.reasoningText === 'string' && normalized.reasoningText.trim().length) {
139
- merged.push(normalized.reasoningText.trim());
140
- }
141
- for (const segment of reasoningSegments) {
142
- if (typeof segment === 'string' && segment.trim().length) {
143
- merged.push(segment.trim());
144
- }
145
- }
146
- if (!content.trim().length && !merged.length) {
147
- return null;
148
- }
149
- const message = { role, content };
150
- if (merged.length) {
151
- message.reasoning_content = merged.join('\n');
152
- }
153
- return message;
154
- }
155
- function mapToolResults(state) {
156
- const toolResults = new Map();
157
- const append = (id, output, name) => {
158
- if (typeof id !== 'string' || !id.trim()) {
159
- return;
160
- }
161
- let text = '';
162
- if (typeof output === 'string') {
163
- text = output;
164
- }
165
- else if (output != null) {
166
- try {
167
- text = JSON.stringify(output);
168
- }
169
- catch {
170
- text = String(output);
171
- }
172
- }
173
- const trimmed = text.trim();
174
- toolResults.set(id, trimmed);
175
- if (typeof name === 'string' && name.trim().length) {
176
- toolResults.set(`${id}::name`, name.trim());
177
- }
178
- };
179
- const captured = state.capturedToolResults;
180
- if (Array.isArray(captured)) {
181
- for (const entry of captured) {
182
- append(entry?.tool_call_id ?? entry?.call_id, entry?.output, entry?.name);
183
- }
184
- }
185
- return toolResults;
186
- }
187
- function findFirstNonToolIndex(messages, startIndex) {
188
- let index = startIndex;
189
- while (index < messages.length) {
190
- const msg = messages[index];
191
- if (!msg || typeof msg !== 'object') {
192
- break;
193
- }
194
- const role = String(msg.role || '').toLowerCase();
195
- if (role !== 'tool') {
196
- break;
197
- }
198
- index++;
199
- }
200
- return index;
201
- }
202
- function extractToolCallIdentifier(message) {
203
- const id = message.tool_call_id ||
204
- message.call_id ||
205
- message.tool_use_id ||
206
- message.id;
207
- return typeof id === 'string' && id.trim().length ? id.trim() : undefined;
208
- }
209
- function findToolResponseIndex(messages, startIndex, endIndex, callId) {
210
- const limit = Math.min(endIndex, messages.length);
211
- for (let i = startIndex; i < limit; i++) {
212
- const msg = messages[i];
213
- if (!msg || typeof msg !== 'object') {
214
- continue;
215
- }
216
- const role = String(msg.role || '').toLowerCase();
217
- if (role !== 'tool') {
218
- break;
219
- }
220
- const id = extractToolCallIdentifier(msg);
221
- if (id === callId) {
222
- return i;
223
- }
61
+ const normalized = applyBridgeInjectSystemInstructionWithNative({
62
+ stage: ctx.stage,
63
+ options: ctx.descriptor.options && typeof ctx.descriptor.options === 'object'
64
+ ? ctx.descriptor.options
65
+ : undefined,
66
+ messages,
67
+ rawRequest: ctx.state.rawRequest && typeof ctx.state.rawRequest === 'object'
68
+ ? ctx.state.rawRequest
69
+ : undefined
70
+ });
71
+ if (Array.isArray(normalized.messages)) {
72
+ applyMessagesInPlace(messages, normalized.messages);
73
+ ctx.state.messages = messages;
224
74
  }
225
- return -1;
226
- }
227
- function extractToolResponse(messages, startIndex, callId) {
228
- for (let i = startIndex; i < messages.length; i++) {
229
- const msg = messages[i];
230
- if (!msg || typeof msg !== 'object') {
231
- continue;
232
- }
233
- const role = String(msg.role || '').toLowerCase();
234
- if (role !== 'tool') {
235
- continue;
236
- }
237
- const id = extractToolCallIdentifier(msg);
238
- if (id === callId) {
239
- const [removed] = messages.splice(i, 1);
240
- return removed;
241
- }
242
- }
243
- return undefined;
244
- }
245
- function buildToolPlaceholder(callId, call, toolResults) {
246
- const content = toolResults.get(callId) ?? '';
247
- const inferredName = toolResults.get(`${callId}::name`) ||
248
- normalizeToolName(call.name) ||
249
- normalizeToolName(call?.function?.name);
250
- const toolMessage = {
251
- role: 'tool',
252
- tool_call_id: callId,
253
- content
254
- };
255
- if (inferredName) {
256
- toolMessage.name = inferredName;
257
- }
258
- return toolMessage;
259
- }
260
- function normalizeToolName(value) {
261
- if (typeof value === 'string' && value.trim().length) {
262
- return value.trim();
263
- }
264
- return undefined;
265
- }
75
+ };
266
76
  const ensureToolResponsePlaceholders = (ctx) => {
267
77
  const messages = ensureMessagesArray(ctx.state);
268
78
  if (!messages.length)
269
79
  return;
270
- const toolResults = mapToolResults(ctx.state);
271
- let index = 0;
272
- while (index < messages.length) {
273
- const message = messages[index];
274
- if (!message || typeof message !== 'object') {
275
- index++;
276
- continue;
277
- }
278
- const role = String(message.role || '').toLowerCase();
279
- if (role !== 'assistant') {
280
- index++;
281
- continue;
282
- }
283
- const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : undefined;
284
- if (!toolCalls?.length) {
285
- index++;
286
- continue;
287
- }
288
- let insertionIndex = index + 1;
289
- for (const call of toolCalls) {
290
- if (!call || typeof call !== 'object')
291
- continue;
292
- const callId = call.id ||
293
- call.call_id ||
294
- call.tool_call_id;
295
- if (typeof callId !== 'string' || !callId.trim())
296
- continue;
297
- const normalizedId = callId.trim();
298
- const windowEnd = findFirstNonToolIndex(messages, insertionIndex);
299
- const existingIndex = findToolResponseIndex(messages, insertionIndex, windowEnd, normalizedId);
300
- if (existingIndex >= 0) {
301
- insertionIndex = existingIndex + 1;
302
- continue;
303
- }
304
- const relocated = extractToolResponse(messages, windowEnd, normalizedId);
305
- if (relocated) {
306
- messages.splice(insertionIndex, 0, relocated);
307
- insertionIndex++;
308
- continue;
309
- }
310
- const placeholder = buildToolPlaceholder(normalizedId, call, toolResults);
311
- messages.splice(insertionIndex, 0, placeholder);
312
- insertionIndex++;
313
- }
314
- index = Math.max(index + 1, insertionIndex);
315
- }
80
+ const output = applyBridgeEnsureToolPlaceholdersWithNative({
81
+ stage: ctx.stage,
82
+ messages,
83
+ capturedToolResults: Array.isArray(ctx.state.capturedToolResults)
84
+ ? ctx.state.capturedToolResults
85
+ : undefined,
86
+ rawRequest: ctx.state.rawRequest && typeof ctx.state.rawRequest === 'object'
87
+ ? ctx.state.rawRequest
88
+ : undefined,
89
+ rawResponse: ctx.state.rawResponse && typeof ctx.state.rawResponse === 'object'
90
+ ? ctx.state.rawResponse
91
+ : undefined
92
+ });
93
+ if (Array.isArray(output.messages)) {
94
+ applyMessagesInPlace(messages, output.messages);
95
+ ctx.state.messages = messages;
96
+ }
97
+ ctx.state.capturedToolResults = applyCapturedToolResultsInPlace(ctx.state.capturedToolResults, output.toolOutputs);
316
98
  };
317
- function assignReasoning(message, parts) {
318
- const trimmed = parts.map((item) => item.trim()).filter(Boolean);
319
- if (!trimmed.length)
320
- return;
321
- const existing = typeof message.reasoning_content === 'string' && message.reasoning_content.trim().length
322
- ? [message.reasoning_content.trim()]
323
- : [];
324
- const merged = [...existing, ...trimmed].filter(Boolean);
325
- if (merged.length) {
326
- message.reasoning_content = merged.join('\n').trim();
327
- }
328
- }
329
- function processTextValue(value, collector, dropFromContent) {
330
- try {
331
- const sanitized = extractReasoningSegments(value, collector);
332
- return dropFromContent ? sanitized : value;
333
- }
334
- catch {
335
- return value;
336
- }
337
- }
338
- function stripReasoningFromValue(value, collector, dropFromContent) {
339
- if (typeof value === 'string') {
340
- return processTextValue(value, collector, dropFromContent);
341
- }
342
- if (Array.isArray(value)) {
343
- for (let i = 0; i < value.length; i++) {
344
- value[i] = stripReasoningFromValue(value[i], collector, dropFromContent);
345
- }
346
- return value;
347
- }
348
- if (value && typeof value === 'object') {
349
- const record = value;
350
- if (typeof record.text === 'string') {
351
- record.text = processTextValue(record.text, collector, dropFromContent);
352
- }
353
- if (typeof record.content === 'string') {
354
- record.content = processTextValue(record.content, collector, dropFromContent);
355
- }
356
- else if (Array.isArray(record.content)) {
357
- record.content = record.content.map((part) => stripReasoningFromValue(part, collector, dropFromContent));
358
- }
359
- return value;
360
- }
361
- return value;
362
- }
363
99
  const extractReasoningAction = (ctx) => {
364
100
  const messages = ensureMessagesArray(ctx.state);
365
101
  if (!messages.length)
@@ -368,26 +104,14 @@ const extractReasoningAction = (ctx) => {
368
104
  const idPrefixBase = typeof ctx.descriptor.options?.idPrefix === 'string' && ctx.descriptor.options.idPrefix.trim().length
369
105
  ? ctx.descriptor.options.idPrefix.trim()
370
106
  : `${ctx.protocol ?? 'bridge'}_${ctx.stage}`;
371
- for (let i = 0; i < messages.length; i++) {
372
- const message = messages[i];
373
- if (!message || typeof message !== 'object')
374
- continue;
375
- const reasoningParts = [];
376
- if (typeof message.content === 'string') {
377
- message.content = processTextValue(message.content, reasoningParts, dropFromContent);
378
- }
379
- else if (Array.isArray(message.content)) {
380
- message.content = message.content.map((part) => stripReasoningFromValue(part, reasoningParts, dropFromContent));
381
- }
382
- assignReasoning(message, reasoningParts);
383
- try {
384
- normalizeMessageReasoningTools(message, {
385
- idPrefix: `${idPrefixBase}_${i + 1}`
386
- });
387
- }
388
- catch {
389
- // best-effort normalization; ignore failures
390
- }
107
+ const normalized = applyBridgeReasoningExtractWithNative({
108
+ messages,
109
+ dropFromContent,
110
+ idPrefixBase
111
+ });
112
+ if (Array.isArray(normalized.messages)) {
113
+ applyMessagesInPlace(messages, normalized.messages);
114
+ ctx.state.messages = messages;
391
115
  }
392
116
  };
393
117
  registerBridgeAction('messages.inject-system-instruction', injectSystemInstructionAction);
@@ -395,50 +119,20 @@ registerBridgeAction('reasoning.extract', extractReasoningAction);
395
119
  registerBridgeAction('tools.ensure-response-placeholders', ensureToolResponsePlaceholders);
396
120
  const ensureSystemInstructionAction = (ctx) => {
397
121
  const messages = ensureMessagesArray(ctx.state);
398
- if (!messages.length) {
399
- return;
400
- }
401
- if (ctx.stage === 'request_outbound') {
402
- const originals = [];
403
- for (const message of messages) {
404
- if (!message || typeof message !== 'object')
405
- continue;
406
- const role = String(message.role || '').toLowerCase();
407
- if (role !== 'system')
408
- continue;
409
- const text = flattenContentToString(message.content);
410
- if (typeof text === 'string' && text.trim().length) {
411
- originals.push(text.trim());
412
- }
413
- }
414
- if (!originals.length) {
415
- return;
416
- }
417
- const metadata = ensureMetadataRecord(ctx.state);
418
- metadata.systemInstruction = originals.join('\n\n');
419
- metadata.originalSystemMessages = originals.slice();
420
- return;
421
- }
422
- if (ctx.stage === 'request_inbound') {
423
- const metadata = ctx.state.metadata;
424
- const systemInstruction = typeof metadata?.systemInstruction === 'string' && metadata.systemInstruction.trim().length
425
- ? metadata.systemInstruction.trim()
426
- : undefined;
427
- if (!systemInstruction) {
428
- return;
429
- }
430
- const hasSystem = messages.some((message) => {
431
- if (!message || typeof message !== 'object')
432
- return false;
433
- return String(message.role || '').toLowerCase() === 'system';
434
- });
435
- if (hasSystem) {
436
- return;
437
- }
438
- messages.unshift({
439
- role: 'system',
440
- content: systemInstruction
441
- });
122
+ const normalized = applyBridgeEnsureSystemInstructionWithNative({
123
+ stage: ctx.stage,
124
+ messages,
125
+ metadata: ctx.state.metadata && typeof ctx.state.metadata === 'object'
126
+ ? ctx.state.metadata
127
+ : undefined
128
+ });
129
+ if (Array.isArray(normalized.messages)) {
130
+ applyMessagesInPlace(messages, normalized.messages);
131
+ ctx.state.messages = messages;
132
+ }
133
+ const metadata = applyObjectInPlace(ctx.state.metadata, normalized.metadata);
134
+ if (metadata) {
135
+ ctx.state.metadata = metadata;
442
136
  }
443
137
  };
444
138
  const normalizeHistoryAction = (ctx) => {
@@ -454,16 +148,17 @@ const normalizeHistoryAction = (ctx) => {
454
148
  Array.isArray(ctx.state.rawRequest.tools)
455
149
  ? ctx.state.rawRequest.tools
456
150
  : undefined;
457
- try {
458
- const result = convertMessagesToBridgeInput({
459
- messages,
460
- tools: toolsNode
461
- });
151
+ const normalized = applyBridgeNormalizeHistoryWithNative({
152
+ messages,
153
+ tools: toolsNode
154
+ });
155
+ if (Array.isArray(normalized.messages)) {
156
+ applyMessagesInPlace(messages, normalized.messages);
157
+ ctx.state.messages = messages;
158
+ }
159
+ if (normalized.bridgeHistory && typeof normalized.bridgeHistory === 'object' && !Array.isArray(normalized.bridgeHistory)) {
462
160
  const metadata = ensureMetadataRecord(ctx.state);
463
- metadata.bridgeHistory = result;
464
- }
465
- catch {
466
- // normalization best-effort
161
+ metadata.bridgeHistory = normalized.bridgeHistory;
467
162
  }
468
163
  };
469
164
  const ensureOutputFieldsAction = (ctx) => {
@@ -477,74 +172,42 @@ const ensureOutputFieldsAction = (ctx) => {
477
172
  const fallback = typeof ctx.descriptor.options?.toolFallback === 'string' && ctx.descriptor.options.toolFallback.trim().length
478
173
  ? ctx.descriptor.options.toolFallback.trim()
479
174
  : 'Tool call completed (no output).';
480
- for (const message of messages) {
481
- if (!message || typeof message !== 'object') {
482
- continue;
483
- }
484
- const role = String(message.role || '').toLowerCase();
485
- if (role === 'tool') {
486
- let text = flattenContentToString(message.content);
487
- if (text === undefined) {
488
- text = fallback;
489
- }
490
- message.content = text;
491
- continue;
492
- }
493
- if (role === 'assistant') {
494
- const toolCalls = Array.isArray(message.tool_calls)
495
- ? message.tool_calls
496
- : undefined;
497
- const hasToolCalls = Boolean(toolCalls && toolCalls.length);
498
- const text = flattenContentToString(message.content);
499
- const hasText = typeof text === 'string' && text.trim().length > 0;
500
- if (text !== undefined && typeof message.content !== 'string') {
501
- message.content = text;
502
- continue;
503
- }
504
- if (hasText) {
505
- continue;
506
- }
507
- const reasoningText = typeof message.reasoning_content === 'string' && message.reasoning_content.trim().length
508
- ? message.reasoning_content.trim()
509
- : undefined;
510
- if (reasoningText) {
511
- message.content = reasoningText;
512
- continue;
513
- }
514
- if (hasToolCalls) {
515
- // Assistant-only tool call is valid; leave content empty to avoid fabricated summaries.
516
- continue;
517
- }
518
- const assistantFallback = typeof ctx.descriptor.options?.assistantFallback === 'string' && ctx.descriptor.options.assistantFallback.trim().length
519
- ? ctx.descriptor.options.assistantFallback.trim()
520
- : 'Assistant response unavailable.';
521
- message.content = assistantFallback;
522
- }
175
+ const assistantFallback = typeof ctx.descriptor.options?.assistantFallback === 'string' && ctx.descriptor.options.assistantFallback.trim().length
176
+ ? ctx.descriptor.options.assistantFallback.trim()
177
+ : 'Assistant response unavailable.';
178
+ const normalized = ensureBridgeOutputFieldsWithNative({
179
+ messages,
180
+ toolFallback: fallback,
181
+ assistantFallback
182
+ });
183
+ if (Array.isArray(normalized.messages)) {
184
+ applyMessagesInPlace(messages, normalized.messages);
185
+ ctx.state.messages = messages;
523
186
  }
524
187
  };
525
188
  const ensureToolPlaceholdersAction = (ctx) => {
526
189
  ensureToolResponsePlaceholders(ctx);
527
190
  };
528
191
  const captureToolResultsAction = (ctx) => {
529
- const source = ctx.stage.startsWith('request') ? ctx.state.rawRequest :
530
- ctx.stage.startsWith('response') ? ctx.state.rawResponse :
531
- undefined;
532
- if ((!Array.isArray(ctx.state.capturedToolResults) || ctx.state.capturedToolResults.length === 0) &&
533
- source &&
534
- typeof source === 'object' &&
535
- Array.isArray(source.tool_outputs)) {
536
- ctx.state.capturedToolResults = source.tool_outputs.map((entry) => ({
537
- tool_call_id: typeof entry?.tool_call_id === 'string' ? entry.tool_call_id : undefined,
538
- call_id: typeof entry?.call_id === 'string' ? entry.call_id : undefined,
539
- output: entry?.output,
540
- name: typeof entry?.name === 'string' ? entry.name : undefined
541
- }));
542
- }
543
- if (ctx.stage === 'request_outbound' &&
544
- Array.isArray(ctx.state.capturedToolResults) &&
545
- ctx.state.capturedToolResults.length) {
546
- const metadata = ensureMetadataRecord(ctx.state);
547
- metadata.capturedToolResults = ctx.state.capturedToolResults.map((entry) => ({ ...entry }));
192
+ const output = applyBridgeCaptureToolResultsWithNative({
193
+ stage: ctx.stage,
194
+ capturedToolResults: Array.isArray(ctx.state.capturedToolResults)
195
+ ? ctx.state.capturedToolResults
196
+ : undefined,
197
+ rawRequest: ctx.state.rawRequest && typeof ctx.state.rawRequest === 'object'
198
+ ? ctx.state.rawRequest
199
+ : undefined,
200
+ rawResponse: ctx.state.rawResponse && typeof ctx.state.rawResponse === 'object'
201
+ ? ctx.state.rawResponse
202
+ : undefined,
203
+ metadata: ctx.state.metadata && typeof ctx.state.metadata === 'object'
204
+ ? ctx.state.metadata
205
+ : undefined
206
+ });
207
+ ctx.state.capturedToolResults = applyCapturedToolResultsInPlace(ctx.state.capturedToolResults, output.capturedToolResults);
208
+ const metadata = applyObjectInPlace(ctx.state.metadata, output.metadata);
209
+ if (metadata) {
210
+ ctx.state.metadata = metadata;
548
211
  }
549
212
  };
550
213
  const attachReasoningOutputAction = (ctx) => {
@@ -569,313 +232,135 @@ function deriveToolIdPrefix(ctx) {
569
232
  const base = ctx.protocol || 'bridge';
570
233
  return `${base}_tool`;
571
234
  }
572
- const normalizeToolIdentifiersAction = (ctx) => {
573
- const messages = ensureMessagesArray(ctx.state);
574
- const idPrefix = deriveToolIdPrefix(ctx);
575
- let counter = 0;
576
- const aliasMap = new Map();
577
- const knownIds = new Set();
578
- const pendingQueue = [];
579
- const registerAlias = (raw, normalized) => {
580
- if (typeof raw === 'string' && raw.trim().length) {
581
- aliasMap.set(raw.trim(), normalized);
582
- }
583
- aliasMap.set(normalized, normalized);
584
- };
585
- const consumePending = (id) => {
586
- const index = pendingQueue.indexOf(id);
587
- if (index >= 0) {
588
- pendingQueue.splice(index, 1);
589
- }
590
- };
591
- const nextId = () => {
592
- counter += 1;
593
- return `${idPrefix}_${counter}`;
594
- };
595
- const normalizeIdValue = (raw, consumeQueue) => {
596
- if (typeof raw === 'string' && raw.trim().length) {
597
- const existing = raw.trim();
598
- if (aliasMap.has(existing)) {
599
- const normalized = aliasMap.get(existing);
600
- knownIds.add(normalized);
601
- return normalized;
602
- }
603
- if (consumeQueue && pendingQueue.length) {
604
- const queued = pendingQueue.shift();
605
- knownIds.add(queued);
606
- aliasMap.set(existing, queued);
607
- aliasMap.set(queued, queued);
608
- return queued;
609
- }
610
- knownIds.add(existing);
611
- aliasMap.set(existing, existing);
612
- return existing;
613
- }
614
- if (consumeQueue && pendingQueue.length) {
615
- const queued = pendingQueue.shift();
616
- knownIds.add(queued);
617
- aliasMap.set(queued, queued);
618
- return queued;
619
- }
620
- const generated = nextId();
621
- knownIds.add(generated);
622
- aliasMap.set(generated, generated);
623
- return generated;
624
- };
625
- const normalizeToolCall = (call, consumeQueue) => {
626
- if (!call || typeof call !== 'object') {
627
- return undefined;
628
- }
629
- const normalized = normalizeIdValue(call.id || call.tool_call_id || call.call_id, consumeQueue);
630
- registerAlias(call.id, normalized);
631
- registerAlias(call.tool_call_id, normalized);
632
- registerAlias(call.call_id, normalized);
633
- call.id = normalized;
634
- call.tool_call_id = normalized;
635
- call.call_id = normalized;
636
- return normalized;
637
- };
638
- const normalizeToolOutputs = (outputs) => {
639
- if (!Array.isArray(outputs)) {
640
- return;
641
- }
642
- outputs.forEach((entry) => {
643
- if (!entry || typeof entry !== 'object') {
644
- return;
645
- }
646
- const normalized = normalizeIdValue(entry.tool_call_id || entry.call_id || entry.id, true);
647
- registerAlias(entry.tool_call_id, normalized);
648
- registerAlias(entry.call_id, normalized);
649
- consumePending(normalized);
650
- entry.tool_call_id = normalized;
651
- entry.call_id = normalized;
652
- });
653
- };
654
- for (const message of messages) {
655
- if (!message || typeof message !== 'object')
235
+ function overwriteRecordInPlace(target, source) {
236
+ for (const key of Object.keys(target)) {
237
+ delete target[key];
238
+ }
239
+ for (const [key, value] of Object.entries(source)) {
240
+ target[key] = value;
241
+ }
242
+ }
243
+ function applyMessagesInPlace(current, next) {
244
+ const normalizedNext = next.filter((entry) => Boolean(entry) && typeof entry === 'object' && !Array.isArray(entry));
245
+ const nextLength = normalizedNext.length;
246
+ for (let i = 0; i < nextLength; i++) {
247
+ const nextEntry = normalizedNext[i];
248
+ const currentEntry = current[i];
249
+ if (currentEntry && typeof currentEntry === 'object' && !Array.isArray(currentEntry)) {
250
+ overwriteRecordInPlace(currentEntry, nextEntry);
656
251
  continue;
657
- const role = String(message.role || '').toLowerCase();
658
- if (role === 'assistant') {
659
- const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : undefined;
660
- if (!toolCalls?.length)
661
- continue;
662
- for (const call of toolCalls) {
663
- const normalized = normalizeToolCall(call, false);
664
- if (normalized) {
665
- pendingQueue.push(normalized);
666
- }
667
- }
668
- }
669
- else if (role === 'tool') {
670
- const normalized = normalizeIdValue(message.tool_call_id || message.call_id || message.id, true);
671
- registerAlias(message.tool_call_id, normalized);
672
- registerAlias(message.call_id, normalized);
673
- consumePending(normalized);
674
- message.tool_call_id = normalized;
675
- message.call_id = normalized;
676
- if (!message.id) {
677
- message.id = normalized;
678
- }
679
252
  }
253
+ current[i] = { ...nextEntry };
680
254
  }
681
- if (ctx.state.rawRequest && typeof ctx.state.rawRequest === 'object') {
682
- normalizeToolOutputs(ctx.state.rawRequest.tool_outputs);
683
- const required = ctx.state.rawRequest.required_action;
684
- if (required && typeof required === 'object') {
685
- const submit = required.submit_tool_outputs;
686
- if (submit && typeof submit === 'object' && Array.isArray(submit.tool_calls)) {
687
- const submitCalls = submit.tool_calls;
688
- submitCalls.forEach((call) => normalizeToolCall(call, false));
689
- }
690
- }
255
+ if (current.length > nextLength) {
256
+ current.splice(nextLength);
691
257
  }
692
- if (ctx.state.capturedToolResults && Array.isArray(ctx.state.capturedToolResults)) {
693
- ctx.state.capturedToolResults.forEach((entry) => {
694
- const normalized = normalizeIdValue(entry?.tool_call_id ?? entry?.call_id, true);
695
- registerAlias(entry?.tool_call_id, normalized);
696
- registerAlias(entry?.call_id, normalized);
697
- consumePending(normalized);
698
- entry.tool_call_id = normalized;
699
- entry.call_id = normalized;
700
- });
258
+ }
259
+ function applyObjectInPlace(current, next) {
260
+ if (!next || typeof next !== 'object' || Array.isArray(next)) {
261
+ return undefined;
262
+ }
263
+ const nextRecord = next;
264
+ if (current && typeof current === 'object' && !Array.isArray(current)) {
265
+ overwriteRecordInPlace(current, nextRecord);
266
+ return current;
701
267
  }
268
+ return { ...nextRecord };
269
+ }
270
+ function applyCapturedToolResultsInPlace(current, next) {
271
+ if (!Array.isArray(next)) {
272
+ return current;
273
+ }
274
+ const normalizedNext = next
275
+ .filter((entry) => Boolean(entry) && typeof entry === 'object' && !Array.isArray(entry))
276
+ .map((entry) => ({ ...entry }));
277
+ if (!Array.isArray(current)) {
278
+ return normalizedNext;
279
+ }
280
+ current.splice(0, current.length, ...normalizedNext);
281
+ return current;
282
+ }
283
+ const normalizeToolIdentifiersAction = (ctx) => {
284
+ const messages = ensureMessagesArray(ctx.state);
285
+ const idPrefix = deriveToolIdPrefix(ctx);
286
+ const normalized = applyBridgeNormalizeToolIdentifiersWithNative({
287
+ stage: ctx.stage,
288
+ protocol: ctx.protocol,
289
+ moduleType: ctx.moduleType,
290
+ messages,
291
+ rawRequest: ctx.state.rawRequest,
292
+ capturedToolResults: Array.isArray(ctx.state.capturedToolResults)
293
+ ? ctx.state.capturedToolResults
294
+ : undefined,
295
+ idPrefix
296
+ });
297
+ if (Array.isArray(normalized.messages)) {
298
+ applyMessagesInPlace(messages, normalized.messages);
299
+ ctx.state.messages = messages;
300
+ }
301
+ const rawRequest = applyObjectInPlace(ctx.state.rawRequest, normalized.rawRequest);
302
+ if (rawRequest) {
303
+ ctx.state.rawRequest = rawRequest;
304
+ }
305
+ ctx.state.capturedToolResults = applyCapturedToolResultsInPlace(ctx.state.capturedToolResults, normalized.capturedToolResults);
702
306
  };
703
307
  registerBridgeAction('tools.normalize-call-ids', normalizeToolIdentifiersAction);
704
308
  const responsesOutputReasoningAction = (ctx) => {
705
309
  if (ctx.stage !== 'response_inbound')
706
310
  return;
707
- const raw = ctx.state.rawResponse;
708
- if (!raw || typeof raw !== 'object')
709
- return;
710
- const { textParts, reasoningParts } = extractOutputSegments(raw);
711
- if (!textParts.length && !reasoningParts.length)
712
- return;
713
311
  const messages = ensureMessagesArray(ctx.state);
714
- if (!messages.length) {
715
- messages.push({
716
- role: 'assistant',
717
- content: textParts.join('\n')
718
- });
719
- }
720
- const target = messages.find((message) => {
721
- if (!message || typeof message !== 'object')
722
- return false;
723
- const role = String(message.role || '').toLowerCase();
724
- return role === 'assistant';
725
- }) ?? messages[0];
726
- if (!target || typeof target !== 'object')
727
- return;
728
- if (textParts.length) {
729
- const combinedText = textParts.join('\n');
730
- const existing = target.content;
731
- if (typeof existing !== 'string' || !existing.trim().length) {
732
- target.content = combinedText;
733
- }
734
- }
735
- if (reasoningParts.length) {
736
- assignReasoning(target, reasoningParts);
737
- }
738
- try {
739
- normalizeMessageReasoningTools(target, {
740
- idPrefix: `${ctx.protocol ?? 'responses'}_${ctx.stage}_output`
741
- });
742
- }
743
- catch {
744
- // best-effort normalization
312
+ const normalized = applyBridgeResponsesOutputReasoningWithNative({
313
+ messages,
314
+ rawResponse: ctx.state.rawResponse && typeof ctx.state.rawResponse === 'object'
315
+ ? ctx.state.rawResponse
316
+ : undefined,
317
+ idPrefix: `${ctx.protocol ?? 'responses'}_${ctx.stage}_output`
318
+ });
319
+ if (Array.isArray(normalized.messages)) {
320
+ applyMessagesInPlace(messages, normalized.messages);
321
+ ctx.state.messages = messages;
745
322
  }
746
323
  };
747
324
  registerBridgeAction('responses.output-reasoning', responsesOutputReasoningAction);
748
- function collectExtraFieldsMap(source, allowed) {
749
- const extras = {};
750
- for (const [key, value] of Object.entries(source)) {
751
- if (allowed && allowed.has(key))
752
- continue;
753
- extras[key] = value;
325
+ function applyMetadataActionWithNative(ctx, actionName) {
326
+ const output = applyBridgeMetadataActionWithNative({
327
+ actionName,
328
+ stage: ctx.stage,
329
+ options: ctx.descriptor.options && typeof ctx.descriptor.options === 'object'
330
+ ? ctx.descriptor.options
331
+ : undefined,
332
+ rawRequest: ctx.state.rawRequest && typeof ctx.state.rawRequest === 'object'
333
+ ? ctx.state.rawRequest
334
+ : undefined,
335
+ rawResponse: ctx.state.rawResponse && typeof ctx.state.rawResponse === 'object'
336
+ ? ctx.state.rawResponse
337
+ : undefined,
338
+ metadata: ctx.state.metadata && typeof ctx.state.metadata === 'object'
339
+ ? ctx.state.metadata
340
+ : undefined
341
+ });
342
+ const rawRequest = applyObjectInPlace(ctx.state.rawRequest, output.rawRequest);
343
+ if (rawRequest) {
344
+ ctx.state.rawRequest = rawRequest;
345
+ }
346
+ const rawResponse = applyObjectInPlace(ctx.state.rawResponse, output.rawResponse);
347
+ if (rawResponse) {
348
+ ctx.state.rawResponse = rawResponse;
349
+ }
350
+ const metadata = applyObjectInPlace(ctx.state.metadata, output.metadata);
351
+ if (metadata) {
352
+ ctx.state.metadata = metadata;
754
353
  }
755
- if (Object.keys(extras).length === 0) {
756
- return undefined;
757
- }
758
- return extras;
759
354
  }
760
355
  const metadataExtraFieldsAction = (ctx) => {
761
- const allowedKeysRaw = ctx.descriptor.options?.allowedKeys;
762
- const allowed = Array.isArray(allowedKeysRaw)
763
- ? new Set(allowedKeysRaw
764
- .map((key) => (typeof key === 'string' ? key : undefined))
765
- .filter((key) => !!key))
766
- : undefined;
767
- const target = ctx.stage.startsWith('request') ? ctx.state.rawRequest : ctx.state.rawResponse;
768
- if (!target || typeof target !== 'object')
769
- return;
770
- if (ctx.stage.endsWith('inbound')) {
771
- const extras = collectExtraFieldsMap(target, allowed);
772
- if (!extras)
773
- return;
774
- const metadata = ensureMetadataRecord(ctx.state);
775
- const existing = metadata.extraFields && typeof metadata.extraFields === 'object'
776
- ? metadata.extraFields
777
- : undefined;
778
- metadata.extraFields = existing ? { ...existing, ...extras } : extras;
779
- return;
780
- }
781
- const metadata = ctx.state.metadata;
782
- if (!metadata || typeof metadata !== 'object')
783
- return;
784
- const extras = metadata.extraFields;
785
- if (!extras || typeof extras !== 'object')
786
- return;
787
- const source = extras;
788
- for (const [key, value] of Object.entries(source)) {
789
- if (target[key] === undefined) {
790
- target[key] = value;
791
- }
792
- }
356
+ applyMetadataActionWithNative(ctx, 'metadata.extra-fields');
793
357
  };
794
358
  const metadataProviderFieldAction = (ctx) => {
795
- const field = typeof ctx.descriptor.options?.field === 'string' ? ctx.descriptor.options.field : 'metadata';
796
- const targetKey = typeof ctx.descriptor.options?.target === 'string' ? ctx.descriptor.options.target : 'providerMetadata';
797
- const payload = ctx.stage.startsWith('request') ? ctx.state.rawRequest :
798
- ctx.stage.startsWith('response') ? ctx.state.rawResponse :
799
- undefined;
800
- if (!payload || typeof payload !== 'object')
801
- return;
802
- if (ctx.stage.endsWith('inbound')) {
803
- const value = payload[field];
804
- if (!value || typeof value !== 'object')
805
- return;
806
- const metadata = ensureMetadataRecord(ctx.state);
807
- metadata[targetKey] = value;
808
- return;
809
- }
810
- const metadata = ctx.state.metadata;
811
- if (!metadata || typeof metadata !== 'object')
812
- return;
813
- const provider = metadata[targetKey];
814
- if (!provider || typeof provider !== 'object')
815
- return;
816
- if (payload[field] === undefined) {
817
- payload[field] = provider;
818
- }
359
+ applyMetadataActionWithNative(ctx, 'metadata.provider-field');
819
360
  };
820
361
  const metadataProviderSentinelAction = (ctx) => {
821
- const sentinel = typeof ctx.descriptor.options?.sentinel === 'string'
822
- ? ctx.descriptor.options.sentinel
823
- : undefined;
824
- if (!sentinel)
825
- return;
826
- const targetKey = typeof ctx.descriptor.options?.target === 'string'
827
- ? ctx.descriptor.options.target
828
- : 'providerMetadata';
829
- const payload = ctx.stage.startsWith('request') ? ctx.state.rawRequest :
830
- ctx.stage.startsWith('response') ? ctx.state.rawResponse :
831
- undefined;
832
- if (!payload || typeof payload !== 'object')
833
- return;
834
- if (ctx.stage.endsWith('inbound')) {
835
- const rawValue = payload[sentinel];
836
- if (rawValue === undefined)
837
- return;
838
- let provider;
839
- if (typeof rawValue === 'string') {
840
- try {
841
- const parsed = JSON.parse(rawValue);
842
- if (parsed && typeof parsed === 'object')
843
- provider = parsed;
844
- }
845
- catch {
846
- provider = undefined;
847
- }
848
- }
849
- else if (rawValue && typeof rawValue === 'object') {
850
- provider = rawValue;
851
- }
852
- delete payload[sentinel];
853
- if (!provider)
854
- return;
855
- const metadata = ensureMetadataRecord(ctx.state);
856
- metadata[targetKey] = provider;
857
- return;
858
- }
859
- if (ctx.stage === 'request_outbound') {
860
- delete payload[sentinel];
861
- return;
862
- }
863
- const metadata = ctx.state.metadata;
864
- if (!metadata || typeof metadata !== 'object') {
865
- delete payload[sentinel];
866
- return;
867
- }
868
- const provider = metadata[targetKey];
869
- if (!provider || typeof provider !== 'object') {
870
- delete payload[sentinel];
871
- return;
872
- }
873
- try {
874
- payload[sentinel] = JSON.stringify(provider);
875
- }
876
- catch {
877
- payload[sentinel] = provider;
878
- }
362
+ applyMetadataActionWithNative(ctx, 'metadata.provider-sentinel');
879
363
  };
364
+ registerBridgeAction('metadata.extra-fields', metadataExtraFieldsAction);
880
365
  registerBridgeAction('metadata.provider-field', metadataProviderFieldAction);
881
366
  registerBridgeAction('metadata.provider-sentinel', metadataProviderSentinelAction);