@heywavie/cli 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/package.json +1 -1
- package/src/claude.mjs +89 -28
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
|
|
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
package/src/claude.mjs
CHANGED
|
@@ -8,7 +8,8 @@ import path from 'node:path';
|
|
|
8
8
|
* Wavie's turns on the person's own Claude plan, run on their computer by the Claude Code they signed in to there. Model
|
|
9
9
|
* only: Claude Code's own tools are off (no shell, no files, no web), none of their settings, hooks or plugins load, and
|
|
10
10
|
* it runs in an empty folder of its own. Wavie's tools reach it from heywavie over MCP, by the turn's own token. The
|
|
11
|
-
* server sends words and that token's
|
|
11
|
+
* server sends words and that token's addresses (its tools, its events, the PDFs and pictures attached), never a command:
|
|
12
|
+
* the command is this file's.
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
const WINDOWS = process.platform === 'win32';
|
|
@@ -48,10 +49,21 @@ export function claudeEnv(env = process.env, home = os.homedir(), bin = null, {
|
|
|
48
49
|
return { ...base, USER: who, LOGNAME: env.LOGNAME || who, PATH: [...own, ...UNIX_PATH].join(':') };
|
|
49
50
|
}
|
|
50
51
|
|
|
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
|
+
|
|
51
63
|
/** The one command a turn runs: model only, Wavie's tools at `tools` its only ones. */
|
|
52
|
-
export function claudeArgs({ systemFile, tools, maxSteps }) {
|
|
64
|
+
export function claudeArgs({ systemFile, tools, maxSteps, model = CLAUDE_MODEL }) {
|
|
53
65
|
const mcp = { mcpServers: { wavie: { type: 'http', url: tools } } };
|
|
54
|
-
return ['-p', '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', '
|
|
66
|
+
return ['-p', ...(model ? ['--model', model] : []), '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', 'stream-json',
|
|
55
67
|
'--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', JSON.stringify(mcp),
|
|
56
68
|
'--allowedTools', 'mcp__wavie', '--permission-prompts', 'none', '--no-session-persistence', '--disable-slash-commands',
|
|
57
69
|
// No --safe-mode: it turns off MCP servers too, Wavie's with them. Their CLAUDE.md stays out without it.
|
|
@@ -59,15 +71,22 @@ export function claudeArgs({ systemFile, tools, maxSteps }) {
|
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
const TURN_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/(tools|events)$/;
|
|
74
|
+
const FILE_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/files\/([0-9a-f-]{36})$/;
|
|
75
|
+
const FILE_TYPES = new Set(['application/pdf', 'image/png', 'image/jpeg', 'image/gif', 'image/webp']);
|
|
62
76
|
|
|
63
|
-
/** A turn as the server asked for it, checked: its
|
|
77
|
+
/** A turn as the server asked for it, checked: its addresses on Wavie's own API (`api`), its words, its files, its steps. */
|
|
64
78
|
export function turnOf(input, api) {
|
|
65
79
|
const tools = TURN_PATH.exec(String(input?.tools ?? ''));
|
|
66
80
|
const events = TURN_PATH.exec(String(input?.events ?? ''));
|
|
67
81
|
if (!tools || !events || tools[2] !== 'tools' || events[2] !== 'events' || tools[1] !== events[1]) throw new Error('Not a turn of Wavie\'s.');
|
|
68
82
|
if (typeof input.system !== 'string' || typeof input.prompt !== 'string' || !input.prompt.trim()) throw new Error('A turn with nothing to answer.');
|
|
69
83
|
const steps = Number.isInteger(input.maxSteps) && input.maxSteps >= 1 && input.maxSteps <= 60 ? input.maxSteps : 24;
|
|
70
|
-
|
|
84
|
+
const files = (Array.isArray(input.files) ? input.files : []).slice(0, 10).map((f) => {
|
|
85
|
+
const at = FILE_PATH.exec(String(f?.url ?? ''));
|
|
86
|
+
if (!at || at[1] !== tools[1] || !FILE_TYPES.has(f.mediaType)) throw new Error('Not a turn of Wavie\'s.');
|
|
87
|
+
return { url: `${api}${at[0]}`, name: String(f.name ?? 'file').slice(0, 200), mediaType: f.mediaType };
|
|
88
|
+
});
|
|
89
|
+
return { tools: `${api}${tools[0]}`, events: `${api}${events[0]}`, system: input.system, prompt: input.prompt, files, maxSteps: steps };
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
/** Who Claude Code on this computer is signed in as: `{ found, loggedIn, email, kind }`. */
|
|
@@ -93,6 +112,23 @@ export function claudeStatus(bin, { env = process.env, home = os.homedir() } = {
|
|
|
93
112
|
});
|
|
94
113
|
}
|
|
95
114
|
|
|
115
|
+
/** The chat as Claude Code's one message: its words, then each attached PDF or picture itself, for Claude to read. */
|
|
116
|
+
async function messageOf(turn, fetch) {
|
|
117
|
+
const content = [{ type: 'text', text: turn.prompt }];
|
|
118
|
+
for (const f of turn.files ?? []) {
|
|
119
|
+
try {
|
|
120
|
+
const res = await fetch(f.url, { signal: AbortSignal.timeout(60_000) });
|
|
121
|
+
if (!res.ok) throw new Error(String(res.status));
|
|
122
|
+
const data = Buffer.from(await res.arrayBuffer()).toString('base64');
|
|
123
|
+
content.push({ type: 'text', text: `Attached: ${f.name}` },
|
|
124
|
+
{ type: f.mediaType === 'application/pdf' ? 'document' : 'image', source: { type: 'base64', media_type: f.mediaType, data } });
|
|
125
|
+
} catch {
|
|
126
|
+
content.push({ type: 'text', text: `[${f.name} could not be fetched from Wavie: tell them if it matters.]` });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return `${JSON.stringify({ type: 'user', message: { role: 'user', content } })}\n`;
|
|
130
|
+
}
|
|
131
|
+
|
|
96
132
|
// Claude Code running a turn now, to stop when the service stops.
|
|
97
133
|
const children = new Set();
|
|
98
134
|
|
|
@@ -104,7 +140,7 @@ export function stopAll() {
|
|
|
104
140
|
const BATCH_LINES = 500;
|
|
105
141
|
|
|
106
142
|
/**
|
|
107
|
-
* Runs one turn: Claude Code reads the chat on its input and streams stream-json, which goes to the turn's `events`
|
|
143
|
+
* Runs one turn: Claude Code reads the chat (and its files) on its input and streams stream-json, which goes to the turn's `events`
|
|
108
144
|
* address in order, in numbered batches (one sent again is read once), with an empty one now and then so the server knows
|
|
109
145
|
* the computer is still there. The answer to each says whether to stop: a card or a started task ends the turn on the
|
|
110
146
|
* server's side. `ready` holds the turn while this computer is busy with others (it still says it's there). Resolves once Claude Code
|
|
@@ -162,35 +198,60 @@ export async function runTurn({ bin, turn, fetch = globalThis.fetch, env = proce
|
|
|
162
198
|
await send({ end: { code: -1, error: 'stopped before it started' } });
|
|
163
199
|
return -1;
|
|
164
200
|
}
|
|
201
|
+
const message = await messageOf(turn, fetch);
|
|
165
202
|
const dir = await mkdtemp(path.join(os.tmpdir(), 'wavie-claude-'));
|
|
166
203
|
const systemFile = path.join(dir, 'system.md');
|
|
167
204
|
await writeFile(systemFile, turn.system, { mode: 0o600 });
|
|
168
|
-
child = spawn(bin, claudeArgs({ systemFile, tools: turn.tools, maxSteps: turn.maxSteps }), { cwd: dir, env: claudeEnv(env, home, bin, { user: os.userInfo().username }),
|
|
169
|
-
stdio: ['pipe', 'pipe', 'pipe'] });
|
|
170
|
-
children.add(child);
|
|
171
205
|
const ceiling = setTimeout(stop, maxMs);
|
|
172
|
-
let buffer = '';
|
|
173
206
|
let errText = '';
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
207
|
+
// Once on CLAUDE_MODEL. A plan that can't use it (Claude Code answers its first message with model_not_found) runs
|
|
208
|
+
// again on Claude Code's own default: Claude Code's opening lines wait until the first message shows which, and the
|
|
209
|
+
// refusal never reaches the server as an answer.
|
|
210
|
+
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 }),
|
|
212
|
+
stdio: ['pipe', 'pipe', 'pipe'] });
|
|
213
|
+
children.add(child);
|
|
214
|
+
let buffer = '';
|
|
215
|
+
let held = model ? [] : null;
|
|
216
|
+
let refused = false;
|
|
217
|
+
const take = (line) => {
|
|
218
|
+
if (refused) return;
|
|
219
|
+
if (!held) { pending.push(line); return; }
|
|
220
|
+
let item = null;
|
|
221
|
+
try { item = JSON.parse(line); } catch { /* not JSON: shown as it is */ }
|
|
222
|
+
if (item?.type === 'system') { held.push(line); return; }
|
|
223
|
+
const words = item?.type === 'assistant' ? (item.message?.content ?? []).map((b) => b?.text ?? '').join(' ') : '';
|
|
224
|
+
if (item?.type === 'assistant' && (item.error === 'model_not_found' || REFUSED.test(words))) { refused = true; held = null; return; }
|
|
225
|
+
pending.push(...held, line);
|
|
226
|
+
held = null;
|
|
227
|
+
};
|
|
228
|
+
errText = '';
|
|
229
|
+
child.stdout.on('data', (d) => {
|
|
230
|
+
buffer += d;
|
|
231
|
+
let nl;
|
|
232
|
+
while ((nl = buffer.indexOf('\n')) >= 0) {
|
|
233
|
+
const line = buffer.slice(0, nl).trim();
|
|
234
|
+
buffer = buffer.slice(nl + 1);
|
|
235
|
+
if (line) take(line);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
child.stderr.on('data', (d) => { errText = (errText + d).slice(-4000); });
|
|
239
|
+
child.stdin.on('error', () => {});
|
|
240
|
+
child.stdin.end(message);
|
|
241
|
+
const exit = await new Promise((resolve) => {
|
|
242
|
+
child.on('error', (err) => { errText = `${errText}\n${err?.message ?? err}`; resolve(-1); });
|
|
243
|
+
child.on('close', (c) => resolve(c ?? -1));
|
|
244
|
+
});
|
|
245
|
+
children.delete(child);
|
|
246
|
+
if (buffer.trim()) take(buffer.trim());
|
|
247
|
+
if (held) pending.push(...held);
|
|
248
|
+
return { exit, refused };
|
|
249
|
+
};
|
|
250
|
+
let { exit: code, refused } = await once(refusedHere ? null : CLAUDE_MODEL);
|
|
251
|
+
if (refused) refusedHere = true;
|
|
252
|
+
if (refused && !stopping) ({ exit: code } = await once(null));
|
|
191
253
|
clearInterval(timer);
|
|
192
254
|
clearTimeout(ceiling);
|
|
193
|
-
if (buffer.trim()) pending.push(buffer.trim());
|
|
194
255
|
for (let i = 0; i < 4 && (pending.length || batches.length || i === 0); i++) await flush(true);
|
|
195
256
|
for (let i = 0; i < 3; i++) if (await send({ end: { code, error: errText } })) break;
|
|
196
257
|
await rm(dir, { recursive: true, force: true }).catch(() => {});
|