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

Change chat variable syntax to match remote agent proposal #191427

Merged
merged 1 commit into from
Aug 28, 2023
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
4 changes: 3 additions & 1 deletion src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ export class ChatService extends Disposable implements IChatService {
};

if (typeof request.message === 'string') {
request.variables = await this.chatVariablesService.resolveVariables(request.message, model, token);
const varResult = await this.chatVariablesService.resolveVariables(request.message, model, token);
request.variables = varResult.variables;
request.message = varResult.prompt;
}

rawResponse = await provider.provideReply(request, progressCallback, token);
Expand Down
36 changes: 28 additions & 8 deletions src/vs/workbench/contrib/chat/common/chatVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ export interface IChatVariablesService {
/**
* Resolves all variables that occur in `prompt`
*/
resolveVariables(prompt: string, model: IChatModel, token: CancellationToken): Promise<Record<string, IChatRequestVariableValue[]>>;
resolveVariables(prompt: string, model: IChatModel, token: CancellationToken): Promise<IChatVariableResolveResult>;
}

interface IChatData {
data: IChatVariableData;
resolver: IChatVariableResolver;
}

interface IChatVariableResolveResult {
variables: Record<string, IChatRequestVariableValue[]>;
prompt: string;
}

export class ChatVariablesService implements IChatVariablesService {
declare _serviceBrand: undefined;

Expand All @@ -54,32 +59,47 @@ export class ChatVariablesService implements IChatVariablesService {
constructor() {
}

async resolveVariables(prompt: string, model: IChatModel, token: CancellationToken): Promise<Record<string, IChatRequestVariableValue[]>> {
async resolveVariables(prompt: string, model: IChatModel, token: CancellationToken): Promise<IChatVariableResolveResult> {
const resolvedVariables: Record<string, IChatRequestVariableValue[]> = {};
const jobs: Promise<any>[] = [];

const regex = /(^|\s)@(\w+)(:\w+)?(\s|$)/ig;
// TODO have a separate parser that is also used for decorations
const regex = /(^|\s)@(\w+)(:\w+)?(?=\s|$|\b)/ig;

let lastMatch = 0;
const parsedPrompt: string[] = [];
let match: RegExpMatchArray | null;
while (match = regex.exec(prompt)) {
const candidate = match[2];
const data = this._resolver.get(candidate.toLowerCase());
const [fullMatch, leading, varName, arg] = match;
const data = this._resolver.get(varName.toLowerCase());
if (data) {
const arg = match[3];
if (!arg || data.data.canTakeArgument) {
parsedPrompt.push(prompt.substring(lastMatch, match.index!));
parsedPrompt.push('');
lastMatch = match.index! + fullMatch.length;
const varIndex = parsedPrompt.length - 1;
const argWithoutColon = arg?.slice(1);
const fullVarName = varName + (arg ?? '');
jobs.push(data.resolver(prompt, argWithoutColon, model, token).then(value => {
if (value) {
resolvedVariables[candidate + (arg ?? '')] = value;
resolvedVariables[fullVarName] = value;
parsedPrompt[varIndex] = `${leading}[@${fullVarName}](values:${fullVarName})`;
} else {
parsedPrompt[varIndex] = fullMatch;
}
}).catch(onUnexpectedExternalError));
}
}
}

parsedPrompt.push(prompt.substring(lastMatch));

await Promise.allSettled(jobs);

return Promise.resolve(resolvedVariables);
return {
variables: resolvedVariables,
prompt: parsedPrompt.join('')
};
}

getVariables(): Iterable<Readonly<IChatVariableData>> {
Expand Down
33 changes: 21 additions & 12 deletions src/vs/workbench/contrib/chat/test/common/chatVariables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,42 @@ suite('ChatVariables', function () {

{
const data = await service.resolveVariables('Hello @foo and@far', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 1);
assert.deepEqual(Object.keys(data).sort(), ['foo']);
assert.strictEqual(Object.keys(data.variables).length, 1);
assert.deepEqual(Object.keys(data.variables).sort(), ['foo']);
assert.strictEqual(data.prompt, 'Hello [@foo](values:foo) and@far');
}
{
const data = await service.resolveVariables('@foo Hello', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 1);
assert.deepEqual(Object.keys(data).sort(), ['foo']);
assert.strictEqual(Object.keys(data.variables).length, 1);
assert.deepEqual(Object.keys(data.variables).sort(), ['foo']);
assert.strictEqual(data.prompt, '[@foo](values:foo) Hello');
}
{
const data = await service.resolveVariables('Hello @foo', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 1);
assert.deepEqual(Object.keys(data).sort(), ['foo']);
assert.strictEqual(Object.keys(data.variables).length, 1);
assert.deepEqual(Object.keys(data.variables).sort(), ['foo']);
}
{
const data = await service.resolveVariables('Hello @foo?', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data.variables).length, 1);
assert.deepEqual(Object.keys(data.variables).sort(), ['foo']);
assert.strictEqual(data.prompt, 'Hello [@foo](values:foo)?');
}
{
const data = await service.resolveVariables('Hello @foo and@far @foo', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 1);
assert.deepEqual(Object.keys(data).sort(), ['foo']);
assert.strictEqual(Object.keys(data.variables).length, 1);
assert.deepEqual(Object.keys(data.variables).sort(), ['foo']);
}
{
const data = await service.resolveVariables('Hello @foo and @far @foo', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 2);
assert.deepEqual(Object.keys(data).sort(), ['far', 'foo']);
assert.strictEqual(Object.keys(data.variables).length, 2);
assert.deepEqual(Object.keys(data.variables).sort(), ['far', 'foo']);
}
{
const data = await service.resolveVariables('Hello @foo and @far @foo @unknown', null!, CancellationToken.None);
assert.strictEqual(Object.keys(data).length, 2);
assert.deepEqual(Object.keys(data).sort(), ['far', 'foo']);
assert.strictEqual(Object.keys(data.variables).length, 2);
assert.deepEqual(Object.keys(data.variables).sort(), ['far', 'foo']);
assert.strictEqual(data.prompt, 'Hello [@foo](values:foo) and [@far](values:far) [@foo](values:foo) @unknown');
}
});
});