mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-04 08:54:28 -07:00
refactor(core): make LegacyAgentSession dependencies optional (#24287)
Co-authored-by: Adam Weidman <adamfweidman@gmail.com> Co-authored-by: Adam Weidman <adamfweidman@google.com>
This commit is contained in:
@@ -17,6 +17,9 @@ import type {
|
|||||||
ToolCallRequestInfo,
|
ToolCallRequestInfo,
|
||||||
} from '../scheduler/types.js';
|
} from '../scheduler/types.js';
|
||||||
import { CoreToolCallStatus } from '../scheduler/types.js';
|
import { CoreToolCallStatus } from '../scheduler/types.js';
|
||||||
|
import type { GeminiClient } from '../core/client.js';
|
||||||
|
import type { Scheduler } from '../scheduler/scheduler.js';
|
||||||
|
import type { Config } from '../config/config.js';
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Mock helpers
|
// Mock helpers
|
||||||
@@ -24,7 +27,7 @@ import { CoreToolCallStatus } from '../scheduler/types.js';
|
|||||||
|
|
||||||
function createMockDeps(
|
function createMockDeps(
|
||||||
overrides?: Partial<LegacyAgentSessionDeps>,
|
overrides?: Partial<LegacyAgentSessionDeps>,
|
||||||
): LegacyAgentSessionDeps {
|
): Required<LegacyAgentSessionDeps> {
|
||||||
const mockClient = {
|
const mockClient = {
|
||||||
sendMessageStream: vi.fn(),
|
sendMessageStream: vi.fn(),
|
||||||
getChat: vi.fn().mockReturnValue({
|
getChat: vi.fn().mockReturnValue({
|
||||||
@@ -40,18 +43,22 @@ function createMockDeps(
|
|||||||
const mockConfig = {
|
const mockConfig = {
|
||||||
getMaxSessionTurns: vi.fn().mockReturnValue(-1),
|
getMaxSessionTurns: vi.fn().mockReturnValue(-1),
|
||||||
getModel: vi.fn().mockReturnValue('gemini-2.5-pro'),
|
getModel: vi.fn().mockReturnValue('gemini-2.5-pro'),
|
||||||
|
getGeminiClient: vi.fn().mockReturnValue(mockClient),
|
||||||
|
getMessageBus: vi.fn().mockImplementation(() => ({
|
||||||
|
subscribe: vi.fn(),
|
||||||
|
unsubscribe: vi.fn(),
|
||||||
|
})),
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
client: mockClient as unknown as LegacyAgentSessionDeps['client'],
|
client: mockClient as unknown as GeminiClient,
|
||||||
|
scheduler: mockScheduler as unknown as Scheduler,
|
||||||
scheduler: mockScheduler as unknown as LegacyAgentSessionDeps['scheduler'],
|
config: mockConfig as unknown as Config,
|
||||||
|
|
||||||
config: mockConfig as unknown as LegacyAgentSessionDeps['config'],
|
|
||||||
promptId: 'test-prompt',
|
promptId: 'test-prompt',
|
||||||
streamId: 'test-stream',
|
streamId: 'test-stream',
|
||||||
|
getPreferredEditor: vi.fn().mockReturnValue(undefined),
|
||||||
...overrides,
|
...overrides,
|
||||||
};
|
} as Required<LegacyAgentSessionDeps>;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function* makeStream(
|
async function* makeStream(
|
||||||
@@ -129,7 +136,7 @@ async function collectEvents(
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
describe('LegacyAgentSession', () => {
|
describe('LegacyAgentSession', () => {
|
||||||
let deps: LegacyAgentSessionDeps;
|
let deps: Required<LegacyAgentSessionDeps>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
deps = createMockDeps();
|
deps = createMockDeps();
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ import type { Part } from '@google/genai';
|
|||||||
import type { GeminiClient } from '../core/client.js';
|
import type { GeminiClient } from '../core/client.js';
|
||||||
import type { Config } from '../config/config.js';
|
import type { Config } from '../config/config.js';
|
||||||
import type { ToolCallRequestInfo } from '../scheduler/types.js';
|
import type { ToolCallRequestInfo } from '../scheduler/types.js';
|
||||||
import type { Scheduler } from '../scheduler/scheduler.js';
|
import { Scheduler } from '../scheduler/scheduler.js';
|
||||||
import { recordToolCallInteractions } from '../code_assist/telemetry.js';
|
import { recordToolCallInteractions } from '../code_assist/telemetry.js';
|
||||||
import { ToolErrorType, isFatalToolError } from '../tools/tool-error.js';
|
import { ToolErrorType, isFatalToolError } from '../tools/tool-error.js';
|
||||||
import { debugLogger } from '../utils/debugLogger.js';
|
import { debugLogger } from '../utils/debugLogger.js';
|
||||||
|
import type { EditorType } from '../utils/editor.js';
|
||||||
import {
|
import {
|
||||||
buildToolResponseData,
|
buildToolResponseData,
|
||||||
contentPartsToGeminiParts,
|
contentPartsToGeminiParts,
|
||||||
@@ -45,14 +46,17 @@ function isAbortLikeError(err: unknown): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface LegacyAgentSessionDeps {
|
export interface LegacyAgentSessionDeps {
|
||||||
client: GeminiClient;
|
|
||||||
scheduler: Scheduler;
|
|
||||||
config: Config;
|
config: Config;
|
||||||
promptId: string;
|
client?: GeminiClient;
|
||||||
|
scheduler?: Scheduler;
|
||||||
|
promptId?: string;
|
||||||
streamId?: string;
|
streamId?: string;
|
||||||
|
getPreferredEditor?: () => EditorType | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
class LegacyAgentProtocol implements AgentProtocol {
|
const schedulerMap = new WeakMap<Config, Scheduler>();
|
||||||
|
|
||||||
|
export class LegacyAgentProtocol implements AgentProtocol {
|
||||||
private _events: AgentEvent[] = [];
|
private _events: AgentEvent[] = [];
|
||||||
private _subscribers = new Set<(event: AgentEvent) => void>();
|
private _subscribers = new Set<(event: AgentEvent) => void>();
|
||||||
private _translationState: TranslationState;
|
private _translationState: TranslationState;
|
||||||
@@ -69,10 +73,26 @@ class LegacyAgentProtocol implements AgentProtocol {
|
|||||||
constructor(deps: LegacyAgentSessionDeps) {
|
constructor(deps: LegacyAgentSessionDeps) {
|
||||||
this._translationState = createTranslationState(deps.streamId);
|
this._translationState = createTranslationState(deps.streamId);
|
||||||
this._nextStreamIdOverride = deps.streamId;
|
this._nextStreamIdOverride = deps.streamId;
|
||||||
this._client = deps.client;
|
|
||||||
this._scheduler = deps.scheduler;
|
|
||||||
this._config = deps.config;
|
this._config = deps.config;
|
||||||
this._promptId = deps.promptId;
|
this._client = deps.client ?? deps.config.getGeminiClient();
|
||||||
|
this._promptId = deps.promptId ?? deps.config.promptId ?? '';
|
||||||
|
|
||||||
|
if (deps.scheduler) {
|
||||||
|
this._scheduler = deps.scheduler;
|
||||||
|
} else {
|
||||||
|
let scheduler = schedulerMap.get(deps.config);
|
||||||
|
if (!scheduler) {
|
||||||
|
const sessionId = deps.config.getSessionId();
|
||||||
|
const schedulerId = `legacy-agent-scheduler-${sessionId}`;
|
||||||
|
scheduler = new Scheduler({
|
||||||
|
context: deps.config,
|
||||||
|
schedulerId,
|
||||||
|
getPreferredEditor: deps.getPreferredEditor ?? (() => undefined),
|
||||||
|
});
|
||||||
|
schedulerMap.set(deps.config, scheduler);
|
||||||
|
}
|
||||||
|
this._scheduler = scheduler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get events(): readonly AgentEvent[] {
|
get events(): readonly AgentEvent[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user