Skip to content

Commit 95ea01f

Browse files
cteclaude
andauthored
feat(cli): support images in stdin stream commands (#11831)
feat(cli): support images in stdin stream start and message commands Add optional `images` field (array of base64 data URIs) to the `start` and `message` CLI stdin stream commands, allowing callers to attach images to prompts. The images are validated, forwarded through the extension host, and included in queued messages. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5a4ab2b commit 95ea01f

File tree

7 files changed

+238
-6
lines changed

7 files changed

+238
-6
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { runStreamCase, StreamEvent } from "../lib/stream-harness"
2+
3+
const LONG_PROMPT =
4+
'Run exactly this command and do not summarize until it finishes: sleep 20 && echo "done". After it finishes, reply with exactly "done".'
5+
6+
async function main() {
7+
const startRequestId = `start-${Date.now()}`
8+
const messageRequestId = `message-${Date.now()}`
9+
const shutdownRequestId = `shutdown-${Date.now()}`
10+
const testImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
11+
12+
let initSeen = false
13+
let startAccepted = false
14+
let messageAccepted = false
15+
let messageQueued = false
16+
let queueImageCountObserved = false
17+
let shutdownSent = false
18+
let shutdownAck = false
19+
let shutdownDone = false
20+
21+
await runStreamCase({
22+
timeoutMs: 180_000,
23+
onEvent(event: StreamEvent, context) {
24+
if (event.type === "system" && event.subtype === "init" && !initSeen) {
25+
initSeen = true
26+
context.sendCommand({ command: "start", requestId: startRequestId, prompt: LONG_PROMPT })
27+
return
28+
}
29+
30+
if (
31+
event.type === "control" &&
32+
event.subtype === "ack" &&
33+
event.command === "start" &&
34+
event.requestId === startRequestId &&
35+
!startAccepted
36+
) {
37+
startAccepted = true
38+
39+
context.sendCommand({
40+
command: "message",
41+
requestId: messageRequestId,
42+
prompt: "Respond with exactly IMAGE-QUEUED when this message is processed.",
43+
images: [testImage],
44+
})
45+
46+
return
47+
}
48+
49+
if (
50+
event.type === "control" &&
51+
event.subtype === "ack" &&
52+
event.command === "message" &&
53+
event.requestId === messageRequestId
54+
) {
55+
messageAccepted = true
56+
return
57+
}
58+
59+
if (
60+
event.type === "control" &&
61+
event.subtype === "done" &&
62+
event.command === "message" &&
63+
event.requestId === messageRequestId &&
64+
event.code === "queued"
65+
) {
66+
messageQueued = true
67+
return
68+
}
69+
70+
if (
71+
event.type === "queue" &&
72+
(event.subtype === "snapshot" || event.subtype === "enqueued" || event.subtype === "updated") &&
73+
Array.isArray(event.queue) &&
74+
event.queue.some((item) => item?.imageCount === 1)
75+
) {
76+
queueImageCountObserved = true
77+
78+
if (!shutdownSent) {
79+
context.sendCommand({ command: "shutdown", requestId: shutdownRequestId })
80+
shutdownSent = true
81+
}
82+
83+
return
84+
}
85+
86+
if (
87+
event.type === "control" &&
88+
event.subtype === "ack" &&
89+
event.command === "shutdown" &&
90+
event.requestId === shutdownRequestId
91+
) {
92+
shutdownAck = true
93+
return
94+
}
95+
96+
if (
97+
event.type === "control" &&
98+
event.subtype === "done" &&
99+
event.command === "shutdown" &&
100+
event.requestId === shutdownRequestId
101+
) {
102+
shutdownDone = true
103+
}
104+
},
105+
onTimeoutMessage() {
106+
return `timed out waiting for queue image metadata (initSeen=${initSeen}, startAccepted=${startAccepted}, messageAccepted=${messageAccepted}, messageQueued=${messageQueued}, queueImageCountObserved=${queueImageCountObserved}, shutdownSent=${shutdownSent}, shutdownAck=${shutdownAck}, shutdownDone=${shutdownDone})`
107+
},
108+
})
109+
110+
if (!messageAccepted || !messageQueued || !queueImageCountObserved) {
111+
throw new Error(
112+
`expected queued message with image metadata (messageAccepted=${messageAccepted}, messageQueued=${messageQueued}, queueImageCountObserved=${queueImageCountObserved})`,
113+
)
114+
}
115+
116+
if (!shutdownAck || !shutdownDone) {
117+
throw new Error("shutdown control events were not fully observed")
118+
}
119+
}
120+
121+
main().catch((error) => {
122+
console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`)
123+
process.exit(1)
124+
})

apps/cli/scripts/integration/lib/stream-harness.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type StreamCommand = {
3030
command: "start" | "message" | "cancel" | "ping" | "shutdown"
3131
requestId: string
3232
prompt?: string
33+
images?: string[]
3334
}
3435

3536
export interface StreamCaseContext {

apps/cli/src/agent/extension-host.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ interface WebviewViewProvider {
108108
export interface ExtensionHostInterface extends IExtensionHost<ExtensionHostEventMap> {
109109
client: ExtensionClient
110110
activate(): Promise<void>
111-
runTask(prompt: string, taskId?: string, configuration?: RooCodeSettings): Promise<void>
111+
runTask(prompt: string, taskId?: string, configuration?: RooCodeSettings, images?: string[]): Promise<void>
112112
resumeTask(taskId: string): Promise<void>
113113
sendToExtension(message: WebviewMessage): void
114114
dispose(): Promise<void>
@@ -510,8 +510,19 @@ export class ExtensionHost extends EventEmitter implements ExtensionHostInterfac
510510
})
511511
}
512512

513-
public async runTask(prompt: string, taskId?: string, configuration?: RooCodeSettings): Promise<void> {
514-
this.sendToExtension({ type: "newTask", text: prompt, taskId, taskConfiguration: configuration })
513+
public async runTask(
514+
prompt: string,
515+
taskId?: string,
516+
configuration?: RooCodeSettings,
517+
images?: string[],
518+
): Promise<void> {
519+
this.sendToExtension({
520+
type: "newTask",
521+
text: prompt,
522+
taskId,
523+
taskConfiguration: configuration,
524+
...(images !== undefined ? { images } : {}),
525+
})
515526
return this.waitForTaskCompletion()
516527
}
517528

apps/cli/src/commands/cli/__tests__/parse-stdin-command.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ describe("parseStdinStreamCommand", () => {
1818
expect(result).toEqual({ command: "message", requestId: "req-2", prompt: "follow up" })
1919
})
2020

21+
it("parses start and message images", () => {
22+
const start = parseStdinStreamCommand(
23+
JSON.stringify({
24+
command: "start",
25+
requestId: "req-img-start",
26+
prompt: "hello",
27+
images: ["data:image/jpeg;base64,abc123"],
28+
}),
29+
1,
30+
)
31+
expect(start).toEqual({
32+
command: "start",
33+
requestId: "req-img-start",
34+
prompt: "hello",
35+
images: ["data:image/jpeg;base64,abc123"],
36+
})
37+
38+
const message = parseStdinStreamCommand(
39+
JSON.stringify({
40+
command: "message",
41+
requestId: "req-img-msg",
42+
prompt: "follow up",
43+
images: ["data:image/png;base64,xyz456"],
44+
}),
45+
1,
46+
)
47+
expect(message).toEqual({
48+
command: "message",
49+
requestId: "req-img-msg",
50+
prompt: "follow up",
51+
images: ["data:image/png;base64,xyz456"],
52+
})
53+
})
54+
2155
it.each(["cancel", "ping", "shutdown"] as const)("parses a %s command (no prompt required)", (command) => {
2256
const result = parseStdinStreamCommand(JSON.stringify({ command, requestId: "req-3" }), 1)
2357
expect(result).toEqual({ command, requestId: "req-3" })
@@ -100,5 +134,31 @@ describe("parseStdinStreamCommand", () => {
100134
parseStdinStreamCommand(JSON.stringify({ command: "message", requestId: "req", prompt: " " }), 1),
101135
).toThrow('"message" requires non-empty string "prompt"')
102136
})
137+
138+
it("throws when start or message images are not string arrays", () => {
139+
expect(() =>
140+
parseStdinStreamCommand(
141+
JSON.stringify({
142+
command: "start",
143+
requestId: "req-start-img",
144+
prompt: "hello",
145+
images: "not-an-array",
146+
}),
147+
1,
148+
),
149+
).toThrow('"start" images must be an array of strings')
150+
151+
expect(() =>
152+
parseStdinStreamCommand(
153+
JSON.stringify({
154+
command: "message",
155+
requestId: "req-msg-img",
156+
prompt: "follow up",
157+
images: ["ok", 123],
158+
}),
159+
1,
160+
),
161+
).toThrow('"message" images must be an array of strings')
162+
})
103163
})
104164
})

apps/cli/src/commands/cli/stdin-stream.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,38 @@ export function parseStdinStreamCommand(line: string, lineNumber: number): Stdin
6363

6464
if (command === "start" || command === "message") {
6565
const promptRaw = parsed.prompt
66+
6667
if (typeof promptRaw !== "string" || promptRaw.trim().length === 0) {
6768
throw new Error(`stdin command line ${lineNumber}: "${command}" requires non-empty string "prompt"`)
6869
}
6970

71+
const imagesRaw = parsed.images
72+
let images: string[] | undefined
73+
74+
if (imagesRaw !== undefined) {
75+
if (!Array.isArray(imagesRaw) || !imagesRaw.every((image) => typeof image === "string")) {
76+
throw new Error(`stdin command line ${lineNumber}: "${command}" images must be an array of strings`)
77+
}
78+
79+
images = imagesRaw
80+
}
81+
7082
if (command === "start" && isRecord(parsed.configuration)) {
7183
return {
7284
command,
7385
requestId,
7486
prompt: promptRaw,
87+
...(images !== undefined ? { images } : {}),
7588
configuration: parsed.configuration as RooCliStartCommand["configuration"],
7689
}
7790
}
7891

79-
return { command, requestId, prompt: promptRaw }
92+
return {
93+
command,
94+
requestId,
95+
prompt: promptRaw,
96+
...(images !== undefined ? { images } : {}),
97+
}
8098
}
8199

82100
return { command, requestId }
@@ -601,7 +619,7 @@ export async function runStdinStreamMode({ host, jsonEmitter, setStreamRequestId
601619
}
602620

603621
activeTaskPromise = host
604-
.runTask(stdinCommand.prompt, latestTaskId, taskConfiguration)
622+
.runTask(stdinCommand.prompt, latestTaskId, taskConfiguration, stdinCommand.images)
605623
.catch((error) => {
606624
const message = error instanceof Error ? error.message : String(error)
607625

@@ -691,7 +709,11 @@ export async function runStdinStreamMode({ host, jsonEmitter, setStreamRequestId
691709
success: true,
692710
})
693711

694-
host.sendToExtension({ type: "queueMessage", text: stdinCommand.prompt })
712+
host.sendToExtension({
713+
type: "queueMessage",
714+
text: stdinCommand.prompt,
715+
images: stdinCommand.images,
716+
})
695717
pendingQueuedMessageRequestIds.push(stdinCommand.requestId)
696718
if (host.isWaitingForInput()) {
697719
setStreamRequestId(stdinCommand.requestId)

packages/types/src/__tests__/cli.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ describe("CLI types", () => {
1212
command: "start",
1313
requestId: "req-1",
1414
prompt: "hello",
15+
images: ["data:image/png;base64,abc"],
1516
configuration: {},
1617
})
1718

1819
expect(result.success).toBe(true)
1920
})
2021

22+
it("validates a message command with images", () => {
23+
const result = rooCliInputCommandSchema.safeParse({
24+
command: "message",
25+
requestId: "req-2a",
26+
prompt: "follow up",
27+
images: ["data:image/png;base64,xyz"],
28+
})
29+
30+
expect(result.success).toBe(true)
31+
})
32+
2133
it("rejects a message command without prompt", () => {
2234
const result = rooCliInputCommandSchema.safeParse({
2335
command: "message",

packages/types/src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type RooCliCommandBase = z.infer<typeof rooCliCommandBaseSchema>
2222
export const rooCliStartCommandSchema = rooCliCommandBaseSchema.extend({
2323
command: z.literal("start"),
2424
prompt: z.string(),
25+
images: z.array(z.string()).optional(),
2526
configuration: rooCodeSettingsSchema.optional(),
2627
})
2728

@@ -30,6 +31,7 @@ export type RooCliStartCommand = z.infer<typeof rooCliStartCommandSchema>
3031
export const rooCliMessageCommandSchema = rooCliCommandBaseSchema.extend({
3132
command: z.literal("message"),
3233
prompt: z.string(),
34+
images: z.array(z.string()).optional(),
3335
})
3436

3537
export type RooCliMessageCommand = z.infer<typeof rooCliMessageCommandSchema>

0 commit comments

Comments
 (0)