@zimtsui/brainswitch 0.0.56 → 0.0.57

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 (322) hide show
  1. package/README.md +109 -54
  2. package/assets/verbatim/declarations.handlebars +11 -0
  3. package/assets/verbatim/instruction.md +49 -0
  4. package/build/adaptor.d.ts +40 -9
  5. package/build/adaptor.js +36 -38
  6. package/build/adaptor.js.map +1 -1
  7. package/build/api-types/anthropic/billing.d.ts +5 -5
  8. package/build/api-types/anthropic/billing.js +1 -2
  9. package/build/api-types/anthropic/billing.js.map +1 -1
  10. package/build/api-types/anthropic/tool-codec.d.ts +11 -12
  11. package/build/api-types/anthropic/tool-codec.js +27 -42
  12. package/build/api-types/anthropic/tool-codec.js.map +1 -1
  13. package/build/api-types/google/billing.d.ts +5 -5
  14. package/build/api-types/google/billing.js +2 -3
  15. package/build/api-types/google/billing.js.map +1 -1
  16. package/build/api-types/google/restful-request.d.ts +1 -1
  17. package/build/api-types/google/restful-request.js +0 -1
  18. package/build/api-types/google/restful-request.js.map +1 -1
  19. package/build/api-types/google/tool-codec.d.ts +11 -11
  20. package/build/api-types/google/tool-codec.js +21 -19
  21. package/build/api-types/google/tool-codec.js.map +1 -1
  22. package/build/api-types/{openai-chatcompletion → openai-chatcompletions}/billing.d.ts +2 -3
  23. package/build/api-types/{openai-chatcompletion → openai-chatcompletions}/billing.js +0 -2
  24. package/build/api-types/openai-chatcompletions/billing.js.map +1 -0
  25. package/build/api-types/openai-chatcompletions/tool-codec.d.ts +17 -0
  26. package/build/api-types/{openai-chatcompletion → openai-chatcompletions}/tool-codec.js +26 -32
  27. package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -0
  28. package/build/api-types/openai-responses/billing.d.ts +5 -5
  29. package/build/api-types/openai-responses/billing.js +1 -2
  30. package/build/api-types/openai-responses/billing.js.map +1 -1
  31. package/build/api-types/openai-responses/tool-codec.d.ts +11 -11
  32. package/build/api-types/openai-responses/tool-codec.js +36 -32
  33. package/build/api-types/openai-responses/tool-codec.js.map +1 -1
  34. package/build/assets.d.ts +7 -0
  35. package/build/assets.js +8 -0
  36. package/build/assets.js.map +1 -0
  37. package/build/compatible-engine/session.d.ts +67 -0
  38. package/build/compatible-engine/session.js +92 -0
  39. package/build/compatible-engine/session.js.map +1 -0
  40. package/build/compatible-engine/structuring.d.ts +36 -0
  41. package/build/compatible-engine/structuring.js +34 -0
  42. package/build/compatible-engine/structuring.js.map +1 -0
  43. package/build/compatible-engine/validation.d.ts +21 -0
  44. package/build/compatible-engine/validation.js +82 -0
  45. package/build/compatible-engine/validation.js.map +1 -0
  46. package/build/compatible-engine.d/agentloop.d.ts +12 -0
  47. package/build/compatible-engine.d/agentloop.js +52 -0
  48. package/build/compatible-engine.d/agentloop.js.map +1 -0
  49. package/build/compatible-engine.d/aliyun/transport.d.ts +12 -0
  50. package/build/compatible-engine.d/aliyun/transport.js +7 -0
  51. package/build/compatible-engine.d/aliyun/transport.js.map +1 -0
  52. package/build/compatible-engine.d/aliyun.d.ts +24 -0
  53. package/build/compatible-engine.d/aliyun.js +46 -0
  54. package/build/compatible-engine.d/aliyun.js.map +1 -0
  55. package/build/compatible-engine.d/anthropic/choice-codec.d.ts +5 -0
  56. package/build/compatible-engine.d/anthropic/choice-codec.js +26 -0
  57. package/build/compatible-engine.d/anthropic/choice-codec.js.map +1 -0
  58. package/build/compatible-engine.d/anthropic/message-codec.d.ts +36 -0
  59. package/build/compatible-engine.d/anthropic/message-codec.js +86 -0
  60. package/build/compatible-engine.d/anthropic/message-codec.js.map +1 -0
  61. package/build/compatible-engine.d/anthropic/transport.d.ts +34 -0
  62. package/build/compatible-engine.d/anthropic/transport.js +150 -0
  63. package/build/compatible-engine.d/anthropic/transport.js.map +1 -0
  64. package/build/compatible-engine.d/anthropic.d.ts +26 -0
  65. package/build/compatible-engine.d/anthropic.js +46 -0
  66. package/build/compatible-engine.d/anthropic.js.map +1 -0
  67. package/build/compatible-engine.d/google/choice-codec.d.ts +5 -0
  68. package/build/compatible-engine.d/google/choice-codec.js +27 -0
  69. package/build/compatible-engine.d/google/choice-codec.js.map +1 -0
  70. package/build/compatible-engine.d/google/message-codec.d.ts +36 -0
  71. package/build/compatible-engine.d/google/message-codec.js +94 -0
  72. package/build/compatible-engine.d/google/message-codec.js.map +1 -0
  73. package/build/compatible-engine.d/google/transport.d.ts +30 -0
  74. package/build/{compatible.d → compatible-engine.d}/google/transport.js +22 -18
  75. package/build/compatible-engine.d/google/transport.js.map +1 -0
  76. package/build/compatible-engine.d/google.d.ts +26 -0
  77. package/build/compatible-engine.d/google.js +50 -0
  78. package/build/compatible-engine.d/google.js.map +1 -0
  79. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.d.ts +5 -0
  80. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js +31 -0
  81. package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js.map +1 -0
  82. package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +24 -0
  83. package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +71 -0
  84. package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +1 -0
  85. package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.d.ts +38 -0
  86. package/build/{compatible.d/openai-chatcompletions.d → compatible-engine.d/openai-chatcompletions/transport.d}/monolith.js +25 -22
  87. package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js.map +1 -0
  88. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.d.ts +37 -0
  89. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js +190 -0
  90. package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js.map +1 -0
  91. package/build/compatible-engine.d/openai-chatcompletions/transport.d.ts +10 -0
  92. package/build/compatible-engine.d/openai-chatcompletions/transport.js +11 -0
  93. package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +1 -0
  94. package/build/compatible-engine.d/openai-chatcompletions.d.ts +17 -0
  95. package/build/compatible-engine.d/openai-chatcompletions.js +14 -0
  96. package/build/compatible-engine.d/openai-chatcompletions.js.map +1 -0
  97. package/build/compatible-engine.d/openai-responses/choice-codec.d.ts +5 -0
  98. package/build/compatible-engine.d/openai-responses/choice-codec.js +30 -0
  99. package/build/compatible-engine.d/openai-responses/choice-codec.js.map +1 -0
  100. package/build/compatible-engine.d/openai-responses/message-codec.d.ts +36 -0
  101. package/build/compatible-engine.d/openai-responses/message-codec.js +96 -0
  102. package/build/compatible-engine.d/openai-responses/message-codec.js.map +1 -0
  103. package/build/compatible-engine.d/openai-responses/transport.d.ts +35 -0
  104. package/build/{compatible.d → compatible-engine.d}/openai-responses/transport.js +21 -29
  105. package/build/compatible-engine.d/openai-responses/transport.js.map +1 -0
  106. package/build/compatible-engine.d/openai-responses.d.ts +26 -0
  107. package/build/compatible-engine.d/openai-responses.js +46 -0
  108. package/build/compatible-engine.d/openai-responses.js.map +1 -0
  109. package/build/compatible-engine.d.ts +22 -0
  110. package/build/compatible-engine.js +29 -0
  111. package/build/compatible-engine.js.map +1 -0
  112. package/build/config.d.ts +1 -0
  113. package/build/config.js +1 -2
  114. package/build/config.js.map +1 -1
  115. package/build/endpoint-spec.d.ts +1 -0
  116. package/build/endpoint-spec.js +2 -1
  117. package/build/endpoint-spec.js.map +1 -1
  118. package/build/engine/session.d.ts +7 -0
  119. package/build/{session.js.map → engine/session.js.map} +1 -1
  120. package/build/engine/transport.d.ts +5 -0
  121. package/build/engine/transport.js +1 -0
  122. package/build/engine/transport.js.map +1 -0
  123. package/build/engine/validation.d.ts +9 -0
  124. package/build/engine/validation.js +1 -0
  125. package/build/engine/validation.js.map +1 -0
  126. package/build/engine.d.ts +49 -40
  127. package/build/engine.js +88 -70
  128. package/build/engine.js.map +1 -1
  129. package/build/exports.d.ts +14 -14
  130. package/build/exports.js +14 -14
  131. package/build/exports.js.map +1 -1
  132. package/build/function.d.ts +79 -66
  133. package/build/function.js +30 -47
  134. package/build/function.js.map +1 -1
  135. package/build/inference-context.d.ts +2 -2
  136. package/build/inference-context.js +0 -1
  137. package/build/inference-context.js.map +1 -1
  138. package/build/native-engines.d/google/message-codec.d.ts +18 -16
  139. package/build/native-engines.d/google/message-codec.js +31 -33
  140. package/build/native-engines.d/google/message-codec.js.map +1 -1
  141. package/build/native-engines.d/google/session.d.ts +44 -51
  142. package/build/native-engines.d/google/session.js +85 -81
  143. package/build/native-engines.d/google/session.js.map +1 -1
  144. package/build/native-engines.d/google/transport.d.ts +22 -19
  145. package/build/native-engines.d/google/transport.js +13 -13
  146. package/build/native-engines.d/google/transport.js.map +1 -1
  147. package/build/native-engines.d/google/validation.d.ts +22 -0
  148. package/build/native-engines.d/google/validation.js +20 -0
  149. package/build/native-engines.d/google/validation.js.map +1 -0
  150. package/build/native-engines.d/google.d/agentloop.d.ts +12 -0
  151. package/build/native-engines.d/google.d/agentloop.js +57 -0
  152. package/build/native-engines.d/google.d/agentloop.js.map +1 -0
  153. package/build/native-engines.d/google.d/exports.d.ts +3 -0
  154. package/build/native-engines.d/google.d/exports.js +4 -0
  155. package/build/native-engines.d/google.d/exports.js.map +1 -0
  156. package/build/native-engines.d/google.d.ts +42 -0
  157. package/build/native-engines.d/google.js +83 -0
  158. package/build/native-engines.d/google.js.map +1 -0
  159. package/build/native-engines.d/openai-responses/choice-codec.d.ts +5 -0
  160. package/build/native-engines.d/openai-responses/choice-codec.js +30 -0
  161. package/build/native-engines.d/openai-responses/choice-codec.js.map +1 -0
  162. package/build/native-engines.d/openai-responses/message-codec.d.ts +19 -17
  163. package/build/native-engines.d/openai-responses/message-codec.js +31 -25
  164. package/build/native-engines.d/openai-responses/message-codec.js.map +1 -1
  165. package/build/native-engines.d/openai-responses/session.d.ts +47 -45
  166. package/build/native-engines.d/openai-responses/session.js +75 -105
  167. package/build/native-engines.d/openai-responses/session.js.map +1 -1
  168. package/build/native-engines.d/openai-responses/structuring.d.ts +19 -0
  169. package/build/native-engines.d/openai-responses/structuring.js +19 -0
  170. package/build/native-engines.d/openai-responses/structuring.js.map +1 -0
  171. package/build/native-engines.d/openai-responses/tool.d.ts +15 -12
  172. package/build/native-engines.d/openai-responses/tool.js +1 -12
  173. package/build/native-engines.d/openai-responses/tool.js.map +1 -1
  174. package/build/native-engines.d/openai-responses/transport.d.ts +24 -25
  175. package/build/native-engines.d/openai-responses/transport.js +21 -41
  176. package/build/native-engines.d/openai-responses/transport.js.map +1 -1
  177. package/build/native-engines.d/openai-responses/validation.d.ts +20 -0
  178. package/build/native-engines.d/openai-responses/validation.js +80 -0
  179. package/build/native-engines.d/openai-responses/validation.js.map +1 -0
  180. package/build/native-engines.d/openai-responses.d/agentloop.d.ts +12 -0
  181. package/build/native-engines.d/openai-responses.d/agentloop.js +61 -0
  182. package/build/native-engines.d/openai-responses.d/agentloop.js.map +1 -0
  183. package/build/native-engines.d/openai-responses.d/exports.d.ts +4 -0
  184. package/build/native-engines.d/openai-responses.d/exports.js +5 -0
  185. package/build/native-engines.d/openai-responses.d/exports.js.map +1 -0
  186. package/build/native-engines.d/openai-responses.d.ts +44 -0
  187. package/build/native-engines.d/openai-responses.js +78 -0
  188. package/build/native-engines.d/openai-responses.js.map +1 -0
  189. package/build/throttle.d.ts +6 -5
  190. package/build/throttle.js +17 -14
  191. package/build/throttle.js.map +1 -1
  192. package/build/tsconfig.tsbuildinfo +1 -1
  193. package/build/verbatim/codec/declarations.d.ts +2 -0
  194. package/build/verbatim/codec/declarations.js +22 -0
  195. package/build/verbatim/codec/declarations.js.map +1 -0
  196. package/build/verbatim/codec/request.d.ts +7 -0
  197. package/build/verbatim/codec/request.js +53 -0
  198. package/build/verbatim/codec/request.js.map +1 -0
  199. package/build/verbatim/codec.d.ts +2 -0
  200. package/build/verbatim/codec.js +3 -0
  201. package/build/verbatim/codec.js.map +1 -0
  202. package/build/verbatim.d.ts +74 -0
  203. package/build/verbatim.js +17 -0
  204. package/build/verbatim.js.map +1 -0
  205. package/examples/agent.ts +65 -0
  206. package/examples/chat.ts +24 -0
  207. package/examples/config.ts +24 -0
  208. package/{test/src → examples}/tsconfig.json +6 -2
  209. package/examples/verbatim.ts +44 -0
  210. package/package.json +10 -9
  211. package/build/agentloop.d.ts +0 -12
  212. package/build/agentloop.js +0 -43
  213. package/build/agentloop.js.map +0 -1
  214. package/build/api-types/openai-chatcompletion/billing.js.map +0 -1
  215. package/build/api-types/openai-chatcompletion/tool-codec.d.ts +0 -18
  216. package/build/api-types/openai-chatcompletion/tool-codec.js.map +0 -1
  217. package/build/compatible/engine.d/aliyun.d.ts +0 -23
  218. package/build/compatible/engine.d/aliyun.js +0 -44
  219. package/build/compatible/engine.d/aliyun.js.map +0 -1
  220. package/build/compatible/engine.d/anthropic.d.ts +0 -23
  221. package/build/compatible/engine.d/anthropic.js +0 -41
  222. package/build/compatible/engine.d/anthropic.js.map +0 -1
  223. package/build/compatible/engine.d/google.d.ts +0 -23
  224. package/build/compatible/engine.d/google.js +0 -45
  225. package/build/compatible/engine.d/google.js.map +0 -1
  226. package/build/compatible/engine.d/openai-responses.d.ts +0 -23
  227. package/build/compatible/engine.d/openai-responses.js +0 -41
  228. package/build/compatible/engine.d/openai-responses.js.map +0 -1
  229. package/build/compatible/engine.d.ts +0 -18
  230. package/build/compatible/engine.js +0 -21
  231. package/build/compatible/engine.js.map +0 -1
  232. package/build/compatible/session.d.ts +0 -78
  233. package/build/compatible/session.js +0 -138
  234. package/build/compatible/session.js.map +0 -1
  235. package/build/compatible/tool-call-validator.d.ts +0 -11
  236. package/build/compatible/tool-call-validator.js +0 -12
  237. package/build/compatible/tool-call-validator.js.map +0 -1
  238. package/build/compatible.d/aliyun/transport.d.ts +0 -11
  239. package/build/compatible.d/aliyun/transport.js +0 -9
  240. package/build/compatible.d/aliyun/transport.js.map +0 -1
  241. package/build/compatible.d/anthropic/message-codec.d.ts +0 -31
  242. package/build/compatible.d/anthropic/message-codec.js +0 -88
  243. package/build/compatible.d/anthropic/message-codec.js.map +0 -1
  244. package/build/compatible.d/anthropic/transport.d.ts +0 -31
  245. package/build/compatible.d/anthropic/transport.js +0 -137
  246. package/build/compatible.d/anthropic/transport.js.map +0 -1
  247. package/build/compatible.d/google/message-codec.d.ts +0 -31
  248. package/build/compatible.d/google/message-codec.js +0 -98
  249. package/build/compatible.d/google/message-codec.js.map +0 -1
  250. package/build/compatible.d/google/transport.d.ts +0 -28
  251. package/build/compatible.d/google/transport.js.map +0 -1
  252. package/build/compatible.d/openai-chatcompletions/message-codec.d.ts +0 -19
  253. package/build/compatible.d/openai-chatcompletions/message-codec.js +0 -66
  254. package/build/compatible.d/openai-chatcompletions/message-codec.js.map +0 -1
  255. package/build/compatible.d/openai-chatcompletions/transport.d.ts +0 -9
  256. package/build/compatible.d/openai-chatcompletions/transport.js +0 -25
  257. package/build/compatible.d/openai-chatcompletions/transport.js.map +0 -1
  258. package/build/compatible.d/openai-chatcompletions.d/monolith.d.ts +0 -36
  259. package/build/compatible.d/openai-chatcompletions.d/monolith.js.map +0 -1
  260. package/build/compatible.d/openai-chatcompletions.d/stream.d.ts +0 -35
  261. package/build/compatible.d/openai-chatcompletions.d/stream.js +0 -196
  262. package/build/compatible.d/openai-chatcompletions.d/stream.js.map +0 -1
  263. package/build/compatible.d/openai-responses/message-codec.d.ts +0 -32
  264. package/build/compatible.d/openai-responses/message-codec.js +0 -105
  265. package/build/compatible.d/openai-responses/message-codec.js.map +0 -1
  266. package/build/compatible.d/openai-responses/transport.d.ts +0 -33
  267. package/build/compatible.d/openai-responses/transport.js.map +0 -1
  268. package/build/frontmatter.d.ts +0 -5
  269. package/build/frontmatter.js +0 -13
  270. package/build/frontmatter.js.map +0 -1
  271. package/build/native-engines.d/google/agentloop.d.ts +0 -12
  272. package/build/native-engines.d/google/agentloop.js +0 -42
  273. package/build/native-engines.d/google/agentloop.js.map +0 -1
  274. package/build/native-engines.d/google/engine.d.ts +0 -35
  275. package/build/native-engines.d/google/engine.js +0 -73
  276. package/build/native-engines.d/google/engine.js.map +0 -1
  277. package/build/native-engines.d/google/exports.d.ts +0 -5
  278. package/build/native-engines.d/google/exports.js +0 -6
  279. package/build/native-engines.d/google/exports.js.map +0 -1
  280. package/build/native-engines.d/openai-responses/agentloop.d.ts +0 -13
  281. package/build/native-engines.d/openai-responses/agentloop.js +0 -53
  282. package/build/native-engines.d/openai-responses/agentloop.js.map +0 -1
  283. package/build/native-engines.d/openai-responses/engine.d.ts +0 -32
  284. package/build/native-engines.d/openai-responses/engine.js +0 -63
  285. package/build/native-engines.d/openai-responses/engine.js.map +0 -1
  286. package/build/native-engines.d/openai-responses/exports.d.ts +0 -6
  287. package/build/native-engines.d/openai-responses/exports.js +0 -7
  288. package/build/native-engines.d/openai-responses/exports.js.map +0 -1
  289. package/build/native-engines.d/openai-responses/tool-call-validator.d.ts +0 -12
  290. package/build/native-engines.d/openai-responses/tool-call-validator.js +0 -33
  291. package/build/native-engines.d/openai-responses/tool-call-validator.js.map +0 -1
  292. package/build/session.d.ts +0 -7
  293. package/test/build/test-codecs.d.ts +0 -1
  294. package/test/build/test-codecs.js +0 -230
  295. package/test/build/test-codecs.js.map +0 -1
  296. package/test/build/test-compatible-engine.d.ts +0 -1
  297. package/test/build/test-compatible-engine.js +0 -27
  298. package/test/build/test-compatible-engine.js.map +0 -1
  299. package/test/build/test-compatible-engines.d.ts +0 -1
  300. package/test/build/test-compatible-engines.js +0 -112
  301. package/test/build/test-compatible-engines.js.map +0 -1
  302. package/test/build/test-engine.d.ts +0 -1
  303. package/test/build/test-engine.js +0 -97
  304. package/test/build/test-engine.js.map +0 -1
  305. package/test/build/test-helpers.d.ts +0 -46
  306. package/test/build/test-helpers.js +0 -45
  307. package/test/build/test-helpers.js.map +0 -1
  308. package/test/build/test-native-engines.d.ts +0 -1
  309. package/test/build/test-native-engines.js +0 -64
  310. package/test/build/test-native-engines.js.map +0 -1
  311. package/test/build/test.d.ts +0 -5
  312. package/test/build/test.js +0 -6
  313. package/test/build/test.js.map +0 -1
  314. package/test/build/tsconfig.tsbuildinfo +0 -1
  315. package/test/src/test-codecs.ts +0 -256
  316. package/test/src/test-compatible-engine.ts +0 -45
  317. package/test/src/test-compatible-engines.ts +0 -129
  318. package/test/src/test-engine.ts +0 -144
  319. package/test/src/test-helpers.ts +0 -53
  320. package/test/src/test-native-engines.ts +0 -75
  321. package/test/src/test.ts +0 -5
  322. /package/build/{session.js → engine/session.js} +0 -0
