@jangts/agent-core 0.1.1 → 0.2.0

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 (523) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/development/docs/api/reference.md +1907 -1874
  3. package/development/docs/api/support.md +27 -0
  4. package/development/skills/agent-core-app/scripts/create-app.mjs +267 -70
  5. package/development/skills/agent-core-app/scripts/lib/knowledge.mjs +40 -15
  6. package/development/skills/agent-core-app/scripts/lib/source.mjs +30 -2
  7. package/development/skills/agent-core-app/scripts/plan.mjs +37 -12
  8. package/development/skills/agent-core-app/scripts/prepare-source.mjs +61 -10
  9. package/development/skills/agent-core-app/scripts/resources.mjs +45 -17
  10. package/development/skills/agent-core-app/scripts/validate-knowledge.mjs +18 -1
  11. package/development/skills/agent-core-app/templates/catalog.json +36 -22
  12. package/dist/cjs/capabilities/index.js +13 -0
  13. package/dist/cjs/capabilities/invocation.js +80 -10
  14. package/dist/cjs/capabilities/model.js +126 -13
  15. package/dist/cjs/capabilities/tool-runtime.js +64 -12
  16. package/dist/cjs/capabilities/tools.js +266 -31
  17. package/dist/cjs/context/assembly.js +46 -5
  18. package/dist/cjs/context/audit.js +31 -6
  19. package/dist/cjs/context/fragments.js +54 -11
  20. package/dist/cjs/context/index.js +12 -0
  21. package/dist/cjs/context/journal.js +84 -7
  22. package/dist/cjs/context/plan.js +30 -2
  23. package/dist/cjs/diagnostics/catalog.js +11 -3
  24. package/dist/cjs/diagnostics/index.js +149 -29
  25. package/dist/cjs/diagnostics/sub-flow.js +240 -40
  26. package/dist/cjs/errors.js +99 -17
  27. package/dist/cjs/execution/branch.js +20 -4
  28. package/dist/cjs/execution/engine.js +470 -56
  29. package/dist/cjs/execution/events.js +136 -18
  30. package/dist/cjs/execution/index.js +7 -0
  31. package/dist/cjs/execution/reporting.js +75 -6
  32. package/dist/cjs/execution/state.js +161 -22
  33. package/dist/cjs/execution/workflow.js +224 -32
  34. package/dist/cjs/index.js +18 -0
  35. package/dist/cjs/kits/aggregation.js +45 -3
  36. package/dist/cjs/kits/answering/batch.js +23 -4
  37. package/dist/cjs/kits/answering/check-result.js +31 -8
  38. package/dist/cjs/kits/answering/checks.js +24 -3
  39. package/dist/cjs/kits/answering/flow.js +192 -36
  40. package/dist/cjs/kits/answering/index.js +19 -1
  41. package/dist/cjs/kits/answering/tasks.js +88 -13
  42. package/dist/cjs/kits/artifacts.js +36 -4
  43. package/dist/cjs/kits/candidates.js +47 -5
  44. package/dist/cjs/kits/delivery.js +40 -5
  45. package/dist/cjs/kits/durable.js +155 -31
  46. package/dist/cjs/kits/evidence.js +66 -15
  47. package/dist/cjs/kits/family.js +152 -22
  48. package/dist/cjs/kits/index.js +12 -1
  49. package/dist/cjs/kits/inference.js +104 -16
  50. package/dist/cjs/kits/intents/catalog.js +67 -16
  51. package/dist/cjs/kits/intents/index.js +11 -0
  52. package/dist/cjs/kits/intents/registry.js +188 -43
  53. package/dist/cjs/kits/intents/scopes.js +215 -46
  54. package/dist/cjs/kits/interaction/command.js +140 -36
  55. package/dist/cjs/kits/interaction/commands.js +86 -11
  56. package/dist/cjs/kits/interaction/handlers.js +27 -10
  57. package/dist/cjs/kits/interaction/index.js +10 -0
  58. package/dist/cjs/kits/interaction/transfer.js +80 -14
  59. package/dist/cjs/kits/knowledge/catalog.js +62 -11
  60. package/dist/cjs/kits/knowledge/index.js +16 -1
  61. package/dist/cjs/kits/knowledge/publication.js +99 -23
  62. package/dist/cjs/kits/knowledge/sections.js +33 -4
  63. package/dist/cjs/kits/normalization.js +51 -7
  64. package/dist/cjs/kits/policies.js +79 -15
  65. package/dist/cjs/kits/presentation/events.js +54 -10
  66. package/dist/cjs/kits/presentation/index.js +24 -1
  67. package/dist/cjs/kits/presentation/outputs.js +176 -38
  68. package/dist/cjs/kits/presentation/templates.js +33 -5
  69. package/dist/cjs/kits/presentation/variants.js +31 -2
  70. package/dist/cjs/kits/projection.js +63 -8
  71. package/dist/cjs/kits/rank-fusion.js +27 -4
  72. package/dist/cjs/kits/reflection-log-source.js +89 -16
  73. package/dist/cjs/kits/reflection.js +234 -60
  74. package/dist/cjs/kits/retrieval.js +98 -18
  75. package/dist/cjs/kits/search.js +180 -27
  76. package/dist/cjs/kits/text-skills.js +104 -19
  77. package/dist/cjs/kits/tools.js +415 -74
  78. package/dist/cjs/kits/vector-search.js +50 -16
  79. package/dist/cjs/mock/node.js +7 -1
  80. package/dist/cjs/mock/web.js +7 -1
  81. package/dist/cjs/mock.js +7 -1
  82. package/dist/cjs/model/binding.js +68 -17
  83. package/dist/cjs/model/call-budget.js +45 -4
  84. package/dist/cjs/model/decision-retry.js +19 -1
  85. package/dist/cjs/model/defaults.js +27 -4
  86. package/dist/cjs/model/index.js +7 -0
  87. package/dist/cjs/model/interactions/arguments.js +36 -1
  88. package/dist/cjs/model/interactions/json-object.js +48 -12
  89. package/dist/cjs/model/interactions/native-functions.js +52 -10
  90. package/dist/cjs/model/message-diagnostics.js +17 -4
  91. package/dist/cjs/model/options.js +9 -1
  92. package/dist/cjs/model/preparation.js +86 -12
  93. package/dist/cjs/model/runner.js +413 -79
  94. package/dist/cjs/model/selection-transport.js +48 -8
  95. package/dist/cjs/model/settings.js +67 -13
  96. package/dist/cjs/model/token-budget.js +69 -11
  97. package/dist/cjs/model/transports.js +59 -6
  98. package/dist/cjs/structs/common.js +11 -0
  99. package/dist/cjs/structs/debug.js +34 -5
  100. package/dist/cjs/structs/errors.js +26 -1
  101. package/dist/cjs/structs/index.js +11 -1
  102. package/dist/cjs/structs/kits/durable.js +7 -0
  103. package/dist/cjs/structs/kits/family.js +1 -0
  104. package/dist/cjs/structs/kits/interaction.js +8 -1
  105. package/dist/cjs/structs/kits/knowledge-publication.js +1 -0
  106. package/dist/cjs/structs/kits/reflection.js +18 -3
  107. package/dist/cjs/structs/model/chat-provider.js +6 -0
  108. package/dist/cjs/structs/support/model/completion.js +2 -0
  109. package/dist/cjs/structs/support/model/messages.js +2 -0
  110. package/dist/cjs/support/capabilities.js +43 -6
  111. package/dist/cjs/support/context/assembly.js +88 -16
  112. package/dist/cjs/support/context/compression.js +48 -6
  113. package/dist/cjs/support/context/fragments.js +6 -0
  114. package/dist/cjs/support/context/history.js +36 -1
  115. package/dist/cjs/support/context/index.js +11 -0
  116. package/dist/cjs/support/context/nodes.js +164 -35
  117. package/dist/cjs/support/context/processing.js +319 -61
  118. package/dist/cjs/support/context/selection-policy.js +98 -21
  119. package/dist/cjs/support/context/selection.js +61 -10
  120. package/dist/cjs/support/context/snapshot.js +29 -7
  121. package/dist/cjs/support/context/strategies.js +120 -28
  122. package/dist/cjs/support/index.js +9 -1
  123. package/dist/cjs/support/model/capabilities.js +20 -1
  124. package/dist/cjs/support/model/chat-completions.js +92 -20
  125. package/dist/cjs/support/model/children.js +34 -6
  126. package/dist/cjs/support/model/compatible-provider.js +189 -59
  127. package/dist/cjs/support/model/completion/calls.js +61 -0
  128. package/dist/cjs/support/model/completion/default.js +32 -0
  129. package/dist/cjs/support/model/completion/index.js +28 -0
  130. package/dist/cjs/support/model/completion/session.js +102 -0
  131. package/dist/cjs/support/model/index.js +12 -0
  132. package/dist/cjs/support/model/interactions/index.js +12 -1
  133. package/dist/cjs/support/model/interactions/json-object.js +6 -0
  134. package/dist/cjs/support/model/interactions/native-functions.js +6 -0
  135. package/dist/cjs/support/model/observer.js +32 -6
  136. package/dist/cjs/support/model/preview-rules.js +66 -16
  137. package/dist/cjs/support/model/reply-preview.js +77 -17
  138. package/dist/cjs/support/model/stage-transport.js +33 -7
  139. package/dist/cjs/support/model/strategies.js +75 -14
  140. package/dist/cjs/support/model/transports.js +53 -11
  141. package/dist/cjs/support/registration.js +49 -7
  142. package/dist/cjs/support/schema.js +46 -4
  143. package/dist/cjs/support/stream-events.js +60 -8
  144. package/dist/cjs/support/tool-runtime.js +6 -0
  145. package/dist/cjs/support/tools.js +79 -10
  146. package/dist/cjs/support/transport/index.js +7 -0
  147. package/dist/cjs/support/transport/json.js +28 -4
  148. package/dist/cjs/support/transport/sse.js +25 -2
  149. package/dist/cjs/utils/break-observer.js +20 -2
  150. package/dist/cjs/utils/cancellation.js +77 -9
  151. package/dist/cjs/utils/catalog.js +19 -4
  152. package/dist/cjs/utils/debug.js +14 -2
  153. package/dist/cjs/utils/index.js +8 -0
  154. package/dist/cjs/utils/interaction-definition.js +26 -6
  155. package/dist/cjs/utils/invocation.js +42 -4
  156. package/dist/cjs/utils/model-protocol.js +20 -1
  157. package/dist/cjs/utils/object.js +16 -0
  158. package/dist/cjs/utils/observer.js +22 -2
  159. package/dist/cjs/utils/protocol-registry.js +47 -4
  160. package/dist/cjs/utils/query-key.js +21 -2
  161. package/dist/cjs/utils/registration.js +18 -2
  162. package/dist/cjs/utils/rules.js +39 -11
  163. package/dist/cjs/utils/schema-adapter.js +25 -2
  164. package/dist/cjs/utils/synchronous.js +64 -7
  165. package/dist/cjs/utils/text.js +15 -1
  166. package/dist/esm/capabilities/index.d.ts +13 -0
  167. package/dist/esm/capabilities/index.js +13 -0
  168. package/dist/esm/capabilities/invocation.d.ts +16 -2
  169. package/dist/esm/capabilities/invocation.js +80 -10
  170. package/dist/esm/capabilities/model.d.ts +68 -1
  171. package/dist/esm/capabilities/model.js +126 -13
  172. package/dist/esm/capabilities/tool-runtime.d.ts +11 -0
  173. package/dist/esm/capabilities/tool-runtime.js +64 -12
  174. package/dist/esm/capabilities/tools.d.ts +96 -3
  175. package/dist/esm/capabilities/tools.js +267 -32
  176. package/dist/esm/context/assembly.d.ts +13 -2
  177. package/dist/esm/context/assembly.js +46 -5
  178. package/dist/esm/context/audit.d.ts +23 -1
  179. package/dist/esm/context/audit.js +31 -6
  180. package/dist/esm/context/fragments.d.ts +37 -3
  181. package/dist/esm/context/fragments.js +54 -11
  182. package/dist/esm/context/index.d.ts +12 -0
  183. package/dist/esm/context/index.js +12 -0
  184. package/dist/esm/context/journal.d.ts +32 -1
  185. package/dist/esm/context/journal.js +84 -7
  186. package/dist/esm/context/plan.d.ts +11 -1
  187. package/dist/esm/context/plan.js +30 -2
  188. package/dist/esm/diagnostics/catalog.js +11 -3
  189. package/dist/esm/diagnostics/index.d.ts +30 -2
  190. package/dist/esm/diagnostics/index.js +149 -29
  191. package/dist/esm/diagnostics/sub-flow.d.ts +41 -3
  192. package/dist/esm/diagnostics/sub-flow.js +240 -40
  193. package/dist/esm/errors.d.ts +22 -2
  194. package/dist/esm/errors.js +99 -17
  195. package/dist/esm/execution/branch.d.ts +19 -1
  196. package/dist/esm/execution/branch.js +20 -4
  197. package/dist/esm/execution/engine.d.ts +291 -5
  198. package/dist/esm/execution/engine.js +471 -57
  199. package/dist/esm/execution/events.d.ts +16 -2
  200. package/dist/esm/execution/events.js +136 -18
  201. package/dist/esm/execution/index.d.ts +7 -0
  202. package/dist/esm/execution/index.js +7 -0
  203. package/dist/esm/execution/reporting.d.ts +49 -1
  204. package/dist/esm/execution/reporting.js +75 -6
  205. package/dist/esm/execution/state.d.ts +126 -10
  206. package/dist/esm/execution/state.js +161 -22
  207. package/dist/esm/execution/workflow.d.ts +121 -3
  208. package/dist/esm/execution/workflow.js +224 -32
  209. package/dist/esm/index.d.ts +17 -1
  210. package/dist/esm/index.js +18 -0
  211. package/dist/esm/kits/aggregation.d.ts +34 -2
  212. package/dist/esm/kits/aggregation.js +45 -3
  213. package/dist/esm/kits/answering/batch.d.ts +17 -0
  214. package/dist/esm/kits/answering/batch.js +23 -4
  215. package/dist/esm/kits/answering/check-result.d.ts +18 -1
  216. package/dist/esm/kits/answering/check-result.js +31 -8
  217. package/dist/esm/kits/answering/checks.d.ts +11 -0
  218. package/dist/esm/kits/answering/checks.js +24 -3
  219. package/dist/esm/kits/answering/flow.d.ts +25 -2
  220. package/dist/esm/kits/answering/flow.js +192 -36
  221. package/dist/esm/kits/answering/index.d.ts +12 -0
  222. package/dist/esm/kits/answering/index.js +19 -1
  223. package/dist/esm/kits/answering/tasks.d.ts +32 -1
  224. package/dist/esm/kits/answering/tasks.js +88 -13
  225. package/dist/esm/kits/artifacts.d.ts +11 -0
  226. package/dist/esm/kits/artifacts.js +36 -4
  227. package/dist/esm/kits/candidates.d.ts +23 -1
  228. package/dist/esm/kits/candidates.js +47 -5
  229. package/dist/esm/kits/delivery.d.ts +34 -0
  230. package/dist/esm/kits/delivery.js +40 -5
  231. package/dist/esm/kits/durable.d.ts +19 -0
  232. package/dist/esm/kits/durable.js +155 -31
  233. package/dist/esm/kits/evidence.d.ts +28 -5
  234. package/dist/esm/kits/evidence.js +66 -15
  235. package/dist/esm/kits/family.d.ts +17 -1
  236. package/dist/esm/kits/family.js +152 -22
  237. package/dist/esm/kits/index.d.ts +12 -1
  238. package/dist/esm/kits/index.js +12 -1
  239. package/dist/esm/kits/inference.d.ts +64 -0
  240. package/dist/esm/kits/inference.js +104 -16
  241. package/dist/esm/kits/intents/catalog.d.ts +13 -1
  242. package/dist/esm/kits/intents/catalog.js +67 -16
  243. package/dist/esm/kits/intents/index.d.ts +11 -0
  244. package/dist/esm/kits/intents/index.js +11 -0
  245. package/dist/esm/kits/intents/registry.d.ts +61 -3
  246. package/dist/esm/kits/intents/registry.js +188 -43
  247. package/dist/esm/kits/intents/scopes.d.ts +73 -3
  248. package/dist/esm/kits/intents/scopes.js +215 -46
  249. package/dist/esm/kits/interaction/command.d.ts +38 -4
  250. package/dist/esm/kits/interaction/command.js +140 -36
  251. package/dist/esm/kits/interaction/commands.d.ts +66 -2
  252. package/dist/esm/kits/interaction/commands.js +86 -11
  253. package/dist/esm/kits/interaction/handlers.d.ts +19 -2
  254. package/dist/esm/kits/interaction/handlers.js +27 -10
  255. package/dist/esm/kits/interaction/index.d.ts +10 -0
  256. package/dist/esm/kits/interaction/index.js +10 -0
  257. package/dist/esm/kits/interaction/transfer.d.ts +39 -2
  258. package/dist/esm/kits/interaction/transfer.js +80 -14
  259. package/dist/esm/kits/knowledge/catalog.d.ts +13 -0
  260. package/dist/esm/kits/knowledge/catalog.js +62 -11
  261. package/dist/esm/kits/knowledge/index.d.ts +11 -0
  262. package/dist/esm/kits/knowledge/index.js +16 -1
  263. package/dist/esm/kits/knowledge/publication.d.ts +15 -1
  264. package/dist/esm/kits/knowledge/publication.js +99 -23
  265. package/dist/esm/kits/knowledge/sections.d.ts +31 -1
  266. package/dist/esm/kits/knowledge/sections.js +33 -4
  267. package/dist/esm/kits/normalization.d.ts +25 -2
  268. package/dist/esm/kits/normalization.js +51 -7
  269. package/dist/esm/kits/policies.d.ts +26 -3
  270. package/dist/esm/kits/policies.js +79 -15
  271. package/dist/esm/kits/presentation/events.d.ts +51 -4
  272. package/dist/esm/kits/presentation/events.js +54 -10
  273. package/dist/esm/kits/presentation/index.d.ts +12 -0
  274. package/dist/esm/kits/presentation/index.js +25 -2
  275. package/dist/esm/kits/presentation/outputs.d.ts +30 -1
  276. package/dist/esm/kits/presentation/outputs.js +176 -38
  277. package/dist/esm/kits/presentation/templates.d.ts +32 -3
  278. package/dist/esm/kits/presentation/templates.js +33 -5
  279. package/dist/esm/kits/presentation/variants.d.ts +29 -0
  280. package/dist/esm/kits/presentation/variants.js +31 -2
  281. package/dist/esm/kits/projection.d.ts +42 -5
  282. package/dist/esm/kits/projection.js +63 -8
  283. package/dist/esm/kits/rank-fusion.d.ts +21 -1
  284. package/dist/esm/kits/rank-fusion.js +28 -5
  285. package/dist/esm/kits/reflection-log-source.d.ts +28 -1
  286. package/dist/esm/kits/reflection-log-source.js +89 -16
  287. package/dist/esm/kits/reflection.d.ts +17 -1
  288. package/dist/esm/kits/reflection.js +235 -61
  289. package/dist/esm/kits/retrieval.d.ts +18 -2
  290. package/dist/esm/kits/retrieval.js +98 -18
  291. package/dist/esm/kits/search.d.ts +56 -3
  292. package/dist/esm/kits/search.js +180 -27
  293. package/dist/esm/kits/text-skills.d.ts +16 -2
  294. package/dist/esm/kits/text-skills.js +104 -19
  295. package/dist/esm/kits/tools.d.ts +122 -2
  296. package/dist/esm/kits/tools.js +416 -74
  297. package/dist/esm/kits/vector-search.d.ts +47 -1
  298. package/dist/esm/kits/vector-search.js +51 -17
  299. package/dist/esm/mock/node.d.ts +7 -0
  300. package/dist/esm/mock/node.js +7 -1
  301. package/dist/esm/mock/web.d.ts +7 -0
  302. package/dist/esm/mock/web.js +7 -1
  303. package/dist/esm/mock.d.ts +7 -0
  304. package/dist/esm/mock.js +7 -1
  305. package/dist/esm/model/binding.d.ts +32 -3
  306. package/dist/esm/model/binding.js +68 -17
  307. package/dist/esm/model/call-budget.d.ts +30 -2
  308. package/dist/esm/model/call-budget.js +45 -4
  309. package/dist/esm/model/decision-retry.d.ts +10 -1
  310. package/dist/esm/model/decision-retry.js +19 -1
  311. package/dist/esm/model/defaults.d.ts +13 -2
  312. package/dist/esm/model/defaults.js +27 -4
  313. package/dist/esm/model/index.d.ts +7 -0
  314. package/dist/esm/model/index.js +7 -0
  315. package/dist/esm/model/interactions/arguments.d.ts +27 -0
  316. package/dist/esm/model/interactions/arguments.js +36 -1
  317. package/dist/esm/model/interactions/json-object.d.ts +7 -1
  318. package/dist/esm/model/interactions/json-object.js +48 -12
  319. package/dist/esm/model/interactions/native-functions.d.ts +6 -0
  320. package/dist/esm/model/interactions/native-functions.js +52 -10
  321. package/dist/esm/model/message-diagnostics.d.ts +17 -1
  322. package/dist/esm/model/message-diagnostics.js +17 -4
  323. package/dist/esm/model/options.d.ts +18 -1
  324. package/dist/esm/model/options.js +9 -1
  325. package/dist/esm/model/preparation.d.ts +30 -2
  326. package/dist/esm/model/preparation.js +86 -12
  327. package/dist/esm/model/runner.d.ts +129 -5
  328. package/dist/esm/model/runner.js +414 -80
  329. package/dist/esm/model/selection-transport.d.ts +14 -1
  330. package/dist/esm/model/selection-transport.js +48 -8
  331. package/dist/esm/model/settings.d.ts +11 -0
  332. package/dist/esm/model/settings.js +67 -13
  333. package/dist/esm/model/token-budget.d.ts +59 -1
  334. package/dist/esm/model/token-budget.js +69 -11
  335. package/dist/esm/model/transports.d.ts +20 -2
  336. package/dist/esm/model/transports.js +59 -6
  337. package/dist/esm/structs/capabilities/invocation.d.ts +36 -0
  338. package/dist/esm/structs/capabilities/model.d.ts +27 -0
  339. package/dist/esm/structs/capabilities/tools.d.ts +38 -0
  340. package/dist/esm/structs/common.d.ts +62 -2
  341. package/dist/esm/structs/common.js +11 -0
  342. package/dist/esm/structs/context/fragments.d.ts +28 -0
  343. package/dist/esm/structs/context/journal.d.ts +28 -3
  344. package/dist/esm/structs/context/processing.d.ts +11 -0
  345. package/dist/esm/structs/debug.d.ts +58 -1
  346. package/dist/esm/structs/debug.js +34 -5
  347. package/dist/esm/structs/diagnostics.d.ts +119 -1
  348. package/dist/esm/structs/errors.d.ts +42 -1
  349. package/dist/esm/structs/errors.js +26 -1
  350. package/dist/esm/structs/execution/engine.d.ts +24 -1
  351. package/dist/esm/structs/execution/events.d.ts +12 -0
  352. package/dist/esm/structs/execution/workflow.d.ts +71 -0
  353. package/dist/esm/structs/index.d.ts +11 -1
  354. package/dist/esm/structs/index.js +11 -1
  355. package/dist/esm/structs/invocation.d.ts +30 -0
  356. package/dist/esm/structs/kits/aggregation.d.ts +12 -0
  357. package/dist/esm/structs/kits/answering/checks.d.ts +11 -0
  358. package/dist/esm/structs/kits/answering/flow.d.ts +35 -1
  359. package/dist/esm/structs/kits/answering/state.d.ts +21 -0
  360. package/dist/esm/structs/kits/answering/tasks.d.ts +21 -0
  361. package/dist/esm/structs/kits/artifacts.d.ts +14 -0
  362. package/dist/esm/structs/kits/candidates.d.ts +19 -0
  363. package/dist/esm/structs/kits/delivery.d.ts +22 -0
  364. package/dist/esm/structs/kits/durable.d.ts +56 -3
  365. package/dist/esm/structs/kits/durable.js +7 -0
  366. package/dist/esm/structs/kits/evidence.d.ts +23 -0
  367. package/dist/esm/structs/kits/family.d.ts +49 -1
  368. package/dist/esm/structs/kits/family.js +1 -0
  369. package/dist/esm/structs/kits/intents/catalog.d.ts +26 -0
  370. package/dist/esm/structs/kits/intents/registry.d.ts +34 -0
  371. package/dist/esm/structs/kits/intents/scopes.d.ts +36 -1
  372. package/dist/esm/structs/kits/interaction.d.ts +64 -0
  373. package/dist/esm/structs/kits/interaction.js +8 -1
  374. package/dist/esm/structs/kits/knowledge-publication.d.ts +43 -1
  375. package/dist/esm/structs/kits/knowledge-publication.js +1 -0
  376. package/dist/esm/structs/kits/knowledge.d.ts +39 -2
  377. package/dist/esm/structs/kits/normalization.d.ts +15 -0
  378. package/dist/esm/structs/kits/policies.d.ts +25 -1
  379. package/dist/esm/structs/kits/presentation/events.d.ts +28 -4
  380. package/dist/esm/structs/kits/presentation/outputs.d.ts +38 -1
  381. package/dist/esm/structs/kits/presentation/templates.d.ts +15 -0
  382. package/dist/esm/structs/kits/projection.d.ts +21 -0
  383. package/dist/esm/structs/kits/reflection.d.ts +61 -2
  384. package/dist/esm/structs/kits/reflection.js +18 -3
  385. package/dist/esm/structs/kits/retrieval.d.ts +38 -0
  386. package/dist/esm/structs/kits/search.d.ts +22 -0
  387. package/dist/esm/structs/kits/text-skills.d.ts +36 -0
  388. package/dist/esm/structs/kits/tools.d.ts +87 -0
  389. package/dist/esm/structs/model/chat-provider.d.ts +24 -1
  390. package/dist/esm/structs/model/chat-provider.js +6 -0
  391. package/dist/esm/structs/model/interactions.d.ts +38 -0
  392. package/dist/esm/structs/model/messages.d.ts +30 -0
  393. package/dist/esm/structs/model/runtime.d.ts +104 -6
  394. package/dist/esm/structs/support/capabilities.d.ts +19 -2
  395. package/dist/esm/structs/support/context-processing.d.ts +88 -9
  396. package/dist/esm/structs/support/context.d.ts +29 -1
  397. package/dist/esm/structs/support/model/completion.d.ts +50 -0
  398. package/dist/esm/structs/support/model/completion.js +1 -0
  399. package/dist/esm/structs/support/model/interactions.d.ts +13 -1
  400. package/dist/esm/structs/support/model/messages.d.ts +52 -0
  401. package/dist/esm/structs/support/model/messages.js +1 -0
  402. package/dist/esm/structs/support/model/provider-observer.d.ts +22 -1
  403. package/dist/esm/structs/support/model/provider.d.ts +71 -6
  404. package/dist/esm/structs/support/registration.d.ts +13 -0
  405. package/dist/esm/structs/support/schema.d.ts +14 -0
  406. package/dist/esm/structs/support/tools.d.ts +28 -0
  407. package/dist/esm/structs/support/transport/json.d.ts +16 -0
  408. package/dist/esm/structs/support/transport/sse.d.ts +15 -0
  409. package/dist/esm/structs/utils/rules.d.ts +19 -0
  410. package/dist/esm/support/capabilities.d.ts +25 -2
  411. package/dist/esm/support/capabilities.js +43 -6
  412. package/dist/esm/support/context/assembly.d.ts +35 -4
  413. package/dist/esm/support/context/assembly.js +88 -16
  414. package/dist/esm/support/context/compression.d.ts +17 -1
  415. package/dist/esm/support/context/compression.js +48 -6
  416. package/dist/esm/support/context/fragments.d.ts +6 -0
  417. package/dist/esm/support/context/fragments.js +6 -0
  418. package/dist/esm/support/context/history.d.ts +43 -0
  419. package/dist/esm/support/context/history.js +36 -1
  420. package/dist/esm/support/context/index.d.ts +13 -2
  421. package/dist/esm/support/context/index.js +12 -1
  422. package/dist/esm/support/context/nodes.d.ts +49 -3
  423. package/dist/esm/support/context/nodes.js +165 -36
  424. package/dist/esm/support/context/processing.d.ts +145 -1
  425. package/dist/esm/support/context/processing.js +320 -62
  426. package/dist/esm/support/context/selection-policy.d.ts +42 -2
  427. package/dist/esm/support/context/selection-policy.js +98 -21
  428. package/dist/esm/support/context/selection.d.ts +14 -1
  429. package/dist/esm/support/context/selection.js +61 -10
  430. package/dist/esm/support/context/snapshot.d.ts +23 -1
  431. package/dist/esm/support/context/snapshot.js +29 -7
  432. package/dist/esm/support/context/strategies.d.ts +35 -2
  433. package/dist/esm/support/context/strategies.js +120 -28
  434. package/dist/esm/support/index.d.ts +9 -1
  435. package/dist/esm/support/index.js +9 -1
  436. package/dist/esm/support/model/capabilities.d.ts +10 -0
  437. package/dist/esm/support/model/capabilities.js +20 -1
  438. package/dist/esm/support/model/chat-completions.d.ts +44 -15
  439. package/dist/esm/support/model/chat-completions.js +92 -20
  440. package/dist/esm/support/model/children.d.ts +32 -2
  441. package/dist/esm/support/model/children.js +34 -6
  442. package/dist/esm/support/model/compatible-provider.d.ts +29 -3
  443. package/dist/esm/support/model/compatible-provider.js +190 -60
  444. package/dist/esm/support/model/completion/calls.d.ts +40 -0
  445. package/dist/esm/support/model/completion/calls.js +58 -0
  446. package/dist/esm/support/model/completion/default.d.ts +10 -0
  447. package/dist/esm/support/model/completion/default.js +29 -0
  448. package/dist/esm/support/model/completion/index.d.ts +10 -0
  449. package/dist/esm/support/model/completion/index.js +22 -0
  450. package/dist/esm/support/model/completion/session.d.ts +11 -0
  451. package/dist/esm/support/model/completion/session.js +99 -0
  452. package/dist/esm/support/model/index.d.ts +14 -0
  453. package/dist/esm/support/model/index.js +12 -0
  454. package/dist/esm/support/model/interactions/index.d.ts +1 -1
  455. package/dist/esm/support/model/interactions/index.js +12 -1
  456. package/dist/esm/support/model/interactions/json-object.d.ts +6 -0
  457. package/dist/esm/support/model/interactions/json-object.js +6 -0
  458. package/dist/esm/support/model/interactions/native-functions.d.ts +6 -0
  459. package/dist/esm/support/model/interactions/native-functions.js +6 -0
  460. package/dist/esm/support/model/observer.d.ts +25 -2
  461. package/dist/esm/support/model/observer.js +32 -6
  462. package/dist/esm/support/model/preview-rules.d.ts +20 -2
  463. package/dist/esm/support/model/preview-rules.js +66 -16
  464. package/dist/esm/support/model/reply-preview.d.ts +15 -1
  465. package/dist/esm/support/model/reply-preview.js +77 -17
  466. package/dist/esm/support/model/stage-transport.d.ts +9 -1
  467. package/dist/esm/support/model/stage-transport.js +33 -7
  468. package/dist/esm/support/model/strategies.d.ts +24 -2
  469. package/dist/esm/support/model/strategies.js +75 -14
  470. package/dist/esm/support/model/transports.d.ts +17 -1
  471. package/dist/esm/support/model/transports.js +53 -11
  472. package/dist/esm/support/registration.d.ts +20 -1
  473. package/dist/esm/support/registration.js +49 -7
  474. package/dist/esm/support/schema.d.ts +31 -1
  475. package/dist/esm/support/schema.js +46 -4
  476. package/dist/esm/support/stream-events.d.ts +22 -1
  477. package/dist/esm/support/stream-events.js +60 -8
  478. package/dist/esm/support/tool-runtime.d.ts +6 -0
  479. package/dist/esm/support/tool-runtime.js +6 -0
  480. package/dist/esm/support/tools.d.ts +22 -0
  481. package/dist/esm/support/tools.js +79 -10
  482. package/dist/esm/support/transport/index.d.ts +7 -0
  483. package/dist/esm/support/transport/index.js +7 -0
  484. package/dist/esm/support/transport/json.d.ts +29 -2
  485. package/dist/esm/support/transport/json.js +28 -4
  486. package/dist/esm/support/transport/sse.d.ts +31 -1
  487. package/dist/esm/support/transport/sse.js +25 -2
  488. package/dist/esm/types.d.ts +9 -1
  489. package/dist/esm/utils/break-observer.d.ts +27 -2
  490. package/dist/esm/utils/break-observer.js +20 -2
  491. package/dist/esm/utils/cancellation.d.ts +56 -5
  492. package/dist/esm/utils/cancellation.js +77 -9
  493. package/dist/esm/utils/catalog.d.ts +20 -1
  494. package/dist/esm/utils/catalog.js +19 -4
  495. package/dist/esm/utils/debug.d.ts +20 -1
  496. package/dist/esm/utils/debug.js +14 -2
  497. package/dist/esm/utils/index.d.ts +8 -0
  498. package/dist/esm/utils/index.js +8 -0
  499. package/dist/esm/utils/interaction-definition.d.ts +18 -1
  500. package/dist/esm/utils/interaction-definition.js +26 -6
  501. package/dist/esm/utils/invocation.d.ts +36 -1
  502. package/dist/esm/utils/invocation.js +42 -4
  503. package/dist/esm/utils/model-protocol.d.ts +8 -0
  504. package/dist/esm/utils/model-protocol.js +20 -1
  505. package/dist/esm/utils/object.d.ts +16 -0
  506. package/dist/esm/utils/object.js +16 -0
  507. package/dist/esm/utils/observer.d.ts +19 -1
  508. package/dist/esm/utils/observer.js +22 -2
  509. package/dist/esm/utils/protocol-registry.d.ts +38 -1
  510. package/dist/esm/utils/protocol-registry.js +47 -4
  511. package/dist/esm/utils/query-key.d.ts +11 -1
  512. package/dist/esm/utils/query-key.js +21 -2
  513. package/dist/esm/utils/registration.d.ts +34 -2
  514. package/dist/esm/utils/registration.js +18 -2
  515. package/dist/esm/utils/rules.d.ts +81 -4
  516. package/dist/esm/utils/rules.js +39 -11
  517. package/dist/esm/utils/schema-adapter.d.ts +12 -1
  518. package/dist/esm/utils/schema-adapter.js +25 -2
  519. package/dist/esm/utils/synchronous.d.ts +60 -4
  520. package/dist/esm/utils/synchronous.js +64 -7
  521. package/dist/esm/utils/text.d.ts +15 -1
  522. package/dist/esm/utils/text.js +15 -1
  523. package/package.json +3 -2
