Skip to content

Commit 7c3da33

Browse files
aiday-maralexr00
andauthored
Handling undefined values in summarize notifications tool (#6421)
* Inaccurate result when asking for authored issues (#6419) Part of #6362 * handling undefined values * changing the order of the response text and tool command result * undo change in search tools * removing the changing of order * review comments * plolishing * merging return statements * pushing the new text model part into the summarize tool result --------- Co-authored-by: Alex Ross <[email protected]>
1 parent 9251815 commit 7c3da33

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,7 @@
33233323
"type": "string",
33243324
"description": "The repo in which the issue/PR is located"
33253325
},
3326-
"unreadComments": {
3326+
"comments": {
33273327
"type": "array",
33283328
"items": {
33293329
"type": "object",
@@ -3380,13 +3380,15 @@
33803380
},
33813381
"required": [
33823382
"title",
3383-
"unreadComments",
3383+
"comments",
33843384
"lastUpdatedAt",
33853385
"unread",
33863386
"threadId",
33873387
"notificationKey",
33883388
"owner",
3389-
"repo"
3389+
"repo",
3390+
"itemNumber",
3391+
"itemType"
33903392
]
33913393
},
33923394
"when": "config.githubPullRequests.experimental.chat"

src/lm/tools/fetchNotificationTool.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ export interface FetchNotificationResult {
2525
unread: boolean;
2626
title: string;
2727
body: string;
28-
unreadComments: {
28+
comments?: {
2929
author: string;
3030
body: string;
3131
}[];
3232
owner: string;
3333
repo: string;
34-
itemNumber: string;
35-
itemType: 'issue' | 'pr';
34+
itemNumber?: string;
35+
itemType?: 'issue' | 'pr';
3636
fileChanges?: FileChange[];
37-
threadId: number,
38-
notificationKey: string
37+
threadId?: number,
38+
notificationKey?: string
3939
}
4040

4141
export class FetchNotificationTool extends RepoToolBase<FetchNotificationToolParameters> {
@@ -73,20 +73,20 @@ export class FetchNotificationTool extends RepoToolBase<FetchNotificationToolPar
7373
}
7474
const itemType = issueOrPR instanceof PullRequestModel ? 'pr' : 'issue';
7575
const notificationKey = getNotificationKey(owner, name, String(issueOrPR.number));
76-
const comments = issueOrPR.item.comments ?? [];
77-
let unreadComments: { body: string; author: string }[];
78-
if (lastReadAt !== undefined && comments) {
79-
unreadComments = comments.filter(comment => {
76+
const itemComments = issueOrPR.item.comments ?? [];
77+
let comments: { body: string; author: string }[];
78+
if (lastReadAt !== undefined && itemComments) {
79+
comments = itemComments.filter(comment => {
8080
return comment.createdAt > lastReadAt;
8181
}).map(comment => { return { body: comment.body, author: comment.author.login }; });
8282
} else {
83-
unreadComments = comments.map(comment => { return { body: comment.body, author: comment.author.login }; });
83+
comments = itemComments.map(comment => { return { body: comment.body, author: comment.author.login }; });
8484
}
8585
const result: FetchNotificationResult = {
8686
lastReadAt,
8787
lastUpdatedAt,
8888
unread,
89-
unreadComments,
89+
comments,
9090
threadId,
9191
notificationKey,
9292
title: issueOrPR.title,

src/lm/tools/summarizeNotificationsTool.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export class NotificationSummarizationTool implements vscode.LanguageModelTool<F
1313

1414
async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<FetchNotificationResult>): Promise<vscode.PreparedToolInvocation> {
1515
const parameters = options.parameters;
16+
if (!parameters.itemType || !parameters.itemNumber) {
17+
return {
18+
invocationMessage: vscode.l10n.t('Summarizing notification')
19+
};
20+
}
1621
const type = parameters.itemType === 'issue' ? 'issues' : 'pull';
1722
const url = `https://github.com/${parameters.owner}/${parameters.repo}/${type}/${parameters.itemNumber}`;
1823
return {
@@ -44,47 +49,48 @@ Patch: ${fileChange.patch}
4449
}
4550
}
4651

47-
const unreadComments = options.parameters.unreadComments;
48-
if (unreadComments.length > 0) {
52+
const unreadComments = options.parameters.comments;
53+
if (unreadComments && unreadComments.length > 0) {
4954
notificationInfo += `
5055
The following are the unread comments of the thread:
5156
`;
52-
}
53-
for (const [index, comment] of unreadComments.entries()) {
54-
notificationInfo += `
57+
for (const [index, comment] of unreadComments.entries()) {
58+
notificationInfo += `
5559
Comment ${index} :
5660
Author: ${comment.author}
5761
Body: ${comment.body}
5862
`;
63+
}
5964
}
6065
const models = await vscode.lm.selectChatModels({
6166
vendor: 'copilot',
6267
family: 'gpt-4o'
6368
});
6469
const model = models[0];
65-
const markAsReadCommand: vscode.Command = {
66-
title: 'Mark As Read',
67-
command: 'notification.markAsRead',
68-
arguments: [{
69-
threadId: options.parameters.threadId,
70-
notificationKey: options.parameters.notificationKey
71-
}]
72-
};
70+
const content: vscode.LanguageModelTextPart[] = [];
71+
const threadId = options.parameters.threadId;
72+
const notificationKey = options.parameters.notificationKey;
73+
if (threadId && notificationKey) {
74+
const markAsReadCommand = {
75+
title: 'Mark As Read',
76+
command: 'notification.markAsRead',
77+
arguments: [{ threadId, notificationKey }]
78+
};
79+
content.push(new vscode.LanguageModelTextPart(TOOL_COMMAND_RESULT));
80+
content.push(new vscode.LanguageModelTextPart(JSON.stringify(markAsReadCommand)));
81+
}
7382
if (model) {
7483
const messages = [vscode.LanguageModelChatMessage.User(this.summarizeInstructions(options.parameters.owner, options.parameters.repo))];
7584
messages.push(vscode.LanguageModelChatMessage.User(`The notification information is as follows:`));
7685
messages.push(vscode.LanguageModelChatMessage.User(notificationInfo));
7786
const response = await model.sendRequest(messages, {});
7887
const responseText = await concatAsyncIterable(response.text);
79-
80-
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(TOOL_COMMAND_RESULT),
81-
new vscode.LanguageModelTextPart(JSON.stringify(markAsReadCommand)),
82-
new vscode.LanguageModelTextPart(responseText)]);
88+
content.push(new vscode.LanguageModelTextPart(responseText));
8389
} else {
84-
return new vscode.LanguageModelToolResult([new vscode.LanguageModelTextPart(TOOL_COMMAND_RESULT),
85-
new vscode.LanguageModelTextPart(JSON.stringify(markAsReadCommand)),
86-
new vscode.LanguageModelTextPart(notificationInfo)]);
90+
content.push(new vscode.LanguageModelTextPart(notificationInfo));
8791
}
92+
content.push(new vscode.LanguageModelTextPart('Above is a summary of the notification. Extract and output this notification summary directly as is to the user. Do not output the result from the call to the fetch notification tool.'));
93+
return new vscode.LanguageModelToolResult(content);
8894
}
8995

9096
private summarizeInstructions(owner: string, repo: string): string {

0 commit comments

Comments
 (0)