Unified · Split
| @@ -30,7 +30,7 @@ import { · 29 unmodified lines | |||
| 30 | requiredTemplateVariables, | 30 | requiredTemplateVariables, |
| 31 | type OpenApiIntegrationConfig, | 31 | type OpenApiIntegrationConfig, |
| 32 | } from "./config"; | 32 | } from "./config"; |
| 33 | import { OpenApiExtractionError, OpenApiParseError } from "./errors"; | 33 | import { OpenApiExtractionError, OpenApiInvocationError, OpenApiParseError } from "./errors"; |
| 34 | import { | 34 | import { |
| 35 | buildInputSchema, | 35 | buildInputSchema, |
| 36 | extract, | 36 | extract, |
| @@ -38,7 +38,13 @@ import { · 1 unmodified line | |||
| 38 | streamOperationBindingsFromStructure, | 38 | streamOperationBindingsFromStructure, |
| 39 | } from "./extract"; | 39 | } from "./extract"; |
| 40 | import { compileToolDefinitions, type ToolDefinition } from "./definitions"; | 40 | import { compileToolDefinitions, type ToolDefinition } from "./definitions"; |
| 41 | import { annotationsForOperation, buildRequest, invokeWithLayer, REQUIRE_APPROVAL } from "./invoke"; | 41 | import { |
| 42 | annotationsForOperation, | ||
| 43 | buildRequest, | ||
| 44 | invokeWithLayer, | ||
| 45 | REQUIRE_APPROVAL, | ||
| 46 | type InvokeOptions, | ||
| 47 | } from "./invoke"; | ||
| 42 | import { parse, type ParsedDocument } from "./parse"; | 48 | import { parse, type ParsedDocument } from "./parse"; |
| 43 | import { parseEntry, structuralSplit, type KeepPathItem, type SpecStructure } from "./split"; | 49 | import { parseEntry, structuralSplit, type KeepPathItem, type SpecStructure } from "./split"; |
| 44 | import { type OpenapiStore, type StoredOperation } from "./store"; | 50 | import { type OpenapiStore, type StoredOperation } from "./store"; |
| @@ -609,6 +615,7 @@ export const invokeOpenApiBackedTool = (input: { · 564 unmodified lines | |||
| 609 | readonly credential: ToolInvocationCredential; | 615 | readonly credential: ToolInvocationCredential; |
| 610 | readonly args: unknown; | 616 | readonly args: unknown; |
| 611 | readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>; | 617 | readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>; |
| 618 | readonly invokeOptions?: InvokeOptions; | ||
| 612 | }) => | 619 | }) => |
| 613 | Effect.gen(function* () { | 620 | Effect.gen(function* () { |
| 614 | const integration = input.toolRow.integration; | 621 | const integration = input.toolRow.integration; |
| @@ -671,15 +678,33 @@ export const invokeOpenApiBackedTool = (input: { · 56 unmodified lines | |||
| 671 | Object.assign(queryParams, rendered.queryParams); | 678 | Object.assign(queryParams, rendered.queryParams); |
| 672 | } | 679 | } |
| 673 | 680 | ||
| 674 | const result = yield* invokeWithLayer( | 681 | const invocation = yield* invokeWithLayer( |
| 675 | binding, | 682 | binding, |
| 676 | (input.args ?? {}) as Record<string, unknown>, | 683 | (input.args ?? {}) as Record<string, unknown>, |
| 677 | config?.baseUrl ?? "", | 684 | config?.baseUrl ?? "", |
| 678 | headers, | 685 | headers, |
| 679 | queryParams, | 686 | queryParams, |
| 680 | input.httpClientLayer, | 687 | input.httpClientLayer, |
| 688 | input.invokeOptions, | ||
| 689 | ).pipe( | ||
| 690 | Effect.map((result) => ({ ok: true as const, result })), | ||
| 691 | Effect.catchTag("OpenApiInvocationError", (error: OpenApiInvocationError) => | ||
| 692 | error.message.startsWith("Upstream returned no response headers within ") | ||
| 693 | ? Effect.succeed({ | ||
| 694 | ok: false as const, | ||
| 695 | failure: ToolResult.fail({ | ||
| 696 | code: "upstream_http_error", | ||
| 697 | message: error.message, | ||
| 698 | details: error.cause ?? error, | ||
| 699 | }), | ||
| 700 | }) | ||
| 701 | : Effect.fail(error), | ||
| 702 | ), | ||
| 681 | ); | 703 | ); |
| 682 | 704 | ||
| 705 | if (!invocation.ok) return invocation.failure; | ||
| 706 | |||
| 707 | const result = invocation.result; | ||
| 683 | const ok = result.status >= 200 && result.status < 300; | 708 | const ok = result.status >= 200 && result.status < 300; |
| 684 | if (!ok) { | 709 | if (!ok) { |
| 685 | if (result.status === 401 || result.status === 403) { | 710 | if (result.status === 401 || result.status === 403) { |
| @@ -12,7 +12,14 @@ export { · 11 unmodified lines | |||
| 12 | type ByteRange, | 12 | type ByteRange, |
| 13 | type KeepPathItem, | 13 | type KeepPathItem, |
| 14 | } from "./split"; | 14 | } from "./split"; |
| 15 | export { invoke, invokeWithLayer, buildRequest, annotationsForOperation } from "./invoke"; | 15 | export { |
| 16 | invoke, | ||
| 17 | invokeWithLayer, | ||
| 18 | buildRequest, | ||
| 19 | annotationsForOperation, | ||
| 20 | RESPONSE_HEADERS_TIMEOUT_MS, | ||
| 21 | type InvokeOptions, | ||
| 22 | } from "./invoke"; | ||
| 16 | export { | 23 | export { |
| 17 | buildDefsJsonStreaming, | 24 | buildDefsJsonStreaming, |
| 18 | checkHealthOpenApi, | 25 | checkHealthOpenApi, |
| @@ -1,5 +1,5 @@ | |||
| 1 | import { Effect, Exit, Layer, Option, Schema, Stream } from "effect"; | 1 | import { Effect, Exit, Fiber, Layer, Option, Schema, Stream } from "effect"; |
| 2 | import { HttpClient, HttpClientRequest } from "effect/unstable/http"; | 2 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; |
| 3 | import type { ToolFileValue } from "@executor-js/sdk/core"; | 3 | import type { ToolFileValue } from "@executor-js/sdk/core"; |
| 4 | 4 | ||
| 5 | import { OpenApiInvocationError } from "./errors"; | 5 | import { OpenApiInvocationError } from "./errors"; |
| @@ -153,6 +153,7 @@ const normalizeContentType = (ct: string | null | undefined): string => · 147 unmodified lines | |||
| 153 | 153 | ||
| 154 | export const STREAM_MAX_BYTES = 1_000_000; | 154 | export const STREAM_MAX_BYTES = 1_000_000; |
| 155 | export const STREAM_MAX_MS = 10_000; | 155 | export const STREAM_MAX_MS = 10_000; |
| 156 | export const RESPONSE_HEADERS_TIMEOUT_MS = 60_000; | ||
| 156 | 157 | ||
| 157 | class StreamCapReached extends Error { | 158 | class StreamCapReached extends Error { |
| 158 | readonly _tag = "StreamCapReached"; | 159 | readonly _tag = "StreamCapReached"; |
| @@ -163,6 +164,16 @@ export interface StreamingResponseCaps { · 4 unmodified lines | |||
| 163 | readonly maxMs?: number; | 164 | readonly maxMs?: number; |
| 164 | } | 165 | } |
| 165 | 166 | ||
| 167 | export interface InvokeOptions { | ||
| 168 | readonly responseHeadersTimeoutMs?: number; | ||
| 169 | } | ||
| 170 | |||
| 171 | const formatTimeout = (timeoutMs: number): string => | ||
| 172 | timeoutMs % 1_000 === 0 ? `${timeoutMs / 1_000}s` : `${timeoutMs}ms`; | ||
| 173 | |||
| 174 | const responseHeadersTimeoutMessage = (timeoutMs: number): string => | ||
| 175 | `Upstream returned no response headers within ${formatTimeout(timeoutMs)}. The endpoint may be a live stream with no data to send (for example, runtime logs of an idle deployment); the request was aborted. Retry when the resource has activity, or use a non-streaming endpoint.`; | ||
| 176 | |||
| 166 | const STREAMING_RESPONSE_CONTENT_TYPES = new Set([ | 177 | const STREAMING_RESPONSE_CONTENT_TYPES = new Set([ |
| 167 | "application/stream+json", | 178 | "application/stream+json", |
| 168 | "application/x-ndjson", | 179 | "application/x-ndjson", |
| @@ -260,39 +271,71 @@ export const collectStreamingBody = ( · 91 unmodified lines | |||
| 260 | let truncated = false; | 271 | let truncated = false; |
| 261 | const startedAt = Date.now(); | 272 | const startedAt = Date.now(); |
| 262 | 273 | ||
| 263 | const completed = yield* stream.pipe( | 274 | const runFork = Effect.runForkWith(yield* Effect.context<never>()); |
| 264 | Stream.timeout(maxMs), | 275 | const completedExitOption = yield* Effect.callback<Option.Option<Exit.Exit<void, unknown>>>( |
| 265 | Stream.runForEach((chunk) => | 276 | (resume, signal) => { |
| 266 | Effect.gen(function* () { | 277 | let settled = false; |
| 267 | const remaining = maxBytes - bytes; | 278 | const collectEffect = stream.pipe( |
| 268 | if (remaining <= 0) { | 279 | Stream.timeout(maxMs), |
| 269 | truncated = true; | 280 | Stream.runForEach((chunk) => |
| 270 | return yield* Effect.fail(new StreamCapReached()); | 281 | Effect.gen(function* () { |
| 271 | } | 282 | const remaining = maxBytes - bytes; |
| 272 | 283 | if (remaining <= 0) { | |
| 273 | if (chunk.byteLength > remaining) { | 284 | truncated = true; |
| 274 | chunks.push(chunk.subarray(0, remaining)); | 285 | return yield* Effect.fail(new StreamCapReached()); |
| 275 | bytes += remaining; | 286 | } |
| 276 | truncated = true; | 287 | |
| 277 | return yield* Effect.fail(new StreamCapReached()); | 288 | if (chunk.byteLength > remaining) { |
| 278 | } | 289 | chunks.push(chunk.subarray(0, remaining)); |
| 279 | 290 | bytes += remaining; | |
| 280 | chunks.push(chunk); | 291 | truncated = true; |
| 281 | bytes += chunk.byteLength; | 292 | return yield* Effect.fail(new StreamCapReached()); |
| 282 | if (bytes >= maxBytes) { | 293 | } |
| 283 | truncated = true; | 294 | |
| 284 | return yield* Effect.fail(new StreamCapReached()); | 295 | chunks.push(chunk); |
| 285 | } | 296 | bytes += chunk.byteLength; |
| 286 | }), | 297 | if (bytes >= maxBytes) { |
| 287 | ), | 298 | truncated = true; |
| 288 | Effect.timeoutOption(maxMs + 1_000), | 299 | return yield* Effect.fail(new StreamCapReached()); |
| 289 | Effect.catch((error: unknown) => | 300 | } |
| 290 | error instanceof StreamCapReached | 301 | }), |
| 291 | ? Effect.succeed(Option.some(undefined)) | 302 | ), |
| 292 | : Effect.fail(error), | 303 | Effect.catch((error: unknown) => |
| 293 | ), | 304 | error instanceof StreamCapReached ? Effect.void : Effect.fail(error), |
| 305 | ), | ||
| 306 | Effect.exit, | ||
| 307 | Effect.tap((exit) => | ||
| 308 | Effect.sync(() => { | ||
| 309 | if (settled) return; | ||
| 310 | settled = true; | ||
| 311 | clearTimeout(timer); | ||
| 312 | resume(Effect.succeed(Option.some(exit))); | ||
| 313 | }), | ||
| 314 | ), | ||
| 315 | ); | ||
| 316 | const fiber = runFork(collectEffect); | ||
| 317 | const interrupt = () => { | ||
| 318 | runFork(Fiber.interrupt(fiber)); | ||
| 319 | }; | ||
| 320 | const timer = setTimeout(() => { | ||
| 321 | if (settled) return; | ||
| 322 | settled = true; | ||
| 323 | truncated = true; | ||
| 324 | interrupt(); | ||
| 325 | resume(Effect.succeed(Option.none())); | ||
| 326 | }, maxMs + 1_000); | ||
| 327 | signal.addEventListener("abort", interrupt, { once: true }); | ||
| 328 | return Effect.sync(() => { | ||
| 329 | clearTimeout(timer); | ||
| 330 | signal.removeEventListener("abort", interrupt); | ||
| 331 | if (!settled) interrupt(); | ||
| 332 | }); | ||
| 333 | }, | ||
| 294 | ); | 334 | ); |
| 295 | if (Option.isNone(completed)) truncated = true; | 335 | if (Option.isSome(completedExitOption) && Exit.isFailure(completedExitOption.value)) { |
| 336 | return yield* Effect.failCause(completedExitOption.value.cause); | ||
| 337 | } | ||
| 338 | if (Option.isNone(completedExitOption)) truncated = true; | ||
| 296 | 339 | ||
| 297 | const durationMs = Date.now() - startedAt; | 340 | const durationMs = Date.now() - startedAt; |
| 298 | if (durationMs >= maxMs) truncated = true; | 341 | if (durationMs >= maxMs) truncated = true; |
| @@ -952,6 +995,7 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 653 unmodified lines | |||
| 952 | args: Record<string, unknown>, | 995 | args: Record<string, unknown>, |
| 953 | resolvedHeaders: Record<string, string>, | 996 | resolvedHeaders: Record<string, string>, |
| 954 | sourceQueryParams: Record<string, string> = {}, | 997 | sourceQueryParams: Record<string, string> = {}, |
| 998 | options: InvokeOptions = {}, | ||
| 955 | ) { | 999 | ) { |
| 956 | const client = yield* HttpClient.HttpClient; | 1000 | const client = yield* HttpClient.HttpClient; |
| 957 | 1001 | ||
| @@ -965,16 +1009,57 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 7 unmodified lines | |||
| 965 | 1009 | ||
| 966 | const request = yield* buildRequest(operation, args, resolvedHeaders, sourceQueryParams); | 1010 | const request = yield* buildRequest(operation, args, resolvedHeaders, sourceQueryParams); |
| 967 | 1011 | ||
| 968 | const response = yield* client.execute(request).pipe( | 1012 | const responseHeadersTimeoutMs = options.responseHeadersTimeoutMs ?? RESPONSE_HEADERS_TIMEOUT_MS; |
| 969 | Effect.mapError( | 1013 | const runFork = Effect.runForkWith(yield* Effect.context<never>()); |
| 970 | (err) => | 1014 | const responseExitOption = yield* Effect.callback< |
| 971 | new OpenApiInvocationError({ | 1015 | Option.Option<Exit.Exit<HttpClientResponse.HttpClientResponse, OpenApiInvocationError>> |
| 972 | message: "HTTP request failed", | 1016 | >((resume, signal) => { |
| 973 | statusCode: Option.none(), | 1017 | let settled = false; |
| 974 | cause: err, | 1018 | const responseEffect = client.execute(request).pipe( |
| 1019 | Effect.mapError( | ||
| 1020 | (err) => | ||
| 1021 | new OpenApiInvocationError({ | ||
| 1022 | message: "HTTP request failed", | ||
| 1023 | statusCode: Option.none(), | ||
| 1024 | cause: err, | ||
| 1025 | }), | ||
| 1026 | ), | ||
| 1027 | Effect.exit, | ||
| 1028 | Effect.tap((exit) => | ||
| 1029 | Effect.sync(() => { | ||
| 1030 | if (settled) return; | ||
| 1031 | settled = true; | ||
| 1032 | clearTimeout(timer); | ||
| 1033 | resume(Effect.succeed(Option.some(exit))); | ||
| 975 | }), | 1034 | }), |
| 976 | ), | 1035 | ), |
| 977 | ); | 1036 | ); |
| 1037 | const fiber = runFork(responseEffect); | ||
| 1038 | const interrupt = () => { | ||
| 1039 | runFork(Fiber.interrupt(fiber)); | ||
| 1040 | }; | ||
| 1041 | const timer = setTimeout(() => { | ||
| 1042 | if (settled) return; | ||
| 1043 | settled = true; | ||
| 1044 | interrupt(); | ||
| 1045 | resume(Effect.succeed(Option.none())); | ||
| 1046 | }, responseHeadersTimeoutMs); | ||
| 1047 | signal.addEventListener("abort", interrupt, { once: true }); | ||
| 1048 | return Effect.sync(() => { | ||
| 1049 | clearTimeout(timer); | ||
| 1050 | signal.removeEventListener("abort", interrupt); | ||
| 1051 | if (!settled) interrupt(); | ||
| 1052 | }); | ||
| 1053 | }); | ||
| 1054 | if (Option.isNone(responseExitOption)) { | ||
| 1055 | return yield* new OpenApiInvocationError({ | ||
| 1056 | message: responseHeadersTimeoutMessage(responseHeadersTimeoutMs), | ||
| 1057 | statusCode: Option.none(), | ||
| 1058 | }); | ||
| 1059 | } | ||
| 1060 | const responseExit = responseExitOption.value; | ||
| 1061 | if (Exit.isFailure(responseExit)) return yield* Effect.failCause(responseExit.cause); | ||
| 1062 | const response = responseExit.value; | ||
| 978 | 1063 | ||
| 979 | const status = response.status; | 1064 | const status = response.status; |
| 980 | yield* Effect.annotateCurrentSpan({ | 1065 | yield* Effect.annotateCurrentSpan({ |
| @@ -1073,6 +1158,7 @@ export const invokeWithLayer = ( · 92 unmodified lines | |||
| 1073 | resolvedHeaders: Record<string, string>, | 1158 | resolvedHeaders: Record<string, string>, |
| 1074 | sourceQueryParams: Record<string, string>, | 1159 | sourceQueryParams: Record<string, string>, |
| 1075 | httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>, | 1160 | httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>, |
| 1161 | options: InvokeOptions = {}, | ||
| 1076 | ) => { | 1162 | ) => { |
| 1077 | const effectiveBaseUrl = resolveRequestHost(operation.servers ?? [], args.server, baseUrl); | 1163 | const effectiveBaseUrl = resolveRequestHost(operation.servers ?? [], args.server, baseUrl); |
| 1078 | const clientWithBaseUrl = effectiveBaseUrl | 1164 | const clientWithBaseUrl = effectiveBaseUrl |
| @@ -1085,7 +1171,7 @@ export const invokeWithLayer = ( · 6 unmodified lines | |||
| 1085 | ).pipe(Layer.provide(httpClientLayer)) | 1171 | ).pipe(Layer.provide(httpClientLayer)) |
| 1086 | : httpClientLayer; | 1172 | : httpClientLayer; |
| 1087 | 1173 | ||
| 1088 | return invoke(operation, args, resolvedHeaders, sourceQueryParams).pipe( | 1174 | return invoke(operation, args, resolvedHeaders, sourceQueryParams, options).pipe( |
| 1089 | Effect.provide(clientWithBaseUrl), | 1175 | Effect.provide(clientWithBaseUrl), |
| 1090 | // `invoke` annotates http.status_code on ITS span (`OpenApi.invoke`, | 1176 | // `invoke` annotates http.status_code on ITS span (`OpenApi.invoke`, |
| 1091 | // via Effect.fn) — annotateCurrentSpan inside it never reaches this | 1177 | // via Effect.fn) — annotateCurrentSpan inside it never reaches this |
| @@ -48,6 +48,7 @@ import { · 47 unmodified lines | |||
| 48 | resolveOpenApiBackedTools, | 48 | resolveOpenApiBackedTools, |
| 49 | validateOpenApiBackedToolArgs, | 49 | validateOpenApiBackedToolArgs, |
| 50 | } from "./backing"; | 50 | } from "./backing"; |
| 51 | import type { InvokeOptions } from "./invoke"; | ||
| 51 | import { resolveServerUrl } from "./openapi-utils"; | 52 | import { resolveServerUrl } from "./openapi-utils"; |
| 52 | 53 | ||
| 53 | // --------------------------------------------------------------------------- | 54 | // --------------------------------------------------------------------------- |
| @@ -563,6 +564,7 @@ export const describeOpenApiIntegrationDisplay = ( · 509 unmodified lines | |||
| 563 | 564 | ||
| 564 | export interface OpenApiPluginOptions { | 565 | export interface OpenApiPluginOptions { |
| 565 | readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient, never, never>; | 566 | readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient, never, never>; |
| 567 | readonly invokeOptions?: InvokeOptions; | ||
| 566 | } | 568 | } |
| 567 | 569 | ||
| 568 | export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { | 570 | export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { |
| @@ -1044,6 +1046,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 475 unmodified lines | |||
| 1044 | credential, | 1046 | credential, |
| 1045 | args, | 1047 | args, |
| 1046 | httpClientLayer, | 1048 | httpClientLayer, |
| 1049 | invokeOptions: options?.invokeOptions, | ||
| 1047 | }); | 1050 | }); |
| 1048 | }, | 1051 | }, |
| 1049 | 1052 | ||
| @@ -0,0 +1,207 @@ | |||
| 1 | import { describe, expect, it } from "@effect/vitest"; | ||
| 2 | import { Deferred, Effect, Option, Schema } from "effect"; | ||
| 3 | import { FetchHttpClient } from "effect/unstable/http"; | ||
| 4 | import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"; | ||
| 5 | import { createServer, type ServerResponse } from "node:http"; | ||
| 6 | import type { AddressInfo, Socket } from "node:net"; | ||
| 7 | |||
| 8 | import { | ||
| 9 | createExecutor, | ||
| 10 | AuthTemplateSlug, | ||
| 11 | ConnectionName, | ||
| 12 | IntegrationSlug, | ||
| 13 | ToolAddress, | ||
| 14 | } from "@executor-js/sdk"; | ||
| 15 | import { makeTestConfig, memoryCredentialsPlugin } from "@executor-js/sdk/testing"; | ||
| 16 | import { makeOpenApiHttpApiTestSourceConfig, unwrapInvocation } from "../testing"; | ||
| 17 | |||
| 18 | import { invokeWithLayer } from "./invoke"; | ||
| 19 | import { openApiPlugin } from "./plugin"; | ||
| 20 | import type { OperationBinding } from "./types"; | ||
| 21 | |||
| 22 | const RESPONSE_HEADERS_TIMEOUT_MS = 100; | ||
| 23 | const STREAM_TOOL = "logs.getLogs"; | ||
| 24 | const encoder = new TextEncoder(); | ||
| 25 | |||
| 26 | const LogsGroup = HttpApiGroup.make("logs").add( | ||
| 27 | HttpApiEndpoint.get("getLogs", "/logs", { | ||
| 28 | success: Schema.Unknown, | ||
| 29 | }), | ||
| 30 | ); | ||
| 31 | |||
| 32 | const TimeoutApi = HttpApi.make("responseHeadersTimeoutTest") | ||
| 33 | .add(LogsGroup) | ||
| 34 | .annotateMerge(OpenApi.annotations({ title: "ResponseHeadersTimeoutTest", version: "1.0.0" })); | ||
| 35 | |||
| 36 | const testPlugins = () => | ||
| 37 | [ | ||
| 38 | openApiPlugin({ | ||
| 39 | httpClientLayer: FetchHttpClient.layer, | ||
| 40 | invokeOptions: { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS }, | ||
| 41 | }), | ||
| 42 | memoryCredentialsPlugin(), | ||
| 43 | ] as const; | ||
| 44 | |||
| 45 | const buildExecutor = (baseUrl: string) => | ||
| 46 | Effect.gen(function* () { | ||
| 47 | const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() })); | ||
| 48 | yield* executor.openapi.addSpec( | ||
| 49 | makeOpenApiHttpApiTestSourceConfig(TimeoutApi, { slug: "headers_timeout", baseUrl }), | ||
| 50 | ); | ||
| 51 | yield* executor.connections.create({ | ||
| 52 | owner: "org", | ||
| 53 | name: ConnectionName.make("main"), | ||
| 54 | integration: IntegrationSlug.make("headers_timeout"), | ||
| 55 | template: AuthTemplateSlug.make("apiKey"), | ||
| 56 | value: "token", | ||
| 57 | }); | ||
| 58 | return { | ||
| 59 | executor, | ||
| 60 | address: ToolAddress.make(`tools.headers_timeout.org.main.${STREAM_TOOL}`), | ||
| 61 | }; | ||
| 62 | }); | ||
| 63 | |||
| 64 | const startRawServer = (handler: (res: ServerResponse) => void) => | ||
| 65 | Effect.acquireRelease( | ||
| 66 | Effect.callback<{ baseUrl: string; close: () => void }>((resume) => { | ||
| 67 | const sockets = new Set<Socket>(); | ||
| 68 | const timers = new Set<ReturnType<typeof setTimeout>>(); | ||
| 69 | const server = createServer((req, res) => { | ||
| 70 | if (req.url?.split("?")[0] !== "/logs") { | ||
| 71 | res.writeHead(200, { "content-type": "application/json" }); | ||
| 72 | res.end(JSON.stringify({ ok: true })); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | handler(res); | ||
| 76 | }); | ||
| 77 | server.on("connection", (socket) => { | ||
| 78 | sockets.add(socket); | ||
| 79 | socket.on("close", () => sockets.delete(socket)); | ||
| 80 | }); | ||
| 81 | server.listen(0, "127.0.0.1", () => { | ||
| 82 | const port = (server.address() as AddressInfo).port; | ||
| 83 | resume( | ||
| 84 | Effect.succeed({ | ||
| 85 | baseUrl: `http://127.0.0.1:${port}`, | ||
| 86 | close: () => { | ||
| 87 | for (const timer of timers) clearTimeout(timer); | ||
| 88 | for (const socket of sockets) socket.destroy(); | ||
| 89 | server.close(); | ||
| 90 | }, | ||
| 91 | }), | ||
| 92 | ); | ||
| 93 | }); | ||
| 94 | }), | ||
| 95 | (server) => Effect.sync(() => server.close()), | ||
| 96 | ); | ||
| 97 | |||
| 98 | const startHeaderlessServer = (closed: Deferred.Deferred<void>) => | ||
| 99 | Effect.acquireRelease( | ||
| 100 | Effect.callback<{ baseUrl: string; close: () => void }>((resume) => { | ||
| 101 | const sockets = new Set<Socket>(); | ||
| 102 | const server = createServer((req, res) => { | ||
| 103 | if (req.url?.split("?")[0] === "/logs") return; | ||
| 104 | res.writeHead(200, { "content-type": "application/json" }); | ||
| 105 | res.end(JSON.stringify({ ok: true })); | ||
| 106 | }); | ||
| 107 | server.on("connection", (socket) => { | ||
| 108 | sockets.add(socket); | ||
| 109 | socket.on("close", () => { | ||
| 110 | sockets.delete(socket); | ||
| 111 | Effect.runFork(Deferred.succeed(closed, undefined)); | ||
| 112 | }); | ||
| 113 | }); | ||
| 114 | server.listen(0, "127.0.0.1", () => { | ||
| 115 | const port = (server.address() as AddressInfo).port; | ||
| 116 | resume( | ||
| 117 | Effect.succeed({ | ||
| 118 | baseUrl: `http://127.0.0.1:${port}`, | ||
| 119 | close: () => { | ||
| 120 | for (const socket of sockets) socket.destroy(); | ||
| 121 | server.close(); | ||
| 122 | }, | ||
| 123 | }), | ||
| 124 | ); | ||
| 125 | }); | ||
| 126 | }), | ||
| 127 | (server) => Effect.sync(() => server.close()), | ||
| 128 | ); | ||
| 129 | |||
| 130 | const operation: OperationBinding = { | ||
| 131 | method: "get", | ||
| 132 | pathTemplate: "/logs", | ||
| 133 | parameters: [], | ||
| 134 | requestBody: Option.none(), | ||
| 135 | responseBody: Option.none(), | ||
| 136 | servers: [], | ||
| 137 | }; | ||
| 138 | |||
| 139 | describe("OpenAPI response headers timeout", () => { | ||
| 140 | it.effect("aborts a headerless upstream and returns an actionable tool failure", () => | ||
| 141 | Effect.gen(function* () { | ||
| 142 | const closed = yield* Deferred.make<void>(); | ||
| 143 | const server = yield* startHeaderlessServer(closed); | ||
| 144 | const { executor, address } = yield* buildExecutor(server.baseUrl); | ||
| 145 | const startedAt = Date.now(); | ||
| 146 | |||
| 147 | const result = yield* executor.execute(address, {}); | ||
| 148 | const elapsedMs = Date.now() - startedAt; | ||
| 149 | const socketClosed = yield* Deferred.await(closed).pipe(Effect.timeoutOption(1_000)); | ||
| 150 | |||
| 151 | expect(elapsedMs).toBeGreaterThanOrEqual(RESPONSE_HEADERS_TIMEOUT_MS - 25); | ||
| 152 | expect(elapsedMs).toBeLessThan(2_000); | ||
| 153 | expect(Option.isSome(socketClosed)).toBe(true); | ||
| 154 | expect(result).toMatchObject({ | ||
| 155 | ok: false, | ||
| 156 | error: { | ||
| 157 | code: "upstream_http_error", | ||
| 158 | message: expect.stringContaining("Upstream returned no response headers within 100ms"), | ||
| 159 | }, | ||
| 160 | }); | ||
| 161 | }), | ||
| 162 | ); | ||
| 163 | |||
| 164 | it.effect("leaves normal fast JSON responses unaffected", () => | ||
| 165 | Effect.gen(function* () { | ||
| 166 | const server = yield* startRawServer((res) => { | ||
| 167 | res.writeHead(200, { "content-type": "application/json" }); | ||
| 168 | res.end(JSON.stringify({ ok: true })); | ||
| 169 | }); | ||
| 170 | const { executor, address } = yield* buildExecutor(server.baseUrl); | ||
| 171 | |||
| 172 | const result = unwrapInvocation<Record<string, unknown>>( | ||
| 173 | yield* executor.execute(address, {}), | ||
| 174 | ); | ||
| 175 | |||
| 176 | expect(result.status).toBe(200); | ||
| 177 | expect(result.data).toEqual({ ok: true }); | ||
| 178 | }), | ||
| 179 | ); | ||
| 180 | |||
| 181 | it.effect("does not apply the headers timeout after streaming response headers arrive", () => | ||
| 182 | Effect.gen(function* () { | ||
| 183 | const server = yield* startRawServer((res) => { | ||
| 184 | res.writeHead(200, { "content-type": "application/jsonl" }); | ||
| 185 | res.write(encoder.encode(`${JSON.stringify({ id: 1 })}\n`)); | ||
| 186 | setTimeout(() => { | ||
| 187 | res.write(encoder.encode(`${JSON.stringify({ id: 2 })}\n`)); | ||
| 188 | res.end(); | ||
| 189 | }, RESPONSE_HEADERS_TIMEOUT_MS + 100); | ||
| 190 | }); | ||
| 191 | |||
| 192 | const result = yield* invokeWithLayer( | ||
| 193 | operation, | ||
| 194 | {}, | ||
| 195 | server.baseUrl, | ||
| 196 | {}, | ||
| 197 | {}, | ||
| 198 | FetchHttpClient.layer, | ||
| 199 | { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS }, | ||
| 200 | ); | ||
| 201 | |||
| 202 | expect(result.status).toBe(200); | ||
| 203 | expect(result.data).toEqual([{ id: 1 }, { id: 2 }]); | ||
| 204 | expect(result.headers["x-executor-stream"]).toBe("complete"); | ||
| 205 | }), | ||
| 206 | ); | ||
| 207 | }); | ||