@ketd/gemini-cli-sdk 0.3.6 → 0.3.8

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
@@ -268,7 +268,9 @@ declare enum JsonStreamEventType {
268
268
  /** Message completion with metadata */
269
269
  FINISHED = "finished",
270
270
  /** Model information */
271
- MODEL_INFO = "model_info"
271
+ MODEL_INFO = "model_info",
272
+ /** Ask user questions (CLI → Host) */
273
+ ASK_USER = "ask_user"
272
274
  }
273
275
  /**
274
276
  * Base event interface
@@ -410,10 +412,45 @@ interface ModelInfoEvent extends BaseJsonStreamEvent {
410
412
  type: JsonStreamEventType.MODEL_INFO;
411
413
  value: string;
412
414
  }
415
+ /**
416
+ * Question option for AskUserEvent
417
+ */
418
+ interface QuestionOption {
419
+ label: string;
420
+ description: string;
421
+ }
422
+ /**
423
+ * Question type for AskUserEvent
424
+ */
425
+ declare enum QuestionType {
426
+ CHOICE = "choice",
427
+ TEXT = "text",
428
+ YESNO = "yesno"
429
+ }
430
+ /**
431
+ * Question in AskUserEvent
432
+ */
433
+ interface Question {
434
+ question: string;
435
+ header: string;
436
+ type?: QuestionType;
437
+ options?: QuestionOption[];
438
+ multiSelect?: boolean;
439
+ placeholder?: string;
440
+ }
441
+ /**
442
+ * Ask user event (CLI → Host)
443
+ * Emitted when the ask_user tool needs structured user input
444
+ */
445
+ interface AskUserEvent extends BaseJsonStreamEvent {
446
+ type: JsonStreamEventType.ASK_USER;
447
+ questions: Question[];
448
+ correlation_id: string;
449
+ }
413
450
  /**
414
451
  * Union type of all JSON stream events
415
452
  */
416
- type JsonStreamEvent = InitEvent | MessageEvent | ToolUseEvent | ToolCallRequestEvent | ToolResultEvent | ThoughtEvent | ErrorEvent | ResultEvent | ContentEvent | FinishedEvent | ModelInfoEvent;
453
+ type JsonStreamEvent = InitEvent | MessageEvent | ToolUseEvent | ToolCallRequestEvent | ToolResultEvent | ThoughtEvent | ErrorEvent | ResultEvent | ContentEvent | FinishedEvent | ModelInfoEvent | AskUserEvent;
417
454
  /**
418
455
  * Gemini CLI exit codes
419
456
  */
@@ -491,7 +528,9 @@ declare enum JsonInputMessageType {
491
528
  /** User message */
492
529
  USER = "user",
493
530
  /** Control command */
494
- CONTROL = "control"
531
+ CONTROL = "control",
532
+ /** Response to ask_user event (Host → CLI) */
533
+ ASK_USER_RESPONSE = "ask_user_response"
495
534
  }
496
535
  /**
497
536
  * User message sent to CLI
@@ -506,7 +545,7 @@ interface UserInputMessage {
506
545
  /**
507
546
  * Control subtypes for control messages
508
547
  */
509
- type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session';
548
+ type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session' | 'replace_history';
510
549
  /**
511
550
  * Interrupt control - stop current processing
512
551
  */
@@ -514,13 +553,21 @@ interface InterruptControl {
514
553
  subtype: 'interrupt' | 'cancel' | 'shutdown';
515
554
  }
516
555
  /**
517
- * Truncate history control - remove messages from a specific index
556
+ * Truncate history control - remove messages from a specific index or by user turn count
518
557
  * Used for edit/retry functionality
558
+ *
559
+ * Two modes:
560
+ * - keepUserTurns: Semantic mode — keep N complete user turns (handles tool call Content items correctly)
561
+ * - fromIndex: Raw mode — truncate from a specific Content[] index (legacy, doesn't account for tool calls)
562
+ *
563
+ * When both are provided, keepUserTurns takes precedence.
519
564
  */
520
565
  interface TruncateHistoryControl {
521
566
  subtype: 'truncate_history';
522
- /** Index from which to truncate (0-based, inclusive) */
523
- fromIndex: number;
567
+ /** Index from which to truncate (0-based, inclusive). Used when keepUserTurns is not set. */
568
+ fromIndex?: number;
569
+ /** Number of complete user turns to keep. The CLI scans its history to find the correct Content[] split point. */
570
+ keepUserTurns?: number;
524
571
  }
