@jangts/agent-core 0.1.0 → 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 (527) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +59 -0
  3. package/development/docs/api/reference.md +1907 -1874
  4. package/development/docs/api/support.md +27 -0
  5. package/development/docs/assets/agent-core-icon.png +0 -0
  6. package/development/docs/assets/agent-core-logo.png +0 -0
  7. package/development/docs/assets/character-agent-core.png +0 -0
  8. package/development/skills/agent-core-app/scripts/create-app.mjs +267 -70
  9. package/development/skills/agent-core-app/scripts/lib/knowledge.mjs +40 -15
  10. package/development/skills/agent-core-app/scripts/lib/source.mjs +30 -2
  11. package/development/skills/agent-core-app/scripts/plan.mjs +37 -12
  12. package/development/skills/agent-core-app/scripts/prepare-source.mjs +61 -10
  13. package/development/skills/agent-core-app/scripts/resources.mjs +45 -17
  14. package/development/skills/agent-core-app/scripts/validate-knowledge.mjs +18 -1
  15. package/development/skills/agent-core-app/templates/catalog.json +36 -22
  16. package/dist/cjs/capabilities/index.js +13 -0
  17. package/dist/cjs/capabilities/invocation.js +80 -10
  18. package/dist/cjs/capabilities/model.js +126 -13
  19. package/dist/cjs/capabilities/tool-runtime.js +64 -12
  20. package/dist/cjs/capabilities/tools.js +266 -31
  21. package/dist/cjs/context/assembly.js +46 -5
  22. package/dist/cjs/context/audit.js +31 -6
  23. package/dist/cjs/context/fragments.js +54 -11
  24. package/dist/cjs/context/index.js +12 -0
  25. package/dist/cjs/context/journal.js +84 -7
  26. package/dist/cjs/context/plan.js +30 -2
  27. package/dist/cjs/diagnostics/catalog.js +11 -3
  28. package/dist/cjs/diagnostics/index.js +149 -29
  29. package/dist/cjs/diagnostics/sub-flow.js +240 -40
  30. package/dist/cjs/errors.js +99 -17
  31. package/dist/cjs/execution/branch.js +20 -4
  32. package/dist/cjs/execution/engine.js +470 -56
  33. package/dist/cjs/execution/events.js +136 -18
  34. package/dist/cjs/execution/index.js +7 -0
  35. package/dist/cjs/execution/reporting.js +75 -6
  36. package/dist/cjs/execution/state.js +161 -22
  37. package/dist/cjs/execution/workflow.js +224 -32
  38. package/dist/cjs/index.js +18 -0
  39. package/dist/cjs/kits/aggregation.js +45 -3
  40. package/dist/cjs/kits/answering/batch.js +23 -4
  41. package/dist/cjs/kits/answering/check-result.js +31 -8
  42. package/dist/cjs/kits/answering/checks.js +24 -3
  43. package/dist/cjs/kits/answering/flow.js +192 -36
  44. package/dist/cjs/kits/answering/index.js +19 -1
  45. package/dist/cjs/kits/answering/tasks.js +88 -13
  46. package/dist/cjs/kits/artifacts.js +36 -4
  47. package/dist/cjs/kits/candidates.js +47 -5
  48. package/dist/cjs/kits/delivery.js +40 -5
  49. package/dist/cjs/kits/durable.js +155 -31
  50. package/dist/cjs/kits/evidence.js +66 -15
  51. package/dist/cjs/kits/family.js +152 -22
  52. package/dist/cjs/kits/index.js +12 -1
  53. package/dist/cjs/kits/inference.js +104 -16
  54. package/dist/cjs/kits/intents/catalog.js +67 -16
  55. package/dist/cjs/kits/intents/index.js +11 -0
  56. package/dist/cjs/kits/intents/registry.js +188 -43
  57. package/dist/cjs/kits/intents/scopes.js +215 -46
  58. package/dist/cjs/kits/interaction/command.js +140 -36
  59. package/dist/cjs/kits/interaction/commands.js +86 -11
  60. package/dist/cjs/kits/interaction/handlers.js +27 -10
  61. package/dist/cjs/kits/interaction/index.js +10 -0
  62. package/dist/cjs/kits/interaction/transfer.js +80 -14
  63. package/dist/cjs/kits/knowledge/catalog.js +62 -11
  64. package/dist/cjs/kits/knowledge/index.js +16 -1
  65. package/dist/cjs/kits/knowledge/publication.js +99 -23
  66. package/dist/cjs/kits/knowledge/sections.js +33 -4
  67. package/dist/cjs/kits/normalization.js +51 -7
  68. package/dist/cjs/kits/policies.js +79 -15
  69. package/dist/cjs/kits/presentation/events.js +54 -10
  70. package/dist/cjs/kits/presentation/index.js +24 -1
  71. package/dist/cjs/kits/presentation/outputs.js +176 -38
  72. package/dist/cjs/kits/presentation/templates.js +33 -5
  73. package/dist/cjs/kits/presentation/variants.js +31 -2
  74. package/dist/cjs/kits/projection.js +63 -8
  75. package/dist/cjs/kits/rank-fusion.js +27 -4
  76. package/dist/cjs/kits/reflection-log-source.js +89 -16
  77. package/dist/cjs/kits/reflection.js +234 -60
  78. package/dist/cjs/kits/retrieval.js +98 -18
  79. package/dist/cjs/kits/search.js +180 -27
  80. package/dist/cjs/kits/text-skills.js +104 -19
  81. package/dist/cjs/kits/tools.js +415 -74
  82. package/dist/cjs/kits/vector-search.js +50 -16
  83. package/dist/cjs/mock/node.js +7 -1
  84. package/dist/cjs/mock/web.js +7 -1
  85. package/dist/cjs/mock.js +7 -1
  86. package/dist/cjs/model/binding.js +68 -17
  87. package/dist/cjs/model/call-budget.js +45 -4
  88. package/dist/cjs/model/decision-retry.js +19 -1
  89. package/dist/cjs/model/defaults.js +27 -4
  90. package/dist/cjs/model/index.js +7 -0
  91. package/dist/cjs/model/interactions/arguments.js +36 -1
  92. package/dist/cjs/model/interactions/json-object.js +48 -12
  93. package/dist/cjs/model/interactions/native-functions.js +52 -10
  94. package/dist/cjs/model/message-diagnostics.js +17 -4
  95. package/dist/cjs/model/options.js +9 -1
  96. package/dist/cjs/model/preparation.js +86 -12
  97. package/dist/cjs/model/runner.js +413 -79
  98. package/dist/cjs/model/selection-transport.js +48 -8
  99. package/dist/cjs/model/settings.js +67 -13
  100. package/dist/cjs/model/token-budget.js +69 -11
  101. package/dist/cjs/model/transports.js +59 -6
  102. package/dist/cjs/structs/common.js +11 -0
  103. package/dist/cjs/structs/debug.js +34 -5
  104. package/dist/cjs/structs/errors.js +26 -1
  105. package/dist/cjs/structs/index.js +11 -1
  106. package/dist/cjs/structs/kits/durable.js +7 -0
  107. package/dist/cjs/structs/kits/family.js +1 -0
  108. package/dist/cjs/structs/kits/interaction.js +8 -1
  109. package/dist/cjs/structs/kits/knowledge-publication.js +1 -0
  110. package/dist/cjs/structs/kits/reflection.js +18 -3
  111. package/dist/cjs/structs/model/chat-provider.js +6 -0
  112. package/dist/cjs/structs/support/model/completion.js +2 -0
  113. package/dist/cjs/structs/support/model/messages.js +2 -0
  114. package/dist/cjs/support/capabilities.js +43 -6
  115. package/dist/cjs/support/context/assembly.js +88 -16
  116. package/dist/cjs/support/context/compression.js +48 -6
  117. package/dist/cjs/support/context/fragments.js +6 -0
  118. package/dist/cjs/support/context/history.js +36 -1
  119. package/dist/cjs/support/context/index.js +11 -0
  120. package/dist/cjs/support/context/nodes.js +164 -35
  121. package/dist/cjs/support/context/processing.js +319 -61
  122. package/dist/cjs/support/context/selection-policy.js +98 -21
  123. package/dist/cjs/support/context/selection.js +61 -10
  124. package/dist/cjs/support/context/snapshot.js +29 -7
  125. package/dist/cjs/support/context/strategies.js +120 -28
  126. package/dist/cjs/support/index.js +9 -1
  127. package/dist/cjs/support/model/capabilities.js +20 -1
  128. package/dist/cjs/support/model/chat-completions.js +92 -20
  129. package/dist/cjs/support/model/children.js +34 -6
  130. package/dist/cjs/support/model/compatible-provider.js +189 -59
  131. package/dist/cjs/support/model/completion/calls.js +61 -0
  132. package/dist/cjs/support/model/completion/default.js +32 -0
  133. package/dist/cjs/support/model/completion/index.js +28 -0
  134. package/dist/cjs/support/model/completion/session.js +102 -0
  135. package/dist/cjs/support/model/index.js +12 -0
  136. package/dist/cjs/support/model/interactions/index.js +12 -1
  137. package/dist/cjs/support/model/interactions/json-object.js +6 -0
  138. package/dist/cjs/support/model/interactions/native-functions.js +6 -0
  139. package/dist/cjs/support/model/observer.js +32 -6
  140. package/dist/cjs/support/model/preview-rules.js +66 -16
  141. package/dist/cjs/support/model/reply-preview.js +77 -17
  142. package/dist/cjs/support/model/stage-transport.js +33 -7
  143. package/dist/cjs/support/model/strategies.js +75 -14
  144. package/dist/cjs/support/model/transports.js +53 -11
  145. package/dist/cjs/support/registration.js +49 -7
  146. package/dist/cjs/support/schema.js +46 -4
  147. package/dist/cjs/support/stream-events.js +60 -8
  148. package/dist/cjs/support/tool-runtime.js +6 -0
  149. package/dist/cjs/support/tools.js +79 -10
  150. package/dist/cjs/support/transport/index.js +7 -0
  151. package/dist/cjs/support/transport/json.js +28 -4
  152. package/dist/cjs/support/transport/sse.js +25 -2
  153. package/dist/cjs/utils/break-observer.js +20 -2
  154. package/dist/cjs/utils/cancellation.js +77 -9
  155. package/dist/cjs/utils/catalog.js +19 -4
  156. package/dist/cjs/utils/debug.js +14 -2
  157. package/dist/cjs/utils/index.js +8 -0
  158. package/dist/cjs/utils/interaction-definition.js +26 -6
  159. package/dist/cjs/utils/invocation.js +42 -4
  160. package/dist/cjs/utils/model-protocol.js +20 -1
  161. package/dist/cjs/utils/object.js +16 -0
  162. package/dist/cjs/utils/observer.js +22 -2
  163. package/dist/cjs/utils/protocol-registry.js +47 -4
  164. package/dist/cjs/utils/query-key.js +21 -2
  165. package/dist/cjs/utils/registration.js +18 -2
  166. package/dist/cjs/utils/rules.js +39 -11
  167. package/dist/cjs/utils/schema-adapter.js +25 -2
  168. package/dist/cjs/utils/synchronous.js +64 -7
  169. package/dist/cjs/utils/text.js +15 -1
  170. package/dist/esm/capabilities/index.d.ts +13 -0
  171. package/dist/esm/capabilities/index.js +13 -0
  172. package/dist/esm/capabilities/invocation.d.ts +16 -2
  173. package/dist/esm/capabilities/invocation.js +80 -10
  174. package/dist/esm/capabilities/model.d.ts +68 -1
  175. package/dist/esm/capabilities/model.js +126 -13
  176. package/dist/esm/capabilities/tool-runtime.d.ts +11 -0
  177. package/dist/esm/capabilities/tool-runtime.js +64 -12
  178. package/dist/esm/capabilities/tools.d.ts +96 -3
  179. package/dist/esm/capabilities/tools.js +267 -32
  180. package/dist/esm/context/assembly.d.ts +13 -2
  181. package/dist/esm/context/assembly.js +46 -5
  182. package/dist/esm/context/audit.d.ts +23 -1
  183. package/dist/esm/context/audit.js +31 -6
  184. package/dist/esm/context/fragments.d.ts +37 -3
  185. package/dist/esm/context/fragments.js +54 -11
  186. package/dist/esm/context/index.d.ts +12 -0
  187. package/dist/esm/context/index.js +12 -0
  188. package/dist/esm/context/journal.d.ts +32 -1
  189. package/dist/esm/context/journal.js +84 -7
  190. package/dist/esm/context/plan.d.ts +11 -1
  191. package/dist/esm/context/plan.js +30 -2
  192. package/dist/esm/diagnostics/catalog.js +11 -3
  193. package/dist/esm/diagnostics/index.d.ts +30 -2
  194. package/dist/esm/diagnostics/index.js +149 -29
  195. package/dist/esm/diagnostics/sub-flow.d.ts +41 -3
  196. package/dist/esm/diagnostics/sub-flow.js +240 -40
  197. package/dist/esm/errors.d.ts +22 -2
  198. package/dist/esm/errors.js +99 -17
  199. package/dist/esm/execution/branch.d.ts +19 -1
  200. package/dist/esm/execution/branch.js +20 -4
  201. package/dist/esm/execution/engine.d.ts +291 -5
  202. package/dist/esm/execution/engine.js +471 -57
  203. package/dist/esm/execution/events.d.ts +16 -2
  204. package/dist/esm/execution/events.js +136 -18
  205. package/dist/esm/execution/index.d.ts +7 -0
  206. package/dist/esm/execution/index.js +7 -0
  207. package/dist/esm/execution/reporting.d.ts +49 -1
  208. package/dist/esm/execution/reporting.js +75 -6
  209. package/dist/esm/execution/state.d.ts +126 -10
  210. package/dist/esm/execution/state.js +161 -22
  211. package/dist/esm/execution/workflow.d.ts +121 -3
  212. package/dist/esm/execution/workflow.js +224 -32
  213. package/dist/esm/index.d.ts +17 -1
  214. package/dist/esm/index.js +18 -0
  215. package/dist/esm/kits/aggregation.d.ts +34 -2
  216. package/dist/esm/kits/aggregation.js +45 -3
  217. package/dist/esm/kits/answering/batch.d.ts +17 -0
  218. package/dist/esm/kits/answering/batch.js +23 -4
  219. package/dist/esm/kits/answering/check-result.d.ts +18 -1
  220. package/dist/esm/kits/answering/check-result.js +31 -8
  221. package/dist/esm/kits/answering/checks.d.ts +11 -0
  222. package/dist/esm/kits/answering/checks.js +24 -3
  223. package/dist/esm/kits/answering/flow.d.ts +25 -2
  224. package/dist/esm/kits/answering/flow.js +192 -36
  225. package/dist/esm/kits/answering/index.d.ts +12 -0
  226. package/dist/esm/kits/answering/index.js +19 -1
  227. package/dist/esm/kits/answering/tasks.d.ts +32 -1
  228. package/dist/esm/kits/answering/tasks.js +88 -13
  229. package/dist/esm/kits/artifacts.d.ts +11 -0
  230. package/dist/esm/kits/artifacts.js +36 -4
  231. package/dist/esm/kits/candidates.d.ts +23 -1
  232. package/dist/esm/kits/candidates.js +47 -5
  233. package/dist/esm/kits/delivery.d.ts +34 -0
  234. package/dist/esm/kits/delivery.js +40 -5
  235. package/dist/esm/kits/durable.d.ts +19 -0
  236. package/dist/esm/kits/durable.js +155 -31
  237. package/dist/esm/kits/evidence.d.ts +28 -5
  238. package/dist/esm/kits/evidence.js +66 -15
  239. package/dist/esm/kits/family.d.ts +17 -1
  240. package/dist/esm/kits/family.js +152 -22
  241. package/dist/esm/kits/index.d.ts +12 -1
  242. package/dist/esm/kits/index.js +12 -1
  243. package/dist/esm/kits/inference.d.ts +64 -0
  244. package/dist/esm/kits/inference.js +104 -16
  245. package/dist/esm/kits/intents/catalog.d.ts +13 -1
  246. package/dist/esm/kits/intents/catalog.js +67 -16
  247. package/dist/esm/kits/intents/index.d.ts +11 -0
  248. package/dist/esm/kits/intents/index.js +11 -0
  249. package/dist/esm/kits/intents/registry.d.ts +61 -3
  250. package/dist/esm/kits/intents/registry.js +188 -43
  251. package/dist/esm/kits/intents/scopes.d.ts +73 -3
  252. package/dist/esm/kits/intents/scopes.js +215 -46
  253. package/dist/esm/kits/interaction/command.d.ts +38 -4
  254. package/dist/esm/kits/interaction/command.js +140 -36
  255. package/dist/esm/kits/interaction/commands.d.ts +66 -2
  256. package/dist/esm/kits/interaction/commands.js +86 -11
  257. package/dist/esm/kits/interaction/handlers.d.ts +19 -2
  258. package/dist/esm/kits/interaction/handlers.js +27 -10
  259. package/dist/esm/kits/interaction/index.d.ts +10 -0
  260. package/dist/esm/kits/interaction/index.js +10 -0
  261. package/dist/esm/kits/interaction/transfer.d.ts +39 -2
  262. package/dist/esm/kits/interaction/transfer.js +80 -14
  263. package/dist/esm/kits/knowledge/catalog.d.ts +13 -0
  264. package/dist/esm/kits/knowledge/catalog.js +62 -11
  265. package/dist/esm/kits/knowledge/index.d.ts +11 -0
  266. package/dist/esm/kits/knowledge/index.js +16 -1
  267. package/dist/esm/kits/knowledge/publication.d.ts +15 -1
  268. package/dist/esm/kits/knowledge/publication.js +99 -23
  269. package/dist/esm/kits/knowledge/sections.d.ts +31 -1
  270. package/dist/esm/kits/knowledge/sections.js +33 -4
  271. package/dist/esm/kits/normalization.d.ts +25 -2
  272. package/dist/esm/kits/normalization.js +51 -7
  273. package/dist/esm/kits/policies.d.ts +26 -3
  274. package/dist/esm/kits/policies.js +79 -15
  275. package/dist/esm/kits/presentation/events.d.ts +51 -4
  276. package/dist/esm/kits/presentation/events.js +54 -10
  277. package/dist/esm/kits/presentation/index.d.ts +12 -0
  278. package/dist/esm/kits/presentation/index.js +25 -2
  279. package/dist/esm/kits/presentation/outputs.d.ts +30 -1
  280. package/dist/esm/kits/presentation/outputs.js +176 -38
  281. package/dist/esm/kits/presentation/templates.d.ts +32 -3
  282. package/dist/esm/kits/presentation/templates.js +33 -5
  283. package/dist/esm/kits/presentation/variants.d.ts +29 -0
  284. package/dist/esm/kits/presentation/variants.js +31 -2
  285. package/dist/esm/kits/projection.d.ts +42 -5
  286. package/dist/esm/kits/projection.js +63 -8
  287. package/dist/esm/kits/rank-fusion.d.ts +21 -1
  288. package/dist/esm/kits/rank-fusion.js +28 -5
  289. package/dist/esm/kits/reflection-log-source.d.ts +28 -1
  290. package/dist/esm/kits/reflection-log-source.js +89 -16
  291. package/dist/esm/kits/reflection.d.ts +17 -1
  292. package/dist/esm/kits/reflection.js +235 -61
  293. package/dist/esm/kits/retrieval.d.ts +18 -2
  294. package/dist/esm/kits/retrieval.js +98 -18
  295. package/dist/esm/kits/search.d.ts +56 -3
  296. package/dist/esm/kits/search.js +180 -27
  297. package/dist/esm/kits/text-skills.d.ts +16 -2
  298. package/dist/esm/kits/text-skills.js +104 -19
  299. package/dist/esm/kits/tools.d.ts +122 -2
  300. package/dist/esm/kits/tools.js +416 -74
  301. package/dist/esm/kits/vector-search.d.ts +47 -1
  302. package/dist/esm/kits/vector-search.js +51 -17
  303. package/dist/esm/mock/node.d.ts +7 -0
  304. package/dist/esm/mock/node.js +7 -1
  305. package/dist/esm/mock/web.d.ts +7 -0
  306. package/dist/esm/mock/web.js +7 -1
  307. package/dist/esm/mock.d.ts +7 -0
  308. package/dist/esm/mock.js +7 -1
  309. package/dist/esm/model/binding.d.ts +32 -3
  310. package/dist/esm/model/binding.js +68 -17
  311. package/dist/esm/model/call-budget.d.ts +30 -2
  312. package/dist/esm/model/call-budget.js +45 -4
  313. package/dist/esm/model/decision-retry.d.ts +10 -1
  314. package/dist/esm/model/decision-retry.js +19 -1
  315. package/dist/esm/model/defaults.d.ts +13 -2
  316. package/dist/esm/model/defaults.js +27 -4
  317. package/dist/esm/model/index.d.ts +7 -0
  318. package/dist/esm/model/index.js +7 -0
  319. package/dist/esm/model/interactions/arguments.d.ts +27 -0
  320. package/dist/esm/model/interactions/arguments.js +36 -1
  321. package/dist/esm/model/interactions/json-object.d.ts +7 -1
  322. package/dist/esm/model/interactions/json-object.js +48 -12
  323. package/dist/esm/model/interactions/native-functions.d.ts +6 -0
  324. package/dist/esm/model/interactions/native-functions.js +52 -10
  325. package/dist/esm/model/message-diagnostics.d.ts +17 -1
  326. package/dist/esm/model/message-diagnostics.js +17 -4
  327. package/dist/esm/model/options.d.ts +18 -1
  328. package/dist/esm/model/options.js +9 -1
  329. package/dist/esm/model/preparation.d.ts +30 -2
  330. package/dist/esm/model/preparation.js +86 -12
  331. package/dist/esm/model/runner.d.ts +129 -5
  332. package/dist/esm/model/runner.js +414 -80
  333. package/dist/esm/model/selection-transport.d.ts +14 -1
  334. package/dist/esm/model/selection-transport.js +48 -8
  335. package/dist/esm/model/settings.d.ts +11 -0
  336. package/dist/esm/model/settings.js +67 -13
  337. package/dist/esm/model/token-budget.d.ts +59 -1
  338. package/dist/esm/model/token-budget.js +69 -11
  339. package/dist/esm/model/transports.d.ts +20 -2
  340. package/dist/esm/model/transports.js +59 -6
  341. package/dist/esm/structs/capabilities/invocation.d.ts +36 -0
  342. package/dist/esm/structs/capabilities/model.d.ts +27 -0
  343. package/dist/esm/structs/capabilities/tools.d.ts +38 -0
  344. package/dist/esm/structs/common.d.ts +62 -2
  345. package/dist/esm/structs/common.js +11 -0
  346. package/dist/esm/structs/context/fragments.d.ts +28 -0
  347. package/dist/esm/structs/context/journal.d.ts +28 -3
  348. package/dist/esm/structs/context/processing.d.ts +11 -0
  349. package/dist/esm/structs/debug.d.ts +58 -1
  350. package/dist/esm/structs/debug.js +34 -5
  351. package/dist/esm/structs/diagnostics.d.ts +119 -1
  352. package/dist/esm/structs/errors.d.ts +42 -1
  353. package/dist/esm/structs/errors.js +26 -1
  354. package/dist/esm/structs/execution/engine.d.ts +24 -1
  355. package/dist/esm/structs/execution/events.d.ts +12 -0
  356. package/dist/esm/structs/execution/workflow.d.ts +71 -0
  357. package/dist/esm/structs/index.d.ts +11 -1
  358. package/dist/esm/structs/index.js +11 -1
  359. package/dist/esm/structs/invocation.d.ts +30 -0
  360. package/dist/esm/structs/kits/aggregation.d.ts +12 -0
  361. package/dist/esm/structs/kits/answering/checks.d.ts +11 -0
  362. package/dist/esm/structs/kits/answering/flow.d.ts +35 -1
  363. package/dist/esm/structs/kits/answering/state.d.ts +21 -0
  364. package/dist/esm/structs/kits/answering/tasks.d.ts +21 -0
  365. package/dist/esm/structs/kits/artifacts.d.ts +14 -0
  366. package/dist/esm/structs/kits/candidates.d.ts +19 -0
  367. package/dist/esm/structs/kits/delivery.d.ts +22 -0
  368. package/dist/esm/structs/kits/durable.d.ts +56 -3
  369. package/dist/esm/structs/kits/durable.js +7 -0
  370. package/dist/esm/structs/kits/evidence.d.ts +23 -0
  371. package/dist/esm/structs/kits/family.d.ts +49 -1
  372. package/dist/esm/structs/kits/family.js +1 -0
  373. package/dist/esm/structs/kits/intents/catalog.d.ts +26 -0
  374. package/dist/esm/structs/kits/intents/registry.d.ts +34 -0
  375. package/dist/esm/structs/kits/intents/scopes.d.ts +36 -1
  376. package/dist/esm/structs/kits/interaction.d.ts +64 -0
  377. package/dist/esm/structs/kits/interaction.js +8 -1
  378. package/dist/esm/structs/kits/knowledge-publication.d.ts +43 -1
  379. package/dist/esm/structs/kits/knowledge-publication.js +1 -0
  380. package/dist/esm/structs/kits/knowledge.d.ts +39 -2
  381. package/dist/esm/structs/kits/normalization.d.ts +15 -0
  382. package/dist/esm/structs/kits/policies.d.ts +25 -1
  383. package/dist/esm/structs/kits/presentation/events.d.ts +28 -4
  384. package/dist/esm/structs/kits/presentation/outputs.d.ts +38 -1
  385. package/dist/esm/structs/kits/presentation/templates.d.ts +15 -0
  386. package/dist/esm/structs/kits/projection.d.ts +21 -0
  387. package/dist/esm/structs/kits/reflection.d.ts +61 -2
  388. package/dist/esm/structs/kits/reflection.js +18 -3
  389. package/dist/esm/structs/kits/retrieval.d.ts +38 -0
  390. package/dist/esm/structs/kits/search.d.ts +22 -0
  391. package/dist/esm/structs/kits/text-skills.d.ts +36 -0
  392. package/dist/esm/structs/kits/tools.d.ts +87 -0
  393. package/dist/esm/structs/model/chat-provider.d.ts +24 -1
  394. package/dist/esm/structs/model/chat-provider.js +6 -0
  395. package/dist/esm/structs/model/interactions.d.ts +38 -0
  396. package/dist/esm/structs/model/messages.d.ts +30 -0
  397. package/dist/esm/structs/model/runtime.d.ts +104 -6
  398. package/dist/esm/structs/support/capabilities.d.ts +19 -2
  399. package/dist/esm/structs/support/context-processing.d.ts +88 -9
  400. package/dist/esm/structs/support/context.d.ts +29 -1
  401. package/dist/esm/structs/support/model/completion.d.ts +50 -0
  402. package/dist/esm/structs/support/model/completion.js +1 -0
  403. package/dist/esm/structs/support/model/interactions.d.ts +13 -1
  404. package/dist/esm/structs/support/model/messages.d.ts +52 -0
  405. package/dist/esm/structs/support/model/messages.js +1 -0
  406. package/dist/esm/structs/support/model/provider-observer.d.ts +22 -1
  407. package/dist/esm/structs/support/model/provider.d.ts +71 -6
  408. package/dist/esm/structs/support/registration.d.ts +13 -0
  409. package/dist/esm/structs/support/schema.d.ts +14 -0
  410. package/dist/esm/structs/support/tools.d.ts +28 -0
  411. package/dist/esm/structs/support/transport/json.d.ts +16 -0
  412. package/dist/esm/structs/support/transport/sse.d.ts +15 -0
  413. package/dist/esm/structs/utils/rules.d.ts +19 -0
  414. package/dist/esm/support/capabilities.d.ts +25 -2
  415. package/dist/esm/support/capabilities.js +43 -6
  416. package/dist/esm/support/context/assembly.d.ts +35 -4
  417. package/dist/esm/support/context/assembly.js +88 -16
  418. package/dist/esm/support/context/compression.d.ts +17 -1
  419. package/dist/esm/support/context/compression.js +48 -6
  420. package/dist/esm/support/context/fragments.d.ts +6 -0
  421. package/dist/esm/support/context/fragments.js +6 -0
  422. package/dist/esm/support/context/history.d.ts +43 -0
  423. package/dist/esm/support/context/history.js +36 -1
  424. package/dist/esm/support/context/index.d.ts +13 -2
  425. package/dist/esm/support/context/index.js +12 -1
  426. package/dist/esm/support/context/nodes.d.ts +49 -3
  427. package/dist/esm/support/context/nodes.js +165 -36
  428. package/dist/esm/support/context/processing.d.ts +145 -1
  429. package/dist/esm/support/context/processing.js +320 -62
  430. package/dist/esm/support/context/selection-policy.d.ts +42 -2
  431. package/dist/esm/support/context/selection-policy.js +98 -21
  432. package/dist/esm/support/context/selection.d.ts +14 -1
  433. package/dist/esm/support/context/selection.js +61 -10
  434. package/dist/esm/support/context/snapshot.d.ts +23 -1
  435. package/dist/esm/support/context/snapshot.js +29 -7
  436. package/dist/esm/support/context/strategies.d.ts +35 -2
  437. package/dist/esm/support/context/strategies.js +120 -28
  438. package/dist/esm/support/index.d.ts +9 -1
  439. package/dist/esm/support/index.js +9 -1
  440. package/dist/esm/support/model/capabilities.d.ts +10 -0
  441. package/dist/esm/support/model/capabilities.js +20 -1
  442. package/dist/esm/support/model/chat-completions.d.ts +44 -15
  443. package/dist/esm/support/model/chat-completions.js +92 -20
  444. package/dist/esm/support/model/children.d.ts +32 -2
  445. package/dist/esm/support/model/children.js +34 -6
  446. package/dist/esm/support/model/compatible-provider.d.ts +29 -3
  447. package/dist/esm/support/model/compatible-provider.js +190 -60
  448. package/dist/esm/support/model/completion/calls.d.ts +40 -0
  449. package/dist/esm/support/model/completion/calls.js +58 -0
  450. package/dist/esm/support/model/completion/default.d.ts +10 -0
  451. package/dist/esm/support/model/completion/default.js +29 -0
  452. package/dist/esm/support/model/completion/index.d.ts +10 -0
  453. package/dist/esm/support/model/completion/index.js +22 -0
  454. package/dist/esm/support/model/completion/session.d.ts +11 -0
  455. package/dist/esm/support/model/completion/session.js +99 -0
  456. package/dist/esm/support/model/index.d.ts +14 -0
  457. package/dist/esm/support/model/index.js +12 -0
  458. package/dist/esm/support/model/interactions/index.d.ts +1 -1
  459. package/dist/esm/support/model/interactions/index.js +12 -1
  460. package/dist/esm/support/model/interactions/json-object.d.ts +6 -0
  461. package/dist/esm/support/model/interactions/json-object.js +6 -0
  462. package/dist/esm/support/model/interactions/native-functions.d.ts +6 -0
  463. package/dist/esm/support/model/interactions/native-functions.js +6 -0
  464. package/dist/esm/support/model/observer.d.ts +25 -2
  465. package/dist/esm/support/model/observer.js +32 -6
  466. package/dist/esm/support/model/preview-rules.d.ts +20 -2
  467. package/dist/esm/support/model/preview-rules.js +66 -16
  468. package/dist/esm/support/model/reply-preview.d.ts +15 -1
  469. package/dist/esm/support/model/reply-preview.js +77 -17
  470. package/dist/esm/support/model/stage-transport.d.ts +9 -1
  471. package/dist/esm/support/model/stage-transport.js +33 -7
  472. package/dist/esm/support/model/strategies.d.ts +24 -2
  473. package/dist/esm/support/model/strategies.js +75 -14
  474. package/dist/esm/support/model/transports.d.ts +17 -1
  475. package/dist/esm/support/model/transports.js +53 -11
  476. package/dist/esm/support/registration.d.ts +20 -1
  477. package/dist/esm/support/registration.js +49 -7
  478. package/dist/esm/support/schema.d.ts +31 -1
  479. package/dist/esm/support/schema.js +46 -4
  480. package/dist/esm/support/stream-events.d.ts +22 -1
  481. package/dist/esm/support/stream-events.js +60 -8
  482. package/dist/esm/support/tool-runtime.d.ts +6 -0
  483. package/dist/esm/support/tool-runtime.js +6 -0
  484. package/dist/esm/support/tools.d.ts +22 -0
  485. package/dist/esm/support/tools.js +79 -10
  486. package/dist/esm/support/transport/index.d.ts +7 -0
  487. package/dist/esm/support/transport/index.js +7 -0
  488. package/dist/esm/support/transport/json.d.ts +29 -2
  489. package/dist/esm/support/transport/json.js +28 -4
  490. package/dist/esm/support/transport/sse.d.ts +31 -1
  491. package/dist/esm/support/transport/sse.js +25 -2
  492. package/dist/esm/types.d.ts +9 -1
  493. package/dist/esm/utils/break-observer.d.ts +27 -2
  494. package/dist/esm/utils/break-observer.js +20 -2
  495. package/dist/esm/utils/cancellation.d.ts +56 -5
  496. package/dist/esm/utils/cancellation.js +77 -9
  497. package/dist/esm/utils/catalog.d.ts +20 -1
  498. package/dist/esm/utils/catalog.js +19 -4
  499. package/dist/esm/utils/debug.d.ts +20 -1
  500. package/dist/esm/utils/debug.js +14 -2
  501. package/dist/esm/utils/index.d.ts +8 -0
  502. package/dist/esm/utils/index.js +8 -0
  503. package/dist/esm/utils/interaction-definition.d.ts +18 -1
  504. package/dist/esm/utils/interaction-definition.js +26 -6
  505. package/dist/esm/utils/invocation.d.ts +36 -1
  506. package/dist/esm/utils/invocation.js +42 -4
  507. package/dist/esm/utils/model-protocol.d.ts +8 -0
  508. package/dist/esm/utils/model-protocol.js +20 -1
  509. package/dist/esm/utils/object.d.ts +16 -0
  510. package/dist/esm/utils/object.js +16 -0
  511. package/dist/esm/utils/observer.d.ts +19 -1
  512. package/dist/esm/utils/observer.js +22 -2
  513. package/dist/esm/utils/protocol-registry.d.ts +38 -1
  514. package/dist/esm/utils/protocol-registry.js +47 -4
  515. package/dist/esm/utils/query-key.d.ts +11 -1
  516. package/dist/esm/utils/query-key.js +21 -2
  517. package/dist/esm/utils/registration.d.ts +34 -2
  518. package/dist/esm/utils/registration.js +18 -2
  519. package/dist/esm/utils/rules.d.ts +81 -4
  520. package/dist/esm/utils/rules.js +39 -11
  521. package/dist/esm/utils/schema-adapter.d.ts +12 -1
  522. package/dist/esm/utils/schema-adapter.js +25 -2
  523. package/dist/esm/utils/synchronous.d.ts +60 -4
  524. package/dist/esm/utils/synchronous.js +64 -7
  525. package/dist/esm/utils/text.d.ts +15 -1
  526. package/dist/esm/utils/text.js +15 -1
  527. package/package.json +23 -3
