Updates schema, UX and prompt for IDE context (#5046)

This commit is contained in:
Shreya Keshive 2025-07-28 11:03:22 -04:00 committed by GitHub
parent f2e006179d
commit e275441651
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 680 additions and 259 deletions

View File

@ -153,8 +153,8 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
}); });
const ideContextMock = { const ideContextMock = {
getOpenFilesContext: vi.fn(), getIdeContext: vi.fn(),
subscribeToOpenFiles: vi.fn(() => vi.fn()), // subscribe returns an unsubscribe function subscribeToIdeContext: vi.fn(() => vi.fn()), // subscribe returns an unsubscribe function
}; };
return { return {
@ -277,7 +277,7 @@ describe('App UI', () => {
// Ensure a theme is set so the theme dialog does not appear. // Ensure a theme is set so the theme dialog does not appear.
mockSettings = createMockSettings({ workspace: { theme: 'Default' } }); mockSettings = createMockSettings({ workspace: { theme: 'Default' } });
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue(undefined); vi.mocked(ideContext.getIdeContext).mockReturnValue(undefined);
}); });
afterEach(() => { afterEach(() => {
@ -289,10 +289,17 @@ describe('App UI', () => {
}); });
it('should display active file when available', async () => { it('should display active file when available', async () => {
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue({ vi.mocked(ideContext.getIdeContext).mockReturnValue({
activeFile: '/path/to/my-file.ts', workspaceState: {
recentOpenFiles: [{ filePath: '/path/to/my-file.ts', content: 'hello' }], openFiles: [
selectedText: 'hello', {
path: '/path/to/my-file.ts',
isActive: true,
selectedText: 'hello',
timestamp: 0,
},
],
},
}); });
const { lastFrame, unmount } = render( const { lastFrame, unmount } = render(
@ -304,12 +311,14 @@ describe('App UI', () => {
); );
currentUnmount = unmount; currentUnmount = unmount;
await Promise.resolve(); await Promise.resolve();
expect(lastFrame()).toContain('1 recent file (ctrl+e to view)'); expect(lastFrame()).toContain('1 open file (ctrl+e to view)');
}); });
it('should not display active file when not available', async () => { it('should not display any files when not available', async () => {
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue({ vi.mocked(ideContext.getIdeContext).mockReturnValue({
activeFile: '', workspaceState: {
openFiles: [],
},
}); });
const { lastFrame, unmount } = render( const { lastFrame, unmount } = render(
@ -324,11 +333,54 @@ describe('App UI', () => {
expect(lastFrame()).not.toContain('Open File'); expect(lastFrame()).not.toContain('Open File');
}); });
it('should display active file and other open files', async () => {
vi.mocked(ideContext.getIdeContext).mockReturnValue({
workspaceState: {
openFiles: [
{
path: '/path/to/my-file.ts',
isActive: true,
selectedText: 'hello',
timestamp: 0,
},
{
path: '/path/to/another-file.ts',
isActive: false,
timestamp: 1,
},
{
path: '/path/to/third-file.ts',
isActive: false,
timestamp: 2,
},
],
},
});
const { lastFrame, unmount } = render(
<App
config={mockConfig as unknown as ServerConfig}
settings={mockSettings}
version={mockVersion}
/>,
);
currentUnmount = unmount;
await Promise.resolve();
expect(lastFrame()).toContain('3 open files (ctrl+e to view)');
});
it('should display active file and other context', async () => { it('should display active file and other context', async () => {
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue({ vi.mocked(ideContext.getIdeContext).mockReturnValue({
activeFile: '/path/to/my-file.ts', workspaceState: {
recentOpenFiles: [{ filePath: '/path/to/my-file.ts', content: 'hello' }], openFiles: [
selectedText: 'hello', {
path: '/path/to/my-file.ts',
isActive: true,
selectedText: 'hello',
timestamp: 0,
},
],
},
}); });
mockConfig.getGeminiMdFileCount.mockReturnValue(1); mockConfig.getGeminiMdFileCount.mockReturnValue(1);
mockConfig.getAllGeminiMdFilenames.mockReturnValue(['GEMINI.md']); mockConfig.getAllGeminiMdFilenames.mockReturnValue(['GEMINI.md']);
@ -343,7 +395,7 @@ describe('App UI', () => {
currentUnmount = unmount; currentUnmount = unmount;
await Promise.resolve(); await Promise.resolve();
expect(lastFrame()).toContain( expect(lastFrame()).toContain(
'Using: 1 recent file (ctrl+e to view) | 1 GEMINI.md file', 'Using: 1 open file (ctrl+e to view) | 1 GEMINI.md file',
); );
}); });

View File

@ -60,7 +60,7 @@ import {
FlashFallbackEvent, FlashFallbackEvent,
logFlashFallback, logFlashFallback,
AuthType, AuthType,
type OpenFiles, type IdeContext,
ideContext, ideContext,
} from '@google/gemini-cli-core'; } from '@google/gemini-cli-core';
import { validateAuthMethod } from '../config/auth.js'; import { validateAuthMethod } from '../config/auth.js';
@ -169,13 +169,15 @@ const App = ({ config, settings, startupWarnings = [], version }: AppProps) => {
const [modelSwitchedFromQuotaError, setModelSwitchedFromQuotaError] = const [modelSwitchedFromQuotaError, setModelSwitchedFromQuotaError] =
useState<boolean>(false); useState<boolean>(false);
const [userTier, setUserTier] = useState<UserTierId | undefined>(undefined); const [userTier, setUserTier] = useState<UserTierId | undefined>(undefined);
const [openFiles, setOpenFiles] = useState<OpenFiles | undefined>(); const [ideContextState, setIdeContextState] = useState<
IdeContext | undefined
>();
const [isProcessing, setIsProcessing] = useState<boolean>(false); const [isProcessing, setIsProcessing] = useState<boolean>(false);
useEffect(() => { useEffect(() => {
const unsubscribe = ideContext.subscribeToOpenFiles(setOpenFiles); const unsubscribe = ideContext.subscribeToIdeContext(setIdeContextState);
// Set the initial value // Set the initial value
setOpenFiles(ideContext.getOpenFilesContext()); setIdeContextState(ideContext.getIdeContext());
return unsubscribe; return unsubscribe;
}, []); }, []);
@ -568,7 +570,7 @@ const App = ({ config, settings, startupWarnings = [], version }: AppProps) => {
if (Object.keys(mcpServers || {}).length > 0) { if (Object.keys(mcpServers || {}).length > 0) {
handleSlashCommand(newValue ? '/mcp desc' : '/mcp nodesc'); handleSlashCommand(newValue ? '/mcp desc' : '/mcp nodesc');
} }
} else if (key.ctrl && input === 'e' && ideContext) { } else if (key.ctrl && input === 'e' && ideContextState) {
setShowIDEContextDetail((prev) => !prev); setShowIDEContextDetail((prev) => !prev);
} else if (key.ctrl && (input === 'c' || input === 'C')) { } else if (key.ctrl && (input === 'c' || input === 'C')) {
handleExit(ctrlCPressedOnce, setCtrlCPressedOnce, ctrlCTimerRef); handleExit(ctrlCPressedOnce, setCtrlCPressedOnce, ctrlCTimerRef);
@ -943,7 +945,7 @@ const App = ({ config, settings, startupWarnings = [], version }: AppProps) => {
</Text> </Text>
) : ( ) : (
<ContextSummaryDisplay <ContextSummaryDisplay
openFiles={openFiles} ideContext={ideContextState}
geminiMdFileCount={geminiMdFileCount} geminiMdFileCount={geminiMdFileCount}
contextFileNames={contextFileNames} contextFileNames={contextFileNames}
mcpServers={config.getMcpServers()} mcpServers={config.getMcpServers()}
@ -963,7 +965,7 @@ const App = ({ config, settings, startupWarnings = [], version }: AppProps) => {
</Box> </Box>
</Box> </Box>
{showIDEContextDetail && ( {showIDEContextDetail && (
<IDEContextDetailDisplay openFiles={openFiles} /> <IDEContextDetailDisplay ideContext={ideContextState} />
)} )}
{showErrorDetails && ( {showErrorDetails && (
<OverflowProvider> <OverflowProvider>

View File

@ -7,7 +7,7 @@
import React from 'react'; import React from 'react';
import { Text } from 'ink'; import { Text } from 'ink';
import { Colors } from '../colors.js'; import { Colors } from '../colors.js';
import { type OpenFiles, type MCPServerConfig } from '@google/gemini-cli-core'; import { type IdeContext, type MCPServerConfig } from '@google/gemini-cli-core';
interface ContextSummaryDisplayProps { interface ContextSummaryDisplayProps {
geminiMdFileCount: number; geminiMdFileCount: number;
@ -15,7 +15,7 @@ interface ContextSummaryDisplayProps {
mcpServers?: Record<string, MCPServerConfig>; mcpServers?: Record<string, MCPServerConfig>;
blockedMcpServers?: Array<{ name: string; extensionName: string }>; blockedMcpServers?: Array<{ name: string; extensionName: string }>;
showToolDescriptions?: boolean; showToolDescriptions?: boolean;
openFiles?: OpenFiles; ideContext?: IdeContext;
} }
export const ContextSummaryDisplay: React.FC<ContextSummaryDisplayProps> = ({ export const ContextSummaryDisplay: React.FC<ContextSummaryDisplayProps> = ({
@ -24,26 +24,28 @@ export const ContextSummaryDisplay: React.FC<ContextSummaryDisplayProps> = ({
mcpServers, mcpServers,
blockedMcpServers, blockedMcpServers,
showToolDescriptions, showToolDescriptions,
openFiles, ideContext,
}) => { }) => {
const mcpServerCount = Object.keys(mcpServers || {}).length; const mcpServerCount = Object.keys(mcpServers || {}).length;
const blockedMcpServerCount = blockedMcpServers?.length || 0; const blockedMcpServerCount = blockedMcpServers?.length || 0;
const openFileCount = ideContext?.workspaceState?.openFiles?.length ?? 0;
if ( if (
geminiMdFileCount === 0 && geminiMdFileCount === 0 &&
mcpServerCount === 0 && mcpServerCount === 0 &&
blockedMcpServerCount === 0 && blockedMcpServerCount === 0 &&
(openFiles?.recentOpenFiles?.length ?? 0) === 0 openFileCount === 0
) { ) {
return <Text> </Text>; // Render an empty space to reserve height return <Text> </Text>; // Render an empty space to reserve height
} }
const recentFilesText = (() => { const openFilesText = (() => {
const count = openFiles?.recentOpenFiles?.length ?? 0; if (openFileCount === 0) {
if (count === 0) {
return ''; return '';
} }
return `${count} recent file${count > 1 ? 's' : ''} (ctrl+e to view)`; return `${openFileCount} open file${
openFileCount > 1 ? 's' : ''
} (ctrl+e to view)`;
})(); })();
const geminiMdText = (() => { const geminiMdText = (() => {
@ -81,8 +83,8 @@ export const ContextSummaryDisplay: React.FC<ContextSummaryDisplayProps> = ({
let summaryText = 'Using: '; let summaryText = 'Using: ';
const summaryParts = []; const summaryParts = [];
if (recentFilesText) { if (openFilesText) {
summaryParts.push(recentFilesText); summaryParts.push(openFilesText);
} }
if (geminiMdText) { if (geminiMdText) {
summaryParts.push(geminiMdText); summaryParts.push(geminiMdText);

View File

@ -5,25 +5,21 @@
*/ */
import { Box, Text } from 'ink'; import { Box, Text } from 'ink';
import { type OpenFiles } from '@google/gemini-cli-core'; import { type File, type IdeContext } from '@google/gemini-cli-core';
import { Colors } from '../colors.js'; import { Colors } from '../colors.js';
import path from 'node:path'; import path from 'node:path';
interface IDEContextDetailDisplayProps { interface IDEContextDetailDisplayProps {
openFiles: OpenFiles | undefined; ideContext: IdeContext | undefined;
} }
export function IDEContextDetailDisplay({ export function IDEContextDetailDisplay({
openFiles, ideContext,
}: IDEContextDetailDisplayProps) { }: IDEContextDetailDisplayProps) {
if ( const openFiles = ideContext?.workspaceState?.openFiles;
!openFiles || if (!openFiles || openFiles.length === 0) {
!openFiles.recentOpenFiles ||
openFiles.recentOpenFiles.length === 0
) {
return null; return null;
} }
const recentFiles = openFiles.recentOpenFiles || [];
return ( return (
<Box <Box
@ -36,13 +32,13 @@ export function IDEContextDetailDisplay({
<Text color={Colors.AccentCyan} bold> <Text color={Colors.AccentCyan} bold>
IDE Context (ctrl+e to toggle) IDE Context (ctrl+e to toggle)
</Text> </Text>
{recentFiles.length > 0 && ( {openFiles.length > 0 && (
<Box flexDirection="column" marginTop={1}> <Box flexDirection="column" marginTop={1}>
<Text bold>Recent files:</Text> <Text bold>Open files:</Text>
{recentFiles.map((file) => ( {openFiles.map((file: File) => (
<Text key={file.filePath}> <Text key={file.path}>
- {path.basename(file.filePath)} - {path.basename(file.path)}
{file.filePath === openFiles.activeFile ? ' (active)' : ''} {file.isActive ? ' (active)' : ''}
</Text> </Text>
))} ))}
</Box> </Box>

View File

@ -647,14 +647,26 @@ describe('Gemini Client (client.ts)', () => {
describe('sendMessageStream', () => { describe('sendMessageStream', () => {
it('should include IDE context when ideMode is enabled', async () => { it('should include IDE context when ideMode is enabled', async () => {
// Arrange // Arrange
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue({ vi.mocked(ideContext.getIdeContext).mockReturnValue({
activeFile: '/path/to/active/file.ts', workspaceState: {
selectedText: 'hello', openFiles: [
cursor: { line: 5, character: 10 }, {
recentOpenFiles: [ path: '/path/to/active/file.ts',
{ filePath: '/path/to/recent/file1.ts', timestamp: Date.now() }, timestamp: Date.now(),
{ filePath: '/path/to/recent/file2.ts', timestamp: Date.now() }, isActive: true,
], selectedText: 'hello',
cursor: { line: 5, character: 10 },
},
{
path: '/path/to/recent/file1.ts',
timestamp: Date.now(),
},
{
path: '/path/to/recent/file2.ts',
timestamp: Date.now(),
},
],
},
}); });
vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true); vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true);
@ -689,15 +701,188 @@ describe('Gemini Client (client.ts)', () => {
} }
// Assert // Assert
expect(ideContext.getOpenFilesContext).toHaveBeenCalled(); expect(ideContext.getIdeContext).toHaveBeenCalled();
const expectedContext = ` const expectedContext = `
This is the file that the user was most recently looking at: This is the file that the user is looking at:
- Path: /path/to/active/file.ts - Path: /path/to/active/file.ts
This is the cursor position in the file: This is the cursor position in the file:
- Cursor Position: Line 5, Character 10 - Cursor Position: Line 5, Character 10
This is the selected text in the active file: This is the selected text in the file:
- hello - hello
Here are files the user has recently opened, with the most recent at the top: Here are some other files the user has open, with the most recent at the top:
- /path/to/recent/file1.ts
- /path/to/recent/file2.ts
`.trim();
const expectedRequest = [{ text: expectedContext }, ...initialRequest];
expect(mockTurnRunFn).toHaveBeenCalledWith(
expectedRequest,
expect.any(Object),
);
});
it('should not add context if ideMode is enabled but no open files', async () => {
// Arrange
vi.mocked(ideContext.getIdeContext).mockReturnValue({
workspaceState: {
openFiles: [],
},
});
vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true);
const mockStream = (async function* () {
yield { type: 'content', value: 'Hello' };
})();
mockTurnRunFn.mockReturnValue(mockStream);
const mockChat: Partial<GeminiChat> = {
addHistory: vi.fn(),
getHistory: vi.fn().mockReturnValue([]),
};
client['chat'] = mockChat as GeminiChat;
const mockGenerator: Partial<ContentGenerator> = {
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
generateContent: mockGenerateContentFn,
};
client['contentGenerator'] = mockGenerator as ContentGenerator;
const initialRequest = [{ text: 'Hi' }];
// Act
const stream = client.sendMessageStream(
initialRequest,
new AbortController().signal,
'prompt-id-ide',
);
for await (const _ of stream) {
// consume stream
}
// Assert
expect(ideContext.getIdeContext).toHaveBeenCalled();
expect(mockTurnRunFn).toHaveBeenCalledWith(
initialRequest,
expect.any(Object),
);
});
it('should add context if ideMode is enabled and there is one active file', async () => {
// Arrange
vi.mocked(ideContext.getIdeContext).mockReturnValue({
workspaceState: {
openFiles: [
{
path: '/path/to/active/file.ts',
timestamp: Date.now(),
isActive: true,
selectedText: 'hello',
cursor: { line: 5, character: 10 },
},
],
},
});
vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true);
const mockStream = (async function* () {
yield { type: 'content', value: 'Hello' };
})();
mockTurnRunFn.mockReturnValue(mockStream);
const mockChat: Partial<GeminiChat> = {
addHistory: vi.fn(),
getHistory: vi.fn().mockReturnValue([]),
};
client['chat'] = mockChat as GeminiChat;
const mockGenerator: Partial<ContentGenerator> = {
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
generateContent: mockGenerateContentFn,
};
client['contentGenerator'] = mockGenerator as ContentGenerator;
const initialRequest = [{ text: 'Hi' }];
// Act
const stream = client.sendMessageStream(
initialRequest,
new AbortController().signal,
'prompt-id-ide',
);
for await (const _ of stream) {
// consume stream
}
// Assert
expect(ideContext.getIdeContext).toHaveBeenCalled();
const expectedContext = `
This is the file that the user is looking at:
- Path: /path/to/active/file.ts
This is the cursor position in the file:
- Cursor Position: Line 5, Character 10
This is the selected text in the file:
- hello
`.trim();
const expectedRequest = [{ text: expectedContext }, ...initialRequest];
expect(mockTurnRunFn).toHaveBeenCalledWith(
expectedRequest,
expect.any(Object),
);
});
it('should add context if ideMode is enabled and there are open files but no active file', async () => {
// Arrange
vi.mocked(ideContext.getIdeContext).mockReturnValue({
workspaceState: {
openFiles: [
{
path: '/path/to/recent/file1.ts',
timestamp: Date.now(),
},
{
path: '/path/to/recent/file2.ts',
timestamp: Date.now(),
},
],
},
});
vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true);
const mockStream = (async function* () {
yield { type: 'content', value: 'Hello' };
})();
mockTurnRunFn.mockReturnValue(mockStream);
const mockChat: Partial<GeminiChat> = {
addHistory: vi.fn(),
getHistory: vi.fn().mockReturnValue([]),
};
client['chat'] = mockChat as GeminiChat;
const mockGenerator: Partial<ContentGenerator> = {
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
generateContent: mockGenerateContentFn,
};
client['contentGenerator'] = mockGenerator as ContentGenerator;
const initialRequest = [{ text: 'Hi' }];
// Act
const stream = client.sendMessageStream(
initialRequest,
new AbortController().signal,
'prompt-id-ide',
);
for await (const _ of stream) {
// consume stream
}
// Assert
expect(ideContext.getIdeContext).toHaveBeenCalled();
const expectedContext = `
Here are some files the user has open, with the most recent at the top:
- /path/to/recent/file1.ts - /path/to/recent/file1.ts
- /path/to/recent/file2.ts - /path/to/recent/file2.ts
`.trim(); `.trim();

View File

@ -320,32 +320,40 @@ export class GeminiClient {
} }
if (this.config.getIdeMode()) { if (this.config.getIdeMode()) {
const openFiles = ideContext.getOpenFilesContext(); const ideContextState = ideContext.getIdeContext();
if (openFiles) { const openFiles = ideContextState?.workspaceState?.openFiles;
if (openFiles && openFiles.length > 0) {
const contextParts: string[] = []; const contextParts: string[] = [];
if (openFiles.activeFile) { const firstFile = openFiles[0];
const activeFile = firstFile.isActive ? firstFile : undefined;
if (activeFile) {
contextParts.push( contextParts.push(
`This is the file that the user was most recently looking at:\n- Path: ${openFiles.activeFile}`, `This is the file that the user is looking at:\n- Path: ${activeFile.path}`,
); );
if (openFiles.cursor) { if (activeFile.cursor) {
contextParts.push( contextParts.push(
`This is the cursor position in the file:\n- Cursor Position: Line ${openFiles.cursor.line}, Character ${openFiles.cursor.character}`, `This is the cursor position in the file:\n- Cursor Position: Line ${activeFile.cursor.line}, Character ${activeFile.cursor.character}`,
); );
} }
if (openFiles.selectedText) { if (activeFile.selectedText) {
contextParts.push( contextParts.push(
`This is the selected text in the active file:\n- ${openFiles.selectedText}`, `This is the selected text in the file:\n- ${activeFile.selectedText}`,
); );
} }
} }
if (openFiles.recentOpenFiles && openFiles.recentOpenFiles.length > 0) { const otherOpenFiles = activeFile ? openFiles.slice(1) : openFiles;
const recentFiles = openFiles.recentOpenFiles
.map((file) => `- ${file.filePath}`) if (otherOpenFiles.length > 0) {
const recentFiles = otherOpenFiles
.map((file) => `- ${file.path}`)
.join('\n'); .join('\n');
contextParts.push( const heading = activeFile
`Here are files the user has recently opened, with the most recent at the top:\n${recentFiles}`, ? `Here are some other files the user has open, with the most recent at the top:`
); : `Here are some files the user has open, with the most recent at the top:`;
contextParts.push(`${heading}\n${recentFiles}`);
} }
if (contextParts.length > 0) { if (contextParts.length > 0) {

View File

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import { ideContext, OpenFilesNotificationSchema } from '../ide/ideContext.js'; import { ideContext, IdeContextNotificationSchema } from '../ide/ideContext.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
@ -77,20 +77,20 @@ export class IdeClient {
await this.client.connect(transport); await this.client.connect(transport);
this.client.setNotificationHandler( this.client.setNotificationHandler(
OpenFilesNotificationSchema, IdeContextNotificationSchema,
(notification) => { (notification) => {
ideContext.setOpenFilesContext(notification.params); ideContext.setIdeContext(notification.params);
}, },
); );
this.client.onerror = (error) => { this.client.onerror = (error) => {
logger.debug('IDE MCP client error:', error); logger.debug('IDE MCP client error:', error);
this.connectionStatus = IDEConnectionStatus.Disconnected; this.connectionStatus = IDEConnectionStatus.Disconnected;
ideContext.clearOpenFilesContext(); ideContext.clearIdeContext();
}; };
this.client.onclose = () => { this.client.onclose = () => {
logger.debug('IDE MCP client connection closed.'); logger.debug('IDE MCP client connection closed.');
this.connectionStatus = IDEConnectionStatus.Disconnected; this.connectionStatus = IDEConnectionStatus.Disconnected;
ideContext.clearOpenFilesContext(); ideContext.clearIdeContext();
}; };
this.connectionStatus = IDEConnectionStatus.Connected; this.connectionStatus = IDEConnectionStatus.Connected;

View File

@ -5,136 +5,300 @@
*/ */
import { describe, it, expect, beforeEach, vi } from 'vitest'; import { describe, it, expect, beforeEach, vi } from 'vitest';
import { createIdeContextStore } from './ideContext.js'; import {
createIdeContextStore,
FileSchema,
IdeContextSchema,
} from './ideContext.js';
describe('ideContext - Active File', () => { describe('ideContext', () => {
let ideContext: ReturnType<typeof createIdeContextStore>; describe('createIdeContextStore', () => {
let ideContext: ReturnType<typeof createIdeContextStore>;
beforeEach(() => { beforeEach(() => {
// Create a fresh, isolated instance for each test // Create a fresh, isolated instance for each test
ideContext = createIdeContextStore(); ideContext = createIdeContextStore();
});
it('should return undefined initially for active file context', () => {
expect(ideContext.getOpenFilesContext()).toBeUndefined();
});
it('should set and retrieve the active file context', () => {
const testFile = {
activeFile: '/path/to/test/file.ts',
selectedText: '1234',
};
ideContext.setOpenFilesContext(testFile);
const activeFile = ideContext.getOpenFilesContext();
expect(activeFile).toEqual(testFile);
});
it('should update the active file context when called multiple times', () => {
const firstFile = {
activeFile: '/path/to/first.js',
selectedText: '1234',
};
ideContext.setOpenFilesContext(firstFile);
const secondFile = {
activeFile: '/path/to/second.py',
cursor: { line: 20, character: 30 },
};
ideContext.setOpenFilesContext(secondFile);
const activeFile = ideContext.getOpenFilesContext();
expect(activeFile).toEqual(secondFile);
});
it('should handle empty string for file path', () => {
const testFile = {
activeFile: '',
selectedText: '1234',
};
ideContext.setOpenFilesContext(testFile);
expect(ideContext.getOpenFilesContext()).toEqual(testFile);
});
it('should notify subscribers when active file context changes', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
ideContext.subscribeToOpenFiles(subscriber1);
ideContext.subscribeToOpenFiles(subscriber2);
const testFile = {
activeFile: '/path/to/subscribed.ts',
cursor: { line: 15, character: 25 },
};
ideContext.setOpenFilesContext(testFile);
expect(subscriber1).toHaveBeenCalledTimes(1);
expect(subscriber1).toHaveBeenCalledWith(testFile);
expect(subscriber2).toHaveBeenCalledTimes(1);
expect(subscriber2).toHaveBeenCalledWith(testFile);
// Test with another update
const newFile = {
activeFile: '/path/to/new.js',
selectedText: '1234',
};
ideContext.setOpenFilesContext(newFile);
expect(subscriber1).toHaveBeenCalledTimes(2);
expect(subscriber1).toHaveBeenCalledWith(newFile);
expect(subscriber2).toHaveBeenCalledTimes(2);
expect(subscriber2).toHaveBeenCalledWith(newFile);
});
it('should stop notifying a subscriber after unsubscribe', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
const unsubscribe1 = ideContext.subscribeToOpenFiles(subscriber1);
ideContext.subscribeToOpenFiles(subscriber2);
ideContext.setOpenFilesContext({
activeFile: '/path/to/file1.txt',
selectedText: '1234',
}); });
expect(subscriber1).toHaveBeenCalledTimes(1);
expect(subscriber2).toHaveBeenCalledTimes(1);
unsubscribe1(); it('should return undefined initially for ide context', () => {
expect(ideContext.getIdeContext()).toBeUndefined();
ideContext.setOpenFilesContext({ });
activeFile: '/path/to/file2.txt',
selectedText: '1234', it('should set and retrieve the ide context', () => {
const testFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/test/file.ts',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(testFile);
const activeFile = ideContext.getIdeContext();
expect(activeFile).toEqual(testFile);
});
it('should update the ide context when called multiple times', () => {
const firstFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/first.js',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(firstFile);
const secondFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/second.py',
isActive: true,
cursor: { line: 20, character: 30 },
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(secondFile);
const activeFile = ideContext.getIdeContext();
expect(activeFile).toEqual(secondFile);
});
it('should handle empty string for file path', () => {
const testFile = {
workspaceState: {
openFiles: [
{
path: '',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(testFile);
expect(ideContext.getIdeContext()).toEqual(testFile);
});
it('should notify subscribers when ide context changes', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
ideContext.subscribeToIdeContext(subscriber1);
ideContext.subscribeToIdeContext(subscriber2);
const testFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/subscribed.ts',
isActive: true,
cursor: { line: 15, character: 25 },
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(testFile);
expect(subscriber1).toHaveBeenCalledTimes(1);
expect(subscriber1).toHaveBeenCalledWith(testFile);
expect(subscriber2).toHaveBeenCalledTimes(1);
expect(subscriber2).toHaveBeenCalledWith(testFile);
// Test with another update
const newFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/new.js',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(newFile);
expect(subscriber1).toHaveBeenCalledTimes(2);
expect(subscriber1).toHaveBeenCalledWith(newFile);
expect(subscriber2).toHaveBeenCalledTimes(2);
expect(subscriber2).toHaveBeenCalledWith(newFile);
});
it('should stop notifying a subscriber after unsubscribe', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
const unsubscribe1 = ideContext.subscribeToIdeContext(subscriber1);
ideContext.subscribeToIdeContext(subscriber2);
ideContext.setIdeContext({
workspaceState: {
openFiles: [
{
path: '/path/to/file1.txt',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
});
expect(subscriber1).toHaveBeenCalledTimes(1);
expect(subscriber2).toHaveBeenCalledTimes(1);
unsubscribe1();
ideContext.setIdeContext({
workspaceState: {
openFiles: [
{
path: '/path/to/file2.txt',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
});
expect(subscriber1).toHaveBeenCalledTimes(1); // Should not be called again
expect(subscriber2).toHaveBeenCalledTimes(2);
});
it('should clear the ide context', () => {
const testFile = {
workspaceState: {
openFiles: [
{
path: '/path/to/test/file.ts',
isActive: true,
selectedText: '1234',
timestamp: 0,
},
],
},
};
ideContext.setIdeContext(testFile);
expect(ideContext.getIdeContext()).toEqual(testFile);
ideContext.clearIdeContext();
expect(ideContext.getIdeContext()).toBeUndefined();
}); });
expect(subscriber1).toHaveBeenCalledTimes(1); // Should not be called again
expect(subscriber2).toHaveBeenCalledTimes(2);
}); });
it('should allow the cursor to be optional', () => { describe('FileSchema', () => {
const testFile = { it('should validate a file with only required fields', () => {
activeFile: '/path/to/test/file.ts', const file = {
}; path: '/path/to/file.ts',
timestamp: 12345,
};
const result = FileSchema.safeParse(file);
expect(result.success).toBe(true);
});
ideContext.setOpenFilesContext(testFile); it('should validate a file with all fields', () => {
const file = {
path: '/path/to/file.ts',
timestamp: 12345,
isActive: true,
selectedText: 'const x = 1;',
cursor: {
line: 10,
character: 20,
},
};
const result = FileSchema.safeParse(file);
expect(result.success).toBe(true);
});
const activeFile = ideContext.getOpenFilesContext(); it('should fail validation if path is missing', () => {
expect(activeFile).toEqual(testFile); const file = {
timestamp: 12345,
};
const result = FileSchema.safeParse(file);
expect(result.success).toBe(false);
});
it('should fail validation if timestamp is missing', () => {
const file = {
path: '/path/to/file.ts',
};
const result = FileSchema.safeParse(file);
expect(result.success).toBe(false);
});
}); });
it('should clear the active file context', () => { describe('IdeContextSchema', () => {
const testFile = { it('should validate an empty context', () => {
activeFile: '/path/to/test/file.ts', const context = {};
selectedText: '1234', const result = IdeContextSchema.safeParse(context);
}; expect(result.success).toBe(true);
});
ideContext.setOpenFilesContext(testFile); it('should validate a context with an empty workspaceState', () => {
const context = {
workspaceState: {},
};
const result = IdeContextSchema.safeParse(context);
expect(result.success).toBe(true);
});
expect(ideContext.getOpenFilesContext()).toEqual(testFile); it('should validate a context with an empty openFiles array', () => {
const context = {
workspaceState: {
openFiles: [],
},
};
const result = IdeContextSchema.safeParse(context);
expect(result.success).toBe(true);
});
ideContext.clearOpenFilesContext(); it('should validate a context with a valid file', () => {
const context = {
workspaceState: {
openFiles: [
{
path: '/path/to/file.ts',
timestamp: 12345,
},
],
},
};
const result = IdeContextSchema.safeParse(context);
expect(result.success).toBe(true);
});
expect(ideContext.getOpenFilesContext()).toBeUndefined(); it('should fail validation with an invalid file', () => {
const context = {
workspaceState: {
openFiles: [
{
timestamp: 12345, // path is missing
},
],
},
};
const result = IdeContextSchema.safeParse(context);
expect(result.success).toBe(false);
});
}); });
}); });

View File

@ -7,97 +7,96 @@
import { z } from 'zod'; import { z } from 'zod';
/** /**
* Zod schema for validating a cursor position. * Zod schema for validating a file context from the IDE.
*/ */
export const CursorSchema = z.object({ export const FileSchema = z.object({
line: z.number(), path: z.string(),
character: z.number(), timestamp: z.number(),
}); isActive: z.boolean().optional(),
export type Cursor = z.infer<typeof CursorSchema>;
/**
* Zod schema for validating an active file context from the IDE.
*/
export const OpenFilesSchema = z.object({
activeFile: z.string(),
selectedText: z.string().optional(), selectedText: z.string().optional(),
cursor: CursorSchema.optional(), cursor: z
recentOpenFiles: z .object({
.array( line: z.number(),
z.object({ character: z.number(),
filePath: z.string(), })
timestamp: z.number(),
}),
)
.optional(), .optional(),
}); });
export type OpenFiles = z.infer<typeof OpenFilesSchema>; export type File = z.infer<typeof FileSchema>;
export const IdeContextSchema = z.object({
workspaceState: z
.object({
openFiles: z.array(FileSchema).optional(),
})
.optional(),
});
export type IdeContext = z.infer<typeof IdeContextSchema>;
/** /**
* Zod schema for validating the 'ide/openFilesChanged' notification from the IDE. * Zod schema for validating the 'ide/contextUpdate' notification from the IDE.
*/ */
export const OpenFilesNotificationSchema = z.object({ export const IdeContextNotificationSchema = z.object({
method: z.literal('ide/openFilesChanged'), method: z.literal('ide/contextUpdate'),
params: OpenFilesSchema, params: IdeContextSchema,
}); });
type OpenFilesSubscriber = (openFiles: OpenFiles | undefined) => void; type IdeContextSubscriber = (ideContext: IdeContext | undefined) => void;
/** /**
* Creates a new store for managing the IDE's active file context. * Creates a new store for managing the IDE's context.
* This factory function encapsulates the state and logic, allowing for the creation * This factory function encapsulates the state and logic, allowing for the creation
* of isolated instances, which is particularly useful for testing. * of isolated instances, which is particularly useful for testing.
* *
* @returns An object with methods to interact with the active file context. * @returns An object with methods to interact with the IDE context.
*/ */
export function createIdeContextStore() { export function createIdeContextStore() {
let openFilesContext: OpenFiles | undefined = undefined; let ideContextState: IdeContext | undefined = undefined;
const subscribers = new Set<OpenFilesSubscriber>(); const subscribers = new Set<IdeContextSubscriber>();
/** /**
* Notifies all registered subscribers about the current active file context. * Notifies all registered subscribers about the current IDE context.
*/ */
function notifySubscribers(): void { function notifySubscribers(): void {
for (const subscriber of subscribers) { for (const subscriber of subscribers) {
subscriber(openFilesContext); subscriber(ideContextState);
} }
} }
/** /**
* Sets the active file context and notifies all registered subscribers of the change. * Sets the IDE context and notifies all registered subscribers of the change.
* @param newOpenFiles The new active file context from the IDE. * @param newIdeContext The new IDE context from the IDE.
*/ */
function setOpenFilesContext(newOpenFiles: OpenFiles): void { function setIdeContext(newIdeContext: IdeContext): void {
openFilesContext = newOpenFiles; ideContextState = newIdeContext;
notifySubscribers(); notifySubscribers();
} }
/** /**
* Clears the active file context and notifies all registered subscribers of the change. * Clears the IDE context and notifies all registered subscribers of the change.
*/ */
function clearOpenFilesContext(): void { function clearIdeContext(): void {
openFilesContext = undefined; ideContextState = undefined;
notifySubscribers(); notifySubscribers();
} }
/** /**
* Retrieves the current active file context. * Retrieves the current IDE context.
* @returns The `OpenFiles` object if a file is active; otherwise, `undefined`. * @returns The `IdeContext` object if a file is active; otherwise, `undefined`.
*/ */
function getOpenFilesContext(): OpenFiles | undefined { function getIdeContext(): IdeContext | undefined {
return openFilesContext; return ideContextState;
} }
/** /**
* Subscribes to changes in the active file context. * Subscribes to changes in the IDE context.
* *
* When the active file context changes, the provided `subscriber` function will be called. * When the IDE context changes, the provided `subscriber` function will be called.
* Note: The subscriber is not called with the current value upon subscription. * Note: The subscriber is not called with the current value upon subscription.
* *
* @param subscriber The function to be called when the active file context changes. * @param subscriber The function to be called when the IDE context changes.
* @returns A function that, when called, will unsubscribe the provided subscriber. * @returns A function that, when called, will unsubscribe the provided subscriber.
*/ */
function subscribeToOpenFiles(subscriber: OpenFilesSubscriber): () => void { function subscribeToIdeContext(subscriber: IdeContextSubscriber): () => void {
subscribers.add(subscriber); subscribers.add(subscriber);
return () => { return () => {
subscribers.delete(subscriber); subscribers.delete(subscriber);
@ -105,10 +104,10 @@ export function createIdeContextStore() {
} }
return { return {
setOpenFilesContext, setIdeContext,
getOpenFilesContext, getIdeContext,
subscribeToOpenFiles, subscribeToIdeContext,
clearOpenFilesContext, clearIdeContext,
}; };
} }

View File

@ -20,16 +20,17 @@ const MCP_SESSION_ID_HEADER = 'mcp-session-id';
const IDE_SERVER_PORT_ENV_VAR = 'GEMINI_CLI_IDE_SERVER_PORT'; const IDE_SERVER_PORT_ENV_VAR = 'GEMINI_CLI_IDE_SERVER_PORT';
const MAX_SELECTED_TEXT_LENGTH = 16384; // 16 KiB limit const MAX_SELECTED_TEXT_LENGTH = 16384; // 16 KiB limit
function sendOpenFilesChangedNotification( function sendIdeContextUpdateNotification(
transport: StreamableHTTPServerTransport, transport: StreamableHTTPServerTransport,
log: (message: string) => void, log: (message: string) => void,
recentFilesManager: RecentFilesManager, recentFilesManager: RecentFilesManager,
) { ) {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
const filePath = const activeFile =
editor && editor.document.uri.scheme === 'file' editor && editor.document.uri.scheme === 'file'
? editor.document.uri.fsPath ? editor.document.uri.fsPath
: ''; : undefined;
const selection = editor?.selection; const selection = editor?.selection;
const cursor = selection const cursor = selection
? { ? {
@ -38,25 +39,37 @@ function sendOpenFilesChangedNotification(
character: selection.active.character, character: selection.active.character,
} }
: undefined; : undefined;
let selectedText = editor?.document.getText(selection) ?? undefined; let selectedText = editor?.document.getText(selection) ?? undefined;
if (selectedText && selectedText.length > MAX_SELECTED_TEXT_LENGTH) { if (selectedText && selectedText.length > MAX_SELECTED_TEXT_LENGTH) {
selectedText = selectedText =
selectedText.substring(0, MAX_SELECTED_TEXT_LENGTH) + '... [TRUNCATED]'; selectedText.substring(0, MAX_SELECTED_TEXT_LENGTH) + '... [TRUNCATED]';
} }
const openFiles = recentFilesManager.recentFiles.map((file) => {
const isActive = file.filePath === activeFile;
return {
path: file.filePath,
timestamp: file.timestamp,
isActive,
...(isActive && {
cursor,
selectedText,
}),
};
});
const notification: JSONRPCNotification = { const notification: JSONRPCNotification = {
jsonrpc: '2.0', jsonrpc: '2.0',
method: 'ide/openFilesChanged', method: 'ide/contextUpdate',
params: { params: {
activeFile: filePath, workspaceState: {
recentOpenFiles: recentFilesManager.recentFiles.filter( openFiles,
(file) => file.filePath !== filePath, },
),
cursor,
selectedText,
}, },
}; };
log( log(
`Sending active file changed notification: ${JSON.stringify( `Sending IDE context update notification: ${JSON.stringify(
notification, notification,
null, null,
2, 2,
@ -87,7 +100,7 @@ export class IDEServer {
const recentFilesManager = new RecentFilesManager(context); const recentFilesManager = new RecentFilesManager(context);
const onDidChangeSubscription = recentFilesManager.onDidChange(() => { const onDidChangeSubscription = recentFilesManager.onDidChange(() => {
for (const transport of Object.values(transports)) { for (const transport of Object.values(transports)) {
sendOpenFilesChangedNotification( sendIdeContextUpdateNotification(
transport, transport,
this.log.bind(this), this.log.bind(this),
recentFilesManager, recentFilesManager,
@ -191,7 +204,7 @@ export class IDEServer {
} }
if (!sessionsWithInitialNotification.has(sessionId)) { if (!sessionsWithInitialNotification.has(sessionId)) {
sendOpenFilesChangedNotification( sendIdeContextUpdateNotification(
transport, transport,
this.log.bind(this), this.log.bind(this),
recentFilesManager, recentFilesManager,