@ketd/gemini-cli-sdk 0.1.8 → 0.2.1

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/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
+ import { EventEmitter as EventEmitter$1 } from 'node:events';
2
3
 
3
4
  /**
4
5
  * Type definitions for Gemini CLI SDK
@@ -289,6 +290,90 @@ interface QueryResult {
289
290
  message: string;
290
291
  };
291
292
  }
293
+ /**
294
+ * Input message types for Stream Client (SDK → CLI via stdin)
295
+ */
296
+ declare enum JsonInputMessageType {
297
+ /** User message */
298
+ USER = "user",
299
+ /** Control command */
300
+ CONTROL = "control"
301
+ }
302
+ /**
303
+ * User message sent to CLI
304
+ */
305
+ interface UserInputMessage {
306
+ type: JsonInputMessageType.USER;
307
+ content: string;
308
+ session_id?: string;
309
+ }
310
+ /**
311
+ * Control message sent to CLI
312
+ */
313
+ interface ControlInputMessage {
314
+ type: JsonInputMessageType.CONTROL;
315
+ control: {
316
+ subtype: 'interrupt' | 'cancel' | 'shutdown';
317
+ };
318
+ session_id?: string;
319
+ }
320
+ /**
321
+ * Union type for input messages
322
+ */
323
+ type JsonInputMessage = UserInputMessage | ControlInputMessage;
324
+ /**
325
+ * Options for GeminiStreamClient
326
+ */
327
+ interface GeminiStreamOptions {
328
+ /**
329
+ * Path to Gemini CLI executable
330
+ * @example 'node_modules/@google/gemini-cli/bundle/gemini.js'
331
+ */
332
+ pathToGeminiCLI: string;
333
+ /**
334
+ * Session ID for this client instance
335
+ * Each client manages one session
336
+ */
337
+ sessionId: string;
338
+ /**
339
+ * Workspace ID (optional, for tracking)
340
+ */
341
+ workspaceId?: string;
342
+ /**
343
+ * Google AI API Key
344
+ * Can also be set via GEMINI_API_KEY environment variable
345
+ */
346
+ apiKey?: string;
347
+ /**
348
+ * Model name
349
+ * @default 'gemini-2.0-flash-exp'
350
+ */
351
+ model?: string;
352
+ /**
353
+ * Working directory for CLI execution
354
+ * @default process.cwd()
355
+ */
356
+ cwd?: string;
357
+ /**
358
+ * Approval mode for tool execution
359
+ * @default 'default'
360
+ */
361
+ approvalMode?: 'default' | 'auto_edit' | 'yolo';
362
+ /**
363
+ * Custom environment variables
364
+ */
365
+ env?: Record<string, string>;
366
+ /**
367
+ * Enable debug mode
368
+ * @default false
369
+ */
370
+ debug?: boolean;
371
+ /**
372
+ * Timeout for process initialization (ms)
373
+ * @default 30000
374
+ */
375
+ initTimeout?: number;
376
+ }
292
377
 
