@aigne/agent-runtime 1.12.0-beta.5

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 (234) hide show
  1. package/LICENSE.md +26 -0
  2. package/README.md +79 -0
  3. package/dist/agent-runners/ai-runner.cjs +133 -0
  4. package/dist/agent-runners/ai-runner.d.cts +33 -0
  5. package/dist/agent-runners/ai-runner.d.cts.map +1 -0
  6. package/dist/agent-runners/ai-runner.d.mts +33 -0
  7. package/dist/agent-runners/ai-runner.d.mts.map +1 -0
  8. package/dist/agent-runners/ai-runner.mjs +134 -0
  9. package/dist/agent-runners/ai-runner.mjs.map +1 -0
  10. package/dist/agent-runners/ash-runner.cjs +272 -0
  11. package/dist/agent-runners/ash-runner.d.cts +30 -0
  12. package/dist/agent-runners/ash-runner.d.cts.map +1 -0
  13. package/dist/agent-runners/ash-runner.d.mts +30 -0
  14. package/dist/agent-runners/ash-runner.d.mts.map +1 -0
  15. package/dist/agent-runners/ash-runner.mjs +273 -0
  16. package/dist/agent-runners/ash-runner.mjs.map +1 -0
  17. package/dist/agent-runners/budget.cjs +71 -0
  18. package/dist/agent-runners/budget.d.cts +40 -0
  19. package/dist/agent-runners/budget.d.cts.map +1 -0
  20. package/dist/agent-runners/budget.d.mts +40 -0
  21. package/dist/agent-runners/budget.d.mts.map +1 -0
  22. package/dist/agent-runners/budget.mjs +67 -0
  23. package/dist/agent-runners/budget.mjs.map +1 -0
  24. package/dist/agent-runners/code-runner.cjs +61 -0
  25. package/dist/agent-runners/code-runner.mjs +61 -0
  26. package/dist/agent-runners/code-runner.mjs.map +1 -0
  27. package/dist/agent-runners/env-whitelist.cjs +36 -0
  28. package/dist/agent-runners/env-whitelist.d.cts +5 -0
  29. package/dist/agent-runners/env-whitelist.d.cts.map +1 -0
  30. package/dist/agent-runners/env-whitelist.d.mts +5 -0
  31. package/dist/agent-runners/env-whitelist.d.mts.map +1 -0
  32. package/dist/agent-runners/env-whitelist.mjs +36 -0
  33. package/dist/agent-runners/env-whitelist.mjs.map +1 -0
  34. package/dist/agent-runners/guide-rails.cjs +47 -0
  35. package/dist/agent-runners/guide-rails.d.cts +16 -0
  36. package/dist/agent-runners/guide-rails.d.cts.map +1 -0
  37. package/dist/agent-runners/guide-rails.d.mts +16 -0
  38. package/dist/agent-runners/guide-rails.d.mts.map +1 -0
  39. package/dist/agent-runners/guide-rails.mjs +48 -0
  40. package/dist/agent-runners/guide-rails.mjs.map +1 -0
  41. package/dist/agent-runners/hook-channel.cjs +55 -0
  42. package/dist/agent-runners/hook-channel.d.cts +38 -0
  43. package/dist/agent-runners/hook-channel.d.cts.map +1 -0
  44. package/dist/agent-runners/hook-channel.d.mts +38 -0
  45. package/dist/agent-runners/hook-channel.d.mts.map +1 -0
  46. package/dist/agent-runners/hook-channel.mjs +55 -0
  47. package/dist/agent-runners/hook-channel.mjs.map +1 -0
  48. package/dist/agent-runners/hooks.cjs +107 -0
  49. package/dist/agent-runners/hooks.d.cts +41 -0
  50. package/dist/agent-runners/hooks.d.cts.map +1 -0
  51. package/dist/agent-runners/hooks.d.mts +41 -0
  52. package/dist/agent-runners/hooks.d.mts.map +1 -0
  53. package/dist/agent-runners/hooks.mjs +107 -0
  54. package/dist/agent-runners/hooks.mjs.map +1 -0
  55. package/dist/agent-runners/memory-afs.cjs +342 -0
  56. package/dist/agent-runners/memory-afs.d.cts +221 -0
  57. package/dist/agent-runners/memory-afs.d.cts.map +1 -0
  58. package/dist/agent-runners/memory-afs.d.mts +221 -0
  59. package/dist/agent-runners/memory-afs.d.mts.map +1 -0
  60. package/dist/agent-runners/memory-afs.mjs +326 -0
  61. package/dist/agent-runners/memory-afs.mjs.map +1 -0
  62. package/dist/agent-runners/memory-archive.cjs +263 -0
  63. package/dist/agent-runners/memory-archive.d.cts +47 -0
  64. package/dist/agent-runners/memory-archive.d.cts.map +1 -0
  65. package/dist/agent-runners/memory-archive.d.mts +47 -0
  66. package/dist/agent-runners/memory-archive.d.mts.map +1 -0
  67. package/dist/agent-runners/memory-archive.mjs +257 -0
  68. package/dist/agent-runners/memory-archive.mjs.map +1 -0
  69. package/dist/agent-runners/orchestrator-runner.cjs +302 -0
  70. package/dist/agent-runners/orchestrator-runner.d.cts +23 -0
  71. package/dist/agent-runners/orchestrator-runner.d.cts.map +1 -0
  72. package/dist/agent-runners/orchestrator-runner.d.mts +23 -0
  73. package/dist/agent-runners/orchestrator-runner.d.mts.map +1 -0
  74. package/dist/agent-runners/orchestrator-runner.mjs +303 -0
  75. package/dist/agent-runners/orchestrator-runner.mjs.map +1 -0
  76. package/dist/agent-runners/orchestrator-state.cjs +68 -0
  77. package/dist/agent-runners/orchestrator-state.d.cts +23 -0
  78. package/dist/agent-runners/orchestrator-state.d.cts.map +1 -0
  79. package/dist/agent-runners/orchestrator-state.d.mts +23 -0
  80. package/dist/agent-runners/orchestrator-state.d.mts.map +1 -0
  81. package/dist/agent-runners/orchestrator-state.mjs +65 -0
  82. package/dist/agent-runners/orchestrator-state.mjs.map +1 -0
  83. package/dist/agent-runners/retry.cjs +30 -0
  84. package/dist/agent-runners/retry.d.cts +13 -0
  85. package/dist/agent-runners/retry.d.cts.map +1 -0
  86. package/dist/agent-runners/retry.d.mts +13 -0
  87. package/dist/agent-runners/retry.d.mts.map +1 -0
  88. package/dist/agent-runners/retry.mjs +30 -0
  89. package/dist/agent-runners/retry.mjs.map +1 -0
  90. package/dist/agent-runners/scratchpad-afs.cjs +276 -0
  91. package/dist/agent-runners/scratchpad-afs.d.cts +103 -0
  92. package/dist/agent-runners/scratchpad-afs.d.cts.map +1 -0
  93. package/dist/agent-runners/scratchpad-afs.d.mts +103 -0
  94. package/dist/agent-runners/scratchpad-afs.d.mts.map +1 -0
  95. package/dist/agent-runners/scratchpad-afs.mjs +271 -0
  96. package/dist/agent-runners/scratchpad-afs.mjs.map +1 -0
  97. package/dist/agent-runners/stream-router.cjs +72 -0
  98. package/dist/agent-runners/stream-router.d.cts +22 -0
  99. package/dist/agent-runners/stream-router.d.cts.map +1 -0
  100. package/dist/agent-runners/stream-router.d.mts +22 -0
  101. package/dist/agent-runners/stream-router.d.mts.map +1 -0
  102. package/dist/agent-runners/stream-router.mjs +72 -0
  103. package/dist/agent-runners/stream-router.mjs.map +1 -0
  104. package/dist/agent-runners/system-prompt.cjs +35 -0
  105. package/dist/agent-runners/system-prompt.d.cts +17 -0
  106. package/dist/agent-runners/system-prompt.d.cts.map +1 -0
  107. package/dist/agent-runners/system-prompt.d.mts +17 -0
  108. package/dist/agent-runners/system-prompt.d.mts.map +1 -0
  109. package/dist/agent-runners/system-prompt.mjs +34 -0
  110. package/dist/agent-runners/system-prompt.mjs.map +1 -0
  111. package/dist/agent-runners/team-modes/aggregates.cjs +36 -0
  112. package/dist/agent-runners/team-modes/aggregates.mjs +35 -0
  113. package/dist/agent-runners/team-modes/aggregates.mjs.map +1 -0
  114. package/dist/agent-runners/team-modes/iterate.cjs +46 -0
  115. package/dist/agent-runners/team-modes/iterate.mjs +47 -0
  116. package/dist/agent-runners/team-modes/iterate.mjs.map +1 -0
  117. package/dist/agent-runners/team-modes/parallel.cjs +59 -0
  118. package/dist/agent-runners/team-modes/parallel.mjs +60 -0
  119. package/dist/agent-runners/team-modes/parallel.mjs.map +1 -0
  120. package/dist/agent-runners/team-modes/reflection.cjs +155 -0
  121. package/dist/agent-runners/team-modes/reflection.mjs +156 -0
  122. package/dist/agent-runners/team-modes/reflection.mjs.map +1 -0
  123. package/dist/agent-runners/team-modes/sequential.cjs +63 -0
  124. package/dist/agent-runners/team-modes/sequential.mjs +64 -0
  125. package/dist/agent-runners/team-modes/sequential.mjs.map +1 -0
  126. package/dist/agent-runners/team-modes/utils.cjs +39 -0
  127. package/dist/agent-runners/team-modes/utils.mjs +36 -0
  128. package/dist/agent-runners/team-modes/utils.mjs.map +1 -0
  129. package/dist/agent-runners/team-runner.cjs +277 -0
  130. package/dist/agent-runners/team-runner.d.cts +34 -0
  131. package/dist/agent-runners/team-runner.d.cts.map +1 -0
  132. package/dist/agent-runners/team-runner.d.mts +34 -0
  133. package/dist/agent-runners/team-runner.d.mts.map +1 -0
  134. package/dist/agent-runners/team-runner.mjs +278 -0
  135. package/dist/agent-runners/team-runner.mjs.map +1 -0
  136. package/dist/agent-runners/template-context.cjs +32 -0
  137. package/dist/agent-runners/template-context.d.cts +43 -0
  138. package/dist/agent-runners/template-context.d.cts.map +1 -0
  139. package/dist/agent-runners/template-context.d.mts +43 -0
  140. package/dist/agent-runners/template-context.d.mts.map +1 -0
  141. package/dist/agent-runners/template-context.mjs +33 -0
  142. package/dist/agent-runners/template-context.mjs.map +1 -0
  143. package/dist/index.cjs +123 -0
  144. package/dist/index.d.cts +29 -0
  145. package/dist/index.d.mts +29 -0
  146. package/dist/index.mjs +29 -0
  147. package/dist/program/agent-action.cjs +496 -0
  148. package/dist/program/agent-action.d.cts +42 -0
  149. package/dist/program/agent-action.d.cts.map +1 -0
  150. package/dist/program/agent-action.d.mts +42 -0
  151. package/dist/program/agent-action.d.mts.map +1 -0
  152. package/dist/program/agent-action.mjs +497 -0
  153. package/dist/program/agent-action.mjs.map +1 -0
  154. package/dist/program/agent-messages-tools.cjs +137 -0
  155. package/dist/program/agent-messages-tools.d.cts +59 -0
  156. package/dist/program/agent-messages-tools.d.cts.map +1 -0
  157. package/dist/program/agent-messages-tools.d.mts +59 -0
  158. package/dist/program/agent-messages-tools.d.mts.map +1 -0
  159. package/dist/program/agent-messages-tools.mjs +130 -0
  160. package/dist/program/agent-messages-tools.mjs.map +1 -0
  161. package/dist/program/agent-runner-registry.cjs +314 -0
  162. package/dist/program/agent-runner-registry.d.cts +52 -0
  163. package/dist/program/agent-runner-registry.d.cts.map +1 -0
  164. package/dist/program/agent-runner-registry.d.mts +52 -0
  165. package/dist/program/agent-runner-registry.d.mts.map +1 -0
  166. package/dist/program/agent-runner-registry.mjs +313 -0
  167. package/dist/program/agent-runner-registry.mjs.map +1 -0
  168. package/dist/program/agent-subscribe.cjs +72 -0
  169. package/dist/program/agent-subscribe.d.cts +18 -0
  170. package/dist/program/agent-subscribe.d.cts.map +1 -0
  171. package/dist/program/agent-subscribe.d.mts +18 -0
  172. package/dist/program/agent-subscribe.d.mts.map +1 -0
  173. package/dist/program/agent-subscribe.mjs +73 -0
  174. package/dist/program/agent-subscribe.mjs.map +1 -0
  175. package/dist/program/agent-tools.cjs +135 -0
  176. package/dist/program/agent-tools.d.cts +35 -0
  177. package/dist/program/agent-tools.d.cts.map +1 -0
  178. package/dist/program/agent-tools.d.mts +35 -0
  179. package/dist/program/agent-tools.d.mts.map +1 -0
  180. package/dist/program/agent-tools.mjs +136 -0
  181. package/dist/program/agent-tools.mjs.map +1 -0
  182. package/dist/program/manifest-nesting.cjs +57 -0
  183. package/dist/program/manifest-nesting.d.cts +13 -0
  184. package/dist/program/manifest-nesting.d.cts.map +1 -0
  185. package/dist/program/manifest-nesting.d.mts +13 -0
  186. package/dist/program/manifest-nesting.d.mts.map +1 -0
  187. package/dist/program/manifest-nesting.mjs +57 -0
  188. package/dist/program/manifest-nesting.mjs.map +1 -0
  189. package/dist/program/result-contract.cjs +55 -0
  190. package/dist/program/result-contract.d.cts +23 -0
  191. package/dist/program/result-contract.d.cts.map +1 -0
  192. package/dist/program/result-contract.d.mts +23 -0
  193. package/dist/program/result-contract.d.mts.map +1 -0
  194. package/dist/program/result-contract.mjs +51 -0
  195. package/dist/program/result-contract.mjs.map +1 -0
  196. package/dist/program/runner-error.cjs +71 -0
  197. package/dist/program/runner-error.d.cts +25 -0
  198. package/dist/program/runner-error.d.cts.map +1 -0
  199. package/dist/program/runner-error.d.mts +25 -0
  200. package/dist/program/runner-error.d.mts.map +1 -0
  201. package/dist/program/runner-error.mjs +72 -0
  202. package/dist/program/runner-error.mjs.map +1 -0
  203. package/dist/program/scoped-afs.cjs +538 -0
  204. package/dist/program/scoped-afs.d.cts +245 -0
  205. package/dist/program/scoped-afs.d.cts.map +1 -0
  206. package/dist/program/scoped-afs.d.mts +245 -0
  207. package/dist/program/scoped-afs.d.mts.map +1 -0
  208. package/dist/program/scoped-afs.mjs +531 -0
  209. package/dist/program/scoped-afs.mjs.map +1 -0
  210. package/dist/program/session-hierarchy.cjs +1 -0
  211. package/dist/program/session-hierarchy.d.cts +2 -0
  212. package/dist/program/session-hierarchy.d.mts +2 -0
  213. package/dist/program/session-hierarchy.mjs +3 -0
  214. package/dist/program/tool-discovery/ash-frontmatter.cjs +83 -0
  215. package/dist/program/tool-discovery/ash-frontmatter.d.cts +21 -0
  216. package/dist/program/tool-discovery/ash-frontmatter.d.cts.map +1 -0
  217. package/dist/program/tool-discovery/ash-frontmatter.d.mts +21 -0
  218. package/dist/program/tool-discovery/ash-frontmatter.d.mts.map +1 -0
  219. package/dist/program/tool-discovery/ash-frontmatter.mjs +84 -0
  220. package/dist/program/tool-discovery/ash-frontmatter.mjs.map +1 -0
  221. package/dist/program/tool-discovery/dot-path-resolver.cjs +60 -0
  222. package/dist/program/tool-discovery/dot-path-resolver.mjs +61 -0
  223. package/dist/program/tool-discovery/dot-path-resolver.mjs.map +1 -0
  224. package/dist/program/tool-discovery/skill-gate.cjs +36 -0
  225. package/dist/program/tool-discovery/skill-gate.d.cts +10 -0
  226. package/dist/program/tool-discovery/skill-gate.d.cts.map +1 -0
  227. package/dist/program/tool-discovery/skill-gate.d.mts +10 -0
  228. package/dist/program/tool-discovery/skill-gate.d.mts.map +1 -0
  229. package/dist/program/tool-discovery/skill-gate.mjs +35 -0
  230. package/dist/program/tool-discovery/skill-gate.mjs.map +1 -0
  231. package/dist/providers/basic/ash/dist/index.cjs +1809 -0
  232. package/dist/providers/basic/ash/dist/index.mjs +1807 -0
  233. package/dist/providers/basic/ash/dist/index.mjs.map +1 -0
  234. package/package.json +67 -0
