@steipete/summarize 0.19.0 → 0.20.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/CHANGELOG.md +25 -0
- package/README.md +5 -3
- package/completions/summarize.fish +1 -1
- package/dist/esm/application/asset-execution.js +122 -0
- package/dist/esm/application/asset-execution.js.map +1 -0
- package/dist/esm/application/execute-summarize.js +37 -318
- package/dist/esm/application/execute-summarize.js.map +1 -1
- package/dist/esm/application/execution-input.js +81 -0
- package/dist/esm/application/execution-input.js.map +1 -0
- package/dist/esm/application/url-routing.js +117 -0
- package/dist/esm/application/url-routing.js.map +1 -0
- package/dist/esm/config/parse-helpers.js +22 -0
- package/dist/esm/config/parse-helpers.js.map +1 -1
- package/dist/esm/config/sections.js +46 -126
- package/dist/esm/config/sections.js.map +1 -1
- package/dist/esm/content/asset.js +11 -1
- package/dist/esm/content/asset.js.map +1 -1
- package/dist/esm/daemon/agent-model.js +1 -1
- package/dist/esm/daemon/agent-model.js.map +1 -1
- package/dist/esm/daemon/agent.js +64 -32
- package/dist/esm/daemon/agent.js.map +1 -1
- package/dist/esm/daemon/cli-health.js +61 -0
- package/dist/esm/daemon/cli-health.js.map +1 -0
- package/dist/esm/daemon/cli-service.js +135 -0
- package/dist/esm/daemon/cli-service.js.map +1 -0
- package/dist/esm/daemon/cli.js +5 -211
- package/dist/esm/daemon/cli.js.map +1 -1
- package/dist/esm/daemon/config.js +1 -1
- package/dist/esm/daemon/config.js.map +1 -1
- package/dist/esm/daemon/models.js +1 -1
- package/dist/esm/daemon/models.js.map +1 -1
- package/dist/esm/daemon/schtasks.js +7 -2
- package/dist/esm/daemon/schtasks.js.map +1 -1
- package/dist/esm/engine/model-executor.js +76 -17
- package/dist/esm/engine/model-executor.js.map +1 -1
- package/dist/esm/llm/cli-provider-output/codex.js +143 -0
- package/dist/esm/llm/cli-provider-output/codex.js.map +1 -0
- package/dist/esm/llm/cli-provider-output/opencode.js +102 -0
- package/dist/esm/llm/cli-provider-output/opencode.js.map +1 -0
- package/dist/esm/llm/cli-provider-output/pi.js +94 -0
- package/dist/esm/llm/cli-provider-output/pi.js.map +1 -0
- package/dist/esm/llm/cli-provider-output/shared.js +11 -0
- package/dist/esm/llm/cli-provider-output/shared.js.map +1 -0
- package/dist/esm/llm/cli-provider-output.js +41 -440
- package/dist/esm/llm/cli-provider-output.js.map +1 -1
- package/dist/esm/llm/cli-runners/codex.js +97 -0
- package/dist/esm/llm/cli-runners/codex.js.map +1 -0
- package/dist/esm/llm/cli-runners/json.js +43 -0
- package/dist/esm/llm/cli-runners/json.js.map +1 -0
- package/dist/esm/llm/cli-runners/openclaw.js +45 -0
- package/dist/esm/llm/cli-runners/openclaw.js.map +1 -0
- package/dist/esm/llm/cli-runners/opencode.js +31 -0
- package/dist/esm/llm/cli-runners/opencode.js.map +1 -0
- package/dist/esm/llm/cli-runners/pi.js +43 -0
- package/dist/esm/llm/cli-runners/pi.js.map +1 -0
- package/dist/esm/llm/cli-runners/plain.js +63 -0
- package/dist/esm/llm/cli-runners/plain.js.map +1 -0
- package/dist/esm/llm/cli-runners/types.js +2 -0
- package/dist/esm/llm/cli-runners/types.js.map +1 -0
- package/dist/esm/llm/cli.js +34 -366
- package/dist/esm/llm/cli.js.map +1 -1
- package/dist/esm/llm/generate-text-shared.js +127 -9
- package/dist/esm/llm/generate-text-shared.js.map +1 -1
- package/dist/esm/llm/generate-text-stream.js +6 -4
- package/dist/esm/llm/generate-text-stream.js.map +1 -1
- package/dist/esm/llm/generate-text.js +83 -42
- package/dist/esm/llm/generate-text.js.map +1 -1
- package/dist/esm/llm/github-models.js +23 -0
- package/dist/esm/llm/github-models.js.map +1 -1
- package/dist/esm/llm/providers/anthropic.js +32 -14
- package/dist/esm/llm/providers/anthropic.js.map +1 -1
- package/dist/esm/llm/providers/google.js +52 -8
- package/dist/esm/llm/providers/google.js.map +1 -1
- package/dist/esm/llm/providers/models.js +23 -17
- package/dist/esm/llm/providers/models.js.map +1 -1
- package/dist/esm/llm/providers/openai/chat-completions.js +2 -8
- package/dist/esm/llm/providers/openai/chat-completions.js.map +1 -1
- package/dist/esm/llm/providers/openai/github-models.js +1 -13
- package/dist/esm/llm/providers/openai/github-models.js.map +1 -1
- package/dist/esm/llm/providers/openai/responses.js +2 -8
- package/dist/esm/llm/providers/openai/responses.js.map +1 -1
- package/dist/esm/llm/providers/openai/sse.js +28 -44
- package/dist/esm/llm/providers/openai/sse.js.map +1 -1
- package/dist/esm/llm/providers/openai.js +3 -1
- package/dist/esm/llm/providers/openai.js.map +1 -1
- package/dist/esm/llm/providers/shared.js +10 -1
- package/dist/esm/llm/providers/shared.js.map +1 -1
- package/dist/esm/model-auto.js +1 -1
- package/dist/esm/model-auto.js.map +1 -1
- package/dist/esm/run/help.js +2 -1
- package/dist/esm/run/help.js.map +1 -1
- package/dist/esm/slides/frame-extraction.js +5 -1
- package/dist/esm/slides/frame-extraction.js.map +1 -1
- package/dist/esm/slides/scene-detection.js +19 -3
- package/dist/esm/slides/scene-detection.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/application/asset-execution.d.ts +21 -0
- package/dist/types/application/execution-input.d.ts +13 -0
- package/dist/types/application/url-routing.d.ts +37 -0
- package/dist/types/config/parse-helpers.d.ts +6 -0
- package/dist/types/content/asset.d.ts +3 -0
- package/dist/types/daemon/cli-health.d.ts +25 -0
- package/dist/types/daemon/cli-service.d.ts +39 -0
- package/dist/types/llm/cli-provider-output/codex.d.ts +9 -0
- package/dist/types/llm/cli-provider-output/opencode.d.ts +6 -0
- package/dist/types/llm/cli-provider-output/pi.d.ts +6 -0
- package/dist/types/llm/cli-provider-output/shared.d.ts +2 -0
- package/dist/types/llm/cli-provider-output.d.ts +3 -18
- package/dist/types/llm/cli-runners/codex.d.ts +2 -0
- package/dist/types/llm/cli-runners/json.d.ts +3 -0
- package/dist/types/llm/cli-runners/openclaw.d.ts +2 -0
- package/dist/types/llm/cli-runners/opencode.d.ts +2 -0
- package/dist/types/llm/cli-runners/pi.d.ts +2 -0
- package/dist/types/llm/cli-runners/plain.d.ts +3 -0
- package/dist/types/llm/cli-runners/types.d.ts +22 -0
- package/dist/types/llm/cli.d.ts +1 -6
- package/dist/types/llm/generate-text-shared.d.ts +2 -1
- package/dist/types/llm/github-models.d.ts +2 -0
- package/dist/types/llm/providers/anthropic.d.ts +15 -8
- package/dist/types/llm/providers/models.d.ts +4 -1
- package/dist/types/llm/providers/openai/sse.d.ts +1 -0
- package/dist/types/llm/providers/shared.d.ts +1 -0
- package/dist/types/slides/scene-detection.d.ts +7 -1
- package/dist/types/version.d.ts +1 -1
- package/docs/chrome-extension.md +11 -6
- package/docs/commands/daemon.md +3 -3
- package/docs/commands/summarize.md +1 -1
- package/docs/install.md +1 -1
- package/docs/llm.md +1 -1
- package/package.json +11 -11
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { toNumber } from "./shared.js";
|
|
2
|
+
function parsePiUsage(usage) {
|
|
3
|
+
if (!usage || typeof usage !== "object")
|
|
4
|
+
return null;
|
|
5
|
+
const record = usage;
|
|
6
|
+
const promptTokens = toNumber(record.input);
|
|
7
|
+
const completionTokens = toNumber(record.output);
|
|
8
|
+
const totalTokens = toNumber(record.totalTokens);
|
|
9
|
+
if (promptTokens === null && completionTokens === null && totalTokens === null)
|
|
10
|
+
return null;
|
|
11
|
+
return { promptTokens, completionTokens, totalTokens };
|
|
12
|
+
}
|
|
13
|
+
function parsePiCost(usage) {
|
|
14
|
+
if (!usage || typeof usage !== "object")
|
|
15
|
+
return null;
|
|
16
|
+
const cost = usage.cost;
|
|
17
|
+
return cost && typeof cost === "object"
|
|
18
|
+
? toNumber(cost.total)
|
|
19
|
+
: null;
|
|
20
|
+
}
|
|
21
|
+
function extractPiTextContent(content) {
|
|
22
|
+
if (!Array.isArray(content))
|
|
23
|
+
return "";
|
|
24
|
+
return content
|
|
25
|
+
.filter((block) => block && typeof block === "object" && block.type === "text")
|
|
26
|
+
.map((block) => block.text)
|
|
27
|
+
.filter((text) => typeof text === "string" && text.trim().length > 0)
|
|
28
|
+
.join("")
|
|
29
|
+
.trim();
|
|
30
|
+
}
|
|
31
|
+
export function parsePiOutputFromJsonl(output) {
|
|
32
|
+
const trimmed = output.trim();
|
|
33
|
+
if (!trimmed)
|
|
34
|
+
throw new Error("CLI returned empty output");
|
|
35
|
+
const lines = trimmed
|
|
36
|
+
.split(/\r?\n/)
|
|
37
|
+
.map((line) => line.trim())
|
|
38
|
+
.filter((line) => line.length > 0);
|
|
39
|
+
const textDeltaParts = [];
|
|
40
|
+
let fullText = null;
|
|
41
|
+
let usage = null;
|
|
42
|
+
let costUsd = null;
|
|
43
|
+
let sawStructuredEvent = false;
|
|
44
|
+
const errorMessages = [];
|
|
45
|
+
const plainLines = [];
|
|
46
|
+
for (const line of lines) {
|
|
47
|
+
if (!line.startsWith("{")) {
|
|
48
|
+
plainLines.push(line);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(line);
|
|
53
|
+
sawStructuredEvent = true;
|
|
54
|
+
if (parsed.type === "message_update") {
|
|
55
|
+
const event = parsed.assistantMessageEvent;
|
|
56
|
+
if (event?.type === "text_delta" &&
|
|
57
|
+
typeof event.delta === "string" &&
|
|
58
|
+
event.delta.length > 0) {
|
|
59
|
+
textDeltaParts.push(event.delta);
|
|
60
|
+
}
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (parsed.type !== "message_end" && parsed.type !== "turn_end")
|
|
64
|
+
continue;
|
|
65
|
+
const message = parsed.message;
|
|
66
|
+
if (!message || message.role !== "assistant")
|
|
67
|
+
continue;
|
|
68
|
+
if (typeof message.errorMessage === "string" && message.errorMessage.trim().length > 0) {
|
|
69
|
+
const error = message.errorMessage.trim();
|
|
70
|
+
if (!errorMessages.includes(error))
|
|
71
|
+
errorMessages.push(error);
|
|
72
|
+
}
|
|
73
|
+
const extracted = extractPiTextContent(message.content);
|
|
74
|
+
if (extracted)
|
|
75
|
+
fullText = extracted;
|
|
76
|
+
usage = parsePiUsage(message.usage) ?? usage;
|
|
77
|
+
costUsd = parsePiCost(message.usage) ?? costUsd;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Ignore malformed JSON lines.
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const text = fullText ?? textDeltaParts.join("").trim();
|
|
84
|
+
if (text)
|
|
85
|
+
return { text, usage, costUsd };
|
|
86
|
+
if (errorMessages.length > 0)
|
|
87
|
+
throw new Error(errorMessages.join("\n"));
|
|
88
|
+
if (sawStructuredEvent && plainLines.length > 0)
|
|
89
|
+
throw new Error(plainLines.join("\n"));
|
|
90
|
+
if (sawStructuredEvent)
|
|
91
|
+
throw new Error("CLI returned empty output");
|
|
92
|
+
return { text: trimmed, usage: null, costUsd: null };
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=pi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pi.js","sourceRoot":"","sources":["../../../../src/llm/cli-provider-output/pi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,SAAS,YAAY,CAAC,KAAc;IAClC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACrD,MAAM,MAAM,GAAG,KAAgC,CAAC;IAChD,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5C,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACjD,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACjD,IAAI,YAAY,KAAK,IAAI,IAAI,gBAAgB,KAAK,IAAI,IAAI,WAAW,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAC5F,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,WAA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value) {
|
|
2
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
3
|
+
}
|
|
4
|
+
export function sumNullable(current, next) {
|
|
5
|
+
if (typeof next !== "number")
|
|
6
|
+
return current;
|
|
7
|
+
if (typeof current !== "number")
|
|
8
|
+
return next;
|
|
9
|
+
return current + next;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../../src/llm/cli-provider-output/shared.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5E,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,OAAsB,EAAE,IAAmB;IACrE,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,OAAO,CAAC;IAC7C,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC7C,OAAO,OAAO,GAAG,IAAI,CAAC;AACxB,CAAC"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import { toNumber } from "./cli-provider-output/shared.js";
|
|
2
|
+
export { parseCodexOutputFromJsonl, parseCodexUsageFromJsonl, } from "./cli-provider-output/codex.js";
|
|
3
|
+
export { parseOpenCodeOutputFromJsonl } from "./cli-provider-output/opencode.js";
|
|
4
|
+
export { parsePiOutputFromJsonl } from "./cli-provider-output/pi.js";
|
|
1
5
|
const JSON_RESULT_FIELDS = ["result", "response", "output", "message", "text"];
|
|
2
6
|
export function isJsonCliProvider(provider) {
|
|
3
|
-
return
|
|
4
|
-
provider !== "openclaw" &&
|
|
5
|
-
provider !== "opencode" &&
|
|
6
|
-
provider !== "copilot" &&
|
|
7
|
-
provider !== "agy" &&
|
|
8
|
-
provider !== "pi");
|
|
7
|
+
return !["codex", "openclaw", "opencode", "copilot", "agy", "pi"].includes(provider);
|
|
9
8
|
}
|
|
10
|
-
|
|
9
|
+
function parseJsonFromOutput(output) {
|
|
11
10
|
const trimmed = output.trim();
|
|
12
11
|
if (!trimmed)
|
|
13
12
|
return null;
|
|
@@ -16,52 +15,37 @@ const parseJsonFromOutput = (output) => {
|
|
|
16
15
|
return JSON.parse(trimmed);
|
|
17
16
|
}
|
|
18
17
|
catch {
|
|
19
|
-
//
|
|
18
|
+
// Try the final JSON object after CLI diagnostics.
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
const lastBraceIndex = trimmed.lastIndexOf("\n{");
|
|
23
|
-
if (lastBraceIndex
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
22
|
+
if (lastBraceIndex < 0)
|
|
23
|
+
return null;
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(trimmed.slice(lastBraceIndex + 1).trim());
|
|
31
26
|
}
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
const toNumber = (value) => {
|
|
35
|
-
if (typeof value !== "number" || !Number.isFinite(value))
|
|
27
|
+
catch {
|
|
36
28
|
return null;
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (typeof next !== "number")
|
|
41
|
-
return current;
|
|
42
|
-
if (typeof current !== "number")
|
|
43
|
-
return next;
|
|
44
|
-
return current + next;
|
|
45
|
-
};
|
|
46
|
-
const parseClaudeUsage = (payload) => {
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function parseClaudeUsage(payload) {
|
|
47
32
|
const usage = payload.usage;
|
|
48
33
|
if (!usage || typeof usage !== "object")
|
|
49
34
|
return null;
|
|
50
|
-
const
|
|
51
|
-
const inputTokens = toNumber(
|
|
52
|
-
const cacheCreationTokens = toNumber(
|
|
53
|
-
const cacheReadTokens = toNumber(
|
|
54
|
-
const
|
|
55
|
-
if (inputTokens === null &&
|
|
35
|
+
const record = usage;
|
|
36
|
+
const inputTokens = toNumber(record.input_tokens);
|
|
37
|
+
const cacheCreationTokens = toNumber(record.cache_creation_input_tokens) ?? 0;
|
|
38
|
+
const cacheReadTokens = toNumber(record.cache_read_input_tokens) ?? 0;
|
|
39
|
+
const completionTokens = toNumber(record.output_tokens);
|
|
40
|
+
if (inputTokens === null && completionTokens === null)
|
|
56
41
|
return null;
|
|
57
42
|
const promptTokens = inputTokens !== null ? inputTokens + cacheCreationTokens + cacheReadTokens : null;
|
|
58
|
-
const completionTokens = outputTokens;
|
|
59
43
|
const totalTokens = typeof promptTokens === "number" && typeof completionTokens === "number"
|
|
60
44
|
? promptTokens + completionTokens
|
|
61
45
|
: null;
|
|
62
46
|
return { promptTokens, completionTokens, totalTokens };
|
|
63
|
-
}
|
|
64
|
-
|
|
47
|
+
}
|
|
48
|
+
function parseGeminiUsage(payload) {
|
|
65
49
|
const stats = payload.stats;
|
|
66
50
|
if (!stats || typeof stats !== "object")
|
|
67
51
|
return null;
|
|
@@ -80,18 +64,19 @@ const parseGeminiUsage = (payload) => {
|
|
|
80
64
|
const tokens = entry.tokens;
|
|
81
65
|
if (!tokens || typeof tokens !== "object")
|
|
82
66
|
continue;
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
|
|
67
|
+
const record = tokens;
|
|
68
|
+
const prompt = toNumber(record.prompt);
|
|
69
|
+
const candidates = toNumber(record.candidates);
|
|
70
|
+
const total = toNumber(record.total);
|
|
71
|
+
if (prompt !== null) {
|
|
87
72
|
promptSum += prompt;
|
|
88
73
|
hasPrompt = true;
|
|
89
74
|
}
|
|
90
|
-
if (
|
|
75
|
+
if (candidates !== null) {
|
|
91
76
|
completionSum += candidates;
|
|
92
77
|
hasCompletion = true;
|
|
93
78
|
}
|
|
94
|
-
if (
|
|
79
|
+
if (total !== null) {
|
|
95
80
|
totalSum += total;
|
|
96
81
|
hasTotal = true;
|
|
97
82
|
}
|
|
@@ -106,423 +91,39 @@ const parseGeminiUsage = (payload) => {
|
|
|
106
91
|
? promptTokens + completionTokens
|
|
107
92
|
: null;
|
|
108
93
|
return { promptTokens, completionTokens, totalTokens };
|
|
109
|
-
};
|
|
110
|
-
export const parseCodexUsageFromJsonl = (output) => {
|
|
111
|
-
const lines = output
|
|
112
|
-
.split(/\r?\n/)
|
|
113
|
-
.map((line) => line.trim())
|
|
114
|
-
.filter((line) => line.length > 0);
|
|
115
|
-
let usage = null;
|
|
116
|
-
let costUsd = null;
|
|
117
|
-
for (const line of lines) {
|
|
118
|
-
if (!line.startsWith("{"))
|
|
119
|
-
continue;
|
|
120
|
-
try {
|
|
121
|
-
const parsed = JSON.parse(line);
|
|
122
|
-
const candidates = [
|
|
123
|
-
parsed.usage,
|
|
124
|
-
parsed.response?.usage,
|
|
125
|
-
parsed.metrics?.usage,
|
|
126
|
-
].filter(Boolean);
|
|
127
|
-
for (const candidate of candidates) {
|
|
128
|
-
const input = toNumber(candidate.input_tokens) ??
|
|
129
|
-
toNumber(candidate.prompt_tokens) ??
|
|
130
|
-
toNumber(candidate.inputTokens) ??
|
|
131
|
-
null;
|
|
132
|
-
const outputTokens = toNumber(candidate.output_tokens) ??
|
|
133
|
-
toNumber(candidate.completion_tokens) ??
|
|
134
|
-
toNumber(candidate.outputTokens) ??
|
|
135
|
-
null;
|
|
136
|
-
const totalTokens = toNumber(candidate.total_tokens) ??
|
|
137
|
-
toNumber(candidate.totalTokens) ??
|
|
138
|
-
(typeof input === "number" && typeof outputTokens === "number"
|
|
139
|
-
? input + outputTokens
|
|
140
|
-
: null);
|
|
141
|
-
if (input !== null || outputTokens !== null || totalTokens !== null) {
|
|
142
|
-
usage = { promptTokens: input, completionTokens: outputTokens, totalTokens };
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
if (costUsd === null) {
|
|
146
|
-
const costValue = toNumber(parsed.cost_usd) ??
|
|
147
|
-
toNumber(parsed.usage?.cost_usd) ??
|
|
148
|
-
null;
|
|
149
|
-
if (typeof costValue === "number")
|
|
150
|
-
costUsd = costValue;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
// ignore malformed JSON lines
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return { usage, costUsd };
|
|
158
|
-
};
|
|
159
|
-
function extractCodexTextFromContentBlocks(blocks) {
|
|
160
|
-
if (!Array.isArray(blocks))
|
|
161
|
-
return null;
|
|
162
|
-
const text = blocks
|
|
163
|
-
.flatMap((block) => {
|
|
164
|
-
if (!block || typeof block !== "object")
|
|
165
|
-
return [];
|
|
166
|
-
const record = block;
|
|
167
|
-
if (typeof record.text === "string" && record.text.trim().length > 0) {
|
|
168
|
-
return [record.text];
|
|
169
|
-
}
|
|
170
|
-
const nested = record.content;
|
|
171
|
-
if (!Array.isArray(nested))
|
|
172
|
-
return [];
|
|
173
|
-
return nested
|
|
174
|
-
.map((part) => {
|
|
175
|
-
if (!part || typeof part !== "object")
|
|
176
|
-
return "";
|
|
177
|
-
const partRecord = part;
|
|
178
|
-
return typeof partRecord.text === "string" ? partRecord.text : "";
|
|
179
|
-
})
|
|
180
|
-
.filter((part) => part.trim().length > 0);
|
|
181
|
-
})
|
|
182
|
-
.join("");
|
|
183
|
-
return text.trim().length > 0 ? text.trim() : null;
|
|
184
|
-
}
|
|
185
|
-
function extractCodexTextEvent(parsed) {
|
|
186
|
-
const type = typeof parsed.type === "string" ? parsed.type : "";
|
|
187
|
-
if (type === "response.output_text.delta" && typeof parsed.delta === "string") {
|
|
188
|
-
return { deltaText: parsed.delta, fullText: null };
|
|
189
|
-
}
|
|
190
|
-
if (type === "response.output_text.done") {
|
|
191
|
-
const text = typeof parsed.text === "string"
|
|
192
|
-
? parsed.text
|
|
193
|
-
: typeof parsed.delta === "string"
|
|
194
|
-
? parsed.delta
|
|
195
|
-
: null;
|
|
196
|
-
return { deltaText: null, fullText: text };
|
|
197
|
-
}
|
|
198
|
-
if (typeof parsed.output_text === "string" && parsed.output_text.trim().length > 0) {
|
|
199
|
-
return { deltaText: null, fullText: parsed.output_text.trim() };
|
|
200
|
-
}
|
|
201
|
-
const response = parsed.response;
|
|
202
|
-
if (response && typeof response === "object") {
|
|
203
|
-
const responseRecord = response;
|
|
204
|
-
if (typeof responseRecord.output_text === "string" &&
|
|
205
|
-
responseRecord.output_text.trim().length > 0) {
|
|
206
|
-
return { deltaText: null, fullText: responseRecord.output_text.trim() };
|
|
207
|
-
}
|
|
208
|
-
const responseOutput = extractCodexTextFromContentBlocks(responseRecord.output);
|
|
209
|
-
if (responseOutput) {
|
|
210
|
-
return { deltaText: null, fullText: responseOutput };
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
const message = parsed.message;
|
|
214
|
-
if (message && typeof message === "object") {
|
|
215
|
-
const messageOutput = extractCodexTextFromContentBlocks([message]);
|
|
216
|
-
if (messageOutput) {
|
|
217
|
-
return { deltaText: null, fullText: messageOutput };
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
const item = parsed.item;
|
|
221
|
-
if (item && typeof item === "object") {
|
|
222
|
-
const itemOutput = extractCodexTextFromContentBlocks([item]);
|
|
223
|
-
if (itemOutput) {
|
|
224
|
-
return { deltaText: null, fullText: itemOutput };
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return { deltaText: null, fullText: null };
|
|
228
|
-
}
|
|
229
|
-
export function parseCodexOutputFromJsonl(output) {
|
|
230
|
-
const trimmed = output.trim();
|
|
231
|
-
if (!trimmed) {
|
|
232
|
-
return { text: null, sawStructuredEvent: false };
|
|
233
|
-
}
|
|
234
|
-
const lines = trimmed
|
|
235
|
-
.split(/\r?\n/)
|
|
236
|
-
.map((line) => line.trim())
|
|
237
|
-
.filter((line) => line.length > 0);
|
|
238
|
-
const deltaParts = [];
|
|
239
|
-
let fullText = null;
|
|
240
|
-
let sawStructuredEvent = false;
|
|
241
|
-
for (const line of lines) {
|
|
242
|
-
if (!line.startsWith("{"))
|
|
243
|
-
continue;
|
|
244
|
-
try {
|
|
245
|
-
const parsed = JSON.parse(line);
|
|
246
|
-
sawStructuredEvent = true;
|
|
247
|
-
const { deltaText, fullText: eventFullText } = extractCodexTextEvent(parsed);
|
|
248
|
-
if (typeof deltaText === "string" && deltaText.length > 0) {
|
|
249
|
-
deltaParts.push(deltaText);
|
|
250
|
-
continue;
|
|
251
|
-
}
|
|
252
|
-
if (!fullText && typeof eventFullText === "string" && eventFullText.length > 0) {
|
|
253
|
-
fullText = eventFullText;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
catch {
|
|
257
|
-
// ignore malformed JSON lines
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
const deltaText = deltaParts.join("").trim();
|
|
261
|
-
if (deltaText)
|
|
262
|
-
return { text: deltaText, sawStructuredEvent };
|
|
263
|
-
if (fullText)
|
|
264
|
-
return { text: fullText, sawStructuredEvent };
|
|
265
|
-
return { text: null, sawStructuredEvent };
|
|
266
|
-
}
|
|
267
|
-
function parseOpenCodeTokens(payload) {
|
|
268
|
-
const tokens = payload.tokens;
|
|
269
|
-
if (!tokens || typeof tokens !== "object")
|
|
270
|
-
return null;
|
|
271
|
-
const record = tokens;
|
|
272
|
-
const promptTokens = toNumber(record.input);
|
|
273
|
-
const completionTokens = toNumber(record.output);
|
|
274
|
-
const totalTokens = toNumber(record.total) ??
|
|
275
|
-
(typeof promptTokens === "number" && typeof completionTokens === "number"
|
|
276
|
-
? promptTokens + completionTokens
|
|
277
|
-
: null);
|
|
278
|
-
if (promptTokens === null && completionTokens === null && totalTokens === null)
|
|
279
|
-
return null;
|
|
280
|
-
return { promptTokens, completionTokens, totalTokens };
|
|
281
|
-
}
|
|
282
|
-
function extractOpenCodeErrorMessage(payload) {
|
|
283
|
-
const error = payload.error;
|
|
284
|
-
if (!error)
|
|
285
|
-
return null;
|
|
286
|
-
if (typeof error === "string" && error.trim().length > 0)
|
|
287
|
-
return error.trim();
|
|
288
|
-
if (typeof error !== "object")
|
|
289
|
-
return null;
|
|
290
|
-
const errorRecord = error;
|
|
291
|
-
const data = errorRecord.data;
|
|
292
|
-
if (data && typeof data === "object") {
|
|
293
|
-
const message = data.message;
|
|
294
|
-
if (typeof message === "string" && message.trim().length > 0)
|
|
295
|
-
return message.trim();
|
|
296
|
-
}
|
|
297
|
-
const message = errorRecord.message;
|
|
298
|
-
if (typeof message === "string" && message.trim().length > 0)
|
|
299
|
-
return message.trim();
|
|
300
|
-
const name = errorRecord.name;
|
|
301
|
-
return typeof name === "string" && name.trim().length > 0 ? name.trim() : null;
|
|
302
|
-
}
|
|
303
|
-
export function parseOpenCodeOutputFromJsonl(output) {
|
|
304
|
-
const trimmed = output.trim();
|
|
305
|
-
if (!trimmed) {
|
|
306
|
-
throw new Error("CLI returned empty output");
|
|
307
|
-
}
|
|
308
|
-
const lines = trimmed
|
|
309
|
-
.split(/\r?\n/)
|
|
310
|
-
.map((line) => line.trim())
|
|
311
|
-
.filter((line) => line.length > 0);
|
|
312
|
-
const textParts = [];
|
|
313
|
-
const errorMessages = [];
|
|
314
|
-
let promptTokens = null;
|
|
315
|
-
let completionTokens = null;
|
|
316
|
-
let totalTokens = null;
|
|
317
|
-
let costUsd = null;
|
|
318
|
-
let sawStructuredEvent = false;
|
|
319
|
-
for (const line of lines) {
|
|
320
|
-
if (!line.startsWith("{"))
|
|
321
|
-
continue;
|
|
322
|
-
try {
|
|
323
|
-
const parsed = JSON.parse(line);
|
|
324
|
-
sawStructuredEvent = true;
|
|
325
|
-
if (parsed.type === "text") {
|
|
326
|
-
const part = parsed.part;
|
|
327
|
-
if (part && typeof part === "object") {
|
|
328
|
-
const text = part.text;
|
|
329
|
-
if (typeof text === "string" && text.length > 0) {
|
|
330
|
-
textParts.push(text);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
if (parsed.type === "step_finish") {
|
|
336
|
-
const part = parsed.part;
|
|
337
|
-
if (!part || typeof part !== "object")
|
|
338
|
-
continue;
|
|
339
|
-
const usage = parseOpenCodeTokens(part);
|
|
340
|
-
if (usage) {
|
|
341
|
-
promptTokens = sumNullable(promptTokens, usage.promptTokens);
|
|
342
|
-
completionTokens = sumNullable(completionTokens, usage.completionTokens);
|
|
343
|
-
totalTokens = sumNullable(totalTokens, usage.totalTokens);
|
|
344
|
-
}
|
|
345
|
-
const cost = toNumber(part.cost);
|
|
346
|
-
if (typeof cost === "number") {
|
|
347
|
-
costUsd = typeof costUsd === "number" ? costUsd + cost : cost;
|
|
348
|
-
}
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
if (parsed.type === "error") {
|
|
352
|
-
const message = extractOpenCodeErrorMessage(parsed);
|
|
353
|
-
if (message)
|
|
354
|
-
errorMessages.push(message);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
catch {
|
|
358
|
-
// ignore malformed JSON lines
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
const text = textParts.join("").trim();
|
|
362
|
-
const usage = promptTokens !== null || completionTokens !== null || totalTokens !== null
|
|
363
|
-
? { promptTokens, completionTokens, totalTokens }
|
|
364
|
-
: null;
|
|
365
|
-
if (text) {
|
|
366
|
-
return { text, usage, costUsd };
|
|
367
|
-
}
|
|
368
|
-
if (errorMessages.length > 0) {
|
|
369
|
-
throw new Error(errorMessages.join("\n"));
|
|
370
|
-
}
|
|
371
|
-
if (sawStructuredEvent) {
|
|
372
|
-
throw new Error("CLI returned empty output");
|
|
373
|
-
}
|
|
374
|
-
return { text: trimmed, usage: null, costUsd: null };
|
|
375
94
|
}
|
|
376
95
|
function extractJsonResultText(payload) {
|
|
377
96
|
for (const key of JSON_RESULT_FIELDS) {
|
|
378
97
|
const value = payload[key];
|
|
379
|
-
if (typeof value === "string" && value.trim().length > 0)
|
|
98
|
+
if (typeof value === "string" && value.trim().length > 0)
|
|
380
99
|
return value.trim();
|
|
381
|
-
}
|
|
382
100
|
}
|
|
383
101
|
return null;
|
|
384
102
|
}
|
|
385
|
-
function parseJsonProviderUsage(provider, payload) {
|
|
386
|
-
if (provider === "claude")
|
|
387
|
-
return parseClaudeUsage(payload);
|
|
388
|
-
if (provider === "gemini")
|
|
389
|
-
return parseGeminiUsage(payload);
|
|
390
|
-
return null;
|
|
391
|
-
}
|
|
392
|
-
function parseJsonProviderCostUsd(provider, payload) {
|
|
393
|
-
if (provider !== "claude")
|
|
394
|
-
return null;
|
|
395
|
-
return toNumber(payload.total_cost_usd) ?? null;
|
|
396
|
-
}
|
|
397
103
|
export function parseJsonProviderOutput(args) {
|
|
398
104
|
const trimmed = args.stdout.trim();
|
|
399
|
-
if (!trimmed)
|
|
105
|
+
if (!trimmed)
|
|
400
106
|
throw new Error("CLI returned empty output");
|
|
401
|
-
}
|
|
402
107
|
const parsed = parseJsonFromOutput(trimmed);
|
|
403
108
|
if (parsed && typeof parsed === "object") {
|
|
404
109
|
const payload = Array.isArray(parsed)
|
|
405
110
|
? (parsed.find((item) => item && typeof item === "object" && item.type === "result") ?? null)
|
|
406
111
|
: parsed;
|
|
407
112
|
if (payload) {
|
|
408
|
-
const
|
|
409
|
-
if (
|
|
113
|
+
const text = extractJsonResultText(payload);
|
|
114
|
+
if (text) {
|
|
410
115
|
return {
|
|
411
|
-
text
|
|
412
|
-
usage:
|
|
413
|
-
|
|
116
|
+
text,
|
|
117
|
+
usage: args.provider === "claude"
|
|
118
|
+
? parseClaudeUsage(payload)
|
|
119
|
+
: args.provider === "gemini"
|
|
120
|
+
? parseGeminiUsage(payload)
|
|
121
|
+
: null,
|
|
122
|
+
costUsd: args.provider === "claude" ? toNumber(payload.total_cost_usd) : null,
|
|
414
123
|
};
|
|
415
124
|
}
|
|
416
125
|
}
|
|
417
126
|
}
|
|
418
127
|
return { text: trimmed, usage: null, costUsd: null };
|
|
419
128
|
}
|
|
420
|
-
// ── pi CLI parser (JSONL) ────────────────────────────────────────────────
|
|
421
|
-
function parsePiUsage(usage) {
|
|
422
|
-
if (!usage || typeof usage !== "object")
|
|
423
|
-
return null;
|
|
424
|
-
const record = usage;
|
|
425
|
-
const promptTokens = toNumber(record.input);
|
|
426
|
-
const completionTokens = toNumber(record.output);
|
|
427
|
-
const totalTokens = toNumber(record.totalTokens);
|
|
428
|
-
if (promptTokens === null && completionTokens === null && totalTokens === null)
|
|
429
|
-
return null;
|
|
430
|
-
return { promptTokens, completionTokens, totalTokens };
|
|
431
|
-
}
|
|
432
|
-
function parsePiCost(usage) {
|
|
433
|
-
if (!usage || typeof usage !== "object")
|
|
434
|
-
return null;
|
|
435
|
-
const record = usage;
|
|
436
|
-
const cost = record.cost;
|
|
437
|
-
if (!cost || typeof cost !== "object")
|
|
438
|
-
return null;
|
|
439
|
-
return toNumber(cost.total);
|
|
440
|
-
}
|
|
441
|
-
function extractPiTextContent(content) {
|
|
442
|
-
if (!Array.isArray(content))
|
|
443
|
-
return "";
|
|
444
|
-
return content
|
|
445
|
-
.filter((block) => block && typeof block === "object" && block.type === "text")
|
|
446
|
-
.map((block) => block.text)
|
|
447
|
-
.filter((text) => typeof text === "string" && text.trim().length > 0)
|
|
448
|
-
.join("")
|
|
449
|
-
.trim();
|
|
450
|
-
}
|
|
451
|
-
export function parsePiOutputFromJsonl(output) {
|
|
452
|
-
const trimmed = output.trim();
|
|
453
|
-
if (!trimmed) {
|
|
454
|
-
throw new Error("CLI returned empty output");
|
|
455
|
-
}
|
|
456
|
-
const lines = trimmed
|
|
457
|
-
.split(/\r?\n/)
|
|
458
|
-
.map((line) => line.trim())
|
|
459
|
-
.filter((line) => line.length > 0);
|
|
460
|
-
const textDeltaParts = [];
|
|
461
|
-
let fullText = null;
|
|
462
|
-
let usage = null;
|
|
463
|
-
let costUsd = null;
|
|
464
|
-
let sawStructuredEvent = false;
|
|
465
|
-
const errorMessages = [];
|
|
466
|
-
const plainLines = [];
|
|
467
|
-
for (const line of lines) {
|
|
468
|
-
if (!line.startsWith("{")) {
|
|
469
|
-
plainLines.push(line);
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
try {
|
|
473
|
-
const parsed = JSON.parse(line);
|
|
474
|
-
sawStructuredEvent = true;
|
|
475
|
-
// Collect text deltas from streaming events.
|
|
476
|
-
if (parsed.type === "message_update") {
|
|
477
|
-
const event = parsed.assistantMessageEvent;
|
|
478
|
-
if (event?.type === "text_delta" &&
|
|
479
|
-
typeof event.delta === "string" &&
|
|
480
|
-
event.delta.length > 0) {
|
|
481
|
-
textDeltaParts.push(event.delta);
|
|
482
|
-
}
|
|
483
|
-
continue;
|
|
484
|
-
}
|
|
485
|
-
// Extract full text and usage from the final message events.
|
|
486
|
-
if (parsed.type === "message_end" || parsed.type === "turn_end") {
|
|
487
|
-
const message = parsed.message;
|
|
488
|
-
if (message) {
|
|
489
|
-
if (message.role !== "assistant")
|
|
490
|
-
continue;
|
|
491
|
-
if (typeof message.errorMessage === "string" && message.errorMessage.trim().length > 0) {
|
|
492
|
-
const errorMessage = message.errorMessage.trim();
|
|
493
|
-
if (!errorMessages.includes(errorMessage))
|
|
494
|
-
errorMessages.push(errorMessage);
|
|
495
|
-
}
|
|
496
|
-
const extracted = extractPiTextContent(message.content);
|
|
497
|
-
if (extracted)
|
|
498
|
-
fullText = extracted;
|
|
499
|
-
const parsedUsage = parsePiUsage(message.usage);
|
|
500
|
-
if (parsedUsage)
|
|
501
|
-
usage = parsedUsage;
|
|
502
|
-
const parsedCost = parsePiCost(message.usage);
|
|
503
|
-
if (parsedCost !== null)
|
|
504
|
-
costUsd = parsedCost;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
catch {
|
|
509
|
-
// ignore malformed JSON lines
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
const deltaText = textDeltaParts.join("").trim();
|
|
513
|
-
const text = fullText ?? deltaText;
|
|
514
|
-
if (text) {
|
|
515
|
-
return { text, usage, costUsd };
|
|
516
|
-
}
|
|
517
|
-
if (errorMessages.length > 0) {
|
|
518
|
-
throw new Error(errorMessages.join("\n"));
|
|
519
|
-
}
|
|
520
|
-
if (sawStructuredEvent && plainLines.length > 0) {
|
|
521
|
-
throw new Error(plainLines.join("\n"));
|
|
522
|
-
}
|
|
523
|
-
if (sawStructuredEvent) {
|
|
524
|
-
throw new Error("CLI returned empty output");
|
|
525
|
-
}
|
|
526
|
-
return { text: trimmed, usage: null, costUsd: null };
|
|
527
|
-
}
|
|
528
129
|
//# sourceMappingURL=cli-provider-output.js.map
|