@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.
@@ -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 +1 @@
1
- export * from "./orchestrator/index.js";
1
+ export {};
package/lib/esm/index.js CHANGED
@@ -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
  }
@@ -1,28 +1,35 @@
1
- import { AIAgent, Agent, PromptTemplate, getMessage, } from "@aigne/core";
2
- import { FULL_PLAN_PROMPT_TEMPLATE, FullPlanSchema, PLAN_RESULT_TEMPLATE, STEP_RESULT_TEMPLATE, SYNTHESIZE_PLAN_PROMPT_TEMPLATE, TASK_PROMPT_TEMPLATE, TASK_RESULT_TEMPLATE, } from "./orchestrator-prompts.js";
1
+ import { AIAgent, Agent, PromptTemplate, createMessage, getMessage, } from "@aigne/core";
2
+ import { checkArguments } from "@aigne/core/utils/type-utils.js";
3
+ import fastq from "fastq";
4
+ import { z } from "zod";
5
+ import { FULL_PLAN_PROMPT_TEMPLATE, SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE, SYNTHESIZE_STEP_PROMPT_TEMPLATE, TASK_PROMPT_TEMPLATE, getFullPlanSchema, } from "./orchestrator-prompts.js";
3
6
  const DEFAULT_MAX_ITERATIONS = 30;
7
+ const DEFAULT_TASK_CONCURRENCY = 5;
4
8
  export * from "./orchestrator-prompts.js";
