dsh-plugin-agentdeck 0.1.0 → 0.2.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/lib/index.js CHANGED
@@ -1,17 +1,25 @@
1
1
  import { homedir } from 'node:os';
2
2
  import { join } from 'node:path';
3
3
  import {
4
+ AWAKE_MODES,
5
+ checkDaemonUpdate,
4
6
  controlAgentDeckDaemon,
5
7
  getAgentDeckStatus,
6
8
  runAgentDeckSetup,
9
+ setAgentDeckAwake,
10
+ syncAgentDeckModels,
11
+ upgradeAgentDeckDaemon,
7
12
  } from './setup.js';
8
13
 
9
14
  export const name = 'agentdeck';
10
15
  export const inject = ['webServer'];
11
16
 
12
17
  const STATUS_PATH = '/api/agentdeck/status';
18
+ const UPDATE_CHECK_PATH = '/api/agentdeck/update-check';
13
19
  const SETUP_PATH = '/api/agentdeck/setup';
14
20
  const DAEMON_PATH = '/api/agentdeck/daemon';
21
+ const AWAKE_PATH = '/api/agentdeck/awake';
22
+ const SYNC_MODELS_PATH = '/api/agentdeck/sync-models';
15
23
  const MAX_BODY_BYTES = 16 * 1024;
16
24
 
