runtime-plugin-discovery.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createSessionId,
  4. PLUGIN_DISCOVERY_INPUT,
  5. type ModelRequest,
  6. type PluginDiscoveryPort,
  7. } from "@zcode/contracts";
  8. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  9. import { createTestSessionEventStore } from "./test-event-store.js";
  10. import { createTestModelFactory } from "./test-runtime-model.js";
  11. describe("runtime plugin discovery wiring", () => {
  12. it.each(["build", "yolo"] as const)(
  13. "passes both tools through the runtime and waits in %s mode",
  14. async (mode) => {
  15. const requests: ModelRequest[] = [];
  16. let confirm!: () => void;
  17. const port: PluginDiscoveryPort = {
  18. search: vi.fn(async () => ({ plugins: [], stale: false })),
  19. suggest: vi.fn(async () => {
  20. await new Promise<void>((resolve) => {
  21. confirm = resolve;
  22. });
  23. return {
  24. pluginId: "mail@zcode-plugins-official",
  25. status: "skipped",
  26. message: "User skipped",
  27. };
  28. }),
  29. };
  30. const runtime = createTestAgentRuntime(
  31. createSessionId(),
  32. {
  33. mode,
  34. modelStreaming: "off",
  35. workingDirectory: process.cwd(),
  36. memory: { enabled: false },
  37. compact: { enabled: false },
  38. },
  39. {
  40. eventStore: createTestSessionEventStore(),
  41. pluginDiscoveryPort: port,
  42. modelFactory: createTestModelFactory({
  43. async generateText(request) {
  44. requests.push(request);
  45. if (requests.length < 3)
  46. return {
  47. finishReason: "tool-calls" as const,
  48. text: "",
  49. usage: {},
  50. toolCalls: [
  51. {
  52. id: `call-${requests.length}`,
  53. name: requests.length === 1 ? "SearchPlugins" : "SuggestPluginInstall",
  54. input:
  55. requests.length === 1
  56. ? { query: "mail" }
  57. : { pluginId: "mail@zcode-plugins-official", reason: "Read mail" },
  58. },
  59. ],
  60. };
  61. return { finishReason: "stop" as const, text: "Continue without plugins", usage: {} };
  62. },
  63. }),
  64. },
  65. );
  66. const run = runtime.executeTurn("Summarize my inbox", undefined, {
  67. intent: {
  68. [PLUGIN_DISCOVERY_INPUT]: true,
  69. sourceCommandId: "cmd",
  70. clientId: "app",
  71. kind: "sendText",
  72. requestedDelivery: "startNow",
  73. admittedDelivery: "startNow",
  74. queueItemId: "cmd",
  75. admissionSeq: 1,
  76. admittedAt: 0,
  77. },
  78. });
  79. await vi.waitFor(() => expect(port.suggest).toHaveBeenCalledOnce());
  80. expect(requests).toHaveLength(2);
  81. expect(port.search).toHaveBeenCalledWith(
  82. { query: "mail" },
  83. expect.objectContaining({
  84. sessionId: runtime.sessionId,
  85. signal: expect.any(AbortSignal),
  86. emitEvent: expect.any(Function),
  87. }),
  88. );
  89. confirm();
  90. await run;
  91. expect(requests).toHaveLength(3);
  92. await runtime.executeTurn("Ordinary input without an interactive attachment");
  93. const names = requests.at(-1)?.tools?.map((tool) => tool.name) ?? [];
  94. expect(names).not.toContain("SuggestPluginInstall");
  95. expect(names).not.toContain("SearchPlugins");
  96. expect(names).toContain("Read");
  97. expect(
  98. requests[2]?.messages.some(
  99. (message) => message.role === "tool" && message.toolName === "SuggestPluginInstall",
  100. ),
  101. ).toBe(true);
  102. },
  103. );
  104. });