@heywavie/cli 0.2.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/claude.mjs +33 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heywavie/cli",
3
- "version": "0.2.1",
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
 
@@ -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 = '';