@autonome-research/thread-phase 3.0.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 (263) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +226 -0
  3. package/dist/agent/index.d.ts +28 -0
  4. package/dist/agent/index.d.ts.map +1 -0
  5. package/dist/agent/index.js +28 -0
  6. package/dist/agent/index.js.map +1 -0
  7. package/dist/agent/openai-adapter.d.ts +15 -0
  8. package/dist/agent/openai-adapter.d.ts.map +1 -0
  9. package/dist/agent/openai-adapter.js +57 -0
  10. package/dist/agent/openai-adapter.js.map +1 -0
  11. package/dist/agent/parse-json.d.ts +12 -0
  12. package/dist/agent/parse-json.d.ts.map +1 -0
  13. package/dist/agent/parse-json.js +31 -0
  14. package/dist/agent/parse-json.js.map +1 -0
  15. package/dist/agent/retry.d.ts +15 -0
  16. package/dist/agent/retry.d.ts.map +1 -0
  17. package/dist/agent/retry.js +35 -0
  18. package/dist/agent/retry.js.map +1 -0
  19. package/dist/agent/runner.d.ts +25 -0
  20. package/dist/agent/runner.d.ts.map +1 -0
  21. package/dist/agent/runner.js +270 -0
  22. package/dist/agent/runner.js.map +1 -0
  23. package/dist/agent/stream-consumer.d.ts +57 -0
  24. package/dist/agent/stream-consumer.d.ts.map +1 -0
  25. package/dist/agent/stream-consumer.js +126 -0
  26. package/dist/agent/stream-consumer.js.map +1 -0
  27. package/dist/agent/types.d.ts +135 -0
  28. package/dist/agent/types.d.ts.map +1 -0
  29. package/dist/agent/types.js +9 -0
  30. package/dist/agent/types.js.map +1 -0
  31. package/dist/agent-runner.d.ts +10 -0
  32. package/dist/agent-runner.d.ts.map +1 -0
  33. package/dist/agent-runner.js +10 -0
  34. package/dist/agent-runner.js.map +1 -0
  35. package/dist/agents/capability.d.ts +36 -0
  36. package/dist/agents/capability.d.ts.map +1 -0
  37. package/dist/agents/capability.js +51 -0
  38. package/dist/agents/capability.js.map +1 -0
  39. package/dist/agents/event-bus.d.ts +20 -0
  40. package/dist/agents/event-bus.d.ts.map +1 -0
  41. package/dist/agents/event-bus.js +40 -0
  42. package/dist/agents/event-bus.js.map +1 -0
  43. package/dist/agents/index.d.ts +23 -0
  44. package/dist/agents/index.d.ts.map +1 -0
  45. package/dist/agents/index.js +33 -0
  46. package/dist/agents/index.js.map +1 -0
  47. package/dist/agents/inference-adapter.d.ts +52 -0
  48. package/dist/agents/inference-adapter.d.ts.map +1 -0
  49. package/dist/agents/inference-adapter.js +209 -0
  50. package/dist/agents/inference-adapter.js.map +1 -0
  51. package/dist/agents/job-store-bridge.d.ts +44 -0
  52. package/dist/agents/job-store-bridge.d.ts.map +1 -0
  53. package/dist/agents/job-store-bridge.js +58 -0
  54. package/dist/agents/job-store-bridge.js.map +1 -0
  55. package/dist/agents/memory.d.ts +40 -0
  56. package/dist/agents/memory.d.ts.map +1 -0
  57. package/dist/agents/memory.js +14 -0
  58. package/dist/agents/memory.js.map +1 -0
  59. package/dist/agents/protocol.d.ts +302 -0
  60. package/dist/agents/protocol.d.ts.map +1 -0
  61. package/dist/agents/protocol.js +36 -0
  62. package/dist/agents/protocol.js.map +1 -0
  63. package/dist/agents/run-helpers.d.ts +70 -0
  64. package/dist/agents/run-helpers.d.ts.map +1 -0
  65. package/dist/agents/run-helpers.js +131 -0
  66. package/dist/agents/run-helpers.js.map +1 -0
  67. package/dist/agents/serialize-error.d.ts +18 -0
  68. package/dist/agents/serialize-error.d.ts.map +1 -0
  69. package/dist/agents/serialize-error.js +27 -0
  70. package/dist/agents/serialize-error.js.map +1 -0
  71. package/dist/agents/structured-output.d.ts +90 -0
  72. package/dist/agents/structured-output.d.ts.map +1 -0
  73. package/dist/agents/structured-output.js +101 -0
  74. package/dist/agents/structured-output.js.map +1 -0
  75. package/dist/agents/test-utils/conformance.d.ts +59 -0
  76. package/dist/agents/test-utils/conformance.d.ts.map +1 -0
  77. package/dist/agents/test-utils/conformance.js +207 -0
  78. package/dist/agents/test-utils/conformance.js.map +1 -0
  79. package/dist/agents/test-utils/index.d.ts +12 -0
  80. package/dist/agents/test-utils/index.d.ts.map +1 -0
  81. package/dist/agents/test-utils/index.js +12 -0
  82. package/dist/agents/test-utils/index.js.map +1 -0
  83. package/dist/agents/test-utils/mock-agent.d.ts +66 -0
  84. package/dist/agents/test-utils/mock-agent.d.ts.map +1 -0
  85. package/dist/agents/test-utils/mock-agent.js +244 -0
  86. package/dist/agents/test-utils/mock-agent.js.map +1 -0
  87. package/dist/agents/thread.d.ts +57 -0
  88. package/dist/agents/thread.d.ts.map +1 -0
  89. package/dist/agents/thread.js +128 -0
  90. package/dist/agents/thread.js.map +1 -0
  91. package/dist/agents/turn-accumulator.d.ts +94 -0
  92. package/dist/agents/turn-accumulator.d.ts.map +1 -0
  93. package/dist/agents/turn-accumulator.js +150 -0
  94. package/dist/agents/turn-accumulator.js.map +1 -0
  95. package/dist/agents/with-memory.d.ts +55 -0
  96. package/dist/agents/with-memory.d.ts.map +1 -0
  97. package/dist/agents/with-memory.js +155 -0
  98. package/dist/agents/with-memory.js.map +1 -0
  99. package/dist/agents/with-thread.d.ts +45 -0
  100. package/dist/agents/with-thread.d.ts.map +1 -0
  101. package/dist/agents/with-thread.js +70 -0
  102. package/dist/agents/with-thread.js.map +1 -0
  103. package/dist/cache.d.ts +47 -0
  104. package/dist/cache.d.ts.map +1 -0
  105. package/dist/cache.js +81 -0
  106. package/dist/cache.js.map +1 -0
  107. package/dist/context/compressor.d.ts +36 -0
  108. package/dist/context/compressor.d.ts.map +1 -0
  109. package/dist/context/compressor.js +158 -0
  110. package/dist/context/compressor.js.map +1 -0
  111. package/dist/context/index.d.ts +4 -0
  112. package/dist/context/index.d.ts.map +1 -0
  113. package/dist/context/index.js +4 -0
  114. package/dist/context/index.js.map +1 -0
  115. package/dist/context/result-capper.d.ts +32 -0
  116. package/dist/context/result-capper.d.ts.map +1 -0
  117. package/dist/context/result-capper.js +50 -0
  118. package/dist/context/result-capper.js.map +1 -0
  119. package/dist/context/token-budget.d.ts +81 -0
  120. package/dist/context/token-budget.d.ts.map +1 -0
  121. package/dist/context/token-budget.js +99 -0
  122. package/dist/context/token-budget.js.map +1 -0
  123. package/dist/helpers/caller.d.ts +18 -0
  124. package/dist/helpers/caller.d.ts.map +1 -0
  125. package/dist/helpers/caller.js +40 -0
  126. package/dist/helpers/caller.js.map +1 -0
  127. package/dist/helpers/hook.d.ts +73 -0
  128. package/dist/helpers/hook.d.ts.map +1 -0
  129. package/dist/helpers/hook.js +244 -0
  130. package/dist/helpers/hook.js.map +1 -0
  131. package/dist/helpers/index.d.ts +12 -0
  132. package/dist/helpers/index.d.ts.map +1 -0
  133. package/dist/helpers/index.js +11 -0
  134. package/dist/helpers/index.js.map +1 -0
  135. package/dist/helpers/one-shot.d.ts +27 -0
  136. package/dist/helpers/one-shot.d.ts.map +1 -0
  137. package/dist/helpers/one-shot.js +43 -0
  138. package/dist/helpers/one-shot.js.map +1 -0
  139. package/dist/helpers/schedule.d.ts +59 -0
  140. package/dist/helpers/schedule.d.ts.map +1 -0
  141. package/dist/helpers/schedule.js +118 -0
  142. package/dist/helpers/schedule.js.map +1 -0
  143. package/dist/helpers/types.d.ts +34 -0
  144. package/dist/helpers/types.d.ts.map +1 -0
  145. package/dist/helpers/types.js +11 -0
  146. package/dist/helpers/types.js.map +1 -0
  147. package/dist/index.d.ts +26 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +37 -0
  150. package/dist/index.js.map +1 -0
  151. package/dist/inference.d.ts +27 -0
  152. package/dist/inference.d.ts.map +1 -0
  153. package/dist/inference.js +34 -0
  154. package/dist/inference.js.map +1 -0
  155. package/dist/messages.d.ts +64 -0
  156. package/dist/messages.d.ts.map +1 -0
  157. package/dist/messages.js +17 -0
  158. package/dist/messages.js.map +1 -0
  159. package/dist/orchestrator.d.ts +56 -0
  160. package/dist/orchestrator.d.ts.map +1 -0
  161. package/dist/orchestrator.js +62 -0
  162. package/dist/orchestrator.js.map +1 -0
  163. package/dist/patterns/bounded-fanout-of.d.ts +61 -0
  164. package/dist/patterns/bounded-fanout-of.d.ts.map +1 -0
  165. package/dist/patterns/bounded-fanout-of.js +142 -0
  166. package/dist/patterns/bounded-fanout-of.js.map +1 -0
  167. package/dist/patterns/bounded-fanout.d.ts +111 -0
  168. package/dist/patterns/bounded-fanout.d.ts.map +1 -0
  169. package/dist/patterns/bounded-fanout.js +151 -0
  170. package/dist/patterns/bounded-fanout.js.map +1 -0
  171. package/dist/patterns/index.d.ts +14 -0
  172. package/dist/patterns/index.d.ts.map +1 -0
  173. package/dist/patterns/index.js +13 -0
  174. package/dist/patterns/index.js.map +1 -0
  175. package/dist/patterns/intent-gate.d.ts +27 -0
  176. package/dist/patterns/intent-gate.d.ts.map +1 -0
  177. package/dist/patterns/intent-gate.js +32 -0
  178. package/dist/patterns/intent-gate.js.map +1 -0
  179. package/dist/patterns/match.d.ts +30 -0
  180. package/dist/patterns/match.d.ts.map +1 -0
  181. package/dist/patterns/match.js +58 -0
  182. package/dist/patterns/match.js.map +1 -0
  183. package/dist/patterns/parallel-fanout.d.ts +28 -0
  184. package/dist/patterns/parallel-fanout.d.ts.map +1 -0
  185. package/dist/patterns/parallel-fanout.js +24 -0
  186. package/dist/patterns/parallel-fanout.js.map +1 -0
  187. package/dist/patterns/parallel-phases.d.ts +27 -0
  188. package/dist/patterns/parallel-phases.d.ts.map +1 -0
  189. package/dist/patterns/parallel-phases.js +77 -0
  190. package/dist/patterns/parallel-phases.js.map +1 -0
  191. package/dist/patterns/preflight-confidence.d.ts +20 -0
  192. package/dist/patterns/preflight-confidence.d.ts.map +1 -0
  193. package/dist/patterns/preflight-confidence.js +38 -0
  194. package/dist/patterns/preflight-confidence.js.map +1 -0
  195. package/dist/patterns/spot-check.d.ts +19 -0
  196. package/dist/patterns/spot-check.d.ts.map +1 -0
  197. package/dist/patterns/spot-check.js +33 -0
  198. package/dist/patterns/spot-check.js.map +1 -0
  199. package/dist/patterns/sub-pipeline.d.ts +84 -0
  200. package/dist/patterns/sub-pipeline.d.ts.map +1 -0
  201. package/dist/patterns/sub-pipeline.js +90 -0
  202. package/dist/patterns/sub-pipeline.js.map +1 -0
  203. package/dist/patterns/synthesize-with-followup.d.ts +35 -0
  204. package/dist/patterns/synthesize-with-followup.d.ts.map +1 -0
  205. package/dist/patterns/synthesize-with-followup.js +45 -0
  206. package/dist/patterns/synthesize-with-followup.js.map +1 -0
  207. package/dist/patterns/while-condition.d.ts +31 -0
  208. package/dist/patterns/while-condition.d.ts.map +1 -0
  209. package/dist/patterns/while-condition.js +59 -0
  210. package/dist/patterns/while-condition.js.map +1 -0
  211. package/dist/patterns/with-retry.d.ts +37 -0
  212. package/dist/patterns/with-retry.d.ts.map +1 -0
  213. package/dist/patterns/with-retry.js +73 -0
  214. package/dist/patterns/with-retry.js.map +1 -0
  215. package/dist/phase.d.ts +78 -0
  216. package/dist/phase.d.ts.map +1 -0
  217. package/dist/phase.js +36 -0
  218. package/dist/phase.js.map +1 -0
  219. package/dist/session/index.d.ts +5 -0
  220. package/dist/session/index.d.ts.map +1 -0
  221. package/dist/session/index.js +4 -0
  222. package/dist/session/index.js.map +1 -0
  223. package/dist/session/job-runner.d.ts +67 -0
  224. package/dist/session/job-runner.d.ts.map +1 -0
  225. package/dist/session/job-runner.js +131 -0
  226. package/dist/session/job-runner.js.map +1 -0
  227. package/dist/session/job-store.d.ts +98 -0
  228. package/dist/session/job-store.d.ts.map +1 -0
  229. package/dist/session/job-store.js +37 -0
  230. package/dist/session/job-store.js.map +1 -0
  231. package/dist/session/sqlite-job-store.d.ts +40 -0
  232. package/dist/session/sqlite-job-store.d.ts.map +1 -0
  233. package/dist/session/sqlite-job-store.js +200 -0
  234. package/dist/session/sqlite-job-store.js.map +1 -0
  235. package/dist/session/sse.d.ts +60 -0
  236. package/dist/session/sse.d.ts.map +1 -0
  237. package/dist/session/sse.js +97 -0
  238. package/dist/session/sse.js.map +1 -0
  239. package/dist/tools/index.d.ts +2 -0
  240. package/dist/tools/index.d.ts.map +1 -0
  241. package/dist/tools/index.js +2 -0
  242. package/dist/tools/index.js.map +1 -0
  243. package/dist/tools/registry.d.ts +44 -0
  244. package/dist/tools/registry.d.ts.map +1 -0
  245. package/dist/tools/registry.js +74 -0
  246. package/dist/tools/registry.js.map +1 -0
  247. package/dist/triggers/index.d.ts +15 -0
  248. package/dist/triggers/index.d.ts.map +1 -0
  249. package/dist/triggers/index.js +14 -0
  250. package/dist/triggers/index.js.map +1 -0
  251. package/dist/triggers/run-trigger.d.ts +86 -0
  252. package/dist/triggers/run-trigger.d.ts.map +1 -0
  253. package/dist/triggers/run-trigger.js +146 -0
  254. package/dist/triggers/run-trigger.js.map +1 -0
  255. package/dist/triggers/timer-trigger.d.ts +46 -0
  256. package/dist/triggers/timer-trigger.d.ts.map +1 -0
  257. package/dist/triggers/timer-trigger.js +74 -0
  258. package/dist/triggers/timer-trigger.js.map +1 -0
  259. package/dist/triggers/types.d.ts +61 -0
  260. package/dist/triggers/types.d.ts.map +1 -0
  261. package/dist/triggers/types.js +23 -0
  262. package/dist/triggers/types.js.map +1 -0
  263. package/package.json +64 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,UAAU,EACV,KAAK,KAAK,EACV,KAAK,mBAAmB,EACxB,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AAKpB,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,eAAe,GACrB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACL,iBAAiB,EACjB,SAAS,EACT,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,KAAK,SAAS,GACf,MAAM,kBAAkB,CAAC;AAM1B,OAAO,EACL,KAAK,QAAQ,EACb,cAAc,EACd,SAAS,EACT,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,SAAS,GACf,MAAM,oBAAoB,CAAC;AAI5B,OAAO,EACL,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,mBAAmB,GACzB,MAAM,kBAAkB,CAAC;AAK1B,OAAO,EACL,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,WAAW,EACX,WAAW,EACX,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,cAAc,GACpB,MAAM,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Public API — the v1 stable surface.
