@heywavie/cli 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,6 +18,10 @@ Claude Code they use there. Published to npm as `@heywavie/cli` (its command is
18
18
 
19
19
  The token works once, for 30 minutes, and rides after `#`, so fetching the page never sends it.
20
20
 
21
+ The Mac app (`desktop/`, 1.0.7 on) skips the sentence: it ships this package and runs `link` itself, its service run
22
+ by the app as Node. A change here reaches those Macs with the app's next version: started after an update, the app
23
+ puts its own copy in (`refresh`), when the CLI's version changed.
24
+
21
25
  ## What the service does
22
26
 
23
27
  `heywavie run` waits on `GET /device/asks` by the computer's key, as its own device, for `claude` asks only, and runs
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@heywavie/cli",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Claude on your computer, for Wavie: your agents think with your own Claude plan",
5
5
  "homepage": "https://heywavie.com",
6
- "license": "UNLICENSED",
6
+ "license": "AGPL-3.0-only",
7
7
  "type": "module",
8
8
  "bin": {
9
9
  "heywavie": "bin/heywavie.mjs"
package/src/claude.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { existsSync } from 'node:fs';
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises';
3
+ import { mkdtemp, readdir, rm, stat, writeFile } from 'node:fs/promises';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
6
 
@@ -60,16 +60,40 @@ const REFUSED = /issue with the selected model/i;
60
60
  // Refused once here: later turns go straight to Claude Code's own default, until the service starts again.
61
61
  let refusedHere = false;
62
62
 
63
- /** The one command a turn runs: model only, Wavie's tools at `tools` its only ones. */
64
- export function claudeArgs({ systemFile, tools, maxSteps, model = CLAUDE_MODEL }) {
65
- const mcp = { mcpServers: { wavie: { type: 'http', url: tools } } };
63
+ /** Where Claude Code finds Wavie's tools: the turn's own address, which works as them until the turn ends. */
64
+ export const mcpConfig = (tools) => ({ mcpServers: { wavie: { type: 'http', url: tools } } });
65
+
66
+ /**
67
+ * The one command a turn runs: model only, Wavie's tools its only ones. The instructions and the tools' address are in
68
+ * files only they can read (`systemFile`, `mcpFile`), never on the command line, where anyone on the computer sees it (ps).
69
+ */
70
+ export function claudeArgs({ systemFile, mcpFile, maxSteps, model = CLAUDE_MODEL }) {
66
71
  return ['-p', ...(model ? ['--model', model] : []), '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', 'stream-json',
67
- '--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', JSON.stringify(mcp),
72
+ '--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', mcpFile,
68
73
  '--allowedTools', 'mcp__wavie', '--permission-prompts', 'none', '--no-session-persistence', '--disable-slash-commands',
69
74
  // No --safe-mode: it turns off MCP servers too, Wavie's with them. Their CLAUDE.md stays out without it.
70
75
  '--setting-sources', '', '--max-turns', String(maxSteps)];
71
76
  }
72
77
 
78
+ // A turn's own folder, with its instructions in it.
79
+ const TURN_DIR = /^wavie-claude-/;
80
+
81
+ /**
82
+ * Folders turns left behind (the service was stopped or the computer slept mid-turn), older than any turn runs: their
83
+ * instructions don't stay on the computer. A turn still running is younger than `olderThanMs`.
84
+ */
85
+ export async function clearLeftTurns({ tmp = os.tmpdir(), olderThanMs, now = Date.now() } = {}) {
86
+ const names = await readdir(tmp).catch(() => []);
87
+ let cleared = 0;
88
+ for (const name of names.filter((n) => TURN_DIR.test(n))) {
89
+ const dir = path.join(tmp, name);
90
+ const at = await stat(dir).then((s) => s.mtimeMs).catch(() => null);
91
+ if (at === null || now - at < olderThanMs) continue;
92
+ await rm(dir, { recursive: true, force: true }).then(() => { cleared += 1; }).catch(() => {});
93
+ }
94
+ return cleared;
95
+ }
96
+
73
97
  const TURN_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/(tools|events)$/;
74
98
  const FILE_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/files\/([0-9a-f-]{36})$/;
75
99
  const FILE_TYPES = new Set(['application/pdf', 'image/png', 'image/jpeg', 'image/gif', 'image/webp']);
@@ -199,16 +223,19 @@ export async function runTurn({ bin, turn, fetch = globalThis.fetch, env = proce
199
223
  return -1;
200
224
  }
201
225
  const message = await messageOf(turn, fetch);
226
+ await clearLeftTurns({ olderThanMs: maxMs + 5 * 60_000 });
202
227
  const dir = await mkdtemp(path.join(os.tmpdir(), 'wavie-claude-'));
203
228
  const systemFile = path.join(dir, 'system.md');
229
+ const mcpFile = path.join(dir, 'mcp.json');
204
230
  await writeFile(systemFile, turn.system, { mode: 0o600 });
231
+ await writeFile(mcpFile, JSON.stringify(mcpConfig(turn.tools)), { mode: 0o600 });
205
232
  const ceiling = setTimeout(stop, maxMs);
206
233
  let errText = '';
207
234
  // Once on CLAUDE_MODEL. A plan that can't use it (Claude Code answers its first message with model_not_found) runs
208
235
  // again on Claude Code's own default: Claude Code's opening lines wait until the first message shows which, and the
209
236
  // refusal never reaches the server as an answer.
210
237
  const once = async (model) => {
211
- child = spawn(bin, claudeArgs({ systemFile, tools: turn.tools, maxSteps: turn.maxSteps, model }), { cwd: dir, env: claudeEnv(env, home, bin, { user: os.userInfo().username }),
238
+ child = spawn(bin, claudeArgs({ systemFile, mcpFile, maxSteps: turn.maxSteps, model }), { cwd: dir, env: claudeEnv(env, home, bin, { user: os.userInfo().username }),
212
239
  stdio: ['pipe', 'pipe', 'pipe'] });
213
240
  children.add(child);
214
241
  let buffer = '';
package/src/host.mjs CHANGED
@@ -11,9 +11,17 @@ const WAIT_S = 20;
11
11
 
12
12
  const PLATFORM = { darwin: 'darwin', linux: 'linux', win32: 'win32' }[process.platform] ?? 'linux';
13
13
  const sleep = (ms, signal) => new Promise((resolve) => {
14
+ if (signal?.aborted) return resolve();
14
15
  const t = setTimeout(resolve, ms);
15
16
  signal?.addEventListener('abort', () => { clearTimeout(t); resolve(); }, { once: true });
16
17
  });
18
+ // A body that ends when the service stops: once its request is aborted, fetch can leave res.json() waiting for good.
19
+ const bodyOf = (res, signal) => new Promise((resolve) => {
20
+ if (signal?.aborted) return resolve({});
21
+ const stop = () => resolve({});
22
+ signal?.addEventListener('abort', stop, { once: true });
23
+ res.json().then(resolve, () => resolve({})).finally(() => signal?.removeEventListener('abort', stop));
24
+ });
17
25
 
18
26
  /** Turns running here at once, at most `max`: `place()` resolves when there is room, `free()` hands it on. */
19
27
  export function places(max = TURNS_MAX) {
@@ -90,7 +98,8 @@ export async function host({ link, fetch = globalThis.fetch, signal = null, log
90
98
  continue;
91
99
  }
92
100
  backoff = 1000;
93
- const { asks = [] } = await res.json().catch(() => ({}));
101
+ const { asks = [] } = await bodyOf(res, signal);
102
+ if (signal?.aborted) break;
94
103
  for (const ask of asks) if (ask?.ability === 'claude') await start(ask);
95
104
  }
96
105
  stopAll();