@qjp88995/agent-runtime 0.0.0-stage → 0.1.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 (477) hide show
  1. package/dist/agent-extensions.d.ts +30 -0
  2. package/dist/agent-extensions.d.ts.map +1 -0
  3. package/dist/agent-extensions.js +4 -0
  4. package/dist/agent-extensions.js.map +1 -0
  5. package/dist/builtins/ask-outcome.d.ts +49 -0
  6. package/dist/builtins/ask-outcome.d.ts.map +1 -0
  7. package/dist/builtins/ask-outcome.js +128 -0
  8. package/dist/builtins/ask-outcome.js.map +1 -0
  9. package/dist/builtins/ask-schema.d.ts +110 -0
  10. package/dist/builtins/ask-schema.d.ts.map +1 -0
  11. package/dist/builtins/ask-schema.js +124 -0
  12. package/dist/builtins/ask-schema.js.map +1 -0
  13. package/dist/builtins/ask.service.d.ts +210 -0
  14. package/dist/builtins/ask.service.d.ts.map +1 -0
  15. package/dist/builtins/ask.service.js +373 -0
  16. package/dist/builtins/ask.service.js.map +1 -0
  17. package/dist/builtins/ask.tool.d.ts +49 -0
  18. package/dist/builtins/ask.tool.d.ts.map +1 -0
  19. package/dist/builtins/ask.tool.js +109 -0
  20. package/dist/builtins/ask.tool.js.map +1 -0
  21. package/dist/builtins/index.d.ts +8 -0
  22. package/dist/builtins/index.d.ts.map +1 -0
  23. package/dist/builtins/index.js +8 -0
  24. package/dist/builtins/index.js.map +1 -0
  25. package/dist/builtins/todo-schema.d.ts +90 -0
  26. package/dist/builtins/todo-schema.d.ts.map +1 -0
  27. package/dist/builtins/todo-schema.js +97 -0
  28. package/dist/builtins/todo-schema.js.map +1 -0
  29. package/dist/builtins/todo-settle.hook.d.ts +110 -0
  30. package/dist/builtins/todo-settle.hook.d.ts.map +1 -0
  31. package/dist/builtins/todo-settle.hook.js +174 -0
  32. package/dist/builtins/todo-settle.hook.js.map +1 -0
  33. package/dist/builtins/todo.tool.d.ts +59 -0
  34. package/dist/builtins/todo.tool.d.ts.map +1 -0
  35. package/dist/builtins/todo.tool.js +107 -0
  36. package/dist/builtins/todo.tool.js.map +1 -0
  37. package/dist/context/context-budget-tracker.d.ts +185 -0
  38. package/dist/context/context-budget-tracker.d.ts.map +1 -0
  39. package/dist/context/context-budget-tracker.js +400 -0
  40. package/dist/context/context-budget-tracker.js.map +1 -0
  41. package/dist/context/context-budget.d.ts +48 -0
  42. package/dist/context/context-budget.d.ts.map +1 -0
  43. package/dist/context/context-budget.js +89 -0
  44. package/dist/context/context-budget.js.map +1 -0
  45. package/dist/context/index.d.ts +3 -0
  46. package/dist/context/index.d.ts.map +1 -0
  47. package/dist/context/index.js +3 -0
  48. package/dist/context/index.js.map +1 -0
  49. package/dist/extensions/repeat-call.hook.d.ts +32 -0
  50. package/dist/extensions/repeat-call.hook.d.ts.map +1 -0
  51. package/dist/extensions/repeat-call.hook.js +54 -0
  52. package/dist/extensions/repeat-call.hook.js.map +1 -0
  53. package/dist/hooks/index.d.ts +3 -0
  54. package/dist/hooks/index.d.ts.map +1 -0
  55. package/dist/hooks/index.js +3 -0
  56. package/dist/hooks/index.js.map +1 -0
  57. package/dist/hooks/turn-hook.registry.d.ts +51 -0
  58. package/dist/hooks/turn-hook.registry.d.ts.map +1 -0
  59. package/dist/hooks/turn-hook.registry.js +233 -0
  60. package/dist/hooks/turn-hook.registry.js.map +1 -0
  61. package/dist/hooks/turn-hook.types.d.ts +179 -0
  62. package/dist/hooks/turn-hook.types.d.ts.map +1 -0
  63. package/dist/hooks/turn-hook.types.js +48 -0
  64. package/dist/hooks/turn-hook.types.js.map +1 -0
  65. package/dist/images/byte-cache.d.ts +24 -0
  66. package/dist/images/byte-cache.d.ts.map +1 -0
  67. package/dist/images/byte-cache.js +51 -0
  68. package/dist/images/byte-cache.js.map +1 -0
  69. package/dist/images/delivery.d.ts +115 -0
  70. package/dist/images/delivery.d.ts.map +1 -0
  71. package/dist/images/delivery.js +321 -0
  72. package/dist/images/delivery.js.map +1 -0
  73. package/dist/images/index.d.ts +5 -0
  74. package/dist/images/index.d.ts.map +1 -0
  75. package/dist/images/index.js +5 -0
  76. package/dist/images/index.js.map +1 -0
  77. package/dist/images/prompt.d.ts +151 -0
  78. package/dist/images/prompt.d.ts.map +1 -0
  79. package/dist/images/prompt.js +172 -0
  80. package/dist/images/prompt.js.map +1 -0
  81. package/dist/images/vendor-files.d.ts +93 -0
  82. package/dist/images/vendor-files.d.ts.map +1 -0
  83. package/dist/images/vendor-files.js +156 -0
  84. package/dist/images/vendor-files.js.map +1 -0
  85. package/dist/in-process/conversation-store.d.ts +64 -0
  86. package/dist/in-process/conversation-store.d.ts.map +1 -0
  87. package/dist/in-process/conversation-store.js +222 -0
  88. package/dist/in-process/conversation-store.js.map +1 -0
  89. package/dist/in-process/coordination.d.ts +54 -0
  90. package/dist/in-process/coordination.d.ts.map +1 -0
  91. package/dist/in-process/coordination.js +137 -0
  92. package/dist/in-process/coordination.js.map +1 -0
  93. package/dist/in-process/image-source.d.ts +15 -0
  94. package/dist/in-process/image-source.d.ts.map +1 -0
  95. package/dist/in-process/image-source.js +27 -0
  96. package/dist/in-process/image-source.js.map +1 -0
  97. package/dist/in-process/index.d.ts +36 -0
  98. package/dist/in-process/index.d.ts.map +1 -0
  99. package/dist/in-process/index.js +37 -0
  100. package/dist/in-process/index.js.map +1 -0
  101. package/dist/in-process/question-store.d.ts +28 -0
  102. package/dist/in-process/question-store.d.ts.map +1 -0
  103. package/dist/in-process/question-store.js +122 -0
  104. package/dist/in-process/question-store.js.map +1 -0
  105. package/dist/in-process/run-store.d.ts +36 -0
  106. package/dist/in-process/run-store.d.ts.map +1 -0
  107. package/dist/in-process/run-store.js +81 -0
  108. package/dist/in-process/run-store.js.map +1 -0
  109. package/dist/in-process/session-store.d.ts +33 -0
  110. package/dist/in-process/session-store.d.ts.map +1 -0
  111. package/dist/in-process/session-store.js +93 -0
  112. package/dist/in-process/session-store.js.map +1 -0
  113. package/dist/in-process/stream-bus.d.ts +52 -0
  114. package/dist/in-process/stream-bus.d.ts.map +1 -0
  115. package/dist/in-process/stream-bus.js +200 -0
  116. package/dist/in-process/stream-bus.js.map +1 -0
  117. package/dist/in-process/vendor-file-cache.d.ts +11 -0
  118. package/dist/in-process/vendor-file-cache.d.ts.map +1 -0
  119. package/dist/in-process/vendor-file-cache.js +47 -0
  120. package/dist/in-process/vendor-file-cache.js.map +1 -0
  121. package/dist/index.d.ts +21 -0
  122. package/dist/index.d.ts.map +1 -0
  123. package/dist/index.js +21 -0
  124. package/dist/index.js.map +1 -0
  125. package/dist/loop/active-tick.registry.d.ts +31 -0
  126. package/dist/loop/active-tick.registry.d.ts.map +1 -0
  127. package/dist/loop/active-tick.registry.js +45 -0
  128. package/dist/loop/active-tick.registry.js.map +1 -0
  129. package/dist/loop/index.d.ts +6 -0
  130. package/dist/loop/index.d.ts.map +1 -0
  131. package/dist/loop/index.js +6 -0
  132. package/dist/loop/index.js.map +1 -0
  133. package/dist/loop/loop-driver.d.ts +127 -0
  134. package/dist/loop/loop-driver.d.ts.map +1 -0
  135. package/dist/loop/loop-driver.js +199 -0
  136. package/dist/loop/loop-driver.js.map +1 -0
  137. package/dist/loop/message-hub.d.ts +236 -0
  138. package/dist/loop/message-hub.d.ts.map +1 -0
  139. package/dist/loop/message-hub.js +439 -0
  140. package/dist/loop/message-hub.js.map +1 -0
  141. package/dist/loop/orphan-run.sweeper.d.ts +79 -0
  142. package/dist/loop/orphan-run.sweeper.d.ts.map +1 -0
  143. package/dist/loop/orphan-run.sweeper.js +145 -0
  144. package/dist/loop/orphan-run.sweeper.js.map +1 -0
  145. package/dist/loop/root-host.d.ts +51 -0
  146. package/dist/loop/root-host.d.ts.map +1 -0
  147. package/dist/loop/root-host.js +65 -0
  148. package/dist/loop/root-host.js.map +1 -0
  149. package/dist/ports/context-store.d.ts +69 -0
  150. package/dist/ports/context-store.d.ts.map +1 -0
  151. package/dist/ports/context-store.js +2 -0
  152. package/dist/ports/context-store.js.map +1 -0
  153. package/dist/ports/conversation-store.d.ts +144 -0
  154. package/dist/ports/conversation-store.d.ts.map +1 -0
  155. package/dist/ports/conversation-store.js +2 -0
  156. package/dist/ports/conversation-store.js.map +1 -0
  157. package/dist/ports/coordination.d.ts +76 -0
  158. package/dist/ports/coordination.d.ts.map +1 -0
  159. package/dist/ports/coordination.js +12 -0
  160. package/dist/ports/coordination.js.map +1 -0
  161. package/dist/ports/image-source.d.ts +26 -0
  162. package/dist/ports/image-source.d.ts.map +1 -0
  163. package/dist/ports/image-source.js +2 -0
  164. package/dist/ports/image-source.js.map +1 -0
  165. package/dist/ports/index.d.ts +11 -0
  166. package/dist/ports/index.d.ts.map +1 -0
  167. package/dist/ports/index.js +4 -0
  168. package/dist/ports/index.js.map +1 -0
  169. package/dist/ports/logger.d.ts +14 -0
  170. package/dist/ports/logger.d.ts.map +1 -0
  171. package/dist/ports/logger.js +23 -0
  172. package/dist/ports/logger.js.map +1 -0
  173. package/dist/ports/question-store.d.ts +91 -0
  174. package/dist/ports/question-store.d.ts.map +1 -0
  175. package/dist/ports/question-store.js +8 -0
  176. package/dist/ports/question-store.js.map +1 -0
  177. package/dist/ports/run-store.d.ts +66 -0
  178. package/dist/ports/run-store.d.ts.map +1 -0
  179. package/dist/ports/run-store.js +2 -0
  180. package/dist/ports/run-store.js.map +1 -0
  181. package/dist/ports/session-store.d.ts +72 -0
  182. package/dist/ports/session-store.d.ts.map +1 -0
  183. package/dist/ports/session-store.js +9 -0
  184. package/dist/ports/session-store.js.map +1 -0
  185. package/dist/ports/stream-bus.d.ts +52 -0
  186. package/dist/ports/stream-bus.d.ts.map +1 -0
  187. package/dist/ports/stream-bus.js +2 -0
  188. package/dist/ports/stream-bus.js.map +1 -0
  189. package/dist/ports/vendor-file-cache.d.ts +46 -0
  190. package/dist/ports/vendor-file-cache.d.ts.map +1 -0
  191. package/dist/ports/vendor-file-cache.js +2 -0
  192. package/dist/ports/vendor-file-cache.js.map +1 -0
  193. package/dist/schema/agent-event.d.ts +35 -0
  194. package/dist/schema/agent-event.d.ts.map +1 -0
  195. package/dist/schema/agent-event.js +46 -0
  196. package/dist/schema/agent-event.js.map +1 -0
  197. package/dist/schema/agent-run.d.ts +23 -0
  198. package/dist/schema/agent-run.d.ts.map +1 -0
  199. package/dist/schema/agent-run.js +37 -0
  200. package/dist/schema/agent-run.js.map +1 -0
  201. package/dist/schema/index.d.ts +4 -0
  202. package/dist/schema/index.d.ts.map +1 -0
  203. package/dist/schema/index.js +4 -0
  204. package/dist/schema/index.js.map +1 -0
  205. package/dist/schema/message-part.d.ts +73 -0
  206. package/dist/schema/message-part.d.ts.map +1 -0
  207. package/dist/schema/message-part.js +90 -0
  208. package/dist/schema/message-part.js.map +1 -0
  209. package/dist/subagent/delegate.tool.d.ts +145 -0
  210. package/dist/subagent/delegate.tool.d.ts.map +1 -0
  211. package/dist/subagent/delegate.tool.js +329 -0
  212. package/dist/subagent/delegate.tool.js.map +1 -0
  213. package/dist/subagent/index.d.ts +10 -0
  214. package/dist/subagent/index.d.ts.map +1 -0
  215. package/dist/subagent/index.js +10 -0
  216. package/dist/subagent/index.js.map +1 -0
  217. package/dist/subagent/milestone-body.d.ts +82 -0
  218. package/dist/subagent/milestone-body.d.ts.map +1 -0
  219. package/dist/subagent/milestone-body.js +112 -0
  220. package/dist/subagent/milestone-body.js.map +1 -0
  221. package/dist/subagent/output-scan.d.ts +14 -0
  222. package/dist/subagent/output-scan.d.ts.map +1 -0
  223. package/dist/subagent/output-scan.js +41 -0
  224. package/dist/subagent/output-scan.js.map +1 -0
  225. package/dist/subagent/registry-spawn-policy.d.ts +32 -0
  226. package/dist/subagent/registry-spawn-policy.d.ts.map +1 -0
  227. package/dist/subagent/registry-spawn-policy.js +48 -0
  228. package/dist/subagent/registry-spawn-policy.js.map +1 -0
  229. package/dist/subagent/roster-block.d.ts +45 -0
  230. package/dist/subagent/roster-block.d.ts.map +1 -0
  231. package/dist/subagent/roster-block.js +69 -0
  232. package/dist/subagent/roster-block.js.map +1 -0
  233. package/dist/subagent/situation-block.d.ts +16 -0
  234. package/dist/subagent/situation-block.d.ts.map +1 -0
  235. package/dist/subagent/situation-block.js +71 -0
  236. package/dist/subagent/situation-block.js.map +1 -0
  237. package/dist/subagent/spawn-guards.d.ts +32 -0
  238. package/dist/subagent/spawn-guards.d.ts.map +1 -0
  239. package/dist/subagent/spawn-guards.js +39 -0
  240. package/dist/subagent/spawn-guards.js.map +1 -0
  241. package/dist/subagent/subagent-registry.d.ts +150 -0
  242. package/dist/subagent/subagent-registry.d.ts.map +1 -0
  243. package/dist/subagent/subagent-registry.js +167 -0
  244. package/dist/subagent/subagent-registry.js.map +1 -0
  245. package/dist/subagent/subagent-sessions.d.ts +213 -0
  246. package/dist/subagent/subagent-sessions.d.ts.map +1 -0
  247. package/dist/subagent/subagent-sessions.js +295 -0
  248. package/dist/subagent/subagent-sessions.js.map +1 -0
  249. package/dist/testing/contracts.d.ts +37 -0
  250. package/dist/testing/contracts.d.ts.map +1 -0
  251. package/dist/testing/contracts.js +607 -0
  252. package/dist/testing/contracts.js.map +1 -0
  253. package/dist/testing/index.d.ts +5 -0
  254. package/dist/testing/index.d.ts.map +1 -0
  255. package/dist/testing/index.js +5 -0
  256. package/dist/testing/index.js.map +1 -0
  257. package/dist/testing/stub.d.ts +28 -0
  258. package/dist/testing/stub.d.ts.map +1 -0
  259. package/dist/testing/stub.js +3 -0
  260. package/dist/testing/stub.js.map +1 -0
  261. package/dist/tools/agent-tool.base.d.ts +186 -0
  262. package/dist/tools/agent-tool.base.d.ts.map +1 -0
  263. package/dist/tools/agent-tool.base.js +243 -0
  264. package/dist/tools/agent-tool.base.js.map +1 -0
  265. package/dist/tools/agent-tool.interface.d.ts +95 -0
  266. package/dist/tools/agent-tool.interface.d.ts.map +1 -0
  267. package/dist/tools/agent-tool.interface.js +2 -0
  268. package/dist/tools/agent-tool.interface.js.map +1 -0
  269. package/dist/tools/builtin-names.d.ts +18 -0
  270. package/dist/tools/builtin-names.d.ts.map +1 -0
  271. package/dist/tools/builtin-names.js +20 -0
  272. package/dist/tools/builtin-names.js.map +1 -0
  273. package/dist/tools/index.d.ts +9 -0
  274. package/dist/tools/index.d.ts.map +1 -0
  275. package/dist/tools/index.js +6 -0
  276. package/dist/tools/index.js.map +1 -0
  277. package/dist/tools/tool-definition.d.ts +18 -0
  278. package/dist/tools/tool-definition.d.ts.map +1 -0
  279. package/dist/tools/tool-definition.js +77 -0
  280. package/dist/tools/tool-definition.js.map +1 -0
  281. package/dist/tools/tool-meta.d.ts +55 -0
  282. package/dist/tools/tool-meta.d.ts.map +1 -0
  283. package/dist/tools/tool-meta.js +2 -0
  284. package/dist/tools/tool-meta.js.map +1 -0
  285. package/dist/tools/tool-registry.d.ts +190 -0
  286. package/dist/tools/tool-registry.d.ts.map +1 -0
  287. package/dist/tools/tool-registry.js +379 -0
  288. package/dist/tools/tool-registry.js.map +1 -0
  289. package/dist/tools/tool-result.d.ts +41 -0
  290. package/dist/tools/tool-result.d.ts.map +1 -0
  291. package/dist/tools/tool-result.js +19 -0
  292. package/dist/tools/tool-result.js.map +1 -0
  293. package/dist/tools/tool-wire.d.ts +24 -0
  294. package/dist/tools/tool-wire.d.ts.map +1 -0
  295. package/dist/tools/tool-wire.js +2 -0
  296. package/dist/tools/tool-wire.js.map +1 -0
  297. package/dist/turn/agent-runtime.d.ts +31 -0
  298. package/dist/turn/agent-runtime.d.ts.map +1 -0
  299. package/dist/turn/agent-runtime.js +30 -0
  300. package/dist/turn/agent-runtime.js.map +1 -0
  301. package/dist/turn/chunk-projection.d.ts +32 -0
  302. package/dist/turn/chunk-projection.d.ts.map +1 -0
  303. package/dist/turn/chunk-projection.js +62 -0
  304. package/dist/turn/chunk-projection.js.map +1 -0
  305. package/dist/turn/compaction.d.ts +48 -0
  306. package/dist/turn/compaction.d.ts.map +1 -0
  307. package/dist/turn/compaction.js +89 -0
  308. package/dist/turn/compaction.js.map +1 -0
  309. package/dist/turn/context-overflow.d.ts +11 -0
  310. package/dist/turn/context-overflow.d.ts.map +1 -0
  311. package/dist/turn/context-overflow.js +53 -0
  312. package/dist/turn/context-overflow.js.map +1 -0
  313. package/dist/turn/context-pressure.d.ts +100 -0
  314. package/dist/turn/context-pressure.d.ts.map +1 -0
  315. package/dist/turn/context-pressure.js +21 -0
  316. package/dist/turn/context-pressure.js.map +1 -0
  317. package/dist/turn/events-to-steps.d.ts +8 -0
  318. package/dist/turn/events-to-steps.d.ts.map +1 -0
  319. package/dist/turn/events-to-steps.js +64 -0
  320. package/dist/turn/events-to-steps.js.map +1 -0
  321. package/dist/turn/index.d.ts +17 -0
  322. package/dist/turn/index.d.ts.map +1 -0
  323. package/dist/turn/index.js +16 -0
  324. package/dist/turn/index.js.map +1 -0
  325. package/dist/turn/json-preview.d.ts +22 -0
  326. package/dist/turn/json-preview.d.ts.map +1 -0
  327. package/dist/turn/json-preview.js +34 -0
  328. package/dist/turn/json-preview.js.map +1 -0
  329. package/dist/turn/json-string-field.d.ts +30 -0
  330. package/dist/turn/json-string-field.d.ts.map +1 -0
  331. package/dist/turn/json-string-field.js +125 -0
  332. package/dist/turn/json-string-field.js.map +1 -0
  333. package/dist/turn/loop-reminder.d.ts +79 -0
  334. package/dist/turn/loop-reminder.d.ts.map +1 -0
  335. package/dist/turn/loop-reminder.js +0 -0
  336. package/dist/turn/loop-reminder.js.map +1 -0
  337. package/dist/turn/nul-safe.d.ts +21 -0
  338. package/dist/turn/nul-safe.d.ts.map +1 -0
  339. package/dist/turn/nul-safe.js +23 -0
  340. package/dist/turn/nul-safe.js.map +1 -0
  341. package/dist/turn/parts-from-events.d.ts +71 -0
  342. package/dist/turn/parts-from-events.d.ts.map +1 -0
  343. package/dist/turn/parts-from-events.js +217 -0
  344. package/dist/turn/parts-from-events.js.map +1 -0
  345. package/dist/turn/parts-to-model-messages.d.ts +158 -0
  346. package/dist/turn/parts-to-model-messages.d.ts.map +1 -0
  347. package/dist/turn/parts-to-model-messages.js +334 -0
  348. package/dist/turn/parts-to-model-messages.js.map +1 -0
  349. package/dist/turn/pending-work.d.ts +10 -0
  350. package/dist/turn/pending-work.d.ts.map +1 -0
  351. package/dist/turn/pending-work.js +14 -0
  352. package/dist/turn/pending-work.js.map +1 -0
  353. package/dist/turn/provider-error.d.ts +45 -0
  354. package/dist/turn/provider-error.d.ts.map +1 -0
  355. package/dist/turn/provider-error.js +154 -0
  356. package/dist/turn/provider-error.js.map +1 -0
  357. package/dist/turn/tool-input-preview.d.ts +94 -0
  358. package/dist/turn/tool-input-preview.d.ts.map +1 -0
  359. package/dist/turn/tool-input-preview.js +143 -0
  360. package/dist/turn/tool-input-preview.js.map +1 -0
  361. package/dist/turn/tool-input-progress.d.ts +47 -0
  362. package/dist/turn/tool-input-progress.d.ts.map +1 -0
  363. package/dist/turn/tool-input-progress.js +50 -0
  364. package/dist/turn/tool-input-progress.js.map +1 -0
  365. package/dist/turn/turn-envelope.d.ts +184 -0
  366. package/dist/turn/turn-envelope.d.ts.map +1 -0
  367. package/dist/turn/turn-envelope.js +2 -0
  368. package/dist/turn/turn-envelope.js.map +1 -0
  369. package/dist/turn/turn-journal.d.ts +49 -0
  370. package/dist/turn/turn-journal.d.ts.map +1 -0
  371. package/dist/turn/turn-journal.js +51 -0
  372. package/dist/turn/turn-journal.js.map +1 -0
  373. package/dist/turn/turn-run.d.ts +487 -0
  374. package/dist/turn/turn-run.d.ts.map +1 -0
  375. package/dist/turn/turn-run.js +1167 -0
  376. package/dist/turn/turn-run.js.map +1 -0
  377. package/dist/turn/usage.d.ts +27 -0
  378. package/dist/turn/usage.d.ts.map +1 -0
  379. package/dist/turn/usage.js +51 -0
  380. package/dist/turn/usage.js.map +1 -0
  381. package/package.json +55 -3
  382. package/src/agent-extensions.ts +33 -0
  383. package/src/builtins/ask-outcome.ts +168 -0
  384. package/src/builtins/ask-schema.ts +186 -0
  385. package/src/builtins/ask.service.ts +482 -0
  386. package/src/builtins/ask.tool.ts +138 -0
  387. package/src/builtins/index.ts +25 -0
  388. package/src/builtins/todo-schema.ts +108 -0
  389. package/src/builtins/todo-settle.hook.ts +221 -0
  390. package/src/builtins/todo.tool.ts +134 -0
  391. package/src/context/context-budget-tracker.ts +519 -0
  392. package/src/context/context-budget.ts +124 -0
  393. package/src/context/index.ts +17 -0
  394. package/src/extensions/repeat-call.hook.ts +77 -0
  395. package/src/hooks/index.ts +19 -0
  396. package/src/hooks/turn-hook.registry.ts +307 -0
  397. package/src/hooks/turn-hook.types.ts +210 -0
  398. package/src/images/byte-cache.ts +61 -0
  399. package/src/images/delivery.ts +508 -0
  400. package/src/images/index.ts +35 -0
  401. package/src/images/prompt.ts +289 -0
  402. package/src/images/vendor-files.ts +235 -0
  403. package/src/in-process/conversation-store.ts +304 -0
  404. package/src/in-process/coordination.ts +177 -0
  405. package/src/in-process/image-source.ts +29 -0
  406. package/src/in-process/index.ts +52 -0
  407. package/src/in-process/question-store.ts +155 -0
  408. package/src/in-process/run-store.ts +115 -0
  409. package/src/in-process/session-store.ts +118 -0
  410. package/src/in-process/stream-bus.ts +244 -0
  411. package/src/in-process/vendor-file-cache.ts +67 -0
  412. package/src/index.ts +28 -0
  413. package/src/loop/active-tick.registry.ts +49 -0
  414. package/src/loop/index.ts +25 -0
  415. package/src/loop/loop-driver.ts +275 -0
  416. package/src/loop/message-hub.ts +532 -0
  417. package/src/loop/orphan-run.sweeper.ts +204 -0
  418. package/src/loop/root-host.ts +119 -0
  419. package/src/ports/context-store.ts +71 -0
  420. package/src/ports/conversation-store.ts +165 -0
  421. package/src/ports/coordination.ts +81 -0
  422. package/src/ports/image-source.ts +26 -0
  423. package/src/ports/index.ts +56 -0
  424. package/src/ports/logger.ts +34 -0
  425. package/src/ports/question-store.ts +117 -0
  426. package/src/ports/run-store.ts +69 -0
  427. package/src/ports/session-store.ts +80 -0
  428. package/src/ports/stream-bus.ts +59 -0
  429. package/src/ports/vendor-file-cache.ts +54 -0
  430. package/src/schema/agent-event.ts +69 -0
  431. package/src/schema/agent-run.ts +44 -0
  432. package/src/schema/index.ts +25 -0
  433. package/src/schema/message-part.ts +100 -0
  434. package/src/subagent/delegate.tool.ts +476 -0
  435. package/src/subagent/index.ts +63 -0
  436. package/src/subagent/milestone-body.ts +144 -0
  437. package/src/subagent/output-scan.ts +56 -0
  438. package/src/subagent/registry-spawn-policy.ts +85 -0
  439. package/src/subagent/roster-block.ts +103 -0
  440. package/src/subagent/situation-block.ts +83 -0
  441. package/src/subagent/spawn-guards.ts +54 -0
  442. package/src/subagent/subagent-registry.ts +283 -0
  443. package/src/subagent/subagent-sessions.ts +496 -0
  444. package/src/testing/contracts.ts +882 -0
  445. package/src/testing/index.ts +14 -0
  446. package/src/testing/stub.ts +28 -0
  447. package/src/tools/agent-tool.base.ts +361 -0
  448. package/src/tools/agent-tool.interface.ts +101 -0
  449. package/src/tools/builtin-names.ts +26 -0
  450. package/src/tools/index.ts +39 -0
  451. package/src/tools/tool-definition.ts +93 -0
  452. package/src/tools/tool-meta.ts +60 -0
  453. package/src/tools/tool-registry.ts +513 -0
  454. package/src/tools/tool-result.ts +45 -0
  455. package/src/tools/tool-wire.ts +24 -0
  456. package/src/turn/agent-runtime.ts +43 -0
  457. package/src/turn/chunk-projection.ts +81 -0
  458. package/src/turn/compaction.ts +103 -0
  459. package/src/turn/context-overflow.ts +54 -0
  460. package/src/turn/context-pressure.ts +114 -0
  461. package/src/turn/events-to-steps.ts +73 -0
  462. package/src/turn/index.ts +71 -0
  463. package/src/turn/json-preview.ts +36 -0
  464. package/src/turn/json-string-field.ts +134 -0
  465. package/src/turn/loop-reminder.ts +0 -0
  466. package/src/turn/nul-safe.ts +22 -0
  467. package/src/turn/parts-from-events.ts +248 -0
  468. package/src/turn/parts-to-model-messages.ts +428 -0
  469. package/src/turn/pending-work.ts +14 -0
  470. package/src/turn/provider-error.ts +160 -0
  471. package/src/turn/tool-input-preview.ts +223 -0
  472. package/src/turn/tool-input-progress.ts +76 -0
  473. package/src/turn/turn-envelope.ts +185 -0
  474. package/src/turn/turn-journal.ts +102 -0
  475. package/src/turn/turn-run.ts +1479 -0
  476. package/src/turn/usage.ts +56 -0
  477. package/README.md +0 -3
