@cloud411716/fancy-webnovel 4.12.1 → 4.13.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.md +11 -0
- package/lib/command-recovery.js +217 -0
- package/lib/material-db.js +8 -4
- package/lib/task-list.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -68,3 +68,14 @@ allowBuilds:
|
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
具体用法见各命令的 `/fancy-* help` 和 `docs/`。
|
|
71
|
+
|
|
72
|
+
## 命令中断恢复
|
|
73
|
+
|
|
74
|
+
DSH 在 `/fancy-*` 命令执行期间重启或异常退出时,命令可能来不及写入配对的 `command/done`。会话重新打开时,插件会做一次收尾:
|
|
75
|
+
|
|
76
|
+
- 给孤立的 `/fancy-*` 命令补一条 error 类型的 `command/done`,命令卡片会打印“DSH 重启或异常退出,命令已中断”,不再一直显示“执行中…”;
|
|
77
|
+
- 把遗留任务列表里 `in_progress` 的步骤改为 `pending`,停止转圈;
|
|
78
|
+
- 如果之后已经有新的 LLM 回合或新命令,不复活旧任务列表;
|
|
79
|
+
- 只处理 fancy-webnovel 自己的命令,不碰其他插件。
|
|
80
|
+
|
|
81
|
+
这不是伪造成功:被中断的命令仍然需要重新运行。
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* command-recovery.js — 收尾 DSH 重启/异常退出遗留的命令生命周期。
|
|
3
|
+
*
|
|
4
|
+
* DSH 的 slash 命令用一对事件记录执行过程:
|
|
5
|
+
*
|
|
6
|
+
* command/run → 处理函数开始时追加
|
|
7
|
+
* command/done → 处理函数结束时追加
|
|
8
|
+
*
|
|
9
|
+
* 如果 DSH 在命令执行期间被重启或异常退出,第二条事件可能永远没有写入。
|
|
10
|
+
* 会话重新打开时 UI 只能看到孤立的 command/run,于是命令卡片会一直显示
|
|
11
|
+
* “执行中…”;同样,命令最后一次 todo/write 里 in_progress 的步骤也会一直
|
|
12
|
+
* 转圈。
|
|
13
|
+
*
|
|
14
|
+
* 这里在会话恢复时做一次插件侧收尾:
|
|
15
|
+
*
|
|
16
|
+
* - 为 fancy-webnovel 命令的孤立 command/run 补一条 command/done(error);
|
|
17
|
+
* - 把遗留任务列表里的 in_progress 改成 pending,避免继续转圈;
|
|
18
|
+
* - 不伪造成功,命令卡片会明确打印“DSH 重启或异常退出,命令已中断”。
|
|
19
|
+
*
|
|
20
|
+
* 只处理 fancy-webnovel 自己的命令,不碰其它插件的事件。
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const INSTALLED_ROOTS = new WeakSet();
|
|
24
|
+
|
|
25
|
+
/** 已通过 registerCommandWithTasks 注册的命令名。 */
|
|
26
|
+
const MANAGED_COMMANDS = new Set();
|
|
27
|
+
|
|
28
|
+
/** 记录一个 fancy-webnovel 命令名。 */
|
|
29
|
+
export function registerManagedCommand(name) {
|
|
30
|
+
const text = String(name == null ? '' : name).trim();
|
|
31
|
+
if (text) MANAGED_COMMANDS.add(text);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isManagedCommand(name) {
|
|
35
|
+
return typeof name === 'string'
|
|
36
|
+
&& (MANAGED_COMMANDS.has(name) || name.startsWith('fancy-'));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 找出会话里没有配对 command/done 的 fancy-webnovel command/run。
|
|
41
|
+
*
|
|
42
|
+
* @param {readonly object[]} events 会话事件快照
|
|
43
|
+
* @param {number} [inheritedEventCount] fork 继承前缀长度
|
|
44
|
+
* @returns {{commandId: string, name: string, seq: number}[]}
|
|
45
|
+
*/
|
|
46
|
+
export function findInterruptedCommands(events, inheritedEventCount = 0) {
|
|
47
|
+
const inherited = Number.isInteger(inheritedEventCount) ? inheritedEventCount : 0;
|
|
48
|
+
const runs = new Map();
|
|
49
|
+
const settled = new Set();
|
|
50
|
+
for (const event of Array.isArray(events) ? events : []) {
|
|
51
|
+
if (!event || typeof event !== 'object') continue;
|
|
52
|
+
const seq = Number(event.seq);
|
|
53
|
+
if (Number.isInteger(seq) && seq < inherited) continue;
|
|
54
|
+
const data = event.data;
|
|
55
|
+
if (!data || typeof data !== 'object') continue;
|
|
56
|
+
const commandId = typeof data.commandId === 'string' ? data.commandId : '';
|
|
57
|
+
if (!commandId) continue;
|
|
58
|
+
if (event.type === 'command/run') {
|
|
59
|
+
runs.set(commandId, {
|
|
60
|
+
commandId,
|
|
61
|
+
name: typeof data.name === 'string' ? data.name : '',
|
|
62
|
+
seq: Number.isInteger(seq) ? seq : 0,
|
|
63
|
+
});
|
|
64
|
+
} else if (event.type === 'command/done') {
|
|
65
|
+
settled.add(commandId);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return [...runs.values()]
|
|
69
|
+
.filter((run) => !settled.has(run.commandId) && isManagedCommand(run.name))
|
|
70
|
+
.sort((left, right) => left.seq - right.seq);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function lastEvent(events, type, afterSeq, inheritedEventCount) {
|
|
74
|
+
let found = null;
|
|
75
|
+
for (const event of Array.isArray(events) ? events : []) {
|
|
76
|
+
if (!event || event.type !== type) continue;
|
|
77
|
+
const seq = Number(event.seq);
|
|
78
|
+
if (Number.isInteger(seq) && seq < inheritedEventCount) continue;
|
|
79
|
+
if (Number.isInteger(afterSeq) && (!Number.isInteger(seq) || seq <= afterSeq)) continue;
|
|
80
|
+
found = event;
|
|
81
|
+
}
|
|
82
|
+
return found;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function hasEventAfter(events, type, seq, inheritedEventCount) {
|
|
86
|
+
for (const event of Array.isArray(events) ? events : []) {
|
|
87
|
+
if (!event || event.type !== type) continue;
|
|
88
|
+
const current = Number(event.seq);
|
|
89
|
+
if (!Number.isInteger(current) || current < inheritedEventCount) continue;
|
|
90
|
+
if (current > seq) return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function todoSnapshot(event) {
|
|
96
|
+
const todos = event && event.data && Array.isArray(event.data.todos) ? event.data.todos : [];
|
|
97
|
+
const clean = [];
|
|
98
|
+
for (const item of todos) {
|
|
99
|
+
if (!item || typeof item !== 'object') continue;
|
|
100
|
+
const content = String(item.content == null ? '' : item.content).trim();
|
|
101
|
+
if (!content) continue;
|
|
102
|
+
if (item.status !== 'pending' && item.status !== 'in_progress' && item.status !== 'completed') continue;
|
|
103
|
+
clean.push({ content, status: item.status });
|
|
104
|
+
}
|
|
105
|
+
return clean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function interruptedTodo(event) {
|
|
109
|
+
const todos = todoSnapshot(event);
|
|
110
|
+
if (!todos.some((item) => item.status === 'in_progress')) return null;
|
|
111
|
+
return todos.map((item) => item.status === 'in_progress'
|
|
112
|
+
? { content: item.content, status: 'pending' }
|
|
113
|
+
: item);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 收尾一个已恢复会话中的中断命令。
|
|
118
|
+
*
|
|
119
|
+
* 调用方应在 session/created 的同一同步栈里先取好 findAll 结果,再把这个
|
|
120
|
+
* 函数放到微任务里执行;DSH 在 session/created 派发完成前不允许 append。
|
|
121
|
+
*
|
|
122
|
+
* @param {object} session 已恢复的 DSH session
|
|
123
|
+
* @param {{orphans?: object[], inheritedEventCount?: number}} [options]
|
|
124
|
+
* @returns {{commands: string[], todo: boolean}}
|
|
125
|
+
*/
|
|
126
|
+
export function recoverInterruptedSession(session, options = {}) {
|
|
127
|
+
const result = { commands: [], todo: false };
|
|
128
|
+
if (!session || typeof session.append !== 'function' || typeof session.snapshotEvents !== 'function') {
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
const inherited = Number.isInteger(options.inheritedEventCount)
|
|
132
|
+
? options.inheritedEventCount
|
|
133
|
+
: (Number.isInteger(session.inheritedEventCount) ? session.inheritedEventCount : 0);
|
|
134
|
+
let events;
|
|
135
|
+
try {
|
|
136
|
+
events = session.snapshotEvents();
|
|
137
|
+
} catch {
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
const orphans = Array.isArray(options.orphans)
|
|
141
|
+
? options.orphans
|
|
142
|
+
: findInterruptedCommands(events, inherited);
|
|
143
|
+
const settled = new Set();
|
|
144
|
+
for (const event of events) {
|
|
145
|
+
if (event && event.type === 'command/done' && event.data && typeof event.data.commandId === 'string') {
|
|
146
|
+
settled.add(event.data.commandId);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let latestOrphanSeq = -1;
|
|
151
|
+
for (const run of orphans) {
|
|
152
|
+
if (!run || typeof run.commandId !== 'string' || settled.has(run.commandId)) continue;
|
|
153
|
+
const name = typeof run.name === 'string' && run.name ? run.name : 'fancy-webnovel 命令';
|
|
154
|
+
const seq = Number.isInteger(run.seq) ? run.seq : 0;
|
|
155
|
+
latestOrphanSeq = Math.max(latestOrphanSeq, seq);
|
|
156
|
+
try {
|
|
157
|
+
session.append('command/done', {
|
|
158
|
+
commandId: run.commandId,
|
|
159
|
+
kind: 'error',
|
|
160
|
+
text: 'DSH 在 /' + name + ' 执行期间重启或异常退出,命令已中断。请重新运行 /' + name + '。',
|
|
161
|
+
});
|
|
162
|
+
result.commands.push(run.commandId);
|
|
163
|
+
} catch {
|
|
164
|
+
// 会话可能已经再次卸载;恢复失败不能反过来影响打开会话。
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (latestOrphanSeq < 0) return result;
|
|
169
|
+
|
|
170
|
+
// 只修复仍然停留在界面上的最后一份任务列表:
|
|
171
|
+
// - 它必须写在中止命令之后;
|
|
172
|
+
// - 之后不能再有新的 turn/start(turn 开始会清空旧列表);
|
|
173
|
+
// - 之后不能再有新的 command/run(否则可能属于另一条正在执行的命令)。
|
|
174
|
+
const todoEvent = lastEvent(events, 'todo/write', latestOrphanSeq, inherited);
|
|
175
|
+
if (!todoEvent || !Number.isInteger(Number(todoEvent.seq))) return result;
|
|
176
|
+
const todoSeq = Number(todoEvent.seq);
|
|
177
|
+
if (hasEventAfter(events, 'turn/start', todoSeq, inherited)) return result;
|
|
178
|
+
if (hasEventAfter(events, 'command/run', todoSeq, inherited)) return result;
|
|
179
|
+
const todos = interruptedTodo(todoEvent);
|
|
180
|
+
if (!todos) return result;
|
|
181
|
+
try {
|
|
182
|
+
session.append('todo/write', { todos });
|
|
183
|
+
result.todo = true;
|
|
184
|
+
} catch {
|
|
185
|
+
// 同上:任务列表只是展示,恢复失败不影响会话打开。
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 在每个 fancy-webnovel 插件注册命令时调用;整个进程只安装一次监听。
|
|
192
|
+
*
|
|
193
|
+
* @param {object} ctx DSH 插件上下文
|
|
194
|
+
*/
|
|
195
|
+
export function installCommandRecovery(ctx) {
|
|
196
|
+
const root = ctx && ctx.root ? ctx.root : ctx;
|
|
197
|
+
if (!root || typeof root.on !== 'function' || INSTALLED_ROOTS.has(root)) return;
|
|
198
|
+
INSTALLED_ROOTS.add(root);
|
|
199
|
+
root.on('session/created', (session) => {
|
|
200
|
+
let events;
|
|
201
|
+
try {
|
|
202
|
+
events = session && typeof session.snapshotEvents === 'function'
|
|
203
|
+
? session.snapshotEvents()
|
|
204
|
+
: null;
|
|
205
|
+
} catch {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (!Array.isArray(events)) return;
|
|
209
|
+
const inherited = Number.isInteger(session.inheritedEventCount) ? session.inheritedEventCount : 0;
|
|
210
|
+
const orphans = findInterruptedCommands(events, inherited);
|
|
211
|
+
if (!orphans.length) return;
|
|
212
|
+
// session/created 派发完成前 DSH 拒绝 append;派发结束后再收尾。
|
|
213
|
+
queueMicrotask(() => {
|
|
214
|
+
recoverInterruptedSession(session, { orphans, inheritedEventCount: inherited });
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
package/lib/material-db.js
CHANGED
|
@@ -42,12 +42,16 @@ export async function connectMaterial(root) {
|
|
|
42
42
|
await startServer(root);
|
|
43
43
|
const workspace = readWorkspace(root);
|
|
44
44
|
const secrets = readSecrets(root);
|
|
45
|
-
const
|
|
46
|
-
await db.connect(rpcUrl(workspace));
|
|
47
|
-
await db.signin({
|
|
45
|
+
const authentication = {
|
|
48
46
|
username: secrets.surreal?.username || 'root',
|
|
49
47
|
password: secrets.surreal?.password,
|
|
50
|
-
}
|
|
48
|
+
};
|
|
49
|
+
const db = new Surreal({ engines: { ...createRemoteEngines() } });
|
|
50
|
+
// Pass credentials as the connection auth provider, not as a one-off signin.
|
|
51
|
+
// SurrealDB tokens have a one-hour TTL; a job-store connection kept open
|
|
52
|
+
// across a long douyin crawl must be renewed automatically or later writes
|
|
53
|
+
// fail with "IAM error: Not enough permissions".
|
|
54
|
+
await db.connect(rpcUrl(workspace), { authentication });
|
|
51
55
|
const namespace = workspace.surreal?.namespace || 'fancy';
|
|
52
56
|
const database = materialDatabaseName(workspace);
|
|
53
57
|
await db.use({ namespace, database });
|
package/lib/task-list.js
CHANGED
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
* 保持 pending,不谎报完成。
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
+
import { installCommandRecovery, registerManagedCommand } from './command-recovery.js';
|
|
29
|
+
|
|
28
30
|
const VALID_STATUSES = new Set(['pending', 'in_progress', 'completed']);
|
|
29
31
|
const WATCH_INTERVAL_MS = 200;
|
|
30
32
|
|
|
@@ -372,6 +374,8 @@ export function registerCommandWithTasks(ctx, definition, titles) {
|
|
|
372
374
|
if (typeof original !== 'function') {
|
|
373
375
|
throw new Error('registerCommandWithTasks: definition.handler 必须是函数');
|
|
374
376
|
}
|
|
377
|
+
registerManagedCommand(definition.name);
|
|
378
|
+
installCommandRecovery(ctx);
|
|
375
379
|
return ctx.commands.register({
|
|
376
380
|
...definition,
|
|
377
381
|
handler: (invocation) => withTaskList(
|