525
572
  /**
526
573
  * Resume session control - load history from a session file
@@ -531,18 +578,41 @@ interface ResumeSessionControl {
531
578
  /** Path to the session file to resume from */
532
579
  sessionFilePath: string;
533
580
  }
581
+ /**
582
+ * Replace history control - fully replace CLI in-memory history
583
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt
584
+ */
585
+ interface ReplaceHistoryControl {
586
+ subtype: 'replace_history';
587
+ /** Full replacement history as Gemini Content[] format */
588
+ contents: Array<{
589
+ role: string;
590
+ parts: unknown[];
591
+ }>;
592
+ }
534
593
  /**
535
594
  * Control message sent to CLI
536
595
  */
537
596
  interface ControlInputMessage {
538
597
  type: JsonInputMessageType.CONTROL;
539
- control: InterruptControl | TruncateHistoryControl | ResumeSessionControl;
598
+ control: InterruptControl | TruncateHistoryControl | ResumeSessionControl | ReplaceHistoryControl;
540
599
  session_id?: string;
541
600
  }
601
+ /**
602
+ * Response to ask_user event (Host → CLI)
603
+ * Sent when the host has collected user answers for an ask_user request
604
+ */
605
+ interface AskUserResponseInputMessage {
606
+ type: JsonInputMessageType.ASK_USER_RESPONSE;
607
+ correlation_id: string;
608
+ answers: {
609
+ [questionIndex: string]: string;
610
+ };
611
+ }
542
612
  /**
543
613
  * Union type for input messages
544
614
  */
545
- type JsonInputMessage = UserInputMessage | ControlInputMessage;
615
+ type JsonInputMessage = UserInputMessage | ControlInputMessage | AskUserResponseInputMessage;
546
616
  /**
547
617
  * Hook configuration types (from Gemini CLI)
548
618
  */
