dsh-plugin-agentdeck 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +66 -2
- package/cordis.patch.yml +3 -0
- package/icon.svg +7 -0
- package/lib/client.js +911 -0
- package/lib/index.js +366 -0
- package/lib/setup.js +1223 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +69 -4
package/lib/setup.js
ADDED
|
@@ -0,0 +1,1223 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import {
|
|
3
|
+
accessSync,
|
|
4
|
+
constants as fsConstants,
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
chmodSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
statSync,
|
|
11
|
+
unlinkSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from 'node:fs';
|
|
14
|
+
import { arch, homedir } from 'node:os';
|
|
15
|
+
import { delimiter, dirname, join, resolve } from 'node:path';
|
|
16
|
+
import QRCode from 'qrcode';
|
|
17
|
+
import { parseDocument } from 'yaml';
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_RELAY_URL = 'wss://agentdeck.xianqiao.wang/ws';
|
|
20
|
+
const LEGACY_DEFAULT_RELAY_URL = 'wss://agent-deck.xianqiao.wang/ws';
|
|
21
|
+
|
|
22
|
+
export const ACP_PLUGIN_PACKAGES = [
|
|
23
|
+
'@openma/deepseek-harness-acp',
|
|
24
|
+
'@cnctem/dsh-acp',
|
|
25
|
+
];
|
|
26
|
+
export const DEFAULT_ACP_PLUGIN = '@openma/deepseek-harness-acp@latest';
|
|
27
|
+
export const AWAKE_MODES = ['never', 'plugged', 'active', 'always'];
|
|
28
|
+
|
|
29
|
+
const BUILTIN_DEEPSEEK_PROVIDERS = new Set([
|
|
30
|
+
'deepseek-official',
|
|
31
|
+
'deepseek-account',
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const PATCH_HEADER =
|
|
35
|
+
'# Your patch layer for this dsh profile, applied after every bundle layer:\n' +
|
|
36
|
+
'# a top-level YAML array of loader patch entries (id-targeted config\n' +
|
|
37
|
+
'# overrides, disables, and insert lists; `!!js` expressions allowed).\n';
|
|
38
|
+
|
|
39
|
+
const JS_YAML_TAG = {
|
|
40
|
+
tag: 'tag:yaml.org,2002:js',
|
|
41
|
+
resolve: (str) => str,
|
|
42
|
+
identify: () => false,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolve AgentDeck's local data directory, matching `dirs::data_local_dir()/agentdeck`.
|
|
47
|
+
*/
|
|
48
|
+
export function getAgentDeckDataDir({
|
|
49
|
+
platform = process.platform,
|
|
50
|
+
env = process.env,
|
|
51
|
+
home = homedir(),
|
|
52
|
+
} = {}) {
|
|
53
|
+
if (platform === 'darwin') {
|
|
54
|
+
return join(home, 'Library', 'Application Support', 'agentdeck');
|
|
55
|
+
}
|
|
56
|
+
if (platform === 'win32') {
|
|
57
|
+
const localAppData = env.LOCALAPPDATA || join(home, 'AppData', 'Local');
|
|
58
|
+
return join(localAppData, 'agentdeck');
|
|
59
|
+
}
|
|
60
|
+
const xdgDataHome = env.XDG_DATA_HOME || join(home, '.local', 'share');
|
|
61
|
+
return join(xdgDataHome, 'agentdeck');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build the AgentDeck pairing URI (`agentdeck://connect?pairingId=...`).
|
|
66
|
+
* Includes `relayUrl` only when using a non-default Relay.
|
|
67
|
+
*/
|
|
68
|
+
export function buildPairingUri(relayId, relayUrl) {
|
|
69
|
+
const effectiveRelay =
|
|
70
|
+
relayUrl && relayUrl !== LEGACY_DEFAULT_RELAY_URL
|
|
71
|
+
? relayUrl
|
|
72
|
+
: DEFAULT_RELAY_URL;
|
|
73
|
+
const url = new URL('agentdeck://connect');
|
|
74
|
+
url.searchParams.set('pairingId', relayId);
|
|
75
|
+
if (effectiveRelay !== DEFAULT_RELAY_URL) {
|
|
76
|
+
url.searchParams.set('relayUrl', effectiveRelay);
|
|
77
|
+
}
|
|
78
|
+
return url.toString();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function generatePairingQrSvg(pairingUri) {
|
|
82
|
+
return QRCode.toString(pairingUri, {
|
|
83
|
+
type: 'svg',
|
|
84
|
+
margin: 2,
|
|
85
|
+
width: 220,
|
|
86
|
+
errorCorrectionLevel: 'M',
|
|
87
|
+
color: {
|
|
88
|
+
dark: '#111111',
|
|
89
|
+
light: '#ffffff',
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isExecutableFile(filePath, platform = process.platform) {
|
|
95
|
+
if (!filePath || !existsSync(filePath)) return false;
|
|
96
|
+
if (platform === 'win32') return true;
|
|
97
|
+
try {
|
|
98
|
+
accessSync(filePath, fsConstants.X_OK);
|
|
99
|
+
return true;
|
|
100
|
+
} catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function findExecutable(
|
|
106
|
+
name,
|
|
107
|
+
{
|
|
108
|
+
platform = process.platform,
|
|
109
|
+
env = process.env,
|
|
110
|
+
home = homedir(),
|
|
111
|
+
extraDirs = [],
|
|
112
|
+
} = {},
|
|
113
|
+
) {
|
|
114
|
+
const candidates =
|
|
115
|
+
platform === 'win32'
|
|
116
|
+
? [`${name}.exe`, `${name}.cmd`, `${name}.bat`, name]
|
|
117
|
+
: [name];
|
|
118
|
+
const pathDirs = (env.PATH || '').split(delimiter).filter(Boolean);
|
|
119
|
+
const nodeBinDir = dirname(process.execPath);
|
|
120
|
+
const fallbackDirs =
|
|
121
|
+
platform === 'win32'
|
|
122
|
+
? [
|
|
123
|
+
nodeBinDir,
|
|
124
|
+
join(home, 'scoop', 'shims'),
|
|
125
|
+
join(env.APPDATA || join(home, 'AppData', 'Roaming'), 'npm'),
|
|
126
|
+
join(home, '.cargo', 'bin'),
|
|
127
|
+
]
|
|
128
|
+
: [
|
|
129
|
+
nodeBinDir,
|
|
130
|
+
join(home, '.local', 'bin'),
|
|
131
|
+
'/opt/homebrew/bin',
|
|
132
|
+
'/usr/local/bin',
|
|
133
|
+
join(home, '.cargo', 'bin'),
|
|
134
|
+
join(home, '.npm-global', 'bin'),
|
|
135
|
+
join(home, 'Library', 'pnpm'),
|
|
136
|
+
join(home, '.local', 'share', 'pnpm'),
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
const seen = new Set();
|
|
140
|
+
for (const dir of [...extraDirs, ...pathDirs, ...fallbackDirs]) {
|
|
141
|
+
if (!dir || seen.has(dir)) continue;
|
|
142
|
+
seen.add(dir);
|
|
143
|
+
for (const file of candidates) {
|
|
144
|
+
const full = join(dir, file);
|
|
145
|
+
if (isExecutableFile(full, platform)) return full;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function platformSuffix(platform = process.platform, cpu = arch()) {
|
|
152
|
+
if (platform === 'darwin' && cpu === 'arm64') return 'darwin-arm64';
|
|
153
|
+
if (platform === 'darwin' && cpu === 'x64') return 'darwin-x64';
|
|
154
|
+
if (platform === 'linux' && cpu === 'x64') return 'linux-x64-gnu';
|
|
155
|
+
if (platform === 'win32' && cpu === 'x64') return 'windows-x64';
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Find `agentdeckd` executable on PATH, standard package-manager bin dirs,
|
|
161
|
+
* or the plugin's dedicated fallback install prefix (`<dataDir>/cli`).
|
|
162
|
+
*/
|
|
163
|
+
export function findAgentDeckDaemon({
|
|
164
|
+
dataDir = getAgentDeckDataDir(),
|
|
165
|
+
platform = process.platform,
|
|
166
|
+
env = process.env,
|
|
167
|
+
home = homedir(),
|
|
168
|
+
extraDirs = [],
|
|
169
|
+
} = {}) {
|
|
170
|
+
const onPath = findExecutable('agentdeckd', { platform, env, home, extraDirs });
|
|
171
|
+
if (onPath) return onPath;
|
|
172
|
+
|
|
173
|
+
const cliDir = join(dataDir, 'cli');
|
|
174
|
+
const suffix = platformSuffix(platform);
|
|
175
|
+
const exe = platform === 'win32' ? 'agentdeckd.exe' : 'agentdeckd';
|
|
176
|
+
if (suffix) {
|
|
177
|
+
const directNative = join(
|
|
178
|
+
cliDir,
|
|
179
|
+
'node_modules',
|
|
180
|
+
`agentdeckd-${suffix}`,
|
|
181
|
+
'bin',
|
|
182
|
+
exe,
|
|
183
|
+
);
|
|
184
|
+
if (isExecutableFile(directNative, platform)) return directNative;
|
|
185
|
+
const nestedNative = join(
|
|
186
|
+
cliDir,
|
|
187
|
+
'node_modules',
|
|
188
|
+
'agentdeckd',
|
|
189
|
+
'node_modules',
|
|
190
|
+
`agentdeckd-${suffix}`,
|
|
191
|
+
'bin',
|
|
192
|
+
exe,
|
|
193
|
+
);
|
|
194
|
+
if (isExecutableFile(nestedNative, platform)) return nestedNative;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return findExecutable('agentdeckd', {
|
|
198
|
+
platform,
|
|
199
|
+
env,
|
|
200
|
+
home,
|
|
201
|
+
extraDirs: [join(cliDir, 'node_modules', '.bin')],
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Resolve the `dsh` executable path. Works for both CLI installs and
|
|
207
|
+
* DeepSeek Harness Desktop (bundled `runtime/cli/bin/dsh`).
|
|
208
|
+
*/
|
|
209
|
+
export function resolveDshCommand({
|
|
210
|
+
platform = process.platform,
|
|
211
|
+
env = process.env,
|
|
212
|
+
home = homedir(),
|
|
213
|
+
resourcesPath = process.resourcesPath,
|
|
214
|
+
argv1 = process.argv[1],
|
|
215
|
+
} = {}) {
|
|
216
|
+
const bundledDirs = [];
|
|
217
|
+
if (resourcesPath) {
|
|
218
|
+
bundledDirs.push(join(resourcesPath, 'runtime', 'cli', 'bin'));
|
|
219
|
+
}
|
|
220
|
+
if (argv1) {
|
|
221
|
+
let cursor = dirname(resolve(argv1));
|
|
222
|
+
for (let i = 0; i < 6; i++) {
|
|
223
|
+
bundledDirs.push(join(cursor, 'bin'));
|
|
224
|
+
bundledDirs.push(join(cursor, 'runtime', 'cli', 'bin'));
|
|
225
|
+
const parent = dirname(cursor);
|
|
226
|
+
if (parent === cursor) break;
|
|
227
|
+
cursor = parent;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Prefer user PATH / ~/.local/bin first, then bundled Desktop CLI.
|
|
232
|
+
const onPath = findExecutable('dsh', { platform, env, home });
|
|
233
|
+
if (onPath) return onPath;
|
|
234
|
+
return findExecutable('dsh', { platform, env, home, extraDirs: bundledDirs });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isPidAlive(pid) {
|
|
238
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
239
|
+
try {
|
|
240
|
+
process.kill(pid, 0);
|
|
241
|
+
return true;
|
|
242
|
+
} catch (err) {
|
|
243
|
+
return err?.code === 'EPERM';
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function readDaemonState(
|
|
248
|
+
dataDir,
|
|
249
|
+
{ platform = process.platform, env = process.env, home = homedir() } = {},
|
|
250
|
+
) {
|
|
251
|
+
const binaryPath = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
252
|
+
const configPath = join(dataDir, 'daemon.json');
|
|
253
|
+
const lockPath = join(dataDir, 'daemon.lock');
|
|
254
|
+
|
|
255
|
+
let pid = null;
|
|
256
|
+
let running = false;
|
|
257
|
+
if (existsSync(lockPath)) {
|
|
258
|
+
try {
|
|
259
|
+
const parsed = Number.parseInt(readFileSync(lockPath, 'utf8').trim(), 10);
|
|
260
|
+
if (Number.isInteger(parsed) && parsed > 0) {
|
|
261
|
+
pid = parsed;
|
|
262
|
+
running = isPidAlive(parsed);
|
|
263
|
+
}
|
|
264
|
+
} catch {
|
|
265
|
+
// Ignore unreadable lock file
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let relayId = null;
|
|
270
|
+
let relayUrl = DEFAULT_RELAY_URL;
|
|
271
|
+
let hasCustomRelay = false;
|
|
272
|
+
let awakeMode = 'never';
|
|
273
|
+
if (existsSync(configPath)) {
|
|
274
|
+
try {
|
|
275
|
+
const raw = JSON.parse(readFileSync(configPath, 'utf8'));
|
|
276
|
+
if (typeof raw?.relay_id === 'string' && raw.relay_id.trim()) {
|
|
277
|
+
relayId = raw.relay_id.trim();
|
|
278
|
+
}
|
|
279
|
+
if (
|
|
280
|
+
typeof raw?.relay_url === 'string' &&
|
|
281
|
+
raw.relay_url.trim() &&
|
|
282
|
+
raw.relay_url.trim() !== LEGACY_DEFAULT_RELAY_URL
|
|
283
|
+
) {
|
|
284
|
+
relayUrl = raw.relay_url.trim();
|
|
285
|
+
hasCustomRelay = relayUrl !== DEFAULT_RELAY_URL;
|
|
286
|
+
}
|
|
287
|
+
if (typeof raw?.awake === 'string' && AWAKE_MODES.includes(raw.awake)) {
|
|
288
|
+
awakeMode = raw.awake;
|
|
289
|
+
}
|
|
290
|
+
} catch {
|
|
291
|
+
// Ignore corrupt daemon.json
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const awakeStatusPath = join(dataDir, 'awake_status.json');
|
|
296
|
+
let awakeActive = false;
|
|
297
|
+
let awakeError = null;
|
|
298
|
+
if (running && existsSync(awakeStatusPath)) {
|
|
299
|
+
try {
|
|
300
|
+
const st = JSON.parse(readFileSync(awakeStatusPath, 'utf8'));
|
|
301
|
+
if (st?.pid === pid && st?.mode === awakeMode) {
|
|
302
|
+
awakeActive = Boolean(st.on);
|
|
303
|
+
awakeError = typeof st.error === 'string' ? st.error : null;
|
|
304
|
+
}
|
|
305
|
+
} catch {
|
|
306
|
+
// Ignore unreadable awake_status.json
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let qrSvg = null;
|
|
311
|
+
let maskedPairingId = null;
|
|
312
|
+
if (relayId) {
|
|
313
|
+
const uri = buildPairingUri(relayId, relayUrl);
|
|
314
|
+
qrSvg = await generatePairingQrSvg(uri);
|
|
315
|
+
maskedPairingId =
|
|
316
|
+
relayId.length > 14
|
|
317
|
+
? `${relayId.slice(0, 9)}…${relayId.slice(-4)}`
|
|
318
|
+
: relayId;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
installed: Boolean(binaryPath) || running,
|
|
323
|
+
binaryPath,
|
|
324
|
+
running,
|
|
325
|
+
pid: running ? pid : null,
|
|
326
|
+
pairedReady: Boolean(relayId),
|
|
327
|
+
maskedPairingId,
|
|
328
|
+
relayUrl,
|
|
329
|
+
hasCustomRelay,
|
|
330
|
+
qrSvg,
|
|
331
|
+
awake: {
|
|
332
|
+
mode: awakeMode,
|
|
333
|
+
active: awakeActive,
|
|
334
|
+
error: awakeError,
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function readAcpPluginState(dshHome) {
|
|
340
|
+
const acpDir = join(dshHome, 'profiles', 'acp');
|
|
341
|
+
const pkgPath = join(acpDir, 'package.json');
|
|
342
|
+
if (!existsSync(pkgPath)) {
|
|
343
|
+
return { installed: false, packageName: null, version: null, acpDir };
|
|
344
|
+
}
|
|
345
|
+
try {
|
|
346
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
347
|
+
const deps = { ...(pkg.devDependencies || {}), ...(pkg.dependencies || {}) };
|
|
348
|
+
for (const candidate of ACP_PLUGIN_PACKAGES) {
|
|
349
|
+
if (deps[candidate]) {
|
|
350
|
+
const installedPkgJson = join(
|
|
351
|
+
acpDir,
|
|
352
|
+
'node_modules',
|
|
353
|
+
...candidate.split('/'),
|
|
354
|
+
'package.json',
|
|
355
|
+
);
|
|
356
|
+
if (existsSync(installedPkgJson)) {
|
|
357
|
+
let version = deps[candidate];
|
|
358
|
+
try {
|
|
359
|
+
const inner = JSON.parse(readFileSync(installedPkgJson, 'utf8'));
|
|
360
|
+
if (inner.version) version = inner.version;
|
|
361
|
+
} catch {
|
|
362
|
+
// Fall back to declared version range
|
|
363
|
+
}
|
|
364
|
+
return { installed: true, packageName: candidate, version, acpDir };
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
} catch {
|
|
369
|
+
// Ignore malformed profile package.json
|
|
370
|
+
}
|
|
371
|
+
return { installed: false, packageName: null, version: null, acpDir };
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function parsePatchDocument(text) {
|
|
375
|
+
const doc = parseDocument(text || '[]', {
|
|
376
|
+
customTags: [JS_YAML_TAG],
|
|
377
|
+
keepSourceTokens: false,
|
|
378
|
+
});
|
|
379
|
+
if (!doc.contents || !Array.isArray(doc.contents.items)) {
|
|
380
|
+
doc.contents = doc.createNode([]);
|
|
381
|
+
}
|
|
382
|
+
return doc;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function findPatchEntryIndex(items, id) {
|
|
386
|
+
for (let i = 0; i < items.length; i++) {
|
|
387
|
+
const item = items[i];
|
|
388
|
+
if (item && typeof item.get === 'function' && item.get('id') === id) {
|
|
389
|
+
return i;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return -1;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function readPatchFile(patchPath) {
|
|
396
|
+
if (!patchPath || !existsSync(patchPath)) return null;
|
|
397
|
+
try {
|
|
398
|
+
return readFileSync(patchPath, 'utf8');
|
|
399
|
+
} catch {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function extractModelSummary(patchPath) {
|
|
405
|
+
const text = readPatchFile(patchPath);
|
|
406
|
+
if (!text) {
|
|
407
|
+
return {
|
|
408
|
+
exists: false,
|
|
409
|
+
defaultModel: null,
|
|
410
|
+
acpPlugin: null,
|
|
411
|
+
hasCustomProviders: false,
|
|
412
|
+
customProviderNames: [],
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const doc = parsePatchDocument(text);
|
|
417
|
+
const arr = doc.toJS();
|
|
418
|
+
if (!Array.isArray(arr)) {
|
|
419
|
+
return {
|
|
420
|
+
exists: true,
|
|
421
|
+
defaultModel: null,
|
|
422
|
+
acpPlugin: null,
|
|
423
|
+
hasCustomProviders: false,
|
|
424
|
+
customProviderNames: [],
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const defaultEntry = arr.find((e) => e?.id === 'agent-default-model');
|
|
428
|
+
const acpEntry = arr.find((e) => e?.id === 'acp-plugin');
|
|
429
|
+
const piAiEntry = arr.find((e) => e?.id === 'llm-pi-ai');
|
|
430
|
+
const providers =
|
|
431
|
+
piAiEntry?.config?.providers &&
|
|
432
|
+
typeof piAiEntry.config.providers === 'object'
|
|
433
|
+
? Object.keys(piAiEntry.config.providers)
|
|
434
|
+
: [];
|
|
435
|
+
return {
|
|
436
|
+
exists: true,
|
|
437
|
+
defaultModel: defaultEntry?.config
|
|
438
|
+
? {
|
|
439
|
+
provider: defaultEntry.config.provider || null,
|
|
440
|
+
model: defaultEntry.config.model || null,
|
|
441
|
+
reasoningEffort: defaultEntry.config.reasoningEffort || null,
|
|
442
|
+
}
|
|
443
|
+
: null,
|
|
444
|
+
acpPlugin: acpEntry?.config
|
|
445
|
+
? {
|
|
446
|
+
provider: acpEntry.config.provider || null,
|
|
447
|
+
model: acpEntry.config.model || null,
|
|
448
|
+
}
|
|
449
|
+
: null,
|
|
450
|
+
hasCustomProviders: providers.length > 0,
|
|
451
|
+
customProviderNames: providers,
|
|
452
|
+
};
|
|
453
|
+
} catch {
|
|
454
|
+
return {
|
|
455
|
+
exists: true,
|
|
456
|
+
defaultModel: null,
|
|
457
|
+
acpPlugin: null,
|
|
458
|
+
hasCustomProviders: false,
|
|
459
|
+
customProviderNames: [],
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function writeFileAtomic(filePath, content, { mode } = {}) {
|
|
465
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
466
|
+
let targetMode = mode;
|
|
467
|
+
if (targetMode === undefined && existsSync(filePath)) {
|
|
468
|
+
try {
|
|
469
|
+
targetMode = statSync(filePath).mode & 0o777;
|
|
470
|
+
} catch {
|
|
471
|
+
// Fall back to default umask
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const tmpPath = `${filePath}.tmp.${process.pid}.${Date.now()}`;
|
|
475
|
+
try {
|
|
476
|
+
writeFileSync(tmpPath, content, {
|
|
477
|
+
encoding: 'utf8',
|
|
478
|
+
...(targetMode !== undefined ? { mode: targetMode } : {}),
|
|
479
|
+
});
|
|
480
|
+
if (targetMode !== undefined && process.platform !== 'win32') {
|
|
481
|
+
try {
|
|
482
|
+
chmodSync(tmpPath, targetMode);
|
|
483
|
+
} catch {
|
|
484
|
+
// Ignore chmod failure on unsupported filesystems
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
renameSync(tmpPath, filePath);
|
|
488
|
+
} catch (err) {
|
|
489
|
+
try {
|
|
490
|
+
unlinkSync(tmpPath);
|
|
491
|
+
} catch {
|
|
492
|
+
// Ignore cleanup failure
|
|
493
|
+
}
|
|
494
|
+
throw err;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Sync model/provider configuration from the active profile into the `acp` profile
|
|
500
|
+
* `cordis.patch.yml`, preserving unrelated entries and `!!js` expressions.
|
|
501
|
+
*
|
|
502
|
+
* Also mirrors non-DeepSeek `provider`/`model` into `acp-plugin.config` so
|
|
503
|
+
* `@openma/deepseek-harness-acp` validates the matching provider credential
|
|
504
|
+
* instead of requiring `DEEPSEEK_API_KEY`.
|
|
505
|
+
*/
|
|
506
|
+
export function syncAcpPatch({
|
|
507
|
+
sourcePatchPath,
|
|
508
|
+
acpPatchPath,
|
|
509
|
+
syncModels = false,
|
|
510
|
+
}) {
|
|
511
|
+
const sourceText = readPatchFile(sourcePatchPath);
|
|
512
|
+
const acpText = readPatchFile(acpPatchPath);
|
|
513
|
+
|
|
514
|
+
const acpDoc = parsePatchDocument(acpText || '[]');
|
|
515
|
+
const acpItems = acpDoc.contents.items;
|
|
516
|
+
let changed = false;
|
|
517
|
+
|
|
518
|
+
const acpExistingDefaultIdx = findPatchEntryIndex(
|
|
519
|
+
acpItems,
|
|
520
|
+
'agent-default-model',
|
|
521
|
+
);
|
|
522
|
+
const shouldSyncFromSource =
|
|
523
|
+
Boolean(sourceText) && (syncModels || acpExistingDefaultIdx === -1);
|
|
524
|
+
|
|
525
|
+
if (shouldSyncFromSource) {
|
|
526
|
+
const sourceDoc = parsePatchDocument(sourceText);
|
|
527
|
+
const sourceItems = sourceDoc.contents.items;
|
|
528
|
+
|
|
529
|
+
for (const id of ['llm-pi-ai', 'agent-default-model']) {
|
|
530
|
+
const srcIdx = findPatchEntryIndex(sourceItems, id);
|
|
531
|
+
if (srcIdx === -1) continue;
|
|
532
|
+
const cloned = sourceItems[srcIdx].clone();
|
|
533
|
+
const dstIdx = findPatchEntryIndex(acpItems, id);
|
|
534
|
+
if (dstIdx === -1) {
|
|
535
|
+
acpItems.push(cloned);
|
|
536
|
+
} else {
|
|
537
|
+
acpItems[dstIdx] = cloned;
|
|
538
|
+
}
|
|
539
|
+
changed = true;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const effectiveDefaultIdx = findPatchEntryIndex(
|
|
544
|
+
acpItems,
|
|
545
|
+
'agent-default-model',
|
|
546
|
+
);
|
|
547
|
+
if (effectiveDefaultIdx !== -1) {
|
|
548
|
+
const defObj = acpItems[effectiveDefaultIdx].toJS(acpDoc);
|
|
549
|
+
const provider = defObj?.config?.provider;
|
|
550
|
+
const model = defObj?.config?.model;
|
|
551
|
+
const acpPluginIdx = findPatchEntryIndex(acpItems, 'acp-plugin');
|
|
552
|
+
const existingAcpPlugin =
|
|
553
|
+
acpPluginIdx !== -1 ? acpItems[acpPluginIdx].toJS(acpDoc) : null;
|
|
554
|
+
|
|
555
|
+
if (provider && !BUILTIN_DEEPSEEK_PROVIDERS.has(provider)) {
|
|
556
|
+
const nextConfig = {
|
|
557
|
+
...(existingAcpPlugin?.config || {}),
|
|
558
|
+
provider,
|
|
559
|
+
...(model ? { model } : {}),
|
|
560
|
+
};
|
|
561
|
+
const needsUpdate =
|
|
562
|
+
!existingAcpPlugin ||
|
|
563
|
+
existingAcpPlugin?.config?.provider !== nextConfig.provider ||
|
|
564
|
+
(model && existingAcpPlugin?.config?.model !== nextConfig.model);
|
|
565
|
+
if (needsUpdate) {
|
|
566
|
+
const entryNode = acpDoc.createNode({
|
|
567
|
+
id: 'acp-plugin',
|
|
568
|
+
config: nextConfig,
|
|
569
|
+
});
|
|
570
|
+
if (acpPluginIdx === -1) {
|
|
571
|
+
acpItems.push(entryNode);
|
|
572
|
+
} else {
|
|
573
|
+
acpItems[acpPluginIdx] = entryNode;
|
|
574
|
+
}
|
|
575
|
+
changed = true;
|
|
576
|
+
}
|
|
577
|
+
} else if (
|
|
578
|
+
shouldSyncFromSource &&
|
|
579
|
+
provider &&
|
|
580
|
+
BUILTIN_DEEPSEEK_PROVIDERS.has(provider) &&
|
|
581
|
+
acpPluginIdx !== -1
|
|
582
|
+
) {
|
|
583
|
+
const nextConfig = { ...(existingAcpPlugin?.config || {}) };
|
|
584
|
+
nextConfig.provider = provider;
|
|
585
|
+
if (model) nextConfig.model = model;
|
|
586
|
+
acpItems[acpPluginIdx] = acpDoc.createNode({
|
|
587
|
+
id: 'acp-plugin',
|
|
588
|
+
config: nextConfig,
|
|
589
|
+
});
|
|
590
|
+
changed = true;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (changed) {
|
|
595
|
+
const body = acpDoc.toString({ lineWidth: 0 });
|
|
596
|
+
const output = acpText ? body : `${PATCH_HEADER}${body}`;
|
|
597
|
+
writeFileAtomic(acpPatchPath, output);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return { changed, syncedFromSource: shouldSyncFromSource };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function readCustomAgentState(customAgentsPath) {
|
|
604
|
+
if (!existsSync(customAgentsPath)) {
|
|
605
|
+
return { configured: false, entry: null, path: customAgentsPath };
|
|
606
|
+
}
|
|
607
|
+
try {
|
|
608
|
+
const data = JSON.parse(readFileSync(customAgentsPath, 'utf8'));
|
|
609
|
+
const agents = Array.isArray(data?.agents) ? data.agents : [];
|
|
610
|
+
const entry = agents.find((a) => a?.id === 'dsh') || null;
|
|
611
|
+
const configured = Boolean(
|
|
612
|
+
entry &&
|
|
613
|
+
typeof entry.command === 'string' &&
|
|
614
|
+
entry.command.trim() &&
|
|
615
|
+
Array.isArray(entry.args) &&
|
|
616
|
+
entry.args.includes('--profile') &&
|
|
617
|
+
entry.args.includes('acp'),
|
|
618
|
+
);
|
|
619
|
+
return { configured, entry, path: customAgentsPath };
|
|
620
|
+
} catch {
|
|
621
|
+
return { configured: false, entry: null, path: customAgentsPath };
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
export function upsertCustomAgent({ customAgentsPath, dshCommand }) {
|
|
626
|
+
let data = { agents: [] };
|
|
627
|
+
if (existsSync(customAgentsPath)) {
|
|
628
|
+
try {
|
|
629
|
+
const parsed = JSON.parse(readFileSync(customAgentsPath, 'utf8'));
|
|
630
|
+
if (parsed && typeof parsed === 'object') {
|
|
631
|
+
data = {
|
|
632
|
+
...parsed,
|
|
633
|
+
agents: Array.isArray(parsed.agents) ? [...parsed.agents] : [],
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
} catch {
|
|
637
|
+
// Start fresh if file is empty/corrupt JSON
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
const nextEntry = {
|
|
642
|
+
id: 'dsh',
|
|
643
|
+
name: 'DeepSeek Harness',
|
|
644
|
+
command: dshCommand,
|
|
645
|
+
args: ['--profile', 'acp'],
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
const idx = data.agents.findIndex((a) => a?.id === 'dsh');
|
|
649
|
+
if (idx === -1) {
|
|
650
|
+
data.agents.push(nextEntry);
|
|
651
|
+
} else {
|
|
652
|
+
data.agents[idx] = { ...data.agents[idx], ...nextEntry };
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
writeFileAtomic(customAgentsPath, `${JSON.stringify(data, null, 2)}\n`);
|
|
656
|
+
return nextEntry;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function runCommand(
|
|
660
|
+
command,
|
|
661
|
+
args,
|
|
662
|
+
{ cwd, env = process.env, timeoutMs = 180_000 } = {},
|
|
663
|
+
) {
|
|
664
|
+
return new Promise((resolvePromise, reject) => {
|
|
665
|
+
const needsWindowsShell =
|
|
666
|
+
process.platform === 'win32' && /\.(cmd|bat)$/i.test(command);
|
|
667
|
+
const child = spawn(command, args, {
|
|
668
|
+
cwd,
|
|
669
|
+
env,
|
|
670
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
671
|
+
windowsHide: true,
|
|
672
|
+
shell: needsWindowsShell,
|
|
673
|
+
});
|
|
674
|
+
let stdout = '';
|
|
675
|
+
let stderr = '';
|
|
676
|
+
const timer = setTimeout(() => {
|
|
677
|
+
child.kill('SIGTERM');
|
|
678
|
+
reject(
|
|
679
|
+
new Error(`Command timed out after ${Math.round(timeoutMs / 1000)}s`),
|
|
680
|
+
);
|
|
681
|
+
}, timeoutMs);
|
|
682
|
+
|
|
683
|
+
child.stdout.on('data', (chunk) => {
|
|
684
|
+
stdout += chunk.toString('utf8');
|
|
685
|
+
if (stdout.length > 16_000) stdout = stdout.slice(-16_000);
|
|
686
|
+
});
|
|
687
|
+
child.stderr.on('data', (chunk) => {
|
|
688
|
+
stderr += chunk.toString('utf8');
|
|
689
|
+
if (stderr.length > 16_000) stderr = stderr.slice(-16_000);
|
|
690
|
+
});
|
|
691
|
+
child.on('error', (err) => {
|
|
692
|
+
clearTimeout(timer);
|
|
693
|
+
reject(err);
|
|
694
|
+
});
|
|
695
|
+
child.on('close', (code) => {
|
|
696
|
+
clearTimeout(timer);
|
|
697
|
+
resolvePromise({ code, stdout, stderr });
|
|
698
|
+
});
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function sleep(ms) {
|
|
703
|
+
return new Promise((resolvePromise) => setTimeout(resolvePromise, ms));
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Install `agentdeckd` automatically:
|
|
708
|
+
* 1. macOS: Homebrew (`mantou132/tap/agentdeckd`) if available (signed native binary)
|
|
709
|
+
* 2. Global `npm install -g agentdeckd --include=optional` (macOS/Linux/Windows)
|
|
710
|
+
* 3. Windows: `scoop` fallback if available
|
|
711
|
+
* 4. Universal user-local fallback in `<dataDir>/cli` via DSH's bundled `packageManager` (pnpm) or `npm`
|
|
712
|
+
*/
|
|
713
|
+
export async function installAgentDeckDaemon({
|
|
714
|
+
dataDir = getAgentDeckDataDir(),
|
|
715
|
+
platform = process.platform,
|
|
716
|
+
env = process.env,
|
|
717
|
+
home = homedir(),
|
|
718
|
+
packageManager = null,
|
|
719
|
+
} = {}) {
|
|
720
|
+
const existing = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
721
|
+
if (existing) {
|
|
722
|
+
return { installed: false, method: 'existing', binaryPath: existing };
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const errors = [];
|
|
726
|
+
const brewBin =
|
|
727
|
+
platform === 'darwin' || platform === 'linux'
|
|
728
|
+
? findExecutable('brew', { platform, env, home })
|
|
729
|
+
: null;
|
|
730
|
+
const npmBin = findExecutable('npm', { platform, env, home });
|
|
731
|
+
const scoopBin =
|
|
732
|
+
platform === 'win32'
|
|
733
|
+
? findExecutable('scoop', { platform, env, home })
|
|
734
|
+
: null;
|
|
735
|
+
|
|
736
|
+
const tryBrew = async () => {
|
|
737
|
+
if (!brewBin) return null;
|
|
738
|
+
const res = await runCommand(
|
|
739
|
+
brewBin,
|
|
740
|
+
['install', 'mantou132/tap/agentdeckd'],
|
|
741
|
+
{
|
|
742
|
+
env: { ...env, HOMEBREW_NO_AUTO_UPDATE: '1', HOMEBREW_NO_ENV_HINTS: '1' },
|
|
743
|
+
timeoutMs: 240_000,
|
|
744
|
+
},
|
|
745
|
+
);
|
|
746
|
+
if (res.code === 0) {
|
|
747
|
+
const bin = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
748
|
+
if (bin) return { installed: true, method: 'homebrew', binaryPath: bin };
|
|
749
|
+
}
|
|
750
|
+
errors.push(
|
|
751
|
+
`brew: ${(res.stderr || res.stdout || `exit ${res.code}`).trim().slice(-240)}`,
|
|
752
|
+
);
|
|
753
|
+
return null;
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
const tryNpmGlobal = async () => {
|
|
757
|
+
if (!npmBin) return null;
|
|
758
|
+
const res = await runCommand(
|
|
759
|
+
npmBin,
|
|
760
|
+
['install', '-g', 'agentdeckd', '--include=optional'],
|
|
761
|
+
{ env, timeoutMs: 180_000 },
|
|
762
|
+
);
|
|
763
|
+
if (res.code === 0) {
|
|
764
|
+
const npmExtraDirs = [dirname(npmBin)];
|
|
765
|
+
try {
|
|
766
|
+
const prefixRes = await runCommand(npmBin, ['prefix', '-g'], {
|
|
767
|
+
env,
|
|
768
|
+
timeoutMs: 15_000,
|
|
769
|
+
});
|
|
770
|
+
const prefix = prefixRes.code === 0 ? prefixRes.stdout.trim() : '';
|
|
771
|
+
if (prefix) {
|
|
772
|
+
npmExtraDirs.push(platform === 'win32' ? prefix : join(prefix, 'bin'));
|
|
773
|
+
}
|
|
774
|
+
} catch {
|
|
775
|
+
// Ignore prefix lookup failure
|
|
776
|
+
}
|
|
777
|
+
const bin = findAgentDeckDaemon({
|
|
778
|
+
dataDir,
|
|
779
|
+
platform,
|
|
780
|
+
env,
|
|
781
|
+
home,
|
|
782
|
+
extraDirs: npmExtraDirs,
|
|
783
|
+
});
|
|
784
|
+
if (bin) return { installed: true, method: 'npm-global', binaryPath: bin };
|
|
785
|
+
}
|
|
786
|
+
errors.push(
|
|
787
|
+
`npm -g: ${(res.stderr || res.stdout || `exit ${res.code}`).trim().slice(-240)}`,
|
|
788
|
+
);
|
|
789
|
+
return null;
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
const tryScoop = async () => {
|
|
793
|
+
if (!scoopBin) return null;
|
|
794
|
+
await runCommand(
|
|
795
|
+
scoopBin,
|
|
796
|
+
['bucket', 'add', 'mantou132', 'https://github.com/mantou132/scoop-bucket'],
|
|
797
|
+
{ env, timeoutMs: 60_000 },
|
|
798
|
+
);
|
|
799
|
+
const res = await runCommand(scoopBin, ['install', 'agentdeckd'], {
|
|
800
|
+
env,
|
|
801
|
+
timeoutMs: 180_000,
|
|
802
|
+
});
|
|
803
|
+
if (res.code === 0) {
|
|
804
|
+
const bin = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
805
|
+
if (bin) return { installed: true, method: 'scoop', binaryPath: bin };
|
|
806
|
+
}
|
|
807
|
+
errors.push(
|
|
808
|
+
`scoop: ${(res.stderr || res.stdout || `exit ${res.code}`).trim().slice(-240)}`,
|
|
809
|
+
);
|
|
810
|
+
return null;
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
const tryLocalPrefix = async () => {
|
|
814
|
+
const cliDir = join(dataDir, 'cli');
|
|
815
|
+
mkdirSync(cliDir, { recursive: true });
|
|
816
|
+
const pkgPath = join(cliDir, 'package.json');
|
|
817
|
+
if (!existsSync(pkgPath)) {
|
|
818
|
+
writeFileAtomic(
|
|
819
|
+
pkgPath,
|
|
820
|
+
`${JSON.stringify({ name: 'agentdeck-cli', private: true }, null, 2)}\n`,
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (packageManager?.command) {
|
|
825
|
+
const res = await runCommand(
|
|
826
|
+
packageManager.command,
|
|
827
|
+
[...(packageManager.args || []), 'add', 'agentdeckd'],
|
|
828
|
+
{
|
|
829
|
+
cwd: cliDir,
|
|
830
|
+
env: { ...env, ...(packageManager.env || {}) },
|
|
831
|
+
timeoutMs: 180_000,
|
|
832
|
+
},
|
|
833
|
+
);
|
|
834
|
+
if (res.code === 0) {
|
|
835
|
+
const bin = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
836
|
+
if (bin) {
|
|
837
|
+
return { installed: true, method: 'local-pnpm', binaryPath: bin };
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
errors.push(
|
|
841
|
+
`bundled pnpm: ${(res.stderr || res.stdout || `exit ${res.code}`).trim().slice(-240)}`,
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (npmBin) {
|
|
846
|
+
const res = await runCommand(
|
|
847
|
+
npmBin,
|
|
848
|
+
['install', '--prefix', cliDir, 'agentdeckd', '--include=optional'],
|
|
849
|
+
{ env, timeoutMs: 180_000 },
|
|
850
|
+
);
|
|
851
|
+
if (res.code === 0) {
|
|
852
|
+
const bin = findAgentDeckDaemon({ dataDir, platform, env, home });
|
|
853
|
+
if (bin) {
|
|
854
|
+
return { installed: true, method: 'local-npm', binaryPath: bin };
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
errors.push(
|
|
858
|
+
`local npm: ${(res.stderr || res.stdout || `exit ${res.code}`).trim().slice(-240)}`,
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
return null;
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
const attempts =
|
|
865
|
+
platform === 'darwin'
|
|
866
|
+
? [tryBrew, tryNpmGlobal, tryLocalPrefix]
|
|
867
|
+
: platform === 'win32'
|
|
868
|
+
? [tryNpmGlobal, tryScoop, tryLocalPrefix]
|
|
869
|
+
: [tryNpmGlobal, tryBrew, tryLocalPrefix];
|
|
870
|
+
|
|
871
|
+
for (const attempt of attempts) {
|
|
872
|
+
try {
|
|
873
|
+
const result = await attempt();
|
|
874
|
+
if (result) return result;
|
|
875
|
+
} catch (err) {
|
|
876
|
+
errors.push(err instanceof Error ? err.message : String(err));
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
throw new Error(
|
|
881
|
+
`Failed to install agentdeckd automatically. ${errors.join(' | ')}`,
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Control the local `agentdeckd` autostart service (`start` / `stop` / `restart`).
|
|
887
|
+
* `agentdeckd start` registers OS autostart (launchd / systemd / Task Scheduler),
|
|
888
|
+
* starts the background daemon, and creates `daemon.json` with a Pairing ID.
|
|
889
|
+
*/
|
|
890
|
+
export async function controlAgentDeckDaemon({
|
|
891
|
+
action,
|
|
892
|
+
profileName = 'web',
|
|
893
|
+
dshHome = join(homedir(), '.dsh'),
|
|
894
|
+
sourcePatchPath = null,
|
|
895
|
+
dataDir = getAgentDeckDataDir(),
|
|
896
|
+
platform = process.platform,
|
|
897
|
+
packageManager = null,
|
|
898
|
+
} = {}) {
|
|
899
|
+
if (!['start', 'stop', 'restart'].includes(action)) {
|
|
900
|
+
throw new Error(`Unsupported daemon action: ${action}`);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
let binaryPath = findAgentDeckDaemon({ dataDir, platform });
|
|
904
|
+
if (!binaryPath && action === 'start') {
|
|
905
|
+
const installed = await installAgentDeckDaemon({
|
|
906
|
+
dataDir,
|
|
907
|
+
platform,
|
|
908
|
+
packageManager,
|
|
909
|
+
});
|
|
910
|
+
binaryPath = installed.binaryPath;
|
|
911
|
+
}
|
|
912
|
+
if (!binaryPath) {
|
|
913
|
+
throw new Error(
|
|
914
|
+
'Could not locate the `agentdeckd` executable. Click One-Click Setup first.',
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
const res = await runCommand(binaryPath, [action], { timeoutMs: 30_000 });
|
|
919
|
+
if (res.code !== 0) {
|
|
920
|
+
const detail = (res.stderr || res.stdout || '').trim().slice(-400);
|
|
921
|
+
throw new Error(
|
|
922
|
+
`agentdeckd ${action} failed (exit ${res.code})${detail ? `: ${detail}` : ''}`,
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// Wait briefly for daemon.lock / daemon.json to settle
|
|
927
|
+
const expectRunning = action !== 'stop';
|
|
928
|
+
for (let i = 0; i < 10; i++) {
|
|
929
|
+
const daemonState = await readDaemonState(dataDir, { platform });
|
|
930
|
+
if (daemonState.running === expectRunning) break;
|
|
931
|
+
await sleep(200);
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
const status = await getAgentDeckStatus({
|
|
935
|
+
profileName,
|
|
936
|
+
dshHome,
|
|
937
|
+
sourcePatchPath,
|
|
938
|
+
dataDir,
|
|
939
|
+
platform,
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
return { ok: true, action, status };
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
export async function getAgentDeckStatus({
|
|
946
|
+
profileName = 'web',
|
|
947
|
+
dshHome = join(homedir(), '.dsh'),
|
|
948
|
+
sourcePatchPath = null,
|
|
949
|
+
dataDir = getAgentDeckDataDir(),
|
|
950
|
+
platform = process.platform,
|
|
951
|
+
} = {}) {
|
|
952
|
+
const acpPatchPath = join(dshHome, 'profiles', 'acp', 'cordis.patch.yml');
|
|
953
|
+
const customAgentsPath = join(dataDir, 'custom_agents.json');
|
|
954
|
+
const dshCommand = resolveDshCommand({ platform });
|
|
955
|
+
const acpPlugin = readAcpPluginState(dshHome);
|
|
956
|
+
const sourceModel = extractModelSummary(sourcePatchPath);
|
|
957
|
+
const acpModel = extractModelSummary(acpPatchPath);
|
|
958
|
+
const customAgent = readCustomAgentState(customAgentsPath);
|
|
959
|
+
const daemon = await readDaemonState(dataDir, { platform });
|
|
960
|
+
|
|
961
|
+
const modelMismatch = Boolean(
|
|
962
|
+
sourceModel.defaultModel &&
|
|
963
|
+
acpModel.defaultModel &&
|
|
964
|
+
(sourceModel.defaultModel.provider !== acpModel.defaultModel.provider ||
|
|
965
|
+
sourceModel.defaultModel.model !== acpModel.defaultModel.model),
|
|
966
|
+
);
|
|
967
|
+
|
|
968
|
+
const acpNeedsProviderPin = Boolean(
|
|
969
|
+
acpModel.defaultModel?.provider &&
|
|
970
|
+
!BUILTIN_DEEPSEEK_PROVIDERS.has(acpModel.defaultModel.provider) &&
|
|
971
|
+
acpModel.acpPlugin?.provider !== acpModel.defaultModel.provider,
|
|
972
|
+
);
|
|
973
|
+
|
|
974
|
+
const ready = Boolean(
|
|
975
|
+
acpPlugin.installed &&
|
|
976
|
+
customAgent.configured &&
|
|
977
|
+
!acpNeedsProviderPin &&
|
|
978
|
+
daemon.running &&
|
|
979
|
+
daemon.pairedReady,
|
|
980
|
+
);
|
|
981
|
+
|
|
982
|
+
return {
|
|
983
|
+
platform,
|
|
984
|
+
profileName,
|
|
985
|
+
dshHome,
|
|
986
|
+
dshCommand,
|
|
987
|
+
ready,
|
|
988
|
+
acpPlugin,
|
|
989
|
+
models: {
|
|
990
|
+
source: sourceModel,
|
|
991
|
+
acp: acpModel,
|
|
992
|
+
mismatch: modelMismatch,
|
|
993
|
+
acpNeedsProviderPin,
|
|
994
|
+
},
|
|
995
|
+
customAgent,
|
|
996
|
+
daemon,
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
export async function runAgentDeckSetup({
|
|
1001
|
+
profileName = 'web',
|
|
1002
|
+
dshHome = join(homedir(), '.dsh'),
|
|
1003
|
+
sourcePatchPath = null,
|
|
1004
|
+
dataDir = getAgentDeckDataDir(),
|
|
1005
|
+
syncModels = false,
|
|
1006
|
+
startDaemon = true,
|
|
1007
|
+
platform = process.platform,
|
|
1008
|
+
packageManager = null,
|
|
1009
|
+
} = {}) {
|
|
1010
|
+
const steps = [];
|
|
1011
|
+
const dshCommand = resolveDshCommand({ platform });
|
|
1012
|
+
if (!dshCommand) {
|
|
1013
|
+
throw new Error(
|
|
1014
|
+
'Could not locate the `dsh` executable. Install DeepSeek Harness CLI or add `dsh` to your PATH.',
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
// Step 1: Ensure interactive ACP adapter is installed in the `acp` profile
|
|
1019
|
+
const acpState = readAcpPluginState(dshHome);
|
|
1020
|
+
if (acpState.installed) {
|
|
1021
|
+
steps.push({
|
|
1022
|
+
id: 'acp-plugin',
|
|
1023
|
+
status: 'skipped',
|
|
1024
|
+
detail: `${acpState.packageName}@${acpState.version}`,
|
|
1025
|
+
});
|
|
1026
|
+
} else {
|
|
1027
|
+
const result = await runCommand(
|
|
1028
|
+
dshCommand,
|
|
1029
|
+
['plugin', '--profile', 'acp', 'add', DEFAULT_ACP_PLUGIN],
|
|
1030
|
+
{
|
|
1031
|
+
env: { ...process.env, DSH_HOME: dshHome },
|
|
1032
|
+
},
|
|
1033
|
+
);
|
|
1034
|
+
if (result.code !== 0) {
|
|
1035
|
+
const detail = (result.stderr || result.stdout || '').trim().slice(-600);
|
|
1036
|
+
throw new Error(
|
|
1037
|
+
`Failed to install ${DEFAULT_ACP_PLUGIN} (exit ${result.code})${detail ? `: ${detail}` : ''}`,
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
steps.push({
|
|
1041
|
+
id: 'acp-plugin',
|
|
1042
|
+
status: 'installed',
|
|
1043
|
+
detail: DEFAULT_ACP_PLUGIN,
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
// Step 2: Sync model/provider config into `acp/cordis.patch.yml`
|
|
1048
|
+
const acpPatchPath = join(dshHome, 'profiles', 'acp', 'cordis.patch.yml');
|
|
1049
|
+
const syncResult = syncAcpPatch({
|
|
1050
|
+
sourcePatchPath,
|
|
1051
|
+
acpPatchPath,
|
|
1052
|
+
syncModels,
|
|
1053
|
+
});
|
|
1054
|
+
steps.push({
|
|
1055
|
+
id: 'model-sync',
|
|
1056
|
+
status: syncResult.changed ? 'updated' : 'unchanged',
|
|
1057
|
+
detail: acpPatchPath,
|
|
1058
|
+
});
|
|
1059
|
+
|
|
1060
|
+
// Step 3: Upsert `dsh` entry in AgentDeck's `custom_agents.json`
|
|
1061
|
+
const customAgentsPath = join(dataDir, 'custom_agents.json');
|
|
1062
|
+
const entry = upsertCustomAgent({ customAgentsPath, dshCommand });
|
|
1063
|
+
steps.push({
|
|
1064
|
+
id: 'custom-agents',
|
|
1065
|
+
status: 'updated',
|
|
1066
|
+
detail: `${entry.command} ${entry.args.join(' ')}`,
|
|
1067
|
+
});
|
|
1068
|
+
|
|
1069
|
+
// Step 4: Install `agentdeckd` if not already installed or running
|
|
1070
|
+
const daemonBefore = await readDaemonState(dataDir, { platform });
|
|
1071
|
+
let daemonBin = daemonBefore.binaryPath;
|
|
1072
|
+
if (daemonBefore.running) {
|
|
1073
|
+
steps.push({
|
|
1074
|
+
id: 'agentdeckd-install',
|
|
1075
|
+
status: 'skipped',
|
|
1076
|
+
detail: daemonBin || `running (PID ${daemonBefore.pid})`,
|
|
1077
|
+
});
|
|
1078
|
+
steps.push({
|
|
1079
|
+
id: 'agentdeckd-autostart',
|
|
1080
|
+
status: 'skipped',
|
|
1081
|
+
detail: `PID ${daemonBefore.pid}`,
|
|
1082
|
+
});
|
|
1083
|
+
} else {
|
|
1084
|
+
if (!daemonBin) {
|
|
1085
|
+
const installRes = await installAgentDeckDaemon({
|
|
1086
|
+
dataDir,
|
|
1087
|
+
platform,
|
|
1088
|
+
packageManager,
|
|
1089
|
+
});
|
|
1090
|
+
daemonBin = installRes.binaryPath;
|
|
1091
|
+
steps.push({
|
|
1092
|
+
id: 'agentdeckd-install',
|
|
1093
|
+
status: installRes.installed ? 'installed' : 'skipped',
|
|
1094
|
+
detail: `${installRes.method}: ${daemonBin}`,
|
|
1095
|
+
});
|
|
1096
|
+
} else {
|
|
1097
|
+
steps.push({
|
|
1098
|
+
id: 'agentdeckd-install',
|
|
1099
|
+
status: 'skipped',
|
|
1100
|
+
detail: daemonBin,
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// Step 5: Register autostart service & start `agentdeckd`
|
|
1105
|
+
if (!startDaemon) {
|
|
1106
|
+
steps.push({
|
|
1107
|
+
id: 'agentdeckd-autostart',
|
|
1108
|
+
status: 'skipped',
|
|
1109
|
+
detail: 'stopped by user',
|
|
1110
|
+
});
|
|
1111
|
+
} else {
|
|
1112
|
+
const startRes = await runCommand(daemonBin, ['start'], {
|
|
1113
|
+
timeoutMs: 30_000,
|
|
1114
|
+
});
|
|
1115
|
+
if (startRes.code !== 0) {
|
|
1116
|
+
const detail = (startRes.stderr || startRes.stdout || '').trim().slice(-400);
|
|
1117
|
+
throw new Error(
|
|
1118
|
+
`agentdeckd start failed (exit ${startRes.code})${detail ? `: ${detail}` : ''}`,
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
for (let i = 0; i < 10; i++) {
|
|
1122
|
+
const check = await readDaemonState(dataDir, { platform });
|
|
1123
|
+
if (check.running && check.pairedReady) break;
|
|
1124
|
+
await sleep(200);
|
|
1125
|
+
}
|
|
1126
|
+
steps.push({
|
|
1127
|
+
id: 'agentdeckd-autostart',
|
|
1128
|
+
status: 'started',
|
|
1129
|
+
detail: 'autostart enabled',
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
const status = await getAgentDeckStatus({
|
|
1135
|
+
profileName,
|
|
1136
|
+
dshHome,
|
|
1137
|
+
sourcePatchPath,
|
|
1138
|
+
dataDir,
|
|
1139
|
+
platform,
|
|
1140
|
+
});
|
|
1141
|
+
|
|
1142
|
+
return { ok: true, steps, status };
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Update `daemon.json` `awake` mode atomically (preserving `relay_id`, `relay_url`,
|
|
1147
|
+
* and `features`). A running `agentdeckd` automatically hot-reloads this setting.
|
|
1148
|
+
*/
|
|
1149
|
+
export async function setAgentDeckAwake({
|
|
1150
|
+
mode,
|
|
1151
|
+
profileName = 'web',
|
|
1152
|
+
dshHome = join(homedir(), '.dsh'),
|
|
1153
|
+
sourcePatchPath = null,
|
|
1154
|
+
dataDir = getAgentDeckDataDir(),
|
|
1155
|
+
platform = process.platform,
|
|
1156
|
+
} = {}) {
|
|
1157
|
+
if (!AWAKE_MODES.includes(mode)) {
|
|
1158
|
+
throw new Error(`Invalid awake mode "${mode}". Expected one of: ${AWAKE_MODES.join(', ')}`);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
const configPath = join(dataDir, 'daemon.json');
|
|
1162
|
+
let config = {};
|
|
1163
|
+
if (existsSync(configPath)) {
|
|
1164
|
+
try {
|
|
1165
|
+
const parsed = JSON.parse(readFileSync(configPath, 'utf8'));
|
|
1166
|
+
if (parsed && typeof parsed === 'object') config = parsed;
|
|
1167
|
+
} catch {
|
|
1168
|
+
throw new Error(`Invalid daemon config at ${configPath}`);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
config.awake = mode;
|
|
1172
|
+
writeFileAtomic(configPath, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
1173
|
+
|
|
1174
|
+
// If daemon is running, wait briefly for awake_status.json to reflect the new mode
|
|
1175
|
+
const awakeStatusPath = join(dataDir, 'awake_status.json');
|
|
1176
|
+
const lockPath = join(dataDir, 'daemon.lock');
|
|
1177
|
+
let runningPid = null;
|
|
1178
|
+
if (existsSync(lockPath)) {
|
|
1179
|
+
const p = Number.parseInt(readFileSync(lockPath, 'utf8').trim(), 10);
|
|
1180
|
+
if (isPidAlive(p)) runningPid = p;
|
|
1181
|
+
}
|
|
1182
|
+
if (runningPid) {
|
|
1183
|
+
for (let i = 0; i < 12; i++) {
|
|
1184
|
+
if (existsSync(awakeStatusPath)) {
|
|
1185
|
+
try {
|
|
1186
|
+
const st = JSON.parse(readFileSync(awakeStatusPath, 'utf8'));
|
|
1187
|
+
if (st?.pid === runningPid && st?.mode === mode) break;
|
|
1188
|
+
} catch {
|
|
1189
|
+
// Ignore transient read
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
await sleep(100);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
const status = await getAgentDeckStatus({
|
|
1197
|
+
profileName,
|
|
1198
|
+
dshHome,
|
|
1199
|
+
sourcePatchPath,
|
|
1200
|
+
dataDir,
|
|
1201
|
+
platform,
|
|
1202
|
+
});
|
|
1203
|
+
return { ok: true, mode, status };
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
export async function syncAgentDeckModels({
|
|
1207
|
+
profileName = 'web',
|
|
1208
|
+
dshHome = join(homedir(), '.dsh'),
|
|
1209
|
+
sourcePatchPath = null,
|
|
1210
|
+
dataDir = getAgentDeckDataDir(),
|
|
1211
|
+
platform = process.platform,
|
|
1212
|
+
} = {}) {
|
|
1213
|
+
const acpPatchPath = join(dshHome, 'profiles', 'acp', 'cordis.patch.yml');
|
|
1214
|
+
syncAcpPatch({ sourcePatchPath, acpPatchPath, syncModels: true });
|
|
1215
|
+
const status = await getAgentDeckStatus({
|
|
1216
|
+
profileName,
|
|
1217
|
+
dshHome,
|
|
1218
|
+
sourcePatchPath,
|
|
1219
|
+
dataDir,
|
|
1220
|
+
platform,
|
|
1221
|
+
});
|
|
1222
|
+
return { ok: true, status };
|
|
1223
|
+
}
|