16
16
17
17
import {
18
18
Agent ,
19
+ CommunicationRecordingService ,
19
20
getJsonOfResponse ,
20
21
isLanguageModelParsedResponse ,
21
22
LanguageModelRegistry , LanguageModelRequirement ,
22
23
PromptService
23
24
} from '@theia/ai-core/lib/common' ;
24
- import { ILogger } from '@theia/core' ;
25
+ import { generateUuid , ILogger } from '@theia/core' ;
25
26
import { inject , injectable } from '@theia/core/shared/inversify' ;
26
27
import { z } from 'zod' ;
27
28
import zodToJsonSchema from 'zod-to-json-schema' ;
@@ -33,6 +34,8 @@ type Commands = z.infer<typeof Commands>;
33
34
34
35
@injectable ( )
35
36
export class AiTerminalAgent implements Agent {
37
+ @inject ( CommunicationRecordingService )
38
+ protected recordingService : CommunicationRecordingService ;
36
39
37
40
id = 'Terminal Assistant' ;
38
41
name = 'Terminal Assistant' ;
@@ -153,6 +156,18 @@ recent-terminal-contents:
153
156
return [ ] ;
154
157
}
155
158
159
+ // since we do not actually hold complete conversions, the request/response pair is considered a session
160
+ const sessionId = generateUuid ( ) ;
161
+ const requestId = generateUuid ( ) ;
162
+ this . recordingService . recordRequest ( {
163
+ agentId : this . id ,
164
+ sessionId,
165
+ timestamp : Date . now ( ) ,
166
+ requestId,
167
+ request : systemPrompt ,
168
+ messages : [ userPrompt ] ,
169
+ } ) ;
170
+
156
171
try {
157
172
const result = await lm . request ( {
158
173
messages : [
@@ -181,12 +196,28 @@ recent-terminal-contents:
181
196
// model returned structured output
182
197
const parsedResult = Commands . safeParse ( result . parsed ) ;
183
198
if ( parsedResult . success ) {
199
+ const responseTextfromParsed = JSON . stringify ( parsedResult . data . commands ) ;
200
+ this . recordingService . recordResponse ( {
201
+ agentId : this . id ,
202
+ sessionId,
203
+ timestamp : Date . now ( ) ,
204
+ requestId,
205
+ response : responseTextfromParsed ,
206
+ } ) ;
184
207
return parsedResult . data . commands ;
185
208
}
186
209
}
187
210
188
211
// fall back to agent-based parsing of result
189
212
const jsonResult = await getJsonOfResponse ( result ) ;
213
+ const responseTextFromJSON = JSON . stringify ( jsonResult ) ;
214
+ this . recordingService . recordResponse ( {
215
+ agentId : this . id ,
216
+ sessionId,
217
+ timestamp : Date . now ( ) ,
218
+ requestId,
219
+ response : responseTextFromJSON
220
+ } ) ;
190
221
const parsedJsonResult = Commands . safeParse ( jsonResult ) ;
191
222
if ( parsedJsonResult . success ) {
192
223
return parsedJsonResult . data . commands ;
0 commit comments