Skip to content

Commit ce5e13f

Browse files
authored
[Theia AI] Terminal agent records its requests (#14246)
* Terminal agent records its requests fixed #14245 Signed-off-by: Jonas Helming <[email protected]>
1 parent a9b01fe commit ce5e13f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

packages/ai-terminal/src/browser/ai-terminal-agent.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
import {
1818
Agent,
19+
CommunicationRecordingService,
1920
getJsonOfResponse,
2021
isLanguageModelParsedResponse,
2122
LanguageModelRegistry, LanguageModelRequirement,
2223
PromptService
2324
} from '@theia/ai-core/lib/common';
24-
import { ILogger } from '@theia/core';
25+
import { generateUuid, ILogger } from '@theia/core';
2526
import { inject, injectable } from '@theia/core/shared/inversify';
2627
import { z } from 'zod';
2728
import zodToJsonSchema from 'zod-to-json-schema';
@@ -33,6 +34,8 @@ type Commands = z.infer<typeof Commands>;
3334

3435
@injectable()
3536
export class AiTerminalAgent implements Agent {
37+
@inject(CommunicationRecordingService)
38+
protected recordingService: CommunicationRecordingService;
3639

3740
id = 'Terminal Assistant';
3841
name = 'Terminal Assistant';
@@ -153,6 +156,18 @@ recent-terminal-contents:
153156
return [];
154157
}
155158

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+
156171
try {
157172
const result = await lm.request({
158173
messages: [
@@ -181,12 +196,28 @@ recent-terminal-contents:
181196
// model returned structured output
182197
const parsedResult = Commands.safeParse(result.parsed);
183198
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+
});
184207
return parsedResult.data.commands;
185208
}
186209
}
187210

188211
// fall back to agent-based parsing of result
189212
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+
});
190221
const parsedJsonResult = Commands.safeParse(jsonResult);
191222
if (parsedJsonResult.success) {
192223
return parsedJsonResult.data.commands;

0 commit comments

Comments
 (0)