@aigne/agent-library 1.1.0-beta.9 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ ## [1.2.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.1.0...agent-library-v1.2.0) (2025-03-18)
2
+
3
+ - chore: release 1.2.0
4
+
5
+ ## 1.1.0-beta.17 (2025-3-18)
6
+
7
+ - chore: add support for esm module
8
+
9
+ ## 1.1.0-beta.16 (2025-3-18)
10
+
11
+ - chore: add puppeteer in linux need docker_container
12
+
13
+ ## 1.1.0-beta.15 (2025-3-18)
14
+
15
+ - chore: make coverage report as text to terminal
16
+ - chore: update contributing docs
17
+
18
+ ## 1.1.0-beta.14 (2025-3-18)
19
+
20
+ - chore(example): add code-execution example
21
+
22
+ ## 1.1.0-beta.13 (2025-3-18)
23
+
24
+ - feat: add OrchestratorAgent in agent library
@@ -1 +1 @@
1
- export {};
1
+ export * from "./orchestrator/index.js";
package/lib/cjs/index.js CHANGED
@@ -1,2 +1,17 @@
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
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./orchestrator/index.js"), exports);
@@ -0,0 +1,40 @@
1
+ import type { AgentOptions, AgentOutput, Context } from "@aigne/core-next";
2
+ import type { AgentInput } from "@aigne/core-next";
3
+ import { Agent } from "@aigne/core-next";
4
+ import { type FullPlanOutput, type Step } from "./orchestrator-prompts.js";
5
+ export interface StepResult {
6
+ step: Step;
7
+ task_results: Array<TaskWithResult>;
8
+ }
9
+ export interface TaskWithResult {
10
+ description: string;
11
+ agent: string;
12
+ result: string;
13
+ }
14
+ export interface PlanResult extends AgentOutput {
15
+ objective: string;
16
+ plan?: FullPlanOutput;
17
+ is_complete?: boolean;
18
+ result?: string;
19
+ step_results: StepResult[];
20
+ }
21
+ export interface FullPlanInput extends AgentInput {
22
+ objective: string;
23
+ plan_result: string;
24
+ agents: string;
25
+ }
26
+ export interface OrchestratorAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
27
+ maxIterations?: number;
28
+ }
29
+ export declare class OrchestratorAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
30
+ static from<I extends AgentInput, O extends AgentOutput>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
31
+ constructor(options: OrchestratorAgentOptions<I, O>);
32
+ private planner;
33
+ private completer;
34
+ maxIterations?: number;
35
+ process(input: I, context?: Context): Promise<O>;
36
+ private getFullPlan;
37
+ private executeStep;
38
+ private formatPlanResult;
39
+ private formatStepsResults;
40
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrchestratorAgent = void 0;
4
+ const core_next_1 = require("@aigne/core-next");
5
+ const orchestrator_prompts_js_1 = require("./orchestrator-prompts.js");
6
+ const DEFAULT_MAX_ITERATIONS = 30;
7
+ class OrchestratorAgent extends core_next_1.Agent {
8
+ static from(options) {
9
+ return new OrchestratorAgent(options);
10
+ }
11
+ constructor(options) {
12
+ super({ ...options });
13
+ this.maxIterations = options.maxIterations;
14
+ this.planner = new core_next_1.AIAgent({
15
+ name: "llm_orchestration_planner",
16
+ instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
17
+ outputSchema: orchestrator_prompts_js_1.FullPlanSchema,
18
+ });
19
+ this.completer = new core_next_1.AIAgent({
20
+ name: "llm_orchestration_completer",
21
+ instructions: orchestrator_prompts_js_1.SYNTHESIZE_PLAN_PROMPT_TEMPLATE,
22
+ outputSchema: this.outputSchema,
23
+ });
24
+ }
25
+ planner;
26
+ completer;
27
+ maxIterations;
28
+ async process(input, context) {
29
+ const model = context?.model;
30
+ if (!model)
31
+ throw new Error("model is required to run OrchestratorAgent");
32
+ const objective = (0, core_next_1.getUserInputMessage)(input);
33
+ if (!objective)
34
+ throw new Error("Objective is required to run OrchestratorAgent");
35
+ const result = {
36
+ objective,
37
+ step_results: [],
38
+ };
39
+ let iterations = 0;
40
+ const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
41
+ while (iterations++ < maxIterations) {
42
+ const plan = await this.getFullPlan(objective, result, context);
43
+ result.plan = plan;
44
+ if (plan.is_complete) {
45
+ return this.completer.call({ plan_result: this.formatPlanResult(result) }, context);
46
+ }
47
+ for (const step of plan.steps) {
48
+ const stepResult = await this.executeStep(result, step, context);
49
+ result.step_results.push(stepResult);
50
+ }
51
+ }
52
+ throw new Error(`Max iterations reached: ${maxIterations}`);
53
+ }
54
+ async getFullPlan(objective, planResult, context) {
55
+ const agents = this.tools
56
+ .map((agent, idx) => `${idx + 1}. Agent Name: ${agent.name}
57
+ Description: ${agent.description}
58
+ Functions: ${agent.tools.map((tool) => `- ${tool.name} ${tool.description}`).join("\n")}`)
59
+ .join("\n");
60
+ return this.planner.call({ objective, plan_result: this.formatPlanResult(planResult), agents }, context);
61
+ }
62
+ async executeStep(previousResult, step, context) {
63
+ const model = context?.model;
64
+ if (!model)
65
+ throw new Error("model is required to run OrchestratorAgent");
66
+ const taskResults = [];
67
+ for (const task of step.tasks) {
68
+ const agent = this.tools.find((agent) => agent.name === task.agent);
69
+ if (!agent)
70
+ throw new Error(`Agent ${task.agent} not found`);
71
+ const prompt = core_next_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_PROMPT_TEMPLATE).format({
72
+ objective: previousResult.objective,
73
+ task: task.description,
74
+ context: this.formatPlanResult(previousResult),
75
+ });
76
+ let result;
77
+ if (agent.isCallable) {
78
+ result = JSON.stringify(await agent.call(prompt, context));
79
+ }
80
+ else {
81
+ const executor = core_next_1.AIAgent.from({
82
+ instructions: prompt,
83
+ tools: agent.tools,
84
+ });
85
+ result = JSON.stringify(await executor.call({}, context));
86
+ }
87
+ taskResults.push({ ...task, result });
88
+ }
89
+ return {
90
+ step,
91
+ task_results: taskResults,
92
+ };
93
+ }
94
+ formatPlanResult(planResult) {
95
+ return core_next_1.PromptTemplate.from(orchestrator_prompts_js_1.PLAN_RESULT_TEMPLATE).format({
96
+ plan_objective: planResult.objective,
97
+ steps_str: this.formatStepsResults(planResult.step_results),
98
+ plan_status: planResult.is_complete ? "Complete" : "In Progress",
99
+ plan_result: planResult.result || "No results yet",
100
+ });
101
+ }
102
+ formatStepsResults(stepResults) {
103
+ if (!stepResults.length)
104
+ return "No steps executed yet";
105
+ return stepResults
106
+ .map((stepResult, index) => `${index + 1}:\n${stepResult.task_results.length
107
+ ? core_next_1.PromptTemplate.from(orchestrator_prompts_js_1.STEP_RESULT_TEMPLATE).format({
108
+ step_description: stepResult.step.description,
109
+ tasks_str: stepResult.task_results
110
+ .map((task) => `- ${core_next_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_RESULT_TEMPLATE).format({
111
+ task_description: task.description,
112
+ task_result: task.result,
113
+ })}`)
114
+ .join("\n"),
115
+ })
116
+ : "No result"}`)
117
+ .join("\n\n");
118
+ }
119
+ }
120
+ exports.OrchestratorAgent = OrchestratorAgent;
@@ -0,0 +1,91 @@
1
+ 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<{
44
+ steps: z.ZodArray<z.ZodObject<{
45
+ description: z.ZodString;
46
+ tasks: z.ZodArray<z.ZodObject<{
47
+ description: z.ZodString;
48
+ agent: z.ZodString;
49
+ }, "strip", z.ZodTypeAny, {
50
+ description: string;
51
+ agent: string;
52
+ }, {
53
+ description: string;
54
+ agent: string;
55
+ }>, "many">;
56
+ }, "strip", z.ZodTypeAny, {
57
+ description: string;
58
+ tasks: {
59
+ description: string;
60
+ agent: string;
61
+ }[];
62
+ }, {
63
+ description: string;
64
+ tasks: {
65
+ description: string;
66
+ agent: string;
67
+ }[];
68
+ }>, "many">;
69
+ is_complete: z.ZodBoolean;
70
+ }, "strip", z.ZodTypeAny, {
71
+ steps: {
72
+ description: string;
73
+ tasks: {
74
+ description: string;
75
+ agent: string;
76
+ }[];
77
+ }[];
78
+ is_complete: boolean;
79
+ }, {
80
+ steps: {
81
+ description: string;
82
+ tasks: {
83
+ description: string;
84
+ agent: string;
85
+ }[];
86
+ }[];
87
+ is_complete: boolean;
88
+ }>;
89
+ export type Task = z.infer<typeof TaskSchema>;
90
+ export type Step = z.infer<typeof StepSchema>;
91
+ export type FullPlanOutput = z.infer<typeof FullPlanSchema>;
@@ -0,0 +1,57 @@
1
+ "use strict";
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;
4
+ 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}}`;
17
+ exports.FULL_PLAN_PROMPT_TEMPLATE = `You are tasked with orchestrating a plan to complete an objective.
18
+ You can analyze results from the previous steps already executed to decide if the objective is complete.
19
+ Your plan must be structured in sequential steps, with each step containing independent parallel subtasks.
20
+
21
+ Objective: {{objective}}
22
+
23
+ {{plan_result}}
24
+
25
+ If the previous results achieve the objective, return is_complete=true.
26
+ Otherwise, generate remaining steps needed.
27
+
28
+ You have access to the following Agents(which are collections of tools/functions):
29
+
30
+ Agents:
31
+ {{agents}}
32
+
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:
37
+ 1. Clear description of the task that an LLM can execute
38
+ 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}}.
41
+
42
+ 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
+ });
@@ -1 +1 @@
1
- export {};
1
+ export * from "./orchestrator/index.js";
@@ -0,0 +1,40 @@
1
+ import type { AgentOptions, AgentOutput, Context } from "@aigne/core-next";
2
+ import type { AgentInput } from "@aigne/core-next";
3
+ import { Agent } from "@aigne/core-next";
4
+ import { type FullPlanOutput, type Step } from "./orchestrator-prompts.js";
5
+ export interface StepResult {
6
+ step: Step;
7
+ task_results: Array<TaskWithResult>;
8
+ }
9
+ export interface TaskWithResult {
10
+ description: string;
11
+ agent: string;
12
+ result: string;
13
+ }
14
+ export interface PlanResult extends AgentOutput {
15
+ objective: string;
16
+ plan?: FullPlanOutput;
17
+ is_complete?: boolean;
18
+ result?: string;
19
+ step_results: StepResult[];
20
+ }
21
+ export interface FullPlanInput extends AgentInput {
22
+ objective: string;
23
+ plan_result: string;
24
+ agents: string;
25
+ }
26
+ export interface OrchestratorAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
27
+ maxIterations?: number;
28
+ }
29
+ export declare class OrchestratorAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
30
+ static from<I extends AgentInput, O extends AgentOutput>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
31
+ constructor(options: OrchestratorAgentOptions<I, O>);
32
+ private planner;
33
+ private completer;
34
+ maxIterations?: number;
35
+ process(input: I, context?: Context): Promise<O>;
36
+ private getFullPlan;
37
+ private executeStep;
38
+ private formatPlanResult;
39
+ private formatStepsResults;
40
+ }
@@ -0,0 +1,91 @@
1
+ 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<{
44
+ steps: z.ZodArray<z.ZodObject<{
45
+ description: z.ZodString;
46
+ tasks: z.ZodArray<z.ZodObject<{
47
+ description: z.ZodString;
48
+ agent: z.ZodString;
49
+ }, "strip", z.ZodTypeAny, {
50
+ description: string;
51
+ agent: string;
52
+ }, {
53
+ description: string;
54
+ agent: string;
55
+ }>, "many">;
56
+ }, "strip", z.ZodTypeAny, {
57
+ description: string;
58
+ tasks: {
59
+ description: string;
60
+ agent: string;
61
+ }[];
62
+ }, {
63
+ description: string;
64
+ tasks: {
65
+ description: string;
66
+ agent: string;
67
+ }[];
68
+ }>, "many">;
69
+ is_complete: z.ZodBoolean;
70
+ }, "strip", z.ZodTypeAny, {
71
+ steps: {
72
+ description: string;
73
+ tasks: {
74
+ description: string;
75
+ agent: string;
76
+ }[];
77
+ }[];
78
+ is_complete: boolean;
79
+ }, {
80
+ steps: {
81
+ description: string;
82
+ tasks: {
83
+ description: string;
84
+ agent: string;
85
+ }[];
86
+ }[];
87
+ is_complete: boolean;
88
+ }>;
89
+ export type Task = z.infer<typeof TaskSchema>;
90
+ export type Step = z.infer<typeof StepSchema>;
91
+ export type FullPlanOutput = z.infer<typeof FullPlanSchema>;
@@ -1 +1 @@
1
- export {};
1
+ export * from "./orchestrator/index.js";
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export * from "./orchestrator/index.js";
@@ -0,0 +1,40 @@
1
+ import type { AgentOptions, AgentOutput, Context } from "@aigne/core-next";
2
+ import type { AgentInput } from "@aigne/core-next";
3
+ import { Agent } from "@aigne/core-next";
4
+ import { type FullPlanOutput, type Step } from "./orchestrator-prompts.js";
5
+ export interface StepResult {
6
+ step: Step;
7
+ task_results: Array<TaskWithResult>;
8
+ }
9
+ export interface TaskWithResult {
10
+ description: string;
11
+ agent: string;
12
+ result: string;
13
+ }
14
+ export interface PlanResult extends AgentOutput {
15
+ objective: string;
16
+ plan?: FullPlanOutput;
17
+ is_complete?: boolean;
18
+ result?: string;
19
+ step_results: StepResult[];
20
+ }
21
+ export interface FullPlanInput extends AgentInput {
22
+ objective: string;
23
+ plan_result: string;
24
+ agents: string;
25
+ }
26
+ export interface OrchestratorAgentOptions<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends AgentOptions<I, O> {
27
+ maxIterations?: number;
28
+ }
29
+ export declare class OrchestratorAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
30
+ static from<I extends AgentInput, O extends AgentOutput>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
31
+ constructor(options: OrchestratorAgentOptions<I, O>);
32
+ private planner;
33
+ private completer;
34
+ maxIterations?: number;
35
+ process(input: I, context?: Context): Promise<O>;
36
+ private getFullPlan;
37
+ private executeStep;
38
+ private formatPlanResult;
39
+ private formatStepsResults;
40
+ }
@@ -0,0 +1,116 @@
1
+ import { AIAgent, Agent, PromptTemplate, getUserInputMessage } from "@aigne/core-next";
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";
3
+ const DEFAULT_MAX_ITERATIONS = 30;
4
+ export class OrchestratorAgent extends Agent {
5
+ static from(options) {
6
+ return new OrchestratorAgent(options);
7
+ }
8
+ constructor(options) {
9
+ super({ ...options });
10
+ this.maxIterations = options.maxIterations;
11
+ this.planner = new AIAgent({
12
+ name: "llm_orchestration_planner",
13
+ instructions: FULL_PLAN_PROMPT_TEMPLATE,
14
+ outputSchema: FullPlanSchema,
15
+ });
16
+ this.completer = new AIAgent({
17
+ name: "llm_orchestration_completer",
18
+ instructions: SYNTHESIZE_PLAN_PROMPT_TEMPLATE,
19
+ outputSchema: this.outputSchema,
20
+ });
21
+ }
22
+ planner;
23
+ completer;
24
+ maxIterations;
25
+ async process(input, context) {
26
+ const model = context?.model;
27
+ if (!model)
28
+ throw new Error("model is required to run OrchestratorAgent");
29
+ const objective = getUserInputMessage(input);
30
+ if (!objective)
31
+ throw new Error("Objective is required to run OrchestratorAgent");
32
+ const result = {
33
+ objective,
34
+ step_results: [],
35
+ };
36
+ let iterations = 0;
37
+ const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
38
+ while (iterations++ < maxIterations) {
39
+ const plan = await this.getFullPlan(objective, result, context);
40
+ result.plan = plan;
41
+ if (plan.is_complete) {
42
+ return this.completer.call({ plan_result: this.formatPlanResult(result) }, context);
43
+ }
44
+ for (const step of plan.steps) {
45
+ const stepResult = await this.executeStep(result, step, context);
46
+ result.step_results.push(stepResult);
47
+ }
48
+ }
49
+ throw new Error(`Max iterations reached: ${maxIterations}`);
50
+ }
51
+ async getFullPlan(objective, planResult, context) {
52
+ const agents = this.tools
53
+ .map((agent, idx) => `${idx + 1}. Agent Name: ${agent.name}
54
+ Description: ${agent.description}
55
+ Functions: ${agent.tools.map((tool) => `- ${tool.name} ${tool.description}`).join("\n")}`)
56
+ .join("\n");
57
+ return this.planner.call({ objective, plan_result: this.formatPlanResult(planResult), agents }, context);
58
+ }
59
+ async executeStep(previousResult, step, context) {
60
+ const model = context?.model;
61
+ if (!model)
62
+ throw new Error("model is required to run OrchestratorAgent");
63
+ const taskResults = [];
64
+ for (const task of step.tasks) {
65
+ const agent = this.tools.find((agent) => agent.name === task.agent);
66
+ if (!agent)
67
+ throw new Error(`Agent ${task.agent} not found`);
68
+ const prompt = PromptTemplate.from(TASK_PROMPT_TEMPLATE).format({
69
+ objective: previousResult.objective,
70
+ task: task.description,
71
+ context: this.formatPlanResult(previousResult),
72
+ });
73
+ let result;
74
+ if (agent.isCallable) {
75
+ result = JSON.stringify(await agent.call(prompt, context));
76
+ }
77
+ else {
78
+ const executor = AIAgent.from({
79
+ instructions: prompt,
80
+ tools: agent.tools,
81
+ });
82
+ result = JSON.stringify(await executor.call({}, context));
83
+ }
84
+ taskResults.push({ ...task, result });
85
+ }
86
+ return {
87
+ step,
88
+ task_results: taskResults,
89
+ };
90
+ }
91
+ formatPlanResult(planResult) {
92
+ return PromptTemplate.from(PLAN_RESULT_TEMPLATE).format({
93
+ plan_objective: planResult.objective,
94
+ steps_str: this.formatStepsResults(planResult.step_results),
95
+ plan_status: planResult.is_complete ? "Complete" : "In Progress",
96
+ plan_result: planResult.result || "No results yet",
97
+ });
98
+ }
99
+ formatStepsResults(stepResults) {
100
+ if (!stepResults.length)
101
+ return "No steps executed yet";
102
+ return stepResults
103
+ .map((stepResult, index) => `${index + 1}:\n${stepResult.task_results.length
104
+ ? PromptTemplate.from(STEP_RESULT_TEMPLATE).format({
105
+ step_description: stepResult.step.description,
106
+ tasks_str: stepResult.task_results
107
+ .map((task) => `- ${PromptTemplate.from(TASK_RESULT_TEMPLATE).format({
108
+ task_description: task.description,
109
+ task_result: task.result,
110
+ })}`)
111
+ .join("\n"),
112
+ })
113
+ : "No result"}`)
114
+ .join("\n\n");
115
+ }
116
+ }
@@ -0,0 +1,91 @@
1
+ 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<{
44
+ steps: z.ZodArray<z.ZodObject<{
45
+ description: z.ZodString;
46
+ tasks: z.ZodArray<z.ZodObject<{
47
+ description: z.ZodString;
48
+ agent: z.ZodString;
49
+ }, "strip", z.ZodTypeAny, {
50
+ description: string;
51
+ agent: string;
52
+ }, {
53
+ description: string;
54
+ agent: string;
55
+ }>, "many">;
56
+ }, "strip", z.ZodTypeAny, {
57
+ description: string;
58
+ tasks: {
59
+ description: string;
60
+ agent: string;
61
+ }[];
62
+ }, {
63
+ description: string;
64
+ tasks: {
65
+ description: string;
66
+ agent: string;
67
+ }[];
68
+ }>, "many">;
69
+ is_complete: z.ZodBoolean;
70
+ }, "strip", z.ZodTypeAny, {
71
+ steps: {
72
+ description: string;
73
+ tasks: {
74
+ description: string;
75
+ agent: string;
76
+ }[];
77
+ }[];
78
+ is_complete: boolean;
79
+ }, {
80
+ steps: {
81
+ description: string;
82
+ tasks: {
83
+ description: string;
84
+ agent: string;
85
+ }[];
86
+ }[];
87
+ is_complete: boolean;
88
+ }>;
89
+ export type Task = z.infer<typeof TaskSchema>;
90
+ export type Step = z.infer<typeof StepSchema>;
91
+ export type FullPlanOutput = z.infer<typeof FullPlanSchema>;
@@ -0,0 +1,54 @@
1
+ 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}}`;
14
+ export const FULL_PLAN_PROMPT_TEMPLATE = `You are tasked with orchestrating a plan to complete an objective.
15
+ You can analyze results from the previous steps already executed to decide if the objective is complete.
16
+ Your plan must be structured in sequential steps, with each step containing independent parallel subtasks.
17
+
18
+ Objective: {{objective}}
19
+
20
+ {{plan_result}}
21
+
22
+ If the previous results achieve the objective, return is_complete=true.
23
+ Otherwise, generate remaining steps needed.
24
+
25
+ You have access to the following Agents(which are collections of tools/functions):
26
+
27
+ Agents:
28
+ {{agents}}
29
+
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:
34
+ 1. Clear description of the task that an LLM can execute
35
+ 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}}.
38
+
39
+ 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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/agent-library",
3
- "version": "1.1.0-beta.9",
3
+ "version": "1.2.0",
4
4
  "description": "Collection of agent libraries for AIGNE framework",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -33,10 +33,10 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "zod": "^3.24.2",
36
- "@aigne/core-next": "^1.1.0-beta.9"
36
+ "@aigne/core-next": "^1.2.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@types/bun": "^1.2.4",
39
+ "@types/bun": "^1.2.5",
40
40
  "npm-run-all": "^4.1.5",
41
41
  "rimraf": "^6.0.1",
42
42
  "typescript": "^5.8.2"