@ai-zen/agents-sdk 2.0.0-alpha.0 → 2.0.0-alpha.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 +73 -16
- package/README.md +397 -113
- package/README.zh.md +314 -112
- package/dist/agent-plugins/AssetTransportPlugin.d.ts +51 -0
- package/dist/agent-plugins/AssetTransportPlugin.js +145 -0
- package/dist/agent-plugins/AutoMigratePlugin.d.ts +35 -14
- package/dist/agent-plugins/AutoMigratePlugin.js +36 -36
- package/dist/agent-plugins/AutoRefreshToolsPlugin.d.ts +6 -6
- package/dist/agent-plugins/AutoRefreshToolsPlugin.js +2 -2
- package/dist/agent-plugins/ContextGuardPlugin.d.ts +20 -24
- package/dist/agent-plugins/ContextGuardPlugin.js +25 -24
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.d.ts +25 -35
- package/dist/agent-plugins/SubAgentGuardInstallerPlugin.js +26 -41
- package/dist/agent-plugins/UnknownToolHintPlugin.d.ts +14 -9
- package/dist/agent-plugins/UnknownToolHintPlugin.js +15 -13
- package/dist/config/ConfigManager.d.ts +13 -5
- package/dist/config/ConfigManager.js +58 -39
- package/dist/config/constants.d.ts +1 -1
- package/dist/config/constants.js +2 -2
- package/dist/config/factory-skills.js +2 -2
- package/dist/config/normalize.d.ts +93 -0
- package/dist/config/normalize.js +158 -0
- package/dist/crud/AgentRepository.d.ts +3 -1
- package/dist/crud/AgentRepository.js +4 -2
- package/dist/index.d.ts +19 -9
- package/dist/index.js +14 -7
- package/dist/runtime/AgentRuntime.d.ts +293 -0
- package/dist/runtime/AgentRuntime.js +457 -0
- package/dist/runtime/HandoffGuardPlugin.d.ts +82 -15
- package/dist/runtime/HandoffGuardPlugin.js +136 -17
- package/dist/runtime/HandoffService.d.ts +244 -119
- package/dist/runtime/HandoffService.js +373 -251
- package/dist/runtime/RuntimePeerSessions.d.ts +34 -0
- package/dist/runtime/RuntimePeerSessions.js +50 -0
- package/dist/runtime/Scopes.d.ts +123 -0
- package/dist/runtime/Scopes.js +169 -0
- package/dist/runtime/SdkAgent.d.ts +19 -8
- package/dist/runtime/SdkAgent.js +23 -6
- package/dist/runtime/completeOnce.d.ts +3 -3
- package/dist/runtime/completeOnce.js +3 -3
- package/dist/runtime/createAgent.d.ts +29 -7
- package/dist/runtime/createAgent.js +57 -12
- package/dist/runtime/createModel.d.ts +4 -4
- package/dist/runtime/createModel.js +6 -6
- package/dist/runtime/lastPromptTokens.d.ts +18 -0
- package/dist/runtime/lastPromptTokens.js +26 -0
- package/dist/runtime/resolveModel.d.ts +9 -9
- package/dist/runtime/resolveModel.js +8 -8
- package/dist/scope/Scope.d.ts +10 -9
- package/dist/scope/Scope.js +6 -6
- package/dist/scope/ScopePlugin.d.ts +2 -8
- package/dist/scope/disclosure.d.ts +1 -1
- package/dist/scope/disclosure.js +1 -1
- package/dist/scope/plugins/allInOne.d.ts +13 -1
- package/dist/scope/plugins/allInOne.js +9 -5
- package/dist/scope/plugins/builtin/AsyncExecTool.d.ts +12 -0
- package/dist/scope/plugins/builtin/AsyncExecTool.js +103 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.d.ts +10 -0
- package/dist/scope/plugins/builtin/AsyncTimeoutTool.js +90 -0
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +1 -1
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +1 -1
- package/dist/scope/plugins/builtin/DownloadFileTool.js +1 -1
- package/dist/scope/plugins/builtin/ExecTool.js +10 -199
- package/dist/scope/plugins/builtin/FindTextTool.d.ts +1 -1
- package/dist/scope/plugins/builtin/FindTextTool.js +1 -1
- package/dist/scope/plugins/builtin/GenerateImageTool.d.ts +1 -4
- package/dist/scope/plugins/builtin/GenerateImageTool.js +2 -8
- package/dist/scope/plugins/builtin/InspectFileTool.js +1 -1
- package/dist/scope/plugins/builtin/SdkCallbackTool.d.ts +4 -4
- package/dist/scope/plugins/builtin/SdkCallbackTool.js +3 -3
- package/dist/scope/plugins/builtin/SleepTool.js +2 -3
- package/dist/scope/plugins/builtin/ViewImageTool.d.ts +2 -4
- package/dist/scope/plugins/builtin/ViewImageTool.js +4 -6
- package/dist/scope/plugins/builtin/WriteTool.d.ts +7 -0
- package/dist/scope/plugins/builtin/WriteTool.js +8 -1
- package/dist/scope/plugins/builtin/asyncTasks.d.ts +41 -0
- package/dist/scope/plugins/builtin/asyncTasks.js +97 -0
- package/dist/scope/plugins/builtin/commandOutput.d.ts +22 -0
- package/dist/scope/plugins/builtin/commandOutput.js +55 -0
- package/dist/scope/plugins/builtin/commandRun.d.ts +48 -0
- package/dist/scope/plugins/builtin/commandRun.js +169 -0
- package/dist/scope/plugins/builtin/discover.d.ts +1 -3
- package/dist/scope/plugins/builtin/discover.js +1 -3
- package/dist/scope/plugins/builtin/index.d.ts +5 -7
- package/dist/scope/plugins/builtin/index.js +7 -7
- package/dist/scope/plugins/builtin/outputGuard.d.ts +1 -5
- package/dist/scope/plugins/builtin/outputGuard.js +1 -5
- package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +56 -11
- package/dist/scope/plugins/mcp/McpConnectionManager.js +169 -74
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +17 -7
- package/dist/scope/plugins/mcp/McpScopePlugin.js +36 -12
- package/dist/scope/plugins/mcp/discover.d.ts +9 -2
- package/dist/scope/plugins/mcp/discover.js +12 -14
- package/dist/scope/plugins/mcp/mcpTaskNotice.d.ts +52 -0
- package/dist/scope/plugins/mcp/mcpTaskNotice.js +8 -0
- package/dist/scope/plugins/mcp/mcpTools.d.ts +20 -2
- package/dist/scope/plugins/mcp/mcpTools.js +220 -39
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.d.ts +4 -7
- package/dist/scope/plugins/peers/PeerSessionsScopePlugin.js +4 -7
- package/dist/scope/plugins/peers/peerSessionTools.d.ts +97 -46
- package/dist/scope/plugins/peers/peerSessionTools.js +147 -121
- package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +10 -4
- package/dist/scope/plugins/skills/discover.d.ts +15 -8
- package/dist/scope/plugins/skills/discover.js +15 -26
- package/dist/scope/plugins/skills/skillTools.d.ts +8 -7
- package/dist/scope/plugins/skills/skillTools.js +18 -14
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +4 -1
- package/dist/scope/plugins/subagents/discover.d.ts +7 -1
- package/dist/scope/plugins/subagents/discover.js +7 -3
- package/dist/scope/plugins/subagents/subAgentTools.d.ts +0 -1
- package/dist/scope/plugins/subagents/subAgentTools.js +11 -1
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +4 -0
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +4 -1
- package/dist/scope/plugins/usertools/discover.d.ts +7 -8
- package/dist/scope/plugins/usertools/discover.js +17 -27
- package/dist/shared/EntityRepository.d.ts +22 -5
- package/dist/shared/EntityRepository.js +45 -29
- package/dist/shared/content-blocks.d.ts +9 -3
- package/dist/shared/content-blocks.js +12 -19
- package/dist/shared/errors.d.ts +5 -7
- package/dist/shared/errors.js +6 -8
- package/dist/shared/logger.d.ts +21 -10
- package/dist/shared/logger.js +9 -15
- package/dist/types/index.d.ts +36 -13
- package/package.json +2 -2
- package/dist/config/migrate.d.ts +0 -115
- package/dist/config/migrate.js +0 -200
- package/dist/scope/plugins/builtin/test-helpers.d.ts +0 -3
- package/dist/scope/plugins/builtin/test-helpers.js +0 -4
|
@@ -2,42 +2,64 @@ import type { Agent, Tool } from "@ai-zen/agents-core";
|
|
|
2
2
|
/**
|
|
3
3
|
* 平级会话(peer session)的三个工具 —— **只负责把模型的话翻译成端口调用**。
|
|
4
4
|
*
|
|
5
|
-
* 端口({@link PeerSessionPort}
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* `label
|
|
9
|
-
*
|
|
10
|
-
* 为什么这几句话必须同时出现在 description
|
|
11
|
-
*
|
|
12
|
-
*
|
|
5
|
+
* 端口({@link PeerSessionPort})由宿主实现:开一段会话、把一句话投进去、现读它此刻落在哪一格,
|
|
6
|
+
* 都是宿主的事(会话是宿主的财产,SDK 不认识会话)。SDK 这一侧只有三样东西:参数说明、
|
|
7
|
+
* **给模型看的那几句话**(别轮询:她说完会自己 `post_message` 回来;记不住 `sessionId` 就靠
|
|
8
|
+
* `label`),以及三个工具各自的返回形状。
|
|
9
|
+
*
|
|
10
|
+
* ## 为什么这几句话必须同时出现在 description 与返回文案两处
|
|
11
|
+
*
|
|
12
|
+
* 模型只读它拿到的东西 —— 第一次只看得到 description,之后每次调用只看得到返回值。少写一处,
|
|
13
|
+
* 模型就会开始忙等轮询(`read_session_status` 一个接一个),而每次轮询都要烧一次模型调用。
|
|
14
|
+
*
|
|
15
|
+
* ## 投递不承诺任何未来
|
|
16
|
+
*
|
|
17
|
+
* `post_message` 的回执只有「收下了」。那边忙不忙、什么时候取件、取走之后会做什么,**都不是
|
|
18
|
+
* 这一句话能定下的**,写在回执里就是对未来的断言 —— 而未来只能靠观测(`read_session_status`)
|
|
19
|
+
* 与对方自己发来的话去读。所以本文件的回执里没有「把它叫起来了」「排在它手头那批后面」这类话。
|
|
20
|
+
*
|
|
21
|
+
* ## 投递不看关系
|
|
22
|
+
*
|
|
23
|
+
* 谁都能给谁投:`post_message` 只问「目标在不在、活不活」,不问「你俩是什么关系」。平级之间
|
|
24
|
+
* 能不能互发、发了对方理不理,靠的是 agent 自己的知识(它在提示里就知道自己跟谁在说话),
|
|
25
|
+
* 不是一道校验。只有**创建**还有「只限一层」这条产品限制,它写在 `create_session` 那边。
|
|
13
26
|
*/
|
|
14
|
-
/** `create_session
|
|
27
|
+
/** `create_session`:开一段平级会话,建完即回 */
|
|
15
28
|
export declare const CREATE_SESSION_TOOL = "create_session";
|
|
16
|
-
/** `read_session_status`:只问状态 +
|
|
29
|
+
/** `read_session_status`:只问状态 + 一句摘要,绝不返回全量消息 */
|
|
17
30
|
export declare const READ_SESSION_STATUS_TOOL = "read_session_status";
|
|
18
|
-
/** `
|
|
19
|
-
export declare const
|
|
31
|
+
/** `post_message`:给另一段会话投一句话(不等结果,投递不看关系) */
|
|
32
|
+
export declare const POST_MESSAGE_TOOL = "post_message";
|
|
20
33
|
/** 本插件向 `tools` 维度贡献的候选名(顺序即展示顺序) */
|
|
21
|
-
export declare const PEER_SESSION_TOOL_NAMES: readonly ["create_session", "read_session_status", "
|
|
34
|
+
export declare const PEER_SESSION_TOOL_NAMES: readonly ["create_session", "read_session_status", "post_message"];
|
|
22
35
|
/**
|
|
23
|
-
*
|
|
36
|
+
* 忙不忙 —— **只读的观测值**,就是 core 的 `agent.state` 那两格(`PRD.md` 4.4),名字不改。
|
|
24
37
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
38
|
+
* 这里**没有** `paused`、也没有 `dead`:那两件事不是「忙不忙」—— 一个是输出闸关着(它闲着,
|
|
39
|
+
* 但不会自己动),一个是根本没有 agent 对象。挤进同一格,模型就分不清「它闲着」与「它压根
|
|
40
|
+
* 不是活的」。死活由同一个答复里的 `alive` 说;两道闸对平级会话不报(`PRD.md` 4.6 只要
|
|
41
|
+
* 「忙不忙 + 最近一句摘要」)。
|
|
42
|
+
*
|
|
43
|
+
* 也**没有** `running` / `failed` / `aborted`:失败是某条消息上的事实(那条消息的 `status` /
|
|
44
|
+
* `error`),不是一段会话此刻的状态;也**没有** `gone`:会话找不到就是 `found: false`。
|
|
27
45
|
*/
|
|
28
|
-
export type
|
|
46
|
+
export type PeerSessionActivity = "working" | "idle";
|
|
29
47
|
/**
|
|
30
48
|
* `create_session` 被**结构化拒绝**的原因。
|
|
31
49
|
*
|
|
32
50
|
* 拒绝是返回结果而不是异常:模型要能读懂并改变策略(「这层不能开分身了,那我自己做」
|
|
33
|
-
*
|
|
51
|
+
* 「这个助手没有可用模型,换一个」「手头那几个还忙,先等一等或分给更少的会话」),异常只会
|
|
52
|
+
* 把它这一步打断。
|
|
53
|
+
*
|
|
54
|
+
* 三条都是**创建**的限制:`post_message` 没有「只限一层」「限额」这类判据 —— 投递只看目标
|
|
55
|
+
* 在不在、活不活。
|
|
34
56
|
*/
|
|
35
57
|
export type PeerSessionRejectReason = "depth_exceeded" | "model_unresolved" | "too_many_peers";
|
|
36
58
|
/**
|
|
37
59
|
* `create_session` 的答案。
|
|
38
60
|
*
|
|
39
|
-
* 成功只给两个字段:`sessionId
|
|
40
|
-
*
|
|
61
|
+
* 成功只给两个字段:`sessionId`(新开那段会话的 id)与可读的 `label`。**没有结果** ——
|
|
62
|
+
* 建完即回:这里从来不等那边答完,也不会有任何东西被推回给发起者。
|
|
41
63
|
*/
|
|
42
64
|
export type PeerSessionCreated = {
|
|
43
65
|
ok: true;
|
|
@@ -49,35 +71,54 @@ export type PeerSessionCreated = {
|
|
|
49
71
|
hint?: string;
|
|
50
72
|
};
|
|
51
73
|
/**
|
|
52
|
-
* `read_session_status`
|
|
74
|
+
* `read_session_status` 的答案 —— **判别式**:要么「没有这一段」,要么「有这一段」并给出忙不忙与死活。
|
|
75
|
+
*
|
|
76
|
+
* `found: false` 那支的 `status` 就是 `null`:找不到不是一个状态值(忙不忙、活不活都是活对象
|
|
77
|
+
* 身上的事实,找不到就没有对象可读)。把它挤进同一格会让模型把「会话被删了」当成一种可等的情形。
|
|
53
78
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
79
|
+
* 有这一段时只有三格:**忙不忙**(`status`)、**叫什么**(`label`)、**最近一句说了什么**
|
|
80
|
+
* (`lastMessage`:最近一条助手消息的正文摘要,截断到 300 字,超出以省略号收尾;它还没说过话
|
|
81
|
+
* 就是 `null`)—— 这就是需求说的「忙不忙 + 最近一句摘要」的全部(`PRD.md` 2.3 / 4.6、
|
|
82
|
+
* `PLAN.md` 10.2)。
|
|
83
|
+
*
|
|
84
|
+
* **不给全量消息,也不给历史**(没有消息数组、没有条数、没有原文):给了原文模型就会把它当
|
|
85
|
+
* 上下文搬运,token 立刻炸,而且绕过了我们自己的上下文管理。
|
|
56
86
|
*/
|
|
57
|
-
export
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
87
|
+
export type PeerSessionStatus = {
|
|
88
|
+
found: false;
|
|
89
|
+
status: null;
|
|
90
|
+
} | {
|
|
91
|
+
found: true;
|
|
92
|
+
/** 忙不忙(两格,见 {@link PeerSessionActivity}) */
|
|
93
|
+
status: PeerSessionActivity;
|
|
94
|
+
/** 死活:那一段此刻还有没有 agent 对象(`false` = 死了,投过去也没人取件) */
|
|
95
|
+
alive: boolean;
|
|
96
|
+
label: string;
|
|
97
|
+
/** 最近一条助手消息的正文摘要(最多 300 字 + 省略号);还没说过话就是 `null` */
|
|
98
|
+
lastMessage: string | null;
|
|
99
|
+
};
|
|
63
100
|
/**
|
|
64
|
-
* `
|
|
101
|
+
* `post_message` 被拒绝的两种原因 —— 就是投递的两条判据。
|
|
65
102
|
*
|
|
66
|
-
*
|
|
67
|
-
* `
|
|
68
|
-
*
|
|
103
|
+
* `gone`:这段会话不在(会话行被删了);
|
|
104
|
+
* `dead`:会话在,但那边没有 agent 对象,投过去也没有人会取件。
|
|
105
|
+
*
|
|
106
|
+
* 两者对模型是同一类答复:这句话没投出去,别等回音。
|
|
107
|
+
*/
|
|
108
|
+
export type PeerSessionSendRejectReason = "gone" | "dead";
|
|
109
|
+
/**
|
|
110
|
+
* `post_message` 的答案。
|
|
69
111
|
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
112
|
+
* 成功只有 `ok`,一个字段都不多:投递那一刻能定下的只有「收下了」。那边忙不忙、什么时候取件
|
|
113
|
+
* 都不是回执该说的话(那是对未来的断言)。
|
|
72
114
|
*/
|
|
73
115
|
export type PeerSessionSent = {
|
|
74
116
|
ok: true;
|
|
75
|
-
state: "running" | "idle" | "settling";
|
|
76
117
|
} | {
|
|
77
118
|
ok: false;
|
|
78
|
-
reason:
|
|
119
|
+
reason: PeerSessionSendRejectReason;
|
|
79
120
|
};
|
|
80
|
-
/** `
|
|
121
|
+
/** `post_message` 的入参(`caller` 由插件从 `ToolCallContext` 取,不由模型给) */
|
|
81
122
|
export interface PeerSessionSendInput {
|
|
82
123
|
/** 谁在调这个工具(`ctx.agent`)—— 宿主据此认领「哪一段会话在说话」与消息署名 */
|
|
83
124
|
caller: Agent;
|
|
@@ -86,7 +127,7 @@ export interface PeerSessionSendInput {
|
|
|
86
127
|
}
|
|
87
128
|
/** `create_session` 的入参(`caller` 由插件从 `ToolCallContext` 取,不由模型给) */
|
|
88
129
|
export interface PeerSessionCreateInput {
|
|
89
|
-
/** 谁在调这个工具(`ctx.agent`)——
|
|
130
|
+
/** 谁在调这个工具(`ctx.agent`)—— 宿主据此认领「哪一段会话在开新会话」 */
|
|
90
131
|
caller: Agent;
|
|
91
132
|
prompt: string;
|
|
92
133
|
agentId?: string;
|
|
@@ -96,27 +137,37 @@ export interface PeerSessionCreateInput {
|
|
|
96
137
|
/**
|
|
97
138
|
* 宿主实现给 SDK 的端口(**SDK 不依赖宿主**:这个接口是唯一的一处接线)。
|
|
98
139
|
*
|
|
99
|
-
*
|
|
140
|
+
* 不给端口时插件不产出任何工具。
|
|
100
141
|
*/
|
|
101
142
|
export interface PeerSessionPort {
|
|
143
|
+
/**
|
|
144
|
+
* 开一段会话,**建完立刻回** `{ sessionId, label }`。
|
|
145
|
+
*
|
|
146
|
+
* 「立刻」是这一格的全部意思:不在里面等那边答完,也不挂 `.then` 去接任何后续事实。
|
|
147
|
+
* 拒绝照 {@link PeerSessionCreated} 的样子回,不抛错。
|
|
148
|
+
*/
|
|
102
149
|
create(input: PeerSessionCreateInput): Promise<PeerSessionCreated>;
|
|
150
|
+
/**
|
|
151
|
+
* 现读那一格**加上最近一句的摘要**(不存镜像:状态是活对象身上的事实);找不到就是
|
|
152
|
+
* `found: false`。摘要只是「最近一条助手消息的开头一段」,不是消息列表。
|
|
153
|
+
*/
|
|
103
154
|
status(input: {
|
|
104
155
|
caller: Agent;
|
|
105
156
|
sessionId: string;
|
|
106
157
|
}): Promise<PeerSessionStatus>;
|
|
107
158
|
/**
|
|
108
|
-
*
|
|
159
|
+
* 把一句话投给一段会话(**不等结果,也不回状态**)。
|
|
160
|
+
*
|
|
161
|
+
* 与 `create` / `status` 同构:**只收 `caller`**,不收来源名 / label —— 正文那行
|
|
162
|
+
* 「来自 会话「…」」由宿主按调用方解析(模型说自己是谁不算数)。
|
|
109
163
|
*
|
|
110
|
-
*
|
|
111
|
-
* 自动推送」由宿主按调用方解析(模型说自己是谁不算数)。
|
|
164
|
+
* 收下了就是 `{ ok: true }`;没投出去给原因({@link PeerSessionSendRejectReason})。
|
|
112
165
|
*/
|
|
113
166
|
send(input: PeerSessionSendInput): Promise<PeerSessionSent>;
|
|
114
167
|
}
|
|
115
|
-
/** `send_to_session` 成功回执里的 `state`(与宿主的 `DeliveryOutcome.state` 同义) */
|
|
116
|
-
export type PeerSessionSendState = "running" | "idle" | "settling";
|
|
117
168
|
/**
|
|
118
169
|
* 造三个工具(顺序即返回顺序)。
|
|
119
170
|
*
|
|
120
|
-
* 三个工具都是即时返回的 `CallbackTool
|
|
171
|
+
* 三个工具都是即时返回的 `CallbackTool`:真正的等待在宿主那一侧(那边的 agent 自己跑)。
|
|
121
172
|
*/
|
|
122
173
|
export declare function createPeerSessionTools(port: PeerSessionPort): Tool[];
|
|
@@ -2,109 +2,119 @@ import { CallbackTool } from "@ai-zen/agents-core";
|
|
|
2
2
|
/**
|
|
3
3
|
* 平级会话(peer session)的三个工具 —— **只负责把模型的话翻译成端口调用**。
|
|
4
4
|
*
|
|
5
|
-
* 端口({@link PeerSessionPort}
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* `label
|
|
5
|
+
* 端口({@link PeerSessionPort})由宿主实现:开一段会话、把一句话投进去、现读它此刻落在哪一格,
|
|
6
|
+
* 都是宿主的事(会话是宿主的财产,SDK 不认识会话)。SDK 这一侧只有三样东西:参数说明、
|
|
7
|
+
* **给模型看的那几句话**(别轮询:她说完会自己 `post_message` 回来;记不住 `sessionId` 就靠
|
|
8
|
+
* `label`),以及三个工具各自的返回形状。
|
|
9
9
|
*
|
|
10
|
-
* 为什么这几句话必须同时出现在 description
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* ## 为什么这几句话必须同时出现在 description 与返回文案两处
|
|
11
|
+
*
|
|
12
|
+
* 模型只读它拿到的东西 —— 第一次只看得到 description,之后每次调用只看得到返回值。少写一处,
|
|
13
|
+
* 模型就会开始忙等轮询(`read_session_status` 一个接一个),而每次轮询都要烧一次模型调用。
|
|
14
|
+
*
|
|
15
|
+
* ## 投递不承诺任何未来
|
|
16
|
+
*
|
|
17
|
+
* `post_message` 的回执只有「收下了」。那边忙不忙、什么时候取件、取走之后会做什么,**都不是
|
|
18
|
+
* 这一句话能定下的**,写在回执里就是对未来的断言 —— 而未来只能靠观测(`read_session_status`)
|
|
19
|
+
* 与对方自己发来的话去读。所以本文件的回执里没有「把它叫起来了」「排在它手头那批后面」这类话。
|
|
20
|
+
*
|
|
21
|
+
* ## 投递不看关系
|
|
22
|
+
*
|
|
23
|
+
* 谁都能给谁投:`post_message` 只问「目标在不在、活不活」,不问「你俩是什么关系」。平级之间
|
|
24
|
+
* 能不能互发、发了对方理不理,靠的是 agent 自己的知识(它在提示里就知道自己跟谁在说话),
|
|
25
|
+
* 不是一道校验。只有**创建**还有「只限一层」这条产品限制,它写在 `create_session` 那边。
|
|
13
26
|
*/
|
|
14
|
-
/** `create_session
|
|
27
|
+
/** `create_session`:开一段平级会话,建完即回 */
|
|
15
28
|
export const CREATE_SESSION_TOOL = "create_session";
|
|
16
|
-
/** `read_session_status`:只问状态 +
|
|
29
|
+
/** `read_session_status`:只问状态 + 一句摘要,绝不返回全量消息 */
|
|
17
30
|
export const READ_SESSION_STATUS_TOOL = "read_session_status";
|
|
18
|
-
/** `
|
|
19
|
-
export const
|
|
31
|
+
/** `post_message`:给另一段会话投一句话(不等结果,投递不看关系) */
|
|
32
|
+
export const POST_MESSAGE_TOOL = "post_message";
|
|
20
33
|
/** 本插件向 `tools` 维度贡献的候选名(顺序即展示顺序) */
|
|
21
34
|
export const PEER_SESSION_TOOL_NAMES = [
|
|
22
35
|
CREATE_SESSION_TOOL,
|
|
23
36
|
READ_SESSION_STATUS_TOOL,
|
|
24
|
-
|
|
37
|
+
POST_MESSAGE_TOOL,
|
|
25
38
|
];
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
/**
|
|
40
|
+
* 「别轮询」那半句:**她说完会自己 post_message 回来**。
|
|
41
|
+
*
|
|
42
|
+
* 这是唯一一句能替模型省下轮询的话:投递不看关系(谁都能给谁投),所以对方真有话要说时不必
|
|
43
|
+
* 等谁去问。
|
|
44
|
+
*/
|
|
45
|
+
const NO_POLL_HINT = "Do not poll it: when that session has something for me it calls post_message on me itself.";
|
|
46
|
+
/** 「靠 label 认会话」那半句:`sessionId` 是 UUID,长会话里记不住 */
|
|
47
|
+
const LABEL_HINT = "Keep `label` next to `sessionId`: if I forget the id later, `label` is how I name this session.";
|
|
30
48
|
/** 拒绝各自的一句话:说清「为什么」与「接下来能做什么」(宿主没给 hint 时用它) */
|
|
31
49
|
const REJECT_HINTS = {
|
|
32
|
-
depth_exceeded: "This
|
|
33
|
-
"
|
|
34
|
-
"that started this one
|
|
35
|
-
model_unresolved: "No model could be resolved for that
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
too_many_peers: "This
|
|
39
|
-
"Wait
|
|
50
|
+
depth_exceeded: "This session is itself a peer session, and peer sessions cannot open further ones " +
|
|
51
|
+
"(creating is one level only — posting a message is not limited this way). Do the work here " +
|
|
52
|
+
"instead, or report back and let the session that started this one open it.",
|
|
53
|
+
model_unresolved: "No model could be resolved for that session (no session override, no modelId on the agent " +
|
|
54
|
+
"definition, no global default model). Pass `modelId`, or pass an `agentId` that has a model " +
|
|
55
|
+
"bound.",
|
|
56
|
+
too_many_peers: "This session already has the maximum number of peer sessions busy at the same time. " +
|
|
57
|
+
"Wait until some of them are free (read_session_status tells me which one is idle), or give " +
|
|
40
58
|
"the work to fewer sessions instead of opening another one.",
|
|
41
59
|
};
|
|
42
60
|
/**
|
|
43
|
-
* `
|
|
61
|
+
* `post_message` 被拒绝的两种原因各自的一句话(端口只回原因,不给文案)。
|
|
44
62
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
63
|
+
* 两条都要说清「这句话没投出去、不会有人回音」,否则模型会等一条永远不会到的话。两者都不建议
|
|
64
|
+
* 重试:目标还在就等它自己说话,目标已经不在就自己把活干掉。
|
|
47
65
|
*/
|
|
48
66
|
const SEND_REJECT_HINTS = {
|
|
49
|
-
gone: "There is no
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
"
|
|
54
|
-
"sent — handle it here, or report back and let the conversation that started this one relay.",
|
|
67
|
+
gone: "There is no such session any more: it was deleted, or the host restarted and that in-process " +
|
|
68
|
+
"record is gone (it is now an ordinary session that still shows up in the session list). " +
|
|
69
|
+
"Nothing was sent and no answer will arrive — do the work here instead.",
|
|
70
|
+
dead: "That session exists but there is no agent behind it any more, so nobody will take the " +
|
|
71
|
+
"message. Nothing was sent and no answer will arrive — do the work here instead.",
|
|
55
72
|
};
|
|
56
73
|
/**
|
|
57
|
-
*
|
|
74
|
+
* 「摘要那一格」那半句:答案里另给一格**最近一句**的摘要 —— 不是全量、不是历史。
|
|
58
75
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* ② **不要等结果**,跑完会自动推回来;
|
|
62
|
-
* ③ 记不住 `sessionId` 就靠 `label` 认。
|
|
63
|
-
* 「排在末尾 / 叫起来 / 先攒着」只差头一句,后两句三支一模一样。
|
|
76
|
+
* 写它的理由与 {@link NO_POLL_HINT} 一样:模型会照着自己拿到的那段话理解能力边界,不说清
|
|
77
|
+
* 「这只是摘要」,它下一句就会以为自己在读对方的会话。
|
|
64
78
|
*/
|
|
65
|
-
const
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
const EXCERPT_HINT = "`lastMessage` in this answer is only the beginning of the last thing it said (cut at 300 " +
|
|
80
|
+
"characters), and it is all I get: never that session's full transcript, never its history, " +
|
|
81
|
+
"no message count — do not use this answer to fetch context.";
|
|
82
|
+
/**
|
|
83
|
+
* 「收下了」那半句:只说两件当场能定下的事 —— ① 已进那边的 inbox;② 别轮询。
|
|
84
|
+
*
|
|
85
|
+
* **不写**「它现在在忙 / 会把它叫起来」,也不写「不会自动推给我」:后者与「她说完会自己
|
|
86
|
+
* post_message 回来」相互打脸,而且两者都是对未来的断言。真正能说的只有
|
|
87
|
+
* {@link NO_POLL_HINT} 那一句(“真要说话时会自己发过来”),它刚好把「别轮询」说清。
|
|
88
|
+
*/
|
|
89
|
+
const SEND_ACCEPTED_HINT = "Accepted: the message is in that session's inbox now. That is all this call confirms — " +
|
|
90
|
+
"whether or when that session acts on it is not promised. This call does not come back. " +
|
|
91
|
+
`${NO_POLL_HINT} ${LABEL_HINT}`;
|
|
92
|
+
/**
|
|
93
|
+
* 给模型的一句话:先把「忙不忙 / 还活不活着」说清(两件事,各自一个字段,不合成一格),
|
|
94
|
+
* 再带上「摘要只是摘要」与「靠 label」两句,活着的时候再加「别轮询」。
|
|
95
|
+
*/
|
|
96
|
+
function statusHint(status, alive) {
|
|
97
|
+
if (!alive) {
|
|
98
|
+
return ("There is no agent behind that session any more (it is dead): nothing will be taken in " +
|
|
99
|
+
`there and no answer will arrive. Do the work here instead. ${EXCERPT_HINT} ${LABEL_HINT}`);
|
|
78
100
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
function statusHint(state) {
|
|
82
|
-
switch (state) {
|
|
83
|
-
case "running":
|
|
84
|
-
return ("It is still running. Its result will be pushed to me automatically when it settles — do " +
|
|
85
|
-
"not poll it. If I forget the `sessionId`, the `label` identifies this session.");
|
|
86
|
-
case "idle":
|
|
87
|
-
return ("It finished and its result was already pushed to me automatically. Do not poll it again. " +
|
|
88
|
-
"If I forget the `sessionId`, the `label` identifies this session.");
|
|
89
|
-
case "failed":
|
|
90
|
-
case "aborted":
|
|
91
|
-
return ("It stopped without finishing; the result (including why it stopped) was already pushed " +
|
|
92
|
-
"to me automatically. Do not poll it again. If I forget the `sessionId`, the `label` " +
|
|
93
|
-
"identifies this session.");
|
|
94
|
-
case "gone":
|
|
95
|
-
return ("There is no record of this session any more: it was deleted, or the host restarted and " +
|
|
96
|
-
"that in-process record is gone (in that case it is now an ordinary conversation that " +
|
|
97
|
-
"still shows up in the session list). Do not wait for a result that will never arrive; if " +
|
|
98
|
-
"I only remember the `label`, that session can no longer be looked up by it.");
|
|
101
|
+
if (status === "working") {
|
|
102
|
+
return `It is working: it has work in hand right now. ${NO_POLL_HINT} ${EXCERPT_HINT} ${LABEL_HINT}`;
|
|
99
103
|
}
|
|
104
|
+
return `It is idle: it exists and has nothing in hand at this moment. ${NO_POLL_HINT} ${EXCERPT_HINT} ${LABEL_HINT}`;
|
|
100
105
|
}
|
|
106
|
+
/** 会话根本找不到(`found: false`)时给模型的一句话:这不是第五格,是「没有这一段」 */
|
|
107
|
+
const NOT_FOUND_HINT = "There is no record of that session any more: it was deleted, or the host restarted and " +
|
|
108
|
+
"that in-process record is gone (in that case it is now an ordinary session that " +
|
|
109
|
+
"still shows up in the session list). No answer will arrive there. If I only remember the " +
|
|
110
|
+
"`label`, that session can no longer be looked up by it.";
|
|
101
111
|
/**
|
|
102
112
|
* 造三个工具(顺序即返回顺序)。
|
|
103
113
|
*
|
|
104
|
-
* 三个工具都是即时返回的 `CallbackTool
|
|
114
|
+
* 三个工具都是即时返回的 `CallbackTool`:真正的等待在宿主那一侧(那边的 agent 自己跑)。
|
|
105
115
|
*/
|
|
106
116
|
export function createPeerSessionTools(port) {
|
|
107
|
-
return [createSessionTool(port), readSessionStatusTool(port),
|
|
117
|
+
return [createSessionTool(port), readSessionStatusTool(port), post_messageTool(port)];
|
|
108
118
|
}
|
|
109
119
|
/** `create_session({ prompt, agentId?, modelId?, label? })` → `{ sessionId, label }` */
|
|
110
120
|
function createSessionTool(port) {
|
|
@@ -112,15 +122,16 @@ function createSessionTool(port) {
|
|
|
112
122
|
function: {
|
|
113
123
|
name: CREATE_SESSION_TOOL,
|
|
114
124
|
description: [
|
|
115
|
-
"Open a PEER SESSION: a separate
|
|
116
|
-
"
|
|
117
|
-
"
|
|
118
|
-
"
|
|
119
|
-
"
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
"
|
|
123
|
-
"
|
|
125
|
+
"Open a PEER SESSION: a separate session with its own agent that works on `prompt`",
|
|
126
|
+
"alongside this one (own context, own usage, and it stays readable in the session list",
|
|
127
|
+
"afterwards). Use it for work that can happen in parallel with mine instead of blocking me.",
|
|
128
|
+
"This call returns as soon as the session exists: it does not wait for any answer and no",
|
|
129
|
+
"answer is delivered to me here.",
|
|
130
|
+
"Talk to it afterwards with post_message; do not poll it — when that session has something",
|
|
131
|
+
"for me it calls post_message on me itself.",
|
|
132
|
+
"Creating is limited to one level (a peer session cannot open another one); posting a",
|
|
133
|
+
"message is not limited that way — any session may post a message to any other.",
|
|
134
|
+
"Remember the returned `label` as well as `sessionId`: in a long session I may no",
|
|
124
135
|
"longer recall the id, and the label is how I refer to this session.",
|
|
125
136
|
].join(" "),
|
|
126
137
|
parameters: {
|
|
@@ -128,23 +139,23 @@ function createSessionTool(port) {
|
|
|
128
139
|
properties: {
|
|
129
140
|
prompt: {
|
|
130
141
|
type: "string",
|
|
131
|
-
description: "What that session should do. Write it as a self-contained task: the
|
|
132
|
-
"starts from an empty context and cannot see this
|
|
142
|
+
description: "What that session should do. Write it as a self-contained task: the new session " +
|
|
143
|
+
"starts from an empty context and cannot see this session.",
|
|
133
144
|
},
|
|
134
145
|
agentId: {
|
|
135
146
|
type: "string",
|
|
136
|
-
description: "Agent (by id) that runs the
|
|
137
|
-
"
|
|
147
|
+
description: "Agent (by id) that runs the new session (optional, defaults to the agent of this " +
|
|
148
|
+
"session)",
|
|
138
149
|
},
|
|
139
150
|
modelId: {
|
|
140
151
|
type: "string",
|
|
141
|
-
description: "Model id for the
|
|
152
|
+
description: "Model id for the new session (optional, defaults to the usual resolution: session " +
|
|
142
153
|
"> agent definition > global default)",
|
|
143
154
|
},
|
|
144
155
|
label: {
|
|
145
156
|
type: "string",
|
|
146
157
|
description: 'Short readable name for this session (optional, e.g. "run the tests"). It is what ' +
|
|
147
|
-
"
|
|
158
|
+
"read_session_status shows and what I use to refer to it later.",
|
|
148
159
|
},
|
|
149
160
|
},
|
|
150
161
|
required: ["prompt"],
|
|
@@ -162,22 +173,33 @@ function createSessionTool(port) {
|
|
|
162
173
|
// 结构化拒绝:模型能按字段分支,不用去读自由文本(宿主没给 hint 时用 SDK 这一份)
|
|
163
174
|
if (!created.ok)
|
|
164
175
|
return { rejected: created.reason, hint: created.hint ?? REJECT_HINTS[created.reason] };
|
|
165
|
-
return {
|
|
176
|
+
return {
|
|
177
|
+
sessionId: created.sessionId,
|
|
178
|
+
label: created.label,
|
|
179
|
+
hint: `${NO_POLL_HINT} ${LABEL_HINT}`,
|
|
180
|
+
};
|
|
166
181
|
},
|
|
167
182
|
});
|
|
168
183
|
}
|
|
169
|
-
/** `read_session_status({ sessionId })` → `{
|
|
184
|
+
/** `read_session_status({ sessionId })` → `{ found, status, label, lastMessage, hint }` */
|
|
170
185
|
function readSessionStatusTool(port) {
|
|
171
186
|
return new CallbackTool({
|
|
172
187
|
function: {
|
|
173
188
|
name: READ_SESSION_STATUS_TOOL,
|
|
174
189
|
description: [
|
|
175
|
-
"Check a
|
|
176
|
-
"
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"
|
|
180
|
-
"
|
|
190
|
+
"Check a session created earlier: whether it is working or idle right now, and whether it",
|
|
191
|
+
"is still alive (`alive: false` means no agent is behind it any more), or that there is no",
|
|
192
|
+
"such session at all (found: false).",
|
|
193
|
+
"About that session the answer gives exactly two facts: whether it is busy (that one state)",
|
|
194
|
+
"and `lastMessage` — a short excerpt of the last thing it said (cut at 300 characters; null",
|
|
195
|
+
"while it has not said anything yet); the `label` is only the name it was created with.",
|
|
196
|
+
"That excerpt is all I get: the answer is never the full transcript",
|
|
197
|
+
"and never its history, it carries no message list and no message count, and it does not",
|
|
198
|
+
"say what that session is doing — so do not use it to fetch context.",
|
|
199
|
+
"This is a single check, not a waiting loop: which of those states it is in (or that it",
|
|
200
|
+
"is not there any more) is a fact about this moment only, and it does not tell me when",
|
|
201
|
+
"anything will happen. Do not poll —",
|
|
202
|
+
"when that session has something for me it calls post_message on me itself.",
|
|
181
203
|
"If I no longer remember the `sessionId`, the `label` returned by create_session is what",
|
|
182
204
|
"identifies the session — it comes back in the answer, so I can tell them apart.",
|
|
183
205
|
].join(" "),
|
|
@@ -186,8 +208,7 @@ function readSessionStatusTool(port) {
|
|
|
186
208
|
properties: {
|
|
187
209
|
sessionId: {
|
|
188
210
|
type: "string",
|
|
189
|
-
description: "The sessionId returned by create_session (that
|
|
190
|
-
"that session)",
|
|
211
|
+
description: "The sessionId returned by create_session (that is the peer session's own id)",
|
|
191
212
|
},
|
|
192
213
|
},
|
|
193
214
|
required: ["sessionId"],
|
|
@@ -199,31 +220,37 @@ function readSessionStatusTool(port) {
|
|
|
199
220
|
caller: ctx.agent,
|
|
200
221
|
sessionId: typeof input.sessionId === "string" ? input.sessionId : "",
|
|
201
222
|
});
|
|
202
|
-
//
|
|
223
|
+
// 两种形状原样转给模型:找不到就是 `found: false` + `status: null`,不挤成一个状态值
|
|
224
|
+
if (!status.found)
|
|
225
|
+
return { found: false, status: null, hint: NOT_FOUND_HINT };
|
|
226
|
+
// 有这一段就给四样:忙不忙、还活不活着、那个名字、最近一句的摘要(`null` = 还没说过话)。
|
|
227
|
+
// 消息数组 / 条数 / 历史一概不带:需求要的就是「忙不忙 + 最近一句摘要」。
|
|
203
228
|
return {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
229
|
+
found: true,
|
|
230
|
+
status: status.status,
|
|
231
|
+
alive: status.alive,
|
|
232
|
+
label: status.label,
|
|
233
|
+
lastMessage: status.lastMessage,
|
|
234
|
+
hint: statusHint(status.status, status.alive),
|
|
209
235
|
};
|
|
210
236
|
},
|
|
211
237
|
});
|
|
212
238
|
}
|
|
213
|
-
/** `
|
|
214
|
-
function
|
|
239
|
+
/** `post_message({ sessionId, content })` → `{ accepted, hint }` / `{ rejected, hint }` */
|
|
240
|
+
function post_messageTool(port) {
|
|
215
241
|
return new CallbackTool({
|
|
216
242
|
function: {
|
|
217
|
-
name:
|
|
243
|
+
name: POST_MESSAGE_TOOL,
|
|
218
244
|
description: [
|
|
219
|
-
"
|
|
220
|
-
"
|
|
221
|
-
"
|
|
222
|
-
"
|
|
223
|
-
"
|
|
224
|
-
"
|
|
225
|
-
"
|
|
226
|
-
"not poll it
|
|
245
|
+
"Post a message to another session (one created by create_session, or any session I know",
|
|
246
|
+
"the id of) — it is how I talk to that side instead of opening a new one. Whoever it is,",
|
|
247
|
+
"the message is accepted as long as that session exists and has a live agent: posting",
|
|
248
|
+
"does not depend on how the two of us are related.",
|
|
249
|
+
"The call returns whether the message was taken. It does not wait for anything: no answer",
|
|
250
|
+
"comes back here and nothing is delivered to me automatically — when that side has",
|
|
251
|
+
"something to tell me it posts a message to me itself.",
|
|
252
|
+
"Do not poll it: how it acts on the message is its own business, and read_session_status",
|
|
253
|
+
"only tells me which state it is in at that moment.",
|
|
227
254
|
"If I no longer remember the `sessionId`, the `label` returned by create_session is how I",
|
|
228
255
|
"identify that session.",
|
|
229
256
|
].join(" "),
|
|
@@ -232,13 +259,12 @@ function sendToSessionTool(port) {
|
|
|
232
259
|
properties: {
|
|
233
260
|
sessionId: {
|
|
234
261
|
type: "string",
|
|
235
|
-
description: "The sessionId returned by create_session (that
|
|
236
|
-
"that session)",
|
|
262
|
+
description: "The sessionId returned by create_session (that is the peer session's own id)",
|
|
237
263
|
},
|
|
238
264
|
content: {
|
|
239
265
|
type: "string",
|
|
240
|
-
description: "What to say to that
|
|
241
|
-
"
|
|
266
|
+
description: "What to say to that side. Write it as a self-contained instruction: it cannot see " +
|
|
267
|
+
"this session, it only has its own history.",
|
|
242
268
|
},
|
|
243
269
|
},
|
|
244
270
|
required: ["sessionId", "content"],
|
|
@@ -254,7 +280,7 @@ function sendToSessionTool(port) {
|
|
|
254
280
|
// 结构化拒绝:模型能按字段分支,不用去读自由文本
|
|
255
281
|
if (!sent.ok)
|
|
256
282
|
return { rejected: sent.reason, hint: SEND_REJECT_HINTS[sent.reason] };
|
|
257
|
-
return { accepted: true,
|
|
283
|
+
return { accepted: true, hint: SEND_ACCEPTED_HINT };
|
|
258
284
|
},
|
|
259
285
|
});
|
|
260
286
|
}
|