mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 21:03:05 -07:00
chore(core): improve encapsulation and remove unused exports (#19556)
This commit is contained in:
@@ -28,7 +28,7 @@ export function extractMessageText(message: Message | undefined): string {
|
|||||||
/**
|
/**
|
||||||
* Extracts text from a single Part.
|
* Extracts text from a single Part.
|
||||||
*/
|
*/
|
||||||
export function extractPartText(part: Part): string {
|
function extractPartText(part: Part): string {
|
||||||
if (isTextPart(part)) {
|
if (isTextPart(part)) {
|
||||||
return part.text;
|
return part.text;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,14 @@ import * as path from 'node:path';
|
|||||||
import * as os from 'node:os';
|
import * as os from 'node:os';
|
||||||
import * as crypto from 'node:crypto';
|
import * as crypto from 'node:crypto';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { GEMINI_DIR, homedir } from '../utils/paths.js';
|
import {
|
||||||
|
GEMINI_DIR,
|
||||||
|
homedir,
|
||||||
|
GOOGLE_ACCOUNTS_FILENAME,
|
||||||
|
} from '../utils/paths.js';
|
||||||
import { ProjectRegistry } from './projectRegistry.js';
|
import { ProjectRegistry } from './projectRegistry.js';
|
||||||
import { StorageMigration } from './storageMigration.js';
|
import { StorageMigration } from './storageMigration.js';
|
||||||
|
|
||||||
export const GOOGLE_ACCOUNTS_FILENAME = 'google_accounts.json';
|
|
||||||
export const OAUTH_FILE = 'oauth_creds.json';
|
export const OAUTH_FILE = 'oauth_creds.json';
|
||||||
const TMP_DIR_NAME = 'tmp';
|
const TMP_DIR_NAME = 'tmp';
|
||||||
const BIN_DIR_NAME = 'bin';
|
const BIN_DIR_NAME = 'bin';
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export function isCloudShell(): boolean {
|
|||||||
return !!(process.env['EDITOR_IN_CLOUD_SHELL'] || process.env['CLOUD_SHELL']);
|
return !!(process.env['EDITOR_IN_CLOUD_SHELL'] || process.env['CLOUD_SHELL']);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isJetBrains(): boolean {
|
function isJetBrains(): boolean {
|
||||||
return !!process.env['TERMINAL_EMULATOR']
|
return !!process.env['TERMINAL_EMULATOR']
|
||||||
?.toLowerCase()
|
?.toLowerCase()
|
||||||
.includes('jetbrains');
|
.includes('jetbrains');
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import { coreEvents } from '../utils/events.js';
|
|||||||
import { debugLogger } from '../utils/debugLogger.js';
|
import { debugLogger } from '../utils/debugLogger.js';
|
||||||
import { getConsentForOauth } from '../utils/authConsent.js';
|
import { getConsentForOauth } from '../utils/authConsent.js';
|
||||||
|
|
||||||
export const OAUTH_DISPLAY_MESSAGE_EVENT = 'oauth-display-message' as const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OAuth configuration for an MCP server.
|
* OAuth configuration for an MCP server.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export interface ResolutionResult {
|
|||||||
* @param correlationId The correlationId to match.
|
* @param correlationId The correlationId to match.
|
||||||
* @param signal An AbortSignal to cancel the wait and cleanup listeners.
|
* @param signal An AbortSignal to cancel the wait and cleanup listeners.
|
||||||
*/
|
*/
|
||||||
export async function awaitConfirmation(
|
async function awaitConfirmation(
|
||||||
messageBus: MessageBus,
|
messageBus: MessageBus,
|
||||||
correlationId: string,
|
correlationId: string,
|
||||||
signal: AbortSignal,
|
signal: AbortSignal,
|
||||||
|
|||||||
@@ -37,18 +37,18 @@ import { LlmRole } from '../telemetry/types.js';
|
|||||||
* Default threshold for compression token count as a fraction of the model's
|
* Default threshold for compression token count as a fraction of the model's
|
||||||
* token limit. If the chat history exceeds this threshold, it will be compressed.
|
* token limit. If the chat history exceeds this threshold, it will be compressed.
|
||||||
*/
|
*/
|
||||||
export const DEFAULT_COMPRESSION_TOKEN_THRESHOLD = 0.5;
|
const DEFAULT_COMPRESSION_TOKEN_THRESHOLD = 0.5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The fraction of the latest chat history to keep. A value of 0.3
|
* The fraction of the latest chat history to keep. A value of 0.3
|
||||||
* means that only the last 30% of the chat history will be kept after compression.
|
* means that only the last 30% of the chat history will be kept after compression.
|
||||||
*/
|
*/
|
||||||
export const COMPRESSION_PRESERVE_THRESHOLD = 0.3;
|
const COMPRESSION_PRESERVE_THRESHOLD = 0.3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The budget for function response tokens in the preserved history.
|
* The budget for function response tokens in the preserved history.
|
||||||
*/
|
*/
|
||||||
export const COMPRESSION_FUNCTION_RESPONSE_TOKEN_BUDGET = 50_000;
|
const COMPRESSION_FUNCTION_RESPONSE_TOKEN_BUDGET = 50_000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the index of the oldest item to keep when compressing. May return
|
* Returns the index of the oldest item to keep when compressing. May return
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import type {
|
|||||||
ConversationFinishedEvent,
|
ConversationFinishedEvent,
|
||||||
ChatCompressionEvent,
|
ChatCompressionEvent,
|
||||||
MalformedJsonResponseEvent,
|
MalformedJsonResponseEvent,
|
||||||
InvalidChunkEvent,
|
|
||||||
ContentRetryEvent,
|
ContentRetryEvent,
|
||||||
ContentRetryFailureEvent,
|
ContentRetryFailureEvent,
|
||||||
RipgrepFallbackEvent,
|
RipgrepFallbackEvent,
|
||||||
@@ -65,7 +64,6 @@ import {
|
|||||||
recordToolCallMetrics,
|
recordToolCallMetrics,
|
||||||
recordChatCompressionMetrics,
|
recordChatCompressionMetrics,
|
||||||
recordFileOperationMetric,
|
recordFileOperationMetric,
|
||||||
recordInvalidChunk,
|
|
||||||
recordContentRetry,
|
recordContentRetry,
|
||||||
recordContentRetryFailure,
|
recordContentRetryFailure,
|
||||||
recordModelRoutingMetrics,
|
recordModelRoutingMetrics,
|
||||||
@@ -470,22 +468,6 @@ export function logMalformedJsonResponse(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logInvalidChunk(
|
|
||||||
config: Config,
|
|
||||||
event: InvalidChunkEvent,
|
|
||||||
): void {
|
|
||||||
ClearcutLogger.getInstance(config)?.logInvalidChunkEvent(event);
|
|
||||||
bufferTelemetryEvent(() => {
|
|
||||||
const logger = logs.getLogger(SERVICE_NAME);
|
|
||||||
const logRecord: LogRecord = {
|
|
||||||
body: event.toLogBody(),
|
|
||||||
attributes: event.toOpenTelemetryAttributes(config),
|
|
||||||
};
|
|
||||||
logger.emit(logRecord);
|
|
||||||
recordInvalidChunk(config);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function logContentRetry(
|
export function logContentRetry(
|
||||||
config: Config,
|
config: Config,
|
||||||
event: ContentRetryEvent,
|
event: ContentRetryEvent,
|
||||||
|
|||||||
@@ -617,7 +617,7 @@ let baselineComparisonHistogram: Histogram | undefined;
|
|||||||
let isMetricsInitialized = false;
|
let isMetricsInitialized = false;
|
||||||
let isPerformanceMonitoringEnabled = false;
|
let isPerformanceMonitoringEnabled = false;
|
||||||
|
|
||||||
export function getMeter(): Meter | undefined {
|
function getMeter(): Meter | undefined {
|
||||||
if (!cliMeter) {
|
if (!cliMeter) {
|
||||||
cliMeter = metrics.getMeter(SERVICE_NAME);
|
cliMeter = metrics.getMeter(SERVICE_NAME);
|
||||||
}
|
}
|
||||||
@@ -1019,7 +1019,7 @@ function getGenAiOperationName(): GenAiOperationName {
|
|||||||
|
|
||||||
// Performance Monitoring Functions
|
// Performance Monitoring Functions
|
||||||
|
|
||||||
export function initializePerformanceMonitoring(config: Config): void {
|
function initializePerformanceMonitoring(config: Config): void {
|
||||||
const meter = getMeter();
|
const meter = getMeter();
|
||||||
if (!meter) return;
|
if (!meter) return;
|
||||||
|
|
||||||
|
|||||||
@@ -240,7 +240,7 @@ export function toChatMessage(content?: Content): ChatMessage {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toOTelPart(part: Part): AnyPart {
|
function toOTelPart(part: Part): AnyPart {
|
||||||
if (part.thought) {
|
if (part.thought) {
|
||||||
if (part.text) {
|
if (part.text) {
|
||||||
return new ReasoningPart(part.text);
|
return new ReasoningPart(part.text);
|
||||||
@@ -287,7 +287,7 @@ export enum OTelRole {
|
|||||||
TOOL = 'tool',
|
TOOL = 'tool',
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toOTelRole(role?: string): OTelRole {
|
function toOTelRole(role?: string): OTelRole {
|
||||||
switch (role?.toLowerCase()) {
|
switch (role?.toLowerCase()) {
|
||||||
case 'system':
|
case 'system':
|
||||||
return OTelRole.SYSTEM;
|
return OTelRole.SYSTEM;
|
||||||
@@ -322,7 +322,7 @@ export enum OTelFinishReason {
|
|||||||
ERROR = 'error',
|
ERROR = 'error',
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toOTelFinishReason(finishReason?: string): OTelFinishReason {
|
function toOTelFinishReason(finishReason?: string): OTelFinishReason {
|
||||||
switch (finishReason) {
|
switch (finishReason) {
|
||||||
// we have significantly more finish reasons than the spec
|
// we have significantly more finish reasons than the spec
|
||||||
case FinishReason.FINISH_REASON_UNSPECIFIED:
|
case FinishReason.FINISH_REASON_UNSPECIFIED:
|
||||||
@@ -376,7 +376,7 @@ export interface ChatMessage {
|
|||||||
parts: AnyPart[];
|
parts: AnyPart[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TextPart {
|
class TextPart {
|
||||||
readonly type = 'text';
|
readonly type = 'text';
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
@@ -385,7 +385,7 @@ export class TextPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ToolCallRequestPart {
|
class ToolCallRequestPart {
|
||||||
readonly type = 'tool_call';
|
readonly type = 'tool_call';
|
||||||
name?: string;
|
name?: string;
|
||||||
id?: string;
|
id?: string;
|
||||||
@@ -398,7 +398,7 @@ export class ToolCallRequestPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ToolCallResponsePart {
|
class ToolCallResponsePart {
|
||||||
readonly type = 'tool_call_response';
|
readonly type = 'tool_call_response';
|
||||||
response?: string;
|
response?: string;
|
||||||
id?: string;
|
id?: string;
|
||||||
@@ -409,7 +409,7 @@ export class ToolCallResponsePart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ReasoningPart {
|
class ReasoningPart {
|
||||||
readonly type = 'reasoning';
|
readonly type = 'reasoning';
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
@@ -418,7 +418,7 @@ export class ReasoningPart {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GenericPart {
|
class GenericPart {
|
||||||
type: string;
|
type: string;
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export interface ToolCallContext {
|
|||||||
/**
|
/**
|
||||||
* AsyncLocalStorage instance for tool call context.
|
* AsyncLocalStorage instance for tool call context.
|
||||||
*/
|
*/
|
||||||
export const toolCallContext = new AsyncLocalStorage<ToolCallContext>();
|
const toolCallContext = new AsyncLocalStorage<ToolCallContext>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs a function within a tool call context.
|
* Runs a function within a tool call context.
|
||||||
|
|||||||
Reference in New Issue
Block a user