Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 29 additions & 10 deletions extensions/vscode-api-tests/src/singlefolder-tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostChatAgents2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) { }
}

Expand Down
2 changes: 2 additions & 0 deletions src/vscode-dts/vscode.proposed.chatAgents2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

Expand Down