@ketd/gemini-cli-sdk 0.3.6 → 0.3.7

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
@@ -506,7 +506,7 @@ interface UserInputMessage {
506
506
  /**
507
507
  * Control subtypes for control messages
508
508
  */
509
- type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session';
509
+ type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session' | 'replace_history';
510
510
  /**
511
511
  * Interrupt control - stop current processing
512
512
  */
@@ -514,13 +514,21 @@ interface InterruptControl {
514
514
  subtype: 'interrupt' | 'cancel' | 'shutdown';
515
515
  }
516
516
  /**
517
- * Truncate history control - remove messages from a specific index
517
+ * Truncate history control - remove messages from a specific index or by user turn count
518
518
  * Used for edit/retry functionality
519
+ *
520
+ * Two modes:
521
+ * - keepUserTurns: Semantic mode — keep N complete user turns (handles tool call Content items correctly)
522
+ * - fromIndex: Raw mode — truncate from a specific Content[] index (legacy, doesn't account for tool calls)
523
+ *
524
+ * When both are provided, keepUserTurns takes precedence.
519
525
  */
520
526
  interface TruncateHistoryControl {
521
527
  subtype: 'truncate_history';
522
- /** Index from which to truncate (0-based, inclusive) */
523
- fromIndex: number;
528
+ /** Index from which to truncate (0-based, inclusive). Used when keepUserTurns is not set. */
529
+ fromIndex?: number;
530
+ /** Number of complete user turns to keep. The CLI scans its history to find the correct Content[] split point. */
531
+ keepUserTurns?: number;
524
532
  }
525
533
  /**
526
534
  * Resume session control - load history from a session file
@@ -531,12 +539,24 @@ interface ResumeSessionControl {
531
539
  /** Path to the session file to resume from */
532
540
  sessionFilePath: string;
533
541
  }
542
+ /**
543
+ * Replace history control - fully replace CLI in-memory history
544
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt
545
+ */
546
+ interface ReplaceHistoryControl {
547
+ subtype: 'replace_history';
548
+ /** Full replacement history as Gemini Content[] format */
549
+ contents: Array<{
550
+ role: string;
551
+ parts: unknown[];
552
+ }>;
553
+ }
534
554
  /**
535
555
  * Control message sent to CLI
536
556
  */
537
557
  interface ControlInputMessage {
538
558
  type: JsonInputMessageType.CONTROL;
539
- control: InterruptControl | TruncateHistoryControl | ResumeSessionControl;
559
+ control: InterruptControl | TruncateHistoryControl | ResumeSessionControl | ReplaceHistoryControl;
540
560
  session_id?: string;
541
561
  }
542
562
  /**
@@ -733,12 +753,6 @@ interface GeminiStreamOptions {
733
753
  * ```
734
754
  */
735
755
  mcpServers?: MCPServersConfig;
736
- /**
737
- * Resume from a previous session file path
738
- * If provided, Gemini CLI will load the session history using --resume flag
739
- * @example '/path/to/session-2025-01-01T12-00-abc123.json'
740
- */
741
- resumeSessionFilePath?: string;
742
756
  /**
743
757
  * Custom logger implementation
744
758
  * If not provided, uses console with [GeminiStreamClient] prefix
@@ -971,9 +985,13 @@ declare class GeminiStreamClient extends EventEmitter$1 {
971
985
  *
972
986
  * Used for edit/retry functionality where subsequent messages need to be discarded.
973
987
  *
974
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
988
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
975
989
  */
976
- truncateHistory(fromIndex: number): Promise<void>;
990
+ truncateHistory(options: number | {
991
+ keepUserTurns: number;
992
+ } | {
993
+ fromIndex: number;
994
+ }): Promise<void>;
977
995
  /**
978
996
  * Resume session from a session file
979
997
  *
@@ -986,6 +1004,21 @@ declare class GeminiStreamClient extends EventEmitter$1 {
986
1004
  * @param sessionFilePath - Path to the session file to resume from
987
1005
  */
988
1006
  resumeSession(sessionFilePath: string): Promise<void>;
