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
4040 const response = yield* client.execute(request).pipe(
4141 Effect.timeout(Duration.seconds(20)),
4242 Effect.mapError(
43− (cause) =>
43+ (_cause) =>
4444 new OpenApiParseError({
45− message: `Failed to fetch OpenAPI document: ${cause instanceof Error ? cause.message : String(cause)}`,
45+ message: "Failed to fetch OpenAPI document",
4646 }),
4747 ),
4848 );
@@ -53,9 +53,9 @@ export const fetchSpecText = Effect.fn("OpenApi.fetchSpecText")(function* ( · 4 unmodified lines
5353 }
5454 return yield* response.text.pipe(
5555 Effect.mapError(
56− (cause) =>
56+ (_cause) =>
5757 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",
5959 }),
6060 ),
6161 );
@@ -79,13 +79,7 @@ export const resolveSpecText = (input: string, credentials?: SpecFetchCredential · 17 unmodified lines
7979 * the 128MB Cloudflare Workers memory cap.
8080 */
8181 export const parse = Effect.fn("OpenApi.parse")(function* (text: string) {
82− const api = yield* Effect.try({
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− });
82+ const api = yield* parseTextToObject(text);
8983
9084 if (!isOpenApi3(api)) {
9185 return yield* new OpenApiExtractionErrorFromParse({
@@ -104,20 +98,28 @@ export const parse = Effect.fn("OpenApi.parse")(function* (text: string) { · 12 unmodified lines
10498 const isOpenApi3 = (doc: OpenAPI.Document): doc is OpenAPIV3.Document | OpenAPIV3_1.Document =>
10599 "openapi" in doc && typeof doc.openapi === "string" && doc.openapi.startsWith("3.");
106100
107−const parseTextToObject = (text: string): OpenAPI.Document => {
108− const trimmed = text.trim();
109− if (trimmed.length === 0) throw new Error("OpenAPI document is empty");
101+const parseTextToObject = (text: string): Effect.Effect<OpenAPI.Document, OpenApiParseError> =>
102+ Effect.gen(function* () {
103+ const trimmed = text.trim();
104+ if (trimmed.length === 0) {
105+ return yield* new OpenApiParseError({
106+ message: "OpenAPI document is empty",
107+ });
108+ }
110109
111− let parsed: unknown;
112− try {
113− parsed = JSON.parse(trimmed);
114− } catch {
115− parsed = YAML.parse(trimmed);
116− }
110+ const parsed = yield* Effect.try({
111+ try: () => YAML.parse(trimmed) as unknown,
112+ catch: () =>
113+ new OpenApiParseError({
114+ message: "Failed to parse OpenAPI document",
115+ }),
116+ });
117117
118− if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
119− throw new Error("OpenAPI document must parse to an object");
120− }
118+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
119+ return yield* new OpenApiParseError({
120+ message: "OpenAPI document must parse to an object",
121+ });
122+ }
121123
122− return parsed as OpenAPI.Document;
123−};
124+ return parsed as OpenAPI.Document;
125+ });
packages/plugins/openapi/src/sdk/plugin.ts+53−63
@@ -1,4 +1,4 @@
1−import { Effect, Option, Schema } from "effect";
1+import { Effect, Option, Predicate, Schema } from "effect";
22 import type { Layer } from "effect";
33 import { FetchHttpClient, HttpClient } from "effect/unstable/http";
44
@@ -376,13 +376,15 @@ const resolveEffectiveSourceConfig = ( · 371 unmodified lines
376376 base: StoredSource,
377377 ): Effect.Effect<EffectiveSourceConfig, StorageFailure> =>
378378 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+ );
380382 const baseRank = rank.get(base.scope) ?? Infinity;
381383 let fallback: StoredSource | null = null;
382384 for (let index = baseRank + 1; index < ctx.scopes.length; index++) {
383385 const scope = ctx.scopes[index];
384386 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);
386388 if (fallback) break;
387389 }
388390
@@ -435,13 +437,13 @@ const resolveConfiguredHeaders = ( · 46 unmodified lines
435437 value.slot,
436438 );
437439 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+ ),
445447 ),
446448 );
447449 if (secret === null) {
@@ -460,10 +462,8 @@ const resolveConfiguredHeaders = ( · 12 unmodified lines
460462 if (legacy) {
461463 const fallback = yield* resolveHeaders({ [name]: legacy }, ctx.secrets).pipe(
462464 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" })),
467467 ),
468468 );
469469 resolved[name] = fallback;
@@ -488,14 +488,14 @@ const resolveHeaderValues = ( · 18 unmodified lines
488488 message: `Secret not found for "${name}"`,
489489 }),
490490 onError: (err, name) =>
491− "_tag" in err && err._tag === "SecretOwnedByConnectionError"
491+ Predicate.isTagged("SecretOwnedByConnectionError")(err)
492492 ? new OpenApiOAuthError({
493493 message: `Secret not found for "${name}"`,
494494 })
495495 : err,
496496 }).pipe(
497497 Effect.mapError((err) =>
498− "_tag" in err && err._tag === "SecretOwnedByConnectionError"
498+ Predicate.isTagged("SecretOwnedByConnectionError")(err)
499499 ? new OpenApiOAuthError({ message: "Secret resolution failed" })
500500 : err,
501501 ),
@@ -518,7 +518,7 @@ const resolveOAuthConnectionId = ( · 16 unmodified lines
518518 params.oauth2.connectionSlot,
519519 );
520520 if (binding?.value.kind === "connection") {
521− const connectionId = binding.value.connectionId as string;
521+ const connectionId = binding.value.connectionId;
522522 const connection = yield* ctx.connections.get(connectionId);
523523 return connection ? connectionId : null;
524524 }
@@ -763,7 +763,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 238 unmodified lines
763763 const configFile = options?.configFile;
764764
765765 return {
766− previewSpec: (input) =>
766+ previewSpec: (input: string | OpenApiPreviewInput) =>
767767 Effect.gen(function* () {
768768 const previewInput = typeof input === "string" ? { spec: input } : input;
769769 const credentials = yield* resolveSpecFetchCredentials(
@@ -776,7 +776,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines
776776 return yield* previewSpec(specText).pipe(Effect.provide(httpClientLayer));
777777 }),
778778
779− addSpec: (config) =>
779+ addSpec: (config: OpenApiSpecConfig) =>
780780 Effect.gen(function* () {
781781 const result = yield* addSpecInternal(config);
782782 if (configFile) {
@@ -785,7 +785,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 2 unmodified lines
785785 return result;
786786 }),
787787
788− removeSpec: (namespace, scope) =>
788+ removeSpec: (namespace: string, scope: string) =>
789789 Effect.gen(function* () {
790790 yield* ctx.transaction(
791791 Effect.gen(function* () {
@@ -798,7 +798,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines
798798 }
799799 }),
800800
801− getSource: (namespace, scope) =>
801+ getSource: (namespace: string, scope: string) =>
802802 Effect.gen(function* () {
803803 const source = yield* ctx.storage.getSource(namespace, scope);
804804 if (!source) return null;
@@ -809,7 +809,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 4 unmodified lines
809809 };
810810 }),
811811
812− updateSource: (namespace, scope, input) =>
812+ updateSource: (namespace: string, scope: string, input: OpenApiUpdateSourceInput) =>
813813 Effect.gen(function* () {
814814 const existing = yield* ctx.storage.getSource(namespace, scope);
815815 if (!existing) return;
@@ -847,14 +847,14 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 31 unmodified lines
847847 }
848848 }),
849849
850− listSourceBindings: (sourceId, sourceScope) =>
850+ listSourceBindings: (sourceId: string, sourceScope: string) =>
851851 ctx.storage.listSourceBindings(sourceId, sourceScope),
852852
853− setSourceBinding: (input) => ctx.storage.setSourceBinding(input),
853+ setSourceBinding: (input: OpenApiSourceBindingInput) => ctx.storage.setSourceBinding(input),
854854
855− removeSourceBinding: (sourceId, sourceScope, slot, scope) =>
855+ removeSourceBinding: (sourceId: string, sourceScope: string, slot: string, scope: string) =>
856856 ctx.storage.removeSourceBinding(sourceId, sourceScope, slot, scope),
857− } satisfies OpenApiPluginExtension;
857+ };
858858 },
859859
860860 staticSources: (self) => [
@@ -909,7 +909,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 48 unmodified lines
909909 handler: ({ ctx, args }) =>
910910 self.addSpec({
911911 ...(args as AddSourceInput),
912− scope: ctx.scopes.at(-1)!.id as string,
912+ scope: ctx.scopes.at(-1)!.id,
913913 }),
914914 },
915915 ],
@@ -923,16 +923,18 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 7 unmodified lines
923923 // openapi_operation + openapi_source rows live at the same
924924 // scope, so pin every store lookup to it instead of relying
925925 // on the scoped adapter's stack-wide fall-through.
926− const toolScope = toolRow.scope_id as string;
926+ const toolScope = toolRow.scope_id;
927927 const op = yield* ctx.storage.getOperationByToolId(toolRow.id, toolScope);
928928 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+ });
932932 }
933933 const source = yield* ctx.storage.getSource(op.sourceId, toolScope);
934934 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+ });
936938 }
937939
938940 const effective = yield* resolveEffectiveSourceConfig(ctx, source);
@@ -942,10 +944,8 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 3 unmodified lines
942944 sourceScope: effective.headersSource.scope,
943945 headers: config.headers ?? {},
944946 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);
949949
950950 // If the source has OAuth2 auth, resolve a guaranteed-fresh
951951 // access token from the backing Connection and inject the
@@ -959,22 +959,18 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 7 unmodified lines
959959 legacyOAuth2: effective.oauth2Source.legacy?.oauth2,
960960 });
961961 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+ });
965965 }
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+ );
978974 resolvedHeaders.authorization = `Bearer ${accessToken}`;
979975 }
980976
@@ -1000,7 +996,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 19 unmodified lines
1000996 // and we don't fall through to the wrong scope's bindings.
1001997 const scopes = new Set<string>();
1002998 for (const row of toolRows as readonly ToolRow[]) {
1003− scopes.add(row.scope_id as string);
999+ scopes.add(row.scope_id);
10041000 }
10051001 // One listOperationsBySource per scope is independent storage
10061002 // work; run them in parallel so a shadowed source doesn't
@@ -1021,7 +1017,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 14 unmodified lines
10211017
10221018 const out: Record<string, ToolAnnotations> = {};
10231019 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);
10251021 if (binding) {
10261022 out[row.id] = annotationsForOperation(binding.method, binding.pathTemplate);
10271023 }
@@ -1041,9 +1037,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 13 unmodified lines
10411037 usagesForSecret: ({ ctx, args }) =>
10421038 Effect.gen(function* () {
10431039 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);
10471041
10481042 const sourceKeys = new Set<string>();
10491043 for (const b of bindings) {
@@ -1062,8 +1056,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 12 unmodified lines
10621056 scopeId: ScopeId.make(b.scopeId),
10631057 ownerKind: "openapi-source-binding",
10641058 ownerId: b.sourceId,
1065− ownerName:
1066− sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1059+ ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
10671060 slot: `binding:${b.slot}`,
10681061 }),
10691062 );
@@ -1085,9 +1078,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 15 unmodified lines
10851078
10861079 usagesForConnection: ({ ctx, args }) =>
10871080 Effect.gen(function* () {
1088− const bindings = yield* ctx.storage.findBindingsByConnection(
1089− args.connectionId,
1090− );
1081+ const bindings = yield* ctx.storage.findBindingsByConnection(args.connectionId);
10911082 const sourceKeys = new Set<string>();
10921083 for (const b of bindings) {
10931084 sourceKeys.add(`${b.sourceScopeId}:${b.sourceId}`);
@@ -1100,8 +1091,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 6 unmodified lines
11001091 scopeId: ScopeId.make(b.scopeId),
11011092 ownerKind: "openapi-source-binding",
11021093 ownerId: b.sourceId,
1103− ownerName:
1104− sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
1094+ ownerName: sources.get(`${b.sourceScopeId}:${b.sourceId}`) ?? null,
11051095 slot: `binding:${b.slot}`,
11061096 }),
11071097 );

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