mindwire 0.1.25 → 0.1.26

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/dist/cli.js CHANGED
@@ -1,2165 +1,404 @@
1
1
  #!/usr/bin/env node
2
- import { MindwireError, TimeoutError, ApiError, RunFailedError, binaryCacheDirectory, cachedExecutable, verifyReleaseBytes, cacheExecutable, executableChecksum } from './chunk-PFYKJ6LX.js';
2
+ import { defaultStateDirectory, superviseComputer, ensureComputer, computerPairingURI, stopComputer, computerClient, SDK_VERSION, readJSON, writeJSON, currentProcessIdentity, processStateAlive, acquireProcessLock, computerPairingQRData } from './chunk-L3USUTZZ.js';
3
+ import './chunk-T6TD76UA.js';
3
4
  import { promisify, parseArgs } from 'util';
4
- import { fileURLToPath } from 'url';
5
- import { realpathSync } from 'fs';
6
- import * as path2 from 'path';
7
- import { createHash, randomBytes, randomUUID } from 'crypto';
5
+ import { fileURLToPath, pathToFileURL } from 'url';
6
+ import { realpathSync, mkdtempSync, chmodSync, writeFileSync, rmSync } from 'fs';
7
+ import * as path from 'path';
8
+ import { join } from 'path';
9
+ import { createHash } from 'crypto';
8
10
  import { createInterface } from 'readline/promises';
9
11
  import { setTimeout as setTimeout$1 } from 'timers/promises';
10
- import { execFile, execFileSync, spawn } from 'child_process';
12
+ import { execFile, spawn } from 'child_process';
13
+ import { homedir, tmpdir } from 'os';
11
14
  import * as fs from 'fs/promises';
12
- import { readFile, mkdtemp, writeFile, rm } from 'fs/promises';
13
- import { homedir, networkInterfaces, tmpdir } from 'os';
14
15
 
