@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
@@ -3,19 +3,77 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.payloadSnapshot = payloadSnapshot;
4
4
  exports.createSubFlowLogger = createSubFlowLogger;
5
5
  exports.readSubFlowLogger = readSubFlowLogger;
6
+ /**
7
+ * Bounded, self-reported sub-flow logging.
8
+ *
9
+ * A node or tool may report the steps it believes it performed. Such reports are optional
10
+ * evidence, never proof of Core scheduling: they are marked `origin: 'reported'`, validated
11
+ * against a fixed field whitelist, and capped in count, text length and nested payload depth.
12
+ *
13
+ * Issued loggers are tracked in a module-private registry, so only the execution that created
14
+ * one can read it. A lookalike object with a `readAll` method is never accepted.
15
+ *
16
+ * Exports: `payloadSnapshot`, `createSubFlowLogger`, `readSubFlowLogger`.
17
+ */
6
18
  const rules_js_1 = require("../utils/rules.js");
7
19
  const invocation_js_1 = require("../utils/invocation.js");
8
20
  const debug_js_1 = require("../structs/debug.js");
21
+ /**
22
+ * Registry of issued loggers: owner, diagnostic identity and reader for each instance. Kept
23
+ * private so a structurally identical object cannot impersonate a real logger.
24
+ */
9
25
  const instances = new WeakMap();
10
- const writeFields = ['name', 'kind', 'parentStepId', 'relatedDefinitionId', 'status', 'usedModel', 'startedAtMs', 'endedAtMs', 'nextStepIds', 'caseId', 'summary', 'payload'];
11
- const fixFields = ['status', 'usedModel', 'startedAtMs', 'endedAtMs', 'summary', 'payload', 'nextStepIds'];
12
- const textLimits = { name: 256, kind: 128, parentStepId: 256, relatedDefinitionId: 256, caseId: 256, summary: 1024 };
26
+ /** Fields accepted by `write`; anything else is rejected so a declaration cannot smuggle state. */
27
+ const writeFields = [
28
+ 'name',
29
+ 'kind',
30
+ 'parentStepId',
31
+ 'relatedDefinitionId',
32
+ 'status',
33
+ 'usedModel',
34
+ 'startedAtMs',
35
+ 'endedAtMs',
36
+ 'nextStepIds',
37
+ 'caseId',
38
+ 'summary',
39
+ 'payload',
40
+ ];
41
+ /** Fields accepted by `fix`: identity and creation data are intentionally not patchable. */
42
+ const fixFields = [
43
+ 'status',
44
+ 'usedModel',
45
+ 'startedAtMs',
46
+ 'endedAtMs',
47
+ 'summary',
48
+ 'payload',
49
+ 'nextStepIds',
50
+ ];
51
+ /** Per-field character caps, bounding what one reported step can add to a persisted record. */
52
+ const textLimits = {
53
+ name: 256,
54
+ kind: 128,
55
+ parentStepId: 256,
56
+ relatedDefinitionId: 256,
57
+ caseId: 256,
58
+ summary: 1024,
59
+ };
60
+ /**
61
+ * Validate a step input: plain prototype, whitelisted keys, own data properties only.
62
+ *
63
+ * Property descriptors are inspected instead of values being read, so an accessor on the input
64
+ * cannot execute application code during validation.
65
+ */
13
66
  function fields(input, allowed) {
14
67
  (0, rules_js_1.breakWhile)(!input || Object.getPrototypeOf(input) !== Object.prototype, () => new TypeError('Sub-flow input must be a plain parameter object'));
15
- (0, rules_js_1.breakWhile)(Reflect.ownKeys(input).some(key => typeof key !== 'string' || !allowed.includes(key)), () => new TypeError('Unknown sub-flow field'));
68
+ (0, rules_js_1.breakWhile)(Reflect.ownKeys(input).some((key) => typeof key !== 'string' || !allowed.includes(key)), () => new TypeError('Unknown sub-flow field'));
16
69
  for (const key of Reflect.ownKeys(input))
17
- (0, rules_js_1.breakWhile)(!['payload', 'summary'].includes(String(key)) && !('value' in Object.getOwnPropertyDescriptor(input, key)), () => new TypeError('Sub-flow metadata must be plain data'));
70
+ (0, rules_js_1.breakWhile)(!['payload', 'summary'].includes(String(key)) &&
71
+ !('value' in Object.getOwnPropertyDescriptor(input, key)), () => new TypeError('Sub-flow metadata must be plain data'));
18
72
  }
