33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import type * as vscode from 'vscode' ;
76import { CancellationToken } from 'vs/base/common/cancellation' ;
7+ import { onUnexpectedExternalError } from 'vs/base/common/errors' ;
88import { IDisposable , toDisposable } from 'vs/base/common/lifecycle' ;
9- import { ExtensionIdentifier , IExtensionDescription } from 'vs/platform/extensions/common/extensions' ;
10- import { ExtHostChatVariablesShape , IMainContext , MainContext , MainThreadChatVariablesShape } from 'vs/workbench/api/common/extHost.protocol' ;
9+ import { IExtensionDescription } from 'vs/platform/extensions/common/extensions' ;
10+ import { ExtHostChatVariablesShape , IChatVariableResolverProgressDto , IMainContext , MainContext , MainThreadChatVariablesShape } from 'vs/workbench/api/common/extHost.protocol' ;
11+ import * as typeConvert from 'vs/workbench/api/common/extHostTypeConverters' ;
12+ import * as extHostTypes from 'vs/workbench/api/common/extHostTypes' ;
1113import { IChatRequestVariableValue , IChatVariableData } from 'vs/workbench/contrib/chat/common/chatVariables' ;
12- import { onUnexpectedExternalError } from 'vs/base/ common/errors ' ;
13- import { ChatVariable } from 'vs/workbench/api/common/extHostTypeConverters ' ;
14+ import { checkProposedApiEnabled } from 'vs/workbench/services/extensions/ common/extensions ' ;
15+ import type * as vscode from 'vscode ' ;
1416
1517export class ExtHostChatVariables implements ExtHostChatVariablesShape {
1618
1719 private static _idPool = 0 ;
1820
19- private readonly _resolver = new Map < number , { extension : ExtensionIdentifier ; data : IChatVariableData ; resolver : vscode . ChatVariableResolver } > ( ) ;
21+ private readonly _resolver = new Map < number , { extension : IExtensionDescription ; data : IChatVariableData ; resolver : vscode . ChatVariableResolver } > ( ) ;
2022 private readonly _proxy : MainThreadChatVariablesShape ;
2123
2224 constructor ( mainContext : IMainContext ) {
2325 this . _proxy = mainContext . getProxy ( MainContext . MainThreadChatVariables ) ;
2426 }
2527
26- async $resolveVariable ( handle : number , messageText : string , token : CancellationToken ) : Promise < IChatRequestVariableValue [ ] | undefined > {
28+ async $resolveVariable ( handle : number , requestId : string , messageText : string , token : CancellationToken ) : Promise < IChatRequestVariableValue [ ] | undefined > {
2729 const item = this . _resolver . get ( handle ) ;
2830 if ( ! item ) {
2931 return undefined ;
3032 }
3133 try {
32- const value = await item . resolver . resolve ( item . data . name , { prompt : messageText } , token ) ;
33- if ( value ) {
34- return value . map ( ChatVariable . from ) ;
34+ if ( item . resolver . resolve2 ) {
35+ checkProposedApiEnabled ( item . extension , 'chatAgents2Additions' ) ;
36+ const stream = new ChatVariableResolverResponseStream ( requestId , this . _proxy ) ;
37+ const value = await item . resolver . resolve2 ( item . data . name , { prompt : messageText } , stream . apiObject , token ) ;
38+ if ( value ) {
39+ return value . map ( typeConvert . ChatVariable . from ) ;
40+ }
41+ } else {
42+ const value = await item . resolver . resolve ( item . data . name , { prompt : messageText } , token ) ;
43+ if ( value ) {
44+ return value . map ( typeConvert . ChatVariable . from ) ;
45+ }
3546 }
3647 } catch ( err ) {
3748 onUnexpectedExternalError ( err ) ;
@@ -41,7 +52,7 @@ export class ExtHostChatVariables implements ExtHostChatVariablesShape {
4152
4253 registerVariableResolver ( extension : IExtensionDescription , name : string , description : string , resolver : vscode . ChatVariableResolver ) : IDisposable {
4354 const handle = ExtHostChatVariables . _idPool ++ ;
44- this . _resolver . set ( handle , { extension : extension . identifier , data : { name, description } , resolver : resolver } ) ;
55+ this . _resolver . set ( handle , { extension, data : { name, description } , resolver : resolver } ) ;
4556 this . _proxy . $registerVariable ( handle , { name, description } ) ;
4657
4758 return toDisposable ( ( ) => {
@@ -50,3 +61,61 @@ export class ExtHostChatVariables implements ExtHostChatVariablesShape {
5061 } ) ;
5162 }
5263}
64+
65+ class ChatVariableResolverResponseStream {
66+
67+ private _isClosed : boolean = false ;
68+ private _apiObject : vscode . ChatVariableResolverResponseStream | undefined ;
69+
70+ constructor (
71+ private readonly _requestId : string ,
72+ private readonly _proxy : MainThreadChatVariablesShape ,
73+ ) { }
74+
75+ close ( ) {
76+ this . _isClosed = true ;
77+ }
78+
79+ get apiObject ( ) {
80+ if ( ! this . _apiObject ) {
81+ const that = this ;
82+
83+ function throwIfDone ( source : Function | undefined ) {
84+ if ( that . _isClosed ) {
85+ const err = new Error ( 'Response stream has been closed' ) ;
86+ Error . captureStackTrace ( err , source ) ;
87+ throw err ;
88+ }
89+ }
90+
91+ const _report = ( progress : IChatVariableResolverProgressDto ) => {
92+ this . _proxy . $handleProgressChunk ( this . _requestId , progress ) ;
93+ } ;
94+
95+ this . _apiObject = {
96+ progress ( value ) {
97+ throwIfDone ( this . progress ) ;
98+ const part = new extHostTypes . ChatResponseProgressPart ( value ) ;
99+ const dto = typeConvert . ChatResponseProgressPart . to ( part ) ;
100+ _report ( dto ) ;
101+ return this ;
102+ } ,
103+ reference ( value ) {
104+ throwIfDone ( this . reference ) ;
105+ const part = new extHostTypes . ChatResponseReferencePart ( value ) ;
106+ const dto = typeConvert . ChatResponseReferencePart . to ( part ) ;
107+ _report ( dto ) ;
108+ return this ;
109+ } ,
110+ push ( part ) {
111+ throwIfDone ( this . push ) ;
112+ const dto = typeConvert . ChatResponsePart . to ( part ) ;
113+ _report ( dto as IChatVariableResolverProgressDto ) ;
114+ return this ;
115+ }
116+ } ;
117+ }
118+
119+ return this . _apiObject ;
120+ }
121+ }
0 commit comments