@aigne/agent-library 1.22.4-beta → 1.23.0-beta
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 +25 -0
- package/README.md +6 -94
- package/lib/cjs/orchestrator/index.d.ts +39 -71
- package/lib/cjs/orchestrator/index.js +174 -169
- package/lib/cjs/orchestrator/prompt.d.ts +3 -0
- package/lib/cjs/orchestrator/prompt.js +82 -0
- package/lib/cjs/orchestrator/type.d.ts +505 -0
- package/lib/cjs/orchestrator/type.js +83 -0
- package/lib/dts/orchestrator/index.d.ts +39 -71
- package/lib/dts/orchestrator/prompt.d.ts +3 -0
- package/lib/dts/orchestrator/type.d.ts +505 -0
- package/lib/esm/orchestrator/index.d.ts +39 -71
- package/lib/esm/orchestrator/index.js +176 -154
- package/lib/esm/orchestrator/prompt.d.ts +3 -0
- package/lib/esm/orchestrator/prompt.js +79 -0
- package/lib/esm/orchestrator/type.d.ts +505 -0
- package/lib/esm/orchestrator/type.js +77 -0
- package/package.json +12 -6
- package/lib/cjs/orchestrator/orchestrator-prompts.d.ts +0 -130
- package/lib/cjs/orchestrator/orchestrator-prompts.js +0 -135
- package/lib/dts/orchestrator/orchestrator-prompts.d.ts +0 -130
- package/lib/esm/orchestrator/orchestrator-prompts.d.ts +0 -130
- package/lib/esm/orchestrator/orchestrator-prompts.js +0 -131
|
@@ -1,50 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
export * from "./orchestrator-prompts.js";
|
|
7
|
-
/**
|
|
8
|
-
* Represents a complete plan with execution results
|
|
9
|
-
* @hidden
|
|
10
|
-
*/
|
|
11
|
-
export interface FullPlanWithResult {
|
|
12
|
-
/**
|
|
13
|
-
* The overall objective
|
|
14
|
-
*/
|
|
15
|
-
objective: string;
|
|
16
|
-
/**
|
|
17
|
-
* The generated complete plan
|
|
18
|
-
*/
|
|
19
|
-
plan?: FullPlanOutput;
|
|
20
|
-
/**
|
|
21
|
-
* List of executed steps with their results
|
|
22
|
-
*/
|
|
23
|
-
steps: StepWithResult[];
|
|
24
|
-
/**
|
|
25
|
-
* Final result
|
|
26
|
-
*/
|
|
27
|
-
result?: string;
|
|
28
|
-
/**
|
|
29
|
-
* Plan completion status
|
|
30
|
-
*/
|
|
31
|
-
status?: boolean;
|
|
32
|
-
}
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, AIAgent, type AIAgentOptions, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
+
import { type Instructions, type NestAgentSchema } from "@aigne/core/loader/agent-yaml.js";
|
|
3
|
+
import { type LoadOptions } from "@aigne/core/loader/index.js";
|
|
4
|
+
import { type StateManagementOptions } from "./type.js";
|
|
33
5
|
/**
|
|
34
6
|
* Configuration options for the Orchestrator Agent
|
|
35
7
|
*/
|
|
36
|
-
export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
maxIterations?: number;
|
|
8
|
+
export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends Omit<AIAgentOptions<I, O>, "instructions"> {
|
|
9
|
+
objective: PromptBuilder;
|
|
10
|
+
planner?: OrchestratorAgent<I, O>["planner"];
|
|
11
|
+
worker?: OrchestratorAgent<I, O>["worker"];
|
|
12
|
+
completer?: OrchestratorAgent<I, O>["completer"];
|
|
42
13
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
14
|
+
* Configuration for managing execution state
|
|
15
|
+
* Prevents context overflow during long-running executions
|
|
45
16
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
17
|
+
stateManagement?: StateManagementOptions;
|
|
18
|
+
}
|
|
19
|
+
export interface LoadOrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends Omit<AIAgentOptions<I, O>, "instructions"> {
|
|
20
|
+
objective: string | PromptBuilder | Instructions;
|
|
21
|
+
planner?: NestAgentSchema | {
|
|
22
|
+
instructions?: string | PromptBuilder | Instructions;
|
|
23
|
+
};
|
|
24
|
+
worker?: NestAgentSchema | {
|
|
25
|
+
instructions?: string | PromptBuilder | Instructions;
|
|
26
|
+
};
|
|
27
|
+
completer?: NestAgentSchema | {
|
|
28
|
+
instructions?: string | PromptBuilder | Instructions;
|
|
29
|
+
};
|
|
30
|
+
stateManagement?: StateManagementOptions;
|
|
48
31
|
}
|
|
49
32
|
/**
|
|
50
33
|
* Orchestrator Agent Class
|
|
@@ -61,8 +44,13 @@ export interface OrchestratorAgentOptions<I extends Message = Message, O extends
|
|
|
61
44
|
* - Executes tasks and steps according to the plan
|
|
62
45
|
* - Synthesizes final result through completer
|
|
63
46
|
*/
|
|
64
|
-
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends
|
|
47
|
+
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends AIAgent<I, O> {
|
|
65
48
|
tag: string;
|
|
49
|
+
static load<I extends Message = Message, O extends Message = Message>({ filepath, parsed, options, }: {
|
|
50
|
+
filepath: string;
|
|
51
|
+
parsed: AgentOptions<I, O> & LoadOrchestratorAgentOptions<I, O>;
|
|
52
|
+
options?: LoadOptions;
|
|
53
|
+
}): Promise<OrchestratorAgent<I, O>>;
|
|
66
54
|
/**
|
|
67
55
|
* Factory method to create an OrchestratorAgent instance
|
|
68
56
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -74,36 +62,16 @@ export declare class OrchestratorAgent<I extends Message = Message, O extends Me
|
|
|
74
62
|
* @param options - Configuration options for the Orchestrator Agent
|
|
75
63
|
*/
|
|
76
64
|
constructor(options: OrchestratorAgentOptions<I, O>);
|
|
65
|
+
private objective;
|
|
77
66
|
private planner;
|
|
67
|
+
private worker;
|
|
78
68
|
private completer;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Maximum number of iterations
|
|
82
|
-
* Prevents infinite execution loops
|
|
83
|
-
*/
|
|
84
|
-
maxIterations?: number;
|
|
85
|
-
/**
|
|
86
|
-
* Number of concurrent tasks
|
|
87
|
-
* Controls how many tasks can be executed simultaneously
|
|
88
|
-
*/
|
|
89
|
-
tasksConcurrency?: number;
|
|
69
|
+
private stateManagement?;
|
|
90
70
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* Workflow:
|
|
94
|
-
* 1. Extract the objective
|
|
95
|
-
* 2. Loop until plan completion or maximum iterations:
|
|
96
|
-
* a. Generate/update execution plan
|
|
97
|
-
* b. If plan is complete, synthesize result
|
|
98
|
-
* c. Otherwise, execute steps in the plan
|
|
99
|
-
*
|
|
100
|
-
* @param input - Input message containing the objective
|
|
101
|
-
* @param options - Agent invocation options
|
|
102
|
-
* @returns Processing result
|
|
71
|
+
* Compress execution state to prevent context overflow
|
|
72
|
+
* Uses reverse accumulation to efficiently find optimal task count
|
|
103
73
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
private getFullPlan;
|
|
107
|
-
private synthesizePlanResult;
|
|
108
|
-
private executeStep;
|
|
74
|
+
private compressState;
|
|
75
|
+
process(input: I, options: AgentInvokeOptions): AsyncGenerator<import("@aigne/core").AgentResponseChunk<O>, void, unknown>;
|
|
109
76
|
}
|
|
77
|
+
export default OrchestratorAgent;
|
|
@@ -1,20 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { AIAgent, } from "@aigne/core";
|
|
2
|
+
import { getInstructionsSchema, getNestAgentSchema, } from "@aigne/core/loader/agent-yaml.js";
|
|
3
|
+
import { instructionsToPromptBuilder, loadNestAgent, } from "@aigne/core/loader/index.js";
|
|
4
|
+
import { camelizeSchema, optionalize } from "@aigne/core/loader/schema.js";
|
|
5
|
+
import { isAgent } from "@aigne/core/utils/agent-utils.js";
|
|
6
|
+
import { estimateTokens } from "@aigne/core/utils/token-estimator.js";
|
|
7
|
+
import { omit } from "@aigne/core/utils/type-utils.js";
|
|
4
8
|
import { z } from "zod";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
* Default maximum number of iterations to prevent infinite loops
|
|
8
|
-
*/
|
|
9
|
-
const DEFAULT_MAX_ITERATIONS = 30;
|
|
10
|
-
/**
|
|
11
|
-
* Default number of concurrent tasks
|
|
12
|
-
*/
|
|
13
|
-
const DEFAULT_TASK_CONCURRENCY = 5;
|
|
14
|
-
/**
|
|
15
|
-
* Re-export orchestrator prompt templates and related types
|
|
16
|
-
*/
|
|
17
|
-
export * from "./orchestrator-prompts.js";
|
|
9
|
+
import { ORCHESTRATOR_COMPLETE_PROMPT, TODO_PLANNER_PROMPT_TEMPLATE, TODO_WORKER_PROMPT_TEMPLATE, } from "./prompt.js";
|
|
10
|
+
import { completerInputSchema, DEFAULT_MAX_ITERATIONS, plannerInputSchema, plannerOutputSchema, stateManagementOptionsSchema, workerInputSchema, workerOutputSchema, } from "./type.js";
|
|
18
11
|
/**
|
|
19
12
|
* Orchestrator Agent Class
|
|
20
13
|
*
|
|
@@ -30,8 +23,46 @@ export * from "./orchestrator-prompts.js";
|
|
|
30
23
|
* - Executes tasks and steps according to the plan
|
|
31
24
|
* - Synthesizes final result through completer
|
|
32
25
|
*/
|
|
33
|
-
export class OrchestratorAgent extends
|
|
26
|
+
export class OrchestratorAgent extends AIAgent {
|
|
34
27
|
tag = "OrchestratorAgent";
|
|
28
|
+
static async load({ filepath, parsed, options, }) {
|
|
29
|
+
const schema = getOrchestratorAgentSchema({ filepath });
|
|
30
|
+
const valid = await schema.parseAsync(parsed);
|
|
31
|
+
return new OrchestratorAgent({
|
|
32
|
+
...parsed,
|
|
33
|
+
objective: instructionsToPromptBuilder(valid.objective),
|
|
34
|
+
planner: valid.planner
|
|
35
|
+
? (await loadNestAgent(filepath, valid.planner, options, {
|
|
36
|
+
name: "Planner",
|
|
37
|
+
instructions: TODO_PLANNER_PROMPT_TEMPLATE,
|
|
38
|
+
inputSchema: plannerInputSchema,
|
|
39
|
+
outputSchema: plannerOutputSchema,
|
|
40
|
+
afs: parsed.afs,
|
|
41
|
+
skills: parsed.skills,
|
|
42
|
+
}))
|
|
43
|
+
: undefined,
|
|
44
|
+
worker: valid.worker
|
|
45
|
+
? (await loadNestAgent(filepath, valid.worker, options, {
|
|
46
|
+
name: "Worker",
|
|
47
|
+
instructions: TODO_WORKER_PROMPT_TEMPLATE,
|
|
48
|
+
inputSchema: workerInputSchema,
|
|
49
|
+
outputSchema: workerOutputSchema,
|
|
50
|
+
afs: parsed.afs,
|
|
51
|
+
skills: parsed.skills,
|
|
52
|
+
}))
|
|
53
|
+
: undefined,
|
|
54
|
+
completer: valid.completer
|
|
55
|
+
? (await loadNestAgent(filepath, valid.completer, options, {
|
|
56
|
+
instructions: ORCHESTRATOR_COMPLETE_PROMPT,
|
|
57
|
+
inputSchema: completerInputSchema,
|
|
58
|
+
outputSchema: parsed.outputSchema,
|
|
59
|
+
afs: parsed.afs,
|
|
60
|
+
skills: parsed.skills,
|
|
61
|
+
}))
|
|
62
|
+
: undefined,
|
|
63
|
+
stateManagement: valid.stateManagement,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
35
66
|
/**
|
|
36
67
|
* Factory method to create an OrchestratorAgent instance
|
|
37
68
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -45,158 +76,149 @@ export class OrchestratorAgent extends Agent {
|
|
|
45
76
|
* @param options - Configuration options for the Orchestrator Agent
|
|
46
77
|
*/
|
|
47
78
|
constructor(options) {
|
|
48
|
-
checkArguments("OrchestratorAgent", orchestratorAgentOptionsSchema, options);
|
|
49
79
|
super({ ...options });
|
|
50
|
-
this.
|
|
51
|
-
this.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
80
|
+
this.objective = options.objective;
|
|
81
|
+
this.planner = isAgent(options.planner)
|
|
82
|
+
? options.planner
|
|
83
|
+
: new AIAgent({
|
|
84
|
+
...options,
|
|
85
|
+
name: "Planner",
|
|
86
|
+
instructions: TODO_PLANNER_PROMPT_TEMPLATE,
|
|
87
|
+
inputSchema: plannerInputSchema,
|
|
88
|
+
outputSchema: plannerOutputSchema,
|
|
89
|
+
});
|
|
90
|
+
this.worker = isAgent(options.worker)
|
|
91
|
+
? options.worker
|
|
92
|
+
: new AIAgent({
|
|
93
|
+
...options,
|
|
94
|
+
name: "Worker",
|
|
95
|
+
instructions: TODO_WORKER_PROMPT_TEMPLATE,
|
|
96
|
+
inputSchema: workerInputSchema,
|
|
97
|
+
outputSchema: workerOutputSchema,
|
|
98
|
+
});
|
|
99
|
+
this.completer = isAgent(options.completer)
|
|
100
|
+
? options.completer
|
|
101
|
+
: new AIAgent({
|
|
102
|
+
...omit(options, "inputSchema"),
|
|
103
|
+
name: "Completer",
|
|
104
|
+
instructions: ORCHESTRATOR_COMPLETE_PROMPT,
|
|
105
|
+
outputKey: options.outputKey,
|
|
106
|
+
inputSchema: completerInputSchema,
|
|
107
|
+
outputSchema: options.outputSchema,
|
|
108
|
+
});
|
|
109
|
+
// Initialize state management config
|
|
110
|
+
this.stateManagement = options.stateManagement;
|
|
63
111
|
}
|
|
112
|
+
objective;
|
|
64
113
|
planner;
|
|
114
|
+
worker;
|
|
65
115
|
completer;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Maximum number of iterations
|
|
69
|
-
* Prevents infinite execution loops
|
|
70
|
-
*/
|
|
71
|
-
maxIterations;
|
|
116
|
+
stateManagement;
|
|
72
117
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
118
|
+
* Compress execution state to prevent context overflow
|
|
119
|
+
* Uses reverse accumulation to efficiently find optimal task count
|
|
75
120
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
102
|
-
let iterations = 0;
|
|
103
|
-
const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
104
|
-
while (iterations++ < maxIterations) {
|
|
105
|
-
const plan = await this.getFullPlan(result, options.context);
|
|
106
|
-
result.plan = plan;
|
|
107
|
-
if (plan.isComplete) {
|
|
108
|
-
return this.synthesizePlanResult(result, options.context);
|
|
121
|
+
compressState(state) {
|
|
122
|
+
const { maxTokens, keepRecent } = this.stateManagement ?? {};
|
|
123
|
+
if (!maxTokens && !keepRecent) {
|
|
124
|
+
return state;
|
|
125
|
+
}
|
|
126
|
+
const tasks = state.tasks;
|
|
127
|
+
let selectedTasks = tasks;
|
|
128
|
+
// Step 1: Apply keepRecent limit if configured
|
|
129
|
+
if (keepRecent && tasks.length > keepRecent) {
|
|
130
|
+
selectedTasks = tasks.slice(-keepRecent);
|
|
131
|
+
}
|
|
132
|
+
// Step 2: Apply maxTokens limit if configured
|
|
133
|
+
if (maxTokens && selectedTasks.length > 0) {
|
|
134
|
+
// Start from the most recent task and accumulate backwards
|
|
135
|
+
let accumulatedTokens = 0;
|
|
136
|
+
let taskCount = 0;
|
|
137
|
+
for (let i = selectedTasks.length - 1; i >= 0; i--) {
|
|
138
|
+
const taskJson = JSON.stringify(selectedTasks[i]);
|
|
139
|
+
const taskTokens = estimateTokens(taskJson);
|
|
140
|
+
if (accumulatedTokens + taskTokens > maxTokens && taskCount > 0) {
|
|
141
|
+
// Stop if adding this task would exceed limit
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
accumulatedTokens += taskTokens;
|
|
145
|
+
taskCount++;
|
|
109
146
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
147
|
+
// Keep the most recent N tasks that fit within token limit
|
|
148
|
+
if (taskCount < selectedTasks.length) {
|
|
149
|
+
selectedTasks = selectedTasks.slice(-taskCount);
|
|
113
150
|
}
|
|
114
151
|
}
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
getFullPlanInput(planResult) {
|
|
118
|
-
return {
|
|
119
|
-
objective: planResult.objective,
|
|
120
|
-
steps: planResult.steps,
|
|
121
|
-
plan: {
|
|
122
|
-
status: planResult.status ? "Complete" : "In Progress",
|
|
123
|
-
result: planResult.result || "No results yet",
|
|
124
|
-
},
|
|
125
|
-
agents: this.skills.map((i) => ({
|
|
126
|
-
name: i.name,
|
|
127
|
-
description: i.description,
|
|
128
|
-
tools: i.skills.map((i) => ({ name: i.name, description: i.description })),
|
|
129
|
-
})),
|
|
130
|
-
};
|
|
152
|
+
return { tasks: selectedTasks };
|
|
131
153
|
}
|
|
132
|
-
async
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
async synthesizePlanResult(planResult, context) {
|
|
136
|
-
return context.invoke(this.completer, {
|
|
137
|
-
...this.getFullPlanInput(planResult),
|
|
138
|
-
message: SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
async executeStep(planResult, step, context) {
|
|
142
|
-
const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
|
|
143
|
-
const { model } = context;
|
|
154
|
+
async *process(input, options) {
|
|
155
|
+
const model = this.model || options.model || options.context.model;
|
|
144
156
|
if (!model)
|
|
145
157
|
throw new Error("model is required to run OrchestratorAgent");
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
const { tools: availableSkills = [] } = await this.objective.build({
|
|
159
|
+
...options,
|
|
160
|
+
input,
|
|
161
|
+
model,
|
|
162
|
+
agent: this,
|
|
163
|
+
});
|
|
164
|
+
const skills = availableSkills.map((i) => ({
|
|
165
|
+
name: i.function.name,
|
|
166
|
+
description: i.function.description,
|
|
167
|
+
}));
|
|
168
|
+
const { prompt: objective } = await this.objective.buildPrompt({
|
|
169
|
+
input,
|
|
170
|
+
context: options.context,
|
|
171
|
+
});
|
|
172
|
+
const executionState = { tasks: [] };
|
|
173
|
+
let iterationCount = 0;
|
|
174
|
+
const maxIterations = this.stateManagement?.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
175
|
+
while (true) {
|
|
176
|
+
// Check if maximum iterations reached
|
|
177
|
+
if (maxIterations && iterationCount >= maxIterations) {
|
|
178
|
+
console.warn(`Maximum iterations (${maxIterations}) reached. Stopping execution.`);
|
|
179
|
+
break;
|
|
159
180
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
result = getMessageOrJsonString(await context.invoke(executor, {}));
|
|
181
|
+
iterationCount++;
|
|
182
|
+
// Compress state for planner input if needed
|
|
183
|
+
const compressedState = this.compressState(executionState);
|
|
184
|
+
const plan = await this.invokeChildAgent(this.planner, { objective, skills, executionState: compressedState }, { ...options, model, streaming: false });
|
|
185
|
+
if (plan.finished || !plan.nextTask) {
|
|
186
|
+
break;
|
|
167
187
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
188
|
+
const task = plan.nextTask;
|
|
189
|
+
const createdAt = Date.now();
|
|
190
|
+
const taskResult = await this.invokeChildAgent(this.worker, { objective, executionState: compressedState, task }, { ...options, model, streaming: false })
|
|
191
|
+
.then((res) => {
|
|
192
|
+
if (res.error || res.success === false) {
|
|
193
|
+
return { status: "failed", result: res.result, error: res.error };
|
|
194
|
+
}
|
|
195
|
+
return { status: "completed", result: res.result };
|
|
196
|
+
})
|
|
197
|
+
.catch((error) => ({
|
|
198
|
+
status: "failed",
|
|
199
|
+
error: { message: error instanceof Error ? error.message : String(error) },
|
|
200
|
+
}));
|
|
201
|
+
executionState.tasks.push({
|
|
202
|
+
...taskResult,
|
|
203
|
+
task: task,
|
|
204
|
+
createdAt,
|
|
205
|
+
completedAt: Date.now(),
|
|
206
|
+
});
|
|
177
207
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}), { objective: planResult.objective, step, tasks: results }));
|
|
182
|
-
if (!result)
|
|
183
|
-
throw new Error("unexpected empty result from synthesize step's tasks results");
|
|
184
|
-
return {
|
|
185
|
-
step,
|
|
186
|
-
tasks: results,
|
|
187
|
-
result,
|
|
188
|
-
};
|
|
208
|
+
// Compress state for completer input if needed
|
|
209
|
+
const compressedState = this.compressState(executionState);
|
|
210
|
+
yield* await this.invokeChildAgent(this.completer, { objective, executionState: compressedState }, { ...options, model, streaming: true });
|
|
189
211
|
}
|
|
190
212
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
213
|
+
export default OrchestratorAgent;
|
|
214
|
+
function getOrchestratorAgentSchema({ filepath }) {
|
|
215
|
+
const nestAgentSchema = getNestAgentSchema({ filepath });
|
|
216
|
+
const instructionsSchema = getInstructionsSchema({ filepath });
|
|
217
|
+
return camelizeSchema(z.object({
|
|
218
|
+
objective: instructionsSchema,
|
|
219
|
+
planner: optionalize(nestAgentSchema),
|
|
220
|
+
worker: optionalize(nestAgentSchema),
|
|
221
|
+
completer: optionalize(nestAgentSchema),
|
|
222
|
+
stateManagement: optionalize(camelizeSchema(stateManagementOptionsSchema)),
|
|
223
|
+
}));
|
|
198
224
|
}
|
|
199
|
-
const orchestratorAgentOptionsSchema = z.object({
|
|
200
|
-
maxIterations: z.number().optional(),
|
|
201
|
-
tasksConcurrency: z.number().optional(),
|
|
202
|
-
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const ORCHESTRATOR_COMPLETE_PROMPT = "You are an intelligent assistant that synthesizes and presents the results of completed tasks.\n\n## User Objective\n{{ objective }}\n\n## Execution Results\n{{ executionState | yaml.stringify }}\n\n## Your Task\nBased on the execution results above, provide a comprehensive and helpful response to the user's objective.\n";
|
|
2
|
+
export declare const TODO_PLANNER_PROMPT_TEMPLATE = "You are an intelligent task planner that determines what needs to be done next to achieve the user's objective.\n\n## Your Role\nContinuously evaluate progress and decide the next task to execute. You work iteratively - planning one task at a time based on the current state and previous results.\n\n## Available Skills\n{{ skills | yaml.stringify }}\n\n## User Objective\n{{ objective }}\n\n## Current State\n{{ executionState | yaml.stringify }}\n\n## Understanding Task Status\nEach task in the execution state has a status:\n- **completed**: Task finished successfully, result is available\n- **failed**: Task encountered an error, check error field for details\n- **pending**: Task has not been executed yet\n\n## Your Task\nBased on the objective, current state, and any previous results, determine what should happen next:\n\n1. **If the objective is achieved or all work is complete:** Set finished: true and leave nextTask empty\n2. **If a previous task failed:** Decide whether to retry, skip, or use an alternative approach\n3. **If more work is needed:** Provide a clear, actionable nextTask description\n\n### Guidelines:\n- Focus on the immediate next step, not the entire plan\n- Review task history to avoid repeating work and build on previous results\n- Pay attention to task status - handle failures appropriately\n- Write self-contained task descriptions that the worker can execute independently\n- Set finished: true when the objective is satisfied\n- Consider retrying failed tasks with different approaches if appropriate\n";
|
|
3
|
+
export declare const TODO_WORKER_PROMPT_TEMPLATE = "You are a task execution agent. Your job is to execute the specific task assigned to you - nothing more, nothing less.\n\n## Overall Objective (For Context Only)\n{{ objective }}\n\n**CRITICAL CONSTRAINT**: The objective above is provided ONLY for context. You must NOT attempt to:\n- Solve the entire objective\n- Plan additional steps beyond your current task\n- Make decisions about what should happen next\n- Execute any tasks other than the one explicitly assigned to you below\n\nYour SOLE responsibility is to execute the specific task described below and return the result.\n\n## Current Execution State\n{{ executionState | yaml.stringify }}\n\n## Your Current Task\n{{ task }}\n\n## Important Instructions\n- Focus EXCLUSIVELY on completing the current task described above\n- The task is self-contained - execute it completely and accurately\n- Do NOT perform additional tasks beyond what is specified\n- Do NOT try to determine what should happen after this task\n- Use the available tools and skills to accomplish this specific task\n- Return a clear result that the planner can use to decide the next step\n\n## Output Format\nReturn your task execution result as a structured response. The output schema will guide you on the required fields.\n";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export const ORCHESTRATOR_COMPLETE_PROMPT = `\
|
|
2
|
+
You are an intelligent assistant that synthesizes and presents the results of completed tasks.
|
|
3
|
+
|
|
4
|
+
## User Objective
|
|
5
|
+
{{ objective }}
|
|
6
|
+
|
|
7
|
+
## Execution Results
|
|
8
|
+
{{ executionState | yaml.stringify }}
|
|
9
|
+
|
|
10
|
+
## Your Task
|
|
11
|
+
Based on the execution results above, provide a comprehensive and helpful response to the user's objective.
|
|
12
|
+
`;
|
|
13
|
+
export const TODO_PLANNER_PROMPT_TEMPLATE = `\
|
|
14
|
+
You are an intelligent task planner that determines what needs to be done next to achieve the user's objective.
|
|
15
|
+
|
|
16
|
+
## Your Role
|
|
17
|
+
Continuously evaluate progress and decide the next task to execute. You work iteratively - planning one task at a time based on the current state and previous results.
|
|
18
|
+
|
|
19
|
+
## Available Skills
|
|
20
|
+
{{ skills | yaml.stringify }}
|
|
21
|
+
|
|
22
|
+
## User Objective
|
|
23
|
+
{{ objective }}
|
|
24
|
+
|
|
25
|
+
## Current State
|
|
26
|
+
{{ executionState | yaml.stringify }}
|
|
27
|
+
|
|
28
|
+
## Understanding Task Status
|
|
29
|
+
Each task in the execution state has a status:
|
|
30
|
+
- **completed**: Task finished successfully, result is available
|
|
31
|
+
- **failed**: Task encountered an error, check error field for details
|
|
32
|
+
- **pending**: Task has not been executed yet
|
|
33
|
+
|
|
34
|
+
## Your Task
|
|
35
|
+
Based on the objective, current state, and any previous results, determine what should happen next:
|
|
36
|
+
|
|
37
|
+
1. **If the objective is achieved or all work is complete:** Set finished: true and leave nextTask empty
|
|
38
|
+
2. **If a previous task failed:** Decide whether to retry, skip, or use an alternative approach
|
|
39
|
+
3. **If more work is needed:** Provide a clear, actionable nextTask description
|
|
40
|
+
|
|
41
|
+
### Guidelines:
|
|
42
|
+
- Focus on the immediate next step, not the entire plan
|
|
43
|
+
- Review task history to avoid repeating work and build on previous results
|
|
44
|
+
- Pay attention to task status - handle failures appropriately
|
|
45
|
+
- Write self-contained task descriptions that the worker can execute independently
|
|
46
|
+
- Set finished: true when the objective is satisfied
|
|
47
|
+
- Consider retrying failed tasks with different approaches if appropriate
|
|
48
|
+
`;
|
|
49
|
+
export const TODO_WORKER_PROMPT_TEMPLATE = `\
|
|
50
|
+
You are a task execution agent. Your job is to execute the specific task assigned to you - nothing more, nothing less.
|
|
51
|
+
|
|
52
|
+
## Overall Objective (For Context Only)
|
|
53
|
+
{{ objective }}
|
|
54
|
+
|
|
55
|
+
**CRITICAL CONSTRAINT**: The objective above is provided ONLY for context. You must NOT attempt to:
|
|
56
|
+
- Solve the entire objective
|
|
57
|
+
- Plan additional steps beyond your current task
|
|
58
|
+
- Make decisions about what should happen next
|
|
59
|
+
- Execute any tasks other than the one explicitly assigned to you below
|
|
60
|
+
|
|
61
|
+
Your SOLE responsibility is to execute the specific task described below and return the result.
|
|
62
|
+
|
|
63
|
+
## Current Execution State
|
|
64
|
+
{{ executionState | yaml.stringify }}
|
|
65
|
+
|
|
66
|
+
## Your Current Task
|
|
67
|
+
{{ task }}
|
|
68
|
+
|
|
69
|
+
## Important Instructions
|
|
70
|
+
- Focus EXCLUSIVELY on completing the current task described above
|
|
71
|
+
- The task is self-contained - execute it completely and accurately
|
|
72
|
+
- Do NOT perform additional tasks beyond what is specified
|
|
73
|
+
- Do NOT try to determine what should happen after this task
|
|
74
|
+
- Use the available tools and skills to accomplish this specific task
|
|
75
|
+
- Return a clear result that the planner can use to decide the next step
|
|
76
|
+
|
|
77
|
+
## Output Format
|
|
78
|
+
Return your task execution result as a structured response. The output schema will guide you on the required fields.
|
|
79
|
+
`;
|