293
378
  /**
294
379
  * Core query function for Gemini CLI SDK
@@ -392,6 +477,132 @@ declare class GeminiClient extends EventEmitter {
392
477
  getOptions(): Readonly<GeminiOptions>;
393
478
  }
394
479
 
480
+ /**
481
+ * GeminiStreamClient - Persistent stream-based client for Gemini CLI
482
+ *
483
+ * Architecture:
484
+ * - 1 Client = 1 Node.js Process = 1 Session
485
+ * - Communication via stdin/stdout JSONL
486
+ * - Process stays alive for multiple message exchanges
487
+ * - Similar to Claude Agent SDK's SubprocessCLITransport
488
+ */
489
+
490
+ /**
491
+ * Events emitted by GeminiStreamClient
492
+ */
493
+ interface StreamClientEvents {
494
+ /** JSON stream event from CLI */
495
+ event: (event: JsonStreamEvent) => void;
496
+ /** Process started */
497
+ started: () => void;
498
+ /** Process ready (INIT event received) */
499
+ ready: (initEvent: InitEvent) => void;
500
+ /** Process stopped */
501
+ stopped: (code: number | null) => void;
502
+ /** Error occurred */
503
+ error: (error: Error) => void;
504
+ }
505
+ declare interface GeminiStreamClient {
506
+ on<K extends keyof StreamClientEvents>(event: K, listener: StreamClientEvents[K]): this;
507
+ emit<K extends keyof StreamClientEvents>(event: K, ...args: Parameters<StreamClientEvents[K]>): boolean;
508
+ }
509
+ /**
510
+ * GeminiStreamClient
511
+ *
512
+ * Manages a persistent Gemini CLI process for stream-based communication.
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * const client = new GeminiStreamClient({
517
+ * pathToGeminiCLI: './gemini.js',
518
+ * sessionId: 'session-123',
519
+ * apiKey: process.env.GEMINI_API_KEY,
520
+ * });
521
+ *
522
+ * client.on('event', (event) => {
523
+ * console.log('Event:', event);
524
+ * });
525
+ *
526
+ * await client.start();
527
+ * await client.sendMessage('Hello, Gemini!');
528
+ * await client.stop();
529
+ * ```
530
+ */
531
+ declare class GeminiStreamClient extends EventEmitter$1 {
532
+ private options;
533
+ private process;
534
+ private stdinStream;
535
+ private readlineInterface;
536
+ private status;
537
+ private initEvent;
538
+ private initTimeout;
539
+ constructor(options: GeminiStreamOptions);
540
+ /**
541
+ * Start the Gemini CLI process
542
+ */
543
+ start(): Promise<void>;
544
+ /**
545
+ * Send a user message to the CLI
546
+ */
547
+ sendMessage(content: string): Promise<void>;
548
+ /**
549
+ * Send an interrupt control command
550
+ */
551
+ interrupt(): Promise<void>;
552
+ /**
553
+ * Stop the CLI process
554
+ */
555
+ stop(timeout?: number): Promise<void>;
556
+ /**
557
+ * Check if client is ready to send messages
558
+ */
559
+ isReady(): boolean;
560
+ /**
561
+ * Get current status
562
+ */
563
+ getStatus(): ProcessStatus;
564
+ /**
565
+ * Get init event (contains session_id, model, etc.)
566
+ */
567
+ getInitEvent(): InitEvent | null;
568
+ /**
569
+ * Get process PID
570
+ */
571
+ getPid(): number | undefined;
572
+ /**
573
+ * Build CLI command arguments
574
+ */
575
+ private buildCommand;
576
+ /**
577
+ * Build environment variables
578
+ */
579
+ private buildEnv;
580
+ /**
581
+ * Write a JSON message to stdin
582
+ */
583
+ private writeMessage;
584
+ /**
585
+ * Start reading JSONL events from stdout
586
+ */
587
+ private startReadLoop;
588
+ /**
589
+ * Handle a JSON stream event
590
+ */
591
+ private handleEvent;
592
+ /**
593
+ * Wait for INIT event
594
+ */
595
+ private waitForInit;
596
+ /**
597
+ * Handle process exit
598
+ */
599
+ private handleProcessExit;
600
+ /**
601
+ * Handle process error
602
+ */
603
+ private handleProcessError;
604
+ }
605
+
395
606
  /**
396
607
  * Utility functions for Gemini CLI SDK
397
608
  */
@@ -445,4 +656,4 @@ declare function formatDuration(ms: number): string;
445
656
  */
446
657
  declare function formatTokens(tokens: number): string;
447
658
 
448
- export { type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, type InitEvent, type JsonStreamEvent, JsonStreamEventType, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type StreamStats, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, validateApiKey, validateModel };
659
+ export { type ControlInputMessage, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type InitEvent, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type UserInputMessage, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, validateApiKey, validateModel };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
+ import { EventEmitter as EventEmitter$1 } from 'node:events';
2
3
 