@@ -1,3 +1,15 @@
1
+ /**
2
+ * Model runner: the single path from prepared input to a validated result.
3
+ *
4
+ * This module is the model-facing protocol boundary. It owns, in strict order:
5
+ * cancellation, registration and budget checks, input measurement (characters, schema
6
+ * and tokens), budget charging, streaming through the transport's collector, then the
7
+ * journal entry, call trace and terminal event. Nothing here knows business prompts,
8
+ * stages or provider SDKs; those are registered by the App.
9
+ *
10
+ * Exports: `ModelRunner`, the re-exported `BudgetError`, and the runtime/config type
11
+ * re-exports at the bottom of the file.
12
+ */
1
13
  import { executionEvents } from '../execution/events.js';
2
14
  import { ModelCallBudget, parentModelBudget } from './call-budget.js';
3
15
  import { synchronous } from '../utils/synchronous.js';
@@ -5,7 +17,7 @@ import { awaitWithSignal, combineSignals, isTimeoutReason } from '../utils/cance
5
17
  import { invocationId, invocationResult } from '../utils/invocation.js';
6
18
  import { defaultTextTransport, modelRunnerDefaults } from './defaults.js';
7
19
  import { breakWhile } from '../utils/rules.js';
8
- import { compiledModelBinding, modelBindingId, snapshotModelBinding } from './binding.js';
20
+ import { compiledModelBinding, modelBindingId, snapshotModelBinding, } from './binding.js';
9
21
  import { prepareModelTransport, preparedInputSize } from './preparation.js';
