@aigne/agent-library 1.3.0 → 1.3.2

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  - chore: release 1.2.0
4
4
 
5
+ ## [1.3.2](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.3.1...agent-library-v1.3.2) (2025-03-27)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **dx:** export models/utils in submodules ([#43](https://github.com/AIGNE-io/aigne-framework/issues/43)) ([bd561b3](https://github.com/AIGNE-io/aigne-framework/commit/bd561b397de816f04c2d63d58538e81fba82fc7f))
11
+
12
+ ## [1.3.1](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.3.0...agent-library-v1.3.1) (2025-03-26)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * **orchestrator:** refactor and enhance orchestrator with step synthesis ([#31](https://github.com/AIGNE-io/aigne-framework/issues/31)) ([ba9fca0](https://github.com/AIGNE-io/aigne-framework/commit/ba9fca04fad71d49c8f4f52172b56668a94ea714))
18
+
5
19
  ## [1.3.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.2.0...agent-library-v1.3.0) (2025-03-24)
6
20
 
7
21
 
@@ -1 +1 @@
1
- export * from "./orchestrator/index.js";
1
+ export {};
package/lib/cjs/index.js CHANGED
@@ -1,17 +1,2 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./orchestrator/index.js"), exports);
@@ -1,29 +1,16 @@
1
1
  import { Agent, type AgentOptions, type Context, type Message } from "@aigne/core";
2
- import { type FullPlanOutput, type Step } from "./orchestrator-prompts.js";
2
+ import { type FullPlanOutput, type StepWithResult } from "./orchestrator-prompts.js";
3
3
  export * from "./orchestrator-prompts.js";
4
- export interface StepResult {
5
- step: Step;
6
- task_results: Array<TaskWithResult>;
7
- }
8
- export interface TaskWithResult {
9
- description: string;
10
- agent: string;
11
- result: string;
12
- }
13
- export interface PlanResult extends Message {
4
+ export interface FullPlanWithResult {
14
5
  objective: string;
15
6
  plan?: FullPlanOutput;
16
- is_complete?: boolean;
7
+ steps: StepWithResult[];
17
8
  result?: string;
18
- step_results: StepResult[];
19
- }
20
- export interface FullPlanInput extends Message {
21
- objective: string;
22
- plan_result: string;
23
- agents: string;
9
+ status?: boolean;
24
10
  }
25
11
  export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
26
12
  maxIterations?: number;
13
+ tasksConcurrency?: number;
27
14
  }
28
15
  export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
29
16
  static from<I extends Message, O extends Message>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
@@ -31,9 +18,10 @@ export declare class OrchestratorAgent<I extends Message = Message, O extends Me
31
18
  private planner;
32
19
  private completer;
33
20
  maxIterations?: number;
21
+ tasksConcurrency?: number;
34
22
  process(input: I, context?: Context): Promise<O>;
23
+ private getFullPlanInput;
35
24
  private getFullPlan;
25
+ private synthesizePlanResult;
36
26
  private executeStep;
37
- private formatPlanResult;
38
- private formatStepsResults;
39
27
  }
@@ -13,33 +13,43 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
16
19
  Object.defineProperty(exports, "__esModule", { value: true });
17
20
  exports.OrchestratorAgent = void 0;
18
21
  const core_1 = require("@aigne/core");
22
+ const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
23
+ const fastq_1 = __importDefault(require("fastq"));
24
+ const zod_1 = require("zod");
19
25
  const orchestrator_prompts_js_1 = require("./orchestrator-prompts.js");
20
26
  const DEFAULT_MAX_ITERATIONS = 30;
27
+ const DEFAULT_TASK_CONCURRENCY = 5;
21
28
  __exportStar(require("./orchestrator-prompts.js"), exports);
