@jsonstudio/llms 0.4.2

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 (538) hide show
  1. package/README.md +265 -0
  2. package/dist/api/index.d.ts +10 -0
  3. package/dist/api/index.js +11 -0
  4. package/dist/api/llmswitch-config.d.ts +15 -0
  5. package/dist/api/llmswitch-config.js +415 -0
  6. package/dist/api/llmswitch-types.d.ts +412 -0
  7. package/dist/api/llmswitch-types.js +60 -0
  8. package/dist/bridge/routecodex-adapter.d.ts +74 -0
  9. package/dist/bridge/routecodex-adapter.js +226 -0
  10. package/dist/config/index.d.ts +3 -0
  11. package/dist/config/index.js +5 -0
  12. package/dist/config-unified/enhanced-path-resolver.d.ts +5 -0
  13. package/dist/config-unified/enhanced-path-resolver.js +38 -0
  14. package/dist/config-unified/unified-config.d.ts +26 -0
  15. package/dist/config-unified/unified-config.js +187 -0
  16. package/dist/conversion/codec-registry.d.ts +10 -0
  17. package/dist/conversion/codec-registry.js +25 -0
  18. package/dist/conversion/codecs/anthropic-openai-codec.d.ts +23 -0
  19. package/dist/conversion/codecs/anthropic-openai-codec.js +546 -0
  20. package/dist/conversion/codecs/gemini-openai-codec.d.ts +16 -0
  21. package/dist/conversion/codecs/gemini-openai-codec.js +437 -0
  22. package/dist/conversion/codecs/openai-openai-codec.d.ts +12 -0
  23. package/dist/conversion/codecs/openai-openai-codec.js +125 -0
  24. package/dist/conversion/codecs/responses-openai-codec.d.ts +12 -0
  25. package/dist/conversion/codecs/responses-openai-codec.js +154 -0
  26. package/dist/conversion/config/config-manager.d.ts +212 -0
  27. package/dist/conversion/config/config-manager.js +310 -0
  28. package/dist/conversion/conversion-v3/adapters/index.d.ts +2 -0
  29. package/dist/conversion/conversion-v3/adapters/index.js +1 -0
  30. package/dist/conversion/conversion-v3/adapters/v3-to-standard-adapter.d.ts +38 -0
  31. package/dist/conversion/conversion-v3/adapters/v3-to-standard-adapter.js +55 -0
  32. package/dist/conversion/conversion-v3/compatibility/compatibility-profiles.d.ts +22 -0
  33. package/dist/conversion/conversion-v3/compatibility/compatibility-profiles.js +84 -0
  34. package/dist/conversion/conversion-v3/compatibility/compatibility-stages.d.ts +4 -0
  35. package/dist/conversion/conversion-v3/compatibility/compatibility-stages.js +285 -0
  36. package/dist/conversion/conversion-v3/compatibility/index.d.ts +3 -0
  37. package/dist/conversion/conversion-v3/compatibility/index.js +2 -0
  38. package/dist/conversion/conversion-v3/config/default-configs.d.ts +48 -0
  39. package/dist/conversion/conversion-v3/config/default-configs.js +1083 -0
  40. package/dist/conversion/conversion-v3/config/index.d.ts +5 -0
  41. package/dist/conversion/conversion-v3/config/index.js +5 -0
  42. package/dist/conversion/conversion-v3/config/pipeline-config-manager.d.ts +49 -0
  43. package/dist/conversion/conversion-v3/config/pipeline-config-manager.js +65 -0
  44. package/dist/conversion/conversion-v3/index.d.ts +11 -0
  45. package/dist/conversion/conversion-v3/index.js +17 -0
  46. package/dist/conversion/conversion-v3/nodes/index.d.ts +56 -0
  47. package/dist/conversion/conversion-v3/nodes/index.js +149 -0
  48. package/dist/conversion/conversion-v3/nodes/input/anthropic-input-node.d.ts +25 -0
  49. package/dist/conversion/conversion-v3/nodes/input/anthropic-input-node.js +56 -0
  50. package/dist/conversion/conversion-v3/nodes/input/base-input-node.d.ts +75 -0
  51. package/dist/conversion/conversion-v3/nodes/input/base-input-node.js +237 -0
  52. package/dist/conversion/conversion-v3/nodes/input/chat-input-node.d.ts +25 -0
  53. package/dist/conversion/conversion-v3/nodes/input/chat-input-node.js +56 -0
  54. package/dist/conversion/conversion-v3/nodes/input/chat-shape-helpers.d.ts +4 -0
  55. package/dist/conversion/conversion-v3/nodes/input/chat-shape-helpers.js +197 -0
  56. package/dist/conversion/conversion-v3/nodes/input/gemini-input-node.d.ts +16 -0
  57. package/dist/conversion/conversion-v3/nodes/input/gemini-input-node.js +125 -0
  58. package/dist/conversion/conversion-v3/nodes/input/index.d.ts +8 -0
  59. package/dist/conversion/conversion-v3/nodes/input/index.js +8 -0
  60. package/dist/conversion/conversion-v3/nodes/input/input-decision.d.ts +46 -0
  61. package/dist/conversion/conversion-v3/nodes/input/input-decision.js +125 -0
  62. package/dist/conversion/conversion-v3/nodes/input/responses-input-node.d.ts +20 -0
  63. package/dist/conversion/conversion-v3/nodes/input/responses-input-node.js +51 -0
  64. package/dist/conversion/conversion-v3/nodes/output/anthropic-output-node.d.ts +71 -0
  65. package/dist/conversion/conversion-v3/nodes/output/anthropic-output-node.js +320 -0
  66. package/dist/conversion/conversion-v3/nodes/output/base-output-node.d.ts +116 -0
  67. package/dist/conversion/conversion-v3/nodes/output/base-output-node.js +512 -0
  68. package/dist/conversion/conversion-v3/nodes/output/index.d.ts +7 -0
  69. package/dist/conversion/conversion-v3/nodes/output/index.js +7 -0
  70. package/dist/conversion/conversion-v3/nodes/output/openai-output-node.d.ts +71 -0
  71. package/dist/conversion/conversion-v3/nodes/output/openai-output-node.js +293 -0
  72. package/dist/conversion/conversion-v3/nodes/output/output-decision.d.ts +68 -0
  73. package/dist/conversion/conversion-v3/nodes/output/output-decision.js +161 -0
  74. package/dist/conversion/conversion-v3/nodes/output/responses-output-node.d.ts +64 -0
  75. package/dist/conversion/conversion-v3/nodes/output/responses-output-node.js +323 -0
  76. package/dist/conversion/conversion-v3/nodes/process/base-process-node.d.ts +72 -0
  77. package/dist/conversion/conversion-v3/nodes/process/base-process-node.js +246 -0
  78. package/dist/conversion/conversion-v3/nodes/process/chat-process-node.d.ts +135 -0
  79. package/dist/conversion/conversion-v3/nodes/process/chat-process-node.js +854 -0
  80. package/dist/conversion/conversion-v3/nodes/process/compatibility-process-node.d.ts +29 -0
  81. package/dist/conversion/conversion-v3/nodes/process/compatibility-process-node.js +181 -0
  82. package/dist/conversion/conversion-v3/nodes/process/index.d.ts +10 -0
  83. package/dist/conversion/conversion-v3/nodes/process/index.js +10 -0
  84. package/dist/conversion/conversion-v3/nodes/process/passthrough-process-node.d.ts +59 -0
  85. package/dist/conversion/conversion-v3/nodes/process/passthrough-process-node.js +243 -0
  86. package/dist/conversion/conversion-v3/nodes/process/response-process-node.d.ts +17 -0
  87. package/dist/conversion/conversion-v3/nodes/process/response-process-node.js +213 -0
  88. package/dist/conversion/conversion-v3/nodes/process/tool-process-node.d.ts +26 -0
  89. package/dist/conversion/conversion-v3/nodes/process/tool-process-node.js +299 -0
  90. package/dist/conversion/conversion-v3/nodes/process/virtual-router-process-node.d.ts +40 -0
  91. package/dist/conversion/conversion-v3/nodes/process/virtual-router-process-node.js +430 -0
  92. package/dist/conversion/conversion-v3/nodes/response/anthropic-response-input-node.d.ts +12 -0
  93. package/dist/conversion/conversion-v3/nodes/response/anthropic-response-input-node.js +129 -0
  94. package/dist/conversion/conversion-v3/nodes/response/chat-to-responses-response-node.d.ts +15 -0
  95. package/dist/conversion/conversion-v3/nodes/response/chat-to-responses-response-node.js +57 -0
  96. package/dist/conversion/conversion-v3/nodes/response/index.d.ts +4 -0
  97. package/dist/conversion/conversion-v3/nodes/response/index.js +4 -0
  98. package/dist/conversion/conversion-v3/nodes/response/openai-response-input-node.d.ts +15 -0
  99. package/dist/conversion/conversion-v3/nodes/response/openai-response-input-node.js +87 -0
  100. package/dist/conversion/conversion-v3/nodes/response/response-utils.d.ts +1 -0
  101. package/dist/conversion/conversion-v3/nodes/response/response-utils.js +1 -0
  102. package/dist/conversion/conversion-v3/nodes/response/responses-response-input-node.d.ts +12 -0
  103. package/dist/conversion/conversion-v3/nodes/response/responses-response-input-node.js +67 -0
  104. package/dist/conversion/conversion-v3/nodes/sse/index.d.ts +3 -0
  105. package/dist/conversion/conversion-v3/nodes/sse/index.js +3 -0
  106. package/dist/conversion/conversion-v3/nodes/sse/sse-input-node.d.ts +27 -0
  107. package/dist/conversion/conversion-v3/nodes/sse/sse-input-node.js +165 -0
  108. package/dist/conversion/conversion-v3/nodes/sse/sse-output-node.d.ts +19 -0
  109. package/dist/conversion/conversion-v3/nodes/sse/sse-output-node.js +267 -0
  110. package/dist/conversion/conversion-v3/nodes/sse/sse-passthrough-node.d.ts +18 -0
  111. package/dist/conversion/conversion-v3/nodes/sse/sse-passthrough-node.js +48 -0
  112. package/dist/conversion/conversion-v3/pipelines/index.d.ts +6 -0
  113. package/dist/conversion/conversion-v3/pipelines/index.js +6 -0
  114. package/dist/conversion/conversion-v3/pipelines/super-pipeline.d.ts +112 -0
  115. package/dist/conversion/conversion-v3/pipelines/super-pipeline.js +566 -0
  116. package/dist/conversion/conversion-v3/response/provider-response-converter.d.ts +28 -0
  117. package/dist/conversion/conversion-v3/response/provider-response-converter.js +249 -0
  118. package/dist/conversion/conversion-v3/sse/index.d.ts +140 -0
  119. package/dist/conversion/conversion-v3/sse/index.js +115 -0
  120. package/dist/conversion/conversion-v3/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +11 -0
  121. package/dist/conversion/conversion-v3/sse/json-to-sse/anthropic-json-to-sse-converter.js +79 -0
  122. package/dist/conversion/conversion-v3/sse/json-to-sse/chat-json-to-sse-converter.d.ts +79 -0
  123. package/dist/conversion/conversion-v3/sse/json-to-sse/chat-json-to-sse-converter.js +296 -0
  124. package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/chat.d.ts +55 -0
  125. package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/chat.js +233 -0
  126. package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/responses.d.ts +99 -0
  127. package/dist/conversion/conversion-v3/sse/json-to-sse/event-generators/responses.js +599 -0
  128. package/dist/conversion/conversion-v3/sse/json-to-sse/index.d.ts +6 -0
  129. package/dist/conversion/conversion-v3/sse/json-to-sse/index.js +7 -0
  130. package/dist/conversion/conversion-v3/sse/json-to-sse/responses-json-to-sse-converter.d.ts +79 -0
  131. package/dist/conversion/conversion-v3/sse/json-to-sse/responses-json-to-sse-converter.js +322 -0
  132. package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/chat-sequencer.d.ts +37 -0
  133. package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/chat-sequencer.js +218 -0
  134. package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
  135. package/dist/conversion/conversion-v3/sse/json-to-sse/sequencers/responses-sequencer.js +263 -0
  136. package/dist/conversion/conversion-v3/sse/registry/sse-codec-registry.d.ts +32 -0
  137. package/dist/conversion/conversion-v3/sse/registry/sse-codec-registry.js +106 -0
  138. package/dist/conversion/conversion-v3/sse/shared/chat-serializer.d.ts +4 -0
  139. package/dist/conversion/conversion-v3/sse/shared/chat-serializer.js +40 -0
  140. package/dist/conversion/conversion-v3/sse/shared/constants.d.ts +272 -0
  141. package/dist/conversion/conversion-v3/sse/shared/constants.js +321 -0
  142. package/dist/conversion/conversion-v3/sse/shared/serializers/base-serializer.d.ts +158 -0
  143. package/dist/conversion/conversion-v3/sse/shared/serializers/base-serializer.js +210 -0
  144. package/dist/conversion/conversion-v3/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
  145. package/dist/conversion/conversion-v3/sse/shared/serializers/chat-event-serializer.js +275 -0
  146. package/dist/conversion/conversion-v3/sse/shared/serializers/index.d.ts +39 -0
  147. package/dist/conversion/conversion-v3/sse/shared/serializers/index.js +54 -0
  148. package/dist/conversion/conversion-v3/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
  149. package/dist/conversion/conversion-v3/sse/shared/serializers/responses-event-serializer.js +356 -0
  150. package/dist/conversion/conversion-v3/sse/shared/serializers/types.d.ts +51 -0
  151. package/dist/conversion/conversion-v3/sse/shared/serializers/types.js +4 -0
  152. package/dist/conversion/conversion-v3/sse/shared/utils.d.ts +254 -0
  153. package/dist/conversion/conversion-v3/sse/shared/utils.js +543 -0
  154. package/dist/conversion/conversion-v3/sse/shared/writer.d.ts +111 -0
  155. package/dist/conversion/conversion-v3/sse/shared/writer.js +268 -0
  156. package/dist/conversion/conversion-v3/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +13 -0
  157. package/dist/conversion/conversion-v3/sse/sse-to-json/anthropic-sse-to-json-converter.js +139 -0
  158. package/dist/conversion/conversion-v3/sse/sse-to-json/builders/response-builder.d.ts +165 -0
  159. package/dist/conversion/conversion-v3/sse/sse-to-json/builders/response-builder.js +776 -0
  160. package/dist/conversion/conversion-v3/sse/sse-to-json/chat-sse-to-json-converter.d.ts +106 -0
  161. package/dist/conversion/conversion-v3/sse/sse-to-json/chat-sse-to-json-converter.js +540 -0
  162. package/dist/conversion/conversion-v3/sse/sse-to-json/index.d.ts +6 -0
  163. package/dist/conversion/conversion-v3/sse/sse-to-json/index.js +7 -0
  164. package/dist/conversion/conversion-v3/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
  165. package/dist/conversion/conversion-v3/sse/sse-to-json/parsers/sse-parser.js +332 -0
  166. package/dist/conversion/conversion-v3/sse/sse-to-json/responses-sse-to-json-converter.d.ts +59 -0
  167. package/dist/conversion/conversion-v3/sse/sse-to-json/responses-sse-to-json-converter.js +270 -0
  168. package/dist/conversion/conversion-v3/sse/types/anthropic-types.d.ts +87 -0
  169. package/dist/conversion/conversion-v3/sse/types/anthropic-types.js +5 -0
  170. package/dist/conversion/conversion-v3/sse/types/chat-types.d.ts +308 -0
  171. package/dist/conversion/conversion-v3/sse/types/chat-types.js +32 -0
  172. package/dist/conversion/conversion-v3/sse/types/conversion-context.d.ts +211 -0
  173. package/dist/conversion/conversion-v3/sse/types/conversion-context.js +20 -0
  174. package/dist/conversion/conversion-v3/sse/types/core-interfaces.d.ts +90 -0
  175. package/dist/conversion/conversion-v3/sse/types/core-interfaces.js +5 -0
  176. package/dist/conversion/conversion-v3/sse/types/index.d.ts +14 -0
  177. package/dist/conversion/conversion-v3/sse/types/index.js +7 -0
  178. package/dist/conversion/conversion-v3/sse/types/responses-types.d.ts +328 -0
  179. package/dist/conversion/conversion-v3/sse/types/responses-types.js +38 -0
  180. package/dist/conversion/conversion-v3/sse/types/sse-events.d.ts +180 -0
  181. package/dist/conversion/conversion-v3/sse/types/sse-events.js +150 -0
  182. package/dist/conversion/conversion-v3/sse/types/stream-state.d.ts +204 -0
  183. package/dist/conversion/conversion-v3/sse/types/stream-state.js +44 -0
  184. package/dist/conversion/conversion-v3/sse/types/utility-types.d.ts +230 -0
  185. package/dist/conversion/conversion-v3/sse/types/utility-types.js +49 -0
  186. package/dist/conversion/conversion-v3/standards/context-adapter.d.ts +146 -0
  187. package/dist/conversion/conversion-v3/standards/context-adapter.js +438 -0
  188. package/dist/conversion/conversion-v3/standards/errors.d.ts +38 -0
  189. package/dist/conversion/conversion-v3/standards/errors.js +27 -0
  190. package/dist/conversion/conversion-v3/standards/index.d.ts +15 -0
  191. package/dist/conversion/conversion-v3/standards/index.js +11 -0
  192. package/dist/conversion/conversion-v3/standards/node-capabilities.d.ts +123 -0
  193. package/dist/conversion/conversion-v3/standards/node-capabilities.js +330 -0
  194. package/dist/conversion/conversion-v3/standards/pipeline-context.d.ts +237 -0
  195. package/dist/conversion/conversion-v3/standards/pipeline-context.js +84 -0
  196. package/dist/conversion/conversion-v3/standards/pipeline-node.d.ts +196 -0
  197. package/dist/conversion/conversion-v3/standards/pipeline-node.js +316 -0
  198. package/dist/conversion/conversion-v3/standards/snapshot-handles.d.ts +24 -0
  199. package/dist/conversion/conversion-v3/standards/snapshot-handles.js +49 -0
  200. package/dist/conversion/conversion-v3/types/config-types.d.ts +242 -0
  201. package/dist/conversion/conversion-v3/types/config-types.js +13 -0
  202. package/dist/conversion/conversion-v3/types/index.d.ts +8 -0
  203. package/dist/conversion/conversion-v3/types/index.js +11 -0
  204. package/dist/conversion/conversion-v3/types/node-types.d.ts +211 -0
  205. package/dist/conversion/conversion-v3/types/node-types.js +20 -0
  206. package/dist/conversion/conversion-v3/types/pipeline-types.d.ts +246 -0
  207. package/dist/conversion/conversion-v3/types/pipeline-types.js +26 -0
  208. package/dist/conversion/conversion-v3/types/request-types.d.ts +293 -0
  209. package/dist/conversion/conversion-v3/types/request-types.js +26 -0
  210. package/dist/conversion/conversion-v3/utils/provider-payload-sanitizer.d.ts +10 -0
  211. package/dist/conversion/conversion-v3/utils/provider-payload-sanitizer.js +103 -0
  212. package/dist/conversion/conversion-v3/validations/index.d.ts +8 -0
  213. package/dist/conversion/conversion-v3/validations/index.js +7 -0
  214. package/dist/conversion/conversion-v3/validations/node-graph-validator.d.ts +116 -0
  215. package/dist/conversion/conversion-v3/validations/node-graph-validator.js +505 -0
  216. package/dist/conversion/conversion-v3/validations/tool-governance-enforcer.d.ts +139 -0
  217. package/dist/conversion/conversion-v3/validations/tool-governance-enforcer.js +338 -0
  218. package/dist/conversion/hub/config/types.d.ts +26 -0
  219. package/dist/conversion/hub/config/types.js +1 -0
  220. package/dist/conversion/hub/core/detour-registry.d.ts +9 -0
  221. package/dist/conversion/hub/core/detour-registry.js +23 -0
  222. package/dist/conversion/hub/core/hub-context.d.ts +21 -0
  223. package/dist/conversion/hub/core/hub-context.js +1 -0
  224. package/dist/conversion/hub/core/index.d.ts +3 -0
  225. package/dist/conversion/hub/core/index.js +3 -0
  226. package/dist/conversion/hub/core/stage-driver.d.ts +30 -0
  227. package/dist/conversion/hub/core/stage-driver.js +31 -0
  228. package/dist/conversion/hub/format-adapters/anthropic-format-adapter.d.ts +16 -0
  229. package/dist/conversion/hub/format-adapters/anthropic-format-adapter.js +23 -0
  230. package/dist/conversion/hub/format-adapters/chat-format-adapter.d.ts +17 -0
  231. package/dist/conversion/hub/format-adapters/chat-format-adapter.js +23 -0
  232. package/dist/conversion/hub/format-adapters/gemini-format-adapter.d.ts +16 -0
  233. package/dist/conversion/hub/format-adapters/gemini-format-adapter.js +23 -0
  234. package/dist/conversion/hub/format-adapters/index.d.ts +21 -0
  235. package/dist/conversion/hub/format-adapters/index.js +4 -0
  236. package/dist/conversion/hub/format-adapters/responses-format-adapter.d.ts +17 -0
  237. package/dist/conversion/hub/format-adapters/responses-format-adapter.js +23 -0
  238. package/dist/conversion/hub/hub-feature.d.ts +1 -0
  239. package/dist/conversion/hub/hub-feature.js +25 -0
  240. package/dist/conversion/hub/node-support.d.ts +19 -0
  241. package/dist/conversion/hub/node-support.js +115 -0
  242. package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +68 -0
  243. package/dist/conversion/hub/pipeline/hub-pipeline.js +421 -0
  244. package/dist/conversion/hub/pipelines/inbound.d.ts +22 -0
  245. package/dist/conversion/hub/pipelines/inbound.js +64 -0
  246. package/dist/conversion/hub/pipelines/outbound.d.ts +22 -0
  247. package/dist/conversion/hub/pipelines/outbound.js +57 -0
  248. package/dist/conversion/hub/process/chat-process.d.ts +13 -0
  249. package/dist/conversion/hub/process/chat-process.js +50 -0
  250. package/dist/conversion/hub/registry.d.ts +28 -0
  251. package/dist/conversion/hub/registry.js +98 -0
  252. package/dist/conversion/hub/response/chat-response-utils.d.ts +6 -0
  253. package/dist/conversion/hub/response/chat-response-utils.js +89 -0
  254. package/dist/conversion/hub/response/provider-response.d.ts +20 -0
  255. package/dist/conversion/hub/response/provider-response.js +196 -0
  256. package/dist/conversion/hub/response/response-mappers.d.ts +19 -0
  257. package/dist/conversion/hub/response/response-mappers.js +23 -0
  258. package/dist/conversion/hub/response/response-runtime.d.ts +3 -0
  259. package/dist/conversion/hub/response/response-runtime.js +206 -0
  260. package/dist/conversion/hub/semantic-mappers/anthropic-mapper.d.ts +7 -0
  261. package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +308 -0
  262. package/dist/conversion/hub/semantic-mappers/chat-mapper.d.ts +7 -0
  263. package/dist/conversion/hub/semantic-mappers/chat-mapper.js +304 -0
  264. package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +7 -0
  265. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +477 -0
  266. package/dist/conversion/hub/semantic-mappers/index.d.ts +4 -0
  267. package/dist/conversion/hub/semantic-mappers/index.js +4 -0
  268. package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +21 -0
  269. package/dist/conversion/hub/semantic-mappers/responses-mapper.js +379 -0
  270. package/dist/conversion/hub/snapshot-recorder.d.ts +12 -0
  271. package/dist/conversion/hub/snapshot-recorder.js +69 -0
  272. package/dist/conversion/hub/standardized-bridge.d.ts +12 -0
  273. package/dist/conversion/hub/standardized-bridge.js +248 -0
  274. package/dist/conversion/hub/types/chat-envelope.d.ts +67 -0
  275. package/dist/conversion/hub/types/chat-envelope.js +1 -0
  276. package/dist/conversion/hub/types/chat-schema.d.ts +112 -0
  277. package/dist/conversion/hub/types/chat-schema.js +1 -0
  278. package/dist/conversion/hub/types/errors.d.ts +5 -0
  279. package/dist/conversion/hub/types/errors.js +13 -0
  280. package/dist/conversion/hub/types/format-envelope.d.ts +7 -0
  281. package/dist/conversion/hub/types/format-envelope.js +1 -0
  282. package/dist/conversion/hub/types/index.d.ts +6 -0
  283. package/dist/conversion/hub/types/index.js +6 -0
  284. package/dist/conversion/hub/types/json.d.ts +9 -0
  285. package/dist/conversion/hub/types/json.js +9 -0
  286. package/dist/conversion/hub/types/node.d.ts +31 -0
  287. package/dist/conversion/hub/types/node.js +1 -0
  288. package/dist/conversion/hub/types/standardized.d.ts +94 -0
  289. package/dist/conversion/hub/types/standardized.js +1 -0
  290. package/dist/conversion/index.d.ts +28 -0
  291. package/dist/conversion/index.js +28 -0
  292. package/dist/conversion/responses/responses-openai-bridge.d.ts +95 -0
  293. package/dist/conversion/responses/responses-openai-bridge.js +1330 -0
  294. package/dist/conversion/schema-validator.d.ts +7 -0
  295. package/dist/conversion/schema-validator.js +67 -0
  296. package/dist/conversion/shared/args-mapping.d.ts +18 -0
  297. package/dist/conversion/shared/args-mapping.js +213 -0
  298. package/dist/conversion/shared/chat-request-filters.d.ts +9 -0
  299. package/dist/conversion/shared/chat-request-filters.js +74 -0
  300. package/dist/conversion/shared/jsonish.d.ts +3 -0
  301. package/dist/conversion/shared/jsonish.js +177 -0
  302. package/dist/conversion/shared/mcp-injection.d.ts +2 -0
  303. package/dist/conversion/shared/mcp-injection.js +89 -0
  304. package/dist/conversion/shared/media.d.ts +1 -0
  305. package/dist/conversion/shared/media.js +9 -0
  306. package/dist/conversion/shared/openai-finalizer.d.ts +9 -0
  307. package/dist/conversion/shared/openai-finalizer.js +104 -0
  308. package/dist/conversion/shared/openai-message-normalize.d.ts +1 -0
  309. package/dist/conversion/shared/openai-message-normalize.js +442 -0
  310. package/dist/conversion/shared/payload-budget.d.ts +13 -0
  311. package/dist/conversion/shared/payload-budget.js +248 -0
  312. package/dist/conversion/shared/reasoning-mapping.d.ts +5 -0
  313. package/dist/conversion/shared/reasoning-mapping.js +8 -0
  314. package/dist/conversion/shared/responses-conversation-store.d.ts +41 -0
  315. package/dist/conversion/shared/responses-conversation-store.js +285 -0
  316. package/dist/conversion/shared/responses-id-utils.d.ts +7 -0
  317. package/dist/conversion/shared/responses-id-utils.js +29 -0
  318. package/dist/conversion/shared/responses-instructions.d.ts +1 -0
  319. package/dist/conversion/shared/responses-instructions.js +113 -0
  320. package/dist/conversion/shared/responses-metadata.d.ts +1 -0
  321. package/dist/conversion/shared/responses-metadata.js +1 -0
  322. package/dist/conversion/shared/responses-request-adapter.d.ts +66 -0
  323. package/dist/conversion/shared/responses-request-adapter.js +461 -0
  324. package/dist/conversion/shared/snapshot-hooks.d.ts +12 -0
  325. package/dist/conversion/shared/snapshot-hooks.js +7 -0
  326. package/dist/conversion/shared/streaming-text-extractor.d.ts +26 -0
  327. package/dist/conversion/shared/streaming-text-extractor.js +145 -0
  328. package/dist/conversion/shared/text-markup-normalizer.d.ts +9 -0
  329. package/dist/conversion/shared/text-markup-normalizer.js +319 -0
  330. package/dist/conversion/shared/tool-canonicalizer.d.ts +2 -0
  331. package/dist/conversion/shared/tool-canonicalizer.js +50 -0
  332. package/dist/conversion/shared/tool-filter-pipeline.d.ts +17 -0
  333. package/dist/conversion/shared/tool-filter-pipeline.js +79 -0
  334. package/dist/conversion/shared/tool-governor.d.ts +27 -0
  335. package/dist/conversion/shared/tool-governor.js +269 -0
  336. package/dist/conversion/shared/tool-harvester.d.ts +31 -0
  337. package/dist/conversion/shared/tool-harvester.js +321 -0
  338. package/dist/conversion/shared/tool-mapping.d.ts +10 -0
  339. package/dist/conversion/shared/tool-mapping.js +10 -0
  340. package/dist/conversion/shared/tooling.d.ts +26 -0
  341. package/dist/conversion/shared/tooling.js +143 -0
  342. package/dist/conversion/types.d.ts +33 -0
  343. package/dist/conversion/types.js +1 -0
  344. package/dist/core/index.d.ts +6 -0
  345. package/dist/core/index.js +9 -0
  346. package/dist/core/llmswitch-engine-v2.d.ts +132 -0
  347. package/dist/core/llmswitch-engine-v2.js +649 -0
  348. package/dist/core/llmswitch-factory-v2.d.ts +123 -0
  349. package/dist/core/llmswitch-factory-v2.js +521 -0
  350. package/dist/endpoints/base-endpoint-handler.d.ts +128 -0
  351. package/dist/endpoints/base-endpoint-handler.js +545 -0
  352. package/dist/endpoints/chat-endpoint-handler.d.ts +115 -0
  353. package/dist/endpoints/chat-endpoint-handler.js +218 -0
  354. package/dist/endpoints/endpoint-router.d.ts +127 -0
  355. package/dist/endpoints/endpoint-router.js +438 -0
  356. package/dist/endpoints/index.d.ts +13 -0
  357. package/dist/endpoints/index.js +11 -0
  358. package/dist/endpoints/messages-endpoint-handler.d.ts +171 -0
  359. package/dist/endpoints/messages-endpoint-handler.js +238 -0
  360. package/dist/endpoints/responses-endpoint-handler.d.ts +147 -0
  361. package/dist/endpoints/responses-endpoint-handler.js +233 -0
  362. package/dist/filters/builtin/add-fields-filter.d.ts +8 -0
  363. package/dist/filters/builtin/add-fields-filter.js +16 -0
  364. package/dist/filters/builtin/blacklist-filter.d.ts +8 -0
  365. package/dist/filters/builtin/blacklist-filter.js +19 -0
  366. package/dist/filters/builtin/whitelist-filter.d.ts +8 -0
  367. package/dist/filters/builtin/whitelist-filter.js +19 -0
  368. package/dist/filters/engine.d.ts +16 -0
  369. package/dist/filters/engine.js +167 -0
  370. package/dist/filters/index.d.ts +19 -0
  371. package/dist/filters/index.js +24 -0
  372. package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +12 -0
  373. package/dist/filters/special/request-streaming-to-nonstreaming.js +27 -0
  374. package/dist/filters/special/request-tool-choice-policy.d.ts +11 -0
  375. package/dist/filters/special/request-tool-choice-policy.js +27 -0
  376. package/dist/filters/special/request-tool-list-filter.d.ts +11 -0
  377. package/dist/filters/special/request-tool-list-filter.js +167 -0
  378. package/dist/filters/special/request-toolcalls-stringify.d.ts +10 -0
  379. package/dist/filters/special/request-toolcalls-stringify.js +44 -0
  380. package/dist/filters/special/request-tools-normalize.d.ts +12 -0
  381. package/dist/filters/special/request-tools-normalize.js +140 -0
  382. package/dist/filters/special/response-finish-invariants.d.ts +11 -0
  383. package/dist/filters/special/response-finish-invariants.js +30 -0
  384. package/dist/filters/special/response-openai-to-responses-bridge.d.ts +13 -0
  385. package/dist/filters/special/response-openai-to-responses-bridge.js +24 -0
  386. package/dist/filters/special/response-tool-arguments-blacklist.d.ts +12 -0
  387. package/dist/filters/special/response-tool-arguments-blacklist.js +63 -0
  388. package/dist/filters/special/response-tool-arguments-schema-converge.d.ts +13 -0
  389. package/dist/filters/special/response-tool-arguments-schema-converge.js +63 -0
  390. package/dist/filters/special/response-tool-arguments-stringify.d.ts +9 -0
  391. package/dist/filters/special/response-tool-arguments-stringify.js +134 -0
  392. package/dist/filters/special/response-tool-arguments-toon-decode.d.ts +10 -0
  393. package/dist/filters/special/response-tool-arguments-toon-decode.js +94 -0
  394. package/dist/filters/special/response-tool-arguments-whitelist.d.ts +11 -0
  395. package/dist/filters/special/response-tool-arguments-whitelist.js +56 -0
  396. package/dist/filters/special/response-tool-text-canonicalize.d.ts +10 -0
  397. package/dist/filters/special/response-tool-text-canonicalize.js +20 -0
  398. package/dist/filters/special/tool-filter-hooks.d.ts +19 -0
  399. package/dist/filters/special/tool-filter-hooks.js +341 -0
  400. package/dist/filters/special/tool-post-constraints.d.ts +31 -0
  401. package/dist/filters/special/tool-post-constraints.js +120 -0
  402. package/dist/filters/types.d.ts +68 -0
  403. package/dist/filters/types.js +3 -0
  404. package/dist/filters/utils/fieldmap-loader.d.ts +2 -0
  405. package/dist/filters/utils/fieldmap-loader.js +16 -0
  406. package/dist/filters/utils/snapshot-writer.d.ts +10 -0
  407. package/dist/filters/utils/snapshot-writer.js +47 -0
  408. package/dist/guidance/index.d.ts +3 -0
  409. package/dist/guidance/index.js +228 -0
  410. package/dist/hooks/hooks-integration.d.ts +161 -0
  411. package/dist/hooks/hooks-integration.js +519 -0
  412. package/dist/hooks/index.d.ts +10 -0
  413. package/dist/hooks/index.js +13 -0
  414. package/dist/html-to-markdown.d.ts +5 -0
  415. package/dist/html-to-markdown.js +66 -0
  416. package/dist/http/sse-response.d.ts +22 -0
  417. package/dist/http/sse-response.js +103 -0
  418. package/dist/index.d.ts +10 -0
  419. package/dist/index.js +10 -0
  420. package/dist/router/virtual-router/bootstrap.d.ts +6 -0
  421. package/dist/router/virtual-router/bootstrap.js +620 -0
  422. package/dist/router/virtual-router/classifier.d.ts +10 -0
  423. package/dist/router/virtual-router/classifier.js +99 -0
  424. package/dist/router/virtual-router/engine.d.ts +35 -0
  425. package/dist/router/virtual-router/engine.js +260 -0
  426. package/dist/router/virtual-router/error-center.d.ts +10 -0
  427. package/dist/router/virtual-router/error-center.js +39 -0
  428. package/dist/router/virtual-router/features.d.ts +3 -0
  429. package/dist/router/virtual-router/features.js +162 -0
  430. package/dist/router/virtual-router/health-manager.d.ts +18 -0
  431. package/dist/router/virtual-router/health-manager.js +98 -0
  432. package/dist/router/virtual-router/load-balancer.d.ts +18 -0
  433. package/dist/router/virtual-router/load-balancer.js +67 -0
  434. package/dist/router/virtual-router/provider-registry.d.ts +11 -0
  435. package/dist/router/virtual-router/provider-registry.js +74 -0
  436. package/dist/router/virtual-router/types.d.ts +206 -0
  437. package/dist/router/virtual-router/types.js +31 -0
  438. package/dist/sse/index.d.ts +140 -0
  439. package/dist/sse/index.js +115 -0
  440. package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +11 -0
  441. package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.js +79 -0
  442. package/dist/sse/json-to-sse/chat-json-to-sse-converter.d.ts +79 -0
  443. package/dist/sse/json-to-sse/chat-json-to-sse-converter.js +296 -0
  444. package/dist/sse/json-to-sse/event-generators/chat.d.ts +55 -0
  445. package/dist/sse/json-to-sse/event-generators/chat.js +233 -0
  446. package/dist/sse/json-to-sse/event-generators/responses.d.ts +99 -0
  447. package/dist/sse/json-to-sse/event-generators/responses.js +599 -0
  448. package/dist/sse/json-to-sse/index.d.ts +6 -0
  449. package/dist/sse/json-to-sse/index.js +7 -0
  450. package/dist/sse/json-to-sse/responses-json-to-sse-converter.d.ts +79 -0
  451. package/dist/sse/json-to-sse/responses-json-to-sse-converter.js +322 -0
  452. package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +37 -0
  453. package/dist/sse/json-to-sse/sequencers/chat-sequencer.js +218 -0
  454. package/dist/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
  455. package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +263 -0
  456. package/dist/sse/registry/sse-codec-registry.d.ts +32 -0
  457. package/dist/sse/registry/sse-codec-registry.js +106 -0
  458. package/dist/sse/shared/chat-serializer.d.ts +4 -0
  459. package/dist/sse/shared/chat-serializer.js +40 -0
  460. package/dist/sse/shared/constants.d.ts +272 -0
  461. package/dist/sse/shared/constants.js +321 -0
  462. package/dist/sse/shared/serializers/base-serializer.d.ts +158 -0
  463. package/dist/sse/shared/serializers/base-serializer.js +210 -0
  464. package/dist/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
  465. package/dist/sse/shared/serializers/chat-event-serializer.js +275 -0
  466. package/dist/sse/shared/serializers/index.d.ts +39 -0
  467. package/dist/sse/shared/serializers/index.js +54 -0
  468. package/dist/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
  469. package/dist/sse/shared/serializers/responses-event-serializer.js +356 -0
  470. package/dist/sse/shared/serializers/types.d.ts +51 -0
  471. package/dist/sse/shared/serializers/types.js +4 -0
  472. package/dist/sse/shared/utils.d.ts +254 -0
  473. package/dist/sse/shared/utils.js +543 -0
  474. package/dist/sse/shared/writer.d.ts +111 -0
  475. package/dist/sse/shared/writer.js +268 -0
  476. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +13 -0
  477. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.js +139 -0
  478. package/dist/sse/sse-to-json/builders/response-builder.d.ts +165 -0
  479. package/dist/sse/sse-to-json/builders/response-builder.js +776 -0
  480. package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +106 -0
  481. package/dist/sse/sse-to-json/chat-sse-to-json-converter.js +540 -0
  482. package/dist/sse/sse-to-json/index.d.ts +6 -0
  483. package/dist/sse/sse-to-json/index.js +7 -0
  484. package/dist/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
  485. package/dist/sse/sse-to-json/parsers/sse-parser.js +340 -0
  486. package/dist/sse/sse-to-json/responses-sse-to-json-converter.d.ts +59 -0
  487. package/dist/sse/sse-to-json/responses-sse-to-json-converter.js +270 -0
  488. package/dist/sse/types/anthropic-types.d.ts +87 -0
  489. package/dist/sse/types/anthropic-types.js +5 -0
  490. package/dist/sse/types/chat-types.d.ts +308 -0
  491. package/dist/sse/types/chat-types.js +32 -0
  492. package/dist/sse/types/conversion-context.d.ts +211 -0
  493. package/dist/sse/types/conversion-context.js +20 -0
  494. package/dist/sse/types/core-interfaces.d.ts +90 -0
  495. package/dist/sse/types/core-interfaces.js +5 -0
  496. package/dist/sse/types/index.d.ts +14 -0
  497. package/dist/sse/types/index.js +7 -0
  498. package/dist/sse/types/responses-types.d.ts +328 -0
  499. package/dist/sse/types/responses-types.js +38 -0
  500. package/dist/sse/types/sse-events.d.ts +180 -0
  501. package/dist/sse/types/sse-events.js +150 -0
  502. package/dist/sse/types/stream-state.d.ts +204 -0
  503. package/dist/sse/types/stream-state.js +44 -0
  504. package/dist/sse/types/utility-types.d.ts +230 -0
  505. package/dist/sse/types/utility-types.js +49 -0
  506. package/dist/tools/proxy/anthropic-tool-proxy.d.ts +19 -0
  507. package/dist/tools/proxy/anthropic-tool-proxy.js +57 -0
  508. package/dist/tools/proxy/executors/web-fetch.d.ts +6 -0
  509. package/dist/tools/proxy/executors/web-fetch.js +39 -0
  510. package/dist/tools/proxy/registry.d.ts +8 -0
  511. package/dist/tools/proxy/registry.js +14 -0
  512. package/dist/tools/proxy/types.d.ts +29 -0
  513. package/dist/tools/proxy/types.js +1 -0
  514. package/dist/tools/tool-registry.d.ts +8 -0
  515. package/dist/tools/tool-registry.js +154 -0
  516. package/dist/tools/web-fetch-html.d.ts +15 -0
  517. package/dist/tools/web-fetch-html.js +83 -0
  518. package/dist/utils/conversion-adapter.d.ts +65 -0
  519. package/dist/utils/conversion-adapter.js +183 -0
  520. package/dist/utils/error-factory.d.ts +88 -0
  521. package/dist/utils/error-factory.js +138 -0
  522. package/dist/utils/error-utils.d.ts +334 -0
  523. package/dist/utils/error-utils.js +782 -0
  524. package/dist/utils/index.d.ts +8 -0
  525. package/dist/utils/index.js +13 -0
  526. package/dist/utils/performance-utils.d.ts +205 -0
  527. package/dist/utils/performance-utils.js +440 -0
  528. package/dist/utils/profile-mapper.d.ts +32 -0
  529. package/dist/utils/profile-mapper.js +269 -0
  530. package/dist/utils/request-processor.d.ts +52 -0
  531. package/dist/utils/request-processor.js +210 -0
  532. package/dist/utils/request-utils.d.ts +117 -0
  533. package/dist/utils/request-utils.js +333 -0
  534. package/dist/utils/response-utils.d.ts +127 -0
  535. package/dist/utils/response-utils.js +516 -0
  536. package/dist/utils/token-counter.d.ts +89 -0
  537. package/dist/utils/token-counter.js +541 -0
  538. package/package.json +65 -0