@@ -0,0 +1,302 @@
1
+ const require_scratchpad_afs = require('./scratchpad-afs.cjs');
2
+ const require_scoped_afs = require('../program/scoped-afs.cjs');
3
+ const require_agent_runner_registry = require('../program/agent-runner-registry.cjs');
4
+ const require_orchestrator_state = require('./orchestrator-state.cjs');
5
+ let ufo = require("ufo");
6
+ let _aigne_afs = require("@aigne/afs");
7
+
8
+ //#region src/agent-runners/orchestrator-runner.ts
9
+ const MAX_ITERATIONS_CEILING = 30;
10
+ const DEFAULT_KEEP_RECENT = 20;
11
+ const DEFAULT_MAX_TOKENS = 1e5;
12
+ function extractPlannerOutput(raw) {
13
+ if (!raw || typeof raw !== "object") return null;
14
+ const obj = raw;
15
+ const candidates = [
16
+ obj,
17
+ obj.data?.result,
18
+ obj.result,
19
+ obj.data
20
+ ];
21
+ for (const c of candidates) {
22
+ if (!c) continue;
23
+ let parsed = c;
24
+ if (typeof parsed === "string") try {
25
+ parsed = JSON.parse(parsed);
26
+ } catch {
27
+ continue;
28
+ }
29
+ if (typeof parsed?.reply === "string") try {
30
+ parsed = JSON.parse(parsed.reply);
31
+ } catch {
32
+ continue;
33
+ }
34
+ if (parsed && typeof parsed === "object" && ("finished" in parsed || "nextTasks" in parsed)) return parsed;
35
+ }
36
+ return null;
37
+ }
38
+ function extractResult(raw) {
39
+ if (!raw || typeof raw !== "object") return raw;
40
+ const obj = raw;
41
+ return obj.data?.result ?? obj.result ?? obj.data ?? obj;
42
+ }
43
+ async function renderObjective(template, input, manifest, mountPath, afs) {
44
+ try {
45
+ return await (await import("@aigne/ash/src/prompt/template.js")).renderTemplate(template, {
46
+ $input: input,
47
+ $manifest: manifest,
48
+ $now: (/* @__PURE__ */ new Date()).toISOString()
49
+ }, {
50
+ blockletRoot: mountPath,
51
+ afs
52
+ });
53
+ } catch {
54
+ return template;
55
+ }
56
+ }
57
+ function metaResult(iterations, subRuns, maxHit) {
58
+ return {
59
+ iterations,
60
+ sub_runs: subRuns,
61
+ max_iterations_hit: maxHit || void 0
62
+ };
63
+ }
64
+ async function dispatchSubAgent(context, memberPath, input, session, parentScope) {
65
+ const agentYmlPath = (0, ufo.joinURL)((0, _aigne_afs.resolveMemberPath)(memberPath), "agent.yml");
66
+ const read = await context.afs.read(agentYmlPath);
67
+ const subManifest = (0, _aigne_afs.parseAgentManifest)(String(read?.data?.content ?? ""));
68
+ const subContext = {
69
+ afs: require_scoped_afs.projectForChild(parentScope, {
70
+ memberPath,
71
+ childManifest: subManifest,
72
+ session
73
+ }),
74
+ mountPath: "/packages",
75
+ manifest: subManifest,
76
+ session,
77
+ parentSession: parentScope.scope.session,
78
+ chain: context.chain
79
+ };
80
+ const parentInputKey = context.manifest.inputKey ?? "message";
81
+ const subInputKey = subManifest.inputKey ?? "message";
82
+ const bridgedInput = subInputKey !== parentInputKey && input[subInputKey] === void 0 ? bridgeInput(input, parentInputKey, subInputKey) : input;
83
+ return require_agent_runner_registry.getRunnerForType(subManifest.type)(bridgedInput, subContext);
84
+ }
85
+ function bridgeInput(input, parentInputKey, subInputKey) {
86
+ const taskValue = input[parentInputKey] ?? input.message ?? input.task ?? input.text;
87
+ if (taskValue === void 0) return input;
88
+ const { [parentInputKey]: _drop, ...rest } = input;
89
+ return {
90
+ ...rest,
91
+ [subInputKey]: taskValue
92
+ };
93
+ }
94
+ async function runOrchestratorAgent(input, context) {
95
+ const { manifest, mountPath } = context;
96
+ const session = require_scoped_afs.runnerSession(context.afs, context.session);
97
+ const scopedAfs = require_scoped_afs.ensureScopeWithMounts(context.afs, {
98
+ session: session ?? "root",
99
+ manifestTools: require_scoped_afs.normalizeManifestTools(manifest),
100
+ scratchRetention: manifest.scratch ? "session" : "run",
101
+ budget: manifest.budget
102
+ });
103
+ const afs = scopedAfs;
104
+ const scopedContext = {
105
+ ...context,
106
+ afs: scopedAfs,
107
+ session
108
+ };
109
+ const inputKey = manifest.inputKey ?? "message";
110
+ const sm = manifest.stateManagement ?? {};
111
+ const maxIter = Math.min(sm.maxIterations ?? MAX_ITERATIONS_CEILING, MAX_ITERATIONS_CEILING);
112
+ const keepRecent = sm.keepRecent ?? DEFAULT_KEEP_RECENT;
113
+ const maxTokens = sm.maxTokens ?? DEFAULT_MAX_TOKENS;
114
+ const concurrency = Math.min(manifest.concurrency ?? 10, 10);
115
+ const objFile = manifest.objective ?? "objective.md";
116
+ const objCandidates = [(0, ufo.joinURL)(mountPath, objFile), (0, ufo.joinURL)("/packages", objFile)];
117
+ let objective;
118
+ let lastObjPath = objCandidates[0];
119
+ for (const candidate of objCandidates) {
120
+ lastObjPath = candidate;
121
+ try {
122
+ const raw = await afs.read(candidate);
123
+ const content = typeof raw === "string" ? raw : String(raw?.data?.content ?? raw?.data ?? raw ?? "");
124
+ if (content) {
125
+ objective = content;
126
+ break;
127
+ }
128
+ } catch {}
129
+ }
130
+ if (!objective) return {
131
+ success: false,
132
+ error: {
133
+ code: "OBJECTIVE_NOT_FOUND",
134
+ message: `Cannot read objective at ${lastObjPath}`
135
+ }
136
+ };
137
+ objective = await renderObjective(objective, input, {
138
+ name: manifest.name,
139
+ type: manifest.type
140
+ }, mountPath, afs);
141
+ const statePath = session ? (0, ufo.joinURL)(require_scratchpad_afs.SCRATCH_SCOPE_ROOT, "state.yml") : null;
142
+ let state = require_orchestrator_state.createInitialState(objective);
143
+ if (statePath) try {
144
+ const raw = await afs.read(statePath);
145
+ const loaded = require_orchestrator_state.deserializeState(typeof raw === "string" ? raw : String(raw?.data?.content ?? raw ?? ""));
146
+ if (loaded) state = loaded;
147
+ } catch {}
148
+ const parentScopeForSubs = require_scoped_afs.resolveParentAgentScope({
149
+ afs: scopedAfs,
150
+ manifest,
151
+ session
152
+ });
153
+ let subRuns = 0;
154
+ const plannerPath = manifest.planner?.path ?? null;
155
+ const workerPath = manifest.worker?.path ?? null;
156
+ const completerPath = manifest.completer?.path ?? null;
157
+ let lastPlanKey = "";
158
+ for (let i = state.iterations; i < maxIter; i++) {
159
+ state.iterations = i + 1;
160
+ state = require_orchestrator_state.truncateHistory(state, maxTokens, keepRecent);
161
+ let plan;
162
+ if (plannerPath) try {
163
+ const parsed = extractPlannerOutput(await dispatchSubAgent(scopedContext, plannerPath, { [inputKey]: JSON.stringify({
164
+ objective: state.objective,
165
+ history: state.history,
166
+ iteration: i,
167
+ summaries: state.summaries
168
+ }) }, session ? `${session}-planner-${i}` : void 0, parentScopeForSubs));
169
+ if (!parsed) return {
170
+ success: false,
171
+ error: {
172
+ code: "PLANNER_OUTPUT_INVALID",
173
+ message: "Planner must return 'finished' or 'nextTasks'"
174
+ },
175
+ _meta: metaResult(state.iterations, subRuns)
176
+ };
177
+ plan = parsed;
178
+ } catch (e) {
179
+ return {
180
+ success: false,
181
+ error: {
182
+ code: "PLANNER_ERROR",
183
+ message: e.message ?? String(e)
184
+ },
185
+ _meta: metaResult(state.iterations, subRuns)
186
+ };
187
+ }
188
+ else plan = state.history.length > 0 ? { finished: true } : { nextTasks: [{ description: String(input[inputKey] ?? state.objective) }] };
189
+ if (plan.finished) break;
190
+ if (!plan.nextTasks?.length) return {
191
+ success: false,
192
+ error: {
193
+ code: "PLANNER_OUTPUT_INVALID",
194
+ message: "Planner must return 'finished' or 'nextTasks'"
195
+ },
196
+ _meta: metaResult(state.iterations, subRuns)
197
+ };
198
+ const planKey = JSON.stringify(plan.nextTasks.map((t) => t.description).sort());
199
+ if (planKey === lastPlanKey) return {
200
+ success: false,
201
+ error: {
202
+ code: "CIRCULAR_PLANNER",
203
+ message: "Planner is scheduling the same tasks repeatedly"
204
+ },
205
+ _meta: metaResult(state.iterations, subRuns)
206
+ };
207
+ lastPlanKey = planKey;
208
+ const tasks = plan.nextTasks;
209
+ const runWorker = async (task) => {
210
+ subRuns++;
211
+ if (!workerPath) return {
212
+ description: task.description,
213
+ result: null,
214
+ error: "NO_WORKER"
215
+ };
216
+ try {
217
+ const raw = await dispatchSubAgent(scopedContext, workerPath, {
218
+ ...task.input ?? {},
219
+ [inputKey]: task.description
220
+ }, session ? `${session}-w-${subRuns}` : void 0, parentScopeForSubs);
221
+ return {
222
+ description: task.description,
223
+ result: extractResult(raw)
224
+ };
225
+ } catch (e) {
226
+ return {
227
+ description: task.description,
228
+ result: null,
229
+ error: e.message ?? String(e)
230
+ };
231
+ }
232
+ };
233
+ if (plan.parallelTasks && tasks.length > 1) for (let j = 0; j < tasks.length; j += concurrency) {
234
+ const batch = tasks.slice(j, j + concurrency);
235
+ const results = await Promise.all(batch.map(runWorker));
236
+ for (const r of results) state.history.push({
237
+ iteration: i,
238
+ description: r.description,
239
+ result: r.result,
240
+ error: r.error,
241
+ ts: (/* @__PURE__ */ new Date()).toISOString()
242
+ });
243
+ }
244
+ else for (const task of tasks) {
245
+ const r = await runWorker(task);
246
+ state.history.push({
247
+ iteration: i,
248
+ description: r.description,
249
+ result: r.result,
250
+ error: r.error,
251
+ ts: (/* @__PURE__ */ new Date()).toISOString()
252
+ });
253
+ }
254
+ if (statePath) try {
255
+ await afs.write(statePath, { content: require_orchestrator_state.serializeState(state, keepRecent) });
256
+ } catch {}
257
+ }
258
+ const maxHit = state.iterations >= maxIter;
259
+ let finalResult;
260
+ if (completerPath) try {
261
+ finalResult = extractResult(await dispatchSubAgent(scopedContext, completerPath, { [inputKey]: JSON.stringify({
262
+ objective: state.objective,
263
+ history: state.history,
264
+ iterations: state.iterations,
265
+ max_iterations_hit: maxHit
266
+ }) }, session ? `${session}-completer` : void 0, parentScopeForSubs));
267
+ } catch (e) {
268
+ return {
269
+ success: false,
270
+ error: {
271
+ code: "COMPLETER_ERROR",
272
+ message: e.message ?? String(e)
273
+ },
274
+ _meta: {
275
+ iterations: state.iterations,
276
+ sub_runs: subRuns,
277
+ max_iterations_hit: maxHit || void 0,
278
+ keep_recent: keepRecent
279
+ }
280
+ };
281
+ }
282
+ else finalResult = state.history[state.history.length - 1]?.result ?? null;
283
+ if (statePath) try {
284
+ await afs.write(statePath, { content: require_orchestrator_state.serializeState(state, keepRecent) });
285
+ } catch {}
286
+ const result = {
287
+ success: true,
288
+ data: { result: finalResult },
289
+ _meta: {
290
+ iterations: state.iterations,
291
+ sub_runs: subRuns,
292
+ max_iterations_hit: maxHit || void 0,
293
+ truncated: state.truncated || void 0,
294
+ keep_recent: keepRecent
295
+ }
296
+ };
297
+ if (manifest.outputKey && finalResult != null) result.result = { [manifest.outputKey]: finalResult };
298
+ return result;
299
+ }
300
+
301
+ //#endregion
302
+ exports.runOrchestratorAgent = runOrchestratorAgent;
@@ -0,0 +1,23 @@
1
+ import { RunnerContext } from "../program/agent-runner-registry.cjs";
2
+
3
+ //#region src/agent-runners/orchestrator-runner.d.ts
4
+ interface OrchestratorRunnerResult {
5
+ success: boolean;
6
+ data?: Record<string, unknown>;
7
+ result?: Record<string, unknown>;
8
+ error?: {
9
+ code: string;
10
+ message: string;
11
+ };
12
+ _meta?: {
13
+ iterations: number;
14
+ sub_runs: number;
15
+ max_iterations_hit?: boolean;
16
+ truncated?: number;
17
+ keep_recent?: number;
18
+ };
19
+ }
20
+ declare function runOrchestratorAgent(input: Record<string, unknown>, context: RunnerContext): Promise<OrchestratorRunnerResult>;
21
+ //#endregion
22
+ export { OrchestratorRunnerResult, runOrchestratorAgent };
23
+ //# sourceMappingURL=orchestrator-runner.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator-runner.d.cts","names":[],"sources":["../../src/agent-runners/orchestrator-runner.ts"],"mappings":";;;UAoBiB,wBAAA;EACf,OAAA;EACA,IAAA,GAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA;IAAU,IAAA;IAAc,OAAA;EAAA;EACxB,KAAA;IACE,UAAA;IACA,QAAA;IACA,kBAAA;IACA,SAAA;IACA,WAAA;EAAA;AAAA;AAAA,iBA4IkB,oBAAA,CACpB,KAAA,EAAO,MAAA,mBACP,OAAA,EAAS,aAAA,GACR,OAAA,CAAQ,wBAAA"}
@@ -0,0 +1,23 @@
1
+ import { RunnerContext } from "../program/agent-runner-registry.mjs";
2
+
3
+ //#region src/agent-runners/orchestrator-runner.d.ts
4
+ interface OrchestratorRunnerResult {
5
+ success: boolean;
6
+ data?: Record<string, unknown>;
7
+ result?: Record<string, unknown>;
8
+ error?: {
9
+ code: string;
10
+ message: string;
11
+ };
12
+ _meta?: {
13
+ iterations: number;
14
+ sub_runs: number;
15
+ max_iterations_hit?: boolean;
16
+ truncated?: number;
17
+ keep_recent?: number;
18
+ };
19
+ }
20
+ declare function runOrchestratorAgent(input: Record<string, unknown>, context: RunnerContext): Promise<OrchestratorRunnerResult>;
21
+ //#endregion
22
+ export { OrchestratorRunnerResult, runOrchestratorAgent };
23
+ //# sourceMappingURL=orchestrator-runner.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator-runner.d.mts","names":[],"sources":["../../src/agent-runners/orchestrator-runner.ts"],"mappings":";;;UAoBiB,wBAAA;EACf,OAAA;EACA,IAAA,GAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA;IAAU,IAAA;IAAc,OAAA;EAAA;EACxB,KAAA;IACE,UAAA;IACA,QAAA;IACA,kBAAA;IACA,SAAA;IACA,WAAA;EAAA;AAAA;AAAA,iBA4IkB,oBAAA,CACpB,KAAA,EAAO,MAAA,mBACP,OAAA,EAAS,aAAA,GACR,OAAA,CAAQ,wBAAA"}
@@ -0,0 +1,303 @@
1
+ import { SCRATCH_SCOPE_ROOT } from "./scratchpad-afs.mjs";
2
+ import { ensureScopeWithMounts, normalizeManifestTools, projectForChild, resolveParentAgentScope, runnerSession } from "../program/scoped-afs.mjs";
3
+ import { getRunnerForType } from "../program/agent-runner-registry.mjs";
4
+ import { createInitialState, deserializeState, serializeState, truncateHistory } from "./orchestrator-state.mjs";
5
+ import { joinURL } from "ufo";
6
+ import { parseAgentManifest, resolveMemberPath } from "@aigne/afs";
7
+
8
+ //#region src/agent-runners/orchestrator-runner.ts
9
+ const MAX_ITERATIONS_CEILING = 30;
10
+ const DEFAULT_KEEP_RECENT = 20;
11
+ const DEFAULT_MAX_TOKENS = 1e5;
12
+ function extractPlannerOutput(raw) {
13
+ if (!raw || typeof raw !== "object") return null;
14
+ const obj = raw;
15
+ const candidates = [
16
+ obj,
17
+ obj.data?.result,
18
+ obj.result,
19
+ obj.data
20
+ ];
21
+ for (const c of candidates) {
22
+ if (!c) continue;
23
+ let parsed = c;
24
+ if (typeof parsed === "string") try {
25
+ parsed = JSON.parse(parsed);
26
+ } catch {
27
+ continue;
28
+ }
29
+ if (typeof parsed?.reply === "string") try {
30
+ parsed = JSON.parse(parsed.reply);
31
+ } catch {
32
+ continue;
33
+ }
34
+ if (parsed && typeof parsed === "object" && ("finished" in parsed || "nextTasks" in parsed)) return parsed;
35
+ }
36
+ return null;
37
+ }
38
+ function extractResult(raw) {
39
+ if (!raw || typeof raw !== "object") return raw;
40
+ const obj = raw;
41
+ return obj.data?.result ?? obj.result ?? obj.data ?? obj;
42
+ }
43
+ async function renderObjective(template, input, manifest, mountPath, afs) {
44
+ try {
45
+ return await (await import("@aigne/ash/src/prompt/template.js")).renderTemplate(template, {
46
+ $input: input,
47
+ $manifest: manifest,
48
+ $now: (/* @__PURE__ */ new Date()).toISOString()
49
+ }, {
50
+ blockletRoot: mountPath,
51
+ afs
52
+ });
53
+ } catch {
54
+ return template;
55
+ }
56
+ }
57
+ function metaResult(iterations, subRuns, maxHit) {
58
+ return {
59
+ iterations,
60
+ sub_runs: subRuns,
61
+ max_iterations_hit: maxHit || void 0
62
+ };
63
+ }
64
+ async function dispatchSubAgent(context, memberPath, input, session, parentScope) {
65
+ const agentYmlPath = joinURL(resolveMemberPath(memberPath), "agent.yml");
66
+ const read = await context.afs.read(agentYmlPath);
67
+ const subManifest = parseAgentManifest(String(read?.data?.content ?? ""));
68
+ const subContext = {
69
+ afs: projectForChild(parentScope, {
70
+ memberPath,
71
+ childManifest: subManifest,
72
+ session
73
+ }),
74
+ mountPath: "/packages",
75
+ manifest: subManifest,
76
+ session,
77
+ parentSession: parentScope.scope.session,
78
+ chain: context.chain
79
+ };
80
+ const parentInputKey = context.manifest.inputKey ?? "message";
81
+ const subInputKey = subManifest.inputKey ?? "message";
82
+ const bridgedInput = subInputKey !== parentInputKey && input[subInputKey] === void 0 ? bridgeInput(input, parentInputKey, subInputKey) : input;
83
+ return getRunnerForType(subManifest.type)(bridgedInput, subContext);
84
+ }
85
+ function bridgeInput(input, parentInputKey, subInputKey) {
86
+ const taskValue = input[parentInputKey] ?? input.message ?? input.task ?? input.text;
87
+ if (taskValue === void 0) return input;
88
+ const { [parentInputKey]: _drop, ...rest } = input;
89
+ return {
90
+ ...rest,
91
+ [subInputKey]: taskValue
92
+ };
93
+ }
94
+ async function runOrchestratorAgent(input, context) {
95
+ const { manifest, mountPath } = context;
96
+ const session = runnerSession(context.afs, context.session);
97
+ const scopedAfs = ensureScopeWithMounts(context.afs, {
98
+ session: session ?? "root",
99
+ manifestTools: normalizeManifestTools(manifest),
100
+ scratchRetention: manifest.scratch ? "session" : "run",
101
+ budget: manifest.budget
102
+ });
103
+ const afs = scopedAfs;
104
+ const scopedContext = {
105
+ ...context,
106
+ afs: scopedAfs,
107
+ session
108
+ };
109
+ const inputKey = manifest.inputKey ?? "message";
110
+ const sm = manifest.stateManagement ?? {};
111
+ const maxIter = Math.min(sm.maxIterations ?? MAX_ITERATIONS_CEILING, MAX_ITERATIONS_CEILING);
112
+ const keepRecent = sm.keepRecent ?? DEFAULT_KEEP_RECENT;
113
+ const maxTokens = sm.maxTokens ?? DEFAULT_MAX_TOKENS;
114
+ const concurrency = Math.min(manifest.concurrency ?? 10, 10);
115
+ const objFile = manifest.objective ?? "objective.md";
116
+ const objCandidates = [joinURL(mountPath, objFile), joinURL("/packages", objFile)];
117
+ let objective;
118
+ let lastObjPath = objCandidates[0];
119
+ for (const candidate of objCandidates) {
120
+ lastObjPath = candidate;
121
+ try {
122
+ const raw = await afs.read(candidate);
123
+ const content = typeof raw === "string" ? raw : String(raw?.data?.content ?? raw?.data ?? raw ?? "");
124
+ if (content) {
125
+ objective = content;
126
+ break;
127
+ }
128
+ } catch {}
129
+ }
130
+ if (!objective) return {
131
+ success: false,
132
+ error: {
133
+ code: "OBJECTIVE_NOT_FOUND",
134
+ message: `Cannot read objective at ${lastObjPath}`
135
+ }
136
+ };
137
+ objective = await renderObjective(objective, input, {
138
+ name: manifest.name,
139
+ type: manifest.type
140
+ }, mountPath, afs);
141
+ const statePath = session ? joinURL(SCRATCH_SCOPE_ROOT, "state.yml") : null;
142
+ let state = createInitialState(objective);
143
+ if (statePath) try {
144
+ const raw = await afs.read(statePath);
145
+ const loaded = deserializeState(typeof raw === "string" ? raw : String(raw?.data?.content ?? raw ?? ""));
146
+ if (loaded) state = loaded;
147
+ } catch {}
148
+ const parentScopeForSubs = resolveParentAgentScope({
149
+ afs: scopedAfs,
150
+ manifest,
151
+ session
152
+ });
153
+ let subRuns = 0;
154
+ const plannerPath = manifest.planner?.path ?? null;
155
+ const workerPath = manifest.worker?.path ?? null;
156
+ const completerPath = manifest.completer?.path ?? null;
157
+ let lastPlanKey = "";
158
+ for (let i = state.iterations; i < maxIter; i++) {
159
+ state.iterations = i + 1;
160
+ state = truncateHistory(state, maxTokens, keepRecent);
161
+ let plan;
162
+ if (plannerPath) try {
163
+ const parsed = extractPlannerOutput(await dispatchSubAgent(scopedContext, plannerPath, { [inputKey]: JSON.stringify({
164
+ objective: state.objective,
165
+ history: state.history,
166
+ iteration: i,
167
+ summaries: state.summaries
168
+ }) }, session ? `${session}-planner-${i}` : void 0, parentScopeForSubs));
169
+ if (!parsed) return {
170
+ success: false,
171
+ error: {
172
+ code: "PLANNER_OUTPUT_INVALID",
173
+ message: "Planner must return 'finished' or 'nextTasks'"
174
+ },
175
+ _meta: metaResult(state.iterations, subRuns)
176
+ };
177
+ plan = parsed;
178
+ } catch (e) {
179
+ return {
180
+ success: false,
181
+ error: {
182
+ code: "PLANNER_ERROR",
183
+ message: e.message ?? String(e)
184
+ },
185
+ _meta: metaResult(state.iterations, subRuns)
186
+ };
187
+ }
188
+ else plan = state.history.length > 0 ? { finished: true } : { nextTasks: [{ description: String(input[inputKey] ?? state.objective) }] };
189
+ if (plan.finished) break;
190
+ if (!plan.nextTasks?.length) return {
191
+ success: false,
192
+ error: {
193
+ code: "PLANNER_OUTPUT_INVALID",
194
+ message: "Planner must return 'finished' or 'nextTasks'"
195
+ },
196
+ _meta: metaResult(state.iterations, subRuns)
197
+ };
198
+ const planKey = JSON.stringify(plan.nextTasks.map((t) => t.description).sort());
199
+ if (planKey === lastPlanKey) return {
200
+ success: false,
201
+ error: {
202
+ code: "CIRCULAR_PLANNER",
203
+ message: "Planner is scheduling the same tasks repeatedly"
204
+ },
205
+ _meta: metaResult(state.iterations, subRuns)
206
+ };
207
+ lastPlanKey = planKey;
208
+ const tasks = plan.nextTasks;
209
+ const runWorker = async (task) => {
210
+ subRuns++;
211
+ if (!workerPath) return {
212
+ description: task.description,
213
+ result: null,
214
+ error: "NO_WORKER"
215
+ };
216
+ try {
217
+ const raw = await dispatchSubAgent(scopedContext, workerPath, {
218
+ ...task.input ?? {},
219
+ [inputKey]: task.description
220
+ }, session ? `${session}-w-${subRuns}` : void 0, parentScopeForSubs);
221
+ return {
222
+ description: task.description,
223
+ result: extractResult(raw)
224
+ };
225
+ } catch (e) {
226
+ return {
227
+ description: task.description,
228
+ result: null,
229
+ error: e.message ?? String(e)
230
+ };
231
+ }
232
+ };
233
+ if (plan.parallelTasks && tasks.length > 1) for (let j = 0; j < tasks.length; j += concurrency) {
234
+ const batch = tasks.slice(j, j + concurrency);
235
+ const results = await Promise.all(batch.map(runWorker));
236
+ for (const r of results) state.history.push({
237
+ iteration: i,
238
+ description: r.description,
239
+ result: r.result,
240
+ error: r.error,
241
+ ts: (/* @__PURE__ */ new Date()).toISOString()
242
+ });
243
+ }
244
+ else for (const task of tasks) {
245
+ const r = await runWorker(task);
246
+ state.history.push({
247
+ iteration: i,
248
+ description: r.description,
249
+ result: r.result,
250
+ error: r.error,
251
+ ts: (/* @__PURE__ */ new Date()).toISOString()
252
+ });
253
+ }
254
+ if (statePath) try {
255
+ await afs.write(statePath, { content: serializeState(state, keepRecent) });
256
+ } catch {}
257
+ }
258
+ const maxHit = state.iterations >= maxIter;
259
+ let finalResult;
260
+ if (completerPath) try {
261
+ finalResult = extractResult(await dispatchSubAgent(scopedContext, completerPath, { [inputKey]: JSON.stringify({
262
+ objective: state.objective,
263
+ history: state.history,
264
+ iterations: state.iterations,
265
+ max_iterations_hit: maxHit
266
+ }) }, session ? `${session}-completer` : void 0, parentScopeForSubs));
267
+ } catch (e) {
268
+ return {
269
+ success: false,
270
+ error: {
271
+ code: "COMPLETER_ERROR",
272
+ message: e.message ?? String(e)
273
+ },
274
+ _meta: {
275
+ iterations: state.iterations,
276
+ sub_runs: subRuns,
277
+ max_iterations_hit: maxHit || void 0,
278
+ keep_recent: keepRecent
279
+ }
280
+ };
281
+ }
282
+ else finalResult = state.history[state.history.length - 1]?.result ?? null;
283
+ if (statePath) try {
284
+ await afs.write(statePath, { content: serializeState(state, keepRecent) });
285
+ } catch {}
286
+ const result = {
287
+ success: true,
288
+ data: { result: finalResult },
289
+ _meta: {
290
+ iterations: state.iterations,
291
+ sub_runs: subRuns,
292
+ max_iterations_hit: maxHit || void 0,
293
+ truncated: state.truncated || void 0,
294
+ keep_recent: keepRecent
295
+ }
296
+ };
297
+ if (manifest.outputKey && finalResult != null) result.result = { [manifest.outputKey]: finalResult };
298
+ return result;
299
+ }
300
+
301
+ //#endregion
302
+ export { runOrchestratorAgent };
303
+ //# sourceMappingURL=orchestrator-runner.mjs.map