|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import { createServer } from "node:http"; |
| 3 | + |
| 4 | +import { expect } from "@effect/vitest"; |
| 5 | +import { Effect } from "effect"; |
| 6 | +import { composePluginApi } from "@executor-js/api/server"; |
| 7 | +import { openApiHttpPlugin } from "@executor-js/plugin-openapi/api"; |
| 8 | +import { AuthTemplateSlug, ConnectionName, IntegrationSlug } from "@executor-js/sdk/shared"; |
| 9 | + |
| 10 | +import { scenario } from "../src/scenario"; |
| 11 | +import { Api, Target } from "../src/services"; |
| 12 | + |
| 13 | +const api = composePluginApi([openApiHttpPlugin()] as const); |
| 14 | + |
| 15 | +const serveRecordingUpstream = () => |
| 16 | + Effect.acquireRelease( |
| 17 | + Effect.callback<{ |
| 18 | + readonly url: string; |
| 19 | + readonly requests: () => readonly string[]; |
| 20 | + readonly close: () => void; |
| 21 | + }>((resume) => { |
| 22 | + const recorded: string[] = []; |
| 23 | + const server = createServer((request, response) => { |
| 24 | + recorded.push(request.url ?? ""); |
| 25 | + response.writeHead(200, { "content-type": "application/json" }); |
| 26 | + response.end(JSON.stringify({ id: "2", name: "Gadget" })); |
| 27 | + }); |
| 28 | + server.listen(0, "127.0.0.1", () => { |
| 29 | + const address = server.address(); |
| 30 | + const port = typeof address === "object" && address ? address.port : 0; |
| 31 | + resume( |
| 32 | + Effect.succeed({ |
| 33 | + url: `http://127.0.0.1:${port}`, |
| 34 | + requests: () => [...recorded], |
| 35 | + close: () => { |
| 36 | + server.close(); |
| 37 | + server.closeAllConnections(); |
| 38 | + }, |
| 39 | + }), |
| 40 | + ); |
| 41 | + }); |
| 42 | + }), |
| 43 | + (server) => Effect.sync(server.close), |
| 44 | + ); |
| 45 | + |
| 46 | +const itemsSpec = (baseUrl: string): string => |
| 47 | + JSON.stringify({ |
| 48 | + openapi: "3.0.3", |
| 49 | + info: { title: "Items API", version: "1.0.0" }, |
| 50 | + servers: [{ url: baseUrl }], |
| 51 | + paths: { |
| 52 | + "/items/{itemId}": { |
| 53 | + get: { |
| 54 | + operationId: "getItem", |
| 55 | + summary: "Fetch one item", |
| 56 | + parameters: [{ name: "itemId", in: "path", required: true, schema: { type: "string" } }], |
| 57 | + responses: { "200": { description: "the item" } }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | +const invokeByAddressCode = (address: string, args: unknown) => ` |
| 64 | +const segments = ${JSON.stringify(address)}.split(".").slice(1); |
| 65 | +let node = tools; |
| 66 | +for (const segment of segments) node = node[segment]; |
| 67 | +const result = await node(${JSON.stringify(args)}); |
| 68 | +return JSON.stringify(result); |
| 69 | +`; |
| 70 | + |
| 71 | +type ToolEnvelope = { |
| 72 | + readonly ok: boolean; |
| 73 | + readonly error?: { |
| 74 | + readonly code?: string; |
| 75 | + readonly message?: string; |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +scenario( |
| 80 | + "OpenAPI: unknown tool arguments fail locally before any upstream request", |
| 81 | + {}, |
| 82 | + Effect.scoped( |
| 83 | + Effect.gen(function* () { |
| 84 | + const target = yield* Target; |
| 85 | + const { client: makeClient } = yield* Api; |
| 86 | + const identity = yield* target.newIdentity(); |
| 87 | + const client = yield* makeClient(api, identity); |
| 88 | + const upstream = yield* serveRecordingUpstream(); |
| 89 | + const slug = `openapi_unknown_args_${randomBytes(4).toString("hex")}`; |
| 90 | + |
| 91 | + yield* Effect.ensuring( |
| 92 | + Effect.gen(function* () { |
| 93 | + yield* client.openapi.addSpec({ |
| 94 | + payload: { |
| 95 | + spec: { kind: "blob", value: itemsSpec(upstream.url) }, |
| 96 | + slug, |
| 97 | + baseUrl: upstream.url, |
| 98 | + authenticationTemplate: [ |
| 99 | + { |
| 100 | + slug: "apiKey", |
| 101 | + type: "apiKey", |
| 102 | + headers: { authorization: ["Bearer ", { type: "variable", name: "token" }] }, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + }); |
| 107 | + yield* client.connections.create({ |
| 108 | + payload: { |
| 109 | + owner: "org", |
| 110 | + name: ConnectionName.make("main"), |
| 111 | + integration: IntegrationSlug.make(slug), |
| 112 | + template: AuthTemplateSlug.make("apiKey"), |
| 113 | + value: "tok_unknown_args", |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const tools = yield* client.tools.list({ query: {} }); |
| 118 | + const address = tools |
| 119 | + .filter((tool) => String(tool.integration) === slug) |
| 120 | + .map((tool) => String(tool.address)) |
| 121 | + .find((candidate) => candidate.endsWith("getItem")); |
| 122 | + expect(address, "the getItem operation is available").toBeDefined(); |
| 123 | + |
| 124 | + const executed = yield* client.executions.execute({ |
| 125 | + payload: { |
| 126 | + code: invokeByAddressCode(address!, { itemId: "2", doesNotExist: "nope" }), |
| 127 | + autoApprove: true, |
| 128 | + }, |
| 129 | + }); |
| 130 | + expect(executed.status, "the sandbox returns the tool failure as a value").toBe( |
| 131 | + "completed", |
| 132 | + ); |
| 133 | + const outcome = JSON.parse(executed.text) as ToolEnvelope; |
| 134 | + expect(outcome.ok, "the unknown argument fails the tool call").toBe(false); |
| 135 | + expect(outcome.error?.code, "the failure is classified as invalid arguments").toBe( |
| 136 | + "invalid_tool_arguments", |
| 137 | + ); |
| 138 | + expect( |
| 139 | + outcome.error?.message, |
| 140 | + "the failure names the argument the caller must remove", |
| 141 | + ).toContain("doesNotExist"); |
| 142 | + expect( |
| 143 | + upstream.requests(), |
| 144 | + "argument validation stops the call before upstream dispatch", |
| 145 | + ).toEqual([]); |
| 146 | + }), |
| 147 | + Effect.gen(function* () { |
| 148 | + yield* client.connections |
| 149 | + .remove({ |
| 150 | + params: { |
| 151 | + owner: "org", |
| 152 | + integration: IntegrationSlug.make(slug), |
| 153 | + name: ConnectionName.make("main"), |
| 154 | + }, |
| 155 | + }) |
| 156 | + .pipe(Effect.ignore); |
| 157 | + yield* client.openapi.removeSpec({ params: { slug } }).pipe(Effect.ignore); |
| 158 | + }), |
| 159 | + ); |
| 160 | + }), |
| 161 | + ), |
| 162 | +); |
0 commit comments