10
22
  import { getErrorInfo, recordErrorContext } from '../errors.js';
11
23
  import { modelInputTokenLimit, countModelInput, enforceModelInputTokens } from './token-budget.js';
@@ -18,19 +30,46 @@ import { ContextJournal } from '../context/journal.js';
18
30
  import { commonPrefixLength } from '../utils/text.js';
19
31
  import { modelProtocolError } from '../utils/model-protocol.js';
20
32
  import { abortableStream } from '@jangts/streaming/abortable-stream';
33
+ // A context plan has no `messages` key yet; a built context carries already assembled ones.
21
34
  const isContextPlan = (input) => !Array.isArray(input) && !('messages' in input);
22
35
  const isMessageArray = (input) => Array.isArray(input);
36
+ /**
37
+ * Validate a requested completion size.
38
+ *
39
+ * @param tokens - Requested output tokens.
40
+ * @param stageId - Stage used for error context.
41
+ * @throws TypeError When `tokens` is not a positive safe integer.
42
+ */
23
43
  function validateOutputTokens(tokens, stageId) {
24
- breakWhile(!Number.isSafeInteger(tokens) || tokens < 1, () => recordErrorContext(new TypeError('maxOutputTokens must be a positive safe integer'), { stageId, codes: ['MODEL_CONFIG'], isRetryable: false }));
44
+ breakWhile(!Number.isSafeInteger(tokens) || tokens < 1, () => recordErrorContext(new TypeError('maxOutputTokens must be a positive safe integer'), {
45
+ stageId,
46
+ codes: ['MODEL_CONFIG'],
47
+ isRetryable: false,
48
+ }));
25
49
  }
