Skip to content

Commit 065caa1

Browse files
author
linyuan.yang
committed
整理代码结构
1 parent cfd1a20 commit 065caa1

19 files changed

Lines changed: 87 additions & 77 deletions

File tree

‎packages/sbot/prompts/agent/react_system.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
vars:
33
agents: 可调度的子智能体列表 (XML 格式,含 ID 和描述)
44
---
5-
You are a ReAct orchestration expert. Break down the user's request and dispatch sub-tasks to specialized agents using the `task` tool.
5+
You are a ReAct orchestration expert. Break down the user's request and dispatch sub-tasks to specialized agents using the `_dispatch_task` tool.
66

77
<agents>
88
{agents}
@@ -12,7 +12,7 @@ You are a ReAct orchestration expert. Break down the user's request and dispatch
1212
<rule>Fully autonomous: make all decisions yourself. Never ask the user for clarification, confirmation, additional information, or approval — not even once.</rule>
1313
<rule>Assume and proceed: when the request is ambiguous, pick the most reasonable interpretation and act on it immediately without stating your assumption.</rule>
1414
<rule>One call at a time: invoke one agent, wait for the result, then decide the next step based on the output.</rule>
15-
<rule>Self-contained instructions: when starting a fresh session (no `taskId`), the `task` field must include all context the agent needs — no references like "as discussed" or "from the previous step".</rule>
15+
<rule>Self-contained instructions: when starting a fresh session (no `taskId`), the `task` field must include all context the agent needs — no references like "as discussed" or "from the previous step"; use `contextMode: state` only when the sub-agent should inherit a recent snapshot of the main conversation.</rule>
1616
<rule>Continue with taskId: when a follow-up belongs to the same line of work as a prior call, pass back the `task_id` from that call's result as `taskId` to resume the session — the agent already has the prior context, so `task` can be a short next-step instruction.</rule>
1717
<rule>No repeats: once an agent succeeds at a goal, never call it again for the same goal.</rule>
1818
<rule>Finish when done: reply to the user directly as soon as all goals are met; do not call any more tools.</rule>

‎packages/sbot/prompts/agent/react_task.txt‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎packages/sbot/src/Agent/AgentFactory.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ISkillService, SkillService,
1313
ServiceContainer, T_StaticSystemPrompts, T_DynamicSystemPrompts,
1414
ReActAgentService, T_AgentSubNodes, T_CreateAgent, T_ThinkModelService,
15-
T_ReactSystemPromptTemplate, T_ReactSubNodePrompt, T_ReactTaskToolDesc,
15+
T_ReactSystemPromptTemplate, T_ReactSubNodePrompt,
1616
T_SkillSystemPromptTemplate, T_SkillToolReadDesc, T_SkillToolListDesc, T_SkillToolExecDesc,
1717
T_ModelCallTimeout, T_MaxHistoryRounds,
1818
type CreateAgentFn,
@@ -307,7 +307,6 @@ export class AgentFactory {
307307
[T_ToolOverflowDir]: config.getConfigPath("tool-overflow", true),
308308
[T_ReactSystemPromptTemplate]: loadPrompt('agent/react_system.txt'),
309309
[T_ReactSubNodePrompt]: loadPrompt('agent/react_subnode.txt'),
310-
[T_ReactTaskToolDesc]: loadPrompt('agent/react_task.txt'),
311310
});
312311
return container.resolve(ReActAgentService);
313312
}

