@heywavie/cli 0.1.0 → 0.2.0
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/package.json +1 -1
- package/src/claude.mjs +32 -6
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';
|
|
@@ -51,7 +52,7 @@ export function claudeEnv(env = process.env, home = os.homedir(), bin = null, {
|
|
|
51
52
|
/** The one command a turn runs: model only, Wavie's tools at `tools` its only ones. */
|
|
52
53
|
export function claudeArgs({ systemFile, tools, maxSteps }) {
|
|
53
54
|
const mcp = { mcpServers: { wavie: { type: 'http', url: tools } } };
|
|
54
|
-
return ['-p', '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', '
|
|
55
|
+
return ['-p', '--output-format', 'stream-json', '--verbose', '--include-partial-messages', '--input-format', 'stream-json',
|
|
55
56
|
'--system-prompt-file', systemFile, '--tools', '', '--strict-mcp-config', '--mcp-config', JSON.stringify(mcp),
|
|
56
57
|
'--allowedTools', 'mcp__wavie', '--permission-prompts', 'none', '--no-session-persistence', '--disable-slash-commands',
|
|
57
58
|
// No --safe-mode: it turns off MCP servers too, Wavie's with them. Their CLAUDE.md stays out without it.
|
|
@@ -59,15 +60,22 @@ export function claudeArgs({ systemFile, tools, maxSteps }) {
|
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
const TURN_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/(tools|events)$/;
|
|
63
|
+
const FILE_PATH = /^\/plan-turns\/([A-Za-z0-9_-]{32})\/files\/([0-9a-f-]{36})$/;
|
|
64
|
+
const FILE_TYPES = new Set(['application/pdf', 'image/png', 'image/jpeg', 'image/gif', 'image/webp']);
|
|
62
65
|
|
|
63
|
-
/** A turn as the server asked for it, checked: its
|
|
66
|
+
/** A turn as the server asked for it, checked: its addresses on Wavie's own API (`api`), its words, its files, its steps. */
|
|
64
67
|
export function turnOf(input, api) {
|
|
65
68
|
const tools = TURN_PATH.exec(String(input?.tools ?? ''));
|
|
66
69
|
const events = TURN_PATH.exec(String(input?.events ?? ''));
|
|
67
70
|
if (!tools || !events || tools[2] !== 'tools' || events[2] !== 'events' || tools[1] !== events[1]) throw new Error('Not a turn of Wavie\'s.');
|
|
68
71
|
if (typeof input.system !== 'string' || typeof input.prompt !== 'string' || !input.prompt.trim()) throw new Error('A turn with nothing to answer.');
|
|
69
72
|
const steps = Number.isInteger(input.maxSteps) && input.maxSteps >= 1 && input.maxSteps <= 60 ? input.maxSteps : 24;
|
|
70
|
-
|
|
73
|
+
const files = (Array.isArray(input.files) ? input.files : []).slice(0, 10).map((f) => {
|
|
74
|
+
const at = FILE_PATH.exec(String(f?.url ?? ''));
|
|
75
|
+
if (!at || at[1] !== tools[1] || !FILE_TYPES.has(f.mediaType)) throw new Error('Not a turn of Wavie\'s.');
|
|
76
|
+
return { url: `${api}${at[0]}`, name: String(f.name ?? 'file').slice(0, 200), mediaType: f.mediaType };
|
|
77
|
+
});
|
|
78
|
+
return { tools: `${api}${tools[0]}`, events: `${api}${events[0]}`, system: input.system, prompt: input.prompt, files, maxSteps: steps };
|
|
71
79
|
}
|
|
72
80
|
|
|
73
81
|
/** Who Claude Code on this computer is signed in as: `{ found, loggedIn, email, kind }`. */
|
|
@@ -93,6 +101,23 @@ export function claudeStatus(bin, { env = process.env, home = os.homedir() } = {
|
|
|
93
101
|
});
|
|
94
102
|
}
|
|
95
103
|
|
|
104
|
+
/** The chat as Claude Code's one message: its words, then each attached PDF or picture itself, for Claude to read. */
|
|
105
|
+
async function messageOf(turn, fetch) {
|
|
106
|
+
const content = [{ type: 'text', text: turn.prompt }];
|
|
107
|
+
for (const f of turn.files ?? []) {
|
|
108
|
+
try {
|
|
109
|
+
const res = await fetch(f.url, { signal: AbortSignal.timeout(60_000) });
|
|
110
|
+
if (!res.ok) throw new Error(String(res.status));
|
|
111
|
+
const data = Buffer.from(await res.arrayBuffer()).toString('base64');
|
|
112
|
+
content.push({ type: 'text', text: `Attached: ${f.name}` },
|
|
113
|
+
{ type: f.mediaType === 'application/pdf' ? 'document' : 'image', source: { type: 'base64', media_type: f.mediaType, data } });
|
|
114
|
+
} catch {
|
|
115
|
+
content.push({ type: 'text', text: `[${f.name} could not be fetched from Wavie: tell them if it matters.]` });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return `${JSON.stringify({ type: 'user', message: { role: 'user', content } })}\n`;
|
|
119
|
+
}
|
|
120
|
+
|
|
96
121
|
// Claude Code running a turn now, to stop when the service stops.
|
|
97
122
|
const children = new Set();
|
|
98
123
|
|
|
@@ -104,7 +129,7 @@ export function stopAll() {
|
|
|
104
129
|
const BATCH_LINES = 500;
|
|
105
130
|
|
|
106
131
|
/**
|
|
107
|
-
* Runs one turn: Claude Code reads the chat on its input and streams stream-json, which goes to the turn's `events`
|
|
132
|
+
* 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
133
|
* address in order, in numbered batches (one sent again is read once), with an empty one now and then so the server knows
|
|
109
134
|
* 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
135
|
* 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,6 +187,7 @@ export async function runTurn({ bin, turn, fetch = globalThis.fetch, env = proce
|
|
|
162
187
|
await send({ end: { code: -1, error: 'stopped before it started' } });
|
|
163
188
|
return -1;
|
|
164
189
|
}
|
|
190
|
+
const message = await messageOf(turn, fetch);
|
|
165
191
|
const dir = await mkdtemp(path.join(os.tmpdir(), 'wavie-claude-'));
|
|
166
192
|
const systemFile = path.join(dir, 'system.md');
|
|
167
193
|
await writeFile(systemFile, turn.system, { mode: 0o600 });
|
|
@@ -182,7 +208,7 @@ export async function runTurn({ bin, turn, fetch = globalThis.fetch, env = proce
|
|
|
182
208
|
});
|
|
183
209
|
child.stderr.on('data', (d) => { errText = (errText + d).slice(-4000); });
|
|
184
210
|
child.stdin.on('error', () => {});
|
|
185
|
-
child.stdin.end(
|
|
211
|
+
child.stdin.end(message);
|
|
186
212
|
const code = await new Promise((resolve) => {
|
|
187
213
|
child.on('error', (err) => { errText = `${errText}\n${err?.message ?? err}`; resolve(-1); });
|
|
188
214
|
child.on('close', (c) => resolve(c ?? -1));
|