15
- // src/computer.ts
16
- var ComputerApi = class {
17
- constructor(client) {
18
- this.client = client;
19
- }
20
- client;
21
- info() {
22
- return this.client.http.request("GET", "/computer");
23
- }
24
- setRoutes(routes) {
25
- return this.client.http.request("PUT", "/computer/routes", { body: { routes } });
26
- }
27
- updateStatus() {
28
- return this.client.http.request("GET", "/computer/update");
29
- }
30
- requestUpdate(version) {
31
- return this.client.http.request("POST", "/computer/update", { body: { version } });
32
- }
33
- invite(routes) {
34
- return this.client.http.request("POST", "/computer/pairings", { body: { routes } });
35
- }
36
- pairing(id) {
37
- return this.client.http.request("GET", `/computer/pairings/${encodeURIComponent(id)}`);
38
- }
39
- completePairing(id, requestId) {
40
- return this.client.http.request("POST", `/computer/pairings/${encodeURIComponent(id)}/complete`, { body: { requestId } });
41
- }
42
- decide(id, requestId, approve) {
43
- return this.client.http.request("POST", `/computer/pairings/${encodeURIComponent(id)}/decision`, { body: { requestId, approve } });
44
- }
45
- devices() {
46
- return this.client.http.request("GET", "/computer/devices");
47
- }
48
- revoke(id) {
49
- return this.client.http.request("DELETE", `/computer/devices/${encodeURIComponent(id)}`);
50
- }
51
- forwards() {
52
- return this.client.http.request("GET", "/computer/forwards");
53
- }
54
- /** Authorize a loopback port for this paired device. Renew the same ID every 30s;
55
- * grants expire after two minutes. Carry traffic using SSH direct-tcpip. */
56
- forward(request) {
57
- return this.client.http.request("POST", "/computer/forwards", { body: request });
58
- }
59
- closeForward(id) {
60
- return this.client.http.request("DELETE", `/computer/forwards/${encodeURIComponent(id)}`);
61
- }
62
- };
63
- function computerPairingURI(invitation) {
64
- const bytes = new TextEncoder().encode(JSON.stringify(invitation));
65
- let binary = "";
66
- for (const byte of bytes) binary += String.fromCharCode(byte);
67
- const encoded = btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
68
- return `mindwire://pair?v=1#${encoded}`;
69
- }
70
-
71
- // src/version.ts
72
- var SDK_VERSION = "0.1.25" ;
73
-
74
- // src/http.ts
75
- var DEFAULT_TIMEOUT_MS = 12e4;
76
- function isAbortError(e) {
77
- return typeof e === "object" && e !== null && e.name === "AbortError";
78
- }
79
- var Http = class {
80
- fetchImpl;
81
- baseHeaders;
82
- resolver;
83
- timeoutMs;
84
- cached = null;
85
- pending = null;
86
- constructor(opts) {
87
- const f = opts.fetch ?? globalThis.fetch;
88
- if (!f) {
89
- throw new MindwireError(
90
- "mindwire: no global fetch found \u2014 pass a `fetch` implementation in the client options"
91
- );
92
- }
93
- this.fetchImpl = f;
94
- this.baseHeaders = { ...opts.headers };
95
- this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
96
- if (opts.baseUrl) {
97
- const base = { baseUrl: opts.baseUrl.replace(/\/+$/, ""), token: opts.token };
98
- this.resolver = async () => base;
99
- } else if (opts.resolveBase) {
100
- const rb = opts.resolveBase;
101
- this.resolver = async () => {
102
- const r = await rb();
103
- return {
104
- baseUrl: r.baseUrl.replace(/\/+$/, ""),
105
- token: r.token ?? opts.token,
106
- getToken: r.getToken,
107
- headers: r.headers,
108
- fetch: r.fetch
109
- };
110
- };
111
- } else {
112
- throw new MindwireError("mindwire: Http requires a baseUrl or a resolveBase");
113
- }
114
- }
115
- /**
116
- * One fetch, with raw network failures wrapped as {@link MindwireError} so callers see a typed
117
- * SDK error instead of a bare `TypeError: fetch failed`. An abort (the request's own signal, or
118
- * our timeout controller) is re-thrown untouched — `fetchWithTimeout` classifies it.
119
- */
120
- async fetchOnce(f, url, init, method, path4) {
121
- try {
122
- return await f(url, init);
123
- } catch (e) {
124
- const aborted = init.signal?.aborted ?? false;
125
- if (aborted || isAbortError(e)) throw e;
126
- throw new MindwireError(`mindwire: ${method} ${path4} \u2014 network request failed`, { cause: e });
127
- }
128
- }
129
- /**
130
- * `fetchOnce` plus a timeout that composes with the caller's `signal`. A hand-rolled controller
131
- * (not `AbortSignal.timeout`/`AbortSignal.any`, which need Node 20.3+/18.17+) fires after
132
- * `timeoutMs`; whichever aborts first wins. Timeout → {@link TimeoutError}; the caller's own abort
133
- * propagates as-is (its `reason`). `timeoutMs<=0` disables the deadline entirely.
134
- */
135
- async fetchWithTimeout(f, url, init, userSignal, method, path4) {
136
- if (!this.timeoutMs || this.timeoutMs <= 0) {
137
- return this.fetchOnce(f, url, { ...init, ...userSignal ? { signal: userSignal } : {} }, method, path4);
138
- }
139
- const ctrl = new AbortController();
140
- const onAbort = () => ctrl.abort(userSignal?.reason);
141
- if (userSignal) {
142
- if (userSignal.aborted) ctrl.abort(userSignal.reason);
143
- else userSignal.addEventListener("abort", onAbort, { once: true });
144
- }
145
- let timedOut = false;
146
- const timer = setTimeout(() => {
147
- timedOut = true;
148
- ctrl.abort();
149
- }, this.timeoutMs);
150
- try {
151
- return await this.fetchOnce(f, url, { ...init, signal: ctrl.signal }, method, path4);
152
- } catch (e) {
153
- if (timedOut) throw new TimeoutError(method, path4, this.timeoutMs);
154
- if (userSignal?.aborted) throw userSignal.reason ?? e;
155
- throw e;
156
- } finally {
157
- clearTimeout(timer);
158
- if (userSignal) userSignal.removeEventListener("abort", onAbort);
159
- }
160
- }
161
- /**
162
- * The bearer token for the next request. Prefers the transport's dynamic `getToken` (which
163
- * caches and mints on demand); falls back to the static token resolved at base time. `force`
164
- * asks the getter to re-mint — used once on a 401 before retrying.
165
- */
166
- async authToken(base, force) {
167
- if (base.getToken) {
168
- const t = await base.getToken(force ? { force: true } : void 0);
169
- return t ?? base.token;
170
- }
171
- return base.token;
172
- }
173
- base() {
174
- if (this.cached) return Promise.resolve(this.cached);
175
- if (!this.pending) {
176
- this.pending = this.resolver().then((b) => this.cached = b).catch((err) => {
177
- this.pending = null;
178
- throw err;
179
- });
180
- }
181
- return this.pending;
182
- }
183
- /**
184
- * Resolve the base eagerly and memoize it — provisions the transport (spawns the embedded daemon,
185
- * connects the sandbox/SSH/Docker target, …) now instead of on the first request. Idempotent: this
186
- * awaits the *same* memoized promise the first `request()`/`open()` awaits, so the target's
187
- * `connect()` fires exactly once. Backs {@link import("./client.js").Mindwire.ensure}.
188
- */
189
- async ready() {
190
- await this.base();
191
- }
192
- url(baseUrl, path4, query) {
193
- const u = new URL(baseUrl + (path4.startsWith("/") ? path4 : `/${path4}`));
194
- if (query) {
195
- for (const [k, v] of Object.entries(query)) {
196
- if (v !== void 0) u.searchParams.set(k, String(v));
197
- }
198
- }
199
- return u.toString();
200
- }
201
- headers(token, extra, hasBody = false) {
202
- const h = { Accept: "application/json", ...this.baseHeaders, ...extra };
203
- if (token) h["Authorization"] = `Bearer ${token}`;
204
- if (hasBody) h["Content-Type"] = "application/json";
205
- return h;
206
- }
207
- async request(method, path4, init = {}) {
208
- const base = await this.base();
209
- const hasBody = init.body !== void 0;
210
- const url = this.url(base.baseUrl, path4, init.query);
211
- const extra = { ...base.headers, ...init.headers };
212
- const f = base.fetch ?? this.fetchImpl;
213
- const send = async (force) => this.fetchWithTimeout(
214
- f,
215
- url,
216
- {
217
- method,
218
- headers: this.headers(await this.authToken(base, force), extra, hasBody),
219
- body: hasBody ? JSON.stringify(init.body) : void 0
220
- },
221
- init.signal,
222
- method,
223
- path4
224
- );
225
- let res = await send(false);
226
- if (res.status === 401 && base.getToken) res = await send(true);
227
- if (!res.ok) throw await this.toApiError(method, res);
228
- if (res.status === 204) return void 0;
229
- const text = await res.text();
230
- if (text === "") return void 0;
231
- try {
232
- return JSON.parse(text);
233
- } catch (e) {
234
- throw new MindwireError(`mindwire: ${method} ${path4} returned a non-JSON body`, { cause: e });
235
- }
236
- }
237
- /** Open a streaming response (SSE). Caller owns the body. */
238
- async open(method, path4, init = {}) {
239
- const base = await this.base();
240
- const url = this.url(base.baseUrl, path4, init.query);
241
- const extra = { Accept: "text/event-stream", ...base.headers, ...init.headers };
242
- const f = base.fetch ?? this.fetchImpl;
243
- const send = async (force) => this.fetchOnce(
244
- f,
245
- url,
246
- {
247
- method,
248
- headers: this.headers(await this.authToken(base, force), extra, init.body !== void 0),
249
- ...init.body !== void 0 ? { body: JSON.stringify(init.body) } : {},
250
- ...init.signal ? { signal: init.signal } : {}
251
- },
252
- method,
253
- path4
254
- );
255
- let res = await send(false);
256
- if (res.status === 401 && base.getToken) res = await send(true);
257
- if (!res.ok) throw await this.toApiError(method, res);
258
- if (!res.body)
259
- throw new MindwireError(`mindwire: ${method} ${path4} returned no response body to stream`);
260
- return res;
261
- }
262
- async toApiError(method, res) {
263
- let body = "";
264
- try {
265
- const text = await res.text();
266
- try {
267
- body = text ? JSON.parse(text) : "";
268
- } catch {
269
- body = text;
270
- }
271
- } catch {
272
- }
273
- return new ApiError({ status: res.status, url: res.url, method, body });
274
- }
275
- };
276
-
277
- // src/sse.ts
278
- async function* readSSE(body, signal) {
279
- const reader = body.getReader();
280
- const decoder = new TextDecoder();
281
- let buffer = "";
282
- const onAbort = () => void reader.cancel().catch(() => {
283
- });
284
- if (signal) {
285
- if (signal.aborted) {
286
- await reader.cancel().catch(() => {
287
- });
288
- return;
289
- }
290
- signal.addEventListener("abort", onAbort, { once: true });
291
- }
292
- try {
293
- for (; ; ) {
294
- const { done, value } = await reader.read();
295
- if (done) break;
296
- buffer += decoder.decode(value, { stream: true });
297
- let sep;
298
- while ((sep = indexOfBlankLine(buffer)) !== -1) {
299
- const rawEvent = buffer.slice(0, sep);
300
- buffer = buffer.slice(sepEnd(buffer, sep));
301
- const payload2 = parseEvent(rawEvent);
302
- if (payload2 !== void 0) yield JSON.parse(payload2);
303
- }
304
- }
305
- const payload = parseEvent(buffer);
306
- if (payload !== void 0) yield JSON.parse(payload);
307
- } finally {
308
- if (signal) signal.removeEventListener("abort", onAbort);
309
- await reader.cancel().catch(() => {
310
- });
311
- reader.releaseLock();
312
- }
313
- }
314
- function indexOfBlankLine(s) {
315
- const a = s.indexOf("\n\n");
316
- const b = s.indexOf("\r\n\r\n");
317
- if (a === -1) return b;
318
- if (b === -1) return a;
319
- return Math.min(a, b);
320
- }
321
- function sepEnd(s, idx) {
322
- return s.startsWith("\r\n\r\n", idx) ? idx + 4 : idx + 2;
323
- }
324
- function parseEvent(block) {
325
- const lines = block.split(/\r\n|\n/);
326
- const data = [];
327
- for (const line of lines) {
328
- if (line === "" || line.startsWith(":")) continue;
329
- const colon = line.indexOf(":");
330
- const field = colon === -1 ? line : line.slice(0, colon);
331
- if (field !== "data") continue;
332
- let value = colon === -1 ? "" : line.slice(colon + 1);
333
- if (value.startsWith(" ")) value = value.slice(1);
334
- data.push(value);
335
- }
336
- if (data.length === 0) return void 0;
337
- return data.join("\n");
338
- }
339
-
340
- // src/run.ts
341
- var TERMINAL = /* @__PURE__ */ new Set(["done", "error", "cancelled"]);
342
- var Run = class _Run {
343
- data;
344
- http;
345
- constructor(http, data) {
346
- this.http = http;
347
- this.data = data;
348
- }
349
- get id() {
350
- return this.data.id;
351
- }
352
- get chatId() {
353
- return this.data.chatId;
354
- }
355
- get agent() {
356
- return this.data.agent;
357
- }
358
- get status() {
359
- return this.data.status;
360
- }
361
- /** `"resolve"` on the parent of a global-resolve run; `undefined` for an ordinary turn. */
362
- get kind() {
363
- return this.data.kind;
364
- }
365
- /** On a child iteration of a resolve run, the id of its parent resolve run; else `undefined`. */
366
- get parentId() {
367
- return this.data.parentId;
368
- }
369
- /** Parent of a resolve run only: why the loop ended (`"done"` | `"capped"` | `"error"` | …). */
370
- get stopReason() {
371
- return this.data.stopReason;
372
- }
373
- /** Parent of a resolve run only: how many child turns the loop ran. */
374
- get iterations() {
375
- return this.data.iterations;
376
- }
377
- /** The latest known run record. */
378
- get value() {
379
- return this.data;
380
- }
381
- /** Unified SSE event stream: replay buffer, then live events, then close. */
382
- async *stream(opts = {}) {
383
- const cursor = opts.after === void 0 ? "" : `?after=${encodeURIComponent(opts.after)}`;
384
- const res = await this.http.open("GET", `/runs/${encodeURIComponent(this.id)}/stream${cursor}`, {
385
- ...opts.signal ? { signal: opts.signal } : {}
386
- });
387
- for await (const ev of readSSE(res.body, opts.signal)) {
388
- if (!opts.includeOpenSentinel && ev.type === "status" && ev.meta?.["stream"] === "open") {
389
- continue;
390
- }
391
- yield ev;
392
- }
393
- }
394
- /** `for await (const ev of run)` — sugar for `run.stream()`. */
395
- [Symbol.asyncIterator]() {
396
- return this.stream();
397
- }
398
- /** Cancel the in-flight turn (kills the underlying agent process). */
399
- async cancel() {
400
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/cancel`);
401
- }
402
- /**
403
- * Answer a pending permission, question or plan. Message-mode questions remain answerable
404
- * after completion; the daemon steers or resumes the conversation. Read the chat's latest
405
- * run after replying to a completed run. Requires the agent's `respond` capability.
406
- */
407
- async respond(input = {}) {
408
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/respond`, { body: input });
409
- }
410
- /**
411
- * Steer a follow-up message into the running turn without cancelling it. Requires the agent's
412
- * `input` capability.
413
- */
414
- async sendInput(text) {
415
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/input`, { body: { text } });
416
- }
417
- /**
418
- * Soft-stop the running turn (ask the agent to halt current work) without the hard process kill
419
- * {@link Run.cancel} does — the turn stays open for a follow-up via {@link Run.sendInput}.
420
- * Requires the agent's `interrupt` capability.
421
- */
422
- async interrupt() {
423
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/interrupt`);
424
- }
425
- /**
426
- * Switch the model of the live turn. An empty/omitted `model` resets the turn to the agent/CLI
427
- * default. Only meaningful on a persistent (non-bypass) turn; on a one-shot turn it is a
428
- * best-effort no-op. Requires the agent's `setModel` capability.
429
- */
430
- async setModel(model) {
431
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/set-model`, {
432
- body: { model: model ?? "" }
433
- });
434
- }
435
- /**
436
- * Switch the live permission mode using a value from the agent's settings schema.
437
- * Resolves after the harness acknowledges the change; rejects if it cannot apply it.
438
- * Requires `setPermissionMode`. Codex settings apply on the next turn instead.
439
- */
440
- async setPermissionMode(mode) {
441
- await this.http.request("POST", `/runs/${encodeURIComponent(this.id)}/set-permission-mode`, {
442
- body: { mode }
443
- });
444
- }
445
- /**
446
- * `GET /runs/{id}/children` — the child iterations of a global-resolve run, oldest→newest, each as
447
- * its own {@link Run} handle. An ordinary turn (or a resolve that ran a single iteration) returns an
448
- * empty array. Use it to inspect the run tree a {@link Mindwire.resolve} produced.
449
- */
450
- async children() {
451
- const data = await this.http.request(
452
- "GET",
453
- `/runs/${encodeURIComponent(this.id)}/children`
454
- );
455
- return data.map((d) => new _Run(this.http, d));
456
- }
457
- /** Re-fetch the run record from the daemon and update this handle. */
458
- async refresh() {
459
- this.data = await this.http.request("GET", `/runs/${encodeURIComponent(this.id)}`);
460
- return this.data;
461
- }
462
- /** Restore current output once, then follow with `stream({ after: snapshot.sequence })`. */
463
- async snapshot() {
464
- const snapshot = await this.http.request("GET", `/runs/${encodeURIComponent(this.id)}/snapshot`);
465
- this.data = snapshot.run;
466
- return snapshot;
467
- }
468
- /**
469
- * Consume the event stream to completion. Returns the final run record and the `result`
470
- * event's summary (if any). Throws {@link RunFailedError} on an `error`/`cancelled` outcome
471
- * unless `throwOnError` is set to `false`.
472
- */
473
- async wait(opts = {}) {
474
- let result;
475
- let streamError;
476
- for await (const ev of this.stream(opts)) {
477
- if (ev.type === "result") result = ev.result;
478
- else if (ev.type === "error") streamError = ev.error;
479
- }
480
- const run = await this.refresh();
481
- if (opts.throwOnError !== false) {
482
- if (TERMINAL.has(run.status)) {
483
- if (run.status !== "done") {
484
- throw new RunFailedError(run.id, run.status, run.error ?? streamError);
485
- }
486
- } else {
487
- throw new RunFailedError(
488
- run.id,
489
- run.status,
490
- streamError ?? "event stream ended before the run reached a terminal state"
491
- );
492
- }
493
- }
494
- return result !== void 0 ? { run, result } : { run };
495
- }
496
- };
497
-
498
- // src/workspace.ts
499
- var ProjectOperationsApi = class {
500
- constructor(mw) {
501
- this.mw = mw;
502
- }
503
- mw;
504
- async list(activeOnly = false) {
505
- const response = await this.mw.http.request("GET", "/workspace/operations", {
506
- query: { active: activeOnly }
507
- });
508
- return response.operations;
509
- }
510
- get(id) {
511
- return this.mw.http.request("GET", `/workspace/operations/${encodeURIComponent(id)}`);
512
- }
513
- cancel(id) {
514
- return this.mw.http.request("POST", `/workspace/operations/${encodeURIComponent(id)}/cancel`);
515
- }
516
- retry(id, auth) {
517
- return this.mw.http.request("POST", `/workspace/operations/${encodeURIComponent(id)}/retry`, { body: { auth } });
518
- }
519
- /** The first event is the current snapshot, then live changes. Reconnecting never replays old
520
- * progress. Breaking the loop/aborting detaches the observer; cancel(id) explicitly stops work.
521
- */
522
- async *watch(id, opts = {}) {
523
- const controller = new AbortController();
524
- const abort = () => controller.abort();
525
- opts.signal?.addEventListener("abort", abort, { once: true });
526
- if (opts.signal?.aborted) controller.abort();
527
- try {
528
- const response = await this.mw.http.open("GET", `/workspace/operations/${encodeURIComponent(id)}/stream`, {
529
- signal: controller.signal
530
- });
531
- let sequence = -1;
532
- for await (const operation of readSSE(response.body, controller.signal)) {
533
- if (operation.sequence > sequence) {
534
- sequence = operation.sequence;
535
- yield operation;
536
- }
537
- }
538
- } finally {
539
- controller.abort();
540
- opts.signal?.removeEventListener("abort", abort);
541
- }
542
- }
543
- };
544
- var WorkspaceCollection = class {
545
- constructor(mw, kind) {
546
- this.mw = mw;
547
- this.kind = kind;
548
- }
549
- mw;
550
- kind;
551
- /** Create with a stable client-generated ID. For updates, supply the record's last revision. */
552
- put(id, record, expectedRevision) {
553
- return this.mw.http.request("PUT", `/workspace/${this.kind}/${encodeURIComponent(id)}`, {
554
- body: { record, expectedRevision }
555
- });
556
- }
557
- /** Remove membership and dependent chat links. Files and native transcripts are retained.
558
- * Use deleteChat() for an explicit transcript purge. Running chats reject removal with 409.
559
- */
560
- delete(id, revision) {
561
- return this.mw.http.request("DELETE", `/workspace/${this.kind}/${encodeURIComponent(id)}`, {
562
- query: { revision }
563
- });
564
- }
565
- };
566
- var WorkspaceApi = class {
567
- constructor(mw) {
568
- this.mw = mw;
569
- this.git = new GitAccessApi(mw);
570
- this.operations = new ProjectOperationsApi(mw);
571
- this.agents = new WorkspaceCollection(mw, "agents");
572
- this.projects = new WorkspaceCollection(mw, "projects");
573
- this.chats = new WorkspaceCollection(mw, "chats");
574
- }
575
- mw;
576
- git;
577
- operations;
578
- agents;
579
- projects;
580
- chats;
581
- snapshot() {
582
- return this.mw.http.request("GET", "/workspace");
583
- }
584
- /** Start an operation owned by the daemon. The same ID/payload returns the existing operation. */
585
- createProject(request) {
586
- return this.mw.http.request("POST", "/workspace/projects", { body: request });
587
- }
588
- /** Permanently remove the confirmed project's directory and membership.
589
- * projects.delete() retains files. Native harness transcripts are not purged.
590
- */
591
- removeProjectFiles(id, request) {
592
- return this.mw.http.request("POST", `/workspace/projects/${encodeURIComponent(id)}/remove`, { body: request });
593
- }
594
- /** Incremental reconciliation. Pass the previous identity to detect a replaced/restored workspace.
595
- * A 409 requires fetching snapshot() again; never apply a delta to a different registry.
596
- */
597
- changes(since, workspaceId) {
598
- return this.mw.http.request("GET", "/workspace/changes", { query: { since, workspaceId } });
599
- }
600
- /** Import legacy metadata before replacing a local cache. Safe to repeat after interruption. */
601
- import(records) {
602
- return this.mw.http.request("POST", "/workspace/import", { body: records });
603
- }
604
- };
605
- var GitAccessApi = class {
606
- constructor(mw) {
607
- this.mw = mw;
608
- }
609
- mw;
610
- /** Reads attribution independently of GitHub authentication. Requires gitIdentityVersion >= 1. */
611
- identity(context = {}) {
612
- return this.mw.http.request("GET", "/workspace/git/identity", { query: context });
613
- }
614
- /** Saves settings only; never stages or commits. all_workspaces installs this
615
- * workspace's copy of a client-managed default; the client handles fan-out. */
616
- setIdentity(update, context = {}) {
617
- return this.mw.http.request("PUT", "/workspace/git/identity", { query: context, body: update });
618
- }
619
- state() {
620
- return this.mw.http.request("GET", "/workspace/git");
621
- }
622
- setDefault(connection, auth) {
623
- return this.mw.http.request("PUT", "/workspace/git", { body: { connection, auth } });
624
- }
625
- forget(connectionId) {
626
- return this.mw.http.request("DELETE", `/workspace/git/connections/${encodeURIComponent(connectionId)}`);
627
- }
628
- project(projectId) {
629
- return this.mw.http.request("GET", `/workspace/projects/${encodeURIComponent(projectId)}/git`);
630
- }
631
- setProject(projectId, connection, expectedRevision, auth) {
632
- return this.mw.http.request("PUT", `/workspace/projects/${encodeURIComponent(projectId)}/git`, {
633
- body: { connection, expectedRevision, auth }
634
- });
635
- }
636
- /** Compatibility call. Prefer start() and operation()/watch() for reconnectable writes. */
637
- run(projectId, operation, auth) {
638
- return this.mw.http.request("POST", `/workspace/projects/${encodeURIComponent(projectId)}/git/${operation}`, { body: { auth } });
639
- }
640
- /** Requires health.gitOperationsVersion >= 1 (>= 2 for branches, >= 4 for restore_commit). Acceptance persists before Git runs;
641
- * disconnecting only detaches the client. The same ID/intent never runs twice.
642
- */
643
- start(projectId, request) {
644
- return this.mw.http.request("POST", `/workspace/projects/${encodeURIComponent(projectId)}/git/operations`, { body: request });
645
- }
646
- async operations(projectId, activeOnly = false) {
647
- const result = await this.mw.http.request(
648
- "GET",
649
- `/workspace/projects/${encodeURIComponent(projectId)}/git/operations`,
650
- { query: { active: activeOnly, actionsVersion: 4 } }
651
- );
652
- return result.operations;
653
- }
654
- operation(id) {
655
- return this.mw.http.request("GET", `/workspace/git/operations/${encodeURIComponent(id)}`);
656
- }
657
- cancel(id) {
658
- return this.mw.http.request("POST", `/workspace/git/operations/${encodeURIComponent(id)}/cancel`);
659
- }
660
- /** Current snapshot followed by state changes. Aborting observation never cancels the operation. */
661
- async *watch(id, opts = {}) {
662
- const controller = new AbortController();
663
- const abort = () => controller.abort();
664
- opts.signal?.addEventListener("abort", abort, { once: true });
665
- if (opts.signal?.aborted) controller.abort();
666
- try {
667
- const response = await this.mw.http.open("GET", `/workspace/git/operations/${encodeURIComponent(id)}/stream`, {
668
- signal: controller.signal
669
- });
670
- let sequence = -1;
671
- for await (const operation of readSSE(response.body, controller.signal)) {
672
- if (operation.sequence > sequence) {
673
- sequence = operation.sequence;
674
- yield operation;
675
- }
676
- }
677
- } finally {
678
- controller.abort();
679
- opts.signal?.removeEventListener("abort", abort);
680
- }
681
- }
682
- };
683
-
684
- // src/surfaces.ts
685
- var SurfacesApi = class {
686
- constructor(mw) {
687
- this.mw = mw;
688
- }
689
- mw;
690
- list() {
691
- return this.mw.http.request("GET", "/surfaces");
692
- }
693
- status(refresh = false) {
694
- return this.mw.http.request("GET", "/surfaces/desktop", { query: { refresh } });
695
- }
696
- bind(binding) {
697
- return this.mw.http.request("PUT", "/surfaces/desktop/binding", { body: binding });
698
- }
699
- open(request) {
700
- return this.mw.http.request("POST", "/surfaces/desktop/sessions", { body: request });
701
- }
702
- control(id, request) {
703
- return this.mw.http.request("POST", `/surfaces/desktop/sessions/${encodeURIComponent(id)}/control`, { body: request });
704
- }
705
- close(id) {
706
- return this.mw.http.request("DELETE", `/surfaces/desktop/sessions/${encodeURIComponent(id)}`);
707
- }
708
- capture(id) {
709
- return this.mw.http.request("POST", `/surfaces/desktop/sessions/${encodeURIComponent(id)}/captures`);
710
- }
711
- action(request) {
712
- return this.mw.http.request("POST", "/surfaces/desktop/actions", { body: request });
713
- }
714
- receipt(id) {
715
- return this.mw.http.request("GET", `/surfaces/desktop/actions/${encodeURIComponent(id)}`);
716
- }
717
- artifact(id) {
718
- return this.mw.http.request("GET", `/artifacts/${encodeURIComponent(id)}`);
719
- }
720
- /** Every connection starts with the current snapshot, then revisions. Never replays input. */
721
- async *watch(opts = {}) {
722
- const controller = new AbortController();
723
- const abort = () => controller.abort();
724
- opts.signal?.addEventListener("abort", abort, { once: true });
725
- if (opts.signal?.aborted) controller.abort();
726
- try {
727
- const response = await this.mw.http.open("GET", "/surfaces/desktop/events", { signal: controller.signal });
728
- let instance;
729
- let revision = -1;
730
- for await (const snapshot of readSSE(response.body, controller.signal)) {
731
- if (instance !== snapshot.instanceId || snapshot.revision > revision) {
732
- instance = snapshot.instanceId;
733
- revision = snapshot.revision;
734
- yield snapshot;
735
- }
736
- }
737
- } finally {
738
- controller.abort();
739
- opts.signal?.removeEventListener("abort", abort);
740
- }
741
- }
742
- };
743
-
744
- // src/service.ts
745
- var ServiceApi = class {
746
- constructor(client) {
747
- this.client = client;
748
- }
749
- client;
750
- updateStatus() {
751
- return this.client.http.request("GET", "/service/update");
752
- }
753
- /** Download first. Acquire immediately before replacement; busy work returns 409.
754
- * Admission stays closed until release, process exit, or the one-minute expiry. */
755
- acquireUpdate() {
756
- return this.client.http.request("POST", "/service/update");
757
- }
758
- releaseUpdate(id) {
759
- return this.client.http.request("DELETE", `/service/update/${encodeURIComponent(id)}`);
760
- }
761
- };
762
-
763
- // src/execution.ts
764
- var ExecutionApi = class {
765
- constructor(client) {
766
- this.client = client;
767
- this.terminals = new TerminalsApi(client);
768
- }
769
- client;
770
- terminals;
771
- host() {
772
- return this.client.http.request("GET", "/workspace/host");
773
- }
774
- resources() {
775
- return this.client.http.request("GET", "/workspace/resources");
776
- }
777
- files(path4 = "", query) {
778
- return this.client.http.request("GET", "/workspace/files", { query: { path: path4, query } });
779
- }
780
- read(path4) {
781
- return this.client.http.request("GET", "/workspace/file", { query: { path: path4 } });
782
- }
783
- write(path4, content) {
784
- return this.client.http.request("PUT", "/workspace/file", { body: { path: path4, content } });
785
- }
786
- remove(path4) {
787
- return this.client.http.request("DELETE", "/workspace/file", { query: { path: path4 } });
788
- }
789
- exec(command, signal) {
790
- return this.client.http.request("POST", "/workspace/exec", { body: command, signal });
791
- }
792
- /** Output data is base64, preserving partial UTF-8 chunks. Cancelling stops this command. */
793
- async *stream(command, signal) {
794
- const response = await this.client.http.open("POST", "/workspace/exec/stream", { body: command, signal });
795
- yield* readSSE(response.body, signal);
796
- }
797
- };
798
- var TerminalsApi = class {
799
- constructor(client) {
800
- this.client = client;
801
- }
802
- client;
803
- list(directory) {
804
- return this.client.http.request("GET", "/workspace/terminals", { query: { directory } });
805
- }
806
- open(request) {
807
- return this.client.http.request("POST", "/workspace/terminals", { body: request });
808
- }
809
- get(id) {
810
- return this.client.http.request("GET", this.path(id));
811
- }
812
- close(id) {
813
- return this.client.http.request("DELETE", this.path(id));
814
- }
815
- input(id, input) {
816
- return this.client.http.request("POST", `${this.path(id)}/input`, { body: input });
817
- }
818
- resize(id, columns, rows) {
819
- return this.client.http.request("PUT", `${this.path(id)}/size`, { body: { columns, rows } });
820
- }
821
- /** Detaching only closes the subscription. Resume from the last sequence; reset replaces the screen. */
822
- async *events(id, after = 0, signal) {
823
- const response = await this.client.http.open("GET", `${this.path(id)}/events`, { query: { after }, signal });
824
- yield* readSSE(response.body, signal);
825
- }
826
- path(id) {
827
- return `/workspace/terminals/${encodeURIComponent(id)}`;
828
- }
829
- };
830
-
831
- // src/daemon-binary.ts
832
- function supported(platform, arch) {
833
- if (!["darwin", "linux", "win32"].includes(platform) || !["x64", "arm64"].includes(arch)) {
834
- throw new MindwireError(`mindwire: no daemon release for ${platform}-${arch}`);
835
- }
836
- }
837
- function assetName(version, platform, arch) {
838
- const releasePlatform = platform === "win32" ? "windows" : platform;
839
- const releaseArch = arch === "x64" ? "amd64" : arch;
840
- return `mindwired-v${version}-${releasePlatform}-${releaseArch}${platform === "win32" ? ".exe" : ""}`;
841
- }
842
- function checksumFor(text, asset) {
843
- const line = text.split("\n").find((l) => l.trim().endsWith(` ${asset}`) || l.trim().endsWith(` ${asset}`));
844
- const hash = line?.trim().split(/\s+/)[0];
845
- return hash && /^[a-f0-9]{64}$/i.test(hash) ? hash.toLowerCase() : void 0;
846
- }
847
- async function ensureDaemonBinary(opts = {}) {
848
- const proc = globalThis;
849
- const platform = opts.platform ?? proc.process?.platform;
850
- const arch = opts.arch ?? proc.process?.arch;
851
- supported(platform ?? "unknown", arch ?? "unknown");
852
- const daemonPlatform = platform;
853
- const daemonArch = arch;
854
- const version = opts.version ?? SDK_VERSION;
855
- if (!/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/.test(version)) {
856
- throw new MindwireError(`mindwire: cannot download daemon for non-release SDK version ${version}`);
857
- }
858
- const path4 = await import('path');
859
- const { binaryCacheDirectory: binaryCacheDirectory2, cachedExecutable: cachedExecutable2, cacheExecutable: cacheExecutable2 } = await import('./binary-cache-CG52TXCZ.js');
860
- const dir = path4.join(opts.cacheDir ?? await binaryCacheDirectory2(daemonPlatform), version, `${daemonPlatform}-${daemonArch}`);
861
- const asset = assetName(version, daemonPlatform, daemonArch);
862
- const bin = path4.join(dir, asset);
863
- const cached = await cachedExecutable2(bin);
864
- if (cached) return cached;
865
- const base = (opts.releaseBaseUrl ?? proc.process?.env?.MINDWIRE_RELEASE_BASE_URL ?? "https://github.com/oblien/mindwire/releases/download").replace(/\/$/, "");
866
- const release = `${base}/v${version}`;
867
- const request = opts.fetch ?? globalThis.fetch;
868
- if (!request) throw new MindwireError("mindwire: fetch is unavailable; set daemonBin or MINDWIRE_DAEMON");
869
- const [checksums, binary] = await Promise.all([request(`${release}/checksums.txt`), request(`${release}/${asset}`)]);
870
- if (!checksums.ok || !binary.ok) {
871
- if (!opts.releaseBaseUrl && !proc.process?.env?.MINDWIRE_RELEASE_BASE_URL) {
872
- const latest = await request("https://api.github.com/repos/oblien/mindwire/releases/latest");
873
- if (latest.ok) {
874
- const tag = (await latest.json()).tag_name;
875
- const fallback = typeof tag === "string" ? tag.replace(/^v/, "") : "";
876
- if (/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/.test(fallback) && fallback !== version) {
877
- return ensureDaemonBinary({ ...opts, version: fallback });
878
- }
879
- }
880
- }
881
- throw new MindwireError(`mindwire: failed to download daemon v${version} for ${daemonPlatform}-${daemonArch}`);
882
- }
883
- const expected = checksumFor(await checksums.text(), asset);
884
- if (!expected) throw new MindwireError(`mindwire: release v${version} has no checksum for ${asset}`);
885
- const bytes = new Uint8Array(await binary.arrayBuffer());
886
- return cacheExecutable2(bin, bytes, expected);
887
- }
888
-
889
- // src/embedded.ts
890
- var shared = /* @__PURE__ */ new Map();
891
- function configKey(opts) {
892
- return JSON.stringify([opts.cwd ?? "", opts.statePath ?? "", opts.bin ?? ""]);
893
- }
894
- function startEmbedded(opts = {}) {
895
- const key = configKey(opts);
896
- let daemon = shared.get(key);
897
- if (!daemon) {
898
- daemon = spawnDaemon(opts).catch((err) => {
899
- shared.delete(key);
900
- throw err;
901
- });
902
- shared.set(key, daemon);
903
- }
904
- return daemon;
905
- }
906
- function serverRuntime() {
907
- return globalThis;
908
- }
909
- function isServerRuntime() {
910
- const g = serverRuntime();
911
- return typeof g.process !== "undefined" && !!g.process.versions?.node || typeof g.Bun !== "undefined" || typeof g.Deno !== "undefined";
912
- }
913
- async function spawnDaemon(opts) {
914
- if (!isServerRuntime()) {
915
- throw new MindwireError(
916
- "MindWire embedded mode needs a server runtime (Node/Bun/Deno). In the browser or an edge runtime, pass { baseUrl } to connect to a running daemon."
917
- );
918
- }
919
- const { spawn: spawn3 } = await import('child_process');
920
- const net = await import('net');
921
- const proc = serverRuntime().process;
922
- const { randomBytes: randomBytes2 } = await import('crypto');
923
- const resolved = await resolveBinary(opts.bin);
924
- const bin = resolved.bin;
925
- const port = await freePort(net);
926
- const baseUrl = `http://127.0.0.1:${port}`;
927
- const token = randomBytes2(32).toString("hex");
928
- const child = spawn3(bin, [], {
929
- env: {
930
- ...proc.env,
931
- ADDR: `127.0.0.1:${port}`,
932
- AGENT_CWD: opts.cwd ?? proc.cwd(),
933
- STATE_PATH: opts.statePath ?? ".mindwire-state.json",
934
- DAEMON_TOKEN: token
935
- },
936
- stdio: "ignore"
937
- });
938
- let spawnError = null;
939
- child.on("error", (e) => {
940
- spawnError = e;
941
- });
942
- const stop = () => {
943
- try {
944
- child.kill();
945
- } catch {
946
- }
947
- };
948
- proc.once?.("exit", stop);
949
- proc.once?.("SIGINT", () => {
950
- stop();
951
- proc.exit?.(130);
952
- });
953
- await waitHealthy(baseUrl, token, () => spawnError, resolved);
954
- return { baseUrl, token, stop };
955
- }
956
- async function resolveBinary(explicit) {
957
- const fs2 = await import('fs');
958
- const path4 = await import('path');
959
- const proc = serverRuntime().process;
960
- const ext = proc.platform === "win32" ? ".exe" : "";
961
- const candidates = [];
962
- if (explicit) candidates.push(explicit);
963
- if (proc.env?.MINDWIRE_DAEMON) candidates.push(proc.env.MINDWIRE_DAEMON);
964
- try {
965
- const url = await import('url');
966
- const here = path4.dirname(url.fileURLToPath(import.meta.url));
967
- const named = `mindwired-${proc.platform}-${proc.arch}${ext}`;
968
- candidates.push(path4.join(here, "bin", named), path4.join(here, "..", "bin", named));
969
- } catch {
970
- }
971
- for (const c of candidates) {
972
- try {
973
- if (fs2.existsSync(c)) return { bin: c, found: true, checked: candidates };
974
- } catch {
975
- }
976
- }
977
- const downloaded = await ensureDaemonBinary({ platform: proc.platform, arch: proc.arch });
978
- return { bin: downloaded, found: true, checked: [...candidates, downloaded] };
979
- }
980
- function daemonStartError(resolved, cause) {
981
- if (!resolved.found) {
982
- return [
983
- "Couldn't start the mindwire daemon: the `mindwired` binary was not found.",
984
- `Looked in: ${resolved.checked.join(", ")}.`,
985
- "Fixes:",
986
- " \u2022 check GitHub Release access so the SDK can download its matching daemon binary; or",
987
- " \u2022 set MINDWIRE_DAEMON to a `mindwired` binary; or",
988
- " \u2022 pass { baseUrl } to connect to a daemon you run yourself.",
989
- " \u2022 Working inside the mindwire monorepo? Run `go build -o /tmp/mindwired ./daemon/cmd/daemon` and set MINDWIRE_DAEMON.",
990
- `(underlying error: ${cause.message})`
991
- ].join("\n");
992
- }
993
- return `Couldn't start the mindwire daemon (${resolved.bin}): ${cause.message}. Set MINDWIRE_DAEMON to the binary path, or pass { baseUrl } to use a running daemon.`;
994
- }
995
- function freePort(net) {
996
- return new Promise((resolve3, reject) => {
997
- const srv = net.createServer();
998
- srv.on("error", reject);
999
- srv.listen(0, "127.0.0.1", () => {
1000
- const addr = srv.address();
1001
- const port = addr && typeof addr === "object" ? addr.port : 0;
1002
- srv.close(() => resolve3(port));
1003
- });
1004
- });
1005
- }
1006
- async function waitHealthy(baseUrl, token, getError, resolved, timeoutMs = 15e3) {
1007
- const deadline = Date.now() + timeoutMs;
1008
- while (Date.now() < deadline) {
1009
- const err = getError();
1010
- if (err) {
1011
- throw new MindwireError(daemonStartError(resolved, err), { cause: err });
1012
- }
1013
- try {
1014
- const res = await fetch(`${baseUrl}/healthz`, { headers: { Authorization: `Bearer ${token}` } });
1015
- if (res.ok) return;
1016
- } catch {
1017
- }
1018
- await new Promise((r) => setTimeout(r, 150));
1019
- }
1020
- throw new MindwireError("mindwire embedded daemon did not become healthy in time.");
1021
- }
1022
-
1023
- // src/target/index.ts
1024
- function emit(spec, e) {
1025
- if (!spec.onLog) return;
1026
- try {
1027
- spec.onLog(e);
1028
- } catch {
1029
- }
1030
- }
1031
- function local(opts = {}) {
1032
- return {
1033
- name: "local",
1034
- async connect(spec) {
1035
- const d = await startEmbedded(opts);
1036
- emit(spec, { target: "local", phase: "ready", message: `embedded daemon on ${d.baseUrl}` });
1037
- return {
1038
- id: d.baseUrl,
1039
- baseUrl: d.baseUrl,
1040
- ...d.token !== void 0 ? { token: d.token } : {},
1041
- // No-op: the embedded daemon is shared (keyed memoization) and reaped on process exit.
1042
- stop: async () => {
1043
- }
1044
- };
1045
- }
1046
- };
1047
- }
1048
- function remote(baseUrl, opts = {}) {
1049
- return {
1050
- name: "remote",
1051
- async connect(spec) {
1052
- emit(spec, { target: "remote", phase: "skip", message: `using remote daemon at ${baseUrl}` });
1053
- return {
1054
- id: baseUrl,
1055
- baseUrl,
1056
- ...opts.token !== void 0 ? { token: opts.token } : {},
1057
- ...opts.headers !== void 0 ? { headers: opts.headers } : {},
1058
- ...opts.fetch !== void 0 ? { fetch: opts.fetch } : {},
1059
- stop: async () => {
1060
- }
1061
- };
1062
- }
1063
- };
1064
- }
1065
-
1066
- // src/client.ts
1067
- var handleByTransport = /* @__PURE__ */ new WeakMap();
1068
- var Mindwire = class _Mindwire {
1069
- /** Workspace registry: saved agent profiles, projects and chat relationships. */
1070
- workspace;
1071
- surfaces;
1072
- service;
1073
- execution;
1074
- computer;
1075
- http;
1076
- /** The default agent type applied to agent-scoped calls, if set. */
1077
- defaultAgent;
1078
- /** Step-flow auth, scoped to this client's default agent (override per call). */
1079
- auth;
1080
- /** Persistent memory files + saved prompt templates, scoped to this client's default agent. */
1081
- prompts;
1082
- /** Persistent MCP-server config (the config an agent loads every run), scoped to this client's default agent. */
1083
- mcp;
1084
- /** Custom LLM-provider registration (opencode/Codex native config), scoped to this client's default agent. */
1085
- providers;
1086
- /** Daemon-driven notification channels + routing rules (webhook/slack/discord/telegram; per-agent/session/global). */
1087
- notify;
1088
- constructor(opts = {}) {
1089
- const target = opts.target ?? local();
1090
- const spec = { agent: opts.agent, onLog: opts.logger };
1091
- this.http = new Http({
1092
- fetch: opts.fetch,
1093
- headers: opts.headers,
1094
- timeoutMs: opts.requestTimeoutMs,
1095
- resolveBase: () => {
1096
- const h = target.connect(spec);
1097
- handleByTransport.set(this.http, h);
1098
- return h.then((x) => ({
1099
- baseUrl: x.baseUrl,
1100
- token: x.token,
1101
- getToken: x.getToken,
1102
- headers: x.headers,
1103
- fetch: x.fetch
1104
- }));
1105
- }
1106
- });
1107
- this.defaultAgent = opts.agent;
1108
- this.workspace = new WorkspaceApi(this);
1109
- this.surfaces = new SurfacesApi(this);
1110
- this.service = new ServiceApi(this);
1111
- this.execution = new ExecutionApi(this);
1112
- this.computer = new ComputerApi(this);
1113
- this.auth = new AuthApi(this);
1114
- this.prompts = new PromptsApi(this);
1115
- this.mcp = new McpApi(this);
1116
- this.providers = new ProvidersApi(this);
1117
- this.notify = new NotifyApi(this);
1118
- }
1119
- /**
1120
- * Provision the destination and await readiness **now**, rather than lazily on the first request —
1121
- * useful to front-load a fresh SSH/Docker/Oblien box (and to stream its {@link EnsureEvent}s to the
1122
- * `logger`) before the first `turn()`. Idempotent and memoized: repeated `ensure()` calls, and the
1123
- * first real request, all await the same provisioning, so the target connects exactly once.
1124
- */
1125
- async ensure() {
1126
- await this.http.ready();
1127
- }
1128
- /** Return a new client bound to a different default agent (shares the same transport config). */
1129
- withAgent(agent) {
1130
- const clone = Object.create(_Mindwire.prototype);
1131
- clone.http = this.http;
1132
- clone.defaultAgent = agent;
1133
- clone.workspace = new WorkspaceApi(clone);
1134
- clone.surfaces = new SurfacesApi(clone);
1135
- clone.service = new ServiceApi(clone);
1136
- clone.execution = new ExecutionApi(clone);
1137
- clone.computer = new ComputerApi(clone);
1138
- clone.auth = new AuthApi(clone);
1139
- clone.prompts = new PromptsApi(clone);
1140
- clone.mcp = new McpApi(clone);
1141
- clone.providers = new ProvidersApi(clone);
1142
- clone.notify = new NotifyApi(clone);
1143
- return clone;
1144
- }
1145
- /** The `?agent=` value to send for a scoped call: explicit override → client default → none. */
1146
- agentParam(scoped) {
1147
- const a = scoped?.agent ?? this.defaultAgent;
1148
- return a ? { agent: a } : void 0;
1149
- }
1150
- /**
1151
- * Release target-owned resources by calling the {@link TargetHandle}'s `stop()` — once, even across
1152
- * {@link withAgent} clones that share the transport. For an `ssh`/`docker`/`oblien` target this reaps
1153
- * the box (tear down the tunnel, stop or delete the container/workspace, per `stopOnExit`); a no-op
1154
- * for `local`/`remote` (embedded self-cleans on process exit; remote is not ours to stop). Safe to
1155
- * call before the target has connected (nothing to reap yet).
1156
- */
1157
- async close() {
1158
- const handle = handleByTransport.get(this.http);
1159
- if (!handle) return;
1160
- handleByTransport.delete(this.http);
1161
- const h = await handle.catch(() => null);
1162
- if (h) await h.stop();
1163
- }
1164
- // ---- health & catalog ----------------------------------------------------
1165
- /** `GET /healthz` — authenticated liveness check. Resolves with the daemon's health payload when it is up. */
1166
- health() {
1167
- return this.http.request("GET", "/healthz");
1168
- }
1169
- /**
1170
- * `GET /stats` — the daemon **process's** resource snapshot (heap in use, memory reserved from the
1171
- * OS, goroutines, GC cycles, cores, platform, uptime). Cheap enough to call on demand — the daemon
1172
- * reads its own Go runtime, not the machine — so a UI can fetch it when a user opens a daemon's page
1173
- * without any background polling. See {@link Stats} for what each field means and doesn't.
1174
- */
1175
- stats() {
1176
- return this.http.request("GET", "/stats");
1177
- }
1178
- /** `GET /catalog` — every agent this daemon binary supports. */
1179
- catalog() {
1180
- return this.http.request("GET", "/catalog");
1181
- }
1182
- /** `GET /agent` — capabilities + settings schema + auth methods/status for the selected agent. */
1183
- agent(scoped) {
1184
- return this.http.request("GET", "/agent", { query: this.agentParam(scoped) });
1185
- }
1186
- /** `GET /agent/software` — installed version, compatibility and approved update target. */
1187
- software(opts = {}) {
1188
- return this.http.request("GET", "/agent/software", {
1189
- query: { ...this.agentParam(opts), ...opts.refresh ? { refresh: "true" } : {} }
1190
- });
1191
- }
1192
- /**
1193
- * `GET /models` — the models the selected agent can run for the configured account. An empty array
1194
- * is valid (no credentials yet / offline). Throws a 400 {@link ApiError} for an agent whose model is
1195
- * free text (check `capabilities.models` first).
1196
- */
1197
- models(scoped) {
1198
- return this.http.request("GET", "/models", { query: this.agentParam(scoped) });
1199
- }
1200
- /** `GET /doctor` — daemon-level health plus the selected agent's own checks. */
1201
- doctor(scoped) {
1202
- return this.http.request("GET", "/doctor", { query: this.agentParam(scoped) });
1203
- }
1204
- // ---- toolchain setup -----------------------------------------------------
1205
- /** `POST /setup` — start the agent's install toolchain (background; poll {@link setupStatus}). */
1206
- setup(scoped) {
1207
- return this.http.request("POST", "/setup", { query: this.agentParam(scoped) });
1208
- }
1209
- /** `POST /update` — install the catalog's newest tested version compatible with this daemon. */
1210
- update(scoped) {
1211
- return this.http.request("POST", "/update", { query: this.agentParam(scoped) });
1212
- }
1213
- /** `GET /setup` — current toolchain install progress. */
1214
- setupStatus(scoped) {
1215
- return this.http.request("GET", "/setup", { query: this.agentParam(scoped) });
1216
- }
1217
- // ---- config --------------------------------------------------------------
1218
- /** `GET /config` — the declared, non-secret settings for the agent. */
1219
- getConfig(scoped) {
1220
- return this.http.request("GET", "/config", {
1221
- query: this.agentParam(scoped)
1222
- });
1223
- }
1224
- /** `PUT /config` — merge recognized (non-secret) setting keys. Unknown keys are ignored server-side. */
1225
- async setConfig(values, scoped) {
1226
- await this.http.request("PUT", "/config", {
1227
- query: this.agentParam(scoped),
1228
- body: values
1229
- });
1230
- }
1231
- // ---- chats & history -----------------------------------------------------
1232
- /** `GET /chats` — recorded chats (newest first), shared across agents. */
1233
- chats() {
1234
- return this.http.request("GET", "/chats");
1235
- }
1236
- /**
1237
- * `PUT /chats/{id}` — rename a chat. The user title wins over the agent's native auto-title in
1238
- * every listing; an empty title clears the rename (reverting to the native/derived title).
1239
- * Returns the updated summary.
1240
- */
1241
- renameChat(chatId, title) {
1242
- return this.http.request("PUT", `/chats/${encodeURIComponent(chatId)}`, {
1243
- body: { title }
1244
- });
1245
- }
1246
- /**
1247
- * `DELETE /chats/{id}` — a true, irreversible delete: purges ALL of the chat's mindwire
1248
- * bookkeeping and, for every session the chat mapped to, removes that agent's native transcript
1249
- * (the source of truth). Rejects with a 409 error if a turn is live. Native deletion is
1250
- * best-effort per agent; the result reports what was purged vs. failed.
1251
- */
1252
- deleteChat(chatId) {
1253
- return this.http.request("DELETE", `/chats/${encodeURIComponent(chatId)}`);
1254
- }
1255
- /**
1256
- * `POST /chats/{id}/fork` — clone a chat into a new id (generated when `newChatId` is omitted).
1257
- * The fork shares the source's native session until its first turn, which branches it (natively
1258
- * on Claude via `--fork-session`; a fresh session on agents without native fork). Rejects with a
1259
- * 409 if the source has a live turn, 404 if the source is unknown, 400 if the target id is in
1260
- * use. Returns the new chat's summary.
1261
- */
1262
- forkChat(chatId, opts = {}) {
1263
- return this.http.request("POST", `/chats/${encodeURIComponent(chatId)}/fork`, {
1264
- body: opts.newChatId ? { newChatId: opts.newChatId } : {}
1265
- });
1266
- }
1267
- /**
1268
- * `GET /chats/{id}/messages` — a chat's transcript (native when the agent supports it, else
1269
- * the recorded fallback). `limit` caps to the newest N; `before` pages older history.
1270
- */
1271
- messages(chatId, opts = {}) {
1272
- const query = { ...this.agentParam(opts) };
1273
- if (opts.limit !== void 0) query["limit"] = opts.limit;
1274
- if (opts.before !== void 0) query["before"] = opts.before;
1275
- return this.http.request("GET", `/chats/${encodeURIComponent(chatId)}/messages`, {
1276
- query
1277
- });
1278
- }
1279
- /** `GET /chats/{id}/run` — the latest run for a chat (reattach anchor), or `null` if none yet. */
1280
- async latestRun(chatId) {
1281
- const data = await this.http.request(
1282
- "GET",
1283
- `/chats/${encodeURIComponent(chatId)}/run`
1284
- );
1285
- return data ? new Run(this.http, data) : null;
1286
- }
1287
- // ---- turns & runs --------------------------------------------------------
1288
- /**
1289
- * `POST /turns` — start a turn. Returns a {@link Run} handle you can stream, cancel, or await.
1290
- * Rejects with an {@link ApiError} (409) if a turn is already running for the chat.
1291
- *
1292
- * `mode` defaults to `"turn"` — one agent turn that ends when the CLI settles. Pass
1293
- * `mode: "resolve"` (with optional {@link ResolveOptions} `resolve` caps) for a global-resolve run
1294
- * that auto-continues the agent's multi-step work until it's done; {@link Mindwire.resolve} is the
1295
- * clearer entry point for that. The returned {@link Run} is then the parent of the run tree.
1296
- */
1297
- async turn(input) {
1298
- const body = {
1299
- chatId: input.chatId,
1300
- message: input.message
16
+ var enterScreen = "\x1B[?1049h\x1B[?25l";
17
+ var leaveScreen = "\x1B[0m\x1B[?25h\x1B[?1049l";
18
+ var clearScreen = "\x1B[H\x1B[2J";
19
+ var scanInstruction = "Mindwire \u2192 Add computer \u2192 Scan QR code";
20
+ async function createPairingQR(invitation) {
21
+ const QR = await import('qrcode');
22
+ const data = computerPairingQRData(invitation);
23
+ const options = { errorCorrectionLevel: "L", margin: 4 };
24
+ const { modules } = QR.create(data, options);
25
+ const width = modules.size + options.margin * 2;
26
+ const black = (x, y) => x >= 0 && y >= 0 && x < modules.size && y < modules.size && !!modules.data[y * modules.size + x];
27
+ const lines = [];
28
+ for (let y = -options.margin; y < modules.size + options.margin; y += 2) {
29
+ let line = "";
30
+ for (let x = -options.margin; x < modules.size + options.margin; x++) {
31
+ const top = black(x, y), bottom = black(x, y + 1);
32
+ line += top ? bottom ? "\u2588" : "\u2580" : bottom ? "\u2584" : " ";
33
+ }
34
+ lines.push("\x1B[30;107m" + line + "\x1B[0m");
35
+ }
36
+ const svg = await QR.toString(data, { ...options, type: "svg" });
37
+ return { lines, columns: Math.max(width, scanInstruction.length) + 1, rows: lines.length + 5, svg };
38
+ }
39
+ function escapeHTML(value) {
40
+ return value.replace(/[&<>"']/g, (ch) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[ch]);
41
+ }
42
+ function browserPage(invitation, svg) {
43
+ return `<!doctype html>
44
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
45
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
46
+ <title>Connect your phone \xB7 Mindwire</title>
47
+ <style>
48
+ *{box-sizing:border-box}body{margin:0;background:#141416;color:#f5f5f7;font:16px/1.5 system-ui,sans-serif}
49
+ main{width:min(100% - 40px,520px);margin:32px auto;text-align:center}small{color:#a6a6af;letter-spacing:.16em}
50
+ h1{font-size:28px;letter-spacing:-.04em;line-height:1.2;margin:16px 0 6px}p{color:#b7b7bf;margin:8px 0;overflow-wrap:anywhere}
51
+ .code{width:min(100%,480px);margin:24px auto 16px;background:#fff;border-radius:16px;padding:8px}
52
+ svg{display:block;width:100%;height:auto}.steps{margin:20px 0}.steps p{color:#e1e1e6}
53
+ button{font:inherit;font-weight:600;color:#f5f5f7;border:1px solid #45454b;background:#252528;border-radius:10px;padding:10px 18px;cursor:pointer}
54
+ button:disabled{opacity:.45;cursor:default}button:focus-visible{outline:3px solid #a4b8ef;outline-offset:3px}
55
+ textarea{width:100%;margin-top:12px;padding:8px;color:inherit;background:#252528;border:1px solid #45454b;border-radius:8px}
56
+ [hidden]{display:none!important}#expiry{font-size:14px}footer{font-size:13px;color:#93939d;margin:18px 0}
57
+ </style></head><body><main>
58
+ <small>MINDWIRE</small><h1>Connect your phone</h1><p>${escapeHTML(invitation.name)}</p>
59
+ <div class="code" id="code" role="img" aria-label="Mindwire pairing QR code">${svg}</div>
60
+ <p id="expiry" data-expires="${escapeHTML(invitation.expiresAt)}" aria-live="off"></p>
61
+ <div class="steps"><p>In Mindwire, open <strong>Add computer</strong> and scan.</p>
62
+ <p>Then return to the terminal to approve your phone.</p></div>
63
+ <button type="button" id="copy">Copy pairing link</button>
64
+ <textarea id="link" readonly hidden aria-label="Pairing link">${escapeHTML(computerPairingURI(invitation))}</textarea>
65
+ <footer>You can close this page after pairing.</footer>
66
+ </main><script>
67
+ const expiry=document.getElementById('expiry'),code=document.getElementById('code'),copy=document.getElementById('copy'),link=document.getElementById('link');
68
+ function tick(){const seconds=Math.max(0,Math.ceil((Date.parse(expiry.dataset.expires)-Date.now())/1000));
69
+ if(!seconds){code.remove();copy.disabled=true;link.value='';link.hidden=true;expiry.textContent='Code expired. Run mindwire connect for a new code.';return;}
70
+ expiry.textContent='Expires in '+Math.floor(seconds/60)+':'+String(seconds%60).padStart(2,'0');}
71
+ tick();setInterval(tick,1000);
72
+ copy.addEventListener('click',async()=>{try{await navigator.clipboard.writeText(link.value);copy.textContent='Copied';}
73
+ catch{link.hidden=false;link.focus();link.select();copy.textContent='Select and copy the link';}});
74
+ </script></body></html>`;
75
+ }
76
+ async function openBrowser(url) {
77
+ const [command, args] = process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["rundll32.exe", ["url.dll,FileProtocolHandler", url]] : ["xdg-open", [url]];
78
+ return new Promise((resolve2) => {
79
+ const child = spawn(command, args, { stdio: "ignore", windowsHide: true });
80
+ const finish = (ok) => {
81
+ clearTimeout(timer);
82
+ resolve2(ok);
1301
83
  };
1302
- if (input.cwd !== void 0) body.cwd = input.cwd;
1303
- if (input.options !== void 0) body.options = input.options;
1304
- if (input.mode !== void 0) body.mode = input.mode;
1305
- if (input.resolve !== void 0) body.resolve = input.resolve;
1306
- if (input.gitAuth !== void 0) body.gitAuth = input.gitAuth;
1307
- const data = await this.http.request("POST", "/turns", {
1308
- query: this.agentParam(input),
1309
- body
1310
- });
1311
- return new Run(this.http, data);
1312
- }
1313
- /**
1314
- * `POST /turns {mode:"resolve"}` — start a **global-resolve** run: instead of returning after one
1315
- * turn, the daemon holds the task open and auto-continues the agent (resuming on continuable stops
1316
- * and probing for completion) until the work is globally resolved, then aggregates one final result.
1317
- *
1318
- * The returned {@link Run} is the **parent** of a run tree: each auto-continued iteration is a child
1319
- * turn ({@link Run.children}) whose events stream onto the parent's topic, delimited by `continuation`
1320
- * boundary events. `run.wait()` resolves once with the aggregated result; `run.stopReason` /
1321
- * `run.iterations` report how the loop ended. Resolve turns run unattended (no mid-turn approvals)
1322
- * and are bounded by {@link ResolveOptions} caps — see the resolve guide. Rejects with an
1323
- * {@link ApiError} (409) if a turn is already running for the chat.
1324
- */
1325
- async resolve(input) {
1326
- return this.turn({ ...input, mode: "resolve" });
1327
- }
1328
- /** `GET /runs/{id}` — fetch an existing run as a {@link Run} handle. */
1329
- async run(id) {
1330
- const data = await this.http.request("GET", `/runs/${encodeURIComponent(id)}`);
1331
- return new Run(this.http, data);
1332
- }
1333
- /**
1334
- * `POST /chats/{id}/compact` — run an on-demand conversation compaction as a first-class {@link Run}
1335
- * you can stream or await. The agent folds prior context into a summary it carries forward, emitting
1336
- * a `compaction` event on the stream and recording the boundary in history exactly like an
1337
- * auto-compaction. Optional `instructions` focus the continuation summary (Claude's
1338
- * `/compact <instructions>`; agents that don't honor focus still compact). Rejects with an
1339
- * {@link ApiError}: 400 if the agent doesn't support compaction (`capabilities.compactNow`) or the
1340
- * chat has no conversation yet, 409 if a turn is already running for the chat.
1341
- */
1342
- async compact(chatId, opts = {}) {
1343
- const data = await this.http.request(
1344
- "POST",
1345
- `/chats/${encodeURIComponent(chatId)}/compact`,
1346
- {
1347
- query: this.agentParam(opts),
1348
- body: opts.instructions ? { instructions: opts.instructions } : {}
1349
- }
1350
- );
1351
- return new Run(this.http, data);
1352
- }
1353
- // ---- notifications -------------------------------------------------------
1354
- /** `GET /notify/config` — whether a notification channel is wired (token never returned). */
1355
- getNotifyConfig() {
1356
- return this.http.request("GET", "/notify/config");
1357
- }
1358
- /** `PUT /notify/config` — store the provisioned notification channel (daemon-wide). */
1359
- async setNotifyConfig(input) {
1360
- await this.http.request("PUT", "/notify/config", { body: input });
1361
- }
1362
- /** `GET /notify/stream` — SSE feed of the daemon's notifications (replay, then live). */
1363
- async *notifications(opts = {}) {
1364
- const res = await this.http.open("GET", "/notify/stream", {
1365
- ...opts.signal ? { signal: opts.signal } : {}
1366
- });
1367
- yield* readSSE(res.body, opts.signal);
1368
- }
1369
- // ---- live resources ------------------------------------------------------
1370
- /**
1371
- * `GET /processes/stream` — SSE feed of live per-turn CPU/memory, one {@link ProcessFrame} per tick.
1372
- * Sampling is **on demand**: the daemon starts measuring only while a client is connected and stops
1373
- * the instant the last one disconnects, so aborting the `signal` (or ending the loop) tells the
1374
- * daemon to stop — no background work, no leak. Pass `agent` to filter each frame's samples to one
1375
- * agent type. Frames are live snapshots (no replay); an empty `samples` array is a valid keep-alive.
1376
- */
1377
- async *processes(opts = {}) {
1378
- const path4 = "/processes/stream" + (opts.agent ? `?agent=${encodeURIComponent(opts.agent)}` : "");
1379
- const res = await this.http.open("GET", path4, {
1380
- ...opts.signal ? { signal: opts.signal } : {}
1381
- });
1382
- yield* readSSE(res.body, opts.signal);
1383
- }
1384
- };
1385
- var AuthApi = class {
1386
- constructor(mw) {
1387
- this.mw = mw;
1388
- }
1389
- mw;
1390
- /** `GET /auth/methods` — the options list to present. */
1391
- methods(scoped) {
1392
- return this.mw.http.request("GET", "/auth/methods", {
1393
- query: this.mw.agentParam(scoped)
1394
- });
1395
- }
1396
- /** `POST /auth/begin` — start one method (may return `{ url, code, fields, pending }`). */
1397
- begin(method, scoped) {
1398
- return this.mw.http.request("POST", "/auth/begin", {
1399
- query: this.mw.agentParam(scoped),
1400
- body: { method }
1401
- });
1402
- }
1403
- /** `POST /auth/step` — submit fields, or poll an interactive login to completion. */
1404
- step(input, scoped) {
1405
- return this.mw.http.request("POST", "/auth/step", {
1406
- query: this.mw.agentParam(scoped),
1407
- body: input
1408
- });
1409
- }
1410
- /** Read one interactive attempt, scoped by the returned AuthState.flowId. */
1411
- poll(flowId, scoped) {
1412
- return this.step({ _flowId: flowId }, scoped);
1413
- }
1414
- /** Cancel this native login without affecting a newer sign-in attempt. */
1415
- cancel(flowId, scoped) {
1416
- return this.step({ _flowId: flowId, _action: "cancel" }, scoped);
1417
- }
1418
- /** `GET /auth/status` — is the agent authenticated, and via which method. */
1419
- status(scoped) {
1420
- return this.mw.http.request("GET", "/auth/status", {
1421
- query: this.mw.agentParam(scoped)
1422
- });
1423
- }
1424
- /** `POST /auth/logout` — disconnect this harness once its running chats finish. */
1425
- logout(scoped) {
1426
- return this.mw.http.request("POST", "/auth/logout", {
1427
- query: this.mw.agentParam(scoped)
1428
- });
1429
- }
1430
- };
1431
- var PromptsApi = class {
1432
- constructor(mw) {
1433
- this.mw = mw;
1434
- }
1435
- mw;
1436
- query(scoped, extra) {
1437
- return { ...this.mw.agentParam(scoped), ...extra };
1438
- }
1439
- /**
1440
- * `GET /memory` — the agent's memory file at every supported scope (project + user for both
1441
- * Claude and Codex). Each entry carries the resolved `path` and `exists`; `content` is `""` for
1442
- * an absent file.
1443
- */
1444
- memory(opts = {}) {
1445
- return this.mw.http.request("GET", "/memory", {
1446
- query: this.query(opts, { dir: opts.dir })
1447
- });
1448
- }
1449
- /** `PUT /memory` — write the memory file at `scope`. Returns the resulting {@link MemoryDoc}. */
1450
- setMemory(input, opts = {}) {
1451
- return this.mw.http.request("PUT", "/memory", {
1452
- query: this.query(opts, { dir: opts.dir }),
1453
- body: input
1454
- });
1455
- }
1456
- /**
1457
- * `DELETE /memory` — remove the memory file at `scope` (defaults to `user`). Returns the resulting
1458
- * {@link MemoryDoc} (`exists: false` at the resolved path). Idempotent: deleting an absent file still
1459
- * succeeds.
1460
- */
1461
- deleteMemory(opts = {}) {
1462
- return this.mw.http.request("DELETE", "/memory", {
1463
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1464
- });
1465
- }
1466
- /**
1467
- * `GET /prompts` — saved prompt templates across every supported scope (Claude: project + user;
1468
- * Codex: user only). `content` is omitted here; fetch it with {@link get}. A missing project
1469
- * directory yields an empty list for that scope rather than an error.
1470
- */
1471
- list(opts = {}) {
1472
- return this.mw.http.request("GET", "/prompts", {
1473
- query: this.query(opts, { dir: opts.dir })
1474
- });
1475
- }
1476
- /** `GET /prompts/{name}` — one template's full body. Rejects with a 404 `ApiError` if absent. */
1477
- get(name, opts = {}) {
1478
- return this.mw.http.request("GET", `/prompts/${encodeURIComponent(name)}`, {
1479
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1480
- });
1481
- }
1482
- /** `PUT /prompts/{name}` — create or overwrite a template. Returns the resulting {@link PromptTemplate}. */
1483
- set(name, content, opts = {}) {
1484
- return this.mw.http.request("PUT", `/prompts/${encodeURIComponent(name)}`, {
1485
- query: this.query(opts, { scope: opts.scope, dir: opts.dir }),
1486
- body: { content }
1487
- });
1488
- }
1489
- /**
1490
- * `DELETE /prompts/{name}` — remove one template at `scope` (defaults to `user`). Idempotent:
1491
- * deleting an absent template still succeeds. A traversal name rejects with a 400 `ApiError`.
1492
- */
1493
- async delete(name, opts = {}) {
1494
- await this.mw.http.request("DELETE", `/prompts/${encodeURIComponent(name)}`, {
1495
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1496
- });
1497
- }
1498
- /**
1499
- * `GET /subagents` — persistent subagent definitions (Claude `.claude/agents/*.md`) across every
1500
- * supported scope. `content` is omitted here (fetch it with {@link subagent}); `meta` is the parsed
1501
- * frontmatter view. Rejects with a 400 `ApiError` on an agent without the subagent-definition module.
1502
- * Distinct from a turn's per-turn `subagents` passthrough — this is the on-disk definition store.
1503
- */
1504
- subagents(opts = {}) {
1505
- return this.mw.http.request("GET", "/subagents", {
1506
- query: this.query(opts, { dir: opts.dir })
1507
- });
1508
- }
1509
- /** `GET /subagents/{name}` — one definition's raw body + parsed meta. 404 `ApiError` if absent. */
1510
- subagent(name, opts = {}) {
1511
- return this.mw.http.request("GET", `/subagents/${encodeURIComponent(name)}`, {
1512
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1513
- });
1514
- }
1515
- /** `PUT /subagents/{name}` — create or overwrite a definition (raw content is canonical). Returns it. */
1516
- setSubagent(name, content, opts = {}) {
1517
- return this.mw.http.request("PUT", `/subagents/${encodeURIComponent(name)}`, {
1518
- query: this.query(opts, { scope: opts.scope, dir: opts.dir }),
1519
- body: { content }
1520
- });
1521
- }
1522
- /**
1523
- * `DELETE /subagents/{name}` — remove one definition at `scope` (defaults to `user`). Idempotent:
1524
- * deleting an absent definition still succeeds. A traversal name rejects with a 400 `ApiError`.
1525
- */
1526
- async deleteSubagent(name, opts = {}) {
1527
- await this.mw.http.request("DELETE", `/subagents/${encodeURIComponent(name)}`, {
1528
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1529
- });
1530
- }
1531
- };
1532
- var McpApi = class {
1533
- constructor(mw) {
1534
- this.mw = mw;
1535
- }
1536
- mw;
1537
- query(scoped, extra) {
1538
- return { ...this.mw.agentParam(scoped), ...extra };
1539
- }
1540
- /**
1541
- * `GET /mcp` — every persistent MCP server across the agent's supported scopes, keyed
1542
- * `scope → name → server` (Claude: project + user; Codex: user only). A missing config file yields
1543
- * an empty object for that scope rather than an error.
1544
- */
1545
- list(opts = {}) {
1546
- return this.mw.http.request("GET", "/mcp", {
1547
- query: this.query(opts, { dir: opts.dir })
1548
- });
1549
- }
1550
- /** `GET /mcp/{name}` — one server's definition. Rejects with a 404 `ApiError` if it isn't configured. */
1551
- get(name, opts = {}) {
1552
- return this.mw.http.request("GET", `/mcp/${encodeURIComponent(name)}`, {
1553
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1554
- });
1555
- }
1556
- /** `PUT /mcp/{name}` — create or overwrite one server. Returns the stored definition. */
1557
- set(name, server, opts = {}) {
1558
- return this.mw.http.request("PUT", `/mcp/${encodeURIComponent(name)}`, {
1559
- query: this.query(opts, { scope: opts.scope, dir: opts.dir }),
1560
- body: server
1561
- });
1562
- }
1563
- /** `DELETE /mcp/{name}` — remove one server. Idempotent: deleting an absent server still succeeds. */
1564
- async delete(name, opts = {}) {
1565
- await this.mw.http.request("DELETE", `/mcp/${encodeURIComponent(name)}`, {
1566
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1567
- });
1568
- }
1569
- };
1570
- var ProvidersApi = class {
1571
- constructor(mw) {
1572
- this.mw = mw;
1573
- }
1574
- mw;
1575
- query(scoped, extra) {
1576
- return { ...this.mw.agentParam(scoped), ...extra };
1577
- }
1578
- /**
1579
- * `GET /providers` — every registered custom provider across the agent's supported scopes, keyed
1580
- * `scope → id → provider` (opencode/Codex: user only). A missing config file yields an empty object for
1581
- * that scope rather than an error. `hasKey` reports whether a secret is stored; the key is never returned.
1582
- */
1583
- list(opts = {}) {
1584
- return this.mw.http.request("GET", "/providers", {
1585
- query: this.query(opts, { dir: opts.dir })
1586
- });
1587
- }
1588
- /** `GET /providers/{id}` — one provider's definition. Rejects with a 404 `ApiError` if it isn't configured. */
1589
- get(id, opts = {}) {
1590
- return this.mw.http.request("GET", `/providers/${encodeURIComponent(id)}`, {
1591
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1592
- });
1593
- }
1594
- /**
1595
- * `PUT /providers/{id}` — create or overwrite one provider, returning the stored definition (with
1596
- * `hasKey` and the stored `envVars`). Two write-only secret channels, both optional: `opts.apiKey` is a
1597
- * single key (custom endpoints, single-key catalog brands); `opts.secrets` is a NAME→VALUE map for a
1598
- * catalog provider whose entry declares MULTIPLE env vars (e.g. AWS Bedrock). Omitting both leaves any
1599
- * previously stored secret intact. The path `id` wins over any `id` on the provider value.
1600
- */
1601
- set(id, provider, opts = {}) {
1602
- return this.mw.http.request("PUT", `/providers/${encodeURIComponent(id)}`, {
1603
- query: this.query(opts, { scope: opts.scope, dir: opts.dir }),
1604
- body: { ...provider, id, apiKey: opts.apiKey, secrets: opts.secrets }
1605
- });
1606
- }
1607
- /** `DELETE /providers/{id}` — remove one provider and clear its stored key. Idempotent. */
1608
- async delete(id, opts = {}) {
1609
- await this.mw.http.request("DELETE", `/providers/${encodeURIComponent(id)}`, {
1610
- query: this.query(opts, { scope: opts.scope, dir: opts.dir })
1611
- });
1612
- }
1613
- };
1614
- var NotifyApi = class {
1615
- constructor(mw) {
1616
- this.mw = mw;
1617
- }
1618
- mw;
1619
- /** `GET /notify/channels` — every channel, masked (no secrets). */
1620
- channels() {
1621
- return this.mw.http.request("GET", "/notify/channels");
1622
- }
1623
- /** `POST /notify/channels` — create a channel (server-assigns the id). Returns it masked. */
1624
- createChannel(input) {
1625
- return this.mw.http.request("POST", "/notify/channels", { body: input });
1626
- }
1627
- /**
1628
- * `PUT /notify/channels/{id}` — update a channel, merge-preserving any omitted secret
1629
- * (`url`/`token`/`secret`). Returns the updated masked channel. 404 `ApiError` if unknown.
1630
- */
1631
- setChannel(id, input) {
1632
- return this.mw.http.request("PUT", `/notify/channels/${encodeURIComponent(id)}`, {
1633
- body: input
1634
- });
1635
- }
1636
- /** `DELETE /notify/channels/{id}` — remove a channel. Idempotent. */
1637
- async deleteChannel(id) {
1638
- await this.mw.http.request("DELETE", `/notify/channels/${encodeURIComponent(id)}`);
1639
- }
1640
- /**
1641
- * `POST /notify/channels/{id}/test` — deliver a synthetic notification to a channel. A failed
1642
- * delivery is DATA (`{ ok: false, error }`), not a thrown error; only an unknown id rejects (404).
1643
- */
1644
- testChannel(id) {
1645
- return this.mw.http.request(
1646
- "POST",
1647
- `/notify/channels/${encodeURIComponent(id)}/test`
1648
- );
1649
- }
1650
- /** `GET /notify/rules` — every routing rule. */
1651
- rules() {
1652
- return this.mw.http.request("GET", "/notify/rules");
1653
- }
1654
- /** `POST /notify/rules` — create a rule (server-assigns the id). Returns it. */
1655
- createRule(input) {
1656
- return this.mw.http.request("POST", "/notify/rules", { body: input });
1657
- }
1658
- /** `PUT /notify/rules/{id}` — replace a rule. Returns it. 404 `ApiError` if unknown. */
1659
- setRule(id, input) {
1660
- return this.mw.http.request("PUT", `/notify/rules/${encodeURIComponent(id)}`, {
1661
- body: input
1662
- });
1663
- }
1664
- /** `DELETE /notify/rules/{id}` — remove a rule. Idempotent. */
1665
- async deleteRule(id) {
1666
- await this.mw.http.request("DELETE", `/notify/rules/${encodeURIComponent(id)}`);
1667
- }
1668
- };
1669
- var CLOUDFLARED_VERSION = "2026.9.3";
1670
- var execute = promisify(execFile);
1671
- async function ensureCloudflareBinary(options = {}) {
1672
- const platform = options.platform ?? process.platform, arch = options.arch ?? process.arch;
1673
- const cpu = arch === "x64" ? "amd64" : arch === "arm64" ? "arm64" : "";
1674
- if (!cpu || !["darwin", "linux", "win32"].includes(platform) || platform === "win32" && cpu !== "amd64") {
1675
- throw new Error("Install cloudflared for this computer, or select another connection provider.");
1676
- }
1677
- const asset = `cloudflared-${platform === "win32" ? "windows" : platform}-${cpu}${platform === "darwin" ? ".tgz" : platform === "win32" ? ".exe" : ""}`;
1678
- const bin = path2.join(
1679
- options.cacheDir ?? await binaryCacheDirectory(),
1680
- "cloudflared",
1681
- CLOUDFLARED_VERSION,
1682
- `${platform}-${arch}`,
1683
- platform === "win32" ? "cloudflared.exe" : "cloudflared"
1684
- );
1685
- const cached = await cachedExecutable(bin);
1686
- if (cached) return cached;
1687
- await options.onProgress?.("Downloading the secure connection helper\u2026");
1688
- const request = options.fetch ?? fetch;
1689
- const release = await request(
1690
- `https://api.github.com/repos/cloudflare/cloudflared/releases/tags/${CLOUDFLARED_VERSION}`,
1691
- { headers: { Accept: "application/vnd.github+json" }, signal: AbortSignal.timeout(2e4) }
1692
- );
1693
- if (!release.ok) throw new Error("Couldn't download the connection helper. Retry, install cloudflared yourself, or choose another provider.");
1694
- const metadata = await release.json();
1695
- const item = metadata.assets?.find((value) => value.name === asset);
1696
- const checksum = item?.digest?.match(/^sha256:([a-f0-9]{64})$/i)?.[1];
1697
- const expectedURL = `https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/${asset}`;
1698
- if (!checksum || item?.browser_download_url !== expectedURL) throw new Error("The connection helper has no verified release asset for this computer.");
1699
- const downloaded = await request(expectedURL, { signal: AbortSignal.timeout(12e4) });
1700
- if (!downloaded.ok) throw new Error("Couldn't download the connection helper. Check your internet connection and retry.");
1701
- const archive = new Uint8Array(await downloaded.arrayBuffer());
1702
- await verifyReleaseBytes(archive, checksum, asset);
1703
- let executable = archive;
1704
- if (platform === "darwin") {
1705
- const temporary = await mkdtemp(path2.join(tmpdir(), "mindwire-cloudflared-"));
1706
- try {
1707
- const archivePath = path2.join(temporary, asset);
1708
- await writeFile(archivePath, archive, { mode: 384 });
1709
- const result = await execute(
1710
- "/usr/bin/tar",
1711
- ["-xzOf", archivePath, "cloudflared"],
1712
- { encoding: "buffer", maxBuffer: 128 * 1024 * 1024, timeout: 3e4 }
1713
- );
1714
- executable = new Uint8Array(result.stdout);
1715
- } finally {
1716
- await rm(temporary, { recursive: true, force: true });
1717
- }
1718
- }
1719
- return cacheExecutable(bin, executable, await executableChecksum(executable));
1720
- }
1721
- async function cloudflareCommand(options) {
1722
- try {
1723
- await execute("cloudflared", ["--version"], { timeout: 3e3, windowsHide: true });
1724
- return "cloudflared";
1725
- } catch (error) {
1726
- if (error.code !== "ENOENT") throw new Error("The installed cloudflared could not start. Repair it or choose another connection provider.");
1727
- return ensureCloudflareBinary(options);
1728
- }
84
+ const timer = setTimeout(() => {
85
+ child.kill();
86
+ finish(false);
87
+ }, 5e3);
88
+ child.once("error", () => finish(false));
89
+ child.once("exit", (code) => finish(code === 0));
90
+ });
1729
91
  }
92
+ async function showPairingInvitation(invitation, options = {}) {
93
+ const output = options.output ?? process.stdout;
94
+ const environment = options.environment ?? process.env;
95
+ const mode = options.mode ?? "auto";
96
+ const interactive = !!output.isTTY && environment.TERM !== "dumb";
97
+ const linkOnly = mode === "none" || !interactive && mode !== "browser";
98
+ if (linkOnly) {
99
+ output.write(`Paste this link in Mindwire \u2192 Add computer (expires in 5 minutes):
100
+ ${computerPairingURI(invitation)}
1730
101
 
1731
- // src/computer/relay.ts
1732
- function websocketURL(value) {
1733
- const url = new URL(value);
1734
- if (url.protocol === "https:") url.protocol = "wss:";
1735
- if (url.protocol !== "wss:" || !url.hostname || url.username || url.password || url.hash) {
1736
- throw new Error("Relay URLs must use wss:// with no embedded credentials or fragment.");
1737
- }
1738
- if (!url.pathname || url.pathname === "/") url.pathname = "/ssh";
1739
- return url.toString();
1740
- }
1741
- async function startRelay(options, port, runtime = {}) {
1742
- if (options.kind === "none") return { close() {
1743
- } };
1744
- if (options.kind === "custom") {
1745
- if (!options.url) throw new Error("A custom relay needs --relay-url wss://your-host/ssh.");
1746
- return { route: { kind: "websocket", url: websocketURL(options.url) }, close() {
102
+ Waiting for your phone\u2026
103
+ `);
104
+ return { close() {
1747
105
  } };
1748
106
  }
1749
- const origin = `http://127.0.0.1:${port}`;
1750
- const env = { ...process.env };
1751
- let command, args;
1752
- if (options.kind === "cloudflare") {
1753
- command = await cloudflareCommand(runtime);
1754
- await runtime.onProgress?.("Connecting through Cloudflare\u2026");
1755
- if (options.cloudflareTokenFile) {
1756
- if (!options.url) throw new Error("A named Cloudflare tunnel needs --relay-url for its configured hostname.");
1757
- env.TUNNEL_TOKEN = (await readFile(options.cloudflareTokenFile, "utf8")).trim();
1758
- args = ["tunnel", "--no-autoupdate", "run"];
107
+ const qr = await createPairingQR(invitation);
108
+ const useScreen = interactive && mode !== "browser";
109
+ let closed = false, pageDirectory;
110
+ let browser = "idle";
111
+ let pageURL;
112
+ let browserAttempt;
113
+ const fits = () => (output.columns ?? 0) >= qr.columns && (output.rows ?? 0) >= qr.rows;
114
+ const draw = () => {
115
+ if (closed || !useScreen) return;
116
+ let lines;
117
+ if (fits()) {
118
+ lines = [scanInstruction, "", ...qr.lines, "", "Waiting for your phone\u2026"];
1759
119
  } else {
1760
- if (options.url) throw new Error("Use --cloudflare-token-file with a named tunnel, or --relay custom for an existing tunnel.");
1761
- args = ["tunnel", "--no-autoupdate", "--url", origin];
120
+ const size = `Terminal QR needs ${qr.columns} columns \xD7 ${qr.rows} rows.`;
121
+ lines = browser === "opened" ? ["QR opened in your browser.", scanInstruction, "Return here to approve your phone."] : browser === "opening" ? ["Opening a resizable QR in your browser\u2026", "Return here to approve your phone."] : browser === "failed" ? ["Couldn't open a browser here.", size, "Enlarge this terminal, or run:", "mindwire connect --no-qr"] : [size, "Enlarge this terminal, or use --qr browser."];
122
+ lines = lines.map((line) => line.slice(0, Math.max(0, (output.columns ?? 80) - 1))).slice(0, Math.max(0, (output.rows ?? 24) - 1));
1762
123
  }
1763
- } else {
1764
- command = "ngrok";
1765
- args = ["http", origin, "--log", "stdout", "--log-format", "json"];
1766
- if (options.url) {
1767
- const url = new URL(websocketURL(options.url));
1768
- args.push("--url", `https://${url.host}`);
124
+ output.write(clearScreen + lines.join("\r\n"));
125
+ };
126
+ const ensureBrowser = () => {
127
+ if (browserAttempt) return browserAttempt;
128
+ browser = "opening";
129
+ draw();
130
+ browserAttempt = (async () => {
131
+ try {
132
+ pageDirectory = mkdtempSync(join(tmpdir(), "mindwire-pair-"));
133
+ chmodSync(pageDirectory, 448);
134
+ const file = join(pageDirectory, "pair.html");
135
+ writeFileSync(file, browserPage(invitation, qr.svg), { mode: 384 });
136
+ pageURL = pathToFileURL(file).href;
137
+ const remoteShell = !!(environment.SSH_CONNECTION || environment.SSH_CLIENT || environment.SSH_TTY);
138
+ const opened = (mode === "browser" || !remoteShell) && await (options.openBrowser ?? openBrowser)(pageURL);
139
+ browser = opened ? "opened" : "failed";
140
+ } catch {
141
+ browser = "failed";
142
+ }
143
+ if (closed) return;
144
+ if (useScreen) draw();
145
+ else output.write(browser === "opened" ? `QR opened in your browser.
146
+ ${scanInstruction}
147
+ Return here to approve your phone.
148
+ ` : `Open this local QR page in a browser:
149
+ ${pageURL ?? "Page unavailable. Run mindwire connect --no-qr for a pairing link."}
150
+
151
+ Waiting for your phone\u2026
152
+ `);
153
+ })();
154
+ return browserAttempt;
155
+ };
156
+ const resize = () => {
157
+ draw();
158
+ if (!closed && mode === "auto" && !fits()) void ensureBrowser();
159
+ };
160
+ const close = () => {
161
+ if (closed) return;
162
+ closed = true;
163
+ output.off("resize", resize);
164
+ process.off("exit", close);
165
+ process.off("SIGINT", interrupt);
166
+ process.off("SIGTERM", terminate);
167
+ process.off("SIGHUP", hangup);
168
+ if (useScreen) output.write(leaveScreen);
169
+ if (pageDirectory) {
170
+ try {
171
+ rmSync(pageDirectory, { recursive: true, force: true });
172
+ } catch {
173
+ }
1769
174
  }
175
+ };
176
+ const interrupt = () => {
177
+ close();
178
+ process.exit(130);
179
+ };
180
+ const terminate = () => {
181
+ close();
182
+ process.exit(143);
183
+ };
184
+ const hangup = () => {
185
+ close();
186
+ process.exit(129);
187
+ };
188
+ process.once("exit", close);
189
+ process.once("SIGINT", interrupt);
190
+ process.once("SIGTERM", terminate);
191
+ process.once("SIGHUP", hangup);
192
+ if (useScreen) {
193
+ output.write(enterScreen);
194
+ output.on("resize", resize);
195
+ draw();
1770
196
  }
1771
- const child = spawn(command, args, { env, stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
1772
- let buffer = "";
1773
197
  try {
1774
- const address = await new Promise((resolve3, reject) => {
1775
- const timer = setTimeout(() => {
1776
- cleanup();
1777
- reject(new Error(`${command} did not establish its tunnel. Check its account and tunnel configuration.`));
1778
- }, 45e3);
1779
- const cleanup = () => {
1780
- clearTimeout(timer);
1781
- child.off("error", fail);
1782
- child.off("exit", exited);
1783
- };
1784
- const fail = (error) => {
1785
- cleanup();
1786
- reject(new Error(`Could not run ${command}. Install it on the computer first.`, { cause: error }));
1787
- };
1788
- const exited = () => {
1789
- cleanup();
1790
- reject(new Error(`${command} exited before the tunnel connected. Check its account configuration.`));
1791
- };
1792
- let cloudflareAddress = options.url;
1793
- let cloudflareConnected = false;
1794
- const receive = (chunk) => {
1795
- const received = buffer + chunk.toString();
1796
- buffer = received.slice(-16384);
1797
- let found;
1798
- if (options.kind === "cloudflare") {
1799
- cloudflareAddress ??= received.match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com/)?.[0];
1800
- cloudflareConnected ||= /Registered tunnel connection/.test(received);
1801
- if (cloudflareConnected) found = cloudflareAddress;
1802
- } else {
1803
- for (const line of received.split(/\r?\n/)) {
1804
- try {
1805
- const event = JSON.parse(line);
1806
- if (event.msg === "started tunnel" && event.url?.startsWith("https://")) found = event.url;
1807
- } catch {
1808
- }
1809
- }
1810
- }
1811
- if (found) {
1812
- cleanup();
1813
- resolve3(websocketURL(found));
1814
- }
1815
- };
1816
- child.once("error", fail);
1817
- child.once("exit", exited);
1818
- child.stdout.on("data", receive);
1819
- child.stderr.on("data", receive);
1820
- });
1821
- child.stdout.removeAllListeners("data");
1822
- child.stderr.removeAllListeners("data");
1823
- child.stdout.resume();
1824
- child.stderr.resume();
1825
- child.on("error", () => {
1826
- });
1827
- return { route: { kind: "websocket", url: address }, process: child, close() {
1828
- child.kill();
1829
- } };
198
+ if (mode === "browser" || mode === "auto" && !fits()) await ensureBrowser();
199
+ return { close };
1830
200
  } catch (error) {
1831
- child.kill();
201
+ close();
1832
202
  throw error;
1833
203
  }
1834
204
  }
1835
-
1836
- // src/computer/lifecycle.ts
1837
- var defaultStateDirectory = () => path2.join(homedir(), ".mindwire", "computer");
1838
- var defaultComputerConfig = () => ({ directory: homedir(), bind: "0.0.0.0", sshPort: 8791, websocketPort: 8792, relay: { kind: "cloudflare" } });
1839
- async function readJSON(file) {
1840
- try {
1841
- return JSON.parse(await fs.readFile(file, "utf8"));
1842
- } catch (error) {
1843
- if (error.code === "ENOENT") return void 0;
1844
- throw error;
205
+ var execute = promisify(execFile);
206
+ var xml = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
207
+ var systemd = (value, expandEnvironment = false) => '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/%/g, "%%").replace(/\$/g, () => expandEnvironment ? "$$" : "$") + '"';
208
+ var windowsArg = (value) => '"' + value.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/g, "$1$1") + '"';
209
+ function startupPlan(options) {
210
+ const { directory, cliPath } = options;
211
+ const executable = options.executable ?? process.execPath, home = options.home ?? homedir();
212
+ const platform = options.platform ?? process.platform;
213
+ const environmentPath = options.environmentPath ?? process.env.PATH ?? path.dirname(executable);
214
+ for (const value of [directory, cliPath, executable, home, environmentPath]) {
215
+ if (/[\r\n\x00]/.test(value)) throw new Error("Startup paths cannot contain control characters.");
216
+ }
217
+ const id = createHash("sha256").update(directory).digest("hex").slice(0, 12);
218
+ const name = `sh.mindwire.computer.${id}`;
219
+ const args = [cliPath, "_watch", "--state-dir", directory];
220
+ if (platform === "darwin") {
221
+ const domain = `gui/${options.uid ?? process.getuid?.()}`;
222
+ const file = path.join(home, "Library", "LaunchAgents", name + ".plist");
223
+ return {
224
+ kind: "launchd",
225
+ name,
226
+ file,
227
+ content: `<?xml version="1.0" encoding="UTF-8"?>
228
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
229
+ <plist version="1.0"><dict>
230
+ <key>Label</key><string>${name}</string>
231
+ <key>ProgramArguments</key><array>${[executable, ...args].map((v) => `<string>${xml(v)}</string>`).join("")}</array>
232
+ <key>WorkingDirectory</key><string>${xml(home)}</string>
233
+ <key>EnvironmentVariables</key><dict><key>PATH</key><string>${xml(environmentPath)}</string></dict>
234
+ <key>RunAtLoad</key><true/><key>KeepAlive</key><true/>
235
+ <key>ThrottleInterval</key><integer>10</integer>
236
+ <key>StandardOutPath</key><string>${xml(path.join(directory, "startup.log"))}</string>
237
+ <key>StandardErrorPath</key><string>${xml(path.join(directory, "startup.log"))}</string>
238
+ </dict></plist>
239
+ `,
240
+ query: ["launchctl", "print", `${domain}/${name}`],
241
+ enable: [["launchctl", "enable", `${domain}/${name}`], ["launchctl", "bootstrap", domain, file]],
242
+ disable: [["launchctl", "bootout", `${domain}/${name}`]]
243
+ };
1845
244
  }
1846
- }
1847
- async function writeJSON(file, value) {
1848
- const temp = `${file}.${randomUUID()}.tmp`;
1849
- try {
1850
- await fs.writeFile(temp, JSON.stringify(value) + "\n", { mode: 384 });
1851
- await fs.rename(temp, file);
1852
- } finally {
1853
- await fs.rm(temp, { force: true });
245
+ if (platform === "linux") {
246
+ const file = path.join(home, ".config", "systemd", "user", name + ".service");
247
+ return {
248
+ kind: "systemd",
249
+ name,
250
+ file,
251
+ content: `[Unit]
252
+ Description=Mindwire computer connection
253
+ After=network-online.target
254
+ [Service]
255
+ Type=simple
256
+ ExecStart=${[executable, ...args].map((value) => systemd(value, true)).join(" ")}
257
+ WorkingDirectory=${systemd(home)}
258
+ Environment=${systemd("PATH=" + environmentPath)}
259
+ Restart=always
260
+ RestartSec=10
261
+ KillMode=process
262
+ [Install]
263
+ WantedBy=default.target
264
+ `,
265
+ query: ["systemctl", "--user", "is-active", name + ".service"],
266
+ enable: [["systemctl", "--user", "daemon-reload"], ["systemctl", "--user", "enable", "--now", name + ".service"]],
267
+ disable: [["systemctl", "--user", "disable", "--now", name + ".service"], ["systemctl", "--user", "daemon-reload"]]
268
+ };
1854
269
  }
1855
- }
1856
- function alive(pid) {
1857
- if (!pid || !Number.isSafeInteger(pid) || pid < 1) return false;
1858
- try {
1859
- process.kill(pid, 0);
1860
- return true;
1861
- } catch {
1862
- return false;
270
+ if (platform === "win32") {
271
+ if (!options.user) throw new Error("Couldn't determine the Windows user for startup.");
272
+ const file = path.join(directory, "startup-task.xml");
273
+ return {
274
+ kind: "task-scheduler",
275
+ name,
276
+ file,
277
+ content: `<?xml version="1.0" encoding="UTF-16"?>
278
+ <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
279
+ <Triggers><LogonTrigger><Enabled>true</Enabled><UserId>${xml(options.user)}</UserId></LogonTrigger></Triggers>
280
+ <Principals><Principal id="Owner"><UserId>${xml(options.user)}</UserId><LogonType>InteractiveToken</LogonType><RunLevel>LeastPrivilege</RunLevel></Principal></Principals>
281
+ <Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><StartWhenAvailable>true</StartWhenAvailable><ExecutionTimeLimit>PT0S</ExecutionTimeLimit><RestartOnFailure><Interval>PT1M</Interval><Count>999</Count></RestartOnFailure></Settings>
282
+ <Actions Context="Owner"><Exec><Command>${xml(executable)}</Command><Arguments>${xml(args.map(windowsArg).join(" "))}</Arguments><WorkingDirectory>${xml(home)}</WorkingDirectory></Exec></Actions>
283
+ </Task>
284
+ `,
285
+ query: ["schtasks.exe", "/Query", "/TN", name],
286
+ enable: [["schtasks.exe", "/Create", "/TN", name, "/XML", file, "/F"], ["schtasks.exe", "/Run", "/TN", name]],
287
+ disable: [["schtasks.exe", "/End", "/TN", name], ["schtasks.exe", "/Delete", "/TN", name, "/F"]]
288
+ };
1863
289
  }
290
+ throw new Error("Automatic startup supports macOS, Linux with systemd, and Windows. Run mindwire start on this system.");
1864
291
  }
1865
- async function computerClient(directory) {
1866
- const runtime = await readJSON(path2.join(directory, "computer-runtime.json"));
1867
- if (!runtime) throw new Error("Mindwire is not running. Run mindwire connect on the computer.");
1868
- const url = new URL(`http://${runtime.apiAddress}`);
1869
- if (!["127.0.0.1", "[::1]"].includes(url.hostname) || url.username || url.password) throw new Error("Invalid local Mindwire address.");
1870
- const token = (await fs.readFile(path2.join(directory, "daemon.token"), "utf8")).trim();
1871
- const client = new Mindwire({ target: remote(url.origin, { token }), requestTimeoutMs: 3e3 });
1872
- const info = await client.computer.info();
1873
- if (info.computerId !== runtime.computerId || info.pid !== runtime.pid) throw new Error("The local Mindwire process changed. Retry the command.");
1874
- return client;
1875
- }
1876
- function directRoutes(config, port) {
1877
- if (config.host) return [{ kind: "ssh", host: config.host, port }];
1878
- if (!["0.0.0.0", "::"].includes(config.bind)) return [{ kind: "ssh", host: config.bind, port }];
1879
- const addresses = /* @__PURE__ */ new Set();
1880
- for (const entries of Object.values(networkInterfaces())) {
1881
- for (const address of entries ?? []) if (address.family === "IPv4" && !address.internal) addresses.add(address.address);
1882
- }
1883
- return [...addresses].slice(0, 6).map((host) => ({ kind: "ssh", host, port }));
292
+ var run = async ([command, ...args]) => {
293
+ await execute(command, args, { timeout: 15e3, windowsHide: true });
294
+ };
295
+ async function startupStatus(directory) {
296
+ const state = await readJSON(path.join(directory, "computer-startup.json")) ?? { enabled: false };
297
+ const guardian = await readJSON(path.join(directory, "computer-guardian.json"));
298
+ return { ...state, running: state.enabled && await processStateAlive(guardian) };
1884
299
  }
1885
- async function ensureComputer(directory, cliPath, patch = {}, onProgress) {
300
+ async function configureStartup(directory, cliPath, enabled, runtime = {}) {
1886
301
  await fs.mkdir(directory, { recursive: true, mode: 448 });
1887
- let current;
1888
- try {
1889
- current = await computerClient(directory);
1890
- } catch {
1891
- }
1892
- const configPath = path2.join(directory, "computer-config.json");
1893
- const previous = await readJSON(configPath);
1894
- const config = { ...defaultComputerConfig(), ...previous, ...patch };
1895
- const waitUntilReady = async () => {
1896
- let phase;
1897
- for (let attempt = 0; attempt < 1200; attempt++) {
1898
- const controller = await readJSON(path2.join(directory, "computer-controller.json"));
1899
- if (controller?.error) throw new Error(controller.error);
1900
- if (controller?.ready) return computerClient(directory);
1901
- if (controller?.phase && controller.phase !== phase) {
1902
- phase = controller.phase;
1903
- onProgress?.(phase);
1904
- }
1905
- await setTimeout$1(250);
1906
- }
1907
- throw new Error(`Mindwire did not finish starting. Inspect ${path2.join(directory, "computer.log")}.`);
1908
- };
1909
- if (current) {
1910
- if (Object.keys(patch).some((key) => JSON.stringify(config[key]) !== JSON.stringify(previous?.[key]))) {
1911
- throw new Error("Mindwire is running with different connection settings. Run mindwire stop when idle, then retry with the new settings.");
1912
- }
1913
- return waitUntilReady();
1914
- }
1915
- const lockPath = path2.join(directory, "launch.lock");
1916
- let lock;
1917
- for (let attempt = 0; attempt < 120; attempt++) {
1918
- try {
1919
- lock = await fs.open(lockPath, "wx", 384);
1920
- await lock.writeFile(String(process.pid));
1921
- break;
1922
- } catch (error) {
1923
- if (error.code !== "EEXIST") throw error;
1924
- const owner = Number(await fs.readFile(lockPath, "utf8").catch(() => ""));
1925
- const age = Date.now() - (await fs.stat(lockPath).catch(() => ({ mtimeMs: Date.now() }))).mtimeMs;
1926
- if (age > 5e3 && !alive(owner)) {
1927
- await fs.rm(lockPath, { force: true });
1928
- continue;
1929
- }
1930
- const controller = await readJSON(path2.join(directory, "computer-controller.json"));
1931
- if (alive(controller?.pid)) {
1932
- const saved = await readJSON(configPath);
1933
- if (Object.keys(patch).some((key) => JSON.stringify(patch[key]) !== JSON.stringify(saved?.[key]))) {
1934
- throw new Error("Another start used different connection settings. Stop Mindwire when idle before changing them.");
1935
- }
1936
- return waitUntilReady();
1937
- }
1938
- await setTimeout$1(250);
1939
- }
1940
- }
1941
- if (!lock) throw new Error("Another Mindwire start is in progress. Try again shortly.");
302
+ const lockPath = path.join(directory, "startup.lock");
303
+ const lock = await acquireProcessLock(lockPath);
304
+ if (!lock) throw new Error("Startup is already being configured. Try again shortly.");
1942
305
  try {
1943
- let existing;
306
+ const user = process.platform === "win32" && !runtime.plan ? (await execute("whoami.exe", [], { timeout: 3e3, windowsHide: true })).stdout.trim() : void 0;
307
+ const plan = runtime.plan ?? startupPlan({ directory, cliPath, user });
308
+ const command = runtime.run ?? run;
309
+ let loaded = false;
1944
310
  try {
1945
- existing = await computerClient(directory);
311
+ await command(plan.query);
312
+ loaded = true;
1946
313
  } catch {
1947
314
  }
1948
- if (existing) {
1949
- const saved = await readJSON(configPath);
1950
- if (Object.keys(patch).some((key) => JSON.stringify(patch[key]) !== JSON.stringify(saved?.[key]))) {
1951
- throw new Error("Another start used different connection settings. Stop Mindwire when idle before changing them.");
315
+ if (enabled) {
316
+ const previous = await fs.readFile(plan.file, plan.kind === "task-scheduler" ? "utf16le" : "utf8").catch(() => "");
317
+ const changed = previous !== plan.content;
318
+ if (changed) {
319
+ await fs.mkdir(path.dirname(plan.file), { recursive: true, mode: 448 });
320
+ const temporary = plan.file + `.tmp-${process.pid}`;
321
+ try {
322
+ await fs.writeFile(temporary, plan.content, { mode: 384, encoding: plan.kind === "task-scheduler" ? "utf16le" : "utf8" });
323
+ await fs.rename(temporary, plan.file);
324
+ } finally {
325
+ await fs.rm(temporary, { force: true });
326
+ }
327
+ }
328
+ if (!loaded || changed && plan.kind !== "launchd") {
329
+ for (const action of plan.enable) await command(action);
330
+ }
331
+ await command(plan.query);
332
+ if (runtime.verify) await runtime.verify();
333
+ else {
334
+ let running = false;
335
+ for (let attempt = 0; attempt < 60; attempt++) {
336
+ const guardian = await readJSON(path.join(directory, "computer-guardian.json"));
337
+ if (await processStateAlive(guardian)) {
338
+ running = true;
339
+ break;
340
+ }
341
+ await setTimeout$1(250);
342
+ }
343
+ if (!running) throw new Error(`Automatic startup was registered but its guardian didn't start. Inspect ${path.join(directory, "startup.log")}.`);
1952
344
  }
1953
- return waitUntilReady();
1954
- }
1955
- const controller = await readJSON(path2.join(directory, "computer-controller.json"));
1956
- if (alive(controller?.pid)) {
1957
- if (controller?.error) throw new Error(controller.error);
1958
345
  } else {
1959
- await writeJSON(configPath, config);
1960
- await fs.rm(path2.join(directory, "computer-controller.json"), { force: true });
1961
- const log = await fs.open(path2.join(directory, "computer.log"), "a", 384);
1962
- try {
1963
- const child = spawn(process.execPath, [cliPath, "_serve", "--state-dir", directory], {
1964
- detached: true,
1965
- stdio: ["ignore", log.fd, log.fd],
1966
- windowsHide: true
1967
- });
1968
- await new Promise((resolve3, reject) => {
1969
- child.once("spawn", resolve3);
1970
- child.once("error", reject);
1971
- });
1972
- child.unref();
1973
- } finally {
1974
- await log.close();
346
+ if (loaded || await fs.stat(plan.file).then(() => true, () => false)) {
347
+ for (const action of plan.disable) {
348
+ try {
349
+ await command(action);
350
+ } catch (error) {
351
+ if (action.includes("/End") || action.includes("bootout") && !loaded) continue;
352
+ throw error;
353
+ }
354
+ }
1975
355
  }
356
+ await fs.rm(plan.file, { force: true });
1976
357
  }
1977
- return await waitUntilReady();
358
+ const state = { enabled, kind: plan.kind, name: plan.name, file: plan.file };
359
+ await writeJSON(path.join(directory, "computer-startup.json"), state);
360
+ return { ...state, running: enabled };
1978
361
  } finally {
1979
362
  await lock.close();
1980
- await fs.rm(lockPath, { force: true });
1981
- }
1982
- }
1983
- async function terminate(child) {
1984
- if (child.exitCode !== null || child.signalCode !== null) return;
1985
- const exited = new Promise((resolve3) => child.once("exit", () => resolve3()));
1986
- child.kill("SIGTERM");
1987
- const timer = setTimeout(() => child.kill("SIGKILL"), 15e3);
1988
- try {
1989
- await exited;
1990
- } finally {
1991
- clearTimeout(timer);
1992
363
  }
1993
364
  }
1994
- async function superviseComputer(directory) {
1995
- let config = await readJSON(path2.join(directory, "computer-config.json"));
1996
- if (!config) throw new Error("Computer configuration is missing.");
1997
- const controllerPath = path2.join(directory, "computer-controller.json");
1998
- const previous = await readJSON(controllerPath);
1999
- if (alive(previous?.pid)) throw new Error("The computer already has a service controller.");
2000
- const phase = (message) => writeJSON(controllerPath, { pid: process.pid, ready: false, phase: message });
2001
- await phase("Preparing Mindwire\u2026");
2002
- let child, relay, stopping = false;
365
+ async function watchComputer(directory, cliPath) {
366
+ let stopping = false;
367
+ const abort = new AbortController();
2003
368
  const stop = () => {
2004
369
  stopping = true;
370
+ abort.abort();
2005
371
  };
2006
372
  process.on("SIGTERM", stop);
2007
373
  process.on("SIGINT", stop);
2008
- let token;
2009
- try {
2010
- token = (await fs.readFile(path2.join(directory, "daemon.token"), "utf8")).trim();
2011
- } catch (error) {
2012
- if (error.code !== "ENOENT") throw error;
2013
- token = randomBytes(32).toString("hex");
2014
- await fs.writeFile(path2.join(directory, "daemon.token"), token, { mode: 384, flag: "wx" });
2015
- }
2016
- const daemonEnv = { ...process.env };
2017
- if (process.platform === "win32") {
2018
- try {
2019
- const git = execFileSync("where.exe", ["git"], { encoding: "utf8", timeout: 3e3, windowsHide: true }).split(/\r?\n/)[0];
2020
- if (git) {
2021
- const shellDirectory = path2.resolve(path2.dirname(git), "..", "bin");
2022
- await fs.access(path2.join(shellDirectory, "bash.exe"));
2023
- const pathKey = Object.keys(daemonEnv).find((key) => key.toLowerCase() === "path") ?? "PATH";
2024
- daemonEnv[pathKey] = `${shellDirectory}${path2.delimiter}${daemonEnv[pathKey] ?? ""}`;
2025
- }
2026
- } catch {
2027
- }
2028
- }
2029
- let sshPort = config.sshPort, websocketPort = config.websocketPort;
2030
- const launch = async (binary) => {
2031
- await fs.rm(path2.join(directory, "computer-runtime.json"), { force: true });
2032
- child = spawn(binary, ["--computer"], {
2033
- cwd: config.directory,
2034
- stdio: ["ignore", "inherit", "inherit"],
2035
- windowsHide: true,
2036
- env: {
2037
- ...daemonEnv,
2038
- DAEMON_TOKEN: token,
2039
- ADDR: "127.0.0.1:0",
2040
- STATE_PATH: path2.join(directory, "agent-state.json"),
2041
- WORKSPACE_DB_PATH: path2.join(directory, "workspace.db"),
2042
- AGENT_CWD: config.directory,
2043
- COMPUTER_SSH_ADDR: `${config.bind.includes(":") ? `[${config.bind}]` : config.bind}:${sshPort}`,
2044
- COMPUTER_WS_ADDR: `127.0.0.1:${websocketPort}`,
2045
- MINDWIRE_COMPUTER_MANAGED: "1"
2046
- }
2047
- });
2048
- let failure;
2049
- child.on("error", (error) => {
2050
- failure = error;
2051
- });
2052
- for (let attempt = 0; attempt < 100; attempt++) {
2053
- if (failure) throw failure;
2054
- if (child.exitCode !== null || child.signalCode !== null) throw new Error("Mindwire exited during startup. Check computer.log; the downloaded release must support computer connections.");
2055
- try {
2056
- const client = await computerClient(directory);
2057
- const health = await client.health();
2058
- if (health.computerConnectionVersion !== 1) throw new Error("This daemon release does not support computer connections. Update Mindwire.");
2059
- return client;
2060
- } catch (error) {
2061
- if (attempt === 99) throw error;
2062
- }
2063
- await setTimeout$1(100);
2064
- }
2065
- throw new Error("Mindwire startup timed out.");
2066
- };
2067
- let routes = [];
374
+ let reportedError = "";
375
+ const guardianPath = path.join(directory, "computer-guardian.json");
2068
376
  try {
2069
- await phase("Checking the Mindwire service\u2026");
2070
- let binary = config.daemonBin ?? await ensureDaemonBinary({ version: config.version ?? SDK_VERSION, cacheDir: process.env.MINDWIRE_RELEASE_CACHE_DIR });
2071
- await phase("Starting the Mindwire service\u2026");
2072
- let client = await launch(binary);
2073
- const info = await client.computer.info();
2074
- sshPort = info.sshPort;
2075
- websocketPort = info.websocketPort;
2076
- await phase(config.relay.kind === "none" ? "Preparing direct SSH\u2026" : "Preparing your internet connection\u2026");
2077
- relay = await startRelay(config.relay, info.websocketPort, { cacheDir: path2.join(directory, "tools"), onProgress: phase });
2078
- routes = directRoutes(config, info.sshPort);
2079
- if (relay.route) routes.push(relay.route);
2080
- if (routes.length === 0) throw new Error("No network address is available. Use --host with your LAN/VPN address or choose a relay.");
2081
- await client.computer.setRoutes(routes);
2082
- await writeJSON(controllerPath, { pid: process.pid, ready: true, routes });
377
+ await writeJSON(guardianPath, await currentProcessIdentity());
2083
378
  while (!stopping) {
2084
- await setTimeout$1(500);
2085
- if (child.exitCode !== null || child.signalCode !== null) throw new Error("The Mindwire service stopped. Run mindwire start to reconnect.");
2086
- if (relay.process && (relay.process.exitCode !== null || relay.process.signalCode !== null)) {
2087
- routes = routes.filter((r) => r.kind === "ssh");
2088
- if (routes.length > 0) await client.computer.setRoutes(routes);
2089
- await writeJSON(controllerPath, { pid: process.pid, ready: true, routes, error: "The relay disconnected. Direct SSH still works. Restart Mindwire when idle to reconnect the relay." });
2090
- relay = { close() {
2091
- } };
2092
- }
2093
- const updatePath = path2.join(directory, "computer-update.json");
2094
- const update = await readJSON(updatePath);
2095
- if (!update || !["queued", "downloading", "waiting", "restarting"].includes(update.status)) continue;
2096
- const status = async (state, error) => writeJSON(updatePath, { ...update, status: state, error, updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
2097
- try {
2098
- await status("downloading");
2099
- const replacement = await ensureDaemonBinary({ version: update.version, cacheDir: process.env.MINDWIRE_RELEASE_CACHE_DIR });
2100
- await status("waiting");
2101
- let lease;
2102
- while (!stopping && !lease) {
2103
- try {
2104
- lease = await client.service.acquireUpdate();
2105
- } catch (error) {
2106
- if (error.status !== 409) throw error;
2107
- await setTimeout$1(2e3);
2108
- }
2109
- }
2110
- if (stopping || !lease) break;
2111
- if (lease.pid !== child.pid) {
2112
- await client.service.releaseUpdate(lease.id);
2113
- throw new Error("The leased service process changed.");
2114
- }
2115
- await status("restarting");
2116
- await terminate(child);
379
+ const paused = await readJSON(path.join(directory, "computer-stopped.json"));
380
+ const controller = await readJSON(path.join(directory, "computer-controller.json"));
381
+ if (!paused?.stopped && !await processStateAlive(controller)) {
2117
382
  try {
2118
- client = await launch(replacement);
2119
- await client.computer.setRoutes(routes);
2120
- const health = await client.health();
2121
- if (health.version.replace(/^v/, "") !== update.version) throw new Error("The replacement service reports a different release version.");
383
+ await ensureComputer(directory, cliPath, {}, void 0, { resume: false, signal: abort.signal });
384
+ reportedError = "";
2122
385
  } catch (error) {
2123
- if (child) await terminate(child);
2124
- client = await launch(binary);
2125
- await client.computer.setRoutes(routes);
2126
- throw error;
386
+ if (stopping) break;
387
+ const message = error instanceof Error ? error.message : "Computer recovery failed.";
388
+ if (message !== reportedError) {
389
+ process.stderr.write(`Mindwire recovery: ${message}
390
+ `);
391
+ reportedError = message;
392
+ }
2127
393
  }
2128
- binary = replacement;
2129
- config = { ...config, daemonBin: void 0, version: update.version };
2130
- await writeJSON(path2.join(directory, "computer-config.json"), config);
2131
- await status("complete");
2132
- } catch (error) {
2133
- await status("failed", error instanceof Error ? error.message : "Service update failed.");
2134
394
  }
395
+ for (let tick = 0; tick < 10 && !stopping; tick++) await setTimeout$1(500);
2135
396
  }
2136
- } catch (error) {
2137
- await writeJSON(controllerPath, { pid: process.pid, ready: false, error: error instanceof Error ? error.message : "Computer service failed." });
2138
- throw error;
2139
397
  } finally {
2140
- relay?.close();
2141
- if (child) await terminate(child);
398
+ if ((await readJSON(guardianPath))?.pid === process.pid) await writeJSON(guardianPath, { pid: 0 });
2142
399
  process.off("SIGTERM", stop);
2143
400
  process.off("SIGINT", stop);
2144
- if (stopping) await writeJSON(controllerPath, { pid: 0, ready: false });
2145
- }
2146
- }
2147
- async function stopComputer(directory, force = false) {
2148
- const client = await computerClient(directory);
2149
- const controller = await readJSON(path2.join(directory, "computer-controller.json"));
2150
- if (!alive(controller?.pid)) throw new Error("The Mindwire controller is not running.");
2151
- const lease = force ? void 0 : await client.service.acquireUpdate();
2152
- try {
2153
- process.kill(controller.pid, "SIGTERM");
2154
- } catch (error) {
2155
- if (lease) await client.service.releaseUpdate(lease.id);
2156
- throw error;
2157
401
  }
2158
- for (let attempt = 0; attempt < 80; attempt++) {
2159
- if (!alive(controller.pid)) return;
2160
- await setTimeout$1(250);
2161
- }
2162
- throw new Error("Mindwire is still stopping. Check computer.log.");
2163
402
  }
2164
403
 
2165
404
  // src/cli.ts
@@ -2174,6 +413,8 @@ var help = `Mindwire \u2014 connect this computer to your phone
2174
413
  mindwire connect --relay custom --relay-url wss://computer.example/ssh
2175
414
 
2176
415
  mindwire start Start the saved connection in the background
416
+ mindwire startup enable|disable Manage automatic startup after computer login
417
+ mindwire startup status Check automatic startup
2177
418
  mindwire status Show connection and running service
2178
419
  mindwire devices List paired phones
2179
420
  mindwire revoke DEVICE_ID Disconnect and revoke one phone
@@ -2188,7 +429,9 @@ Options:
2188
429
  --cloudflare-token-file PATH Named Cloudflare tunnel token file
2189
430
  --state-dir PATH Private state directory
2190
431
  --json Structured output (QR invitation includes a secret)
432
+ --qr auto|terminal|browser Fit the QR to your terminal or open a local page
2191
433
  --no-qr Print only the pairing link
434
+ --no-startup Pair without changing automatic startup
2192
435
 
2193
436
  The default works across Wi-Fi and mobile networks. Mindwire privately downloads
2194
437
  a verified Cloudflare helper when needed. Relays carry encrypted SSH.
@@ -2204,6 +447,7 @@ async function main(args = process.argv.slice(2)) {
2204
447
  help: { type: "boolean", short: "h" },
2205
448
  json: { type: "boolean" },
2206
449
  "no-qr": { type: "boolean" },
450
+ qr: { type: "string" },
2207
451
  "state-dir": { type: "string" },
2208
452
  directory: { type: "string" },
2209
453
  host: { type: "string" },
@@ -2215,15 +459,17 @@ async function main(args = process.argv.slice(2)) {
2215
459
  port: { type: "string" },
2216
460
  "websocket-port": { type: "string" },
2217
461
  version: { type: "string" },
2218
- force: { type: "boolean" }
462
+ force: { type: "boolean" },
463
+ "no-startup": { type: "boolean" }
2219
464
  } });
2220
465
  const command = positionals[0] ?? "help";
2221
466
  if (values.help || command === "help") {
2222
467
  process.stdout.write(help);
2223
468
  return;
2224
469
  }
2225
- const directory = path2.resolve(values["state-dir"] ?? defaultStateDirectory());
2226
- const emit2 = (event, data = {}, text) => {
470
+ if (values.qr && !["auto", "terminal", "browser"].includes(values.qr)) throw new Error("Choose --qr auto, terminal or browser.");
471
+ const directory = path.resolve(values["state-dir"] ?? defaultStateDirectory());
472
+ const emit = (event, data = {}, text) => {
2227
473
  if (values.json) process.stdout.write(JSON.stringify({ event, ...data }) + "\n");
2228
474
  else if (text) process.stdout.write(text + "\n");
2229
475
  };
@@ -2231,9 +477,20 @@ async function main(args = process.argv.slice(2)) {
2231
477
  await superviseComputer(directory);
2232
478
  return;
2233
479
  }
480
+ if (command === "_watch") {
481
+ await watchComputer(directory, fileURLToPath(import.meta.url));
482
+ return;
483
+ }
484
+ if (command === "startup") {
485
+ const action = positionals[1] ?? "status";
486
+ if (!["enable", "disable", "status"].includes(action)) throw new Error("Use mindwire startup enable, disable or status.");
487
+ const state = action === "status" ? await startupStatus(directory) : await configureStartup(directory, fileURLToPath(import.meta.url), action === "enable");
488
+ emit("startup", state, state.enabled ? "Mindwire starts after you sign in to this computer and recovers automatically." : "Automatic startup is off. Use mindwire start to run the service.");
489
+ return;
490
+ }
2234
491
  if (["connect", "start"].includes(command)) {
2235
492
  const patch = {};
2236
- if (values.directory) patch.directory = path2.resolve(values.directory);
493
+ if (values.directory) patch.directory = path.resolve(values.directory);
2237
494
  if (values.host) patch.host = values.host;
2238
495
  if (values.bind) patch.bind = values.bind;
2239
496
  for (const [flag, field] of [["port", "sshPort"], ["websocket-port", "websocketPort"]]) {
@@ -2243,7 +500,7 @@ async function main(args = process.argv.slice(2)) {
2243
500
  patch[field] = n;
2244
501
  }
2245
502
  }
2246
- if (values["daemon-bin"] || process.env.MINDWIRE_DAEMON) patch.daemonBin = path2.resolve(values["daemon-bin"] ?? process.env.MINDWIRE_DAEMON);
503
+ if (values["daemon-bin"] || process.env.MINDWIRE_DAEMON) patch.daemonBin = path.resolve(values["daemon-bin"] ?? process.env.MINDWIRE_DAEMON);
2247
504
  if (values.version) patch.version = values.version.replace(/^v/, "");
2248
505
  if (values.relay || values["relay-url"] || values["cloudflare-token-file"]) {
2249
506
  const kind = values.relay ?? "custom";
@@ -2251,98 +508,110 @@ async function main(args = process.argv.slice(2)) {
2251
508
  patch.relay = {
2252
509
  kind,
2253
510
  url: values["relay-url"],
2254
- cloudflareTokenFile: values["cloudflare-token-file"] ? path2.resolve(values["cloudflare-token-file"]) : void 0
511
+ cloudflareTokenFile: values["cloudflare-token-file"] ? path.resolve(values["cloudflare-token-file"]) : void 0
2255
512
  };
2256
513
  }
2257
- emit2("starting", {}, "Starting Mindwire\u2026");
514
+ emit("starting", {}, "Starting Mindwire\u2026");
2258
515
  const client2 = await ensureComputer(
2259
516
  directory,
2260
517
  fileURLToPath(import.meta.url),
2261
518
  patch,
2262
- (phase) => emit2("progress", { message: phase }, phase)
519
+ (phase) => emit("progress", { message: phase }, phase)
2263
520
  );
2264
521
  const info = await client2.computer.info();
2265
- emit2("ready", { computerId: info.computerId, routes: info.routes }, "Mindwire is running in the background.");
522
+ emit("ready", { computerId: info.computerId, routes: info.routes }, "Mindwire is running in the background.");
2266
523
  if (command === "start") return;
524
+ const startupPreference = await startupStatus(directory);
525
+ if (!values["no-startup"] && (startupPreference.enabled || !startupPreference.kind)) {
526
+ try {
527
+ const startup = await configureStartup(directory, fileURLToPath(import.meta.url), true);
528
+ emit("startup", startup, "Automatic startup enabled. Mindwire will recover after you sign in to this computer.");
529
+ } catch {
530
+ emit("startup_unavailable", {}, "Mindwire is running, but automatic startup couldn't be enabled. Run mindwire startup enable to check your system's startup service.");
531
+ }
532
+ }
533
+ if (info.routes.some((route) => route.kind === "websocket")) {
534
+ emit("secure_connection", {}, "Your phone connects over end-to-end encrypted SSH. The tunnel carries encrypted traffic; only phones you approve can connect.");
535
+ }
2267
536
  if (info.routes.some((route) => route.kind === "websocket" && new URL(route.url).hostname.endsWith(".trycloudflare.com"))) {
2268
- emit2("temporary_address", {}, "Internet access is ready. This quick-tunnel address changes after a restart; use a stable hostname or VPN for regular access.");
537
+ emit("temporary_address", {}, "Cloudflare internet access is ready. For a permanent address, configure a named Cloudflare tunnel or your VPN; a free temporary address can change after a tunnel restart.");
2269
538
  }
2270
539
  const invitation = await client2.computer.invite(info.routes);
2271
540
  const uri = computerPairingURI(invitation);
2272
- emit2("invitation", { invitation, uri }, "Open Mindwire on your phone \u2192 Add computer \u2192 Scan QR code.");
2273
- if (!values.json) {
2274
- if (!values["no-qr"]) {
2275
- const QR = await import('qrcode');
2276
- process.stdout.write(await QR.toString(uri, { type: "terminal", small: true, errorCorrectionLevel: "L" }));
2277
- }
2278
- process.stdout.write(`
2279
- Pairing link (expires in 5 minutes):
2280
- ${uri}
2281
-
2282
- Waiting for your phone\u2026
2283
- `);
2284
- }
2285
- let shownRequest;
2286
- while (Date.now() < Date.parse(invitation.expiresAt)) {
2287
- const pairing = await client2.computer.pairing(invitation.pairingId);
2288
- if (pairing.status === "approved") {
2289
- emit2("paired", {}, "Phone connected. You can close this terminal; Mindwire keeps running.");
2290
- return;
2291
- }
2292
- if (pairing.status === "rejected") {
2293
- emit2("rejected", {}, "Pairing declined.");
2294
- return;
2295
- }
2296
- if (pairing.request && shownRequest !== pairing.request.id) {
2297
- shownRequest = pairing.request.id;
2298
- const key = Buffer.from(pairing.request.publicKey.split(" ")[1] ?? "", "base64");
2299
- const fingerprint = "SHA256:" + createHash("sha256").update(key).digest("base64").replace(/=+$/, "");
2300
- emit2(
2301
- "approval_required",
2302
- { pairingId: invitation.pairingId, request: pairing.request, fingerprint },
2303
- `
541
+ emit("invitation", { invitation, uri });
542
+ const display = values.json ? void 0 : await showPairingInvitation(invitation, {
543
+ mode: values["no-qr"] ? "none" : values.qr ?? "auto"
544
+ });
545
+ try {
546
+ let shownRequest;
547
+ while (Date.now() < Date.parse(invitation.expiresAt)) {
548
+ const pairing = await client2.computer.pairing(invitation.pairingId);
549
+ if (pairing.status === "approved") {
550
+ display?.close();
551
+ emit("paired", {}, "Phone connected. You can close this terminal; Mindwire keeps running.");
552
+ return;
553
+ }
554
+ if (pairing.status === "rejected") {
555
+ display?.close();
556
+ emit("rejected", {}, "Pairing declined.");
557
+ return;
558
+ }
559
+ if (pairing.request && shownRequest !== pairing.request.id) {
560
+ display?.close();
561
+ shownRequest = pairing.request.id;
562
+ const key = Buffer.from(pairing.request.publicKey.split(" ")[1] ?? "", "base64");
563
+ const fingerprint = "SHA256:" + createHash("sha256").update(key).digest("base64").replace(/=+$/, "");
564
+ emit(
565
+ "approval_required",
566
+ { pairingId: invitation.pairingId, request: pairing.request, fingerprint },
567
+ `
2304
568
  ${safeName(pairing.request.name)} wants to connect.
2305
569
  Device key: ${fingerprint}`
2306
- );
2307
- if (!process.stdin.isTTY) {
2308
- emit2(
2309
- "approval_command",
2310
- { command: `mindwire approve ${invitation.pairingId} ${pairing.request.id}` },
2311
- `Review this device, then run:
2312
- mindwire approve ${invitation.pairingId} ${pairing.request.id}`
2313
570
  );
2314
- } else {
2315
- const input = createInterface({ input: process.stdin, output: process.stderr });
2316
- try {
2317
- const answer = await input.question("Allow this phone to access this workspace? [y/N] ");
2318
- await client2.computer.decide(invitation.pairingId, pairing.request.id, /^y(es)?$/i.test(answer.trim()));
2319
- } finally {
2320
- input.close();
571
+ if (!process.stdin.isTTY) {
572
+ emit(
573
+ "approval_command",
574
+ { command: `mindwire approve ${invitation.pairingId} ${pairing.request.id}` },
575
+ `Review this device, then run:
576
+ mindwire approve ${invitation.pairingId} ${pairing.request.id}`
577
+ );
578
+ } else {
579
+ const input = createInterface({ input: process.stdin, output: process.stderr });
580
+ try {
581
+ const answer = await input.question("Allow this phone to access this workspace? [y/N] ");
582
+ await client2.computer.decide(invitation.pairingId, pairing.request.id, /^y(es)?$/i.test(answer.trim()));
583
+ } finally {
584
+ input.close();
585
+ }
2321
586
  }
2322
587
  }
588
+ await setTimeout$1(750);
2323
589
  }
2324
- await setTimeout$1(750);
590
+ throw new Error("Pairing expired. Run mindwire connect to show a new QR code.");
591
+ } finally {
592
+ display?.close();
2325
593
  }
2326
- throw new Error("Pairing expired. Run mindwire connect to show a new QR code.");
2327
594
  }
2328
595
  if (command === "stop") {
2329
596
  await stopComputer(directory, values.force === true);
2330
- emit2("stopped", {}, "Mindwire stopped.");
597
+ emit("stopped", {}, "Mindwire stopped.");
2331
598
  return;
2332
599
  }
2333
600
  const client = await computerClient(directory);
2334
601
  switch (command) {
2335
602
  case "status": {
2336
- const [info, health, work, controller] = await Promise.all([
603
+ const [info, health, work, controller, startup] = await Promise.all([
2337
604
  client.computer.info(),
2338
605
  client.health(),
2339
606
  client.service.updateStatus(),
2340
- readJSON(path2.join(directory, "computer-controller.json"))
607
+ readJSON(path.join(directory, "computer-controller.json")),
608
+ startupStatus(directory)
2341
609
  ]);
2342
- emit2(
610
+ emit(
2343
611
  "status",
2344
- { ...info, daemonVersion: health.version, ...work, relayError: controller?.error },
612
+ { ...info, daemonVersion: health.version, ...work, startup, recovering: controller?.recovering, relayError: controller?.error },
2345
613
  `Mindwire ${health.version} \xB7 ${work.idle ? "idle" : "working"}
614
+ Startup: ${startup.enabled ? "on" : "off"}
2346
615
  ${info.routes.map((route) => route.kind === "ssh" ? `SSH ${route.host}:${route.port}` : route.url).join("\n")}${controller?.error ? `
2347
616
  ${controller.error}` : ""}`
2348
617
  );
@@ -2350,38 +619,38 @@ ${controller.error}` : ""}`
2350
619
  }
2351
620
  case "devices": {
2352
621
  const devices = await client.computer.devices();
2353
- emit2("devices", { devices }, devices.length ? devices.map((d) => `${safeName(d.name)} ${d.id}${d.revoked ? " (revoked)" : ""}`).join("\n") : "No paired phones yet. Run mindwire connect.");
622
+ emit("devices", { devices }, devices.length ? devices.map((d) => `${safeName(d.name)} ${d.id}${d.revoked ? " (revoked)" : ""}`).join("\n") : "No paired phones yet. Run mindwire connect.");
2354
623
  break;
2355
624
  }
2356
625
  case "revoke": {
2357
626
  if (!positionals[1]) throw new Error("Use mindwire revoke DEVICE_ID (from mindwire devices).");
2358
627
  await client.computer.revoke(positionals[1]);
2359
- emit2("revoked", {}, "Device revoked and disconnected.");
628
+ emit("revoked", {}, "Device revoked and disconnected.");
2360
629
  break;
2361
630
  }
2362
631
  case "approve":
2363
632
  case "reject": {
2364
633
  if (!positionals[1] || !positionals[2]) throw new Error(`Use mindwire ${command} PAIRING_ID REQUEST_ID.`);
2365
634
  await client.computer.decide(positionals[1], positionals[2], command === "approve");
2366
- emit2(command, {}, command === "approve" ? "Phone approved." : "Phone declined.");
635
+ emit(command, {}, command === "approve" ? "Phone approved." : "Phone declined.");
2367
636
  break;
2368
637
  }
2369
638
  case "update": {
2370
639
  const version = (values.version ?? SDK_VERSION).replace(/^v/, "");
2371
640
  const health = await client.health();
2372
641
  if (health.version.replace(/^v/, "") === version) {
2373
- emit2("updated", { version }, `Mindwire ${version} is already installed.`);
642
+ emit("updated", { version }, `Mindwire ${version} is already installed.`);
2374
643
  return;
2375
644
  }
2376
645
  const requested = await client.computer.requestUpdate(version);
2377
- emit2("update_queued", requested, "Update queued. It will wait until all work and terminals are finished.");
646
+ emit("update_queued", requested, "Update queued. It will wait until all work and terminals are finished.");
2378
647
  let last = "";
2379
648
  for (; ; ) {
2380
- const update = await readJSON(path2.join(directory, "computer-update.json"));
649
+ const update = await readJSON(path.join(directory, "computer-update.json"));
2381
650
  if (update?.id !== requested.id) throw new Error("The update request changed. Run mindwire status.");
2382
651
  if (update.status !== last) {
2383
652
  last = update.status;
2384
- emit2("update", update, `Update: ${update.status}`);
653
+ emit("update", update, `Update: ${update.status}`);
2385
654
  }
2386
655
  if (update.status === "complete") return;
2387
656
  if (update.status === "failed") throw new Error(update.error ?? "Update failed.");