Skip to content

Commit

Permalink
Move some display lines into the prepareInvocation method (#6330)
Browse files Browse the repository at this point in the history
and define tool IDs in each class
  • Loading branch information
alexr00 authored Oct 21, 2024
1 parent 22facd1 commit 33f3bfa
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 38 deletions.
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3159,9 +3159,6 @@
"issueNumber"
]
},
"supportedContentTypes": [
"text/json"
],
"when": "config.githubPullRequests.experimental.chat"
},
{
Expand All @@ -3187,9 +3184,6 @@
"thread_id"
]
},
"supportedContentTypes": [
"text/json"
],
"when": "config.githubPullRequests.experimental.chat"
},
{
Expand Down Expand Up @@ -3579,10 +3573,15 @@
"commentCount",
"reactionCount"
]
},
"totalIssues": {
"type": "number",
"description": "The total number of issues in the search."
}
},
"required": [
"issues"
"arrayOfIssues",
"totalIssues"
]
},
"when": "config.githubPullRequests.experimental.chat"
Expand Down
6 changes: 1 addition & 5 deletions src/lm/participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,9 @@ export class ChatParticipant implements vscode.Disposable {
for (const toolCall of toolCalls) {
let toolCallResult = (await toolCall.result);

// const plainText = toolCallResult[MimeTypes.textPlain];
// const markdown: string = toolCallResult[MimeTypes.textMarkdown];
// const json: JSON = toolCallResult[MimeTypes.textJson];
// const display = toolCallResult[MimeTypes.textDisplay]; // our own fake type that we use to indicate something that should be streamed to the user
// const command = toolCallResult[MimeTypes.command]; // our own fake type that we use to indicate something that should be executed as a command
const additionalContent: string[] = [];
let result: vscode.LanguageModelToolResultPart | undefined;

for (let i = 0; i < toolCallResult.content.length; i++) {
const part = toolCallResult.content[i];
if (!(part instanceof vscode.LanguageModelTextPart)) {
Expand Down
13 changes: 9 additions & 4 deletions src/lm/tools/displayIssuesTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ You are an expert on GitHub issues. You can help the user identify the most impo
`;

export class DisplayIssuesTool extends ToolBase<DisplayIssuesParameters> {
static ID = 'DisplayIssuesTool';
public static readonly toolId = 'github-pull-request_renderIssues';
private static ID = 'DisplayIssuesTool';
constructor(chatParticipantState: ChatParticipantState) {
super(chatParticipantState);
}
Expand Down Expand Up @@ -75,7 +76,11 @@ export class DisplayIssuesTool extends ToolBase<DisplayIssuesParameters> {
return ['number', 'title', 'state'];
} else if (indexOfUrl >= 0) {
// Never include the url column
result[indexOfUrl] = 'number';
if (result.indexOf('number') >= 0) {
result.splice(indexOfUrl, 1);
} else {
result[indexOfUrl] = 'number';
}
}

return result;
Expand Down Expand Up @@ -107,9 +112,9 @@ export class DisplayIssuesTool extends ToolBase<DisplayIssuesParameters> {
}).join(' | ')} |`;
}

async prepareInvocation(_options: vscode.LanguageModelToolInvocationPrepareOptions<DisplayIssuesParameters>): Promise<vscode.PreparedToolInvocation> {
async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<DisplayIssuesParameters>): Promise<vscode.PreparedToolInvocation> {
return {
invocationMessage: vscode.l10n.t('Generating markdown table of issues'),
invocationMessage: vscode.l10n.t('Found {0} issues. Generating a markdown table of the first 10', options.parameters.totalIssues)
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/lm/tools/fetchIssueTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface FetchIssueResult {
}

export class FetchIssueTool extends RepoToolBase<FetchIssueToolParameters> {
public static readonly toolId = 'github-pull-request_issue_fetch';

async invoke(options: vscode.LanguageModelToolInvocationOptions<FetchIssueToolParameters>, _token: vscode.CancellationToken): Promise<vscode.LanguageModelToolResult> {
const { owner, name, folderManager } = this.getRepoInfo({ owner: options.parameters.repo?.owner, name: options.parameters.repo?.name });
const issueOrPullRequest = await folderManager.resolveIssueOrPullRequest(owner, name, options.parameters.issueNumber);
Expand Down Expand Up @@ -72,5 +74,6 @@ export class FetchIssueTool extends RepoToolBase<FetchIssueToolParameters> {
return {
invocationMessage: url ? vscode.l10n.t('Fetching item [#{0}]({1}) from GitHub', options.parameters.issueNumber, url) : vscode.l10n.t('Fetching item #{0} from GitHub', options.parameters.issueNumber),
};

}
}
2 changes: 2 additions & 0 deletions src/lm/tools/fetchNotificationTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface FetchNotificationResult {
}

export class FetchNotificationTool extends RepoToolBase<FetchNotificationToolParameters> {
public static readonly toolId = 'github-pull-request_notification_fetch';

async prepareInvocation(_options: vscode.LanguageModelToolInvocationPrepareOptions<FetchNotificationToolParameters>): Promise<vscode.PreparedToolInvocation> {
return {
Expand Down Expand Up @@ -102,4 +103,5 @@ export class FetchNotificationTool extends RepoToolBase<FetchNotificationToolPar
new vscode.LanguageModelTextPart('Above is a stringified JSON representation of the notification. This can be passed to other tools for further processing or display.')
]);
}

}
31 changes: 17 additions & 14 deletions src/lm/tools/searchTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode';
import Logger from '../../common/logger';
import { FolderRepositoryManager } from '../../github/folderRepositoryManager';
import { ILabel } from '../../github/interface';
import { concatAsyncIterable, RepoToolBase, TOOL_MARKDOWN_RESULT } from './toolsUtils';
import { concatAsyncIterable, RepoToolBase } from './toolsUtils';

interface ConvertToQuerySyntaxParameters {
naturalLanguageString: string;
Expand Down Expand Up @@ -78,6 +78,7 @@ const githubSearchSyntax = {
const MATCH_UNQUOTED_SPACES = /(?!\B"[^"]*)\s+(?![^"]*"\B)/;

export class ConvertToSearchSyntaxTool extends RepoToolBase<ConvertToQuerySyntaxParameters> {
public static readonly toolId = 'github-pull-request_formSearchQuery';
static ID = 'ConvertToSearchSyntaxTool';

private async fullQueryAssistantPrompt(folderRepoManager: FolderRepositoryManager): Promise<string> {
Expand Down Expand Up @@ -345,10 +346,6 @@ You are getting ready to make a GitHub search query. Given a natural language qu
return concatAsyncIterable(response.text);
}

private toGitHubUrl(query: string) {
return `https://github.com/issues/?q=${encodeURIComponent(query)}`;
}

async prepareInvocation(_options: vscode.LanguageModelToolInvocationPrepareOptions<ConvertToQuerySyntaxParameters>): Promise<vscode.PreparedToolInvocation> {
return {
invocationMessage: vscode.l10n.t('Converting to search syntax')
Expand Down Expand Up @@ -383,9 +380,7 @@ You are getting ready to make a GitHub search query. Given a natural language qu
name
}
};
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(TOOL_MARKDOWN_RESULT),
new vscode.LanguageModelTextPart(vscode.l10n.t('Query \`{0}\`. [Open on GitHub.com]({1})\n\n', result.query, this.toGitHubUrl(result.query))),
new vscode.LanguageModelTextPart(JSON.stringify(json)),
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(JSON.stringify(json)),
new vscode.LanguageModelTextPart('Above is the query in stringified json format. You can pass this VERBATIM to a tool that knows how to search.')]);
}
}
Expand Down Expand Up @@ -419,14 +414,23 @@ export interface IssueSearchResultItem {

export interface SearchToolResult {
arrayOfIssues: IssueSearchResultItem[];
totalIssues: number;
}

export class SearchTool extends RepoToolBase<SearchToolParameters> {
public static readonly toolId = 'github-pull-request_doSearch';
static ID = 'SearchTool';

async prepareInvocation(_options: vscode.LanguageModelToolInvocationPrepareOptions<SearchToolParameters>): Promise<vscode.PreparedToolInvocation> {

private toGitHubUrl(query: string) {
return `https://github.com/issues/?q=${encodeURIComponent(query)}`;
}

async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<SearchToolParameters>): Promise<vscode.PreparedToolInvocation> {
const parameterQuery = options.parameters.query;

return {
invocationMessage: vscode.l10n.t('Searching for issues')
invocationMessage: vscode.l10n.t('Searching for issues with "{0}". [Open on GitHub.com]({1})', parameterQuery, this.toGitHubUrl(parameterQuery))
};
}

Expand Down Expand Up @@ -458,13 +462,12 @@ export class SearchTool extends RepoToolBase<SearchToolParameters> {
commentCount: item.commentCount,
reactionCount: item.reactionCount
};
})
}),
totalIssues: searchResult.totalCount ?? searchResult.items.length
};
Logger.debug(`Found ${result.arrayOfIssues.length} issues, first issue ${result.arrayOfIssues[0]?.number}.`, SearchTool.ID);

return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(TOOL_MARKDOWN_RESULT),
new vscode.LanguageModelTextPart((result.arrayOfIssues.length < searchResult.items.length) && (searchResult.totalCount !== undefined) ? vscode.l10n.t('Found {0} issues, using the first {1}', searchResult.totalCount, cutoff) : vscode.l10n.t('Found {0} issues.', result.arrayOfIssues.length)),
new vscode.LanguageModelTextPart(JSON.stringify(result)),
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(JSON.stringify(result)),
new vscode.LanguageModelTextPart(`Above are the issues I found for the query ${parameterQuery} in json format. You can pass these to a tool that can display them.`)]);
}
}
3 changes: 3 additions & 0 deletions src/lm/tools/suggestFixTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { RepositoriesManager } from '../../github/repositoriesManager';
import { IssueResult, IssueToolParameters } from './toolsUtils';

