@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,3 +1,10 @@
1
+ /**
2
+ * Public surface of the model protocol layer.
3
+ *
4
+ * Re-exports the runner, the provider and transport contracts, the interaction
5
+ * adapters and the defaults. Everything that interprets model output passes through
6
+ * this module, so consumers should not import the individual files.
7
+ */
1
8
  export * from './runner.js';
2
9
  export * from '../structs/model/chat-provider.js';
3
10
  export * from '../support/model/compatible-provider.js';
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Public surface of the model protocol layer.
3
+ *
4
+ * Re-exports the runner, the provider and transport contracts, the interaction
5
+ * adapters and the defaults. Everything that interprets model output passes through
6
+ * this module, so consumers should not import the individual files.
7
+ */
1
8
  export * from './runner.js';
2
9
  export * from '../structs/model/chat-provider.js';
3
10
  export * from '../support/model/compatible-provider.js';
@@ -1,3 +1,30 @@
1
+ /**
2
+ * Require a plain argument object.
3
+ *
4
+ * @param value - Candidate value.
5
+ * @returns The value typed as a string-keyed record.
6
+ * @throws ValidationError When the value is not a non-array object.
7
+ */
1
8
  export declare function object(value: unknown): Record<string, unknown>;
9
+ /**
10
+ * Parse a complete JSON argument payload.
11
+ *
12
+ * Incomplete JSON is rejected rather than repaired, because a truncated stream must not
13
+ * be executed as a plausible partial call.
14
+ *
15
+ * @param raw - Complete JSON text.
16
+ * @returns The parsed argument object.
17
+ * @throws ValidationError When the text is not complete JSON or not an object.
18
+ */
2
19
  export declare function parse(raw: string): Record<string, unknown>;
20
+ /**
21
+ * Map a business capability id onto a wire-safe function name.
22
+ *
23
+ * Business ids are not constrained by the wire protocol's naming alphabet or length, so
24
+ * an already valid id is returned unchanged and any other id is sanitized and suffixed
25
+ * with an FNV-1a hash. The hash keeps distinct ids distinct after sanitizing.
26
+ *
27
+ * @param id - Registered capability id.
28
+ * @returns A name matching the protocol's expected pattern.
29
+ */
3
30
  export declare function wireName(id: string): string;
@@ -1,9 +1,35 @@
1
+ /**
2
+ * Shared argument decoding for interaction adapters.
3
+ *
4
+ * Every adapter ultimately produces the same pair — capability id and argument
5
+ * object — so parsing, validation and wire naming live here instead of being repeated
6
+ * per wire format.
7
+ *
8
+ * Exports: `object`, `parse` and `wireName`.
9
+ */
1
10
  import { modelProtocolError } from '../../utils/model-protocol.js';
11
+ /**
12
+ * Require a plain argument object.
13
+ *
14
+ * @param value - Candidate value.
15
+ * @returns The value typed as a string-keyed record.
16
+ * @throws ValidationError When the value is not a non-array object.
17
+ */
2
18
  export function object(value) {
3
19
  if (!value || typeof value !== 'object' || Array.isArray(value))
4
20
  modelProtocolError('Model arguments must be an object');
5
21
  return value;
6
22
  }
