@aigne/agent-library 1.2.0 → 1.3.1
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 +20 -0
- package/lib/cjs/orchestrator/index.d.ts +13 -26
- package/lib/cjs/orchestrator/index.js +94 -56
- package/lib/cjs/orchestrator/orchestrator-prompts.d.ts +49 -49
- package/lib/cjs/orchestrator/orchestrator-prompts.js +103 -40
- package/lib/cjs/package.json +1 -0
- package/lib/dts/orchestrator/index.d.ts +13 -26
- package/lib/dts/orchestrator/orchestrator-prompts.d.ts +49 -49
- package/lib/esm/orchestrator/index.d.ts +13 -26
- package/lib/esm/orchestrator/index.js +72 -51
- package/lib/esm/orchestrator/orchestrator-prompts.d.ts +49 -49
- package/lib/esm/orchestrator/orchestrator-prompts.js +101 -39
- package/lib/esm/package.json +1 -0
- package/package.json +7 -5
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
- chore: release 1.2.0
|
|
4
4
|
|
|
5
|
+
## [1.3.1](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.3.0...agent-library-v1.3.1) (2025-03-26)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **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))
|
|
11
|
+
|
|
12
|
+
## [1.3.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.2.0...agent-library-v1.3.0) (2025-03-24)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* **core:** add ChatModelClaude to use models of anthropic ([#30](https://github.com/AIGNE-io/aigne-framework/issues/30)) ([0a62a64](https://github.com/AIGNE-io/aigne-framework/commit/0a62a6499e3da723a4646e67952051708ce7de6a))
|
|
18
|
+
* **core:** add support for subscribing topics for agent memory ([#28](https://github.com/AIGNE-io/aigne-framework/issues/28)) ([eeecc67](https://github.com/AIGNE-io/aigne-framework/commit/eeecc67049a60ebcc4cdba0fbcd987b3d81f4af6))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* rename @aigne/core-next to @aigne/core ([3a81009](https://github.com/AIGNE-io/aigne-framework/commit/3a8100962c81813217b687ae28e8de604419c622))
|
|
24
|
+
|
|
5
25
|
## 1.1.0-beta.17 (2025-3-18)
|
|
6
26
|
|
|
7
27
|
- chore: add support for esm module
|
|
@@ -1,40 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
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 {
|
|
1
|
+
import { Agent, type AgentOptions, type Context, type Message } from "@aigne/core";
|
|
2
|
+
import { type FullPlanOutput, type StepWithResult } from "./orchestrator-prompts.js";
|
|
3
|
+
export * from "./orchestrator-prompts.js";
|
|
4
|
+
export interface FullPlanWithResult {
|
|
15
5
|
objective: string;
|
|
16
6
|
plan?: FullPlanOutput;
|
|
17
|
-
|
|
7
|
+
steps: StepWithResult[];
|
|
18
8
|
result?: string;
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
export interface FullPlanInput extends AgentInput {
|
|
22
|
-
objective: string;
|
|
23
|
-
plan_result: string;
|
|
24
|
-
agents: string;
|
|
9
|
+
status?: boolean;
|
|
25
10
|
}
|
|
26
|
-
export interface OrchestratorAgentOptions<I extends
|
|
11
|
+
export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
27
12
|
maxIterations?: number;
|
|
13
|
+
tasksConcurrency?: number;
|
|
28
14
|
}
|
|
29
|
-
export declare class OrchestratorAgent<I extends
|
|
30
|
-
static from<I extends
|
|
15
|
+
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
16
|
+
static from<I extends Message, O extends Message>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
|
|
31
17
|
constructor(options: OrchestratorAgentOptions<I, O>);
|
|
32
18
|
private planner;
|
|
33
19
|
private completer;
|
|
34
20
|
maxIterations?: number;
|
|
21
|
+
tasksConcurrency?: number;
|
|
35
22
|
process(input: I, context?: Context): Promise<O>;
|
|
23
|
+
private getFullPlanInput;
|
|
36
24
|
private getFullPlan;
|
|
25
|
+
private synthesizePlanResult;
|
|
37
26
|
private executeStep;
|
|
38
|
-
private formatPlanResult;
|
|
39
|
-
private formatStepsResults;
|
|
40
27
|
}
|
|
@@ -1,120 +1,158 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
2
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
20
|
exports.OrchestratorAgent = void 0;
|
|
4
|
-
const
|
|
21
|
+
const core_1 = require("@aigne/core");
|
|
22
|
+
const fastq_1 = __importDefault(require("fastq"));
|
|
5
23
|
const orchestrator_prompts_js_1 = require("./orchestrator-prompts.js");
|
|
6
24
|
const DEFAULT_MAX_ITERATIONS = 30;
|
|
7
|
-
|
|
25
|
+
const DEFAULT_TASK_CONCURRENCY = 5;
|
|
26
|
+
__exportStar(require("./orchestrator-prompts.js"), exports);
|
|
27
|
+
class OrchestratorAgent extends core_1.Agent {
|
|
8
28
|
static from(options) {
|
|
9
29
|
return new OrchestratorAgent(options);
|
|
10
30
|
}
|
|
11
31
|
constructor(options) {
|
|
12
32
|
super({ ...options });
|
|
13
33
|
this.maxIterations = options.maxIterations;
|
|
14
|
-
this.
|
|
34
|
+
this.tasksConcurrency = options.tasksConcurrency;
|
|
35
|
+
this.planner = new core_1.AIAgent({
|
|
15
36
|
name: "llm_orchestration_planner",
|
|
16
37
|
instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
|
|
17
|
-
outputSchema: orchestrator_prompts_js_1.
|
|
38
|
+
outputSchema: () => (0, orchestrator_prompts_js_1.getFullPlanSchema)(this.tools),
|
|
18
39
|
});
|
|
19
|
-
this.completer = new
|
|
40
|
+
this.completer = new core_1.AIAgent({
|
|
20
41
|
name: "llm_orchestration_completer",
|
|
21
|
-
instructions: orchestrator_prompts_js_1.
|
|
42
|
+
instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
|
|
22
43
|
outputSchema: this.outputSchema,
|
|
23
44
|
});
|
|
24
45
|
}
|
|
25
46
|
planner;
|
|
26
47
|
completer;
|
|
27
48
|
maxIterations;
|
|
49
|
+
tasksConcurrency;
|
|
28
50
|
async process(input, context) {
|
|
29
51
|
const model = context?.model;
|
|
30
52
|
if (!model)
|
|
31
53
|
throw new Error("model is required to run OrchestratorAgent");
|
|
32
|
-
const objective = (0,
|
|
54
|
+
const objective = (0, core_1.getMessage)(input);
|
|
33
55
|
if (!objective)
|
|
34
56
|
throw new Error("Objective is required to run OrchestratorAgent");
|
|
35
57
|
const result = {
|
|
36
58
|
objective,
|
|
37
|
-
|
|
59
|
+
steps: [],
|
|
38
60
|
};
|
|
39
61
|
let iterations = 0;
|
|
40
62
|
const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
41
63
|
while (iterations++ < maxIterations) {
|
|
42
|
-
const plan = await this.getFullPlan(
|
|
64
|
+
const plan = await this.getFullPlan(result, context);
|
|
43
65
|
result.plan = plan;
|
|
44
|
-
if (plan.
|
|
45
|
-
return this.
|
|
66
|
+
if (plan.isComplete) {
|
|
67
|
+
return this.synthesizePlanResult(result, context);
|
|
46
68
|
}
|
|
47
69
|
for (const step of plan.steps) {
|
|
48
70
|
const stepResult = await this.executeStep(result, step, context);
|
|
49
|
-
result.
|
|
71
|
+
result.steps.push(stepResult);
|
|
50
72
|
}
|
|
51
73
|
}
|
|
52
74
|
throw new Error(`Max iterations reached: ${maxIterations}`);
|
|
53
75
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
76
|
+
getFullPlanInput(planResult) {
|
|
77
|
+
return {
|
|
78
|
+
objective: planResult.objective,
|
|
79
|
+
steps: planResult.steps,
|
|
80
|
+
plan: {
|
|
81
|
+
status: planResult.status ? "Complete" : "In Progress",
|
|
82
|
+
result: planResult.result || "No results yet",
|
|
83
|
+
},
|
|
84
|
+
agents: this.tools.map((i) => ({
|
|
85
|
+
name: i.name,
|
|
86
|
+
description: i.description,
|
|
87
|
+
tools: i.tools.map((i) => ({ name: i.name, description: i.description })),
|
|
88
|
+
})),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
async getFullPlan(planResult, context) {
|
|
92
|
+
return context.call(this.planner, this.getFullPlanInput(planResult));
|
|
93
|
+
}
|
|
94
|
+
async synthesizePlanResult(planResult, context) {
|
|
95
|
+
return context.call(this.completer, {
|
|
96
|
+
...this.getFullPlanInput(planResult),
|
|
97
|
+
...(0, core_1.createMessage)(orchestrator_prompts_js_1.SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE),
|
|
98
|
+
});
|
|
61
99
|
}
|
|
62
|
-
async executeStep(
|
|
100
|
+
async executeStep(planResult, step, context) {
|
|
101
|
+
const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
|
|
63
102
|
const model = context?.model;
|
|
64
103
|
if (!model)
|
|
65
104
|
throw new Error("model is required to run OrchestratorAgent");
|
|
66
|
-
const
|
|
67
|
-
for (const task of step.tasks) {
|
|
105
|
+
const queue = fastq_1.default.promise(async (task) => {
|
|
68
106
|
const agent = this.tools.find((agent) => agent.name === task.agent);
|
|
69
107
|
if (!agent)
|
|
70
108
|
throw new Error(`Agent ${task.agent} not found`);
|
|
71
|
-
const prompt =
|
|
72
|
-
objective:
|
|
73
|
-
|
|
74
|
-
|
|
109
|
+
const prompt = core_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_PROMPT_TEMPLATE).format({
|
|
110
|
+
objective: planResult.objective,
|
|
111
|
+
step,
|
|
112
|
+
task,
|
|
113
|
+
steps: planResult.steps,
|
|
75
114
|
});
|
|
76
115
|
let result;
|
|
77
116
|
if (agent.isCallable) {
|
|
78
|
-
result =
|
|
117
|
+
result = getMessageOrJsonString(await context.call(agent, prompt));
|
|
79
118
|
}
|
|
80
119
|
else {
|
|
81
|
-
const executor =
|
|
120
|
+
const executor = core_1.AIAgent.from({
|
|
121
|
+
name: "llm_orchestration_task_executor",
|
|
82
122
|
instructions: prompt,
|
|
83
123
|
tools: agent.tools,
|
|
84
124
|
});
|
|
85
|
-
result =
|
|
125
|
+
result = getMessageOrJsonString(await context.call(executor, {}));
|
|
86
126
|
}
|
|
87
|
-
|
|
127
|
+
return { task, result };
|
|
128
|
+
}, concurrency);
|
|
129
|
+
let results;
|
|
130
|
+
try {
|
|
131
|
+
results = await Promise.all(step.tasks.map((task) => queue.push(task)));
|
|
88
132
|
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
queue.kill();
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
const result = getMessageOrJsonString(await context.call(core_1.AIAgent.from({
|
|
138
|
+
name: "llm_orchestration_step_synthesizer",
|
|
139
|
+
instructions: orchestrator_prompts_js_1.SYNTHESIZE_STEP_PROMPT_TEMPLATE,
|
|
140
|
+
}), { objective: planResult.objective, step, tasks: results }));
|
|
141
|
+
if (!result)
|
|
142
|
+
throw new Error("unexpected empty result from synthesize step's tasks results");
|
|
89
143
|
return {
|
|
90
144
|
step,
|
|
91
|
-
|
|
145
|
+
tasks: results,
|
|
146
|
+
result,
|
|
92
147
|
};
|
|
93
148
|
}
|
|
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
149
|
}
|
|
120
150
|
exports.OrchestratorAgent = OrchestratorAgent;
|
|
151
|
+
function getMessageOrJsonString(output) {
|
|
152
|
+
const entries = Object.entries(output);
|
|
153
|
+
const firstValue = entries[0]?.[1];
|
|
154
|
+
if (entries.length === 1 && typeof firstValue === "string") {
|
|
155
|
+
return firstValue;
|
|
156
|
+
}
|
|
157
|
+
return JSON.stringify(output);
|
|
158
|
+
}
|
|
@@ -1,51 +1,12 @@
|
|
|
1
|
+
import { type Agent, type Message } from "@aigne/core";
|
|
1
2
|
import { z } from "zod";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
48
|
+
isComplete: boolean;
|
|
88
49
|
}>;
|
|
89
|
-
export type
|
|
90
|
-
export type Step =
|
|
91
|
-
export type
|
|
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.
|
|
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 core_1 = require("@aigne/core");
|
|
6
|
+
const lodash_es_1 = require("lodash-es");
|
|
4
7
|
const zod_1 = require("zod");
|
|
5
|
-
exports.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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, core_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
|
-
|
|
36
|
+
<objective>
|
|
37
|
+
{{objective}}
|
|
38
|
+
</objective>
|
|
22
39
|
|
|
23
|
-
|
|
40
|
+
<steps_completed>
|
|
41
|
+
{{#steps}}
|
|
42
|
+
- Step: {{step.description}}
|
|
43
|
+
Result: {{result}}
|
|
44
|
+
{{/steps}}
|
|
45
|
+
</steps_completed>
|
|
24
46
|
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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 =
|
|
40
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
{{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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,40 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
|
|
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 {
|
|
1
|
+
import { Agent, type AgentOptions, type Context, type Message } from "@aigne/core";
|
|
2
|
+
import { type FullPlanOutput, type StepWithResult } from "./orchestrator-prompts.js";
|
|
3
|
+
export * from "./orchestrator-prompts.js";
|
|
4
|
+
export interface FullPlanWithResult {
|
|
15
5
|
objective: string;
|
|
16
6
|
plan?: FullPlanOutput;
|
|
17
|
-
|
|
7
|
+
steps: StepWithResult[];
|
|
18
8
|
result?: string;
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
export interface FullPlanInput extends AgentInput {
|
|
22
|
-
objective: string;
|
|
23
|
-
plan_result: string;
|
|
24
|
-
agents: string;
|
|
9
|
+
status?: boolean;
|
|
25
10
|
}
|
|
26
|
-
export interface OrchestratorAgentOptions<I extends
|
|
11
|
+
export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
27
12
|
maxIterations?: number;
|
|
13
|
+
tasksConcurrency?: number;
|
|
28
14
|
}
|
|
29
|
-
export declare class OrchestratorAgent<I extends
|
|
30
|
-
static from<I extends
|
|
15
|
+
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
16
|
+
static from<I extends Message, O extends Message>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
|
|
31
17
|
constructor(options: OrchestratorAgentOptions<I, O>);
|
|
32
18
|
private planner;
|
|
33
19
|
private completer;
|
|
34
20
|
maxIterations?: number;
|
|
21
|
+
tasksConcurrency?: number;
|
|
35
22
|
process(input: I, context?: Context): Promise<O>;
|
|
23
|
+
private getFullPlanInput;
|
|
36
24
|
private getFullPlan;
|
|
25
|
+
private synthesizePlanResult;
|
|
37
26
|
private executeStep;
|
|
38
|
-
private formatPlanResult;
|
|
39
|
-
private formatStepsResults;
|
|
40
27
|
}
|