73
+ /**
74
+ * Check a fully built step: text caps, status vocabulary, time ordering, and the rules tying
75
+ * timestamps to planned and terminal statuses.
76
+ */
19
77
  function validate(step) {
20
78
  for (const [key, limit] of Object.entries(textLimits)) {
21
79
  const value = step[key];
@@ -25,15 +83,33 @@ function validate(step) {
25
83
  (0, rules_js_1.breakWhile)(step.usedModel !== undefined && typeof step.usedModel !== 'boolean', () => new TypeError('Invalid sub-flow usedModel'));
26
84
  for (const time of [step.startedAtMs, step.endedAtMs])
27
85
  (0, rules_js_1.breakWhile)(time !== undefined && (!Number.isFinite(time) || time < 0), () => new TypeError('Invalid sub-flow time'));
28
- (0, rules_js_1.breakWhile)(step.startedAtMs !== undefined && step.endedAtMs !== undefined && step.endedAtMs < step.startedAtMs, () => new TypeError('Invalid sub-flow time order'));
86
+ (0, rules_js_1.breakWhile)(step.startedAtMs !== undefined &&
87
+ step.endedAtMs !== undefined &&
88
+ step.endedAtMs < step.startedAtMs, () => new TypeError('Invalid sub-flow time order'));
29
89
  (0, rules_js_1.breakWhile)(step.endedAtMs !== undefined && !debug_js_1.reportedStepStatusCatalog[step.status].terminal, () => new TypeError('Only terminal sub-flow steps have an end time'));
30
90
  (0, rules_js_1.breakWhile)(['planned', 'skipped'].includes(step.status) && step.startedAtMs !== undefined, () => new TypeError('A planned or skipped sub-flow step has not started'));
31
- (0, rules_js_1.breakWhile)(step.nextStepIds !== undefined && (!Array.isArray(step.nextStepIds) || step.nextStepIds.length > 64 || new Set(step.nextStepIds).size !== step.nextStepIds.length
32
- || step.nextStepIds.some(id => typeof id !== 'string' || !id.trim() || id.length > 256)), () => new TypeError('Invalid sub-flow successors'));
91
+ (0, rules_js_1.breakWhile)(step.nextStepIds !== undefined &&
92
+ (!Array.isArray(step.nextStepIds) ||
93
+ step.nextStepIds.length > 64 ||
94
+ new Set(step.nextStepIds).size !== step.nextStepIds.length ||
95
+ step.nextStepIds.some((id) => typeof id !== 'string' || !id.trim() || id.length > 256)), () => new TypeError('Invalid sub-flow successors'));
33
96
  }
34
- /** Reject accessors, services, cycles and non-JSON values without evaluating application getters. */
97
+ /**
98
+ * Validate and deep-copy a reported payload.
99
+ *
100
+ * Accessors, class instances, cycles and non-JSON values are rejected without evaluating
101
+ * application getters, so a payload can neither run code nor alias live state. Accepted data is
102
+ * frozen.
103
+ *
104
+ * @param input Object to snapshot. It must be a plain object, not an array.
105
+ * @returns A deeply frozen JSON-shaped copy.
106
+ * @throws TypeError when the value is not plain JSON data, contains a getter, or is cyclic.
107
+ * @throws RangeError when node count, nesting depth, string length or array length exceeds the
108
+ * limits below.
109
+ */
35
110
  function payloadSnapshot(input) {
36
111
  let nodes = 0;
112
+ // Ancestors rather than all visited nodes: a shared subtree is fine, a cycle is not.
37
113
  const ancestors = new Set();
38
114
  const copy = (value, depth) => {
39
115
  (0, rules_js_1.breakWhile)(++nodes > 4096 || depth > 16, () => new RangeError('Sub-flow payload is too complex'));
@@ -42,7 +118,11 @@ function payloadSnapshot(input) {
42
118
  return value;
43
119
  if (typeof value === 'number' && Number.isFinite(value))
44
120
  return value;
45
- (0, rules_js_1.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'));
121
+ (0, rules_js_1.breakWhile)(!value ||
122
+ typeof value !== 'object' ||
123
+ (typeof value === 'object' && value !== null && ancestors.has(value)) ||
124
+ // Only plain objects survive: a class instance may hide lazy state behind getters.
125
+ (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype), () => new TypeError('Sub-flow payload must contain JSON data'));
46
126
  (0, rules_js_1.breakWhile)(Array.isArray(value) && value.length > 4096, () => new RangeError('Sub-flow payload array is too large'));
47
127
  const data = value;
48
128
  ancestors.add(data);
@@ -51,21 +131,43 @@ function payloadSnapshot(input) {
51
131
  if (Array.isArray(value) && key === 'length')
52
132
  continue;
53
133
  const field = Object.getOwnPropertyDescriptor(value, key);
54
- (0, rules_js_1.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'));
134
+ (0, rules_js_1.breakWhile)(typeof key !== 'string' ||
135
+ !('value' in field) ||
136
+ !field.enumerable ||
137
+ // Array keys must be canonical indices; extra or sparse keys cannot survive JSON.
138
+ (Array.isArray(value) && !/^(0|[1-9]\d*)$/.test(key)), () => new TypeError('Sub-flow payload must contain plain data fields'));
55
139
  Object.defineProperty(result, key, { value: copy(field.value, depth + 1), enumerable: true });
56
140
  }
57
141
  ancestors.delete(data);
58
- return Array.isArray(value) ? Object.freeze(Array.from({ length: value.length }, (_, i) => result[i] ?? null)) : Object.freeze(result);
142
+ return Array.isArray(value)
143
+ ? Object.freeze(Array.from({ length: value.length }, (_, i) => result[i] ?? null))
144
+ : Object.freeze(result);
59
145
  };
60
146
  (0, rules_js_1.breakWhile)(!input || Array.isArray(input) || Object.getPrototypeOf(input) !== Object.prototype, () => new TypeError('Sub-flow payload must be an object'));
61
147
  return copy(input, 0);
62
148
  }
149
+ /**
150
+ * Merge a payload patch into a recorded payload without replacing recorded facts.
151
+ *
152
+ * Nested objects merge recursively; an existing leaf must keep its value. A mismatch is
153
+ * rejected rather than overwritten, because a fix may add results but not rewrite history.
154
+ */
63
155
  function fill(previous, patch) {
64
156
  const result = { ...previous };
65
157
  for (const [key, value] of Object.entries(patch)) {
66
158
  if (!Object.hasOwn(previous, key))
67
- Object.defineProperty(result, key, { value, enumerable: true, configurable: true, writable: true });
68
- else if (value && previous[key] && typeof value === 'object' && typeof previous[key] === 'object' && !Array.isArray(value) && !Array.isArray(previous[key])) {
159
+ Object.defineProperty(result, key, {
160
+ value,
161
+ enumerable: true,
162
+ configurable: true,
163
+ writable: true,
164
+ });
165
+ else if (value &&
166
+ previous[key] &&
167
+ typeof value === 'object' &&
168
+ typeof previous[key] === 'object' &&
169
+ !Array.isArray(value) &&
170
+ !Array.isArray(previous[key])) {
69
171
  result[key] = fill(previous[key], value);
70
172
  }
71
173
  else
@@ -73,87 +175,185 @@ function fill(previous, patch) {
73
175
  }
74
176
  return Object.freeze(result);
75
177
  }
76
- /** Internal constructor; only active execution contexts issue logger instances. */
178
+ /**
179
+ * Create a logger bound to one active execution visit.
180
+ *
181
+ * Only execution contexts inside this package should call it: the resulting object is the sole
182
+ * way to append reported steps to the current record.
183
+ *
184
+ * @param owner Object that owns the visit. `readSubFlowLogger` compares this identity before
185
+ * returning any step.
186
+ * @param active Reports whether the visit is still open; a closed logger rejects writes.
187
+ * @param content Reports whether content-level detail is enabled. When false, summaries and
188
+ * payloads are dropped before anything is stored.
189
+ * @param changed Called after each accepted write or fix with the progress delta to publish.
190
+ * @param identity Diagnostic run and span the logger belongs to; used to reject cross-scope reads.
191
+ * @returns A frozen logger instance.
192
+ */
77
193
  function createSubFlowLogger(owner, active, content, changed, identity) {
78
194
  class Logger {
79
195
  #steps = Object.freeze([]);
80
196
  #changes = 0;
81
- constructor() { instances.set(this, { owner, identity, read: () => this.#steps }); Object.freeze(this); }
82
- #assertActive() { (0, rules_js_1.breakWhile)(!active(), () => new TypeError('Sub-flow logger belongs to a closed execution')); }
197
+ constructor() {
198
+ instances.set(this, { owner, identity, read: () => this.#steps });
199
+ Object.freeze(this);
200
+ }
201
+ #assertActive() {
202
+ // A logger that outlived its execution would append evidence to a finished request.
203
+ (0, rules_js_1.breakWhile)(!active(), () => new TypeError('Sub-flow logger belongs to a closed execution'));
204
+ }
83
205
  #save(step, operation) {
84
206
  this.#assertActive();
85
- const next = operation === 'write' ? [...this.#steps, step] : this.#steps.map(item => item.stepId === step.stepId ? step : item);
86
- (0, rules_js_1.breakWhile)(step.nextStepIds?.some(id => !next.some(item => item.stepId === id)), () => new TypeError('Sub-flow successors must belong to this logger'));
207
+ // A fix replaces the step with the same id in place; a write always appends.
208
+ const next = operation === 'write'
209
+ ? [...this.#steps, step]
210
+ : this.#steps.map((item) => (item.stepId === step.stepId ? step : item));
211
+ // Successors must resolve inside this logger, otherwise the recorded graph would have
212
+ // dangling edges.
213
+ (0, rules_js_1.breakWhile)(step.nextStepIds?.some((id) => !next.some((item) => item.stepId === id)), () => new TypeError('Sub-flow successors must belong to this logger'));
214
+ // The step and size caps equal the persisted-record limits; the change cap bounds
215
+ // write/fix churn even when the final step count stays small.
87
216
  (0, rules_js_1.breakWhile)(next.length > 256 || this.#changes >= 1024 || JSON.stringify(next).length > 65536, () => new RangeError('Sub-flow log limit exceeded'));
88
217
  this.#steps = Object.freeze(next);
89
218
  this.#changes++;
90
- changed(this, { operation, stepId: step.stepId, sequence: step.sequence, revision: step.revision, createdAtMs: step.createdAtMs, status: step.status, ...(step.usedModel === undefined ? {} : { usedModel: step.usedModel }) });
219
+ changed(this, {
220
+ operation,
221
+ stepId: step.stepId,
222
+ sequence: step.sequence,
223
+ revision: step.revision,
224
+ createdAtMs: step.createdAtMs,
225
+ status: step.status,
226
+ ...(step.usedModel === undefined ? {} : { usedModel: step.usedModel }),
227
+ });
91
228
  }
92
229
  write(input) {
93
230
  this.#assertActive();
94
231
  fields(input, writeFields);
95
232
  (0, rules_js_1.breakWhile)(input.nextStepIds !== undefined && !Array.isArray(input.nextStepIds), () => new TypeError('Invalid sub-flow successors'));
96
233
  const now = Date.now(), status = input.status === undefined ? 'planned' : input.status;
97
- const step = { origin: 'reported', stepId: (0, invocation_js_1.invocationId)(), sequence: this.#steps.length + 1, revision: 1, createdAtMs: now,
98
- name: input.name, kind: input.kind, status,
234
+ // Timestamps are derived from the status: a running or terminal step gets a start time,
235
+ // a terminal step also an end time, using the caller's value when one is supplied.
236
+ const step = {
237
+ origin: 'reported',
238
+ stepId: (0, invocation_js_1.invocationId)(),
239
+ sequence: this.#steps.length + 1,
240
+ revision: 1,
241
+ createdAtMs: now,
242
+ name: input.name,
243
+ kind: input.kind,
244
+ status,
99
245
  ...(input.parentStepId === undefined ? {} : { parentStepId: input.parentStepId }),
100
- ...(input.relatedDefinitionId === undefined ? {} : { relatedDefinitionId: input.relatedDefinitionId }),
246
+ ...(input.relatedDefinitionId === undefined
247
+ ? {}
248
+ : { relatedDefinitionId: input.relatedDefinitionId }),
101
249
  ...(input.caseId === undefined ? {} : { caseId: input.caseId }),
102
250
  ...(input.usedModel === undefined ? {} : { usedModel: input.usedModel }),
103
- ...(input.startedAtMs === undefined && status !== 'running' ? {} : { startedAtMs: input.startedAtMs ?? now }),
104
- ...(input.endedAtMs === undefined && !debug_js_1.reportedStepStatusCatalog[status]?.terminal ? {} : { endedAtMs: input.endedAtMs ?? now }),
105
- ...(input.nextStepIds === undefined ? {} : { nextStepIds: Object.freeze([...input.nextStepIds]) }),
251
+ ...(input.startedAtMs === undefined && status !== 'running'
252
+ ? {}
253
+ : { startedAtMs: input.startedAtMs ?? now }),
254
+ ...(input.endedAtMs === undefined && !debug_js_1.reportedStepStatusCatalog[status]?.terminal
255
+ ? {}
256
+ : { endedAtMs: input.endedAtMs ?? now }),
257
+ ...(input.nextStepIds === undefined
258
+ ? {}
259
+ : { nextStepIds: Object.freeze([...input.nextStepIds]) }),
106
260
  };
107
261
  if (content()) {
262
+ // Summaries and payloads are omitted entirely when content logging is off, so a
263
+ // metadata-only consumer never receives them.
108
264
  if (input.summary !== undefined)
109
265
  Object.assign(step, { summary: input.summary });
110
266
  if (input.payload !== undefined)
111
267
  Object.assign(step, { payload: payloadSnapshot(input.payload) });
112
268
  }
113
269
  validate(step);
114
- (0, rules_js_1.breakWhile)(step.parentStepId && !this.#steps.some(item => item.stepId === step.parentStepId), () => new TypeError('Sub-flow parent must belong to this logger'));
270
+ (0, rules_js_1.breakWhile)(step.parentStepId && !this.#steps.some((item) => item.stepId === step.parentStepId), () => new TypeError('Sub-flow parent must belong to this logger'));
115
271
  this.#save(Object.freeze(step), 'write');
116
272
  return step.stepId;
117
273
  }
118
274
  fix(stepId, patch) {
119
275
  this.#assertActive();
120
276
  fields(patch, fixFields);
121
- const previous = this.#steps.find(step => step.stepId === stepId);
277
+ const previous = this.#steps.find((step) => step.stepId === stepId);
278
+ // The assertion above only satisfies the type checker; this check rejects an unknown id.
122
279
  (0, rules_js_1.breakWhile)(!previous, () => new TypeError('Unknown sub-flow step'));
123
280
  const terminal = debug_js_1.reportedStepStatusCatalog[previous.status].terminal;
281
+ // A terminal step is sealed: its outcome, model use and timestamps cannot be rewritten.
124
282
  (0, rules_js_1.breakWhile)(terminal && patch.status !== undefined && patch.status !== previous.status, () => new TypeError('Terminal sub-flow status is sealed'));
125
- (0, rules_js_1.breakWhile)(terminal && previous.usedModel !== undefined && patch.usedModel !== undefined && previous.usedModel !== patch.usedModel, () => new TypeError('Terminal sub-flow model use is sealed'));
283
+ (0, rules_js_1.breakWhile)(terminal &&
284
+ previous.usedModel !== undefined &&
285
+ patch.usedModel !== undefined &&
286
+ previous.usedModel !== patch.usedModel, () => new TypeError('Terminal sub-flow model use is sealed'));
126
287
  (0, rules_js_1.breakWhile)(previous.usedModel === true && patch.usedModel === false, () => new TypeError('Cannot undo reported model use'));
127
- (0, rules_js_1.breakWhile)(patch.status !== undefined && patch.status !== previous.status && !debug_js_1.reportedStepStatusCatalog[previous.status].next.includes(patch.status), () => new TypeError('Invalid sub-flow status transition'));
288
+ (0, rules_js_1.breakWhile)(patch.status !== undefined &&
289
+ patch.status !== previous.status &&
290
+ // Transitions are catalog-driven; a terminal status has no successors at all.
291
+ !debug_js_1.reportedStepStatusCatalog[previous.status].next.includes(patch.status), () => new TypeError('Invalid sub-flow status transition'));
128
292
  for (const key of ['startedAtMs', 'endedAtMs'])
129
293
  (0, rules_js_1.breakWhile)(previous[key] !== undefined && patch[key] !== undefined && patch[key] !== previous[key], () => new TypeError('Cannot overwrite a sub-flow time'));
130
294
  const status = patch.status ?? previous.status;
131
- (0, rules_js_1.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'));
132
- const step = { ...previous, revision: previous.revision + 1, status,
133
- ...(patch.nextStepIds === undefined ? {} : { nextStepIds: Object.freeze([...patch.nextStepIds]) }),
295
+ (0, rules_js_1.breakWhile)(patch.nextStepIds !== undefined &&
296
+ (!Array.isArray(patch.nextStepIds) ||
297
+ (previous.nextStepIds !== undefined &&
298
+ JSON.stringify(previous.nextStepIds) !== JSON.stringify(patch.nextStepIds))), () => new TypeError('Cannot overwrite sub-flow successors'));
299
+ // A start time is auto-stamped only when the step becomes running and had none; an end
300
+ // time is added once the status becomes terminal.
301
+ const step = {
302
+ ...previous,
303
+ revision: previous.revision + 1,
304
+ status,
305
+ ...(patch.nextStepIds === undefined
306
+ ? {}
307
+ : { nextStepIds: Object.freeze([...patch.nextStepIds]) }),
134
308
  ...(patch.usedModel === undefined ? {} : { usedModel: patch.usedModel }),
135
- ...(patch.startedAtMs === undefined && !(status === 'running' && previous.startedAtMs === undefined) ? {} : { startedAtMs: patch.startedAtMs ?? Date.now() }),
136
- ...(patch.endedAtMs === undefined && (!debug_js_1.reportedStepStatusCatalog[status]?.terminal || previous.endedAtMs !== undefined) ? {} : { endedAtMs: patch.endedAtMs ?? Date.now() }),
309
+ ...(patch.startedAtMs === undefined &&
310
+ !(status === 'running' && previous.startedAtMs === undefined)
311
+ ? {}
312
+ : { startedAtMs: patch.startedAtMs ?? Date.now() }),
313
+ ...(patch.endedAtMs === undefined &&
314
+ (!debug_js_1.reportedStepStatusCatalog[status]?.terminal || previous.endedAtMs !== undefined)
315
+ ? {}
316
+ : { endedAtMs: patch.endedAtMs ?? Date.now() }),
137
317
  };
138
318
  if (content()) {
319
+ // A fix may add a summary once and fill missing payload fields, but it cannot replace
320
+ // a summary or a recorded payload value.
139
321
  if (patch.summary !== undefined) {
140
322
  (0, rules_js_1.breakWhile)(previous.summary !== undefined && previous.summary !== patch.summary, () => new TypeError('Cannot overwrite a sub-flow summary'));
141
323
  Object.assign(step, { summary: patch.summary });
142
324
  }
143
325
  if (patch.payload !== undefined)
144
- Object.assign(step, { payload: fill(previous.payload ?? {}, payloadSnapshot(patch.payload)) });
326
+ Object.assign(step, {
327
+ payload: fill(previous.payload ?? {}, payloadSnapshot(patch.payload)),
328
+ });
145
329
  }
146
330
  validate(step);
147
331
  this.#save(Object.freeze(step), 'fix');
148
332
  }
149
- readAll() { return this.#steps; }
333
+ readAll() {
334
+ return this.#steps;
335
+ }
150
336
  }
151
337
  return new Logger();
152
338
  }
153
- /** Never call a lookalike's readAll method. */
339
+ /**
340
+ * Read the steps recorded by a logger issued by this module.
341
+ *
342
+ * The instance is resolved through the private registry, so a lookalike's `readAll` method is
343
+ * never called and a logger cannot be borrowed across executions.
344
+ *
345
+ * @param input Candidate logger; must be an instance created by `createSubFlowLogger`.
346
+ * @param owner Expected owning execution visit. Omit to accept any owner.
347
+ * @param identity Expected run and span. Omit to accept any diagnostic scope.
348
+ * @returns The recorded steps in write order, as an immutable list shared with the logger.
349
+ * @throws TypeError when the value is not a registered logger, belongs to another owner, or
350
+ * belongs to another diagnostic scope.
351
+ */
154
352
  function readSubFlowLogger(input, owner, identity) {
155
353
  const instance = input && typeof input === 'object' ? instances.get(input) : undefined;
156
- (0, rules_js_1.breakWhile)(!instance || (owner !== undefined && owner !== instance.owner), () => new TypeError('Expected this execution\'s SubFlowLogger instance'));
157
- (0, rules_js_1.breakWhile)(identity && (instance.identity?.runId !== identity.runId || instance.identity?.spanId !== identity.spanId), () => new TypeError('Sub-flow logger belongs to another diagnostic scope'));
354
+ (0, rules_js_1.breakWhile)(!instance || (owner !== undefined && owner !== instance.owner), () => new TypeError("Expected this execution's SubFlowLogger instance"));
355
+ (0, rules_js_1.breakWhile)(identity &&
356
+ (instance.identity?.runId !== identity.runId ||
357
+ instance.identity?.spanId !== identity.spanId), () => new TypeError('Sub-flow logger belongs to another diagnostic scope'));
158
358
  return instance.read();
159
359
  }
@@ -2,11 +2,32 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.recordErrorContext = recordErrorContext;
4
4
  exports.getErrorInfo = getErrorInfo;
5
+ /**
6
+ * Error metadata boundary: records which execution stage produced a failure and exposes a
7
+ * stable, read-only view of it for diagnostics.
8
+ *
9
+ * The original thrown value is never wrapped or replaced, so App code keeps its own error
10
+ * identity and stack. Annotations live in a side table keyed by reference, which works for
11
+ * frozen Errors and DOMException instances whose properties cannot be added.
12
+ *
13
+ * Exports: `recordErrorContext` (annotate at a boundary) and `getErrorInfo` (inspect).
14
+ */
5
15
  const errors_js_1 = require("./structs/errors.js");
6
16
  // Side metadata preserves App error identity, including frozen Errors and DOMException.
7
17
  const contexts = new WeakMap();
8
18
  const reference = (value) => value !== null && (typeof value === 'object' || typeof value === 'function');
9
- /** Internal boundary annotation. Re-throws retain the originating stage and provider details. */
19
+ /**
20
+ * Attach boundary metadata to a thrown value without changing that value.
21
+ *
22
+ * The earliest recorded stage stays authoritative on re-throw, so a failure is attributed
23
+ * to the stage that first caught it rather than to the boundary that merely relays it.
24
+ * Codes and retryability may still be filled in by an outer boundary that knows more.
25
+ *
26
+ * @param error Value being propagated. Non-object values are ignored: identity is tracked
27
+ * by reference, so primitives cannot carry annotations.
28
+ * @param context Boundary annotations to merge into the existing entry, if any.
29
+ * @returns The same reference, allowing `throw recordErrorContext(error, ctx)`.
30
+ */
10
31
  function recordErrorContext(error, context) {
11
32
  if (reference(error)) {
12
33
  const previous = contexts.get(error);
@@ -15,34 +36,86 @@ function recordErrorContext(error, context) {
15
36
  }
16
37
  return error;
17
38
  }
18
- /** Inspect external metadata inside the guarded diagnostic boundary below. */
39
+ /**
40
+ * Read classification metadata from an arbitrary thrown value.
41
+ *
42
+ * Returns normalized issues plus a primary code. `isRetryable` is left undefined when the
43
+ * error neither states it nor makes a final classification, so callers do not mistake
44
+ * "unknown" for "retryable".
45
+ */
19
46
  function inspectError(error) {
20
- const source = reference(error) ? error : {};
47
+ const source = reference(error)
48
+ ? error
49
+ : {};
21
50
  const reportedIssues = source.issues;
51
+ // Structured issues win over the older `errors` array, so an error carrying both is not
52
+ // reported twice. Only the array shape is accepted; any other value falls through.
22
53
  const reportedErrors = Array.isArray(reportedIssues) ? undefined : source.errors;
23
- const raw = Array.isArray(reportedIssues) ? reportedIssues : Array.isArray(reportedErrors) ? reportedErrors : [];
24
- const issues = raw.flatMap(value => {
54
+ const raw = Array.isArray(reportedIssues)
55
+ ? reportedIssues
56
+ : Array.isArray(reportedErrors)
57
+ ? reportedErrors
58
+ : [];
59
+ const issues = raw.flatMap((value) => {
25
60
  if (!value || typeof value !== 'object')
26
61
  return [];
27
62
  const { code, message, path: reportedPath, checkId } = value;
28
63
  if (typeof code !== 'string')
29
64
  return [];
30
- const pathItems = typeof reportedPath === 'string' ? [reportedPath] : Array.isArray(reportedPath) ? Array.from(reportedPath) : undefined;
31
- const path = pathItems?.every((part) => typeof part === 'string' || typeof part === 'number') ? pathItems : undefined;
32
- return [{ code, message: typeof message === 'string' ? message : code,
65
+ const pathItems = typeof reportedPath === 'string'
66
+ ? [reportedPath]
67
+ : Array.isArray(reportedPath)
68
+ ? Array.from(reportedPath)
69
+ : undefined;
70
+ const path = pathItems?.every((part) => typeof part === 'string' || typeof part === 'number')
71
+ ? pathItems
72
+ : undefined;
73
+ return [
74
+ {
75
+ code,
76
+ message: typeof message === 'string' ? message : code,
33
77
  ...(path === undefined ? {} : { path: Object.freeze(path) }),
34
- ...(typeof checkId === 'string' ? { checkId } : {}) }];
78
+ ...(typeof checkId === 'string' ? { checkId } : {}),
79
+ },
80
+ ];
35
81
  });
36
82
  const code = source.code;
83
+ // A string `code` or a structured issue is already a usable classification, so the error
84
+ // `name` is only consulted as a last resort.
37
85
  const name = typeof code === 'string' || issues.length ? undefined : source.name;
38
- const fallback = typeof code === 'string' ? code : issues[0]?.code
39
- ?? (name === 'AbortError' ? 'CANCELLED' : name === 'TimeoutError' ? 'TIMEOUT' : 'UNKNOWN_ERROR');
86
+ // DOMException exposes cancellation through `name` rather than `code`; the mapping keeps
87
+ // cancellation and timeout distinguishable from genuine failures.
88
+ const fallback = typeof code === 'string'
89
+ ? code
90
+ : (issues[0]?.code ??
91
+ (name === 'AbortError'
92
+ ? 'CANCELLED'
93
+ : name === 'TimeoutError'
94
+ ? 'TIMEOUT'
95
+ : 'UNKNOWN_ERROR'));
40
96
  const retryable = source.isRetryable;
41
- const isRetryable = typeof retryable === 'boolean' ? retryable
42
- : error instanceof errors_js_1.BudgetError || error instanceof errors_js_1.ValidationError || issues.length || ['CANCELLED', 'TIMEOUT', 'BUDGET'].includes(fallback) ? false : undefined;
97
+ // Retryability stays unknown unless the error reports it or it is provably final:
98
+ // budget, validation, issue-bearing and cancellation failures must not be retried.
99
+ const isRetryable = typeof retryable === 'boolean'
100
+ ? retryable
101
+ : error instanceof errors_js_1.BudgetError ||
102
+ error instanceof errors_js_1.ValidationError ||
103
+ issues.length ||
104
+ ['CANCELLED', 'TIMEOUT', 'BUDGET'].includes(fallback)
105
+ ? false
106
+ : undefined;
43
107
  return { code: fallback, issues, isRetryable };
44
108
  }
45
- /** A local diagnostic view; hostile metadata must never replace the original failure. */
109
+ /**
110
+ * Produce a frozen diagnostic view of any thrown value.
111
+ *
112
+ * Inspection is guarded: an exotic error (revoked proxy, throwing getter, null prototype)
113
+ * degrades to `UNKNOWN_ERROR` instead of raising a second failure while handling the first.
114
+ *
115
+ * @param error Any thrown value, including non-Error values such as strings.
116
+ * @returns Frozen info. `codes` lists every known code with the primary one first; optional
117
+ * fields are omitted rather than emitted as `undefined`. `cause` is the original value.
118
+ */
46
119
  function getErrorInfo(error) {
47
120
  const context = reference(error) ? contexts.get(error) : undefined;
48
121
  let view;
@@ -53,9 +126,18 @@ function getErrorInfo(error) {
53
126
  view = { code: 'UNKNOWN_ERROR', issues: [] };
54
127
  }
55
128
  const { code: fallback, issues } = view;
56
- const codes = Object.freeze([...(context?.codes ?? (issues.length ? issues.map(issue => issue.code) : [fallback]))]);
129
+ // Recorded codes describe the boundary where the error was first captured and outrank
130
+ // codes inferred from the current shape, which may already have been flattened.
131
+ const codes = Object.freeze([
132
+ ...(context?.codes ?? (issues.length ? issues.map((issue) => issue.code) : [fallback])),
133
+ ]);
57
134
  const isRetryable = context?.isRetryable ?? view.isRetryable;
58
- return Object.freeze({ code: codes[0] ?? fallback, codes, issues: Object.freeze(issues.map(issue => Object.freeze(issue))),
135
+ return Object.freeze({
136
+ code: codes[0] ?? fallback,
137
+ codes,
138
+ issues: Object.freeze(issues.map((issue) => Object.freeze(issue))),
59
139
  ...(context?.stageId === undefined ? {} : { stageId: context.stageId }),
60
- ...(isRetryable === undefined ? {} : { isRetryable }), cause: error });
140
+ ...(isRetryable === undefined ? {} : { isRetryable }),
141
+ cause: error,
142
+ });
61
143
  }
@@ -2,12 +2,28 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.snapshotBranch = snapshotBranch;
4
4
  const rules_js_1 = require("../utils/rules.js");
5
- /** Core identity contract, also used by App-side Family storage adapters. */
5
+ /**
6
+ * Validate and freeze a branch identity.
7
+ *
8
+ * Required ids identify the branch itself; optional ids describe the fork origin. Ids are
9
+ * bounded in length so they remain usable as keys in App storage.
10
+ *
11
+ * @param source - Branch identity supplied by the host or generated by the engine.
12
+ * @returns A frozen identity with the same field set.
13
+ * @throws Error When a required id is missing or any id is blank or too long.
14
+ */
6
15
  function snapshotBranch(source) {
7
16
  (0, rules_js_1.breakWhile)(!source, 'Invalid branch identity');
8
17
  const { agentId, branchId, agentVersionId, parentBranchId, parentAgentVersionId, forkPointId } = source;
9
18
  const valid = (id) => typeof id === 'string' && !!id.trim() && id.length <= 200;
10
- (0, rules_js_1.breakWhile)(![agentId, branchId, agentVersionId].every(valid)
11
- || [parentBranchId, parentAgentVersionId, forkPointId].some(id => id !== undefined && !valid(id)), 'Invalid branch identity');
12
- return Object.freeze({ agentId, branchId, agentVersionId, parentBranchId, parentAgentVersionId, forkPointId });
19
+ (0, rules_js_1.breakWhile)(![agentId, branchId, agentVersionId].every(valid) ||
20
+ [parentBranchId, parentAgentVersionId, forkPointId].some((id) => id !== undefined && !valid(id)), 'Invalid branch identity');
21
+ return Object.freeze({
22
+ agentId,
23
+ branchId,
24
+ agentVersionId,
25
+ parentBranchId,
26
+ parentAgentVersionId,
27
+ forkPointId,
28
+ });
13
29
  }