23
+ /**
24
+ * Parse a complete JSON argument payload.
25
+ *
26
+ * Incomplete JSON is rejected rather than repaired, because a truncated stream must not
27
+ * be executed as a plausible partial call.
28
+ *
29
+ * @param raw - Complete JSON text.
30
+ * @returns The parsed argument object.
31
+ * @throws ValidationError When the text is not complete JSON or not an object.
32
+ */
7
33
  export function parse(raw) {
8
34
  let value;
9
35
  try {
@@ -14,7 +40,16 @@ export function parse(raw) {
14
40
  }
15
41
  return object(value);
16
42
  }
17
- // Business IDs are not constrained by the wire protocol's naming alphabet/length.
43
+ /**
44
+ * Map a business capability id onto a wire-safe function name.
45
+ *
46
+ * Business ids are not constrained by the wire protocol's naming alphabet or length, so
47
+ * an already valid id is returned unchanged and any other id is sanitized and suffixed
48
+ * with an FNV-1a hash. The hash keeps distinct ids distinct after sanitizing.
49
+ *
50
+ * @param id - Registered capability id.
51
+ * @returns A name matching the protocol's expected pattern.
52
+ */
18
53
  export function wireName(id) {
19
54
  if (/^[a-zA-Z0-9_-]{1,64}$/.test(id))
20
55
  return id;
@@ -1,3 +1,9 @@
1
1
  import type { ModelInteractionAdapter } from '../../structs/model/interactions.js';
2
- /** Explicit alternative for providers without native calling. Its envelope belongs to this adapter, not to application messages. */
2
+ /**
3
+ * Adapter that reads a single capability call out of a JSON object response.
4
+ *
5
+ * `strictArguments` is false because the wire format carries no schema enforcement; the
6
+ * adapter validates the envelope shape itself and leaves argument validation to the
7
+ * capability's own schema.
8
+ */
3
9
  export declare const jsonObjectInteraction: ModelInteractionAdapter;
@@ -1,29 +1,65 @@
1
+ /**
2
+ * Interaction adapter for providers without native tool calling.
3
+ *
4
+ * Instead of function declarations, the capability list is embedded in the instruction
5
+ * and the model must answer with one JSON envelope. The envelope belongs to this
6
+ * adapter, not to application messages, so switching adapters never changes App code.
7
+ *
8
+ * Exports: `jsonObjectInteraction`.
9
+ */
1
10
  import { modelProtocolError } from '../../utils/model-protocol.js';
2
11
  import { object, parse } from './arguments.js';
3
- /** Explicit alternative for providers without native calling. Its envelope belongs to this adapter, not to application messages. */
12
+ /**
13
+ * Adapter that reads a single capability call out of a JSON object response.
14
+ *
15
+ * `strictArguments` is false because the wire format carries no schema enforcement; the
16
+ * adapter validates the envelope shape itself and leaves argument validation to the
17
+ * capability's own schema.
18
+ */
4
19
  export const jsonObjectInteraction = Object.freeze({
5
20
  id: 'json-object',
6
21
  features: Object.freeze({ strictArguments: false }),
22
+ /**
23
+ * Build the instruction and response collector for one stage.
24
+ *
25
+ * @param capabilities - Capabilities registered for this stage; must be non-empty.
26
+ * @returns JSON mode options, the measured instruction and the collector factory.
27
+ */
7
28
  prepare(capabilities) {
8
- const declarations = capabilities.map(({ id, description, inputSchema }) => ({ id, description, input: inputSchema.jsonSchema }));
29
+ const declarations = capabilities.map(({ id, description, inputSchema }) => ({
30
+ id,
31
+ description,
32
+ input: inputSchema.jsonSchema,
33
+ }));
9
34
  // Keep the wire shape after potentially long schemas so it cannot be mistaken
10
35
  // for a schema definition or a bare capability argument object.
11
- const instruction = 'Registered capabilities: ' + JSON.stringify(declarations)
12
- + '\nReturn exactly one JSON object with exactly these two fields: {"capability":"registered id","input":{...}}. '
13
- + 'Choose capability from ' + JSON.stringify(capabilities.map(capability => capability.id))
14
- + '; input is the chosen capability\'s actual argument object matching its input schema. '
15
- + 'Do not return the schema, response_format, an array, multiple calls, prose or fences. The outer object must contain capability and input.';
36
+ const instruction = 'Registered capabilities: ' +
37
+ JSON.stringify(declarations) +
38
+ '\nReturn exactly one JSON object with exactly these two fields: {"capability":"registered id","input":{...}}. ' +
39
+ 'Choose capability from ' +
40
+ JSON.stringify(capabilities.map((capability) => capability.id)) +
41
+ "; input is the chosen capability's actual argument object matching its input schema. " +
42
+ 'Do not return the schema, response_format, an array, multiple calls, prose or fences. The outer object must contain capability and input.';
16
43
  return {
17
- options: { json: true }, schemaChars: instruction.length, instruction,
44
+ options: { json: true },
45
+ schemaChars: instruction.length,
46
+ instruction,
18
47
  createResponse() {
19
48
  let raw = '';
20
49
  return {
21
- get outputChars() { return raw.length; },
22
- push(part) { if (typeof part !== 'string')
23
- modelProtocolError('JSON interaction does not accept native call events'); raw += part; },
50
+ get outputChars() {
51
+ return raw.length;
52
+ },
53
+ push(part) {
54
+ if (typeof part !== 'string')
55
+ modelProtocolError('JSON interaction does not accept native call events');
56
+ raw += part;
57
+ },
24
58
  finish() {
25
59
  const value = parse(raw);
26
- if (Object.keys(value).length !== 2 || typeof value.capability !== 'string' || !Object.hasOwn(value, 'input'))
60
+ if (Object.keys(value).length !== 2 ||
61
+ typeof value.capability !== 'string' ||
62
+ !Object.hasOwn(value, 'input'))
27
63
  modelProtocolError('Invalid model selection envelope');
28
64
  return { capabilityId: value.capability, input: object(value.input) };
29
65
  },
@@ -1,2 +1,8 @@
1
1
  import type { ModelInteractionAdapter } from '../../structs/model/interactions.js';
2
+ /**
3
+ * Adapter that decodes a streamed native function call.
4
+ *
5
+ * `strictArguments` is false because declarations are sent with `strict: false`; the
6
+ * adapter therefore validates the argument payload itself via `parse`.
7
+ */
2
8
  export declare const nativeFunctionInteraction: ModelInteractionAdapter;
@@ -1,22 +1,55 @@
1
+ /**
2
+ * Default interaction adapter: native function calling.
3
+ *
4
+ * Declarations are sent as provider functions and the streamed function-call delta is
5
+ * reassembled into exactly one proposal. A call is a proposal, never proof of execution:
6
+ * this adapter only decodes it, and the core decides what to run.
7
+ *
8
+ * Exports: `nativeFunctionInteraction`.
9
+ */
1
10
  import { modelProtocolError } from '../../utils/model-protocol.js';
2
11
  import { parse, wireName } from './arguments.js';
12
+ /**
13
+ * Adapter that decodes a streamed native function call.
14
+ *
15
+ * `strictArguments` is false because declarations are sent with `strict: false`; the
16
+ * adapter therefore validates the argument payload itself via `parse`.
17
+ */
3
18
  export const nativeFunctionInteraction = Object.freeze({
4
19
  id: 'native-functions',
5
20
  features: Object.freeze({ strictArguments: false }),
21
+ /**
22
+ * Build native declarations, the instruction and the response collector.
23
+ *
24
+ * Wire names are derived from capability ids and must stay unique after mapping;
25
+ * a collision would make it impossible to resolve the proposed name back to an id.
26
+ *
27
+ * @param capabilities - Capabilities registered for this stage; must be non-empty.
28
+ * @returns Function declarations, the measured instruction and the collector factory.
29
+ * @throws TypeError When two capability ids map to the same wire name.
30
+ */
6
31
  prepare(capabilities) {
7
- const names = new Map(capabilities.map(capability => [wireName(capability.id), capability.id]));
32
+ const names = new Map(capabilities.map((capability) => [wireName(capability.id), capability.id]));
8
33
  if (names.size !== capabilities.length)
9
34
  throw new TypeError('Model wire name collision');
10
- const functions = capabilities.map(capability => ({ name: wireName(capability.id), description: capability.description,
11
- parameters: capability.inputSchema.jsonSchema, strict: false }));
35
+ const functions = capabilities.map((capability) => ({
36
+ name: wireName(capability.id),
37
+ description: capability.description,
38
+ parameters: capability.inputSchema.jsonSchema,
39
+ strict: false,
40
+ }));
12
41
  const instruction = 'Submit exactly one registered capability through its tool call. Protocol examples describe call arguments, not assistant prose. A call is a proposal, not proof of execution.';
13
42
  return {
14
- options: { json: false, functions }, schemaChars: JSON.stringify(functions).length + instruction.length, instruction,
43
+ options: { json: false, functions },
44
+ schemaChars: JSON.stringify(functions).length + instruction.length,
45
+ instruction,
15
46
  createResponse() {
16
47
  let raw = '';
17
48
  let call;
18
49
  return {
19
- get outputChars() { return raw.length + (call ? call.id.length + call.name.length + call.arguments.length : 0); },
50
+ get outputChars() {
51
+ return (raw.length + (call ? call.id.length + call.name.length + call.arguments.length : 0));
52
+ },
20
53
  push(part) {
21
54
  if (typeof part === 'string') {
22
55
  raw += part;
@@ -29,9 +62,9 @@ export const nativeFunctionInteraction = Object.freeze({
29
62
  modelProtocolError('Expected a native model call');
30
63
  if (index !== 0)
31
64
  modelProtocolError('Expected exactly one model call', 'MODEL_MULTIPLE_CALLS');
32
- if ((id !== undefined && (typeof id !== 'string' || !id))
33
- || (name !== undefined && typeof name !== 'string')
34
- || (args !== undefined && typeof args !== 'string'))
65
+ if ((id !== undefined && (typeof id !== 'string' || !id)) ||
66
+ (name !== undefined && typeof name !== 'string') ||
67
+ (args !== undefined && typeof args !== 'string'))
35
68
  modelProtocolError('Invalid native model call fields');
36
69
  call ??= { id: '', name: '', arguments: '' };
37
70
  if (id !== undefined) {
@@ -46,8 +79,17 @@ export const nativeFunctionInteraction = Object.freeze({
46
79
  if (!call?.id || !call.name || raw.trim())
47
80
  modelProtocolError('Expected one complete model call without prose');
48
81
  const completed = call;
49
- const capability = names.get(completed.name) ?? modelProtocolError('Model call is not registered for this stage');
50
- return { capabilityId: capability, input: parse(completed.arguments), call: { id: completed.id, name: completed.name, argumentChars: completed.arguments.length } };
82
+ const capability = names.get(completed.name) ??
83
+ modelProtocolError('Model call is not registered for this stage');
84
+ return {
85
+ capabilityId: capability,
86
+ input: parse(completed.arguments),
87
+ call: {
88
+ id: completed.id,
89
+ name: completed.name,
90
+ argumentChars: completed.arguments.length,
91
+ },
92
+ };
51
93
  },
52
94
  };
53
95
  },
@@ -1,3 +1,19 @@
1
+ /**
2
+ * Redaction of model messages before they are observed or persisted.
3
+ *
4
+ * Diagnostics may keep message text, but image bytes must never leave the process,
5
+ * even when full content logging is enabled.
6
+ *
7
+ * Exports: `modelMessageDiagnostics`.
8
+ */
1
9
  import type { ModelMessage } from '../structs/model/messages.js';
2
- /** Image bytes are never persisted in model diagnostics, even with full text logging. */
10
+ /**
11
+ * Replace every image payload with a placeholder.
12
+ *
13
+ * Only the `url` is substituted; the `detail` hint is kept because it is small and
14
+ * explains how the image was interpreted. The input array is not mutated.
15
+ *
16
+ * @param messages - Messages about to be reported.
17
+ * @returns New message objects whose images carry a placeholder URL.
18
+ */
3
19
  export declare function modelMessageDiagnostics(messages: readonly ModelMessage[]): ModelMessage[];
@@ -1,6 +1,19 @@
1
- /** Image bytes are never persisted in model diagnostics, even with full text logging. */
1
+ /**
2
+ * Replace every image payload with a placeholder.
3
+ *
4
+ * Only the `url` is substituted; the `detail` hint is kept because it is small and
5
+ * explains how the image was interpreted. The input array is not mutated.
6
+ *
7
+ * @param messages - Messages about to be reported.
8
+ * @returns New message objects whose images carry a placeholder URL.
9
+ */
2
10
  export function modelMessageDiagnostics(messages) {
3
- return messages.map(message => ({ ...message, ...(message.images ? {
4
- images: message.images.map(image => ({ ...image, url: '[image omitted]' })),
5
- } : {}) }));
11
+ return messages.map((message) => ({
12
+ ...message,
13
+ ...(message.images
14
+ ? {
15
+ images: message.images.map((image) => ({ ...image, url: '[image omitted]' })),
16
+ }
17
+ : {}),
18
+ }));
6
19
  }
@@ -1,3 +1,20 @@
1
+ /**
2
+ * Snapshot helper for per-call model options.
3
+ *
4
+ * Options may be class instances with getters, so every field is read explicitly
5
+ * instead of being spread; fields the caller did not supply are then removed so
6
+ * "absent" and "explicitly undefined" stay distinguishable downstream.
7
+ *
8
+ * Exports: `snapshotModelOptions`.
9
+ */
1
10
  import type { ModelOptions } from '../structs/model/messages.js';
2
- /** Read structural options, including prototype getters, while preserving explicit overrides. */
11
+ /**
12
+ * Copy declared model options, preserving explicit overrides.
13
+ *
14
+ * `onUsage` is rebound to the original options object so a method keeps its receiver.
15
+ * The deletion pass restores absent keys that the explicit reads had materialized.
16
+ *
17
+ * @param options - Source options; defaults to an empty object.
18
+ * @returns A plain options object containing only the keys present on the source.
19
+ */
3
20
  export declare function snapshotModelOptions(options?: Readonly<ModelOptions>): ModelOptions;
@@ -1,4 +1,12 @@
1
- /** Read structural options, including prototype getters, while preserving explicit overrides. */
1
+ /**
2
+ * Copy declared model options, preserving explicit overrides.
3
+ *
4
+ * `onUsage` is rebound to the original options object so a method keeps its receiver.
5
+ * The deletion pass restores absent keys that the explicit reads had materialized.
6
+ *
7
+ * @param options - Source options; defaults to an empty object.
8
+ * @returns A plain options object containing only the keys present on the source.
9
+ */
2
10
  export function snapshotModelOptions(options = {}) {
3
11
  const snapshot = {
4
12
  json: options.json,
@@ -1,9 +1,37 @@
1
1
  import type { ModelTransportDefinition } from '../structs/model/runtime.js';
2
2
  import type { PreparedModelTransport } from '../structs/model/interactions.js';
3
3
  import type { ModelMessage } from '../structs/model/messages.js';
4
- /** Snapshot a dynamic transport once, preserving its collector factory receiver. */
4
+ /**
5
+ * Snapshot a dynamic transport once, preserving its collector factory receiver.
6
+ *
7
+ * The transport gets the stage id so it can specialize its instruction. Its options
8
+ * are copied, its function declarations cloned, and the whole snapshot frozen before
9
+ * the response factory is bound to the prepared source.
10
+ *
11
+ * @param transport - Registered transport definition.
12
+ * @param stageId - Stage the call belongs to.
13
+ * @returns A frozen prepared transport reused for measurement and the provider call.
14
+ * @throws TypeError When preparation is asynchronous or produces an invalid shape.
15
+ */
5
16
  export declare function prepareModelTransport(transport: ModelTransportDefinition, stageId: string): PreparedModelTransport;
6
- /** Message text and declarations are measured once, irrespective of native or in-message schema. */
17
+ /**
18
+ * Measure a prepared request into its character accounting.
19
+ *
20
+ * Each message is read once into a frozen snapshot before any sum is computed, so
21
+ * caller mutations and extra getters cannot change the request between accounting,
22
+ * output backpressure and provider I/O.
23
+ *
24
+ * The instruction is merged into the effective messages exactly once, mirroring what
25
+ * the provider receives; `instructionChars` is therefore derived from that merge rather
26
+ * than measured separately. `transportChars` is the larger of the transport's declared
27
+ * schema size and the instruction plus in-message schema size, because a transport may
28
+ * declare native declarations instead of embedding them.
29
+ *
30
+ * @param base - Caller messages; must be a message array.
31
+ * @param prepared - Result of `prepareModelTransport` for the same call.
32
+ * @returns Frozen effective messages plus per-part and total character counts.
33
+ * @throws TypeError When the message array or any message is invalid.
34
+ */
7
35
  export declare function preparedInputSize(base: readonly ModelMessage[], prepared: PreparedModelTransport): {
8
36
  messages: readonly Readonly<ModelMessage>[];
9
37
  messageChars: number;
@@ -1,50 +1,124 @@
1
+ /**
2
+ * One-shot preparation and measurement of a model request.
3
+ *
4
+ * Preparation freezes the transport's options, instruction and response factory before
5
+ * any counting happens; measurement then derives every size from that same snapshot.
6
+ * Splitting the two phases is what guarantees that the numbers checked against the
7
+ * budget are the numbers actually sent to the provider.
8
+ *
9
+ * Exports: `prepareModelTransport` and `preparedInputSize`.
10
+ */
1
11
  import { modelInputMessages } from './token-budget.js';
2
12
  import { synchronous } from '../utils/synchronous.js';
3
13
  import { freezeSchema } from '../utils/object.js';
4
14
  import { snapshotModelOptions } from './options.js';
5
15
  import { breakWhile } from '../utils/rules.js';
6
16
  // Only snapshots owned and frozen by this module are reusable across measurements.
17
+ // Keyed by the frozen options object so a repeated measure of the same prepared call
18
+ // does not re-serialize a large function schema.
7
19
  const functionSizes = new WeakMap();
20
+ /**
21
+ * Validate and freeze one image reference.
22
+ *
23
+ * `detail` is restricted to the values understood by every supported provider so the
24
+ * wire payload never carries an unvalidated hint.
25
+ *
26
+ * @param image - Candidate image reference.
27
+ * @returns A frozen image object without an undefined `detail`.
28
+ * @throws TypeError When the shape or `detail` value is invalid.
29
+ */
8
30
  function snapshotImage(image) {
9
31
  breakWhile(!image || typeof image !== 'object' || Array.isArray(image), () => new TypeError('Invalid model image'));
10
32
  const { url, detail } = image;
11
- breakWhile(typeof url !== 'string' || !url || (detail !== undefined && !['auto', 'low', 'high', 'original'].includes(detail)), () => new TypeError('Invalid model image'));
33
+ breakWhile(typeof url !== 'string' ||
34
+ !url ||
35
+ (detail !== undefined && !['auto', 'low', 'high', 'original'].includes(detail)), () => new TypeError('Invalid model image'));
12
36
  return Object.freeze({ url, ...(detail === undefined ? {} : { detail }) });
13
37
  }
14
- /** Snapshot a dynamic transport once, preserving its collector factory receiver. */
38
+ /**
39
+ * Snapshot a dynamic transport once, preserving its collector factory receiver.
40
+ *
41
+ * The transport gets the stage id so it can specialize its instruction. Its options
42
+ * are copied, its function declarations cloned, and the whole snapshot frozen before
43
+ * the response factory is bound to the prepared source.
44
+ *
45
+ * @param transport - Registered transport definition.
46
+ * @param stageId - Stage the call belongs to.
47
+ * @returns A frozen prepared transport reused for measurement and the provider call.
48
+ * @throws TypeError When preparation is asynchronous or produces an invalid shape.
49
+ */
15
50
  export function prepareModelTransport(transport, stageId) {
16
51
  const source = synchronous(transport.prepare(stageId), 'Model transport preparation must be synchronous');
17
52
  if (!source)
18
53
  throw new TypeError('Invalid prepared model transport');
19
54
  const { schemaChars, instruction, createResponse } = source;
20
- if (!Number.isSafeInteger(schemaChars) || schemaChars < 0 || typeof createResponse !== 'function'
21
- || (instruction !== undefined && typeof instruction !== 'string'))
55
+ if (!Number.isSafeInteger(schemaChars) ||
56
+ schemaChars < 0 ||
57
+ typeof createResponse !== 'function' ||
58
+ (instruction !== undefined && typeof instruction !== 'string'))
22
59
  throw new TypeError('Invalid prepared model transport');
23
60
  const options = snapshotModelOptions(source.options);
24
61
  if (options.functions)
25
62
  options.functions = structuredClone(options.functions);
26
63
  freezeSchema(options);
27
- const prepared = Object.freeze({ options, schemaChars, instruction, createResponse: createResponse.bind(source) });
64
+ const prepared = Object.freeze({
65
+ options,
66
+ schemaChars,
67
+ instruction,
68
+ createResponse: createResponse.bind(source),
69
+ });
28
70
  if (options.functions)
29
71
  functionSizes.set(options.functions, JSON.stringify(options.functions).length);
30
72
  return prepared;
31
73
  }
32
- /** Message text and declarations are measured once, irrespective of native or in-message schema. */
74
+ /**
75
+ * Measure a prepared request into its character accounting.
76
+ *
77
+ * Each message is read once into a frozen snapshot before any sum is computed, so
78
+ * caller mutations and extra getters cannot change the request between accounting,
79
+ * output backpressure and provider I/O.
80
+ *
81
+ * The instruction is merged into the effective messages exactly once, mirroring what
82
+ * the provider receives; `instructionChars` is therefore derived from that merge rather
83
+ * than measured separately. `transportChars` is the larger of the transport's declared
84
+ * schema size and the instruction plus in-message schema size, because a transport may
85
+ * declare native declarations instead of embedding them.
86
+ *
87
+ * @param base - Caller messages; must be a message array.
88
+ * @param prepared - Result of `prepareModelTransport` for the same call.
89
+ * @returns Frozen effective messages plus per-part and total character counts.
90
+ * @throws TypeError When the message array or any message is invalid.
91
+ */
33
92
  export function preparedInputSize(base, prepared) {
34
93
  // Read only protocol fields, once. Caller mutations and extra getters cannot
35
94
  // change the request between accounting, output backpressure and Provider I/O.
36
95
  breakWhile(!Array.isArray(base), () => new TypeError('Model messages must be an array'));
37
- const snapshot = Array.from(base, message => {
96
+ const snapshot = Array.from(base, (message) => {
38
97
  breakWhile(!message || typeof message !== 'object' || Array.isArray(message), () => new TypeError('Invalid model message'));
39
98
  const { role, content, images } = message;
40
- breakWhile(!['system', 'user', 'assistant'].includes(role) || typeof content !== 'string' || (images !== undefined && !Array.isArray(images)), () => new TypeError('Invalid model message'));
41
- return Object.freeze({ role, content, ...(images === undefined ? {} : { images: Object.freeze(Array.from(images, snapshotImage)) }) });
99
+ breakWhile(!['system', 'user', 'assistant'].includes(role) ||
100
+ typeof content !== 'string' ||
101
+ (images !== undefined && !Array.isArray(images)), () => new TypeError('Invalid model message'));
102
+ return Object.freeze({
103
+ role,
104
+ content,
105
+ ...(images === undefined ? {} : { images: Object.freeze(Array.from(images, snapshotImage)) }),
106
+ });
42
107
  });
43
- const messages = Object.freeze(modelInputMessages(snapshot, prepared.instruction).map(message => Object.freeze(message)));
108
+ const messages = Object.freeze(modelInputMessages(snapshot, prepared.instruction).map((message) => Object.freeze(message)));
44
109
  const messageChars = snapshot.reduce((sum, message) => sum + message.content.length, 0);
45
110
  const instructionChars = messages.reduce((sum, message) => sum + message.content.length, 0) - messageChars;
46
111
  const functions = prepared.options.functions;
47
- const schemaChars = functions ? functionSizes.get(functions) ?? JSON.stringify(functions).length : 0;
112
+ const schemaChars = functions
113
+ ? (functionSizes.get(functions) ?? JSON.stringify(functions).length)
114
+ : 0;
48
115
  const transportChars = Math.max(prepared.schemaChars, instructionChars + schemaChars);
49
- return { messages, messageChars, instructionChars, schemaChars, transportChars, inputChars: messageChars + instructionChars + schemaChars };
116
+ return {
117
+ messages,
118
+ messageChars,
119
+ instructionChars,
120
+ schemaChars,
121
+ transportChars,
122
+ inputChars: messageChars + instructionChars + schemaChars,
123
+ };
50
124
  }