@heywavie/cli 0.2.0 → 0.2.2

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
@@ -21,7 +21,8 @@ The token works once, for 30 minutes, and rides after `#`, so fetching the page
21
21
  ## What the service does
22
22
 
23
23
  `heywavie run` waits on `GET /device/asks` by the computer's key, as its own device, for `claude` asks only, and runs
24
- each turn with Claude Code here (`src/claude.mjs`), three at once: model only, Claude Code's own tools off (no shell, no
24
+ each turn with Claude Code here (`src/claude.mjs`), three at once, on Claude Opus 5.5 (named, so neither their Claude Code
25
+ default nor their settings picks it): model only, Claude Code's own tools off (no shell, no
25
26
  files, no web), none of the person's settings, hooks or plugins, an empty folder of its own, and only Wavie's tools,
26
27
  reached over MCP by the turn's own token. What Claude Code says goes back to the server as it says it. The key reaches
27
28
  its turns and `/plans/claude/host`, nothing else of their account; Claude's sign-in stays in Claude Code.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heywavie/cli",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Claude on your computer, for Wavie: your agents think with your own Claude plan",
5
5
  "homepage": "https://heywavie.com",
6
6
  "license": "UNLICENSED",
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
 
@@ -49,16 +49,51 @@ export function claudeEnv(env = process.env, home = os.homedir(), bin = null, {
49
49
  return { ...base, USER: who, LOGNAME: env.LOGNAME || who, PATH: [...own, ...UNIX_PATH].join(':') };
50
50
  }
51
51
 
52
- /** The one command a turn runs: model only, Wavie's tools at `tools` its only ones. */
53
- export function claudeArgs({ systemFile, tools, maxSteps }) {
54
- const mcp = { mcpServers: { wavie: { type: 'http', url: tools } } };
55
- return ['-p', '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', 'stream-json',
56
- '--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', JSON.stringify(mcp),
52
+ /**
53
+ * The Claude model a turn thinks with: named, so their own Claude Code default (Fable on a Max plan) or anything set in
54
+ * their settings, which never load here, doesn't pick it.
55
+ */
56
+ export const CLAUDE_MODEL = 'claude-opus-5-5';
57
+
58
+ /** Claude Code's refusal of a model the account can't use: its first message, marked or in its own words. */
59
+ const REFUSED = /issue with the selected model/i;
60
+ // Refused once here: later turns go straight to Claude Code's own default, until the service starts again.
61
+ let refusedHere = false;
62
+
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 }) {
71
+ return ['-p', ...(model ? ['--model', model] : []), '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', 'stream-json',
72
+ '--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', mcpFile,
57
73
  '--allowedTools', 'mcp__wavie', '--permission-prompts', 'none', '--no-session-persistence', '--disable-slash-commands',
58
74
  // No --safe-mode: it turns off MCP servers too, Wavie's with them. Their CLAUDE.md stays out without it.
59
75
  '--setting-sources', '', '--max-turns', String(maxSteps)];
60
76
  }
61
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
+
62
97
  const TURN_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/(tools|events)$/;
63
98
  const FILE_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/files\/([0-9a-f-]{36})$/;
64
99
  const FILE_TYPES = new Set(['application/pdf', 'image/png', 'image/jpeg', 'image/gif', 'image/webp']);
@@ -188,35 +223,62 @@ export async function runTurn({ bin, turn, fetch = globalThis.fetch, env = proce
188
223
  return -1;
189
224
  }
190
225
  const message = await messageOf(turn, fetch);
226
+ await clearLeftTurns({ olderThanMs: maxMs + 5 * 60_000 });
191
227
  const dir = await mkdtemp(path.join(os.tmpdir(), 'wavie-claude-'));
192
228
  const systemFile = path.join(dir, 'system.md');
229
+ const mcpFile = path.join(dir, 'mcp.json');
193
230
  await writeFile(systemFile, turn.system, { mode: 0o600 });
194
- child = spawn(bin, claudeArgs({ systemFile, tools: turn.tools, maxSteps: turn.maxSteps }), { cwd: dir, env: claudeEnv(env, home, bin, { user: os.userInfo().username }),
195
- stdio: ['pipe', 'pipe', 'pipe'] });
196
- children.add(child);
231
+ await writeFile(mcpFile, JSON.stringify(mcpConfig(turn.tools)), { mode: 0o600 });
197
232
  const ceiling = setTimeout(stop, maxMs);
198
- let buffer = '';
199
233
  let errText = '';
200
- child.stdout.on('data', (d) => {
201
- buffer += d;
202
- let nl;
203
- while ((nl = buffer.indexOf('\n')) >= 0) {
204
- const line = buffer.slice(0, nl).trim();
205
- buffer = buffer.slice(nl + 1);
206
- if (line) pending.push(line);
207
- }
208
- });
209
- child.stderr.on('data', (d) => { errText = (errText + d).slice(-4000); });
210
- child.stdin.on('error', () => {});
211
- child.stdin.end(message);
212
- const code = await new Promise((resolve) => {
213
- child.on('error', (err) => { errText = `${errText}\n${err?.message ?? err}`; resolve(-1); });
214
- child.on('close', (c) => resolve(c ?? -1));
215
- });
216
- children.delete(child);
234
+ // Once on CLAUDE_MODEL. A plan that can't use it (Claude Code answers its first message with model_not_found) runs
235
+ // again on Claude Code's own default: Claude Code's opening lines wait until the first message shows which, and the
236
+ // refusal never reaches the server as an answer.
237
+ const once = async (model) => {
238
+ child = spawn(bin, claudeArgs({ systemFile, mcpFile, maxSteps: turn.maxSteps, model }), { cwd: dir, env: claudeEnv(env, home, bin, { user: os.userInfo().username }),
239
+ stdio: ['pipe', 'pipe', 'pipe'] });
240
+ children.add(child);
241
+ let buffer = '';
242
+ let held = model ? [] : null;
243
+ let refused = false;
244
+ const take = (line) => {
245
+ if (refused) return;
246
+ if (!held) { pending.push(line); return; }
247
+ let item = null;
248
+ try { item = JSON.parse(line); } catch { /* not JSON: shown as it is */ }
249
+ if (item?.type === 'system') { held.push(line); return; }
250
+ const words = item?.type === 'assistant' ? (item.message?.content ?? []).map((b) => b?.text ?? '').join(' ') : '';
251
+ if (item?.type === 'assistant' && (item.error === 'model_not_found' || REFUSED.test(words))) { refused = true; held = null; return; }
252
+ pending.push(...held, line);
253
+ held = null;
254
+ };
255
+ errText = '';
256
+ child.stdout.on('data', (d) => {
257
+ buffer += d;
258
+ let nl;
259
+ while ((nl = buffer.indexOf('\n')) >= 0) {
260
+ const line = buffer.slice(0, nl).trim();
261
+ buffer = buffer.slice(nl + 1);
262
+ if (line) take(line);
263
+ }
264
+ });
265
+ child.stderr.on('data', (d) => { errText = (errText + d).slice(-4000); });
266
+ child.stdin.on('error', () => {});
267
+ child.stdin.end(message);
268
+ const exit = await new Promise((resolve) => {
269
+ child.on('error', (err) => { errText = `${errText}\n${err?.message ?? err}`; resolve(-1); });
270
+ child.on('close', (c) => resolve(c ?? -1));
271
+ });
272
+ children.delete(child);
273
+ if (buffer.trim()) take(buffer.trim());
274
+ if (held) pending.push(...held);
275
+ return { exit, refused };
276
+ };
277
+ let { exit: code, refused } = await once(refusedHere ? null : CLAUDE_MODEL);
278
+ if (refused) refusedHere = true;
279
+ if (refused && !stopping) ({ exit: code } = await once(null));
217
280
  clearInterval(timer);
218
281
  clearTimeout(ceiling);
219
- if (buffer.trim()) pending.push(buffer.trim());
220
282
  for (let i = 0; i < 4 && (pending.length || batches.length || i === 0); i++) await flush(true);
221
283
  for (let i = 0; i < 3; i++) if (await send({ end: { code, error: errText } })) break;
222
284
  await rm(dir, { recursive: true, force: true }).catch(() => {});