crysk 0.4.0 → 0.5.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 +26 -17
- package/config/models/anthropic.json +1 -1
- package/dist/SKILL-k4k0zn1w.md +143 -0
- package/dist/SKILL-vhb4c3vv.md +553 -0
- package/dist/SKILL-xpf6kmdw.md +74 -0
- package/dist/ci-admission-0ken6ehx.md +128 -0
- package/dist/cli.js +3056 -1623
- package/dist/index.js +1744 -938
- package/dist/opencode-ci-ac5zksdn.md +106 -0
- package/dist/workflow-4pkyck01.yml +160 -0
- package/package.json +1 -1
- package/prompts/default/PROVENANCE.md +1 -0
- package/skills/crysk/SKILL.md +16 -16
- package/skills/crysk-ci-setup/SKILL.md +41 -38
- package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
- package/skills/crysk-ci-setup/references/ci-admission.md +1 -1
- package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
- package/skills/crysk-loop/SKILL.md +1 -1
- package/src/asset-manifest.ts +0 -4
- package/src/assets.ts +1 -1
- package/src/ci/admission-ledger.ts +4 -0
- package/src/ci/classify-error.ts +21 -24
- package/src/ci/cli-record.ts +53 -1
- package/src/ci/crysk-secrets.ts +409 -0
- package/src/ci/force-comment.ts +14 -1
- package/src/ci/gates.ts +86 -57
- package/src/ci/outcome.ts +421 -330
- package/src/ci/platform-config.ts +2 -2
- package/src/ci/platform-estimate.ts +111 -0
- package/src/ci/platform-report.ts +274 -41
- package/src/ci/platform-trace.ts +581 -0
- package/src/ci/platform.ts +58 -12
- package/src/ci/publish.ts +19 -6
- package/src/ci/reporter.ts +14 -4
- package/src/ci/review-levels.ts +8 -7
- package/src/ci/run.ts +88 -0
- package/src/ci/setup.ts +485 -118
- package/src/ci/trace-bundle.ts +1486 -0
- package/src/ci/trace-transcripts.ts +480 -0
- package/src/ci/trigger.ts +176 -33
- package/src/cli.ts +11 -3
- package/src/commands/ci-setup-questions.ts +3 -3
- package/src/commands/ci-setup.ts +152 -6
- package/src/commands/config.ts +3 -0
- package/src/commands/doctor.ts +15 -6
- package/src/config/config.ts +16 -3
- package/src/diversity/clustering.ts +0 -1
- package/src/diversity/identity.ts +4 -5
- package/src/diversity/pipeline-integration.ts +1 -9
- package/src/doctor.ts +3 -3
- package/src/execution/attempt-evidence.ts +10 -5
- package/src/execution/claude-session-trace.ts +480 -0
- package/src/execution/contracts.ts +21 -4
- package/src/execution/harness.ts +105 -8
- package/src/execution/model-fallback.ts +239 -0
- package/src/execution/usage-normalized.ts +6 -4
- package/src/home-preflight.ts +1 -1
- package/src/index.ts +1 -1
- package/src/model/catalog.ts +13 -37
- package/src/model/free-discovery.ts +117 -8
- package/src/model/model-name.ts +404 -0
- package/src/model/provider-capabilities.ts +22 -7
- package/src/model/routing.ts +346 -467
- package/src/pr/admission.ts +18 -0
- package/src/pr/ci-admission-gate.ts +2 -0
- package/src/pr/discovery.ts +18 -0
- package/src/pr/plan.ts +55 -5
- package/src/pr/preflight.ts +1 -1
- package/src/pr/publish-outcome.ts +1 -1
- package/src/pr/review-pr.ts +26 -1
- package/src/pr/target.ts +63 -11
- package/src/production-runtime.ts +760 -331
- package/src/review/pipeline.ts +277 -101
- package/src/review/preflight.ts +201 -294
- package/src/review/report.ts +30 -26
- package/src/review/review.ts +20 -3
- package/src/review/route-preflight.ts +79 -11
- package/src/review/run.ts +44 -6
- package/src/review/scout.ts +1 -1
- package/src/review/spec.ts +3 -3
- package/src/review/step-runner.ts +70 -1
- package/src/runner-authority.ts +127 -9
- package/src/security/binding-policy.ts +1 -2
- package/src/security/claude-credential.ts +355 -0
- package/src/security/credential-broker.ts +56 -40
- package/src/security/evidence-redaction.ts +179 -4
- package/src/security/opencode-secret-manifest.ts +114 -0
- package/src/security/redact.ts +45 -0
- package/src/system-tools.ts +48 -21
- package/src/transport-registry.ts +12 -2
- package/src/transports/claude-code-cli.ts +21 -2
- package/src/transports/opencode-sdk.ts +11 -9
- package/src/transports/opencode-server.ts +7 -10
- package/src/ui/config-edit.ts +9 -20
- package/src/ui/config.ts +32 -27
- package/src/ui/plan.ts +84 -50
- package/src/updater.ts +10 -7
- package/prompts/default/deep-review-lifecycle.md +0 -362
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
// Reads one attempt's raw transcript into the conversation a review trace
|
|
2
|
+
// shows (src/ci/trace-bundle.ts): the prompt the model got, what it answered,
|
|
3
|
+
// and each tool call with its arguments, result and timing.
|
|
4
|
+
//
|
|
5
|
+
// Two transcripts exist. A Claude CLI attempt leaves its session JSONL
|
|
6
|
+
// (steps/trace.<step>.attempt<n>.jsonl, copied and redacted by
|
|
7
|
+
// src/execution/claude-session-trace.ts). An OpenCode attempt leaves its
|
|
8
|
+
// HTTP/SSE capture (steps/evidence.<step>.attempt<n>.capture.json, written by
|
|
9
|
+
// src/execution/attempt-evidence.ts), whose last `readback` record holds the
|
|
10
|
+
// session's messages.
|
|
11
|
+
//
|
|
12
|
+
// WHY every read is best effort: these files come from CLIs this engine does
|
|
13
|
+
// not own, and a format change must cost the conversation, never the trace. A
|
|
14
|
+
// line or record that does not have the expected shape is skipped.
|
|
15
|
+
//
|
|
16
|
+
// The message parts follow the OpenTelemetry GenAI message schema (TextPart,
|
|
17
|
+
// ReasoningPart, ToolCallRequestPart; see GENAI_SEMCONV_VERSION in
|
|
18
|
+
// trace-bundle.ts).
|
|
19
|
+
//
|
|
20
|
+
// BUILTINS ONLY: the trace step runs under always(), so it can run before
|
|
21
|
+
// `bun install` (test/ci/platform-trace.test.ts walks the import closure).
|
|
22
|
+
|
|
23
|
+
import { closeSync, openSync, readSync } from "node:fs";
|
|
24
|
+
|
|
25
|
+
export type MessagePart =
|
|
26
|
+
| { type: "text"; content: string }
|
|
27
|
+
| { type: "reasoning"; content: string }
|
|
28
|
+
| { type: "tool_call"; id?: string; name: string; arguments?: unknown };
|
|
29
|
+
|
|
30
|
+
export interface ChatMessage {
|
|
31
|
+
role: "user" | "assistant";
|
|
32
|
+
parts: MessagePart[];
|
|
33
|
+
finish_reason?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ToolCall {
|
|
37
|
+
id?: string;
|
|
38
|
+
name: string;
|
|
39
|
+
arguments?: unknown;
|
|
40
|
+
result?: string;
|
|
41
|
+
isError?: boolean;
|
|
42
|
+
startMs?: number;
|
|
43
|
+
endMs?: number;
|
|
44
|
+
// The index in Conversation.turns of the model turn that made this call.
|
|
45
|
+
turn?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// One model call of an agent loop (#511): an OpenCode attempt is dozens of
|
|
49
|
+
// these, one assistant message each, and each message records its own times,
|
|
50
|
+
// tokens and cost. Token counts are OpenCode's disjoint leaves.
|
|
51
|
+
export interface Turn {
|
|
52
|
+
startMs?: number;
|
|
53
|
+
endMs?: number;
|
|
54
|
+
// The model the provider says answered this turn.
|
|
55
|
+
model?: string;
|
|
56
|
+
finishReason?: string;
|
|
57
|
+
isError?: boolean;
|
|
58
|
+
inputTokens?: number;
|
|
59
|
+
outputTokens?: number;
|
|
60
|
+
reasoningTokens?: number;
|
|
61
|
+
cacheReadTokens?: number;
|
|
62
|
+
cacheWriteTokens?: number;
|
|
63
|
+
costUsd?: number;
|
|
64
|
+
// What the turn answered. Content only: withoutContent drops it.
|
|
65
|
+
message?: ChatMessage;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface Conversation {
|
|
69
|
+
input: ChatMessage[];
|
|
70
|
+
output: ChatMessage[];
|
|
71
|
+
tools: ToolCall[];
|
|
72
|
+
// The model the provider says answered, when the transcript names one.
|
|
73
|
+
responseModel?: string;
|
|
74
|
+
// The last answer's finish reason, as the provider spelled it.
|
|
75
|
+
finishReason?: string;
|
|
76
|
+
startMs?: number;
|
|
77
|
+
endMs?: number;
|
|
78
|
+
// The model turns, when the transcript records each one (OpenCode).
|
|
79
|
+
turns?: Turn[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
83
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function timeOf(value: unknown): number | undefined {
|
|
87
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) {
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
if (typeof value !== "string") return undefined;
|
|
91
|
+
const ms = Date.parse(value);
|
|
92
|
+
return Number.isFinite(ms) ? ms : undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function nonEmpty(value: unknown): string | undefined {
|
|
96
|
+
return typeof value === "string" && value !== "" ? value : undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// A tool result's content: a string, or the text blocks of a block list.
|
|
100
|
+
function resultText(content: unknown): string | undefined {
|
|
101
|
+
if (typeof content === "string") return content;
|
|
102
|
+
if (!Array.isArray(content)) return undefined;
|
|
103
|
+
const texts = content
|
|
104
|
+
.filter(isRecord)
|
|
105
|
+
.map((block) => (typeof block.text === "string" ? block.text : undefined))
|
|
106
|
+
.filter((text): text is string => text !== undefined);
|
|
107
|
+
return texts.length > 0 ? texts.join("\n") : undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function widen(conversation: Conversation, ms: number | undefined): void {
|
|
111
|
+
if (ms === undefined) return;
|
|
112
|
+
if (conversation.startMs === undefined || ms < conversation.startMs) {
|
|
113
|
+
conversation.startMs = ms;
|
|
114
|
+
}
|
|
115
|
+
if (conversation.endMs === undefined || ms > conversation.endMs) {
|
|
116
|
+
conversation.endMs = ms;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The Claude CLI writes one line per content block, so the blocks of one
|
|
121
|
+
// answer share its message id and are joined back into one message here.
|
|
122
|
+
export function parseClaudeSession(text: string): Conversation {
|
|
123
|
+
const reader = new ClaudeSessionReader(true);
|
|
124
|
+
for (const line of text.split("\n")) reader.line(line);
|
|
125
|
+
return reader.conversation;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// What a metadata trace keeps of a conversation: the tool calls' names, ids,
|
|
129
|
+
// times and status, the finish reason, the response model and the times. No
|
|
130
|
+
// message, no tool argument and no tool result.
|
|
131
|
+
export function withoutContent(conversation: Conversation): Conversation {
|
|
132
|
+
return {
|
|
133
|
+
...conversation,
|
|
134
|
+
input: [],
|
|
135
|
+
output: [],
|
|
136
|
+
tools: conversation.tools.map(
|
|
137
|
+
({ arguments: _a, result: _r, ...call }) => call,
|
|
138
|
+
),
|
|
139
|
+
...(conversation.turns === undefined
|
|
140
|
+
? {}
|
|
141
|
+
: {
|
|
142
|
+
turns: conversation.turns.map(({ message: _m, ...turn }) => turn),
|
|
143
|
+
}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface ScanLimits {
|
|
148
|
+
// Bytes read from the file at a time.
|
|
149
|
+
chunkBytes: number;
|
|
150
|
+
// A longer line is skipped without being held whole.
|
|
151
|
+
lineBytes: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// The session copy's own line cap (MAX_LINE_BYTES in
|
|
155
|
+
// src/execution/claude-session-trace.ts): the copy omits a longer line, so a
|
|
156
|
+
// line past it is no line the copy wrote.
|
|
157
|
+
const SCAN_LIMITS: ScanLimits = {
|
|
158
|
+
chunkBytes: 1024 * 1024,
|
|
159
|
+
lineBytes: 4 * 1024 * 1024,
|
|
160
|
+
};
|
|
161
|
+
const NEWLINE = 0x0a;
|
|
162
|
+
|
|
163
|
+
// A metadata trace's read of a Claude session file: the same reducer as
|
|
164
|
+
// parseClaudeSession, fed one line at a time and keeping no content.
|
|
165
|
+
//
|
|
166
|
+
// WHY it streams: a session can reach 64 MiB and a run holds one per attempt;
|
|
167
|
+
// reading each whole only to drop its text cost a metadata trace memory it
|
|
168
|
+
// never sends (eight 30 MB sessions held several hundred MB). Here the file is
|
|
169
|
+
// read in chunks, only the current line is held (at most lineBytes), and each
|
|
170
|
+
// line is parsed and dropped before the next. undefined when the file cannot
|
|
171
|
+
// be opened; a read error keeps what was read before it.
|
|
172
|
+
export function scanClaudeSessionFile(
|
|
173
|
+
file: string,
|
|
174
|
+
limits: ScanLimits = SCAN_LIMITS,
|
|
175
|
+
): Conversation | undefined {
|
|
176
|
+
let fd: number;
|
|
177
|
+
try {
|
|
178
|
+
fd = openSync(file, "r");
|
|
179
|
+
} catch {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
const reader = new ClaudeSessionReader(false);
|
|
183
|
+
const buffer = Buffer.allocUnsafe(limits.chunkBytes);
|
|
184
|
+
// The current line's bytes from earlier chunks, copied out of the buffer the
|
|
185
|
+
// next read overwrites.
|
|
186
|
+
let pending: Buffer[] = [];
|
|
187
|
+
let pendingBytes = 0;
|
|
188
|
+
let overLimit = false;
|
|
189
|
+
const hold = (bytes: Buffer): void => {
|
|
190
|
+
if (overLimit || bytes.length === 0) return;
|
|
191
|
+
if (pendingBytes + bytes.length > limits.lineBytes) {
|
|
192
|
+
overLimit = true;
|
|
193
|
+
pending = [];
|
|
194
|
+
pendingBytes = 0;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
pending.push(Buffer.from(bytes));
|
|
198
|
+
pendingBytes += bytes.length;
|
|
199
|
+
};
|
|
200
|
+
// Decoded only once whole, so a character split across chunks stays whole.
|
|
201
|
+
const finishLine = (tail: Buffer): void => {
|
|
202
|
+
if (!overLimit) {
|
|
203
|
+
if (pendingBytes === 0) {
|
|
204
|
+
if (tail.length <= limits.lineBytes) reader.line(tail.toString("utf8"));
|
|
205
|
+
} else if (pendingBytes + tail.length <= limits.lineBytes) {
|
|
206
|
+
reader.line(
|
|
207
|
+
Buffer.concat(
|
|
208
|
+
[...pending, tail],
|
|
209
|
+
pendingBytes + tail.length,
|
|
210
|
+
).toString("utf8"),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
pending = [];
|
|
215
|
+
pendingBytes = 0;
|
|
216
|
+
overLimit = false;
|
|
217
|
+
};
|
|
218
|
+
try {
|
|
219
|
+
for (;;) {
|
|
220
|
+
const read = readSync(fd, buffer, 0, limits.chunkBytes, null);
|
|
221
|
+
if (read === 0) break;
|
|
222
|
+
const chunk = buffer.subarray(0, read);
|
|
223
|
+
let start = 0;
|
|
224
|
+
for (
|
|
225
|
+
let at = chunk.indexOf(NEWLINE);
|
|
226
|
+
at !== -1;
|
|
227
|
+
at = chunk.indexOf(NEWLINE, start)
|
|
228
|
+
) {
|
|
229
|
+
finishLine(chunk.subarray(start, at));
|
|
230
|
+
start = at + 1;
|
|
231
|
+
}
|
|
232
|
+
hold(chunk.subarray(start));
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
// Best effort: what was read stands.
|
|
236
|
+
} finally {
|
|
237
|
+
closeSync(fd);
|
|
238
|
+
}
|
|
239
|
+
finishLine(Buffer.alloc(0));
|
|
240
|
+
return reader.conversation;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// One session line at a time. Without content it keeps only what
|
|
244
|
+
// withoutContent would leave: no message, thinking, tool argument or result.
|
|
245
|
+
class ClaudeSessionReader {
|
|
246
|
+
readonly conversation: Conversation = { input: [], output: [], tools: [] };
|
|
247
|
+
private readonly byId = new Map<string, ToolCall>();
|
|
248
|
+
private current: { id: string | undefined; message: ChatMessage } | undefined;
|
|
249
|
+
|
|
250
|
+
constructor(private readonly content: boolean) {}
|
|
251
|
+
|
|
252
|
+
line(line: string): void {
|
|
253
|
+
if (line.trim() === "") return;
|
|
254
|
+
let entry: unknown;
|
|
255
|
+
try {
|
|
256
|
+
entry = JSON.parse(line);
|
|
257
|
+
} catch {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (!isRecord(entry) || !isRecord(entry.message)) return;
|
|
261
|
+
const at = timeOf(entry.timestamp);
|
|
262
|
+
if (entry.type === "assistant") this.assistant(entry.message, at);
|
|
263
|
+
else if (entry.type === "user") this.user(entry.message, at);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private assistant(message: Record<string, unknown>, at?: number): void {
|
|
267
|
+
const conversation = this.conversation;
|
|
268
|
+
widen(conversation, at);
|
|
269
|
+
const id = nonEmpty(message.id);
|
|
270
|
+
if (
|
|
271
|
+
this.current === undefined ||
|
|
272
|
+
id === undefined ||
|
|
273
|
+
this.current.id !== id
|
|
274
|
+
) {
|
|
275
|
+
this.current = { id, message: { role: "assistant", parts: [] } };
|
|
276
|
+
if (this.content) conversation.output.push(this.current.message);
|
|
277
|
+
}
|
|
278
|
+
const current = this.current.message;
|
|
279
|
+
const model = nonEmpty(message.model);
|
|
280
|
+
if (model !== undefined) conversation.responseModel = model;
|
|
281
|
+
const stop = nonEmpty(message.stop_reason);
|
|
282
|
+
if (stop !== undefined) {
|
|
283
|
+
current.finish_reason = stop;
|
|
284
|
+
conversation.finishReason = stop;
|
|
285
|
+
}
|
|
286
|
+
for (const block of Array.isArray(message.content)
|
|
287
|
+
? message.content.filter(isRecord)
|
|
288
|
+
: []) {
|
|
289
|
+
const part = claudePart(block);
|
|
290
|
+
if (part === undefined) continue;
|
|
291
|
+
if (this.content) current.parts.push(part);
|
|
292
|
+
if (part.type === "tool_call") {
|
|
293
|
+
const call: ToolCall = {
|
|
294
|
+
...(part.id === undefined ? {} : { id: part.id }),
|
|
295
|
+
name: part.name,
|
|
296
|
+
...(part.arguments === undefined || !this.content
|
|
297
|
+
? {}
|
|
298
|
+
: { arguments: part.arguments }),
|
|
299
|
+
...(at === undefined ? {} : { startMs: at }),
|
|
300
|
+
};
|
|
301
|
+
conversation.tools.push(call);
|
|
302
|
+
if (part.id !== undefined) this.byId.set(part.id, call);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private user(message: Record<string, unknown>, at?: number): void {
|
|
308
|
+
const conversation = this.conversation;
|
|
309
|
+
widen(conversation, at);
|
|
310
|
+
this.current = undefined;
|
|
311
|
+
const content = message.content;
|
|
312
|
+
if (typeof content === "string") {
|
|
313
|
+
if (this.content && conversation.output.length === 0) {
|
|
314
|
+
conversation.input.push(userText(content));
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if (!Array.isArray(content)) return;
|
|
319
|
+
for (const block of content.filter(isRecord)) {
|
|
320
|
+
if (block.type === "tool_result") {
|
|
321
|
+
const call = this.byId.get(String(block.tool_use_id));
|
|
322
|
+
if (call === undefined) continue;
|
|
323
|
+
if (this.content) {
|
|
324
|
+
const result = resultText(block.content);
|
|
325
|
+
if (result !== undefined) call.result = result;
|
|
326
|
+
}
|
|
327
|
+
if (block.is_error === true) call.isError = true;
|
|
328
|
+
if (at !== undefined) call.endMs = at;
|
|
329
|
+
} else if (
|
|
330
|
+
this.content &&
|
|
331
|
+
block.type === "text" &&
|
|
332
|
+
typeof block.text === "string" &&
|
|
333
|
+
conversation.output.length === 0
|
|
334
|
+
) {
|
|
335
|
+
conversation.input.push(userText(block.text));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function userText(content: string): ChatMessage {
|
|
342
|
+
return { role: "user", parts: [{ type: "text", content }] };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function claudePart(block: Record<string, unknown>): MessagePart | undefined {
|
|
346
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
347
|
+
return { type: "text", content: block.text };
|
|
348
|
+
}
|
|
349
|
+
// The CLI can record a thinking block with its text withheld; an empty one
|
|
350
|
+
// says nothing and is left out.
|
|
351
|
+
if (block.type === "thinking" && nonEmpty(block.thinking) !== undefined) {
|
|
352
|
+
return { type: "reasoning", content: block.thinking as string };
|
|
353
|
+
}
|
|
354
|
+
if (block.type === "tool_use" && typeof block.name === "string") {
|
|
355
|
+
return {
|
|
356
|
+
type: "tool_call",
|
|
357
|
+
...(nonEmpty(block.id) === undefined ? {} : { id: block.id as string }),
|
|
358
|
+
name: block.name,
|
|
359
|
+
...(block.input === undefined ? {} : { arguments: block.input }),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// The capture's newest readback: the session's messages as OpenCode returned
|
|
366
|
+
// them, `{ info: { role, modelID, finish, time }, parts: [...] }` each. An
|
|
367
|
+
// absent or elided readback leaves the conversation empty. The capture comes
|
|
368
|
+
// parsed: the trace builder parses each one once, for its conversation and its
|
|
369
|
+
// redacted transcript.
|
|
370
|
+
export function openCodeConversation(capture: unknown): Conversation {
|
|
371
|
+
const conversation: Conversation = { input: [], output: [], tools: [] };
|
|
372
|
+
const records =
|
|
373
|
+
isRecord(capture) && Array.isArray(capture.records)
|
|
374
|
+
? capture.records.filter(isRecord)
|
|
375
|
+
: [];
|
|
376
|
+
for (const record of records) widen(conversation, timeOf(record.observedMs));
|
|
377
|
+
const readback = records
|
|
378
|
+
.filter(
|
|
379
|
+
(record) =>
|
|
380
|
+
record.kind === "readback" &&
|
|
381
|
+
isRecord(record.data) &&
|
|
382
|
+
Array.isArray(record.data.messages),
|
|
383
|
+
)
|
|
384
|
+
.at(-1);
|
|
385
|
+
const messages =
|
|
386
|
+
readback !== undefined && isRecord(readback.data)
|
|
387
|
+
? (readback.data.messages as unknown[]).filter(isRecord)
|
|
388
|
+
: [];
|
|
389
|
+
for (const item of messages) {
|
|
390
|
+
const info = isRecord(item.info) ? item.info : item;
|
|
391
|
+
const parts = Array.isArray(item.parts) ? item.parts.filter(isRecord) : [];
|
|
392
|
+
if (info.role === "user") {
|
|
393
|
+
const texts = parts
|
|
394
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
395
|
+
.map((part) => ({ type: "text" as const, content: String(part.text) }));
|
|
396
|
+
if (texts.length > 0)
|
|
397
|
+
conversation.input.push({ role: "user", parts: texts });
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (info.role !== "assistant") continue;
|
|
401
|
+
const message: ChatMessage = { role: "assistant", parts: [] };
|
|
402
|
+
const turn = openCodeTurn(info);
|
|
403
|
+
const turnIndex = conversation.turns?.length ?? 0;
|
|
404
|
+
conversation.turns = [...(conversation.turns ?? []), turn];
|
|
405
|
+
const model = nonEmpty(info.modelID);
|
|
406
|
+
if (model !== undefined) conversation.responseModel = model;
|
|
407
|
+
const finish = nonEmpty(info.finish);
|
|
408
|
+
if (finish !== undefined) {
|
|
409
|
+
message.finish_reason = finish;
|
|
410
|
+
conversation.finishReason = finish;
|
|
411
|
+
}
|
|
412
|
+
for (const part of parts) {
|
|
413
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
414
|
+
message.parts.push({ type: "text", content: part.text });
|
|
415
|
+
} else if (part.type === "reasoning" && nonEmpty(part.text)) {
|
|
416
|
+
message.parts.push({ type: "reasoning", content: String(part.text) });
|
|
417
|
+
} else if (part.type === "tool" && typeof part.tool === "string") {
|
|
418
|
+
const state = isRecord(part.state) ? part.state : {};
|
|
419
|
+
const time = isRecord(state.time) ? state.time : {};
|
|
420
|
+
const id = nonEmpty(part.callID);
|
|
421
|
+
const call: ToolCall = {
|
|
422
|
+
...(id === undefined ? {} : { id }),
|
|
423
|
+
name: part.tool,
|
|
424
|
+
...(state.input === undefined ? {} : { arguments: state.input }),
|
|
425
|
+
turn: turnIndex,
|
|
426
|
+
};
|
|
427
|
+
const result = nonEmpty(state.output) ?? nonEmpty(state.error);
|
|
428
|
+
if (result !== undefined) call.result = result;
|
|
429
|
+
if (state.status === "error") call.isError = true;
|
|
430
|
+
const start = timeOf(time.start);
|
|
431
|
+
const end = timeOf(time.end);
|
|
432
|
+
if (start !== undefined) call.startMs = start;
|
|
433
|
+
if (end !== undefined) call.endMs = end;
|
|
434
|
+
conversation.tools.push(call);
|
|
435
|
+
message.parts.push({
|
|
436
|
+
type: "tool_call",
|
|
437
|
+
...(id === undefined ? {} : { id }),
|
|
438
|
+
name: part.tool,
|
|
439
|
+
...(call.arguments === undefined
|
|
440
|
+
? {}
|
|
441
|
+
: { arguments: call.arguments }),
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
conversation.output.push(message);
|
|
446
|
+
if (message.parts.length > 0) turn.message = message;
|
|
447
|
+
}
|
|
448
|
+
return conversation;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function count(value: unknown): number | undefined {
|
|
452
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0
|
|
453
|
+
? value
|
|
454
|
+
: undefined;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// An assistant message's own record: `time.created`/`completed` in epoch ms,
|
|
458
|
+
// `tokens` {input, output, reasoning, cache: {read, write}} and `cost` in USD.
|
|
459
|
+
// A message still being written has no `completed` time.
|
|
460
|
+
function openCodeTurn(info: Record<string, unknown>): Turn {
|
|
461
|
+
const time = isRecord(info.time) ? info.time : {};
|
|
462
|
+
const tokens = isRecord(info.tokens) ? info.tokens : {};
|
|
463
|
+
const cache = isRecord(tokens.cache) ? tokens.cache : {};
|
|
464
|
+
const leaves: Array<[keyof Turn, unknown]> = [
|
|
465
|
+
["startMs", timeOf(time.created)],
|
|
466
|
+
["endMs", timeOf(time.completed)],
|
|
467
|
+
["model", nonEmpty(info.modelID)],
|
|
468
|
+
["finishReason", nonEmpty(info.finish)],
|
|
469
|
+
["inputTokens", count(tokens.input)],
|
|
470
|
+
["outputTokens", count(tokens.output)],
|
|
471
|
+
["reasoningTokens", count(tokens.reasoning)],
|
|
472
|
+
["cacheReadTokens", count(cache.read)],
|
|
473
|
+
["cacheWriteTokens", count(cache.write)],
|
|
474
|
+
["costUsd", count(info.cost)],
|
|
475
|
+
];
|
|
476
|
+
const turn: Record<string, unknown> = {};
|
|
477
|
+
for (const [key, value] of leaves) if (value !== undefined) turn[key] = value;
|
|
478
|
+
if (info.error !== undefined && info.error !== null) turn.isError = true;
|
|
479
|
+
return turn as Turn;
|
|
480
|
+
}
|