package/README.md CHANGED
@@ -34,46 +34,14 @@ npm install @zimtsui/brainswitch
34
34
  ## 配置
35
35
 
36
36
  ```ts
37
- export type Config = {
38
- brainswitch: {
39
- endpoints: Record<string, {
40
- baseUrl: string;
41
- apiKey: string;
42
- model: string;
43
- name: string;
44
- apiType:
45
- | 'openai-responses'
46
- | 'google'
47
- | 'aliyun'
48
- | 'anthropic'
49
- ;
50
- proxy?: string;
51
- inputPrice?: number; // CNY per MToken
52
- outputPrice?: number; // CNY per MToken
53
- cachePrice?: number; // CNY per MToken
54
- rpm?: number; // Requests per minute
55
- timeout?: number; // Time limit in milliseconds
56
- maxTokens?: number; // Maximum number of generated tokens
57
- additionalOptions?: Record<string, unknown>;
58
- };
59
- };
60
- }
61
- ```
62
-
63
- ## 快速上手
64
-
65
- ```ts
66
- import { Adaptor, agentloop, RoleMessage, Function, type InferenceContext, type Config, type Session } from '@zimtsui/brainswitch';
67
- import { Type } from '@sinclair/typebox';
68
- import { RWLock } from '@zimtsui/coroutine-locks';
69
-
37
+ import { type Config } from '@zimtsui/brainswitch';
70
38
 
71
39
  // 配置推理服务商 API 接入点
72
- const config: Config = {
40
+ export const config: Config = {
73
41
  brainswitch: {
74
42
  endpoints: {
75
- 'gpt-5-mini': {
76
- name: 'GPT-5 mini',
43
+ 'gpt-5.4-mini': {
44
+ name: 'GPT-5.4 mini',
77
45
  apiType: 'openai-responses',
78
46
  baseUrl: 'https://api.openai.com/v1',
79
47
  apiKey: process.env.OPENAI_API_KEY!,
@@ -89,25 +57,63 @@ const config: Config = {
89
57
  }
90
58
  }
91
59
  }
60
+ ```
61
+
62
+ ## 对话
63
+
64
+ ```ts
65
+ import { Adaptor, RoleMessage, type Session } from '@zimtsui/brainswitch';
66
+ import { config } from './config.ts';
67
+
68
+
69
+ // 创建会话
70
+ const session: Session<never, never> = {
71
+ developerMessage: new RoleMessage.Developer([
72
+ RoleMessage.Part.Text.paragraph('You are a helpful assistant.'),
73
+ ]),
74
+ chatMessages: [
75
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('Hello!') ]),
76
+ ],
77
+ };
78
+
79
+ // 选择推理引擎
80
+ const adaptor = Adaptor.create(config);
81
+ const engine = adaptor.makeCompatibleEngine<{}, {}>({
82
+ endpoint: 'gpt-5.4-mini',
83
+ functionDeclarationMap: {},
84
+ verbatimDeclarationMap: {},
85
+ });
86
+
87
+ const response = await engine.stateless({}, session);
88
+ console.log(response.getText());
89
+ ```
90
+
91
+ ## 智能体
92
+
93
+ ```ts
94
+ import { Adaptor, agentloop, RoleMessage, Function, type Session, Structuring } from '@zimtsui/brainswitch';
95
+ import { Type } from '@sinclair/typebox';
96
+ import { config } from './config.ts';
92
97
 
93
98
  // 声明函数工具
94
99
  const fdm = {
95
100
  get_weather: {
96
101
  description: '获取指定城市的天气',
97
- paraschema: Type.Object({
102
+ parameters: Type.Object({
98
103
  city: Type.String(),
99
104
  unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
100
105
  }),
101
106
  },
102
107
  submit_result: {
103
108
  description: '提交最终结果',
104
- paraschema: Type.Object({
109
+ parameters: Type.Object({
105
110
  weather: Type.String(),
106
111
  advice: Type.String(),
107
112
  }),
108
113
  },
109
- } satisfies Function.Declaration.Map;
114
+ } satisfies Function.Decl.Map.Proto;
110
115
  type fdm = typeof fdm;
116
+ type fdu = Function.Decl.From<fdm>;
111
117
 
112
118
  // 实现函数工具
113
119
  export class Submission {
@@ -123,35 +129,84 @@ const fnm: Function.Map<fdm> = {
123
129
  },
124
130
  };
125
131
 
126
- // 初始化工作流上下文
127
- const wfctx: InferenceContext = {
128
- busy: new RWLock(),
129
- cost(deltaCost) {
130
- console.log((-deltaCost).toFixed(2));
131
- },
132
- signal: null,
133
- };
134
-
135
132
  // 创建会话
136
- const session: Session<fdm> = {
137
- developerMessage: RoleMessage.Developer.create([
138
- RoleMessage.Part.Text.create('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
133
+ const session: Session<fdu, never> = {
134
+ developerMessage: new RoleMessage.Developer([
135
+ RoleMessage.Part.Text.paragraph('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
139
136
  ]),
140
137
  chatMessages: [
141
- RoleMessage.User.create([ RoleMessage.Part.Text.create('请查询现在北京的天气,并给穿衣建议。') ]),
138
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请查询现在北京的天气,并给穿衣建议。') ]),
142
139
  ],
143
140
  };
144
141
 
145
142
  // 选择推理引擎
146
143
  const adaptor = Adaptor.create(config);
147
- const engine = adaptor.makeEngine('gpt-5-mini', fdm, Function.ToolChoice.REQUIRED);
144
+ const engine = adaptor.makeCompatibleEngine<fdm, {}>({
145
+ endpoint: 'gpt-5.4-mini',
146
+ functionDeclarationMap: fdm,
147
+ verbatimDeclarationMap: {},
148
+ structuringChoice: Structuring.Choice.FCall.REQUIRED,
149
+ });
148
150
 
149
151
  // 使用 agentloop 驱动智能体循环,最多 8 轮对话
150
152
  try {
151
- for await (const text of agentloop(wfctx, session, engine, fnm, 8)) console.log(text);
153
+ for await (const text of agentloop({}, session, engine, fnm, 8)) console.log(text);
152
154
  } catch (e) {
153
155
  if (e instanceof Submission) {} else throw e;
154
156
  console.log(e.weather);
155
157
  console.log(e.advice);
156
158
  }
157
159
  ```
