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
10 changed files with 680 additions and 259 deletions

View File

@@ -7,97 +7,96 @@
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({
line: z.number(),
character: z.number(),
});
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(),
export const FileSchema = z.object({
path: z.string(),
timestamp: z.number(),
isActive: z.boolean().optional(),
selectedText: z.string().optional(),
cursor: CursorSchema.optional(),
recentOpenFiles: z
.array(
z.object({
filePath: z.string(),
timestamp: z.number(),
}),
)
cursor: z
.object({
line: z.number(),
character: z.number(),
})
.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({
method: z.literal('ide/openFilesChanged'),
params: OpenFilesSchema,
export const IdeContextNotificationSchema = z.object({
method: z.literal('ide/contextUpdate'),
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
* 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() {
let openFilesContext: OpenFiles | undefined = undefined;
const subscribers = new Set<OpenFilesSubscriber>();
let ideContextState: IdeContext | undefined = undefined;
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 {
for (const subscriber of subscribers) {
subscriber(openFilesContext);
subscriber(ideContextState);
}
}
/**
* Sets the active file context and notifies all registered subscribers of the change.
* @param newOpenFiles The new active file context from the IDE.
* Sets the IDE context and notifies all registered subscribers of the change.
* @param newIdeContext The new IDE context from the IDE.
*/
function setOpenFilesContext(newOpenFiles: OpenFiles): void {
openFilesContext = newOpenFiles;
function setIdeContext(newIdeContext: IdeContext): void {
ideContextState = newIdeContext;
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 {
openFilesContext = undefined;
function clearIdeContext(): void {
ideContextState = undefined;
notifySubscribers();
}
/**
* Retrieves the current active file context.
* @returns The `OpenFiles` object if a file is active; otherwise, `undefined`.
* Retrieves the current IDE context.
* @returns The `IdeContext` object if a file is active; otherwise, `undefined`.
*/
function getOpenFilesContext(): OpenFiles | undefined {
return openFilesContext;
function getIdeContext(): IdeContext | undefined {
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.
*
* @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.
*/
function subscribeToOpenFiles(subscriber: OpenFilesSubscriber): () => void {
function subscribeToIdeContext(subscriber: IdeContextSubscriber): () => void {
subscribers.add(subscriber);
return () => {
subscribers.delete(subscriber);
@@ -105,10 +104,10 @@ export function createIdeContextStore() {
}
return {
setOpenFilesContext,
getOpenFilesContext,
subscribeToOpenFiles,
clearOpenFilesContext,
setIdeContext,
getIdeContext,
subscribeToIdeContext,
clearIdeContext,
};
}