openfeel 1.5.25 → 1.6.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/README.en.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/commands/dashboard.d.ts.map +1 -1
- package/dist/commands/dashboard.js +10 -1
- package/dist/commands/dashboard.js.map +1 -1
- package/dist/commands/flow.d.ts.map +1 -1
- package/dist/commands/flow.js +126 -25
- package/dist/commands/flow.js.map +1 -1
- package/dist/commands/lint.d.ts +5 -1
- package/dist/commands/lint.d.ts.map +1 -1
- package/dist/commands/lint.js +373 -0
- package/dist/commands/lint.js.map +1 -1
- package/dist/commands/req.d.ts.map +1 -1
- package/dist/commands/req.js +814 -88
- package/dist/commands/req.js.map +1 -1
- package/dist/commands/stage.d.ts.map +1 -1
- package/dist/commands/stage.js +19 -0
- package/dist/commands/stage.js.map +1 -1
- package/dist/core/adherence.d.ts +135 -0
- package/dist/core/adherence.d.ts.map +1 -0
- package/dist/core/adherence.js +221 -0
- package/dist/core/adherence.js.map +1 -0
- package/dist/core/dashboard/aggregate.d.ts +39 -0
- package/dist/core/dashboard/aggregate.d.ts.map +1 -1
- package/dist/core/dashboard/aggregate.js +64 -5
- package/dist/core/dashboard/aggregate.js.map +1 -1
- package/dist/core/dashboard/render.d.ts.map +1 -1
- package/dist/core/dashboard/render.js +88 -1
- package/dist/core/dashboard/render.js.map +1 -1
- package/dist/core/dev-status.d.ts +25 -0
- package/dist/core/dev-status.d.ts.map +1 -1
- package/dist/core/dev-status.js +35 -0
- package/dist/core/dev-status.js.map +1 -1
- package/dist/core/errors.d.ts +38 -3
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +22 -3
- package/dist/core/errors.js.map +1 -1
- package/dist/core/flow/gate-override.d.ts +72 -0
- package/dist/core/flow/gate-override.d.ts.map +1 -0
- package/dist/core/flow/gate-override.js +71 -0
- package/dist/core/flow/gate-override.js.map +1 -0
- package/dist/core/flow-manager.d.ts +118 -3
- package/dist/core/flow-manager.d.ts.map +1 -1
- package/dist/core/flow-manager.js +357 -14
- package/dist/core/flow-manager.js.map +1 -1
- package/dist/core/i18n-data/en.d.ts.map +1 -1
- package/dist/core/i18n-data/en.js +96 -5
- package/dist/core/i18n-data/en.js.map +1 -1
- package/dist/core/i18n-data/zh-CN.d.ts.map +1 -1
- package/dist/core/i18n-data/zh-CN.js +96 -5
- package/dist/core/i18n-data/zh-CN.js.map +1 -1
- package/dist/core/lint-req.d.ts +17 -1
- package/dist/core/lint-req.d.ts.map +1 -1
- package/dist/core/lint-req.js +14 -2
- package/dist/core/lint-req.js.map +1 -1
- package/dist/core/plan/body-lint.d.ts +58 -0
- package/dist/core/plan/body-lint.d.ts.map +1 -0
- package/dist/core/plan/body-lint.js +197 -0
- package/dist/core/plan/body-lint.js.map +1 -0
- package/dist/core/plan/filled.d.ts +143 -0
- package/dist/core/plan/filled.d.ts.map +1 -0
- package/dist/core/plan/filled.js +244 -0
- package/dist/core/plan/filled.js.map +1 -0
- package/dist/core/plan/path.d.ts +24 -0
- package/dist/core/plan/path.d.ts.map +1 -1
- package/dist/core/plan/path.js +56 -0
- package/dist/core/plan/path.js.map +1 -1
- package/dist/core/req/aggregate.d.ts.map +1 -1
- package/dist/core/req/aggregate.js +5 -0
- package/dist/core/req/aggregate.js.map +1 -1
- package/dist/core/req/body-file.d.ts +103 -0
- package/dist/core/req/body-file.d.ts.map +1 -1
- package/dist/core/req/body-file.js +221 -2
- package/dist/core/req/body-file.js.map +1 -1
- package/dist/core/req/plan-file.d.ts +83 -0
- package/dist/core/req/plan-file.d.ts.map +1 -0
- package/dist/core/req/plan-file.js +205 -0
- package/dist/core/req/plan-file.js.map +1 -0
- package/dist/core/req/requirement-ops.d.ts +151 -15
- package/dist/core/req/requirement-ops.d.ts.map +1 -1
- package/dist/core/req/requirement-ops.js +402 -22
- package/dist/core/req/requirement-ops.js.map +1 -1
- package/dist/core/req/types.d.ts +63 -2
- package/dist/core/req/types.d.ts.map +1 -1
- package/dist/core/req/types.js +10 -2
- package/dist/core/req/types.js.map +1 -1
- package/dist/core/template-loader.d.ts.map +1 -1
- package/dist/core/template-loader.js +87 -0
- package/dist/core/template-loader.js.map +1 -1
- package/dist/core/templates.d.ts +1 -1
- package/dist/core/templates.d.ts.map +1 -1
- package/dist/core/templates.js +18 -0
- package/dist/core/templates.js.map +1 -1
- package/dsh/ACTION-GUIDE.md +50 -8
- package/dsh/ADAPTER.md +119 -13
- package/dsh/README.md +49 -26
- package/dsh/docs/compat/persistence.md +26 -0
- package/dsh/docs/compat/pipeline.md +24 -0
- package/dsh/docs/compat/req.md +29 -0
- package/dsh/docs/compat/stage.md +25 -0
- package/dsh/lib/adapters/dispatch-ledger.js +432 -0
- package/dsh/lib/adapters/dispatch.js +54 -6
- package/dsh/lib/adapters/next-action.js +336 -0
- package/dsh/lib/guard.js +694 -24
- package/dsh/lib/index.js +37 -0
- package/dsh/lib/surface/prompt.js +66 -178
- package/dsh/lib/tools/index.js +527 -12
- package/dsh/package.json +2 -2
- package/package.json +2 -1
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 动作型 next + 每步提醒(v1.6.0 / T10,REQ-060)
|
|
3
|
+
*
|
|
4
|
+
* 两个职责,都是**纯投影**(不做 I/O、不跑 CLI):
|
|
5
|
+
*
|
|
6
|
+
* 1. `deriveNextAction(snapshot)`:把「下一步该 who do what」变成可执行动作
|
|
7
|
+
* (`dispatch` / `command` / `idle`),由 `feel_status` / `feel_next` 返回。
|
|
8
|
+
* 旧 `next` 字符串/对象保留(不破坏既有断言),内容改成动作型。
|
|
9
|
+
* 2. 运行时提醒:`rememberFocus()` 由工具层在读到快照时喂入,
|
|
10
|
+
* `noteToolCall()` 由 `dsh/lib/index.js` 的 `tools/post-execute` 观察者喂入,
|
|
11
|
+
* `reminderText()` 供 `ctx.systemPrompt.context()` 每请求取值。
|
|
12
|
+
*
|
|
13
|
+
* 为什么提醒走 `systemPrompt.context` 而不是 `additionalContexts`:
|
|
14
|
+
* `tools/post-execute` + `additionalContexts` 在宿主侧确实存在(`dsh-tools/src/events.ts`、
|
|
15
|
+
* `contracts.ts` 的 `PostToolDecision.additionalContexts`),但 `additionalContexts`
|
|
16
|
+
* 的元素必须是 `UserMessage`,只能由 `@deepseek-ai/dsh-llm` 的 `createUserMessage()`
|
|
17
|
+
* 构造;本适配器零运行时依赖(`dsh/package.json` 无 dependencies),实测
|
|
18
|
+
* `createRequire(...).resolve('@deepseek-ai/dsh-llm')` → `MODULE_NOT_FOUND`。
|
|
19
|
+
* 手搓一个 `source.kind` 未注册的 UserMessage 会流进 `agent/inbox/spliced` 持久事件,
|
|
20
|
+
* 无运行时证据前不能赌它不破坏会话。故按 plan §5.3 降级 1:`systemPrompt.context`
|
|
21
|
+
* (动态 user 快照、逐请求刷新),post-execute 只做**观察**(不构造消息、不改结果)。
|
|
22
|
+
* 证据见 `.openfeel/tmp/stage-132/spike.md`。
|
|
23
|
+
*
|
|
24
|
+
* @module openfeel-dsh/adapters/next-action
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { buildDispatchTemplate } from './dispatch.js';
|
|
28
|
+
|
|
29
|
+
/** 提醒上下文名(`ctx.systemPrompt.context` 的唯一标识;重名会抛错)。 */
|
|
30
|
+
export const REMINDER_CONTEXT_NAME = 'openfeel-feel-next';
|
|
31
|
+
|
|
32
|
+
/** 每 N 步至少提醒一次(plan §5.3:写类工具之后 + 每 5 步)。 */
|
|
33
|
+
export const REMINDER_EVERY_STEPS = 5;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 当前阶段是否「开启」(phase 非空且非 done)。
|
|
37
|
+
* @param {unknown} phase
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
function isOpenPhase(phase) {
|
|
41
|
+
return typeof phase === 'string' && phase !== '' && phase !== 'done';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 取当前阶段关联需求。
|
|
46
|
+
*
|
|
47
|
+
* 与 `dispatch.js` 同源口径(优先 CLI 交接标记,回退阶段条目的 `requirementId`),
|
|
48
|
+
* 但这里不 import `requirementOf`(它未导出),保持两份实现的字段读取一致即可。
|
|
49
|
+
* @param {Record<string, any>} snapshot
|
|
50
|
+
* @returns {string}
|
|
51
|
+
*/
|
|
52
|
+
function requirementOf(snapshot) {
|
|
53
|
+
const fromRecovery = snapshot?.recovery?.requirement;
|
|
54
|
+
if (typeof fromRecovery === 'string' && fromRecovery !== '') return fromRecovery;
|
|
55
|
+
const stageId = snapshot?.focus?.stage;
|
|
56
|
+
const stages = Array.isArray(snapshot?.stages) ? snapshot.stages : [];
|
|
57
|
+
const stage = stages.find((entry) => entry?.id === stageId || entry?.name === stageId);
|
|
58
|
+
return typeof stage?.requirementId === 'string' ? stage.requirementId : '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 相位 → 默认负责角色(只用于「本相位缺哪个角色」的判定,不替代 CLI 权威)。
|
|
63
|
+
* @param {string} phase
|
|
64
|
+
* @param {string} responsibleAgent
|
|
65
|
+
* @returns {string} planner | executor | reviewer | tester | archiver | ''
|
|
66
|
+
*/
|
|
67
|
+
function roleForPhase(phase, responsibleAgent) {
|
|
68
|
+
const byPhase =
|
|
69
|
+
phase.startsWith('plan_') ? 'planner'
|
|
70
|
+
: phase.startsWith('scheme_') ? 'planner'
|
|
71
|
+
: phase === 'exec_running' ? 'executor'
|
|
72
|
+
: phase.startsWith('review_') ? 'reviewer'
|
|
73
|
+
: phase.startsWith('test_') ? 'tester'
|
|
74
|
+
: phase === 'archiving' ? 'archiver'
|
|
75
|
+
: '';
|
|
76
|
+
if (byPhase !== '') return byPhase;
|
|
77
|
+
// CLI 的 responsibleAgent 形如 `openfeel-executor` / `feel-tester`,截取末段对齐角色闭集
|
|
78
|
+
const tail = typeof responsibleAgent === 'string' ? responsibleAgent.split('-').pop() : '';
|
|
79
|
+
return ['planner', 'schemer', 'executor', 'reviewer', 'tester', 'archiver'].includes(tail ?? '')
|
|
80
|
+
? (tail === 'schemer' ? 'planner' : tail)
|
|
81
|
+
: '';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 构造一条 `command` 动作。
|
|
86
|
+
* @param {string} tool
|
|
87
|
+
* @param {Record<string, any>} args
|
|
88
|
+
* @param {string} goal
|
|
89
|
+
* @param {string} reason
|
|
90
|
+
* @returns {{ kind: 'command', tool: string, args: Record<string, any>, goal: string, prompt: string, reason: string }}
|
|
91
|
+
*/
|
|
92
|
+
function command(tool, args, goal, reason) {
|
|
93
|
+
return {
|
|
94
|
+
kind: 'command',
|
|
95
|
+
tool,
|
|
96
|
+
args,
|
|
97
|
+
goal,
|
|
98
|
+
prompt: `调用 \`${tool}\`:${JSON.stringify(args)}。为什么:${reason}`,
|
|
99
|
+
reason,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 构造一条 `idle` 动作(无可执行步骤)。
|
|
105
|
+
* @param {string} reason
|
|
106
|
+
* @returns {{ kind: 'idle', tool: null, args: {}, goal: string, prompt: string, reason: string }}
|
|
107
|
+
*/
|
|
108
|
+
function idle(reason) {
|
|
109
|
+
return { kind: 'idle', tool: null, args: {}, goal: '', prompt: '', reason };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 由快照派生「下一步该做什么」——三态:`dispatch` / `command` / `idle`。
|
|
114
|
+
*
|
|
115
|
+
* 判定顺序(先阻塞项,后推进项):
|
|
116
|
+
* 1. 无开启阶段 → `command`(开阶段;门禁自检失败时先修门禁);
|
|
117
|
+
* 2. 本相位派发台账缺当前角色 / 尚无记录 → `dispatch`(`subagent` + `feel_dispatch` 记账);
|
|
118
|
+
* 3. `exec_running` 且已派发 → `idle`(阶段是版本容器,不催推进;等子 agent 回报或继续派发下一任务);
|
|
119
|
+
* 4. 其它相位有合法下一相位 → `command`(`feel_flow action=advance`);
|
|
120
|
+
* 5. 兜底 → `idle`。
|
|
121
|
+
*
|
|
122
|
+
* @param {Record<string, any>} snapshot `readSnapshot()` 结果(缺失字段按空处理)
|
|
123
|
+
* @returns {{ kind: 'dispatch'|'command'|'idle', tool: string|null, args: Record<string, any>, goal: string, prompt: string, reason: string }}
|
|
124
|
+
*/
|
|
125
|
+
export function deriveNextAction(snapshot) {
|
|
126
|
+
const focus = snapshot?.focus ?? {};
|
|
127
|
+
const stage = typeof focus.stage === 'string' ? focus.stage : '';
|
|
128
|
+
const phase = typeof focus.phase === 'string' ? focus.phase : '';
|
|
129
|
+
const phaseLabel = typeof focus.phaseLabel === 'string' && focus.phaseLabel !== '' ? focus.phaseLabel : phase;
|
|
130
|
+
const agent = typeof focus.responsibleAgent === 'string' && focus.responsibleAgent !== ''
|
|
131
|
+
? focus.responsibleAgent
|
|
132
|
+
: 'openfeel-executor';
|
|
133
|
+
const requirement = requirementOf(snapshot);
|
|
134
|
+
const legal = Array.isArray(focus.legalNextPhases) ? focus.legalNextPhases : [];
|
|
135
|
+
const suggested = typeof focus.suggestedCommand === 'string' && focus.suggestedCommand !== ''
|
|
136
|
+
? focus.suggestedCommand
|
|
137
|
+
: '';
|
|
138
|
+
|
|
139
|
+
/* 1) 没有开启阶段:先开阶段(门禁自检失败时先修门禁,别急着建阶段)。 */
|
|
140
|
+
if (!isOpenPhase(phase)) {
|
|
141
|
+
const gate = snapshot?.workflowGate;
|
|
142
|
+
if (gate !== null && gate !== undefined && typeof gate === 'object' && gate.required === true
|
|
143
|
+
&& typeof gate.reason === 'string' && gate.reason.startsWith('guard-')) {
|
|
144
|
+
return command(
|
|
145
|
+
'feel_status',
|
|
146
|
+
{ action: 'status' },
|
|
147
|
+
'核对写入门禁状态',
|
|
148
|
+
`写入门禁不可信(${gate.reason}):先按 guard 段的修复动作处理,再安排阶段。`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return command(
|
|
152
|
+
'feel_stage',
|
|
153
|
+
{ action: 'add', workflow_mode: 'full', exec_mode: 'auto' },
|
|
154
|
+
'按当前小版本开一个阶段(保持 exec_running)',
|
|
155
|
+
'没有开启阶段(phase 为空或 done):先开阶段,再在版本下建需求;阶段是版本容器,完成需求不推进阶段。',
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* 2) 本相位缺派发记录:必须先派子 agent(这是「调度者不是执行者」的唯一检查点)。 */
|
|
160
|
+
const ledger = snapshot?.dispatch;
|
|
161
|
+
const sincePhase = Number.isFinite(ledger?.sincePhase) ? ledger.sincePhase : 0;
|
|
162
|
+
const missingRoles = Array.isArray(ledger?.missingRoles) ? ledger.missingRoles : [];
|
|
163
|
+
const role = roleForPhase(phase, agent);
|
|
164
|
+
const roleMissing = role !== '' && missingRoles.includes(role);
|
|
165
|
+
if (sincePhase === 0 || roleMissing) {
|
|
166
|
+
const template = buildDispatchTemplate(snapshot);
|
|
167
|
+
const goal = goalFor({ stage, phase, phaseLabel, agent, requirement });
|
|
168
|
+
const dispatchReason = sincePhase === 0
|
|
169
|
+
? `phase=${phase} 尚无派发记录:任何执行都必须先派子 agent(feel_dispatch 记账)。`
|
|
170
|
+
: `phase=${phase} 缺 ${role} 角色的派发记录:先派 ${role},再动手。`;
|
|
171
|
+
return {
|
|
172
|
+
kind: 'dispatch',
|
|
173
|
+
tool: 'subagent',
|
|
174
|
+
args: {
|
|
175
|
+
goal,
|
|
176
|
+
context: template.context,
|
|
177
|
+
boundaries: template.boundaries,
|
|
178
|
+
returnFormat: template.returnFormat,
|
|
179
|
+
},
|
|
180
|
+
goal,
|
|
181
|
+
prompt: templatePrompt(goal, template),
|
|
182
|
+
reason: dispatchReason,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/* 3) exec_running 已派发:阶段长期保持,不催推进(等回报或继续派发下一个任务)。 */
|
|
187
|
+
if (phase === 'exec_running') {
|
|
188
|
+
return idle(
|
|
189
|
+
`${stage} 已在 exec_running 且本相位已派发 ${sincePhase} 条:继续派发下一个任务,或等子 agent 回报;不要为了「完成需求」推进阶段。`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* 4) 其它相位:有合法下一相位就推进(命令动作)。 */
|
|
194
|
+
if (legal.length > 0) {
|
|
195
|
+
const to = legal[0];
|
|
196
|
+
return command(
|
|
197
|
+
'feel_flow',
|
|
198
|
+
{ action: 'advance', stage, to },
|
|
199
|
+
`把 ${stage} 从 ${phase} 推进到 ${to}`,
|
|
200
|
+
suggested !== '' ? suggested : `phase=${phase} 的权威合法下一相位是 ${to}。`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* 5) 兜底:读一次状态,别凭记忆。 */
|
|
205
|
+
return idle(`${stage}/${phase} 已派发 ${sincePhase} 条且没有合法下一相位:用 feel_status 复核,不要凭记忆推进。`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 派发目标的一句话(可验收:点名阶段/相位/角色/需求)。
|
|
210
|
+
* @param {{ stage: string, phase: string, phaseLabel: string, agent: string, requirement: string }} input
|
|
211
|
+
* @returns {string}
|
|
212
|
+
*/
|
|
213
|
+
function goalFor({ stage, phase, phaseLabel, agent, requirement }) {
|
|
214
|
+
const who = requirement === '' ? '本相位产物' : `需求 ${requirement} 的本相位产物`;
|
|
215
|
+
return `在 ${stage} 的 ${phase}(${phaseLabel})由 ${agent} 交付${who};完成判据须可验收(给出命令与退出码)。`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 四段式派发 prompt(可直接粘贴进 `subagent`)。
|
|
220
|
+
* @param {string} goal
|
|
221
|
+
* @param {ReturnType<typeof buildDispatchTemplate>} template
|
|
222
|
+
* @returns {string}
|
|
223
|
+
*/
|
|
224
|
+
function templatePrompt(goal, template) {
|
|
225
|
+
return [
|
|
226
|
+
`goal:${goal}`,
|
|
227
|
+
`context:${template.context.join(';')}`,
|
|
228
|
+
`boundaries:写 ${JSON.stringify(template.boundaries.write)};禁读 ${template.boundaries.neverRead.join('/')};禁写 ${template.boundaries.neverWrite.join('/')}`,
|
|
229
|
+
`returnFormat:${template.returnFormat}`,
|
|
230
|
+
].join('\n');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 把动作对象转成工具 wire 的 `next`(`idle` → null,与既有「无建议」语义一致)。
|
|
235
|
+
* @param {ReturnType<typeof deriveNextAction> | null | undefined} action
|
|
236
|
+
* @returns {{ tool: string, arguments: Record<string, any>, why: string } | null}
|
|
237
|
+
*/
|
|
238
|
+
export function toWireNext(action) {
|
|
239
|
+
if (action === null || action === undefined || action.kind === 'idle' || typeof action.tool !== 'string') {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
// dispatch 的完整 prompt 在 `data.nextAction.prompt` 里;wire 只给可扫读的一句话目标,
|
|
243
|
+
// 避免 `renderEnvelope` 把整段 context/boundaries 拼进「下一步建议」。
|
|
244
|
+
const args = action.kind === 'dispatch'
|
|
245
|
+
? { goal: action.goal }
|
|
246
|
+
: (action.args ?? {});
|
|
247
|
+
return { tool: action.tool, arguments: args, why: action.reason };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 动作型一句话(`recovery.nextAction` 字符串;保留旧键、内容换成动作)。
|
|
252
|
+
* @param {ReturnType<typeof deriveNextAction> | null | undefined} action
|
|
253
|
+
* @returns {string | null}
|
|
254
|
+
*/
|
|
255
|
+
export function nextActionLine(action) {
|
|
256
|
+
if (action === null || action === undefined || action.kind === 'idle') return null;
|
|
257
|
+
if (action.kind === 'dispatch') return `dispatch → subagent(goal:${action.goal})`;
|
|
258
|
+
return `command → ${action.tool} ${JSON.stringify(action.args ?? {})}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* ─────────────── 运行时提醒状态(进程内,逐请求取值) ─────────────── */
|
|
262
|
+
|
|
263
|
+
/** @type {{ stage: string, phase: string, line: string }} */
|
|
264
|
+
let focusMemo = { stage: '', phase: '', line: '' };
|
|
265
|
+
/** 已观察到多少次工具调用(每 N 步兜底提醒一次)。 */
|
|
266
|
+
let observedSteps = 0;
|
|
267
|
+
/** 本轮是否需要注入提醒(写类工具之后 / 每 N 步)。 */
|
|
268
|
+
let pending = false;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 记住最近一次快照派生的动作(工具层在 `feel_status` / `feel_next` 成功后调用)。
|
|
272
|
+
* @param {Record<string, any>} snapshot
|
|
273
|
+
* @param {ReturnType<typeof deriveNextAction>} [action] 已派生的动作(省一次重算)
|
|
274
|
+
* @returns {void}
|
|
275
|
+
*/
|
|
276
|
+
export function rememberFocus(snapshot, action) {
|
|
277
|
+
try {
|
|
278
|
+
const derived = action ?? deriveNextAction(snapshot);
|
|
279
|
+
const stage = typeof snapshot?.focus?.stage === 'string' ? snapshot.focus.stage : '';
|
|
280
|
+
const phase = typeof snapshot?.focus?.phase === 'string' ? snapshot.focus.phase : '';
|
|
281
|
+
focusMemo = { stage, phase, line: reminderLineOf(stage, phase, derived) };
|
|
282
|
+
} catch {
|
|
283
|
+
/* 提醒是提示级:绝不影响工具结果 */
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 组装提醒行:`阶段 <stage> / <phase>|下一步:subagent(goal=…)`。
|
|
289
|
+
* @param {string} stage
|
|
290
|
+
* @param {string} phase
|
|
291
|
+
* @param {ReturnType<typeof deriveNextAction>} action
|
|
292
|
+
* @returns {string}
|
|
293
|
+
*/
|
|
294
|
+
function reminderLineOf(stage, phase, action) {
|
|
295
|
+
const where = `阶段 ${stage || '(无)'} / ${phase || '(无相位)'}`;
|
|
296
|
+
if (action?.kind === 'dispatch') return `${where}|下一步:subagent(goal=${action.goal})`;
|
|
297
|
+
if (action?.kind === 'command') return `${where}|下一步:${action.tool}(${JSON.stringify(action.args ?? {})})`;
|
|
298
|
+
if (action?.kind === 'idle') return `${where}|下一步:无(等回报或继续派发)`;
|
|
299
|
+
return `${where}|下一步:先 feel_status 拿阶段/相位`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 观察一次工具调用(`tools/post-execute` 里调用,**只记状态**)。
|
|
304
|
+
*
|
|
305
|
+
* 触发提醒:写类工具之后(`mutatingTools` 命中)或每 `REMINDER_EVERY_STEPS` 步。
|
|
306
|
+
* @param {string} toolName
|
|
307
|
+
* @param {Iterable<string>} [mutatingTools] 写类工具名(来自 `config.mutatingTools`)
|
|
308
|
+
* @returns {void}
|
|
309
|
+
*/
|
|
310
|
+
export function noteToolCall(toolName, mutatingTools) {
|
|
311
|
+
observedSteps += 1;
|
|
312
|
+
const writes = mutatingTools === undefined
|
|
313
|
+
? new Set(['edit', 'write', 'apply_patch', 'bash', 'pwsh'])
|
|
314
|
+
: new Set(mutatingTools);
|
|
315
|
+
pending = writes.has(toolName) || observedSteps % REMINDER_EVERY_STEPS === 0;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 取本请求要注入的提醒文本(空串 = 不注入,零 token 开销)。
|
|
320
|
+
* @returns {string}
|
|
321
|
+
*/
|
|
322
|
+
export function reminderText() {
|
|
323
|
+
if (!pending) return '';
|
|
324
|
+
if (focusMemo.line === '') return '阶段(未读) / (未读相位)|下一步:先 feel_status 拿阶段/相位';
|
|
325
|
+
return focusMemo.line;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 复位提醒状态(仅测试用)。
|
|
330
|
+
* @returns {void}
|
|
331
|
+
*/
|
|
332
|
+
export function resetReminderState() {
|
|
333
|
+
focusMemo = { stage: '', phase: '', line: '' };
|
|
334
|
+
observedSteps = 0;
|
|
335
|
+
pending = false;
|
|
336
|
+
}
|