17
25
  function sendJson(res, statusCode, payload) {
@@ -58,7 +66,7 @@ async function parseJsonRequest(req, res) {
58
66
  .split(';', 1)[0]
59
67
  .trim()
60
68
  .toLowerCase();
61
- if (contentType && contentType !== 'application/json') {
69
+ if (contentType !== 'application/json') {
62
70
  sendJson(res, 415, {
63
71
  ok: false,
64
72
  error: 'content-type must be application/json',
@@ -101,6 +109,57 @@ function getProfileInfo(ctx) {
101
109
 
102
110
  export function apply(ctx) {
103
111
  let opInFlight = null;
112
+ let disposed = false;
113
+ let provisioning = { state: 'idle', error: null };
114
+
115
+ const startAutoProvision = ({ forceStartDaemon = false } = {}) => {
116
+ if (opInFlight || disposed) return opInFlight;
117
+ const info = getProfileInfo(ctx);
118
+ provisioning = { state: 'running', error: null };
119
+ const task = (async () => {
120
+ try {
121
+ const current = await getAgentDeckStatus(info);
122
+ const needsSetup =
123
+ !current.acpPlugin.installed ||
124
+ !current.customAgent.configured ||
125
+ current.models.acpNeedsProviderPin ||
126
+ !current.daemon.installed ||
127
+ (!current.daemon.running &&
128
+ (forceStartDaemon || !current.daemon.pairedReady));
129
+ if (!needsSetup || disposed) {
130
+ provisioning = { state: 'idle', error: null };
131
+ return { ok: true, status: current };
132
+ }
133
+ const result = await runAgentDeckSetup({
134
+ ...info,
135
+ syncModels: false,
136
+ startDaemon: forceStartDaemon || !current.daemon.pairedReady || !current.customAgent.configured,
137
+ });
138
+ provisioning = { state: 'idle', error: null };
139
+ return result;
140
+ } catch (err) {
141
+ const message = err instanceof Error ? err.message : String(err);
142
+ provisioning = { state: 'error', error: message };
143
+ throw err;
144
+ } finally {
145
+ if (opInFlight === task) opInFlight = null;
146
+ }
147
+ })();
148
+ opInFlight = task;
149
+ return task;
150
+ };
151
+
152
+ // Automatically provision ACP, custom_agents.json, and agentdeckd in the
153
+ // background when the plugin activates so the QR code is ready without clicks.
154
+ ctx.effect(() => {
155
+ disposed = false;
156
+ startAutoProvision().catch(() => {
157
+ // Error is captured in `provisioning` and surfaced in the Settings UI
158
+ });
159
+ return () => {
160
+ disposed = true;
161
+ };
162
+ }, 'agentdeck: auto-provision');
104
163
 
105
164
  const isRejected = (req, res) => {
106
165
  const connection = ctx.get?.('connection') ?? Reflect.get(ctx, 'connection');
@@ -111,6 +170,12 @@ export function apply(ctx) {
111
170
  return true;
112
171
  };
113
172
 
173
+ const buildStatusPayload = async () => {
174
+ const info = getProfileInfo(ctx);
175
+ const status = await getAgentDeckStatus(info);
176
+ return { ...status, provisioning };
177
+ };
178
+
114
179
  ctx.effect(
115
180
  () =>
116
181
  ctx.webServer.register({
@@ -123,9 +188,7 @@ export function apply(ctx) {
123
188
  return;
124
189
  }
125
190
  try {
126
- const info = getProfileInfo(ctx);
127
- const status = await getAgentDeckStatus(info);
128
- sendJson(res, 200, status);
191
+ sendJson(res, 200, await buildStatusPayload());
129
192
  } catch (err) {
130
193
  sendJson(res, 500, {
131
194
  ok: false,
@@ -137,6 +200,34 @@ export function apply(ctx) {
137
200
  `agentdeck: GET ${STATUS_PATH}`,
138
201
  );
139
202
 
203
+ ctx.effect(
204
+ () =>
205
+ ctx.webServer.register({
206
+ kind: 'exact',
207
+ path: UPDATE_CHECK_PATH,
208
+ handler: async (req, res) => {
209
+ if (isRejected(req, res)) return;
210
+ if (req.method !== 'GET') {
211
+ sendMethodNotAllowed(res, 'GET');
212
+ return;
213
+ }
214
+ try {
215
+ const url = new URL(req.url || UPDATE_CHECK_PATH, 'http://localhost');
216
+ const force = url.searchParams.get('force') === '1';
217
+ const update = await checkDaemonUpdate({ force });
218
+ sendJson(res, 200, { ok: true, ...update });
219
+ } catch (err) {
220
+ sendJson(res, 500, {
221
+ ok: false,
222
+ error: err instanceof Error ? err.message : String(err),
223
+ });
224
+ }
225
+ },
226
+ }),
227
+ `agentdeck: GET ${UPDATE_CHECK_PATH}`,
228
+ );
229
+
230
+ // Retry/repair endpoint if auto-provisioning failed or user wants to re-run
140
231
  ctx.effect(
141
232
  () =>
142
233
  ctx.webServer.register({
@@ -152,29 +243,25 @@ export function apply(ctx) {
152
243
  if (parsed === null) return;
153
244
 
154
245
  if (opInFlight) {
155
- sendJson(res, 409, {
156
- ok: false,
157
- error: 'Another AgentDeck operation is already in progress',
246
+ sendJson(res, 202, {
247
+ ok: true,
248
+ status: await buildStatusPayload(),
158
249
  });
159
250
  return;
160
251
  }
161
252
 
162
- const info = getProfileInfo(ctx);
163
- const task = runAgentDeckSetup({
164
- ...info,
165
- syncModels: Boolean(parsed?.syncModels),
166
- });
167
- opInFlight = task;
168
253
  try {
169
- const result = await task;
170
- sendJson(res, 200, result);
254
+ const result = await startAutoProvision({ forceStartDaemon: true });
255
+ sendJson(res, 200, {
256
+ ok: true,
257
+ status: { ...result.status, provisioning },
258
+ });
171
259
  } catch (err) {
172
260
  sendJson(res, 500, {
173
261
  ok: false,
174
262
  error: err instanceof Error ? err.message : String(err),
263
+ status: await buildStatusPayload(),
175
264
  });
176
- } finally {
177
- if (opInFlight === task) opInFlight = null;
178
265
  }
179
266
  },
180
267
  }),
@@ -196,10 +283,10 @@ export function apply(ctx) {
196
283
  if (parsed === null) return;
197
284
 
198
285
  const action = parsed?.action;
199
- if (!['start', 'stop', 'restart'].includes(action)) {
286
+ if (!['start', 'stop', 'restart', 'upgrade'].includes(action)) {
200
287
  sendJson(res, 400, {
201
288
  ok: false,
202
- error: 'action must be "start", "stop", or "restart"',
289
+ error: 'action must be "start", "stop", "restart", or "upgrade"',
203
290
  });
204
291
  return;
205
292
  }
@@ -213,15 +300,35 @@ export function apply(ctx) {
213
300
  }
214
301
 
215
302
  const info = getProfileInfo(ctx);
216
- const task = controlAgentDeckDaemon({ ...info, action });
303
+ const task =
304
+ action === 'upgrade'
305
+ ? upgradeAgentDeckDaemon(info)
306
+ : controlAgentDeckDaemon({ ...info, action });
217
307
  opInFlight = task;
218
308
  try {
219
309
  const result = await task;
220
- sendJson(res, 200, result);
310
+ sendJson(res, 200, {
311
+ ok: true,
312
+ status: { ...result.status, provisioning },
313
+ ...(action === 'upgrade'
314
+ ? {
315
+ previousVersion: result.previousVersion ?? null,
316
+ version: result.version ?? null,
317
+ method: result.method ?? null,
318
+ }
319
+ : {}),
320
+ });
221
321
  } catch (err) {
322
+ let statusPayload;
323
+ try {
324
+ statusPayload = await buildStatusPayload();
325
+ } catch {
326
+ // Ignore secondary status failure
327
+ }
222
328
  sendJson(res, 500, {
223
329
  ok: false,
224
330
  error: err instanceof Error ? err.message : String(err),
331
+ ...(statusPayload ? { status: statusPayload } : {}),
225
332
  });
226
333
  } finally {
227
334
  if (opInFlight === task) opInFlight = null;
@@ -230,4 +337,77 @@ export function apply(ctx) {
230
337
  }),
231
338
  `agentdeck: POST ${DAEMON_PATH}`,
232
339
  );
340
+
341
+ ctx.effect(
342
+ () =>
343
+ ctx.webServer.register({
344
+ kind: 'exact',
345
+ path: AWAKE_PATH,
346
+ handler: async (req, res) => {
347
+ if (isRejected(req, res)) return;
348
+ if (req.method !== 'POST') {
349
+ sendMethodNotAllowed(res, 'POST');
350
+ return;
351
+ }
352
+ const parsed = await parseJsonRequest(req, res);
353
+ if (parsed === null) return;
354
+
355
+ const mode = parsed?.mode;
356
+ if (!AWAKE_MODES.includes(mode)) {
357
+ sendJson(res, 400, {
358
+ ok: false,
359
+ error: `mode must be one of: ${AWAKE_MODES.join(', ')}`,
360
+ });
361
+ return;
362
+ }
363
+
364
+ try {
365
+ const info = getProfileInfo(ctx);
366
+ const result = await setAgentDeckAwake({ ...info, mode });
367
+ sendJson(res, 200, {
368
+ ok: true,
369
+ status: { ...result.status, provisioning },
370
+ });
371
+ } catch (err) {
372
+ sendJson(res, 500, {
373
+ ok: false,
374
+ error: err instanceof Error ? err.message : String(err),
375
+ });
376
+ }
377
+ },
378
+ }),
379
+ `agentdeck: POST ${AWAKE_PATH}`,
380
+ );
381
+
382
+ ctx.effect(
383
+ () =>
384
+ ctx.webServer.register({
385
+ kind: 'exact',
386
+ path: SYNC_MODELS_PATH,
387
+ handler: async (req, res) => {
388
+ if (isRejected(req, res)) return;
389
+ if (req.method !== 'POST') {
390
+ sendMethodNotAllowed(res, 'POST');
391
+ return;
392
+ }
393
+ const parsed = await parseJsonRequest(req, res);
394
+ if (parsed === null) return;
395
+
396
+ try {
397
+ const info = getProfileInfo(ctx);
398
+ const result = await syncAgentDeckModels(info);
399
+ sendJson(res, 200, {
400
+ ok: true,
401
+ status: { ...result.status, provisioning },
402
+ });
403
+ } catch (err) {
404
+ sendJson(res, 500, {
405
+ ok: false,
406
+ error: err instanceof Error ? err.message : String(err),
407
+ });
408
+ }
409
+ },
410
+ }),
411
+ `agentdeck: POST ${SYNC_MODELS_PATH}`,
412
+ );
233
413
  }