@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
|
@@ -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,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,6 +1,9 @@
|
|
|
1
|
-
import { AIAgent, Agent, PromptTemplate,
|
|
2
|
-
import
|
|
1
|
+
import { AIAgent, Agent, PromptTemplate, createMessage, getMessage, } from "@aigne/core";
|
|
2
|
+
import fastq from "fastq";
|
|
3
|
+
import { FULL_PLAN_PROMPT_TEMPLATE, SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE, SYNTHESIZE_STEP_PROMPT_TEMPLATE, TASK_PROMPT_TEMPLATE, getFullPlanSchema, } from "./orchestrator-prompts.js";
|
|
3
4
|
const DEFAULT_MAX_ITERATIONS = 30;
|
|
5
|
+
const DEFAULT_TASK_CONCURRENCY = 5;
|
|
6
|
+
export * from "./orchestrator-prompts.js";
|
|
4
7
|
export class OrchestratorAgent extends Agent {
|
|
5
8
|
static from(options) {
|
|
6
9
|
return new OrchestratorAgent(options);
|
|
@@ -8,109 +11,127 @@ export class OrchestratorAgent extends Agent {
|
|
|
8
11
|
constructor(options) {
|
|
9
12
|
super({ ...options });
|
|
10
13
|
this.maxIterations = options.maxIterations;
|
|
14
|
+
this.tasksConcurrency = options.tasksConcurrency;
|
|
11
15
|
this.planner = new AIAgent({
|
|
12
16
|
name: "llm_orchestration_planner",
|
|
13
17
|
instructions: FULL_PLAN_PROMPT_TEMPLATE,
|
|
14
|
-
outputSchema:
|
|
18
|
+
outputSchema: () => getFullPlanSchema(this.tools),
|
|
15
19
|
});
|
|
16
20
|
this.completer = new AIAgent({
|
|
17
21
|
name: "llm_orchestration_completer",
|
|
18
|
-
instructions:
|
|
22
|
+
instructions: FULL_PLAN_PROMPT_TEMPLATE,
|
|
19
23
|
outputSchema: this.outputSchema,
|
|
20
24
|
});
|
|
21
25
|
}
|
|
22
26
|
planner;
|
|
23
27
|
completer;
|
|
24
28
|
maxIterations;
|
|
29
|
+
tasksConcurrency;
|
|
25
30
|
async process(input, context) {
|
|
26
31
|
const model = context?.model;
|
|
27
32
|
if (!model)
|
|
28
33
|
throw new Error("model is required to run OrchestratorAgent");
|
|
29
|
-
const objective =
|
|
34
|
+
const objective = getMessage(input);
|
|
30
35
|
if (!objective)
|
|
31
36
|
throw new Error("Objective is required to run OrchestratorAgent");
|
|
32
37
|
const result = {
|
|
33
38
|
objective,
|
|
34
|
-
|
|
39
|
+
steps: [],
|
|
35
40
|
};
|
|
36
41
|
let iterations = 0;
|
|
37
42
|
const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
38
43
|
while (iterations++ < maxIterations) {
|
|
39
|
-
const plan = await this.getFullPlan(
|
|
44
|
+
const plan = await this.getFullPlan(result, context);
|
|
40
45
|
result.plan = plan;
|
|
41
|
-
if (plan.
|
|
42
|
-
return this.
|
|
46
|
+
if (plan.isComplete) {
|
|
47
|
+
return this.synthesizePlanResult(result, context);
|
|
43
48
|
}
|
|
44
49
|
for (const step of plan.steps) {
|
|
45
50
|
const stepResult = await this.executeStep(result, step, context);
|
|
46
|
-
result.
|
|
51
|
+
result.steps.push(stepResult);
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
54
|
throw new Error(`Max iterations reached: ${maxIterations}`);
|
|
50
55
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
getFullPlanInput(planResult) {
|
|
57
|
+
return {
|
|
58
|
+
objective: planResult.objective,
|
|
59
|
+
steps: planResult.steps,
|
|
60
|
+
plan: {
|
|
61
|
+
status: planResult.status ? "Complete" : "In Progress",
|
|
62
|
+
result: planResult.result || "No results yet",
|
|
63
|
+
},
|
|
64
|
+
agents: this.tools.map((i) => ({
|
|
65
|
+
name: i.name,
|
|
66
|
+
description: i.description,
|
|
67
|
+
tools: i.tools.map((i) => ({ name: i.name, description: i.description })),
|
|
68
|
+
})),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async getFullPlan(planResult, context) {
|
|
72
|
+
return context.call(this.planner, this.getFullPlanInput(planResult));
|
|
73
|
+
}
|
|
74
|
+
async synthesizePlanResult(planResult, context) {
|
|
75
|
+
return context.call(this.completer, {
|
|
76
|
+
...this.getFullPlanInput(planResult),
|
|
77
|
+
...createMessage(SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE),
|
|
78
|
+
});
|
|
58
79
|
}
|
|
59
|
-
async executeStep(
|
|
80
|
+
async executeStep(planResult, step, context) {
|
|
81
|
+
const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
|
|
60
82
|
const model = context?.model;
|
|
61
83
|
if (!model)
|
|
62
84
|
throw new Error("model is required to run OrchestratorAgent");
|
|
63
|
-
const
|
|
64
|
-
for (const task of step.tasks) {
|
|
85
|
+
const queue = fastq.promise(async (task) => {
|
|
65
86
|
const agent = this.tools.find((agent) => agent.name === task.agent);
|
|
66
87
|
if (!agent)
|
|
67
88
|
throw new Error(`Agent ${task.agent} not found`);
|
|
68
89
|
const prompt = PromptTemplate.from(TASK_PROMPT_TEMPLATE).format({
|
|
69
|
-
objective:
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
objective: planResult.objective,
|
|
91
|
+
step,
|
|
92
|
+
task,
|
|
93
|
+
steps: planResult.steps,
|
|
72
94
|
});
|
|
73
95
|
let result;
|
|
74
96
|
if (agent.isCallable) {
|
|
75
|
-
result =
|
|
97
|
+
result = getMessageOrJsonString(await context.call(agent, prompt));
|
|
76
98
|
}
|
|
77
99
|
else {
|
|
78
100
|
const executor = AIAgent.from({
|
|
101
|
+
name: "llm_orchestration_task_executor",
|
|
79
102
|
instructions: prompt,
|
|
80
103
|
tools: agent.tools,
|
|
81
104
|
});
|
|
82
|
-
result =
|
|
105
|
+
result = getMessageOrJsonString(await context.call(executor, {}));
|
|
83
106
|
}
|
|
84
|
-
|
|
107
|
+
return { task, result };
|
|
108
|
+
}, concurrency);
|
|
109
|
+
let results;
|
|
110
|
+
try {
|
|
111
|
+
results = await Promise.all(step.tasks.map((task) => queue.push(task)));
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
queue.kill();
|
|
115
|
+
throw error;
|
|
85
116
|
}
|
|
117
|
+
const result = getMessageOrJsonString(await context.call(AIAgent.from({
|
|
118
|
+
name: "llm_orchestration_step_synthesizer",
|
|
119
|
+
instructions: SYNTHESIZE_STEP_PROMPT_TEMPLATE,
|
|
120
|
+
}), { objective: planResult.objective, step, tasks: results }));
|
|
121
|
+
if (!result)
|
|
122
|
+
throw new Error("unexpected empty result from synthesize step's tasks results");
|
|
86
123
|
return {
|
|
87
124
|
step,
|
|
88
|
-
|
|
125
|
+
tasks: results,
|
|
126
|
+
result,
|
|
89
127
|
};
|
|
90
128
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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");
|
|
129
|
+
}
|
|
130
|
+
function getMessageOrJsonString(output) {
|
|
131
|
+
const entries = Object.entries(output);
|
|
132
|
+
const firstValue = entries[0]?.[1];
|
|
133
|
+
if (entries.length === 1 && typeof firstValue === "string") {
|
|
134
|
+
return firstValue;
|
|
115
135
|
}
|
|
136
|
+
return JSON.stringify(output);
|
|
116
137
|
}
|
|
@@ -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,54 +1,116 @@
|
|
|
1
|
+
import { ensureZodUnionArray } from "@aigne/core";
|
|
2
|
+
import { groupBy, pickBy } from "lodash-es";
|
|
1
3
|
import { z } from "zod";
|
|
2
|
-
export const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
32
|
+
<objective>
|
|
33
|
+
{{objective}}
|
|
34
|
+
</objective>
|
|
19
35
|
|
|
20
|
-
|
|
36
|
+
<steps_completed>
|
|
37
|
+
{{#steps}}
|
|
38
|
+
- Step: {{step.description}}
|
|
39
|
+
Result: {{result}}
|
|
40
|
+
{{/steps}}
|
|
41
|
+
</steps_completed>
|
|
21
42
|
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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 =
|
|
37
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
{{
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|