Skip to content

Commit

Permalink
clean up and add TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
abeatrix committed Jan 7, 2025
1 parent 2e5d77e commit 95d87ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
4 changes: 3 additions & 1 deletion vscode/src/chat/agentic/CodyTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: \`<TOOLSEARCH><query>function name</query></TOOLSEARCH>\``,
Expand Down Expand Up @@ -277,6 +277,8 @@ export class OpenCtxTool extends CodyTool {
const results: ContextItem[] = []
const idObject: Pick<ContextMentionProviderMetadata, 'id'> = { 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
Expand Down
73 changes: 38 additions & 35 deletions vscode/src/chat/agentic/CodyToolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@ export interface ToolConfiguration extends CodyToolConfig {
export class ToolFactory {
private tools: Map<string, ToolConfiguration> = 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)
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 95d87ab

Please sign in to comment.