22
29
  class OrchestratorAgent extends core_1.Agent {
23
30
  static from(options) {
24
31
  return new OrchestratorAgent(options);
25
32
  }
26
33
  constructor(options) {
34
+ (0, type_utils_js_1.checkArguments)("OrchestratorAgent", orchestratorAgentOptionsSchema, options);
27
35
  super({ ...options });
28
36
  this.maxIterations = options.maxIterations;
37
+ this.tasksConcurrency = options.tasksConcurrency;
29
38
  this.planner = new core_1.AIAgent({
30
39
  name: "llm_orchestration_planner",
31
40
  instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
32
- outputSchema: orchestrator_prompts_js_1.FullPlanSchema,
41
+ outputSchema: () => (0, orchestrator_prompts_js_1.getFullPlanSchema)(this.tools),
33
42
  });
34
43
  this.completer = new core_1.AIAgent({
35
44
  name: "llm_orchestration_completer",
36
- instructions: orchestrator_prompts_js_1.SYNTHESIZE_PLAN_PROMPT_TEMPLATE,
45
+ instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
37
46
  outputSchema: this.outputSchema,
38
47
  });
39
48
  }
40
49
  planner;
41
50
  completer;
42
51
  maxIterations;
52
+ tasksConcurrency;
43
53
  async process(input, context) {
44
54
  const model = context?.model;
45
55
  if (!model)
@@ -49,91 +59,107 @@ class OrchestratorAgent extends core_1.Agent {
49
59
  throw new Error("Objective is required to run OrchestratorAgent");
50
60
  const result = {
51
61
  objective,
52
- step_results: [],
62
+ steps: [],
53
63
  };
54
64
  let iterations = 0;
55
65
  const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
56
66
  while (iterations++ < maxIterations) {
57
- const plan = await this.getFullPlan(objective, result, context);
67
+ const plan = await this.getFullPlan(result, context);
58
68
  result.plan = plan;
59
- if (plan.is_complete) {
60
- return context.call(this.completer, { plan_result: this.formatPlanResult(result) });
69
+ if (plan.isComplete) {
70
+ return this.synthesizePlanResult(result, context);
61
71
  }
62
72
  for (const step of plan.steps) {
63
73
  const stepResult = await this.executeStep(result, step, context);
64
- result.step_results.push(stepResult);
74
+ result.steps.push(stepResult);
65
75
  }
66
76
  }
67
77
  throw new Error(`Max iterations reached: ${maxIterations}`);
68
78
  }
69
- async getFullPlan(objective, planResult, context) {
70
- const agents = this.tools
71
- .map((agent, idx) => `${idx + 1}. Agent Name: ${agent.name}
72
- Description: ${agent.description}
73
- Functions: ${agent.tools.map((tool) => `- ${tool.name} ${tool.description}`).join("\n")}`)
74
- .join("\n");
75
- return context.call(this.planner, {
76
- objective,
77
- plan_result: this.formatPlanResult(planResult),
78
- agents,
79
+ getFullPlanInput(planResult) {
80
+ return {
81
+ objective: planResult.objective,
82
+ steps: planResult.steps,
83
+ plan: {
84
+ status: planResult.status ? "Complete" : "In Progress",
85
+ result: planResult.result || "No results yet",
86
+ },
87
+ agents: this.tools.map((i) => ({
88
+ name: i.name,
89
+ description: i.description,
90
+ tools: i.tools.map((i) => ({ name: i.name, description: i.description })),
91
+ })),
92
+ };
93
+ }
94
+ async getFullPlan(planResult, context) {
95
+ return context.call(this.planner, this.getFullPlanInput(planResult));
96
+ }
97
+ async synthesizePlanResult(planResult, context) {
98
+ return context.call(this.completer, {
99
+ ...this.getFullPlanInput(planResult),
100
+ ...(0, core_1.createMessage)(orchestrator_prompts_js_1.SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE),
79
101
  });
80
102
  }
81
- async executeStep(previousResult, step, context) {
103
+ async executeStep(planResult, step, context) {
104
+ const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
82
105
  const model = context?.model;
83
106
  if (!model)
84
107
  throw new Error("model is required to run OrchestratorAgent");
85
- const taskResults = [];
86
- for (const task of step.tasks) {
108
+ const queue = fastq_1.default.promise(async (task) => {
87
109
  const agent = this.tools.find((agent) => agent.name === task.agent);
88
110
  if (!agent)
89
111
  throw new Error(`Agent ${task.agent} not found`);
90
112
  const prompt = core_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_PROMPT_TEMPLATE).format({
91
- objective: previousResult.objective,
92
- task: task.description,
93
- context: this.formatPlanResult(previousResult),
113
+ objective: planResult.objective,
114
+ step,
115
+ task,
116
+ steps: planResult.steps,
94
117
  });
95
118
  let result;
96
119
  if (agent.isCallable) {
97
- result = JSON.stringify(await context.call(agent, prompt));
120
+ result = getMessageOrJsonString(await context.call(agent, prompt));
98
121
  }
99
122
  else {
100
123
  const executor = core_1.AIAgent.from({
124
+ name: "llm_orchestration_task_executor",
101
125
  instructions: prompt,
102
126
  tools: agent.tools,
103
127
  });
104
- result = JSON.stringify(await context.call(executor, {}));
128
+ result = getMessageOrJsonString(await context.call(executor, {}));
105
129
  }
106
- taskResults.push({ ...task, result });
130
+ return { task, result };
131
+ }, concurrency);
132
+ let results;
133
+ try {
134
+ results = await Promise.all(step.tasks.map((task) => queue.push(task)));
107
135
  }
136
+ catch (error) {
137
+ queue.kill();
138
+ throw error;
139
+ }
140
+ const result = getMessageOrJsonString(await context.call(core_1.AIAgent.from({
141
+ name: "llm_orchestration_step_synthesizer",
142
+ instructions: orchestrator_prompts_js_1.SYNTHESIZE_STEP_PROMPT_TEMPLATE,
143
+ }), { objective: planResult.objective, step, tasks: results }));
144
+ if (!result)
145
+ throw new Error("unexpected empty result from synthesize step's tasks results");
108
146
  return {
109
147
  step,
110
- task_results: taskResults,
148
+ tasks: results,
149
+ result,
111
150
  };
112
151
  }
113
- formatPlanResult(planResult) {
114
- return core_1.PromptTemplate.from(orchestrator_prompts_js_1.PLAN_RESULT_TEMPLATE).format({
115
- plan_objective: planResult.objective,
116
- steps_str: this.formatStepsResults(planResult.step_results),
117
- plan_status: planResult.is_complete ? "Complete" : "In Progress",
118
- plan_result: planResult.result || "No results yet",
119
- });
120
- }
121
- formatStepsResults(stepResults) {
122
- if (!stepResults.length)
123
- return "No steps executed yet";
124
- return stepResults
125
- .map((stepResult, index) => `${index + 1}:\n${stepResult.task_results.length
126
- ? core_1.PromptTemplate.from(orchestrator_prompts_js_1.STEP_RESULT_TEMPLATE).format({
127
- step_description: stepResult.step.description,
128
- tasks_str: stepResult.task_results
129
- .map((task) => `- ${core_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_RESULT_TEMPLATE).format({
130
- task_description: task.description,
131
- task_result: task.result,
132
- })}`)
133
- .join("\n"),
134
- })
135
- : "No result"}`)
136
- .join("\n\n");
137
- }
138
152
  }
139
153
  exports.OrchestratorAgent = OrchestratorAgent;
154
+ function getMessageOrJsonString(output) {
155
+ const entries = Object.entries(output);
156
+ const firstValue = entries[0]?.[1];
157
+ if (entries.length === 1 && typeof firstValue === "string") {
158
+ return firstValue;
159
+ }
160
+ return JSON.stringify(output);
161
+ }
162
+ const orchestratorAgentOptionsSchema = zod_1.z.object({
163
+ maxIterations: zod_1.z.number().optional(),
164
+ tasksConcurrency: zod_1.z.number().optional(),
165
+ });
@@ -1,51 +1,12 @@
1
+ import type { Agent, Message } from "@aigne/core";
1
2
  import { z } from "zod";
2
- export declare const TASK_RESULT_TEMPLATE = "Task: {{task_description}}\nResult: {{task_result}}";
3
- export declare const STEP_RESULT_TEMPLATE = "Step: {{step_description}}\nStep Subtasks:\n{{tasks_str}}";
4
- export declare const PLAN_RESULT_TEMPLATE = "Plan Objective: {{plan_objective}}\n\nProgress So Far (steps completed):\n{{steps_str}}\n\nPlan Current Status: {{plan_status}}\nPlan Current Result: {{plan_result}}";
5
- export declare const FULL_PLAN_PROMPT_TEMPLATE = "You are tasked with orchestrating a plan to complete an objective.\nYou can analyze results from the previous steps already executed to decide if the objective is complete.\nYour plan must be structured in sequential steps, with each step containing independent parallel subtasks.\n\nObjective: {{objective}}\n\n{{plan_result}}\n\nIf the previous results achieve the objective, return is_complete=true.\nOtherwise, generate remaining steps needed.\n\nYou have access to the following Agents(which are collections of tools/functions):\n\nAgents:\n{{agents}}\n\nGenerate a plan with all remaining steps needed.\nSteps are sequential, but each Step can have parallel subtasks.\nFor each Step, specify a description of the step and independent subtasks that can run in parallel.\nFor each subtask specify:\n 1. Clear description of the task that an LLM can execute\n 2. Name of 1 Agent to use for the task";
6
- export declare const TASK_PROMPT_TEMPLATE = "You are part of a larger workflow to achieve the objective: {{objective}}.\nYour job is to accomplish only the following task: {{task}}.\n\nResults so far that may provide helpful context:\n{{context}}";
7
- export declare const SYNTHESIZE_PLAN_PROMPT_TEMPLATE = "Synthesize the results of executing all steps in the plan into a cohesive result:\n{{plan_result}}";
8
- export declare const TaskSchema: z.ZodObject<{
9
- description: z.ZodString;
10
- agent: z.ZodString;
11
- }, "strip", z.ZodTypeAny, {
12
- description: string;
13
- agent: string;
14
- }, {
15
- description: string;
16
- agent: string;
17
- }>;
18
- export declare const StepSchema: z.ZodObject<{
19
- description: z.ZodString;
20
- tasks: z.ZodArray<z.ZodObject<{
21
- description: z.ZodString;
22
- agent: z.ZodString;
23
- }, "strip", z.ZodTypeAny, {
24
- description: string;
25
- agent: string;
26
- }, {
27
- description: string;
28
- agent: string;
29
- }>, "many">;
30
- }, "strip", z.ZodTypeAny, {
31
- description: string;
32
- tasks: {
33
- description: string;
34
- agent: string;
35
- }[];
36
- }, {
37
- description: string;
38
- tasks: {
39
- description: string;
40
- agent: string;
41
- }[];
42
- }>;
43
- export declare const FullPlanSchema: z.ZodObject<{
3
+ export declare const SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE = "Synthesize the results of executing all steps in the plan into a cohesive result\n";
4
+ export declare function getFullPlanSchema(agents: Agent[]): z.ZodObject<{
44
5
  steps: z.ZodArray<z.ZodObject<{
45
6
  description: z.ZodString;
46
7
  tasks: z.ZodArray<z.ZodObject<{
47
8
  description: z.ZodString;
48
- agent: z.ZodString;
9
+ agent: z.ZodUnion<[z.ZodLiteral<string>, z.ZodLiteral<string>, ...z.ZodLiteral<string>[]]>;
49
10
  }, "strip", z.ZodTypeAny, {
50
11
  description: string;
51
12
  agent: string;
@@ -66,7 +27,7 @@ export declare const FullPlanSchema: z.ZodObject<{
66
27
  agent: string;
67
28
  }[];
68
29
  }>, "many">;
69
- is_complete: z.ZodBoolean;
30
+ isComplete: z.ZodBoolean;
70
31
  }, "strip", z.ZodTypeAny, {
71
32
  steps: {
72
33
  description: string;
@@ -75,7 +36,7 @@ export declare const FullPlanSchema: z.ZodObject<{
75
36
  agent: string;
76
37
  }[];
77
38
  }[];
78
- is_complete: boolean;
39
+ isComplete: boolean;
79
40
  }, {
80
41
  steps: {
81
42
  description: string;
@@ -84,8 +45,47 @@ export declare const FullPlanSchema: z.ZodObject<{
84
45
  agent: string;
85
46
  }[];
86
47
  }[];
87
- is_complete: boolean;
48
+ isComplete: boolean;
88
49
  }>;
89
- export type Task = z.infer<typeof TaskSchema>;
90
- export type Step = z.infer<typeof StepSchema>;
91
- export type FullPlanOutput = z.infer<typeof FullPlanSchema>;
50
+ export type FullPlanOutput = z.infer<ReturnType<typeof getFullPlanSchema>>;
51
+ export type Step = FullPlanOutput["steps"][number];
52
+ export type Task = Step["tasks"][number];
53
+ export interface StepWithResult {
54
+ step: Step;
55
+ tasks: Array<TaskWithResult>;
56
+ result: string;
57
+ }
58
+ export interface TaskWithResult {
59
+ task: Task;
60
+ result: string;
61
+ }
62
+ export interface FullPlanInput extends Message {
63
+ objective: string;
64
+ steps: StepWithResult[];
65
+ plan: {
66
+ status: string;
67
+ result: string;
68
+ };
69
+ agents: {
70
+ name: string;
71
+ description?: string;
72
+ tools: {
73
+ name: string;
74
+ description?: string;
75
+ }[];
76
+ }[];
77
+ }
78
+ export declare const FULL_PLAN_PROMPT_TEMPLATE = "You are tasked with orchestrating a plan to complete an objective.\nYou can analyze results from the previous steps already executed to decide if the objective is complete.\nYour plan must be structured in sequential steps, with each step containing independent parallel subtasks.\n\n<objective>\n{{objective}}\n</objective>\n\n<steps_completed>\n{{#steps}}\n- Step: {{step.description}}\n Result: {{result}}\n{{/steps}}\n</steps_completed>\n\n<previous_plan_status>\n{{plan.status}}\n</previous_plan_status>\n\n<previous_plan_result>\n{{plan.result}}\n</previous_plan_result>\n\nYou have access to the following Agents(which are collections of tools/functions):\n\n<agents>\n{{#agents}}\n- Agent: {{name}}\n Description: {{description}}\n Functions:\n {{#tools}}\n - Tool: {{name}}\n Description: {{description}}\n {{/tools}}\n{{/agents}}\n</agents>\n\n- If the previous plan results achieve the objective, return isComplete=true.\n- Otherwise, generate remaining steps needed.\n- Generate a plan with all remaining steps needed.\n- Steps are sequential, but each Step can have parallel subtasks.\n- For each Step, specify a description of the step and independent subtasks that can run in parallel.\n- For each subtask specify:\n 1. Clear description of the task that an LLM can execute\n 2. Name of 1 Agent to use for the task";
79
+ export interface TaskPromptInput extends Message {
80
+ objective: string;
81
+ step: Step;
82
+ task: Task;
83
+ steps: StepWithResult[];
84
+ }
85
+ export declare const TASK_PROMPT_TEMPLATE = "You are part of a larger workflow to achieve the step then the objective:\n\n<objective>\n{{objective}}\n</objective>\n\n<step>\n{{step.description}}\n</step>\n\nYour job is to accomplish only the following task:\n\n<task>\n{{task.description}}\n</task>\n\nResults so far that may provide helpful context:\n\n<steps_completed>\n{{#steps}}\n- Step: {{step.description}}\n Result: {{result}}\n{{/steps}}\n</steps_completed>\n";
86
+ export interface SynthesizeStepPromptInput extends Message {
87
+ objective: string;
88
+ step: Step;
89
+ tasks: TaskWithResult[];
90
+ }
91
+ export declare const SYNTHESIZE_STEP_PROMPT_TEMPLATE = "Synthesize the results of these parallel tasks into a cohesive result\n\n<objective>\n{{objective}}\n</objective>\n\n<step>\n{{step.description}}\n</step>\n\n<tasks>\n{{#tasks}}\n- Task: {{task.description}}\n Result: {{result}}\n{{/tasks}}\n</tasks>\n";
@@ -1,57 +1,120 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FullPlanSchema = exports.StepSchema = exports.TaskSchema = exports.SYNTHESIZE_PLAN_PROMPT_TEMPLATE = exports.TASK_PROMPT_TEMPLATE = exports.FULL_PLAN_PROMPT_TEMPLATE = exports.PLAN_RESULT_TEMPLATE = exports.STEP_RESULT_TEMPLATE = exports.TASK_RESULT_TEMPLATE = void 0;
3
+ exports.SYNTHESIZE_STEP_PROMPT_TEMPLATE = exports.TASK_PROMPT_TEMPLATE = exports.FULL_PLAN_PROMPT_TEMPLATE = exports.SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE = void 0;
4
+ exports.getFullPlanSchema = getFullPlanSchema;
5
+ const json_schema_js_1 = require("@aigne/core/utils/json-schema.js");
6
+ const lodash_es_1 = require("lodash-es");
4
7
  const zod_1 = require("zod");
5
- exports.TASK_RESULT_TEMPLATE = `Task: {{task_description}}
6
- Result: {{task_result}}`;
7
- exports.STEP_RESULT_TEMPLATE = `Step: {{step_description}}
8
- Step Subtasks:
9
- {{tasks_str}}`;
10
- exports.PLAN_RESULT_TEMPLATE = `Plan Objective: {{plan_objective}}
11
-
12
- Progress So Far (steps completed):
13
- {{steps_str}}
14
-
15
- Plan Current Status: {{plan_status}}
16
- Plan Current Result: {{plan_result}}`;
8
+ exports.SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE = `\
9
+ Synthesize the results of executing all steps in the plan into a cohesive result
10
+ `;
11
+ function getFullPlanSchema(agents) {
12
+ const agentNames = agents.map((i) => i.name);
13
+ if (new Set(agentNames).size !== agentNames.length) {
14
+ const duplicates = (0, lodash_es_1.pickBy)((0, lodash_es_1.groupBy)(agentNames), (x) => x.length > 1);
15
+ throw new Error(`Tools name must be unique for orchestrator: ${Object.keys(duplicates).join(",")}`);
16
+ }
17
+ const TaskSchema = zod_1.z.object({
18
+ description: zod_1.z.string().describe("Detailed description of the task"),
19
+ agent: zod_1.z
20
+ .union((0, json_schema_js_1.ensureZodUnionArray)(agents.map((i) => zod_1.z.literal(i.name))))
21
+ .describe("Name of the agent to execute the task"),
22
+ });
23
+ const StepSchema = zod_1.z.object({
24
+ description: zod_1.z.string().describe("Detailed description of the step"),
25
+ tasks: zod_1.z.array(TaskSchema).describe("Tasks that can run in parallel in this step"),
26
+ });
27
+ return zod_1.z.object({
28
+ steps: zod_1.z.array(StepSchema).describe("All sequential steps in the plan"),
29
+ isComplete: zod_1.z.boolean().describe("Whether the previous plan results achieve the objective"),
30
+ });
31
+ }
17
32
  exports.FULL_PLAN_PROMPT_TEMPLATE = `You are tasked with orchestrating a plan to complete an objective.
18
33
  You can analyze results from the previous steps already executed to decide if the objective is complete.
19
34
  Your plan must be structured in sequential steps, with each step containing independent parallel subtasks.
20
35
 
21
- Objective: {{objective}}
36
+ <objective>
37
+ {{objective}}
38
+ </objective>
22
39
 
23
- {{plan_result}}
40
+ <steps_completed>
41
+ {{#steps}}
42
+ - Step: {{step.description}}
43
+ Result: {{result}}
44
+ {{/steps}}
45
+ </steps_completed>
24
46
 
25
- If the previous results achieve the objective, return is_complete=true.
26
- Otherwise, generate remaining steps needed.
47
+ <previous_plan_status>
48
+ {{plan.status}}
49
+ </previous_plan_status>
50
+
51
+ <previous_plan_result>
52
+ {{plan.result}}
53
+ </previous_plan_result>
27
54
 
28
55
  You have access to the following Agents(which are collections of tools/functions):
29
56
 
30
- Agents:
31
- {{agents}}
57
+ <agents>
58
+ {{#agents}}
59
+ - Agent: {{name}}
60
+ Description: {{description}}
61
+ Functions:
62
+ {{#tools}}
63
+ - Tool: {{name}}
64
+ Description: {{description}}
65
+ {{/tools}}
66
+ {{/agents}}
67
+ </agents>
32
68
 
33
- Generate a plan with all remaining steps needed.
34
- Steps are sequential, but each Step can have parallel subtasks.
35
- For each Step, specify a description of the step and independent subtasks that can run in parallel.
36
- For each subtask specify:
69
+ - If the previous plan results achieve the objective, return isComplete=true.
70
+ - Otherwise, generate remaining steps needed.
71
+ - Generate a plan with all remaining steps needed.
72
+ - Steps are sequential, but each Step can have parallel subtasks.
73
+ - For each Step, specify a description of the step and independent subtasks that can run in parallel.
74
+ - For each subtask specify:
37
75
  1. Clear description of the task that an LLM can execute
38
76
  2. Name of 1 Agent to use for the task`;
39
- exports.TASK_PROMPT_TEMPLATE = `You are part of a larger workflow to achieve the objective: {{objective}}.
40
- Your job is to accomplish only the following task: {{task}}.
77
+ exports.TASK_PROMPT_TEMPLATE = `\
78
+ You are part of a larger workflow to achieve the step then the objective:
79
+
80
+ <objective>
81
+ {{objective}}
82
+ </objective>
83
+
84
+ <step>
85
+ {{step.description}}
86
+ </step>
87
+
88
+ Your job is to accomplish only the following task:
89
+
90
+ <task>
91
+ {{task.description}}
92
+ </task>
41
93
 
42
94
  Results so far that may provide helpful context:
43
- {{context}}`;
44
- exports.SYNTHESIZE_PLAN_PROMPT_TEMPLATE = `Synthesize the results of executing all steps in the plan into a cohesive result:
45
- {{plan_result}}`;
46
- exports.TaskSchema = zod_1.z.object({
47
- description: zod_1.z.string().describe("Detailed description of the task"),
48
- agent: zod_1.z.string().describe("Name of the agent to execute the task"),
49
- });
50
- exports.StepSchema = zod_1.z.object({
51
- description: zod_1.z.string().describe("Detailed description of the step"),
52
- tasks: zod_1.z.array(exports.TaskSchema).describe("Tasks that can run in parallel in this step"),
53
- });
54
- exports.FullPlanSchema = zod_1.z.object({
55
- steps: zod_1.z.array(exports.StepSchema).describe("All sequential steps in the plan"),
56
- is_complete: zod_1.z.boolean().describe("Whether the plan is complete"),
57
- });
95
+
96
+ <steps_completed>
97
+ {{#steps}}
98
+ - Step: {{step.description}}
99
+ Result: {{result}}
100
+ {{/steps}}
101
+ </steps_completed>
102
+ `;
103
+ exports.SYNTHESIZE_STEP_PROMPT_TEMPLATE = `\
104
+ Synthesize the results of these parallel tasks into a cohesive result
105
+
106
+ <objective>
107
+ {{objective}}
108
+ </objective>
109
+
110
+ <step>
111
+ {{step.description}}
112
+ </step>
113
+
114
+ <tasks>
115
+ {{#tasks}}
116
+ - Task: {{task.description}}
117
+ Result: {{result}}
118
+ {{/tasks}}
119
+ </tasks>
120
+ `;
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
@@ -1 +1 @@
1
- export * from "./orchestrator/index.js";
1
+ export {};
@@ -1,29 +1,16 @@
1
1
  import { Agent, type AgentOptions, type Context, type Message } from "@aigne/core";
2
- import { type FullPlanOutput, type Step } from "./orchestrator-prompts.js";
2
+ import { type FullPlanOutput, type StepWithResult } from "./orchestrator-prompts.js";
3
3
  export * from "./orchestrator-prompts.js";
4
- export interface StepResult {
5
- step: Step;
6
- task_results: Array<TaskWithResult>;
7
- }
8
- export interface TaskWithResult {
9
- description: string;
10
- agent: string;
11
- result: string;
12
- }
13
- export interface PlanResult extends Message {
4
+ export interface FullPlanWithResult {
14
5
  objective: string;
15
6
  plan?: FullPlanOutput;
16
- is_complete?: boolean;
7
+ steps: StepWithResult[];
17
8
  result?: string;
18
- step_results: StepResult[];
19
- }
20
- export interface FullPlanInput extends Message {
21
- objective: string;
22
- plan_result: string;
23
- agents: string;
9
+ status?: boolean;
24
10
  }
25
11
  export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
26
12
  maxIterations?: number;
13
+ tasksConcurrency?: number;
27
14
  }
28
15
  export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
29
16
  static from<I extends Message, O extends Message>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
@@ -31,9 +18,10 @@ export declare class OrchestratorAgent<I extends Message = Message, O extends Me
31
18
  private planner;
32
19
  private completer;
33
20
  maxIterations?: number;
21
+ tasksConcurrency?: number;
34
22
  process(input: I, context?: Context): Promise<O>;
23
+ private getFullPlanInput;
35
24
  private getFullPlan;
25
+ private synthesizePlanResult;
36
26
  private executeStep;
37
- private formatPlanResult;
38
- private formatStepsResults;
39
27
  }