History

Merge remote-tracking branch 'origin/codex/batch16-openapi-plugin-boundaries' into codex/effect-lint-integration

Rhys Sullivan committed 6 months ago

Commit f887e4c2 parents d516810 4256b952 files · +80 −88Browse files

Unified · Split

2 files
  1. packages/plugins/openapi/src/sdk/parse.ts +27 −25
  2. packages/plugins/openapi/src/sdk/plugin.ts +53 −63
packages/plugins/openapi/src/sdk/parse.ts+27−25
@@ -40,9 +40,9 @@ export const fetchSpecText = Effect.fn("OpenApi.fetchSpecText")(function* ( · 39 unmodified lines
40 const response = yield* client.execute(request).pipe(40 const response = yield* client.execute(request).pipe(
41 Effect.timeout(Duration.seconds(20)),41 Effect.timeout(Duration.seconds(20)),
42 Effect.mapError(42 Effect.mapError(
43 (cause) =>43 (_cause) =>
44 new OpenApiParseError({44 new OpenApiParseError({
45 message: `Failed to fetch OpenAPI document: ${cause instanceof Error ? cause.message : String(cause)}`,45 message: "Failed to fetch OpenAPI document",
46 }),46 }),
47 ),47 ),
48 );48 );
@@ -53,9 +53,9 @@ export const fetchSpecText = Effect.fn("OpenApi.fetchSpecText")(function* ( · 4 unmodified lines
53 }53 }
54 return yield* response.text.pipe(54 return yield* response.text.pipe(
55 Effect.mapError(55 Effect.mapError(
56 (cause) =>56 (_cause) =>
57 new OpenApiParseError({57 new OpenApiParseError({
58 message: `Failed to read OpenAPI document body: ${cause instanceof Error ? cause.message : String(cause)}`,58 message: "Failed to read OpenAPI document body",
59 }),59 }),
60 ),60 ),
61 );61 );
@@ -79,13 +79,7 @@ export const resolveSpecText = (input: string, credentials?: SpecFetchCredential · 17 unmodified lines
79 * the 128MB Cloudflare Workers memory cap.79 * the 128MB Cloudflare Workers memory cap.
80 */80 */
81export const parse = Effect.fn("OpenApi.parse")(function* (text: string) {81export const parse = Effect.fn("OpenApi.parse")(function* (text: string) {
82 const api = yield* Effect.try({82 const api = yield* parseTextToObject(text);
83 try: () => parseTextToObject(text),
84 catch: (error) =>
85 new OpenApiParseError({
86 message: `Failed to parse OpenAPI document: ${error instanceof Error ? error.message : String(error)}`,
87 }),
88 });
89 83
90 if (!isOpenApi3(api)) {84 if (!isOpenApi3(api)) {
91 return yield* new OpenApiExtractionErrorFromParse({85 return yield* new OpenApiExtractionErrorFromParse({
@@ -104,20 +98,28 @@ export const parse = Effect.fn("OpenApi.parse")(function* (text: string) { · 12 unmodified lines
104const isOpenApi3 = (doc: OpenAPI.Document): doc is OpenAPIV3.Document | OpenAPIV3_1.Document =>98const isOpenApi3 = (doc: OpenAPI.Document): doc is OpenAPIV3.Document | OpenAPIV3_1.Document =>
105 "openapi" in doc && typeof doc.openapi === "string" && doc.openapi.startsWith("3.");99 "openapi" in doc && typeof doc.openapi === "string" && doc.openapi.startsWith("3.");
106 100
107const parseTextToObject = (text: string): OpenAPI.Document => {101const parseTextToObject = (text: string): Effect.Effect<OpenAPI.Document, OpenApiParseError> =>
108 const trimmed = text.trim();102 Effect.gen(function* () {
109 if (trimmed.length === 0) throw new Error("OpenAPI document is empty");103 const trimmed = text.trim();
104 if (trimmed.length === 0) {
105 return yield* new OpenApiParseError({
106 message: "OpenAPI document is empty",
107 });
108 }
110 109
111 let parsed: unknown;110 const parsed = yield* Effect.try({
112 try {111 try: () => YAML.parse(trimmed) as unknown,
113 parsed = JSON.parse(trimmed);112 catch: () =>
114 } catch {113 new OpenApiParseError({
115 parsed = YAML.parse(trimmed);114 message: "Failed to parse OpenAPI document",
116 }115 }),
116 });
117 117
118 if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {118 if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
119 throw new Error("OpenAPI document must parse to an object");119 return yield* new OpenApiParseError({
120 }120 message: "OpenAPI document must parse to an object",
121 });
122 }
121 123
122 return parsed as OpenAPI.Document;124 return parsed as OpenAPI.Document;
123};125 });
packages/plugins/openapi/src/sdk/plugin.ts+53−63
@@ -1,4 +1,4 @@
1import { Effect, Option, Schema } from "effect";1import { Effect, Option, Predicate, Schema } from "effect";
2import type { Layer } from "effect";2import type { Layer } from "effect";
3import { FetchHttpClient, HttpClient } from "effect/unstable/http";3import { FetchHttpClient, HttpClient } from "effect/unstable/http";
4 4
@@ -376,13 +376,15 @@ const resolveEffectiveSourceConfig = ( · 371 unmodified lines
376 base: StoredSource,376 base: StoredSource,
377): Effect.Effect<EffectiveSourceConfig, StorageFailure> =>377): Effect.Effect<EffectiveSourceConfig, StorageFailure> =>
378 Effect.gen(function* () {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 const baseRank = rank.get(base.scope) ?? Infinity;382 const baseRank = rank.get(base.scope) ?? Infinity;
381 let fallback: StoredSource | null = null;383 let fallback: StoredSource | null = null;
382 for (let index = baseRank + 1; index < ctx.scopes.length; index++) {384 for (let index = baseRank + 1; index < ctx.scopes.length; index++) {
383 const scope = ctx.scopes[index];385 const scope = ctx.scopes[index];
384 if (!scope) continue;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 if (fallback) break;388 if (fallback) break;
387 }389 }
388 390
@@ -435,13 +437,13 @@ const resolveConfiguredHeaders = ( · 46 unmodified lines
435 value.slot,437 value.slot,
436 );438 );
437 if (binding?.value.kind === "secret") {439 if (binding?.value.kind === "secret") {
438 const secret = yield* ctx.secrets.get(binding.value.secretId as string).pipe(440 const secret = yield* ctx.secrets.get(binding.value.secretId).pipe(
439 Effect.mapError((err) =>441 Effect.catchTag("SecretOwnedByConnectionError", () =>
440 "_tag" in err && err._tag === "SecretOwnedByConnectionError"442 Effect.fail(
441 ? new OpenApiOAuthError({443 new OpenApiOAuthError({
442 message: `Secret not found for header "${name}"`,444 message: `Secret not found for header "${name}"`,
443 })445 }),
444 : err,446 ),
445 ),447 ),
446 );448 );
447 if (secret === null) {449 if (secret === null) {
@@ -460,10 +462,8 @@ const resolveConfiguredHeaders = ( · 12 unmodified lines
460 if (legacy) {462 if (legacy) {
461 const fallback = yield* resolveHeaders({ [name]: legacy }, ctx.secrets).pipe(463 const fallback = yield* resolveHeaders({ [name]: legacy }, ctx.secrets).pipe(
462 Effect.map((headers) => headers[name]!),464 Effect.map((headers) => headers[name]!),
463 Effect.mapError((err) =>465 Effect.catchTag("OpenApiInvocationError", () =>
464 err instanceof OpenApiOAuthError466 Effect.fail(new OpenApiOAuthError({ message: "Secret resolution failed" })),
465 ? err
466 : new OpenApiOAuthError({ message: err.message }),
467 ),467 ),
468 );468 );
469 resolved[name] = fallback;469 resolved[name] = fallback;
@@ -488,14 +488,14 @@ const resolveHeaderValues = ( · 18 unmodified lines
488 message: `Secret not found for "${name}"`,488 message: `Secret not found for "${name}"`,
489 }),489 }),
490 onError: (err, name) =>490 onError: (err, name) =>
491 "_tag" in err && err._tag === "SecretOwnedByConnectionError"491 Predicate.isTagged("SecretOwnedByConnectionError")(err)
492 ? new OpenApiOAuthError({492 ? new OpenApiOAuthError({
493 message: `Secret not found for "${name}"`,493 message: `Secret not found for "${name}"`,
494 })494 })
495 : err,495 : err,
496 }).pipe(496 }).pipe(
497 Effect.mapError((err) =>497 Effect.mapError((err) =>
498 "_tag" in err && err._tag === "SecretOwnedByConnectionError"498 Predicate.isTagged("SecretOwnedByConnectionError")(err)
499 ? new OpenApiOAuthError({ message: "Secret resolution failed" })499 ? new OpenApiOAuthError({ message: "Secret resolution failed" })
500 : err,500 : err,
501 ),501 ),
@@ -518,7 +518,7 @@ const resolveOAuthConnectionId = ( · 16 unmodified lines
518 params.oauth2.connectionSlot,518 params.oauth2.connectionSlot,
519 );519 );
520 if (binding?.value.kind === "connection") {520 if (binding?.value.kind === "connection") {
521 const connectionId = binding.value.connectionId as string;521 const connectionId = binding.value.connectionId;
522 const connection = yield* ctx.connections.get(connectionId);522 const connection = yield* ctx.connections.get(connectionId);
523 return connection ? connectionId : null;523 return connection ? connectionId : null;
524 }524 }
@@ -763,7 +763,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 238 unmodified lines
763 const configFile = options?.configFile;763 const configFile = options?.configFile;
764 764
765 return {765 return {
766 previewSpec: (input) =>766 previewSpec: (input: string | OpenApiPreviewInput) =>
767 Effect.gen(function* () {767 Effect.gen(function* () {
768 const previewInput = typeof input === "string" ? { spec: input } : input;768 const previewInput = typeof input === "string" ? { spec: input } : input;
769 const credentials = yield* resolveSpecFetchCredentials(769 const credentials = yield* resolveSpecFetchCredentials(
@@ -776,7 +776,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines
776 return yield* previewSpec(specText).pipe(Effect.provide(httpClientLayer));776 return yield* previewSpec(specText).pipe(Effect.provide(httpClientLayer));
777 }),777 }),
778 778
779 addSpec: (config) =>779 addSpec: (config: OpenApiSpecConfig) =>
780 Effect.gen(function* () {780 Effect.gen(function* () {
781 const result = yield* addSpecInternal(config);781 const result = yield* addSpecInternal(config);
782 if (configFile) {782 if (configFile) {
@@ -785,7 +785,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 2 unmodified lines
785 return result;785 return result;
786 }),786 }),
787 787
788 removeSpec: (namespace, scope) =>788 removeSpec: (namespace: string, scope: string) =>
789 Effect.gen(function* () {789 Effect.gen(function* () {
790 yield* ctx.transaction(790 yield* ctx.transaction(
791 Effect.gen(function* () {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 Effect.gen(function* () {802 Effect.gen(function* () {
803 const source = yield* ctx.storage.getSource(namespace, scope);803 const source = yield* ctx.storage.getSource(namespace, scope);
804 if (!source) return null;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 Effect.gen(function* () {813 Effect.gen(function* () {
814 const existing = yield* ctx.storage.getSource(namespace, scope);814 const existing = yield* ctx.storage.getSource(namespace, scope);
815 if (!existing) return;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 ctx.storage.listSourceBindings(sourceId, sourceScope),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 ctx.storage.removeSourceBinding(sourceId, sourceScope, slot, scope),856 ctx.storage.removeSourceBinding(sourceId, sourceScope, slot, scope),
857 } satisfies OpenApiPluginExtension;857 };
858 },858 },
859 859
860 staticSources: (self) => [860 staticSources: (self) => [
@@ -909,7 +909,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 48 unmodified lines
909 handler: ({ ctx, args }) =>909 handler: ({ ctx, args }) =>
910 self.addSpec({910 self.addSpec({
911 ...(args as AddSourceInput),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 // openapi_operation + openapi_source rows live at the same923 // openapi_operation + openapi_source rows live at the same
924 // scope, so pin every store lookup to it instead of relying924 // scope, so pin every store lookup to it instead of relying
925 // on the scoped adapter's stack-wide fall-through.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 const op = yield* ctx.storage.getOperationByToolId(toolRow.id, toolScope);927 const op = yield* ctx.storage.getOperationByToolId(toolRow.id, toolScope);
928 if (!op) {928 if (!op) {
929 return yield* Effect.fail(929 return yield* new OpenApiExtractionError({
930 new Error(`No OpenAPI operation found for tool "${toolRow.id}"`),930 message: `No OpenAPI operation found for tool "${toolRow.id}"`,
931 );931 });
932 }932 }
933 const source = yield* ctx.storage.getSource(op.sourceId, toolScope);933 const source = yield* ctx.storage.getSource(op.sourceId, toolScope);
934 if (!source) {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 const effective = yield* resolveEffectiveSourceConfig(ctx, source);940 const effective = yield* resolveEffectiveSourceConfig(ctx, source);
@@ -942,10 +944,8 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 3 unmodified lines
942 sourceScope: effective.headersSource.scope,944 sourceScope: effective.headersSource.scope,
943 headers: config.headers ?? {},945 headers: config.headers ?? {},
944 legacyHeaders: effective.headersSource.legacy?.headers,946 legacyHeaders: effective.headersSource.legacy?.headers,
945 }).pipe(Effect.mapError((err) => new Error(err.message)));947 });
946 const resolvedQueryParams = yield* resolveHeaderValues(ctx, config.queryParams).pipe(948 const resolvedQueryParams = yield* resolveHeaderValues(ctx, config.queryParams);
947 Effect.mapError((err) => new Error(err.message)),
948 );
949 949
950 // If the source has OAuth2 auth, resolve a guaranteed-fresh950 // If the source has OAuth2 auth, resolve a guaranteed-fresh
951 // access token from the backing Connection and inject the951 // access token from the backing Connection and inject the
@@ -959,22 +959,18 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 7 unmodified lines
959 legacyOAuth2: effective.oauth2Source.legacy?.oauth2,959 legacyOAuth2: effective.oauth2Source.legacy?.oauth2,
960 });960 });
961 if (!connectionId) {961 if (!connectionId) {
962 return yield* Effect.fail(962 return yield* new OpenApiOAuthError({
963 new Error(`OAuth configuration for "${op.sourceId}" is missing a connection binding`),963 message: `OAuth configuration for "${op.sourceId}" is missing a connection binding`,
964 );964 });
965 }965 }
966 const accessToken = yield* ctx.connections966 const accessToken = yield* ctx.connections.accessToken(connectionId).pipe(
967 .accessToken(connectionId)967 Effect.mapError(
968 .pipe(968 () =>
969 Effect.mapError(969 new OpenApiOAuthError({
970 (err) =>970 message: "OAuth connection resolution failed",
971 new Error(971 }),
972 `OAuth connection resolution failed: ${972 ),
973 "message" in err ? (err as { message: string }).message : String(err)973 );
974 }`,
975 ),
976 ),
977 );
978 resolvedHeaders.authorization = `Bearer ${accessToken}`;974 resolvedHeaders.authorization = `Bearer ${accessToken}`;
979 }975 }
980 976
@@ -1000,7 +996,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 19 unmodified lines
1000 // and we don't fall through to the wrong scope's bindings.996 // and we don't fall through to the wrong scope's bindings.
1001 const scopes = new Set<string>();997 const scopes = new Set<string>();
1002 for (const row of toolRows as readonly ToolRow[]) {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 // One listOperationsBySource per scope is independent storage1001 // One listOperationsBySource per scope is independent storage
1006 // work; run them in parallel so a shadowed source doesn't1002 // 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 const out: Record<string, ToolAnnotations> = {};1018 const out: Record<string, ToolAnnotations> = {};
1023 for (const row of toolRows as readonly ToolRow[]) {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 if (binding) {1021 if (binding) {
1026 out[row.id] = annotationsForOperation(binding.method, binding.pathTemplate);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 usagesForSecret: ({ ctx, args }) =>1037 usagesForSecret: ({ ctx, args }) =>
1042 Effect.gen(function* () {1038 Effect.gen(function* () {
1043 const bindings = yield* ctx.storage.findBindingsBySecret(args.secretId);1039 const bindings = yield* ctx.storage.findBindingsBySecret(args.secretId);
1044 const childRows = yield* ctx.storage.findChildRowsBySecret(1040 const childRows = yield* ctx.storage.findChildRowsBySecret(args.secretId);
1045 args.secretId,
1046 );
1047 1041
1048 const sourceKeys = new Set<string>();1042 const sourceKeys = new Set<string>();
1049 for (const b of bindings) {1043 for (const b of bindings) {
@@ -1062,8 +1056,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 12 unmodified lines
1062 scopeId: ScopeId.make(b.scopeId),1056 scopeId: ScopeId.make(b.scopeId),
1063 ownerKind: "openapi-source-binding",1057 ownerKind: "openapi-source-binding",
1064 ownerId: b.sourceId,1058 ownerId: b.sourceId,
1065 ownerName:1059 ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1066 sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1067 slot: `binding:${b.slot}`,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 usagesForConnection: ({ ctx, args }) =>1079 usagesForConnection: ({ ctx, args }) =>
1087 Effect.gen(function* () {1080 Effect.gen(function* () {
1088 const bindings = yield* ctx.storage.findBindingsByConnection(1081 const bindings = yield* ctx.storage.findBindingsByConnection(args.connectionId);
1089 args.connectionId,
1090 );
1091 const sourceKeys = new Set<string>();1082 const sourceKeys = new Set<string>();
1092 for (const b of bindings) {1083 for (const b of bindings) {
1093 sourceKeys.add(`${b.sourceScopeId}:${b.sourceId}`);1084 sourceKeys.add(`${b.sourceScopeId}:${b.sourceId}`);
@@ -1100,8 +1091,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines
1100 scopeId: ScopeId.make(b.scopeId),1091 scopeId: ScopeId.make(b.scopeId),
1101 ownerKind: "openapi-source-binding",1092 ownerKind: "openapi-source-binding",
1102 ownerId: b.sourceId,1093 ownerId: b.sourceId,
1103 ownerName:1094 ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1104 sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1105 slot: `binding:${b.slot}`,1095 slot: `binding:${b.slot}`,
1106 }),1096 }),
1107 );1097 );

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t