dsh-taskflow 0.0.0-stage → 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/CHANGELOG.md +26 -0
- package/LICENSE +201 -0
- package/NOTICE +17 -0
- package/README.md +169 -3
- package/assets/icon.png +0 -0
- package/assets/icon.svg +1 -0
- package/client.js +312 -0
- package/cordis.patch.yml +8 -0
- package/docs/DETAIL.md +51 -0
- package/docs/DETAIL.zh-CN.md +36 -0
- package/index.js +1 -0
- package/licenses/DeepSeek-MIT.txt +20 -0
- package/licenses/Montserrat-OFL.txt +94 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +133 -6
- package/src/application/task-runtime.mjs +114 -0
- package/src/domain/task-service.mjs +178 -0
- package/src/server/config.mjs +8 -0
- package/src/server/index.mjs +38 -0
- package/src/server/notifications.mjs +19 -0
- package/src/server/prompts.mjs +8 -0
- package/src/server/rpc.mjs +22 -0
- package/src/server/session-context.mjs +17 -0
- package/src/server/tool-views.mjs +14 -0
- package/src/server/tools.mjs +28 -0
- package/src/storage/task-store.mjs +59 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { mkdir, readFile, open, rename, unlink } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
|
|
5
|
+
export const emptyState = () => ({ schema: 1, revision: 0, plans: {}, sessions: {}, outbox: [] });
|
|
6
|
+
|
|
7
|
+
/** Single writer, fsync before rename, publish only after durable commit. */
|
|
8
|
+
export class FileStore {
|
|
9
|
+
constructor(directory) { this.directory = directory; this.path = join(directory, 'taskflow.json'); this.tail = Promise.resolve(); this.state = emptyState(); }
|
|
10
|
+
async init() {
|
|
11
|
+
await mkdir(this.directory, { recursive: true });
|
|
12
|
+
this.lockPath = join(this.directory, 'writer.lock');
|
|
13
|
+
try { this.lock = await open(this.lockPath, 'wx'); }
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code !== 'EEXIST') throw error;
|
|
16
|
+
const pid = Number(await readFile(this.lockPath, 'utf8'));
|
|
17
|
+
if (!Number.isSafeInteger(pid) || pid < 1) throw new Error('Taskflow writer lock is invalid; inspect it before recovery');
|
|
18
|
+
try { process.kill(pid, 0); throw new Error('Taskflow data directory already has an active writer'); }
|
|
19
|
+
catch (e) { if (e.code !== 'ESRCH') throw e; }
|
|
20
|
+
await unlink(this.lockPath); this.lock = await open(this.lockPath, 'wx');
|
|
21
|
+
}
|
|
22
|
+
await this.lock.writeFile(String(process.pid));
|
|
23
|
+
try {
|
|
24
|
+
try { this.state = JSON.parse(await readFile(this.path, 'utf8')); }
|
|
25
|
+
catch (error) { if (error.code !== 'ENOENT') throw new Error(`Cannot read Taskflow state; original file preserved: ${error.message}`); }
|
|
26
|
+
if (this.state.schema !== 1 || !this.state.plans || !this.state.sessions || !Array.isArray(this.state.outbox)) throw new Error('Unsupported Taskflow state schema; migration required');
|
|
27
|
+
return this;
|
|
28
|
+
} catch (error) { await this.close(); throw error; }
|
|
29
|
+
}
|
|
30
|
+
read() { return structuredClone(this.state); }
|
|
31
|
+
transaction(change) {
|
|
32
|
+
const pending = this.tail.then(async () => {
|
|
33
|
+
const next = this.read();
|
|
34
|
+
const result = await change(next);
|
|
35
|
+
next.revision++;
|
|
36
|
+
const temp = `${this.path}.${randomUUID()}.tmp`;
|
|
37
|
+
let handle;
|
|
38
|
+
try {
|
|
39
|
+
handle = await open(temp, 'wx'); await handle.writeFile(JSON.stringify(next)); await handle.sync(); await handle.close(); handle = undefined;
|
|
40
|
+
await rename(temp, this.path);
|
|
41
|
+
} catch (error) { await handle?.close(); await unlink(temp).catch(() => {}); throw error; }
|
|
42
|
+
this.state = next;
|
|
43
|
+
return result;
|
|
44
|
+
});
|
|
45
|
+
this.tail = pending.catch(() => {});
|
|
46
|
+
return pending;
|
|
47
|
+
}
|
|
48
|
+
async close() { await this.tail; if (this.lock) { await this.lock.close(); this.lock = undefined; await unlink(this.lockPath); } }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class MemoryStore {
|
|
52
|
+
constructor(state = emptyState()) { this.state = structuredClone(state); this.tail = Promise.resolve(); }
|
|
53
|
+
read() { return structuredClone(this.state); }
|
|
54
|
+
transaction(change) {
|
|
55
|
+
const pending = this.tail.then(async () => { const next = this.read(); const result = await change(next); next.revision++; this.state = next; return result; });
|
|
56
|
+
this.tail = pending.catch(() => {}); return pending;
|
|
57
|
+
}
|
|
58
|
+
async close() { await this.tail; }
|
|
59
|
+
}
|