tool-paths.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { describe, expect, test } from "bun:test"
  2. import { Effect, Schema } from "effect"
  3. import { CodeMode, Tool } from "../src/index.js"
  4. const echo = (description: string, result: string) =>
  5. Tool.make({
  6. description,
  7. input: Schema.Struct({}),
  8. output: Schema.String,
  9. run: () => Effect.succeed(result),
  10. })
  11. const value = async (runtime: CodeMode.Runtime, code: string) => {
  12. const result = await Effect.runPromise(runtime.execute(code))
  13. if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
  14. return result.value
  15. }
  16. const failure = async (runtime: CodeMode.Runtime, code: string) => {
  17. const result = await Effect.runPromise(runtime.execute(code))
  18. if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
  19. return result.error
  20. }
  21. describe("dotted tool names", () => {
  22. const runtime = CodeMode.make({ tools: { api: { "issues.list": echo("List issues", "listed") } } })
  23. test("a dotted name becomes nested namespaces in the catalog", () => {
  24. const catalog = runtime.catalog()
  25. expect(catalog).toHaveLength(1)
  26. expect(catalog[0]?.path).toBe("api.issues.list")
  27. expect(catalog[0]?.signature).toStartWith("tools.api.issues.list(input:")
  28. expect(runtime.instructions()).toContain("tools.api.issues.list(input:")
  29. })
  30. test("the advertised dotted path is executable", async () => {
  31. expect(await value(runtime, `return await tools.api.issues.list({})`)).toBe("listed")
  32. })
  33. test("bracket access with a dotted segment spells the same canonical path", async () => {
  34. expect(await value(runtime, `return await tools.api["issues.list"]({})`)).toBe("listed")
  35. expect(await value(runtime, `return await tools["api.issues"].list({})`)).toBe("listed")
  36. })
  37. test("intermediate segments enumerate like ordinary namespaces", async () => {
  38. expect(await value(runtime, `return [Object.keys(tools.api), Object.keys(tools.api.issues)]`)).toEqual([
  39. ["issues"],
  40. ["list"],
  41. ])
  42. expect(await value(runtime, `return Object.keys(tools["api.issues"])`)).toEqual(["list"])
  43. })
  44. test("a top-level dotted name nests from the root", async () => {
  45. const flat = CodeMode.make({ tools: { "issues.list": echo("List issues", "flat") } })
  46. expect(flat.catalog()[0]?.path).toBe("issues.list")
  47. expect(await value(flat, `return await tools.issues.list({})`)).toBe("flat")
  48. })
  49. })
  50. describe("callable namespaces", () => {
  51. const runtime = CodeMode.make({
  52. tools: { issues: echo("All issues", "all"), "issues.list": echo("List issues", "list") },
  53. })
  54. test("a path can hold a tool and child tools at once", async () => {
  55. expect(await value(runtime, `return await tools.issues({})`)).toBe("all")
  56. expect(await value(runtime, `return await tools.issues.list({})`)).toBe("list")
  57. expect(runtime.catalog().map((tool) => tool.path)).toEqual(["issues", "issues.list"])
  58. })
  59. test("a callable namespace enumerates its children", async () => {
  60. expect(await value(runtime, `return Object.keys(tools.issues)`)).toEqual(["list"])
  61. })
  62. test("search returns executable paths for both", async () => {
  63. const result = await value(runtime, `return search({ query: "", namespace: "issues" })`)
  64. expect((result as { items: Array<{ path: string }> }).items.map((item) => item.path)).toEqual([
  65. "tools.issues",
  66. "tools.issues.list",
  67. ])
  68. const exact = await value(runtime, `return search({ query: "tools.issues.list" })`)
  69. expect((exact as { items: Array<{ path: string }> }).items.map((item) => item.path)).toEqual(["tools.issues.list"])
  70. })
  71. test("an unknown child under a callable tool is an UnknownTool error", async () => {
  72. const diagnostic = await failure(runtime, `return await tools.issues.missing({})`)
  73. expect(diagnostic.kind).toBe("UnknownTool")
  74. expect(diagnostic.message).toContain("Unknown tool 'issues.missing'")
  75. })
  76. test("a namespace without its own definition stays non-callable", async () => {
  77. const nested = CodeMode.make({ tools: { "issues.list": echo("List issues", "list") } })
  78. const diagnostic = await failure(nested, `return await tools.issues({})`)
  79. expect(diagnostic.kind).toBe("UnknownTool")
  80. expect(diagnostic.message).toContain("Tool 'issues' is not callable")
  81. })
  82. })
  83. describe("blocked member names on tool paths", () => {
  84. const runtime = CodeMode.make({
  85. tools: {
  86. prototype: echo("Prototype tool", "proto"),
  87. "issues.constructor": echo("Constructor tool", "ctor"),
  88. nested: { ["__proto__"]: echo("Proto tool", "dunder") },
  89. },
  90. })
  91. test("tools may use blocked member names because path segments never touch real properties", async () => {
  92. expect(runtime.catalog().map((tool) => tool.path)).toEqual(["prototype", "issues.constructor", "nested.__proto__"])
  93. expect(await value(runtime, `return await tools.prototype({})`)).toBe("proto")
  94. expect(await value(runtime, `return await tools.issues.constructor({})`)).toBe("ctor")
  95. expect(await value(runtime, `return await tools["issues.constructor"]({})`)).toBe("ctor")
  96. expect(await value(runtime, `return await tools.nested.__proto__({})`)).toBe("dunder")
  97. expect(await value(runtime, `return Object.keys(tools.issues)`)).toEqual(["constructor"])
  98. })
  99. test("a literal __proto__ key cannot poison a namespace into a fake definition", async () => {
  100. const poisoned = CodeMode.make({
  101. tools: { ns: { "__proto__": echo("Hidden", "hidden"), real: echo("Real tool", "real") } },
  102. })
  103. expect(poisoned.catalog().map((tool) => tool.path)).toEqual(["ns.real"])
  104. expect(await value(poisoned, `return await tools.ns.real({})`)).toBe("real")
  105. })
  106. test("blocked member access on data values stays blocked", async () => {
  107. const diagnostic = await failure(runtime, `const x = {}; return x.constructor`)
  108. expect(diagnostic.message).toContain("constructor")
  109. expect(Object.keys(Object.prototype)).toEqual([])
  110. })
  111. })
  112. describe("empty segments", () => {
  113. test("tool names with empty segments are rejected at make", () => {
  114. for (const name of ["", "a..b", "trail.", ".lead"]) {
  115. expect(() => CodeMode.make({ tools: { [name]: echo("Bad", "bad") } })).toThrow("empty segment")
  116. }
  117. })
  118. })
  119. describe("canonical path collisions", () => {
  120. test("the last definition supplied for a canonical path wins", async () => {
  121. const runtime = CodeMode.make({
  122. tools: { "issues.list": echo("First", "first"), issues: { list: echo("Second", "second") } },
  123. })
  124. expect(await value(runtime, `return await tools.issues.list({})`)).toBe("second")
  125. expect(runtime.catalog()).toHaveLength(1)
  126. expect(runtime.catalog()[0]?.description).toBe("Second")
  127. })
  128. test("overriding one path keeps sibling tools from both shapes", async () => {
  129. const runtime = CodeMode.make({
  130. tools: {
  131. "issues.list": echo("First list", "first"),
  132. issues: { list: echo("Second list", "second"), get: echo("Get issue", "got") },
  133. "issues.close": echo("Close issue", "closed"),
  134. },
  135. })
  136. // Catalog order follows first appearance of each canonical path.
  137. expect(runtime.catalog().map((tool) => tool.path)).toEqual(["issues.list", "issues.get", "issues.close"])
  138. expect(await value(runtime, `return await tools.issues.list({})`)).toBe("second")
  139. expect(await value(runtime, `return await tools.issues.get({})`)).toBe("got")
  140. expect(await value(runtime, `return await tools.issues.close({})`)).toBe("closed")
  141. })
  142. })