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

Don't show suggestions for agent subcommand in an invalid spot after other text #195752

Merged
merged 1 commit into from
Oct 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ class AgentCompletions extends Disposable {
}

const parsedRequest = (await this.instantiationService.createInstance(ChatRequestParser).parseChatRequest(widget.viewModel.sessionId, model.getValue())).parts;
const usedAgent = parsedRequest.find((p): p is ChatRequestAgentPart => p instanceof ChatRequestAgentPart);
if (!usedAgent) {
const usedAgentIdx = parsedRequest.findIndex((p): p is ChatRequestAgentPart => p instanceof ChatRequestAgentPart);
if (usedAgentIdx < 0) {
return;
}

Expand All @@ -385,6 +385,14 @@ class AgentCompletions extends Disposable {
return;
}

for (const partAfterAgent of parsedRequest.slice(usedAgentIdx + 1)) {
if (!(partAfterAgent instanceof ChatRequestTextPart) || !partAfterAgent.text.match(/^\s+(\/\w*)?$/)) {
// No text allowed between agent and subcommand
return;
}
}

const usedAgent = parsedRequest[usedAgentIdx] as ChatRequestAgentPart;
const commands = await usedAgent.agent.provideSlashCommands(token);

return <CompletionList>{
Expand Down
79 changes: 40 additions & 39 deletions src/vs/workbench/contrib/chat/common/chatRequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';
import { OffsetRange } from 'vs/editor/common/core/offsetRange';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { IChatAgent, IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents';
import { IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents';
import { ChatRequestAgentPart, ChatRequestAgentSubcommandPart, ChatRequestDynamicReferencePart, ChatRequestSlashCommandPart, ChatRequestTextPart, ChatRequestVariablePart, IParsedChatRequest, IParsedChatRequestPart, chatVariableLeader } from 'vs/workbench/contrib/chat/common/chatParserTypes';
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
import { IChatVariablesService } from 'vs/workbench/contrib/chat/common/chatVariables';
Expand All @@ -33,14 +33,14 @@ export class ChatRequestParser {
const previousChar = message.charAt(i - 1);
const char = message.charAt(i);
let newPart: IParsedChatRequestPart | undefined;
if (previousChar === ' ' || i === 0) {
if (previousChar.match(/\s/) || i === 0) {
if (char === chatVariableLeader) {
newPart = this.tryToParseVariable(message.slice(i), i, new Position(lineNumber, column), parts);
} else if (char === '@') {
newPart = this.tryToParseAgent(message.slice(i), i, new Position(lineNumber, column), parts);
newPart = this.tryToParseAgent(message.slice(i), message, i, new Position(lineNumber, column), parts);
} else if (char === '/') {
// TODO try to make this sync
newPart = await this.tryToParseSlashCommand(sessionId, message.slice(i), i, new Position(lineNumber, column), parts);
newPart = await this.tryToParseSlashCommand(sessionId, message.slice(i), message, i, new Position(lineNumber, column), parts);
} else if (char === '$') {
newPart = await this.tryToParseDynamicVariable(sessionId, message.slice(i), i, new Position(lineNumber, column), parts);
}
Expand Down Expand Up @@ -79,36 +79,13 @@ export class ChatRequestParser {
message.slice(lastPartEnd, message.length)));
}


// fix up parts:
// * only one agent at the beginning of the message
// * only one agent command after the agent or at the beginning of the message
let agentIndex = -1;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part instanceof ChatRequestAgentPart) {
if (i === 0) {
agentIndex = 0;
} else {
// agent not first -> make text part
parts[i] = new ChatRequestTextPart(part.range, part.editorRange, part.text);
}
}
if (part instanceof ChatRequestAgentSubcommandPart) {
if (!(i === 0 || agentIndex === 0 && i === 2 && /^\s+$/.test(parts[1].text))) {
// agent command not after agent nor first -> make text part
parts[i] = new ChatRequestTextPart(part.range, part.editorRange, part.text);
}
}
}

return {
parts,
text: message,
};
}

private tryToParseAgent(message: string, offset: number, position: IPosition, parts: ReadonlyArray<IParsedChatRequestPart>): ChatRequestAgentPart | ChatRequestVariablePart | undefined {
private tryToParseAgent(message: string, fullMessage: string, offset: number, position: IPosition, parts: ReadonlyArray<IParsedChatRequestPart>): ChatRequestAgentPart | ChatRequestVariablePart | undefined {
const nextVariableMatch = message.match(agentReg);
if (!nextVariableMatch) {
return;
Expand All @@ -118,17 +95,29 @@ export class ChatRequestParser {
const varRange = new OffsetRange(offset, offset + full.length);
const varEditorRange = new Range(position.lineNumber, position.column, position.lineNumber, position.column + full.length);

let agent: IChatAgent | undefined;
if ((agent = this.agentService.getAgent(name))) {
if (parts.some(p => p instanceof ChatRequestAgentPart)) {
// Only one agent allowed
return;
} else {
return new ChatRequestAgentPart(varRange, varEditorRange, agent);
}
const agent = this.agentService.getAgent(name);
if (!agent) {
return;
}

return;
if (parts.some(p => p instanceof ChatRequestAgentPart)) {
// Only one agent allowed
return;
}

// The agent must come first
if (parts.some(p => (p instanceof ChatRequestTextPart && p.text.trim() !== '') || !(p instanceof ChatRequestAgentPart))) {
return;
}

const previousPart = parts.at(-1);
const previousPartEnd = previousPart?.range.endExclusive ?? 0;
const textSincePreviousPart = fullMessage.slice(previousPartEnd, offset);
if (textSincePreviousPart.trim() !== '') {
return;
}

return new ChatRequestAgentPart(varRange, varEditorRange, agent);
}

private tryToParseVariable(message: string, offset: number, position: IPosition, parts: ReadonlyArray<IParsedChatRequestPart>): ChatRequestAgentPart | ChatRequestVariablePart | undefined {
Expand All @@ -149,8 +138,8 @@ export class ChatRequestParser {
return;
}

private async tryToParseSlashCommand(sessionId: string, message: string, offset: number, position: IPosition, parts: ReadonlyArray<IParsedChatRequestPart>): Promise<ChatRequestSlashCommandPart | ChatRequestAgentSubcommandPart | undefined> {
const nextSlashMatch = message.match(slashReg);
private async tryToParseSlashCommand(sessionId: string, remainingMessage: string, fullMessage: string, offset: number, position: IPosition, parts: ReadonlyArray<IParsedChatRequestPart>): Promise<ChatRequestSlashCommandPart | ChatRequestAgentSubcommandPart | undefined> {
const nextSlashMatch = remainingMessage.match(slashReg);
if (!nextSlashMatch) {
return;
}
Expand All @@ -166,6 +155,18 @@ export class ChatRequestParser {

const usedAgent = parts.find((p): p is ChatRequestAgentPart => p instanceof ChatRequestAgentPart);
if (usedAgent) {
// The slash command must come immediately after the agent
if (parts.some(p => (p instanceof ChatRequestTextPart && p.text.trim() !== '') || !(p instanceof ChatRequestAgentPart) && !(p instanceof ChatRequestTextPart))) {
return;
}

const previousPart = parts.at(-1);
const previousPartEnd = previousPart?.range.endExclusive ?? 0;
const textSincePreviousPart = fullMessage.slice(previousPartEnd, offset);
if (textSincePreviousPart.trim() !== '') {
return;
}

const subCommands = await usedAgent.agent.provideSlashCommands(CancellationToken.None);
const subCommand = subCommands.find(c => c.name === command);
if (subCommand) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
parts: [
{
range: {
start: 0,
endExclusive: 5
},
editorRange: {
startLineNumber: 1,
startColumn: 1,
endLineNumber: 2,
endColumn: 1
},
text: " \n",
kind: "text"
},
{
range: {
start: 5,
endExclusive: 11
},
editorRange: {
startLineNumber: 2,
startColumn: 1,
endLineNumber: 2,
endColumn: 7
},
agent: {
id: "agent",
metadata: { description: "" },
provideSlashCommands: [Function provideSlashCommands]
},
kind: "agent"
},
{
range: {
start: 11,
endExclusive: 12
},
editorRange: {
startLineNumber: 2,
startColumn: 7,
endLineNumber: 3,
endColumn: 1
},
text: "\n",
kind: "text"
},
{
range: {
start: 12,
endExclusive: 23
},
editorRange: {
startLineNumber: 3,
startColumn: 1,
endLineNumber: 3,
endColumn: 12
},
command: {
name: "subCommand",
description: ""
},
kind: "subcommand"
},
{
range: {
start: 23,
endExclusive: 30
},
editorRange: {
startLineNumber: 3,
startColumn: 12,
endLineNumber: 3,
endColumn: 19
},
text: " Thanks",
kind: "text"
}
],
text: " \n@agent\n/subCommand Thanks"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
parts: [
{
range: {
start: 0,
endExclusive: 10
},
editorRange: {
startLineNumber: 1,
startColumn: 1,
endLineNumber: 2,
endColumn: 5
},
text: " \r\n\t ",
kind: "text"
},
{
range: {
start: 10,
endExclusive: 16
},
editorRange: {
startLineNumber: 2,
startColumn: 5,
endLineNumber: 2,
endColumn: 11
},
agent: {
id: "agent",
metadata: { description: "" },
provideSlashCommands: [Function provideSlashCommands]
},
kind: "agent"
},
{
range: {
start: 16,
endExclusive: 23
},
editorRange: {
startLineNumber: 2,
startColumn: 11,
endLineNumber: 3,
endColumn: 5
},
text: " \r\n\t ",
kind: "text"
},
{
range: {
start: 23,
endExclusive: 34
},
editorRange: {
startLineNumber: 3,
startColumn: 5,
endLineNumber: 3,
endColumn: 16
},
command: {
name: "subCommand",
description: ""
},
kind: "subcommand"
},
{
range: {
start: 34,
endExclusive: 41
},
editorRange: {
startLineNumber: 3,
startColumn: 16,
endLineNumber: 3,
endColumn: 23
},
text: " Thanks",
kind: "text"
}
],
text: " \r\n\t @agent \r\n\t /subCommand Thanks"
}