@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Gemini CLI SDK Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,470 @@
1
+ # @google/gemini-cli-sdk
2
+
3
+ TypeScript SDK for Google Gemini CLI - spawn and interact with Gemini CLI as a subprocess.
4
+
5
+ ## Features
6
+
7
+ - 🚀 **Simple API**: Easy-to-use client for Gemini CLI
8
+ - 📡 **Streaming Support**: Real-time event streaming
9
+ - 🔧 **TypeScript**: Full type safety with TypeScript
10
+ - 🎯 **Tool Calling**: Automatic tool execution handling
11
+ - 📦 **Lightweight**: Minimal dependencies
12
+ - 🔄 **Session Management**: Resume previous sessions
13
+ - âš¡ **Async Generators**: Modern async/await patterns
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @google/gemini-cli-sdk @google/gemini-cli
19
+ ```
20
+
21
+ Or with pnpm:
22
+
23
+ ```bash
24
+ pnpm add @google/gemini-cli-sdk @google/gemini-cli
25
+ ```
26
+
27
+ ## Prerequisites
28
+
29
+ - Node.js >= 18.0.0
30
+ - `@google/gemini-cli` installed (peer dependency)
31
+ - Google AI API Key
32
+
33
+ ## Quick Start
34
+
35
+ ### Basic Usage
36
+
37
+ ```typescript
38
+ import { GeminiClient } from '@google/gemini-cli-sdk';
39
+
40
+ const client = new GeminiClient({
41
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
42
+ apiKey: process.env.GOOGLE_API_KEY,
43
+ model: 'gemini-2.0-flash-exp',
44
+ });
45
+
46
+ // Get complete response
47
+ const result = await client.query('Explain TypeScript generics');
48
+ console.log(result.response);
49
+ console.log('Tokens used:', result.stats?.total_tokens);
50
+ ```
51
+
52
+ ### Streaming Events
53
+
54
+ ```typescript
55
+ import { GeminiClient } from '@google/gemini-cli-sdk';
56
+
57
+ const client = new GeminiClient({
58
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
59
+ apiKey: process.env.GOOGLE_API_KEY,
60
+ });
61
+
62
+ // Stream events in real-time
63
+ for await (const event of client.stream('Write a hello world program')) {
64
+ if (event.type === 'message' && event.role === 'assistant' && event.delta) {
65
+ process.stdout.write(event.content);
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### Low-Level API
71
+
72
+ ```typescript
73
+ import { query } from '@google/gemini-cli-sdk';
74
+
75
+ const stream = query('Hello, Gemini!', {
76
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
77
+ apiKey: process.env.GOOGLE_API_KEY,
78
+ });
79
+
80
+ for await (const event of stream) {
81
+ console.log(event);
82
+ }
83
+ ```
84
+
85
+ ## API Reference
86
+
87
+ ### `GeminiClient`
88
+
89
+ High-level client for interacting with Gemini CLI.
90
+
91
+ #### Constructor
92
+
93
+ ```typescript
94
+ new GeminiClient(options: GeminiOptions)
95
+ ```
96
+
97
+ #### Methods
98
+
99
+ ##### `query(prompt: string): Promise<QueryResult>`
100
+
101
+ Send a query and get the complete result.
102
+
103
+ ```typescript
104
+ const result = await client.query('Explain async/await');
105
+ console.log(result.response);
106
+ console.log(result.stats);
107
+ ```
108
+
109
+ ##### `stream(prompt: string): AsyncGenerator<JsonStreamEvent>`
110
+
111
+ Stream events from Gemini CLI.
112
+
113
+ ```typescript
114
+ for await (const event of client.stream('Hello')) {
115
+ if (event.type === 'message') {
116
+ console.log(event.content);
117
+ }
118
+ }
119
+ ```
120
+
121
+ ##### `getStatus(): ProcessStatus`
122
+
123
+ Get current process status.
124
+
125
+ ```typescript
126
+ const status = client.getStatus();
127
+ // 'idle' | 'running' | 'completed' | 'cancelled' | 'error'
128
+ ```
129
+
130
+ ##### `getSessionId(): string | null`
131
+
132
+ Get current session ID.
133
+
134
+ ```typescript
135
+ const sessionId = client.getSessionId();
136
+ ```
137
+
138
+ ### `query(prompt: string, options: GeminiOptions)`
139
+
140
+ Low-level function to query Gemini CLI.
141
+
142
+ ```typescript
143
+ import { query } from '@google/gemini-cli-sdk';
144
+
145
+ for await (const event of query('Hello', options)) {
146
+ console.log(event);
147
+ }
148
+ ```
149
+
150
+ ## Configuration Options
151
+
152
+ ```typescript
153
+ interface GeminiOptions {
154
+ // Required
155
+ pathToGeminiCLI: string;
156
+
157
+ // Optional
158
+ apiKey?: string; // Or set GOOGLE_API_KEY env var
159
+ model?: string; // Default: 'gemini-2.0-flash-exp'
160
+ cwd?: string; // Working directory
161
+ approvalMode?: 'default' | 'auto_edit' | 'yolo';
162
+ allowedTools?: string[]; // e.g., ['read', 'write', 'bash']
163
+ allowedMcpServerNames?: string[];
164
+ debug?: boolean;
165
+ resumeSessionId?: string; // Resume previous session
166
+ sandbox?: boolean;
167
+ includeDirectories?: string[];
168
+ env?: Record<string, string>; // Custom env vars
169
+ timeout?: number; // Timeout in milliseconds
170
+ }
171
+ ```
172
+
173
+ ## Event Types
174
+
175
+ ### `InitEvent`
176
+
177
+ Session initialization event.
178
+
179
+ ```typescript
180
+ {
181
+ type: 'init',
182
+ timestamp: '2025-12-08T10:30:00.123Z',
183
+ session_id: 'abc123',
184
+ model: 'gemini-2.0-flash-exp'
185
+ }
186
+ ```
187
+
188
+ ### `MessageEvent`
189
+
190
+ Message content from user or assistant.
191
+
192
+ ```typescript
193
+ {
194
+ type: 'message',
195
+ timestamp: '2025-12-08T10:30:01.000Z',
196
+ role: 'assistant',
197
+ content: 'Hello! How can I help you?',
198
+ delta: true // Incremental content
199
+ }
200
+ ```
201
+
202
+ ### `ToolUseEvent`
203
+
204
+ Tool call request.
205
+
206
+ ```typescript
207
+ {
208
+ type: 'tool_use',
209
+ timestamp: '2025-12-08T10:30:02.000Z',
210
+ tool_name: 'read',
211
+ tool_id: 'tool_123',
212
+ parameters: { path: 'package.json' }
213
+ }
214
+ ```
215
+
216
+ ### `ToolResultEvent`
217
+
218
+ Tool execution result.
219
+
220
+ ```typescript
221
+ {
222
+ type: 'tool_result',
223
+ timestamp: '2025-12-08T10:30:03.000Z',
224
+ tool_id: 'tool_123',
225
+ status: 'success',
226
+ output: '{"name": "my-app"}'
227
+ }
228
+ ```
229
+
230
+ ### `ErrorEvent`
231
+
232
+ Error or warning event.
233
+
234
+ ```typescript
235
+ {
236
+ type: 'error',
237
+ timestamp: '2025-12-08T10:30:04.000Z',
238
+ severity: 'warning',
239
+ message: 'Loop detected'
240
+ }
241
+ ```
242
+
243
+ ### `ResultEvent`
244
+
245
+ Final result with statistics.
246
+
247
+ ```typescript
248
+ {
249
+ type: 'result',
250
+ timestamp: '2025-12-08T10:30:05.000Z',
251
+ status: 'success',
252
+ stats: {
253
+ total_tokens: 1500,
254
+ input_tokens: 500,
255
+ output_tokens: 1000,
256
+ duration_ms: 5000,
257
+ tool_calls: 3
258
+ }
259
+ }
260
+ ```
261
+
262
+ ## Advanced Examples
263
+
264
+ ### With Tool Approval
265
+
266
+ ```typescript
267
+ const client = new GeminiClient({
268
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
269
+ apiKey: process.env.GOOGLE_API_KEY,
270
+ approvalMode: 'auto_edit', // Auto-approve edit tools
271
+ allowedTools: ['read', 'write'], // Allow these tools without confirmation
272
+ });
273
+
274
+ const result = await client.query('Read package.json and update version');
275
+ ```
276
+
277
+ ### Resume Previous Session
278
+
279
+ ```typescript
280
+ const client = new GeminiClient({
281
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
282
+ apiKey: process.env.GOOGLE_API_KEY,
283
+ resumeSessionId: 'latest', // Or specific session ID
284
+ });
285
+
286
+ const result = await client.query('Continue from where we left off');
287
+ ```
288
+
289
+ ### Event Listeners
290
+
291
+ ```typescript
292
+ const client = new GeminiClient(options);
293
+
294
+ client.on('event', (event) => {
295
+ console.log('Event:', event.type);
296
+ });
297
+
298
+ client.on('status', (status) => {
299
+ console.log('Status:', status);
300
+ });
301
+
302
+ client.on('session', (sessionId) => {
303
+ console.log('Session ID:', sessionId);
304
+ });
305
+
306
+ client.on('error', (error) => {
307
+ console.error('Error:', error);
308
+ });
309
+
310
+ await client.query('Hello');
311
+ ```
312
+
313
+ ### Custom Working Directory
314
+
315
+ ```typescript
316
+ const client = new GeminiClient({
317
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
318
+ apiKey: process.env.GOOGLE_API_KEY,
319
+ cwd: '/path/to/project',
320
+ includeDirectories: ['./src', './docs'],
321
+ });
322
+ ```
323
+
324
+ ### Timeout Handling
325
+
326
+ ```typescript
327
+ const client = new GeminiClient({
328
+ pathToGeminiCLI: './node_modules/@google/gemini-cli/bundle/gemini.js',
329
+ apiKey: process.env.GOOGLE_API_KEY,
330
+ timeout: 30000, // 30 seconds
331
+ });
332
+
333
+ try {
334
+ const result = await client.query('Long running task');
335
+ } catch (error) {
336
+ if (error.code === 130) {
337
+ console.log('Query timed out');
338
+ }
339
+ }
340
+ ```
341
+
342
+ ## Utilities
343
+
344
+ ### `findGeminiCLI(cwd?: string): string`
345
+
346
+ Automatically find Gemini CLI executable.
347
+
348
+ ```typescript
349
+ import { findGeminiCLI } from '@google/gemini-cli-sdk';
350
+
351
+ const cliPath = findGeminiCLI();
352
+ console.log('Found Gemini CLI at:', cliPath);
353
+ ```
354
+
355
+ ### `getApiKey(apiKey?: string): string`
356
+
357
+ Get API key from options or environment.
358
+
359
+ ```typescript
360
+ import { getApiKey } from '@google/gemini-cli-sdk';
361
+
362
+ const apiKey = getApiKey(); // Reads from GOOGLE_API_KEY env var
363
+ ```
364
+
365
+ ### `formatDuration(ms: number): string`
366
+
367
+ Format duration in milliseconds.
368
+
369
+ ```typescript
370
+ import { formatDuration } from '@google/gemini-cli-sdk';
371
+
372
+ console.log(formatDuration(5432)); // "5.43s"
373
+ console.log(formatDuration(125000)); // "2m 5s"
374
+ ```
375
+
376
+ ### `formatTokens(tokens: number): string`
377
+
378
+ Format token count with commas.
379
+
380
+ ```typescript
381
+ import { formatTokens } from '@google/gemini-cli-sdk';
382
+
383
+ console.log(formatTokens(1234567)); // "1,234,567"
384
+ ```
385
+
386
+ ## Error Handling
387
+
388
+ ```typescript
389
+ import { GeminiSDKError, ExitCode } from '@google/gemini-cli-sdk';
390
+
391
+ try {
392
+ const result = await client.query('Hello');
393
+ } catch (error) {
394
+ if (error instanceof GeminiSDKError) {
395
+ console.error('SDK Error:', error.message);
396
+ console.error('Exit Code:', error.code);
397
+ console.error('Details:', error.details);
398
+
399
+ if (error.code === ExitCode.USER_INTERRUPTED) {
400
+ console.log('User cancelled the query');
401
+ }
402
+ }
403
+ }
404
+ ```
405
+
406
+ ## Environment Variables
407
+
408
+ - `GOOGLE_API_KEY`: Google AI API Key (required if not provided in options)
409
+ - `GEMINI_CLI_PATH`: Custom path to Gemini CLI executable
410
+ - `DEBUG`: Enable debug logging
411
+
412
+ ## Architecture
413
+
414
+ This SDK follows the same architecture as `@anthropic-ai/claude-agent-sdk`:
415
+
416
+ 1. **Subprocess Spawning**: Gemini CLI runs as a separate Node.js process
417
+ 2. **stdio Communication**: JSON-Lines format over stdin/stdout
418
+ 3. **Event Streaming**: Real-time event streaming via AsyncGenerator
419
+ 4. **Process Isolation**: Clean separation between SDK and CLI
420
+
421
+ ## Comparison with Claude Code SDK
422
+
423
+ | Feature | Gemini CLI SDK | Claude Code SDK |
424
+ |---------|---------------|-----------------|
425
+ | **Output Format** | `--output-format stream-json` | JSON-Lines |
426
+ | **Tool Calling** | CLI handles automatically | SDK handles |
427
+ | **Session Management** | CLI built-in | SDK manages |
428
+ | **Approval Mode** | `--approval-mode` flag | SDK config |
429
+ | **API Key** | `GOOGLE_API_KEY` | `ANTHROPIC_API_KEY` |
430
+
431
+ ## Development
432
+
433
+ ```bash
434
+ # Install dependencies
435
+ pnpm install
436
+
437
+ # Build
438
+ pnpm build
439
+
440
+ # Watch mode
441
+ pnpm dev
442
+
443
+ # Run tests
444
+ pnpm test
445
+
446
+ # Lint
447
+ pnpm lint
448
+
449
+ # Format
450
+ pnpm format
451
+ ```
452
+
453
+ ## License
454
+
455
+ MIT
456
+
457
+ ## Contributing
458
+
459
+ Contributions are welcome! Please open an issue or submit a pull request.
460
+
461
+ ## Related Projects
462
+
463
+ - [@google/gemini-cli](https://github.com/google-gemini/gemini-cli) - Official Gemini CLI
464
+ - [@anthropic-ai/claude-agent-sdk](https://github.com/anthropics/claude-agent-sdk) - Claude Agent SDK (inspiration)
465
+
466
+ ## Support
467
+
468
+ For issues and questions:
469
+ - GitHub Issues: [github.com/yourusername/gemini-cli-sdk/issues](https://github.com/yourusername/gemini-cli-sdk/issues)
470
+ - Gemini CLI Docs: [github.com/google-gemini/gemini-cli](https://github.com/google-gemini/gemini-cli)