‎packages/sbot/src/Processing/buildExecuteTool.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ChatToolCall, ToolApproval, TASK_TOOL_NAME, READ_SKILL_FILE_TOOL_NAME, EXECUTE_SKILL_SCRIPT_TOOL_NAME, LIST_SKILL_FILES_TOOL_NAME, NOTE_SEARCH_TOOL_NAME, WIKI_SEARCH_TOOL_NAME, WIKI_READ_TOOL_NAME, AGENDA_CREATE_TOOL_NAME, AGENDA_LIST_TOOL_NAME, AGENDA_UPDATE_TOOL_NAME, AGENDA_COMPLETE_TOOL_NAME, AGENDA_CANCEL_TOOL_NAME } from "scorpio.ai";
1+
import { ChatToolCall, ToolApproval, DISPATCH_TASK_TOOL_NAME, READ_SKILL_FILE_TOOL_NAME, EXECUTE_SKILL_SCRIPT_TOOL_NAME, LIST_SKILL_FILES_TOOL_NAME, NOTE_SEARCH_TOOL_NAME, WIKI_SEARCH_TOOL_NAME, WIKI_READ_TOOL_NAME, AGENDA_CREATE_TOOL_NAME, AGENDA_LIST_TOOL_NAME, AGENDA_UPDATE_TOOL_NAME, AGENDA_COMPLETE_TOOL_NAME, AGENDA_CANCEL_TOOL_NAME } from "scorpio.ai";
22
import { SessionService, ASK_TOOL_NAME, SEND_FILE_TOOL_NAME } from "channel.base";
33
import { SLEEP_TOOL_NAME } from "../Tools/Sleep";
44
import { config } from "../Core/Config";
55

