2023-queued-slash-command-followup.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { AgentTool } from "@earendil-works/pi-agent-core";
  2. import { fauxAssistantMessage, fauxToolCall } from "@earendil-works/pi-ai";
  3. import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
  4. import { Type } from "typebox";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { createHarness, getAssistantTexts, getUserTexts, type Harness } from "../harness.ts";
  7. describe("issue #2023 queued slash-command follow-up", () => {
  8. const harnesses: Harness[] = [];
  9. afterEach(() => {
  10. while (harnesses.length > 0) {
  11. harnesses.pop()?.cleanup();
  12. }
  13. });
  14. it("treats extension-origin queued slash-command follow-ups as raw user text instead of dispatching the command", async () => {
  15. let extensionApi: ExtensionAPI | undefined;
  16. const commandRuns: string[] = [];
  17. let releaseToolExecution: (() => void) | undefined;
  18. const toolRelease = new Promise<void>((resolve) => {
  19. releaseToolExecution = resolve;
  20. });
  21. const waitTool: AgentTool = {
  22. name: "wait",
  23. label: "Wait",
  24. description: "Wait for the test to release execution",
  25. parameters: Type.Object({}),
  26. execute: async () => {
  27. await toolRelease;
  28. return {
  29. content: [{ type: "text", text: "released" }],
  30. details: {},
  31. };
  32. },
  33. };
  34. const harness = await createHarness({
  35. tools: [waitTool],
  36. extensionFactories: [
  37. (pi) => {
  38. extensionApi = pi;
  39. pi.registerCommand("testcmd", {
  40. description: "Test command",
  41. handler: async (args) => {
  42. commandRuns.push(args);
  43. },
  44. });
  45. },
  46. ],
  47. });
  48. harnesses.push(harness);
  49. harness.setResponses([
  50. fauxAssistantMessage(fauxToolCall("wait", {}), { stopReason: "toolUse" }),
  51. fauxAssistantMessage("first turn complete"),
  52. fauxAssistantMessage("queued follow-up handled by model"),
  53. ]);
  54. const sawToolStart = new Promise<void>((resolve) => {
  55. const unsubscribe = harness.session.subscribe((event) => {
  56. if (event.type === "tool_execution_start" && event.toolName === "wait") {
  57. unsubscribe();
  58. resolve();
  59. }
  60. });
  61. });
  62. const promptPromise = harness.session.prompt("start");
  63. await sawToolStart;
  64. await new Promise((resolve) => setTimeout(resolve, 0));
  65. extensionApi?.sendUserMessage("/testcmd queued", { deliverAs: "followUp" });
  66. releaseToolExecution?.();
  67. await promptPromise;
  68. expect(commandRuns).toEqual([]);
  69. expect(getUserTexts(harness)).toEqual(["start", "/testcmd queued"]);
  70. expect(getAssistantTexts(harness)).toContain("queued follow-up handled by model");
  71. });
  72. });