@@ -1,8 +1,46 @@
1
1
  import type { RecordedSubStep, SubFlowLogger, SubStepProgress, SubFlowValue } from '../structs/debug.js';
2
2
  import type { DiagnosticIdentity } from '../structs/diagnostics.js';
3
- /** Reject accessors, services, cycles and non-JSON values without evaluating application getters. */
3
+ /**
4
+ * Validate and deep-copy a reported payload.
5
+ *
6
+ * Accessors, class instances, cycles and non-JSON values are rejected without evaluating
7
+ * application getters, so a payload can neither run code nor alias live state. Accepted data is
8
+ * frozen.
9
+ *
10
+ * @param input Object to snapshot. It must be a plain object, not an array.
11
+ * @returns A deeply frozen JSON-shaped copy.
12
+ * @throws TypeError when the value is not plain JSON data, contains a getter, or is cyclic.
13
+ * @throws RangeError when node count, nesting depth, string length or array length exceeds the
14
+ * limits below.
15
+ */
4
16
  export declare function payloadSnapshot(input: unknown): Readonly<Record<string, SubFlowValue>>;
5
- /** Internal constructor; only active execution contexts issue logger instances. */
17
+ /**
18
+ * Create a logger bound to one active execution visit.
19
+ *
20
+ * Only execution contexts inside this package should call it: the resulting object is the sole
21
+ * way to append reported steps to the current record.
22
+ *
23
+ * @param owner Object that owns the visit. `readSubFlowLogger` compares this identity before
24
+ * returning any step.
25
+ * @param active Reports whether the visit is still open; a closed logger rejects writes.
26
+ * @param content Reports whether content-level detail is enabled. When false, summaries and
27
+ * payloads are dropped before anything is stored.
28
+ * @param changed Called after each accepted write or fix with the progress delta to publish.
29
+ * @param identity Diagnostic run and span the logger belongs to; used to reject cross-scope reads.
30
+ * @returns A frozen logger instance.
31
+ */
6
32
  export declare function createSubFlowLogger(owner: object, active: () => boolean, content: () => boolean, changed: (logger: SubFlowLogger, progress: SubStepProgress) => void, identity?: Readonly<DiagnosticIdentity>): SubFlowLogger;