66
/** 内部工具名,直接放行无需用户确认 */
77
const INTERNAL_TOOLS = new Set([
88
ASK_TOOL_NAME,
9-
TASK_TOOL_NAME,
9+
DISPATCH_TASK_TOOL_NAME,
1010
// SkillService 工具
1111
READ_SKILL_FILE_TOOL_NAME,
1212
EXECUTE_SKILL_SCRIPT_TOOL_NAME,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { inject, T_StaticSystemPrompts, T_DynamicSystemPrompts, T_MaxHistoryRounds } from "../../Core";
2-
import { MCPContentType } from "../../Tools/types";
2+
import { MCPContentType } from "../../Tools/Core";
33
import { IModelService } from "../../Model";
44
import { IAgentSaverService } from "../../Saver";
55
import { ILoggerService } from "../../Logger";

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type StructuredToolInterface } from "@langchain/core/tools";
2-
import { inject, ServiceContainer, T_StaticSystemPrompts, T_DynamicSystemPrompts, T_ReactSystemPromptTemplate, T_ReactSubNodePrompt, T_ReactTaskToolDesc, T_ModelCallTimeout, T_ToolOverflowDir, T_ChannelSessionId, truncate } from "../../Core";
2+
import { inject, ServiceContainer, T_StaticSystemPrompts, T_DynamicSystemPrompts, T_ReactSystemPromptTemplate, T_ReactSubNodePrompt, T_ModelCallTimeout, T_ToolOverflowDir, T_ChannelSessionId, truncate } from "../../Core";
33
import { contentToString } from "../../Utils/contentUtils";
44
import { INoteService } from "../../Note";
55
import { IWikiService } from "../../Wiki";
@@ -12,8 +12,8 @@ import { IMemoryService } from "../../Memory";
1212
import { IAgendaService } from "../../Agenda";
1313
import { IAgentToolService } from "../../AgentTool";
1414
import { SingleAgentService } from "../Single/SingleAgentService";
15-
import { createTaskTool, createListTasksTool, TaskContextMode, TaskStatus, type RunTaskFn, type TaskInfo } from "../../Tools";
16-
import { MCPContentType, createTextContent, createImageContent, /* createAudioContent, */ createErrorResult, type MCPContent } from "../../Tools/types";
15+
import { createDispatchTaskTool, createListTasksTool, TaskContextMode, TaskStatus, type RunDispatchTaskFn, type TaskInfo } from "../../Tools";
16+
import { MCPContentType, createTextContent, createImageContent, /* createAudioContent, */ createErrorResult, type MCPContent } from "../../Tools/Core";
1717
import { v4 as uuidv4 } from "uuid";
1818

1919
// ── Tokens ────────────────────────────────────────────────────
@@ -43,9 +43,9 @@ interface TaskRecord {
4343
* 重写 buildSystemMessage / buildTools,其余流程(saver、note、StateGraph 循环)复用父类。
4444
*/
4545
export class ReActAgentService extends SingleAgentService {
46-
/** context:state 注入的父对话消息条数上限 */
46+
/** contextMode:state 注入的父对话消息条数上限 */
4747
private static readonly STATE_CONTEXT_MESSAGES = 8;
48-
/** context:state 每条消息的截断长度 */
48+
/** contextMode:state 每条消息的截断长度 */
4949
private static readonly STATE_CONTEXT_PER_MSG = 1000;
5050
/** 派生树最大深度(根编排者深度 0,故最多嵌套 MAX_SPAWN_DEPTH 层 ReAct)。 */
5151
private static readonly MAX_SPAWN_DEPTH = 5;
@@ -63,7 +63,6 @@ export class ReActAgentService extends SingleAgentService {
6363
@inject(T_CreateAgent) agentFactory: CreateAgentFn,
6464
@inject(T_ReactSystemPromptTemplate) private systemPromptTemplate: string,
6565
@inject(T_ReactSubNodePrompt) private subNodePrompt: string,
66-
@inject(T_ReactTaskToolDesc) private taskToolDesc: string,
6766
@inject(ISkillService) skillService: ISkillService,
6867
@inject(T_ToolOverflowDir) toolOverflowDir: string,
6968
@inject(T_ChannelSessionId) channelSessionId: number,
@@ -114,7 +113,7 @@ export class ReActAgentService extends SingleAgentService {
114113
if (!callback) return [];
115114
const { onMessage: _, ...subCallback } = callback;
116115

117-
const runFn: RunTaskFn = async ({ agentId, task, systemPrompt, taskId, context }) => {
116+
const runFn: RunDispatchTaskFn = async ({ agentId, task, systemPrompt, taskId, contextMode }) => {
118117
let agentService: AgentServiceBase | null = null;
119118
const thinkId = uuidv4();
120119
const resolvedTaskId = taskId ?? uuidv4();
@@ -163,8 +162,8 @@ export class ReActAgentService extends SingleAgentService {
163162
agentService.addDynamicSystemPrompts([systemPrompt.trim()]);
164163
}
165164

166-
// P1 上下文继承:仅新建会话(无 taskId)时按档注入。续接路径忽略 context——子会话已有自己的历史。
167-
if (!taskId && context === TaskContextMode.State) {
165+
// P1 上下文继承:仅新建会话(无 taskId)时按档注入。续接路径忽略 contextMode——子会话已有自己的历史。
166+
if (!taskId && contextMode === TaskContextMode.State) {
168167
const snippet = this.buildParentContextSnippet(await this.saverService.getMessages());
169168
if (snippet) agentService.addDynamicSystemPrompts([snippet]);
170169
}
@@ -206,11 +205,26 @@ export class ReActAgentService extends SingleAgentService {
206205
}
207206
};
208207

208+
const getTasks = async (): Promise<TaskInfo[]> => {
209+
const reg = await this.readRegistry();
210+
return [...reg.entries()]
211+
.sort((a, b) => a[1].updatedAt - b[1].updatedAt)
212+
.map(([taskId, r]) => ({
213+
taskId,
214+
agentId: r.agentId,
215+
name: r.name,
216+
status: r.status,
217+
firstTask: r.firstTask,
218+
lastSummary: r.lastSummary,
219+
turns: r.turns,
220+
}));
221+
};
222+
209223
const agentIds = this.agentSubNodes.map(a => a.id);
210224
const parentTools = await super.buildTools(callback, signal);
211225
return [
212-
createTaskTool(agentIds, runFn, this.taskToolDesc),
213-
createListTasksTool(() => this.listTasks()),
226+
createDispatchTaskTool(agentIds, runFn),
227+
createListTasksTool(getTasks),
214228
...parentTools,
215229
];
216230
}
@@ -250,22 +264,6 @@ export class ReActAgentService extends SingleAgentService {
250264
await this.writeRegistry(reg);
251265
}
252266

253-
/** 导出注册表快照(截断),供 _list_tasks 工具回灌编排 LLM。 */
254-
private async listTasks(): Promise<TaskInfo[]> {
255-
const reg = await this.readRegistry();
256-
return [...reg.entries()]
257-
.sort((a, b) => a[1].updatedAt - b[1].updatedAt)
258-
.map(([taskId, r]) => ({
259-
taskId,
260-
agentId: r.agentId,
261-
name: r.name,
262-
status: r.status,
263-
firstTask: r.firstTask,
264-
lastSummary: r.lastSummary,
265-
turns: r.turns,
266-
}));
267-
}
268-
269267
/** 取主对话最近若干条 Human/AI 文本消息,渲染成纯文本快照。
270268
* 纯文本注入(不进子 saver),故无 tool_calls/ToolMessage 配对约束;tool 消息忽略以降噪。 */
271269
private buildParentContextSnippet(messages: ChatMessage[]): string | undefined {

‎packages/scorpio.ai/src/Core/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export { ServiceContainer, globalServiceContainer, transient, singleton, inject, init, dispose, InjectionToken, Constructor, AbstractConstructor, Lifecycle, Provider, ClassProvider, FactoryProvider, ValueProvider } from "scorpio.di";
99

1010
// 通用 Token
11-
export { T_StaticSystemPrompts, T_DynamicSystemPrompts, T_DBPath, T_DBUrl, T_DBTable, T_ReactSystemPromptTemplate, T_ReactSubNodePrompt, T_ReactTaskToolDesc, T_SkillSystemPromptTemplate, T_SkillToolReadDesc, T_SkillToolListDesc, T_SkillToolExecDesc, T_NoteSystemPromptTemplate, T_NoteCachePath, T_MemoryDir, T_MemoryDbPath, T_MemoryReadTemplate, T_MemoryWriterPrompt, T_WikiSystemPromptTemplate, T_WikiCachePath, T_ModelCallTimeout, T_ToolOverflowDir, T_CompactPromptTemplate, T_PostCompactMessageTemplate, T_PostCompactContinuation, T_MaxHistoryRounds, T_NoteToolDescs, T_WikiToolDescs, T_MCPUtilityToolDescs, T_AgendaExtractorSystemPrompt, T_AgendaDbPath, T_AgendaToolDescs, T_ChannelSessionId } from "./tokens";
11+
export { T_StaticSystemPrompts, T_DynamicSystemPrompts, T_DBPath, T_DBUrl, T_DBTable, T_ReactSystemPromptTemplate, T_ReactSubNodePrompt, T_SkillSystemPromptTemplate, T_SkillToolReadDesc, T_SkillToolListDesc, T_SkillToolExecDesc, T_NoteSystemPromptTemplate, T_NoteCachePath, T_MemoryDir, T_MemoryDbPath, T_MemoryReadTemplate, T_MemoryWriterPrompt, T_WikiSystemPromptTemplate, T_WikiCachePath, T_ModelCallTimeout, T_ToolOverflowDir, T_CompactPromptTemplate, T_PostCompactMessageTemplate, T_PostCompactContinuation, T_MaxHistoryRounds, T_NoteToolDescs, T_WikiToolDescs, T_MCPUtilityToolDescs, T_AgendaExtractorSystemPrompt, T_AgendaDbPath, T_AgendaToolDescs, T_ChannelSessionId } from "./tokens";
1212

1313
// 工具函数
1414
export { parseJson, truncate, listThreadIds, listSubDirs } from "./utils";

‎packages/scorpio.ai/src/Core/tokens.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const T_DBUrl = Symbol("scorpio:T_DBUrl");
1010
export const T_DBTable = Symbol("scorpio:T_DBTable");
1111
export const T_ReactSystemPromptTemplate = Symbol("scorpio:T_ReactSystemPromptTemplate");
1212
export const T_ReactSubNodePrompt = Symbol("scorpio:T_ReactSubNodePrompt");
13-
export const T_ReactTaskToolDesc = Symbol("scorpio:T_ReactTaskToolDesc");
1413
export const T_SkillSystemPromptTemplate = Symbol("scorpio:T_SkillSystemPromptTemplate");
1514
export const T_SkillToolReadDesc = Symbol("scorpio:T_SkillToolReadDesc");
1615
export const T_SkillToolListDesc = Symbol("scorpio:T_SkillToolListDesc");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './types';
2+
export {
3+
truncateMCPToolResult,
4+
type TruncateToolResultOptions,
5+
} from './truncateToolResult';

packages/scorpio.ai/src/Tools/truncateToolResult.ts renamed to packages/scorpio.ai/src/Tools/Core/truncateToolResult.ts

File renamed without changes.

0 commit comments

Comments
 (0)