1007
+ /**
1008
+ * Replace CLI in-memory history with provided contents
1009
+ *
1010
+ * This sends a control message to the CLI to:
1011
+ * 1. Replace the entire in-memory history with the provided contents
1012
+ * 2. Clear session.json (DB is the source of truth)
1013
+ *
1014
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
1015
+ *
1016
+ * @param contents - Full replacement history in Gemini Content[] format
1017
+ */
1018
+ replaceHistory(contents: Array<{
1019
+ role: string;
1020
+ parts: unknown[];
1021
+ }>): Promise<void>;
989
1022
  /**
990
1023
  * Stop the CLI process
991
1024
  */
package/dist/index.d.ts CHANGED
@@ -506,7 +506,7 @@ interface UserInputMessage {
506
506
  /**
507
507
  * Control subtypes for control messages
508
508
  */
509
- type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session';
509
+ type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session' | 'replace_history';
510
510
  /**
511
511
  * Interrupt control - stop current processing
512
512
  */
@@ -514,13 +514,21 @@ interface InterruptControl {
514
514
  subtype: 'interrupt' | 'cancel' | 'shutdown';
515
515
  }
516
516
  /**
517
- * Truncate history control - remove messages from a specific index
517
+ * Truncate history control - remove messages from a specific index or by user turn count
518
518
  * Used for edit/retry functionality
519
+ *
520
+ * Two modes:
521
+ * - keepUserTurns: Semantic mode — keep N complete user turns (handles tool call Content items correctly)
522
+ * - fromIndex: Raw mode — truncate from a specific Content[] index (legacy, doesn't account for tool calls)
523
+ *
524
+ * When both are provided, keepUserTurns takes precedence.
519
525
  */
520
526
  interface TruncateHistoryControl {
521
527
  subtype: 'truncate_history';
522
- /** Index from which to truncate (0-based, inclusive) */
523
- fromIndex: number;
528
+ /** Index from which to truncate (0-based, inclusive). Used when keepUserTurns is not set. */
529
+ fromIndex?: number;
530
+ /** Number of complete user turns to keep. The CLI scans its history to find the correct Content[] split point. */
531
+ keepUserTurns?: number;
524
532
  }
525
533
  /**
526
534
  * Resume session control - load history from a session file
@@ -531,12 +539,24 @@ interface ResumeSessionControl {
531
539
  /** Path to the session file to resume from */
532
540
  sessionFilePath: string;
533
541
  }
542
+ /**
543
+ * Replace history control - fully replace CLI in-memory history
544
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt
545
+ */
546
+ interface ReplaceHistoryControl {
547
+ subtype: 'replace_history';
548
+ /** Full replacement history as Gemini Content[] format */
549
+ contents: Array<{
550
+ role: string;
551
+ parts: unknown[];
552
+ }>;
553
+ }
534
554
  /**
535
555
  * Control message sent to CLI
536
556
  */
537
557
  interface ControlInputMessage {
538
558
  type: JsonInputMessageType.CONTROL;
539
- control: InterruptControl | TruncateHistoryControl | ResumeSessionControl;
559
+ control: InterruptControl | TruncateHistoryControl | ResumeSessionControl | ReplaceHistoryControl;
540
560
  session_id?: string;
541
561
  }
542
562
  /**
@@ -733,12 +753,6 @@ interface GeminiStreamOptions {
733
753
  * ```
734
754
  */
735
755
  mcpServers?: MCPServersConfig;
736
- /**
737
- * Resume from a previous session file path
738
- * If provided, Gemini CLI will load the session history using --resume flag
739
- * @example '/path/to/session-2025-01-01T12-00-abc123.json'
740
- */
741
- resumeSessionFilePath?: string;
742
756
  /**
743
757
  * Custom logger implementation
744
758
  * If not provided, uses console with [GeminiStreamClient] prefix
@@ -971,9 +985,13 @@ declare class GeminiStreamClient extends EventEmitter$1 {
971
985
  *
972
986
  * Used for edit/retry functionality where subsequent messages need to be discarded.
973
987
  *
974
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
988
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
975
989
  */
976
- truncateHistory(fromIndex: number): Promise<void>;
990
+ truncateHistory(options: number | {
991
+ keepUserTurns: number;
992
+ } | {
993
+ fromIndex: number;
994
+ }): Promise<void>;
977
995
  /**
978
996
  * Resume session from a session file
979
997
  *
@@ -986,6 +1004,21 @@ declare class GeminiStreamClient extends EventEmitter$1 {
986
1004
  * @param sessionFilePath - Path to the session file to resume from
987
1005
  */