26
50
  export { BudgetError } from '../structs/errors.js';
27
- /** No business prompts, domain stages or provider SDKs are built into this runner. */
51
+ /**
52
+ * Execute registered model stages under one request's budgets and lifetime.
53
+ *
54
+ * Invariants: at most one call is in flight per runner; `callCount` and `journal` are
55
+ * local to the runner while `totalCallCount` includes descendants; every call is charged
56
+ * to the shared budget before I/O and every failure is annotated with its stage before it
57
+ * propagates. No business prompts, domain stages or provider SDKs are built into this
58
+ * runner.
59
+ */
28
60
  export class ModelRunner {
61
+ /** Record of parsed results for this runner; descendants keep their own journals. */
29
62
  journal = new ContextJournal();
30
63
  completedCallCount = 0;
31
64
  activeCall;
32
- get callCount() { return this.completedCallCount; }
33
- get lastCall() { return this.activeCall ? Object.freeze(structuredClone(this.activeCall)) : undefined; }
65
+ /** Completed calls observed by this runner only. */
66
+ get callCount() {
67
+ return this.completedCallCount;
68
+ }
69
+ /** Detached copy of the most recent call trace, or undefined before the first call. */
70
+ get lastCall() {
71
+ return this.activeCall ? Object.freeze(structuredClone(this.activeCall)) : undefined;
72
+ }
34
73
  priorInputs = new Map();
35
74
  tokenBudget;
36
75
  registeredTransports = new Map();
@@ -48,26 +87,60 @@ export class ModelRunner {
48
87
  depth = 0;
49
88
  inFlight = false;
50
89
  requestGate;
51
- get childLimits() { return this.requestGate.limits; }
90
+ /** Limits shared by every runner of one request; children only read them. */
91
+ get childLimits() {
92
+ return this.requestGate.limits;
93
+ }
52
94
  /** Includes descendant attempts; local callCount and journal remain isolated. */
53
- get totalCallCount() { return this.budget.used; }
54
- /** A fresh runner, never a fork of conversation/journal/sinks. All ancestors retain their budgets. */
95
+ get totalCallCount() {
96
+ return this.budget.used;
97
+ }
98
+ /**
99
+ * Create a fresh runner for a nested stage.
100
+ *
101
+ * The child never inherits conversation, journal or sinks: only the provider, fixed
102
+ * limits, the parent budget and the child's own lifetime. All ancestors keep their
103
+ * budgets, and the child is charged through them, so nested work cannot overspend.
104
+ *
105
+ * @param options - Child id, call allowance, reserved calls, timeout and optional signals.
106
+ * @returns A runner sharing the request gate, with a budget derived from this one.
107
+ * @throws Error When the depth or child capacity is exhausted, a call is in flight,
108
+ * the settings are invalid, or the observer belongs to another run.
109
+ */
55
110
  createChild(options) {
56
111
  breakWhile(this.depth >= this.requestGate.limits.maxDepth, 'Nested model children exceed the request depth limit');
57
112
  breakWhile(this.inFlight, 'Cannot create a child while this model call is active');
58
113
  breakWhile(this.requestGate.children >= this.requestGate.limits.maxChildren, 'Model child capacity exhausted');
59
- breakWhile(typeof options.id !== 'string' || !options.id.trim() || !Number.isSafeInteger(options.timeoutMs) || options.timeoutMs < 1 || options.timeoutMs > 2147483647, 'Invalid child execution settings');
114
+ breakWhile(typeof options.id !== 'string' ||
115
+ !options.id.trim() ||
116
+ !Number.isSafeInteger(options.timeoutMs) ||
117
+ options.timeoutMs < 1 ||
118
+ options.timeoutMs > 2147483647, 'Invalid child execution settings');
60
119
  const budget = this.budget.child(options.maxCalls, options.reserveCalls);
61
120
  const diagnostics = options.diagnostics ?? this.ports.diagnostics;
62
- breakWhile(!!this.ports.diagnostics?.identity?.runId && !!diagnostics?.identity?.runId
63
- && this.ports.diagnostics.identity.runId !== diagnostics.identity.runId, 'Child observation belongs to another run');
64
- const childOptions = { provider: this.provider, [modelBindingId]: this.modelId, maxCalls: options.maxCalls,
65
- callTimeoutMs: this.config.callTimeoutMs, maxInputChars: this.maxInputChars, maxOutputChars: this.config.maxOutputChars,
66
- maxSchemaChars: this.ports.maxSchemaChars, maxOutputTokens: this.maxOutputTokens, tokenBudget: this.tokenBudget,
67
- usage: this.config.usage, transports: options.transports,
121
+ breakWhile(!!this.ports.diagnostics?.identity?.runId &&
122
+ !!diagnostics?.identity?.runId &&
123
+ this.ports.diagnostics.identity.runId !== diagnostics.identity.runId, 'Child observation belongs to another run');
124
+ const childOptions = {
125
+ provider: this.provider,
126
+ [modelBindingId]: this.modelId,
127
+ maxCalls: options.maxCalls,
128
+ callTimeoutMs: this.config.callTimeoutMs,
129
+ maxInputChars: this.maxInputChars,
130
+ maxOutputChars: this.config.maxOutputChars,
131
+ maxSchemaChars: this.ports.maxSchemaChars,
132
+ maxOutputTokens: this.maxOutputTokens,
133
+ tokenBudget: this.tokenBudget,
134
+ usage: this.config.usage,
135
+ transports: options.transports,
68
136
  children: this.requestGate.limits,
69
- execution: { signal: combineSignals(this.signal, AbortSignal.timeout(options.timeoutMs), options.signal),
70
- diagnostics: diagnostics?.child(invocationId(), { definitionKind: 'agent', definitionId: options.id }) },
137
+ execution: {
138
+ signal: combineSignals(this.signal, AbortSignal.timeout(options.timeoutMs), options.signal),
139
+ diagnostics: diagnostics?.child(invocationId(), {
140
+ definitionKind: 'agent',
141
+ definitionId: options.id,
142
+ }),
143
+ },
71
144
  };
72
145
  const child = new ModelRunner(childOptions);
73
146
  child.budget = budget;
@@ -76,9 +149,19 @@ export class ModelRunner {
76
149
  child.requestGate = this.requestGate;
77
150
  return child;
78
151
  }
152
+ /**
153
+ * Build a runner from App options, or adopt an already compiled binding.
154
+ *
155
+ * Compiling here rather than per call is what makes configuration immutable for the
156
+ * runner's lifetime. The text transport is always registered first, then App
157
+ * transports in declaration order.
158
+ *
159
+ * @param options - Runner options; may carry a compiled binding and an inherited budget.
160
+ * @throws TypeError When settings, child limits or a transport are invalid.
161
+ * @throws ValidationError When the token budget contract is incomplete.
162
+ */
79
163
  constructor(options) {
80
- const resolved = options[compiledModelBinding]
81
- ?? snapshotModelBinding(options);
164
+ const resolved = options[compiledModelBinding] ?? snapshotModelBinding(options);
82
165
  this.budget = new ModelCallBudget(resolved.config.maxCalls, options[parentModelBudget]);
83
166
  this.requestGate = { active: 0, children: 0, limits: resolved.children };
84
167
  this.provider = resolved.provider;
@@ -87,7 +170,11 @@ export class ModelRunner {
87
170
  this.signal = options.execution?.signal ?? new AbortController().signal;
88
171
  this.maxOutputTokens = resolved.maxOutputTokens;
89
172
  this.maxInputChars = resolved.maxInputChars;
90
- this.ports = Object.freeze({ ...resolved.ports, diagnostics: executionEvents(options.execution) ?? (resolved.ports.onOutput ? createDiagnostics() : undefined) });
173
+ this.ports = Object.freeze({
174
+ ...resolved.ports,
175
+ diagnostics: executionEvents(options.execution) ??
176
+ (resolved.ports.onOutput ? createDiagnostics() : undefined),
177
+ });
91
178
  this.execution = Object.freeze({ signal: this.signal, diagnostics: this.ports.diagnostics });
92
179
  this.tokenBudget = resolved.tokenBudget;
93
180
  this.transports.set(defaultTextTransport.id, defaultTextTransport);
@@ -96,7 +183,19 @@ export class ModelRunner {
96
183
  this.transports.set(definition.id, definition);
97
184
  }
98
185
  }
99
- get remainingCalls() { return this.budget.remaining; }
186
+ /** Calls still available to this runner after the parent's reserve is applied. */
187
+ get remainingCalls() {
188
+ return this.budget.remaining;
189
+ }
190
+ /**
191
+ * Resolve the character limit for one input shape.
192
+ *
193
+ * A message array uses the runner limit; a built context or plan may declare a lower
194
+ * limit, and the stricter of the two wins so neither contract can be exceeded.
195
+ *
196
+ * @param input - Messages, built context or context plan.
197
+ * @returns The effective input character limit.
198
+ */
100
199
  inputLimit(input) {
101
200
  if (isMessageArray(input))
102
201
  return this.maxInputChars;
@@ -108,12 +207,32 @@ export class ModelRunner {
108
207
  async emitOutput(event, signal) {
109
208
  const sink = this.ports.onOutput;
110
209
  signal.throwIfAborted();
111
- await awaitWithSignal(Promise.resolve().then(() => { signal.throwIfAborted(); return sink(event); }), signal);
210
+ await awaitWithSignal(Promise.resolve().then(() => {
211
+ signal.throwIfAborted();
212
+ return sink(event);
213
+ }), signal);
112
214
  }
215
+ /**
216
+ * Publish and, when a sink is installed, deliver one lifecycle event.
217
+ *
218
+ * Delivery goes through the scope so observers see the event before the sink does;
219
+ * `consume` runs inside the delivery callback, after the abort check, so a cancelled
220
+ * call cannot mutate the collector.
221
+ *
222
+ * @param type - Lifecycle event name.
223
+ * @param payload - Lazily built payload; only evaluated when someone consumes it.
224
+ * @param signal - Signal that bounds publication and delivery.
225
+ * @param scope - Diagnostic scope owning the event and its consumers.
226
+ * @param consume - Optional collector update performed before delivery.
227
+ */
113
228
  async publishOutput(type, payload, signal, scope, consume) {
114
229
  signal.throwIfAborted();
115
230
  if (this.ports.onOutput)
116
- await scope?.deliver(type, payload, event => { signal.throwIfAborted(); consume?.(); return this.emitOutput(event, signal); });
231
+ await scope?.deliver(type, payload, (event) => {
232
+ signal.throwIfAborted();
233
+ consume?.();
234
+ return this.emitOutput(event, signal);
235
+ });
117
236
  else {
118
237
  scope?.emit(type, payload);
119
238
  signal.throwIfAborted();
@@ -127,32 +246,69 @@ export class ModelRunner {
127
246
  if (typeof stageId !== 'string' || !stageId.trim())
128
247
  throw new TypeError('stageId must be a nonempty string');
129
248
  validateOutputTokens(maxOutputTokens, stageId);
130
- const transportId = request.transport === undefined ? defaultTextTransport.id : typeof request.transport === 'string'
131
- ? request.transport : this.registeredTransports.get(request.transport);
249
+ const transportId = request.transport === undefined
250
+ ? defaultTextTransport.id
251
+ : typeof request.transport === 'string'
252
+ ? request.transport
253
+ : this.registeredTransports.get(request.transport);
132
254
  const transport = transportId === undefined ? undefined : this.transports.get(transportId);
133
255
  if (!transport)
134
256
  throw new TypeError('Prepare requires a registered model transport');
135
257
  const prepared = prepareModelTransport(transport, stageId);
136
258
  let state;
137
259
  const runner = this;
138
- const snapshot = Object.freeze({ ...prepared,
139
- stageId, modelId: this.modelId, transportId: transport.id, maxOutputTokens,
140
- get remainingCalls() { return runner.remainingCalls; },
260
+ const snapshot = Object.freeze({
261
+ ...prepared,
262
+ stageId,
263
+ modelId: this.modelId,
264
+ transportId: transport.id,
265
+ maxOutputTokens,
266
+ get remainingCalls() {
267
+ return runner.remainingCalls;
268
+ },
269
+ /**
270
+ * Preflight measurement of one input shape.
271
+ *
272
+ * Measuring does not consume the prepared call and does not charge the budget; it
273
+ * only reports the sizes and whether all three budgets would accept the input.
274
+ *
275
+ * @param input - Prompt string, message array or built context.
276
+ * @returns Frozen character and token measurements with a `withinBudget` verdict.
277
+ * @throws TypeError When the input shape is invalid or the call is already used.
278
+ */
141
279
  measure(input) {
142
280
  runner.signal.throwIfAborted();
143
281
  if (state.used)
144
282
  throw new TypeError('Prepared model call has already been used');
145
- const base = typeof input === 'string' ? [{ role: 'user', content: input }] : isMessageArray(input) ? input : input.messages;
283
+ const base = typeof input === 'string'
284
+ ? [{ role: 'user', content: input }]
285
+ : isMessageArray(input)
286
+ ? input
287
+ : input.messages;
146
288
  const size = preparedInputSize(base, prepared);
147
289
  const inputLimit = typeof input === 'string' ? runner.maxInputChars : runner.inputLimit(input);
148
- const inputTokenLimit = runner.tokenBudget ? modelInputTokenLimit(runner.tokenBudget, maxOutputTokens) : undefined;
149
- const inputTokens = runner.tokenBudget ? countModelInput(runner.tokenBudget, size.messages, prepared.options) : undefined;
290
+ const inputTokenLimit = runner.tokenBudget
291
+ ? modelInputTokenLimit(runner.tokenBudget, maxOutputTokens)
292
+ : undefined;
293
+ const inputTokens = runner.tokenBudget
294
+ ? countModelInput(runner.tokenBudget, size.messages, prepared.options)
295
+ : undefined;
150
296
  const maxSchemaChars = runner.ports.maxSchemaChars;
151
- return Object.freeze({ messageChars: size.messageChars, instructionChars: size.instructionChars, schemaChars: size.schemaChars, transportChars: size.transportChars,
152
- inputChars: size.inputChars, maxInputChars: inputLimit, maxSchemaChars, maxOutputTokens,
153
- ...(runner.tokenBudget ? { counterId: runner.tokenBudget.counterId, inputTokens, inputTokenLimit } : {}),
154
- withinBudget: size.messageChars <= inputLimit && size.transportChars <= maxSchemaChars
155
- && (inputTokens === undefined || inputTokens <= inputTokenLimit),
297
+ return Object.freeze({
298
+ messageChars: size.messageChars,
299
+ instructionChars: size.instructionChars,
300
+ schemaChars: size.schemaChars,
301
+ transportChars: size.transportChars,
302
+ inputChars: size.inputChars,
303
+ maxInputChars: inputLimit,
304
+ maxSchemaChars,
305
+ maxOutputTokens,
306
+ ...(runner.tokenBudget
307
+ ? { counterId: runner.tokenBudget.counterId, inputTokens, inputTokenLimit }
308
+ : {}),
309
+ withinBudget: size.messageChars <= inputLimit &&
310
+ size.transportChars <= maxSchemaChars &&
311
+ (inputTokens === undefined || inputTokens <= inputTokenLimit),
156
312
  });
157
313
  },
158
314
  });
@@ -168,33 +324,81 @@ export class ModelRunner {
168
324
  const prepared = state?.snapshot;
169
325
  const stage = call.stageId ?? prepared?.stageId ?? modelRunnerDefaults.stage;
170
326
  const tokens = call.maxOutputTokens ?? prepared?.maxOutputTokens ?? this.maxOutputTokens;
171
- const invalid = (message) => recordErrorContext(new TypeError(message), { stageId: stage, codes: ['MODEL_CONFIG'], isRetryable: false });
327
+ const invalid = (message) => recordErrorContext(new TypeError(message), {
328
+ stageId: stage,
329
+ codes: ['MODEL_CONFIG'],
330
+ isRetryable: false,
331
+ });
172
332
  breakWhile([
173
333
  { occur: 'stage' in call, error: () => invalid('Use stageId to select a model stage') },
174
- { occur: typeof stage !== 'string' || !stage.trim(), error: () => invalid('stageId must be a nonempty string') },
175
- { occur: 'transportId' in call, error: () => invalid('Use transport to select a registered model transport') },
176
- { occur: typeof call.transport === 'string' && !call.transport.trim(), error: () => invalid('Transport ID must be a nonempty string') },
334
+ {
335
+ occur: typeof stage !== 'string' || !stage.trim(),
336
+ error: () => invalid('stageId must be a nonempty string'),
337
+ },
338
+ {
339
+ occur: 'transportId' in call,
340
+ error: () => invalid('Use transport to select a registered model transport'),
341
+ },
342
+ {
343
+ occur: typeof call.transport === 'string' && !call.transport.trim(),
344
+ error: () => invalid('Transport ID must be a nonempty string'),
345
+ },
177
346
  { occur: 'prompt' in call, error: () => invalid('Use input to provide model input') },
178
347
  { occur: call.input == null, error: () => invalid('Model input is required') },
179
- { occur: call.input !== undefined && typeof call.input !== 'string' && call.instructions !== undefined, error: () => invalid('instructions require string input; message inputs already declare their roles') },
180
- { occur: call.instructions !== undefined && typeof call.instructions !== 'string', error: () => invalid('instructions must be text') },
348
+ {
349
+ occur: call.input !== undefined &&
350
+ typeof call.input !== 'string' &&
351
+ call.instructions !== undefined,
352
+ error: () => invalid('instructions require string input; message inputs already declare their roles'),
353
+ },
354
+ {
355
+ occur: call.instructions !== undefined && typeof call.instructions !== 'string',
356
+ error: () => invalid('instructions must be text'),
357
+ },
181
358
  ]);
182
359
  validateOutputTokens(tokens, stage);
183
360
  const source = call.input;
184
- const input = typeof source === 'string' ? [
185
- ...(call.instructions === undefined ? [] : [{ role: 'system', content: call.instructions }]),
186
- { role: 'user', content: source },
187
- ] : source;
361
+ const input = typeof source === 'string'
362
+ ? [
363
+ ...(call.instructions === undefined
364
+ ? []
365
+ : [{ role: 'system', content: call.instructions }]),
366
+ { role: 'user', content: source },
367
+ ]
368
+ : source;
188
369
  const transport = call.transport;
189
- const transportId = transport === undefined ? prepared?.transportId ?? defaultTextTransport.id
190
- : typeof transport === 'string' ? transport : this.registeredTransports.get(transport);
370
+ const transportId = transport === undefined
371
+ ? (prepared?.transportId ?? defaultTextTransport.id)
372
+ : typeof transport === 'string'
373
+ ? transport
374
+ : this.registeredTransports.get(transport);
191
375
  breakWhile(transportId === undefined, () => invalid('Typed transport must be the registered transport instance'));
192
- if (prepared && (stage !== prepared.stageId || tokens !== prepared.maxOutputTokens || transportId !== prepared.transportId))
376
+ if (prepared &&
377
+ (stage !== prepared.stageId ||
378
+ tokens !== prepared.maxOutputTokens ||
379
+ transportId !== prepared.transportId))
193
380
  throw invalid('Prepared model request settings cannot be replaced');
194
381
  if (state)
195
382
  state.used = true;
196
383
  return this.invokePrepared(stage, input, tokens, transportId, call.diagnostics, prepared, call.signal);
197
384
  }
385
+ /**
386
+ * Acquire the per-runner call slot and execute one call.
387
+ *
388
+ * The concurrency guard is taken and released around `execute`; the error context is
389
+ * decorated with the stage before the failure leaves this method, so later layers do
390
+ * not need to re-derive it.
391
+ *
392
+ * @param stage - Stage id for diagnostics and error context.
393
+ * @param input - Messages, built context or context plan.
394
+ * @param tokens - Requested output tokens.
395
+ * @param transportId - Registered transport to execute.
396
+ * @param diagnostics - Observation scope for this call.
397
+ * @param prepared - Prepared call whose snapshot must be reused.
398
+ * @param requestSignal - Caller signal, combined with the runner signal.
399
+ * @returns The parsed result with invocation metadata.
400
+ * @throws Error When a call is already in flight or the request capacity is exhausted.
401
+ */
198
402
  async invokePrepared(stage, input, tokens, transportId, diagnostics = this.ports.diagnostics, prepared, requestSignal) {
199
403
  const configuredRun = this.ports.diagnostics?.identity?.runId, selectedRun = diagnostics?.identity?.runId;
200
404
  if (configuredRun && selectedRun && configuredRun !== selectedRun)
@@ -216,16 +420,59 @@ export class ModelRunner {
216
420
  }
217
421
  throw failure;
218
422
  }
423
+ /**
424
+ * Execute the prepared call: measure, charge, stream, commit.
425
+ *
426
+ * Phase order is a correctness requirement, not an implementation detail. Every
427
+ * rejection path goes through `reject`, which annotates codes and emits
428
+ * `runtime.rejected`. Budget charging happens only after all input checks pass, so a
429
+ * rejected request never consumes a call. The terminal `model.end` event is published
430
+ * in `finally` with its own timeout, because the call signal is already aborted when a
431
+ * call is cancelled and terminal delivery must still retract previews.
432
+ *
433
+ * @param stage - Stage id for diagnostics.
434
+ * @param input - Messages, built context or context plan.
435
+ * @param tokens - Requested output tokens.
436
+ * @param transportId - Registered transport id.
437
+ * @param signal - Combined execution and request signal.
438
+ * @param parentDiagnostics - Parent observation scope, defaulting to the runner scope.
439
+ * @param prepared - Prepared call whose snapshot must be reused.
440
+ * @returns The parsed result with invocation metadata.
441
+ * @throws Error or ValidationError from any phase, decorated with stage context.
442
+ */
219
443
  async execute(stage, input, tokens, transportId, signal, parentDiagnostics = this.ports.diagnostics, prepared) {
220
444
  const id = invocationId(), startedAtMs = Date.now();
221
445
  this.activeCall = undefined;
222
- const invocationDiagnostics = parentDiagnostics?.child(id, { invocationId: id, modelId: this.modelId, nodeRole: 'invocation', definitionKind: 'model-transport', definitionId: transportId, nodeId: stage, nodeName: stage, nodeKind: 'model', nodeDescription: 'Registered model invocation', definitionRevision: undefined, stageId: stage });
446
+ const invocationDiagnostics = parentDiagnostics?.child(id, {
447
+ invocationId: id,
448
+ modelId: this.modelId,
449
+ nodeRole: 'invocation',
450
+ definitionKind: 'model-transport',
451
+ definitionId: transportId,
452
+ nodeId: stage,
453
+ nodeName: stage,
454
+ nodeKind: 'model',
455
+ nodeDescription: 'Registered model invocation',
456
+ definitionRevision: undefined,
457
+ stageId: stage,
458
+ });
223
459
  let diagnostics = invocationDiagnostics;
224
460
  const reject = (error) => {
225
- recordErrorContext(error, { stageId: stage, ...(signal.aborted
226
- ? { codes: [isTimeoutReason(signal.reason) ? 'ROUND_TIMEOUT' : 'CANCELLED'], isRetryable: false }
227
- : error instanceof BudgetError ? { codes: ['BUDGET'], isRetryable: false } : {}) });
228
- diagnostics?.emit('runtime.rejected', () => ({ stageId: stage, error: diagnosticError(error) }));
461
+ recordErrorContext(error, {
462
+ stageId: stage,
463
+ ...(signal.aborted
464
+ ? {
465
+ codes: [isTimeoutReason(signal.reason) ? 'ROUND_TIMEOUT' : 'CANCELLED'],
466
+ isRetryable: false,
467
+ }
468
+ : error instanceof BudgetError
469
+ ? { codes: ['BUDGET'], isRetryable: false }
470
+ : {}),
471
+ });
472
+ diagnostics?.emit('runtime.rejected', () => ({
473
+ stageId: stage,
474
+ error: diagnosticError(error),
475
+ }));
229
476
  throw error;
230
477
  };
231
478
  const transport = this.transports.get(transportId);
@@ -286,11 +533,15 @@ export class ModelRunner {
286
533
  const functionChars = size.transportChars;
287
534
  // The binding snapshots and validates this fixed allowance at construction.
288
535
  if (functionChars > this.ports.maxSchemaChars)
289
- return reject(new ValidationError([{ code: 'MODEL_SCHEMA_BUDGET', message: 'Model declarations exceed their schema budget' }]));
536
+ return reject(new ValidationError([
537
+ { code: 'MODEL_SCHEMA_BUDGET', message: 'Model declarations exceed their schema budget' },
538
+ ]));
290
539
  const inputChars = size.inputChars;
291
540
  try {
292
- validateContextBudget(messageChars, this.inputLimit(input), limit => this.ports.inputBudgetError?.(limit)
293
- ?? new ValidationError([{ code: 'CONTEXT_BUDGET', message: `Model input exceeds ${limit} characters` }]));
541
+ validateContextBudget(messageChars, this.inputLimit(input), (limit) => this.ports.inputBudgetError?.(limit) ??
542
+ new ValidationError([
543
+ { code: 'CONTEXT_BUDGET', message: `Model input exceeds ${limit} characters` },
544
+ ]));
294
545
  }
295
546
  catch (error) {
296
547
  return reject(error);
@@ -309,13 +560,32 @@ export class ModelRunner {
309
560
  this.budget.take();
310
561
  this.completedCallCount++;
311
562
  const call = this.callCount;
312
- const step = { ...invocationDiagnostics?.identity, modelId: this.modelId, invocationId: id, startedAtMs, callSequence: call, stageId: stage, durationMs: 0, inputChars, outputChars: 0,
313
- firstOutputMs: null, outputDurationMs: 0, status: 'error', codes: [],
314
- ...(inputTokens === undefined ? {} : { inputTokens, inputTokenLimit: tokenLimit }) };
563
+ const step = {
564
+ ...invocationDiagnostics?.identity,
565
+ modelId: this.modelId,
566
+ invocationId: id,
567
+ startedAtMs,
568
+ callSequence: call,
569
+ stageId: stage,
570
+ durationMs: 0,
571
+ inputChars,
572
+ outputChars: 0,
573
+ firstOutputMs: null,
574
+ outputDurationMs: 0,
575
+ status: 'error',
576
+ codes: [],
577
+ ...(inputTokens === undefined ? {} : { inputTokens, inputTokenLimit: tokenLimit }),
578
+ };
315
579
  if (!isMessageArray(input) && diagnostics?.enabled('model.prepare')) {
316
580
  try {
317
- const serialized = JSON.stringify({ messages: modelMessageDiagnostics(messages), interaction: interactionOptions });
318
- step.context = { fragmentUsage: input.fragmentUsage, stablePrefixChars: commonPrefixLength(serialized, this.priorInputs.get(stage) ?? '') };
581
+ const serialized = JSON.stringify({
582
+ messages: modelMessageDiagnostics(messages),
583
+ interaction: interactionOptions,
584
+ });
585
+ step.context = {
586
+ fragmentUsage: input.fragmentUsage,
587
+ stablePrefixChars: commonPrefixLength(serialized, this.priorInputs.get(stage) ?? ''),
588
+ };
319
589
  this.priorInputs.set(stage, serialized);
320
590
  }
321
591
  catch {
@@ -340,7 +610,9 @@ export class ModelRunner {
340
610
  if (call)
341
611
  step.functionCall = call;
342
612
  }
343
- catch { /* best-effort metadata */ }
613
+ catch {
614
+ /* best-effort metadata */
615
+ }
344
616
  };
345
617
  const accountOutput = () => {
346
618
  const outputChars = response.outputChars;
@@ -350,9 +622,18 @@ export class ModelRunner {
350
622
  // largest observed size without requiring its internal buffer to grow.
351
623
  step.outputChars = Math.max(step.outputChars, outputChars);
352
624
  if (this.config.maxOutputChars !== undefined && outputChars > this.config.maxOutputChars)
353
- throw this.ports.outputBudgetError?.() ?? new ValidationError([{ code: 'OUTPUT_LIMIT', message: 'Model output exceeds its character budget' }]);
625
+ throw (this.ports.outputBudgetError?.() ??
626
+ new ValidationError([
627
+ { code: 'OUTPUT_LIMIT', message: 'Model output exceeds its character budget' },
628
+ ]));
354
629
  };
355
- diagnostics?.emit('model.prepare', () => ({ stageId: stage, transportId, messages: modelMessageDiagnostics(messages), options: { ...interactionOptions, maxOutputTokens: tokens }, context: isMessageArray(input) ? undefined : input.fragmentUsage }));
630
+ diagnostics?.emit('model.prepare', () => ({
631
+ stageId: stage,
632
+ transportId,
633
+ messages: modelMessageDiagnostics(messages),
634
+ options: { ...interactionOptions, maxOutputTokens: tokens },
635
+ context: isMessageArray(input) ? undefined : input.fragmentUsage,
636
+ }));
356
637
  try {
357
638
  await this.publishOutput('model.start', () => ({ stageId: stage, callSequence: call, transportId }), callSignal, diagnostics);
358
639
  callSignal.throwIfAborted();
@@ -361,10 +642,18 @@ export class ModelRunner {
361
642
  throw new TypeError('Invalid model response collector');
362
643
  accountOutput();
363
644
  callSignal.throwIfAborted();
364
- for await (const part of abortableStream(this.provider.stream(messages, { signal: callSignal, diagnostics, options: {
645
+ for await (const part of abortableStream(this.provider.stream(messages, {
646
+ signal: callSignal,
647
+ diagnostics,
648
+ options: {
365
649
  ...interactionOptions,
366
- maxOutputTokens: tokens, includeUsage: this.config.usage, onUsage: usage => { step.usage = usage; },
367
- } }), callSignal)) {
650
+ maxOutputTokens: tokens,
651
+ includeUsage: this.config.usage,
652
+ onUsage: (usage) => {
653
+ step.usage = usage;
654
+ },
655
+ },
656
+ }), callSignal)) {
368
657
  const partIndex = receivedParts;
369
658
  receivedParts++;
370
659
  callSignal.throwIfAborted();
@@ -382,29 +671,67 @@ export class ModelRunner {
382
671
  captureCallMetadata();
383
672
  await this.publishOutput('model.parsed', () => ({ stageId: stage, callSequence: call, value }), callSignal, diagnostics);
384
673
  callSignal.throwIfAborted();
385
- this.journal.model({ stageId: stage, value, invocation: { ...invocationDiagnostics?.identity, invocationId: id, startedAtMs, durationMs: Date.now() - startedAtMs } });
674
+ this.journal.model({
675
+ stageId: stage,
676
+ value,
677
+ invocation: {
678
+ ...invocationDiagnostics?.identity,
679
+ invocationId: id,
680
+ startedAtMs,
681
+ durationMs: Date.now() - startedAtMs,
682
+ },
683
+ });
386
684
  step.status = 'success';
387
- return invocationResult(value, { ...invocationDiagnostics?.identity, invocationId: id, modelId: this.modelId, stageId: stage, startedAtMs,
388
- durationMs: Date.now() - startedAtMs, callSequence: call, status: 'success',
389
- ...(step.usage ? { usage: Object.freeze({ ...step.usage }) } : {}) });
685
+ return invocationResult(value, {
686
+ ...invocationDiagnostics?.identity,
687
+ invocationId: id,
688
+ modelId: this.modelId,
689
+ stageId: stage,
690
+ startedAtMs,
691
+ durationMs: Date.now() - startedAtMs,
692
+ callSequence: call,
693
+ status: 'success',
694
+ ...(step.usage ? { usage: Object.freeze({ ...step.usage }) } : {}),
695
+ });
390
696
  }
391
697
  catch (error) {
392
698
  callError = { error };
393
699
  step.status = signal.aborted || callDeadline.aborted ? 'cancelled' : 'error';
394
700
  try {
395
701
  const info = getErrorInfo(error);
396
- const codes = error instanceof ValidationError ? error.issues.map(item => item.code)
397
- : [signal.aborted ? (isTimeoutReason(signal.reason) ? 'ROUND_TIMEOUT' : 'CANCELLED')
398
- : callDeadline.aborted ? 'CALL_TIMEOUT' : info.code === 'UNKNOWN_ERROR' ? 'UPSTREAM_ERROR' : info.code];
399
- recordErrorContext(error, { stageId: stage, codes,
400
- ...(signal.aborted ? { isRetryable: false } : callDeadline.aborted ? { isRetryable: true } : {}) });
702
+ const codes = error instanceof ValidationError
703
+ ? error.issues.map((item) => item.code)
704
+ : [
705
+ signal.aborted
706
+ ? isTimeoutReason(signal.reason)
707
+ ? 'ROUND_TIMEOUT'
708
+ : 'CANCELLED'
709
+ : callDeadline.aborted
710
+ ? 'CALL_TIMEOUT'
711
+ : info.code === 'UNKNOWN_ERROR'
712
+ ? 'UPSTREAM_ERROR'
713
+ : info.code,
714
+ ];
715
+ recordErrorContext(error, {
716
+ stageId: stage,
717
+ codes,
718
+ ...(signal.aborted
719
+ ? { isRetryable: false }
720
+ : callDeadline.aborted
721
+ ? { isRetryable: true }
722
+ : {}),
723
+ });
401
724
  step.codes = [...getErrorInfo(error).codes];
402
725
  }
403
726
  catch {
404
727
  // Even instanceof or an Error subclass's fields can throw. Decoration
405
728
  // must not replace the original value or skip terminal delivery.
406
729
  const cancelled = step.status === 'cancelled', codes = [cancelled ? 'CANCELLED' : 'UPSTREAM_ERROR'];
407
- recordErrorContext(error, { stageId: stage, codes, ...(cancelled ? { isRetryable: false } : {}) });
730
+ recordErrorContext(error, {
731
+ stageId: stage,
732
+ codes,
733
+ ...(cancelled ? { isRetryable: false } : {}),
734
+ });
408
735
  step.codes = codes;
409
736
  }
410
737
  }
@@ -412,13 +739,20 @@ export class ModelRunner {
412
739
  captureCallMetadata();
413
740
  step.durationMs = Date.now() - startedAtMs;
414
741
  step.outputDurationMs = step.firstOutputMs == null ? 0 : step.durationMs - step.firstOutputMs;
415
- const end = () => ({ stageId: stage, state: { ...step }, receivedParts, ...(callError ? { error: diagnosticError(callError.error) } : {}) });
742
+ const end = () => ({
743
+ stageId: stage,
744
+ state: { ...step },
745
+ receivedParts,
746
+ ...(callError ? { error: diagnosticError(callError.error) } : {}),
747
+ });
416
748
  // Terminal delivery also retracts cancelled previews. It needs its own
417
749
  // bounded lifetime: the call signal is already aborted on cancellation.
418
750
  try {
419
751
  await this.publishOutput('model.end', end, AbortSignal.timeout(250), diagnostics);
420
752
  }
421
- catch { /* Terminal notification cannot replace a committed result or its original failure. */ }
753
+ catch {
754
+ /* Terminal notification cannot replace a committed result or its original failure. */
755
+ }
422
756
  }
423
757
  throw callError.error;
424
758
  }