@@ -733,12 +803,6 @@ interface GeminiStreamOptions {
733
803
  * ```
734
804
  */
735
805
  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
806
  /**
743
807
  * Custom logger implementation
744
808
  * If not provided, uses console with [GeminiStreamClient] prefix
@@ -971,9 +1035,13 @@ declare class GeminiStreamClient extends EventEmitter$1 {
971
1035
  *
972
1036
  * Used for edit/retry functionality where subsequent messages need to be discarded.
973
1037
  *
974
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
1038
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
975
1039
  */
976
- truncateHistory(fromIndex: number): Promise<void>;
1040
+ truncateHistory(options: number | {
1041
+ keepUserTurns: number;
1042
+ } | {
1043
+ fromIndex: number;
1044
+ }): Promise<void>;
977
1045
  /**
978
1046
  * Resume session from a session file
979
1047
  *
@@ -986,6 +1054,33 @@ declare class GeminiStreamClient extends EventEmitter$1 {
986
1054
  * @param sessionFilePath - Path to the session file to resume from
987
1055
  */
988
1056
  resumeSession(sessionFilePath: string): Promise<void>;
1057
+ /**
1058
+ * Replace CLI in-memory history with provided contents
1059
+ *
1060
+ * This sends a control message to the CLI to:
1061
+ * 1. Replace the entire in-memory history with the provided contents
1062
+ * 2. Clear session.json (DB is the source of truth)
1063
+ *
1064
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
1065
+ *
1066
+ * @param contents - Full replacement history in Gemini Content[] format
1067
+ */
1068
+ replaceHistory(contents: Array<{
1069
+ role: string;
1070
+ parts: unknown[];
1071
+ }>): Promise<void>;
1072
+ /**
1073
+ * Send a response to an ask_user request from the CLI
1074
+ *
1075
+ * When the CLI emits an 'ask_user' event (from the ask_user tool),
1076
+ * the host collects user answers and sends them back via this method.
1077
+ *
1078
+ * @param correlationId - The correlation_id from the AskUserEvent
1079
+ * @param answers - Map of question index to user's answer string
1080
+ */
1081
+ sendAskUserResponse(correlationId: string, answers: {
1082
+ [questionIndex: string]: string;
1083
+ }): void;
989
1084
  /**
990
1085
  * Stop the CLI process
991
1086
  */
@@ -1104,4 +1199,4 @@ declare function formatDuration(ms: number): string;
1104
1199
  */
1105
1200
  declare function formatTokens(tokens: number): string;
1106
1201
 
1107
- export { type ContextConfig, type ControlInputMessage, type ControlSubtype, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type HooksConfiguration, type InitEvent, type InterruptControl, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, LogLevel, type Logger, type LoggerOptions, type MCPServerConfig, type MCPServersConfig, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type ResumeSessionControl, SDKLogger, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type ToolsConfig, type TruncateHistoryControl, type UserInputMessage, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
1202
+ export { type AskUserEvent, type AskUserResponseInputMessage, type ContextConfig, type ControlInputMessage, type ControlSubtype, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type HooksConfiguration, type InitEvent, type InterruptControl, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, LogLevel, type Logger, type LoggerOptions, type MCPServerConfig, type MCPServersConfig, type MessageEvent, ProcessStatus, type QueryResult, type Question, type QuestionOption, QuestionType, type ResultEvent, type ResumeSessionControl, SDKLogger, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type ToolsConfig, type TruncateHistoryControl, type UserInputMessage, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
package/dist/index.d.ts CHANGED
@@ -268,7 +268,9 @@ declare enum JsonStreamEventType {
268
268
  /** Message completion with metadata */
269
269
  FINISHED = "finished",
270
270
  /** Model information */
271
- MODEL_INFO = "model_info"
271
+ MODEL_INFO = "model_info",
272
+ /** Ask user questions (CLI → Host) */
273
+ ASK_USER = "ask_user"
272
274
  }
273
275
  /**
274
276
  * Base event interface
@@ -410,10 +412,45 @@ interface ModelInfoEvent extends BaseJsonStreamEvent {
410
412
  type: JsonStreamEventType.MODEL_INFO;
411
413
  value: string;
412
414
  }
415
+ /**
416
+ * Question option for AskUserEvent
417
+ */
418
+ interface QuestionOption {
419
+ label: string;
420
+ description: string;
421
+ }
422
+ /**
423
+ * Question type for AskUserEvent
424
+ */
425
+ declare enum QuestionType {
426
+ CHOICE = "choice",
427
+ TEXT = "text",
428
+ YESNO = "yesno"
429
+ }
430
+ /**
431
+ * Question in AskUserEvent
432
+ */
433
+ interface Question {
434
+ question: string;
435
+ header: string;
436
+ type?: QuestionType;
437
+ options?: QuestionOption[];
438
+ multiSelect?: boolean;
439
+ placeholder?: string;
440
+ }
441
+ /**
442
+ * Ask user event (CLI → Host)
443
+ * Emitted when the ask_user tool needs structured user input
444
+ */
445
+ interface AskUserEvent extends BaseJsonStreamEvent {
446
+ type: JsonStreamEventType.ASK_USER;
447
+ questions: Question[];
448
+ correlation_id: string;
449
+ }
413
450
  /**
414
451
  * Union type of all JSON stream events
415
452
  */
416
- type JsonStreamEvent = InitEvent | MessageEvent | ToolUseEvent | ToolCallRequestEvent | ToolResultEvent | ThoughtEvent | ErrorEvent | ResultEvent | ContentEvent | FinishedEvent | ModelInfoEvent;
453
+ type JsonStreamEvent = InitEvent | MessageEvent | ToolUseEvent | ToolCallRequestEvent | ToolResultEvent | ThoughtEvent | ErrorEvent | ResultEvent | ContentEvent | FinishedEvent | ModelInfoEvent | AskUserEvent;
417
454
  /**
418
455
  * Gemini CLI exit codes
419
456
  */
@@ -491,7 +528,9 @@ declare enum JsonInputMessageType {
491
528
  /** User message */
492
529
  USER = "user",
493
530
  /** Control command */
494
- CONTROL = "control"
531
+ CONTROL = "control",
532
+ /** Response to ask_user event (Host → CLI) */
533
+ ASK_USER_RESPONSE = "ask_user_response"
495
534
  }
496
535
  /**
497
536
  * User message sent to CLI
@@ -506,7 +545,7 @@ interface UserInputMessage {
506
545
  /**
507
546
  * Control subtypes for control messages
508
547
  */
509
- type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session';
548
+ type ControlSubtype = 'interrupt' | 'cancel' | 'shutdown' | 'truncate_history' | 'resume_session' | 'replace_history';
510
549
  /**
511
550
  * Interrupt control - stop current processing
512
551
  */
@@ -514,13 +553,21 @@ interface InterruptControl {
514
553
  subtype: 'interrupt' | 'cancel' | 'shutdown';
515
554
  }
516
555
  /**
517
- * Truncate history control - remove messages from a specific index
556
+ * Truncate history control - remove messages from a specific index or by user turn count
518
557
  * Used for edit/retry functionality
558
+ *
559
+ * Two modes:
560
+ * - keepUserTurns: Semantic mode — keep N complete user turns (handles tool call Content items correctly)
561
+ * - fromIndex: Raw mode — truncate from a specific Content[] index (legacy, doesn't account for tool calls)
562
+ *
563
+ * When both are provided, keepUserTurns takes precedence.
519
564
  */
520
565
  interface TruncateHistoryControl {
521
566
  subtype: 'truncate_history';
522
- /** Index from which to truncate (0-based, inclusive) */
523
- fromIndex: number;
567
+ /** Index from which to truncate (0-based, inclusive). Used when keepUserTurns is not set. */
568
+ fromIndex?: number;
569
+ /** Number of complete user turns to keep. The CLI scans its history to find the correct Content[] split point. */
570
+ keepUserTurns?: number;
524
571
  }
525
572
  /**
526
573
  * Resume session control - load history from a session file
@@ -531,18 +578,41 @@ interface ResumeSessionControl {
531
578
  /** Path to the session file to resume from */
532
579
  sessionFilePath: string;
533
580
  }
581
+ /**
582
+ * Replace history control - fully replace CLI in-memory history
583
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt
584
+ */
585
+ interface ReplaceHistoryControl {
586
+ subtype: 'replace_history';
587
+ /** Full replacement history as Gemini Content[] format */
588
+ contents: Array<{
589
+ role: string;
590
+ parts: unknown[];
591
+ }>;
592
+ }
534
593
  /**
535
594
  * Control message sent to CLI
536
595
  */
537
596
  interface ControlInputMessage {
538
597
  type: JsonInputMessageType.CONTROL;
539
- control: InterruptControl | TruncateHistoryControl | ResumeSessionControl;
598
+ control: InterruptControl | TruncateHistoryControl | ResumeSessionControl | ReplaceHistoryControl;
540
599
  session_id?: string;
541
600
  }
601
+ /**
602
+ * Response to ask_user event (Host → CLI)
603
+ * Sent when the host has collected user answers for an ask_user request
604
+ */
605
+ interface AskUserResponseInputMessage {
606
+ type: JsonInputMessageType.ASK_USER_RESPONSE;
607
+ correlation_id: string;
608
+ answers: {
609
+ [questionIndex: string]: string;
610
+ };
611
+ }
542
612
  /**
543
613
  * Union type for input messages
544
614
  */
545
- type JsonInputMessage = UserInputMessage | ControlInputMessage;
615
+ type JsonInputMessage = UserInputMessage | ControlInputMessage | AskUserResponseInputMessage;
546
616
  /**
547
617
  * Hook configuration types (from Gemini CLI)
548
618
  */
@@ -733,12 +803,6 @@ interface GeminiStreamOptions {
733
803
  * ```
734
804
  */
735
805
  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
806
  /**
743
807
  * Custom logger implementation
744
808
  * If not provided, uses console with [GeminiStreamClient] prefix
@@ -971,9 +1035,13 @@ declare class GeminiStreamClient extends EventEmitter$1 {
971
1035
  *
972
1036
  * Used for edit/retry functionality where subsequent messages need to be discarded.
973
1037
  *
974
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
1038
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
975
1039
  */
976
- truncateHistory(fromIndex: number): Promise<void>;
1040
+ truncateHistory(options: number | {
1041
+ keepUserTurns: number;
1042
+ } | {
1043
+ fromIndex: number;
1044
+ }): Promise<void>;
977
1045
  /**
978
1046
  * Resume session from a session file
979
1047
  *
@@ -986,6 +1054,33 @@ declare class GeminiStreamClient extends EventEmitter$1 {
986
1054
  * @param sessionFilePath - Path to the session file to resume from
987
1055
  */
988
1056
  resumeSession(sessionFilePath: string): Promise<void>;
1057
+ /**
1058
+ * Replace CLI in-memory history with provided contents
1059
+ *
1060
+ * This sends a control message to the CLI to:
1061
+ * 1. Replace the entire in-memory history with the provided contents
1062
+ * 2. Clear session.json (DB is the source of truth)
1063
+ *
1064
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
1065
+ *
1066
+ * @param contents - Full replacement history in Gemini Content[] format
1067
+ */
1068
+ replaceHistory(contents: Array<{
1069
+ role: string;
1070
+ parts: unknown[];
1071
+ }>): Promise<void>;
1072
+ /**
1073
+ * Send a response to an ask_user request from the CLI
1074
+ *
1075
+ * When the CLI emits an 'ask_user' event (from the ask_user tool),
1076
+ * the host collects user answers and sends them back via this method.
1077
+ *
1078
+ * @param correlationId - The correlation_id from the AskUserEvent
1079
+ * @param answers - Map of question index to user's answer string
1080
+ */
1081
+ sendAskUserResponse(correlationId: string, answers: {
1082
+ [questionIndex: string]: string;
1083
+ }): void;
989
1084
  /**
990
1085
  * Stop the CLI process
991
1086
  */
@@ -1104,4 +1199,4 @@ declare function formatDuration(ms: number): string;
1104
1199
  */
1105
1200
  declare function formatTokens(tokens: number): string;
1106
1201
 
1107
- export { type ContextConfig, type ControlInputMessage, type ControlSubtype, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type HooksConfiguration, type InitEvent, type InterruptControl, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, LogLevel, type Logger, type LoggerOptions, type MCPServerConfig, type MCPServersConfig, type MessageEvent, ProcessStatus, type QueryResult, type ResultEvent, type ResumeSessionControl, SDKLogger, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type ToolsConfig, type TruncateHistoryControl, type UserInputMessage, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
1202
+ export { type AskUserEvent, type AskUserResponseInputMessage, type ContextConfig, type ControlInputMessage, type ControlSubtype, type ErrorEvent, ExitCode, GeminiClient, type GeminiOptions, GeminiSDKError, GeminiStreamClient, type GeminiStreamOptions, type HooksConfiguration, type InitEvent, type InterruptControl, type JsonInputMessage, JsonInputMessageType, type JsonStreamEvent, JsonStreamEventType, LogLevel, type Logger, type LoggerOptions, type MCPServerConfig, type MCPServersConfig, type MessageEvent, ProcessStatus, type QueryResult, type Question, type QuestionOption, QuestionType, type ResultEvent, type ResumeSessionControl, SDKLogger, type StreamStats, type ThoughtEvent, type ToolPermissionDecision, type ToolPermissionRequest, type ToolResultEvent, type ToolUseEvent, type ToolsConfig, type TruncateHistoryControl, type UserInputMessage, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
package/dist/index.js CHANGED
@@ -19,8 +19,15 @@ var JsonStreamEventType = /* @__PURE__ */ ((JsonStreamEventType2) => {
19
19
  JsonStreamEventType2["CONTENT"] = "content";
20
20
  JsonStreamEventType2["FINISHED"] = "finished";
21
21
  JsonStreamEventType2["MODEL_INFO"] = "model_info";
22
+ JsonStreamEventType2["ASK_USER"] = "ask_user";
22
23
  return JsonStreamEventType2;
23
24
  })(JsonStreamEventType || {});
25
+ var QuestionType = /* @__PURE__ */ ((QuestionType2) => {
26
+ QuestionType2["CHOICE"] = "choice";
27
+ QuestionType2["TEXT"] = "text";
28
+ QuestionType2["YESNO"] = "yesno";
29
+ return QuestionType2;
30
+ })(QuestionType || {});
24
31
  var ExitCode = /* @__PURE__ */ ((ExitCode2) => {
25
32
  ExitCode2[ExitCode2["SUCCESS"] = 0] = "SUCCESS";
26
33
  ExitCode2[ExitCode2["GENERAL_ERROR"] = 1] = "GENERAL_ERROR";
@@ -50,6 +57,7 @@ var GeminiSDKError = class _GeminiSDKError extends Error {
50
57
  var JsonInputMessageType = /* @__PURE__ */ ((JsonInputMessageType2) => {
51
58
  JsonInputMessageType2["USER"] = "user";
52
59
  JsonInputMessageType2["CONTROL"] = "control";
60
+ JsonInputMessageType2["ASK_USER_RESPONSE"] = "ask_user_response";
53
61
  return JsonInputMessageType2;
54
62
  })(JsonInputMessageType || {});
55
63
 
@@ -579,21 +587,32 @@ var GeminiStreamClient = class extends EventEmitter {
579
587
  *
580
588
  * Used for edit/retry functionality where subsequent messages need to be discarded.
581
589
  *
582
- * @param fromIndex - Index from which to truncate (0-based, inclusive)
590
+ * @param options - Truncation options: either keepUserTurns (preferred) or fromIndex (legacy)
583
591
  */
584
- async truncateHistory(fromIndex) {
592
+ async truncateHistory(options) {
585
593
  if (!this.isReady()) {
586
594
  throw new GeminiSDKError("Client not ready. Call start() first.");
587
595
  }
588
- if (fromIndex < 0) {
589
- throw new GeminiSDKError("fromIndex must be non-negative");
596
+ let control;
597
+ if (typeof options === "number") {
598
+ if (options < 0) {
599
+ throw new GeminiSDKError("fromIndex must be non-negative");
600
+ }
601
+ control = { subtype: "truncate_history", fromIndex: options };
602
+ } else if ("keepUserTurns" in options) {
603
+ if (options.keepUserTurns < 0) {
604
+ throw new GeminiSDKError("keepUserTurns must be non-negative");
605
+ }
606
+ control = { subtype: "truncate_history", keepUserTurns: options.keepUserTurns };
607
+ } else {
608
+ if (options.fromIndex < 0) {
609
+ throw new GeminiSDKError("fromIndex must be non-negative");
610
+ }
611
+ control = { subtype: "truncate_history", fromIndex: options.fromIndex };
590
612
  }
591
613
  const message = {
592
614
  type: "control" /* CONTROL */,
593
- control: {
594
- subtype: "truncate_history",
595
- fromIndex
596
- },
615
+ control,
597
616
  session_id: this.options.sessionId
598
617
  };
599
618
  this.writeMessage(message);
@@ -626,6 +645,51 @@ var GeminiStreamClient = class extends EventEmitter {
626
645
  };
627
646
  this.writeMessage(message);
628
647
  }
648
+ /**
649
+ * Replace CLI in-memory history with provided contents
650
+ *
651
+ * This sends a control message to the CLI to:
652
+ * 1. Replace the entire in-memory history with the provided contents
653
+ * 2. Clear session.json (DB is the source of truth)
654
+ *
655
+ * Used when messages are deleted from DB and CLI history needs to be rebuilt.
656
+ *
657
+ * @param contents - Full replacement history in Gemini Content[] format
658
+ */
659
+ async replaceHistory(contents) {
660
+ if (!this.isReady()) {
661
+ throw new GeminiSDKError("Client not ready. Call start() first.");
662
+ }
663
+ const message = {
664
+ type: "control" /* CONTROL */,
665
+ control: {
666
+ subtype: "replace_history",
667
+ contents
668
+ },
669
+ session_id: this.options.sessionId
670
+ };
671
+ this.writeMessage(message);
672
+ }
673
+ /**
674
+ * Send a response to an ask_user request from the CLI
675
+ *
676
+ * When the CLI emits an 'ask_user' event (from the ask_user tool),
677
+ * the host collects user answers and sends them back via this method.
678
+ *
679
+ * @param correlationId - The correlation_id from the AskUserEvent
680
+ * @param answers - Map of question index to user's answer string
681
+ */
682
+ sendAskUserResponse(correlationId, answers) {
683
+ if (!this.isReady()) {
684
+ throw new GeminiSDKError("Client not ready. Call start() first.");
685
+ }
686
+ const message = {
687
+ type: "ask_user_response" /* ASK_USER_RESPONSE */,
688
+ correlation_id: correlationId,
689
+ answers
690
+ };
691
+ this.writeMessage(message);
692
+ }
629
693
  /**
630
694
  * Stop the CLI process
631
695
  */
@@ -768,10 +832,6 @@ var GeminiStreamClient = class extends EventEmitter {
768
832
  "--output-format",
769
833
  "stream-json"
770
834
  ];
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
835
  if (this.options.model) {
776
836
  args.push("--model", this.options.model);
777
837
  }
@@ -989,6 +1049,6 @@ function formatTokens(tokens) {
989
1049
  return tokens.toLocaleString();
990
1050
  }
991
1051
 
992
- export { ExitCode, GeminiClient, GeminiSDKError, GeminiStreamClient, JsonInputMessageType, JsonStreamEventType, LogLevel, ProcessStatus, SDKLogger, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
1052
+ export { ExitCode, GeminiClient, GeminiSDKError, GeminiStreamClient, JsonInputMessageType, JsonStreamEventType, LogLevel, ProcessStatus, QuestionType, SDKLogger, createLogger, findGeminiCLI, formatDuration, formatTokens, getApiKey, query, sdkLogger, silentLogger, validateApiKey, validateModel };
993
1053
  //# sourceMappingURL=index.js.map
994
1054
  //# sourceMappingURL=index.js.map