5109-exclude-tools.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Type } from "typebox";
  2. import { describe, expect, it } from "vitest";
  3. import type { ExtensionFactory } from "../../../src/index.ts";
  4. import { createHarness } from "../harness.ts";
  5. function toolNames(tools: Array<{ name: string }>): string[] {
  6. return tools.map((tool) => tool.name).sort();
  7. }
  8. describe("regression #5109: exclude tools", () => {
  9. const extensionFactories: ExtensionFactory[] = [
  10. (pi) => {
  11. pi.on("session_start", () => {
  12. pi.registerTool({
  13. name: "ask_question",
  14. label: "Ask Question",
  15. description: "Ask a question",
  16. promptSnippet: "Ask a question",
  17. parameters: Type.Object({}),
  18. execute: async () => ({
  19. content: [{ type: "text", text: "ok" }],
  20. details: {},
  21. }),
  22. });
  23. pi.registerTool({
  24. name: "dynamic_tool",
  25. label: "Dynamic Tool",
  26. description: "Dynamic test tool",
  27. promptSnippet: "Run dynamic test behavior",
  28. parameters: Type.Object({}),
  29. execute: async () => ({
  30. content: [{ type: "text", text: "ok" }],
  31. details: {},
  32. }),
  33. });
  34. });
  35. },
  36. ];
  37. it("filters built-in and extension tools from available and active tools", async () => {
  38. const harness = await createHarness({
  39. excludedToolNames: ["read", "ask_question"],
  40. extensionFactories,
  41. });
  42. try {
  43. await harness.session.bindExtensions({});
  44. const allToolNames = toolNames(harness.session.getAllTools());
  45. expect(allToolNames).not.toContain("read");
  46. expect(allToolNames).not.toContain("ask_question");
  47. expect(allToolNames).toContain("bash");
  48. expect(allToolNames).toContain("dynamic_tool");
  49. expect(harness.session.getActiveToolNames().sort()).toEqual(["bash", "dynamic_tool", "edit", "write"]);
  50. expect(harness.session.systemPrompt).not.toContain("- read:");
  51. expect(harness.session.systemPrompt).not.toContain("ask_question");
  52. expect(harness.session.systemPrompt).toContain("- dynamic_tool: Run dynamic test behavior");
  53. } finally {
  54. harness.cleanup();
  55. }
  56. });
  57. it("lets excluded tools override the allowlist", async () => {
  58. const harness = await createHarness({
  59. allowedToolNames: ["read", "bash", "ask_question"],
  60. excludedToolNames: ["read", "ask_question"],
  61. initialActiveToolNames: ["read", "bash", "ask_question"],
  62. extensionFactories,
  63. });
  64. try {
  65. await harness.session.bindExtensions({});
  66. expect(toolNames(harness.session.getAllTools())).toEqual(["bash"]);
  67. expect(harness.session.getActiveToolNames()).toEqual(["bash"]);
  68. expect(harness.session.systemPrompt).toContain("- bash:");
  69. expect(harness.session.systemPrompt).not.toContain("- read:");
  70. expect(harness.session.systemPrompt).not.toContain("ask_question");
  71. } finally {
  72. harness.cleanup();
  73. }
  74. });
  75. });