@sixb/core 0.1.26 → 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 (555) hide show
  1. package/dist/actions/index.js +47 -47
  2. package/dist/actions/request.d.ts.map +1 -1
  3. package/dist/actions/run/context.d.ts +1 -0
  4. package/dist/actions/run/context.d.ts.map +1 -1
  5. package/dist/actions/run/execute.d.ts +3 -0
  6. package/dist/actions/run/execute.d.ts.map +1 -1
  7. package/dist/actions/run/executor.d.ts +3 -0
  8. package/dist/actions/run/executor.d.ts.map +1 -1
  9. package/dist/actions/run/types.d.ts +5 -0
  10. package/dist/actions/run/types.d.ts.map +1 -1
  11. package/dist/actions/types/context.d.ts +11 -0
  12. package/dist/actions/types/context.d.ts.map +1 -1
  13. package/dist/agents/adapters.d.ts +30 -4
  14. package/dist/agents/adapters.d.ts.map +1 -1
  15. package/dist/agents/approvals.d.ts +8 -0
  16. package/dist/agents/approvals.d.ts.map +1 -1
  17. package/dist/agents/context-budget.d.ts +48 -0
  18. package/dist/agents/context-budget.d.ts.map +1 -0
  19. package/dist/agents/context-compaction.d.ts +89 -9
  20. package/dist/agents/context-compaction.d.ts.map +1 -1
  21. package/dist/agents/context-files.d.ts +12 -0
  22. package/dist/agents/context-files.d.ts.map +1 -0
  23. package/dist/agents/context-parts.d.ts +41 -0
  24. package/dist/agents/context-parts.d.ts.map +1 -0
  25. package/dist/agents/context-request.d.ts +47 -0
  26. package/dist/agents/context-request.d.ts.map +1 -0
  27. package/dist/agents/context-token-rate.d.ts +98 -0
  28. package/dist/agents/context-token-rate.d.ts.map +1 -0
  29. package/dist/agents/context-use.d.ts +51 -0
  30. package/dist/agents/context-use.d.ts.map +1 -0
  31. package/dist/agents/dispatch.d.ts.map +1 -1
  32. package/dist/agents/execution.d.ts +13 -1
  33. package/dist/agents/execution.d.ts.map +1 -1
  34. package/dist/agents/index.d.ts +21 -7
  35. package/dist/agents/index.d.ts.map +1 -1
  36. package/dist/agents/index.js +109 -55
  37. package/dist/agents/index.js.map +1 -1
  38. package/dist/agents/inline-images.d.ts +17 -0
  39. package/dist/agents/inline-images.d.ts.map +1 -0
  40. package/dist/agents/message.d.ts +55 -0
  41. package/dist/agents/message.d.ts.map +1 -1
  42. package/dist/agents/model-loop.d.ts +23 -3
  43. package/dist/agents/model-loop.d.ts.map +1 -1
  44. package/dist/agents/request.d.ts +6 -1
  45. package/dist/agents/request.d.ts.map +1 -1
  46. package/dist/agents/run-events.d.ts +3 -2
  47. package/dist/agents/run-events.d.ts.map +1 -1
  48. package/dist/agents/run-plan.d.ts +12 -2
  49. package/dist/agents/run-plan.d.ts.map +1 -1
  50. package/dist/agents/run-session-http.d.ts +6 -3
  51. package/dist/agents/run-session-http.d.ts.map +1 -1
  52. package/dist/agents/run-session-serve.d.ts +18 -2
  53. package/dist/agents/run-session-serve.d.ts.map +1 -1
  54. package/dist/agents/run-session.d.ts +3 -1
  55. package/dist/agents/run-session.d.ts.map +1 -1
  56. package/dist/agents/run-tools.d.ts +7 -0
  57. package/dist/agents/run-tools.d.ts.map +1 -1
  58. package/dist/agents/run-turn.d.ts +39 -4
  59. package/dist/agents/run-turn.d.ts.map +1 -1
  60. package/dist/agents/skills.d.ts +9 -2
  61. package/dist/agents/skills.d.ts.map +1 -1
  62. package/dist/agents/step-limits.d.ts +18 -0
  63. package/dist/agents/step-limits.d.ts.map +1 -0
  64. package/dist/agents/streams/index.d.ts +1 -1
  65. package/dist/agents/streams/index.d.ts.map +1 -1
  66. package/dist/agents/streams/index.js +2 -2
  67. package/dist/agents/streams/internal.d.ts +1 -1
  68. package/dist/agents/streams/internal.d.ts.map +1 -1
  69. package/dist/agents/streams/internal.js +7 -3
  70. package/dist/agents/streams/internal.js.map +1 -1
  71. package/dist/agents/streams/protocol.d.ts +36 -1
  72. package/dist/agents/streams/protocol.d.ts.map +1 -1
  73. package/dist/agents/thread-context-projection.d.ts +10 -0
  74. package/dist/agents/thread-context-projection.d.ts.map +1 -1
  75. package/dist/agents/tool-files.d.ts +22 -0
  76. package/dist/agents/tool-files.d.ts.map +1 -1
  77. package/dist/artifacts/publish/index.js +26 -26
  78. package/dist/auth/index.js +12 -12
  79. package/dist/auth/index.js.map +1 -1
  80. package/dist/auth/runtime.d.ts +2 -0
  81. package/dist/auth/runtime.d.ts.map +1 -1
  82. package/dist/auth/types.d.ts +5 -0
  83. package/dist/auth/types.d.ts.map +1 -1
  84. package/dist/authorization/decision.d.ts +2 -0
  85. package/dist/authorization/decision.d.ts.map +1 -1
  86. package/dist/authorization/grant-kinds.d.ts +2 -2
  87. package/dist/authorization/grant-kinds.d.ts.map +1 -1
  88. package/dist/authorization/index.d.ts +1 -1
  89. package/dist/authorization/index.d.ts.map +1 -1
  90. package/dist/authorization/index.js +7 -5
  91. package/dist/authorization/index.js.map +1 -1
  92. package/dist/authorization/run-visibility.d.ts +17 -0
  93. package/dist/authorization/run-visibility.d.ts.map +1 -1
  94. package/dist/bootstrap/index.js +53 -53
  95. package/dist/{chunk-t3brw0wd.js → chunk-0g5nyx0k.js} +181 -30
  96. package/dist/chunk-0g5nyx0k.js.map +14 -0
  97. package/dist/{chunk-fakrpfy3.js → chunk-0k1h5hmr.js} +2 -1
  98. package/dist/{chunk-fakrpfy3.js.map → chunk-0k1h5hmr.js.map} +3 -3
  99. package/dist/{chunk-c0vk1djt.js → chunk-165c9wm6.js} +1 -1
  100. package/dist/{chunk-8mb7mt8n.js → chunk-21q0j5f2.js} +2 -2
  101. package/dist/{chunk-hn76sanm.js → chunk-23s66j86.js} +1 -1
  102. package/dist/{chunk-h1na2cxc.js → chunk-2g0r46hy.js} +2 -2
  103. package/dist/{chunk-8v6xzwgr.js → chunk-3x3bebz7.js} +95 -19
  104. package/dist/{chunk-8v6xzwgr.js.map → chunk-3x3bebz7.js.map} +10 -9
  105. package/dist/{chunk-tdxfsg1c.js → chunk-47e09r7z.js} +2 -2
  106. package/dist/{chunk-73j0v4qv.js → chunk-4zm1k3xj.js} +14 -2
  107. package/dist/{chunk-73j0v4qv.js.map → chunk-4zm1k3xj.js.map} +3 -3
  108. package/dist/{chunk-npcr0228.js → chunk-5kyvrtjh.js} +1 -1
  109. package/dist/{chunk-zhh1jd89.js → chunk-5taem5aw.js} +1 -1
  110. package/dist/{chunk-tnhqtfea.js → chunk-6b61s40c.js} +1 -1
  111. package/dist/{chunk-4ymrcdbm.js → chunk-7hd2ez3q.js} +1 -1
  112. package/dist/{chunk-315qye3g.js → chunk-80nh73br.js} +1 -1
  113. package/dist/{chunk-phm3c7b9.js → chunk-855n80en.js} +1 -1
  114. package/dist/{chunk-qst145py.js → chunk-8dcfj58k.js} +24 -14
  115. package/dist/chunk-8dcfj58k.js.map +18 -0
  116. package/dist/{chunk-7wn5z7mq.js → chunk-8hvfs0fr.js} +5 -5
  117. package/dist/{chunk-2vjnyz1s.js → chunk-9rh8ncs5.js} +2 -2
  118. package/dist/{chunk-pgrf64hp.js → chunk-agn8d8ys.js} +1 -1
  119. package/dist/{chunk-mqx25wwh.js → chunk-c78jjzhy.js} +12 -10
  120. package/dist/{chunk-mqx25wwh.js.map → chunk-c78jjzhy.js.map} +3 -3
  121. package/dist/{chunk-7ex2p3zp.js → chunk-cgznz22f.js} +2 -2
  122. package/dist/{chunk-06sg8s4z.js → chunk-d3t26vah.js} +2 -2
  123. package/dist/{chunk-vhbw5sqm.js → chunk-d7v97qxq.js} +7 -1
  124. package/dist/chunk-d7v97qxq.js.map +10 -0
  125. package/dist/chunk-de5sfbc3.js +3 -0
  126. package/dist/{chunk-ratkv4zc.js.map → chunk-de5sfbc3.js.map} +1 -1
  127. package/dist/{chunk-qgvbxfqv.js → chunk-djs3hw9p.js} +1 -1
  128. package/dist/{chunk-ye5c8016.js → chunk-dz7a4t3a.js} +26 -5
  129. package/dist/{chunk-ye5c8016.js.map → chunk-dz7a4t3a.js.map} +4 -4
  130. package/dist/{chunk-dgtd0836.js → chunk-e7v3trpz.js} +1 -1
  131. package/dist/{chunk-8v5a54qz.js → chunk-e99kp9jh.js} +161 -38
  132. package/dist/{chunk-8v5a54qz.js.map → chunk-e99kp9jh.js.map} +10 -10
  133. package/dist/{chunk-z9mg7wya.js → chunk-edx8xcsm.js} +111 -11
  134. package/dist/chunk-edx8xcsm.js.map +13 -0
  135. package/dist/{chunk-0bc3w0zq.js → chunk-ekkpzcka.js} +5 -5
  136. package/dist/{chunk-zbc929zg.js → chunk-escedwyt.js} +9 -1
  137. package/dist/{chunk-zbc929zg.js.map → chunk-escedwyt.js.map} +3 -3
  138. package/dist/{chunk-4jq2bg8t.js → chunk-f2w6328x.js} +6 -6
  139. package/dist/{chunk-b6ghvap7.js → chunk-fb3m4sa1.js} +7 -7
  140. package/dist/{chunk-yavhtnac.js → chunk-fdkpkkpw.js} +89 -43
  141. package/dist/{chunk-yavhtnac.js.map → chunk-fdkpkkpw.js.map} +3 -3
  142. package/dist/{chunk-jt3r6thp.js → chunk-fg1db9xx.js} +46 -6
  143. package/dist/chunk-fg1db9xx.js.map +10 -0
  144. package/dist/{chunk-73v685wb.js → chunk-frsz8wxd.js} +4 -4
  145. package/dist/{chunk-73v685wb.js.map → chunk-frsz8wxd.js.map} +3 -3
  146. package/dist/{chunk-jn3ysaf2.js → chunk-he7g39e5.js} +11 -11
  147. package/dist/{chunk-jn3ysaf2.js.map → chunk-he7g39e5.js.map} +1 -1
  148. package/dist/{chunk-yd00vzqr.js → chunk-hjy3dsws.js} +1009 -354
  149. package/dist/chunk-hjy3dsws.js.map +45 -0
  150. package/dist/{chunk-59kf345w.js → chunk-jbw70d6t.js} +2 -2
  151. package/dist/{chunk-cbcgs9f7.js → chunk-jftd33rc.js} +21 -21
  152. package/dist/chunk-jftd33rc.js.map +11 -0
  153. package/dist/{chunk-78ymxrn0.js → chunk-jhbk10wf.js} +1 -1
  154. package/dist/{chunk-18sk89cg.js → chunk-jk97v21j.js} +7 -2
  155. package/dist/chunk-jk97v21j.js.map +10 -0
  156. package/dist/chunk-kcqpk9w1.js.map +2 -2
  157. package/dist/{chunk-ft3w5ahs.js → chunk-m3qe54bb.js} +1 -1
  158. package/dist/{chunk-8xnz9vvp.js → chunk-n54k068r.js} +22 -18
  159. package/dist/chunk-n54k068r.js.map +12 -0
  160. package/dist/{chunk-y7j6x4h0.js → chunk-neftrg0n.js} +3 -3
  161. package/dist/{chunk-07kkp2e1.js → chunk-nghc7eq0.js} +2 -2
  162. package/dist/{chunk-1xvqgg88.js → chunk-nz7815t3.js} +18 -2
  163. package/dist/chunk-nz7815t3.js.map +14 -0
  164. package/dist/{chunk-fe8zwt8b.js → chunk-peggaard.js} +2 -2
  165. package/dist/{chunk-e24fvw8g.js → chunk-qbrmpgvv.js} +1 -1
  166. package/dist/{chunk-dp0cdtj2.js → chunk-qsg8rw04.js} +2 -2
  167. package/dist/{chunk-mdxmt0n3.js → chunk-qya541bk.js} +1071 -121
  168. package/dist/chunk-qya541bk.js.map +129 -0
  169. package/dist/{chunk-0kk2wt0h.js → chunk-r1cxq7vv.js} +2 -2
  170. package/dist/{chunk-kyjcwqz6.js → chunk-rtkqczbr.js} +3 -1
  171. package/dist/{chunk-kyjcwqz6.js.map → chunk-rtkqczbr.js.map} +3 -3
  172. package/dist/{chunk-2adz0h75.js → chunk-s7xqfxs9.js} +35 -3
  173. package/dist/chunk-s7xqfxs9.js.map +11 -0
  174. package/dist/{chunk-g663fahq.js → chunk-scyq22td.js} +1 -1
  175. package/dist/{chunk-xm8qgbqm.js → chunk-semf1yqn.js} +54 -44
  176. package/dist/{chunk-xm8qgbqm.js.map → chunk-semf1yqn.js.map} +5 -5
  177. package/dist/{chunk-pdt5cw8z.js → chunk-tvefbgvw.js} +5 -2
  178. package/dist/{chunk-pdt5cw8z.js.map → chunk-tvefbgvw.js.map} +3 -3
  179. package/dist/{chunk-vx4e4zv4.js → chunk-vkweppkw.js} +3 -3
  180. package/dist/{chunk-45ngkxkp.js → chunk-w122ymx4.js} +2 -2
  181. package/dist/{chunk-48jezz3j.js → chunk-w976a4me.js} +1 -1
  182. package/dist/{chunk-qtzftvxa.js → chunk-wx7smkn2.js} +1 -1
  183. package/dist/{chunk-6qn7nmd3.js → chunk-xj97s5js.js} +9 -9
  184. package/dist/{chunk-c14ap3s9.js → chunk-y2x2935j.js} +3 -3
  185. package/dist/{chunk-7hf6tkgs.js → chunk-ytzcxtpt.js} +5 -5
  186. package/dist/{chunk-7hf6tkgs.js.map → chunk-ytzcxtpt.js.map} +1 -1
  187. package/dist/{chunk-axb4ss9a.js → chunk-ztf5vtmp.js} +5 -5
  188. package/dist/datasets/execution.d.ts +26 -1
  189. package/dist/datasets/execution.d.ts.map +1 -1
  190. package/dist/datasets/index.d.ts +1 -1
  191. package/dist/datasets/index.d.ts.map +1 -1
  192. package/dist/datasets/lineage.d.ts +55 -0
  193. package/dist/datasets/lineage.d.ts.map +1 -0
  194. package/dist/datasets/write.js +5 -5
  195. package/dist/deploy/types.d.ts +8 -1
  196. package/dist/deploy/types.d.ts.map +1 -1
  197. package/dist/env/index.d.ts +36 -0
  198. package/dist/env/index.d.ts.map +1 -0
  199. package/dist/env/index.js +17 -0
  200. package/dist/env/index.js.map +10 -0
  201. package/dist/error-reporting/internal.js +3 -3
  202. package/dist/errors/catalog.d.ts +8 -0
  203. package/dist/errors/catalog.d.ts.map +1 -1
  204. package/dist/errors/internal.js +1 -1
  205. package/dist/events/index.js +3 -3
  206. package/dist/execution/agent.js +47 -47
  207. package/dist/execution/index.d.ts +1 -0
  208. package/dist/execution/index.d.ts.map +1 -1
  209. package/dist/execution/primitive.js +47 -47
  210. package/dist/execution/request-client.d.ts +21 -0
  211. package/dist/execution/request-client.d.ts.map +1 -0
  212. package/dist/execution/request.d.ts.map +1 -1
  213. package/dist/execution/request.js +49 -47
  214. package/dist/execution/request.js.map +3 -3
  215. package/dist/http/api-routes.d.ts.map +1 -1
  216. package/dist/http/index.js +1 -1
  217. package/dist/index.d.ts +7 -5
  218. package/dist/index.d.ts.map +1 -1
  219. package/dist/index.js +65 -65
  220. package/dist/index.js.map +1 -1
  221. package/dist/lake-storage/index.js +5 -5
  222. package/dist/lineage/commits.d.ts +45 -0
  223. package/dist/lineage/commits.d.ts.map +1 -0
  224. package/dist/lineage/executors.d.ts +14 -0
  225. package/dist/lineage/executors.d.ts.map +1 -0
  226. package/dist/lineage/link.d.ts +10 -0
  227. package/dist/lineage/link.d.ts.map +1 -0
  228. package/dist/lineage/object.d.ts +10 -0
  229. package/dist/lineage/object.d.ts.map +1 -0
  230. package/dist/lineage/projections.d.ts +34 -0
  231. package/dist/lineage/projections.d.ts.map +1 -0
  232. package/dist/lineage/reader.d.ts +34 -0
  233. package/dist/lineage/reader.d.ts.map +1 -0
  234. package/dist/lineage/sources.d.ts +62 -0
  235. package/dist/lineage/sources.d.ts.map +1 -0
  236. package/dist/lineage/types.d.ts +140 -0
  237. package/dist/lineage/types.d.ts.map +1 -0
  238. package/dist/materializer/effective/resolve.d.ts +63 -6
  239. package/dist/materializer/effective/resolve.d.ts.map +1 -1
  240. package/dist/materializer/index.js +27 -27
  241. package/dist/models/errors.d.ts +8 -0
  242. package/dist/models/errors.d.ts.map +1 -1
  243. package/dist/models/events.d.ts +10 -0
  244. package/dist/models/events.d.ts.map +1 -1
  245. package/dist/models/execution/index.d.ts +1 -0
  246. package/dist/models/execution/index.d.ts.map +1 -1
  247. package/dist/models/execution/index.js +34 -31
  248. package/dist/models/execution/index.js.map +1 -1
  249. package/dist/models/execution/model-call-recorder.d.ts +2 -0
  250. package/dist/models/execution/model-call-recorder.d.ts.map +1 -1
  251. package/dist/models/index.d.ts +2 -1
  252. package/dist/models/index.d.ts.map +1 -1
  253. package/dist/models/index.js +3 -3
  254. package/dist/models/language-model.d.ts +29 -2
  255. package/dist/models/language-model.d.ts.map +1 -1
  256. package/dist/objects/execution.d.ts +4 -1
  257. package/dist/objects/execution.d.ts.map +1 -1
  258. package/dist/objects/index.js +47 -47
  259. package/dist/objects/query/executor.d.ts.map +1 -1
  260. package/dist/objects/query/index.d.ts +1 -0
  261. package/dist/objects/query/index.d.ts.map +1 -1
  262. package/dist/objects/query/index.js +30 -28
  263. package/dist/objects/query/index.js.map +1 -1
  264. package/dist/objects/query/ir.d.ts +16 -0
  265. package/dist/objects/query/ir.d.ts.map +1 -1
  266. package/dist/objects/query/link-endpoints.d.ts +11 -0
  267. package/dist/objects/query/link-endpoints.d.ts.map +1 -0
  268. package/dist/objects/query/normalize.d.ts.map +1 -1
  269. package/dist/objects/query/planner.d.ts +5 -0
  270. package/dist/objects/query/planner.d.ts.map +1 -1
  271. package/dist/objects/query/validate.d.ts +2 -0
  272. package/dist/objects/query/validate.d.ts.map +1 -1
  273. package/dist/objects/sdk/object-set.d.ts +2 -0
  274. package/dist/objects/sdk/object-set.d.ts.map +1 -1
  275. package/dist/ontology/index.js +5 -5
  276. package/dist/ontology/internal.js +3 -3
  277. package/dist/pipelines/index.js +31 -31
  278. package/dist/projections/internal.js +27 -27
  279. package/dist/query.js +2 -2
  280. package/dist/queues/in-memory.d.ts +1 -1
  281. package/dist/queues/in-memory.d.ts.map +1 -1
  282. package/dist/runtime/create.d.ts +3 -0
  283. package/dist/runtime/create.d.ts.map +1 -1
  284. package/dist/runtime/internal.js +33 -33
  285. package/dist/runtime/resolve-definitions.d.ts.map +1 -1
  286. package/dist/runtime/types.d.ts +16 -0
  287. package/dist/runtime/types.d.ts.map +1 -1
  288. package/dist/sandboxes/exec.d.ts.map +1 -1
  289. package/dist/sandboxes/index.js +5 -5
  290. package/dist/security/grants.d.ts +1 -1
  291. package/dist/security/grants.d.ts.map +1 -1
  292. package/dist/security/internal.js +1 -1
  293. package/dist/security/project.d.ts.map +1 -1
  294. package/dist/security/public.js +2 -2
  295. package/dist/security/types.d.ts +10 -3
  296. package/dist/security/types.d.ts.map +1 -1
  297. package/dist/security/validation.d.ts.map +1 -1
  298. package/dist/shares/execution.d.ts +9 -0
  299. package/dist/shares/execution.d.ts.map +1 -1
  300. package/dist/shares/index.d.ts +1 -1
  301. package/dist/shares/index.d.ts.map +1 -1
  302. package/dist/shares/internal.js +24 -24
  303. package/dist/storage/action-runs/failure.js +2 -2
  304. package/dist/storage/agents/in-memory.d.ts +38 -1
  305. package/dist/storage/agents/in-memory.d.ts.map +1 -1
  306. package/dist/storage/agents/index.d.ts +1 -1
  307. package/dist/storage/agents/index.d.ts.map +1 -1
  308. package/dist/storage/agents/provider.d.ts +43 -5
  309. package/dist/storage/agents/provider.d.ts.map +1 -1
  310. package/dist/storage/agents/provider.js +30 -6
  311. package/dist/storage/agents/provider.js.map +1 -1
  312. package/dist/storage/agents/types.d.ts +132 -10
  313. package/dist/storage/agents/types.d.ts.map +1 -1
  314. package/dist/storage/ai-cost/analytics.d.ts +13 -5
  315. package/dist/storage/ai-cost/analytics.d.ts.map +1 -1
  316. package/dist/storage/ai-cost/groups.d.ts +19 -2
  317. package/dist/storage/ai-cost/groups.d.ts.map +1 -1
  318. package/dist/storage/ai-cost/in-memory.d.ts +6 -1
  319. package/dist/storage/ai-cost/in-memory.d.ts.map +1 -1
  320. package/dist/storage/ai-cost/index.d.ts +1 -1
  321. package/dist/storage/ai-cost/index.d.ts.map +1 -1
  322. package/dist/storage/ai-cost/provider.d.ts +1 -1
  323. package/dist/storage/ai-cost/provider.d.ts.map +1 -1
  324. package/dist/storage/ai-cost/provider.js +7 -5
  325. package/dist/storage/ai-cost/provider.js.map +1 -1
  326. package/dist/storage/ai-cost/types.d.ts +65 -9
  327. package/dist/storage/ai-cost/types.d.ts.map +1 -1
  328. package/dist/storage/ai-limits/enforcement.js +4 -4
  329. package/dist/storage/ai-limits/provider.js +2 -2
  330. package/dist/storage/connector-connections/provider.js +1 -1
  331. package/dist/storage/file-upload-sessions/provider.js +10 -10
  332. package/dist/storage/in-memory/index.d.ts.map +1 -1
  333. package/dist/storage/index.d.ts +3 -3
  334. package/dist/storage/index.d.ts.map +1 -1
  335. package/dist/storage/index.js +31 -31
  336. package/dist/storage/index.js.map +1 -1
  337. package/dist/storage/internal.js +6 -6
  338. package/dist/storage/objects/in-memory/query.d.ts.map +1 -1
  339. package/dist/storage/objects/query-preparation.d.ts +5 -0
  340. package/dist/storage/objects/query-preparation.d.ts.map +1 -1
  341. package/dist/storage/ontology/commits.d.ts +6 -1
  342. package/dist/storage/ontology/commits.d.ts.map +1 -1
  343. package/dist/storage/ontology/in-memory/commits.d.ts.map +1 -1
  344. package/dist/storage/ontology/in-memory/materializations.d.ts +2 -1
  345. package/dist/storage/ontology/in-memory/materializations.d.ts.map +1 -1
  346. package/dist/storage/ontology/index.d.ts +1 -1
  347. package/dist/storage/ontology/index.d.ts.map +1 -1
  348. package/dist/storage/ontology/materializations.d.ts +16 -0
  349. package/dist/storage/ontology/materializations.d.ts.map +1 -1
  350. package/dist/storage/ontology/provider.d.ts +6 -1
  351. package/dist/storage/ontology/provider.d.ts.map +1 -1
  352. package/dist/storage/ontology/provider.js +4 -2
  353. package/dist/storage/ontology/provider.js.map +1 -1
  354. package/dist/storage/operation-scope.d.ts +10 -0
  355. package/dist/storage/operation-scope.d.ts.map +1 -1
  356. package/dist/storage/operation-scope.js +7 -3
  357. package/dist/storage/operation-scope.js.map +1 -1
  358. package/dist/storage/projection-runs/provider.js +2 -2
  359. package/dist/storage/workflow-runs/provider.js +10 -10
  360. package/dist/syncs/index.js +32 -32
  361. package/dist/testing/agent-storage-contract.d.ts.map +1 -1
  362. package/dist/testing/agent-token-rate-storage-contract.d.ts +14 -0
  363. package/dist/testing/agent-token-rate-storage-contract.d.ts.map +1 -0
  364. package/dist/testing/ai-model-call-groups-contract.d.ts +1 -1
  365. package/dist/testing/ai-model-call-groups-contract.d.ts.map +1 -1
  366. package/dist/testing/index.d.ts +1 -0
  367. package/dist/testing/index.d.ts.map +1 -1
  368. package/dist/testing/index.js +4365 -3518
  369. package/dist/testing/index.js.map +10 -9
  370. package/dist/testing/materializer-storage-contract.d.ts.map +1 -1
  371. package/dist/testing/object-query-contract.d.ts.map +1 -1
  372. package/dist/testing/ontology-contract-fixture.d.ts +3 -3
  373. package/dist/testing/ontology-contract-fixture.d.ts.map +1 -1
  374. package/dist/testing/ontology-storage-contract.d.ts.map +1 -1
  375. package/dist/webhooks/internal.js +25 -25
  376. package/dist/workers/index.js +1 -1
  377. package/dist/workflows/builders.d.ts.map +1 -1
  378. package/dist/workflows/index.js +52 -52
  379. package/dist/workflows/types.d.ts +6 -0
  380. package/dist/workflows/types.d.ts.map +1 -1
  381. package/dist/workflows/validation.d.ts.map +1 -1
  382. package/package.json +7 -1
  383. package/src/actions/request.ts +6 -0
  384. package/src/actions/run/context.ts +6 -0
  385. package/src/actions/run/execute.ts +8 -2
  386. package/src/actions/run/executor.ts +4 -0
  387. package/src/actions/run/types.ts +5 -0
  388. package/src/actions/types/context.ts +11 -0
  389. package/src/agents/README.md +11 -3
  390. package/src/agents/adapters.ts +73 -23
  391. package/src/agents/approvals.ts +22 -0
  392. package/src/agents/context-budget.ts +121 -0
  393. package/src/agents/context-compaction.ts +317 -150
  394. package/src/agents/context-files.ts +48 -0
  395. package/src/agents/context-parts.ts +219 -0
  396. package/src/agents/context-request.ts +100 -0
  397. package/src/agents/context-token-rate.ts +224 -0
  398. package/src/agents/context-use.ts +187 -0
  399. package/src/agents/dispatch.ts +2 -0
  400. package/src/agents/execution.ts +105 -0
  401. package/src/agents/index.ts +46 -2
  402. package/src/agents/inline-images.ts +17 -0
  403. package/src/agents/message.ts +70 -1
  404. package/src/agents/model-loop.ts +192 -61
  405. package/src/agents/request.ts +18 -1
  406. package/src/agents/run-events.ts +3 -2
  407. package/src/agents/run-plan.ts +22 -3
  408. package/src/agents/run-session-http.ts +8 -5
  409. package/src/agents/run-session-serve.ts +133 -9
  410. package/src/agents/run-session.ts +30 -4
  411. package/src/agents/run-tools.ts +10 -2
  412. package/src/agents/run-turn.ts +116 -26
  413. package/src/agents/skills.ts +9 -2
  414. package/src/agents/step-limits.ts +27 -0
  415. package/src/agents/streams/index.ts +2 -0
  416. package/src/agents/streams/internal.ts +2 -0
  417. package/src/agents/streams/protocol.ts +125 -2
  418. package/src/agents/thread-context-projection.ts +25 -17
  419. package/src/agents/tool-files.ts +41 -3
  420. package/src/auth/runtime.ts +30 -1
  421. package/src/auth/types.ts +5 -0
  422. package/src/authorization/decision.ts +5 -0
  423. package/src/authorization/grant-kinds.ts +8 -0
  424. package/src/authorization/index.ts +2 -0
  425. package/src/authorization/run-visibility.ts +37 -0
  426. package/src/datasets/execution.ts +89 -2
  427. package/src/datasets/index.ts +1 -0
  428. package/src/datasets/lineage.ts +194 -0
  429. package/src/deploy/types.ts +8 -1
  430. package/src/env/index.ts +46 -0
  431. package/src/errors/catalog.ts +12 -0
  432. package/src/execution/index.ts +1 -0
  433. package/src/execution/request-client.ts +44 -0
  434. package/src/execution/request.ts +2 -0
  435. package/src/http/api-routes.ts +24 -25
  436. package/src/index.ts +32 -0
  437. package/src/lineage/README.md +107 -0
  438. package/src/lineage/commits.ts +173 -0
  439. package/src/lineage/executors.ts +103 -0
  440. package/src/lineage/link.ts +138 -0
  441. package/src/lineage/object.ts +218 -0
  442. package/src/lineage/projections.ts +174 -0
  443. package/src/lineage/reader.ts +89 -0
  444. package/src/lineage/sources.ts +156 -0
  445. package/src/lineage/types.ts +149 -0
  446. package/src/materializer/effective/resolve.ts +198 -50
  447. package/src/models/errors.ts +10 -0
  448. package/src/models/events.ts +13 -1
  449. package/src/models/execution/index.ts +1 -0
  450. package/src/models/execution/model-call-recorder.ts +7 -1
  451. package/src/models/index.ts +3 -0
  452. package/src/models/language-model.ts +34 -2
  453. package/src/objects/execution.ts +6 -1
  454. package/src/objects/query/executor.ts +28 -13
  455. package/src/objects/query/index.ts +1 -0
  456. package/src/objects/query/ir.ts +16 -0
  457. package/src/objects/query/link-endpoints.ts +29 -0
  458. package/src/objects/query/normalize.ts +3 -0
  459. package/src/objects/query/planner.ts +5 -0
  460. package/src/objects/query/validate.ts +73 -3
  461. package/src/objects/sdk/object-set.ts +33 -1
  462. package/src/runtime/create.ts +6 -0
  463. package/src/runtime/resolve-definitions.ts +7 -0
  464. package/src/runtime/sixb.ts +4 -1
  465. package/src/runtime/types.ts +18 -0
  466. package/src/sandboxes/exec.ts +19 -0
  467. package/src/security/grants.ts +3 -0
  468. package/src/security/project.ts +1 -0
  469. package/src/security/types.ts +8 -3
  470. package/src/security/validation.ts +5 -4
  471. package/src/shares/execution.ts +35 -3
  472. package/src/shares/index.ts +1 -0
  473. package/src/storage/agents/in-memory.ts +179 -8
  474. package/src/storage/agents/index.ts +8 -0
  475. package/src/storage/agents/provider.ts +219 -6
  476. package/src/storage/agents/types.ts +145 -9
  477. package/src/storage/ai-cost/analytics.ts +177 -10
  478. package/src/storage/ai-cost/groups.ts +122 -35
  479. package/src/storage/ai-cost/in-memory.ts +10 -4
  480. package/src/storage/ai-cost/index.ts +5 -0
  481. package/src/storage/ai-cost/provider.ts +5 -1
  482. package/src/storage/ai-cost/types.ts +65 -9
  483. package/src/storage/in-memory/index.ts +10 -0
  484. package/src/storage/index.ts +15 -0
  485. package/src/storage/objects/in-memory/query.ts +5 -1
  486. package/src/storage/objects/query-preparation.ts +10 -0
  487. package/src/storage/ontology/commits.ts +6 -1
  488. package/src/storage/ontology/in-memory/commits.ts +7 -0
  489. package/src/storage/ontology/in-memory/materializations.ts +24 -0
  490. package/src/storage/ontology/index.ts +2 -0
  491. package/src/storage/ontology/materializations.ts +21 -0
  492. package/src/storage/ontology/provider.ts +41 -1
  493. package/src/storage/operation-scope.ts +41 -0
  494. package/src/testing/agent-storage-contract.ts +426 -20
  495. package/src/testing/agent-token-rate-storage-contract.ts +171 -0
  496. package/src/testing/ai-model-call-groups-contract.ts +225 -7
  497. package/src/testing/index.ts +5 -0
  498. package/src/testing/materializer-storage-contract.ts +294 -10
  499. package/src/testing/object-query-contract.ts +48 -0
  500. package/src/testing/ontology-contract-fixture.ts +18 -7
  501. package/src/testing/ontology-storage-contract.ts +42 -0
  502. package/src/workflows/builders.ts +8 -1
  503. package/src/workflows/types.ts +6 -0
  504. package/src/workflows/validation.ts +1 -0
  505. package/dist/chunk-18sk89cg.js.map +0 -10
  506. package/dist/chunk-1xvqgg88.js.map +0 -14
  507. package/dist/chunk-2adz0h75.js.map +0 -11
  508. package/dist/chunk-8xnz9vvp.js.map +0 -12
  509. package/dist/chunk-cbcgs9f7.js.map +0 -11
  510. package/dist/chunk-jt3r6thp.js.map +0 -10
  511. package/dist/chunk-mdxmt0n3.js.map +0 -120
  512. package/dist/chunk-qst145py.js.map +0 -18
  513. package/dist/chunk-ratkv4zc.js +0 -3
  514. package/dist/chunk-t3brw0wd.js.map +0 -14
  515. package/dist/chunk-vhbw5sqm.js.map +0 -10
  516. package/dist/chunk-yd00vzqr.js.map +0 -37
  517. package/dist/chunk-z9mg7wya.js.map +0 -13
  518. /package/dist/{chunk-c0vk1djt.js.map → chunk-165c9wm6.js.map} +0 -0
  519. /package/dist/{chunk-8mb7mt8n.js.map → chunk-21q0j5f2.js.map} +0 -0
  520. /package/dist/{chunk-hn76sanm.js.map → chunk-23s66j86.js.map} +0 -0
  521. /package/dist/{chunk-h1na2cxc.js.map → chunk-2g0r46hy.js.map} +0 -0
  522. /package/dist/{chunk-tdxfsg1c.js.map → chunk-47e09r7z.js.map} +0 -0
  523. /package/dist/{chunk-npcr0228.js.map → chunk-5kyvrtjh.js.map} +0 -0
  524. /package/dist/{chunk-zhh1jd89.js.map → chunk-5taem5aw.js.map} +0 -0
  525. /package/dist/{chunk-tnhqtfea.js.map → chunk-6b61s40c.js.map} +0 -0
  526. /package/dist/{chunk-4ymrcdbm.js.map → chunk-7hd2ez3q.js.map} +0 -0
  527. /package/dist/{chunk-315qye3g.js.map → chunk-80nh73br.js.map} +0 -0
  528. /package/dist/{chunk-phm3c7b9.js.map → chunk-855n80en.js.map} +0 -0
  529. /package/dist/{chunk-7wn5z7mq.js.map → chunk-8hvfs0fr.js.map} +0 -0
  530. /package/dist/{chunk-2vjnyz1s.js.map → chunk-9rh8ncs5.js.map} +0 -0
  531. /package/dist/{chunk-pgrf64hp.js.map → chunk-agn8d8ys.js.map} +0 -0
  532. /package/dist/{chunk-7ex2p3zp.js.map → chunk-cgznz22f.js.map} +0 -0
  533. /package/dist/{chunk-06sg8s4z.js.map → chunk-d3t26vah.js.map} +0 -0
  534. /package/dist/{chunk-qgvbxfqv.js.map → chunk-djs3hw9p.js.map} +0 -0
  535. /package/dist/{chunk-dgtd0836.js.map → chunk-e7v3trpz.js.map} +0 -0
  536. /package/dist/{chunk-0bc3w0zq.js.map → chunk-ekkpzcka.js.map} +0 -0
  537. /package/dist/{chunk-4jq2bg8t.js.map → chunk-f2w6328x.js.map} +0 -0
  538. /package/dist/{chunk-b6ghvap7.js.map → chunk-fb3m4sa1.js.map} +0 -0
  539. /package/dist/{chunk-59kf345w.js.map → chunk-jbw70d6t.js.map} +0 -0
  540. /package/dist/{chunk-78ymxrn0.js.map → chunk-jhbk10wf.js.map} +0 -0
  541. /package/dist/{chunk-ft3w5ahs.js.map → chunk-m3qe54bb.js.map} +0 -0
  542. /package/dist/{chunk-y7j6x4h0.js.map → chunk-neftrg0n.js.map} +0 -0
  543. /package/dist/{chunk-07kkp2e1.js.map → chunk-nghc7eq0.js.map} +0 -0
  544. /package/dist/{chunk-fe8zwt8b.js.map → chunk-peggaard.js.map} +0 -0
  545. /package/dist/{chunk-e24fvw8g.js.map → chunk-qbrmpgvv.js.map} +0 -0
  546. /package/dist/{chunk-dp0cdtj2.js.map → chunk-qsg8rw04.js.map} +0 -0
  547. /package/dist/{chunk-0kk2wt0h.js.map → chunk-r1cxq7vv.js.map} +0 -0
  548. /package/dist/{chunk-g663fahq.js.map → chunk-scyq22td.js.map} +0 -0
  549. /package/dist/{chunk-vx4e4zv4.js.map → chunk-vkweppkw.js.map} +0 -0
  550. /package/dist/{chunk-45ngkxkp.js.map → chunk-w122ymx4.js.map} +0 -0
  551. /package/dist/{chunk-48jezz3j.js.map → chunk-w976a4me.js.map} +0 -0
  552. /package/dist/{chunk-qtzftvxa.js.map → chunk-wx7smkn2.js.map} +0 -0
  553. /package/dist/{chunk-6qn7nmd3.js.map → chunk-xj97s5js.js.map} +0 -0
  554. /package/dist/{chunk-c14ap3s9.js.map → chunk-y2x2935j.js.map} +0 -0
  555. /package/dist/{chunk-axb4ss9a.js.map → chunk-ztf5vtmp.js.map} +0 -0