@@ -0,0 +1,269 @@
1
+ // Unified tool governance API (标准)
2
+ // Centralizes tool augmentation, guidance injection/refinement, and textual→tool_calls canonicalization
3
+ function isObject(v) { return !!v && typeof v === 'object' && !Array.isArray(v); }
4
+ // Note: tool schema strict augmentation removed per alignment
5
+ function enforceChatBudget(chat, _modelId) { return chat; }
6
+ function tryWriteSnapshot(options, stage, data) {
7
+ try {
8
+ // 仅在 verbose 级别保存快照(环境变量)
9
+ const envLevel = String(process.env.RCC_HOOKS_VERBOSITY || process.env.ROUTECODEX_HOOKS_VERBOSITY || '').toLowerCase();
10
+ const isVerbose = envLevel === 'verbose';
11
+ if (!isVerbose)
12
+ return;
13
+ const snap = options?.snapshot;
14
+ if (!snap || snap.enabled === false)
15
+ return;
16
+ const os = require('os');
17
+ const fs = require('fs');
18
+ const path = require('path');
19
+ const home = os.homedir?.() || process.env.HOME || '';
20
+ const base = snap.baseDir || path.join(home, '.routecodex', 'codex-samples');
21
+ const ep = String(snap.endpoint || 'chat').toLowerCase();
22
+ const group = ep.includes('responses') ? 'openai-responses' : ep.includes('messages') ? 'anthropic-messages' : 'openai-chat';
23
+ const rid = String(snap.requestId || `req_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`);
24
+ const dir = path.join(base, group);
25
+ const file = path.join(dir, `${rid}_govern-${stage}.json`);
26
+ if (fs.existsSync(file))
27
+ return; // 不重复
28
+ fs.mkdirSync(dir, { recursive: true });
29
+ const payload = JSON.stringify(data, null, 2);
30
+ fs.writeFileSync(file, payload, 'utf-8');
31
+ }
32
+ catch { /* ignore snapshot errors */ }
33
+ }
34
+ /**
35
+ * Process OpenAI Chat request (messages/tools) with unified 标准 governance.
36
+ * - Augment tools (strict schemas)
37
+ * - Inject/Refine system tool guidance (idempotent)
38
+ * - Canonicalize textual tool markup to tool_calls; set content=null when applicable
39
+ */
40
+ export function processChatRequestTools(request, opts) {
41
+ const options = { ...(opts || {}) };
42
+ if (!isObject(request))
43
+ return request;
44
+ const out = JSON.parse(JSON.stringify(request));
45
+ // tools 形状最小修复:为缺失 function.parameters 的工具补一个空对象,避免上游
46
+ // Responses/OpenAI 校验 422(外部错误必须暴露,但这里属于规范化入口)。
47
+ try {
48
+ const tools = Array.isArray(out?.tools) ? out.tools : [];
49
+ if (tools.length > 0) {
50
+ for (const t of tools) {
51
+ if (!t || typeof t !== 'object')
52
+ continue;
53
+ const fn = t.function;
54
+ if (!fn || typeof fn !== 'object')
55
+ continue;
56
+ const typeStr = String(t.type || '').toLowerCase();
57
+ const nameStr = typeof fn.name === 'string' ? String(fn.name).toLowerCase() : '';
58
+ const shouldPatch = typeStr === 'function' ||
59
+ nameStr === 'apply_patch';
60
+ if (!shouldPatch)
61
+ continue;
62
+ if (!Object.prototype.hasOwnProperty.call(fn, 'parameters')) {
63
+ t.function = { ...fn, parameters: {} };
64
+ }
65
+ }
66
+ }
67
+ }
68
+ catch { /* best-effort: 保持原样 */ }
69
+ // 1) 移除工具 schema 严格化(与 统一标准,不在此处约束 tools 结构)
70
+ // NOTE: system guidance injection removed by design (align with parameter-level strategy)
71
+ try {
72
+ // 请求侧不再对历史消息做 shell 形状包装;仅维持最小字符串化等轻度修复
73
+ const canonical = out;
74
+ // 3.1) tool_choice 策略(与标准 tooluse 对齐):对特定模型可设置 required;默认 auto
75
+ try {
76
+ const modelId = typeof canonical?.model === 'string' ? String(canonical.model) : 'unknown';
77
+ const raw = String(process?.env?.RCC_TOOL_CHOICE_REQUIRED || '').trim();
78
+ const wanted = raw ? raw.split(',').map((s) => s.trim()).filter(Boolean) : [];
79
+ if (Array.isArray(canonical?.tools) && canonical.tools.length > 0) {
80
+ // default auto
81
+ if (!canonical.tool_choice)
82
+ canonical.tool_choice = 'auto';
83
+ if (wanted.length > 0 && wanted.some((p) => modelId.includes(p))) {
84
+ canonical.tool_choice = 'required';
85
+ }
86
+ }
87
+ }
88
+ catch { /* ignore */ }
89
+ // 4) Enforce payload budget (context bytes) with minimal loss policy
90
+ const modelId = typeof canonical?.model === 'string' ? String(canonical.model) : 'unknown';
91
+ return enforceChatBudget(canonical, modelId);
92
+ }
93
+ catch {
94
+ return out;
95
+ }
96
+ }
97
+ /**
98
+ * Process OpenAI Chat response (choices[0].message) with unified 标准 governance.
99
+ * - Canonicalize textual tool markup to tool_calls; ensure finish_reason and content=null policy
100
+ */
101
+ import { repairArgumentsToString, parseLenient } from './jsonish.js';
102
+ function enhanceResponseToolArguments(chat) {
103
+ try {
104
+ const enable = String(process?.env?.RCC_TOOL_ENHANCE ?? '1').trim() !== '0';
105
+ if (!enable)
106
+ return chat;
107
+ const out = JSON.parse(JSON.stringify(chat));
108
+ const choices = Array.isArray(out?.choices) ? out.choices : [];
109
+ for (const ch of choices) {
110
+ const msg = ch && ch.message ? ch.message : undefined;
111
+ const tcs = Array.isArray(msg?.tool_calls) ? msg.tool_calls : [];
112
+ if (!tcs.length)
113
+ continue;
114
+ // Helpers for shell normalization (idempotent, aligned with ResponseToolArgumentsStringifyFilter)
115
+ const toStr = (v) => String(v);
116
+ const isBashLc = (arr) => arr.length >= 2 && arr[0] === 'bash' && arr[1] === '-lc';
117
+ const hasMetaToken = (arr) => {
118
+ const metas = ['|', '>', '>>', '<', '<<', ';', '&&', '||', '(', ')'];
119
+ return arr.some(t => {
120
+ const s = String(t);
121
+ if (metas.includes(s))
122
+ return true;
123
+ return /[|<>;&]/.test(s) || s.includes('&&') || s.includes('||') || s.includes('<<');
124
+ });
125
+ };
126
+ const repairFindMeta = (s) => {
127
+ try {
128
+ const hasFind = /(^|\s)find\s/.test(s);
129
+ if (!hasFind)
130
+ return s;
131
+ let out = s;
132
+ out = out.replace(/-exec([^;]*?)(?<!\\);/g, (_m, g1) => `-exec${g1} \\;`);
133
+ out = out.replace(/-exec([^;]*?)\\+;/g, (_m, g1) => `-exec${g1} \\;`);
134
+ out = out.replace(/(?<!\\)\(/g, '\\(').replace(/(?<!\\)\)/g, '\\)');
135
+ return out;
136
+ }
137
+ catch {
138
+ return s;
139
+ }
140
+ };
141
+ for (const tc of tcs) {
142
+ try {
143
+ const fn = tc && tc.function ? tc.function : undefined;
144
+ const name = typeof fn?.name === 'string' ? String(fn.name).toLowerCase() : '';
145
+ const argIn = fn?.arguments;
146
+ const repaired = repairArgumentsToString(argIn);
147
+ // Default to repaired JSON string
148
+ let finalStr = repaired;
149
+ // Extra normalization for shell
150
+ if (name === 'shell') {
151
+ let parsed;
152
+ try {
153
+ parsed = JSON.parse(repaired);
154
+ }
155
+ catch {
156
+ parsed = parseLenient(repaired);
157
+ }
158
+ if (parsed && typeof parsed === 'object') {
159
+ const cmd = parsed.command;
160
+ if (Array.isArray(cmd)) {
161
+ const tokens = cmd.map(toStr);
162
+ if (isBashLc(tokens)) {
163
+ const joined = tokens.slice(2).join(' ');
164
+ parsed.command = ['bash', '-lc', repairFindMeta(joined)];
165
+ }
166
+ else if (hasMetaToken(tokens)) {
167
+ parsed.command = ['bash', '-lc', repairFindMeta(tokens.join(' '))];
168
+ }
169
+ }
170
+ else if (typeof cmd === 'string') {
171
+ const s = cmd.trim();
172
+ const hasMeta = /[|<>;&]/.test(s) || s.includes('&&') || s.includes('||') || s.includes('<<');
173
+ if (hasMeta && !/^\s*bash\s+-lc\s+/.test(s)) {
174
+ parsed.command = ['bash', '-lc', repairFindMeta(s)];
175
+ }
176
+ else if (hasMeta && /^\s*bash\s+-lc\s+/.test(s)) {
177
+ // Already wrapped but ensure idempotent repairs
178
+ const body = s.replace(/^\s*bash\s+-lc\s+/, '');
179
+ parsed.command = ['bash', '-lc', repairFindMeta(body)];
180
+ }
181
+ }
182
+ try {
183
+ finalStr = JSON.stringify(parsed);
184
+ }
185
+ catch {
186
+ finalStr = repaired;
187
+ }
188
+ }
189
+ }
190
+ if (fn)
191
+ fn.arguments = finalStr;
192
+ }
193
+ catch { /* keep original */ }
194
+ }
195
+ // Ensure finish_reason/tool_calls invariant if missing (idempotent with canonicalizer)
196
+ try {
197
+ if (!ch.finish_reason)
198
+ ch.finish_reason = 'tool_calls';
199
+ }
200
+ catch { /* ignore */ }
201
+ try {
202
+ if (msg && typeof msg === 'object' && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0)
203
+ msg.content = null;
204
+ }
205
+ catch { /* ignore */ }
206
+ }
207
+ return out;
208
+ }
209
+ catch {
210
+ return chat;
211
+ }
212
+ }
213
+ export function processChatResponseTools(resp) {
214
+ if (!isObject(resp))
215
+ return resp;
216
+ try {
217
+ const { canonicalizeChatResponseTools } = require('./tool-canonicalizer.js');
218
+ const canon = canonicalizeChatResponseTools(resp);
219
+ return enhanceResponseToolArguments(canon);
220
+ }
221
+ catch {
222
+ return resp;
223
+ }
224
+ }
225
+ // Unified, 对齐 governance entry
226
+ export function governTools(payload, ctx) {
227
+ const phase = ctx?.phase || 'request';
228
+ const ep = ctx?.endpoint || 'chat';
229
+ if (phase === 'request') {
230
+ return processChatRequestTools(payload, {
231
+ injectGuidance: ctx?.injectGuidance !== false,
232
+ snapshot: ctx?.snapshot || { enabled: true, endpoint: ep, requestId: ctx?.requestId }
233
+ });
234
+ }
235
+ // response phase
236
+ // 变更前快照:响应侧 canonicalize 之前
237
+ try {
238
+ const opts = { snapshot: ctx?.snapshot || { enabled: true, endpoint: ep, requestId: ctx?.requestId } };
239
+ tryWriteSnapshot(opts, 'response_before_canonicalize', payload);
240
+ }
241
+ catch { /* ignore */ }
242
+ let out = processChatResponseTools(payload);
243
+ // 变更后快照:响应侧 canonicalize 之后
244
+ try {
245
+ const opts = { snapshot: ctx?.snapshot || { enabled: true, endpoint: ep, requestId: ctx?.requestId } };
246
+ tryWriteSnapshot(opts, 'response_after_canonicalize', out);
247
+ }
248
+ catch { /* ignore */ }
249
+ if (ep === 'responses' && ctx?.stream !== true && ctx?.produceRequiredAction !== false) {
250
+ // 变更前快照:构造 required_action 之前
251
+ try {
252
+ const opts = { snapshot: ctx?.snapshot || { enabled: true, endpoint: ep, requestId: ctx?.requestId } };
253
+ tryWriteSnapshot(opts, 'response_before_required_action', out);
254
+ }
255
+ catch { /* ignore */ }
256
+ try {
257
+ const { buildResponsesPayloadFromChat } = require('../responses/responses-openai-bridge.js');
258
+ const res = buildResponsesPayloadFromChat(out, { requestId: ctx?.requestId });
259
+ try {
260
+ const opts = { snapshot: ctx?.snapshot || { enabled: true, endpoint: ep, requestId: ctx?.requestId } };
261
+ tryWriteSnapshot(opts, 'response_after_required_action', res);
262
+ }
263
+ catch { /* ignore */ }
264
+ return res;
265
+ }
266
+ catch { /* ignore mapping errors and return canonicalized chat */ }
267
+ }
268
+ return out;
269
+ }
@@ -0,0 +1,31 @@
1
+ type Unknown = Record<string, unknown>;
2
+ export interface HarvestContext {
3
+ requestId?: string;
4
+ idPrefix?: string;
5
+ chunkSize?: number;
6
+ source?: 'chat' | 'responses' | 'messages';
7
+ }
8
+ export interface HarvestSignal {
9
+ type: 'delta' | 'final' | 'compat';
10
+ payload: Unknown;
11
+ }
12
+ export interface DeltaEvent {
13
+ tool_calls?: Array<{
14
+ index: number;
15
+ id: string;
16
+ type: 'function';
17
+ function: {
18
+ name?: string;
19
+ arguments?: string;
20
+ };
21
+ }>;
22
+ content?: string;
23
+ role?: string;
24
+ }
25
+ export interface HarvestResult {
26
+ deltaEvents: DeltaEvent[];
27
+ normalized?: Unknown;
28
+ stats?: Unknown;
29
+ }
30
+ export declare function harvestTools(signal: HarvestSignal, ctx?: HarvestContext): HarvestResult;
31
+ export {};
@@ -0,0 +1,321 @@
1
+ // Unified tool harvesting (对齐, single entry)
2
+ // - First-time harvesting only (no late-stage repair)
3
+ // - Handles textual markers (<function=execute>, unified diff)
4
+ // - Handles structural shapes (function_call legacy, tool_calls)
5
+ // - Normalizes arguments (single JSON string), sets finish_reason when applicable
6
+ function isObject(v) {
7
+ return !!v && typeof v === 'object' && !Array.isArray(v);
8
+ }
9
+ // Adjacent dedupe state (per request)
10
+ const dedupeState = new Map();
11
+ function hashString(s) {
12
+ try {
13
+ return require('crypto').createHash('sha1').update(s).digest('hex');
14
+ }
15
+ catch {
16
+ return String(s.length);
17
+ }
18
+ }
19
+ function toJsonString(v) {
20
+ if (typeof v === 'string')
21
+ return v;
22
+ try {
23
+ return JSON.stringify(v ?? {});
24
+ }
25
+ catch {
26
+ return '{}';
27
+ }
28
+ }
29
+ function genId(ctx, i = 0) {
30
+ const p = ctx?.idPrefix || 'call';
31
+ return `${p}_${Date.now()}_${(Math.random().toString(36).slice(2, 8))}_${i}`;
32
+ }
33
+ function chunkString(input, size) {
34
+ try {
35
+ const s = String(input ?? '');
36
+ if (!s)
37
+ return [];
38
+ const out = [];
39
+ for (let i = 0; i < s.length; i += size)
40
+ out.push(s.slice(i, i + size));
41
+ return out;
42
+ }
43
+ catch {
44
+ return [String(input ?? '')];
45
+ }
46
+ }
47
+ function extractFromTextual(content, ctx) {
48
+ const events = [];
49
+ if (typeof content !== 'string' || !content)
50
+ return events;
51
+ // 1) unified diff
52
+ const beginIdx = content.indexOf('*** Begin Patch');
53
+ const endIdx = content.indexOf('*** End Patch');
54
+ if (beginIdx >= 0 && endIdx > beginIdx) {
55
+ const patchText = content.slice(beginIdx, endIdx + '*** End Patch'.length);
56
+ const id = genId(ctx, 0);
57
+ const argStr = toJsonString({ patch: patchText });
58
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { name: 'apply_patch' } }] });
59
+ const parts = chunkString(argStr, Math.max(32, Math.min(1024, ctx?.chunkSize || 256)));
60
+ for (const d of parts) {
61
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { arguments: d } }] });
62
+ }
63
+ return events;
64
+ }
65
+ // 2) <function=execute>
66
+ const execRe = /<function=execute>[\s\S]*?<parameter=command>([\s\S]*?)<\/parameter>[\s\S]*?<\/function=execute>/i;
67
+ const mExec = execRe.exec(content);
68
+ if (mExec && mExec[1]) {
69
+ const cmdRaw = mExec[1].trim();
70
+ const argv = splitCommand(cmdRaw);
71
+ const id = genId(ctx, 0);
72
+ const argStr = toJsonString({ command: argv });
73
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { name: 'shell' } }] });
74
+ const parts = chunkString(argStr, Math.max(32, Math.min(1024, ctx?.chunkSize || 256)));
75
+ for (const d of parts) {
76
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { arguments: d } }] });
77
+ }
78
+ return events;
79
+ }
80
+ // 3) rcc.tool.v1 JSON envelope (removed)
81
+ // 4) Generic <tool_call> textual block with <arg_key>/<arg_value>
82
+ try {
83
+ // Explicit wrapper form
84
+ const blockRe = /<tool_call[^>]*>[\s\S]*?<\/tool_call>/gi;
85
+ const blocks = content.match(blockRe) || [];
86
+ let matched = false;
87
+ for (const b of blocks) {
88
+ const nameTag = /<tool_name>([\s\S]*?)<\/tool_name>/i.exec(b);
89
+ let nm = nameTag && nameTag[1] ? String(nameTag[1]).trim() : '';
90
+ if (!nm) {
91
+ if (/<arg_key>\s*command\s*<\/arg_key>/i.test(b))
92
+ nm = 'shell';
93
+ else if (/\bapply_patch\b/i.test(b))
94
+ nm = 'apply_patch';
95
+ else if (/\bupdate_plan\b/i.test(b))
96
+ nm = 'update_plan';
97
+ else if (/\bview_image\b/i.test(b))
98
+ nm = 'view_image';
99
+ }
100
+ const pairRe = /<arg_key>([\s\S]*?)<\/arg_key>\s*<arg_value>([\s\S]*?)<\/arg_value>/gi;
101
+ const argsObj = {};
102
+ let m;
103
+ while ((m = pairRe.exec(b)) !== null) {
104
+ const k = String((m[1] || '').trim());
105
+ const vRaw = (m[2] || '').trim();
106
+ let v = vRaw;
107
+ try {
108
+ v = JSON.parse(vRaw);
109
+ }
110
+ catch { /* keep as string */ }
111
+ if (nm === 'shell' && k === 'command') {
112
+ if (!Array.isArray(v))
113
+ v = splitCommand(String(vRaw));
114
+ }
115
+ argsObj[k] = v;
116
+ }
117
+ if (nm) {
118
+ const id = genId(ctx, 0);
119
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { name: nm } }] });
120
+ const argStr = toJsonString(argsObj);
121
+ const parts = chunkString(argStr, Math.max(32, Math.min(1024, ctx?.chunkSize || 256)));
122
+ for (const d of parts) {
123
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { arguments: d } }] });
124
+ }
125
+ matched = true;
126
+ }
127
+ }
128
+ if (matched)
129
+ return events;
130
+ // Inline fallback: e.g., 'shell\n<arg_key>command</arg_key>...'
131
+ const inlineRe = /(shell|apply_patch|update_plan|view_image)[\s\S]*?<arg_key>([\s\S]*?)<\/arg_key>\s*<arg_value>([\s\S]*?)<\/arg_value>/i;
132
+ const mi = inlineRe.exec(content);
133
+ if (mi) {
134
+ const nm = String(mi[1]).trim();
135
+ const k = String(mi[2]).trim();
136
+ const vRaw = String(mi[3]).trim();
137
+ let v = vRaw;
138
+ try {
139
+ v = JSON.parse(vRaw);
140
+ }
141
+ catch { /* keep as string */ }
142
+ const args = {};
143
+ if (nm === 'shell' && k === 'command') {
144
+ if (!Array.isArray(v))
145
+ v = splitCommand(String(vRaw));
146
+ }
147
+ args[k] = v;
148
+ const id = genId(ctx, 0);
149
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { name: nm } }] });
150
+ const argStr = toJsonString(args);
151
+ for (const d of chunkString(argStr, Math.max(32, Math.min(1024, ctx?.chunkSize || 256)))) {
152
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { arguments: d } }] });
153
+ }
154
+ return events;
155
+ }
156
+ }
157
+ catch { /* ignore textual tool_call parse errors */ }
158
+ return events;
159
+ }
160
+ function splitCommand(s) {
161
+ try {
162
+ const out = [];
163
+ let cur = '';
164
+ let quote = null;
165
+ for (let i = 0; i < s.length; i++) {
166
+ const ch = s[i];
167
+ if (quote) {
168
+ if (ch === quote) {
169
+ quote = null;
170
+ }
171
+ else {
172
+ cur += ch;
173
+ }
174
+ }
175
+ else {
176
+ if (ch === '"' || ch === '\'') {
177
+ quote = ch;
178
+ }
179
+ else if (/\s/.test(ch)) {
180
+ if (cur) {
181
+ out.push(cur);
182
+ cur = '';
183
+ }
184
+ }
185
+ else {
186
+ cur += ch;
187
+ }
188
+ }
189
+ }
190
+ if (cur)
191
+ out.push(cur);
192
+ return out;
193
+ }
194
+ catch {
195
+ return [s];
196
+ }
197
+ }
198
+ export function harvestTools(signal, ctx) {
199
+ const result = { deltaEvents: [] };
200
+ const chunkSize = Math.max(32, Math.min(1024, ctx?.chunkSize || 256));
201
+ try {
202
+ if (signal.type === 'delta') {
203
+ // OpenAI streaming chunk delta
204
+ const delta = signal.payload;
205
+ const events = [];
206
+ const content = delta?.choices?.[0]?.delta?.content;
207
+ if (typeof content === 'string' && content.trim()) {
208
+ events.push(...extractFromTextual(content, ctx));
209
+ }
210
+ // legacy function_call → tool_calls
211
+ const fc = delta?.choices?.[0]?.delta?.function_call;
212
+ if (isObject(fc)) {
213
+ const name = typeof fc.name === 'string' ? fc.name : undefined;
214
+ const argStr = toJsonString(fc.arguments);
215
+ const id = genId(ctx, 0);
216
+ if (name) {
217
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { name } }] });
218
+ for (const d of chunkString(argStr, chunkSize)) {
219
+ events.push({ tool_calls: [{ index: 0, id, type: 'function', function: { arguments: d } }] });
220
+ }
221
+ }
222
+ }
223
+ // direct delta.tool_calls
224
+ const tcd = delta?.choices?.[0]?.delta?.tool_calls;
225
+ if (Array.isArray(tcd)) {
226
+ let idx = 0;
227
+ for (const tc of tcd) {
228
+ const id = typeof tc?.id === 'string' ? tc.id : genId(ctx, idx);
229
+ const name = typeof tc?.function?.name === 'string' ? tc.function.name : undefined;
230
+ const args = (tc?.function?.arguments !== undefined) ? toJsonString(tc.function.arguments) : undefined;
231
+ // Adjacent dedupe per requestId
232
+ const key = ctx?.requestId || 'default';
233
+ const state = dedupeState.get(key) || {};
234
+ const argsHash = typeof args === 'string' ? hashString(args) : undefined;
235
+ const isDup = (state.name === name && state.argsHash && argsHash && state.argsHash === argsHash);
236
+ if (!isDup) {
237
+ if (name) {
238
+ events.push({ tool_calls: [{ index: idx, id, type: 'function', function: { name } }] });
239
+ }
240
+ if (typeof args === 'string') {
241
+ for (const d of chunkString(args, chunkSize)) {
242
+ events.push({ tool_calls: [{ index: idx, id, type: 'function', function: { arguments: d } }] });
243
+ }
244
+ }
245
+ dedupeState.set(key, { name, argsHash });
246
+ }
247
+ idx += 1;
248
+ }
249
+ }
250
+ result.deltaEvents = events;
251
+ return result;
252
+ }
253
+ // final/compat: normalize choices[0].message.tool_calls (no synthesis)
254
+ const src = signal.payload;
255
+ const choice = Array.isArray(src?.choices) ? src.choices[0] : null;
256
+ const msg = choice?.message || {};
257
+ const toolCalls = Array.isArray(msg?.tool_calls) ? msg.tool_calls : [];
258
+ if (toolCalls.length > 0) {
259
+ // Ensure arguments are stringified
260
+ for (const tc of toolCalls) {
261
+ const fn = tc.function || {};
262
+ if (fn && fn.arguments && typeof fn.arguments !== 'string') {
263
+ fn.arguments = toJsonString(fn.arguments);
264
+ }
265
+ }
266
+ // finish_reason
267
+ if (choice && choice.finish_reason !== 'tool_calls') {
268
+ choice.finish_reason = 'tool_calls';
269
+ }
270
+ // content policy: set to empty string or null unchanged
271
+ result.normalized = src;
272
+ return result;
273
+ }
274
+ // If no structured tool_calls, attempt textual harvesting from message.content
275
+ try {
276
+ const content = msg?.content;
277
+ if (typeof content === 'string' && content.trim()) {
278
+ const evs = extractFromTextual(content, ctx);
279
+ if (Array.isArray(evs) && evs.length > 0) {
280
+ // Aggregate deltaEvents into final tool_calls (group by id)
281
+ const map = {};
282
+ for (const e of evs) {
283
+ const tcs = Array.isArray(e.tool_calls) ? e.tool_calls : [];
284
+ for (const t of tcs) {
285
+ const id = String(t.id || genId(ctx, 0));
286
+ if (!map[id])
287
+ map[id] = { name: undefined, args: [], type: 'function' };
288
+ const nm = t.function?.name;
289
+ const arg = t.function?.arguments;
290
+ if (typeof nm === 'string' && !map[id].name)
291
+ map[id].name = nm;
292
+ if (typeof arg === 'string')
293
+ map[id].args.push(arg);
294
+ }
295
+ }
296
+ const merged = [];
297
+ for (const [id, item] of Object.entries(map)) {
298
+ const argStr = item.args.join('');
299
+ merged.push({ id, type: 'function', function: { name: item.name, arguments: argStr } });
300
+ }
301
+ if (merged.length > 0) {
302
+ msg.tool_calls = merged;
303
+ // Clear textual content to avoid duplicate display
304
+ msg.content = '';
305
+ if (choice && choice.finish_reason !== 'tool_calls') {
306
+ choice.finish_reason = 'tool_calls';
307
+ }
308
+ result.normalized = src;
309
+ return result;
310
+ }
311
+ }
312
+ }
313
+ }
314
+ catch { /* ignore textual harvest on final */ }
315
+ result.normalized = src;
316
+ return result;
317
+ }
318
+ catch {
319
+ return result;
320
+ }
321
+ }
@@ -0,0 +1,10 @@
1
+ export interface ToolCallFunction {
2
+ name: string;
3
+ arguments: string;
4
+ }
5
+ export interface ToolCallItem {
6
+ id?: string;
7
+ type: 'function';
8
+ function: ToolCallFunction;
9
+ }
10
+ export declare function stringifyArgs(args: unknown): string;
@@ -0,0 +1,10 @@
1
+ export function stringifyArgs(args) {
2
+ if (typeof args === 'string')
3
+ return args;
4
+ try {
5
+ return JSON.stringify(args ?? {});
6
+ }
7
+ catch {
8
+ return String(args);
9
+ }
10
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Shared helpers for standard tool normalization (shell packing rules).
3
+ * The goal is deterministic, minimal shaping so executors succeed consistently.
4
+ */
5
+ export interface ShellArgs {
6
+ command: string | string[];
7
+ workdir?: string;
8
+ timeout_ms?: number;
9
+ }
10
+ export declare function splitCommandString(input: string): string[];
11
+ /**
12
+ * Pack shell arguments per unified rules:
13
+ * - command: string -> ["bash","-lc","<string>"]
14
+ * - command: tokens[]
15
+ * - if starts with ["cd", path, ...rest]:
16
+ * - set workdir to path when absent
17
+ * - if rest empty => command=["pwd"]
18
+ * - else if rest has control tokens => command=["bash","-lc", join(rest)]
19
+ * - else command=rest (argv)
20
+ * - else if tokens contain control tokens => command=["bash","-lc", join(tokens)]
21
+ * - else command=tokens (argv)
22
+ * - join(rest) uses single-space join without extra quoting
23
+ */
24
+ export declare function packShellArgs(input: Record<string, unknown>): Record<string, unknown>;
25
+ export declare function flattenByComma(arr: string[]): string[];
26
+ export declare function chunkString(s: string, minParts?: number, maxParts?: number, targetChunk?: number): string[];