-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex.ts
More file actions
333 lines (297 loc) · 8.66 KB
/
Copy pathcodex.ts
File metadata and controls
333 lines (297 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { normalizeToolName } from "../tools.js"
import type { NormalizedEvent, NormalizeOptions } from "../types.js"
export interface CodexEntry {
timestamp?: string
type: string
payload?: Record<string, unknown>
}
function parseTimestamp(entry: CodexEntry): number {
if (entry.timestamp) {
const ms = Date.parse(entry.timestamp)
if (Number.isFinite(ms)) return ms
}
return Date.now()
}
function findLastCompactionIndex(entries: Array<CodexEntry>): number {
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i]!
if (entry.type === `compacted`) {
return i
}
}
return 0
}
function parseArguments(args: unknown): Record<string, unknown> {
if (typeof args === `string`) {
try {
return JSON.parse(args) as Record<string, unknown>
} catch {
return { raw: args }
}
}
if (args && typeof args === `object`) {
return args as Record<string, unknown>
}
return {}
}
/**
* Normalize a single Codex session entry — one parsed JSONL line (the
* `rollout-*.jsonl` format), or one event from `@openai/codex-sdk`
* (the SDK and the JSONL share a shape: each JSONL line is a
* serialized agent-loop event).
*
* Returns zero or more `NormalizedEvent`s. The function is pure: no
* cross-event state is carried, so callers driving a live SDK stream
* can iterate the SDK's async iterator and feed each event in
* directly. The "filter from the last compaction" behaviour of the
* batch API lives in `normalizeCodex`, which composes this function
* over a parsed file.
*/
export function normalizeCodexEvent(
entry: CodexEntry
): Array<NormalizedEvent> {
const ts = parseTimestamp(entry)
const payload = entry.payload ?? {}
const out: Array<NormalizedEvent> = []
if (entry.type === `session_meta`) {
const git = payload.git as Record<string, unknown> | null | undefined
out.push({
v: 1,
ts,
type: `session_init`,
sessionId: String(payload.id ?? ``),
cwd: String(payload.cwd ?? ``),
model: undefined,
agent: `codex`,
agentVersion: payload.cli_version
? String(payload.cli_version)
: undefined,
git: git
? {
branch: git.branch ? String(git.branch) : undefined,
commit: git.commit_hash ? String(git.commit_hash) : undefined,
remote: git.repository_url
? String(git.repository_url)
: undefined,
}
: undefined,
})
return out
}
if (entry.type === `compacted`) {
out.push({ v: 1, ts, type: `compaction` })
return out
}
if (entry.type === `event_msg`) {
const msgType = payload.type as string | undefined
if (msgType === `turn_aborted`) {
out.push({
v: 1,
ts,
type: `turn_aborted`,
reason: String(payload.reason ?? `interrupted`),
})
return out
}
if (msgType === `context_compacted`) {
out.push({ v: 1, ts, type: `compaction` })
return out
}
// skip: token_count, agent_reasoning, agent_message, user_message (mirrors)
return out
}
if (entry.type === `response_item`) {
const itemType = payload.type as string | undefined
if (itemType === `message`) {
const role = payload.role as string | undefined
const content = payload.content as
| Array<Record<string, unknown>>
| undefined
if (role === `user`) {
const text = content
?.filter((c) => typeof c.text === `string`)
.map((c) => c.text as string)
.join(`\n`)
if (text) {
out.push({ v: 1, ts, type: `user_message`, text })
}
return out
}
if (role === `assistant`) {
const text = content
?.filter((c) => typeof c.text === `string`)
.map((c) => c.text as string)
.join(`\n`)
if (text) {
out.push({
v: 1,
ts,
type: `assistant_message`,
text,
phase:
payload.phase === `commentary`
? `commentary`
: payload.phase === `final_answer`
? `final`
: undefined,
})
}
return out
}
// skip developer messages (system instructions)
return out
}
if (itemType === `function_call`) {
const args = parseArguments(payload.arguments)
const mapping = normalizeToolName(
String(payload.name ?? ``),
`codex`,
args
)
out.push({
v: 1,
ts,
type: `tool_call`,
callId: String(payload.call_id ?? ``),
tool: mapping.normalized,
originalTool: mapping.originalTool,
originalAgent: `codex`,
input: args,
})
return out
}
if (itemType === `function_call_output`) {
out.push({
v: 1,
ts,
type: `tool_result`,
callId: String(payload.call_id ?? ``),
output: String(payload.output ?? ``),
isError: false,
})
return out
}
if (itemType === `custom_tool_call`) {
const mapping = normalizeToolName(String(payload.name ?? ``), `codex`, {
input: payload.input,
})
out.push({
v: 1,
ts,
type: `tool_call`,
callId: String(payload.call_id ?? ``),
tool: mapping.normalized,
originalTool: mapping.originalTool,
originalAgent: `codex`,
input:
typeof payload.input === `string`
? { raw: payload.input }
: ((payload.input as Record<string, unknown>) ?? {}),
})
return out
}
if (itemType === `custom_tool_call_output`) {
let output = String(payload.output ?? ``)
let isError = false
try {
const parsed = JSON.parse(output) as Record<string, unknown>
if (typeof parsed.output === `string`) {
output = parsed.output
}
const meta = parsed.metadata as Record<string, unknown> | undefined
if (
meta &&
typeof meta.exit_code === `number` &&
meta.exit_code !== 0
) {
isError = true
}
} catch {
// use raw output
}
out.push({
v: 1,
ts,
type: `tool_result`,
callId: String(payload.call_id ?? ``),
output,
isError,
})
return out
}
if (itemType === `reasoning`) {
const summaryArr = payload.summary as
| Array<Record<string, unknown>>
| undefined
const summaryText =
summaryArr
?.map((s) => (typeof s.text === `string` ? s.text : ``))
.filter(Boolean)
.join(` `) ?? `(thinking)`
out.push({
v: 1,
ts,
type: `thinking`,
summary: summaryText || `(thinking)`,
text: null,
})
return out
}
if (itemType === `web_search_call`) {
const action = payload.action as Record<string, unknown> | undefined
const mapping = normalizeToolName(`web_search`, `codex`, {
action,
})
// The original batch normaliser used the iteration index to
// synthesize a unique callId here. For the per-event API we
// derive a deterministic id from the payload + ts so consecutive
// searches don't collide. (Codex's response_item for
// web_search_call doesn't carry a `call_id` of its own.)
const callId = (() => {
const explicit = payload.call_id ?? payload.id
if (typeof explicit === `string` && explicit) return explicit
const url =
typeof action?.url === `string` ? (action.url as string) : ``
return `web-${ts}-${url.slice(0, 32)}`
})()
out.push({
v: 1,
ts,
type: `tool_call`,
callId,
tool: mapping.normalized,
originalTool: `web_search`,
originalAgent: `codex`,
input: action ? { url: action.url } : {},
})
return out
}
// skip other response_item types
return out
}
// skip: turn_context, etc.
return out
}
export function normalizeCodex(
lines: Array<string>,
options: NormalizeOptions = {}
): Array<NormalizedEvent> {
const { fromCompaction = true } = options
const entries: Array<CodexEntry> = []
for (const line of lines) {
const trimmed = line.trim()
if (!trimmed) continue
try {
entries.push(JSON.parse(trimmed) as CodexEntry)
} catch {
// skip malformed lines
}
}
const startIndex = fromCompaction ? findLastCompactionIndex(entries) : 0
const events: Array<NormalizedEvent> = []
for (let i = startIndex; i < entries.length; i++) {
const entry = entries[i]!
for (const ev of normalizeCodexEvent(entry)) events.push(ev)
}
return events
}