@@ -1,21 +1,32 @@
1
1
  import { stableJsonStringify } from "../json"
2
+ import type { ModelMessage, ModelResponseFormat, ModelToolSpecification } from "../models"
2
3
  import type { AgentMessageRecord } from "../storage/agents/types"
3
- import { AGENT_CONTINUATION_INSTRUCTION } from "./adapters"
4
+ import { AGENT_CONTINUATION_INSTRUCTION, agentMessageSteps } from "./adapters"
4
5
  import { agentMailInstruction } from "./agent-mail"
5
6
  import { approvalOutcomeInstruction } from "./approval-outcome"
6
- import type { AgentMessage, AgentMessagePart } from "./message"
7
+ import { agentContextFileKind } from "./context-files"
8
+ import { AGENT_IMAGE_INPUT_TOKENS, AGENT_INLINE_IMAGE_LIMITS } from "./inline-images"
9
+ import type {
10
+ AgentContextPartBytes,
11
+ AgentHistoryPartName,
12
+ AgentMessage,
13
+ AgentMessagePart,
14
+ } from "./message"
15
+ import { isAgentThreadSummaryText } from "./thread-context-projection"
16
+ import { isAgentToolResult } from "./tool-result"
7
17
 
8
- export const AGENT_CONTEXT_ESTIMATOR_VERSION = 1 as const
18
+ /**
19
+ * 2: text counts in UTF-8 bytes at the model's own bytes per token; retained attachments, the
20
+ * images tools returned and stored model-facing tool output count as the model reads them.
21
+ */
22
+ export const AGENT_CONTEXT_ESTIMATOR_VERSION = 2 as const
9
23
 