@@ -0,0 +1,155 @@
1
+ import { randomUUID } from 'node:crypto';
2
+
3
+ import type { AgentQuestionAnswer } from '../builtins/ask-schema.js';
4
+ import {
5
+ type NewQuestion,
6
+ QuestionConflictError,
7
+ type QuestionProbe,
8
+ type QuestionRecord,
9
+ type QuestionStore,
10
+ type SettledQuestionStatus,
11
+ } from '../ports/question-store.js';
12
+
13
+ /**
14
+ * 内存问题行。时钟可注入(单测拨表判到期);读—改—写之间没有 await,单线程里
15
+ * 天然原子(`open` 的「作废残骸 + 插入」与各个 pending 谓词都靠这一点)。
16
+ */
17
+ export class InMemoryQuestionStore implements QuestionStore {
18
+ private readonly rows = new Map<string, QuestionRecord>();
19
+
20
+ constructor(private readonly clock: () => number = Date.now) {}
21
+
22
+ /** 删掉一个会话的全部问题(单测模拟「会话被删」,数据库里是级联删除)。 */
23
+ deleteSession(sessionId: string): void {
24
+ for (const [id, row] of this.rows) {
25
+ if (row.sessionId === sessionId) this.rows.delete(id);
26
+ }
27
+ }
28
+
29
+ open(
30
+ input: NewQuestion
31
+ ): Promise<{ question: QuestionRecord; swept: QuestionRecord[] }> {
32
+ const pending = [...this.rows.values()].filter(
33
+ r => r.sessionId === input.sessionId && r.status === 'pending'
34
+ );
35
+ if (pending.some(r => r.runId === input.runId)) {
36
+ return Promise.reject(new QuestionConflictError(input.sessionId));
37
+ }
38
+ const now = this.clock();
39
+ const swept = pending.map(r => {
40
+ const next = this.write(r.id, {
41
+ status: 'cancelled',
42
+ resolvedAt: new Date(now),
43
+ });
44
+ return next;
45
+ });
46
+ const row: QuestionRecord = {
47
+ id: randomUUID(),
48
+ sessionId: input.sessionId,
49
+ userId: input.userId,
50
+ runId: input.runId,
51
+ toolCallId: input.toolCallId,
52
+ questions: structuredClone(input.questions),
53
+ status: 'pending',
54
+ deadlineAt:
55
+ input.timeoutSeconds === undefined
56
+ ? null
57
+ : new Date(now + input.timeoutSeconds * 1000),
58
+ answers: null,
59
+ createdAt: new Date(now),
60
+ resolvedAt: null,
61
+ };
62
+ this.rows.set(row.id, row);
63
+ return Promise.resolve({ question: copy(row), swept });
64
+ }
65
+
66
+ probe(id: string, maxWaitSeconds: number): Promise<QuestionProbe | null> {
67
+ const row = this.rows.get(id);
68
+ if (!row) return Promise.resolve(null);
69
+ const now = this.clock();
70
+ return Promise.resolve({
71
+ question: copy(row),
72
+ due: row.deadlineAt !== null && row.deadlineAt.getTime() <= now,
73
+ overdue: row.createdAt.getTime() + maxWaitSeconds * 1000 <= now,
74
+ });
75
+ }
76
+
77
+ get(id: string, sessionId?: string): Promise<QuestionRecord | null> {
78
+ const row = this.rows.get(id);
79
+ if (!row || (sessionId !== undefined && row.sessionId !== sessionId)) {
80
+ return Promise.resolve(null);
81
+ }
82
+ return Promise.resolve(copy(row));
83
+ }
84
+
85
+ settle(
86
+ id: string,
87
+ status: SettledQuestionStatus,
88
+ answers: AgentQuestionAnswer[] | null
89
+ ): Promise<QuestionRecord | null> {
90
+ const row = this.rows.get(id);
91
+ if (!row || row.status !== 'pending') return Promise.resolve(null);
92
+ return Promise.resolve(
93
+ this.write(id, {
94
+ status,
95
+ answers: answers ? structuredClone(answers) : null,
96
+ resolvedAt: new Date(this.clock()),
97
+ })
98
+ );
99
+ }
100
+
101
+ settleSession(
102
+ sessionId: string,
103
+ status: 'superseded' | 'cancelled',
104
+ onlyId?: string
105
+ ): Promise<QuestionRecord[]> {
106
+ const resolvedAt = new Date(this.clock());
107
+ const out: QuestionRecord[] = [];
108
+ for (const row of this.sorted()) {
109
+ if (row.sessionId !== sessionId || row.status !== 'pending') continue;
110
+ if (onlyId !== undefined && row.id !== onlyId) continue;
111
+ out.push(this.write(row.id, { status, resolvedAt }));
112
+ }
113
+ return Promise.resolve(out);
114
+ }
115
+
116
+ hold(id: string, sessionId: string): Promise<QuestionRecord | null> {
117
+ const row = this.rows.get(id);
118
+ if (
119
+ !row ||
120
+ row.sessionId !== sessionId ||
121
+ row.status !== 'pending' ||
122
+ row.deadlineAt === null
123
+ ) {
124
+ return Promise.resolve(null);
125
+ }
126
+ return Promise.resolve(this.write(id, { deadlineAt: null }));
127
+ }
128
+
129
+ list(sessionId: string): Promise<QuestionRecord[]> {
130
+ return Promise.resolve(
131
+ this.sorted()
132
+ .filter(r => r.sessionId === sessionId)
133
+ .map(copy)
134
+ );
135
+ }
136
+
137
+ now(): Promise<Date> {
138
+ return Promise.resolve(new Date(this.clock()));
139
+ }
140
+
141
+ /** 落行顺序(Map 保插入序;同一毫秒落的两行也分得清先后)。 */
142
+ private sorted(): QuestionRecord[] {
143
+ return [...this.rows.values()];
144
+ }
145
+
146
+ private write(id: string, patch: Partial<QuestionRecord>): QuestionRecord {
147
+ const row = { ...this.rows.get(id)!, ...patch };
148
+ this.rows.set(id, row);
149
+ return copy(row);
150
+ }
151
+ }
152
+
153
+ function copy(row: QuestionRecord): QuestionRecord {
154
+ return structuredClone(row);
155
+ }
@@ -0,0 +1,115 @@
1
+ import type {
2
+ RunFinishInput,
3
+ RunOpenInput,
4
+ RunStore,
5
+ StaleRun,
6
+ } from '../ports/run-store.js';
7
+ import type {
8
+ AgentRunStatus,
9
+ AgentRunStepEntry,
10
+ StopReason,
11
+ } from '../schema/agent-run.js';
12
+
13
+ /** 内存里的一条跑次记录(也是 `InMemoryRunStore.get` 的返回形状,便于单测断言)。 */
14
+ export interface StoredRun extends RunOpenInput {
15
+ status: AgentRunStatus;
16
+ stopReason: StopReason | null;
17
+ text: string;
18
+ steps: AgentRunStepEntry[];
19
+ usage?: Record<string, number>;
20
+ error?: string;
21
+ startedAt: number;
22
+ finishedAt: number | null;
23
+ }
24
+
25
+ /**
26
+ * 内存跑次记录。进程重启即丢;`rootSessionId` 由构造时给的查询函数补
27
+ * (跑次本身只记 sessionId,根会话归会话存储管)。
28
+ */
29
+ export class InMemoryRunStore implements RunStore {
30
+ private readonly runs = new Map<string, StoredRun>();
31
+ private readonly now: () => number;
32
+ private readonly rootOf: (sessionId: string) => string | null;
33
+
34
+ constructor(
35
+ options: {
36
+ now?: () => number;
37
+ /** sessionId → 根会话 id。缺省把 sessionId 自己当根。 */
38
+ rootSessionOf?: (sessionId: string) => string | null;
39
+ } = {}
40
+ ) {
41
+ this.now = options.now ?? Date.now;
42
+ this.rootOf = options.rootSessionOf ?? (id => id);
43
+ }
44
+
45
+ async open(input: RunOpenInput): Promise<void> {
46
+ if (this.runs.has(input.id)) {
47
+ throw new Error(`run '${input.id}' already exists`);
48
+ }
49
+ this.runs.set(input.id, {
50
+ ...input,
51
+ status: 'running',
52
+ stopReason: null,
53
+ text: '',
54
+ steps: [],
55
+ startedAt: this.now(),
56
+ finishedAt: null,
57
+ });
58
+ }
59
+
60
+ async finish(input: RunFinishInput): Promise<void> {
61
+ const run = this.runs.get(input.id);
62
+ if (!run) return;
63
+ Object.assign(run, {
64
+ status: input.status,
65
+ stopReason: input.stopReason,
66
+ text: input.text,
67
+ steps: input.steps,
68
+ usage: input.usage,
69
+ error: input.error,
70
+ finishedAt: this.now(),
71
+ });
72
+ }
73
+
74
+ async findStaleRunning(olderThanMs: number): Promise<StaleRun[]> {
75
+ const cutoff = this.now() - olderThanMs;
76
+ const out: StaleRun[] = [];
77
+ for (const run of this.runs.values()) {
78
+ if (run.status !== 'running' || run.startedAt >= cutoff) continue;
79
+ out.push({
80
+ id: run.id,
81
+ streamId: run.streamId,
82
+ sessionId: run.sessionId,
83
+ rootSessionId: run.sessionId ? this.rootOf(run.sessionId) : null,
84
+ parentRunId: run.parentRunId,
85
+ depth: run.depth,
86
+ });
87
+ }
88
+ return out;
89
+ }
90
+
91
+ async markInterrupted(id: string, error: string): Promise<boolean> {
92
+ const run = this.runs.get(id);
93
+ if (!run || run.status !== 'running') return false;
94
+ Object.assign(run, {
95
+ status: 'aborted',
96
+ stopReason: 'aborted',
97
+ error,
98
+ finishedAt: this.now(),
99
+ });
100
+ return true;
101
+ }
102
+
103
+ async getSessionId(runId: string): Promise<string | null> {
104
+ return this.runs.get(runId)?.sessionId ?? null;
105
+ }
106
+
107
+ async getDepth(runId: string): Promise<number> {
108
+ return this.runs.get(runId)?.depth ?? 0;
109
+ }
110
+
111
+ /** 读一条记录(单测 / 调试用,不在端口上)。 */
112
+ get(runId: string): StoredRun | undefined {
113
+ return this.runs.get(runId);
114
+ }
115
+ }
@@ -0,0 +1,118 @@
1
+ import { randomUUID } from 'node:crypto';
2
+
3
+ import type {
4
+ NewSubSession,
5
+ SessionRecord,
6
+ SessionStore,
7
+ } from '../ports/session-store.js';
8
+ import type { InMemoryConversationStore } from './conversation-store.js';
9
+
10
+ /**
11
+ * 内存会话行。兜底扫描要看消息与「更新时间」,所以挂在同一个
12
+ * `InMemoryConversationStore` 上(数据库实现里那是同一个库的两张表)。
13
+ *
14
+ * 读—改—写之间没有 await,单线程里天然原子(`advanceMilestone` 的 CAS 靠这一点)。
15
+ */
16
+ export class InMemorySessionStore implements SessionStore {
17
+ private readonly rows = new Map<string, SessionRecord>();
18
+
19
+ constructor(
20
+ private readonly conversations: InMemoryConversationStore,
21
+ private readonly now: () => number = Date.now
22
+ ) {}
23
+
24
+ /** 建一个根会话(宿主的事;进程内实现给单测与参考宿主用)。 */
25
+ createRoot(
26
+ input: {
27
+ id?: string;
28
+ userId?: string | null;
29
+ title?: string | null;
30
+ metadata?: Readonly<Record<string, unknown>>;
31
+ attrs?: Readonly<Record<string, unknown>>;
32
+ } = {}
33
+ ): SessionRecord {
34
+ const id = input.id ?? randomUUID();
35
+ const row: SessionRecord = {
36
+ id,
37
+ kind: 'root',
38
+ rootSessionId: id,
39
+ parentRunId: null,
40
+ subagentName: null,
41
+ title: input.title ?? null,
42
+ userId: input.userId ?? null,
43
+ lastMilestoneSeq: null,
44
+ metadata: input.metadata ?? {},
45
+ attrs: input.attrs ?? {},
46
+ };
47
+ this.rows.set(id, row);
48
+ return row;
49
+ }
50
+
51
+ /** 删掉一行(单测模拟「会话被删」)。 */
52
+ delete(id: string): void {
53
+ this.rows.delete(id);
54
+ }
55
+
56
+ createSubSession(input: NewSubSession): Promise<SessionRecord> {
57
+ const row: SessionRecord = {
58
+ id: randomUUID(),
59
+ kind: 'subagent',
60
+ rootSessionId: input.rootSessionId,
61
+ parentRunId: input.parentRunId,
62
+ subagentName: input.subagentName,
63
+ title: input.title,
64
+ userId: input.userId,
65
+ lastMilestoneSeq: null,
66
+ metadata: input.metadata ?? {},
67
+ attrs: input.attrs ?? {},
68
+ };
69
+ this.rows.set(row.id, row);
70
+ return Promise.resolve({ ...row });
71
+ }
72
+
73
+ getSession(id: string): Promise<SessionRecord | null> {
74
+ const row = this.rows.get(id);
75
+ return Promise.resolve(row ? { ...row } : null);
76
+ }
77
+
78
+ advanceMilestone(
79
+ id: string,
80
+ expected: number | null,
81
+ next: number
82
+ ): Promise<boolean> {
83
+ const row = this.rows.get(id);
84
+ if (!row || row.lastMilestoneSeq !== expected) {
85
+ return Promise.resolve(false);
86
+ }
87
+ row.lastMilestoneSeq = next;
88
+ return Promise.resolve(true);
89
+ }
90
+
91
+ async listSubagentSessionsForSweep(
92
+ idleForMs: number
93
+ ): Promise<SessionRecord[]> {
94
+ const cutoff = this.now() - idleForMs;
95
+ const out: SessionRecord[] = [];
96
+ for (const row of this.rows.values()) {
97
+ if (row.kind !== 'subagent') continue;
98
+ if (this.conversations.touchedAt(row.id) >= cutoff) continue;
99
+ const pending = await this.conversations.listMessages(row.id, {
100
+ role: 'assistant',
101
+ afterSeq: row.lastMilestoneSeq ?? 0,
102
+ });
103
+ if (pending.length > 0) out.push({ ...row });
104
+ }
105
+ return out;
106
+ }
107
+
108
+ countAliveSubsByRoot(): Promise<{ rootSessionId: string; count: number }[]> {
109
+ const counts = new Map<string, number>();
110
+ for (const row of this.rows.values()) {
111
+ if (row.kind !== 'subagent' || row.lastMilestoneSeq !== null) continue;
112
+ counts.set(row.rootSessionId, (counts.get(row.rootSessionId) ?? 0) + 1);
113
+ }
114
+ return Promise.resolve(
115
+ [...counts].map(([rootSessionId, count]) => ({ rootSessionId, count }))
116
+ );
117
+ }
118
+ }
@@ -0,0 +1,244 @@
1
+ import { EventEmitter } from 'node:events';
2
+
3
+ import type { SessionStreamEntry, StreamBus } from '../ports/stream-bus.js';
4
+ import { type AgentEvent, createSeqCounter } from '../schema/agent-event.js';
5
+
6
+ interface StreamState {
7
+ buffer: AgentEvent[];
8
+ emitter: EventEmitter;
9
+ controller: AbortController;
10
+ active: boolean;
11
+ closedAt?: number;
12
+ }
13
+
14
+ interface SessionState {
15
+ buffer: SessionStreamEntry[];
16
+ emitter: EventEmitter;
17
+ seq: number;
18
+ /** 最后一次有事件进来的时刻,过期清理用。 */
19
+ touchedAt: number;
20
+ }
21
+
22
+ export interface InProcessStreamBusOptions {
23
+ /** close 后 buffer 保留多久(供重连补发)。缺省 5 分钟。 */
24
+ bufferTtlMs?: number;
25
+ /** 会话面多久没有新事件就整块清掉。缺省 1 小时。 */
26
+ sessionTtlMs?: number;
27
+ /** 清理间隔。缺省 1 分钟;0 = 不起定时器(单测手动调 `sweep`)。 */
28
+ sweepIntervalMs?: number;
29
+ now?: () => number;
30
+ }
31
+
32
+ /**
33
+ * 进程内事件总线。搬自 agent-base `InProcessStreamBus`,改动:
34
+ * 去掉 Nest;会话面也按 TTL 清理(agent-base 生产走 Redis,进程内的会话面只在单测里用,
35
+ * 从没清过——在这里它是默认实现,不清就无界增长);加 `dispose()` 停定时器。
36
+ *
37
+ * **只在单进程内成立。**
38
+ */
39
+ export class InProcessStreamBus implements StreamBus {
40
+ private readonly streams = new Map<string, StreamState>();
41
+ private readonly sessions = new Map<string, SessionState>();
42
+ private readonly bufferTtlMs: number;
43
+ private readonly sessionTtlMs: number;
44
+ private readonly now: () => number;
45
+ private readonly timer: NodeJS.Timeout | null;
46
+
47
+ constructor(options: InProcessStreamBusOptions = {}) {
48
+ this.bufferTtlMs = options.bufferTtlMs ?? 5 * 60_000;
49
+ this.sessionTtlMs = options.sessionTtlMs ?? 60 * 60_000;
50
+ this.now = options.now ?? Date.now;
51
+ const interval = options.sweepIntervalMs ?? 60_000;
52
+ this.timer =
53
+ interval > 0 ? setInterval(() => this.sweep(), interval) : null;
54
+ this.timer?.unref();
55
+ }
56
+
57
+ open(streamId: string): { nextSeq: () => number } {
58
+ if (this.streams.has(streamId)) {
59
+ throw new Error(`stream '${streamId}' already open`);
60
+ }
61
+ const emitter = new EventEmitter();
62
+ emitter.setMaxListeners(0);
63
+ this.streams.set(streamId, {
64
+ buffer: [],
65
+ emitter,
66
+ controller: new AbortController(),
67
+ active: true,
68
+ });
69
+ return { nextSeq: createSeqCounter() };
70
+ }
71
+
72
+ publish(event: AgentEvent): void {
73
+ if (this.publishToStream(event)) this.publishToSession(event);
74
+ }
75
+
76
+ publishSessionEvent(event: AgentEvent): void {
77
+ this.publishToSession(event);
78
+ }
79
+
80
+ /**
81
+ * 只喂 per-stream 面。流已 close 或不存在时不接收(不入 buffer、不 emit)。返回是否投递了。
82
+ * 给子类用:Redis 适配保留进程内的 per-stream 面(pump 与取消信号都在跑这一轮的进程里),
83
+ * 只把会话面换成跨实例的实现。
84
+ */
85
+ protected publishToStream(event: AgentEvent): boolean {
86
+ const state = this.streams.get(event.streamId);
87
+ if (!state || !state.active) return false;
88
+ state.buffer.push(event);
89
+ state.emitter.emit('event', event);
90
+ return true;
91
+ }
92
+
93
+ private publishToSession(event: AgentEvent): void {
94
+ let s = this.sessions.get(event.rootSessionId);
95
+ if (!s) {
96
+ const emitter = new EventEmitter();
97
+ emitter.setMaxListeners(0);
98
+ s = { buffer: [], emitter, seq: 0, touchedAt: this.now() };
99
+ this.sessions.set(event.rootSessionId, s);
100
+ }
101
+ s.touchedAt = this.now();
102
+ const entry: SessionStreamEntry = { id: String(++s.seq), event };
103
+ s.buffer.push(entry);
104
+ s.emitter.emit('entry', entry);
105
+ }
106
+
107
+ subscribe(
108
+ streamId: string,
109
+ fromSeq = 1,
110
+ signal?: AbortSignal
111
+ ): AsyncIterable<AgentEvent> {
112
+ const state = this.streams.get(streamId);
113
+ if (!state) return (async function* () {})();
114
+ const queue: AgentEvent[] = state.buffer.filter(e => e.seq >= fromSeq);
115
+ let notify: (() => void) | null = null;
116
+ let ended = !state.active;
117
+ let aborted = signal?.aborted ?? false;
118
+
119
+ const onEvent = (e: AgentEvent) => {
120
+ queue.push(e);
121
+ notify?.();
122
+ };
123
+ const onClose = () => {
124
+ ended = true;
125
+ notify?.();
126
+ };
127
+ const onAbort = () => {
128
+ aborted = true;
129
+ notify?.();
130
+ };
131
+ state.emitter.on('event', onEvent);
132
+ state.emitter.on('close', onClose);
133
+ signal?.addEventListener('abort', onAbort);
134
+
135
+ return {
136
+ async *[Symbol.asyncIterator]() {
137
+ try {
138
+ for (;;) {
139
+ // abort 只阻止「等待」,不阻止排空已入队的事件。
140
+ while (queue.length > 0) yield queue.shift() as AgentEvent;
141
+ if (ended || aborted || signal?.aborted) return;
142
+ await new Promise<void>(resolve => {
143
+ notify = resolve;
144
+ });
145
+ notify = null;
146
+ }
147
+ } finally {
148
+ state.emitter.off('event', onEvent);
149
+ state.emitter.off('close', onClose);
150
+ signal?.removeEventListener('abort', onAbort);
151
+ }
152
+ },
153
+ };
154
+ }
155
+
156
+ async *subscribeSession(
157
+ rootSessionId: string,
158
+ fromId: string | undefined,
159
+ signal: AbortSignal
160
+ ): AsyncIterable<SessionStreamEntry> {
161
+ // 还没有任何事件时也要能订阅:先建出会话面,之后的事件才收得到。
162
+ let s = this.sessions.get(rootSessionId);
163
+ if (!s) {
164
+ const emitter = new EventEmitter();
165
+ emitter.setMaxListeners(0);
166
+ s = { buffer: [], emitter, seq: 0, touchedAt: this.now() };
167
+ this.sessions.set(rootSessionId, s);
168
+ }
169
+ const session = s;
170
+ const from = fromId ? Number(fromId) : 0;
171
+ const queue = session.buffer.filter(e => Number(e.id) > from);
172
+ let notify: (() => void) | null = null;
173
+ let aborted = signal.aborted;
174
+ const onEntry = (e: SessionStreamEntry) => {
175
+ queue.push(e);
176
+ notify?.();
177
+ };
178
+ const onAbort = () => {
179
+ aborted = true;
180
+ notify?.();
181
+ };
182
+ session.emitter.on('entry', onEntry);
183
+ signal.addEventListener('abort', onAbort);
184
+ try {
185
+ for (;;) {
186
+ while (queue.length > 0) yield queue.shift() as SessionStreamEntry;
187
+ if (aborted || signal.aborted) return;
188
+ await new Promise<void>(resolve => {
189
+ notify = resolve;
190
+ });
191
+ notify = null;
192
+ }
193
+ } finally {
194
+ session.emitter.off('entry', onEntry);
195
+ signal.removeEventListener('abort', onAbort);
196
+ }
197
+ }
198
+
199
+ /** 进程内没有「重放量」这回事:buffer 随 TTL 整体过期,退役是 no-op。 */
200
+ retireUpTo(_runId: string, _uptoSeq: number): void {}
201
+
202
+ close(streamId: string): void {
203
+ const state = this.streams.get(streamId);
204
+ if (!state || !state.active) return;
205
+ state.active = false;
206
+ state.closedAt = this.now();
207
+ state.emitter.emit('close');
208
+ }
209
+
210
+ abort(streamId: string): void {
211
+ this.streams.get(streamId)?.controller.abort();
212
+ }
213
+
214
+ isActive(streamId: string): boolean {
215
+ return this.streams.get(streamId)?.active ?? false;
216
+ }
217
+
218
+ signalOf(streamId: string): AbortSignal | undefined {
219
+ return this.streams.get(streamId)?.controller.signal;
220
+ }
221
+
222
+ /** 清掉过期的流与没人订阅的过期会话面。定时器会调它;单测可以手动调。 */
223
+ sweep(): void {
224
+ const now = this.now();
225
+ for (const [id, state] of this.streams) {
226
+ if (!state.active && now - (state.closedAt ?? 0) > this.bufferTtlMs) {
227
+ this.streams.delete(id);
228
+ }
229
+ }
230
+ for (const [id, s] of this.sessions) {
231
+ if (
232
+ now - s.touchedAt > this.sessionTtlMs &&
233
+ s.emitter.listenerCount('entry') === 0
234
+ ) {
235
+ this.sessions.delete(id);
236
+ }
237
+ }
238
+ }
239
+
240
+ /** 停掉清理定时器。 */
241
+ dispose(): void {
242
+ if (this.timer) clearInterval(this.timer);
243
+ }
244
+ }
@@ -0,0 +1,67 @@
1
+ import type {
2
+ VendorFileCache,
3
+ VendorFileKey,
4
+ VendorFileRecord,
5
+ } from '../ports/vendor-file-cache.js';
6
+
7
+ const keyOf = (k: VendorFileKey) =>
8
+ `${k.vendor}\u0000${k.account}\u0000${k.contentHash}`;
9
+
10
+ /** `VendorFileCache` 的进程内实现:一张 Map。进程重启后要重传(厂商侧旧副本到期自清)。 */
11
+ export class InMemoryVendorFileCache implements VendorFileCache {
12
+ private readonly rows = new Map<string, VendorFileRecord>();
13
+
14
+ find(
15
+ vendor: string,
16
+ contentHashes: readonly string[]
17
+ ): Promise<VendorFileRecord[]> {
18
+ const wanted = new Set(contentHashes);
19
+ const out: VendorFileRecord[] = [];
20
+ for (const row of this.rows.values()) {
21
+ if (row.vendor === vendor && wanted.has(row.contentHash)) {
22
+ out.push({ ...row });
23
+ }
24
+ }
25
+ return Promise.resolve(out);
26
+ }
27
+
28
+ put(record: VendorFileRecord): Promise<void> {
29
+ this.rows.set(keyOf(record), { ...record });
30
+ return Promise.resolve();
31
+ }
32
+
33
+ touch(key: VendorFileKey, at: Date): Promise<void> {
34
+ const row = this.rows.get(keyOf(key));
35
+ if (row) row.lastUsedAt = at;
36
+ return Promise.resolve();
37
+ }
38
+
39
+ forget(
40
+ vendor: string,
41
+ account: string,
42
+ fileIds: readonly string[]
43
+ ): Promise<void> {
44
+ const ids = new Set(fileIds);
45
+ for (const [k, row] of this.rows) {
46
+ if (
47
+ row.vendor === vendor &&
48
+ row.account === account &&
49
+ ids.has(row.fileId)
50
+ ) {
51
+ this.rows.delete(k);
52
+ }
53
+ }
54
+ return Promise.resolve();
55
+ }
56
+
57
+ sweepExpired(now: Date): Promise<number> {
58
+ let n = 0;
59
+ for (const [k, row] of this.rows) {
60
+ if (row.expiresAt && row.expiresAt.getTime() < now.getTime()) {
61
+ this.rows.delete(k);
62
+ n++;
63
+ }
64
+ }
65
+ return Promise.resolve(n);
66
+ }
67
+ }