@wwwfz/dsh-agent-bridge 0.8.2 → 0.8.3
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 +194 -184
- package/cordis.patch.yml +4 -4
- package/package.json +40 -40
- package/src/assets.js +348 -348
- package/src/catalog.js +605 -605
- package/src/client.js +310 -310
- package/src/config.js +88 -88
- package/src/export.js +12 -12
- package/src/index.js +136 -136
- package/src/project-archive.js +145 -145
- package/src/projects.js +980 -980
- package/src/session-import.js +93 -93
- package/src/watch.js +309 -309
- package/web/Geist-OFL.txt +93 -93
- package/web/agent-icons/NOTICE.md +19 -19
- package/web/agent-icons/antigravity.svg +76 -76
- package/web/agent-icons/claude.svg +3 -3
- package/web/agent-icons/dsh.svg +58 -58
- package/web/agent-icons/licenses/copilot.txt +21 -21
- package/web/agent-icons/licenses/dsh.txt +21 -21
- package/web/agent-icons/licenses/gemini.txt +201 -201
- package/web/agent-icons/licenses/opencode.txt +21 -21
- package/web/agent-icons/licenses/qoder-notice.txt +18 -18
- package/web/agent-icons/licenses/qoder.txt +22 -22
- package/web/agent-icons/licenses/qwen.txt +202 -202
- package/web/agent-icons/qoder.svg +4 -4
- package/web/agent-icons/qwen.svg +5 -5
- package/web/agent-icons/workbuddy.svg +29 -29
- package/web/panel.css +502 -502
- package/web/panel.html +246 -246
- package/web/panel.js +1559 -1559
- package/web/project-context.js +25 -25
package/src/watch.js
CHANGED
|
@@ -1,309 +1,309 @@
|
|
|
1
|
-
import { watch } from 'node:fs';
|
|
2
|
-
import { readdir, realpath, stat } from 'node:fs/promises';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
const maxWatchers = 224;
|
|
6
|
-
const maxSelected = 5;
|
|
7
|
-
const maxRootsPerAgent = 12;
|
|
8
|
-
const planLifetime = 30_000;
|
|
9
|
-
const note = /\.(?:md|markdown)$/i;
|
|
10
|
-
const transcript = /\.jsonl(?:\.zst)?$/i;
|
|
11
|
-
const skill = /^skill\.md$/i;
|
|
12
|
-
const projectFolders = [
|
|
13
|
-
'.agent', '.agents', '.claude', '.codex', '.dsh', '.gemini', '.opencode',
|
|
14
|
-
'.qoder', '.qwen', '.trae', '.workbuddy',
|
|
15
|
-
];
|
|
16
|
-
const hubFolders = ['决策', '经验', '资料', '交接', '笔记'];
|
|
17
|
-
const aliases = {
|
|
18
|
-
'claude-code': 'claude', 'codex-cli': 'codex', 'open-code': 'opencode', 'qoder-cn': 'qoder',
|
|
19
|
-
'qwen-code': 'qwen', 'trae-cn': 'trae', 'trae-solo': 'trae', 'gemini-cli': 'gemini',
|
|
20
|
-
'antigravity-cli': 'antigravity', vscode: 'copilot', 'vscode-insiders': 'copilot',
|
|
21
|
-
'vscode-copilot': 'copilot', vscodium: 'copilot', 'code-oss': 'copilot',
|
|
22
|
-
};
|
|
23
|
-
const all = () => true;
|
|
24
|
-
const matches = pattern => name => pattern.test(name);
|
|
25
|
-
const named = (...values) => {
|
|
26
|
-
const names = new Set(values.map(value => value.toLowerCase()));
|
|
27
|
-
return name => names.has(name.toLowerCase());
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
function fileKey(value) {
|
|
31
|
-
const normalized = path.normalize(value);
|
|
32
|
-
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function recentDirectories(directory, limit) {
|
|
36
|
-
const entries = await readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
37
|
-
const candidates = entries.filter(item => item.isDirectory() && !item.name.startsWith('.')).slice(0, 256);
|
|
38
|
-
const checked = await Promise.all(candidates.map(async item => {
|
|
39
|
-
const file = path.join(directory, item.name);
|
|
40
|
-
const info = await stat(file).catch(() => undefined);
|
|
41
|
-
return { file, modified: info?.mtimeMs ?? 0 };
|
|
42
|
-
}));
|
|
43
|
-
checked.sort((a, b) => b.modified - a.modified);
|
|
44
|
-
return checked.slice(0, limit).map(item => item.file);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function selectedProjects(entries) {
|
|
48
|
-
return entries.slice(0, maxSelected).map(item => typeof item === 'string' ? { cwd: item } : item)
|
|
49
|
-
.filter(item => typeof item?.cwd === 'string' && path.isAbsolute(item.cwd));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function sourceConfigPath(config) {
|
|
53
|
-
return config.configFile ?? config.file ?? process.env.DSH_AGENT_BRIDGE_CONFIG
|
|
54
|
-
?? (process.env.DSH_HOME ? path.join(process.env.DSH_HOME, 'agent-bridge.json')
|
|
55
|
-
: config.home ? path.join(config.home, '.dsh', 'agent-bridge.json') : undefined);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async function watchPlan(config, selected) {
|
|
59
|
-
const wanted = new Map();
|
|
60
|
-
function add(directory, matcher, priority = 1) {
|
|
61
|
-
if (typeof directory !== 'string' || !path.isAbsolute(directory)) return;
|
|
62
|
-
const key = fileKey(directory);
|
|
63
|
-
const current = wanted.get(key);
|
|
64
|
-
if (current) {
|
|
65
|
-
current.matchers.push(matcher);
|
|
66
|
-
current.priority = Math.min(current.priority, priority);
|
|
67
|
-
} else wanted.set(key, { directory, matchers: [matcher], priority });
|
|
68
|
-
}
|
|
69
|
-
function file(value, sqlite = false) {
|
|
70
|
-
if (typeof value !== 'string' || !path.isAbsolute(value)) return;
|
|
71
|
-
const name = path.basename(value);
|
|
72
|
-
add(path.dirname(value), sqlite ? named(name, `${name}-wal`, `${name}-journal`) : named(name), 0);
|
|
73
|
-
}
|
|
74
|
-
async function skills(root, priority) {
|
|
75
|
-
add(root, all, priority);
|
|
76
|
-
const children = await recentDirectories(root, 20);
|
|
77
|
-
for (const child of children) add(child, matches(skill), priority + 1);
|
|
78
|
-
const system = path.join(root, '.system');
|
|
79
|
-
add(system, all, priority + 1);
|
|
80
|
-
for (const child of await recentDirectories(system, 8)) add(child, matches(skill), priority + 1);
|
|
81
|
-
}
|
|
82
|
-
async function childTranscripts(root, extra = []) {
|
|
83
|
-
add(root, all);
|
|
84
|
-
for (const child of await recentDirectories(root, 8)) {
|
|
85
|
-
add(child, name => transcript.test(name) || name.endsWith('-session.json') || extra.includes(name), 2);
|
|
86
|
-
for (const name of extra) {
|
|
87
|
-
if (name === 'chats' || name === 'chatSessions') add(path.join(child, name), matches(/\.jsonl?$/i), 2);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function codexTranscripts(root) {
|
|
92
|
-
add(root, all);
|
|
93
|
-
const now = new Date();
|
|
94
|
-
const days = new Set();
|
|
95
|
-
for (const value of [now, new Date(now.valueOf() - 86_400_000)]) {
|
|
96
|
-
const local = [value.getFullYear(), value.getMonth() + 1, value.getDate()].map(item => String(item).padStart(2, '0')).join('/');
|
|
97
|
-
days.add(local);
|
|
98
|
-
days.add(value.toISOString().slice(0, 10).replaceAll('-', '/'));
|
|
99
|
-
}
|
|
100
|
-
for (const day of days) {
|
|
101
|
-
const directory = path.join(root, day);
|
|
102
|
-
add(path.dirname(directory), all, 2);
|
|
103
|
-
add(directory, matches(transcript), 2);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const jobs = [];
|
|
108
|
-
file(sourceConfigPath(config));
|
|
109
|
-
for (const archive of config.archiveFiles ?? []) file(archive, true);
|
|
110
|
-
for (const project of selected) {
|
|
111
|
-
const { cwd } = project;
|
|
112
|
-
add(cwd, name => /^(?:agents|claude|gemini|qwen|memory)\.md$/i.test(name) || name === '.agent-bridge' || projectFolders.includes(name), 0);
|
|
113
|
-
const bridge = path.join(cwd, '.agent-bridge');
|
|
114
|
-
add(bridge, named('memory', 'handoffs'), 0);
|
|
115
|
-
for (const folder of ['memory', 'handoffs']) add(path.join(bridge, folder), matches(note), 0);
|
|
116
|
-
for (const folder of projectFolders) {
|
|
117
|
-
const root = path.join(cwd, folder);
|
|
118
|
-
add(root, name => note.test(name) || name === 'memory' || name === 'skills', 0);
|
|
119
|
-
add(path.join(root, 'memory'), matches(note), 0);
|
|
120
|
-
jobs.push(skills(path.join(root, 'skills'), 0));
|
|
121
|
-
}
|
|
122
|
-
for (const [workspace, sources] of Object.entries(config.memoryExports ?? {})) {
|
|
123
|
-
if (fileKey(workspace) !== fileKey(cwd)) continue;
|
|
124
|
-
for (const source of sources) {
|
|
125
|
-
if (note.test(source)) file(source);
|
|
126
|
-
else add(source, matches(note), 0);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
for (const root of config.workspaceHubRoots ?? []) {
|
|
130
|
-
for (const id of (project.projectIds ?? []).filter(value => /^P\d{3,}$/.test(value)).slice(0, 8)) {
|
|
131
|
-
const folder = path.join(root, '项目档案', id);
|
|
132
|
-
add(folder, name => note.test(name) || name === 'project.yaml' || hubFolders.includes(name), 0);
|
|
133
|
-
for (const subfolder of hubFolders) add(path.join(folder, subfolder), matches(note), 0);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const root of config.workspaceHubRoots ?? []) {
|
|
138
|
-
const projects = path.basename(root) === '项目档案' ? root : path.join(root, '项目档案');
|
|
139
|
-
add(projects, matches(/^P\d{3,}$/), 0);
|
|
140
|
-
jobs.push(skills(path.join(root, 'skills', 'source'), 2));
|
|
141
|
-
}
|
|
142
|
-
for (const root of config.projectRoots ?? []) {
|
|
143
|
-
if (typeof root === 'string' && path.isAbsolute(root)) add(path.dirname(root), named(path.basename(root)), 0);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const globalSkills = [...(config.skillRoots ?? []), ...(config.opencodeSkillRoots ?? []).map(root => path.join(root, 'skills'))];
|
|
147
|
-
if (config.home) globalSkills.push(path.join(config.home, '.agents', 'skills'));
|
|
148
|
-
for (const root of globalSkills) jobs.push(skills(root, 2));
|
|
149
|
-
|
|
150
|
-
for (const [provider, roots] of Object.entries(config.agentRoots ?? {})) {
|
|
151
|
-
const agent = aliases[provider] ?? provider;
|
|
152
|
-
for (const root of roots.slice(0, maxRootsPerAgent)) {
|
|
153
|
-
if (agent === 'opencode' && (await stat(root).catch(() => undefined))?.isFile()) {
|
|
154
|
-
file(root, true);
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
add(root, name => /^(?:state_\d+\.sqlite(?:-wal|-journal)?|(?:opencode|conversation_summaries)\.db(?:-wal|-journal)?|history\.jsonl|session_index\.jsonl)$/i.test(name)
|
|
158
|
-
|| note.test(name) || ['projects', 'sessions', 'archived_sessions', 'workspaceStorage', 'User', 'tmp', 'skills', 'memory', 'memories', 'memories_v2', 'agent-memory', 'knowledge', 'qoder-knowledge', 'diary'].includes(name), 0);
|
|
159
|
-
|
|
160
|
-
if (agent === 'codex') {
|
|
161
|
-
const tail = path.basename(root).toLowerCase();
|
|
162
|
-
if (tail !== 'archived_sessions') codexTranscripts(tail === 'sessions' ? root : path.join(root, 'sessions'));
|
|
163
|
-
if (tail !== 'sessions') add(tail === 'archived_sessions' ? root : path.join(root, 'archived_sessions'), matches(transcript));
|
|
164
|
-
}
|
|
165
|
-
if (['claude', 'qoder', 'qwen', 'workbuddy'].includes(agent)) {
|
|
166
|
-
const projects = path.basename(root).toLowerCase() === 'projects' ? root : path.join(root, 'projects');
|
|
167
|
-
jobs.push(childTranscripts(projects));
|
|
168
|
-
for (const { cwd } of selected) {
|
|
169
|
-
const slug = cwd.replace(/[^\p{L}\p{N}]/gu, '-');
|
|
170
|
-
const asciiSlug = cwd.replace(/[^a-zA-Z0-9]/g, '-');
|
|
171
|
-
for (const name of new Set([slug, asciiSlug, slug.toLowerCase()])) {
|
|
172
|
-
const bucket = path.join(projects, name);
|
|
173
|
-
add(bucket, name => transcript.test(name) || name === 'memory', 0);
|
|
174
|
-
add(path.join(bucket, 'memory'), matches(note), 0);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (agent === 'gemini') jobs.push(childTranscripts(path.basename(root) === 'tmp' ? root : path.join(root, 'tmp'), ['chats', '.project_root']));
|
|
179
|
-
if (agent === 'opencode') {
|
|
180
|
-
file(path.join(root, 'opencode.db'), true);
|
|
181
|
-
file(path.join(root, 'opencode', 'opencode.db'), true);
|
|
182
|
-
}
|
|
183
|
-
if (agent === 'copilot' || agent === 'trae') {
|
|
184
|
-
const stores = path.basename(root).toLowerCase() === 'workspacestorage'
|
|
185
|
-
? [root] : [path.join(root, 'workspaceStorage'), path.join(root, 'User', 'workspaceStorage')];
|
|
186
|
-
for (const store of stores) jobs.push(childTranscripts(store, ['workspace.json', 'chatSessions']));
|
|
187
|
-
}
|
|
188
|
-
for (const folder of ['memory', 'memories', 'memories_v2', 'agent-memory', 'knowledge', 'qoder-knowledge', 'diary']) {
|
|
189
|
-
add(path.join(root, folder), matches(note), 2);
|
|
190
|
-
}
|
|
191
|
-
if (agent === 'codex') {
|
|
192
|
-
for (const folder of ['memories', 'memories_v2']) add(path.join(root, folder, 'extensions', 'ad_hoc', 'notes'), matches(note), 2);
|
|
193
|
-
}
|
|
194
|
-
jobs.push(skills(path.join(root, 'skills'), 2));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
await Promise.all(jobs);
|
|
198
|
-
return [...wanted.values()].sort((a, b) => a.priority - b.priority);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function watchTarget(entry) {
|
|
202
|
-
let { directory, matchers } = entry;
|
|
203
|
-
for (let level = 0; level < 4; level++) {
|
|
204
|
-
try {
|
|
205
|
-
const actual = await realpath(directory);
|
|
206
|
-
const info = await stat(actual, { bigint: true });
|
|
207
|
-
if (!info.isDirectory()) return undefined;
|
|
208
|
-
return { directory: actual, matchers, identity: `${info.dev}:${info.ino}` };
|
|
209
|
-
} catch (error) {
|
|
210
|
-
if (!['ENOENT', 'ENOTDIR'].includes(error.code)) return undefined;
|
|
211
|
-
const parent = path.dirname(directory);
|
|
212
|
-
if (parent === directory) return undefined;
|
|
213
|
-
matchers = [named(path.basename(directory))];
|
|
214
|
-
directory = parent;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return undefined;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function createSourceMonitor({ onChange, debounceMs = 300 } = {}) {
|
|
221
|
-
const watchers = new Map();
|
|
222
|
-
let disposed = false;
|
|
223
|
-
let dirty = true;
|
|
224
|
-
let nextPlan = 0;
|
|
225
|
-
let signature;
|
|
226
|
-
let generation = 0;
|
|
227
|
-
let changeTimer;
|
|
228
|
-
|
|
229
|
-
function signal(event) {
|
|
230
|
-
if (disposed) return;
|
|
231
|
-
if (event === 'rename') dirty = true;
|
|
232
|
-
if (changeTimer) return;
|
|
233
|
-
changeTimer = setTimeout(() => {
|
|
234
|
-
changeTimer = undefined;
|
|
235
|
-
if (!disposed) onChange?.();
|
|
236
|
-
}, debounceMs);
|
|
237
|
-
changeTimer.unref?.();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async function update(config = {}, projectDirectories = []) {
|
|
241
|
-
if (disposed) return;
|
|
242
|
-
const selected = selectedProjects(projectDirectories);
|
|
243
|
-
const current = JSON.stringify([config, selected]);
|
|
244
|
-
if (!dirty && signature === current && Date.now() < nextPlan) return;
|
|
245
|
-
const token = ++generation;
|
|
246
|
-
signature = current;
|
|
247
|
-
dirty = false;
|
|
248
|
-
nextPlan = Date.now() + planLifetime;
|
|
249
|
-
const plan = await watchPlan(config, selected);
|
|
250
|
-
const wanted = new Map();
|
|
251
|
-
for (let offset = 0; offset < plan.length && wanted.size < maxWatchers; offset += 48) {
|
|
252
|
-
const chunk = await Promise.all(plan.slice(offset, offset + 48).map(watchTarget));
|
|
253
|
-
for (const target of chunk) {
|
|
254
|
-
if (!target) continue;
|
|
255
|
-
const key = fileKey(target.directory);
|
|
256
|
-
const current = wanted.get(key);
|
|
257
|
-
if (current) current.matchers.push(...target.matchers);
|
|
258
|
-
else if (wanted.size < maxWatchers) wanted.set(key, target);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (disposed || token !== generation) return;
|
|
262
|
-
for (const [key, entry] of watchers) {
|
|
263
|
-
if (wanted.has(key)) continue;
|
|
264
|
-
entry.watcher.close();
|
|
265
|
-
watchers.delete(key);
|
|
266
|
-
}
|
|
267
|
-
for (const [key, target] of wanted) {
|
|
268
|
-
const current = watchers.get(key);
|
|
269
|
-
if (current?.identity === target.identity) {
|
|
270
|
-
current.matchers = target.matchers;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
if (current) {
|
|
274
|
-
watchers.delete(key);
|
|
275
|
-
current.watcher.close();
|
|
276
|
-
}
|
|
277
|
-
try {
|
|
278
|
-
const entry = { matchers: target.matchers, identity: target.identity };
|
|
279
|
-
const watcher = watch(target.directory, { persistent: false }, (event, filename) => {
|
|
280
|
-
if (!filename || entry.matchers.some(matcher => matcher(path.basename(String(filename))))) signal(event);
|
|
281
|
-
});
|
|
282
|
-
entry.watcher = watcher;
|
|
283
|
-
watcher.on('error', () => {
|
|
284
|
-
if (watchers.get(key) !== entry) return;
|
|
285
|
-
watcher.close();
|
|
286
|
-
watchers.delete(key);
|
|
287
|
-
dirty = true;
|
|
288
|
-
});
|
|
289
|
-
watcher.on('close', () => {
|
|
290
|
-
if (watchers.get(key) !== entry) return;
|
|
291
|
-
watchers.delete(key);
|
|
292
|
-
dirty = true;
|
|
293
|
-
});
|
|
294
|
-
watchers.set(key, entry);
|
|
295
|
-
} catch {
|
|
296
|
-
// Unsupported filesystems remain covered by the caller's periodic polling.
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function dispose() {
|
|
302
|
-
disposed = true;
|
|
303
|
-
clearTimeout(changeTimer);
|
|
304
|
-
for (const { watcher } of watchers.values()) watcher.close();
|
|
305
|
-
watchers.clear();
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return { update, dispose };
|
|
309
|
-
}
|
|
1
|
+
import { watch } from 'node:fs';
|
|
2
|
+
import { readdir, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const maxWatchers = 224;
|
|
6
|
+
const maxSelected = 5;
|
|
7
|
+
const maxRootsPerAgent = 12;
|
|
8
|
+
const planLifetime = 30_000;
|
|
9
|
+
const note = /\.(?:md|markdown)$/i;
|
|
10
|
+
const transcript = /\.jsonl(?:\.zst)?$/i;
|
|
11
|
+
const skill = /^skill\.md$/i;
|
|
12
|
+
const projectFolders = [
|
|
13
|
+
'.agent', '.agents', '.claude', '.codex', '.dsh', '.gemini', '.opencode',
|
|
14
|
+
'.qoder', '.qwen', '.trae', '.workbuddy',
|
|
15
|
+
];
|
|
16
|
+
const hubFolders = ['决策', '经验', '资料', '交接', '笔记'];
|
|
17
|
+
const aliases = {
|
|
18
|
+
'claude-code': 'claude', 'codex-cli': 'codex', 'open-code': 'opencode', 'qoder-cn': 'qoder',
|
|
19
|
+
'qwen-code': 'qwen', 'trae-cn': 'trae', 'trae-solo': 'trae', 'gemini-cli': 'gemini',
|
|
20
|
+
'antigravity-cli': 'antigravity', vscode: 'copilot', 'vscode-insiders': 'copilot',
|
|
21
|
+
'vscode-copilot': 'copilot', vscodium: 'copilot', 'code-oss': 'copilot',
|
|
22
|
+
};
|
|
23
|
+
const all = () => true;
|
|
24
|
+
const matches = pattern => name => pattern.test(name);
|
|
25
|
+
const named = (...values) => {
|
|
26
|
+
const names = new Set(values.map(value => value.toLowerCase()));
|
|
27
|
+
return name => names.has(name.toLowerCase());
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function fileKey(value) {
|
|
31
|
+
const normalized = path.normalize(value);
|
|
32
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function recentDirectories(directory, limit) {
|
|
36
|
+
const entries = await readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
37
|
+
const candidates = entries.filter(item => item.isDirectory() && !item.name.startsWith('.')).slice(0, 256);
|
|
38
|
+
const checked = await Promise.all(candidates.map(async item => {
|
|
39
|
+
const file = path.join(directory, item.name);
|
|
40
|
+
const info = await stat(file).catch(() => undefined);
|
|
41
|
+
return { file, modified: info?.mtimeMs ?? 0 };
|
|
42
|
+
}));
|
|
43
|
+
checked.sort((a, b) => b.modified - a.modified);
|
|
44
|
+
return checked.slice(0, limit).map(item => item.file);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function selectedProjects(entries) {
|
|
48
|
+
return entries.slice(0, maxSelected).map(item => typeof item === 'string' ? { cwd: item } : item)
|
|
49
|
+
.filter(item => typeof item?.cwd === 'string' && path.isAbsolute(item.cwd));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sourceConfigPath(config) {
|
|
53
|
+
return config.configFile ?? config.file ?? process.env.DSH_AGENT_BRIDGE_CONFIG
|
|
54
|
+
?? (process.env.DSH_HOME ? path.join(process.env.DSH_HOME, 'agent-bridge.json')
|
|
55
|
+
: config.home ? path.join(config.home, '.dsh', 'agent-bridge.json') : undefined);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function watchPlan(config, selected) {
|
|
59
|
+
const wanted = new Map();
|
|
60
|
+
function add(directory, matcher, priority = 1) {
|
|
61
|
+
if (typeof directory !== 'string' || !path.isAbsolute(directory)) return;
|
|
62
|
+
const key = fileKey(directory);
|
|
63
|
+
const current = wanted.get(key);
|
|
64
|
+
if (current) {
|
|
65
|
+
current.matchers.push(matcher);
|
|
66
|
+
current.priority = Math.min(current.priority, priority);
|
|
67
|
+
} else wanted.set(key, { directory, matchers: [matcher], priority });
|
|
68
|
+
}
|
|
69
|
+
function file(value, sqlite = false) {
|
|
70
|
+
if (typeof value !== 'string' || !path.isAbsolute(value)) return;
|
|
71
|
+
const name = path.basename(value);
|
|
72
|
+
add(path.dirname(value), sqlite ? named(name, `${name}-wal`, `${name}-journal`) : named(name), 0);
|
|
73
|
+
}
|
|
74
|
+
async function skills(root, priority) {
|
|
75
|
+
add(root, all, priority);
|
|
76
|
+
const children = await recentDirectories(root, 20);
|
|
77
|
+
for (const child of children) add(child, matches(skill), priority + 1);
|
|
78
|
+
const system = path.join(root, '.system');
|
|
79
|
+
add(system, all, priority + 1);
|
|
80
|
+
for (const child of await recentDirectories(system, 8)) add(child, matches(skill), priority + 1);
|
|
81
|
+
}
|
|
82
|
+
async function childTranscripts(root, extra = []) {
|
|
83
|
+
add(root, all);
|
|
84
|
+
for (const child of await recentDirectories(root, 8)) {
|
|
85
|
+
add(child, name => transcript.test(name) || name.endsWith('-session.json') || extra.includes(name), 2);
|
|
86
|
+
for (const name of extra) {
|
|
87
|
+
if (name === 'chats' || name === 'chatSessions') add(path.join(child, name), matches(/\.jsonl?$/i), 2);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function codexTranscripts(root) {
|
|
92
|
+
add(root, all);
|
|
93
|
+
const now = new Date();
|
|
94
|
+
const days = new Set();
|
|
95
|
+
for (const value of [now, new Date(now.valueOf() - 86_400_000)]) {
|
|
96
|
+
const local = [value.getFullYear(), value.getMonth() + 1, value.getDate()].map(item => String(item).padStart(2, '0')).join('/');
|
|
97
|
+
days.add(local);
|
|
98
|
+
days.add(value.toISOString().slice(0, 10).replaceAll('-', '/'));
|
|
99
|
+
}
|
|
100
|
+
for (const day of days) {
|
|
101
|
+
const directory = path.join(root, day);
|
|
102
|
+
add(path.dirname(directory), all, 2);
|
|
103
|
+
add(directory, matches(transcript), 2);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const jobs = [];
|
|
108
|
+
file(sourceConfigPath(config));
|
|
109
|
+
for (const archive of config.archiveFiles ?? []) file(archive, true);
|
|
110
|
+
for (const project of selected) {
|
|
111
|
+
const { cwd } = project;
|
|
112
|
+
add(cwd, name => /^(?:agents|claude|gemini|qwen|memory)\.md$/i.test(name) || name === '.agent-bridge' || projectFolders.includes(name), 0);
|
|
113
|
+
const bridge = path.join(cwd, '.agent-bridge');
|
|
114
|
+
add(bridge, named('memory', 'handoffs'), 0);
|
|
115
|
+
for (const folder of ['memory', 'handoffs']) add(path.join(bridge, folder), matches(note), 0);
|
|
116
|
+
for (const folder of projectFolders) {
|
|
117
|
+
const root = path.join(cwd, folder);
|
|
118
|
+
add(root, name => note.test(name) || name === 'memory' || name === 'skills', 0);
|
|
119
|
+
add(path.join(root, 'memory'), matches(note), 0);
|
|
120
|
+
jobs.push(skills(path.join(root, 'skills'), 0));
|
|
121
|
+
}
|
|
122
|
+
for (const [workspace, sources] of Object.entries(config.memoryExports ?? {})) {
|
|
123
|
+
if (fileKey(workspace) !== fileKey(cwd)) continue;
|
|
124
|
+
for (const source of sources) {
|
|
125
|
+
if (note.test(source)) file(source);
|
|
126
|
+
else add(source, matches(note), 0);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (const root of config.workspaceHubRoots ?? []) {
|
|
130
|
+
for (const id of (project.projectIds ?? []).filter(value => /^P\d{3,}$/.test(value)).slice(0, 8)) {
|
|
131
|
+
const folder = path.join(root, '项目档案', id);
|
|
132
|
+
add(folder, name => note.test(name) || name === 'project.yaml' || hubFolders.includes(name), 0);
|
|
133
|
+
for (const subfolder of hubFolders) add(path.join(folder, subfolder), matches(note), 0);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const root of config.workspaceHubRoots ?? []) {
|
|
138
|
+
const projects = path.basename(root) === '项目档案' ? root : path.join(root, '项目档案');
|
|
139
|
+
add(projects, matches(/^P\d{3,}$/), 0);
|
|
140
|
+
jobs.push(skills(path.join(root, 'skills', 'source'), 2));
|
|
141
|
+
}
|
|
142
|
+
for (const root of config.projectRoots ?? []) {
|
|
143
|
+
if (typeof root === 'string' && path.isAbsolute(root)) add(path.dirname(root), named(path.basename(root)), 0);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const globalSkills = [...(config.skillRoots ?? []), ...(config.opencodeSkillRoots ?? []).map(root => path.join(root, 'skills'))];
|
|
147
|
+
if (config.home) globalSkills.push(path.join(config.home, '.agents', 'skills'));
|
|
148
|
+
for (const root of globalSkills) jobs.push(skills(root, 2));
|
|
149
|
+
|
|
150
|
+
for (const [provider, roots] of Object.entries(config.agentRoots ?? {})) {
|
|
151
|
+
const agent = aliases[provider] ?? provider;
|
|
152
|
+
for (const root of roots.slice(0, maxRootsPerAgent)) {
|
|
153
|
+
if (agent === 'opencode' && (await stat(root).catch(() => undefined))?.isFile()) {
|
|
154
|
+
file(root, true);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
add(root, name => /^(?:state_\d+\.sqlite(?:-wal|-journal)?|(?:opencode|conversation_summaries)\.db(?:-wal|-journal)?|history\.jsonl|session_index\.jsonl)$/i.test(name)
|
|
158
|
+
|| note.test(name) || ['projects', 'sessions', 'archived_sessions', 'workspaceStorage', 'User', 'tmp', 'skills', 'memory', 'memories', 'memories_v2', 'agent-memory', 'knowledge', 'qoder-knowledge', 'diary'].includes(name), 0);
|
|
159
|
+
|
|
160
|
+
if (agent === 'codex') {
|
|
161
|
+
const tail = path.basename(root).toLowerCase();
|
|
162
|
+
if (tail !== 'archived_sessions') codexTranscripts(tail === 'sessions' ? root : path.join(root, 'sessions'));
|
|
163
|
+
if (tail !== 'sessions') add(tail === 'archived_sessions' ? root : path.join(root, 'archived_sessions'), matches(transcript));
|
|
164
|
+
}
|
|
165
|
+
if (['claude', 'qoder', 'qwen', 'workbuddy'].includes(agent)) {
|
|
166
|
+
const projects = path.basename(root).toLowerCase() === 'projects' ? root : path.join(root, 'projects');
|
|
167
|
+
jobs.push(childTranscripts(projects));
|
|
168
|
+
for (const { cwd } of selected) {
|
|
169
|
+
const slug = cwd.replace(/[^\p{L}\p{N}]/gu, '-');
|
|
170
|
+
const asciiSlug = cwd.replace(/[^a-zA-Z0-9]/g, '-');
|
|
171
|
+
for (const name of new Set([slug, asciiSlug, slug.toLowerCase()])) {
|
|
172
|
+
const bucket = path.join(projects, name);
|
|
173
|
+
add(bucket, name => transcript.test(name) || name === 'memory', 0);
|
|
174
|
+
add(path.join(bucket, 'memory'), matches(note), 0);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (agent === 'gemini') jobs.push(childTranscripts(path.basename(root) === 'tmp' ? root : path.join(root, 'tmp'), ['chats', '.project_root']));
|
|
179
|
+
if (agent === 'opencode') {
|
|
180
|
+
file(path.join(root, 'opencode.db'), true);
|
|
181
|
+
file(path.join(root, 'opencode', 'opencode.db'), true);
|
|
182
|
+
}
|
|
183
|
+
if (agent === 'copilot' || agent === 'trae') {
|
|
184
|
+
const stores = path.basename(root).toLowerCase() === 'workspacestorage'
|
|
185
|
+
? [root] : [path.join(root, 'workspaceStorage'), path.join(root, 'User', 'workspaceStorage')];
|
|
186
|
+
for (const store of stores) jobs.push(childTranscripts(store, ['workspace.json', 'chatSessions']));
|
|
187
|
+
}
|
|
188
|
+
for (const folder of ['memory', 'memories', 'memories_v2', 'agent-memory', 'knowledge', 'qoder-knowledge', 'diary']) {
|
|
189
|
+
add(path.join(root, folder), matches(note), 2);
|
|
190
|
+
}
|
|
191
|
+
if (agent === 'codex') {
|
|
192
|
+
for (const folder of ['memories', 'memories_v2']) add(path.join(root, folder, 'extensions', 'ad_hoc', 'notes'), matches(note), 2);
|
|
193
|
+
}
|
|
194
|
+
jobs.push(skills(path.join(root, 'skills'), 2));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
await Promise.all(jobs);
|
|
198
|
+
return [...wanted.values()].sort((a, b) => a.priority - b.priority);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function watchTarget(entry) {
|
|
202
|
+
let { directory, matchers } = entry;
|
|
203
|
+
for (let level = 0; level < 4; level++) {
|
|
204
|
+
try {
|
|
205
|
+
const actual = await realpath(directory);
|
|
206
|
+
const info = await stat(actual, { bigint: true });
|
|
207
|
+
if (!info.isDirectory()) return undefined;
|
|
208
|
+
return { directory: actual, matchers, identity: `${info.dev}:${info.ino}` };
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (!['ENOENT', 'ENOTDIR'].includes(error.code)) return undefined;
|
|
211
|
+
const parent = path.dirname(directory);
|
|
212
|
+
if (parent === directory) return undefined;
|
|
213
|
+
matchers = [named(path.basename(directory))];
|
|
214
|
+
directory = parent;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function createSourceMonitor({ onChange, debounceMs = 300 } = {}) {
|
|
221
|
+
const watchers = new Map();
|
|
222
|
+
let disposed = false;
|
|
223
|
+
let dirty = true;
|
|
224
|
+
let nextPlan = 0;
|
|
225
|
+
let signature;
|
|
226
|
+
let generation = 0;
|
|
227
|
+
let changeTimer;
|
|
228
|
+
|
|
229
|
+
function signal(event) {
|
|
230
|
+
if (disposed) return;
|
|
231
|
+
if (event === 'rename') dirty = true;
|
|
232
|
+
if (changeTimer) return;
|
|
233
|
+
changeTimer = setTimeout(() => {
|
|
234
|
+
changeTimer = undefined;
|
|
235
|
+
if (!disposed) onChange?.();
|
|
236
|
+
}, debounceMs);
|
|
237
|
+
changeTimer.unref?.();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function update(config = {}, projectDirectories = []) {
|
|
241
|
+
if (disposed) return;
|
|
242
|
+
const selected = selectedProjects(projectDirectories);
|
|
243
|
+
const current = JSON.stringify([config, selected]);
|
|
244
|
+
if (!dirty && signature === current && Date.now() < nextPlan) return;
|
|
245
|
+
const token = ++generation;
|
|
246
|
+
signature = current;
|
|
247
|
+
dirty = false;
|
|
248
|
+
nextPlan = Date.now() + planLifetime;
|
|
249
|
+
const plan = await watchPlan(config, selected);
|
|
250
|
+
const wanted = new Map();
|
|
251
|
+
for (let offset = 0; offset < plan.length && wanted.size < maxWatchers; offset += 48) {
|
|
252
|
+
const chunk = await Promise.all(plan.slice(offset, offset + 48).map(watchTarget));
|
|
253
|
+
for (const target of chunk) {
|
|
254
|
+
if (!target) continue;
|
|
255
|
+
const key = fileKey(target.directory);
|
|
256
|
+
const current = wanted.get(key);
|
|
257
|
+
if (current) current.matchers.push(...target.matchers);
|
|
258
|
+
else if (wanted.size < maxWatchers) wanted.set(key, target);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (disposed || token !== generation) return;
|
|
262
|
+
for (const [key, entry] of watchers) {
|
|
263
|
+
if (wanted.has(key)) continue;
|
|
264
|
+
entry.watcher.close();
|
|
265
|
+
watchers.delete(key);
|
|
266
|
+
}
|
|
267
|
+
for (const [key, target] of wanted) {
|
|
268
|
+
const current = watchers.get(key);
|
|
269
|
+
if (current?.identity === target.identity) {
|
|
270
|
+
current.matchers = target.matchers;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (current) {
|
|
274
|
+
watchers.delete(key);
|
|
275
|
+
current.watcher.close();
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const entry = { matchers: target.matchers, identity: target.identity };
|
|
279
|
+
const watcher = watch(target.directory, { persistent: false }, (event, filename) => {
|
|
280
|
+
if (!filename || entry.matchers.some(matcher => matcher(path.basename(String(filename))))) signal(event);
|
|
281
|
+
});
|
|
282
|
+
entry.watcher = watcher;
|
|
283
|
+
watcher.on('error', () => {
|
|
284
|
+
if (watchers.get(key) !== entry) return;
|
|
285
|
+
watcher.close();
|
|
286
|
+
watchers.delete(key);
|
|
287
|
+
dirty = true;
|
|
288
|
+
});
|
|
289
|
+
watcher.on('close', () => {
|
|
290
|
+
if (watchers.get(key) !== entry) return;
|
|
291
|
+
watchers.delete(key);
|
|
292
|
+
dirty = true;
|
|
293
|
+
});
|
|
294
|
+
watchers.set(key, entry);
|
|
295
|
+
} catch {
|
|
296
|
+
// Unsupported filesystems remain covered by the caller's periodic polling.
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function dispose() {
|
|
302
|
+
disposed = true;
|
|
303
|
+
clearTimeout(changeTimer);
|
|
304
|
+
for (const { watcher } of watchers.values()) watcher.close();
|
|
305
|
+
watchers.clear();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return { update, dispose };
|
|
309
|
+
}
|