7
- /** Never call a lookalike's readAll method. */
33
+ /**
34
+ * Read the steps recorded by a logger issued by this module.
35
+ *
36
+ * The instance is resolved through the private registry, so a lookalike's `readAll` method is
37
+ * never called and a logger cannot be borrowed across executions.
38
+ *
39
+ * @param input Candidate logger; must be an instance created by `createSubFlowLogger`.
40
+ * @param owner Expected owning execution visit. Omit to accept any owner.
41
+ * @param identity Expected run and span. Omit to accept any diagnostic scope.
42
+ * @returns The recorded steps in write order, as an immutable list shared with the logger.
43
+ * @throws TypeError when the value is not a registered logger, belongs to another owner, or
44
+ * belongs to another diagnostic scope.
45
+ */
8
46
  export declare function readSubFlowLogger(input: unknown, owner?: object, identity?: Readonly<DiagnosticIdentity>): readonly Readonly<RecordedSubStep>[];
@@ -1,16 +1,74 @@
1
+ /**
2
+ * Bounded, self-reported sub-flow logging.
3
+ *
4
+ * A node or tool may report the steps it believes it performed. Such reports are optional
5
+ * evidence, never proof of Core scheduling: they are marked `origin: 'reported'`, validated
6
+ * against a fixed field whitelist, and capped in count, text length and nested payload depth.
7
+ *
8
+ * Issued loggers are tracked in a module-private registry, so only the execution that created
9
+ * one can read it. A lookalike object with a `readAll` method is never accepted.
10
+ *
11
+ * Exports: `payloadSnapshot`, `createSubFlowLogger`, `readSubFlowLogger`.
12
+ */
1
13
  import { breakWhile } from '../utils/rules.js';
