cyrus-gemini-runner 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +411 -0
- package/dist/GeminiRunner.d.ts +136 -0
- package/dist/GeminiRunner.d.ts.map +1 -0
- package/dist/GeminiRunner.js +683 -0
- package/dist/GeminiRunner.js.map +1 -0
- package/dist/SimpleGeminiRunner.d.ts +27 -0
- package/dist/SimpleGeminiRunner.d.ts.map +1 -0
- package/dist/SimpleGeminiRunner.js +149 -0
- package/dist/SimpleGeminiRunner.js.map +1 -0
- package/dist/adapters.d.ts +37 -0
- package/dist/adapters.d.ts.map +1 -0
- package/dist/adapters.js +317 -0
- package/dist/adapters.js.map +1 -0
- package/dist/formatter.d.ts +40 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +363 -0
- package/dist/formatter.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts/system.md +108 -0
- package/dist/schemas.d.ts +1472 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +678 -0
- package/dist/schemas.js.map +1 -0
- package/dist/settingsGenerator.d.ts +72 -0
- package/dist/settingsGenerator.d.ts.map +1 -0
- package/dist/settingsGenerator.js +255 -0
- package/dist/settingsGenerator.js.map +1 -0
- package/dist/systemPromptManager.d.ts +27 -0
- package/dist/systemPromptManager.d.ts.map +1 -0
- package/dist/systemPromptManager.js +65 -0
- package/dist/systemPromptManager.js.map +1 -0
- package/dist/types.d.ts +113 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +23 -0
- package/dist/types.js.map +1 -0
- package/package.json +37 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;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C;AAED,wBAAgB,4BAA4B,CAC3C,KAAK,EAAE,kBAAkB,GACvB,6BAA6B,GAAG,IAAI,CAGtC;AAED,wBAAgB,0BAA0B,CACzC,KAAK,EAAE,kBAAkB,GACvB,2BAA2B,GAAG,IAAI,CAGpC;AAED,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,kBAAkB,GACvB,sBAAsB,GAAG,IAAI,CAG/B;AAED,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,kBAAkB,GACvB,mBAAmB,GAAG,IAAI,CAG5B;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC7B,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,oBAAoB,CAK/B;AAED,wBAAgB,eAAe,CAC9B,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,qBAAqB,CAKhC;AAED,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,yBAAyB,CAKpC;AAED,wBAAgB,uBAAuB,CACtC,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,6BAA6B,CAKxC;AAED,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,2BAA2B,CAKtC;AAED,wBAAgB,gBAAgB,CAC/B,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,sBAAsB,CAKjC;AAED,wBAAgB,aAAa,CAC5B,KAAK,EAAE,kBAAkB,GACvB,KAAK,IAAI,mBAAmB,CAK9B"}
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod Schemas for Gemini CLI Stream Events
|
|
3
|
+
*
|
|
4
|
+
* These schemas provide runtime validation for Gemini CLI's stream-json output format.
|
|
5
|
+
* TypeScript types are derived from these schemas using z.infer<> for type safety.
|
|
6
|
+
*
|
|
7
|
+
* Note: The official `@google/gemini-cli-core` package (v0.17.1) exports TypeScript
|
|
8
|
+
* interfaces for these event types. However, we use custom Zod schemas because:
|
|
9
|
+
* 1. Runtime validation - official types are TypeScript-only, no runtime checks
|
|
10
|
+
* 2. Detailed tool typing - official uses `Record<string, unknown>` for tool params
|
|
11
|
+
* 3. Type guards and parsers - utility functions for narrowing event/tool types
|
|
12
|
+
* 4. Tool result typing - result schemas typed by tool_id prefix
|
|
13
|
+
*
|
|
14
|
+
* Our schemas are structurally compatible with the official types.
|
|
15
|
+
*
|
|
16
|
+
* Official type definitions (pinned to v0.17.0):
|
|
17
|
+
* @see https://github.com/google-gemini/gemini-cli/blob/v0.17.0/packages/core/src/output/types.ts
|
|
18
|
+
* @see https://www.npmjs.com/package/@google/gemini-cli-core/v/0.17.0
|
|
19
|
+
*
|
|
20
|
+
* Documentation:
|
|
21
|
+
* @see https://github.com/google-gemini/gemini-cli/blob/v0.17.0/docs/cli/headless.md
|
|
22
|
+
*/
|
|
23
|
+
import { z } from "zod";
|
|
24
|
+
// ============================================================================
|
|
25
|
+
// Base Schemas
|
|
26
|
+
// ============================================================================
|
|
27
|
+
/**
|
|
28
|
+
* ISO 8601 timestamp string (e.g., "2025-11-25T03:27:51.000Z")
|
|
29
|
+
*/
|
|
30
|
+
const TimestampSchema = z.string().datetime({ offset: true });
|
|
31
|
+
// ============================================================================
|
|
32
|
+
// Init Event Schema
|
|
33
|
+
// ============================================================================
|
|
34
|
+
/**
|
|
35
|
+
* Session initialization event
|
|
36
|
+
*
|
|
37
|
+
* Example:
|
|
38
|
+
* ```json
|
|
39
|
+
* {"type":"init","timestamp":"2025-11-25T03:27:51.000Z","session_id":"c25acda3-b51f-41f9-9bc5-954c70c17bf4","model":"auto"}
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export const GeminiInitEventSchema = z.object({
|
|
43
|
+
type: z.literal("init"),
|
|
44
|
+
timestamp: TimestampSchema,
|
|
45
|
+
session_id: z.string().uuid(),
|
|
46
|
+
model: z.string(),
|
|
47
|
+
});
|
|
48
|
+
// ============================================================================
|
|
49
|
+
// Message Event Schema
|
|
50
|
+
// ============================================================================
|
|
51
|
+
/**
|
|
52
|
+
* User or assistant message event
|
|
53
|
+
*
|
|
54
|
+
* When delta is true, this message should be accumulated with previous delta messages
|
|
55
|
+
* of the same role. The caller (GeminiRunner) is responsible for accumulating delta messages.
|
|
56
|
+
*
|
|
57
|
+
* Examples:
|
|
58
|
+
* ```json
|
|
59
|
+
* {"type":"message","timestamp":"2025-11-25T03:27:51.001Z","role":"user","content":"What is 2 + 2?"}
|
|
60
|
+
* {"type":"message","timestamp":"2025-11-25T03:28:05.256Z","role":"assistant","content":"2 + 2 = 4.","delta":true}
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export const GeminiMessageEventSchema = z.object({
|
|
64
|
+
type: z.literal("message"),
|
|
65
|
+
timestamp: TimestampSchema,
|
|
66
|
+
role: z.enum(["user", "assistant"]),
|
|
67
|
+
content: z.string(),
|
|
68
|
+
delta: z.boolean().optional(),
|
|
69
|
+
});
|
|
70
|
+
// ============================================================================
|
|
71
|
+
// Tool Parameter Schemas
|
|
72
|
+
// ============================================================================
|
|
73
|
+
/**
|
|
74
|
+
* Parameters for the read_file tool
|
|
75
|
+
*
|
|
76
|
+
* Example:
|
|
77
|
+
* ```json
|
|
78
|
+
* {"file_path":"package.json"}
|
|
79
|
+
* {"file_path":"app/mcts.py"}
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export const ReadFileParametersSchema = z.object({
|
|
83
|
+
file_path: z.string(),
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* Parameters for the write_file tool
|
|
87
|
+
*
|
|
88
|
+
* Example:
|
|
89
|
+
* ```json
|
|
90
|
+
* {"file_path":"tests/test_snake.py","content":"import unittest\n..."}
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export const WriteFileParametersSchema = z.object({
|
|
94
|
+
file_path: z.string(),
|
|
95
|
+
content: z.string(),
|
|
96
|
+
});
|
|
97
|
+
/**
|
|
98
|
+
* Parameters for the list_directory tool
|
|
99
|
+
*
|
|
100
|
+
* Example:
|
|
101
|
+
* ```json
|
|
102
|
+
* {"dir_path":"."}
|
|
103
|
+
* {"dir_path":"./src"}
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export const ListDirectoryParametersSchema = z.object({
|
|
107
|
+
dir_path: z.string(),
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* Parameters for the search_file_content tool
|
|
111
|
+
*
|
|
112
|
+
* Example:
|
|
113
|
+
* ```json
|
|
114
|
+
* {"pattern":"(TODO|FIXME)"}
|
|
115
|
+
* {"pattern":"function.*export"}
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export const SearchFileContentParametersSchema = z.object({
|
|
119
|
+
pattern: z.string(),
|
|
120
|
+
});
|
|
121
|
+
/**
|
|
122
|
+
* Parameters for the run_shell_command tool
|
|
123
|
+
*
|
|
124
|
+
* Example:
|
|
125
|
+
* ```json
|
|
126
|
+
* {"command":"/usr/bin/python3 -m pytest tests/"}
|
|
127
|
+
* {"command":"git status"}
|
|
128
|
+
* {"command":"flake8 --version"}
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export const RunShellCommandParametersSchema = z.object({
|
|
132
|
+
command: z.string(),
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* Todo item for the write_todos tool
|
|
136
|
+
*/
|
|
137
|
+
export const TodoItemSchema = z.object({
|
|
138
|
+
description: z.string(),
|
|
139
|
+
status: z.enum(["pending", "in_progress", "completed"]).optional(),
|
|
140
|
+
});
|
|
141
|
+
/**
|
|
142
|
+
* Parameters for the write_todos tool
|
|
143
|
+
*
|
|
144
|
+
* Example:
|
|
145
|
+
* ```json
|
|
146
|
+
* {"todos":[{"description":"Explore codebase to identify bugs","status":"in_progress"},{"description":"Fix coordinate system","status":"pending"}]}
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export const WriteTodosParametersSchema = z.object({
|
|
150
|
+
todos: z.array(TodoItemSchema),
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
* Parameters for the replace tool (AI-powered code editing)
|
|
154
|
+
*
|
|
155
|
+
* Can use either instruction-based or literal string replacement:
|
|
156
|
+
* - instruction: Natural language description of the change
|
|
157
|
+
* - old_string/new_string: Literal string replacement
|
|
158
|
+
*
|
|
159
|
+
* Examples:
|
|
160
|
+
* Instruction-based:
|
|
161
|
+
* ```json
|
|
162
|
+
* {"instruction":"Modify get_other_snake_heads to return a list instead of dict","file_path":"app/mcts.py"}
|
|
163
|
+
* {"instruction":"Clean up comments in is_terminal.","file_path":"app/mcts.py"}
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* Literal replacement:
|
|
167
|
+
* ```json
|
|
168
|
+
* {"file_path":"app/mcts.py","old_string":" # Simulate other snakes' moves\\n othe","new_string":" # Track enemy positions\\n enemy"}
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export const ReplaceParametersSchema = z.object({
|
|
172
|
+
instruction: z.string().optional(),
|
|
173
|
+
file_path: z.string().optional(),
|
|
174
|
+
old_string: z.string().optional(),
|
|
175
|
+
new_string: z.string().optional(),
|
|
176
|
+
});
|
|
177
|
+
/**
|
|
178
|
+
* Union of all known tool parameter schemas
|
|
179
|
+
*/
|
|
180
|
+
export const GeminiToolParametersSchema = z.union([
|
|
181
|
+
ReadFileParametersSchema,
|
|
182
|
+
WriteFileParametersSchema,
|
|
183
|
+
ListDirectoryParametersSchema,
|
|
184
|
+
SearchFileContentParametersSchema,
|
|
185
|
+
RunShellCommandParametersSchema,
|
|
186
|
+
WriteTodosParametersSchema,
|
|
187
|
+
ReplaceParametersSchema,
|
|
188
|
+
]);
|
|
189
|
+
// ============================================================================
|
|
190
|
+
// Typed Tool Use Event Schemas (for specific tools)
|
|
191
|
+
// ============================================================================
|
|
192
|
+
/**
|
|
193
|
+
* Base schema for tool use events with timestamp and tool_id
|
|
194
|
+
*/
|
|
195
|
+
const ToolUseBaseSchema = z.object({
|
|
196
|
+
type: z.literal("tool_use"),
|
|
197
|
+
timestamp: TimestampSchema,
|
|
198
|
+
tool_id: z.string(),
|
|
199
|
+
});
|
|
200
|
+
/**
|
|
201
|
+
* Typed read_file tool use event
|
|
202
|
+
*/
|
|
203
|
+
export const ReadFileToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
204
|
+
tool_name: z.literal("read_file"),
|
|
205
|
+
parameters: ReadFileParametersSchema,
|
|
206
|
+
});
|
|
207
|
+
/**
|
|
208
|
+
* Typed write_file tool use event
|
|
209
|
+
*/
|
|
210
|
+
export const WriteFileToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
211
|
+
tool_name: z.literal("write_file"),
|
|
212
|
+
parameters: WriteFileParametersSchema,
|
|
213
|
+
});
|
|
214
|
+
/**
|
|
215
|
+
* Typed list_directory tool use event
|
|
216
|
+
*/
|
|
217
|
+
export const ListDirectoryToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
218
|
+
tool_name: z.literal("list_directory"),
|
|
219
|
+
parameters: ListDirectoryParametersSchema,
|
|
220
|
+
});
|
|
221
|
+
/**
|
|
222
|
+
* Typed search_file_content tool use event
|
|
223
|
+
*/
|
|
224
|
+
export const SearchFileContentToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
225
|
+
tool_name: z.literal("search_file_content"),
|
|
226
|
+
parameters: SearchFileContentParametersSchema,
|
|
227
|
+
});
|
|
228
|
+
/**
|
|
229
|
+
* Typed run_shell_command tool use event
|
|
230
|
+
*/
|
|
231
|
+
export const RunShellCommandToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
232
|
+
tool_name: z.literal("run_shell_command"),
|
|
233
|
+
parameters: RunShellCommandParametersSchema,
|
|
234
|
+
});
|
|
235
|
+
/**
|
|
236
|
+
* Typed write_todos tool use event
|
|
237
|
+
*/
|
|
238
|
+
export const WriteTodosToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
239
|
+
tool_name: z.literal("write_todos"),
|
|
240
|
+
parameters: WriteTodosParametersSchema,
|
|
241
|
+
});
|
|
242
|
+
/**
|
|
243
|
+
* Typed replace tool use event
|
|
244
|
+
*/
|
|
245
|
+
export const ReplaceToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
246
|
+
tool_name: z.literal("replace"),
|
|
247
|
+
parameters: ReplaceParametersSchema,
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* Unknown tool use event (for tools not explicitly typed)
|
|
251
|
+
*/
|
|
252
|
+
export const UnknownToolUseEventSchema = ToolUseBaseSchema.extend({
|
|
253
|
+
tool_name: z.string(),
|
|
254
|
+
parameters: z.record(z.unknown()),
|
|
255
|
+
});
|
|
256
|
+
// ============================================================================
|
|
257
|
+
// Tool Use Event Schema
|
|
258
|
+
// ============================================================================
|
|
259
|
+
/**
|
|
260
|
+
* Tool use event - represents a tool invocation by the model
|
|
261
|
+
*
|
|
262
|
+
* The tool_id is assigned by Gemini CLI and follows the format:
|
|
263
|
+
* `{tool_name}-{timestamp_ms}-{random_hex}`
|
|
264
|
+
*
|
|
265
|
+
* Example:
|
|
266
|
+
* ```json
|
|
267
|
+
* {"type":"tool_use","timestamp":"2025-11-25T03:27:54.691Z","tool_name":"list_directory","tool_id":"list_directory-1764041274691-eabd3cbcdee66","parameters":{"dir_path":"."}}
|
|
268
|
+
* {"type":"tool_use","timestamp":"2025-11-25T03:27:54.691Z","tool_name":"read_file","tool_id":"read_file-1764041274691-e1084c2fd73dc","parameters":{"file_path":"test.ts"}}
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
export const GeminiToolUseEventSchema = z.object({
|
|
272
|
+
type: z.literal("tool_use"),
|
|
273
|
+
timestamp: TimestampSchema,
|
|
274
|
+
tool_name: z.string(),
|
|
275
|
+
tool_id: z.string(),
|
|
276
|
+
parameters: z.record(z.unknown()),
|
|
277
|
+
});
|
|
278
|
+
// ============================================================================
|
|
279
|
+
// Tool Result Event Schema
|
|
280
|
+
// ============================================================================
|
|
281
|
+
/**
|
|
282
|
+
* Error information in tool result
|
|
283
|
+
*/
|
|
284
|
+
const ToolResultErrorSchema = z.object({
|
|
285
|
+
type: z.string().optional(),
|
|
286
|
+
message: z.string(),
|
|
287
|
+
code: z.string().optional(),
|
|
288
|
+
});
|
|
289
|
+
/**
|
|
290
|
+
* Tool result event - the result of a tool execution
|
|
291
|
+
*
|
|
292
|
+
* Uses tool_id (not tool_name) to match the corresponding tool_use event.
|
|
293
|
+
* Contains either output (success) or error (failure).
|
|
294
|
+
*
|
|
295
|
+
* Examples:
|
|
296
|
+
* Success:
|
|
297
|
+
* ```json
|
|
298
|
+
* {"type":"tool_result","timestamp":"2025-11-25T03:27:54.724Z","tool_id":"list_directory-1764041274691-eabd3cbcdee66","status":"success","output":"Listed 2 item(s)."}
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* Error:
|
|
302
|
+
* ```json
|
|
303
|
+
* {"type":"tool_result","timestamp":"2025-11-25T03:28:13.200Z","tool_id":"read_file-1764041293170-fd5f6da4bd4a1","status":"error","output":"File path must be within...","error":{"type":"invalid_tool_params","message":"File path must be within..."}}
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
export const GeminiToolResultEventSchema = z.object({
|
|
307
|
+
type: z.literal("tool_result"),
|
|
308
|
+
timestamp: TimestampSchema,
|
|
309
|
+
tool_id: z.string(),
|
|
310
|
+
status: z.enum(["success", "error"]),
|
|
311
|
+
output: z.string().optional(),
|
|
312
|
+
error: ToolResultErrorSchema.optional(),
|
|
313
|
+
});
|
|
314
|
+
// ============================================================================
|
|
315
|
+
// Typed Tool Result Schemas
|
|
316
|
+
// ============================================================================
|
|
317
|
+
/**
|
|
318
|
+
* Tool result output types based on the originating tool
|
|
319
|
+
*
|
|
320
|
+
* These describe the expected output format for each tool type.
|
|
321
|
+
* The tool_id prefix indicates which tool generated the result.
|
|
322
|
+
*/
|
|
323
|
+
/**
|
|
324
|
+
* read_file tool result - returns empty string on success (file content is in context)
|
|
325
|
+
*
|
|
326
|
+
* Example:
|
|
327
|
+
* ```json
|
|
328
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:12:40.148Z","tool_id":"read_file-1764015160012-767cb93e436f3","status":"success","output":""}
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
export const ReadFileToolResultSchema = z.object({
|
|
332
|
+
type: z.literal("tool_result"),
|
|
333
|
+
timestamp: TimestampSchema,
|
|
334
|
+
tool_id: z.string().startsWith("read_file-"),
|
|
335
|
+
status: z.enum(["success", "error"]),
|
|
336
|
+
output: z.string().optional(),
|
|
337
|
+
error: ToolResultErrorSchema.optional(),
|
|
338
|
+
});
|
|
339
|
+
/**
|
|
340
|
+
* write_file tool result - returns empty output on success
|
|
341
|
+
*
|
|
342
|
+
* Example:
|
|
343
|
+
* ```json
|
|
344
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:13:55.193Z","tool_id":"write_file-1764015234674-0581b9629931a","status":"success"}
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
export const WriteFileToolResultSchema = z.object({
|
|
348
|
+
type: z.literal("tool_result"),
|
|
349
|
+
timestamp: TimestampSchema,
|
|
350
|
+
tool_id: z.string().startsWith("write_file-"),
|
|
351
|
+
status: z.enum(["success", "error"]),
|
|
352
|
+
output: z.string().optional(),
|
|
353
|
+
error: ToolResultErrorSchema.optional(),
|
|
354
|
+
});
|
|
355
|
+
/**
|
|
356
|
+
* list_directory tool result - returns summary of items found
|
|
357
|
+
*
|
|
358
|
+
* Example:
|
|
359
|
+
* ```json
|
|
360
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:12:53.273Z","tool_id":"list_directory-1764015173255-396a90dd79fa6","status":"success","output":"Listed 4 item(s). (1 ignored)"}
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
export const ListDirectoryToolResultSchema = z.object({
|
|
364
|
+
type: z.literal("tool_result"),
|
|
365
|
+
timestamp: TimestampSchema,
|
|
366
|
+
tool_id: z.string().startsWith("list_directory-"),
|
|
367
|
+
status: z.enum(["success", "error"]),
|
|
368
|
+
output: z.string().optional(),
|
|
369
|
+
error: ToolResultErrorSchema.optional(),
|
|
370
|
+
});
|
|
371
|
+
/**
|
|
372
|
+
* search_file_content tool result - returns match info or "No matches found"
|
|
373
|
+
*
|
|
374
|
+
* Example:
|
|
375
|
+
* ```json
|
|
376
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:12:40.196Z","tool_id":"search_file_content-1764015160072-c1e0f530591f6","status":"success","output":"No matches found"}
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
export const SearchFileContentToolResultSchema = z.object({
|
|
380
|
+
type: z.literal("tool_result"),
|
|
381
|
+
timestamp: TimestampSchema,
|
|
382
|
+
tool_id: z.string().startsWith("search_file_content-"),
|
|
383
|
+
status: z.enum(["success", "error"]),
|
|
384
|
+
output: z.string().optional(),
|
|
385
|
+
error: ToolResultErrorSchema.optional(),
|
|
386
|
+
});
|
|
387
|
+
/**
|
|
388
|
+
* run_shell_command tool result - returns command output
|
|
389
|
+
*
|
|
390
|
+
* Examples:
|
|
391
|
+
* ```json
|
|
392
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:13:15.060Z","tool_id":"run_shell_command-1764015194969-e79bcda1d6e9","status":"success","output":"/usr/bin/python3: No module named pytest"}
|
|
393
|
+
* {"type":"tool_result","timestamp":"2025-11-24T20:19:49.805Z","tool_id":"run_shell_command-1764015589776-b029531d6e71e","status":"success","output":"node"}
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
export const RunShellCommandToolResultSchema = z.object({
|
|
397
|
+
type: z.literal("tool_result"),
|
|
398
|
+
timestamp: TimestampSchema,
|
|
399
|
+
tool_id: z.string().startsWith("run_shell_command-"),
|
|
400
|
+
status: z.enum(["success", "error"]),
|
|
401
|
+
output: z.string().optional(),
|
|
402
|
+
error: ToolResultErrorSchema.optional(),
|
|
403
|
+
});
|
|
404
|
+
/**
|
|
405
|
+
* write_todos tool result - returns empty output on success, or error if invalid
|
|
406
|
+
*
|
|
407
|
+
* Examples:
|
|
408
|
+
* Success:
|
|
409
|
+
* ```json
|
|
410
|
+
* {"type":"tool_result","timestamp":"2025-11-24T19:29:56.539Z","tool_id":"write_todos-1764012596037-37082c9903ce7","status":"success"}
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* Error (multiple in_progress):
|
|
414
|
+
* ```json
|
|
415
|
+
* {"type":"tool_result","timestamp":"2025-11-24T19:37:13.465Z","tool_id":"write_todos-1764013031965-70bbdf7c35856","status":"error","output":"Invalid parameters: Only one task can be \"in_progress\" at a time."}
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
418
|
+
export const WriteTodosToolResultSchema = z.object({
|
|
419
|
+
type: z.literal("tool_result"),
|
|
420
|
+
timestamp: TimestampSchema,
|
|
421
|
+
tool_id: z.string().startsWith("write_todos-"),
|
|
422
|
+
status: z.enum(["success", "error"]),
|
|
423
|
+
output: z.string().optional(),
|
|
424
|
+
error: ToolResultErrorSchema.optional(),
|
|
425
|
+
});
|
|
426
|
+
/**
|
|
427
|
+
* replace tool result - returns empty output on success
|
|
428
|
+
*
|
|
429
|
+
* Example:
|
|
430
|
+
* ```json
|
|
431
|
+
* {"type":"tool_result","timestamp":"2025-11-24T19:31:12.165Z","tool_id":"replace-1764012672140-c56f46960e14a","status":"success"}
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
export const ReplaceToolResultSchema = z.object({
|
|
435
|
+
type: z.literal("tool_result"),
|
|
436
|
+
timestamp: TimestampSchema,
|
|
437
|
+
tool_id: z.string().startsWith("replace-"),
|
|
438
|
+
status: z.enum(["success", "error"]),
|
|
439
|
+
output: z.string().optional(),
|
|
440
|
+
error: ToolResultErrorSchema.optional(),
|
|
441
|
+
});
|
|
442
|
+
/**
|
|
443
|
+
* Type guards for tool results based on tool_id prefix
|
|
444
|
+
*/
|
|
445
|
+
export function isReadFileToolResult(event) {
|
|
446
|
+
return event.tool_id.startsWith("read_file-");
|
|
447
|
+
}
|
|
448
|
+
export function isWriteFileToolResult(event) {
|
|
449
|
+
return event.tool_id.startsWith("write_file-");
|
|
450
|
+
}
|
|
451
|
+
export function isListDirectoryToolResult(event) {
|
|
452
|
+
return event.tool_id.startsWith("list_directory-");
|
|
453
|
+
}
|
|
454
|
+
export function isSearchFileContentToolResult(event) {
|
|
455
|
+
return event.tool_id.startsWith("search_file_content-");
|
|
456
|
+
}
|
|
457
|
+
export function isRunShellCommandToolResult(event) {
|
|
458
|
+
return event.tool_id.startsWith("run_shell_command-");
|
|
459
|
+
}
|
|
460
|
+
export function isWriteTodosToolResult(event) {
|
|
461
|
+
return event.tool_id.startsWith("write_todos-");
|
|
462
|
+
}
|
|
463
|
+
export function isReplaceToolResult(event) {
|
|
464
|
+
return event.tool_id.startsWith("replace-");
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Extract tool name from tool_id
|
|
468
|
+
*
|
|
469
|
+
* Tool IDs follow the format: `{tool_name}-{timestamp_ms}-{random_hex}`
|
|
470
|
+
*
|
|
471
|
+
* @param toolId - The tool_id from a tool_use or tool_result event
|
|
472
|
+
* @returns The tool name, or null if format is invalid
|
|
473
|
+
*/
|
|
474
|
+
export function extractToolNameFromId(toolId) {
|
|
475
|
+
// Tool ID format: {tool_name}-{timestamp_ms}-{random_hex}
|
|
476
|
+
// Split on hyphen and rejoin all but last two parts
|
|
477
|
+
const parts = toolId.split("-");
|
|
478
|
+
if (parts.length < 3) {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
// Remove the timestamp and random hex (last 2 parts)
|
|
482
|
+
return parts.slice(0, -2).join("-");
|
|
483
|
+
}
|
|
484
|
+
// ============================================================================
|
|
485
|
+
// Error Event Schema
|
|
486
|
+
// ============================================================================
|
|
487
|
+
/**
|
|
488
|
+
* Non-fatal error event
|
|
489
|
+
*
|
|
490
|
+
* Example:
|
|
491
|
+
* ```json
|
|
492
|
+
* {"type":"error","timestamp":"2025-11-25T03:28:00.000Z","message":"Rate limit exceeded","code":429}
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
export const GeminiErrorEventSchema = z.object({
|
|
496
|
+
type: z.literal("error"),
|
|
497
|
+
timestamp: TimestampSchema,
|
|
498
|
+
message: z.string(),
|
|
499
|
+
code: z.number().optional(),
|
|
500
|
+
});
|
|
501
|
+
// ============================================================================
|
|
502
|
+
// Result Event Schema
|
|
503
|
+
// ============================================================================
|
|
504
|
+
/**
|
|
505
|
+
* Statistics from the Gemini session
|
|
506
|
+
*/
|
|
507
|
+
const ResultStatsSchema = z.object({
|
|
508
|
+
total_tokens: z.number().optional(),
|
|
509
|
+
input_tokens: z.number().optional(),
|
|
510
|
+
output_tokens: z.number().optional(),
|
|
511
|
+
duration_ms: z.number().optional(),
|
|
512
|
+
tool_calls: z.number().optional(),
|
|
513
|
+
});
|
|
514
|
+
/**
|
|
515
|
+
* Error information in result event
|
|
516
|
+
*/
|
|
517
|
+
const ResultErrorSchema = z.object({
|
|
518
|
+
type: z.string(),
|
|
519
|
+
message: z.string(),
|
|
520
|
+
code: z.number().optional(),
|
|
521
|
+
});
|
|
522
|
+
/**
|
|
523
|
+
* Final result event with session statistics
|
|
524
|
+
*
|
|
525
|
+
* Examples:
|
|
526
|
+
* Success:
|
|
527
|
+
* ```json
|
|
528
|
+
* {"type":"result","timestamp":"2025-11-25T03:28:05.262Z","status":"success","stats":{"total_tokens":8064,"input_tokens":7854,"output_tokens":58,"duration_ms":2534,"tool_calls":0}}
|
|
529
|
+
* ```
|
|
530
|
+
*
|
|
531
|
+
* Error:
|
|
532
|
+
* ```json
|
|
533
|
+
* {"type":"result","timestamp":"2025-11-25T03:27:54.727Z","status":"error","error":{"type":"FatalTurnLimitedError","message":"Reached max session turns..."},"stats":{"total_tokens":8255,"input_tokens":7862,"output_tokens":90,"duration_ms":0,"tool_calls":2}}
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
export const GeminiResultEventSchema = z.object({
|
|
537
|
+
type: z.literal("result"),
|
|
538
|
+
timestamp: TimestampSchema,
|
|
539
|
+
status: z.enum(["success", "error"]),
|
|
540
|
+
stats: ResultStatsSchema.optional(),
|
|
541
|
+
error: ResultErrorSchema.optional(),
|
|
542
|
+
});
|
|
543
|
+
// ============================================================================
|
|
544
|
+
// Union Schema for All Events
|
|
545
|
+
// ============================================================================
|
|
546
|
+
/**
|
|
547
|
+
* Discriminated union of all Gemini stream events
|
|
548
|
+
*
|
|
549
|
+
* Uses the 'type' field as the discriminator for type narrowing.
|
|
550
|
+
*/
|
|
551
|
+
export const GeminiStreamEventSchema = z.discriminatedUnion("type", [
|
|
552
|
+
GeminiInitEventSchema,
|
|
553
|
+
GeminiMessageEventSchema,
|
|
554
|
+
GeminiToolUseEventSchema,
|
|
555
|
+
GeminiToolResultEventSchema,
|
|
556
|
+
GeminiErrorEventSchema,
|
|
557
|
+
GeminiResultEventSchema,
|
|
558
|
+
]);
|
|
559
|
+
// ============================================================================
|
|
560
|
+
// Validation Utilities
|
|
561
|
+
// ============================================================================
|
|
562
|
+
/**
|
|
563
|
+
* Parse and validate a Gemini stream event from a JSON string
|
|
564
|
+
*
|
|
565
|
+
* @param jsonString - Raw JSON string from Gemini CLI stdout
|
|
566
|
+
* @returns Validated and typed GeminiStreamEvent
|
|
567
|
+
* @throws ZodError if validation fails
|
|
568
|
+
*/
|
|
569
|
+
export function parseGeminiStreamEvent(jsonString) {
|
|
570
|
+
const parsed = JSON.parse(jsonString);
|
|
571
|
+
return GeminiStreamEventSchema.parse(parsed);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Safely parse a Gemini stream event, returning null on failure
|
|
575
|
+
*
|
|
576
|
+
* @param jsonString - Raw JSON string from Gemini CLI stdout
|
|
577
|
+
* @returns Validated GeminiStreamEvent or null if parsing/validation fails
|
|
578
|
+
*/
|
|
579
|
+
export function safeParseGeminiStreamEvent(jsonString) {
|
|
580
|
+
try {
|
|
581
|
+
const parsed = JSON.parse(jsonString);
|
|
582
|
+
const result = GeminiStreamEventSchema.safeParse(parsed);
|
|
583
|
+
return result.success ? result.data : null;
|
|
584
|
+
}
|
|
585
|
+
catch {
|
|
586
|
+
return null;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Type guard for checking if an event is a specific type
|
|
591
|
+
*/
|
|
592
|
+
export function isGeminiInitEvent(event) {
|
|
593
|
+
return event.type === "init";
|
|
594
|
+
}
|
|
595
|
+
export function isGeminiMessageEvent(event) {
|
|
596
|
+
return event.type === "message";
|
|
597
|
+
}
|
|
598
|
+
export function isGeminiToolUseEvent(event) {
|
|
599
|
+
return event.type === "tool_use";
|
|
600
|
+
}
|
|
601
|
+
export function isGeminiToolResultEvent(event) {
|
|
602
|
+
return event.type === "tool_result";
|
|
603
|
+
}
|
|
604
|
+
export function isGeminiErrorEvent(event) {
|
|
605
|
+
return event.type === "error";
|
|
606
|
+
}
|
|
607
|
+
export function isGeminiResultEvent(event) {
|
|
608
|
+
return event.type === "result";
|
|
609
|
+
}
|
|
610
|
+
// ============================================================================
|
|
611
|
+
// Typed Tool Use Parsing Utilities
|
|
612
|
+
// ============================================================================
|
|
613
|
+
/**
|
|
614
|
+
* Parse a tool use event as a specific typed tool
|
|
615
|
+
*
|
|
616
|
+
* @param event - A GeminiToolUseEvent to parse
|
|
617
|
+
* @returns The typed tool use event, or null if the tool name doesn't match or validation fails
|
|
618
|
+
*/
|
|
619
|
+
export function parseAsReadFileTool(event) {
|
|
620
|
+
const result = ReadFileToolUseEventSchema.safeParse(event);
|
|
621
|
+
return result.success ? result.data : null;
|
|
622
|
+
}
|
|
623
|
+
export function parseAsWriteFileTool(event) {
|
|
624
|
+
const result = WriteFileToolUseEventSchema.safeParse(event);
|
|
625
|
+
return result.success ? result.data : null;
|
|
626
|
+
}
|
|
627
|
+
export function parseAsListDirectoryTool(event) {
|
|
628
|
+
const result = ListDirectoryToolUseEventSchema.safeParse(event);
|
|
629
|
+
return result.success ? result.data : null;
|
|
630
|
+
}
|
|
631
|
+
export function parseAsSearchFileContentTool(event) {
|
|
632
|
+
const result = SearchFileContentToolUseEventSchema.safeParse(event);
|
|
633
|
+
return result.success ? result.data : null;
|
|
634
|
+
}
|
|
635
|
+
export function parseAsRunShellCommandTool(event) {
|
|
636
|
+
const result = RunShellCommandToolUseEventSchema.safeParse(event);
|
|
637
|
+
return result.success ? result.data : null;
|
|
638
|
+
}
|
|
639
|
+
export function parseAsWriteTodosTool(event) {
|
|
640
|
+
const result = WriteTodosToolUseEventSchema.safeParse(event);
|
|
641
|
+
return result.success ? result.data : null;
|
|
642
|
+
}
|
|
643
|
+
export function parseAsReplaceTool(event) {
|
|
644
|
+
const result = ReplaceToolUseEventSchema.safeParse(event);
|
|
645
|
+
return result.success ? result.data : null;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Type guard for specific tool types based on tool_name
|
|
649
|
+
*/
|
|
650
|
+
export function isReadFileTool(event) {
|
|
651
|
+
return (event.tool_name === "read_file" &&
|
|
652
|
+
ReadFileParametersSchema.safeParse(event.parameters).success);
|
|
653
|
+
}
|
|
654
|
+
export function isWriteFileTool(event) {
|
|
655
|
+
return (event.tool_name === "write_file" &&
|
|
656
|
+
WriteFileParametersSchema.safeParse(event.parameters).success);
|
|
657
|
+
}
|
|
658
|
+
export function isListDirectoryTool(event) {
|
|
659
|
+
return (event.tool_name === "list_directory" &&
|
|
660
|
+
ListDirectoryParametersSchema.safeParse(event.parameters).success);
|
|
661
|
+
}
|
|
662
|
+
export function isSearchFileContentTool(event) {
|
|
663
|
+
return (event.tool_name === "search_file_content" &&
|
|
664
|
+
SearchFileContentParametersSchema.safeParse(event.parameters).success);
|
|
665
|
+
}
|
|
666
|
+
export function isRunShellCommandTool(event) {
|
|
667
|
+
return (event.tool_name === "run_shell_command" &&
|
|
668
|
+
RunShellCommandParametersSchema.safeParse(event.parameters).success);
|
|
669
|
+
}
|
|
670
|
+
export function isWriteTodosTool(event) {
|
|
671
|
+
return (event.tool_name === "write_todos" &&
|
|
672
|
+
WriteTodosParametersSchema.safeParse(event.parameters).success);
|
|
673
|
+
}
|
|
674
|
+
export function isReplaceTool(event) {
|
|
675
|
+
return (event.tool_name === "replace" &&
|
|
676
|
+
ReplaceParametersSchema.safeParse(event.parameters).success);
|
|
677
|
+
}
|
|
678
|
+
//# sourceMappingURL=schemas.js.map
|