ralphflow-dsh 0.1.0 → 2.0.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.
Files changed (67) hide show
  1. package/README.md +227 -158
  2. package/lib/create.d.ts +7 -11
  3. package/lib/create.js +132 -83
  4. package/lib/engine.d.ts +667 -262
  5. package/lib/engine.js +3583 -2579
  6. package/lib/index.d.ts +3 -4
  7. package/lib/index.js +222 -163
  8. package/lib/message-source.d.ts +25 -0
  9. package/lib/message-source.js +2 -0
  10. package/lib/reset.d.ts +40 -0
  11. package/lib/reset.js +147 -0
  12. package/lib/tools.d.ts +31 -14
  13. package/lib/tools.js +407 -684
  14. package/lib/verify.d.ts +57 -0
  15. package/lib/verify.js +326 -0
  16. package/lib/voting.d.ts +63 -0
  17. package/lib/voting.js +131 -0
  18. package/package.json +24 -45
  19. package/workflows/loop.yaml +21 -6
  20. package/workflows/spec.yaml +51 -46
  21. package/lib/actions.d.ts +0 -37
  22. package/lib/actions.js +0 -55
  23. package/lib/check-voting.d.ts +0 -54
  24. package/lib/check-voting.js +0 -289
  25. package/lib/check.d.ts +0 -64
  26. package/lib/check.js +0 -329
  27. package/lib/client/CommandCard.d.ts +0 -24
  28. package/lib/client/CommandCard.js +0 -69
  29. package/lib/client/HeaderAction.d.ts +0 -39
  30. package/lib/client/HeaderAction.js +0 -509
  31. package/lib/client/RunCard.d.ts +0 -27
  32. package/lib/client/RunCard.js +0 -375
  33. package/lib/client/client.d.ts +0 -3
  34. package/lib/client/client.js +0 -275
  35. package/lib/client/definition.d.ts +0 -126
  36. package/lib/client/definition.js +0 -259
  37. package/lib/client/hidden-runs.d.ts +0 -6
  38. package/lib/client/hidden-runs.js +0 -52
  39. package/lib/client/http-state.d.ts +0 -67
  40. package/lib/client/http-state.js +0 -81
  41. package/lib/client/markdown.d.ts +0 -26
  42. package/lib/client/markdown.js +0 -174
  43. package/lib/client/notify-watch.d.ts +0 -38
  44. package/lib/client/notify-watch.js +0 -98
  45. package/lib/client/notify.d.ts +0 -21
  46. package/lib/client/notify.js +0 -168
  47. package/lib/client/snapshots.d.ts +0 -8
  48. package/lib/client/snapshots.js +0 -101
  49. package/lib/client.js +0 -2361
  50. package/lib/commands.d.ts +0 -41
  51. package/lib/commands.js +0 -223
  52. package/lib/deps.d.ts +0 -17
  53. package/lib/deps.js +0 -19
  54. package/lib/done-detect.d.ts +0 -2
  55. package/lib/done-detect.js +0 -28
  56. package/lib/driver.d.ts +0 -32
  57. package/lib/driver.js +0 -248
  58. package/lib/events.d.ts +0 -35
  59. package/lib/events.js +0 -52
  60. package/lib/jobs.d.ts +0 -56
  61. package/lib/jobs.js +0 -355
  62. package/lib/mutex.d.ts +0 -9
  63. package/lib/mutex.js +0 -21
  64. package/lib/session-hygiene.d.ts +0 -25
  65. package/lib/session-hygiene.js +0 -205
  66. package/lib/voting-progress.d.ts +0 -37
  67. package/lib/voting-progress.js +0 -89
