@snoai/mem-claude 1.2.13 → 1.2.14
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/dist/worker.d.ts +19 -0
- package/dist/worker.js +10 -5
- package/package.json +1 -1
package/dist/worker.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export interface WorkerDependencies {
|
|
|
4
4
|
runChild(prompt: string, cwd: string, timeoutMs?: number): Promise<{
|
|
5
5
|
kind: "ok";
|
|
6
6
|
text: string;
|
|
7
|
+
usage?: {
|
|
8
|
+
inputTokens: number;
|
|
9
|
+
outputTokens: number;
|
|
10
|
+
};
|
|
7
11
|
} | {
|
|
8
12
|
kind: "cancelled";
|
|
9
13
|
reason: string;
|
|
@@ -18,5 +22,20 @@ export interface WorkerDependencies {
|
|
|
18
22
|
handoff?(): Promise<void>;
|
|
19
23
|
}
|
|
20
24
|
export declare function startWorkerDetached(): void;
|
|
25
|
+
export declare function runClaudeChild(prompt: string, cwd: string, timeoutMs?: number): Promise<{
|
|
26
|
+
kind: "ok";
|
|
27
|
+
text: string;
|
|
28
|
+
usage?: {
|
|
29
|
+
inputTokens: number;
|
|
30
|
+
outputTokens: number;
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
33
|
+
kind: "cancelled";
|
|
34
|
+
reason: string;
|
|
35
|
+
} | {
|
|
36
|
+
kind: "error";
|
|
37
|
+
category: "transport";
|
|
38
|
+
message: string;
|
|
39
|
+
}>;
|
|
21
40
|
export declare function hasActionableSpool(): Promise<boolean>;
|
|
22
41
|
export declare function runWorker(dependencies?: WorkerDependencies): Promise<"drained" | "locked" | "failed">;
|
package/dist/worker.js
CHANGED
|
@@ -55,7 +55,7 @@ export function startWorkerDetached() {
|
|
|
55
55
|
closeSync(log);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
async function runClaudeChild(prompt, cwd, timeoutMs = CODING_SKIN_CHILD_DEADLINE_MS) {
|
|
58
|
+
export async function runClaudeChild(prompt, cwd, timeoutMs = CODING_SKIN_CHILD_DEADLINE_MS) {
|
|
59
59
|
if (timeoutMs <= 0)
|
|
60
60
|
return { kind: "cancelled", reason: "deadline" };
|
|
61
61
|
const env = { ...process.env };
|
|
@@ -84,7 +84,7 @@ async function runClaudeChild(prompt, cwd, timeoutMs = CODING_SKIN_CHILD_DEADLIN
|
|
|
84
84
|
const parsed = z.object({
|
|
85
85
|
result: z.string().optional(),
|
|
86
86
|
is_error: z.boolean(),
|
|
87
|
-
usage: z.object({ cache_creation_input_tokens: z.number().optional(), input_tokens: z.number().optional(), output_tokens: z.number().optional() }).optional(),
|
|
87
|
+
usage: z.object({ cache_creation_input_tokens: z.number().optional(), cache_read_input_tokens: z.number().optional(), input_tokens: z.number().optional(), output_tokens: z.number().optional() }).optional(),
|
|
88
88
|
}).safeParse(output);
|
|
89
89
|
if (!parsed.success)
|
|
90
90
|
return { kind: "error", category: "transport", message: "invalid-result" };
|
|
@@ -94,7 +94,11 @@ async function runClaudeChild(prompt, cwd, timeoutMs = CODING_SKIN_CHILD_DEADLIN
|
|
|
94
94
|
const text = parsed.data.result?.trim();
|
|
95
95
|
if (!text)
|
|
96
96
|
return { kind: "error", category: "transport", message: "empty-output" };
|
|
97
|
-
|
|
97
|
+
const reported = parsed.data.usage;
|
|
98
|
+
const usage = reported && reported.input_tokens !== undefined && reported.output_tokens !== undefined
|
|
99
|
+
? { inputTokens: reported.input_tokens + (reported.cache_creation_input_tokens ?? 0) + (reported.cache_read_input_tokens ?? 0), outputTokens: reported.output_tokens }
|
|
100
|
+
: undefined;
|
|
101
|
+
return usage ? { kind: "ok", text, usage } : { kind: "ok", text };
|
|
98
102
|
}
|
|
99
103
|
function promptFromMessages(value) {
|
|
100
104
|
const parsed = z.object({ model: z.string(), messages: z.array(z.object({ role: z.enum(["system", "user"]), content: z.string() })) }).safeParse(value);
|
|
@@ -150,12 +154,13 @@ async function callbackServer(credential, activeCwd, isImport, onCall, runChild)
|
|
|
150
154
|
lastImportChildAt = Date.now();
|
|
151
155
|
console.log(JSON.stringify({ event: "import-child-start", at: lastImportChildAt }));
|
|
152
156
|
}
|
|
157
|
+
const startedAt = Date.now();
|
|
153
158
|
const result = await runChild(requestBody.prompt, cwd, Number.isFinite(deadline) ? deadline - Date.now() : undefined);
|
|
154
159
|
const status = result.kind === "ok" ? 200 : result.kind === "cancelled" ? 504 : 503;
|
|
155
|
-
console.log(JSON.stringify({ event: "model-callback", model: requestBody.model, status }));
|
|
160
|
+
console.log(JSON.stringify({ event: "model-callback", model: requestBody.model, status, outcome: result.kind, duration_ms: Date.now() - startedAt, ...(result.kind === "ok" ? {} : { reason: result.kind === "cancelled" ? result.reason : `${result.category}: ${result.message}` }) }));
|
|
156
161
|
response.setHeader("content-type", "application/json");
|
|
157
162
|
if (result.kind === "ok") {
|
|
158
|
-
response.writeHead(200).end(JSON.stringify({ choices: [{ message: { role: "assistant", content: result.text } }] }));
|
|
163
|
+
response.writeHead(200).end(JSON.stringify({ choices: [{ message: { role: "assistant", content: result.text } }], ...(result.usage ? { usage: { prompt_tokens: result.usage.inputTokens, completion_tokens: result.usage.outputTokens, total_tokens: result.usage.inputTokens + result.usage.outputTokens } } : {}) }));
|
|
159
164
|
return;
|
|
160
165
|
}
|
|
161
166
|
response.writeHead(result.kind === "cancelled" ? 504 : 503).end(JSON.stringify({ error: result }));
|