@blocklet/pages-kit-agents 0.5.47 → 0.5.49
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/.env.local.example +7 -0
- package/cli.ts +1 -1
- package/index.ts +0 -1
- package/lib/cjs/agents/content-reviwer.d.ts +1 -1
- package/lib/cjs/agents/multi-agent-page-writer.js +3 -10
- package/lib/cjs/agents/orchestrator/index.d.ts +115 -0
- package/lib/cjs/agents/orchestrator/index.js +264 -0
- package/lib/cjs/agents/orchestrator/orchestrator-prompts.d.ts +130 -0
- package/lib/cjs/agents/orchestrator/orchestrator-prompts.js +135 -0
- package/lib/cjs/agents/page-metadata-generator.d.ts +1 -1
- package/lib/cjs/agents/page-metadata-generator.js +4 -1
- package/lib/cjs/agents/page-structure-planner.d.ts +59 -22
- package/lib/cjs/agents/page-structure-planner.js +108 -78
- package/lib/cjs/agents/router-datacache-generator.d.ts +24 -0
- package/lib/cjs/agents/router-datacache-generator.js +78 -0
- package/lib/cjs/agents/section-content-generator.js +11 -8
- package/lib/cjs/agents/template-designer.d.ts +7 -6
- package/lib/cjs/agents/template-designer.js +22 -17
- package/lib/cjs/agents/template-generator.d.ts +55 -7
- package/lib/cjs/agents/template-generator.js +99 -89
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/prompts/fs-memory-recorder.md +59 -0
- package/lib/cjs/prompts/fs-memory-retriever.md +30 -0
- package/lib/cjs/prompts/html-generator.md +91 -0
- package/lib/cjs/prompts/page-structure-planner-prompt.md +47 -0
- package/lib/cjs/prompts/page-structure-validator-prompt.md +78 -0
- package/lib/cjs/prompts/pages-kit-template-rule.md +491 -0
- package/lib/cjs/prompts/site-map-generator.md +126 -0
- package/lib/cjs/skill-agents/parse-component-list-agent.d.ts +100 -0
- package/lib/cjs/skill-agents/parse-component-list-agent.js +198 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/agent-utils.d.ts +4 -0
- package/lib/cjs/utils/agent-utils.js +106 -0
- package/lib/esm/agents/content-reviwer.d.ts +1 -1
- package/lib/esm/agents/multi-agent-page-writer.js +5 -12
- package/lib/esm/agents/orchestrator/index.d.ts +115 -0
- package/lib/esm/agents/orchestrator/index.js +243 -0
- package/lib/esm/agents/orchestrator/orchestrator-prompts.d.ts +130 -0
- package/lib/esm/agents/orchestrator/orchestrator-prompts.js +131 -0
- package/lib/esm/agents/page-metadata-generator.d.ts +1 -1
- package/lib/esm/agents/page-metadata-generator.js +4 -1
- package/lib/esm/agents/page-structure-planner.d.ts +59 -22
- package/lib/esm/agents/page-structure-planner.js +105 -78
- package/lib/esm/agents/router-datacache-generator.d.ts +24 -0
- package/lib/esm/agents/router-datacache-generator.js +72 -0
- package/lib/esm/agents/section-content-generator.js +11 -8
- package/lib/esm/agents/template-designer.d.ts +7 -6
- package/lib/esm/agents/template-designer.js +22 -14
- package/lib/esm/agents/template-generator.d.ts +55 -7
- package/lib/esm/agents/template-generator.js +98 -88
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/prompts/fs-memory-recorder.md +59 -0
- package/lib/esm/prompts/fs-memory-retriever.md +30 -0
- package/lib/esm/prompts/html-generator.md +91 -0
- package/lib/esm/prompts/page-structure-planner-prompt.md +47 -0
- package/lib/esm/prompts/page-structure-validator-prompt.md +78 -0
- package/lib/esm/prompts/pages-kit-template-rule.md +491 -0
- package/lib/esm/prompts/site-map-generator.md +126 -0
- package/lib/esm/skill-agents/parse-component-list-agent.d.ts +100 -0
- package/lib/esm/skill-agents/parse-component-list-agent.js +192 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/agent-utils.d.ts +4 -0
- package/lib/esm/utils/agent-utils.js +105 -0
- package/lib/types/agents/content-reviwer.d.ts +1 -1
- package/lib/types/agents/orchestrator/index.d.ts +115 -0
- package/lib/types/agents/orchestrator/orchestrator-prompts.d.ts +130 -0
- package/lib/types/agents/page-metadata-generator.d.ts +1 -1
- package/lib/types/agents/page-structure-planner.d.ts +59 -22
- package/lib/types/agents/router-datacache-generator.d.ts +24 -0
- package/lib/types/agents/template-designer.d.ts +7 -6
- package/lib/types/agents/template-generator.d.ts +55 -7
- package/lib/types/index.d.ts +0 -1
- package/lib/types/skill-agents/parse-component-list-agent.d.ts +100 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/agent-utils.d.ts +4 -0
- package/package.json +8 -3
- package/workflow-cli.ts +61 -41
- package/lib/cjs/agents/page-structure-validator.d.ts +0 -30
- package/lib/cjs/agents/page-structure-validator.js +0 -124
- package/lib/esm/agents/page-structure-validator.d.ts +0 -30
- package/lib/esm/agents/page-structure-validator.js +0 -121
- package/lib/types/agents/page-structure-validator.d.ts +0 -30
package/.env.local.example
CHANGED
|
@@ -9,3 +9,10 @@ XAI_API_KEY=""
|
|
|
9
9
|
DEEPSEEK_API_KEY=""
|
|
10
10
|
|
|
11
11
|
OPENAI_BASE_URL=https://openai.arcblock.io/v1
|
|
12
|
+
|
|
13
|
+
PAGES_KIT_DEBUG_API_URL=<your-pages-kit-domain-with-mount-point>/api/sdk/upload-data
|
|
14
|
+
PAGES_KIT_DEBUG_ACCESS_KEY_ID=
|
|
15
|
+
PAGES_KIT_DEBUG_ACCESS_KEY_SECRET=
|
|
16
|
+
PAGES_KIT_DEBUG_PROJECT_ID=
|
|
17
|
+
|
|
18
|
+
|
package/cli.ts
CHANGED
|
@@ -270,7 +270,7 @@ async function main() {
|
|
|
270
270
|
|
|
271
271
|
if (modelsToRun.length === 0) {
|
|
272
272
|
logger.warn(
|
|
273
|
-
'No models
|
|
273
|
+
'No available models. Please enable at least one model in config.yaml or specify a model via command line.'
|
|
274
274
|
);
|
|
275
275
|
return;
|
|
276
276
|
}
|
package/index.ts
CHANGED
|
@@ -7,7 +7,6 @@ export {
|
|
|
7
7
|
} from './agents/multi-agent-page-writer';
|
|
8
8
|
export * from './agents/page-metadata-generator';
|
|
9
9
|
export * from './agents/page-structure-planner';
|
|
10
|
-
export * from './agents/page-structure-validator';
|
|
11
10
|
export * from './agents/section-content-generator';
|
|
12
11
|
|
|
13
12
|
// Export utilities that might be useful for consumers
|
|
@@ -8,7 +8,6 @@ const agent_utils_js_1 = require("../utils/agent-utils.js");
|
|
|
8
8
|
const logger_js_1 = require("../utils/logger.js");
|
|
9
9
|
const page_metadata_generator_js_1 = require("./page-metadata-generator.js");
|
|
10
10
|
const page_structure_planner_js_1 = require("./page-structure-planner.js");
|
|
11
|
-
const page_structure_validator_js_1 = require("./page-structure-validator.js");
|
|
12
11
|
const section_content_generator_js_1 = require("./section-content-generator.js");
|
|
13
12
|
/**
|
|
14
13
|
* 多Agent协作生成页面内容
|
|
@@ -56,26 +55,20 @@ async function generatePageContentMultiAgent(input) {
|
|
|
56
55
|
// 3-4 使用反思工作流生成页面结构
|
|
57
56
|
const planningEngine = new core_1.AIGNE({
|
|
58
57
|
model,
|
|
59
|
-
agents: [page_structure_planner_js_1.pageStructurePlannerAgent, page_structure_validator_js_1.pageStructureValidatorAgent],
|
|
60
|
-
limits: {
|
|
61
|
-
maxAgentInvokes: 30,
|
|
62
|
-
},
|
|
63
58
|
});
|
|
64
|
-
await planningEngine.invoke(page_structure_planner_js_1.
|
|
59
|
+
const structurePlanResult = await planningEngine.invoke(page_structure_planner_js_1.pageStructurePlannerWithReflection, {
|
|
65
60
|
context,
|
|
66
61
|
question,
|
|
67
62
|
locale,
|
|
68
|
-
|
|
63
|
+
structurePlan: Object.entries(sectionsRules || {}).map(([sectionName, sectionContentPlan]) => ({
|
|
69
64
|
sectionName,
|
|
70
65
|
sectionContentPlan,
|
|
71
66
|
})),
|
|
72
67
|
outputSchema: JSON.stringify(outputSchemaJson),
|
|
73
68
|
expectedSections: JSON.stringify(sectionNames),
|
|
74
69
|
});
|
|
75
|
-
const
|
|
76
|
-
const { structurePlan } = structurePlanResult.message;
|
|
70
|
+
const { structurePlan } = structurePlanResult;
|
|
77
71
|
logger_js_1.logger.info(`[${modelName}] Initial structure plan generated`, structurePlan);
|
|
78
|
-
await planningEngine.shutdown();
|
|
79
72
|
// 触发页面结构生成成功事件
|
|
80
73
|
if (onProgress) {
|
|
81
74
|
onProgress({
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from '@aigne/core';
|
|
2
|
+
import { type FullPlanOutput, type StepWithResult } from './orchestrator-prompts.js';
|
|
3
|
+
/**
|
|
4
|
+
* Re-export orchestrator prompt templates and related types
|
|
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
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Configuration options for the Orchestrator Agent
|
|
35
|
+
*/
|
|
36
|
+
export interface OrchestratorAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
37
|
+
/**
|
|
38
|
+
* Maximum number of iterations to prevent infinite loops
|
|
39
|
+
* Default: 30
|
|
40
|
+
*/
|
|
41
|
+
maxIterations?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Number of concurrent tasks
|
|
44
|
+
* Default: 5
|
|
45
|
+
*/
|
|
46
|
+
tasksConcurrency?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Chain step results as input to the next step
|
|
49
|
+
* Default: false
|
|
50
|
+
*/
|
|
51
|
+
chainStepResults?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Orchestrator Agent Class
|
|
55
|
+
*
|
|
56
|
+
* This Agent is responsible for:
|
|
57
|
+
* 1. Generating an execution plan based on the objective
|
|
58
|
+
* 2. Breaking down the plan into steps and tasks
|
|
59
|
+
* 3. Coordinating the execution of steps and tasks
|
|
60
|
+
* 4. Synthesizing the final result
|
|
61
|
+
*
|
|
62
|
+
* Workflow:
|
|
63
|
+
* - Receives input objective
|
|
64
|
+
* - Uses planner to create execution plan
|
|
65
|
+
* - Executes tasks and steps according to the plan
|
|
66
|
+
* - Synthesizes final result through completer
|
|
67
|
+
*/
|
|
68
|
+
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
69
|
+
/**
|
|
70
|
+
* Factory method to create an OrchestratorAgent instance
|
|
71
|
+
* @param options - Configuration options for the Orchestrator Agent
|
|
72
|
+
* @returns A new OrchestratorAgent instance
|
|
73
|
+
*/
|
|
74
|
+
static from<I extends Message, O extends Message>(options: OrchestratorAgentOptions<I, O>): OrchestratorAgent<I, O>;
|
|
75
|
+
/**
|
|
76
|
+
* Creates an OrchestratorAgent instance
|
|
77
|
+
* @param options - Configuration options for the Orchestrator Agent
|
|
78
|
+
*/
|
|
79
|
+
constructor(options: OrchestratorAgentOptions<I, O>);
|
|
80
|
+
private planner;
|
|
81
|
+
private completer;
|
|
82
|
+
/**
|
|
83
|
+
* Maximum number of iterations
|
|
84
|
+
* Prevents infinite execution loops
|
|
85
|
+
*/
|
|
86
|
+
maxIterations?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Number of concurrent tasks
|
|
89
|
+
* Controls how many tasks can be executed simultaneously
|
|
90
|
+
*/
|
|
91
|
+
tasksConcurrency?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Whether to chain step results as input to next step
|
|
94
|
+
*/
|
|
95
|
+
chainStepResults?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Process input and execute the orchestrator workflow
|
|
98
|
+
*
|
|
99
|
+
* Workflow:
|
|
100
|
+
* 1. Extract the objective
|
|
101
|
+
* 2. Loop until plan completion or maximum iterations:
|
|
102
|
+
* a. Generate/update execution plan
|
|
103
|
+
* b. If plan is complete, synthesize result
|
|
104
|
+
* c. Otherwise, execute steps in the plan
|
|
105
|
+
*
|
|
106
|
+
* @param input - Input message containing the objective
|
|
107
|
+
* @param options - Agent invocation options
|
|
108
|
+
* @returns Processing result
|
|
109
|
+
*/
|
|
110
|
+
process(input: I, options: AgentInvokeOptions): Promise<O>;
|
|
111
|
+
private getFullPlanInput;
|
|
112
|
+
private getFullPlan;
|
|
113
|
+
private synthesizePlanResult;
|
|
114
|
+
private executeStep;
|
|
115
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
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
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.OrchestratorAgent = void 0;
|
|
21
|
+
/* eslint-disable no-await-in-loop */
|
|
22
|
+
const core_1 = require("@aigne/core");
|
|
23
|
+
const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
|
|
24
|
+
const fastq_1 = __importDefault(require("fastq"));
|
|
25
|
+
const zod_1 = require("zod");
|
|
26
|
+
const orchestrator_prompts_js_1 = require("./orchestrator-prompts.js");
|
|
27
|
+
/**
|
|
28
|
+
* Default maximum number of iterations to prevent infinite loops
|
|
29
|
+
*/
|
|
30
|
+
const DEFAULT_MAX_ITERATIONS = 30;
|
|
31
|
+
/**
|
|
32
|
+
* Default number of concurrent tasks
|
|
33
|
+
*/
|
|
34
|
+
const DEFAULT_TASK_CONCURRENCY = 5;
|
|
35
|
+
/**
|
|
36
|
+
* Re-export orchestrator prompt templates and related types
|
|
37
|
+
*/
|
|
38
|
+
__exportStar(require("./orchestrator-prompts.js"), exports);
|
|
39
|
+
/**
|
|
40
|
+
* Orchestrator Agent Class
|
|
41
|
+
*
|
|
42
|
+
* This Agent is responsible for:
|
|
43
|
+
* 1. Generating an execution plan based on the objective
|
|
44
|
+
* 2. Breaking down the plan into steps and tasks
|
|
45
|
+
* 3. Coordinating the execution of steps and tasks
|
|
46
|
+
* 4. Synthesizing the final result
|
|
47
|
+
*
|
|
48
|
+
* Workflow:
|
|
49
|
+
* - Receives input objective
|
|
50
|
+
* - Uses planner to create execution plan
|
|
51
|
+
* - Executes tasks and steps according to the plan
|
|
52
|
+
* - Synthesizes final result through completer
|
|
53
|
+
*/
|
|
54
|
+
class OrchestratorAgent extends core_1.Agent {
|
|
55
|
+
/**
|
|
56
|
+
* Factory method to create an OrchestratorAgent instance
|
|
57
|
+
* @param options - Configuration options for the Orchestrator Agent
|
|
58
|
+
* @returns A new OrchestratorAgent instance
|
|
59
|
+
*/
|
|
60
|
+
static from(options) {
|
|
61
|
+
return new OrchestratorAgent(options);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates an OrchestratorAgent instance
|
|
65
|
+
* @param options - Configuration options for the Orchestrator Agent
|
|
66
|
+
*/
|
|
67
|
+
constructor(options) {
|
|
68
|
+
(0, type_utils_js_1.checkArguments)('OrchestratorAgent', orchestratorAgentOptionsSchema, options);
|
|
69
|
+
super({ ...options });
|
|
70
|
+
this.maxIterations = options.maxIterations;
|
|
71
|
+
this.tasksConcurrency = options.tasksConcurrency;
|
|
72
|
+
this.chainStepResults = options.chainStepResults;
|
|
73
|
+
this.planner = new core_1.AIAgent({
|
|
74
|
+
name: 'llm_orchestration_planner',
|
|
75
|
+
instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
|
|
76
|
+
outputSchema: () => (0, orchestrator_prompts_js_1.getFullPlanSchema)(this.skills),
|
|
77
|
+
});
|
|
78
|
+
this.completer = new core_1.AIAgent({
|
|
79
|
+
name: 'llm_orchestration_completer',
|
|
80
|
+
instructions: orchestrator_prompts_js_1.FULL_PLAN_PROMPT_TEMPLATE,
|
|
81
|
+
outputSchema: this.outputSchema,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
planner;
|
|
85
|
+
completer;
|
|
86
|
+
/**
|
|
87
|
+
* Maximum number of iterations
|
|
88
|
+
* Prevents infinite execution loops
|
|
89
|
+
*/
|
|
90
|
+
maxIterations;
|
|
91
|
+
/**
|
|
92
|
+
* Number of concurrent tasks
|
|
93
|
+
* Controls how many tasks can be executed simultaneously
|
|
94
|
+
*/
|
|
95
|
+
tasksConcurrency;
|
|
96
|
+
/**
|
|
97
|
+
* Whether to chain step results as input to next step
|
|
98
|
+
*/
|
|
99
|
+
chainStepResults;
|
|
100
|
+
/**
|
|
101
|
+
* Process input and execute the orchestrator workflow
|
|
102
|
+
*
|
|
103
|
+
* Workflow:
|
|
104
|
+
* 1. Extract the objective
|
|
105
|
+
* 2. Loop until plan completion or maximum iterations:
|
|
106
|
+
* a. Generate/update execution plan
|
|
107
|
+
* b. If plan is complete, synthesize result
|
|
108
|
+
* c. Otherwise, execute steps in the plan
|
|
109
|
+
*
|
|
110
|
+
* @param input - Input message containing the objective
|
|
111
|
+
* @param options - Agent invocation options
|
|
112
|
+
* @returns Processing result
|
|
113
|
+
*/
|
|
114
|
+
async process(input, options) {
|
|
115
|
+
const { model } = options.context;
|
|
116
|
+
if (!model)
|
|
117
|
+
throw new Error('model is required to run OrchestratorAgent');
|
|
118
|
+
const objective = (0, core_1.getMessage)(input);
|
|
119
|
+
if (!objective)
|
|
120
|
+
throw new Error('Objective is required to run OrchestratorAgent');
|
|
121
|
+
const result = {
|
|
122
|
+
objective,
|
|
123
|
+
steps: [],
|
|
124
|
+
};
|
|
125
|
+
let iterations = 0;
|
|
126
|
+
const maxIterations = this.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
127
|
+
while (iterations++ < maxIterations) {
|
|
128
|
+
const plan = await this.getFullPlan(result, options.context);
|
|
129
|
+
result.plan = plan;
|
|
130
|
+
if (plan.isComplete) {
|
|
131
|
+
return this.synthesizePlanResult(result, options.context);
|
|
132
|
+
}
|
|
133
|
+
for (const step of plan.steps) {
|
|
134
|
+
const stepResult = await this.executeStep(result, step, options.context);
|
|
135
|
+
result.steps.push(stepResult);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
throw new Error(`Max iterations reached: ${maxIterations}`);
|
|
139
|
+
}
|
|
140
|
+
getFullPlanInput(planResult) {
|
|
141
|
+
return {
|
|
142
|
+
objective: planResult.objective,
|
|
143
|
+
steps: planResult.steps,
|
|
144
|
+
plan: {
|
|
145
|
+
status: planResult.status ? 'Complete' : 'In Progress',
|
|
146
|
+
result: planResult.result || 'No results yet',
|
|
147
|
+
},
|
|
148
|
+
agents: this.skills.map((i) => ({
|
|
149
|
+
name: i.name,
|
|
150
|
+
description: i.description,
|
|
151
|
+
tools: i.skills.map((i) => ({ name: i.name, description: i.description })),
|
|
152
|
+
})),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async getFullPlan(planResult, context) {
|
|
156
|
+
return context.invoke(this.planner, this.getFullPlanInput(planResult));
|
|
157
|
+
}
|
|
158
|
+
async synthesizePlanResult(planResult, context) {
|
|
159
|
+
return context.invoke(this.completer, {
|
|
160
|
+
...this.getFullPlanInput(planResult),
|
|
161
|
+
...(0, core_1.createMessage)(orchestrator_prompts_js_1.SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async executeStep(planResult, step, context) {
|
|
165
|
+
const concurrency = this.tasksConcurrency ?? DEFAULT_TASK_CONCURRENCY;
|
|
166
|
+
const { model } = context;
|
|
167
|
+
if (!model)
|
|
168
|
+
throw new Error('model is required to run OrchestratorAgent');
|
|
169
|
+
let lastTaskResult = {};
|
|
170
|
+
// 将上一步最后一个任务的输入作为下一步的输入
|
|
171
|
+
if (this.chainStepResults) {
|
|
172
|
+
if (planResult.steps.length > 0) {
|
|
173
|
+
const lastStep = planResult.steps[planResult.steps.length - 1];
|
|
174
|
+
if (lastStep?.result) {
|
|
175
|
+
try {
|
|
176
|
+
const lastStepResult = JSON.parse(lastStep.result);
|
|
177
|
+
if (lastStepResult?.step?.results && Array.isArray(lastStepResult.step.results) && lastStepResult.step.results.length > 0) {
|
|
178
|
+
const lastStepResults = lastStepResult.step.results[lastStepResult.step.results.length - 1];
|
|
179
|
+
console.log('lastStepResults', lastStepResults.result);
|
|
180
|
+
lastTaskResult = JSON.parse(lastStepResults.result);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error('Failed to parse last step result', error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const queue = fastq_1.default.promise(async (task) => {
|
|
190
|
+
const agent = this.skills.find((skill) => skill.name === task.agent);
|
|
191
|
+
if (!agent)
|
|
192
|
+
throw new Error(`Agent ${task.agent} not found`);
|
|
193
|
+
const prompt = core_1.PromptTemplate.from(orchestrator_prompts_js_1.TASK_PROMPT_TEMPLATE).format({
|
|
194
|
+
// eslint-disable-next-line prettier/prettier
|
|
195
|
+
objective: planResult.objective,
|
|
196
|
+
step,
|
|
197
|
+
task,
|
|
198
|
+
steps: planResult.steps,
|
|
199
|
+
});
|
|
200
|
+
let result;
|
|
201
|
+
if (agent.isInvokable) {
|
|
202
|
+
result = getMessageOrJsonString(await context.invoke(agent, { ...lastTaskResult, [core_1.MESSAGE_KEY]: prompt }));
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
const executor = core_1.AIAgent.from({
|
|
206
|
+
name: 'llm_orchestration_task_executor',
|
|
207
|
+
instructions: prompt,
|
|
208
|
+
skills: agent.skills,
|
|
209
|
+
});
|
|
210
|
+
result = getMessageOrJsonString(await context.invoke(executor, this.chainStepResults ? { ...lastTaskResult } : {}));
|
|
211
|
+
}
|
|
212
|
+
return { task, result };
|
|
213
|
+
}, concurrency);
|
|
214
|
+
let results;
|
|
215
|
+
try {
|
|
216
|
+
results = await Promise.all(step.tasks.map((task) => queue.push(task)));
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
queue.kill();
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
let result = '';
|
|
223
|
+
if (!this.chainStepResults) {
|
|
224
|
+
result = getMessageOrJsonString(await context.invoke(core_1.AIAgent.from({
|
|
225
|
+
name: 'llm_orchestration_step_synthesizer',
|
|
226
|
+
instructions: orchestrator_prompts_js_1.SYNTHESIZE_STEP_PROMPT_TEMPLATE,
|
|
227
|
+
}), { objective: planResult.objective, step, tasks: results }));
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
const resultData = {
|
|
231
|
+
step: {
|
|
232
|
+
description: step.description,
|
|
233
|
+
results: results.map((task) => ({
|
|
234
|
+
agent: task.task.agent,
|
|
235
|
+
task: task.task.description,
|
|
236
|
+
result: task.result,
|
|
237
|
+
})),
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
result = JSON.stringify(resultData);
|
|
241
|
+
}
|
|
242
|
+
if (!result)
|
|
243
|
+
throw new Error('unexpected empty result from synthesize step\'s tasks results');
|
|
244
|
+
return {
|
|
245
|
+
step,
|
|
246
|
+
tasks: results,
|
|
247
|
+
result,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
exports.OrchestratorAgent = OrchestratorAgent;
|
|
252
|
+
function getMessageOrJsonString(output) {
|
|
253
|
+
const entries = Object.entries(output);
|
|
254
|
+
const firstValue = entries[0]?.[1];
|
|
255
|
+
if (entries.length === 1 && typeof firstValue === 'string') {
|
|
256
|
+
return firstValue;
|
|
257
|
+
}
|
|
258
|
+
return JSON.stringify(output);
|
|
259
|
+
}
|
|
260
|
+
const orchestratorAgentOptionsSchema = zod_1.z.object({
|
|
261
|
+
maxIterations: zod_1.z.number().optional(),
|
|
262
|
+
tasksConcurrency: zod_1.z.number().optional(),
|
|
263
|
+
chainStepResults: zod_1.z.boolean().optional(),
|
|
264
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { Agent, Message } from '@aigne/core';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* @hidden
|
|
5
|
+
*/
|
|
6
|
+
export declare const SYNTHESIZE_PLAN_USER_PROMPT_TEMPLATE = "Synthesize the results of executing all steps in the plan into a cohesive result\n";
|
|
7
|
+
/**
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
export declare function getFullPlanSchema(agents: Agent[]): z.ZodObject<{
|
|
11
|
+
steps: z.ZodArray<z.ZodObject<{
|
|
12
|
+
description: z.ZodString;
|
|
13
|
+
tasks: z.ZodArray<z.ZodObject<{
|
|
14
|
+
description: z.ZodString;
|
|
15
|
+
agent: z.ZodUnion<[z.ZodLiteral<string>, z.ZodLiteral<string>, ...z.ZodLiteral<string>[]]>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
description: string;
|
|
18
|
+
agent: string;
|
|
19
|
+
}, {
|
|
20
|
+
description: string;
|
|
21
|
+
agent: string;
|
|
22
|
+
}>, "many">;
|
|
23
|
+
}, "strip", z.ZodTypeAny, {
|
|
24
|
+
description: string;
|
|
25
|
+
tasks: {
|
|
26
|
+
description: string;
|
|
27
|
+
agent: string;
|
|
28
|
+
}[];
|
|
29
|
+
}, {
|
|
30
|
+
description: string;
|
|
31
|
+
tasks: {
|
|
32
|
+
description: string;
|
|
33
|
+
agent: string;
|
|
34
|
+
}[];
|
|
35
|
+
}>, "many">;
|
|
36
|
+
isComplete: z.ZodBoolean;
|
|
37
|
+
}, "strip", z.ZodTypeAny, {
|
|
38
|
+
steps: {
|
|
39
|
+
description: string;
|
|
40
|
+
tasks: {
|
|
41
|
+
description: string;
|
|
42
|
+
agent: string;
|
|
43
|
+
}[];
|
|
44
|
+
}[];
|
|
45
|
+
isComplete: boolean;
|
|
46
|
+
}, {
|
|
47
|
+
steps: {
|
|
48
|
+
description: string;
|
|
49
|
+
tasks: {
|
|
50
|
+
description: string;
|
|
51
|
+
agent: string;
|
|
52
|
+
}[];
|
|
53
|
+
}[];
|
|
54
|
+
isComplete: boolean;
|
|
55
|
+
}>;
|
|
56
|
+
/**
|
|
57
|
+
* @hidden
|
|
58
|
+
*/
|
|
59
|
+
export type FullPlanOutput = z.infer<ReturnType<typeof getFullPlanSchema>>;
|
|
60
|
+
/**
|
|
61
|
+
* @hidden
|
|
62
|
+
*/
|
|
63
|
+
export type Step = FullPlanOutput['steps'][number];
|
|
64
|
+
/**
|
|
65
|
+
* @hidden
|
|
66
|
+
*/
|
|
67
|
+
export type Task = Step['tasks'][number];
|
|
68
|
+
/**
|
|
69
|
+
* @hidden
|
|
70
|
+
*/
|
|
71
|
+
export interface StepWithResult {
|
|
72
|
+
step: Step;
|
|
73
|
+
tasks: Array<TaskWithResult>;
|
|
74
|
+
result: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @hidden
|
|
78
|
+
*/
|
|
79
|
+
export interface TaskWithResult {
|
|
80
|
+
task: Task;
|
|
81
|
+
result: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @hidden
|
|
85
|
+
*/
|
|
86
|
+
export interface FullPlanInput extends Message {
|
|
87
|
+
objective: string;
|
|
88
|
+
steps: StepWithResult[];
|
|
89
|
+
plan: {
|
|
90
|
+
status: string;
|
|
91
|
+
result: string;
|
|
92
|
+
};
|
|
93
|
+
agents: {
|
|
94
|
+
name: string;
|
|
95
|
+
description?: string;
|
|
96
|
+
tools: {
|
|
97
|
+
name: string;
|
|
98
|
+
description?: string;
|
|
99
|
+
}[];
|
|
100
|
+
}[];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* @hidden
|
|
104
|
+
*/
|
|
105
|
+
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";
|
|
106
|
+
/**
|
|
107
|
+
* @hidden
|
|
108
|
+
*/
|
|
109
|
+
export interface TaskPromptInput extends Message {
|
|
110
|
+
objective: string;
|
|
111
|
+
step: Step;
|
|
112
|
+
task: Task;
|
|
113
|
+
steps: StepWithResult[];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @hidden
|
|
117
|
+
*/
|
|
118
|
+
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";
|
|
119
|
+
/**
|
|
120
|
+
* @hidden
|
|
121
|
+
*/
|
|
122
|
+
export interface SynthesizeStepPromptInput extends Message {
|
|
123
|
+
objective: string;
|
|
124
|
+
step: Step;
|
|
125
|
+
tasks: TaskWithResult[];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @hidden
|
|
129
|
+
*/
|
|
130
|
+
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";
|