2
14
  import { invocationId } from '../utils/invocation.js';
3
15
  import { reportedStepStatusCatalog } from '../structs/debug.js';
16
+ /**
17
+ * Registry of issued loggers: owner, diagnostic identity and reader for each instance. Kept
18
+ * private so a structurally identical object cannot impersonate a real logger.
19
+ */
4
20
  const instances = new WeakMap();
5
- const writeFields = ['name', 'kind', 'parentStepId', 'relatedDefinitionId', 'status', 'usedModel', 'startedAtMs', 'endedAtMs', 'nextStepIds', 'caseId', 'summary', 'payload'];
6
- const fixFields = ['status', 'usedModel', 'startedAtMs', 'endedAtMs', 'summary', 'payload', 'nextStepIds'];
7
- const textLimits = { name: 256, kind: 128, parentStepId: 256, relatedDefinitionId: 256, caseId: 256, summary: 1024 };
21
+ /** Fields accepted by `write`; anything else is rejected so a declaration cannot smuggle state. */
22
+ const writeFields = [
23
+ 'name',
24
+ 'kind',
25
+ 'parentStepId',
26
+ 'relatedDefinitionId',
27
+ 'status',
28
+ 'usedModel',
29
+ 'startedAtMs',
30
+ 'endedAtMs',
31
+ 'nextStepIds',
32
+ 'caseId',
33
+ 'summary',
34
+ 'payload',
35
+ ];
36
+ /** Fields accepted by `fix`: identity and creation data are intentionally not patchable. */
37
+ const fixFields = [
38
+ 'status',
39
+ 'usedModel',
40
+ 'startedAtMs',
41
+ 'endedAtMs',
42
+ 'summary',
43
+ 'payload',
44
+ 'nextStepIds',
45
+ ];
46
+ /** Per-field character caps, bounding what one reported step can add to a persisted record. */
47
+ const textLimits = {
48
+ name: 256,
49
+ kind: 128,
50
+ parentStepId: 256,
51
+ relatedDefinitionId: 256,
52
+ caseId: 256,
53
+ summary: 1024,
54
+ };
55
+ /**
56
+ * Validate a step input: plain prototype, whitelisted keys, own data properties only.
57
+ *
58
+ * Property descriptors are inspected instead of values being read, so an accessor on the input
59
+ * cannot execute application code during validation.
60
+ */
8
61
  function fields(input, allowed) {
9
62
  breakWhile(!input || Object.getPrototypeOf(input) !== Object.prototype, () => new TypeError('Sub-flow input must be a plain parameter object'));
10
- breakWhile(Reflect.ownKeys(input).some(key => typeof key !== 'string' || !allowed.includes(key)), () => new TypeError('Unknown sub-flow field'));
63
+ breakWhile(Reflect.ownKeys(input).some((key) => typeof key !== 'string' || !allowed.includes(key)), () => new TypeError('Unknown sub-flow field'));
11
64
  for (const key of Reflect.ownKeys(input))
12
- breakWhile(!['payload', 'summary'].includes(String(key)) && !('value' in Object.getOwnPropertyDescriptor(input, key)), () => new TypeError('Sub-flow metadata must be plain data'));
65
+ breakWhile(!['payload', 'summary'].includes(String(key)) &&
66
+ !('value' in Object.getOwnPropertyDescriptor(input, key)), () => new TypeError('Sub-flow metadata must be plain data'));
13
67
  }
