conversation-session-toolsearch-catalog.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import type { ServerResponse } from "node:http";
  2. import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
  3. import { startToolSearchRuntimeDriver } from "#e2e/helpers/toolsearch-runtime-driver";
  4. import {
  5. isToolSearchCompact,
  6. TOOLSEARCH_NOTICE,
  7. type ToolSearchRequest,
  8. } from "#e2e/helpers/toolsearch-provider";
  9. type Delta = { addedNames: string[]; removedNames: string[]; readdedNames: string[] };
  10. type Entry = { metadata?: { toolSearchAnnouncement?: Delta }; message?: unknown };
  11. const text = (body: ToolSearchRequest) =>
  12. body.messages
  13. .flatMap((message) =>
  14. typeof message.content === "string"
  15. ? [message.content]
  16. : message.content.flatMap((block) => (block.type === "text" ? [block.text ?? ""] : [])),
  17. )
  18. .join("\n");
  19. const deltas = (history: unknown) =>
  20. (history as Entry[]).flatMap((entry) =>
  21. entry.metadata?.toolSearchAnnouncement ? [entry.metadata.toolSearchAnnouncement] : [],
  22. );
  23. function reply(
  24. fixture: Awaited<ReturnType<typeof createToolSearchCliFixture>>,
  25. response: ServerResponse,
  26. body: ToolSearchRequest,
  27. stage = "catalog-done",
  28. ) {
  29. fixture.reply(body, response, `${stage}-${body.stream ? "stream" : "json"}`);
  30. }
  31. // 目录由隔离 driver 注入;HTTP adapter、Runtime、SQLite 和 compact 均是真实实现。
  32. // 与 CLI/TUI 产品 E2E 分开计数,不把此文件声称为 MCP 热更新用户路径。
  33. describe("ToolSearch 动态目录(Runtime / HTTP 集成)", () => {
  34. for (const streaming of ["on", "off"] as const) {
  35. for (const boundary of ["input", "tool batch"] as const) {
  36. it(`TS09: Continue 中目录变化等到 ${boundary} 才公告,streaming=${streaming}`, async function () {
  37. this.timeout(120_000);
  38. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  39. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  40. try {
  41. await driver.catalog("TodoRead", "absent");
  42. let mainCalls = 0;
  43. fixture.setResponse((body, response) => {
  44. void (async () => {
  45. mainCalls++;
  46. if (mainCalls === 1) {
  47. await driver.catalog("TodoRead", "deferred");
  48. reply(fixture, response, body, "catalog-partial");
  49. } else if (mainCalls === 2 && boundary === "tool batch") {
  50. reply(fixture, response, body, "catalog-search");
  51. } else reply(fixture, response, body);
  52. })().catch((error) => response.destroy(error as Error));
  53. });
  54. await driver.turn("E2E_TOOLSEARCH_CATALOG_CONTINUE");
  55. if (boundary === "input") await driver.turn("E2E_TOOLSEARCH_CATALOG_NEXT");
  56. expect(fixture.requests).toHaveLength(3);
  57. const second = fixture.requests[1]!.body;
  58. expect(text(second)).not.toContain("TodoRead");
  59. expect(text(second).split(TOOLSEARCH_NOTICE)).toHaveLength(2);
  60. const third = fixture.requests[2]!.body;
  61. expect(
  62. text(third)
  63. .split("\n")
  64. .filter((line) => line === "TodoRead"),
  65. ).toHaveLength(1);
  66. expect(deltas(await driver.history())).toEqual([
  67. { addedNames: ["TaskOutput"], removedNames: [], readdedNames: [] },
  68. { addedNames: ["TodoRead"], removedNames: [], readdedNames: [] },
  69. ]);
  70. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  71. expect(deltas(await driver.history())).toHaveLength(2);
  72. if (boundary === "tool batch")
  73. expect(third.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading).toBe(true);
  74. } finally {
  75. await driver.close();
  76. await fixture.close();
  77. }
  78. });
  79. }
  80. it(`TS04: 增量尾部连续 reactive compact 补全名称且不重复,streaming=${streaming}`, async function () {
  81. this.timeout(120_000);
  82. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  83. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  84. try {
  85. await driver.catalog("TodoRead", "absent");
  86. let mainCalls = 0;
  87. let summaries = 0;
  88. fixture.setResponse((body, response) => {
  89. if (isToolSearchCompact(body)) {
  90. summaries++;
  91. reply(fixture, response, body, "catalog-summary");
  92. } else if ([3, 5].includes(++mainCalls)) {
  93. fixture.reply(body, response, "context-error");
  94. } else reply(fixture, response, body);
  95. });
  96. await driver.turn("E2E_TOOLSEARCH_CATALOG_SEED_ONE");
  97. await driver.turn("E2E_TOOLSEARCH_CATALOG_SEED_TWO");
  98. await driver.catalog("TodoRead", "deferred");
  99. await driver.turn("E2E_TOOLSEARCH_CATALOG_EXPAND");
  100. expect(summaries).toBe(1);
  101. expect(deltas(await driver.history())).toEqual([
  102. { addedNames: ["TodoRead"], removedNames: [], readdedNames: [] },
  103. { addedNames: ["TaskOutput"], removedNames: [], readdedNames: [] },
  104. ]);
  105. for (let pass = 0; pass < 2; pass++) {
  106. const last = fixture.requests.at(-1)!.body;
  107. expect(text(last)).toContain("E2E_TOOLSEARCH_CATALOG_SUMMARY");
  108. for (const name of ["TaskOutput", "TodoRead"]) {
  109. expect(
  110. text(last)
  111. .split("\n")
  112. .filter((line) => line === name),
  113. ).toHaveLength(1);
  114. expect(last.tools?.some((tool) => tool.name === name)).toBe(false);
  115. }
  116. if (pass === 0) await driver.turn("E2E_TOOLSEARCH_CATALOG_COMPACT_AGAIN");
  117. }
  118. expect(summaries).toBe(2);
  119. const before = deltas(await driver.history());
  120. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  121. expect(deltas(await driver.history())).toEqual(before);
  122. } finally {
  123. await driver.close();
  124. await fixture.close();
  125. }
  126. });
  127. it(`TS06: resident 转换不误删,实际移除后恢复只公告一次,streaming=${streaming}`, async function () {
  128. this.timeout(120_000);
  129. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  130. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  131. try {
  132. fixture.setResponse((body, response) => reply(fixture, response, body));
  133. await driver.turn("E2E_TOOLSEARCH_CATALOG_INITIAL");
  134. await driver.catalog("TaskOutput", "resident");
  135. await driver.turn("E2E_TOOLSEARCH_CATALOG_RESIDENT");
  136. expect(
  137. fixture.requests.at(-1)!.body.tools?.find((tool) => tool.name === "TaskOutput"),
  138. ).toBeDefined();
  139. expect(
  140. fixture.requests.at(-1)!.body.tools?.find((tool) => tool.name === "TaskOutput")
  141. ?.defer_loading,
  142. ).toBeUndefined();
  143. expect(deltas(await driver.history())).toHaveLength(1);
  144. await driver.catalog("TaskOutput", "deferred");
  145. await driver.turn("E2E_TOOLSEARCH_CATALOG_DEFERRED");
  146. expect(
  147. fixture.requests.at(-1)!.body.tools?.some((tool) => tool.name === "TaskOutput"),
  148. ).toBe(false);
  149. expect(text(fixture.requests.at(-1)!.body)).not.toContain("no longer available");
  150. await driver.catalog("TaskOutput", "absent");
  151. await driver.turn("E2E_TOOLSEARCH_CATALOG_REMOVED");
  152. expect(deltas(await driver.history()).at(-1)?.removedNames).toEqual(["TaskOutput"]);
  153. expect(text(fixture.requests.at(-1)!.body)).toContain("no longer available");
  154. await driver.catalog("TaskOutput", "deferred");
  155. await driver.turn("E2E_TOOLSEARCH_CATALOG_RESTORED");
  156. expect(deltas(await driver.history()).at(-1)).toEqual({
  157. addedNames: ["TaskOutput"],
  158. removedNames: [],
  159. readdedNames: ["TaskOutput"],
  160. });
  161. expect(text(fixture.requests.at(-1)!.body)).toContain("available again");
  162. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  163. expect(deltas(await driver.history())).toHaveLength(3);
  164. expect(text(fixture.requests.at(-1)!.body).split("available again")).toHaveLength(2);
  165. } finally {
  166. await driver.close();
  167. await fixture.close();
  168. }
  169. });
  170. }
  171. });