export class SuggestFixTool implements vscode.LanguageModelTool<IssueToolParameters> {
public static readonly toolId = 'github-pull-request_suggest-fix';

constructor(private readonly repositoriesManager: RepositoriesManager) { }

async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IssueToolParameters>): Promise<vscode.PreparedToolInvocation> {
Expand Down Expand Up @@ -74,4 +76,5 @@ export class SuggestFixTool implements vscode.LanguageModelTool<IssueToolParamet
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(responseResult)]);
}


}
2 changes: 2 additions & 0 deletions src/lm/tools/summarizeIssueTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FetchIssueResult } from './fetchIssueTool';
import { concatAsyncIterable } from './toolsUtils';

export class IssueSummarizationTool implements vscode.LanguageModelTool<FetchIssueResult> {
public static readonly toolId = 'github-pull-request_issue_summarize';

async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<FetchIssueResult>): Promise<vscode.PreparedToolInvocation> {
if (!options.parameters.title) {
Expand Down Expand Up @@ -74,4 +75,5 @@ Do not output code. When you try to summarize PR changes, write in a textual for
Make sure the summary is at least as short or shorter than the issue or PR with the comments and the patches if there are.
`;
}

}
2 changes: 2 additions & 0 deletions src/lm/tools/summarizeNotificationsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FetchNotificationResult } from './fetchNotificationTool';
import { concatAsyncIterable, TOOL_COMMAND_RESULT } from './toolsUtils';

export class NotificationSummarizationTool implements vscode.LanguageModelTool<FetchNotificationResult> {
public static readonly toolId = 'github-pull-request_notification_summarize';

async invoke(options: vscode.LanguageModelToolInvocationOptions<FetchNotificationResult>, _token: vscode.CancellationToken): Promise<vscode.LanguageModelToolResult | undefined> {
let notificationInfo: string = '';
Expand Down Expand Up @@ -104,4 +105,5 @@ Both 'Unread Thread' and 'Unread Comments' should not appear at the same time as
<comments>
`;
}

}
16 changes: 8 additions & 8 deletions src/lm/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ export function registerTools(context: vscode.ExtensionContext, credentialStore:
}

