Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions packages/chat/test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,67 @@ describe("workbench command dispatch", () => {
};
expect(body.command).toEqual({
type: "message",
text: "Unknown command: /nope",
text:
"Unknown command: /nope. No agent commands are available in this " +
"workbench yet.",
});

// Only the result reaches the timeline; the raw "/nope some args"
// never does.
expect(timelineTexts(await timelineOf(deps, workbench.id))).toEqual([
"Unknown command: /nope",
"Unknown command: /nope. No agent commands are available in this " +
"workbench yet.",
]);
});

test("a path-shaped message starting with '/' is posted normally, never swallowed as a command", async () => {
const registry = createCommandRegistry();
const deps = buildDeps({ commands: registry });
const app = mountAs(createChatRoutes(deps), "prn_alice");
const { body: workbench } = await createWorkbench(app, {
kind: "workbench",
});

const response = await sendText(app, workbench.id, "/usr/local/bin");
expect(response.status).toBe(201);
const body = (await response.json()) as Record<string, unknown>;
expect(body["command"]).toBeUndefined();
expect(timelineTexts(await timelineOf(deps, workbench.id))).toEqual([
"/usr/local/bin",
]);
});

test("/<agent> invokes that agent directly with the rest of the line as its message — CL-6499", async () => {
const platform = fakePlatform({
invitable: [{ id: "wfd_jimmy", name: "jimmy", description: "Jimmy" }],
});
const deps = buildWorkflowCommandDeps(platform);
const app = mountAs(createChatRoutes(deps), "prn_alice");
const { body: workbench } = await createWorkbench(app, {
kind: "workbench",
});

const response = await sendText(
app,
workbench.id,
"/jimmy throw me a gif for shipping code",
);
expect(response.status).toBe(201);
const body = (await response.json()) as {
command: { type: string; handle: string };
};
expect(body.command.type).toBe("workflow-started");
expect(body.command.handle).toBe("jimmy");
expect(platform.launchInviteCalls).toHaveLength(1);

const delivered = platform.sentMail.find(
(mail) => mail.workbenchId === "ins_invited1",
);
expect(delivered?.content.content).toContain(
"throw me a gif for shipping code",
);
});