3
+ *
4
+ * Everything exported from this file is covered by semver:
5
+ * - patch (1.0.x): bug fixes, no API changes
6
+ * - minor (1.x.0): additive only — new exports, new optional fields
7
+ * - major (x.0.0): breaking changes
8
+ *
9
+ * Items marked @internal in their own files (e.g. `consumeStream`,
10
+ * `toOpenAIMessages`) are reachable via deep import for advanced callers
11
+ * but are NOT part of this stable surface and may change in minor releases.
12
+ *
13
+ * Subpath exports (./patterns, ./context, ./session, ./tools) live in their
14
+ * own index files and are exposed via package.json `exports`. They follow
15
+ * the same stability policy.
16
+ */
17
+ // Phase framework
18
+ export { PipelineCache } from './cache.js';
19
+ export { runPipeline, runPipelineToSummary, } from './orchestrator.js';
20
+ export { requireCtx, } from './phase.js';
21
+ // Inference — convenience builders for an OpenAI-compatible client.
22
+ export { loadInferenceConfig, createInferenceClient, } from './inference.js';
23
+ // Agent runner — the iterated tool-use loop primitive.
24
+ export { runAgentWithTools, parseJSON, } from './agent/index.js';
25
+ // Session — persisted event log + job runner. SqliteJobStore is the
26
+ // bundled default; JobStore is the interface — bring your own backend if
27
+ // needed. The interface is sync by deliberate choice for v1 (sqlite hot
28
+ // path); see ROADMAP for the rationale.
29
+ export { SqliteJobStore, JobRunner, } from './session/index.js';
30
+ // Tools — registry with optional ajv arg validation. Implements
31
+ // ToolExecutor so it can be passed directly to runAgentWithTools.
32
+ export { ToolRegistry, } from './tools/index.js';
33
+ // Helpers — one-call wrappers for the common Trigger + Pipeline shapes.
34
+ // Each returns an `ExtensionRegisterFn` `(api) => void` consumable by the
35
+ // CLI auto-loader.
36
+ export { oneShot, schedule, hook, CronTrigger, HttpTrigger, } from './helpers/index.js';
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,kBAAkB;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EACL,WAAW,EACX,oBAAoB,GAErB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,UAAU,GAIX,MAAM,YAAY,CAAC;AAiBpB,oEAAoE;AACpE,OAAO,EACL,mBAAmB,EACnB,qBAAqB,GAEtB,MAAM,gBAAgB,CAAC;AAExB,uDAAuD;AACvD,OAAO,EACL,iBAAiB,EACjB,SAAS,GAQV,MAAM,kBAAkB,CAAC;AAE1B,oEAAoE;AACpE,yEAAyE;AACzE,wEAAwE;AACxE,wCAAwC;AACxC,OAAO,EAEL,cAAc,EACd,SAAS,GAMV,MAAM,oBAAoB,CAAC;AAE5B,gEAAgE;AAChE,kEAAkE;AAClE,OAAO,EACL,YAAY,GAGb,MAAM,kBAAkB,CAAC;AAE1B,wEAAwE;AACxE,0EAA0E;AAC1E,mBAAmB;AACnB,OAAO,EACL,OAAO,EACP,QAAQ,EACR,IAAI,EACJ,WAAW,EACX,WAAW,GAUZ,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Inference provider configuration.
3
+ *
4
+ * Single source of truth for which OpenAI-compatible endpoint we hit. Reads
5
+ * from env by default; downstream apps can pass overrides for testing or to
6
+ * point a single process at multiple providers.
7
+ */
8
+ import OpenAI from 'openai';
9
+ export interface InferenceConfig {
10
+ /** Base URL of the OpenAI-compatible endpoint. Defaults to local vLLM. */
11
+ baseUrl: string;
12
+ /** API key. Local vLLM accepts any non-empty string. */
13
+ apiKey: string;
14
+ /** Default model name for heavy reasoning. */
15
+ defaultModel: string;
16
+ /** Lighter model for cheap/fast phases (planner, classifier, intent gate). */
17
+ lightModel: string;
18
+ /** Context window in tokens. Drives the budget tracker's default. */
19
+ contextLength: number;
20
+ }
21
+ export declare function loadInferenceConfig(overrides?: Partial<InferenceConfig>): InferenceConfig;
22
+ /**
23
+ * Build an OpenAI client pointed at the configured endpoint.
24
+ * Uses the standard `openai` SDK; works against any /v1/chat/completions service.
25
+ */
26
+ export declare function createInferenceClient(overrides?: Partial<InferenceConfig>): OpenAI;
27
+ //# sourceMappingURL=inference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inference.d.ts","sourceRoot":"","sources":["../src/inference.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,WAAW,eAAe;IAC9B,0EAA0E;IAC1E,OAAO,EAAE,MAAM,CAAC;IAChB,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,YAAY,EAAE,MAAM,CAAC;IACrB,8EAA8E;IAC9E,UAAU,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,aAAa,EAAE,MAAM,CAAC;CACvB;AAMD,wBAAgB,mBAAmB,CAAC,SAAS,GAAE,OAAO,CAAC,eAAe,CAAM,GAAG,eAAe,CAU7F;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,GAAE,OAAO,CAAC,eAAe,CAAM,GAAG,MAAM,CAMtF"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Inference provider configuration.
3
+ *
4
+ * Single source of truth for which OpenAI-compatible endpoint we hit. Reads
5
+ * from env by default; downstream apps can pass overrides for testing or to
6
+ * point a single process at multiple providers.
7
+ */
8
+ import OpenAI from 'openai';
9
+ const DEFAULT_BASE_URL = 'http://localhost:8000/v1';
10
+ const DEFAULT_MODEL = 'qwen3.6-27b';
11
+ const DEFAULT_CONTEXT_LENGTH = 131_072;
12
+ export function loadInferenceConfig(overrides = {}) {
13
+ const defaultModel = process.env.INFERENCE_MODEL ?? DEFAULT_MODEL;
14
+ return {
15
+ baseUrl: process.env.INFERENCE_BASE_URL ?? DEFAULT_BASE_URL,
16
+ apiKey: process.env.INFERENCE_API_KEY ?? 'not-needed-for-local-vllm',
17
+ defaultModel,
18
+ lightModel: process.env.INFERENCE_MODEL_LIGHT ?? defaultModel,
19
+ contextLength: Number(process.env.INFERENCE_CONTEXT_LENGTH ?? DEFAULT_CONTEXT_LENGTH),
20
+ ...overrides,
21
+ };
22
+ }
23
+ /**
24
+ * Build an OpenAI client pointed at the configured endpoint.
25
+ * Uses the standard `openai` SDK; works against any /v1/chat/completions service.
26
+ */
27
+ export function createInferenceClient(overrides = {}) {
28
+ const config = loadInferenceConfig(overrides);
29
+ return new OpenAI({
30
+ baseURL: config.baseUrl,
31
+ apiKey: config.apiKey,
32
+ });
33
+ }
34
+ //# sourceMappingURL=inference.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inference.js","sourceRoot":"","sources":["../src/inference.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAe5B,MAAM,gBAAgB,GAAG,0BAA0B,CAAC;AACpD,MAAM,aAAa,GAAG,aAAa,CAAC;AACpC,MAAM,sBAAsB,GAAG,OAAO,CAAC;AAEvC,MAAM,UAAU,mBAAmB,CAAC,YAAsC,EAAE;IAC1E,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,aAAa,CAAC;IAClE,OAAO;QACL,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,gBAAgB;QAC3D,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,2BAA2B;QACpE,YAAY;QACZ,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB,IAAI,YAAY;QAC7D,aAAa,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,sBAAsB,CAAC;QACrF,GAAG,SAAS;KACb,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAsC,EAAE;IAC5E,MAAM,MAAM,GAAG,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC9C,OAAO,IAAI,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,MAAM,EAAE,MAAM,CAAC,MAAM;KACtB,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Internal message representation.
3
+ *
4
+ * thread-phase uses its own Message shape so the framework isn't coupled to
5
+ * either Anthropic's content-block model or OpenAI's tool-call model.
6
+ * Translation to/from the wire format happens at the inference boundary
7
+ * (see agent-runner.ts).
8
+ *
9
+ * Shape choice: closer to OpenAI than Anthropic, because:
10
+ * - tool_calls live on the assistant message as a separate field, not as
11
+ * embedded content blocks.
12
+ * - tool results are their own role:'tool' messages with a tool_call_id link.
13
+ * This matches vLLM / Ollama / llama.cpp / OpenAI all natively, and the
14
+ * Anthropic SDK can be adapted at the boundary if ever needed.
15
+ */
16
+ export interface ToolCall {
17
+ /** Unique id for this call. Used to match tool result messages back to the call. */
18
+ id: string;
19
+ /** Tool name from the registered ToolDefinition. */
20
+ name: string;
21
+ /** Parsed arguments. The framework parses JSON from the wire and passes structured args. */
22
+ input: Record<string, unknown>;
23
+ }
24
+ export interface SystemMessage {
25
+ role: 'system';
26
+ content: string;
27
+ }
28
+ export interface UserMessage {
29
+ role: 'user';
30
+ content: string;
31
+ }
32
+ export interface AssistantMessage {
33
+ role: 'assistant';
34
+ /** May be empty string when the assistant only emitted tool calls. */
35
+ content: string;
36
+ /** Empty array when no tool calls were made. */
37
+ toolCalls: ToolCall[];
38
+ }
39
+ export interface ToolResultMessage {
40
+ role: 'tool';
41
+ /** Must match a ToolCall.id from a prior assistant message. */
42
+ toolCallId: string;
43
+ content: string;
44
+ }
45
+ export type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage;
46
+ export interface ToolDefinition {
47
+ name: string;
48
+ description: string;
49
+ /** JSON Schema (object). Translated to OpenAI's `function.parameters` at the boundary. */
50
+ inputSchema: {
51
+ type: 'object';
52
+ properties: Record<string, unknown>;
53
+ required?: string[];
54
+ additionalProperties?: boolean;
55
+ };
56
+ }
57
+ export interface ToolResult {
58
+ toolCallId: string;
59
+ content: string;
60
+ }
61
+ export interface ToolExecutor {
62
+ execute(name: string, toolCallId: string, args: Record<string, unknown>): Promise<ToolResult>;
63
+ }
64
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAMH,MAAM,WAAW,QAAQ;IACvB,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAC;IACX,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAC;IACb,4FAA4F;IAC5F,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAMD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,WAAW,CAAC;IAClB,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAC;IAChB,gDAAgD;IAChD,SAAS,EAAE,QAAQ,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,WAAW,GAAG,gBAAgB,GAAG,iBAAiB,CAAC;AAMzF,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,0FAA0F;IAC1F,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ,CAAC;QACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACpC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;QACpB,oBAAoB,CAAC,EAAE,OAAO,CAAC;KAChC,CAAC;CACH;AAMD,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC/F"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Internal message representation.
3
+ *
4
+ * thread-phase uses its own Message shape so the framework isn't coupled to
5
+ * either Anthropic's content-block model or OpenAI's tool-call model.
6
+ * Translation to/from the wire format happens at the inference boundary
7
+ * (see agent-runner.ts).
8
+ *
9
+ * Shape choice: closer to OpenAI than Anthropic, because:
10
+ * - tool_calls live on the assistant message as a separate field, not as
11
+ * embedded content blocks.
12
+ * - tool results are their own role:'tool' messages with a tool_call_id link.
13
+ * This matches vLLM / Ollama / llama.cpp / OpenAI all natively, and the
14
+ * Anthropic SDK can be adapted at the boundary if ever needed.
15
+ */
16
+ export {};
17
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Pipeline orchestrator — runs a list of phases over a shared context.
3
+ *
4
+ * Owns the canonical terminal events (`done` / `error`) so phases never have
5
+ * to emit them themselves. Halts the pipeline cleanly when any phase sets
6
+ * `ctx.stop`.
7
+ *
8
+ * Composition is just an array of phases. Reorder by reordering the array.
9
+ * Add a phase by including a new entry. No DAG framework, no plugin system.
10
+ *
11
+ * Generic over the phases' event type. Downstream apps that parameterize
12
+ * Phase with a custom TEvent get the same TEvent surfaced through the
13
+ * orchestrator's stream — but TEvent must be assignable from the
14
+ * framework's `done`/`error` shapes (see PipelineEvent), so the simplest
15
+ * downstream pattern is `type MyEvent = PipelineEvent | { type: 'mine' };`.
16
+ */
17
+ import type { BasePipelineContext, Phase, PipelineEvent } from './phase.js';
18
+ /**
19
+ * The terminal state of a pipeline run.
20
+ *
21
+ * - `completed` — all phases ran to completion.
22
+ * - `stopped` — a phase set `ctx.stop`; pipeline halted cleanly with that reason.
23
+ *
24
+ * Phase exceptions do not produce a summary; they propagate to the caller.
25
+ * Use `runPipelineToSummary` to wrap the generator and convert throws into
26
+ * promise rejections (with the original error preserved).
27
+ */
28
+ export interface PipelineSummary {
29
+ readonly status: 'completed' | 'stopped';
30
+ /** Present iff `status === 'stopped'`. The `ctx.stop.reason` value. */
31
+ readonly reason?: string;
32
+ /** Total events yielded, including the terminal `done` event. */
33
+ readonly eventCount: number;
34
+ }
35
+ export interface RunPipelineOptions {
36
+ /**
37
+ * AbortSignal observed between phases. If the signal aborts at any point,
38
+ * `runPipeline` throws an `AbortError` (`name === 'AbortError'`) before
39
+ * the next phase runs. Phases that want mid-phase cancellation should
40
+ * observe the signal themselves and unwind cleanly.
41
+ */
42
+ signal?: AbortSignal;
43
+ }
44
+ export declare function runPipeline<TCtx extends BasePipelineContext, TEvent = PipelineEvent>(phases: ReadonlyArray<Phase<TCtx, TEvent>>, ctx: TCtx, options?: RunPipelineOptions): AsyncGenerator<TEvent, void>;
45
+ /**
46
+ * Consume `runPipeline` to completion and return a typed summary.
47
+ *
48
+ * On success, resolves with `{ status, reason?, eventCount }`. Phase
49
+ * exceptions reject the promise with the original error (the cache is
50
+ * still cleared via the generator's `finally`).
51
+ *
52
+ * Use this when you want a single `await` for the whole pipeline rather
53
+ * than iterating events yourself.
54
+ */
55
+ export declare function runPipelineToSummary<TCtx extends BasePipelineContext, TEvent extends PipelineEvent = PipelineEvent>(phases: ReadonlyArray<Phase<TCtx, TEvent>>, ctx: TCtx, options?: RunPipelineOptions): Promise<PipelineSummary>;
56
+ //# sourceMappingURL=orchestrator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5E;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IACzC,uEAAuE;IACvE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,iEAAiE;IACjE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,kBAAkB;IACjC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,wBAAuB,WAAW,CAChC,IAAI,SAAS,mBAAmB,EAChC,MAAM,GAAG,aAAa,EAEtB,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,EAC1C,GAAG,EAAE,IAAI,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC3B,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,CAqB9B;AAED;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,SAAS,mBAAmB,EAChC,MAAM,SAAS,aAAa,GAAG,aAAa,EAE5C,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,EAC1C,GAAG,EAAE,IAAI,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,eAAe,CAAC,CAY1B"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Pipeline orchestrator — runs a list of phases over a shared context.
3
+ *
4
+ * Owns the canonical terminal events (`done` / `error`) so phases never have
5
+ * to emit them themselves. Halts the pipeline cleanly when any phase sets
6
+ * `ctx.stop`.
7
+ *
8
+ * Composition is just an array of phases. Reorder by reordering the array.
9
+ * Add a phase by including a new entry. No DAG framework, no plugin system.
10
+ *
11
+ * Generic over the phases' event type. Downstream apps that parameterize
12
+ * Phase with a custom TEvent get the same TEvent surfaced through the
13
+ * orchestrator's stream — but TEvent must be assignable from the
14
+ * framework's `done`/`error` shapes (see PipelineEvent), so the simplest
15
+ * downstream pattern is `type MyEvent = PipelineEvent | { type: 'mine' };`.
16
+ */
17
+ export async function* runPipeline(phases, ctx, options) {
18
+ const signal = options?.signal;
19
+ try {
20
+ for (const phase of phases) {
21
+ if (signal?.aborted) {
22
+ const reason = signal.reason ?? 'aborted';
23
+ const err = new Error(`runPipeline aborted: ${reason}`);
24
+ err.name = 'AbortError';
25
+ throw err;
26
+ }
27
+ yield* phase.run(ctx);
28
+ if (ctx.stop) {
29
+ yield { type: 'done', reason: ctx.stop.reason };
30
+ return;
31
+ }
32
+ }
33
+ yield { type: 'done' };
34
+ }
35
+ finally {
36
+ ctx.cache.clear();
37
+ }
38
+ }
39
+ /**
40
+ * Consume `runPipeline` to completion and return a typed summary.
41
+ *
42
+ * On success, resolves with `{ status, reason?, eventCount }`. Phase
43
+ * exceptions reject the promise with the original error (the cache is
44
+ * still cleared via the generator's `finally`).
45
+ *
46
+ * Use this when you want a single `await` for the whole pipeline rather
47
+ * than iterating events yourself.
48
+ */
49
+ export async function runPipelineToSummary(phases, ctx, options) {
50
+ let eventCount = 0;
51
+ let stopReason;
52
+ for await (const event of runPipeline(phases, ctx, options)) {
53
+ eventCount++;
54
+ if (event.type === 'done') {
55
+ stopReason = event.reason;
56
+ }
57
+ }
58
+ return stopReason !== undefined
59
+ ? { status: 'stopped', reason: stopReason, eventCount }
60
+ : { status: 'completed', eventCount };
61
+ }
62
+ //# sourceMappingURL=orchestrator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator.js","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAgCH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,WAAW,CAIhC,MAA0C,EAC1C,GAAS,EACT,OAA4B;IAE5B,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAC/B,IAAI,CAAC;QACH,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;gBACpB,MAAM,MAAM,GACT,MAAM,CAAC,MAA6B,IAAI,SAAS,CAAC;gBACrD,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,wBAAwB,MAAM,EAAE,CAAC,CAAC;gBACxD,GAAG,CAAC,IAAI,GAAG,YAAY,CAAC;gBACxB,MAAM,GAAG,CAAC;YACZ,CAAC;YACD,KAAK,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACtB,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;gBACb,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,EAAuB,CAAC;gBACrE,OAAO;YACT,CAAC;QACH,CAAC;QACD,MAAM,EAAE,IAAI,EAAE,MAAM,EAAuB,CAAC;IAC9C,CAAC;YAAS,CAAC;QACT,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAIxC,MAA0C,EAC1C,GAAS,EACT,OAA4B;IAE5B,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,IAAI,UAA8B,CAAC;IACnC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,WAAW,CAAe,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;QAC1E,UAAU,EAAE,CAAC;QACb,IAAK,KAAuB,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC7C,UAAU,GAAI,KAA6B,CAAC,MAAM,CAAC;QACrD,CAAC;IACH,CAAC;IACD,OAAO,UAAU,KAAK,SAAS;QAC7B,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE;QACvD,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * `boundedFanoutOf` — fan an `AgentAdapter` over N items with capped
3
+ * concurrency, with automatic event-bus propagation so every parallel
4
+ * adapter run lands events on one shared bus.
5
+ *
6
+ * Shape captured: same as `boundedFanout`, but the per-item runner is an
7
+ * adapter + buildConfig pair instead of a free function. The adapter's
8
+ * canonical AgentEvents — including events from all N parallel runs —
9
+ * fan into one `options.eventBus` for downstream consumers (JobStore,
10
+ * SSE, audit log). Cancellation propagates from `options.signal` to
11
+ * every in-flight adapter call.
12
+ *
13
+ * When to reach for this: you have a list of items, you want to run a
14
+ * pre-built agent (claude code, hermes, anthropic) on each, with capped
15
+ * concurrency, and you want a single event stream for the whole batch.
16
+ *
17
+ * When NOT to use: when you control the per-item runner directly (no
18
+ * adapter abstraction in play), reach for `boundedFanout` — its callback
19
+ * form is more direct. When you have one item, just call the adapter
20
+ * directly.
21
+ *
22
+ * @internal — exported via thread-phase/patterns
23
+ */
24
+ import type { AgentAdapterMeta, AgentEventBus, AgentRunResult } from '../agents/protocol.js';
25
+ /** Per-item failure handling. Mirrors boundedFanout's mode. @internal */
26
+ export type BoundedFanoutOfMode = 'fail-fast' | 'collect';
27
+ /** @internal */
28
+ export interface BoundedFanoutOfOptions<TItem, TConfig> {
29
+ items: ReadonlyArray<TItem>;
30
+ concurrency: number;
31
+ adapter: AgentAdapterMeta<TConfig>;
32
+ buildConfig: (item: TItem, index: number) => TConfig;
33
+ signal?: AbortSignal;
34
+ /** Shared event bus — every per-item adapter run mirrors events here. */
35
+ eventBus?: AgentEventBus;
36
+ /** Optional traceId propagated into each adapter's options.traceId. */
37
+ traceId?: string;
38
+ /** Default 'fail-fast'. */
39
+ mode?: BoundedFanoutOfMode;
40
+ /** Called when an item's adapter result is in. Synchronous-only. */
41
+ onItemEnd?: (item: TItem, index: number, result: AgentRunResult) => void;
42
+ /** Called when fail-fast is triggered. The first failing index/result is passed. */
43
+ onItemError?: (item: TItem, index: number, result: AgentRunResult) => void;
44
+ }
45
+ /**
46
+ * Run an adapter over N items with capped concurrency. Returns results in
47
+ * input order. In `collect` mode, individual failures are stored as
48
+ * error results (finishReason: 'error') and the batch completes. In
49
+ * `fail-fast` (default), the first failure cancels in-flight runs and
50
+ * the function rejects with an Error wrapping the failed item.
51
+ *
52
+ * @internal
53
+ */
54
+ export declare function boundedFanoutOf<TItem, TConfig>(opts: BoundedFanoutOfOptions<TItem, TConfig>): Promise<AgentRunResult[]>;
55
+ /** @internal */
56
+ export declare class BoundedFanoutOfError extends Error {
57
+ itemIndex: number;
58
+ result: AgentRunResult;
59
+ constructor(itemIndex: number, result: AgentRunResult);
60
+ }
61
+ //# sourceMappingURL=bounded-fanout-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bounded-fanout-of.d.ts","sourceRoot":"","sources":["../../src/patterns/bounded-fanout-of.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EAEb,cAAc,EACf,MAAM,uBAAuB,CAAC;AAE/B,yEAAyE;AACzE,MAAM,MAAM,mBAAmB,GAAG,WAAW,GAAG,SAAS,CAAC;AAE1D,gBAAgB;AAChB,MAAM,WAAW,sBAAsB,CAAC,KAAK,EAAE,OAAO;IACpD,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACnC,WAAW,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IACrD,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,yEAAyE;IACzE,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2BAA2B;IAC3B,IAAI,CAAC,EAAE,mBAAmB,CAAC;IAC3B,oEAAoE;IACpE,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IACzE,oFAAoF;IACpF,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;CAC5E;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,OAAO,EAClD,IAAI,EAAE,sBAAsB,CAAC,KAAK,EAAE,OAAO,CAAC,GAC3C,OAAO,CAAC,cAAc,EAAE,CAAC,CA4G3B;AAED,gBAAgB;AAChB,qBAAa,oBAAqB,SAAQ,KAAK;IAEpC,SAAS,EAAE,MAAM;IACjB,MAAM,EAAE,cAAc;gBADtB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,cAAc;CAKhC"}
@@ -0,0 +1,142 @@
1
+ /**
2
+ * `boundedFanoutOf` — fan an `AgentAdapter` over N items with capped
3
+ * concurrency, with automatic event-bus propagation so every parallel
4
+ * adapter run lands events on one shared bus.
5
+ *
6
+ * Shape captured: same as `boundedFanout`, but the per-item runner is an
7
+ * adapter + buildConfig pair instead of a free function. The adapter's
8
+ * canonical AgentEvents — including events from all N parallel runs —
9
+ * fan into one `options.eventBus` for downstream consumers (JobStore,
10
+ * SSE, audit log). Cancellation propagates from `options.signal` to
11
+ * every in-flight adapter call.
12
+ *
13
+ * When to reach for this: you have a list of items, you want to run a
14
+ * pre-built agent (claude code, hermes, anthropic) on each, with capped
15
+ * concurrency, and you want a single event stream for the whole batch.
16
+ *
17
+ * When NOT to use: when you control the per-item runner directly (no
18
+ * adapter abstraction in play), reach for `boundedFanout` — its callback
19
+ * form is more direct. When you have one item, just call the adapter
20
+ * directly.
21
+ *
22
+ * @internal — exported via thread-phase/patterns
23
+ */
24
+ /**
25
+ * Run an adapter over N items with capped concurrency. Returns results in
26
+ * input order. In `collect` mode, individual failures are stored as
27
+ * error results (finishReason: 'error') and the batch completes. In
28
+ * `fail-fast` (default), the first failure cancels in-flight runs and
29
+ * the function rejects with an Error wrapping the failed item.
30
+ *
31
+ * @internal
32
+ */
33
+ export async function boundedFanoutOf(opts) {
34
+ const items = opts.items;
35
+ if (items.length === 0)
36
+ return [];
37
+ const concurrency = Math.max(1, Math.min(opts.concurrency, items.length));
38
+ const mode = opts.mode ?? 'fail-fast';
39
+ const results = new Array(items.length);
40
+ const inFlight = new Set();
41
+ let failed = null;
42
+ let cursor = 0;
43
+ const abortAllInFlight = () => {
44
+ for (const entry of inFlight) {
45
+ entry.controller.abort();
46
+ entry.run.abort('boundedFanoutOf fail-fast');
47
+ }
48
+ };
49
+ const worker = async () => {
50
+ while (true) {
51
+ if (failed)
52
+ return;
53
+ if (opts.signal?.aborted)
54
+ return;
55
+ const i = cursor++;
56
+ if (i >= items.length)
57
+ return;
58
+ const item = items[i];
59
+ const itemController = new AbortController();
60
+ const compositeSignal = opts.signal
61
+ ? AbortSignal.any([opts.signal, itemController.signal])
62
+ : itemController.signal;
63
+ const config = opts.buildConfig(item, i);
64
+ const run = opts.adapter.adapter(config, {
65
+ signal: compositeSignal,
66
+ eventBus: opts.eventBus,
67
+ traceId: opts.traceId,
68
+ });
69
+ const entry = { controller: itemController, run };
70
+ inFlight.add(entry);
71
+ // The adapter's events iterable is intentionally NOT consumed here.
72
+ // The event bus is the multi-subscriber seam; double-iteration would
73
+ // break the single-consumer invariant. Awaiting `result` is sufficient
74
+ // to start lazy adapters.
75
+ let result;
76
+ try {
77
+ result = await run.result;
78
+ }
79
+ finally {
80
+ inFlight.delete(entry);
81
+ }
82
+ if (result.finishReason === 'error') {
83
+ if (mode === 'fail-fast') {
84
+ if (!failed) {
85
+ failed = { index: i, result };
86
+ opts.onItemError?.(item, i, result);
87
+ abortAllInFlight();
88
+ }
89
+ return;
90
+ }
91
+ // collect: store and keep going.
92
+ results[i] = result;
93
+ opts.onItemEnd?.(item, i, result);
94
+ continue;
95
+ }
96
+ results[i] = result;
97
+ opts.onItemEnd?.(item, i, result);
98
+ }
99
+ };
100
+ const workers = [];
101
+ for (let w = 0; w < concurrency; w++)
102
+ workers.push(worker());
103
+ await Promise.all(workers);
104
+ if (failed) {
105
+ const f = failed;
106
+ throw new BoundedFanoutOfError(f.index, f.result);
107
+ }
108
+ // collect mode may still have undefined slots if the outer signal aborted
109
+ // before some items were dispatched. Adapters honoring `compositeSignal`
110
+ // produce `finishReason: 'aborted'` results for items that started post-
111
+ // abort; items that never started leave the slot undefined. Fill those
112
+ // with a synthetic aborted result so the returned array stays position-
113
+ // stable with the input items array.
114
+ if (opts.signal?.aborted) {
115
+ for (let i = 0; i < items.length; i++) {
116
+ if (results[i] === undefined) {
117
+ results[i] = syntheticAbortedResult();
118
+ }
119
+ }
120
+ }
121
+ return results;
122
+ }
123
+ /** @internal */
124
+ export class BoundedFanoutOfError extends Error {
125
+ itemIndex;
126
+ result;
127
+ constructor(itemIndex, result) {
128
+ super(`boundedFanoutOf failed at item ${itemIndex}: ${result.finishReason}`);
129
+ this.itemIndex = itemIndex;
130
+ this.result = result;
131
+ this.name = 'BoundedFanoutOfError';
132
+ }
133
+ }
134
+ function syntheticAbortedResult() {
135
+ return {
136
+ text: '',
137
+ finishReason: 'aborted',
138
+ usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
139
+ executedToolCalls: [],
140
+ };
141
+ }
142
+ //# sourceMappingURL=bounded-fanout-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bounded-fanout-of.js","sourceRoot":"","sources":["../../src/patterns/bounded-fanout-of.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AA+BH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAA4C;IAE5C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACzB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAElC,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1E,MAAM,IAAI,GAAwB,IAAI,CAAC,IAAI,IAAI,WAAW,CAAC;IAC3D,MAAM,OAAO,GAAsC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