3
4
  /**
4
5
  * Type definitions for Gemini CLI SDK
@@ -289,6 +290,90 @@ interface QueryResult {
289
290
  message: string;
290
291
  };
291
292
  }
293
+ /**
294
+ * Input message types for Stream Client (SDK → CLI via stdin)
295
+ */
296
+ declare enum JsonInputMessageType {
297
+ /** User message */
298
+ USER = "user",
299
+ /** Control command */
300
+ CONTROL = "control"
301
+ }
302
+ /**
303
+ * User message sent to CLI
304
+ */
305
+ interface UserInputMessage {
306
+ type: JsonInputMessageType.USER;
307
+ content: string;
308
+ session_id?: string;
309
+ }
310
+ /**
311
+ * Control message sent to CLI
312
+ */
313
+ interface ControlInputMessage {
314
+ type: JsonInputMessageType.CONTROL;
315
+ control: {
316
+ subtype: 'interrupt' | 'cancel' | 'shutdown';
317
+ };
318
+ session_id?: string;
319
+ }
320
+ /**
321
+ * Union type for input messages
322
+ */
323
+ type JsonInputMessage = UserInputMessage | ControlInputMessage;
324
+ /**
325
+ * Options for GeminiStreamClient
326
+ */
327
+ interface GeminiStreamOptions {
328
+ /**
329
+ * Path to Gemini CLI executable
330
+ * @example 'node_modules/@google/gemini-cli/bundle/gemini.js'
331
+ */
332
+ pathToGeminiCLI: string;
333
+ /**
334
+ * Session ID for this client instance
335
+ * Each client manages one session
336
+ */
337
+ sessionId: string;
338
+ /**
339
+ * Workspace ID (optional, for tracking)
340
+ */
341
+ workspaceId?: string;
342
+ /**
343
+ * Google AI API Key
344
+ * Can also be set via GEMINI_API_KEY environment variable
345
+ */
346
+ apiKey?: string;
347
+ /**
348
+ * Model name
349
+ * @default 'gemini-2.0-flash-exp'
350
+ */
351
+ model?: string;
352
+ /**
353
+ * Working directory for CLI execution
354
+ * @default process.cwd()
355
+ */
356
+ cwd?: string;
357
+ /**
358
+ * Approval mode for tool execution
359
+ * @default 'default'
360
+ */
361
+ approvalMode?: 'default' | 'auto_edit' | 'yolo';
362
+ /**
363
+ * Custom environment variables
364
+ */
365
+ env?: Record<string, string>;
366
+ /**
367
+ * Enable debug mode
368
+ * @default false
369
+ */
370
+ debug?: boolean;
371
+ /**
372
+ * Timeout for process initialization (ms)
373
+ * @default 30000
374
+ */
375
+ initTimeout?: number;
376
+ }
292
377
 
