Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit b55409c

Browse files
authored
Add cli types (#11781)
1 parent 50c8101 commit b55409c

6 files changed

Lines changed: 291 additions & 62 deletions

File tree

apps/cli/src/agent/json-event-emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class JsonEventEmitter {
156156
emitControl(event: {
157157
subtype: "ack" | "done" | "error"
158158
requestId?: string
159-
command?: string
159+
command?: JsonEvent["command"]
160160
taskId?: string
161161
content?: string
162162
success?: boolean

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { createInterface } from "readline"
22
import { randomUUID } from "crypto"
33

4-
import type { RooCodeSettings } from "@roo-code/types"
4+
import {
5+
rooCliCommandNames,
6+
type RooCliCommandName,
7+
type RooCliInputCommand,
8+
type RooCliStartCommand,
9+
} from "@roo-code/types"
510

611
import { isRecord } from "@/lib/utils/guards.js"
712

@@ -12,20 +17,15 @@ import type { JsonEventEmitter } from "@/agent/json-event-emitter.js"
1217
// Types
1318
// ---------------------------------------------------------------------------
1419

15-
export type StdinStreamCommandName = "start" | "message" | "cancel" | "ping" | "shutdown"
20+
export type StdinStreamCommandName = RooCliCommandName
1621

17-
export type StdinStreamCommand =
18-
| { command: "start"; requestId: string; prompt: string; configuration?: RooCodeSettings }
19-
| { command: "message"; requestId: string; prompt: string }
20-
| { command: "cancel"; requestId: string }
21-
| { command: "ping"; requestId: string }
22-
| { command: "shutdown"; requestId: string }
22+
export type StdinStreamCommand = RooCliInputCommand
2323

2424
// ---------------------------------------------------------------------------
2525
// Parsing
2626
// ---------------------------------------------------------------------------
2727

28-
export const VALID_STDIN_COMMANDS = new Set<StdinStreamCommandName>(["start", "message", "cancel", "ping", "shutdown"])
28+
export const VALID_STDIN_COMMANDS = new Set<StdinStreamCommandName>(rooCliCommandNames)
2929

3030
export function parseStdinStreamCommand(line: string, lineNumber: number): StdinStreamCommand {
3131
let parsed: unknown
@@ -67,7 +67,12 @@ export function parseStdinStreamCommand(line: string, lineNumber: number): Stdin
6767
}
6868

6969
if (command === "start" && isRecord(parsed.configuration)) {
70-
return { command, requestId, prompt: promptRaw, configuration: parsed.configuration as RooCodeSettings }
70+
return {
71+
command,
72+
requestId,
73+
prompt: promptRaw,
74+
configuration: parsed.configuration as RooCliStartCommand["configuration"],
75+
}
7176
}
7277

7378
return { command, requestId, prompt: promptRaw }

apps/cli/src/types/json-events.ts

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
import {
2+
rooCliOutputFormats,
3+
type RooCliCost,
4+
type RooCliEventType,
5+
type RooCliFinalOutput,
6+
type RooCliOutputFormat,
7+
type RooCliQueueItem,
8+
type RooCliStreamEvent,
9+
type RooCliToolResult,
10+
type RooCliToolUse,
11+
} from "@roo-code/types"
12+
113
/**
214
* JSON Event Types for Structured CLI Output
315
*
@@ -14,9 +26,9 @@
1426
/**
1527
* Output format options for the CLI.
1628
*/
17-
export const OUTPUT_FORMATS = ["text", "json", "stream-json"] as const
29+
export const OUTPUT_FORMATS = rooCliOutputFormats
1830

19-
export type OutputFormat = (typeof OUTPUT_FORMATS)[number]
31+
export type OutputFormat = RooCliOutputFormat
2032

2133
export function isValidOutputFormat(format: string): format is OutputFormat {
2234
return (OUTPUT_FORMATS as readonly string[]).includes(format)
@@ -25,66 +37,24 @@ export function isValidOutputFormat(format: string): format is OutputFormat {
2537
/**
2638
* Event type discriminators for JSON output.
2739
*/
28-
export type JsonEventType =
29-
| "system" // System messages (init, ready, shutdown)
30-
| "control" // Transport/control protocol events
31-
| "queue" // Message queue telemetry from extension state
32-
| "assistant" // Assistant text messages
33-
| "user" // User messages (echoed input)
34-
| "tool_use" // Tool invocations (file ops, commands, browser, MCP)
35-
| "tool_result" // Results from tool execution
36-
| "thinking" // Reasoning/thinking content
37-
| "error" // Errors
38-
| "result" // Final task result
40+
export type JsonEventType = RooCliEventType
3941

40-
export interface JsonEventQueueItem {
41-
/** Queue item id generated by MessageQueueService */
42-
id: string
43-
/** Queued text prompt preview */
44-
text?: string
45-
/** Number of attached images in the queued message */
46-
imageCount?: number
47-
/** Queue insertion/update timestamp (ms epoch) */
48-
timestamp?: number
49-
}
42+
export type JsonEventQueueItem = RooCliQueueItem
5043

5144
/**
5245
* Tool use information for tool_use events.
5346
*/
54-
export interface JsonEventToolUse {
55-
/** Tool name (e.g., "read_file", "write_to_file", "execute_command") */
56-
name: string
57-
/** Tool input parameters */
58-
input?: Record<string, unknown>
59-
}
47+
export type JsonEventToolUse = RooCliToolUse
6048

6149
/**
6250
* Tool result information for tool_result events.
6351
*/
64-
export interface JsonEventToolResult {
65-
/** Tool name that produced this result */
66-
name: string
67-
/** Tool output (for successful execution) */
68-
output?: string
69-
/** Error message (for failed execution) */
70-
error?: string
71-
}
52+
export type JsonEventToolResult = RooCliToolResult
7253

7354
/**
7455
* Cost and token usage information.
7556
*/
76-
export interface JsonEventCost {
77-
/** Total cost in USD */
78-
totalCost?: number
79-
/** Input tokens used */
80-
inputTokens?: number
81-
/** Output tokens generated */
82-
outputTokens?: number
83-
/** Cache write tokens */
84-
cacheWrites?: number
85-
/** Cache read tokens */
86-
cacheReads?: number
87-
}
57+
export type JsonEventCost = RooCliCost
8858

8959
/**
9060
* Base JSON event structure.
@@ -94,7 +64,7 @@ export interface JsonEventCost {
9464
* - Each delta includes `id` for easy correlation
9565
* - Final message has `done: true`
9666
*/
97-
export interface JsonEvent {
67+
export type JsonEvent = RooCliStreamEvent & {
9868
/** Event type discriminator */
9969
type: JsonEventType
10070
/** Protocol schema version (included on system.init) */
@@ -137,7 +107,7 @@ export interface JsonEvent {
137107
* Final JSON output for "json" mode (single object at end).
138108
* Contains the result and accumulated messages.
139109
*/
140-
export interface JsonFinalOutput {
110+
export type JsonFinalOutput = RooCliFinalOutput & {
141111
/** Final result type */
142112
type: "result"
143113
/** Whether the task succeeded */
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
rooCliControlEventSchema,
3+
rooCliFinalOutputSchema,
4+
rooCliInputCommandSchema,
5+
rooCliStreamEventSchema,
6+
} from "../cli.js"
7+
8+
describe("CLI types", () => {
9+
describe("rooCliInputCommandSchema", () => {
10+
it("validates a start command", () => {
11+
const result = rooCliInputCommandSchema.safeParse({
12+
command: "start",
13+
requestId: "req-1",
14+
prompt: "hello",
15+
configuration: {},
16+
})
17+
18+
expect(result.success).toBe(true)
19+
})
20+
21+
it("rejects a message command without prompt", () => {
22+
const result = rooCliInputCommandSchema.safeParse({
23+
command: "message",
24+
requestId: "req-2",
25+
})
26+
27+
expect(result.success).toBe(false)
28+
})
29+
})
30+
31+
describe("rooCliControlEventSchema", () => {
32+
it("validates a control done event", () => {
33+
const result = rooCliControlEventSchema.safeParse({
34+
type: "control",
35+
subtype: "done",
36+
requestId: "req-3",
37+
command: "start",
38+
success: true,
39+
code: "task_completed",
40+
})
41+
42+
expect(result.success).toBe(true)
43+
})
44+
45+
it("rejects control event without requestId", () => {
46+
const result = rooCliControlEventSchema.safeParse({
47+
type: "control",
48+
subtype: "ack",
49+
})
50+
51+
expect(result.success).toBe(false)
52+
})
53+
})
54+
55+
describe("rooCliStreamEventSchema", () => {
56+
it("accepts passthrough fields for forward compatibility", () => {
57+
const result = rooCliStreamEventSchema.safeParse({
58+
type: "assistant",
59+
id: 42,
60+
content: "partial",
61+
customField: "future",
62+
})
63+
64+
expect(result.success).toBe(true)
65+
})
66+
})
67+
68+
describe("rooCliFinalOutputSchema", () => {
69+
it("validates final json output shape", () => {
70+
const result = rooCliFinalOutputSchema.safeParse({
71+
type: "result",
72+
success: true,
73+
content: "done",
74+
events: [],
75+
})
76+
77+
expect(result.success).toBe(true)
78+
})
79+
})
80+
})

0 commit comments

Comments
 (0)