@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,6 +1,19 @@
1
1
  import type { ModelTransportRegistration } from '../structs/model/interactions.js';
2
2
  import type { ModelTransportDefinition } from '../structs/model/runtime.js';
3
- /** The transport translates a wire selection into the single Core response protocol. */
3
+ /**
4
+ * Compose a registered selection transport.
5
+ *
6
+ * The capability source may be a static catalog or a function of the stage id; the
7
+ * resulting definitions are captured when `prepare` runs and are used both to build
8
+ * the instruction and to reject unknown capability ids at finish time.
9
+ *
10
+ * `requireStrictArguments` is a registration-time assertion: a transport cannot demand
11
+ * strict arguments from an adapter that does not support them.
12
+ *
13
+ * @param registration - Adapter, capability source, options and the transport id.
14
+ * @returns A frozen transport definition whose result is the resolved capability output.
15
+ * @throws TypeError When strict arguments are unsupported or preparation is invalid.
16
+ */
4
17
  export declare function createModelTransport<Result, const Id extends string = string>(registration: ModelTransportRegistration<Result> & {
5
18
  readonly id: Id;
6
19
  }): ModelTransportDefinition<Result> & {
@@ -1,34 +1,74 @@
1
+ /**
2
+ * Adapt an interaction adapter plus capability catalog into a transport.
3
+ *
4
+ * This is the seam between "how a provider expresses a call" and "what the core
5
+ * accepts": whatever the wire format, the transport must yield one capability id and
6
+ * one argument object. Capability availability is checked against the catalog captured
7
+ * at preparation time, so a late registration cannot be called.
8
+ *
9
+ * Exports: `createModelTransport`.
10
+ */
1
11
  import { synchronous } from '../utils/synchronous.js';
2
12
  import { nativeFunctionInteraction } from './interactions/native-functions.js';
3
13
  import { snapshotModelOptions } from './options.js';
4
14
  import { modelProtocolError } from '../utils/model-protocol.js';
5
- /** The transport translates a wire selection into the single Core response protocol. */
15
+ /**
16
+ * Compose a registered selection transport.
17
+ *
18
+ * The capability source may be a static catalog or a function of the stage id; the
19
+ * resulting definitions are captured when `prepare` runs and are used both to build
20
+ * the instruction and to reject unknown capability ids at finish time.
21
+ *
22
+ * `requireStrictArguments` is a registration-time assertion: a transport cannot demand
23
+ * strict arguments from an adapter that does not support them.
24
+ *
25
+ * @param registration - Adapter, capability source, options and the transport id.
26
+ * @returns A frozen transport definition whose result is the resolved capability output.
27
+ * @throws TypeError When strict arguments are unsupported or preparation is invalid.
28
+ */
6
29
  export function createModelTransport(registration) {
7
30
  const adapter = registration.adapter ?? nativeFunctionInteraction;
8
31
  if (registration.requireStrictArguments && !adapter.features.strictArguments)
9
32
  throw new TypeError(`Model interaction does not support strict arguments: ${adapter.id}`);
10
33
  const source = registration.capabilities, createSource = typeof source === 'function' ? source.bind(registration) : undefined;
11
34
  const prepare = adapter.prepare.bind(adapter), options = Object.freeze(snapshotModelOptions(registration.options));
12
- return Object.freeze({ id: registration.id, prepare(stageId) {
13
- const catalog = createSource ? synchronous(createSource(stageId), 'Model capability sources must be synchronous') : source;
35
+ return Object.freeze({
36
+ id: registration.id,
37
+ prepare(stageId) {
38
+ const catalog = createSource
39
+ ? synchronous(createSource(stageId), 'Model capability sources must be synchronous')
40
+ : source;
14
41
  const definitions = [...catalog.values()];
15
42
  if (!definitions.length)
16
43
  throw new TypeError('Model stage requires registered capabilities');
17
- const capabilityIds = new Set(definitions.map(capability => capability.id));
44
+ const capabilityIds = new Set(definitions.map((capability) => capability.id));
18
45
  const prepared = synchronous(prepare(definitions), 'Model interaction preparation must be synchronous');
19
46
  const createResponse = prepared.createResponse.bind(prepared), resolve = catalog.resolve.bind(catalog);
20
- return { options: { ...options, ...snapshotModelOptions(prepared.options) }, schemaChars: prepared.schemaChars, instruction: prepared.instruction,
47
+ return {
48
+ options: { ...options, ...snapshotModelOptions(prepared.options) },
49
+ schemaChars: prepared.schemaChars,
50
+ instruction: prepared.instruction,
21
51
  createResponse() {
22
52
  const buffer = createResponse();
23
53
  let call;
24
- return { get outputChars() { return buffer.outputChars; }, get call() { return call; }, push: buffer.push.bind(buffer), finish() {
54
+ return {
55
+ get outputChars() {
56
+ return buffer.outputChars;
57
+ },
58
+ get call() {
59
+ return call;
60
+ },
61
+ push: buffer.push.bind(buffer),
62
+ finish() {
25
63
  const selection = synchronous(buffer.finish(), 'Model selection parsers must be synchronous');
26
64
  call = selection.call;
27
65
  if (!capabilityIds.has(selection.capabilityId))
28
66
  modelProtocolError('Model capability is unavailable in this prepared call');
29
67
  return synchronous(resolve(selection, { stageId }), 'Model result parsers must be synchronous');
30
- } };
68
+ },
69
+ };
31
70
  },
32
71
  };
33
- } });
72
+ },
73
+ });
34
74
  }
@@ -1,5 +1,16 @@
1
1
  import type { ModelRunnerOptions, ModelRunnerPorts, ModelRuntimeConfig } from '../structs/model/runtime.js';
2
2
  import type { ChatProvider } from '../structs/model/chat-provider.js';
3
+ /**
4
+ * Validate options and fill in baseline defaults.
5
+ *
6
+ * Legacy names (`config`, `ports`, `observe`, `signal`, `contextPolicy` and friends) are
7
+ * rejected rather than ignored, because silently dropping them would change behavior.
8
+ * Returned limits are frozen; ports keep live App callbacks with their receivers bound.
9
+ *
10
+ * @param options - Runner options as supplied by the App.
11
+ * @returns The bound provider, frozen runtime config, input limit, ports and token limit.
12
+ * @throws TypeError When any option is invalid, removed or out of range.
13
+ */
3
14
  export declare function resolveModelSettings(options: ModelRunnerOptions): {
4
15
  provider: ChatProvider;
5
16
  config: ModelRuntimeConfig;
@@ -1,6 +1,26 @@
1
+ /**
2
+ * Validation and defaulting of model runner settings.
3
+ *
4
+ * This is the single place where App-supplied settings become trusted: removed option
5
+ * names are rejected with a migration hint, defaults are applied, and every numeric
6
+ * limit is range-checked before the runner binds its provider.
7
+ *
8
+ * Exports: `resolveModelSettings`.
9
+ */
1
10
  import { modelRunnerDefaults } from './defaults.js';
2
11
  import { breakWhile } from '../utils/rules.js';
3
12
  import { recordErrorContext } from '../errors.js';
13
+ /**
14
+ * Validate options and fill in baseline defaults.
15
+ *
16
+ * Legacy names (`config`, `ports`, `observe`, `signal`, `contextPolicy` and friends) are
17
+ * rejected rather than ignored, because silently dropping them would change behavior.
18
+ * Returned limits are frozen; ports keep live App callbacks with their receivers bound.
19
+ *
20
+ * @param options - Runner options as supplied by the App.
21
+ * @returns The bound provider, frozen runtime config, input limit, ports and token limit.
22
+ * @throws TypeError When any option is invalid, removed or out of range.
23
+ */
4
24
  export function resolveModelSettings(options) {
5
25
  const provider = options.provider;
6
26
  const stream = provider?.stream;
@@ -11,8 +31,16 @@ export function resolveModelSettings(options) {
11
31
  };
12
32
  breakWhile([
13
33
  { occur: typeof stream !== 'function', error: () => invalid('provider.stream is required') },
14
- { occur: 'contextPolicy' in options || 'contextCompressors' in options || 'contextSelectors' in options, error: () => invalid('Configure context processing with explicit Workflow nodes') },
15
- { occur: 'config' in options || 'ports' in options || 'observe' in options, error: () => invalid('Use named model settings and execution, sink, observe or errors') },
34
+ {
35
+ occur: 'contextPolicy' in options ||
36
+ 'contextCompressors' in options ||
37
+ 'contextSelectors' in options,
38
+ error: () => invalid('Configure context processing with explicit Workflow nodes'),
39
+ },
40
+ {
41
+ occur: 'config' in options || 'ports' in options || 'observe' in options,
42
+ error: () => invalid('Use named model settings and execution, sink, observe or errors'),
43
+ },
16
44
  ]);
17
45
  const defaulted = (value, fallback) => value === undefined ? fallback : value;
18
46
  const config = {
@@ -22,8 +50,10 @@ export function resolveModelSettings(options) {
22
50
  usage: options.usage,
23
51
  };
24
52
  const ports = {
25
- transports: options.transports, diagnostics: options.execution?.diagnostics,
26
- maxSchemaChars: options.maxSchemaChars, tokenBudget: options.tokenBudget,
53
+ transports: options.transports,
54
+ diagnostics: options.execution?.diagnostics,
55
+ maxSchemaChars: options.maxSchemaChars,
56
+ tokenBudget: options.tokenBudget,
27
57
  onOutput: options.sink?.output?.bind(options.sink),
28
58
  inputBudgetError: options.errors?.inputBudget?.bind(options.errors),
29
59
  outputBudgetError: options.errors?.outputBudget?.bind(options.errors),
@@ -32,15 +62,39 @@ export function resolveModelSettings(options) {
32
62
  ports.maxSchemaChars = defaulted(ports.maxSchemaChars, modelRunnerDefaults.maxSchemaChars);
33
63
  const maxInputChars = defaulted(options.maxInputChars, modelRunnerDefaults.maxInputChars);
34
64
  const maxOutputTokens = defaulted(options.maxOutputTokens, modelRunnerDefaults.maxOutputTokens);
35
- const integer = (value, minimum, infinity = false) => infinity && value === Infinity
36
- || typeof value === 'number' && Number.isSafeInteger(value) && value >= minimum;
65
+ const integer = (value, minimum, infinity = false) => (infinity && value === Infinity) ||
66
+ (typeof value === 'number' && Number.isSafeInteger(value) && value >= minimum);
37
67
  breakWhile([
38
- { occur: !integer(config.maxCalls, 0), error: () => invalid('maxCalls must be a nonnegative safe integer') },
39
- { occur: !integer(config.callTimeoutMs, 1) || config.callTimeoutMs > 2147483647, error: () => invalid('callTimeoutMs must be a positive supported timeout') },
40
- { occur: !integer(maxInputChars, 0, true), error: () => invalid('maxInputChars must be a nonnegative safe integer or Infinity') },
41
- { occur: config.maxOutputChars !== undefined && !integer(config.maxOutputChars, 0, true), error: () => invalid('maxOutputChars must be a nonnegative safe integer or Infinity') },
42
- { occur: ports.maxSchemaChars !== undefined && !integer(ports.maxSchemaChars, 1), error: () => invalid('maxSchemaChars must be a positive safe integer') },
43
- { occur: !integer(maxOutputTokens, 1), error: () => invalid('maxOutputTokens must be a positive safe integer') },
68
+ {
69
+ occur: !integer(config.maxCalls, 0),
70
+ error: () => invalid('maxCalls must be a nonnegative safe integer'),
71
+ },
72
+ {
73
+ occur: !integer(config.callTimeoutMs, 1) || config.callTimeoutMs > 2147483647,
74
+ error: () => invalid('callTimeoutMs must be a positive supported timeout'),
75
+ },
76
+ {
77
+ occur: !integer(maxInputChars, 0, true),
78
+ error: () => invalid('maxInputChars must be a nonnegative safe integer or Infinity'),
79
+ },
80
+ {
81
+ occur: config.maxOutputChars !== undefined && !integer(config.maxOutputChars, 0, true),
82
+ error: () => invalid('maxOutputChars must be a nonnegative safe integer or Infinity'),
83
+ },
84
+ {
85
+ occur: ports.maxSchemaChars !== undefined && !integer(ports.maxSchemaChars, 1),
86
+ error: () => invalid('maxSchemaChars must be a positive safe integer'),
87
+ },
88
+ {
89
+ occur: !integer(maxOutputTokens, 1),
90
+ error: () => invalid('maxOutputTokens must be a positive safe integer'),
91
+ },
44
92
  ]);
45
- return { provider: Object.freeze({ stream: stream.bind(provider) }), maxOutputTokens, config: Object.freeze(config), maxInputChars, ports };
93
+ return {
94
+ provider: Object.freeze({ stream: stream.bind(provider) }),
95
+ maxOutputTokens,
96
+ config: Object.freeze(config),
97
+ maxInputChars,
98
+ ports,
99
+ };
46
100
  }
@@ -1,8 +1,66 @@
1
+ /**
2
+ * Token accounting for inputs that must fit a context window.
3
+ *
4
+ * Character limits alone cannot protect a provider call, so a host that supplies a
5
+ * token counter also declares the context window and a safety margin. The limit
6
+ * reserves room for the requested output before any input is counted.
7
+ *
8
+ * Exports: `validateModelTokenBudget`, `modelInputTokenLimit`, `countModelInput`,
9
+ * `enforceModelInputTokens` and `modelInputMessages`.
10
+ */
1
11
  import type { ModelTokenBudget } from '../structs/model/runtime.js';
2
12
  import type { ModelMessage, ModelOptions } from '../structs/model/messages.js';
13
+ /**
14
+ * Validate a host-supplied token budget contract.
15
+ *
16
+ * @param budget - Budget to check: counter id, context window, optional safety margin, counter.
17
+ * @throws ValidationError When any field is missing or out of range.
18
+ */
3
19
  export declare function validateModelTokenBudget(budget: ModelTokenBudget): void;
20
+ /**
21
+ * Compute how many input tokens remain for one call.
22
+ *
23
+ * The requested output tokens and the safety margin are subtracted from the context
24
+ * window, so a call that reserves more than the window is rejected before counting.
25
+ *
26
+ * @param budget - Declared context window and safety margin.
27
+ * @param outputTokens - Tokens reserved for the completion.
28
+ * @returns The positive input token allowance.
29
+ * @throws ValidationError When the reservation is invalid or leaves no input capacity.
30
+ */
4
31
  export declare function modelInputTokenLimit(budget: ModelTokenBudget, outputTokens: number): number;
32
+ /**
33
+ * Count the tokens of a prepared request.
34
+ *
35
+ * The counter observes a request, so both messages and declarations are cloned before
36
+ * being handed over: accidental mutations by the counter must not rewrite the provider
37
+ * input. The returned count must itself be a nonnegative safe integer.
38
+ *
39
+ * @param budget - Budget supplying the counter.
40
+ * @param messages - Effective messages, including the merged instruction.
41
+ * @param options - Function declarations and JSON mode as they will be sent.
42
+ * @returns The token count reported by the host counter.
43
+ * @throws ValidationError When the counter returns an unusable number.
44
+ */
5
45
  export declare function countModelInput(budget: ModelTokenBudget, messages: readonly ModelMessage[], options: Pick<ModelOptions, 'functions' | 'json'>): number;
46
+ /**
47
+ * Enforce the measured input against its token allowance.
48
+ *
49
+ * @param count - Measured input tokens.
50
+ * @param limit - Allowance from `modelInputTokenLimit`.
51
+ * @throws ValidationError When the input exceeds the allowance.
52
+ */
6
53
  export declare function enforceModelInputTokens(count: number, limit: number): void;
7
- /** Reused by preflight measurement and the provider call so instructions are counted exactly once. */
54
+ /**
55
+ * Merge a transport instruction into the message list.
56
+ *
57
+ * An existing system message keeps its position and receives the instruction as a new
58
+ * paragraph; otherwise a system message is prepended. Reusing the same function for
59
+ * preflight measurement and the provider call is what makes instructions count exactly
60
+ * once.
61
+ *
62
+ * @param base - Caller messages; not mutated.
63
+ * @param instruction - Optional instruction to merge.
64
+ * @returns A new message array.
65
+ */
8
66
  export declare function modelInputMessages(base: readonly ModelMessage[], instruction?: string): ModelMessage[];
@@ -1,34 +1,92 @@
1
1
  import { ValidationError } from '../structs/errors.js';
2
2
  import { breakWhile } from '../utils/rules.js';
3
3
  const invalid = (message) => new ValidationError([{ code: 'MODEL_TOKEN_BUDGET', message }]);
4
+ /**
5
+ * Validate a host-supplied token budget contract.
6
+ *
7
+ * @param budget - Budget to check: counter id, context window, optional safety margin, counter.
8
+ * @throws ValidationError When any field is missing or out of range.
9
+ */
4
10
  export function validateModelTokenBudget(budget) {
5
- breakWhile(!budget.counterId || !Number.isSafeInteger(budget.contextWindowTokens) || budget.contextWindowTokens < 1
6
- || !Number.isSafeInteger(budget.safetyMarginTokens ?? 0) || (budget.safetyMarginTokens ?? 0) < 0
7
- || typeof budget.countTokens !== 'function', invalid('Invalid model token budget'));
11
+ breakWhile(!budget.counterId ||
12
+ !Number.isSafeInteger(budget.contextWindowTokens) ||
13
+ budget.contextWindowTokens < 1 ||
14
+ !Number.isSafeInteger(budget.safetyMarginTokens ?? 0) ||
15
+ (budget.safetyMarginTokens ?? 0) < 0 ||
16
+ typeof budget.countTokens !== 'function', invalid('Invalid model token budget'));
8
17
  }
18
+ /**
19
+ * Compute how many input tokens remain for one call.
20
+ *
21
+ * The requested output tokens and the safety margin are subtracted from the context
22
+ * window, so a call that reserves more than the window is rejected before counting.
23
+ *
24
+ * @param budget - Declared context window and safety margin.
25
+ * @param outputTokens - Tokens reserved for the completion.
26
+ * @returns The positive input token allowance.
27
+ * @throws ValidationError When the reservation is invalid or leaves no input capacity.
28
+ */
9
29
  export function modelInputTokenLimit(budget, outputTokens) {
10
30
  const limit = budget.contextWindowTokens - outputTokens - (budget.safetyMarginTokens ?? 0);
11
31
  breakWhile(!Number.isSafeInteger(outputTokens) || outputTokens < 1 || limit < 1, invalid('No input capacity after reserving output and safety margin'));
12
32
  return limit;
13
33
  }
34
+ /**
35
+ * Count the tokens of a prepared request.
36
+ *
37
+ * The counter observes a request, so both messages and declarations are cloned before
38
+ * being handed over: accidental mutations by the counter must not rewrite the provider
39
+ * input. The returned count must itself be a nonnegative safe integer.
40
+ *
41
+ * @param budget - Budget supplying the counter.
42
+ * @param messages - Effective messages, including the merged instruction.
43
+ * @param options - Function declarations and JSON mode as they will be sent.
44
+ * @returns The token count reported by the host counter.
45
+ * @throws ValidationError When the counter returns an unusable number.
46
+ */
14
47
  export function countModelInput(budget, messages, options) {
15
48
  // A counter observes a request; accidental mutations must not rewrite the provider input.
16
- const snapshot = messages.map(message => ({ ...message,
17
- ...(message.images ? { images: message.images.map(image => ({ ...image })) } : {}),
49
+ const snapshot = messages.map((message) => ({
50
+ ...message,
51
+ ...(message.images ? { images: message.images.map((image) => ({ ...image })) } : {}),
18
52
  }));
19
- const count = budget.countTokens({ messages: snapshot,
20
- functions: options.functions ? structuredClone(options.functions) : undefined, json: options.json });
53
+ const count = budget.countTokens({
54
+ messages: snapshot,
55
+ functions: options.functions ? structuredClone(options.functions) : undefined,
56
+ json: options.json,
57
+ });
21
58
  breakWhile(!Number.isSafeInteger(count) || count < 0, invalid('Token counter must return a nonnegative integer'));
22
59
  return count;
23
60
  }
61
+ /**
62
+ * Enforce the measured input against its token allowance.
63
+ *
64
+ * @param count - Measured input tokens.
65
+ * @param limit - Allowance from `modelInputTokenLimit`.
66
+ * @throws ValidationError When the input exceeds the allowance.
67
+ */
24
68
  export function enforceModelInputTokens(count, limit) {
25
69
  breakWhile(count > limit, invalid(`Model input uses ${count} tokens; available input budget is ${limit}`));
26
70
  }
27
- /** Reused by preflight measurement and the provider call so instructions are counted exactly once. */
71
+ /**
72
+ * Merge a transport instruction into the message list.
73
+ *
74
+ * An existing system message keeps its position and receives the instruction as a new
75
+ * paragraph; otherwise a system message is prepended. Reusing the same function for
76
+ * preflight measurement and the provider call is what makes instructions count exactly
77
+ * once.
78
+ *
79
+ * @param base - Caller messages; not mutated.
80
+ * @param instruction - Optional instruction to merge.
81
+ * @returns A new message array.
82
+ */
28
83
  export function modelInputMessages(base, instruction) {
29
84
  if (!instruction)
30
85
  return [...base];
31
- const index = base.findIndex(message => message.role === 'system');
32
- return index < 0 ? [{ role: 'system', content: instruction }, ...base]
33
- : base.map((message, position) => position === index ? { ...message, content: `${message.content}\n\n${instruction}` } : message);
86
+ const index = base.findIndex((message) => message.role === 'system');
87
+ return index < 0
88
+ ? [{ role: 'system', content: instruction }, ...base]
89
+ : base.map((message, position) => position === index
90
+ ? { ...message, content: `${message.content}\n\n${instruction}` }
91
+ : message);
34
92
  }
@@ -1,8 +1,26 @@
1
1
  import type { ModelOptions } from '../structs/model/messages.js';
2
2
  import type { ModelTransportDefinition } from '../structs/model/runtime.js';
3
- /** Shared Core mechanism, also used by the optional App-side text creator. */
3
+ /**
4
+ * Build a transport that expects a single plain-text completion.
5
+ *
6
+ * JSON mode is forced off because a text stage has no envelope to parse, and the final
7
+ * text is trimmed so transport-specific whitespace never reaches the caller.
8
+ *
9
+ * @param id - Unique transport id, also used as the typed handle in `ModelRunner`.
10
+ * @param options - Optional request options; only `temperature` is honored.
11
+ * @returns A frozen transport definition.
12
+ */
4
13
  export declare function createPlainTextTransport<const Id extends string>(id: Id, options?: Pick<ModelOptions, 'temperature'>): ModelTransportDefinition<string> & {
5
14
  readonly id: Id;
6
15
  };
7
- /** Pin the supplied implementation and preserve its receiver at registration. */
16
+ /**
17
+ * Freeze a transport and pin its preparation receiver.
18
+ *
19
+ * Binding at registration means a later caller cannot swap the definition's identity
20
+ * or detach `prepare` from the object that specializes it.
21
+ *
22
+ * @param definition - Transport supplied by the App or by a factory.
23
+ * @returns A frozen definition with a bound `prepare`.
24
+ * @throws TypeError When the id is blank or `prepare` is missing.
25
+ */
8
26
  export declare function normalizeModelTransport<Result>(definition: ModelTransportDefinition<Result>): ModelTransportDefinition<Result>;
@@ -1,20 +1,73 @@
1
+ /**
2
+ * Transport definitions and the built-in plain-text transport.
3
+ *
4
+ * A transport owns the wire-level request shape (options, declarations, instruction)
5
+ * and the collector that decodes streamed parts into one result. The runner never
6
+ * interprets provider output itself, so adding a protocol means adding a transport
7
+ * rather than branching in the core.
8
+ *
9
+ * Exports: `createPlainTextTransport` and `normalizeModelTransport`.
10
+ */
1
11
  import { synchronous } from '../utils/synchronous.js';
2
12
  import { ValidationError } from '../structs/errors.js';
3
13
  import { snapshotModelOptions } from './options.js';
14
+ /**
15
+ * Collector that concatenates plain text into one decoded value.
16
+ *
17
+ * A non-string part means the provider emitted a native call event, which a plain-text
18
+ * stage cannot represent; that is reported as a protocol violation instead of being
19
+ * stringified.
20
+ *
21
+ * @param decode - Final conversion from accumulated raw text to the result.
22
+ * @returns A collector reporting the buffered character count.
23
+ */
4
24
  function textResponse(decode) {
5
25
  let raw = '';
6
- return { get outputChars() { return raw.length; }, push(part) {
26
+ return {
27
+ get outputChars() {
28
+ return raw.length;
29
+ },
30
+ push(part) {
7
31
  if (typeof part !== 'string')
8
- throw new ValidationError([{ code: 'MODEL_PROTOCOL', message: 'Text transport does not accept model calls' }]);
32
+ throw new ValidationError([
33
+ { code: 'MODEL_PROTOCOL', message: 'Text transport does not accept model calls' },
34
+ ]);
9
35
  raw += part;
10
- }, finish: () => synchronous(decode(raw), 'Model result parsers must be synchronous') };
36
+ },
37
+ finish: () => synchronous(decode(raw), 'Model result parsers must be synchronous'),
38
+ };
11
39
  }
12
- /** Shared Core mechanism, also used by the optional App-side text creator. */
40
+ /**
41
+ * Build a transport that expects a single plain-text completion.
42
+ *
43
+ * JSON mode is forced off because a text stage has no envelope to parse, and the final
44
+ * text is trimmed so transport-specific whitespace never reaches the caller.
45
+ *
46
+ * @param id - Unique transport id, also used as the typed handle in `ModelRunner`.
47
+ * @param options - Optional request options; only `temperature` is honored.
48
+ * @returns A frozen transport definition.
49
+ */
13
50
  export function createPlainTextTransport(id, options = {}) {
14
51
  const settings = Object.freeze({ ...snapshotModelOptions(options), json: false });
15
- return Object.freeze({ id, prepare: (_stageId) => ({ options: settings, schemaChars: 0, createResponse: () => textResponse(raw => raw.trim()) }) });
52
+ return Object.freeze({
53
+ id,
54
+ prepare: (_stageId) => ({
55
+ options: settings,
56
+ schemaChars: 0,
57
+ createResponse: () => textResponse((raw) => raw.trim()),
58
+ }),
59
+ });
16
60
  }
17
- /** Pin the supplied implementation and preserve its receiver at registration. */
61
+ /**
62
+ * Freeze a transport and pin its preparation receiver.
63
+ *
64
+ * Binding at registration means a later caller cannot swap the definition's identity
65
+ * or detach `prepare` from the object that specializes it.
66
+ *
67
+ * @param definition - Transport supplied by the App or by a factory.
68
+ * @returns A frozen definition with a bound `prepare`.
69
+ * @throws TypeError When the id is blank or `prepare` is missing.
70
+ */
18
71
  export function normalizeModelTransport(definition) {
19
72
  if (!definition.id?.trim() || typeof definition.prepare !== 'function')
20
73
  throw new TypeError('Invalid model transport');
@@ -1,28 +1,64 @@
1
+ /**
2
+ * Tool invocation lifecycle contracts: request-local journal, handler shape and host ports.
3
+ *
4
+ * These declarations let an App compose tool handlers from a journal and optional ports without
5
+ * Core learning any domain concept. The journal records execution receipts; the host decides what
6
+ * a receipt means for its own domain.
7
+ *
8
+ * Exports: `ToolJournalStart`, `ToolJournalFinish`, `ToolJournal`, `InvocationHandler`,
9
+ * `ToolInvocationPorts`.
10
+ */
1
11
  import type { InvocationMetadata } from '../invocation.js';
12
+ /** Opening record for one tool call, written before the handler runs. */
2
13
  export interface ToolJournalStart {
14
+ /** Tool being invoked. */
3
15
  toolId: string;
16
+ /** Parsed input; stored as-is so the host can build its own domain record. */
4
17
  input: unknown;
18
+ /** What the caller already knows. The journal fills gaps and generates an id when absent. */
5
19
  invocation?: Partial<InvocationMetadata>;
6
20
  }
21
+ /** Closing record for one tool call, written once with the terminal outcome. */
7
22
  export interface ToolJournalFinish {
23
+ /** Invocation opened by `beginTool`. */
8
24
  invocationId: string;
25
+ /** Terminal status; `cancelled` is distinct from `error`. */
9
26
  status: 'success' | 'error' | 'cancelled';
27
+ /** Produced value on success; failure detail on error; ignored for cancellation. */
10
28
  value: unknown;
11
29
  }
12
30
  /** Request-local execution receipts; the host decides their domain content. */
13
31
  export interface ToolJournal {
32
+ /** Opens a receipt and returns the invocation id later passed to `finishTool`. */
14
33
  beginTool(request: ToolJournalStart): string;
34
+ /** Closes the receipt; must be called exactly once per opened invocation. */
15
35
  finishTool(request: ToolJournalFinish): void;
16
36
  }
37
+ /** A named handler invoked by a tool lifecycle step. */
17
38
  export interface InvocationHandler<Input, Context> {
18
39
  readonly id: string;
40
+ /**
41
+ * Runs the handler.
42
+ *
43
+ * The return value is ignored, so handlers report outcomes through the tool value or journal.
44
+ * @param input - Step input supplied by the lifecycle.
45
+ * @param context - Host context of the request.
46
+ * @param execution - Execution facts of this invocation.
47
+ */
19
48
  call(input: Input, context: Context, execution: import('./tools.js').ToolExecutionContext): void;
20
49
  }
50
+ /** Optional capabilities the lifecycle resolves from the host context for one invocation. */
21
51
  export interface ToolInvocationPorts<Context> {
52
+ /** Diagnostic scope; omitted disables recording for this tool. */
22
53
  readonly diagnostics?: (context: Context) => import('../diagnostics.js').DiagnosticContext | undefined;
54
+ /** Extra cancellation; must not replace the tool execution signal. */
23
55
  readonly signal?: (context: Context) => AbortSignal | undefined;
56
+ /** Journal receiving begin/finish receipts. */
24
57
  readonly journal?: (context: Context) => ToolJournal | undefined;
58
+ /** Extracts the domain checkpoint recorded with the receipt. */
25
59
  readonly checkpoint?: (context: Context) => unknown;
60
+ /** Builds the domain receipt from a checkpoint after execution. */
26
61
  readonly receipt?: (context: Context, checkpoint: unknown) => unknown;
62
+ /** Converts an error into the receipt's replacement value. */
27
63
  readonly receiptError?: (error: unknown) => unknown;
28
64
  }
@@ -1,25 +1,52 @@
1
+ /**
2
+ * Model capability contract: the business operations a model may select during a call.
3
+ *
4
+ * A capability pairs one JSON schema with a resolver that turns model-supplied arguments into a
5
+ * host value. Registration snapshots the schema and pins the resolver, so a request can project a
6
+ * sealed subset without cloning handlers.
7
+ *
8
+ * Exports: `ModelCapability`, `ModelCapabilityRegistration`, `RegisteredModelCapability`,
9
+ * `ModelSelection`, `ModelCapabilitySource`, `ModelCapabilityCatalog`.
10
+ */
1
11
  import type { SchemaAdapter, ReadonlyCatalog } from '../common.js';
12
+ /** One selectable model operation. */
2
13
  export interface ModelCapability<Input = unknown, Output = Input> {
14
+ /** Unique id within its registry; never blank. */
3
15
  readonly id: string;
16
+ /** Model-visible description explaining when the capability applies. */
4
17
  readonly description: string;
18
+ /** Schema describing the arguments; must be an object schema. */
5
19
  readonly inputSchema: SchemaAdapter<Input>;
20
+ /**
21
+ * Converts validated arguments into the value handed back to the caller.
22
+ *
23
+ * Runs inside the model call, so it must return synchronously; throwing rejects the selection.
24
+ */
6
25
  resolve(input: Input, context: Readonly<{
7
26
  stageId: string;
8
27
  }>): Output;
9
28
  }
29
+ /** Registration form: input typing is erased, output typing is preserved. */
10
30
  export type ModelCapabilityRegistration<Output = unknown> = ModelCapability<any, Output>;
31
+ /** Frozen snapshot stored by a registry after schema validation. */
11
32
  export type RegisteredModelCapability<Output = unknown> = Readonly<ModelCapability<unknown, Output>>;
33
+ /** A model's chosen capability together with its parsed arguments. */
12
34
  export type ModelSelection = {
35
+ /** Arguments exactly as produced by the model, before schema parsing. */
13
36
  readonly input: Record<string, unknown>;
37
+ /** Native function call that carried the selection, when the provider used one. */
14
38
  readonly call?: {
15
39
  id: string;
16
40
  name: string;
17
41
  argumentChars: number;
18
42
  };
43
+ /** Capability the selection resolved to. */
19
44
  readonly capabilityId: string;
20
45
  };
46
+ /** Minimal source a stage needs: enumerate capabilities and resolve one selection. */
21
47
  export interface ModelCapabilitySource<Output = unknown> {
22
48
  values(): IterableIterator<RegisteredModelCapability<Output>>;
49
+ /** Resolves one selection; throws when the capability is absent from this source. */
23
50
  resolve(selection: ModelSelection, context: Readonly<{
24
51
  stageId: string;
25
52
  }>): Output;