Skip to content

Commit 1cb9196

Browse files
author
linyuan.yang
committed
静态 动态 prompt
1 parent ecbbc4b commit 1cb9196

5 files changed

Lines changed: 37 additions & 42 deletions

File tree

packages/scorpio.ai/src/Agents/AgentServiceBase.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ export abstract class AgentServiceBase {
108108
this.logger = loggerService?.getLogger(this.constructor.name);
109109
}
110110

111-
/**
112-
* 在构造后追加系统提示词(子类按需 override;编排 Agent 调用子 Agent 时使用)
113-
*/
114111
// eslint-disable-next-line @typescript-eslint/no-unused-vars
115-
addSystemPrompts(_prompts: string[]): void {}
112+
addStaticSystemPrompts(_prompts: string[]): void {}
113+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
114+
addDynamicSystemPrompts(_prompts: string[]): void {}
116115

117116
/**
118117
* 以无回调方式调用 stream,返回 stream 的结果消息列表。

packages/scorpio.ai/src/Agents/Generative/GenerativeAgentService.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@ export { ChatMessage, MessageRole, IAgentCallback, AgentCancelledError } from ".
1616
*/
1717
export class GenerativeAgentService extends AgentServiceBase {
1818
protected modelService: IModelService;
19-
protected systemMessages: ChatMessage[];
19+
protected staticSystemPrompts: string[];
2020
protected dynamicSystemPrompts: string[];
2121

2222
constructor(
2323
@inject(IModelService) modelService: IModelService,
24-
@inject(T_StaticSystemPrompts, { optional: true }) systemPrompts?: string[],
24+
@inject(T_StaticSystemPrompts, { optional: true }) staticSystemPrompts?: string[],
2525
@inject(T_DynamicSystemPrompts, { optional: true }) dynamicSystemPrompts?: string[],
2626
@inject(ILoggerService, { optional: true }) loggerService?: ILoggerService,
2727
@inject(IAgentSaverService, { optional: true }) agentSaver?: IAgentSaverService,
2828
@inject(IMemoryService, { optional: true }) memoryServices?: IMemoryService[],
2929
) {
3030
super(loggerService, agentSaver, memoryServices);
3131
this.modelService = modelService;
32-
this.systemMessages = (systemPrompts ?? []).map(p => ({ role: MessageRole.System, content: p }));
32+
this.staticSystemPrompts = staticSystemPrompts ?? [];
3333
this.dynamicSystemPrompts = dynamicSystemPrompts ?? [];
3434
}
3535

36-
override addSystemPrompts(prompts: string[]): void {
37-
this.systemMessages.unshift(...prompts.map(p => ({ role: MessageRole.System, content: p })));
36+
override addStaticSystemPrompts(prompts: string[]): void {
37+
this.staticSystemPrompts.unshift(...prompts);
38+
}
39+
40+
override addDynamicSystemPrompts(prompts: string[]): void {
41+
this.dynamicSystemPrompts.push(...prompts);
3842
}
3943

4044
override async stream(query: MessageContent, callback: IAgentCallback, signal?: AbortSignal): Promise<ChatMessage[]> {
@@ -44,17 +48,13 @@ export class GenerativeAgentService extends AgentServiceBase {
4448
if (!savedHistory || savedHistory.length === 0) {
4549
throw new Error('historyMessages is empty, cannot call model');
4650
}
47-
const staticContent = this.systemMessages.map(m => m.content as string).join('\n\n').trim();
51+
const contentBlocks: Array<{ type: "text"; text: string }> = [];
52+
const staticContent = this.staticSystemPrompts.join('\n\n').trim();
53+
if (staticContent) contentBlocks.push({ type: "text", text: staticContent });
4854
const dynamicContent = this.dynamicSystemPrompts.join('\n\n').trim();
49-
let systemMsg: ChatMessage | undefined;
50-
if (staticContent || dynamicContent) {
51-
const contentBlocks: Array<{ type: string; text: string }> = [];
52-
if (staticContent) contentBlocks.push({ type: "text", text: staticContent });
53-
if (dynamicContent) contentBlocks.push({ type: "text", text: dynamicContent });
54-
systemMsg = { role: MessageRole.System, content: contentBlocks };
55-
}
55+
if (dynamicContent) contentBlocks.push({ type: "text", text: dynamicContent });
5656
const messages: ChatMessage[] = [
57-
...(systemMsg ? [systemMsg] : []),
57+
...(contentBlocks.length > 0 ? [{ role: MessageRole.System, content: contentBlocks }] : []),
5858
...savedHistory,
5959
];
6060

packages/scorpio.ai/src/Agents/ReAct/ReActAgentService.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class ReActAgentService extends SingleAgentService {
5757
@inject(T_ReactSystemPromptTemplate) private systemPromptTemplate: string,
5858
@inject(T_ReactSubNodePrompt) private subNodePrompt: string,
5959
@inject(T_ReactTaskToolDesc) private taskToolDesc: string,
60-
@inject(T_StaticSystemPrompts, { optional: true }) systemPrompts?: string[],
60+
@inject(T_StaticSystemPrompts, { optional: true }) staticSystemPrompts?: string[],
6161
@inject(T_DynamicSystemPrompts, { optional: true }) dynamicSystemPrompts?: string[],
6262
@inject(IAgentSaverService, { optional: true }) agentSaver?: IAgentSaverService,
6363
@inject(ILoggerService, { optional: true }) loggerService?: ILoggerService,
@@ -68,7 +68,7 @@ export class ReActAgentService extends SingleAgentService {
6868
@inject(T_MemorySystemPromptTemplate, { optional: true }) memorySystemPromptTemplate?: string,
6969
@inject(T_WikiSystemPromptTemplate, { optional: true }) private wikiSystemPromptTemplateValue?: string,
7070
) {
71-
super(thinkModelService, systemPrompts, dynamicSystemPrompts, loggerService, agentSaver, skillService, toolService, memoryServices, wikiServices, memorySystemPromptTemplate, wikiSystemPromptTemplateValue);
71+
super(thinkModelService, staticSystemPrompts, dynamicSystemPrompts, loggerService, agentSaver, skillService, toolService, memoryServices, wikiServices, memorySystemPromptTemplate, wikiSystemPromptTemplateValue);
7272
this.agentSubNodes = agentSubNodes;
7373
this.agentFactory = agentFactory;
7474
}
@@ -100,7 +100,7 @@ export class ReActAgentService extends SingleAgentService {
100100
if (!callback) return [];
101101
const { onMessage: _, ...subCallback } = callback;
102102

103-
const runFn: RunTaskFn = async ({ agentId, goal, task, systemPrompt }) => {
103+
const runFn: RunTaskFn = async ({ agentId, task, systemPrompt }) => {
104104
let agentService: AgentServiceBase | null = null;
105105
const thinkId = uuidv4();
106106
try {
@@ -116,11 +116,10 @@ export class ReActAgentService extends SingleAgentService {
116116

117117
agentService = await this.agentFactory(agentId, subContainer);
118118

119-
const extraPrompts: string[] = [];
120-
if (goal?.trim()) extraPrompts.push(`<goal>${goal.trim()}</goal>`);
121-
if (systemPrompt?.trim()) extraPrompts.push(systemPrompt.trim());
122-
extraPrompts.push(this.subNodePrompt);
123-
agentService.addSystemPrompts(extraPrompts);
119+
agentService.addStaticSystemPrompts([this.subNodePrompt]);
120+
if (systemPrompt?.trim()) {
121+
agentService.addDynamicSystemPrompts([systemPrompt.trim()]);
122+
}
124123

125124
const messages = await agentService.stream(task, subCallback, signal);
126125
const content: MCPContent[] = [];

packages/scorpio.ai/src/Agents/Single/SingleAgentService.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ export class SingleAgentService extends AgentServiceBase {
5555
protected modelService: IModelService;
5656
protected skillService?: ISkillService;
5757
protected toolService?: IAgentToolService;
58-
protected systemMessages: ChatMessage[];
58+
protected staticSystemPrompts: string[];
5959
protected dynamicSystemPrompts: string[];
6060
protected memorySystemPromptTemplate?: string;
6161
protected modelCallTimeout?: number;
6262
protected compactor?: ConversationCompactor;
6363

6464
constructor(
6565
@inject(IModelService) modelService: IModelService,
66-
@inject(T_StaticSystemPrompts, { optional: true }) systemPrompts?: string[],
66+
@inject(T_StaticSystemPrompts, { optional: true }) staticSystemPrompts?: string[],
6767
@inject(T_DynamicSystemPrompts, { optional: true }) dynamicSystemPrompts?: string[],
6868
@inject(ILoggerService, { optional: true }) loggerService?: ILoggerService,
6969
@inject(IAgentSaverService, { optional: true }) agentSaver?: IAgentSaverService,
@@ -80,23 +80,24 @@ export class SingleAgentService extends AgentServiceBase {
8080
this.modelService = modelService;
8181
this.skillService = skillService;
8282
this.toolService = toolService;
83-
this.systemMessages = (systemPrompts ?? []).map(p => ({ role: MessageRole.System, content: p }));
83+
this.staticSystemPrompts = staticSystemPrompts ?? [];
8484
this.dynamicSystemPrompts = dynamicSystemPrompts ?? [];
8585
this.memorySystemPromptTemplate = memorySystemPromptTemplate;
8686
this.modelCallTimeout = modelCallTimeout;
8787
this.compactor = compactor;
8888
}
8989

90-
override addSystemPrompts(prompts: string[]): void {
91-
this.systemMessages.unshift(...prompts.map(p => ({ role: MessageRole.System, content: p })));
90+
override addStaticSystemPrompts(prompts: string[]): void {
91+
this.staticSystemPrompts.unshift(...prompts);
92+
}
93+
94+
override addDynamicSystemPrompts(prompts: string[]): void {
95+
this.dynamicSystemPrompts.push(...prompts);
9296
}
9397

94-
/**
95-
* 构建本轮 system message,分为静态块(可缓存)和动态块(每次请求变化)
96-
*/
9798
protected async buildSystemMessage(query: MessageContent): Promise<ChatMessage | undefined> {
9899
// ── 静态部分(跨请求不变,可被 prompt caching 缓存) ──
99-
const staticParts: string[] = this.systemMessages.map(m => m.content as string);
100+
const staticParts: string[] = [...this.staticSystemPrompts];
100101
if (this.skillService) {
101102
const skillMessage = await this.skillService.getSystemMessage();
102103
if (skillMessage) staticParts.push(skillMessage);
@@ -130,13 +131,12 @@ export class SingleAgentService extends AgentServiceBase {
130131
}
131132
}
132133

133-
const staticContent = staticParts.join("\n\n").trim();
134-
const dynamicContent = dynamicParts.join("\n\n").trim();
135-
if (!staticContent && !dynamicContent) return undefined;
136-
137134
const contentBlocks: Array<{ type: string; text: string }> = [];
135+
const staticContent = staticParts.join("\n\n").trim();
138136
if (staticContent) contentBlocks.push({ type: "text", text: staticContent });
137+
const dynamicContent = dynamicParts.join("\n\n").trim();
139138
if (dynamicContent) contentBlocks.push({ type: "text", text: dynamicContent });
139+
if (contentBlocks.length === 0) return undefined;
140140
return { role: MessageRole.System, content: contentBlocks };
141141
}
142142

packages/scorpio.ai/src/Tools/TaskTool.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { createSuccessResult, type MCPToolResult } from "./types";
66

77
export interface TaskToolParams {
88
agentId: string;
9-
goal: string;
109
task: string;
1110
systemPrompt?: string;
1211
}
@@ -21,8 +20,6 @@ export function createTaskTool(agentIds: string[], runFn: RunTaskFn, description
2120
const schema = z.object({
2221
agentId: z.enum(agentIds as [string, ...string[]])
2322
.describe("ID of the sub-agent to invoke"),
24-
goal: z.string()
25-
.describe("The primary objective of this task in one concise sentence. Summarizes the end result expected from the sub-agent."),
2623
task: z.string()
2724
.describe("Complete, self-contained task instruction with all steps the agent must perform. Include every detail needed — the agent has no memory of prior conversation."),
2825
systemPrompt: z.string().optional()

0 commit comments

Comments
 (0)