Skip to content

Commit 8ae7303

Browse files
authored
Consume new API (#3322)
* Consume new API * Update snapshot * Update api version
1 parent e6f4698 commit 8ae7303

7 files changed

Lines changed: 44 additions & 64 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"mappedEditsProvider",
113113
"aiRelatedInformation",
114114
"aiSettingsSearch",
115-
"chatParticipantAdditions",
115+
"chatParticipantAdditions@1",
116116
"defaultChatParticipant@4",
117117
"contribSourceControlInputBoxMenu",
118118
"authLearnMore",

src/extension/codeBlocks/node/codeBlockProcessor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class CodeBlockTrackingChatResponseStream implements ChatResponseStream {
124124
externalEdit = this.forward(this._wrapped.externalEdit.bind(this._wrapped));
125125
beginToolInvocation = this.forward(this._wrapped.beginToolInvocation.bind(this._wrapped));
126126
updateToolInvocation = this.forward(this._wrapped.updateToolInvocation.bind(this._wrapped));
127+
usage = this.forward(this._wrapped.usage.bind(this._wrapped));
127128

128129
questionCarousel(questions: ChatQuestion[], allowSkip?: boolean): Thenable<Record<string, unknown> | undefined> {
129130
this._codeBlockProcessor.flush();

src/extension/intents/node/toolCallingLoop.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,16 @@ export abstract class ToolCallingLoop<TOptions extends IToolCallingLoopOptions =
491491
tools: availableTools,
492492
});
493493
fetchStreamSource?.resolve();
494-
let chatResult = await processResponsePromise ?? undefined;
495-
496-
// hydrate the token usage into the chat result as this renders the context window widget
497-
if (fetchResult.type === ChatFetchResponseType.Success && fetchResult.usage) {
498-
chatResult = {
499-
...chatResult, usage: {
500-
completionTokens: fetchResult.usage.completion_tokens,
501-
promptTokens: fetchResult.usage.prompt_tokens,
502-
promptTokenDetails,
503-
}
504-
};
494+
const chatResult = await processResponsePromise ?? undefined;
495+
496+
// Report token usage to the stream for rendering the context window widget
497+
const stream = streamParticipants[streamParticipants.length - 1];
498+
if (fetchResult.type === ChatFetchResponseType.Success && fetchResult.usage && stream) {
499+
stream.usage({
500+
completionTokens: fetchResult.usage.completion_tokens,
501+
promptTokens: fetchResult.usage.prompt_tokens,
502+
promptTokenDetails,
503+
});
505504
}
506505

507506
// Validate authentication session upgrade and handle accordingly

src/extension/linkify/common/responseStreamWithLinkification.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import type { ChatQuestion, ChatResponseClearToPreviousToolInvocationReason, ChatResponseFileTree, ChatResponsePart, ChatResponseStream, ChatToolInvocationStreamData, ChatVulnerability, ChatWorkspaceFileEdit, Command, Location, NotebookEdit, TextEdit, ThinkingDelta, Uri } from 'vscode';
5+
import type { ChatQuestion, ChatResponseClearToPreviousToolInvocationReason, ChatResponseFileTree, ChatResponsePart, ChatResponseStream, ChatResultUsage, ChatToolInvocationStreamData, ChatVulnerability, ChatWorkspaceFileEdit, Command, Location, NotebookEdit, TextEdit, ThinkingDelta, Uri } from 'vscode';
66
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
77
import { FinalizableChatResponseStream } from '../../../util/common/chatResponseStreamImpl';
88
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
@@ -171,6 +171,11 @@ export class ResponseStreamWithLinkification implements FinalizableChatResponseS
171171
return this.enqueue(() => this._progress.questionCarousel(questions, allowSkip), true);
172172
}
173173

174+
usage(usage: ChatResultUsage): ChatResponseStream {
175+
this.enqueue(() => this._progress.usage(usage), false);
176+
return this;
177+
}
178+
174179
//#endregion
175180

176181
private sequencer: Promise<unknown> = Promise.resolve();

src/extension/prompt/node/test/__snapshots__/defaultIntentRequestHandler.spec.ts.snap

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,6 @@ exports[`defaultIntentRequestHandler > confirms on max tool call iterations, and
251251
},
252252
],
253253
},
254-
"usage": {
255-
"completionTokens": 0,
256-
"promptTokenDetails": [
257-
{
258-
"category": "User Context",
259-
"label": "Messages",
260-
"percentageOfPrompt": 70,
261-
},
262-
],
263-
"promptTokens": 0,
264-
},
265254
}
266255
`;
267256

@@ -363,17 +352,6 @@ exports[`defaultIntentRequestHandler > confirms on max tool call iterations, and
363352
},
364353
],
365354
},
366-
"usage": {
367-
"completionTokens": 0,
368-
"promptTokenDetails": [
369-
{
370-
"category": "User Context",
371-
"label": "Messages",
372-
"percentageOfPrompt": 70,
373-
},
374-
],
375-
"promptTokens": 0,
376-
},
377355
}
378356
`;
379357

@@ -2473,17 +2451,6 @@ exports[`defaultIntentRequestHandler > makes a successful request with a single
24732451
},
24742452
],
24752453
},
2476-
"usage": {
2477-
"completionTokens": 0,
2478-
"promptTokenDetails": [
2479-
{
2480-
"category": "User Context",
2481-
"label": "Messages",
2482-
"percentageOfPrompt": 70,
2483-
},
2484-
],
2485-
"promptTokens": 0,
2486-
},
24872454
}
24882455
`;
24892456

@@ -2822,17 +2789,6 @@ exports[`defaultIntentRequestHandler > makes a tool call turn 1`] = `
28222789
},
28232790
],
28242791
},
2825-
"usage": {
2826-
"completionTokens": 0,
2827-
"promptTokenDetails": [
2828-
{
2829-
"category": "User Context",
2830-
"label": "Messages",
2831-
"percentageOfPrompt": 70,
2832-
},
2833-
],
2834-
"promptTokens": 0,
2835-
},
28362792
}
28372793
`;
28382794

src/extension/vscode.proposed.chatParticipantAdditions.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
// version: 1
7+
68
declare module 'vscode' {
79

810
export interface ChatParticipant {
@@ -553,6 +555,13 @@ declare module 'vscode' {
553555
push(part: ExtendedChatResponsePart): void;
554556

555557
clearToPreviousToolInvocation(reason: ChatResponseClearToPreviousToolInvocationReason): void;
558+
559+
/**
560+
* Report token usage information for this request.
561+
* This is typically called when the underlying language model provides usage statistics.
562+
* @param usage Token usage information including prompt and completion tokens
563+
*/
564+
usage(usage: ChatResultUsage): void;
556565
}
557566

558567
export enum ChatResponseReferencePartStatusKind {
@@ -744,12 +753,6 @@ declare module 'vscode' {
744753
* An optional detail string that will be rendered at the end of the response in certain UI contexts.
745754
*/
746755
details?: string;
747-
748-
/**
749-
* Token usage information for this request, if available.
750-
* This is typically provided by the underlying language model.
751-
*/
752-
readonly usage?: ChatResultUsage;
753756
}
754757

755758
export namespace chat {

src/util/common/chatResponseStreamImpl.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { ChatResponseReferencePartStatusKind } from '@vscode/prompt-tsx';
7-
import type { ChatQuestion, ChatResponseFileTree, ChatResponseStream, ChatToolInvocationStreamData, ChatVulnerability, ChatWorkspaceFileEdit, Command, ExtendedChatResponsePart, Location, NotebookEdit, Progress, ThinkingDelta, Uri } from 'vscode';
7+
import type { ChatQuestion, ChatResponseFileTree, ChatResponseStream, ChatResultUsage, ChatToolInvocationStreamData, ChatVulnerability, ChatWorkspaceFileEdit, Command, ExtendedChatResponsePart, Location, NotebookEdit, Progress, ThinkingDelta, Uri } from 'vscode';
88
import { ChatResponseAnchorPart, ChatResponseClearToPreviousToolInvocationReason, ChatResponseCodeblockUriPart, ChatResponseCodeCitationPart, ChatResponseCommandButtonPart, ChatResponseConfirmationPart, ChatResponseExternalEditPart, ChatResponseFileTreePart, ChatResponseMarkdownPart, ChatResponseMarkdownWithVulnerabilitiesPart, ChatResponseNotebookEditPart, ChatResponseProgressPart, ChatResponseProgressPart2, ChatResponseReferencePart, ChatResponseReferencePart2, ChatResponseTextEditPart, ChatResponseThinkingProgressPart, ChatResponseWarningPart, ChatResponseWorkspaceEditPart, MarkdownString, TextEdit } from '../../vscodeTypes';
99
import type { ThemeIcon } from '../vs/base/common/themables';
1010

@@ -43,6 +43,9 @@ export class ChatResponseStreamImpl implements FinalizableChatResponseStream {
4343
},
4444
(questions, allowSkip) => {
4545
return stream.questionCarousel(questions, allowSkip);
46+
},
47+
(usage) => {
48+
stream.usage(usage);
4649
}
4750
);
4851
}
@@ -66,6 +69,9 @@ export class ChatResponseStreamImpl implements FinalizableChatResponseStream {
6669
},
6770
(questions, allowSkip) => {
6871
return stream.questionCarousel(questions, allowSkip);
72+
},
73+
(usage) => {
74+
stream.usage(usage);
6975
});
7076
}
7177

@@ -89,6 +95,9 @@ export class ChatResponseStreamImpl implements FinalizableChatResponseStream {
8995
},
9096
(questions, allowSkip) => {
9197
return stream.questionCarousel(questions, allowSkip);
98+
},
99+
(usage) => {
100+
stream.usage(usage);
92101
});
93102
}
94103

@@ -99,6 +108,7 @@ export class ChatResponseStreamImpl implements FinalizableChatResponseStream {
99108
private readonly _beginToolInvocation?: (toolCallId: string, toolName: string, streamData?: ChatToolInvocationStreamData) => void,
100109
private readonly _updateToolInvocation?: (toolCallId: string, streamData: ChatToolInvocationStreamData) => void,
101110
private readonly _questionCarousel?: (questions: ChatQuestion[], allowSkip?: boolean) => Thenable<Record<string, unknown> | undefined>,
111+
private readonly _usage?: (usage: ChatResultUsage) => void,
102112
) { }
103113

104114
async finalize(): Promise<void> {
@@ -219,4 +229,10 @@ export class ChatResponseStreamImpl implements FinalizableChatResponseStream {
219229
}
220230
return Promise.resolve(undefined);
221231
}
232+
233+
usage(usage: ChatResultUsage): void {
234+
if (this._usage) {
235+
this._usage(usage);
236+
}
237+
}
222238
}

0 commit comments

Comments
 (0)