@vessel-agent/agent 0.0.0-stage → 2.3.0-beta.3
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/package.json +30 -4
- package/src/address-display.ts +141 -0
- package/src/conversation-budget.ts +651 -0
- package/src/conversation-store.ts +351 -0
- package/src/distress.ts +161 -0
- package/src/env-info-scope.ts +153 -0
- package/src/error-text.ts +28 -0
- package/src/inbox-source.ts +73 -0
- package/src/inbox.ts +2335 -0
- package/src/index.ts +273 -0
- package/src/member-cwd.ts +128 -0
- package/src/member-ports.ts +162 -0
- package/src/refs.ts +51 -0
- package/src/registry.ts +741 -0
- package/src/runtime.ts +2501 -0
- package/src/self-card-delta.ts +428 -0
- package/src/service.ts +9212 -0
- package/src/snapshot.ts +614 -0
- package/src/team-address.ts +96 -0
- package/src/team-log.ts +487 -0
- package/src/team-service.ts +1966 -0
- package/src/team-slug.ts +167 -0
- package/src/team-store.ts +619 -0
- package/src/tool.ts +1306 -0
- package/src/types/budget.ts +168 -0
- package/src/types/constants.ts +17 -0
- package/src/types/dispatch.ts +133 -0
- package/src/types/events.ts +132 -0
- package/src/types/execution.ts +289 -0
- package/src/types/lifecycle.ts +372 -0
- package/src/types/member.ts +221 -0
- package/src/types/message.ts +113 -0
- package/src/types/results.ts +64 -0
- package/src/types/roster.ts +137 -0
- package/src/types/service.ts +702 -0
- package/src/types/session.ts +102 -0
- package/src/types/team.ts +558 -0
- package/src/types.ts +22 -0
- package/README.md +0 -3
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self Card 增量告知(0.52):基础信息改了,**不重建 body、不热替换 Self Card**,
|
|
3
|
+
* 而是在该成员的下一轮 BeforeLlm 追加一条 `vessel:notice` 环境信息,说清楚
|
|
4
|
+
* 「你 Self Card 里的那一行现在是什么」。
|
|
5
|
+
*
|
|
6
|
+
* 为什么不热替换(用户裁决,2026-02):
|
|
7
|
+
* - Self Card 是 system 前缀的一部分(CONTRACT-prefix-invariant):`selfCard` /
|
|
8
|
+
* `systemPrompt` / `model` 在 `ExecutionUnit` 构造期赋值、之后只读。改它要么破坏只读
|
|
9
|
+
* 契约、要么每轮让整段历史缓存失效;两者都比「多一条尾消息」贵得多。
|
|
10
|
+
* - 各字段的**真实生效点本来就不一样**:改名 / 改 Team / 改 parent 立即生效(花名册、
|
|
11
|
+
* 地址、权限档位),改 model / system prompt 要等下一具 body。把这件事如实说出来,
|
|
12
|
+
* 比假装卡没变有用 —— 模型据此知道该信哪一半。
|
|
13
|
+
*
|
|
14
|
+
* 不新造事件、不新造通道:这条尾消息与 `runtime-notice-hook` / `refs-notice-hook` 是
|
|
15
|
+
* 同一条(`role: 'user'` + `name: 'vessel:notice'`),Web 由 `env-notice.ts` 渲成环境
|
|
16
|
+
* 信息卡。scope 按**变更种类**分(`identity` / `model` / `context` / `tools`,词表见
|
|
17
|
+
* `env-info-scope.ts`),一批变更跨种类时拆成多条消息 —— 一条消息只表达一种种类,否则卡头
|
|
18
|
+
* 标签必然撒谎。不写 system、不新增事件类型、不进 `RunOptions.messages`。
|
|
19
|
+
*
|
|
20
|
+
* 去重口径(与 `refs-notice-hook` 同一族):记住「模型现在相信的值」,注入一次就把它推进
|
|
21
|
+
* 到当前值;下一轮 diff 为空 ⇒ 不再注入。所以**同一变更只报一次**,而回滚(A→B→A)因为
|
|
22
|
+
* 与「相信的值」不同、照样会报 —— 只按 spawn 快照去重的话,回滚会被静默吞掉。
|
|
23
|
+
*
|
|
24
|
+
* 一处已知边界:本模块只覆盖**成员**(`MemberExecutionHost` 的 body)。owner 的
|
|
25
|
+
* Self Card 由 `src/tenant-runtime-factory.ts` 构造,不经这条通道。
|
|
26
|
+
*
|
|
27
|
+
* @module @vessel-agent/agent
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { EnvCadence, EnvChannel, Hook, HookType, Message } from '@vessel-agent/core';
|
|
31
|
+
import {
|
|
32
|
+
ENV_INFO_DETAIL_MARK,
|
|
33
|
+
ENV_INFO_MESSAGE_NAME,
|
|
34
|
+
ENV_INFO_SCOPES,
|
|
35
|
+
type EnvInfoScope,
|
|
36
|
+
formatEnvInfo,
|
|
37
|
+
} from './env-info-scope.js';
|
|
38
|
+
import type { MemberExecutionPermissionMode } from './runtime.js';
|
|
39
|
+
import type { MemberExecutionMember } from './types.js';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `HookType.BeforeLlm` 的字面值(`packages/core/src/types/hook.ts`)。
|
|
43
|
+
*
|
|
44
|
+
* 为什么不用 `HookType.BeforeLlm`:本模块要保持**零运行时依赖**,`src/self-card-delta.test.ts`
|
|
45
|
+
* 才能在任何一份 checkout 里直接 `bun run`(与 `refs-notice-hook.test.ts` 同口径;未装
|
|
46
|
+
* node_modules 时 `@vessel-agent/core` 解析不到)。而 D5(`scripts/check-deps.ts`)禁止
|
|
47
|
+
* 非 core 包相对 import core 内部,所以也不能走相对路径 —— 只剩「值取自字面量、类型由
|
|
48
|
+
* 枚举兜住」这一条。上面的 `as HookType` 让拼错的值在 typecheck 里就报错。
|
|
49
|
+
*/
|
|
50
|
+
const BEFORE_LLM = 'before_llm' as HookType;
|
|
51
|
+
|
|
52
|
+
/** 注册名(同一具 body 的插件宿主上幂等注册用)。 */
|
|
53
|
+
export const SELF_CARD_DELTA_HOOK = 'self-card-delta';
|
|
54
|
+
|
|
55
|
+
/** 注入消息的 `name`:与其它环境信息同一条通道(Web 按它认 `notice`)。 */
|
|
56
|
+
export const SELF_CARD_DELTA_MESSAGE_NAME = ENV_INFO_MESSAGE_NAME;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* details 行的线格式前缀:两个空格 + `·`。词表与线格式的唯一真相是
|
|
60
|
+
* `env-info-scope.ts`(三条环境信息通道共用一份,否则 Web 的解析器会对不上其中一条)。
|
|
61
|
+
* 这里保留别名是为了既有调用点与测试不必改名。
|
|
62
|
+
*/
|
|
63
|
+
export const SELF_CARD_DELTA_DETAIL_MARK = ENV_INFO_DETAIL_MARK;
|
|
64
|
+
|
|
65
|
+
/** 单个值在环境信息里的最大长度(名字 / model / team id 都很短,防脏数据把行撑爆)。 */
|
|
66
|
+
const VALUE_MAX = 60;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Self Card 里会被「基础信息变更」作废的那几行。
|
|
70
|
+
*
|
|
71
|
+
* 不含 `depth` / `limits` / `context`:它们来自进程级配置(宿主选项),没有运行期改动
|
|
72
|
+
* 路径;也不含 `tools`:工具面热刷新是另一条通道(`refreshTools`),不在本模块范围内。
|
|
73
|
+
*/
|
|
74
|
+
export type SelfCardField = 'name' | 'parent' | 'team' | 'model' | 'system_prompt' | 'permission';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 固定顺序:diff 与文案都按这个顺序走。
|
|
78
|
+
*
|
|
79
|
+
* 顺序只影响「一条消息里几行的先后」,但必须**稳定**:同一批变更在任何一轮里都要拼出同一个
|
|
80
|
+
* 字符串,否则去重与测试都会变成看运气。
|
|
81
|
+
*/
|
|
82
|
+
export const SELF_CARD_FIELDS: readonly SelfCardField[] = [
|
|
83
|
+
'name',
|
|
84
|
+
'parent',
|
|
85
|
+
'team',
|
|
86
|
+
'model',
|
|
87
|
+
'system_prompt',
|
|
88
|
+
'permission',
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 一份「Self Card 相关身份」快照。
|
|
93
|
+
*
|
|
94
|
+
* 只收 Self Card 真正渲染出来的行(外加决定 `how to ask for help` 的权限档),因为这条
|
|
95
|
+
* 通道存在的唯一理由就是纠正那张卡;收多了就变成「环境信息里复述一遍成员记录」。
|
|
96
|
+
*/
|
|
97
|
+
export interface SelfCardIdentity {
|
|
98
|
+
/** Self Card 的 `role:` 行装的是**成员名**(不是 `AgentRole`),见 `buildSelfCard`。 */
|
|
99
|
+
readonly name?: string;
|
|
100
|
+
readonly parent_id?: string;
|
|
101
|
+
readonly team_id?: string;
|
|
102
|
+
readonly model?: string;
|
|
103
|
+
/** system prompt 正文只用来判「变没变」,**绝不**写进环境信息(可能上千字)。 */
|
|
104
|
+
readonly system_prompt?: string;
|
|
105
|
+
/** 决定工具面与 `how to ask for help` 的档位(`effectivePermissionMode` 的结果)。 */
|
|
106
|
+
readonly permission_mode?: MemberExecutionPermissionMode;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** diff 的一条:字段 + 旧值 + 新值(`undefined` = 那一行当时不存在 / 未设置)。 */
|
|
110
|
+
export interface SelfCardChange {
|
|
111
|
+
readonly field: SelfCardField;
|
|
112
|
+
readonly from?: string;
|
|
113
|
+
readonly to?: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 空串按「没有」处理:`team_id: ''` 与缺省都意味着 unbound,不能报成一次变更。
|
|
118
|
+
*
|
|
119
|
+
* `system_prompt` 也走这里判空(空串 = 没设置),但保留原文用于比较:首尾空白是用户写
|
|
120
|
+
* 的内容,改了就该报。
|
|
121
|
+
*/
|
|
122
|
+
function normalizeOf(value: string | undefined): string | undefined {
|
|
123
|
+
if (typeof value !== 'string') return undefined;
|
|
124
|
+
const trimmed = value.trim();
|
|
125
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* `MemberExecutionMember`(+ 当前权限档)→ 身份快照。
|
|
130
|
+
*
|
|
131
|
+
* 权限档由调用方给:它是 `effectivePermissionMode(member)` 的结果,只有宿主知道
|
|
132
|
+
* (Team 成员在 `forceBridgeForTeams` 下会被强制成 bridge,见 `buildSurface`)。
|
|
133
|
+
*/
|
|
134
|
+
export function selfCardIdentityOf(
|
|
135
|
+
member: Pick<MemberExecutionMember, 'name' | 'parent_id' | 'team_id' | 'model' | 'system_prompt'>,
|
|
136
|
+
permissionMode?: MemberExecutionPermissionMode,
|
|
137
|
+
): SelfCardIdentity {
|
|
138
|
+
const name = normalizeOf(member.name);
|
|
139
|
+
const parentId = normalizeOf(member.parent_id);
|
|
140
|
+
const teamId = normalizeOf(member.team_id);
|
|
141
|
+
const model = normalizeOf(member.model);
|
|
142
|
+
const systemPrompt = member.system_prompt;
|
|
143
|
+
return {
|
|
144
|
+
...(name !== undefined ? { name } : {}),
|
|
145
|
+
...(parentId !== undefined ? { parent_id: parentId } : {}),
|
|
146
|
+
...(teamId !== undefined ? { team_id: teamId } : {}),
|
|
147
|
+
...(model !== undefined ? { model } : {}),
|
|
148
|
+
...(systemPrompt !== undefined && normalizeOf(systemPrompt) !== undefined
|
|
149
|
+
? { system_prompt: systemPrompt }
|
|
150
|
+
: {}),
|
|
151
|
+
...(permissionMode !== undefined ? { permission_mode: permissionMode } : {}),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 字段 → 快照里的值(唯一映射点:diff 从这里取,不许各写一份)。 */
|
|
156
|
+
function fieldValueOf(identity: SelfCardIdentity, field: SelfCardField): string | undefined {
|
|
157
|
+
switch (field) {
|
|
158
|
+
case 'name':
|
|
159
|
+
return identity.name;
|
|
160
|
+
case 'parent':
|
|
161
|
+
return identity.parent_id;
|
|
162
|
+
case 'team':
|
|
163
|
+
return identity.team_id;
|
|
164
|
+
case 'model':
|
|
165
|
+
return identity.model;
|
|
166
|
+
case 'system_prompt':
|
|
167
|
+
return identity.system_prompt;
|
|
168
|
+
case 'permission':
|
|
169
|
+
return identity.permission_mode;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 「模型相信的值」→「这一轮的真值」,逐字段 diff(纯函数)。
|
|
175
|
+
*
|
|
176
|
+
* 只报**真的不同**的字段:没变更 / 字段没变都返回空数组,调用方据此完全不注入。
|
|
177
|
+
*/
|
|
178
|
+
export function diffSelfCardIdentity(
|
|
179
|
+
reported: SelfCardIdentity,
|
|
180
|
+
current: SelfCardIdentity,
|
|
181
|
+
): SelfCardChange[] {
|
|
182
|
+
const changes: SelfCardChange[] = [];
|
|
183
|
+
for (const field of SELF_CARD_FIELDS) {
|
|
184
|
+
const from = fieldValueOf(reported, field);
|
|
185
|
+
const to = fieldValueOf(current, field);
|
|
186
|
+
if (from === to) continue;
|
|
187
|
+
changes.push({
|
|
188
|
+
field,
|
|
189
|
+
...(from !== undefined ? { from } : {}),
|
|
190
|
+
...(to !== undefined ? { to } : {}),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return changes;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** 字段 → 行内标签(前三个与 Self Card 的行名对齐,模型能直接对上是哪一行)。 */
|
|
197
|
+
const FIELD_LABELS: Readonly<Record<SelfCardField, string>> = {
|
|
198
|
+
name: 'name',
|
|
199
|
+
parent: 'parent',
|
|
200
|
+
team: 'team',
|
|
201
|
+
model: 'model',
|
|
202
|
+
system_prompt: 'system prompt',
|
|
203
|
+
permission: 'permission mode',
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 字段 → 「这件事到底生效到哪一步」。
|
|
208
|
+
*
|
|
209
|
+
* 全部来自实测(报告 0.51 §2.3–§2.5):Self Card 与 EU 的 `model` / `systemPrompt` 都是
|
|
210
|
+
* 构造期快照,改记录只影响**下一具 body**;而改名 / Team / parent 在花名册与地址里立即
|
|
211
|
+
* 生效,只有模型看到的那张卡还停在旧值。写成一句真话,模型才不会拿旧 model 解释新行为。
|
|
212
|
+
*/
|
|
213
|
+
const FIELD_APPLIES: Readonly<Record<SelfCardField, string>> = {
|
|
214
|
+
name: 'rename is already effective, but the `role:` line of your Self Card still shows the old name',
|
|
215
|
+
parent: 'the `parent:` line of your Self Card is out of date',
|
|
216
|
+
team: 'the `team:` line of your Self Card is out of date',
|
|
217
|
+
model: 'this body keeps the old model; the next body uses the new one',
|
|
218
|
+
system_prompt: 'this body keeps the old system prompt; the next body uses the new one',
|
|
219
|
+
permission: 'this body keeps the old tool surface; the next body applies the new mode',
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** 值 → 行内文本:压成一行 + 裁长 + 空值占位(`(none)` 与 Web 卡的读感一致)。 */
|
|
223
|
+
function valueText(value: string | undefined): string {
|
|
224
|
+
if (value === undefined) return '(none)';
|
|
225
|
+
const flat = value.replace(/\s+/g, ' ');
|
|
226
|
+
return flat.length <= VALUE_MAX ? flat : `${flat.slice(0, VALUE_MAX)}…`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** 一条 change → details 行(system prompt 只说「加了 / 去了 / 换了」,不贴正文)。 */
|
|
230
|
+
function changeLineOf(change: SelfCardChange): string {
|
|
231
|
+
const applies = FIELD_APPLIES[change.field];
|
|
232
|
+
if (change.field === 'system_prompt') {
|
|
233
|
+
const verb =
|
|
234
|
+
change.from === undefined ? 'added' : change.to === undefined ? 'removed' : 'replaced';
|
|
235
|
+
return `${FIELD_LABELS.system_prompt}: ${verb} (${applies})`;
|
|
236
|
+
}
|
|
237
|
+
const from = valueText(change.from);
|
|
238
|
+
const to = valueText(change.to);
|
|
239
|
+
return `${FIELD_LABELS[change.field]}: ${from} → ${to} (${applies})`;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 字段 → scope(词表见 `env-info-scope.ts`)。
|
|
244
|
+
*
|
|
245
|
+
* 为什么字段要分 scope 而不是一律 `selfcard`:用户裁定「用 scope 区分种类」——改名、换模型、
|
|
246
|
+
* 换系统提示词对模型的意义完全不同(前者立即生效、后两者要等下一具 body),一种是同一张卡,
|
|
247
|
+
* 另一种是把三件事糊成一句「基础信息变了」。`permission` 归 `tools`:它真正改变的是可用工具面。
|
|
248
|
+
*/
|
|
249
|
+
const FIELD_SCOPE: Readonly<Record<SelfCardField, EnvInfoScope>> = {
|
|
250
|
+
name: 'identity',
|
|
251
|
+
parent: 'identity',
|
|
252
|
+
team: 'identity',
|
|
253
|
+
model: 'model',
|
|
254
|
+
system_prompt: 'context',
|
|
255
|
+
permission: 'tools',
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 本 hook 实际产卡的 scope 值域(**派生自 `FIELD_SCOPE`,无第二份字面量**)。
|
|
260
|
+
*
|
|
261
|
+
* 装配层整 hook 的 `finestCadence` 入参用它(`runtime.ts`):幽灵词 `fields` 不在
|
|
262
|
+
* `FIELD_SCOPE` 值域里(零活生产者),若混进最细计算,用户把四个活词全配 `run`
|
|
263
|
+
* 整 hook 仍被 `fields` 的缺省 `request` 钉死(UX 陷阱,q-single-source-derive ③F4)。
|
|
264
|
+
* `fields` 词本身保留在 `ENV_INFO_SCOPES` / `DEFAULT_ENV_CADENCE` / 显示序表里供
|
|
265
|
+
* 历史落盘消息解析(`peer` retired 先例同口径,读兼容留)。
|
|
266
|
+
*/
|
|
267
|
+
export const SELF_CARD_SCOPES: readonly EnvInfoScope[] = [...new Set(Object.values(FIELD_SCOPE))];
|
|
268
|
+
|
|
269
|
+
/** scope → 摘要(只说「哪一类变了」,具体每条在明细里)。 */
|
|
270
|
+
const SCOPE_SUMMARY: Readonly<Record<string, string>> = {
|
|
271
|
+
identity: 'identity changed; the Self Card in your system prompt is out of date',
|
|
272
|
+
model: 'model changed; this body keeps the old model',
|
|
273
|
+
context: 'system prompt changed; this body keeps the old prompt',
|
|
274
|
+
tools: 'tool surface setting changed; this body keeps the old tool surface',
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 一批变更 → 环境信息正文(**按 scope 分组,一组一条**)。
|
|
279
|
+
*
|
|
280
|
+
* 真实形状(`\n` 是换行;这里演示一批里同时有身份与模型变更 → 两条消息):
|
|
281
|
+
* ```
|
|
282
|
+
* identity: identity changed; the Self Card in your system prompt is out of date
|
|
283
|
+
* · name: human-2 → Minelog-main (rename is already effective, but the `role:` line of your Self Card still shows the old name)
|
|
284
|
+
* ```
|
|
285
|
+
* ```
|
|
286
|
+
* model: model changed; this body keeps the old model
|
|
287
|
+
* · model: volcengine/deepseek-flash → volcengine/deepseek-pro (this body keeps the old model; the next body uses the new one)
|
|
288
|
+
* ```
|
|
289
|
+
* 首行必须带 scope 词:Web 的 `env-notice.ts` 按**行首 token** 认 scope,认不出就落到兜底
|
|
290
|
+
* `context`(内容照样能读,但卡头标签会变成「上下文」)。返回数组而不是单串:一条环境信息
|
|
291
|
+
* 只表达**一种种类**,混在一张卡里=卡头标签必然撒谎。
|
|
292
|
+
*/
|
|
293
|
+
export function formatSelfCardDeltas(changes: readonly SelfCardChange[]): string[] {
|
|
294
|
+
const groups = new Map<EnvInfoScope, string[]>();
|
|
295
|
+
for (const change of changes) {
|
|
296
|
+
const scope = FIELD_SCOPE[change.field];
|
|
297
|
+
const lines = groups.get(scope) ?? [];
|
|
298
|
+
lines.push(changeLineOf(change));
|
|
299
|
+
groups.set(scope, lines);
|
|
300
|
+
}
|
|
301
|
+
const texts: string[] = [];
|
|
302
|
+
// 顺序固定按词表(`ENV_INFO_SCOPES`):同一批变更永远拼出同一个序列,测试与去重才可靠。
|
|
303
|
+
for (const scope of ENV_INFO_SCOPES) {
|
|
304
|
+
const details = groups.get(scope);
|
|
305
|
+
if (details === undefined) continue;
|
|
306
|
+
texts.push(
|
|
307
|
+
formatEnvInfo({
|
|
308
|
+
scope,
|
|
309
|
+
summary: SCOPE_SUMMARY[scope] ?? 'a field the model should know about has changed',
|
|
310
|
+
details,
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
return texts;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface SelfCardDeltaHookOptions {
|
|
318
|
+
/**
|
|
319
|
+
* spawn 那一刻 Self Card 里的值:**模型现在看到的就是它**。
|
|
320
|
+
*
|
|
321
|
+
* 不用 registry 的当前值当起点(那是「事实」,不是「模型相信的东西」),否则改名发生在
|
|
322
|
+
* spawn 之后、注册 hook 之前的那一版会被误判成「已经告过」。
|
|
323
|
+
*/
|
|
324
|
+
baseline: SelfCardIdentity;
|
|
325
|
+
/**
|
|
326
|
+
* 这一轮的真值(每轮 BeforeLlm 现算一次)。
|
|
327
|
+
*
|
|
328
|
+
* 用函数而不是快照:基础信息由 `AgentService` 在 `agents.update` 时推进,权限档还要跟着
|
|
329
|
+
* `effectivePermissionMode`(Team 成员被强制 bridge)走;快照在第二次变更后就过期。
|
|
330
|
+
* 探针抛错按「这一轮不报」处理,绝不让一条提示拖垮一轮 LLM 请求。
|
|
331
|
+
*/
|
|
332
|
+
current: () => SelfCardIdentity;
|
|
333
|
+
/**
|
|
334
|
+
* 检测节奏(注入粒度框架轴 A;缺省 'request' = 今天行为,零回归)。
|
|
335
|
+
*
|
|
336
|
+
* 'run' ⇒ 每 run 只在首次请求观察 / diff(`request_index > 0` 早退)。
|
|
337
|
+
* **整 hook 取最细**:装配层传 `finestCadence(SELF_CARD_SCOPES, …)`(派生 FIELD_SCOPE
|
|
338
|
+
* 值域:identity/model/context/tools 四活词),任一 scope 是 request ⇒ 整 hook request。
|
|
339
|
+
* 不做 per-kind 分裂(避免 per-kind reported 跟踪复杂度):混合配置整 hook 取 request,
|
|
340
|
+
* 要整体推迟须把四个活 scope 全配 run。
|
|
341
|
+
*/
|
|
342
|
+
cadence?: EnvCadence | (() => EnvCadence);
|
|
343
|
+
/**
|
|
344
|
+
* 投递通道 resolver(P2 轴 C):按 scope 判 `ui-only` ⇒ 该 scope 的增量只落 UI 卡事实
|
|
345
|
+
* (ctx.emitFact),不写 ctx.messages。缺省 ⇒ 全 model+ui(零回归)。
|
|
346
|
+
* per-scope `off` 由装配层经 cadence resolver / 本 resolver 同源配置判定(见 run)。
|
|
347
|
+
*/
|
|
348
|
+
channel?: (scope: string) => EnvChannel;
|
|
349
|
+
/**
|
|
350
|
+
* per-scope off resolver(P2 轴 A):返回 true ⇒ 该 scope 整段静默(不产卡)。
|
|
351
|
+
* 缺省 ⇒ 无 off(零回归)。整 hook 的 finestCadence gate 只处理「全 off」;混合配置里
|
|
352
|
+
* 个别 scope off 由本 resolver 在分组后过滤。
|
|
353
|
+
*/
|
|
354
|
+
scopeOff?: (scope: string) => boolean;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 一具 body 的 Self Card 增量告知 hook(spawn 时注册,与 `createInboxNoticeHook` 同位)。
|
|
359
|
+
*
|
|
360
|
+
* 每个 hook 实例 = 一具 body:状态(`reported`)放在闭包里,rotate 换 body 就从新快照重新
|
|
361
|
+
* 开始 —— 新 body 的 Self Card 本来就是重算的,旧 body 的记忆在这里既没用也会误导。
|
|
362
|
+
*/
|
|
363
|
+
export function createSelfCardDeltaHook(options: SelfCardDeltaHookOptions): Hook {
|
|
364
|
+
/** 模型此刻相信的 Self Card 值(初始 = spawn 快照);每注入一次就推进到当前值。 */
|
|
365
|
+
let reported = options.baseline;
|
|
366
|
+
return {
|
|
367
|
+
name: SELF_CARD_DELTA_HOOK,
|
|
368
|
+
type: BEFORE_LLM,
|
|
369
|
+
async run(ctx): Promise<void> {
|
|
370
|
+
// 节奏 gate 在观察 / diff 之前:否则 reported 已推进而卡被吞 = 变更永丢。
|
|
371
|
+
// request_index 缺省 0(旧 caller)⇒ 恒放行,零回归。live resolver 每请求求值(P2)。
|
|
372
|
+
const cadence = typeof options.cadence === 'function' ? options.cadence() : options.cadence;
|
|
373
|
+
if (cadence === 'off') return;
|
|
374
|
+
if (cadence === 'run' && (ctx.request_index ?? 0) > 0) return;
|
|
375
|
+
let current: SelfCardIdentity;
|
|
376
|
+
try {
|
|
377
|
+
current = options.current();
|
|
378
|
+
} catch {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const changes = diffSelfCardIdentity(reported, current);
|
|
382
|
+
if (changes.length === 0) return;
|
|
383
|
+
// 先推进再注入:同一批变更在**同一 run 的后续 LLM 请求**里(tool-calling loop 每轮都
|
|
384
|
+
// 跑一次 BeforeLlm)也会被 diff 挡掉,不需要另存一份「已报签名」。
|
|
385
|
+
reported = current;
|
|
386
|
+
// 一条变更种类 = 一条消息(scope 决定卡头标签,混着写就等于标签撒谎)。
|
|
387
|
+
const timestamp = Date.now();
|
|
388
|
+
const injected: Message[] = [];
|
|
389
|
+
for (const content of formatSelfCardDeltas(changes)) {
|
|
390
|
+
// 首行 scope 词(线格式 `<scope>: …`);解析不出按 model+ui 现状(零回归)。
|
|
391
|
+
const scope = /^([A-Za-z_][A-Za-z0-9_]*)\s*[::]/.exec(
|
|
392
|
+
content.split('\n', 1)[0] ?? '',
|
|
393
|
+
)?.[1];
|
|
394
|
+
if (scope !== undefined && options.scopeOff?.(scope) === true) continue;
|
|
395
|
+
const message: Message = {
|
|
396
|
+
role: 'user',
|
|
397
|
+
name: SELF_CARD_DELTA_MESSAGE_NAME,
|
|
398
|
+
content,
|
|
399
|
+
timestamp,
|
|
400
|
+
};
|
|
401
|
+
// P2 轴 C:ui-only ⇒ 只落 UI 卡事实(message.injected),不写 ctx.messages。
|
|
402
|
+
// 缺省 emitFact(旧 core / 裸宿主)⇒ 回退注入(model+ui 现状,additive)。
|
|
403
|
+
if (
|
|
404
|
+
scope !== undefined &&
|
|
405
|
+
options.channel?.(scope) === 'ui-only' &&
|
|
406
|
+
ctx.emitFact !== undefined
|
|
407
|
+
) {
|
|
408
|
+
ctx.emitFact({
|
|
409
|
+
type: 'message.injected',
|
|
410
|
+
run_id: ctx.run_id,
|
|
411
|
+
data: {
|
|
412
|
+
run_id: ctx.run_id,
|
|
413
|
+
session_id: ctx.session_id,
|
|
414
|
+
message: {
|
|
415
|
+
...message,
|
|
416
|
+
message_id: `${ctx.run_id}:notice:${String(timestamp)}:${scope}`,
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
ts: timestamp,
|
|
420
|
+
});
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
injected.push(message);
|
|
424
|
+
}
|
|
425
|
+
if (injected.length > 0) ctx.messages = [...(ctx.messages ?? []), ...injected];
|
|
426
|
+
},
|
|
427
|
+
};
|
|
428
|
+
}
|