workbuddy-cli 1.1.0 → 1.2.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/README.md +154 -128
- package/README.zh-CN.md +227 -0
- package/bin/acp-server.mjs +739 -0
- package/bin/sessions.mjs +92 -0
- package/bin/workbuddy.mjs +451 -66
- package/package.json +2 -2
package/bin/sessions.mjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
export function cliHome() {
|
|
7
|
+
return process.env.WORKBUDDY_CLI_HOME || path.join(os.homedir(), '.workbuddy-cli');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function authFilePath() {
|
|
11
|
+
return path.join(cliHome(), 'auth.json');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function sessionsDir() {
|
|
15
|
+
return path.join(cliHome(), 'sessions');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createSessionId() {
|
|
19
|
+
return `wb_${crypto.randomBytes(12).toString('hex')}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function assertSessionId(id) {
|
|
23
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_.:-]{0,80}$/.test(id)) {
|
|
24
|
+
throw new Error(`非法会话 id: ${id}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function sessionPath(id) {
|
|
29
|
+
assertSessionId(id);
|
|
30
|
+
return path.join(sessionsDir(), `${id}.json`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sameCwd(a, b) {
|
|
34
|
+
if (!a || !b) return false;
|
|
35
|
+
try {
|
|
36
|
+
return fs.realpathSync(a) === fs.realpathSync(b);
|
|
37
|
+
} catch {
|
|
38
|
+
return path.resolve(a) === path.resolve(b);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function readSessionFile(file) {
|
|
43
|
+
try {
|
|
44
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
45
|
+
if (!data || typeof data.id !== 'string' || !Array.isArray(data.messages)) return null;
|
|
46
|
+
return data;
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function listSessions() {
|
|
53
|
+
const dir = sessionsDir();
|
|
54
|
+
if (!fs.existsSync(dir)) return [];
|
|
55
|
+
const sessions = [];
|
|
56
|
+
for (const name of fs.readdirSync(dir)) {
|
|
57
|
+
if (!name.endsWith('.json')) continue;
|
|
58
|
+
const session = readSessionFile(path.join(dir, name));
|
|
59
|
+
if (session) sessions.push(session);
|
|
60
|
+
}
|
|
61
|
+
sessions.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));
|
|
62
|
+
return sessions;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function loadSession(idOrPrefix) {
|
|
66
|
+
const id = String(idOrPrefix || '').trim();
|
|
67
|
+
if (!id) return null;
|
|
68
|
+
const exact = sessionPath(id);
|
|
69
|
+
if (fs.existsSync(exact)) return readSessionFile(exact);
|
|
70
|
+
|
|
71
|
+
const matches = listSessions().filter(session => session.id.startsWith(id));
|
|
72
|
+
if (matches.length === 1) return matches[0];
|
|
73
|
+
if (matches.length > 1) {
|
|
74
|
+
const sample = matches.slice(0, 5).map(session => session.id).join(', ');
|
|
75
|
+
throw new Error(`会话前缀 "${id}" 匹配到 ${matches.length} 条(${sample}),请给出更长的 id`);
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function latestSession(cwd) {
|
|
81
|
+
return listSessions().find(session => sameCwd(session.cwd, cwd)) || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function saveSession(session) {
|
|
85
|
+
const dir = sessionsDir();
|
|
86
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
87
|
+
const target = sessionPath(session.id);
|
|
88
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
89
|
+
fs.writeFileSync(tmp, JSON.stringify(session, null, 2) + '\n', { mode: 0o600 });
|
|
90
|
+
fs.renameSync(tmp, target);
|
|
91
|
+
return session;
|
|
92
|
+
}
|