Chat API: add 'command' to response history (#205377)

This commit is contained in:
Rob Lourens 2024-02-16 16:09:04 +00:00 committed by GitHub
parent 00124e9e58
commit 4ba66bf35e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 12 deletions

View file

@ -5,8 +5,8 @@
import * as assert from 'assert';
import 'mocha';
import { CancellationToken, chat, ChatAgentRequest, ChatAgentResult2, ChatVariableLevel, Disposable, interactive, InteractiveSession, ProviderResult } from 'vscode';
import { assertNoRpc, closeAllEditors, DeferredPromise, disposeAll } from '../utils';
import { CancellationToken, ChatAgentContext, ChatAgentRequest, ChatAgentResult2, ChatVariableLevel, Disposable, Event, EventEmitter, InteractiveSession, ProviderResult, chat, interactive } from 'vscode';
import { DeferredPromise, assertNoRpc, closeAllEditors, disposeAll } from '../utils';
suite('chat', () => {
@ -22,6 +22,15 @@ suite('chat', () => {
});
function getDeferredForRequest(): DeferredPromise<ChatAgentRequest> {
const deferred = new DeferredPromise<ChatAgentRequest>();
disposables.push(setupAgent()(request => deferred.complete(request.request)));
return deferred;
}
function setupAgent(): Event<{ request: ChatAgentRequest; context: ChatAgentContext }> {
const emitter = new EventEmitter<{ request: ChatAgentRequest; context: ChatAgentContext }>();
disposables.push();
disposables.push(interactive.registerInteractiveSessionProvider('provider', {
prepareSession: (_token: CancellationToken): ProviderResult<InteractiveSession> => {
return {
@ -31,9 +40,8 @@ suite('chat', () => {
},
}));
const deferred = new DeferredPromise<ChatAgentRequest>();
const agent = chat.createChatAgent('agent', (request, _context, _progress, _token) => {
deferred.complete(request);
const agent = chat.createChatAgent('agent', (request, context, _progress, _token) => {
emitter.fire({ request, context });
return null;
});
agent.isDefault = true;
@ -43,15 +51,26 @@ suite('chat', () => {
}
};
disposables.push(agent);
return deferred;
return emitter.event;
}
test('agent and slash command', async () => {
const deferred = getDeferredForRequest();
const onRequest = setupAgent();
interactive.sendInteractiveRequestToProvider('provider', { message: '@agent /hello friend' });
const request = await deferred.p;
assert.deepStrictEqual(request.command, 'hello');
assert.strictEqual(request.prompt, 'friend');
let i = 0;
onRequest(request => {
if (i === 0) {
assert.deepStrictEqual(request.request.command, 'hello');
assert.strictEqual(request.request.prompt, 'friend');
i++;
interactive.sendInteractiveRequestToProvider('provider', { message: '@agent /hello friend' });
} else {
assert.strictEqual(request.context.history.length, 1);
assert.strictEqual(request.context.history[0].agent.agent, 'agent');
assert.strictEqual(request.context.history[0].command, 'hello');
}
});
});
test('agent and variable', async () => {

View file

@ -239,7 +239,7 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
// RESPONSE turn
const parts = coalesce(h.response.map(r => typeConvert.ChatResponsePart.from(r, this.commands.converter)));
res.push(new extHostTypes.ChatAgentResponseTurn(parts, result, { extensionId: '', agent: h.request.agentId }));
res.push(new extHostTypes.ChatAgentResponseTurn(parts, result, { extensionId: '', agent: h.request.agentId }, h.request.command));
}
return res;

View file

@ -4274,7 +4274,8 @@ export class ChatAgentResponseTurn implements vscode.ChatAgentResponseTurn {
constructor(
readonly response: ReadonlyArray<ChatResponseTextPart | ChatResponseMarkdownPart | ChatResponseFileTreePart | ChatResponseAnchorPart | ChatResponseCommandButtonPart>,
readonly result: vscode.ChatAgentResult2,
readonly agent: { extensionId: string; agent: string }
readonly agent: { extensionId: string; agent: string },
readonly command?: string
) { }
}

View file

@ -54,6 +54,8 @@ declare module 'vscode' {
*/
readonly agent: { readonly extensionId: string; readonly agent: string };
readonly command?: string;
private constructor(response: ReadonlyArray<ChatResponseTextPart | ChatResponseMarkdownPart | ChatResponseFileTreePart | ChatResponseAnchorPart | ChatResponseCommandButtonPart>, result: ChatAgentResult2, agentId: { extensionId: string; agent: string });
}