293
378
  /**
294
379
  * Core query function for Gemini CLI SDK
@@ -392,6 +477,132 @@ declare class GeminiClient extends EventEmitter {
392
477
  getOptions(): Readonly<GeminiOptions>;
393
478
  }
394
479
 
480
+ /**
481
+ * GeminiStreamClient - Persistent stream-based client for Gemini CLI
482
+ *
483
+ * Architecture:
484
+ * - 1 Client = 1 Node.js Process = 1 Session
485
+ * - Communication via stdin/stdout JSONL
486
+ * - Process stays alive for multiple message exchanges
487
+ * - Similar to Claude Agent SDK's SubprocessCLITransport
488
+ */
489
+
490
+ /**
491
+ * Events emitted by GeminiStreamClient
492
+ */
493
+ interface StreamClientEvents {
494
+ /** JSON stream event from CLI */
495
+ event: (event: JsonStreamEvent) => void;
496
+ /** Process started */
497
+ started: () => void;
498
+ /** Process ready (INIT event received) */
499
+ ready: (initEvent: InitEvent) => void;
500
+ /** Process stopped */
501
+ stopped: (code: number | null) => void;
502
+ /** Error occurred */
503
+ error: (error: Error) => void;
504
+ }
505
+ declare interface GeminiStreamClient {
506
+ on<K extends keyof StreamClientEvents>(event: K, listener: StreamClientEvents[K]): this;
507
+ emit<K extends keyof StreamClientEvents>(event: K, ...args: Parameters<StreamClientEvents[K]>): boolean;
508
+ }
509
+ /**
510
+ * GeminiStreamClient
511
+ *
512
+ * Manages a persistent Gemini CLI process for stream-based communication.
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * const client = new GeminiStreamClient({
517
+ * pathToGeminiCLI: './gemini.js',
518
+ * sessionId: 'session-123',
519
+ * apiKey: process.env.GEMINI_API_KEY,
520
+ * });
521
+ *
522
+ * client.on('event', (event) => {
523
+ * console.log('Event:', event);
524
+ * });
525
+ *
526
+ * await client.start();
527
+ * await client.sendMessage('Hello, Gemini!');
528
+ * await client.stop();
529
+ * ```
530
+ */
531
+ declare class GeminiStreamClient extends EventEmitter$1 {
532
+ private options;
533
+ private process;
534
+ private stdinStream;
535
+ private readlineInterface;
536
+ private status;
537
+ private initEvent;
538
+ private initTimeout;
539
+ constructor(options: GeminiStreamOptions);
540
+ /**
541
+ * Start the Gemini CLI process
542
+ */
543
+ start(): Promise<void>;
544
+ /**
545
+ * Send a user message to the CLI
546
+ */
547
+ sendMessage(content: string): Promise<void>;
548
+ /**
549
+ * Send an interrupt control command
550
+ */
551
+ interrupt(): Promise<void>;
552
+ /**
553
+ * Stop the CLI process
554
+ */
555
+ stop(timeout?: number): Promise<void>;
556
+ /**
557
+ * Check if client is ready to send messages
558
+ */
559
+ isReady(): boolean;
560
+ /**
561
+ * Get current status
562
+ */
563
+ getStatus(): ProcessStatus;
564
+ /**
565
+ * Get init event (contains session_id, model, etc.)
566
+ */
567
+ getInitEvent(): InitEvent | null;
568
+ /**
569
+ * Get process PID
570
+ */
571
+ getPid(): number | undefined;
572
+ /**
573
+ * Build CLI command arguments
574
+ */
575
+ private buildCommand;
576
+ /**
577
+ * Build environment variables
578
+ */
579
+ private buildEnv;
580
+ /**
581
+ * Write a JSON message to stdin
582
+ */
583
+ private writeMessage;
584
+ /**
585
+ * Start reading JSONL events from stdout
586
+ */
587
+ private startReadLoop;
588
+ /**
589
+ * Handle a JSON stream event
590
+ */
591
+ private handleEvent;
592
+ /**
593
+ * Wait for INIT event
594
+ */
595
+ private waitForInit;
596
+ /**
597
+ * Handle process exit
598
+ */
599
+ private handleProcessExit;
600
+ /**
601
+ * Handle process error
602
+ */
603
+ private handleProcessError;
604
+ }
605
+
395
606
  /**
396
607
  * Utility functions for Gemini CLI SDK
397
608
  */
@@ -445,4 +656,4 @@ declare function formatDuration(ms: number): string;
445
656
  */
446
657
  declare function formatTokens(tokens: number): string;
447
658
 
448
- export { type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, type InitEvent, type JsonStreamEvent, JsonStreamEventType, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type StreamStats, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, validateApiKey, validateModel };
659
+ export { type ControlInputMessage, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type InitEvent, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type UserInputMessage, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, validateApiKey, validateModel };