(line: string, lineNumber: number)
| 30 | export const VALID_STDIN_COMMANDS = new Set<StdinStreamCommandName>(rooCliCommandNames) |
| 31 | |
| 32 | export function parseStdinStreamCommand(line: string, lineNumber: number): StdinStreamCommand { |
| 33 | let parsed: unknown |
| 34 | |
| 35 | try { |
| 36 | parsed = JSON.parse(line) |
| 37 | } catch { |
| 38 | throw new Error(`stdin command line ${lineNumber}: invalid JSON`) |
| 39 | } |
| 40 | |
| 41 | if (!isRecord(parsed)) { |
| 42 | throw new Error(`stdin command line ${lineNumber}: expected JSON object`) |
| 43 | } |
| 44 | |
| 45 | const commandRaw = parsed.command |
| 46 | const requestIdRaw = parsed.requestId |
| 47 | |
| 48 | if (typeof commandRaw !== "string") { |
| 49 | throw new Error(`stdin command line ${lineNumber}: missing string "command"`) |
| 50 | } |
| 51 | |
| 52 | if (!VALID_STDIN_COMMANDS.has(commandRaw as StdinStreamCommandName)) { |
| 53 | throw new Error( |
| 54 | `stdin command line ${lineNumber}: unsupported command "${commandRaw}" (expected start|message|cancel|ping|shutdown)`, |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | if (typeof requestIdRaw !== "string" || requestIdRaw.trim().length === 0) { |
| 59 | throw new Error(`stdin command line ${lineNumber}: missing non-empty string "requestId"`) |
| 60 | } |
| 61 | |
| 62 | const command = commandRaw as StdinStreamCommandName |
| 63 | const requestId = requestIdRaw.trim() |
| 64 | |
| 65 | if (command === "start" || command === "message") { |
| 66 | const promptRaw = parsed.prompt |
| 67 | |
| 68 | if (typeof promptRaw !== "string" || promptRaw.trim().length === 0) { |
| 69 | throw new Error(`stdin command line ${lineNumber}: "${command}" requires non-empty string "prompt"`) |
| 70 | } |
| 71 | |
| 72 | const imagesRaw = parsed.images |
| 73 | let images: string[] | undefined |
| 74 | |
| 75 | if (imagesRaw !== undefined) { |
| 76 | if (!Array.isArray(imagesRaw) || !imagesRaw.every((image) => typeof image === "string")) { |
| 77 | throw new Error(`stdin command line ${lineNumber}: "${command}" images must be an array of strings`) |
| 78 | } |
| 79 | |
| 80 | images = imagesRaw |
| 81 | } |
| 82 | |
| 83 | if (command === "start") { |
| 84 | const taskIdRaw = parsed.taskId |
| 85 | let taskId: string | undefined |
| 86 | |
| 87 | if (taskIdRaw !== undefined) { |
| 88 | if (typeof taskIdRaw !== "string" || taskIdRaw.trim().length === 0) { |
| 89 | throw new Error(`stdin command line ${lineNumber}: "start" taskId must be a non-empty string`) |
no test coverage detected