5
9
  export class OrchestratorAgent extends Agent {
6
10
  static from(options) {
7
11
  return new OrchestratorAgent(options);
8
12
  }
9
13
  constructor(options) {
14
+ checkArguments("OrchestratorAgent", orchestratorAgentOptionsSchema, options);
10
15
  super({ ...options });
11
16
  this.maxIterations = options.maxIterations;
17
+ this.tasksConcurrency = options.tasksConcurrency;
12
18
  this.planner = new AIAgent({
13
19
  name: "llm_orchestration_planner",
14
20
  instructions: FULL_PLAN_PROMPT_TEMPLATE,
15
- outputSchema: FullPlanSchema,
21
+ outputSchema: () => getFullPlanSchema(this.tools),
16
22
  });
17
23
  this.completer = new AIAgent({
18
24
  name: "llm_orchestration_completer",
19
- instructions: SYNTHESIZE_PLAN_PROMPT_TEMPLATE,
25
+ instructions: FULL_PLAN_PROMPT_TEMPLATE,
20
26
  outputSchema: this.outputSchema,
21
27
  });
22
28
  }
23
29
  planner;
24
30
  completer;
25
31
  maxIterations;
32
+ tasksConcurrency;
26
33
  async process(input, context) {
27
34
  const model = context?.model;
28
35
  if (!model)
@@ -32,90 +39,106 @@ export class OrchestratorAgent extends Agent {
32
39
  throw new Error("Objective is required to run OrchestratorAgent");
33
40
  const result = {
34
41
  objective,
35
- step_results: [],
42
+ steps: [],
36
43
  };
37
44
  let iterations = 0;
38
45
  const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
39
46
  while (iterations++ < maxIterations) {
40
- const plan = await this.getFullPlan(objective, result, context);
47
+ const plan = await this.getFullPlan(result, context);
41
48
  result.plan = plan;
42
- if (plan.is_complete) {
43
- return context.call(this.completer, { plan_result: this.formatPlanResult(result) });
49
+ if (plan.isComplete) {
50
+ return this.synthesizePlanResult(result, context);
44
51
  }
45
52
  for (const step of plan.steps) {
46
53
  const stepResult = await this.executeStep(result, step, context);
47
- result.step_results.push(stepResult);
54
+ result.steps.push(stepResult);
48
55
  }
49
56
  }
50
57
  throw new Error(`Max iterations reached: ${maxIterations}`);
51
58
  }
52
- async getFullPlan(objective, planResult, context) {
53
- const agents = this.tools
54
- .map((agent, idx) => `${idx + 1}. Agent Name: ${agent.name}
55
- Description: ${agent.description}
56
- Functions: ${agent.tools.map((tool) => `- ${tool.name} ${tool.description}`).join("\n")}`)
57
- .join("\n");
58
- return context.call(this.planner, {
59
- objective,
60
- plan_result: this.formatPlanResult(planResult),
61
- agents,
59
+ getFullPlanInput(planResult) {
60
+ return {
61
+ objective: planResult.objective,
62
+ steps: planResult.steps,
63
+ plan: {
64
+ status: planResult.status ? "Complete" : "In Progress",
65
+ result: planResult.result || "No results yet",
66
+ },
67
+ agents: this.tools.map((i) => ({
68
+ name: i.name,
69
+ description: i.description,
70
+ tools: i.tools.map((i) => ({ name: i.name, description: i.description })),
71
+ })),
72
+ };
73
+ }
74
+ async getFullPlan(planResult, context) {
75
+ return context.call(this.planner, this.getFullPlanInput(planResult));
76
+ }
77
+ async synthesizePlanResult(planResult, context) {
78
+ return context.call(this.completer, {
79
+ ...this.getFullPlanInput(planResult),
80
+ ...createMessage(SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE),
62
81
  });
63
82
  }
64
- async executeStep(previousResult, step, context) {
83
+ async executeStep(planResult, step, context) {
84
+ const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
65
85
  const model = context?.model;
66
86
  if (!model)
67
87
  throw new Error("model is required to run OrchestratorAgent");
68
- const taskResults = [];
69
- for (const task of step.tasks) {
88
+ const queue = fastq.promise(async (task) => {
70
89
  const agent = this.tools.find((agent) => agent.name === task.agent);
71
90
  if (!agent)
72
91
  throw new Error(`Agent ${task.agent} not found`);
73
92
  const prompt = PromptTemplate.from(TASK_PROMPT_TEMPLATE).format({
74
- objective: previousResult.objective,
75
- task: task.description,
76
- context: this.formatPlanResult(previousResult),
93
+ objective: planResult.objective,
94
+ step,
95
+ task,
96
+ steps: planResult.steps,
77
97
  });
78
98
  let result;
79
99
  if (agent.isCallable) {
80
- result = JSON.stringify(await context.call(agent, prompt));
100
+ result = getMessageOrJsonString(await context.call(agent, prompt));
81
101
  }
82
102
  else {
83
103
  const executor = AIAgent.from({
104
+ name: "llm_orchestration_task_executor",
84
105
  instructions: prompt,
85
106
  tools: agent.tools,
86
107
  });
87
- result = JSON.stringify(await context.call(executor, {}));
108
+ result = getMessageOrJsonString(await context.call(executor, {}));
88
109
  }
89
- taskResults.push({ ...task, result });
110
+ return { task, result };
111
+ }, concurrency);
112
+ let results;
113
+ try {
114
+ results = await Promise.all(step.tasks.map((task) => queue.push(task)));
115
+ }
116
+ catch (error) {
117
+ queue.kill();
118
+ throw error;
90
119
  }
120
+ const result = getMessageOrJsonString(await context.call(AIAgent.from({
121
+ name: "llm_orchestration_step_synthesizer",
122
+ instructions: SYNTHESIZE_STEP_PROMPT_TEMPLATE,
123
+ }), { objective: planResult.objective, step, tasks: results }));
124
+ if (!result)
125
+ throw new Error("unexpected empty result from synthesize step's tasks results");
91
126
  return {
92
127
  step,
93
- task_results: taskResults,
128
+ tasks: results,
129
+ result,
94
130
  };
95
131
  }
96
- formatPlanResult(planResult) {
97
- return PromptTemplate.from(PLAN_RESULT_TEMPLATE).format({
98
- plan_objective: planResult.objective,
99
- steps_str: this.formatStepsResults(planResult.step_results),
100
- plan_status: planResult.is_complete ? "Complete" : "In Progress",
101
- plan_result: planResult.result || "No results yet",
102
- });
103
- }
104
- formatStepsResults(stepResults) {
105
- if (!stepResults.length)
106
- return "No steps executed yet";
107
- return stepResults
108
- .map((stepResult, index) => `${index + 1}:\n${stepResult.task_results.length
109
- ? PromptTemplate.from(STEP_RESULT_TEMPLATE).format({
110
- step_description: stepResult.step.description,
111
- tasks_str: stepResult.task_results
112
- .map((task) => `- ${PromptTemplate.from(TASK_RESULT_TEMPLATE).format({
113
- task_description: task.description,
114
- task_result: task.result,
115
- })}`)
116
- .join("\n"),
117
- })
118
- : "No result"}`)
119
- .join("\n\n");
132
+ }
133
+ function getMessageOrJsonString(output) {
134
+ const entries = Object.entries(output);
135
+ const firstValue = entries[0]?.[1];
136
+ if (entries.length === 1 && typeof firstValue === "string") {
137
+ return firstValue;
120
138
  }
139
+ return JSON.stringify(output);
121
140
  }
141
+ const orchestratorAgentOptionsSchema = z.object({
142
+ maxIterations: z.number().optional(),
143
+ tasksConcurrency: z.number().optional(),
144
+ });
@@ -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,54 +1,116 @@
1
+ import { ensureZodUnionArray } from "@aigne/core/utils/json-schema.js";
2
+ import { groupBy, pickBy } from "lodash-es";
1
3
  import { z } from "zod";
2
- export const TASK_RESULT_TEMPLATE = `Task: {{task_description}}
3
- Result: {{task_result}}`;
4
- export const STEP_RESULT_TEMPLATE = `Step: {{step_description}}
5
- Step Subtasks:
6
- {{tasks_str}}`;
7
- export const PLAN_RESULT_TEMPLATE = `Plan Objective: {{plan_objective}}
8
-
9
- Progress So Far (steps completed):
10
- {{steps_str}}
11
-
12
- Plan Current Status: {{plan_status}}
13
- Plan Current Result: {{plan_result}}`;
4
+ export const SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE = `\
5
+ Synthesize the results of executing all steps in the plan into a cohesive result
6
+ `;
7
+ export function getFullPlanSchema(agents) {
8
+ const agentNames = agents.map((i) => i.name);
9
+ if (new Set(agentNames).size !== agentNames.length) {
10
+ const duplicates = pickBy(groupBy(agentNames), (x) => x.length > 1);
11
+ throw new Error(`Tools name must be unique for orchestrator: ${Object.keys(duplicates).join(",")}`);
12
+ }
13
+ const TaskSchema = z.object({
14
+ description: z.string().describe("Detailed description of the task"),
15
+ agent: z
16
+ .union(ensureZodUnionArray(agents.map((i) => z.literal(i.name))))
17
+ .describe("Name of the agent to execute the task"),
18
+ });
19
+ const StepSchema = z.object({
20
+ description: z.string().describe("Detailed description of the step"),
21
+ tasks: z.array(TaskSchema).describe("Tasks that can run in parallel in this step"),
22
+ });
23
+ return z.object({
24
+ steps: z.array(StepSchema).describe("All sequential steps in the plan"),
25
+ isComplete: z.boolean().describe("Whether the previous plan results achieve the objective"),
26
+ });
27
+ }
14
28
  export const FULL_PLAN_PROMPT_TEMPLATE = `You are tasked with orchestrating a plan to complete an objective.
15
29
  You can analyze results from the previous steps already executed to decide if the objective is complete.
16
30
  Your plan must be structured in sequential steps, with each step containing independent parallel subtasks.
17
31
 
18
- Objective: {{objective}}
32
+ <objective>
33
+ {{objective}}
34
+ </objective>
19
35
 
20
- {{plan_result}}
36
+ <steps_completed>
37
+ {{#steps}}
38
+ - Step: {{step.description}}
39
+ Result: {{result}}
40
+ {{/steps}}
41
+ </steps_completed>
21
42
 
22
- If the previous results achieve the objective, return is_complete=true.
23
- Otherwise, generate remaining steps needed.
43
+ <previous_plan_status>
44
+ {{plan.status}}
45
+ </previous_plan_status>
46
+
47
+ <previous_plan_result>
48
+ {{plan.result}}
49
+ </previous_plan_result>
24
50
 
25
51
  You have access to the following Agents(which are collections of tools/functions):
26
52
 
27
- Agents:
28
- {{agents}}
53
+ <agents>
54
+ {{#agents}}
55
+ - Agent: {{name}}
56
+ Description: {{description}}
57
+ Functions:
58
+ {{#tools}}
59
+ - Tool: {{name}}
60
+ Description: {{description}}
61
+ {{/tools}}
62
+ {{/agents}}
63
+ </agents>
29
64
 
30
- Generate a plan with all remaining steps needed.
31
- Steps are sequential, but each Step can have parallel subtasks.
32
- For each Step, specify a description of the step and independent subtasks that can run in parallel.
33
- For each subtask specify:
65
+ - If the previous plan results achieve the objective, return isComplete=true.
66
+ - Otherwise, generate remaining steps needed.
67
+ - Generate a plan with all remaining steps needed.
68
+ - Steps are sequential, but each Step can have parallel subtasks.
69
+ - For each Step, specify a description of the step and independent subtasks that can run in parallel.
70
+ - For each subtask specify:
34
71
  1. Clear description of the task that an LLM can execute
35
72
  2. Name of 1 Agent to use for the task`;
36
- export const TASK_PROMPT_TEMPLATE = `You are part of a larger workflow to achieve the objective: {{objective}}.
37
- Your job is to accomplish only the following task: {{task}}.
73
+ export const TASK_PROMPT_TEMPLATE = `\
74
+ You are part of a larger workflow to achieve the step then the objective:
75
+
76
+ <objective>
77
+ {{objective}}
78
+ </objective>
79
+
80
+ <step>
81
+ {{step.description}}
82
+ </step>
83
+
84
+ Your job is to accomplish only the following task:
85
+
86
+ <task>
87
+ {{task.description}}
88
+ </task>
38
89
 
39
90
  Results so far that may provide helpful context:
40
- {{context}}`;
41
- export const SYNTHESIZE_PLAN_PROMPT_TEMPLATE = `Synthesize the results of executing all steps in the plan into a cohesive result:
42
- {{plan_result}}`;
43
- export const TaskSchema = z.object({
44
- description: z.string().describe("Detailed description of the task"),
45
- agent: z.string().describe("Name of the agent to execute the task"),
46
- });
47
- export const StepSchema = z.object({
48
- description: z.string().describe("Detailed description of the step"),
49
- tasks: z.array(TaskSchema).describe("Tasks that can run in parallel in this step"),
50
- });
51
- export const FullPlanSchema = z.object({
52
- steps: z.array(StepSchema).describe("All sequential steps in the plan"),
53
- is_complete: z.boolean().describe("Whether the plan is complete"),
54
- });
91
+
92
+ <steps_completed>
93
+ {{#steps}}
94
+ - Step: {{step.description}}
95
+ Result: {{result}}
96
+ {{/steps}}
97
+ </steps_completed>
98
+ `;
99
+ export const SYNTHESIZE_STEP_PROMPT_TEMPLATE = `\
100
+ Synthesize the results of these parallel tasks into a cohesive result
101
+
102
+ <objective>
103
+ {{objective}}
104
+ </objective>
105
+
106
+ <step>
107
+ {{step.description}}
108
+ </step>
109
+
110
+ <tasks>
111
+ {{#tasks}}
112
+ - Task: {{task.description}}
113
+ Result: {{result}}
114
+ {{/tasks}}
115
+ </tasks>
116
+ `;