@@ -1,168 +0,0 @@
1
- /**
2
- * Ralph Flow for dsh — 桌面通知 + 标签页标题提醒
3
- *
4
- * 等待的本质问题是「你在等它,但不知道要等多久」——于是切去干别的,回来才
5
- * 发现它早就停在那等你了。终态事件(审查门/暂停/完成)弹系统通知并让标签页
6
- * 标题闪烁,点通知聚焦窗口。全部容错:权限被拒/环境不支持时静默降级为零
7
- * 存在感;sessionStorage 去重防止事件重放(接管历史回放)重复轰炸。
8
- */
9
- const NOTIFIED_KEY = "ralphflow:notified";
10
- /** 同一会话内已通知过的 `${runId}:${kind}` 集合 */
11
- function notifiedSet() {
12
- try {
13
- const raw = sessionStorage.getItem(NOTIFIED_KEY);
14
- return new Set(raw ? JSON.parse(raw) : []);
15
- }
16
- catch {
17
- return new Set();
18
- }
19
- }
20
- function markNotified(key) {
21
- try {
22
- const s = notifiedSet();
23
- s.add(key);
24
- sessionStorage.setItem(NOTIFIED_KEY, JSON.stringify([...s].slice(-200)));
25
- }
26
- catch { }
27
- }
28
- export function notificationsSupported() {
29
- try {
30
- return typeof window !== "undefined" && "Notification" in window;
31
- }
32
- catch {
33
- return false;
34
- }
35
- }
36
- /** 首次交互时请求权限(浏览器要求用户手势);静默失败不打扰 */
37
- export function primeNotifications() {
38
- try {
39
- if (!notificationsSupported())
40
- return;
41
- if (Notification.permission === "default") {
42
- // 不在加载即请求:挂一次性的一次性手势监听
43
- const ask = () => {
44
- try {
45
- void Notification.requestPermission();
46
- }
47
- catch { }
48
- window.removeEventListener("pointerdown", ask);
49
- window.removeEventListener("keydown", ask);
50
- };
51
- window.addEventListener("pointerdown", ask, { once: true });
52
- window.addEventListener("keydown", ask, { once: true });
53
- }
54
- }
55
- catch { }
56
- }
57
- function systemNotify(title, body, tag) {
58
- try {
59
- if (!notificationsSupported() || Notification.permission !== "granted")
60
- return;
61
- const n = new Notification(title, { body, tag });
62
- n.onclick = () => {
63
- try {
64
- window.focus();
65
- n.close();
66
- }
67
- catch { }
68
- };
69
- }
70
- catch { }
71
- }
72
- // ── 标签页标题闪烁 ─────────────────────────────────────────────────────────────
73
- let flashTimer;
74
- let flashCount = 0;
75
- /** 标题闪烁直到窗口获得焦点或超次;原始标题始终可恢复 */
76
- export function flashTitle(text) {
77
- try {
78
- if (flashTimer)
79
- return; // 已在闪烁中:保留最早的提醒,不叠加
80
- const original = document.title;
81
- flashCount = 0;
82
- flashTimer = setInterval(() => {
83
- try {
84
- flashCount++;
85
- if (flashCount > 40 || document.hasFocus()) {
86
- stopFlashTitle(original);
87
- return;
88
- }
89
- document.title = flashCount % 2 === 1 ? `⏰ ${text}` : original;
90
- }
91
- catch {
92
- stopFlashTitle(original);
93
- }
94
- }, 1200);
95
- const onFocus = () => stopFlashTitle(original);
96
- window.addEventListener("focus", onFocus, { once: true });
97
- }
98
- catch { }
99
- }
100
- function stopFlashTitle(original) {
101
- try {
102
- if (flashTimer) {
103
- clearInterval(flashTimer);
104
- flashTimer = undefined;
105
- }
106
- document.title = original;
107
- }
108
- catch { }
109
- }
110
- /**
111
- * 判定一个 run 状态变化是否值得提醒用户(供 UI 层调用;纯函数便于单测)。
112
- * kind: gate=待审查 / paused=已暂停 / done=已完成。cancelled 是用户自己点的,
113
- * 不打扰。
114
- */
115
- export function shouldNotifyRun(prevStopReason, nextStopReason, prevGate, nextGate) {
116
- const hasGate = !!nextGate;
117
- const gateArrived = hasGate && !prevGate;
118
- if (gateArrived)
119
- return "gate";
120
- if (nextStopReason === "failed" && prevStopReason !== "failed")
121
- return "paused";
122
- if (nextStopReason === "done" && prevStopReason !== "done")
123
- return "done";
124
- return null;
125
- }
126
- const CROSS_TAB_KEY = "ralphflow:notif-cross-tab";
127
- const CROSS_TAB_WINDOW_MS = 6000;
128
- /**
129
- * 跨标签页去重:同一 runId:kind 在短窗口内只提醒一次。每个标签页各自跑全局
130
- * 监视(notify-watch),两个标签都会算出同一事件——用 localStorage 时间戳
131
- * 协调,桌面通知只响一次。窗口内键淘汰,防无限增长。
132
- */
133
- function crossTabSeen(key) {
134
- try {
135
- const raw = window.localStorage.getItem(CROSS_TAB_KEY);
136
- const map = raw ? JSON.parse(raw) : {};
137
- const now = Date.now();
138
- for (const k of Object.keys(map)) {
139
- if (now - map[k] > 60_000)
140
- delete map[k];
141
- }
142
- if (map[key] && now - map[key] < CROSS_TAB_WINDOW_MS)
143
- return true;
144
- map[key] = now;
145
- window.localStorage.setItem(CROSS_TAB_KEY, JSON.stringify(map));
146
- return false;
147
- }
148
- catch {
149
- return false;
150
- }
151
- }
152
- /** UI 副作用入口:去重后发系统通知 + 标题闪烁 */
153
- export function notifyRunEvent(runId, workflow, kind) {
154
- try {
155
- const key = `${runId || "?"}:${kind}`;
156
- if (notifiedSet().has(key))
157
- return;
158
- // 跨标签页协调(同一浏览器多个 tab 都开着的场景)
159
- if (crossTabSeen(key))
160
- return;
161
- markNotified(key);
162
- const label = kind === "gate" ? "待你审查" : kind === "paused" ? "已暂停,需要处理" : "已完成 ✓";
163
- const title = `🌀 Ralph Flow · ${workflow || "工作流"} ${label}`;
164
- systemNotify(title, kind === "gate" ? "点击回到对话处理审查门。" : "点击回到对话查看详情。", key);
165
- flashTitle(`Ralph Flow · ${label}`);
166
- }
167
- catch { }
168
- }
@@ -1,8 +0,0 @@
1
- /** 精简一份 run state:长文本截断,去掉逐票 reasoning(体积大头) */
2
- export declare function compactRunState(data: Record<string, unknown>): Record<string, unknown>;
3
- /** RunCard 状态变化时调用(写侧) */
4
- export declare function saveRunSnapshot(data: Record<string, unknown>): void;
5
- /** collectRuns 的补盲源(读侧)。返回 runId → state 映射(1s 节流 + 24h 过期) */
6
- export declare function loadRunSnapshots(): Record<string, Record<string, unknown>>;
7
- /** 抽屉读快照时要跳过被用户手动关闭的 run */
8
- export declare function isRunHiddenSnap(runId: string): boolean;
@@ -1,101 +0,0 @@
1
- /**
2
- * Ralph Flow for dsh — client 端 run 状态快照(刷新失明的对策)
3
- *
4
- * 宿主的 session 持久化只保留「模型面」事件(message/tool/turn/step),
5
- * 插件经 session.append 发送的自定义帧(tool-ralphflow/*,连宿主自己的
6
- * command/run 也一样)不落盘——实时帧可达 client,但刷新页面后折叠器无帧
7
- * 可重放,卡片全部消失、页头计数归零(「刷新即瞎」)。
8
- *
9
- * 对策:RunCard 每次状态变化把精简快照写 localStorage;HeaderAction 的
10
- * collectRuns 以 chat.nodes 为主源、快照补盲——刷新后抽屉仍能看到实例
11
- * 列表与状态(对话流内嵌卡受框架限制暂不恢复,定位按钮降级为 status 查询)。
12
- */
13
- const KEY = "ralphflow:run-snapshots";
14
- const MAX_SNAPS = 20;
15
- /** 单快照里长文本的截断上限(reasoning/失败原因可能上万字符) */
16
- const MAX_TEXT = 1500;
17
- /** 快照有效期:超龄的不再进抽屉(状态早已失真) */
18
- const MAX_AGE_MS = 24 * 60 * 60 * 1000;
19
- /** selector 高频执行——读结果做 1s 节流缓存 */
20
- let cacheAt = 0;
21
- let cacheValue = {};
22
- function loadAll() {
23
- try {
24
- const raw = localStorage.getItem(KEY);
25
- return raw ? JSON.parse(raw) : {};
26
- }
27
- catch {
28
- return {};
29
- }
30
- }
31
- function persist(all) {
32
- try {
33
- // 按 savedAt 淘汰最旧的,控制总量
34
- const entries = Object.entries(all)
35
- .sort((a, b) => b[1].savedAt - a[1].savedAt)
36
- .slice(0, MAX_SNAPS);
37
- localStorage.setItem(KEY, JSON.stringify(Object.fromEntries(entries)));
38
- }
39
- catch { }
40
- }
41
- /** 精简一份 run state:长文本截断,去掉逐票 reasoning(体积大头) */
42
- export function compactRunState(data) {
43
- const clip = (v) => typeof v === "string" && v.length > MAX_TEXT ? `${v.slice(0, MAX_TEXT)}…` : v;
44
- const verdicts = Array.isArray(data.verdicts)
45
- ? data.verdicts.map((v) => ({ ...v, reasoning: clip(v.reasoning) }))
46
- : data.verdicts;
47
- const lastCheck = data.lastCheck && typeof data.lastCheck === "object"
48
- ? { ...data.lastCheck, reason: clip(data.lastCheck.reason) }
49
- : data.lastCheck;
50
- const report = data.report && typeof data.report === "object"
51
- ? { ...data.report, text: clip(data.report.text) }
52
- : data.report;
53
- return { ...data, verdicts, lastCheck, report };
54
- }
55
- /** RunCard 状态变化时调用(写侧) */
56
- export function saveRunSnapshot(data) {
57
- try {
58
- const runId = String(data?.runId ?? "");
59
- if (!runId)
60
- return;
61
- const all = loadAll();
62
- all[runId] = { savedAt: Date.now(), state: compactRunState(data) };
63
- persist(all);
64
- cacheAt = 0; // 写后失效读缓存
65
- }
66
- catch { }
67
- }
68
- /** collectRuns 的补盲源(读侧)。返回 runId → state 映射(1s 节流 + 24h 过期) */
69
- export function loadRunSnapshots() {
70
- try {
71
- const now = Date.now();
72
- if (now - cacheAt < 1000)
73
- return cacheValue;
74
- const all = loadAll();
75
- const out = {};
76
- for (const [runId, rec] of Object.entries(all)) {
77
- if (!rec?.state)
78
- continue;
79
- if (now - (rec.savedAt || 0) > MAX_AGE_MS)
80
- continue;
81
- out[runId] = rec.state;
82
- }
83
- cacheAt = now;
84
- cacheValue = out;
85
- return out;
86
- }
87
- catch {
88
- return {};
89
- }
90
- }
91
- /** 抽屉读快照时要跳过被用户手动关闭的 run */
92
- export function isRunHiddenSnap(runId) {
93
- try {
94
- const raw = localStorage.getItem("ralphflow:hidden-runs");
95
- const set = raw ? JSON.parse(raw) : [];
96
- return set.includes(runId);
97
- }
98
- catch {
99
- return false;
100
- }
101
- }