160
+
161
+ ### XML Verbatim 频道
162
+
163
+ When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
164
+
165
+ XML Verbatim Channel is designed to avoid escaping in structured output of large text.
166
+
167
+ ```ts
168
+ import { Adaptor, RoleMessage, type Session, Structuring, Verbatim } from '@zimtsui/brainswitch';
169
+ import Assets from '@zimtsui/brainswitch/assets';
170
+ import * as Codec from '@zimtsui/brainswitch/codec';
171
+ import { config } from './config.ts';
172
+
173
+ // 声明 XML Verbatim 频道
174
+ const vdm = {
175
+ bash: {
176
+ description: '执行 Bash 命令',
177
+ parameters: {
178
+ command: {
179
+ description: 'Bash 命令',
180
+ mimeType: 'text/plain',
181
+ },
182
+ },
183
+ },
184
+ } satisfies Verbatim.Decl.Map.Proto;
185
+ type vdm = typeof vdm;
186
+ type vdu = Verbatim.Decl.From<vdm>;
187
+
188
+
189
+ // 创建会话
190
+ const session: Session<never, vdu> = {
191
+ developerMessage: new RoleMessage.Developer([
192
+ Assets.verbatim.instruction,
193
+ RoleMessage.Part.Text.paragraph('# Available Verbatim Channels'),
194
+ RoleMessage.Part.Text.paragraph(Codec.Declarations.encode(vdm)),
195
+ ]),
196
+ chatMessages: [
197
+ new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请使用 Bash 命令查询当前系统时间。') ]),
198
+ ],
199
+ };
200
+
201
+ // 选择推理引擎
202
+ const adaptor = Adaptor.create(config);
203
+ const engine = adaptor.makeCompatibleEngine<{}, vdm>({
204
+ endpoint: 'gpt-5.4-mini',
205
+ functionDeclarationMap: {},
206
+ verbatimDeclarationMap: vdm,
207
+ structuringChoice: Structuring.Choice.VRequest.ANYONE,
208
+ });
209
+
210
+ const response = await engine.stateless({}, session);
211
+ console.log(response.getOnlyVerbatimRequest().args.command);
212
+ ```
@@ -0,0 +1,11 @@
1
+ {{#each declarations}}
2
+ <verbatim:declaration name="{{this.name}}">
3
+ <verbatim:description>{{this.description}}</verbatim:description>
4
+ {{#each this.parameters}}
5
+ <verbatim:parameter name="{{this.name}}">
6
+ <verbatim:description>{{this.description}}</verbatim:description>
7
+ <verbatim:mime-type>{{this.mimeType}}</verbatim:mime-type>
8
+ </verbatim:parameter>
9
+ {{/each}}
10
+ </verbatim:declaration>
11
+ {{/each}}
@@ -0,0 +1,49 @@
1
+ # XML Verbatim Channel
2
+
3
+ ## Motivation
4
+
5
+ When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
6
+
7
+ XML Verbatim Channel is designed to avoid escaping in structured output of large text.
8
+
9
+ ## Declaration
10
+
11
+ The system or the user will declare all available XML Verbatim Channels in the form of
12
+
13
+ <verbatim:declaration name="CHANNEL_1_NAME">
14
+ <verbatim:description>DESCRIPTION_OF_CHANNEL_1</verbatim:description>
15
+ <verbatim:parameter name="PARAMETER_1_NAME">
16
+ <verbatim:description>DESCRIPTION_OF_PARAMETER_1</verbatim:description>
17
+ <verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_1</verbatim:mime-type>
18
+ </verbatim:parameter>
19
+ <verbatim:parameter name="PARAMETER_2_NAME">
20
+ <verbatim:description>DESCRIPTION_OF_PARAMETER_2</verbatim:description>
21
+ <verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_2</verbatim:mime-type>
22
+ </verbatim:parameter>
23
+ </verbatim:declaration>
24
+
25
+ ## Request
26
+
27
+ You can make a request through the channel CHANNEL_1_NAME in the form of
28
+
29
+ <verbatim:request name="CHANNEL_1_NAME">
30
+ <verbatim:argument name="PARAMETER_1_NAME"><![CDATA[PARAMETER_1_VALUE]]></verbatim:argument>
31
+ <verbatim:argument name="PARAMETER_2_NAME"><![CDATA[PARAMETER_2_VALUE]]></verbatim:argument>
32
+ </verbatim:request>
33
+
34
+ - All parameters are required
35
+ - All arguments must be wrapped in CDATA
36
+
37
+ # Available XML Verbatim Channels
38
+
39
+ <verbatim:declaration name="submit">
40
+ <verbatim:description>提交文章</verbatim:description>
41
+ <verbatim:parameter name="title">
42
+ <verbatim:description>文章标题</verbatim:description>
43
+ <verbatim:mime-type>text/plain</verbatim:mime-type>
44
+ </verbatim:parameter>
45
+ <verbatim:parameter name="body">
46
+ <verbatim:description>文章内容</verbatim:description>
47
+ <verbatim:mime-type>text/markdown;dialect=gfm+tex_math_dollars</verbatim:mime-type>
48
+ </verbatim:parameter>
49
+ </verbatim:declaration>
@@ -1,14 +1,45 @@
1
- import { Config } from '#@/config.ts';
2
- import { Function } from '#@/function.ts';
3
- import { type CompatibleEngine } from '#@/compatible/engine.ts';
4
- import { OpenAIResponsesNativeEngine } from '#@/native-engines.d/openai-responses/engine.ts';
5
- import { GoogleNativeEngine } from '#@/native-engines.d/google/engine.ts';
1
+ import { Config } from './config.ts';
2
+ import { Function } from './function.ts';
3
+ import { type CompatibleEngine } from './compatible-engine.ts';
4
+ import { Throttle } from './throttle.ts';
5
+ import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses.ts';
6
+ import { GoogleNativeEngine } from './native-engines.d/google.ts';
7
+ import type { Verbatim } from './verbatim.ts';
8
+ import type { Structuring } from './compatible-engine/structuring.ts';
9
+ import type { Structuring as OpenAIResponsesNativeStructuring } from './native-engines.d/openai-responses/structuring.ts';
6
10
  export declare class Adaptor {
7
11
  config: Config;
8
12
  static create(config: Config): Adaptor;
9
- private throttles;
13
+ protected throttles: Map<string, Throttle>;
10
14
  protected constructor(config: Config);
11
- makeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): CompatibleEngine<fdm>;
12
- makeOpenAIResponsesNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, applyPatch?: boolean, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): OpenAIResponsesNativeEngine<fdm>;
13
- makeGoogleNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, codeExecution?: boolean, urlContext?: boolean, googleSearch?: boolean, parallelToolCall?: boolean): GoogleNativeEngine<fdm>;
15
+ makeCompatibleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.CompatibleEngine.Options<fdm, vdm>): CompatibleEngine<fdm, vdm>;
16
+ makeOpenAIResponsesNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.OpenAIResponsesNativeEngineOptions<fdm, vdm>): OpenAIResponsesNativeEngine<fdm, vdm>;
17
+ makeGoogleNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.GoogleNativeEngineOptions<fdm, vdm>): GoogleNativeEngine<fdm, vdm>;
18
+ }
19
+ export declare namespace Adaptor {
20
+ namespace CompatibleEngine {
21
+ interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
22
+ endpoint: string;
23
+ functionDeclarationMap: fdm;
24
+ verbatimDeclarationMap: vdm;
25
+ structuringChoice?: Structuring.Choice.From<fdm, vdm>;
26
+ }
27
+ }
28
+ interface OpenAIResponsesNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
29
+ endpoint: string;
30
+ functionDeclarationMap: fdm;
31
+ verbatimDeclarationMap: vdm;
32
+ applyPatch?: boolean;
33
+ structuringChoice?: OpenAIResponsesNativeStructuring.Choice.From<fdm, vdm>;
34
+ }
35
+ interface GoogleNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
36
+ endpoint: string;
37
+ functionDeclarationMap: fdm;
38
+ verbatimDeclarationMap: vdm;
39
+ structuringChoice?: Structuring.Choice.From<fdm, vdm>;
40
+ codeExecution?: boolean;
41
+ urlContext?: boolean;
42
+ googleSearch?: boolean;
43
+ parallelToolCall?: boolean;
44
+ }
14
45
  }
