From 95d87ab7a0ac7e3d277461ff11a60990e183e813 Mon Sep 17 00:00:00 2001 From: Beatrix Date: Mon, 6 Jan 2025 16:51:15 -0800 Subject: [PATCH] clean up and add TODO --- vscode/src/chat/agentic/CodyTool.ts | 4 +- vscode/src/chat/agentic/CodyToolProvider.ts | 73 +++++++++++---------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/vscode/src/chat/agentic/CodyTool.ts b/vscode/src/chat/agentic/CodyTool.ts index 1f18196bf9fb..d1178aa6c889 100644 --- a/vscode/src/chat/agentic/CodyTool.ts +++ b/vscode/src/chat/agentic/CodyTool.ts @@ -209,7 +209,7 @@ class SearchTool extends CodyTool { subTag: ps`query`, }, prompt: { - instruction: ps`Perform a symbol query search in the codebase-Do not support nature language search`, + instruction: ps`Perform a symbol query search in the codebase-Do not support natural language search`, placeholder: ps`SEARCH_QUERY`, examples: [ ps`Locate a function found in an error log: \`function name\``, @@ -277,6 +277,8 @@ export class OpenCtxTool extends CodyTool { const results: ContextItem[] = [] const idObject: Pick = { id: this.provider.id } try { + // TODO: Investigate if we can batch queries for better performance. + // For example, would it cause issues if we fire 10 requests to a OpenCtx provider for fetching Linear? for (const query of queries) { const mention = parseMentionQuery(query, idObject) // First get the items without content diff --git a/vscode/src/chat/agentic/CodyToolProvider.ts b/vscode/src/chat/agentic/CodyToolProvider.ts index e939aaccc031..70a1a7c8271d 100644 --- a/vscode/src/chat/agentic/CodyToolProvider.ts +++ b/vscode/src/chat/agentic/CodyToolProvider.ts @@ -46,7 +46,22 @@ export interface ToolConfiguration extends CodyToolConfig { export class ToolFactory { private tools: Map = new Map() - constructor(private contextRetriever: Retriever) {} + constructor(private contextRetriever: Retriever) { + for (const [name, { tool, useContextRetriever }] of Object.entries(TOOL_CONFIGS)) { + this.register({ + name, + ...tool.prototype.config, + createInstance: useContextRetriever + ? (_, contextRetriever) => { + if (!contextRetriever) { + throw new Error(`Context retriever required for ${name}`) + } + return new tool(contextRetriever) + } + : () => new tool(), + }) + } + } public register(toolConfig: ToolConfiguration): void { this.tools.set(toolConfig.name, toolConfig) @@ -146,50 +161,38 @@ export class ToolFactory { * - Access tools using getTools() * - Set up OpenCtx integration using setupOpenCtxProviderListener() */ -export namespace CodyToolProvider { - export let factory: ToolFactory - let openCtxSubscription: Unsubscribable | undefined +export class CodyToolProvider { + public factory: ToolFactory - export function initialize(contextRetriever: Retriever): void { - factory = new ToolFactory(contextRetriever) - initializeRegistry() + private static instance: CodyToolProvider | undefined + public static openCtxSubscription: Unsubscribable | undefined + + private constructor(contextRetriever: Retriever) { + this.factory = new ToolFactory(contextRetriever) } - export function getTools(): CodyTool[] { - const instances = factory.getInstances() - return instances + public static initialize(contextRetriever: Retriever): void { + CodyToolProvider.instance = new CodyToolProvider(contextRetriever) } - export function setupOpenCtxProviderListener(): void { - if (!openCtxSubscription && factory && openCtx.controller) { - openCtxSubscription = openCtx.controller - .metaChanges({}, {}) - .pipe(map(providers => providers.filter(p => !!p.mentions).map(openCtxProviderMetadata))) - .subscribe(providerMeta => factory?.createOpenCtxTools(providerMeta)) - } + public static getTools(): CodyTool[] { + return CodyToolProvider.instance?.factory.getInstances() ?? [] } - function initializeRegistry(): void { - for (const [name, { tool, useContextRetriever }] of Object.entries(TOOL_CONFIGS)) { - factory.register({ - name, - ...tool.prototype.config, - createInstance: useContextRetriever - ? (_, contextRetriever) => { - if (!contextRetriever) { - throw new Error(`Context retriever required for ${name}`) - } - return new tool(contextRetriever) - } - : () => new tool(), - }) + public static setupOpenCtxProviderListener(): void { + const provider = CodyToolProvider.instance + if (provider && !CodyToolProvider.openCtxSubscription && openCtx.controller) { + CodyToolProvider.openCtxSubscription = openCtx.controller + .metaChanges({}, {}) + .pipe(map(providers => providers.filter(p => !!p.mentions).map(openCtxProviderMetadata))) + .subscribe(providerMeta => provider.factory.createOpenCtxTools(providerMeta)) } } - export function dispose(): void { - if (openCtxSubscription) { - openCtxSubscription.unsubscribe() - openCtxSubscription = undefined + public static dispose(): void { + if (CodyToolProvider.openCtxSubscription) { + CodyToolProvider.openCtxSubscription.unsubscribe() + CodyToolProvider.openCtxSubscription = undefined } } }