ralphflow-dsh 0.1.0
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/LICENSE +21 -0
- package/README.md +196 -0
- package/lib/actions.d.ts +37 -0
- package/lib/actions.js +55 -0
- package/lib/check-voting.d.ts +54 -0
- package/lib/check-voting.js +289 -0
- package/lib/check.d.ts +64 -0
- package/lib/check.js +329 -0
- package/lib/client/CommandCard.d.ts +24 -0
- package/lib/client/CommandCard.js +69 -0
- package/lib/client/HeaderAction.d.ts +39 -0
- package/lib/client/HeaderAction.js +509 -0
- package/lib/client/RunCard.d.ts +27 -0
- package/lib/client/RunCard.js +375 -0
- package/lib/client/client.d.ts +3 -0
- package/lib/client/client.js +275 -0
- package/lib/client/definition.d.ts +126 -0
- package/lib/client/definition.js +259 -0
- package/lib/client/hidden-runs.d.ts +6 -0
- package/lib/client/hidden-runs.js +52 -0
- package/lib/client/http-state.d.ts +67 -0
- package/lib/client/http-state.js +81 -0
- package/lib/client/markdown.d.ts +26 -0
- package/lib/client/markdown.js +174 -0
- package/lib/client/notify-watch.d.ts +38 -0
- package/lib/client/notify-watch.js +98 -0
- package/lib/client/notify.d.ts +21 -0
- package/lib/client/notify.js +168 -0
- package/lib/client/snapshots.d.ts +8 -0
- package/lib/client/snapshots.js +101 -0
- package/lib/client.js +2361 -0
- package/lib/commands.d.ts +41 -0
- package/lib/commands.js +223 -0
- package/lib/create.d.ts +14 -0
- package/lib/create.js +98 -0
- package/lib/deps.d.ts +17 -0
- package/lib/deps.js +19 -0
- package/lib/done-detect.d.ts +2 -0
- package/lib/done-detect.js +28 -0
- package/lib/driver.d.ts +32 -0
- package/lib/driver.js +248 -0
- package/lib/engine.d.ts +306 -0
- package/lib/engine.js +2851 -0
- package/lib/events.d.ts +35 -0
- package/lib/events.js +52 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +201 -0
- package/lib/jobs.d.ts +56 -0
- package/lib/jobs.js +355 -0
- package/lib/mutex.d.ts +9 -0
- package/lib/mutex.js +21 -0
- package/lib/session-hygiene.d.ts +25 -0
- package/lib/session-hygiene.js +205 -0
- package/lib/tools.d.ts +24 -0
- package/lib/tools.js +716 -0
- package/lib/voting-progress.d.ts +37 -0
- package/lib/voting-progress.js +89 -0
- package/package.json +73 -0
- package/workflows/loop.yaml +27 -0
- package/workflows/spec.yaml +105 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ralph Flow for dsh — CLIENT 端入口
|
|
3
|
+
*
|
|
4
|
+
* 注册:conversationEvents 折叠器(kind 'ralphflow-run')+ conversation.chat.node
|
|
5
|
+
* keyed 渲染器 + conversation.session.header.actions 页头入口 + locale 词典。
|
|
6
|
+
* 全程只加不换,不碰外壳/侧栏/输入区。
|
|
7
|
+
*/
|
|
8
|
+
import { RALPHFLOW_RUN_KIND, RALPHFLOW_COMMAND_KIND, createDefinition, createCommandDefinition } from "./definition.js";
|
|
9
|
+
import { RalphRunCard } from "./RunCard.js";
|
|
10
|
+
import { RalphCommandCard } from "./CommandCard.js";
|
|
11
|
+
import { HeaderAction } from "./HeaderAction.js";
|
|
12
|
+
import { primeNotifications } from "./notify.js";
|
|
13
|
+
import { startHttpPolling } from "./http-state.js";
|
|
14
|
+
import { startNotifyWatch } from "./notify-watch.js";
|
|
15
|
+
export const name = "ralphflow-client";
|
|
16
|
+
export const inject = ["conversationEvents", "slots", "sessions", "locale"];
|
|
17
|
+
const NS = "ralphflow";
|
|
18
|
+
const zh = {
|
|
19
|
+
"run.running": "进行中",
|
|
20
|
+
"run.check": "验证中",
|
|
21
|
+
"run.gate": "待审查",
|
|
22
|
+
"run.done": "已完成",
|
|
23
|
+
"run.failed": "已失败",
|
|
24
|
+
"run.cancelled": "已取消",
|
|
25
|
+
"run.paused": "⏸ 已暂停",
|
|
26
|
+
"run.paused.hint": "工作流已暂停(验证未通过或达到失败上限)——这不是终点:修复问题后即可继续,已完成的工作全部保留。",
|
|
27
|
+
"run.paused.hint.checkfailed": "这一步的验证没有通过,工作流停在这里等你。已完成的工作全部保留:点「继续」让模型带着失败原因重做,或用 /ralphflow-rewind 回退到更早的步骤。",
|
|
28
|
+
"run.resume": "继续",
|
|
29
|
+
"run.approve": "通过",
|
|
30
|
+
"run.return": "打回",
|
|
31
|
+
"run.return.opinion": "打回意见(会随命令发给模型)",
|
|
32
|
+
"run.return.placeholder": "例如:边界情况没覆盖,测试全挂了,请先修 CI 再交付…",
|
|
33
|
+
"run.return.send": "发送打回",
|
|
34
|
+
"run.cancel.action": "取消",
|
|
35
|
+
"run.chip.tests": "测试没过,请先修复",
|
|
36
|
+
"run.chip.scope": "偏离需求范围,请对照原始任务",
|
|
37
|
+
"run.chip.quality": "质量不达标,请自查后重新提交",
|
|
38
|
+
"run.hint.approve": "已把 /ralphflow-continue 填入输入框并尝试发送——若未发出,按回车即批准进入验证。",
|
|
39
|
+
"run.hint.return": "已在输入框填入 /ralphflow-reset——在后面补上你的修改意见再回车,模型会据此返工(不会自动发送)。",
|
|
40
|
+
"run.hint.returned": "打回意见已发送,模型会按你的意见返工本步骤。",
|
|
41
|
+
"run.gate.requirement": "本步要求(审批材料)",
|
|
42
|
+
"run.verifier": "验证者 {v}/{count}",
|
|
43
|
+
"run.voter.queued": "排队中",
|
|
44
|
+
"run.voter.running": "运行中",
|
|
45
|
+
"run.voter.retrying": "重试中",
|
|
46
|
+
"run.votes": "独立验证 · 已收 {done}/{total} 票",
|
|
47
|
+
"run.timeout.left": "超时上限 {min} 分钟",
|
|
48
|
+
"run.timeout.near": "接近超时上限——超时后自动暂停,已投出的票保留",
|
|
49
|
+
"run.waiting.start": "正在等待模型开始执行…{step}",
|
|
50
|
+
"run.pass": "通过",
|
|
51
|
+
"run.fail": "未通过",
|
|
52
|
+
"run.infra": "环境故障",
|
|
53
|
+
"run.check.passed": "验证通过",
|
|
54
|
+
"run.check.failed": "验证未通过",
|
|
55
|
+
"run.rewind": "↩ 回退 {from} → {to}",
|
|
56
|
+
"run.report": "最终报告",
|
|
57
|
+
"run.elapsed.title": "当前步骤已运行时长",
|
|
58
|
+
"status.done": "已完成",
|
|
59
|
+
"status.cancelled": "已取消",
|
|
60
|
+
"status.failed": "已暂停",
|
|
61
|
+
"status.gate": "待审查",
|
|
62
|
+
"status.check": "验证中",
|
|
63
|
+
"status.do": "进行中",
|
|
64
|
+
"status.detached": "已转交",
|
|
65
|
+
"run.hide": "关闭这张卡片",
|
|
66
|
+
"run.detached": "此工作流已在另一个会话继续——本卡仅保留历史,不再更新。",
|
|
67
|
+
"drawer.ended.more": "…以及更早的 {n} 条",
|
|
68
|
+
"drawer.cancel.confirm": "确认取消?",
|
|
69
|
+
"drawer.cancel.back": "返回",
|
|
70
|
+
"drawer.report": "报告",
|
|
71
|
+
"drawer.timeleft": "剩余 {t}",
|
|
72
|
+
"drawer.timeout": "已超时,等待自动暂停",
|
|
73
|
+
"header.aria": "Ralph Flow:{live} 运行中,{gate} 待审查,{fail} 已暂停",
|
|
74
|
+
"header.list": "Ralph Flow 任务列表",
|
|
75
|
+
"drawer.live": "进行中",
|
|
76
|
+
"drawer.todo": "待处理",
|
|
77
|
+
"drawer.owner": "属主",
|
|
78
|
+
"drawer.owner.me": "本会话",
|
|
79
|
+
"drawer.gate.todo": "审查要求",
|
|
80
|
+
"voter.status.pending": "排队中",
|
|
81
|
+
"voter.status.running": "运行中",
|
|
82
|
+
"voter.status.passed": "通过",
|
|
83
|
+
"voter.status.failed": "未通过",
|
|
84
|
+
"voter.status.infra_pending": "环境重试",
|
|
85
|
+
"voter.status.infra_failed": "环境故障",
|
|
86
|
+
"voter.status.cancelled": "已取消",
|
|
87
|
+
"drawer.ended": "已结束",
|
|
88
|
+
"drawer.step.prefix": "▸",
|
|
89
|
+
"drawer.locate": "定位到对话",
|
|
90
|
+
"drawer.detail": "状态详情",
|
|
91
|
+
"command.error": "出错",
|
|
92
|
+
"command.running": "执行中…",
|
|
93
|
+
"run.hint.approved": "已提交,进度将实时更新。",
|
|
94
|
+
"drawer.collapse": "收起详情",
|
|
95
|
+
"drawer.task": "任务",
|
|
96
|
+
"drawer.pausereason": "暂停原因",
|
|
97
|
+
"drawer.failreason": "失败原因",
|
|
98
|
+
"drawer.voters": "验证者",
|
|
99
|
+
"coldstart.aria": "Ralph Flow:工作流引擎(点击查看上手引导)",
|
|
100
|
+
"coldstart.title": "开始使用 Ralph Flow",
|
|
101
|
+
"coldstart.desc": "DO→CHECK 状态机工作流:模型执行任务,独立验证者对抗检查,失败自动返工,关键步骤停下等你审查。",
|
|
102
|
+
"coldstart.try.loop": "/loop <任务描述> — 迭代执行直到验证通过",
|
|
103
|
+
"coldstart.try.list": "/ralphflow-list — 查看可用工作流",
|
|
104
|
+
};
|
|
105
|
+
const en = {
|
|
106
|
+
"run.running": "Running",
|
|
107
|
+
"run.check": "Verifying",
|
|
108
|
+
"run.gate": "Pending review",
|
|
109
|
+
"run.done": "Completed",
|
|
110
|
+
"run.failed": "Failed",
|
|
111
|
+
"run.cancelled": "Cancelled",
|
|
112
|
+
"run.paused": "⏸ Paused",
|
|
113
|
+
"run.paused.hint": "The workflow is paused (a check failed or the retry limit was reached) — this is not the end: fix the issue and continue; all completed work is preserved.",
|
|
114
|
+
"run.paused.hint.checkfailed": "The check for this step did not pass and the workflow is waiting for you. All finished work is preserved: click Resume to let the model redo it with the failure reasons, or use /ralphflow-rewind to go back to an earlier step.",
|
|
115
|
+
"run.resume": "Resume",
|
|
116
|
+
"run.approve": "Approve",
|
|
117
|
+
"run.return": "Send back",
|
|
118
|
+
"run.return.opinion": "Change request (sent to the model with the command)",
|
|
119
|
+
"run.return.placeholder": "e.g. edge cases not covered, tests are broken — fix CI first…",
|
|
120
|
+
"run.return.send": "Send back",
|
|
121
|
+
"run.cancel.action": "Cancel",
|
|
122
|
+
"run.chip.tests": "Tests failing — fix them first",
|
|
123
|
+
"run.chip.scope": "Drifted off-scope — recheck the original task",
|
|
124
|
+
"run.chip.quality": "Quality below bar — self-review and resubmit",
|
|
125
|
+
"run.hint.approve": "/ralphflow-continue filled into the composer and submit attempted — press Enter if it was not sent to approve verification.",
|
|
126
|
+
"run.hint.return": "/ralphflow-reset filled into the composer — append your change request, then press Enter yourself (it is never auto-sent).",
|
|
127
|
+
"run.hint.returned": "Change request sent — the model will redo this step accordingly.",
|
|
128
|
+
"run.gate.requirement": "Step requirement (review material)",
|
|
129
|
+
"run.verifier": "Verifier {v}/{count}",
|
|
130
|
+
"run.voter.queued": "Queued",
|
|
131
|
+
"run.voter.running": "Running",
|
|
132
|
+
"run.voter.retrying": "Retrying",
|
|
133
|
+
"run.votes": "Independent check · {done}/{total} votes in",
|
|
134
|
+
"run.timeout.left": "Timeout cap {min} min",
|
|
135
|
+
"run.timeout.near": "Near the timeout cap — it will auto-pause; votes already cast are kept",
|
|
136
|
+
"run.waiting.start": "Waiting for the model to start…{step}",
|
|
137
|
+
"run.pass": "PASS",
|
|
138
|
+
"run.fail": "FAIL",
|
|
139
|
+
"run.infra": "Infra error",
|
|
140
|
+
"run.check.passed": "Check passed",
|
|
141
|
+
"run.check.failed": "Check failed",
|
|
142
|
+
"run.rewind": "↩ Rewound {from} → {to}",
|
|
143
|
+
"run.report": "Final report",
|
|
144
|
+
"run.elapsed.title": "Time elapsed on current step",
|
|
145
|
+
"status.done": "Done",
|
|
146
|
+
"status.cancelled": "Cancelled",
|
|
147
|
+
"status.failed": "Paused",
|
|
148
|
+
"status.gate": "Pending review",
|
|
149
|
+
"status.check": "Verifying",
|
|
150
|
+
"status.do": "Running",
|
|
151
|
+
"status.detached": "Handed off",
|
|
152
|
+
"run.hide": "Dismiss this card",
|
|
153
|
+
"run.detached": "This workflow continues in another session — this card keeps history only and will not update.",
|
|
154
|
+
"drawer.ended.more": "…and {n} earlier entries",
|
|
155
|
+
"drawer.cancel.confirm": "Confirm cancel?",
|
|
156
|
+
"drawer.cancel.back": "Back",
|
|
157
|
+
"drawer.report": "Report",
|
|
158
|
+
"drawer.timeleft": "{t} left",
|
|
159
|
+
"drawer.timeout": "Timed out — auto-pausing",
|
|
160
|
+
"header.aria": "Ralph Flow: {live} running, {gate} pending review, {fail} paused",
|
|
161
|
+
"header.list": "Ralph Flow task list",
|
|
162
|
+
"drawer.live": "In progress",
|
|
163
|
+
"drawer.todo": "Needs attention",
|
|
164
|
+
"drawer.owner": "Owner",
|
|
165
|
+
"drawer.owner.me": "This session",
|
|
166
|
+
"drawer.gate.todo": "Review requirement",
|
|
167
|
+
"voter.status.pending": "Queued",
|
|
168
|
+
"voter.status.running": "Running",
|
|
169
|
+
"voter.status.passed": "Passed",
|
|
170
|
+
"voter.status.failed": "Failed",
|
|
171
|
+
"voter.status.infra_pending": "Infra retry",
|
|
172
|
+
"voter.status.infra_failed": "Infra error",
|
|
173
|
+
"voter.status.cancelled": "Cancelled",
|
|
174
|
+
"drawer.ended": "Ended",
|
|
175
|
+
"drawer.step.prefix": "▸",
|
|
176
|
+
"drawer.locate": "Locate in chat",
|
|
177
|
+
"drawer.detail": "Status detail",
|
|
178
|
+
"command.error": "Error",
|
|
179
|
+
"command.running": "Running…",
|
|
180
|
+
"run.hint.approved": "Submitted — progress updates live.",
|
|
181
|
+
"drawer.collapse": "Collapse details",
|
|
182
|
+
"drawer.task": "Task",
|
|
183
|
+
"drawer.pausereason": "Pause reason",
|
|
184
|
+
"drawer.failreason": "Failure reason",
|
|
185
|
+
"drawer.voters": "Verifiers",
|
|
186
|
+
"coldstart.aria": "Ralph Flow: workflow engine (click for onboarding)",
|
|
187
|
+
"coldstart.title": "Get started with Ralph Flow",
|
|
188
|
+
"coldstart.desc": "DO→CHECK state-machine workflows: the model does the work, independent verifiers adversarially check it, failures loop back automatically, and key steps pause for your review.",
|
|
189
|
+
"coldstart.try.loop": "/loop <task> — iterate until verification passes",
|
|
190
|
+
"coldstart.try.list": "/ralphflow-list — list available workflows",
|
|
191
|
+
};
|
|
192
|
+
export function apply(ctx) {
|
|
193
|
+
// 0. HTTP 状态轮询(UI 主数据源:替代已废弃的会话帧通道)+ 全局通知/快照
|
|
194
|
+
// 监视(不依赖任何组件挂载:空态页/其它会话时审查门也能提醒)+ 权限预热
|
|
195
|
+
try {
|
|
196
|
+
startHttpPolling();
|
|
197
|
+
}
|
|
198
|
+
catch { }
|
|
199
|
+
try {
|
|
200
|
+
startNotifyWatch();
|
|
201
|
+
}
|
|
202
|
+
catch { }
|
|
203
|
+
try {
|
|
204
|
+
primeNotifications();
|
|
205
|
+
}
|
|
206
|
+
catch { }
|
|
207
|
+
// 1. 事件折叠器(host 的 tool-ralphflow/* 帧 → 持久对话节点)
|
|
208
|
+
ctx.conversationEvents.register(createDefinition());
|
|
209
|
+
// 1b. 命令结果折叠器(/ral 的 opencode 式反馈 → 独立可见结果卡)
|
|
210
|
+
ctx.conversationEvents.register(createCommandDefinition());
|
|
211
|
+
// 2. locale 词典
|
|
212
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), "ralphflow:dictionaries");
|
|
213
|
+
// 3. 对话内嵌卡(keyed chat node)
|
|
214
|
+
ctx.slots.inject("conversation.chat.node", () => ctx.slots.register({
|
|
215
|
+
name: "conversation.chat.node",
|
|
216
|
+
key: RALPHFLOW_RUN_KIND,
|
|
217
|
+
locale: NS,
|
|
218
|
+
inject: () => ({
|
|
219
|
+
openFile: (path) => {
|
|
220
|
+
try {
|
|
221
|
+
window.open(path, "_blank");
|
|
222
|
+
}
|
|
223
|
+
catch { }
|
|
224
|
+
},
|
|
225
|
+
}),
|
|
226
|
+
}, RalphRunCard));
|
|
227
|
+
// 3b. 命令结果卡(独立 keyed 节点)
|
|
228
|
+
ctx.slots.inject("conversation.chat.node", () => ctx.slots.register({
|
|
229
|
+
name: "conversation.chat.node",
|
|
230
|
+
key: RALPHFLOW_COMMAND_KIND,
|
|
231
|
+
locale: NS,
|
|
232
|
+
}, RalphCommandCard));
|
|
233
|
+
// 4. 页头入口(无任务时不渲染)
|
|
234
|
+
ctx.slots.inject("conversation.session.header.actions", () => ctx.slots.register({
|
|
235
|
+
name: "conversation.session.header.actions",
|
|
236
|
+
id: "ralphflow-header",
|
|
237
|
+
locale: NS,
|
|
238
|
+
order: 100,
|
|
239
|
+
}, HeaderAction));
|
|
240
|
+
// 5. 页面级命令转发:卡片/弹层发出的 ralphflow:command 事件 → 注入输入框。
|
|
241
|
+
// dsh composer 有内部 claim 机制,插件无法安全代提交;这里填入命令后尝试
|
|
242
|
+
// 触发一次 Enter keydown(composer 若监听该键即直接发出),失败时输入框里
|
|
243
|
+
// 保留命令文本,用户回车即可——绝不静默丢弃。
|
|
244
|
+
try {
|
|
245
|
+
const findComposer = () => (document.querySelector("textarea[data-dsh-composer]")
|
|
246
|
+
?? document.querySelector(".composer textarea")
|
|
247
|
+
?? document.querySelector("textarea"));
|
|
248
|
+
const onCommand = (e) => {
|
|
249
|
+
const detail = e.detail;
|
|
250
|
+
if (!detail?.command)
|
|
251
|
+
return;
|
|
252
|
+
const input = findComposer();
|
|
253
|
+
if (!input)
|
|
254
|
+
return;
|
|
255
|
+
const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")?.set;
|
|
256
|
+
if (setter)
|
|
257
|
+
setter.call(input, detail.command);
|
|
258
|
+
else
|
|
259
|
+
input.value = detail.command;
|
|
260
|
+
input.focus();
|
|
261
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
262
|
+
// autoSubmit=false(「打回」按钮)只填充绝不代提交:用户必须先补上修改
|
|
263
|
+
// 意见再自己回车,防止空意见的 reset 被抢发导致盲目返工。
|
|
264
|
+
if (detail.autoSubmit === false)
|
|
265
|
+
return;
|
|
266
|
+
// 尝试代提交:React 在 root 上监听原生 keydown,合成 Enter 通常可触发
|
|
267
|
+
const opts = { key: "Enter", code: "Enter", keyCode: 13, bubbles: true, cancelable: true };
|
|
268
|
+
input.dispatchEvent(new KeyboardEvent("keydown", opts));
|
|
269
|
+
input.dispatchEvent(new KeyboardEvent("keyup", opts));
|
|
270
|
+
};
|
|
271
|
+
window.addEventListener("ralphflow:command", onCommand);
|
|
272
|
+
ctx.effect(() => () => window.removeEventListener("ralphflow:command", onCommand), "ralphflow:command-bridge");
|
|
273
|
+
}
|
|
274
|
+
catch { }
|
|
275
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ralph Flow for dsh — client 端事件折叠器(仿官方 workflowRunDefinition)
|
|
3
|
+
*
|
|
4
|
+
* 把 host 端发射的 tool-ralphflow/* 事件帧折叠成持久的对话内嵌节点
|
|
5
|
+
* (kind: 'ralphflow-run'),注册进 conversation.chat.node keyed 槽。
|
|
6
|
+
* 折叠幂等:update 只累加/替换字段,按事件 seq 顺序重放可恢复同一视图。
|
|
7
|
+
*/
|
|
8
|
+
export interface RalphVerdict {
|
|
9
|
+
voter: number;
|
|
10
|
+
count: number;
|
|
11
|
+
model: string;
|
|
12
|
+
status: "pass" | "fail" | "infra";
|
|
13
|
+
reasoning: string;
|
|
14
|
+
/** 判定到达时刻(ms,来自 verdict 帧 ts)——与 voter-start 相减得该票用时 */
|
|
15
|
+
arrivedAt?: number;
|
|
16
|
+
}
|
|
17
|
+
/** 验证者活体:check-voter-start 帧(某票开跑)——黑盒期可观测的数据源 */
|
|
18
|
+
export interface RalphVoterStart {
|
|
19
|
+
voter: number;
|
|
20
|
+
count: number;
|
|
21
|
+
model?: string;
|
|
22
|
+
startedAt: number;
|
|
23
|
+
retry?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface RalphRunState {
|
|
26
|
+
workflow: string;
|
|
27
|
+
task: string;
|
|
28
|
+
/** 实例 id(host 的 runId)——页头任务行用它精确定位 /ralphflow-status */
|
|
29
|
+
runId?: string;
|
|
30
|
+
steps: {
|
|
31
|
+
id: string;
|
|
32
|
+
desc: string;
|
|
33
|
+
}[];
|
|
34
|
+
current: {
|
|
35
|
+
step: string;
|
|
36
|
+
phase: "do" | "check";
|
|
37
|
+
failCount: number;
|
|
38
|
+
} | null;
|
|
39
|
+
/** 当前步骤的开始时间戳(ms,来自 host 帧的 ts;用于「已运行 Xm」展示) */
|
|
40
|
+
stepStartedAt?: number;
|
|
41
|
+
/** 当前步骤验证者总数(step-start check 帧携带;首票落地前即可显示 M/N 进度) */
|
|
42
|
+
voters?: number;
|
|
43
|
+
/** 当前验证轮的超时上限(ms)——超时进度条的终点感 */
|
|
44
|
+
timeoutMs?: number;
|
|
45
|
+
/** 已启动的验证者(排队/运行中二态的判定源;verdict 到达即视为完成) */
|
|
46
|
+
voterStarts: RalphVoterStart[];
|
|
47
|
+
verdicts: RalphVerdict[];
|
|
48
|
+
lastCheck: {
|
|
49
|
+
passed: boolean;
|
|
50
|
+
reason?: string;
|
|
51
|
+
} | null;
|
|
52
|
+
gate: {
|
|
53
|
+
step: string;
|
|
54
|
+
title: string;
|
|
55
|
+
reason: string;
|
|
56
|
+
taskExcerpt?: string;
|
|
57
|
+
} | null;
|
|
58
|
+
rewinds: {
|
|
59
|
+
fromStep: string;
|
|
60
|
+
toStep: string;
|
|
61
|
+
reason: string;
|
|
62
|
+
}[];
|
|
63
|
+
report: {
|
|
64
|
+
text: string;
|
|
65
|
+
reportPath?: string;
|
|
66
|
+
} | null;
|
|
67
|
+
stopReason: "done" | "cancelled" | "failed" | undefined;
|
|
68
|
+
/** 已转交其他会话(接管时旧会话收到 run-detach)——不再计入运行中 */
|
|
69
|
+
detached?: boolean;
|
|
70
|
+
}
|
|
71
|
+
export declare const RALPHFLOW_RUN_KIND = "ralphflow-run";
|
|
72
|
+
declare const EVENT_TYPES: readonly ["tool-ralphflow/run-start", "tool-ralphflow/run-detach", "tool-ralphflow/step-start", "tool-ralphflow/check-voter-start", "tool-ralphflow/check-verdict", "tool-ralphflow/check-result", "tool-ralphflow/gate", "tool-ralphflow/rewind", "tool-ralphflow/reset", "tool-ralphflow/report", "tool-ralphflow/run-end"];
|
|
73
|
+
export type RalphflowEventType = (typeof EVENT_TYPES)[number];
|
|
74
|
+
export declare function match(event: {
|
|
75
|
+
type: string;
|
|
76
|
+
data?: any;
|
|
77
|
+
}): {
|
|
78
|
+
id: string;
|
|
79
|
+
role: "start" | "update";
|
|
80
|
+
} | null;
|
|
81
|
+
export declare function startState(data: any): RalphRunState;
|
|
82
|
+
export declare function updateState(state: RalphRunState, event: {
|
|
83
|
+
type: string;
|
|
84
|
+
data?: any;
|
|
85
|
+
}): RalphRunState;
|
|
86
|
+
export declare function buildViewNode(context: any): any;
|
|
87
|
+
/** 注册 conversationEvents 定义(供 client.ts apply 调用) */
|
|
88
|
+
export declare function createDefinition(): {
|
|
89
|
+
kind: string;
|
|
90
|
+
target: string;
|
|
91
|
+
match: typeof match;
|
|
92
|
+
start: (_context: any, match: any) => RalphRunState;
|
|
93
|
+
update: (context: any, match: any) => RalphRunState;
|
|
94
|
+
buildViewNode: typeof buildViewNode;
|
|
95
|
+
};
|
|
96
|
+
export declare const RALPHFLOW_COMMAND_KIND = "ralphflow-command";
|
|
97
|
+
export interface RalphCommandState {
|
|
98
|
+
/** 命令名(不含前导斜杠),如 ralphflow-list、loop */
|
|
99
|
+
command: string;
|
|
100
|
+
/** 宿主返回的 handler 输出文本(command/done.text) */
|
|
101
|
+
text: string;
|
|
102
|
+
/** command/run 已到、command/done 未到:执行中 */
|
|
103
|
+
running: boolean;
|
|
104
|
+
/** command/done.kind === "error" */
|
|
105
|
+
error: boolean;
|
|
106
|
+
}
|
|
107
|
+
export declare function registerWorkflowShortcutNames(names: string[]): void;
|
|
108
|
+
export declare function matchCommand(event: {
|
|
109
|
+
type: string;
|
|
110
|
+
data?: any;
|
|
111
|
+
}): {
|
|
112
|
+
id: string;
|
|
113
|
+
role: "start" | "update";
|
|
114
|
+
} | null;
|
|
115
|
+
export declare function startCommandState(data: any): RalphCommandState;
|
|
116
|
+
export declare function updateCommandState(state: RalphCommandState, data: any): RalphCommandState;
|
|
117
|
+
export declare function buildCommandViewNode(context: any): any;
|
|
118
|
+
export declare function createCommandDefinition(): {
|
|
119
|
+
kind: string;
|
|
120
|
+
target: string;
|
|
121
|
+
match: typeof matchCommand;
|
|
122
|
+
start: (_context: any, m: any) => RalphCommandState;
|
|
123
|
+
update: (context: any, m: any) => RalphCommandState;
|
|
124
|
+
buildViewNode: typeof buildCommandViewNode;
|
|
125
|
+
};
|
|
126
|
+
export {};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ralph Flow for dsh — client 端事件折叠器(仿官方 workflowRunDefinition)
|
|
3
|
+
*
|
|
4
|
+
* 把 host 端发射的 tool-ralphflow/* 事件帧折叠成持久的对话内嵌节点
|
|
5
|
+
* (kind: 'ralphflow-run'),注册进 conversation.chat.node keyed 槽。
|
|
6
|
+
* 折叠幂等:update 只累加/替换字段,按事件 seq 顺序重放可恢复同一视图。
|
|
7
|
+
*/
|
|
8
|
+
import { isHiddenEvent } from "./hidden-runs.js";
|
|
9
|
+
export const RALPHFLOW_RUN_KIND = "ralphflow-run";
|
|
10
|
+
const EVENT_TYPES = [
|
|
11
|
+
"tool-ralphflow/run-start",
|
|
12
|
+
"tool-ralphflow/run-detach",
|
|
13
|
+
"tool-ralphflow/step-start",
|
|
14
|
+
"tool-ralphflow/check-voter-start",
|
|
15
|
+
"tool-ralphflow/check-verdict",
|
|
16
|
+
"tool-ralphflow/check-result",
|
|
17
|
+
"tool-ralphflow/gate",
|
|
18
|
+
"tool-ralphflow/rewind",
|
|
19
|
+
"tool-ralphflow/reset",
|
|
20
|
+
"tool-ralphflow/report",
|
|
21
|
+
"tool-ralphflow/run-end",
|
|
22
|
+
];
|
|
23
|
+
export function match(event) {
|
|
24
|
+
// 缺 runId 的帧无法归属到任何 run——丢弃,避免不同 run 的帧折叠进
|
|
25
|
+
// 字面量 "undefined" 的同一节点互相污染。
|
|
26
|
+
if (!event.data?.runId)
|
|
27
|
+
return null;
|
|
28
|
+
// 用户手动关闭的卡:整条 run 的帧都不再匹配(存量僵尸卡的最终出口)
|
|
29
|
+
if (isHiddenEvent(event.data))
|
|
30
|
+
return null;
|
|
31
|
+
if (event.type === "tool-ralphflow/run-start") {
|
|
32
|
+
return { id: String(event.data?.runId), role: "start" };
|
|
33
|
+
}
|
|
34
|
+
if (EVENT_TYPES.includes(event.type) && event.type !== "tool-ralphflow/run-start") {
|
|
35
|
+
return { id: String(event.data?.runId), role: "update" };
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
export function startState(data) {
|
|
40
|
+
return {
|
|
41
|
+
workflow: data?.workflow ?? "",
|
|
42
|
+
task: data?.task ?? "",
|
|
43
|
+
runId: data?.runId ? String(data.runId) : undefined,
|
|
44
|
+
steps: Array.isArray(data?.steps) ? data.steps : [],
|
|
45
|
+
current: null,
|
|
46
|
+
stepStartedAt: undefined,
|
|
47
|
+
voterStarts: [],
|
|
48
|
+
verdicts: [],
|
|
49
|
+
lastCheck: null,
|
|
50
|
+
gate: null,
|
|
51
|
+
rewinds: [],
|
|
52
|
+
report: null,
|
|
53
|
+
stopReason: undefined,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function updateState(state, event) {
|
|
57
|
+
// 终态冻结:仅真正的终态(完成/取消)冻结视图。stopReason:"failed" 是暂停
|
|
58
|
+
// (max_failures/check_error 后仍可 continue 恢复),不能冻结——否则恢复后
|
|
59
|
+
// 的 step-start/verdict 帧全被丢弃,卡片永远停在「已失败」。
|
|
60
|
+
if (state.stopReason === "done" || state.stopReason === "cancelled")
|
|
61
|
+
return state;
|
|
62
|
+
const d = event.data ?? {};
|
|
63
|
+
switch (event.type) {
|
|
64
|
+
// 转交:实例已被另一个会话接管,本会话这张卡停止计数(僵尸运行消除)
|
|
65
|
+
case "tool-ralphflow/run-detach":
|
|
66
|
+
return { ...state, detached: true };
|
|
67
|
+
case "tool-ralphflow/step-start":
|
|
68
|
+
return {
|
|
69
|
+
...state,
|
|
70
|
+
current: { step: d.step, phase: d.phase, failCount: d.failCount ?? 0 },
|
|
71
|
+
stepStartedAt: typeof d.ts === "number" && d.ts > 0 ? d.ts : Date.now(),
|
|
72
|
+
voters: typeof d.voters === "number" && d.voters > 0 ? d.voters : state.voters,
|
|
73
|
+
timeoutMs: typeof d.timeoutMs === "number" && d.timeoutMs > 0 ? d.timeoutMs : state.timeoutMs,
|
|
74
|
+
// 新验证轮开始:上一轮的启动记录清空(verdicts 由 check-result 清)
|
|
75
|
+
voterStarts: [],
|
|
76
|
+
// step-start 到达即说明审查门已批准/跳过(门只在 DO 完成点停住),
|
|
77
|
+
// check 相位的第一帧也要摘掉门卡,不能等下一步 DO 才清。
|
|
78
|
+
gate: null,
|
|
79
|
+
};
|
|
80
|
+
case "tool-ralphflow/check-voter-start": {
|
|
81
|
+
const incoming = {
|
|
82
|
+
voter: typeof d.voter === "number" ? d.voter : 1,
|
|
83
|
+
count: typeof d.count === "number" ? d.count : 1,
|
|
84
|
+
model: typeof d.model === "string" && d.model ? d.model : undefined,
|
|
85
|
+
startedAt: typeof d.startedAt === "number" && d.startedAt > 0 ? d.startedAt : Date.now(),
|
|
86
|
+
retry: !!d.retry,
|
|
87
|
+
};
|
|
88
|
+
const idx = state.voterStarts.findIndex((s) => s.voter === incoming.voter);
|
|
89
|
+
const voterStarts = idx >= 0
|
|
90
|
+
? state.voterStarts.map((s, i) => (i === idx ? incoming : s))
|
|
91
|
+
: [...state.voterStarts, incoming];
|
|
92
|
+
return { ...state, voterStarts };
|
|
93
|
+
}
|
|
94
|
+
case "tool-ralphflow/check-verdict": {
|
|
95
|
+
const status = d.status === "pass" || d.status === "fail" || d.status === "infra" ? d.status : "infra";
|
|
96
|
+
// 按 voter 序号 upsert(同一轮内 voter 唯一):帧重放/乱序重复不会多计票,
|
|
97
|
+
// 迟到的旧票也不会覆盖新一轮的同号票之外的数据。
|
|
98
|
+
const incoming = {
|
|
99
|
+
voter: d.voter ?? 1,
|
|
100
|
+
count: d.count ?? 1,
|
|
101
|
+
model: d.model ?? "",
|
|
102
|
+
status,
|
|
103
|
+
reasoning: d.reasoning ?? "",
|
|
104
|
+
...(typeof d.arrivedAt === "number" && d.arrivedAt > 0 ? { arrivedAt: d.arrivedAt } : {}),
|
|
105
|
+
};
|
|
106
|
+
const idx = state.verdicts.findIndex((v) => v.voter === incoming.voter);
|
|
107
|
+
const verdicts = idx >= 0
|
|
108
|
+
? state.verdicts.map((v, i) => (i === idx ? incoming : v))
|
|
109
|
+
: [...state.verdicts, incoming];
|
|
110
|
+
return { ...state, verdicts };
|
|
111
|
+
}
|
|
112
|
+
case "tool-ralphflow/check-result":
|
|
113
|
+
return { ...state, lastCheck: { passed: !!d.passed, reason: d.reason }, verdicts: [] };
|
|
114
|
+
case "tool-ralphflow/gate":
|
|
115
|
+
return {
|
|
116
|
+
...state,
|
|
117
|
+
gate: {
|
|
118
|
+
step: d.step, title: d.title, reason: d.reason,
|
|
119
|
+
...(typeof d.taskExcerpt === "string" && d.taskExcerpt ? { taskExcerpt: d.taskExcerpt } : {}),
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
case "tool-ralphflow/rewind":
|
|
123
|
+
return {
|
|
124
|
+
...state,
|
|
125
|
+
rewinds: [...state.rewinds, { fromStep: d.fromStep, toStep: d.toStep, reason: d.reason }],
|
|
126
|
+
gate: null,
|
|
127
|
+
verdicts: [],
|
|
128
|
+
lastCheck: null,
|
|
129
|
+
};
|
|
130
|
+
case "tool-ralphflow/reset":
|
|
131
|
+
return { ...state, gate: null };
|
|
132
|
+
case "tool-ralphflow/report":
|
|
133
|
+
return { ...state, report: { text: d.text ?? "", ...(typeof d.reportPath === "string" && d.reportPath ? { reportPath: d.reportPath } : {}) } };
|
|
134
|
+
case "tool-ralphflow/run-end":
|
|
135
|
+
return { ...state, stopReason: d.stopReason };
|
|
136
|
+
default:
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
export function buildViewNode(context) {
|
|
141
|
+
if (!context.start)
|
|
142
|
+
return null;
|
|
143
|
+
const state = context.state ?? startState({});
|
|
144
|
+
return {
|
|
145
|
+
key: context.key,
|
|
146
|
+
kind: RALPHFLOW_RUN_KIND,
|
|
147
|
+
id: context.id,
|
|
148
|
+
target: "chat",
|
|
149
|
+
anchorSeq: context.start.event.seq,
|
|
150
|
+
location: context.start.location,
|
|
151
|
+
visibility: "visible",
|
|
152
|
+
data: state,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** 注册 conversationEvents 定义(供 client.ts apply 调用) */
|
|
156
|
+
export function createDefinition() {
|
|
157
|
+
return {
|
|
158
|
+
kind: RALPHFLOW_RUN_KIND,
|
|
159
|
+
target: "chat",
|
|
160
|
+
match,
|
|
161
|
+
start: (_context, match) => startState(match.event.data),
|
|
162
|
+
update: (context, match) => updateState(context.state, match.event),
|
|
163
|
+
buildViewNode,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
// ─── 命令结果卡(折叠官方 command/run + command/done 事件)───────────────────
|
|
167
|
+
// 宿主对每条命令自动追加官方已知类型 command/run + command/done(dsh-commands
|
|
168
|
+
// 的 execute 生命周期,见 dsh-commands/lib/index.js appendLifecycle);本折叠器
|
|
169
|
+
// 只匹配 ralphflow 相关命令名渲染成对话内嵌卡。绝不向会话写入任何自定义帧——
|
|
170
|
+
// 第三方插件自定义事件类型在宿主持久化读路径上会砖掉整个会话(无 ignorable
|
|
171
|
+
// 逃生门),而 command/run + command/done 是官方词汇表成员,落盘重放安全。
|
|
172
|
+
export const RALPHFLOW_COMMAND_KIND = "ralphflow-command";
|
|
173
|
+
/** 静态管理命令 + create(宿主注册的 ralphflow 命令全集) */
|
|
174
|
+
const STATIC_COMMAND_NAMES = new Set([
|
|
175
|
+
"ralphflow-start", "ralphflow-continue", "ralphflow-status", "ralphflow-list",
|
|
176
|
+
"ralphflow-cancel", "ralphflow-rewind", "ralphflow-reset", "ralphflow-doctor",
|
|
177
|
+
"ralphflow-create",
|
|
178
|
+
]);
|
|
179
|
+
/** 动态工作流快捷命令(/loop、/spec……)——由 HTTP 通道的工作流清单喂入 */
|
|
180
|
+
const workflowShortcuts = new Set();
|
|
181
|
+
export function registerWorkflowShortcutNames(names) {
|
|
182
|
+
workflowShortcuts.clear();
|
|
183
|
+
for (const n of Array.isArray(names) ? names : []) {
|
|
184
|
+
try {
|
|
185
|
+
workflowShortcuts.add(String(n).toLowerCase());
|
|
186
|
+
}
|
|
187
|
+
catch { }
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function isRalphflowCommand(name) {
|
|
191
|
+
const n = String(name ?? "");
|
|
192
|
+
return STATIC_COMMAND_NAMES.has(n) || n.startsWith("ralphflow-") || workflowShortcuts.has(n.toLowerCase());
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 已确认为 ralphflow 命令的 commandId 集合:command/run 带 name(可过滤),
|
|
196
|
+
* command/done 只有 commandId——用 run 建立白名单、done 按白名单配对,且在
|
|
197
|
+
* 同一会话的事件重放中 run 先于 done 到达,刷新后重放同样成立。
|
|
198
|
+
*/
|
|
199
|
+
const knownCommandIds = new Set();
|
|
200
|
+
export function matchCommand(event) {
|
|
201
|
+
const d = event.data ?? {};
|
|
202
|
+
const commandId = String(d?.commandId ?? "");
|
|
203
|
+
if (!commandId)
|
|
204
|
+
return null;
|
|
205
|
+
if (event.type === "command/run") {
|
|
206
|
+
const name = String(d?.name ?? "");
|
|
207
|
+
if (!name || !isRalphflowCommand(name))
|
|
208
|
+
return null;
|
|
209
|
+
knownCommandIds.add(commandId);
|
|
210
|
+
return { id: `cmd-${commandId}`, role: "start" };
|
|
211
|
+
}
|
|
212
|
+
if (event.type === "command/done") {
|
|
213
|
+
// name 不随 done 携带:只认 run 阶段登记过的 commandId(跨命令不误配)
|
|
214
|
+
if (!knownCommandIds.has(commandId))
|
|
215
|
+
return null;
|
|
216
|
+
knownCommandIds.delete(commandId);
|
|
217
|
+
return { id: `cmd-${commandId}`, role: "update" };
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
export function startCommandState(data) {
|
|
222
|
+
return { command: String(data?.name ?? ""), text: "", running: true, error: false };
|
|
223
|
+
}
|
|
224
|
+
export function updateCommandState(state, data) {
|
|
225
|
+
try {
|
|
226
|
+
const kind = String(data?.kind ?? "");
|
|
227
|
+
const text = String(data?.text ?? "");
|
|
228
|
+
// error 分支宿主会把 handler 抛错的 message 放 text;成功分支 text 即结果
|
|
229
|
+
return { command: state.command || String(data?.name ?? ""), text, running: false, error: kind === "error" };
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
return state;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
export function buildCommandViewNode(context) {
|
|
236
|
+
if (!context.start)
|
|
237
|
+
return null;
|
|
238
|
+
const state = context.state ?? startCommandState({});
|
|
239
|
+
return {
|
|
240
|
+
key: context.key,
|
|
241
|
+
kind: RALPHFLOW_COMMAND_KIND,
|
|
242
|
+
id: context.id,
|
|
243
|
+
target: "chat",
|
|
244
|
+
anchorSeq: context.start.event.seq,
|
|
245
|
+
location: context.start.location,
|
|
246
|
+
visibility: "visible",
|
|
247
|
+
data: state,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
export function createCommandDefinition() {
|
|
251
|
+
return {
|
|
252
|
+
kind: RALPHFLOW_COMMAND_KIND,
|
|
253
|
+
target: "chat",
|
|
254
|
+
match: matchCommand,
|
|
255
|
+
start: (_context, m) => startCommandState(m.event.data),
|
|
256
|
+
update: (context, m) => updateCommandState(context.state, m.event.data),
|
|
257
|
+
buildViewNode: buildCommandViewNode,
|
|
258
|
+
};
|
|
259
|
+
}
|