988
1006
  resumeSession(sessionFilePath: string): Promise<void>;
1007
+ /**
1008
+ * Replace CLI in-memory history with provided contents
1009
+ *
1010
+ * This sends a control message to the CLI to:
1011
+ * 1. Replace the entire in-memory history with the provided contents
1012
+ * 2. Clear session.json (DB is the source of truth)
1013
+ *
1014
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
1015
+ *
1016
+ * @param contents - Full replacement history in Gemini Content[] format
1017
+ */
1018
+ replaceHistory(contents: Array<{
1019
+ role: string;
1020
+ parts: unknown[];
1021
+ }>): Promise<void>;
989
1022
  /**
990
1023
  * Stop the CLI process
991
1024
  */
package/dist/index.js CHANGED
@@ -579,21 +579,32 @@ var GeminiStreamClient = class extends EventEmitter {
579
579
  *
580
580
  * Used for edit/retry functionality where subsequent messages need to be discarded.
581
581
  *
582
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
582
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
583
583
  */
584
- async truncateHistory(fromIndex) {
584
+ async truncateHistory(options) {
585
585
  if (!this.isReady()) {
586
586
  throw new GeminiSDKError("Client not ready. Call start() first.");
587
587
  }
588
- if (fromIndex < 0) {
589
- throw new GeminiSDKError("fromIndex must be non-negative");
588
+ let control;
589
+ if (typeof options === "number") {
590
+ if (options < 0) {
591
+ throw new GeminiSDKError("fromIndex must be non-negative");
592
+ }
593
+ control = { subtype: "truncate_history", fromIndex: options };
594
+ } else if ("keepUserTurns" in options) {
595
+ if (options.keepUserTurns < 0) {
596
+ throw new GeminiSDKError("keepUserTurns must be non-negative");
597
+ }
598
+ control = { subtype: "truncate_history", keepUserTurns: options.keepUserTurns };
599
+ } else {
600
+ if (options.fromIndex < 0) {
601
+ throw new GeminiSDKError("fromIndex must be non-negative");
602
+ }
603
+ control = { subtype: "truncate_history", fromIndex: options.fromIndex };
590
604
  }
591
605
  const message = {
592
606
  type: "control" /* CONTROL */,
593
- control: {
594
- subtype: "truncate_history",
595
- fromIndex
596
- },
607
+ control,
597
608
  session_id: this.options.sessionId
598
609
  };
599
610
  this.writeMessage(message);
@@ -626,6 +637,31 @@ var GeminiStreamClient = class extends EventEmitter {
626
637
  };
627
638
  this.writeMessage(message);
628
639
  }
640
+ /**
641
+ * Replace CLI in-memory history with provided contents
642
+ *
643
+ * This sends a control message to the CLI to:
644
+ * 1. Replace the entire in-memory history with the provided contents
645
+ * 2. Clear session.json (DB is the source of truth)
646
+ *
647
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
648
+ *
649
+ * @param contents - Full replacement history in Gemini Content[] format
650
+ */
651
+ async replaceHistory(contents) {
652
+ if (!this.isReady()) {
653
+ throw new GeminiSDKError("Client not ready. Call start() first.");
654
+ }
655
+ const message = {
656
+ type: "control" /* CONTROL */,
657
+ control: {
658
+ subtype: "replace_history",
659
+ contents
660
+ },
661
+ session_id: this.options.sessionId
662
+ };
663
+ this.writeMessage(message);
664
+ }
629
665
  /**
630
666
  * Stop the CLI process
631
667
  */
@@ -768,10 +804,6 @@ var GeminiStreamClient = class extends EventEmitter {
768
804
  "--output-format",
769
805
  "stream-json"
770
806
  ];
771
- if (this.options.resumeSessionFilePath) {
772
- args.push("--resume-from-file", this.options.resumeSessionFilePath);
773
- this.logger.debug("Resuming from session file:", this.options.resumeSessionFilePath);
774
- }
775
807
  if (this.options.model) {
776
808
  args.push("--model", this.options.model);
777
809
  }