Skip to content

Commit

Permalink
Terminal agent records its requests
Browse files Browse the repository at this point in the history
fixed #14245

Signed-off-by: Jonas Helming <[email protected]>
  • Loading branch information
JonasHelming committed Oct 2, 2024
1 parent a9b01fe commit d457fa2
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/ai-terminal/src/browser/ai-terminal-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import {
Agent,
CommunicationHistoryEntry,
CommunicationRecordingService,
getJsonOfResponse,
isLanguageModelParsedResponse,
LanguageModelRegistry, LanguageModelRequirement,
PromptService
} from '@theia/ai-core/lib/common';
import { ILogger } from '@theia/core';
import { generateUuid, ILogger } from '@theia/core';
import { inject, injectable } from '@theia/core/shared/inversify';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
Expand All @@ -33,6 +35,8 @@ type Commands = z.infer<typeof Commands>;

@injectable()
export class AiTerminalAgent implements Agent {
@inject(CommunicationRecordingService)
protected recordingService: CommunicationRecordingService;

id = 'Terminal Assistant';
name = 'Terminal Assistant';
Expand Down Expand Up @@ -153,6 +157,20 @@ recent-terminal-contents:
return [];
}

// since we do not actually hold complete conversions, the request/response pair is considered a session
const sessionId = generateUuid();
const requestId = generateUuid();
const requestEntry: CommunicationHistoryEntry = {
agentId: this.id,
sessionId,
timestamp: Date.now(),
requestId,
request: systemPrompt,
messages: [userPrompt],
};

this.recordingService.recordRequest(requestEntry);

try {
const result = await lm.request({
messages: [
Expand Down Expand Up @@ -181,12 +199,28 @@ recent-terminal-contents:
// model returned structured output
const parsedResult = Commands.safeParse(result.parsed);
if (parsedResult.success) {
const responseText = JSON.stringify(parsedResult.data.commands);
this.recordingService.recordResponse({
agentId: this.id,
sessionId,
timestamp: Date.now(),
requestId,
response: responseText,
});
return parsedResult.data.commands;
}
}

// fall back to agent-based parsing of result
const jsonResult = await getJsonOfResponse(result);
const responseText = JSON.stringify(jsonResult);
this.recordingService.recordResponse({
agentId: this.id,
sessionId,
timestamp: Date.now(),
requestId,
response: responseText
});
const parsedJsonResult = Commands.safeParse(jsonResult);
if (parsedJsonResult.success) {
return parsedJsonResult.data.commands;
Expand Down

0 comments on commit d457fa2

Please sign in to comment.