@ketd/gemini-cli-sdk 0.1.0
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 +21 -0
- package/README.md +470 -0
- package/dist/index.cjs +419 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +408 -0
- package/dist/index.d.ts +408 -0
- package/dist/index.js +384 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type definitions for Gemini CLI SDK
|
|
5
|
+
*
|
|
6
|
+
* Based on Gemini CLI v0.21.0+ interface specification
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Gemini CLI configuration options
|
|
10
|
+
*/
|
|
11
|
+
interface GeminiOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Path to Gemini CLI executable
|
|
14
|
+
* @example 'node_modules/@google/gemini-cli/bundle/gemini.js'
|
|
15
|
+
*/
|
|
16
|
+
pathToGeminiCLI: string;
|
|
17
|
+
/**
|
|
18
|
+
* Google AI API Key
|
|
19
|
+
* Can also be set via GOOGLE_API_KEY environment variable
|
|
20
|
+
*/
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Model name
|
|
24
|
+
* @default 'gemini-2.0-flash-exp'
|
|
25
|
+
* @example 'gemini-2.0-flash-exp', 'gemini-1.5-pro'
|
|
26
|
+
*/
|
|
27
|
+
model?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Working directory for CLI execution
|
|
30
|
+
* @default process.cwd()
|
|
31
|
+
*/
|
|
32
|
+
cwd?: string;
|
|
33
|
+
/**
|
|
34
|
+
* System prompt (not directly supported by CLI, needs workaround)
|
|
35
|
+
*/
|
|
36
|
+
systemPrompt?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Approval mode for tool execution
|
|
39
|
+
* - default: Prompt for approval
|
|
40
|
+
* - auto_edit: Auto-approve edit tools
|
|
41
|
+
* - yolo: Auto-approve all tools
|
|
42
|
+
* @default 'default'
|
|
43
|
+
*/
|
|
44
|
+
approvalMode?: 'default' | 'auto_edit' | 'yolo';
|
|
45
|
+
/**
|
|
46
|
+
* List of tools that can run without confirmation
|
|
47
|
+
* @example ['read', 'write', 'bash']
|
|
48
|
+
*/
|
|
49
|
+
allowedTools?: string[];
|
|
50
|
+
/**
|
|
51
|
+
* List of allowed MCP server names
|
|
52
|
+
*/
|
|
53
|
+
allowedMcpServerNames?: string[];
|
|
54
|
+
/**
|
|
55
|
+
* Enable debug mode
|
|
56
|
+
* @default false
|
|
57
|
+
*/
|
|
58
|
+
debug?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Session ID to resume
|
|
61
|
+
* Use 'latest' to resume the most recent session
|
|
62
|
+
*/
|
|
63
|
+
resumeSessionId?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Enable sandbox mode
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
sandbox?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Additional directories to include in workspace
|
|
71
|
+
*/
|
|
72
|
+
includeDirectories?: string[];
|
|
73
|
+
/**
|
|
74
|
+
* Custom environment variables
|
|
75
|
+
*/
|
|
76
|
+
env?: Record<string, string>;
|
|
77
|
+
/**
|
|
78
|
+
* Timeout in milliseconds
|
|
79
|
+
* @default undefined (no timeout)
|
|
80
|
+
*/
|
|
81
|
+
timeout?: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* JSON Stream Event Types
|
|
85
|
+
*
|
|
86
|
+
* Based on: @google/gemini-cli/packages/core/src/output/types.ts
|
|
87
|
+
*/
|
|
88
|
+
declare enum JsonStreamEventType {
|
|
89
|
+
/** Session initialization */
|
|
90
|
+
INIT = "init",
|
|
91
|
+
/** Message content (user/assistant) */
|
|
92
|
+
MESSAGE = "message",
|
|
93
|
+
/** Tool call request */
|
|
94
|
+
TOOL_USE = "tool_use",
|
|
95
|
+
/** Tool execution result */
|
|
96
|
+
TOOL_RESULT = "tool_result",
|
|
97
|
+
/** Error event */
|
|
98
|
+
ERROR = "error",
|
|
99
|
+
/** Final result */
|
|
100
|
+
RESULT = "result"
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Base event interface
|
|
104
|
+
*/
|
|
105
|
+
interface BaseJsonStreamEvent {
|
|
106
|
+
type: JsonStreamEventType;
|
|
107
|
+
timestamp: string;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Session initialization event
|
|
111
|
+
*/
|
|
112
|
+
interface InitEvent extends BaseJsonStreamEvent {
|
|
113
|
+
type: JsonStreamEventType.INIT;
|
|
114
|
+
session_id: string;
|
|
115
|
+
model: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Message event
|
|
119
|
+
*/
|
|
120
|
+
interface MessageEvent extends BaseJsonStreamEvent {
|
|
121
|
+
type: JsonStreamEventType.MESSAGE;
|
|
122
|
+
role: 'user' | 'assistant';
|
|
123
|
+
content: string;
|
|
124
|
+
delta?: boolean;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Tool call event
|
|
128
|
+
*/
|
|
129
|
+
interface ToolUseEvent extends BaseJsonStreamEvent {
|
|
130
|
+
type: JsonStreamEventType.TOOL_USE;
|
|
131
|
+
tool_name: string;
|
|
132
|
+
tool_id: string;
|
|
133
|
+
parameters: Record<string, unknown>;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Tool execution result event
|
|
137
|
+
*/
|
|
138
|
+
interface ToolResultEvent extends BaseJsonStreamEvent {
|
|
139
|
+
type: JsonStreamEventType.TOOL_RESULT;
|
|
140
|
+
tool_id: string;
|
|
141
|
+
status: 'success' | 'error';
|
|
142
|
+
output?: string;
|
|
143
|
+
error?: {
|
|
144
|
+
type: string;
|
|
145
|
+
message: string;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Error event
|
|
150
|
+
*/
|
|
151
|
+
interface ErrorEvent extends BaseJsonStreamEvent {
|
|
152
|
+
type: JsonStreamEventType.ERROR;
|
|
153
|
+
severity: 'warning' | 'error';
|
|
154
|
+
message: string;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Stream statistics
|
|
158
|
+
*/
|
|
159
|
+
interface StreamStats {
|
|
160
|
+
total_tokens: number;
|
|
161
|
+
input_tokens: number;
|
|
162
|
+
output_tokens: number;
|
|
163
|
+
duration_ms: number;
|
|
164
|
+
tool_calls: number;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Final result event
|
|
168
|
+
*/
|
|
169
|
+
interface ResultEvent extends BaseJsonStreamEvent {
|
|
170
|
+
type: JsonStreamEventType.RESULT;
|
|
171
|
+
status: 'success' | 'error';
|
|
172
|
+
error?: {
|
|
173
|
+
type: string;
|
|
174
|
+
message: string;
|
|
175
|
+
};
|
|
176
|
+
stats?: StreamStats;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Union type of all JSON stream events
|
|
180
|
+
*/
|
|
181
|
+
type JsonStreamEvent = InitEvent | MessageEvent | ToolUseEvent | ToolResultEvent | ErrorEvent | ResultEvent;
|
|
182
|
+
/**
|
|
183
|
+
* Gemini CLI exit codes
|
|
184
|
+
*/
|
|
185
|
+
declare enum ExitCode {
|
|
186
|
+
/** Success */
|
|
187
|
+
SUCCESS = 0,
|
|
188
|
+
/** General error */
|
|
189
|
+
GENERAL_ERROR = 1,
|
|
190
|
+
/** Configuration error */
|
|
191
|
+
CONFIG_ERROR = 2,
|
|
192
|
+
/** User interrupted (Ctrl+C) */
|
|
193
|
+
USER_INTERRUPTED = 130
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* CLI process status
|
|
197
|
+
*/
|
|
198
|
+
declare enum ProcessStatus {
|
|
199
|
+
/** Not started */
|
|
200
|
+
IDLE = "idle",
|
|
201
|
+
/** Running */
|
|
202
|
+
RUNNING = "running",
|
|
203
|
+
/** Completed successfully */
|
|
204
|
+
COMPLETED = "completed",
|
|
205
|
+
/** Cancelled by user */
|
|
206
|
+
CANCELLED = "cancelled",
|
|
207
|
+
/** Error occurred */
|
|
208
|
+
ERROR = "error"
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Gemini SDK Error
|
|
212
|
+
*/
|
|
213
|
+
declare class GeminiSDKError extends Error {
|
|
214
|
+
code?: ExitCode | undefined;
|
|
215
|
+
details?: unknown | undefined;
|
|
216
|
+
constructor(message: string, code?: ExitCode | undefined, details?: unknown | undefined);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Query result (accumulated from stream)
|
|
220
|
+
*/
|
|
221
|
+
interface QueryResult {
|
|
222
|
+
/** Session ID */
|
|
223
|
+
sessionId: string;
|
|
224
|
+
/** Model used */
|
|
225
|
+
model: string;
|
|
226
|
+
/** Full assistant response text */
|
|
227
|
+
response: string;
|
|
228
|
+
/** Tool calls made during the query */
|
|
229
|
+
toolCalls: Array<{
|
|
230
|
+
tool_name: string;
|
|
231
|
+
tool_id: string;
|
|
232
|
+
parameters: Record<string, unknown>;
|
|
233
|
+
result?: {
|
|
234
|
+
status: 'success' | 'error';
|
|
235
|
+
output?: string;
|
|
236
|
+
error?: {
|
|
237
|
+
type: string;
|
|
238
|
+
message: string;
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
}>;
|
|
242
|
+
/** Final statistics */
|
|
243
|
+
stats?: StreamStats;
|
|
244
|
+
/** Final status */
|
|
245
|
+
status: 'success' | 'error';
|
|
246
|
+
/** Error if status is 'error' */
|
|
247
|
+
error?: {
|
|
248
|
+
type: string;
|
|
249
|
+
message: string;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Core query function for Gemini CLI SDK
|
|
255
|
+
*
|
|
256
|
+
* Spawns Gemini CLI as subprocess and streams JSON events
|
|
257
|
+
*/
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Query Gemini CLI and stream JSON events
|
|
261
|
+
*
|
|
262
|
+
* @param prompt - User prompt
|
|
263
|
+
* @param options - Gemini configuration options
|
|
264
|
+
* @returns AsyncGenerator<JsonStreamEvent> - Stream of JSON events
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* import { query } from '@google/gemini-cli-sdk';
|
|
269
|
+
*
|
|
270
|
+
* const stream = query('Hello, Gemini!', {
|
|
271
|
+
* pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
|
|
272
|
+
* apiKey: process.env.GOOGLE_API_KEY,
|
|
273
|
+
* model: 'gemini-2.0-flash-exp',
|
|
274
|
+
* });
|
|
275
|
+
*
|
|
276
|
+
* for await (const event of stream) {
|
|
277
|
+
* if (event.type === 'message' && event.role === 'assistant' && event.delta) {
|
|
278
|
+
* process.stdout.write(event.content);
|
|
279
|
+
* }
|
|
280
|
+
* }
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
declare function query(prompt: string, options: GeminiOptions): AsyncGenerator<JsonStreamEvent>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Gemini CLI Client
|
|
287
|
+
*
|
|
288
|
+
* High-level client for interacting with Gemini CLI
|
|
289
|
+
*/
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Gemini CLI Client
|
|
293
|
+
*
|
|
294
|
+
* Provides a high-level API for interacting with Gemini CLI
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* import { GeminiClient } from '@google/gemini-cli-sdk';
|
|
299
|
+
*
|
|
300
|
+
* const client = new GeminiClient({
|
|
301
|
+
* pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
|
|
302
|
+
* apiKey: process.env.GOOGLE_API_KEY,
|
|
303
|
+
* model: 'gemini-2.0-flash-exp',
|
|
304
|
+
* });
|
|
305
|
+
*
|
|
306
|
+
* // Stream events
|
|
307
|
+
* for await (const event of client.stream('Hello, Gemini!')) {
|
|
308
|
+
* if (event.type === 'message' && event.role === 'assistant' && event.delta) {
|
|
309
|
+
* process.stdout.write(event.content);
|
|
310
|
+
* }
|
|
311
|
+
* }
|
|
312
|
+
*
|
|
313
|
+
* // Or get complete result
|
|
314
|
+
* const result = await client.query('Explain TypeScript generics');
|
|
315
|
+
* console.log(result.response);
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
declare class GeminiClient extends EventEmitter {
|
|
319
|
+
private options;
|
|
320
|
+
private status;
|
|
321
|
+
private currentSessionId;
|
|
322
|
+
constructor(options: GeminiOptions);
|
|
323
|
+
/**
|
|
324
|
+
* Stream events from Gemini CLI
|
|
325
|
+
*
|
|
326
|
+
* @param prompt - User prompt
|
|
327
|
+
* @returns AsyncGenerator<JsonStreamEvent> - Stream of JSON events
|
|
328
|
+
*/
|
|
329
|
+
stream(prompt: string): AsyncGenerator<JsonStreamEvent>;
|
|
330
|
+
/**
|
|
331
|
+
* Query Gemini CLI and return complete result
|
|
332
|
+
*
|
|
333
|
+
* @param prompt - User prompt
|
|
334
|
+
* @returns Promise<QueryResult> - Complete query result
|
|
335
|
+
*/
|
|
336
|
+
query(prompt: string): Promise<QueryResult>;
|
|
337
|
+
/**
|
|
338
|
+
* Get current process status
|
|
339
|
+
*/
|
|
340
|
+
getStatus(): ProcessStatus;
|
|
341
|
+
/**
|
|
342
|
+
* Get current session ID
|
|
343
|
+
*/
|
|
344
|
+
getSessionId(): string | null;
|
|
345
|
+
/**
|
|
346
|
+
* Update options
|
|
347
|
+
*/
|
|
348
|
+
setOptions(options: Partial<GeminiOptions>): void;
|
|
349
|
+
/**
|
|
350
|
+
* Get current options
|
|
351
|
+
*/
|
|
352
|
+
getOptions(): Readonly<GeminiOptions>;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Utility functions for Gemini CLI SDK
|
|
357
|
+
*/
|
|
358
|
+
/**
|
|
359
|
+
* Find Gemini CLI executable path
|
|
360
|
+
*
|
|
361
|
+
* Searches in common locations:
|
|
362
|
+
* 1. node_modules/@google/gemini-cli/bundle/gemini.js
|
|
363
|
+
* 2. Custom path from environment variable
|
|
364
|
+
* 3. Global installation
|
|
365
|
+
*
|
|
366
|
+
* @param cwd - Current working directory
|
|
367
|
+
* @returns string - Path to Gemini CLI executable
|
|
368
|
+
* @throws Error if Gemini CLI is not found
|
|
369
|
+
*/
|
|
370
|
+
declare function findGeminiCLI(cwd?: string): string;
|
|
371
|
+
/**
|
|
372
|
+
* Validate API key
|
|
373
|
+
*
|
|
374
|
+
* @param apiKey - API key to validate
|
|
375
|
+
* @returns boolean - True if valid
|
|
376
|
+
*/
|
|
377
|
+
declare function validateApiKey(apiKey?: string): boolean;
|
|
378
|
+
/**
|
|
379
|
+
* Get API key from environment or options
|
|
380
|
+
*
|
|
381
|
+
* @param apiKey - Optional API key from options
|
|
382
|
+
* @returns string - API key
|
|
383
|
+
* @throws Error if API key is not found
|
|
384
|
+
*/
|
|
385
|
+
declare function getApiKey(apiKey?: string): string;
|
|
386
|
+
/**
|
|
387
|
+
* Parse model name and validate
|
|
388
|
+
*
|
|
389
|
+
* @param model - Model name
|
|
390
|
+
* @returns string - Validated model name
|
|
391
|
+
*/
|
|
392
|
+
declare function validateModel(model?: string): string;
|
|
393
|
+
/**
|
|
394
|
+
* Format duration in milliseconds to human-readable string
|
|
395
|
+
*
|
|
396
|
+
* @param ms - Duration in milliseconds
|
|
397
|
+
* @returns string - Formatted duration
|
|
398
|
+
*/
|
|
399
|
+
declare function formatDuration(ms: number): string;
|
|
400
|
+
/**
|
|
401
|
+
* Format token count with commas
|
|
402
|
+
*
|
|
403
|
+
* @param tokens - Token count
|
|
404
|
+
* @returns string - Formatted token count
|
|
405
|
+
*/
|
|
406
|
+
declare function formatTokens(tokens: number): string;
|
|
407
|
+
|
|
408
|
+
export { type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, type InitEvent, type JsonStreamEvent, JsonStreamEventType, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type StreamStats, type ToolResultEvent, type ToolUseEvent, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, validateApiKey, validateModel };
|