function registerFetchingTools(context: vscode.ExtensionContext, credentialStore: CredentialStore, repositoriesManager: RepositoriesManager, chatParticipantState: ChatParticipantState) {
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_issue_fetch', new FetchIssueTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_notification_fetch', new FetchNotificationTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool(FetchIssueTool.toolId, new FetchIssueTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool(FetchNotificationTool.toolId, new FetchNotificationTool(credentialStore, repositoriesManager, chatParticipantState)));
}

function registerSummarizationTools(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_issue_summarize', new IssueSummarizationTool()));
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_notification_summarize', new NotificationSummarizationTool()));
context.subscriptions.push(vscode.lm.registerTool(IssueSummarizationTool.toolId, new IssueSummarizationTool()));
context.subscriptions.push(vscode.lm.registerTool(NotificationSummarizationTool.toolId, new NotificationSummarizationTool()));
}

function registerSuggestFixTool(context: vscode.ExtensionContext, repositoriesManager: RepositoriesManager) {
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_suggest-fix', new SuggestFixTool(repositoriesManager)));
context.subscriptions.push(vscode.lm.registerTool(SuggestFixTool.toolId, new SuggestFixTool(repositoriesManager)));
}

function registerSearchTools(context: vscode.ExtensionContext, credentialStore: CredentialStore, repositoriesManager: RepositoriesManager, chatParticipantState: ChatParticipantState) {
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_formSearchQuery', new ConvertToSearchSyntaxTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_doSearch', new SearchTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool('github-pull-request_renderIssues', new DisplayIssuesTool(chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool(ConvertToSearchSyntaxTool.toolId, new ConvertToSearchSyntaxTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool(SearchTool.toolId, new SearchTool(credentialStore, repositoriesManager, chatParticipantState)));
context.subscriptions.push(vscode.lm.registerTool(DisplayIssuesTool.toolId, new DisplayIssuesTool(chatParticipantState)));
}

0 comments on commit 33f3bfa

Please sign in to comment.