68
+ /**
69
+ * Check a fully built step: text caps, status vocabulary, time ordering, and the rules tying
70
+ * timestamps to planned and terminal statuses.
71
+ */
14
72
  function validate(step) {
15
73
  for (const [key, limit] of Object.entries(textLimits)) {
16
74
  const value = step[key];
@@ -20,15 +78,33 @@ function validate(step) {
20
78
  breakWhile(step.usedModel !== undefined && typeof step.usedModel !== 'boolean', () => new TypeError('Invalid sub-flow usedModel'));
21
79
  for (const time of [step.startedAtMs, step.endedAtMs])
22
80
  breakWhile(time !== undefined && (!Number.isFinite(time) || time < 0), () => new TypeError('Invalid sub-flow time'));
23
- breakWhile(step.startedAtMs !== undefined && step.endedAtMs !== undefined && step.endedAtMs < step.startedAtMs, () => new TypeError('Invalid sub-flow time order'));
81
+ breakWhile(step.startedAtMs !== undefined &&
82
+ step.endedAtMs !== undefined &&
83
+ step.endedAtMs < step.startedAtMs, () => new TypeError('Invalid sub-flow time order'));
24
84
  breakWhile(step.endedAtMs !== undefined && !reportedStepStatusCatalog[step.status].terminal, () => new TypeError('Only terminal sub-flow steps have an end time'));
25
85
  breakWhile(['planned', 'skipped'].includes(step.status) && step.startedAtMs !== undefined, () => new TypeError('A planned or skipped sub-flow step has not started'));
26
- breakWhile(step.nextStepIds !== undefined && (!Array.isArray(step.nextStepIds) || step.nextStepIds.length > 64 || new Set(step.nextStepIds).size !== step.nextStepIds.length
27
- || step.nextStepIds.some(id => typeof id !== 'string' || !id.trim() || id.length > 256)), () => new TypeError('Invalid sub-flow successors'));
86
+ breakWhile(step.nextStepIds !== undefined &&
87
+ (!Array.isArray(step.nextStepIds) ||
88
+ step.nextStepIds.length > 64 ||
89
+ new Set(step.nextStepIds).size !== step.nextStepIds.length ||
90
+ step.nextStepIds.some((id) => typeof id !== 'string' || !id.trim() || id.length > 256)), () => new TypeError('Invalid sub-flow successors'));
28
91
  }
29
- /** Reject accessors, services, cycles and non-JSON values without evaluating application getters. */
92
+ /**
93
+ * Validate and deep-copy a reported payload.
94
+ *
95
+ * Accessors, class instances, cycles and non-JSON values are rejected without evaluating
96
+ * application getters, so a payload can neither run code nor alias live state. Accepted data is
97
+ * frozen.
98
+ *
99
+ * @param input Object to snapshot. It must be a plain object, not an array.
100
+ * @returns A deeply frozen JSON-shaped copy.
101
+ * @throws TypeError when the value is not plain JSON data, contains a getter, or is cyclic.
102
+ * @throws RangeError when node count, nesting depth, string length or array length exceeds the
103
+ * limits below.
104
+ */
30
105
  export function payloadSnapshot(input) {
31
106
  let nodes = 0;
107
+ // Ancestors rather than all visited nodes: a shared subtree is fine, a cycle is not.
32
108
  const ancestors = new Set();
33
109
  const copy = (value, depth) => {
34
110
  breakWhile(++nodes > 4096 || depth > 16, () => new RangeError('Sub-flow payload is too complex'));
@@ -37,7 +113,11 @@ export function payloadSnapshot(input) {
37
113
  return value;
38
114
  if (typeof value === 'number' && Number.isFinite(value))
39
115
  return value;
40
- breakWhile(!value || typeof value !== 'object' || (typeof value === 'object' && value !== null && ancestors.has(value)) || (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype), () => new TypeError('Sub-flow payload must contain JSON data'));
116
+ breakWhile(!value ||
117
+ typeof value !== 'object' ||
118
+ (typeof value === 'object' && value !== null && ancestors.has(value)) ||
119
+ // Only plain objects survive: a class instance may hide lazy state behind getters.
120
+ (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype), () => new TypeError('Sub-flow payload must contain JSON data'));
41
121
  breakWhile(Array.isArray(value) && value.length > 4096, () => new RangeError('Sub-flow payload array is too large'));
42
122
  const data = value;
43
123
  ancestors.add(data);
@@ -46,21 +126,43 @@ export function payloadSnapshot(input) {
46
126
  if (Array.isArray(value) && key === 'length')
47
127
  continue;
48
128
  const field = Object.getOwnPropertyDescriptor(value, key);
49
- breakWhile(typeof key !== 'string' || !('value' in field) || !field.enumerable || (Array.isArray(value) && !/^(0|[1-9]\d*)$/.test(key)), () => new TypeError('Sub-flow payload must contain plain data fields'));
129
+ breakWhile(typeof key !== 'string' ||
130
+ !('value' in field) ||
131
+ !field.enumerable ||
132
+ // Array keys must be canonical indices; extra or sparse keys cannot survive JSON.
133
+ (Array.isArray(value) && !/^(0|[1-9]\d*)$/.test(key)), () => new TypeError('Sub-flow payload must contain plain data fields'));
50
134
  Object.defineProperty(result, key, { value: copy(field.value, depth + 1), enumerable: true });
51
135
  }
52
136
  ancestors.delete(data);
53
- return Array.isArray(value) ? Object.freeze(Array.from({ length: value.length }, (_, i) => result[i] ?? null)) : Object.freeze(result);
137
+ return Array.isArray(value)
138
+ ? Object.freeze(Array.from({ length: value.length }, (_, i) => result[i] ?? null))
139
+ : Object.freeze(result);
54
140
  };
55
141
  breakWhile(!input || Array.isArray(input) || Object.getPrototypeOf(input) !== Object.prototype, () => new TypeError('Sub-flow payload must be an object'));
56
142
  return copy(input, 0);
57
143
  }
144
+ /**
145
+ * Merge a payload patch into a recorded payload without replacing recorded facts.
146
+ *
147
+ * Nested objects merge recursively; an existing leaf must keep its value. A mismatch is
148
+ * rejected rather than overwritten, because a fix may add results but not rewrite history.
149
+ */
58
150
  function fill(previous, patch) {
59
151
  const result = { ...previous };
60
152
  for (const [key, value] of Object.entries(patch)) {
61
153
  if (!Object.hasOwn(previous, key))
62
- Object.defineProperty(result, key, { value, enumerable: true, configurable: true, writable: true });
63
- else if (value && previous[key] && typeof value === 'object' && typeof previous[key] === 'object' && !Array.isArray(value) && !Array.isArray(previous[key])) {
154
+ Object.defineProperty(result, key, {
155
+ value,
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ });
160
+ else if (value &&
161
+ previous[key] &&
162
+ typeof value === 'object' &&
163
+ typeof previous[key] === 'object' &&
164
+ !Array.isArray(value) &&
165
+ !Array.isArray(previous[key])) {
64
166
  result[key] = fill(previous[key], value);
65
167
  }
66
168
  else
@@ -68,87 +170,185 @@ function fill(previous, patch) {
68
170
  }
69
171
  return Object.freeze(result);
70
172
  }
71
- /** Internal constructor; only active execution contexts issue logger instances. */
173
+ /**
174
+ * Create a logger bound to one active execution visit.
175
+ *
176
+ * Only execution contexts inside this package should call it: the resulting object is the sole
177
+ * way to append reported steps to the current record.
178
+ *
179
+ * @param owner Object that owns the visit. `readSubFlowLogger` compares this identity before
180
+ * returning any step.
181
+ * @param active Reports whether the visit is still open; a closed logger rejects writes.
182
+ * @param content Reports whether content-level detail is enabled. When false, summaries and
183
+ * payloads are dropped before anything is stored.
184
+ * @param changed Called after each accepted write or fix with the progress delta to publish.
185
+ * @param identity Diagnostic run and span the logger belongs to; used to reject cross-scope reads.
186
+ * @returns A frozen logger instance.
187
+ */
72
188
  export function createSubFlowLogger(owner, active, content, changed, identity) {
73
189
  class Logger {
74
190
  #steps = Object.freeze([]);
75
191
  #changes = 0;
76
- constructor() { instances.set(this, { owner, identity, read: () => this.#steps }); Object.freeze(this); }
77
- #assertActive() { breakWhile(!active(), () => new TypeError('Sub-flow logger belongs to a closed execution')); }
192
+ constructor() {
193
+ instances.set(this, { owner, identity, read: () => this.#steps });
194
+ Object.freeze(this);
195
+ }
196
+ #assertActive() {
197
+ // A logger that outlived its execution would append evidence to a finished request.
198
+ breakWhile(!active(), () => new TypeError('Sub-flow logger belongs to a closed execution'));
199
+ }
78
200
  #save(step, operation) {
79
201
  this.#assertActive();
80
- const next = operation === 'write' ? [...this.#steps, step] : this.#steps.map(item => item.stepId === step.stepId ? step : item);
81
- breakWhile(step.nextStepIds?.some(id => !next.some(item => item.stepId === id)), () => new TypeError('Sub-flow successors must belong to this logger'));
202
+ // A fix replaces the step with the same id in place; a write always appends.
203
+ const next = operation === 'write'
204
+ ? [...this.#steps, step]
205
+ : this.#steps.map((item) => (item.stepId === step.stepId ? step : item));
206
+ // Successors must resolve inside this logger, otherwise the recorded graph would have
207
+ // dangling edges.
208
+ breakWhile(step.nextStepIds?.some((id) => !next.some((item) => item.stepId === id)), () => new TypeError('Sub-flow successors must belong to this logger'));
209
+ // The step and size caps equal the persisted-record limits; the change cap bounds
210
+ // write/fix churn even when the final step count stays small.
82
211
  breakWhile(next.length > 256 || this.#changes >= 1024 || JSON.stringify(next).length > 65536, () => new RangeError('Sub-flow log limit exceeded'));
83
212
  this.#steps = Object.freeze(next);
84
213
  this.#changes++;
85
- changed(this, { operation, stepId: step.stepId, sequence: step.sequence, revision: step.revision, createdAtMs: step.createdAtMs, status: step.status, ...(step.usedModel === undefined ? {} : { usedModel: step.usedModel }) });
214
+ changed(this, {
215
+ operation,
216
+ stepId: step.stepId,
217
+ sequence: step.sequence,
218
+ revision: step.revision,
219
+ createdAtMs: step.createdAtMs,
220
+ status: step.status,
221
+ ...(step.usedModel === undefined ? {} : { usedModel: step.usedModel }),
222
+ });
86
223
  }
87
224
  write(input) {
88
225
  this.#assertActive();
89
226
  fields(input, writeFields);
90
227
  breakWhile(input.nextStepIds !== undefined && !Array.isArray(input.nextStepIds), () => new TypeError('Invalid sub-flow successors'));
91
228
  const now = Date.now(), status = input.status === undefined ? 'planned' : input.status;
92
- const step = { origin: 'reported', stepId: invocationId(), sequence: this.#steps.length + 1, revision: 1, createdAtMs: now,
93
- name: input.name, kind: input.kind, status,
229
+ // Timestamps are derived from the status: a running or terminal step gets a start time,
230
+ // a terminal step also an end time, using the caller's value when one is supplied.
231
+ const step = {
232
+ origin: 'reported',
233
+ stepId: invocationId(),
234
+ sequence: this.#steps.length + 1,
235
+ revision: 1,
236
+ createdAtMs: now,
237
+ name: input.name,
238
+ kind: input.kind,
239
+ status,
94
240
  ...(input.parentStepId === undefined ? {} : { parentStepId: input.parentStepId }),
95
- ...(input.relatedDefinitionId === undefined ? {} : { relatedDefinitionId: input.relatedDefinitionId }),
241
+ ...(input.relatedDefinitionId === undefined
242
+ ? {}
243
+ : { relatedDefinitionId: input.relatedDefinitionId }),
96
244
  ...(input.caseId === undefined ? {} : { caseId: input.caseId }),
97
245
  ...(input.usedModel === undefined ? {} : { usedModel: input.usedModel }),
98
- ...(input.startedAtMs === undefined && status !== 'running' ? {} : { startedAtMs: input.startedAtMs ?? now }),
99
- ...(input.endedAtMs === undefined && !reportedStepStatusCatalog[status]?.terminal ? {} : { endedAtMs: input.endedAtMs ?? now }),
100
- ...(input.nextStepIds === undefined ? {} : { nextStepIds: Object.freeze([...input.nextStepIds]) }),
246
+ ...(input.startedAtMs === undefined && status !== 'running'
247
+ ? {}
248
+ : { startedAtMs: input.startedAtMs ?? now }),
249
+ ...(input.endedAtMs === undefined && !reportedStepStatusCatalog[status]?.terminal
250
+ ? {}
251
+ : { endedAtMs: input.endedAtMs ?? now }),
252
+ ...(input.nextStepIds === undefined
253
+ ? {}
254
+ : { nextStepIds: Object.freeze([...input.nextStepIds]) }),
101
255
  };
102
256
  if (content()) {
257
+ // Summaries and payloads are omitted entirely when content logging is off, so a
258
+ // metadata-only consumer never receives them.
103
259
  if (input.summary !== undefined)
104
260
  Object.assign(step, { summary: input.summary });
105
261
  if (input.payload !== undefined)
106
262
  Object.assign(step, { payload: payloadSnapshot(input.payload) });
107
263
  }
108
264
  validate(step);
109
- breakWhile(step.parentStepId && !this.#steps.some(item => item.stepId === step.parentStepId), () => new TypeError('Sub-flow parent must belong to this logger'));
265
+ breakWhile(step.parentStepId && !this.#steps.some((item) => item.stepId === step.parentStepId), () => new TypeError('Sub-flow parent must belong to this logger'));
110
266
  this.#save(Object.freeze(step), 'write');
111
267
  return step.stepId;
112
268
  }
113
269
  fix(stepId, patch) {
114
270
  this.#assertActive();
115
271
  fields(patch, fixFields);
116
- const previous = this.#steps.find(step => step.stepId === stepId);
272
+ const previous = this.#steps.find((step) => step.stepId === stepId);
273
+ // The assertion above only satisfies the type checker; this check rejects an unknown id.
117
274
  breakWhile(!previous, () => new TypeError('Unknown sub-flow step'));
118
275
  const terminal = reportedStepStatusCatalog[previous.status].terminal;
276
+ // A terminal step is sealed: its outcome, model use and timestamps cannot be rewritten.
119
277
  breakWhile(terminal && patch.status !== undefined && patch.status !== previous.status, () => new TypeError('Terminal sub-flow status is sealed'));
120
- breakWhile(terminal && previous.usedModel !== undefined && patch.usedModel !== undefined && previous.usedModel !== patch.usedModel, () => new TypeError('Terminal sub-flow model use is sealed'));
278
+ breakWhile(terminal &&
279
+ previous.usedModel !== undefined &&
280
+ patch.usedModel !== undefined &&
281
+ previous.usedModel !== patch.usedModel, () => new TypeError('Terminal sub-flow model use is sealed'));
121
282
  breakWhile(previous.usedModel === true && patch.usedModel === false, () => new TypeError('Cannot undo reported model use'));
122
- breakWhile(patch.status !== undefined && patch.status !== previous.status && !reportedStepStatusCatalog[previous.status].next.includes(patch.status), () => new TypeError('Invalid sub-flow status transition'));
283
+ breakWhile(patch.status !== undefined &&
284
+ patch.status !== previous.status &&
285
+ // Transitions are catalog-driven; a terminal status has no successors at all.
286
+ !reportedStepStatusCatalog[previous.status].next.includes(patch.status), () => new TypeError('Invalid sub-flow status transition'));
123
287
  for (const key of ['startedAtMs', 'endedAtMs'])
124
288
  breakWhile(previous[key] !== undefined && patch[key] !== undefined && patch[key] !== previous[key], () => new TypeError('Cannot overwrite a sub-flow time'));
125
289
  const status = patch.status ?? previous.status;
126
- breakWhile(patch.nextStepIds !== undefined && (!Array.isArray(patch.nextStepIds) || (previous.nextStepIds !== undefined && JSON.stringify(previous.nextStepIds) !== JSON.stringify(patch.nextStepIds))), () => new TypeError('Cannot overwrite sub-flow successors'));
127
- const step = { ...previous, revision: previous.revision + 1, status,
128
- ...(patch.nextStepIds === undefined ? {} : { nextStepIds: Object.freeze([...patch.nextStepIds]) }),
290
+ breakWhile(patch.nextStepIds !== undefined &&
291
+ (!Array.isArray(patch.nextStepIds) ||
292
+ (previous.nextStepIds !== undefined &&
293
+ JSON.stringify(previous.nextStepIds) !== JSON.stringify(patch.nextStepIds))), () => new TypeError('Cannot overwrite sub-flow successors'));
294
+ // A start time is auto-stamped only when the step becomes running and had none; an end
295
+ // time is added once the status becomes terminal.
296
+ const step = {
297
+ ...previous,
298
+ revision: previous.revision + 1,
299
+ status,
300
+ ...(patch.nextStepIds === undefined
301
+ ? {}
302
+ : { nextStepIds: Object.freeze([...patch.nextStepIds]) }),
129
303
  ...(patch.usedModel === undefined ? {} : { usedModel: patch.usedModel }),
130
- ...(patch.startedAtMs === undefined && !(status === 'running' && previous.startedAtMs === undefined) ? {} : { startedAtMs: patch.startedAtMs ?? Date.now() }),
131
- ...(patch.endedAtMs === undefined && (!reportedStepStatusCatalog[status]?.terminal || previous.endedAtMs !== undefined) ? {} : { endedAtMs: patch.endedAtMs ?? Date.now() }),
304
+ ...(patch.startedAtMs === undefined &&
305
+ !(status === 'running' && previous.startedAtMs === undefined)
306
+ ? {}
307
+ : { startedAtMs: patch.startedAtMs ?? Date.now() }),
308
+ ...(patch.endedAtMs === undefined &&
309
+ (!reportedStepStatusCatalog[status]?.terminal || previous.endedAtMs !== undefined)
310
+ ? {}
311
+ : { endedAtMs: patch.endedAtMs ?? Date.now() }),
132
312
  };
133
313
  if (content()) {
314
+ // A fix may add a summary once and fill missing payload fields, but it cannot replace
315
+ // a summary or a recorded payload value.
134
316
  if (patch.summary !== undefined) {
135
317
  breakWhile(previous.summary !== undefined && previous.summary !== patch.summary, () => new TypeError('Cannot overwrite a sub-flow summary'));
136
318
  Object.assign(step, { summary: patch.summary });
137
319
  }
138
320
  if (patch.payload !== undefined)
139
- Object.assign(step, { payload: fill(previous.payload ?? {}, payloadSnapshot(patch.payload)) });
321
+ Object.assign(step, {
322
+ payload: fill(previous.payload ?? {}, payloadSnapshot(patch.payload)),
323
+ });
140
324
  }
141
325
  validate(step);
142
326
  this.#save(Object.freeze(step), 'fix');
143
327
  }
144
- readAll() { return this.#steps; }
328
+ readAll() {
329
+ return this.#steps;
330
+ }
145
331
  }
146
332
  return new Logger();
147
333
  }
148
- /** Never call a lookalike's readAll method. */
334
+ /**
335
+ * Read the steps recorded by a logger issued by this module.
336
+ *
337
+ * The instance is resolved through the private registry, so a lookalike's `readAll` method is
338
+ * never called and a logger cannot be borrowed across executions.
339
+ *
340
+ * @param input Candidate logger; must be an instance created by `createSubFlowLogger`.
341
+ * @param owner Expected owning execution visit. Omit to accept any owner.
342
+ * @param identity Expected run and span. Omit to accept any diagnostic scope.
343
+ * @returns The recorded steps in write order, as an immutable list shared with the logger.
344
+ * @throws TypeError when the value is not a registered logger, belongs to another owner, or
345
+ * belongs to another diagnostic scope.
346
+ */
149
347
  export function readSubFlowLogger(input, owner, identity) {
150
348
  const instance = input && typeof input === 'object' ? instances.get(input) : undefined;
151
- breakWhile(!instance || (owner !== undefined && owner !== instance.owner), () => new TypeError('Expected this execution\'s SubFlowLogger instance'));
152
- breakWhile(identity && (instance.identity?.runId !== identity.runId || instance.identity?.spanId !== identity.spanId), () => new TypeError('Sub-flow logger belongs to another diagnostic scope'));
349
+ breakWhile(!instance || (owner !== undefined && owner !== instance.owner), () => new TypeError("Expected this execution's SubFlowLogger instance"));
350
+ breakWhile(identity &&
351
+ (instance.identity?.runId !== identity.runId ||
352
+ instance.identity?.spanId !== identity.spanId), () => new TypeError('Sub-flow logger belongs to another diagnostic scope'));
153
353
  return instance.read();
154
354
  }
@@ -1,5 +1,25 @@
1
1
  import type { AgentErrorInfo, ErrorContext } from './structs/errors.js';
2
- /** Internal boundary annotation. Re-throws retain the originating stage and provider details. */
2
+ /**
3
+ * Attach boundary metadata to a thrown value without changing that value.
4
+ *
5
+ * The earliest recorded stage stays authoritative on re-throw, so a failure is attributed
6
+ * to the stage that first caught it rather than to the boundary that merely relays it.
7
+ * Codes and retryability may still be filled in by an outer boundary that knows more.
8
+ *
9
+ * @param error Value being propagated. Non-object values are ignored: identity is tracked
10
+ * by reference, so primitives cannot carry annotations.
11
+ * @param context Boundary annotations to merge into the existing entry, if any.
12
+ * @returns The same reference, allowing `throw recordErrorContext(error, ctx)`.
13
+ */
3
14
  export declare function recordErrorContext(error: unknown, context: ErrorContext): unknown;
4
- /** A local diagnostic view; hostile metadata must never replace the original failure. */
15
+ /**
16
+ * Produce a frozen diagnostic view of any thrown value.
17
+ *
18
+ * Inspection is guarded: an exotic error (revoked proxy, throwing getter, null prototype)
19
+ * degrades to `UNKNOWN_ERROR` instead of raising a second failure while handling the first.
20
+ *
21
+ * @param error Any thrown value, including non-Error values such as strings.
22
+ * @returns Frozen info. `codes` lists every known code with the primary one first; optional
23
+ * fields are omitted rather than emitted as `undefined`. `cause` is the original value.
24
+ */
5
25
  export declare function getErrorInfo(error: unknown): AgentErrorInfo;
@@ -1,8 +1,29 @@
1
+ /**
2
+ * Error metadata boundary: records which execution stage produced a failure and exposes a
3
+ * stable, read-only view of it for diagnostics.
4
+ *
5
+ * The original thrown value is never wrapped or replaced, so App code keeps its own error
6
+ * identity and stack. Annotations live in a side table keyed by reference, which works for
7
+ * frozen Errors and DOMException instances whose properties cannot be added.
8
+ *
9
+ * Exports: `recordErrorContext` (annotate at a boundary) and `getErrorInfo` (inspect).
10
+ */
1
11
  import { BudgetError, ValidationError } from './structs/errors.js';
2
12
  // Side metadata preserves App error identity, including frozen Errors and DOMException.
3
13
  const contexts = new WeakMap();
4
14
  const reference = (value) => value !== null && (typeof value === 'object' || typeof value === 'function');
5
- /** Internal boundary annotation. Re-throws retain the originating stage and provider details. */
15
+ /**
16
+ * Attach boundary metadata to a thrown value without changing that value.
17
+ *
18
+ * The earliest recorded stage stays authoritative on re-throw, so a failure is attributed
19
+ * to the stage that first caught it rather than to the boundary that merely relays it.
20
+ * Codes and retryability may still be filled in by an outer boundary that knows more.
21
+ *
22
+ * @param error Value being propagated. Non-object values are ignored: identity is tracked
23
+ * by reference, so primitives cannot carry annotations.
24
+ * @param context Boundary annotations to merge into the existing entry, if any.
25
+ * @returns The same reference, allowing `throw recordErrorContext(error, ctx)`.
26
+ */
6
27
  export function recordErrorContext(error, context) {
7
28
  if (reference(error)) {
8
29
  const previous = contexts.get(error);
@@ -11,34 +32,86 @@ export function recordErrorContext(error, context) {
11
32
  }
12
33
  return error;
13
34
  }
14
- /** Inspect external metadata inside the guarded diagnostic boundary below. */
35
+ /**
36
+ * Read classification metadata from an arbitrary thrown value.
37
+ *
38
+ * Returns normalized issues plus a primary code. `isRetryable` is left undefined when the
39
+ * error neither states it nor makes a final classification, so callers do not mistake
40
+ * "unknown" for "retryable".
41
+ */
15
42
  function inspectError(error) {
16
- const source = reference(error) ? error : {};
43
+ const source = reference(error)
44
+ ? error
45
+ : {};
17
46
  const reportedIssues = source.issues;
47
+ // Structured issues win over the older `errors` array, so an error carrying both is not
48
+ // reported twice. Only the array shape is accepted; any other value falls through.
18
49
  const reportedErrors = Array.isArray(reportedIssues) ? undefined : source.errors;
19
- const raw = Array.isArray(reportedIssues) ? reportedIssues : Array.isArray(reportedErrors) ? reportedErrors : [];
20
- const issues = raw.flatMap(value => {
50
+ const raw = Array.isArray(reportedIssues)
51
+ ? reportedIssues
52
+ : Array.isArray(reportedErrors)
53
+ ? reportedErrors
54
+ : [];
55
+ const issues = raw.flatMap((value) => {
21
56
  if (!value || typeof value !== 'object')
22
57
  return [];
23
58
  const { code, message, path: reportedPath, checkId } = value;
24
59
  if (typeof code !== 'string')
25
60
  return [];
26
- const pathItems = typeof reportedPath === 'string' ? [reportedPath] : Array.isArray(reportedPath) ? Array.from(reportedPath) : undefined;
27
- const path = pathItems?.every((part) => typeof part === 'string' || typeof part === 'number') ? pathItems : undefined;
28
- return [{ code, message: typeof message === 'string' ? message : code,
61
+ const pathItems = typeof reportedPath === 'string'
62
+ ? [reportedPath]
63
+ : Array.isArray(reportedPath)
64
+ ? Array.from(reportedPath)
65
+ : undefined;
66
+ const path = pathItems?.every((part) => typeof part === 'string' || typeof part === 'number')
67
+ ? pathItems
68
+ : undefined;
69
+ return [
70
+ {
71
+ code,
72
+ message: typeof message === 'string' ? message : code,
29
73
  ...(path === undefined ? {} : { path: Object.freeze(path) }),
30
- ...(typeof checkId === 'string' ? { checkId } : {}) }];
74
+ ...(typeof checkId === 'string' ? { checkId } : {}),
75
+ },
76
+ ];
31
77
  });
32
78
  const code = source.code;
79
+ // A string `code` or a structured issue is already a usable classification, so the error
80
+ // `name` is only consulted as a last resort.
33
81
  const name = typeof code === 'string' || issues.length ? undefined : source.name;
34
- const fallback = typeof code === 'string' ? code : issues[0]?.code
35
- ?? (name === 'AbortError' ? 'CANCELLED' : name === 'TimeoutError' ? 'TIMEOUT' : 'UNKNOWN_ERROR');
82
+ // DOMException exposes cancellation through `name` rather than `code`; the mapping keeps
83
+ // cancellation and timeout distinguishable from genuine failures.
84
+ const fallback = typeof code === 'string'
85
+ ? code
86
+ : (issues[0]?.code ??
87
+ (name === 'AbortError'
88
+ ? 'CANCELLED'
89
+ : name === 'TimeoutError'
90
+ ? 'TIMEOUT'
91
+ : 'UNKNOWN_ERROR'));
36
92
  const retryable = source.isRetryable;
37
- const isRetryable = typeof retryable === 'boolean' ? retryable
38
- : error instanceof BudgetError || error instanceof ValidationError || issues.length || ['CANCELLED', 'TIMEOUT', 'BUDGET'].includes(fallback) ? false : undefined;
93
+ // Retryability stays unknown unless the error reports it or it is provably final:
94
+ // budget, validation, issue-bearing and cancellation failures must not be retried.
95
+ const isRetryable = typeof retryable === 'boolean'
96
+ ? retryable
97
+ : error instanceof BudgetError ||
98
+ error instanceof ValidationError ||
99
+ issues.length ||
100
+ ['CANCELLED', 'TIMEOUT', 'BUDGET'].includes(fallback)
101
+ ? false
102
+ : undefined;
39
103
  return { code: fallback, issues, isRetryable };
40
104
  }
41
- /** A local diagnostic view; hostile metadata must never replace the original failure. */
105
+ /**
106
+ * Produce a frozen diagnostic view of any thrown value.
107
+ *
108
+ * Inspection is guarded: an exotic error (revoked proxy, throwing getter, null prototype)
109
+ * degrades to `UNKNOWN_ERROR` instead of raising a second failure while handling the first.
110
+ *
111
+ * @param error Any thrown value, including non-Error values such as strings.
112
+ * @returns Frozen info. `codes` lists every known code with the primary one first; optional
113
+ * fields are omitted rather than emitted as `undefined`. `cause` is the original value.
114
+ */
42
115
  export function getErrorInfo(error) {
43
116
  const context = reference(error) ? contexts.get(error) : undefined;
44
117
  let view;
@@ -49,9 +122,18 @@ export function getErrorInfo(error) {
49
122
  view = { code: 'UNKNOWN_ERROR', issues: [] };
50
123
  }
51
124
  const { code: fallback, issues } = view;
52
- const codes = Object.freeze([...(context?.codes ?? (issues.length ? issues.map(issue => issue.code) : [fallback]))]);
125
+ // Recorded codes describe the boundary where the error was first captured and outrank
126
+ // codes inferred from the current shape, which may already have been flattened.
127
+ const codes = Object.freeze([
128
+ ...(context?.codes ?? (issues.length ? issues.map((issue) => issue.code) : [fallback])),
129
+ ]);
53
130
  const isRetryable = context?.isRetryable ?? view.isRetryable;
54
- return Object.freeze({ code: codes[0] ?? fallback, codes, issues: Object.freeze(issues.map(issue => Object.freeze(issue))),
131
+ return Object.freeze({
132
+ code: codes[0] ?? fallback,
133
+ codes,
134
+ issues: Object.freeze(issues.map((issue) => Object.freeze(issue))),
55
135
  ...(context?.stageId === undefined ? {} : { stageId: context.stageId }),
56
- ...(isRetryable === undefined ? {} : { isRetryable }), cause: error });
136
+ ...(isRetryable === undefined ? {} : { isRetryable }),
137
+ cause: error,
138
+ });
57
139
  }