| @@ -1,4 +1,4 @@ |
| 1 | | −import { Effect, Option, Schema } from "effect"; |
| 1 | +import { Effect, Option, Predicate, Schema } from "effect"; |
| 2 | 2 | import type { Layer } from "effect"; |
| 3 | 3 | import { FetchHttpClient, HttpClient } from "effect/unstable/http"; |
| 4 | 4 | |
| @@ -376,13 +376,15 @@ const resolveEffectiveSourceConfig = ( · 371 unmodified lines |
| 376 | 376 | base: StoredSource, |
| 377 | 377 | ): Effect.Effect<EffectiveSourceConfig, StorageFailure> => |
| 378 | 378 | Effect.gen(function* () { |
| 379 | | − const rank = new Map(ctx.scopes.map((scope, index) => [scope.id as string, index] as const)); |
| 379 | + const rank = new Map<string, number>( |
| 380 | + ctx.scopes.map((scope, index) => [scope.id, index] as const), |
| 381 | + ); |
| 380 | 382 | const baseRank = rank.get(base.scope) ?? Infinity; |
| 381 | 383 | let fallback: StoredSource | null = null; |
| 382 | 384 | for (let index = baseRank + 1; index < ctx.scopes.length; index++) { |
| 383 | 385 | const scope = ctx.scopes[index]; |
| 384 | 386 | if (!scope) continue; |
| 385 | | − fallback = yield* ctx.storage.getSource(base.namespace, scope.id as string); |
| 387 | + fallback = yield* ctx.storage.getSource(base.namespace, scope.id); |
| 386 | 388 | if (fallback) break; |
| 387 | 389 | } |
| 388 | 390 | |
| @@ -435,13 +437,13 @@ const resolveConfiguredHeaders = ( · 46 unmodified lines |
| 435 | 437 | value.slot, |
| 436 | 438 | ); |
| 437 | 439 | if (binding?.value.kind === "secret") { |
| 438 | | − const secret = yield* ctx.secrets.get(binding.value.secretId as string).pipe( |
| 439 | | − Effect.mapError((err) => |
| 440 | | − "_tag" in err && err._tag === "SecretOwnedByConnectionError" |
| 441 | | − ? new OpenApiOAuthError({ |
| 442 | | − message: `Secret not found for header "${name}"`, |
| 443 | | − }) |
| 444 | | − : err, |
| 440 | + const secret = yield* ctx.secrets.get(binding.value.secretId).pipe( |
| 441 | + Effect.catchTag("SecretOwnedByConnectionError", () => |
| 442 | + Effect.fail( |
| 443 | + new OpenApiOAuthError({ |
| 444 | + message: `Secret not found for header "${name}"`, |
| 445 | + }), |
| 446 | + ), |
| 445 | 447 | ), |
| 446 | 448 | ); |
| 447 | 449 | if (secret === null) { |
| @@ -460,10 +462,8 @@ const resolveConfiguredHeaders = ( · 12 unmodified lines |
| 460 | 462 | if (legacy) { |
| 461 | 463 | const fallback = yield* resolveHeaders({ [name]: legacy }, ctx.secrets).pipe( |
| 462 | 464 | Effect.map((headers) => headers[name]!), |
| 463 | | − Effect.mapError((err) => |
| 464 | | − err instanceof OpenApiOAuthError |
| 465 | | − ? err |
| 466 | | − : new OpenApiOAuthError({ message: err.message }), |
| 465 | + Effect.catchTag("OpenApiInvocationError", () => |
| 466 | + Effect.fail(new OpenApiOAuthError({ message: "Secret resolution failed" })), |
| 467 | 467 | ), |
| 468 | 468 | ); |
| 469 | 469 | resolved[name] = fallback; |
| @@ -488,14 +488,14 @@ const resolveHeaderValues = ( · 18 unmodified lines |
| 488 | 488 | message: `Secret not found for "${name}"`, |
| 489 | 489 | }), |
| 490 | 490 | onError: (err, name) => |
| 491 | | − "_tag" in err && err._tag === "SecretOwnedByConnectionError" |
| 491 | + Predicate.isTagged("SecretOwnedByConnectionError")(err) |
| 492 | 492 | ? new OpenApiOAuthError({ |
| 493 | 493 | message: `Secret not found for "${name}"`, |
| 494 | 494 | }) |
| 495 | 495 | : err, |
| 496 | 496 | }).pipe( |
| 497 | 497 | Effect.mapError((err) => |
| 498 | | − "_tag" in err && err._tag === "SecretOwnedByConnectionError" |
| 498 | + Predicate.isTagged("SecretOwnedByConnectionError")(err) |
| 499 | 499 | ? new OpenApiOAuthError({ message: "Secret resolution failed" }) |
| 500 | 500 | : err, |
| 501 | 501 | ), |
| @@ -518,7 +518,7 @@ const resolveOAuthConnectionId = ( · 16 unmodified lines |
| 518 | 518 | params.oauth2.connectionSlot, |
| 519 | 519 | ); |
| 520 | 520 | if (binding?.value.kind === "connection") { |
| 521 | | − const connectionId = binding.value.connectionId as string; |
| 521 | + const connectionId = binding.value.connectionId; |
| 522 | 522 | const connection = yield* ctx.connections.get(connectionId); |
| 523 | 523 | return connection ? connectionId : null; |
| 524 | 524 | } |
| @@ -763,7 +763,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 238 unmodified lines |
| 763 | 763 | const configFile = options?.configFile; |
| 764 | 764 | |
| 765 | 765 | return { |
| 766 | | − previewSpec: (input) => |
| 766 | + previewSpec: (input: string | OpenApiPreviewInput) => |
| 767 | 767 | Effect.gen(function* () { |
| 768 | 768 | const previewInput = typeof input === "string" ? { spec: input } : input; |
| 769 | 769 | const credentials = yield* resolveSpecFetchCredentials( |
| @@ -776,7 +776,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines |
| 776 | 776 | return yield* previewSpec(specText).pipe(Effect.provide(httpClientLayer)); |
| 777 | 777 | }), |
| 778 | 778 | |
| 779 | | − addSpec: (config) => |
| 779 | + addSpec: (config: OpenApiSpecConfig) => |
| 780 | 780 | Effect.gen(function* () { |
| 781 | 781 | const result = yield* addSpecInternal(config); |
| 782 | 782 | if (configFile) { |
| @@ -785,7 +785,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 2 unmodified lines |
| 785 | 785 | return result; |
| 786 | 786 | }), |
| 787 | 787 | |
| 788 | | − removeSpec: (namespace, scope) => |
| 788 | + removeSpec: (namespace: string, scope: string) => |
| 789 | 789 | Effect.gen(function* () { |
| 790 | 790 | yield* ctx.transaction( |
| 791 | 791 | Effect.gen(function* () { |
| @@ -798,7 +798,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines |
| 798 | 798 | } |
| 799 | 799 | }), |
| 800 | 800 | |
| 801 | | − getSource: (namespace, scope) => |
| 801 | + getSource: (namespace: string, scope: string) => |
| 802 | 802 | Effect.gen(function* () { |
| 803 | 803 | const source = yield* ctx.storage.getSource(namespace, scope); |
| 804 | 804 | if (!source) return null; |
| @@ -809,7 +809,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 4 unmodified lines |
| 809 | 809 | }; |
| 810 | 810 | }), |
| 811 | 811 | |
| 812 | | − updateSource: (namespace, scope, input) => |
| 812 | + updateSource: (namespace: string, scope: string, input: OpenApiUpdateSourceInput) => |
| 813 | 813 | Effect.gen(function* () { |
| 814 | 814 | const existing = yield* ctx.storage.getSource(namespace, scope); |
| 815 | 815 | if (!existing) return; |
| @@ -847,14 +847,14 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 31 unmodified lines |
| 847 | 847 | } |
| 848 | 848 | }), |
| 849 | 849 | |
| 850 | | − listSourceBindings: (sourceId, sourceScope) => |
| 850 | + listSourceBindings: (sourceId: string, sourceScope: string) => |
| 851 | 851 | ctx.storage.listSourceBindings(sourceId, sourceScope), |
| 852 | 852 | |
| 853 | | − setSourceBinding: (input) => ctx.storage.setSourceBinding(input), |
| 853 | + setSourceBinding: (input: OpenApiSourceBindingInput) => ctx.storage.setSourceBinding(input), |
| 854 | 854 | |
| 855 | | − removeSourceBinding: (sourceId, sourceScope, slot, scope) => |
| 855 | + removeSourceBinding: (sourceId: string, sourceScope: string, slot: string, scope: string) => |
| 856 | 856 | ctx.storage.removeSourceBinding(sourceId, sourceScope, slot, scope), |
| 857 | | − } satisfies OpenApiPluginExtension; |
| 857 | + }; |
| 858 | 858 | }, |
| 859 | 859 | |
| 860 | 860 | staticSources: (self) => [ |
| @@ -909,7 +909,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 48 unmodified lines |
| 909 | 909 | handler: ({ ctx, args }) => |
| 910 | 910 | self.addSpec({ |
| 911 | 911 | ...(args as AddSourceInput), |
| 912 | | − scope: ctx.scopes.at(-1)!.id as string, |
| 912 | + scope: ctx.scopes.at(-1)!.id, |
| 913 | 913 | }), |
| 914 | 914 | }, |
| 915 | 915 | ], |
| @@ -923,16 +923,18 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 7 unmodified lines |
| 923 | 923 | // openapi_operation + openapi_source rows live at the same |
| 924 | 924 | // scope, so pin every store lookup to it instead of relying |
| 925 | 925 | // on the scoped adapter's stack-wide fall-through. |
| 926 | | − const toolScope = toolRow.scope_id as string; |
| 926 | + const toolScope = toolRow.scope_id; |
| 927 | 927 | const op = yield* ctx.storage.getOperationByToolId(toolRow.id, toolScope); |
| 928 | 928 | if (!op) { |
| 929 | | − return yield* Effect.fail( |
| 930 | | − new Error(`No OpenAPI operation found for tool "${toolRow.id}"`), |
| 931 | | − ); |
| 929 | + return yield* new OpenApiExtractionError({ |
| 930 | + message: `No OpenAPI operation found for tool "${toolRow.id}"`, |
| 931 | + }); |
| 932 | 932 | } |
| 933 | 933 | const source = yield* ctx.storage.getSource(op.sourceId, toolScope); |
| 934 | 934 | if (!source) { |
| 935 | | − return yield* Effect.fail(new Error(`No OpenAPI source found for "${op.sourceId}"`)); |
| 935 | + return yield* new OpenApiExtractionError({ |
| 936 | + message: `No OpenAPI source found for "${op.sourceId}"`, |
| 937 | + }); |
| 936 | 938 | } |
| 937 | 939 | |
| 938 | 940 | const effective = yield* resolveEffectiveSourceConfig(ctx, source); |
| @@ -942,10 +944,8 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 3 unmodified lines |
| 942 | 944 | sourceScope: effective.headersSource.scope, |
| 943 | 945 | headers: config.headers ?? {}, |
| 944 | 946 | legacyHeaders: effective.headersSource.legacy?.headers, |
| 945 | | − }).pipe(Effect.mapError((err) => new Error(err.message))); |
| 946 | | − const resolvedQueryParams = yield* resolveHeaderValues(ctx, config.queryParams).pipe( |
| 947 | | − Effect.mapError((err) => new Error(err.message)), |
| 948 | | − ); |
| 947 | + }); |
| 948 | + const resolvedQueryParams = yield* resolveHeaderValues(ctx, config.queryParams); |
| 949 | 949 | |
| 950 | 950 | // If the source has OAuth2 auth, resolve a guaranteed-fresh |
| 951 | 951 | // access token from the backing Connection and inject the |
| @@ -959,22 +959,18 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 7 unmodified lines |
| 959 | 959 | legacyOAuth2: effective.oauth2Source.legacy?.oauth2, |
| 960 | 960 | }); |
| 961 | 961 | if (!connectionId) { |
| 962 | | − return yield* Effect.fail( |
| 963 | | − new Error(`OAuth configuration for "${op.sourceId}" is missing a connection binding`), |
| 964 | | − ); |
| 962 | + return yield* new OpenApiOAuthError({ |
| 963 | + message: `OAuth configuration for "${op.sourceId}" is missing a connection binding`, |
| 964 | + }); |
| 965 | 965 | } |
| 966 | | − const accessToken = yield* ctx.connections |
| 967 | | − .accessToken(connectionId) |
| 968 | | − .pipe( |
| 969 | | − Effect.mapError( |
| 970 | | − (err) => |
| 971 | | − new Error( |
| 972 | | − `OAuth connection resolution failed: ${ |
| 973 | | − "message" in err ? (err as { message: string }).message : String(err) |
| 974 | | − }`, |
| 975 | | − ), |
| 976 | | − ), |
| 977 | | − ); |
| 966 | + const accessToken = yield* ctx.connections.accessToken(connectionId).pipe( |
| 967 | + Effect.mapError( |
| 968 | + () => |
| 969 | + new OpenApiOAuthError({ |
| 970 | + message: "OAuth connection resolution failed", |
| 971 | + }), |
| 972 | + ), |
| 973 | + ); |
| 978 | 974 | resolvedHeaders.authorization = `Bearer ${accessToken}`; |
| 979 | 975 | } |
| 980 | 976 | |
| @@ -1000,7 +996,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 19 unmodified lines |
| 1000 | 996 | // and we don't fall through to the wrong scope's bindings. |
| 1001 | 997 | const scopes = new Set<string>(); |
| 1002 | 998 | for (const row of toolRows as readonly ToolRow[]) { |
| 1003 | | − scopes.add(row.scope_id as string); |
| 999 | + scopes.add(row.scope_id); |
| 1004 | 1000 | } |
| 1005 | 1001 | // One listOperationsBySource per scope is independent storage |
| 1006 | 1002 | // work; run them in parallel so a shadowed source doesn't |
| @@ -1021,7 +1017,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 14 unmodified lines |
| 1021 | 1017 | |
| 1022 | 1018 | const out: Record<string, ToolAnnotations> = {}; |
| 1023 | 1019 | for (const row of toolRows as readonly ToolRow[]) { |
| 1024 | | − const binding = byScope.get(row.scope_id as string)?.get(row.id); |
| 1020 | + const binding = byScope.get(row.scope_id)?.get(row.id); |
| 1025 | 1021 | if (binding) { |
| 1026 | 1022 | out[row.id] = annotationsForOperation(binding.method, binding.pathTemplate); |
| 1027 | 1023 | } |
| @@ -1041,9 +1037,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 13 unmodified lines |
| 1041 | 1037 | usagesForSecret: ({ ctx, args }) => |
| 1042 | 1038 | Effect.gen(function* () { |
| 1043 | 1039 | const bindings = yield* ctx.storage.findBindingsBySecret(args.secretId); |
| 1044 | | − const childRows = yield* ctx.storage.findChildRowsBySecret( |
| 1045 | | − args.secretId, |
| 1046 | | − ); |
| 1040 | + const childRows = yield* ctx.storage.findChildRowsBySecret(args.secretId); |
| 1047 | 1041 | |
| 1048 | 1042 | const sourceKeys = new Set<string>(); |
| 1049 | 1043 | for (const b of bindings) { |
| @@ -1062,8 +1056,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 12 unmodified lines |
| 1062 | 1056 | scopeId: ScopeId.make(b.scopeId), |
| 1063 | 1057 | ownerKind: "openapi-source-binding", |
| 1064 | 1058 | ownerId: b.sourceId, |
| 1065 | | − ownerName: |
| 1066 | | − sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null, |
| 1059 | + ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null, |
| 1067 | 1060 | slot: `binding:${b.slot}`, |
| 1068 | 1061 | }), |
| 1069 | 1062 | ); |
| @@ -1085,9 +1078,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 15 unmodified lines |
| 1085 | 1078 | |
| 1086 | 1079 | usagesForConnection: ({ ctx, args }) => |
| 1087 | 1080 | Effect.gen(function* () { |
| 1088 | | − const bindings = yield* ctx.storage.findBindingsByConnection( |
| 1089 | | − args.connectionId, |
| 1090 | | − ); |
| 1081 | + const bindings = yield* ctx.storage.findBindingsByConnection(args.connectionId); |
| 1091 | 1082 | const sourceKeys = new Set<string>(); |
| 1092 | 1083 | for (const b of bindings) { |
| 1093 | 1084 | sourceKeys.add(`${b.sourceScopeId}:${b.sourceId}`); |
| @@ -1100,8 +1091,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines |
| 1100 | 1091 | scopeId: ScopeId.make(b.scopeId), |
| 1101 | 1092 | ownerKind: "openapi-source-binding", |
| 1102 | 1093 | ownerId: b.sourceId, |
| 1103 | | − ownerName: |
| 1104 | | − sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null, |
| 1094 | + ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null, |
| 1105 | 1095 | slot: `binding:${b.slot}`, |
| 1106 | 1096 | }), |
| 1107 | 1097 | ); |