package/build/adaptor.js CHANGED
@@ -1,13 +1,10 @@
1
- import { Config } from '#@/config.ts';
2
- import { Function } from '#@/function.ts';
3
- import {} from '#@/compatible/engine.ts';
4
- import { Throttle } from '#@/throttle.ts';
5
- import { GoogleCompatibleEngine } from '#@/compatible/engine.d/google.ts';
6
- import { OpenAIResponsesCompatibleEngine } from '#@/compatible/engine.d/openai-responses.ts';
7
- import { AnthropicCompatibleEngine } from '#@/compatible/engine.d/anthropic.ts';
8
- import { AliyunEngine } from '#@/compatible/engine.d/aliyun.ts';
9
- import { OpenAIResponsesNativeEngine } from '#@/native-engines.d/openai-responses/engine.ts';
10
- import { GoogleNativeEngine } from '#@/native-engines.d/google/engine.ts';
1
+ import { Throttle } from "./throttle.js";
2
+ import { GoogleCompatibleEngine } from "./compatible-engine.d/google.js";
3
+ import { OpenAIResponsesCompatibleEngine } from "./compatible-engine.d/openai-responses.js";
4
+ import { AnthropicCompatibleEngine } from "./compatible-engine.d/anthropic.js";
5
+ import { AliyunCompatibleEngine } from "./compatible-engine.d/aliyun.js";
6
+ import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses.js";
7
+ import { GoogleNativeEngine } from "./native-engines.d/google.js";
11
8
  export class Adaptor {
12
9
  config;
13
10
  static create(config) {
@@ -21,72 +18,73 @@ export class Adaptor {
21
18
  this.throttles.set(endpointId, new Throttle(rpm));
22
19
  }
23
20
  }
24
- makeEngine(endpoint, functionDeclarationMap, toolChoice, parallelToolCall) {
25
- const endpointSpec = this.config.brainswitch.endpoints[endpoint];
21
+ makeCompatibleEngine(adaptorOptions) {
22
+ const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
26
23
  if (endpointSpec) { }
27
24
  else
28
25
  throw new Error();
29
- const throttle = this.throttles.get(endpoint);
26
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
30
27
  if (throttle) { }
31
28
  else
32
29
  throw new Error();
33
30
  const options = {
34
31
  ...endpointSpec,
35
- functionDeclarationMap,
36
- toolChoice,
37
- parallelToolCall,
32
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
33
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
34
+ structuringChoice: adaptorOptions.structuringChoice,
38
35
  throttle,
39
36
  };
40
37
  if (endpointSpec.apiType === 'openai-responses')
41
- return new OpenAIResponsesCompatibleEngine(options);
38
+ return new OpenAIResponsesCompatibleEngine.Instance(options);
42
39
  else if (endpointSpec.apiType === 'google')
43
- return new GoogleCompatibleEngine(options);
40
+ return new GoogleCompatibleEngine.Instance(options);
44
41
  else if (endpointSpec.apiType === 'aliyun')
45
- return new AliyunEngine(options);
42
+ return new AliyunCompatibleEngine.Instance(options);
46
43
  else if (endpointSpec.apiType === 'anthropic')
47
- return new AnthropicCompatibleEngine(options);
44
+ return new AnthropicCompatibleEngine.Instance(options);
48
45
  else
49
46
  throw new Error();
50
47
  }
51
- makeOpenAIResponsesNativeEngine(endpoint, functionDeclarationMap, applyPatch, toolChoice, parallelToolCall) {
52
- const endpointSpec = this.config.brainswitch.endpoints[endpoint];
48
+ makeOpenAIResponsesNativeEngine(adaptorOptions) {
49
+ const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
53
50
  if (endpointSpec?.apiType === 'openai-responses') { }
54
51
  else
55
52
  throw new Error();
56
- const throttle = this.throttles.get(endpoint);
53
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
57
54
  if (throttle) { }
58
55
  else
59
56
  throw new Error();
60
57
  const options = {
61
58
  ...endpointSpec,
62
- functionDeclarationMap,
63
- toolChoice,
64
- parallelToolCall,
59
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
60
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
61
+ structuringChoice: adaptorOptions.structuringChoice,
65
62
  throttle,
66
- applyPatch,
63
+ applyPatch: adaptorOptions.applyPatch,
67
64
  };
68
- return new OpenAIResponsesNativeEngine(options);
65
+ return new OpenAIResponsesNativeEngine.Instance(options);
69
66
  }
70
- makeGoogleNativeEngine(endpoint, functionDeclarationMap, toolChoice, codeExecution, urlContext, googleSearch, parallelToolCall) {
71
- const endpointSpec = this.config.brainswitch.endpoints[endpoint];
67
+ makeGoogleNativeEngine(adaptorOptions) {
68
+ const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
72
69
  if (endpointSpec?.apiType === 'google') { }
73
70
  else
74
71
  throw new Error();
75
- const throttle = this.throttles.get(endpoint);
72
+ const throttle = this.throttles.get(adaptorOptions.endpoint);
76
73
  if (throttle) { }
77
74
  else
78
75
  throw new Error();
79
76
  const options = {
80
77
  ...endpointSpec,
81
- functionDeclarationMap,
82
- toolChoice,
83
- parallelToolCall,
78
+ functionDeclarationMap: adaptorOptions.functionDeclarationMap,
79
+ verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
80
+ structuringChoice: adaptorOptions.structuringChoice,
81
+ parallelToolCall: adaptorOptions.parallelToolCall,
84
82
  throttle,
85
- codeExecution,
86
- urlContext,
87
- googleSearch,
83
+ codeExecution: adaptorOptions.codeExecution,
84
+ urlContext: adaptorOptions.urlContext,
85
+ googleSearch: adaptorOptions.googleSearch,
88
86
  };
89
- return new GoogleNativeEngine(options);
87
+ return new GoogleNativeEngine.Instance(options);
90
88
  }
91
89
  }
92
90
  //# sourceMappingURL=adaptor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAyB,MAAM,yBAAyB,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,+BAA+B,EAAE,MAAM,4CAA4C,CAAC;AAC7F,OAAO,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,2BAA2B,EAAE,MAAM,gDAAgD,CAAC;AAC7F,OAAO,EAAE,kB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,cAAc,CAAC,iBAAiB;YACnD,gBAAgB,EAAE,cAAc,CAAC,gBAAgB;YACjD,QAAQ;YACR,aAAa,EAAE,cAAc,CAAC,aAAa;YAC3C,UAAU,EAAE,cAAc,CAAC,UAAU;YACrC,YAAY,EAAE,cAAc,CAAC,YAAY;SAC5C,CAAC;QACF,OAAO,IAAI,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;CACJ"}
@@ -1,11 +1,11 @@
1
- import type { Pricing } from '#@/engine.ts';
1
+ import type { Pricing } from '../../engine.ts';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
- export declare class AnthropicBilling {
4
- protected ctx: AnthropicBilling.Context;
5
- constructor(ctx: AnthropicBilling.Context);
3
+ export declare class Billing {
4
+ protected ctx: Billing.Context;
5
+ constructor(ctx: Billing.Context);
6
6
  charge(usage: Anthropic.Usage): number;
7
7
  }
8
- export declare namespace AnthropicBilling {
8
+ export declare namespace Billing {
9
9
  interface Context {
10
10
  pricing: Pricing;
11
11
  }
@@ -1,5 +1,4 @@
1
- import Anthropic from '@anthropic-ai/sdk';
2
- export class AnthropicBilling {
1
+ export class Billing {
3
2
  ctx;
4
3
  constructor(ctx) {
5
4
  this.ctx = ctx;
@@ -1 +1 @@
1
- {"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAG1C,MAAM,OAAO,gBAAgB;IACI;IAA7B,YAA6B,GAA6B;QAA7B,QAAG,GAAH,GAAG,CAA0B;IAAG,CAAC;IAEvD,MAAM,CAAC,KAAsB;QAChC,MAAM,kBAAkB,GAAG,KAAK,CAAC,uBAAuB,IAAI,CAAC,CAAC;QAC9D,MAAM,mBAAmB,GAAG,KAAK,CAAC,YAAY,GAAG,kBAAkB,CAAC;QACpE,OAAO,CACH,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACvD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YACtD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CAC3D,CAAC;IACN,CAAC;CACJ"}
1
+ {"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;IACa;IAA7B,YAA6B,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE9C,MAAM,CAAC,KAAsB;QAChC,MAAM,kBAAkB,GAAG,KAAK,CAAC,uBAAuB,IAAI,CAAC,CAAC;QAC9D,MAAM,mBAAmB,GAAG,KAAK,CAAC,YAAY,GAAG,kBAAkB,CAAC;QACpE,OAAO,CACH,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACvD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YACtD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CAC3D,CAAC;IACN,CAAC;CACJ"}
@@ -1,17 +1,16 @@
1
- import { Function } from '#@/function.ts';
1
+ import { Function } from '../../function.ts';
2
2
  import Anthropic from '@anthropic-ai/sdk';
3
- export declare class AnthropicToolCodec<in out fdm extends Function.Declaration.Map> {
4
- protected ctx: AnthropicToolCodec.Context<fdm>;
5
- constructor(ctx: AnthropicToolCodec.Context<fdm>);
6
- convertFromFunctionCall(fc: Function.Call.From<fdm>): Anthropic.ToolUseBlock;
7
- convertToFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.From<fdm>;
8
- convertFromFunctionResponse(fr: Function.Response.Distributive<fdm>): Anthropic.ToolResultBlockParam;
9
- protected convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Anthropic.Tool;
10
- convertFromFunctionDeclarationMap(fdm: fdm): Anthropic.Tool[];
11
- convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>, parallelToolCall: boolean): Anthropic.ToolChoice;
3
+ export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
4
+ protected ctx: ToolCodec.Context<fdm>;
5
+ constructor(ctx: ToolCodec.Context<fdm>);
6
+ encodeFunctionCall(fc: Function.Call.From<fdm>): Anthropic.ToolUseBlock;
7
+ decodeFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.From<fdm>;
8
+ encodeFunctionResponse(fr: Function.Response.From<fdm>): Anthropic.ToolResultBlockParam;
9
+ protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): Anthropic.Tool;
10
+ encodeFunctionDeclarationMap(fdm: fdm): Anthropic.Tool[];
12
11
  }
13
- export declare namespace AnthropicToolCodec {
14
- interface Context<in out fdm extends Function.Declaration.Map> {
12
+ export declare namespace ToolCodec {
13
+ interface Context<in out fdm extends Function.Decl.Map.Proto> {
15
14
  fdm: fdm;
16
15
  }
17
16
  }
@@ -1,73 +1,58 @@
1
- import { Function } from '#@/function.ts';
2
- import { ResponseInvalid } from '#@/engine.ts';
3
- import Anthropic from '@anthropic-ai/sdk';
1
+ import { Function } from "../../function.js";
2
+ import { ResponseInvalid } from "../../engine.js";
4
3
  import Ajv from 'ajv';
5
- import {} from '@sinclair/typebox';
6
4
  const ajv = new Ajv();
7
- export class AnthropicToolCodec {
5
+ export class ToolCodec {
8
6
  ctx;
9
7
  constructor(ctx) {
10
8
  this.ctx = ctx;
11
9
  }
12
- convertFromFunctionCall(fc) {
10
+ encodeFunctionCall(fc) {
13
11
  throw new Error('Anthropic compatible engine requires native function calls.');
14
12
  }
15
- convertToFunctionCall(apifc) {
13
+ decodeFunctionCall(apifc) {
16
14
  const fditem = this.ctx.fdm[apifc.name];
17
15
  if (fditem) { }
18
16
  else
19
17
  throw new ResponseInvalid('Unknown function call', { cause: apifc });
20
- const args = (() => {
21
- try {
22
- return JSON.parse(apifc.input);
23
- }
24
- catch {
25
- throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
26
- }
27
- })();
28
- if (ajv.validate(fditem.paraschema, args)) { }
18
+ if (ajv.validate(fditem.parameters, apifc.input)) { }
29
19
  else
30
20
  throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
31
- return Function.Call.create({
21
+ return Function.Call.of({
32
22
  id: apifc.id,
33
23
  name: apifc.name,
34
- args,
24
+ args: apifc.input,
35
25
  });
36
26
  }
37
- convertFromFunctionResponse(fr) {
27
+ encodeFunctionResponse(fr) {
38
28
  if (fr.id) { }
39
29
  else
40
30
  throw new Error();
41
- return {
42
- type: 'tool_result',
43
- tool_use_id: fr.id,
44
- content: fr.text,
45
- };
31
+ if (fr instanceof Function.Response.Successful)
32
+ return {
33
+ type: 'tool_result',
34
+ tool_use_id: fr.id,
35
+ content: fr.text,
36
+ };
37
+ else if (fr instanceof Function.Response.Failed)
38
+ return {
39
+ type: 'tool_result',
40
+ tool_use_id: fr.id,
41
+ content: fr.error,
42
+ };
43
+ else
44
+ throw new Error();
46
45
  }
47
- convertFromFunctionDeclarationEntry(fdentry) {
46
+ encodeFunctionDeclarationEntry(fdentry) {
48
47
  return {
49
48
  name: fdentry[0],
50
49
  description: fdentry[1].description,
51
- input_schema: fdentry[1].paraschema,
50
+ input_schema: fdentry[1].parameters,
52
51
  };
53
52
  }
54
- convertFromFunctionDeclarationMap(fdm) {
53
+ encodeFunctionDeclarationMap(fdm) {
55
54
  const fdentries = Object.entries(fdm);
56
- return fdentries.map(fdentry => this.convertFromFunctionDeclarationEntry(fdentry));
57
- }
58
- convertFromToolChoice(toolChoice, parallelToolCall) {
59
- if (toolChoice === Function.ToolChoice.NONE)
60
- return { type: 'none' };
61
- else if (toolChoice === Function.ToolChoice.REQUIRED)
62
- return { type: 'any', disable_parallel_tool_use: !parallelToolCall };
63
- else if (toolChoice === Function.ToolChoice.AUTO)
64
- return { type: 'auto', disable_parallel_tool_use: !parallelToolCall };
65
- else {
66
- if (toolChoice.length === 1) { }
67
- else
68
- throw new Error();
69
- return { type: 'tool', name: toolChoice[0], disable_parallel_tool_use: !parallelToolCall };
70
- }
55
+ return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
71
56
  }
72
57
  }
73
58
  //# sourceMappingURL=tool-codec.js.map