@jangts/agent-core 0.1.1 → 0.2.1

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