test("a registered slash command runs its handler with the parsed args", async () => {
const registry = createCommandRegistry();
let seenArgs: string | undefined;
Expand Down
30 changes: 27 additions & 3 deletions packages/commands/src/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,30 @@ import type {
CommandResult,
} from "./registry";

function unknownCommandResult(name: string, prefix: "/" | "@"): CommandResult {
return { type: "message", text: `Unknown command: ${prefix}${name}` };
/**
* Names what IS available rather than answering a miss with a bare
* error: every command this tenant actually has right now (built-ins
* plus every invitable agent's own workflow command), so a mistyped
* `/jimmi` tells the sender what to try instead of leaving them to
* guess.
*/
async function unknownCommandResult(
registry: CommandRegistry,
name: string,
prefix: "/" | "@",
ctx: CommandContext,
): Promise<CommandResult> {
const available = await registry.listCommands(ctx.tenantId);
const suffix =
available.length === 0
? "No agent commands are available in this workbench yet."
: `Available: ${available
.map((command) => `${prefix}${command.name}`)
.join(", ")}.`;
return {
type: "message",
text: `Unknown command: ${prefix}${name}. ${suffix}`,
};
}

async function runParsed(
Expand All @@ -26,7 +48,9 @@ async function runParsed(
ctx: CommandContext,
): Promise<CommandResult> {
const command = await registry.getCommand(parsed.name, ctx.tenantId);
if (command === undefined) return unknownCommandResult(parsed.name, prefix);
if (command === undefined) {
return unknownCommandResult(registry, parsed.name, prefix, ctx);
}
return command.handler(parsed.args, ctx);
}

Expand Down
11 changes: 10 additions & 1 deletion packages/commands/src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export interface ParsedCommand {
readonly args: string;
}

// A command name is a bare word — letters, digits, underscore, hyphen —
// the same shape the composer's own `activeSlashQuery` already commits
// to before it ever opens the popover. Anything else (a path like
// "/usr/local/bin", a URL, plain punctuation) is not a command attempt
// at all, so `parseWithPrefix` returns `undefined` for it rather than
// naming it an "unknown command" — that misfire used to swallow an
// ordinary message and answer it with a command error instead.
const NAME_PATTERN = /^[\w-]+$/;

function parseWithPrefix(
text: string,
prefix: string,
Expand All @@ -19,7 +28,7 @@ function parseWithPrefix(
const rest = text.slice(prefix.length);
const spaceIndex = rest.indexOf(" ");
const name = spaceIndex === -1 ? rest : rest.slice(0, spaceIndex);
if (name === "") return undefined;
if (!NAME_PATTERN.test(name)) return undefined;
const args = spaceIndex === -1 ? "" : rest.slice(spaceIndex + 1).trim();
return { name, args };
}
Expand Down
36 changes: 34 additions & 2 deletions packages/commands/test/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,46 @@ describe("dispatchSlashCommand", () => {
expect(await dispatchSlashCommand(registry, "hello", CTX)).toBeUndefined();
});

test("unknown command dispatches a loud message", async () => {
test("unknown command with no commands registered names none as available", async () => {
const registry = createCommandRegistry();
expect(await dispatchSlashCommand(registry, "/nope", CTX)).toEqual({
type: "message",
text: "Unknown command: /nope",
text:
"Unknown command: /nope. No agent commands are available in this " +
"workbench yet.",
});
});

test("unknown command names the commands that ARE available", async () => {
const registry = createCommandRegistry();
registry.registerCommand({
name: "jimmy",
description: "Starts Jimmy",
handler: () => ({ type: "noop" }),
});
registry.registerCommand({
name: "scout",
description: "Starts Scout",
handler: () => ({ type: "noop" }),
});
expect(await dispatchSlashCommand(registry, "/nope", CTX)).toEqual({
type: "message",
text: "Unknown command: /nope. Available: /jimmy, /scout.",
});
});

test("a path-shaped message (not a command) is never swallowed as one", async () => {
const registry = createCommandRegistry();
registry.registerCommand({
name: "jimmy",
description: "Starts Jimmy",
handler: () => ({ type: "noop" }),
});
expect(
await dispatchSlashCommand(registry, "/usr/local/bin", CTX),
).toBeUndefined();
});

test("runs the resolved command's handler with the parsed args and context", async () => {
const registry = createCommandRegistry();
let seenArgs: string | undefined;
Expand Down
12 changes: 12 additions & 0 deletions packages/commands/test/grammar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe("parseSlashCommand", () => {
expect(parseSlashCommand("/")).toBeUndefined();
expect(parseSlashCommand("/ hello")).toBeUndefined();
});

test("undefined for a path-shaped leading slash — never swallows a plain message that happens to start with '/'", () => {
expect(parseSlashCommand("/usr/local/bin")).toBeUndefined();
expect(parseSlashCommand("/usr/local/bin is on my PATH")).toBeUndefined();
});

test("still parses a hyphenated command name", () => {
expect(parseSlashCommand("/code-reviewer take a look")).toEqual({
name: "code-reviewer",
args: "take a look",
});
});
});

describe("parseAtCommand", () => {
Expand Down
42 changes: 41 additions & 1 deletion packages/commands/test/workflow-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,47 @@ describe("createWorkflowCommandPlugin", () => {
const result = await dispatchSlashCommand(registry, "/nonexistent", CTX);
expect(result).toEqual({
type: "message",
text: "Unknown command: /nonexistent",
text:
"Unknown command: /nonexistent. No agent commands are available " +
"in this workbench yet.",
});
});

test("any agent present in the room gets a command automatically — /jimmy routes to the definition named jimmy", async () => {
const registry = createCommandRegistry();
const startCalls: unknown[] = [];
registry.registerCommandPlugin(
createWorkflowCommandPlugin({
listInvitableDefinitions: async () => [
{ id: "def-jimmy", name: "jimmy" },
],
startWorkflow: async (input) => {
startCalls.push(input);
return { handle: "jimmy", address: "ins_jimmy@tenant.test" };
},
}),
);

const result = await dispatchSlashCommand(
registry,
"/jimmy throw me a gif for shipping code",
CTX,
);

expect(startCalls).toEqual([
{
tenantId: CTX.tenantId,
principalId: CTX.principalId,
workbenchId: CTX.workbenchId,
definitionId: "def-jimmy",
args: "throw me a gif for shipping code",
},
]);
expect(result).toEqual({
type: "workflow-started",
definitionId: "def-jimmy",
address: "ins_jimmy@tenant.test",
handle: "jimmy",
});
});
});
Loading