10
- const CHARS_PER_TOKEN = 4
11
24
  const REQUEST_OVERHEAD_TOKENS = 8
12
25
  const MESSAGE_OVERHEAD_TOKENS = 8
13
26
  const TOOL_OVERHEAD_TOKENS = 16
14
27
  const STEP_OVERHEAD_TOKENS = 2
15
- const IMAGE_ALLOWANCE_TOKENS = 1_200
16
28
  const FILE_TEXT_INLINE_MAX_BYTES = 50 * 1024
17
29
  const FILE_TEXT_INLINE_TOTAL_MAX_BYTES = 200 * 1024
18
- const TOOL_RESULT_SUMMARY_MAX_CHARS = 2_000
19
30
 
20
31
  export interface AgentContextEstimateTool {
21
32
  readonly name: string
@@ -30,6 +41,22 @@ export interface EstimateAgentContextRequestTokensInput {
30
41
  readonly messages: readonly AgentMessage[]
31
42
  }
32
43
 
44
+ /**
45
+ * What a request weighs before any tokenizer: the UTF-8 bytes of its text, and the tokens already
46
+ * counted as such (per-item overheads, image allowances).
47
+ */
48
+ export interface AgentContextSize {
49
+ readonly bytes: number
50
+ readonly tokens: number
51
+ /** The images among `tokens`, each counted as the most a provider charges for one. */
52
+ readonly images?: number
53
+ }
54
+
55
+ /** A history's size, with its bytes split by part (see AgentHistoryPartName). */
56
+ export interface AgentContextHistorySize extends AgentContextSize {
57
+ readonly parts: AgentContextPartBytes<AgentHistoryPartName>
58
+ }
59
+
33
60
  export interface AgentContextTokenEstimate {
34
61
  readonly tokens: number
35
62
  readonly estimatorVersion: typeof AGENT_CONTEXT_ESTIMATOR_VERSION
@@ -49,61 +76,223 @@ export function shouldCompactAgentContext(input: {
49
76
  return input.estimatedInputTokens > input.inputBudgetTokens
50
77
  }
51
78
 
52
- /** Estimate the provider request shape with one stable, conservative character heuristic. */
79
+ /** Estimate the provider request shape at the model's bytes-per-token rate. */
53
80
  export function estimateAgentContextRequestTokens(
54
- input: EstimateAgentContextRequestTokensInput
81
+ input: EstimateAgentContextRequestTokensInput & { readonly bytesPerToken: number }
55
82
  ): AgentContextTokenEstimate {
56
- const toolTokens = input.tools.reduce(
57
- (total, tool) =>
58
- total +
59
- TOOL_OVERHEAD_TOKENS +
60
- textTokens(tool.name) +
61
- textTokens(tool.description) +
62
- textTokens(tool.inputSchema),
63
- 0
64
- )
65
83
  return {
66
- tokens:
67
- REQUEST_OVERHEAD_TOKENS +
68
- textTokens(input.systemPrompt) +
69
- toolTokens +
70
- estimateAgentContextMessagesTokens(input.messages).tokens,
84
+ tokens: agentContextSizeTokens(measureAgentContextRequest(input), input.bytesPerToken),
71
85
  estimatorVersion: AGENT_CONTEXT_ESTIMATOR_VERSION,
72
86
  }
73
87
  }
74
88
 
75
- /** Estimate message-only growth after a provider-usage anchor. */
89
+ /**
90
+ * Estimate message-only growth after a provider-usage anchor. Every user message the history keeps
91
+ * sends its files inline, each message within its own text budget, until compaction summarizes it,
92
+ * and so do the images its tools returned after each step.
93
+ *
94
+ * Images count up to the request's image limit. Its byte limit is left out: an image's encoded
95
+ * size is known only once it is projected, and counting more images is the side that cannot
96
+ * overflow.
97
+ */
76
98
  export function estimateAgentContextMessagesTokens(
77
- messages: readonly AgentMessage[]
99
+ messages: readonly AgentMessage[],
100
+ bytesPerToken: number
78
101
  ): AgentContextTokenEstimate {
79
- const attachments = { textBytesRemaining: FILE_TEXT_INLINE_TOTAL_MAX_BYTES }
80
- let tokens = 0
81
- for (const message of messages) {
82
- tokens += estimateMessageTokens(message, attachments)
102
+ return {
103
+ tokens: agentContextSizeTokens(measureAgentContextHistory({ messages }), bytesPerToken),
104
+ estimatorVersion: AGENT_CONTEXT_ESTIMATOR_VERSION,
105
+ }
106
+ }
107
+
108
+ /** Weigh a stored conversation request: its system prompt, tool catalog and messages. */
109
+ export function measureAgentContextRequest(
110
+ input: EstimateAgentContextRequestTokensInput
111
+ ): AgentContextSize {
112
+ return addAgentContextSizes(measureAgentContextFixed(input), measureAgentContextHistory(input))
113
+ }
114
+
115
+ /** What every request of a conversation carries besides its history: its prompt and its tools. */
116
+ export function measureAgentContextFixed(
117
+ input: Pick<EstimateAgentContextRequestTokensInput, "systemPrompt" | "tools">
118
+ ): AgentContextSize {
119
+ return fixedAgentContextSize({
120
+ systemBytes: utf8Bytes(input.systemPrompt),
121
+ tools: {
122
+ count: input.tools.length,
123
+ bytes: input.tools.reduce(
124
+ (bytes, tool) =>
125
+ bytes + utf8Bytes(tool.name) + utf8Bytes(tool.description) + utf8Bytes(tool.inputSchema),
126
+ 0
127
+ ),
128
+ },
129
+ })
130
+ }
131
+
132
+ /** The same, from bytes already measured: a step's recorded request. */
133
+ export function fixedAgentContextSize(input: {
134
+ readonly systemBytes: number
135
+ readonly tools: { readonly count: number; readonly bytes: number }
136
+ }): AgentContextSize {
137
+ return {
138
+ bytes: input.systemBytes + input.tools.bytes,
139
+ tokens: REQUEST_OVERHEAD_TOKENS + input.tools.count * TOOL_OVERHEAD_TOKENS,
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Weigh a history as a request sends it, split by part. A file the agent read with the read or
145
+ * view_file tool under `contextDirectory` counts as what it is (memory, a skill, the ontology).
146
+ */
147
+ export function measureAgentContextHistory(input: {
148
+ readonly messages: readonly AgentMessage[]
149
+ readonly contextDirectory?: string
150
+ }): AgentContextHistorySize {
151
+ const images = { remaining: AGENT_INLINE_IMAGE_LIMITS.maxImages }
152
+ const size: MeasuredHistory = { bytes: 0, tokens: 0, parts: {} }
153
+ for (const message of input.messages) {
154
+ measureMessage(
155
+ message,
156
+ { textBytesRemaining: FILE_TEXT_INLINE_TOTAL_MAX_BYTES },
157
+ images,
158
+ size,
159
+ input.contextDirectory
160
+ )
161
+ if (message.role === "assistant") size.tokens += toolImageTokens(message, images)
162
+ }
163
+ return { ...size, images: AGENT_INLINE_IMAGE_LIMITS.maxImages - images.remaining }
164
+ }
165
+
166
+ /** Two sizes of one request, added. */
167
+ export function addAgentContextSizes(
168
+ left: AgentContextSize,
169
+ right: AgentContextSize
170
+ ): AgentContextSize {
171
+ return {
172
+ bytes: left.bytes + right.bytes,
173
+ tokens: left.tokens + right.tokens,
174
+ images: (left.images ?? 0) + (right.images ?? 0),
175
+ }
176
+ }
177
+
178
+ /** Price a measured request at a tokenizer's rate. */
179
+ export function agentContextSizeTokens(size: AgentContextSize, bytesPerToken: number): number {
180
+ return size.tokens + Math.ceil(size.bytes / bytesPerToken)
181
+ }
182
+
183
+ /**
184
+ * What an agent's model call weighs before any tokenizer, as the conversation estimator weighs it:
185
+ * what its output ceiling, its guard and its measurement of the tokenizer go by.
186
+ *
187
+ * An image counts as the most a provider charges for one, never as its base64 bytes. Attachments
188
+ * stay in a thread's requests until compaction, so giving up on any file would cap every later
189
+ * call of the thread at the reserve. Other inline files count by their encoded bytes.
190
+ */
191
+ export function measureAgentModelRequest(request: {
192
+ readonly messages: readonly ModelMessage[]
193
+ readonly tools: readonly ModelToolSpecification[]
194
+ readonly responseFormat?: ModelResponseFormat
195
+ }): AgentContextSize {
196
+ const size = { ...measureModelRequest(request), images: 0 }
197
+ for (const message of request.messages) {
198
+ if (message.role !== "user") continue
199
+
200
+ for (const part of message.content) {
201
+ if (part.type !== "file") continue
202
+
203
+ if (part.mediaType.startsWith("image/")) {
204
+ size.tokens += AGENT_IMAGE_INPUT_TOKENS
205
+ size.images += 1
206
+ } else {
207
+ size.bytes += utf8Bytes(part.data.href)
208
+ }
209
+ }
83
210
  }
84
- return { tokens, estimatorVersion: AGENT_CONTEXT_ESTIMATOR_VERSION }
211
+ return size
85
212
  }
86
213
 
87
- /** Select the safe user boundary whose retained tail is closest to the configured recent budget. */
214
+ /**
215
+ * Weigh one outgoing model request the same way as a stored conversation: the same parts, the
216
+ * same overheads. A file part counts by its name and media type only; what an attached file costs
217
+ * is the caller's call.
218
+ */
219
+ export function measureModelRequest(request: {
220
+ readonly messages: readonly ModelMessage[]
221
+ readonly tools: readonly ModelToolSpecification[]
222
+ readonly responseFormat?: ModelResponseFormat
223
+ }): AgentContextSize {
224
+ const size = { bytes: 0, tokens: REQUEST_OVERHEAD_TOKENS }
225
+ for (const tool of request.tools) {
226
+ size.tokens += TOOL_OVERHEAD_TOKENS
227
+ size.bytes +=
228
+ utf8Bytes(tool.name) +
229
+ utf8Bytes(tool.description) +
230
+ utf8Bytes(stableJsonStringify(tool.inputSchema))
231
+ }
232
+ if (request.responseFormat) {
233
+ size.bytes += utf8Bytes(stableJsonStringify(request.responseFormat.schema))
234
+ }
235
+
236
+ for (const message of request.messages) {
237
+ size.tokens += MESSAGE_OVERHEAD_TOKENS
238
+ if (message.role === "system") {
239
+ size.bytes += utf8Bytes(message.content)
240
+ continue
241
+ }
242
+ for (const part of message.content) {
243
+ switch (part.type) {
244
+ case "text":
245
+ case "reasoning":
246
+ size.bytes += utf8Bytes(part.text)
247
+ break
248
+ case "file":
249
+ size.bytes += utf8Bytes(`${part.filename ?? ""} ${part.mediaType}`)
250
+ break
251
+ case "provider-state":
252
+ size.bytes += utf8Bytes(stableJsonStringify(part.data))
253
+ break
254
+ case "tool-call":
255
+ size.tokens += TOOL_OVERHEAD_TOKENS
256
+ size.bytes += utf8Bytes(part.toolName) + utf8Bytes(stableJsonStringify(part.input))
257
+ break
258
+ case "tool-result":
259
+ size.tokens += TOOL_OVERHEAD_TOKENS
260
+ size.bytes +=
261
+ typeof part.output.value === "string"
262
+ ? utf8Bytes(part.output.value)
263
+ : utf8Bytes(stableJsonStringify(part.output.value))
264
+ break
265
+ }
266
+ }
267
+ }
268
+
269
+ return size
270
+ }
271
+
272
+ /**
273
+ * Select the user boundary that keeps the most recent work within `keepRecentTokens`: the oldest
274
+ * user message whose turn, and everything after it, fit; else the newest one, whose turn is always
275
+ * kept. The tail never grows past the allowance to land closer to it, so a compaction ends where
276
+ * it aims and the next turn does not compact again.
277
+ */
88
278
  export function selectAgentContextCompactionBoundary(input: {
89
279
  readonly messages: readonly AgentMessageRecord[]
90
280
  readonly keepRecentTokens: number
281
+ readonly bytesPerToken: number
91
282
  }): AgentContextCompactionBoundary | null {
92
283
  if (input.messages.length < 2) return null
93
284
 
94
285
  let retainedTokens = 0
95
286
  let boundaryIndex: number | null = null
96
- let closestDistance = Number.POSITIVE_INFINITY
97
287
  for (let index = input.messages.length - 1; index > 0; index -= 1) {
98
- retainedTokens += estimateAgentContextMessagesTokens([input.messages[index]!]).tokens
288
+ retainedTokens += estimateAgentContextMessagesTokens(
289
+ [input.messages[index]!],
290
+ input.bytesPerToken
291
+ ).tokens
99
292
  if (input.messages[index]?.role !== "user") continue
293
+ if (boundaryIndex !== null && retainedTokens > input.keepRecentTokens) break
100
294
 
101
- const distance = Math.abs(input.keepRecentTokens - retainedTokens)
102
- if (distance <= closestDistance) {
103
- boundaryIndex = index
104
- closestDistance = distance
105
- }
106
- if (retainedTokens >= input.keepRecentTokens) break
295
+ boundaryIndex = index
107
296
  }
108
297
 
109
298
  if (boundaryIndex === null) return null
@@ -117,70 +306,82 @@ export function selectAgentContextCompactionBoundary(input: {
117
306
  }
118
307
  }
119
308
 
120
- /** Serialize durable conversation content as escaped quoted data for the summary model. */
121
- export function serializeAgentMessagesForSummary(messages: readonly AgentMessageRecord[]): string {
122
- const lines = ["<sixb_conversation_history>"]
123
- for (const message of messages) {
124
- lines.push(
125
- ` <message id="${escapeXml(message.id)}" seq="${message.seq}" role="${message.role}">`
126
- )
127
- message.parts.forEach((part, partIndex) => {
128
- lines.push(...serializeSummaryPart(message, part, partIndex))
129
- })
130
- lines.push(" </message>")
131
- }
132
- lines.push("</sixb_conversation_history>")
133
- return lines.join("\n")
309
+ interface MeasuredHistory {
310
+ bytes: number
311
+ tokens: number
312
+ readonly parts: { [Part in AgentHistoryPartName]?: number }
134
313
  }
135
314
 
136
- function estimateMessageTokens(
315
+ function addBytes(size: MeasuredHistory, part: AgentHistoryPartName, bytes: number): void {
316
+ size.bytes += bytes
317
+ size.parts[part] = (size.parts[part] ?? 0) + bytes
318
+ }
319
+
320
+ function measureMessage(
137
321
  message: AgentMessage,
138
- attachments: { textBytesRemaining: number }
139
- ): number {
140
- let tokens = MESSAGE_OVERHEAD_TOKENS
322
+ attachments: { textBytesRemaining: number },
323
+ images: { remaining: number },
324
+ size: MeasuredHistory,
325
+ contextDirectory: string | undefined
326
+ ): void {
327
+ size.tokens += MESSAGE_OVERHEAD_TOKENS
141
328
  for (const part of message.parts) {
142
329
  switch (part.type) {
143
330
  case "text":
331
+ addBytes(
332
+ size,
333
+ message.role === "user" && isAgentThreadSummaryText(part.text) ? "summary" : "messages",
334
+ utf8Bytes(part.text)
335
+ )
336
+ break
144
337
  case "reasoning":
145
- tokens += textTokens(part.text)
338
+ addBytes(size, "messages", utf8Bytes(part.text))
146
339
  break
147
340
  case "step-start":
148
- tokens += STEP_OVERHEAD_TOKENS
341
+ size.tokens += STEP_OVERHEAD_TOKENS
149
342
  break
150
343
  case "continuation":
151
- tokens += textTokens(AGENT_CONTINUATION_INSTRUCTION)
344
+ addBytes(size, "messages", utf8Bytes(AGENT_CONTINUATION_INSTRUCTION))
152
345
  break
153
346
  case "approval-outcome":
154
- tokens += textTokens(approvalOutcomeInstruction(part))
347
+ addBytes(size, "messages", utf8Bytes(approvalOutcomeInstruction(part)))
155
348
  break
156
349
  case "mail":
157
- tokens += textTokens(agentMailInstruction(part))
350
+ addBytes(size, "messages", utf8Bytes(agentMailInstruction(part)))
158
351
  break
159
352
  case "context":
160
- tokens += textTokens(stableJsonStringify(part.context))
353
+ addBytes(size, "messages", utf8Bytes(stableJsonStringify(part.context)))
161
354
  break
162
355
  case "provider-state":
163
- tokens += textTokens(stableJsonStringify(part.data))
356
+ addBytes(size, "messages", utf8Bytes(stableJsonStringify(part.data)))
164
357
  break
165
358
  case "tool-call":
166
- tokens +=
167
- TOOL_OVERHEAD_TOKENS +
168
- textTokens(part.toolName) +
169
- textTokens(stableJsonStringify(part.input)) +
170
- (part.state === "output-available"
171
- ? textTokens(stableJsonStringify(part.output))
172
- : textTokens(part.errorText))
359
+ size.tokens += TOOL_OVERHEAD_TOKENS
360
+ addBytes(
361
+ size,
362
+ "messages",
363
+ utf8Bytes(part.toolName) + utf8Bytes(stableJsonStringify(part.input))
364
+ )
365
+ addBytes(
366
+ size,
367
+ agentContextFileKind(part, contextDirectory) ?? "toolResults",
368
+ toolOutputBytes(part)
369
+ )
173
370
  break
174
371
  case "file": {
175
372
  const file = part.fileRef
176
- tokens += textTokens(
177
- [file.blobId, file.digest, file.fileName, file.mediaType, file.logicalPath]
178
- .filter((value): value is string => value !== undefined)
179
- .join(" ")
373
+ addBytes(
374
+ size,
375
+ "files",
376
+ utf8Bytes(
377
+ [file.blobId, file.digest, file.fileName, file.mediaType, file.logicalPath]
378
+ .filter((value): value is string => value !== undefined)
379
+ .join(" ")
380
+ )
180
381
  )
181
382
  if (message.role !== "assistant") {
182
383
  if (looksLikeImage(file.mediaType, file.fileName)) {
183
- tokens += IMAGE_ALLOWANCE_TOKENS
384
+ size.tokens += imageTokens(images)
184
385
  } else {
185
386
  const inlineBytes = Math.min(
186
387
  file.sizeBytes,
@@ -188,96 +389,62 @@ function estimateMessageTokens(
188
389
  attachments.textBytesRemaining
189
390
  )
190
391
  attachments.textBytesRemaining -= inlineBytes
191
- tokens += Math.ceil(inlineBytes / CHARS_PER_TOKEN)
392
+ addBytes(size, "files", inlineBytes)
192
393
  }
193
394
  }
194
395
  break
195
396
  }
196
397
  }
197
398
  }
198
- return tokens
199
399
  }
200
400
 
201
- function serializeSummaryPart(
202
- message: AgentMessageRecord,
203
- part: AgentMessagePart,
204
- partIndex: number
205
- ): string[] {
206
- const indent = " "
207
- switch (part.type) {
208
- case "text":
209
- return [`${indent}<text>${escapeXml(part.text)}</text>`]
210
- case "reasoning":
211
- case "provider-state":
212
- case "step-start":
213
- return []
214
- case "continuation":
215
- return [`${indent}<continuation_request />`]
216
- case "approval-outcome":
217
- return [`${indent}${escapeXml(approvalOutcomeInstruction(part))}`]
218
- case "mail":
219
- return [`${indent}${escapeXml(agentMailInstruction(part))}`]
220
- case "context":
221
- return part.context.kind === "object"
222
- ? [
223
- `${indent}<object_context origin="${part.origin}" object_type_id="${escapeXml(part.context.ref.objectTypeId)}" primary_id="${escapeXml(part.context.ref.primaryId)}" />`,
224
- ]
225
- : [
226
- `${indent}<app_context origin="${part.origin}" id="${escapeXml(part.context.id)}" label="${escapeXml(part.context.label)}">`,
227
- `${indent} <description>${escapeXml(part.context.description)}</description>`,
228
- `${indent} <value format="json">${escapeXml(stableJsonStringify(part.context.value))}</value>`,
229
- `${indent}</app_context>`,
230
- ]
231
- case "file": {
232
- const file = part.fileRef
233
- const attributes = [
234
- `message_id="${escapeXml(message.id)}"`,
235
- `part_index="${partIndex}"`,
236
- `blob_id="${escapeXml(file.blobId)}"`,
237
- `digest="${escapeXml(file.digest)}"`,
238
- `size_bytes="${file.sizeBytes}"`,
239
- ...(file.fileName === undefined ? [] : [`file_name="${escapeXml(file.fileName)}"`]),
240
- ...(file.mediaType === undefined ? [] : [`media_type="${escapeXml(file.mediaType)}"`]),
241
- ...(file.logicalPath === undefined
242
- ? []
243
- : [`logical_path="${escapeXml(file.logicalPath)}"`]),
244
- ]
245
- return [`${indent}<file ${attributes.join(" ")} />`]
246
- }
247
- case "tool-call": {
248
- const output =
249
- part.state === "output-available"
250
- ? truncateToolResult(stableJsonStringify(part.output))
251
- : truncateToolResult(part.errorText)
252
- return [
253
- `${indent}<tool_call id="${escapeXml(part.toolCallId)}" name="${escapeXml(part.toolName)}" state="${part.state}">`,
254
- `${indent} <input format="json">${escapeXml(stableJsonStringify(part.input))}</input>`,
255
- `${indent} <result>${escapeXml(output)}</result>`,
256
- `${indent}</tool_call>`,
257
- ]
401
+ function looksLikeImage(mediaType: string | undefined, fileName: string | undefined): boolean {
402
+ if (mediaType?.toLowerCase().startsWith("image/")) return true
403
+ return /\.(?:avif|gif|jpe?g|png|webp)$/i.test(fileName ?? "")
404
+ }
405
+
406
+ /**
407
+ * The images each step's calls returned, once each, as the message after the step inlines them.
408
+ * Every call counts, whether or not its tool's images reach the model: the side that cannot
409
+ * overflow.
410
+ */
411
+ function toolImageTokens(message: AgentMessage, images: { remaining: number }): number {
412
+ let tokens = 0
413
+ for (const step of agentMessageSteps(message)) {
414
+ const seen = new Set<string>()
415
+ for (const { part } of step.localToolCalls) {
416
+ if (part.state !== "output-available" || !isAgentToolResult(part.output)) continue
417
+
418
+ for (const content of part.output.content) {
419
+ if (content.type !== "file") continue
420
+ if (!looksLikeImage(content.fileRef.mediaType, content.fileRef.fileName)) continue
421
+
422
+ const identity = `${content.fileRef.digest}:${content.fileRef.sizeBytes}`
423
+ if (seen.has(identity)) continue
424
+ seen.add(identity)
425
+ tokens += imageTokens(images)
426
+ }
258
427
  }
259
428
  }
429
+ return tokens
260
430
  }
261
431
 
262
- function truncateToolResult(value: string): string {
263
- if (value.length <= TOOL_RESULT_SUMMARY_MAX_CHARS) return value
264
- return `${value.slice(0, TOOL_RESULT_SUMMARY_MAX_CHARS)}\n[truncated ${value.length - TOOL_RESULT_SUMMARY_MAX_CHARS} characters]`
265
- }
432
+ /** One more inline image, while the request takes any. */
433
+ function imageTokens(images: { remaining: number }): number {
434
+ if (images.remaining === 0) return 0
266
435
 
267
- function looksLikeImage(mediaType: string | undefined, fileName: string | undefined): boolean {
268
- if (mediaType?.toLowerCase().startsWith("image/")) return true
269
- return /\.(?:avif|gif|jpe?g|png|webp)$/i.test(fileName ?? "")
436
+ images.remaining -= 1
437
+ return AGENT_IMAGE_INPUT_TOKENS
270
438
  }
271
439
 
272
- function textTokens(value: string): number {
273
- return Math.ceil(new TextEncoder().encode(value).byteLength / CHARS_PER_TOKEN)
440
+ /** What the model reads of a call's outcome: the output its tool projected, when it stored one. */
441
+ function toolOutputBytes(part: Extract<AgentMessagePart, { readonly type: "tool-call" }>): number {
442
+ if (part.state === "output-error") return utf8Bytes(part.errorText)
443
+ if (part.modelOutput?.type === "text") return utf8Bytes(part.modelOutput.value)
444
+ if (part.modelOutput) return utf8Bytes(stableJsonStringify(part.modelOutput.value))
445
+ return utf8Bytes(stableJsonStringify(part.output))
274
446
  }
275
447
 
276
- function escapeXml(value: string): string {
277
- return value
278
- .replaceAll("&", "&amp;")
279
- .replaceAll("<", "&lt;")
280
- .replaceAll(">", "&gt;")
281
- .replaceAll('"', "&quot;")
282
- .replaceAll("'", "&apos;")
448
+ function utf8Bytes(value: string): number {
449
+ return new TextEncoder().encode(value).byteLength
283
450
  }
@@ -0,0 +1,48 @@
1
+ import { posix } from "node:path"
2
+ import type { JsonValue } from "../json"
3
+ import type { AgentHistoryPartName } from "./message"
4
+
5
+ /** The tools that read a file whole into the context: the agent package's `read` and `view_file`. */
6
+ const CONTEXT_FILE_READERS: ReadonlySet<string> = new Set(["read", "view_file"])
7
+
8
+ /** The folders of the run's context directory a context meter names, and the variable for each. */
9
+ const CONTEXT_FOLDERS = [
10
+ { part: "memory", folder: "memory", variable: "$SIXB_MEMORY_DIR" },
11
+ { part: "skills", folder: "skills", variable: "$SIXB_SKILLS_DIR" },
12
+ { part: "ontology", folder: "ontology", variable: "$SIXB_ONTOLOGY_DIR" },
13
+ ] as const
14
+
15
+ /**
16
+ * What a tool call read when it read one of the run's context files: memory, a skill or the
17
+ * ontology, by the path its `read` or `view_file` call named. Anything else is a tool result,
18
+ * including the same file printed by a bash `cat`: a command's output is not a file read.
19
+ */
20
+ export function agentContextFileKind(
21
+ call: { readonly toolName: string; readonly input: JsonValue },
22
+ contextDirectory: string | undefined
23
+ ): Extract<AgentHistoryPartName, "memory" | "skills" | "ontology"> | undefined {
24
+ if (!CONTEXT_FILE_READERS.has(call.toolName)) return undefined
25
+
26
+ const input = call.input
27
+ const path =
28
+ typeof input === "object" && input !== null && !Array.isArray(input) ? input.path : undefined
29
+ if (typeof path !== "string") return undefined
30
+
31
+ const normalized = posix.normalize(path.trim())
32
+ const root = contextDirectory === undefined ? undefined : posix.normalize(contextDirectory)
33
+ // A relative context directory is named from the working directory, which an absolute path
34
+ // spells out in front of it.
35
+ const under = (folder: string) => {
36
+ if (root === undefined) return false
37
+
38
+ const prefix = `${posix.join(root, folder)}/`
39
+ return (
40
+ normalized.startsWith(prefix) ||
41
+ (!posix.isAbsolute(root) && normalized.includes(`/${prefix}`))
42
+ )
43
+ }
44
+ const match = CONTEXT_FOLDERS.find(
45
+ ({ folder, variable }) => normalized.startsWith(`${variable}/`) || under(folder)
46
+ )
47
+ return match?.part
48
+ }