Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate images via OpenAI, Google, OpenRouter, DashScope, Jimeng, Seedream, and Replicate APIs with batch support.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/codex-imagegen/parser.ts
1import type { CodexRunResult, ToolCall, TokenUsage } from "./types.ts";23export function parseEventStream(raw: string): Omit<CodexRunResult, "rawLogPath" | "durationMs"> {4const lines = raw.split("\n").filter((l) => l.trim().length > 0);5let threadId: string | null = null;6let agentMessage: string | null = null;7let usage: TokenUsage | null = null;8const toolCallsById = new Map<string, ToolCall>();910for (const line of lines) {11let event: any;12try {13event = JSON.parse(line);14} catch {15continue;16}17const type = event?.type;18if (type === "thread.started") {19threadId = event.thread_id ?? null;20} else if (type === "item.started" || type === "item.completed") {21const item = event.item;22if (!item?.id) continue;23const tc: ToolCall = {24id: item.id,25tool: deriveToolName(item),26status: item.status ?? (type === "item.completed" ? "completed" : "in_progress"),27command: item.command,28};29toolCallsById.set(item.id, tc);30if (item.type === "agent_message" && type === "item.completed") {31agentMessage = String(item.text ?? "");32}33} else if (type === "turn.completed") {34const u = event.usage;35if (u) {36usage = {37input: u.input_tokens ?? 0,38cached_input: u.cached_input_tokens ?? 0,39output: u.output_tokens ?? 0,40reasoning: u.reasoning_output_tokens ?? 0,41};42}43}44}4546return {47threadId,48toolCalls: Array.from(toolCallsById.values()),49agentMessage,50usage,51};52}5354function deriveToolName(item: any): string {55if (item.type === "command_execution") return "shell";56if (item.type === "agent_message") return "agent_message";57if (item.type === "image_gen" || item.type === "image_generation") return "image_gen";58if (typeof item.tool === "string") return item.tool;59return item.type ?? "unknown";60}6162export function hasImageGenInvocation(toolCalls: ToolCall[]): boolean {63return toolCalls.some((tc) => tc.tool === "image_gen");64}65