tool-search-subagent.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { expect, it } from "vitest";
  2. import { createSessionId, type Model, type ModelRequest } from "@zcode/contracts";
  3. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  4. import { createTestModelFactory } from "./test-runtime-model.js";
  5. import { createTestSessionEventStore } from "./test-event-store.js";
  6. it.each([
  7. { force: false, childCodingPlan: false, searchAllowed: true, local: true, explore: true },
  8. { force: false, childCodingPlan: false, searchAllowed: true, local: true },
  9. { force: false, childCodingPlan: false, searchAllowed: false, local: true },
  10. { force: false, childCodingPlan: false, searchAllowed: true },
  11. { force: false, childCodingPlan: true, searchAllowed: true },
  12. { force: true, childCodingPlan: false, searchAllowed: true },
  13. { force: true, childCodingPlan: false, searchAllowed: false },
  14. ])(
  15. "uses the child Model, startup override, independent discovery and allowed scope: %j",
  16. async ({ force, childCodingPlan, searchAllowed, local = false, explore = false }) => {
  17. const childRequests: ModelRequest[] = [];
  18. const parentRequests: ModelRequest[] = [];
  19. const childEnabled = (local || force || childCodingPlan) && searchAllowed;
  20. const factory = createTestModelFactory({
  21. async generateText(request, observation) {
  22. const child = observation.invocationContext?.metadata?.querySource === "subagent";
  23. const requests = child ? childRequests : parentRequests;
  24. requests.push(request);
  25. if (!child && requests.length === 1)
  26. return {
  27. text: "",
  28. usage: {},
  29. finishReason: "tool-calls",
  30. toolCalls: [
  31. {
  32. id: "spawn",
  33. name: "Agent",
  34. input: {
  35. description: "Search scoped tools",
  36. prompt: "Discover WebFetch",
  37. subagent_type: explore ? "Explore" : "researcher",
  38. },
  39. },
  40. ],
  41. };
  42. if (child && childEnabled && requests.length === 1)
  43. return {
  44. text: "",
  45. usage: {},
  46. finishReason: "tool-calls",
  47. toolCalls: [
  48. {
  49. id: "child-search",
  50. name: local ? "LocalToolSearch" : "ToolSearch",
  51. input: { query: explore ? "select:WebFetch" : "select:WebFetch,TodoWrite" },
  52. },
  53. ],
  54. };
  55. return { text: "done", usage: {}, finishReason: "stop" };
  56. },
  57. });
  58. const withCapability = (model: Model): Model => ({
  59. ...model,
  60. requestCapabilities: {
  61. toolSearch: {
  62. protocolSupported: true,
  63. defaultEnabled: model.modelId === "child" ? childCodingPlan : true,
  64. },
  65. },
  66. bind: (options) => withCapability(model.bind(options)),
  67. });
  68. const runtime = createTestAgentRuntime(
  69. createSessionId("toolsearch-subagent"),
  70. {
  71. mode: "yolo",
  72. modelStreaming: "off",
  73. titleGeneration: { enabled: false },
  74. toolSearch: { mode: local ? "local" : force ? "native" : "auto" },
  75. modelSelection: { providerId: "test", modelId: "parent" },
  76. subagents: explore
  77. ? undefined
  78. : {
  79. profiles: [
  80. {
  81. name: "researcher",
  82. description: "Research",
  83. source: "user",
  84. systemPrompt: "Search tools",
  85. modelSelection: { providerId: "test", modelId: "child" },
  86. tools: searchAllowed
  87. ? ["ToolSearch", ...(local ? ["DeferredToolCall"] : []), "WebFetch"]
  88. : ["WebFetch"],
  89. },
  90. ],
  91. },
  92. },
  93. {
  94. eventStore: createTestSessionEventStore(),
  95. modelFactory: (input) => withCapability(factory(input)),
  96. },
  97. );
  98. try {
  99. await runtime.executeTurn("Run the researcher.");
  100. expect(childRequests.length).toBe(childEnabled ? 2 : 1);
  101. expect(childRequests[0]?.toolSearchEnabled).toBe(childEnabled && !local);
  102. expect(
  103. childRequests[0]?.tools?.some(
  104. (tool) => tool.name === (local ? "LocalToolSearch" : "ToolSearch"),
  105. ),
  106. ).toBe(childEnabled);
  107. if (childEnabled) {
  108. expect(childRequests[0]?.tools?.some((tool) => tool.name === "WebFetch")).toBe(false);
  109. if (local) {
  110. expect(childRequests[1]?.tools).toEqual(childRequests[0]?.tools);
  111. expect(JSON.stringify(childRequests[1]?.messages)).toContain("<functions>");
  112. expect(JSON.stringify(childRequests[1]?.messages)).not.toContain(
  113. '"toolName":"TodoWrite"',
  114. );
  115. } else {
  116. expect(
  117. childRequests[1]?.tools?.find((tool) => tool.name === "WebFetch")?.deferLoading,
  118. ).toBe(true);
  119. expect(
  120. childRequests[1]?.messages.find((message) => message.role === "tool")?.content,
  121. ).toEqual([{ type: "tool_reference", toolName: "WebFetch" }]);
  122. }
  123. } else expect(childRequests[0]?.tools?.some((tool) => tool.name === "WebFetch")).toBe(true);
  124. expect(parentRequests.at(-1)?.tools?.some((tool) => tool.name === "WebFetch")).toBe(false);
  125. } finally {
  126. await runtime.closeBrowserSession();
  127. }
  128. },
  129. );