Sync upstream Gemini-CLI v0.8.2 (#838)

This commit is contained in:
tanzhenxin
2025-10-23 09:27:04 +08:00
committed by GitHub
parent 096fabb5d6
commit eb95c131be
644 changed files with 70389 additions and 23709 deletions

View File

@@ -4,34 +4,43 @@
* SPDX-License-Identifier: Apache-2.0
*/
import type { Content } from '@google/genai';
// Node built-ins
import type { EventEmitter } from 'node:events';
import * as path from 'node:path';
import process from 'node:process';
// External dependencies
import { ProxyAgent, setGlobalDispatcher } from 'undici';
// Types
import type {
ContentGenerator,
ContentGeneratorConfig,
} from '../core/contentGenerator.js';
import type { FallbackModelHandler } from '../fallback/types.js';
import type { MCPOAuthConfig } from '../mcp/oauth-provider.js';
import type { ShellExecutionConfig } from '../services/shellExecutionService.js';
import type { AnyToolInvocation } from '../tools/tools.js';
// Core
import { BaseLlmClient } from '../core/baseLlmClient.js';
import { GeminiClient } from '../core/client.js';
import type { ContentGeneratorConfig } from '../core/contentGenerator.js';
import {
AuthType,
createContentGenerator,
createContentGeneratorConfig,
} from '../core/contentGenerator.js';
import { IdeClient } from '../ide/ide-client.js';
import type { MCPOAuthConfig } from '../mcp/oauth-provider.js';
import { PromptRegistry } from '../prompts/prompt-registry.js';
import { tokenLimit } from '../core/tokenLimits.js';
// Services
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
import {
type FileSystemService,
StandardFileSystemService,
} from '../services/fileSystemService.js';
import { GitService } from '../services/gitService.js';
import { SubagentManager } from '../subagents/subagent-manager.js';
import type { TelemetryTarget } from '../telemetry/index.js';
import {
DEFAULT_OTLP_ENDPOINT,
DEFAULT_TELEMETRY_TARGET,
initializeTelemetry,
StartSessionEvent,
} from '../telemetry/index.js';
import { logCliConfiguration, logIdeConnection } from '../telemetry/loggers.js';
import { IdeConnectionEvent, IdeConnectionType } from '../telemetry/types.js';
// Tools
import { EditTool } from '../tools/edit.js';
import { ExitPlanModeTool } from '../tools/exitPlanMode.js';
import { GlobTool } from '../tools/glob.js';
@@ -40,27 +49,53 @@ import { LSTool } from '../tools/ls.js';
import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js';
import { ReadFileTool } from '../tools/read-file.js';
import { ReadManyFilesTool } from '../tools/read-many-files.js';
import { RipGrepTool } from '../tools/ripGrep.js';
import { canUseRipgrep, RipGrepTool } from '../tools/ripGrep.js';
import { ShellTool } from '../tools/shell.js';
import { SmartEditTool } from '../tools/smart-edit.js';
import { TaskTool } from '../tools/task.js';
import { TodoWriteTool } from '../tools/todoWrite.js';
import { ToolRegistry } from '../tools/tool-registry.js';
import type { AnyToolInvocation } from '../tools/tools.js';
import { WebFetchTool } from '../tools/web-fetch.js';
import { WebSearchTool } from '../tools/web-search.js';
import { WriteFileTool } from '../tools/write-file.js';
// Other modules
import { ideContextStore } from '../ide/ideContext.js';
import { OutputFormat } from '../output/types.js';
import { PromptRegistry } from '../prompts/prompt-registry.js';
import { SubagentManager } from '../subagents/subagent-manager.js';
import {
DEFAULT_OTLP_ENDPOINT,
DEFAULT_TELEMETRY_TARGET,
initializeTelemetry,
logCliConfiguration,
logRipgrepFallback,
RipgrepFallbackEvent,
StartSessionEvent,
type TelemetryTarget,
uiTelemetryService,
} from '../telemetry/index.js';
// Utils
import { shouldAttemptBrowserLaunch } from '../utils/browser.js';
import { FileExclusions } from '../utils/ignorePatterns.js';
import { WorkspaceContext } from '../utils/workspaceContext.js';
import {
DEFAULT_GEMINI_EMBEDDING_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
} from './models.js';
import { Storage } from './storage.js';
import { Logger, type ModelSwitchEvent } from '../core/logger.js';
// Re-export OAuth config type
export type { AnyToolInvocation, MCPOAuthConfig };
// Local config modules
import type { FileFilteringOptions } from './constants.js';
import {
DEFAULT_FILE_FILTERING_OPTIONS,
DEFAULT_MEMORY_FILE_FILTERING_OPTIONS,
} from './constants.js';
import { DEFAULT_QWEN_EMBEDDING_MODEL } from './models.js';
import { Storage } from './storage.js';
// Re-export types
export type { AnyToolInvocation, FileFilteringOptions, MCPOAuthConfig };
export {
DEFAULT_FILE_FILTERING_OPTIONS,
DEFAULT_MEMORY_FILE_FILTERING_OPTIONS,
};
export enum ApprovalMode {
PLAN = 'plan',
@@ -95,6 +130,11 @@ export interface TelemetrySettings {
otlpProtocol?: 'grpc' | 'http';
logPrompts?: boolean;
outfile?: string;
useCollector?: boolean;
}
export interface OutputSettings {
format?: OutputFormat;
}
export interface GitCoAuthorSettings {
@@ -108,21 +148,20 @@ export interface GeminiCLIExtension {
version: string;
isActive: boolean;
path: string;
installMetadata?: ExtensionInstallMetadata;
}
export interface FileFilteringOptions {
respectGitIgnore: boolean;
respectGeminiIgnore: boolean;
export interface ExtensionInstallMetadata {
source: string;
type: 'git' | 'local' | 'link' | 'github-release';
releaseTag?: string; // Only present for github-release installs.
ref?: string;
autoUpdate?: boolean;
}
// For memory files
export const DEFAULT_MEMORY_FILE_FILTERING_OPTIONS: FileFilteringOptions = {
respectGitIgnore: false,
respectGeminiIgnore: true,
};
// For all other files
export const DEFAULT_FILE_FILTERING_OPTIONS: FileFilteringOptions = {
respectGitIgnore: true,
respectGeminiIgnore: true,
};
export const DEFAULT_TRUNCATE_TOOL_OUTPUT_THRESHOLD = 4_000_000;
export const DEFAULT_TRUNCATE_TOOL_OUTPUT_LINES = 1000;
export class MCPServerConfig {
constructor(
// For stdio transport
@@ -148,12 +187,18 @@ export class MCPServerConfig {
// OAuth configuration
readonly oauth?: MCPOAuthConfig,
readonly authProviderType?: AuthProviderType,
// Service Account Configuration
/* targetAudience format: CLIENT_ID.apps.googleusercontent.com */
readonly targetAudience?: string,
/* targetServiceAccount format: <service-account-name>@<project-num>.iam.gserviceaccount.com */
readonly targetServiceAccount?: string,
) {}
}
export enum AuthProviderType {
DYNAMIC_DISCOVERY = 'dynamic_discovery',
GOOGLE_CREDENTIALS = 'google_credentials',
SERVICE_ACCOUNT_IMPERSONATION = 'service_account_impersonation',
}
export interface SandboxConfig {
@@ -161,12 +206,6 @@ export interface SandboxConfig {
image: string;
}
export type FlashFallbackHandler = (
currentModel: string,
fallbackModel: string,
error?: unknown,
) => Promise<boolean | string | null>;
export interface ConfigParameters {
sessionId: string;
embeddingModel?: string;
@@ -193,7 +232,7 @@ export interface ConfigParameters {
usageStatisticsEnabled?: boolean;
fileFiltering?: {
respectGitIgnore?: boolean;
respectGeminiIgnore?: boolean;
respectQwenIgnore?: boolean;
enableRecursiveFileSearch?: boolean;
disableFuzzySearch?: boolean;
};
@@ -216,21 +255,8 @@ export interface ConfigParameters {
folderTrustFeature?: boolean;
folderTrust?: boolean;
ideMode?: boolean;
enableOpenAILogging?: boolean;
systemPromptMappings?: Array<{
baseUrls: string[];
modelNames: string[];
template: string;
}>;
authType?: AuthType;
contentGenerator?: {
timeout?: number;
maxRetries?: number;
disableCacheControl?: boolean;
samplingParams?: {
[key: string]: unknown;
};
};
generationConfig?: Partial<ContentGeneratorConfig>;
cliVersion?: string;
loadMemoryFromIncludeDirectories?: boolean;
// Web search providers
@@ -241,19 +267,28 @@ export interface ConfigParameters {
useRipgrep?: boolean;
shouldUseNodePtyShell?: boolean;
skipNextSpeakerCheck?: boolean;
shellExecutionConfig?: ShellExecutionConfig;
extensionManagement?: boolean;
enablePromptCompletion?: boolean;
skipLoopDetection?: boolean;
vlmSwitchMode?: string;
truncateToolOutputThreshold?: number;
truncateToolOutputLines?: number;
enableToolOutputTruncation?: boolean;
eventEmitter?: EventEmitter;
useSmartEdit?: boolean;
output?: OutputSettings;
}
export class Config {
private toolRegistry!: ToolRegistry;
private promptRegistry!: PromptRegistry;
private subagentManager!: SubagentManager;
private sessionId: string;
private readonly sessionId: string;
private fileSystemService: FileSystemService;
private contentGeneratorConfig!: ContentGeneratorConfig;
private contentGenerator!: ContentGenerator;
private readonly _generationConfig: ContentGeneratorConfig;
private readonly embeddingModel: string;
private readonly sandbox: SandboxConfig | undefined;
private readonly targetDir: string;
@@ -277,9 +312,10 @@ export class Config {
private readonly gitCoAuthor: GitCoAuthorSettings;
private readonly usageStatisticsEnabled: boolean;
private geminiClient!: GeminiClient;
private baseLlmClient!: BaseLlmClient;
private readonly fileFiltering: {
respectGitIgnore: boolean;
respectGeminiIgnore: boolean;
respectQwenIgnore: boolean;
enableRecursiveFileSearch: boolean;
disableFuzzySearch: boolean;
};
@@ -289,19 +325,13 @@ export class Config {
private readonly proxy: string | undefined;
private readonly cwd: string;
private readonly bugCommand: BugCommandSettings | undefined;
private readonly model: string;
private readonly extensionContextFilePaths: string[];
private readonly noBrowser: boolean;
private readonly folderTrustFeature: boolean;
private readonly folderTrust: boolean;
private ideMode: boolean;
private ideClient!: IdeClient;
private inFallbackMode = false;
private readonly systemPromptMappings?: Array<{
baseUrls?: string[];
modelNames?: string[];
template?: string;
}>;
private readonly maxSessionTurns: number;
private readonly sessionTokenLimit: number;
private readonly listExtensions: boolean;
@@ -310,19 +340,11 @@ export class Config {
name: string;
extensionName: string;
}>;
flashFallbackHandler?: FlashFallbackHandler;
fallbackModelHandler?: FallbackModelHandler;
private quotaErrorOccurred: boolean = false;
private readonly summarizeToolOutput:
| Record<string, SummarizeToolOutputSettings>
| undefined;
private authType?: AuthType;
private readonly enableOpenAILogging: boolean;
private readonly contentGenerator?: {
timeout?: number;
maxRetries?: number;
disableCacheControl?: boolean;
samplingParams?: Record<string, unknown>;
};
private readonly cliVersion?: string;
private readonly experimentalZedIntegration: boolean = false;
private readonly loadMemoryFromIncludeDirectories: boolean = false;
@@ -333,19 +355,24 @@ export class Config {
private readonly useRipgrep: boolean;
private readonly shouldUseNodePtyShell: boolean;
private readonly skipNextSpeakerCheck: boolean;
private readonly extensionManagement: boolean;
private shellExecutionConfig: ShellExecutionConfig;
private readonly extensionManagement: boolean = true;
private readonly enablePromptCompletion: boolean = false;
private readonly skipLoopDetection: boolean;
private readonly vlmSwitchMode: string | undefined;
private initialized: boolean = false;
readonly storage: Storage;
private readonly fileExclusions: FileExclusions;
private logger: Logger | null = null;
private readonly truncateToolOutputThreshold: number;
private readonly truncateToolOutputLines: number;
private readonly enableToolOutputTruncation: boolean;
private readonly eventEmitter?: EventEmitter;
private readonly useSmartEdit: boolean;
private readonly outputSettings: OutputSettings;
constructor(params: ConfigParameters) {
this.sessionId = params.sessionId;
this.embeddingModel =
params.embeddingModel ?? DEFAULT_GEMINI_EMBEDDING_MODEL;
this.embeddingModel = params.embeddingModel ?? DEFAULT_QWEN_EMBEDDING_MODEL;
this.fileSystemService = new StandardFileSystemService();
this.sandbox = params.sandbox;
this.targetDir = path.resolve(params.targetDir);
@@ -375,6 +402,7 @@ export class Config {
otlpProtocol: params.telemetry?.otlpProtocol,
logPrompts: params.telemetry?.logPrompts ?? true,
outfile: params.telemetry?.outfile,
useCollector: params.telemetry?.useCollector,
};
this.gitCoAuthor = {
enabled: params.gitCoAuthor?.enabled ?? true,
@@ -385,7 +413,7 @@ export class Config {
this.fileFiltering = {
respectGitIgnore: params.fileFiltering?.respectGitIgnore ?? true,
respectGeminiIgnore: params.fileFiltering?.respectGeminiIgnore ?? true,
respectQwenIgnore: params.fileFiltering?.respectQwenIgnore ?? true,
enableRecursiveFileSearch:
params.fileFiltering?.enableRecursiveFileSearch ?? true,
disableFuzzySearch: params.fileFiltering?.disableFuzzySearch ?? false,
@@ -395,7 +423,6 @@ export class Config {
this.cwd = params.cwd ?? process.cwd();
this.fileDiscoveryService = params.fileDiscoveryService ?? null;
this.bugCommand = params.bugCommand;
this.model = params.model;
this.extensionContextFilePaths = params.extensionContextFilePaths ?? [];
this.maxSessionTurns = params.maxSessionTurns ?? -1;
this.sessionTokenLimit = params.sessionTokenLimit ?? -1;
@@ -409,10 +436,11 @@ export class Config {
this.folderTrustFeature = params.folderTrustFeature ?? false;
this.folderTrust = params.folderTrust ?? false;
this.ideMode = params.ideMode ?? false;
this.systemPromptMappings = params.systemPromptMappings;
this.authType = params.authType;
this.enableOpenAILogging = params.enableOpenAILogging ?? false;
this.contentGenerator = params.contentGenerator;
this._generationConfig = {
model: params.model,
...(params.generationConfig || {}),
};
this.contentGeneratorConfig = this._generationConfig;
this.cliVersion = params.cliVersion;
this.loadMemoryFromIncludeDirectories =
@@ -426,20 +454,31 @@ export class Config {
// Web search
this.tavilyApiKey = params.tavilyApiKey;
this.useRipgrep = params.useRipgrep ?? false;
this.useRipgrep = params.useRipgrep ?? true;
this.shouldUseNodePtyShell = params.shouldUseNodePtyShell ?? false;
this.skipNextSpeakerCheck = params.skipNextSpeakerCheck ?? false;
this.extensionManagement = params.extensionManagement ?? false;
this.skipNextSpeakerCheck = params.skipNextSpeakerCheck ?? true;
this.shellExecutionConfig = {
terminalWidth: params.shellExecutionConfig?.terminalWidth ?? 80,
terminalHeight: params.shellExecutionConfig?.terminalHeight ?? 24,
showColor: params.shellExecutionConfig?.showColor ?? false,
pager: params.shellExecutionConfig?.pager ?? 'cat',
};
this.truncateToolOutputThreshold =
params.truncateToolOutputThreshold ??
DEFAULT_TRUNCATE_TOOL_OUTPUT_THRESHOLD;
this.truncateToolOutputLines =
params.truncateToolOutputLines ?? DEFAULT_TRUNCATE_TOOL_OUTPUT_LINES;
this.enableToolOutputTruncation = params.enableToolOutputTruncation ?? true;
this.useSmartEdit = params.useSmartEdit ?? false;
this.extensionManagement = params.extensionManagement ?? true;
this.storage = new Storage(this.targetDir);
this.enablePromptCompletion = params.enablePromptCompletion ?? false;
this.vlmSwitchMode = params.vlmSwitchMode;
this.fileExclusions = new FileExclusions(this);
// Initialize logger asynchronously
this.logger = new Logger(this.sessionId, this.storage);
this.logger.initialize().catch((error) => {
console.debug('Failed to initialize logger:', error);
});
this.eventEmitter = params.eventEmitter;
this.outputSettings = {
format: params.output?.format ?? OutputFormat.TEXT,
};
if (params.contextFileName) {
setGeminiMdFilename(params.contextFileName);
@@ -449,7 +488,10 @@ export class Config {
initializeTelemetry(this);
}
logCliConfiguration(this, new StartSessionEvent(this));
if (this.getProxy()) {
setGlobalDispatcher(new ProxyAgent(this.getProxy() as string));
}
this.geminiClient = new GeminiClient(this);
}
/**
@@ -460,7 +502,7 @@ export class Config {
throw Error('Config was already initialized');
}
this.initialized = true;
this.ideClient = await IdeClient.getInstance();
// Initialize centralized FileDiscoveryService
this.getFileService();
if (this.getCheckpointingEnabled()) {
@@ -469,57 +511,72 @@ export class Config {
this.promptRegistry = new PromptRegistry();
this.subagentManager = new SubagentManager(this);
this.toolRegistry = await this.createToolRegistry();
logCliConfiguration(this, new StartSessionEvent(this, this.toolRegistry));
await this.geminiClient.initialize();
}
getContentGenerator(): ContentGenerator {
return this.contentGenerator;
}
async refreshAuth(authMethod: AuthType) {
// Save the current conversation history before creating a new client
let existingHistory: Content[] = [];
if (this.geminiClient && this.geminiClient.isInitialized()) {
existingHistory = this.geminiClient.getHistory();
// Vertex and Genai have incompatible encryption and sending history with
// throughtSignature from Genai to Vertex will fail, we need to strip them
if (
this.contentGeneratorConfig?.authType === AuthType.USE_GEMINI &&
authMethod === AuthType.LOGIN_WITH_GOOGLE
) {
// Restore the conversation history to the new client
this.geminiClient.stripThoughtsFromHistory();
}
// Create new content generator config
const newContentGeneratorConfig = createContentGeneratorConfig(
this,
authMethod,
this._generationConfig,
);
this.contentGenerator = await createContentGenerator(
newContentGeneratorConfig,
this,
this.getSessionId(),
);
// Create and initialize new client in local variable first
const newGeminiClient = new GeminiClient(this);
await newGeminiClient.initialize(newContentGeneratorConfig);
// Vertex and Genai have incompatible encryption and sending history with
// throughtSignature from Genai to Vertex will fail, we need to strip them
const fromGenaiToVertex =
this.contentGeneratorConfig?.authType === AuthType.USE_GEMINI &&
authMethod === AuthType.LOGIN_WITH_GOOGLE;
// Only assign to instance properties after successful initialization
this.contentGeneratorConfig = newContentGeneratorConfig;
this.geminiClient = newGeminiClient;
// Restore the conversation history to the new client
if (existingHistory.length > 0) {
this.geminiClient.setHistory(existingHistory, {
stripThoughts: fromGenaiToVertex,
});
}
// Initialize BaseLlmClient now that the ContentGenerator is available
this.baseLlmClient = new BaseLlmClient(this.contentGenerator, this);
// Reset the session flag since we're explicitly changing auth and using default model
this.inFallbackMode = false;
this.authType = authMethod;
// Logging the cli configuration here as the auth related configuration params would have been loaded by this point
logCliConfiguration(this, new StartSessionEvent(this, this.toolRegistry));
}
/**
* Provides access to the BaseLlmClient for stateless LLM operations.
*/
getBaseLlmClient(): BaseLlmClient {
if (!this.baseLlmClient) {
// Handle cases where initialization might be deferred or authentication failed
if (this.contentGenerator) {
this.baseLlmClient = new BaseLlmClient(
this.getContentGenerator(),
this,
);
} else {
throw new Error(
'BaseLlmClient not initialized. Ensure authentication has occurred and ContentGenerator is ready.',
);
}
}
return this.baseLlmClient;
}
getSessionId(): string {
return this.sessionId;
}
setSessionId(sessionId: string): void {
this.sessionId = sessionId;
}
shouldLoadMemoryFromIncludeDirectories(): boolean {
return this.loadMemoryFromIncludeDirectories;
}
@@ -529,51 +586,18 @@ export class Config {
}
getModel(): string {
return this.contentGeneratorConfig?.model || this.model;
return this.contentGeneratorConfig.model;
}
async setModel(
newModel: string,
options?: {
reason?: ModelSwitchEvent['reason'];
context?: string;
},
_metadata?: { reason?: string; context?: string },
): Promise<void> {
const oldModel = this.getModel();
if (this.contentGeneratorConfig) {
this.contentGeneratorConfig.model = newModel;
}
// Log the model switch if the model actually changed
if (oldModel !== newModel && this.logger) {
const switchEvent: ModelSwitchEvent = {
fromModel: oldModel,
toModel: newModel,
reason: options?.reason || 'manual',
context: options?.context,
};
// Log asynchronously to avoid blocking
this.logger.logModelSwitch(switchEvent).catch((error) => {
console.debug('Failed to log model switch:', error);
});
}
// Reinitialize chat with updated configuration while preserving history
const geminiClient = this.getGeminiClient();
if (geminiClient && geminiClient.isInitialized()) {
// Now await the reinitialize operation to ensure completion
try {
await geminiClient.reinitialize();
} catch (error) {
console.error(
'Failed to reinitialize chat with updated config:',
error,
);
throw error; // Re-throw to let callers handle the error
}
}
// TODO: Log _metadata for telemetry if needed
// This _metadata can be used for tracking model switches (reason, context)
}
isInFallbackMode(): boolean {
@@ -584,8 +608,8 @@ export class Config {
this.inFallbackMode = active;
}
setFlashFallbackHandler(handler: FlashFallbackHandler): void {
this.flashFallbackHandler = handler;
setFallbackModelHandler(handler: FallbackModelHandler): void {
this.fallbackModelHandler = handler;
}
getMaxSessionTurns(): number {
@@ -705,7 +729,7 @@ export class Config {
setApprovalMode(mode: ApprovalMode): void {
if (
this.isTrustedFolder() === false &&
!this.isTrustedFolder() &&
mode !== ApprovalMode.DEFAULT &&
mode !== ApprovalMode.PLAN
) {
@@ -752,6 +776,10 @@ export class Config {
return this.gitCoAuthor;
}
getTelemetryUseCollector(): boolean {
return this.telemetrySettings.useCollector ?? false;
}
getGeminiClient(): GeminiClient {
return this.geminiClient;
}
@@ -767,14 +795,14 @@ export class Config {
getFileFilteringRespectGitIgnore(): boolean {
return this.fileFiltering.respectGitIgnore;
}
getFileFilteringRespectGeminiIgnore(): boolean {
return this.fileFiltering.respectGeminiIgnore;
getFileFilteringRespectQwenIgnore(): boolean {
return this.fileFiltering.respectQwenIgnore;
}
getFileFilteringOptions(): FileFilteringOptions {
return {
respectGitIgnore: this.fileFiltering.respectGitIgnore,
respectGeminiIgnore: this.fileFiltering.respectGeminiIgnore,
respectQwenIgnore: this.fileFiltering.respectQwenIgnore,
};
}
@@ -863,10 +891,6 @@ export class Config {
return this.tavilyApiKey;
}
getIdeClient(): IdeClient {
return this.ideClient;
}
getIdeMode(): boolean {
return this.ideMode;
}
@@ -875,68 +899,45 @@ export class Config {
return this.folderTrustFeature;
}
/**
* Returns 'true' if the workspace is considered "trusted".
* 'false' for untrusted.
*/
getFolderTrust(): boolean {
return this.folderTrust;
}
isTrustedFolder(): boolean | undefined {
return this.trustedFolder;
isTrustedFolder(): boolean {
// isWorkspaceTrusted in cli/src/config/trustedFolder.js returns undefined
// when the file based trust value is unavailable, since it is mainly used
// in the initialization for trust dialogs, etc. Here we return true since
// config.isTrustedFolder() is used for the main business logic of blocking
// tool calls etc in the rest of the application.
//
// Default value is true since we load with trusted settings to avoid
// restarts in the more common path. If the user chooses to mark the folder
// as untrusted, the CLI will restart and we will have the trust value
// reloaded.
const context = ideContextStore.get();
if (context?.workspaceState?.isTrusted !== undefined) {
return context.workspaceState.isTrusted;
}
return this.trustedFolder ?? true;
}
setIdeMode(value: boolean): void {
this.ideMode = value;
}
async setIdeModeAndSyncConnection(value: boolean): Promise<void> {
this.ideMode = value;
if (value) {
await this.ideClient.connect();
logIdeConnection(this, new IdeConnectionEvent(IdeConnectionType.SESSION));
} else {
await this.ideClient.disconnect();
}
}
getAuthType(): AuthType | undefined {
return this.authType;
}
getEnableOpenAILogging(): boolean {
return this.enableOpenAILogging;
}
getContentGeneratorTimeout(): number | undefined {
return this.contentGenerator?.timeout;
}
getContentGeneratorMaxRetries(): number | undefined {
return this.contentGenerator?.maxRetries;
}
getContentGeneratorDisableCacheControl(): boolean | undefined {
return this.contentGenerator?.disableCacheControl;
}
getContentGeneratorSamplingParams(): ContentGeneratorConfig['samplingParams'] {
return this.contentGenerator?.samplingParams as
| ContentGeneratorConfig['samplingParams']
| undefined;
return this.contentGeneratorConfig.authType;
}
getCliVersion(): string | undefined {
return this.cliVersion;
}
getSystemPromptMappings():
| Array<{
baseUrls?: string[];
modelNames?: string[];
template?: string;
}>
| undefined {
return this.systemPromptMappings;
}
/**
* Get the current FileSystemService
*/
@@ -971,6 +972,20 @@ export class Config {
return this.skipNextSpeakerCheck;
}
getShellExecutionConfig(): ShellExecutionConfig {
return this.shellExecutionConfig;
}
setShellExecutionConfig(config: ShellExecutionConfig): void {
this.shellExecutionConfig = {
terminalWidth:
config.terminalWidth ?? this.shellExecutionConfig.terminalWidth,
terminalHeight:
config.terminalHeight ?? this.shellExecutionConfig.terminalHeight,
showColor: config.showColor ?? this.shellExecutionConfig.showColor,
pager: config.pager ?? this.shellExecutionConfig.pager,
};
}
getScreenReader(): boolean {
return this.accessibility.screenReader ?? false;
}
@@ -987,6 +1002,34 @@ export class Config {
return this.vlmSwitchMode;
}
getEnableToolOutputTruncation(): boolean {
return this.enableToolOutputTruncation;
}
getTruncateToolOutputThreshold(): number {
return Math.min(
// Estimate remaining context window in characters (1 token ~= 4 chars).
4 *
(tokenLimit(this.getModel()) -
uiTelemetryService.getLastPromptTokenCount()),
this.truncateToolOutputThreshold,
);
}
getTruncateToolOutputLines(): number {
return this.truncateToolOutputLines;
}
getUseSmartEdit(): boolean {
return this.useSmartEdit;
}
getOutputFormat(): OutputFormat {
return this.outputSettings?.format
? this.outputSettings.format
: OutputFormat.TEXT;
}
async getGitService(): Promise<GitService> {
if (!this.gitService) {
this.gitService = new GitService(this.targetDir, this.storage);
@@ -1004,7 +1047,7 @@ export class Config {
}
async createToolRegistry(): Promise<ToolRegistry> {
const registry = new ToolRegistry(this);
const registry = new ToolRegistry(this, this.eventEmitter);
// helper to create & register core tools that are enabled
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1013,20 +1056,22 @@ export class Config {
const toolName = ToolClass.Name || className;
const coreTools = this.getCoreTools();
const excludeTools = this.getExcludeTools() || [];
// On some platforms, the className can be minified to _ClassName.
const normalizedClassName = className.replace(/^_+/, '');
let isEnabled = true; // Enabled by default if coreTools is not set.
if (coreTools) {
isEnabled = coreTools.some(
(tool) =>
tool === className ||
tool === toolName ||
tool.startsWith(`${className}(`) ||
tool.startsWith(`${toolName}(`),
tool === normalizedClassName ||
tool.startsWith(`${toolName}(`) ||
tool.startsWith(`${normalizedClassName}(`),
);
}
const isExcluded = excludeTools.some(
(tool) => tool === className || tool === toolName,
(tool) => tool === toolName || tool === normalizedClassName,
);
if (isExcluded) {
@@ -1043,13 +1088,29 @@ export class Config {
registerCoreTool(ReadFileTool, this);
if (this.getUseRipgrep()) {
registerCoreTool(RipGrepTool, this);
let useRipgrep = false;
let errorString: undefined | string = undefined;
try {
useRipgrep = await canUseRipgrep();
} catch (error: unknown) {
errorString = String(error);
}
if (useRipgrep) {
registerCoreTool(RipGrepTool, this);
} else {
logRipgrepFallback(this, new RipgrepFallbackEvent(errorString));
registerCoreTool(GrepTool, this);
}
} else {
registerCoreTool(GrepTool, this);
}
registerCoreTool(GlobTool, this);
registerCoreTool(EditTool, this);
if (this.getUseSmartEdit()) {
registerCoreTool(SmartEditTool, this);
} else {
registerCoreTool(EditTool, this);
}
registerCoreTool(WriteFileTool, this);
registerCoreTool(ReadManyFilesTool, this);
registerCoreTool(ShellTool, this);
@@ -1066,5 +1127,3 @@ export class Config {
return registry;
}
}
// Export model constants for use in CLI
export { DEFAULT_GEMINI_FLASH_MODEL };