-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathplugin.ts
More file actions
1209 lines (1126 loc) · 48.8 KB
/
Copy pathplugin.ts
File metadata and controls
1209 lines (1126 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Effect, Option, Schema } from "effect";
import type { Layer } from "effect";
import { HttpClient } from "effect/unstable/http";
import {
IntegrationAlreadyExistsError,
IntegrationDetectionResult,
IntegrationSlug,
ToolName,
ToolResult,
authToolFailure,
definePlugin,
mergeAuthTemplates,
sha256Hex,
tool,
type AuthMethodDescriptor,
type Integration,
type IntegrationConfig,
type IntegrationRecord,
type PluginCtx,
type ResolveToolsResult,
type StorageFailure,
type ToolDef,
type ToolInvocationCredential,
} from "@executor-js/sdk/core";
import {
decodeOpenApiIntegrationConfig,
renderAuthTemplate,
requiredTemplateVariables,
type OpenApiIntegrationConfig,
} from "./config";
import { OpenApiExtractionError, OpenApiOAuthError, OpenApiParseError } from "./errors";
import { parse, resolveSpecText } from "./parse";
import {
convertGoogleDiscoveryBundleToOpenApi,
convertGoogleDiscoveryToOpenApi,
fetchGoogleDiscoveryDocument,
isGoogleDiscoveryUrl,
} from "./google-discovery";
import { extract } from "./extract";
import { compileToolDefinitions, type ToolDefinition } from "./definitions";
import { annotationsForOperation, invokeWithLayer } from "./invoke";
import { previewSpec, previewSpecText, type SpecPreview } from "./preview";
import { deriveAuthenticationTemplateFromPreview, firstBaseUrlForPreview } from "./derive-auth";
import { openApiPresets } from "./presets";
import { makeDefaultOpenapiStore, type OpenapiStore, type StoredOperation } from "./store";
import type { Authentication } from "./types";
import { OperationBinding, normalizeOpenApiAuthInputs, type AuthenticationInput } from "./types";
import { ApiKeyAuthTemplate, describeApiKeyAuthMethod } from "@executor-js/sdk/http-auth";
// ---------------------------------------------------------------------------
// Plugin config
// ---------------------------------------------------------------------------
const STRINGIFIED_BODY_CAP = 1024;
const UpstreamMessageBody = Schema.Struct({ message: Schema.String });
const UpstreamErrorMessageBody = Schema.Struct({ errorMessage: Schema.String });
const UpstreamNestedErrorBody = Schema.Struct({ error: UpstreamMessageBody });
const UpstreamErrorsArrayBody = Schema.Struct({
errors: Schema.Array(
Schema.Struct({
detail: Schema.optional(Schema.String),
message: Schema.optional(Schema.String),
title: Schema.optional(Schema.String),
}),
),
});
const UpstreamDescriptionBody = Schema.Struct({
detail: Schema.optional(Schema.String),
title: Schema.optional(Schema.String),
description: Schema.optional(Schema.String),
});
const decodeUpstreamMessageBody = Schema.decodeUnknownOption(UpstreamMessageBody);
const decodeUpstreamErrorMessageBody = Schema.decodeUnknownOption(UpstreamErrorMessageBody);
const decodeUpstreamNestedErrorBody = Schema.decodeUnknownOption(UpstreamNestedErrorBody);
const decodeUpstreamErrorsArrayBody = Schema.decodeUnknownOption(UpstreamErrorsArrayBody);
const decodeUpstreamDescriptionBody = Schema.decodeUnknownOption(UpstreamDescriptionBody);
const clampedStringify = (value: unknown): string => {
let s: string;
// oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: JSON.stringify may throw on cycles; fall back to String() so the upstream body can still be surfaced as ToolError.details fallback text
try {
s = JSON.stringify(value);
} catch {
s = String(value);
}
return s.length > STRINGIFIED_BODY_CAP ? `${s.slice(0, STRINGIFIED_BODY_CAP)}…` : s;
};
const firstNonEmpty = (...values: readonly (string | undefined)[]): string | undefined =>
values.find((value) => value !== undefined && value.length > 0);
// Walk known upstream error-body shapes so ToolError.message stays concise
// while ToolError.details preserves the original body.
const extractUpstreamMessage = (body: unknown, status: number): string => {
if (typeof body === "string") {
return body.length > 0 ? body : `Upstream returned HTTP ${status}`;
}
const nested = Option.getOrUndefined(decodeUpstreamNestedErrorBody(body));
const messageBody = Option.getOrUndefined(decodeUpstreamMessageBody(body));
const errorMessageBody = Option.getOrUndefined(decodeUpstreamErrorMessageBody(body));
const errorsBody = Option.getOrUndefined(decodeUpstreamErrorsArrayBody(body));
const descriptionBody = Option.getOrUndefined(decodeUpstreamDescriptionBody(body));
const arrayMessage = errorsBody?.errors
.map(
({
detail,
message: upstreamMessage,
title,
}: {
detail?: string;
message?: string;
title?: string;
}) => firstNonEmpty(detail, upstreamMessage, title),
)
.find((message: string | undefined) => message !== undefined);
const message = firstNonEmpty(
nested?.error.message,
messageBody?.message,
errorMessageBody?.errorMessage,
arrayMessage,
descriptionBody?.detail,
descriptionBody?.title,
descriptionBody?.description,
);
if (message !== undefined) return message;
if (body !== null && typeof body === "object") {
return clampedStringify(body);
}
return `Upstream returned HTTP ${status}`;
};
// ---------------------------------------------------------------------------
// Extension input shapes
// ---------------------------------------------------------------------------
export type OpenApiSpecInput = typeof OpenApiSpecInputSchema.Type;
export interface OpenApiPreviewInput {
readonly spec: string;
}
/** Add an OpenAPI integration to the catalog. The integration is the API
* surface; connections (the credentials) are attached separately and resolve
* their value through the declared `authenticationTemplate`. */
export interface OpenApiSpecConfig {
readonly spec: OpenApiSpecInput;
/** The catalog slug for the new integration (the `<integration>` segment). */
readonly slug: string;
/** Human description (defaults to the spec title). */
readonly description?: string;
readonly baseUrl?: string;
/** Static headers applied to every request (no secret material). */
readonly headers?: Record<string, string>;
/** Static query params applied to every request. */
readonly queryParams?: Record<string, string>;
/** Auth methods a connection's value renders through — canonical
* placements or the request-shaped authoring dialect. */
readonly authenticationTemplate?: readonly AuthenticationInput[];
}
export interface OpenApiExtensionFailure {
readonly _tag: string;
}
/** Add / merge custom auth methods onto an existing OpenAPI integration's
* `authenticationTemplate`. Mirrors the GraphQL plugin's `configure`. */
export interface OpenApiConfigureInput {
/** The auth methods to add. Each entry is appended to (or, when its `slug`
* already exists, replaces) the integration's existing template array. A
* custom apiKey method with no `slug` is assigned a generated `custom_<id>`
* slug that is collision-checked against the existing template. */
readonly authenticationTemplate: readonly AuthenticationInput[];
readonly mode?: "merge" | "replace";
}
export interface OpenApiPluginExtension {
readonly previewSpec: (
input: string | OpenApiPreviewInput,
) => Effect.Effect<
SpecPreview,
OpenApiParseError | OpenApiExtractionError | OpenApiOAuthError | StorageFailure
>;
readonly addSpec: (
config: OpenApiSpecConfig,
) => Effect.Effect<
{ readonly slug: IntegrationSlug; readonly toolCount: number },
| OpenApiParseError
| OpenApiExtractionError
| OpenApiOAuthError
| IntegrationAlreadyExistsError
| StorageFailure
>;
readonly removeSpec: (slug: string) => Effect.Effect<void, StorageFailure>;
readonly getIntegration: (slug: string) => Effect.Effect<Integration | null, StorageFailure>;
/** Read the integration's full opaque config, including its
* `authenticationTemplate`. Returns null when the integration is absent. */
readonly getConfig: (
slug: string,
) => Effect.Effect<OpenApiIntegrationConfig | null, StorageFailure>;
/** Add / merge custom auth methods onto the integration's
* `authenticationTemplate`. Returns the resulting template array. */
readonly configure: (
slug: string,
input: OpenApiConfigureInput,
) => Effect.Effect<readonly Authentication[], StorageFailure>;
}
// ---------------------------------------------------------------------------
// Control-tool input/output schemas
// ---------------------------------------------------------------------------
const PreviewSpecInputSchema = Schema.Struct({
spec: Schema.String,
});
const StaticPreviewServerVariableSchema = Schema.Struct({
default: Schema.String,
enum: Schema.NullOr(Schema.Array(Schema.String)),
description: Schema.NullOr(Schema.String),
});
const StaticPreviewServerSchema = Schema.Struct({
url: Schema.String,
description: Schema.NullOr(Schema.String),
variables: Schema.NullOr(Schema.Record(Schema.String, StaticPreviewServerVariableSchema)),
});
const StaticPreviewOAuthAuthorizationCodeFlowSchema = Schema.Struct({
authorizationUrl: Schema.String,
tokenUrl: Schema.String,
refreshUrl: Schema.NullOr(Schema.String),
scopes: Schema.Record(Schema.String, Schema.String),
});
const StaticPreviewOAuthClientCredentialsFlowSchema = Schema.Struct({
tokenUrl: Schema.String,
refreshUrl: Schema.NullOr(Schema.String),
scopes: Schema.Record(Schema.String, Schema.String),
});
const StaticPreviewOAuthFlowsSchema = Schema.Struct({
authorizationCode: Schema.NullOr(StaticPreviewOAuthAuthorizationCodeFlowSchema),
clientCredentials: Schema.NullOr(StaticPreviewOAuthClientCredentialsFlowSchema),
});
const StaticPreviewSecuritySchemeSchema = Schema.Struct({
name: Schema.String,
type: Schema.Literals(["http", "apiKey", "oauth2", "openIdConnect"]),
scheme: Schema.NullOr(Schema.String),
bearerFormat: Schema.NullOr(Schema.String),
in: Schema.NullOr(Schema.Literals(["header", "query", "cookie"])),
headerName: Schema.NullOr(Schema.String),
description: Schema.NullOr(Schema.String),
flows: Schema.NullOr(StaticPreviewOAuthFlowsSchema),
openIdConnectUrl: Schema.NullOr(Schema.String),
});
const StaticPreviewOAuth2PresetSchema = Schema.Struct({
label: Schema.String,
securitySchemeName: Schema.String,
flow: Schema.Literals(["authorizationCode", "clientCredentials"]),
authorizationUrl: Schema.NullOr(Schema.String),
tokenUrl: Schema.String,
refreshUrl: Schema.NullOr(Schema.String),
scopes: Schema.Record(Schema.String, Schema.String),
identityScopes: Schema.Union([
Schema.Literal("auto"),
Schema.Literal(false),
Schema.Array(Schema.String),
]),
});
const StaticPreviewSpecOutputSchema = Schema.Struct({
title: Schema.NullOr(Schema.String),
version: Schema.NullOr(Schema.String),
servers: Schema.Array(StaticPreviewServerSchema),
operationCount: Schema.Number,
tags: Schema.Array(Schema.String),
securitySchemes: Schema.Array(StaticPreviewSecuritySchemeSchema),
authStrategies: Schema.Array(Schema.Struct({ schemes: Schema.Array(Schema.String) })),
headerPresets: Schema.Array(
Schema.Struct({
label: Schema.String,
headers: Schema.Record(Schema.String, Schema.NullOr(Schema.String)),
secretHeaders: Schema.Array(Schema.String),
}),
),
oauth2Presets: Schema.Array(StaticPreviewOAuth2PresetSchema),
});
type StaticPreviewSpecOutput = typeof StaticPreviewSpecOutputSchema.Type;
const OpenApiSpecInputSchema = Schema.Union([
Schema.Struct({ kind: Schema.Literal("url"), url: Schema.String }),
Schema.Struct({ kind: Schema.Literal("blob"), value: Schema.String }),
Schema.Struct({
kind: Schema.Literal("googleDiscovery"),
url: Schema.String,
}),
Schema.Struct({
kind: Schema.Literal("googleDiscoveryBundle"),
urls: Schema.Array(Schema.String),
}),
]);
const AuthenticationSchema = Schema.Union([
Schema.Struct({
slug: Schema.String,
kind: Schema.Literal("oauth2"),
authorizationUrl: Schema.String,
tokenUrl: Schema.String,
scopes: Schema.Array(Schema.String),
}),
// Credential methods are authored request-shaped — the ONE apikey input
// dialect: `{ type: "apiKey", headers: { Authorization: ["Bearer ",
// variable("token")] }, queryParams: { … } }`.
ApiKeyAuthTemplate,
]);
const AddSourceInputSchema = Schema.Struct({
spec: OpenApiSpecInputSchema,
slug: Schema.String,
description: Schema.optional(Schema.String),
baseUrl: Schema.optional(Schema.String),
headers: Schema.optional(Schema.Record(Schema.String, Schema.String)),
queryParams: Schema.optional(Schema.Record(Schema.String, Schema.String)),
authenticationTemplate: Schema.optional(Schema.Array(AuthenticationSchema)),
});
const AddSourceOutputSchema = Schema.Struct({
slug: Schema.String,
toolCount: Schema.Number,
});
const PreviewSpecInputStandardSchema = Schema.toStandardSchemaV1(
Schema.toStandardJSONSchemaV1(PreviewSpecInputSchema),
);
const PreviewSpecOutputStandardSchema = Schema.toStandardSchemaV1(
Schema.toStandardJSONSchemaV1(StaticPreviewSpecOutputSchema),
);
const AddSourceInputStandardSchema = Schema.toStandardSchemaV1(
Schema.toStandardJSONSchemaV1(AddSourceInputSchema),
);
const AddSourceOutputStandardSchema = Schema.toStandardSchemaV1(
Schema.toStandardJSONSchemaV1(AddSourceOutputSchema),
);
const openApiToolFailure = (code: string, message: string, details?: unknown) =>
ToolResult.fail({
code,
message,
...(details === undefined ? {} : { details }),
});
const openApiAuthToolFailure = (failure: {
readonly code: string;
readonly message: string;
readonly owner: "org" | "user";
readonly integration: string;
readonly connection: string;
readonly credentialKind: "secret" | "oauth" | "upstream";
readonly credentialLabel?: string;
readonly status?: number;
readonly details?: unknown;
}) =>
authToolFailure({
// The auth-tool-failure helper's code set is shared with v1; keep the
// string but reference the connection rather than v1's source/slot.
code: failure.code as Parameters<typeof authToolFailure>[0]["code"],
message: failure.message,
source: { id: failure.integration, scope: failure.owner },
credential: {
kind: failure.credentialKind,
...(failure.credentialLabel ? { label: failure.credentialLabel } : {}),
},
...(failure.status !== undefined ? { status: failure.status } : {}),
...(failure.details !== undefined
? {
upstream: {
...(failure.status !== undefined ? { status: failure.status } : {}),
details: failure.details,
},
}
: {}),
});
const staticPreviewOutput = (preview: SpecPreview): StaticPreviewSpecOutput => ({
title: Option.getOrNull(preview.title),
version: Option.getOrNull(preview.version),
servers: preview.servers.map((server) => ({
url: server.url,
description: Option.getOrNull(server.description),
variables: Option.getOrNull(server.variables)
? Object.fromEntries(
Object.entries(Option.getOrNull(server.variables) ?? {}).map(([name, variable]) => [
name,
{
default: variable.default,
enum: Option.getOrNull(variable.enum),
description: Option.getOrNull(variable.description),
},
]),
)
: null,
})),
operationCount: preview.operationCount,
tags: preview.tags,
securitySchemes: preview.securitySchemes.map((scheme) => ({
name: scheme.name,
type: scheme.type,
scheme: Option.getOrNull(scheme.scheme),
bearerFormat: Option.getOrNull(scheme.bearerFormat),
in: Option.getOrNull(scheme.in),
headerName: Option.getOrNull(scheme.headerName),
description: Option.getOrNull(scheme.description),
flows: Option.isSome(scheme.flows)
? {
authorizationCode: Option.isSome(scheme.flows.value.authorizationCode)
? {
authorizationUrl: scheme.flows.value.authorizationCode.value.authorizationUrl,
tokenUrl: scheme.flows.value.authorizationCode.value.tokenUrl,
refreshUrl: Option.getOrNull(scheme.flows.value.authorizationCode.value.refreshUrl),
scopes: scheme.flows.value.authorizationCode.value.scopes,
}
: null,
clientCredentials: Option.isSome(scheme.flows.value.clientCredentials)
? {
tokenUrl: scheme.flows.value.clientCredentials.value.tokenUrl,
refreshUrl: Option.getOrNull(scheme.flows.value.clientCredentials.value.refreshUrl),
scopes: scheme.flows.value.clientCredentials.value.scopes,
}
: null,
}
: null,
openIdConnectUrl: Option.getOrNull(scheme.openIdConnectUrl),
})),
authStrategies: preview.authStrategies,
headerPresets: preview.headerPresets,
oauth2Presets: preview.oauth2Presets.map((preset) => ({
label: preset.label,
securitySchemeName: preset.securitySchemeName,
flow: preset.flow,
authorizationUrl: Option.getOrNull(preset.authorizationUrl),
tokenUrl: preset.tokenUrl,
refreshUrl: Option.getOrNull(preset.refreshUrl),
scopes: preset.scopes,
identityScopes: preset.identityScopes,
})),
});
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Rewrite OpenAPI `#/components/schemas/X` refs to standard `#/$defs/X`. */
const normalizeOpenApiRefs = (node: unknown): unknown => {
if (node == null || typeof node !== "object") return node;
if (Array.isArray(node)) {
let changed = false;
const out = node.map((item) => {
const n = normalizeOpenApiRefs(item);
if (n !== item) changed = true;
return n;
});
return changed ? out : node;
}
const obj = node as Record<string, unknown>;
if (typeof obj.$ref === "string") {
const match = obj.$ref.match(/^#\/components\/schemas\/(.+)$/);
if (match) return { ...obj, $ref: `#/$defs/${match[1]}` };
return obj;
}
let changed = false;
const result: Record<string, unknown> = {};
for (const [k, v] of Object.entries(obj)) {
const n = normalizeOpenApiRefs(v);
if (n !== v) changed = true;
result[k] = n;
}
return changed ? result : obj;
};
const toBinding = (def: ToolDefinition): OperationBinding =>
OperationBinding.make({
method: def.operation.method,
baseUrl: def.operation.baseUrl,
pathTemplate: def.operation.pathTemplate,
parameters: [...def.operation.parameters],
requestBody: def.operation.requestBody,
});
const descriptionFor = (def: ToolDefinition): string => {
const op = def.operation;
return Option.getOrElse(op.description, () =>
Option.getOrElse(op.summary, () => `${op.method.toUpperCase()} ${op.pathTemplate}`),
);
};
const specInputToSourceUrl = (spec: OpenApiSpecInput): string | undefined =>
spec.kind === "url" || spec.kind === "googleDiscovery" ? spec.url : undefined;
const specInputToGoogleBundle = (spec: OpenApiSpecInput): readonly string[] | undefined =>
spec.kind === "googleDiscoveryBundle" ? spec.urls : undefined;
// ---------------------------------------------------------------------------
// Declared auth methods — project the stored `authenticationTemplate` into the
// catalog's plugin-agnostic `AuthMethodDescriptor[]`. This mirrors the client's
// `authMethodsFromConfig` (in the React auth-method-config module) on the
// server so the catalog field is consistent. apikey/none projection comes from
// the shared model; the oauth method carries the stored endpoints + scopes.
// ---------------------------------------------------------------------------
export const describeOpenApiAuthMethods = (
record: IntegrationRecord,
): readonly AuthMethodDescriptor[] => {
const config = decodeOpenApiIntegrationConfig(record.config);
if (!config) return [];
return (config.authenticationTemplate ?? []).map(
(template: Authentication): AuthMethodDescriptor => {
if (template.kind === "oauth2") {
return {
id: String(template.slug),
label: "OAuth2",
kind: "oauth",
template: String(template.slug),
oauth: {
authorizationUrl: template.authorizationUrl,
tokenUrl: template.tokenUrl,
scopes: template.scopes,
},
};
}
return describeApiKeyAuthMethod(template);
},
);
};
export const describeOpenApiIntegrationDisplay = (
record: IntegrationRecord,
): { readonly url?: string } => {
const config = decodeOpenApiIntegrationConfig(record.config);
return { url: config?.baseUrl ?? config?.sourceUrl };
};
// ---------------------------------------------------------------------------
// Spec text resolution — the stored config carries the spec's content hash
// (`specHash` → blob `spec/<hash>`). Pre-blob rows that inlined the text are
// rewritten by the spec-to-blob migrations before this code reads them.
// ---------------------------------------------------------------------------
const loadSpecText = (
storage: OpenapiStore,
config: OpenApiIntegrationConfig,
): Effect.Effect<string | null, StorageFailure> =>
config.specHash != null ? storage.getSpec(config.specHash) : Effect.succeed(null);
// ---------------------------------------------------------------------------
// Spec → tool definitions (shared by addSpec, resolveTools, and detect)
// ---------------------------------------------------------------------------
interface CompiledSpec {
readonly definitions: readonly ToolDefinition[];
readonly hoistedDefs: Record<string, unknown>;
readonly title: string | undefined;
}
const compileSpec = (
specText: string,
): Effect.Effect<CompiledSpec, OpenApiParseError | OpenApiExtractionError> =>
Effect.gen(function* () {
const doc = yield* parse(specText);
const result = yield* extract(doc);
const hoistedDefs: Record<string, unknown> = {};
if (doc.components?.schemas) {
for (const [k, v] of Object.entries(doc.components.schemas)) {
hoistedDefs[k] = normalizeOpenApiRefs(v);
}
}
return {
definitions: compileToolDefinitions(result.operations),
hoistedDefs,
title: Option.getOrUndefined(result.title),
};
});
// A tool's name carries its structured `group.leaf` path verbatim (e.g.
// `aliases.deleteAlias`). The address grammar
// `tools.<integration>.<owner>.<connection>.<tool>` treats `<tool>` as the
// trailing remainder (see parseToolAddress), so the dotted path needs no
// flattening — it nests naturally as
// `tools.<integration>.<owner>.<connection>.aliases.deleteAlias`, matching how
// the sandbox `tools` proxy joins property access.
const toolDefsFromCompiled = (compiled: CompiledSpec): readonly ToolDef[] =>
compiled.definitions.map(
(def): ToolDef => ({
name: ToolName.make(def.toolPath),
description: descriptionFor(def),
inputSchema: normalizeOpenApiRefs(Option.getOrUndefined(def.operation.inputSchema)),
// The output schema is the upstream response body only — transport
// status/headers live in the ToolResult `http` side channel, not the
// payload (see the invoke handler).
outputSchema: normalizeOpenApiRefs(Option.getOrUndefined(def.operation.outputSchema)),
annotations: annotationsForOperation(def.operation.method, def.operation.pathTemplate),
}),
);
const storedOperationsFromCompiled = (
integration: string,
compiled: CompiledSpec,
): readonly StoredOperation[] =>
compiled.definitions.map((def) => ({
integration,
toolName: def.toolPath,
binding: toBinding(def),
}));
// ---------------------------------------------------------------------------
// Plugin factory
// ---------------------------------------------------------------------------
export interface OpenApiPluginOptions {
readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient, never, never>;
}
const fetchGoogleDiscoveryBundleConversion = (
urls: readonly string[],
httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,
) =>
Effect.forEach(
urls,
(url) =>
fetchGoogleDiscoveryDocument(url).pipe(
Effect.provide(httpClientLayer),
Effect.map((documentText) => ({ discoveryUrl: url, documentText })),
),
{ concurrency: 4 },
).pipe(Effect.flatMap((documents) => convertGoogleDiscoveryBundleToOpenApi({ documents })));
export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => {
const resolveSpecForInput = (
spec: OpenApiSpecInput,
httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,
): Effect.Effect<
{
readonly specText: string;
readonly baseUrl?: string;
// The Google Discovery converters derive the `googleOAuth2` oauth template
// straight from the spec's declared scopes. `addSpec` adopts it when the
// caller didn't pass an explicit `authenticationTemplate` (the bundle add
// path has no preview to detect auth from).
readonly authenticationTemplate?: readonly Authentication[];
},
OpenApiParseError | OpenApiExtractionError | OpenApiOAuthError
> =>
Effect.gen(function* () {
if (spec.kind === "googleDiscovery") {
const conversion = yield* fetchGoogleDiscoveryDocument(spec.url).pipe(
Effect.provide(httpClientLayer),
Effect.flatMap((documentText) =>
convertGoogleDiscoveryToOpenApi({
discoveryUrl: spec.url,
documentText,
}),
),
);
return {
specText: conversion.specText,
baseUrl: conversion.baseUrl,
...(conversion.authenticationTemplate
? { authenticationTemplate: conversion.authenticationTemplate }
: {}),
};
}
if (spec.kind === "googleDiscoveryBundle") {
const conversion = yield* fetchGoogleDiscoveryBundleConversion(spec.urls, httpClientLayer);
return {
specText: conversion.specText,
baseUrl: conversion.baseUrl,
...(conversion.authenticationTemplate
? { authenticationTemplate: conversion.authenticationTemplate }
: {}),
};
}
if (spec.kind === "url") {
const specText = yield* resolveSpecText(spec.url).pipe(Effect.provide(httpClientLayer));
return { specText };
}
return { specText: spec.value };
});
return {
id: "openapi" as const,
packageName: "@executor-js/plugin-openapi",
integrationPresets: openApiPresets.map((preset) => ({
id: preset.id,
name: preset.name,
summary: preset.summary,
...(preset.url ? { url: preset.url } : {}),
...(preset.icon ? { icon: preset.icon } : {}),
...(preset.featured ? { featured: preset.featured } : {}),
})),
storage: (deps): OpenapiStore => makeDefaultOpenapiStore(deps),
extension: (ctx: PluginCtx<OpenapiStore>) => {
const httpClientLayer = options?.httpClientLayer ?? ctx.httpClientLayer;
const addSpec = (config: OpenApiSpecConfig) =>
Effect.gen(function* () {
// Resolve URL → text and parse BEFORE opening a transaction. Holding
// `BEGIN` across a network fetch is the Hyperdrive deadlock path.
const resolved = yield* resolveSpecForInput(config.spec, httpClientLayer);
const compiled = yield* compileSpec(resolved.specText);
// Defaults the add page derives from its preview, applied here so
// headless callers (MCP, API) get the same integration the UI's
// add flow would produce — see e2e/scenarios/connect-handoff.test.ts:
// - baseUrl: the spec's first server (else tools have no host to
// call and every invocation fails with "HTTP request failed")
// - authenticationTemplate: the spec's declared security schemes
// (else the Add-connection modal is a dead "No authentication"
// end with nowhere to paste a credential)
// An explicit input always wins; for auth, an explicit EMPTY array
// means "no auth methods" and suppresses the derivation.
const explicitBaseUrl = config.baseUrl ?? resolved.baseUrl;
const needsDerivedBaseUrl = explicitBaseUrl == null;
const needsDerivedAuth =
config.authenticationTemplate == null && resolved.authenticationTemplate == null;
const preview =
needsDerivedBaseUrl || needsDerivedAuth
? yield* previewSpecText(resolved.specText)
: undefined;
const derivedBaseUrl =
needsDerivedBaseUrl && preview ? firstBaseUrlForPreview(preview) : undefined;
const effectiveBaseUrl = explicitBaseUrl ?? (derivedBaseUrl || undefined);
const derivedAuthenticationTemplate =
needsDerivedAuth && preview
? deriveAuthenticationTemplateFromPreview(preview, effectiveBaseUrl)
: undefined;
const slug = IntegrationSlug.make(config.slug);
// Block re-adding an existing slug. The core `integrations.register`
// primitive upserts (so boot re-registration is idempotent), but an
// explicit add must NOT silently clobber an existing integration's
// tools, connections, and policies. To add more auth, update the
// existing integration instead.
const existing = yield* ctx.core.integrations.get(slug);
if (existing) {
return yield* new IntegrationAlreadyExistsError({ slug });
}
const specHash = yield* sha256Hex(resolved.specText);
const integrationConfig: OpenApiIntegrationConfig = {
specHash,
...(specInputToSourceUrl(config.spec) !== undefined
? { sourceUrl: specInputToSourceUrl(config.spec) }
: {}),
...(specInputToGoogleBundle(config.spec) !== undefined
? { googleDiscoveryUrls: specInputToGoogleBundle(config.spec) }
: {}),
...(effectiveBaseUrl ? { baseUrl: effectiveBaseUrl } : {}),
...(config.headers ? { headers: config.headers } : {}),
...(config.queryParams ? { queryParams: config.queryParams } : {}),
// Prefer the caller's explicit template; otherwise adopt the one the
// Google Discovery converter derived from the spec (the bundle add
// path relies on this — it has no preview to detect auth from);
// otherwise derive from the spec's declared security schemes.
...(config.authenticationTemplate
? {
authenticationTemplate: normalizeOpenApiAuthInputs(config.authenticationTemplate),
}
: resolved.authenticationTemplate
? { authenticationTemplate: resolved.authenticationTemplate }
: derivedAuthenticationTemplate && derivedAuthenticationTemplate.length > 0
? { authenticationTemplate: derivedAuthenticationTemplate }
: {}),
};
// The spec blob is written OUTSIDE the transaction: it's
// content-addressed (re-puts are idempotent) and an aborted register
// leaves only an unreferenced blob behind — while blob backends like
// R2 couldn't roll back with the transaction anyway.
yield* ctx.storage.putSpec(specHash, resolved.specText);
yield* ctx.transaction(
Effect.gen(function* () {
yield* ctx.core.integrations.register({
slug,
description: config.description ?? compiled.title ?? config.slug,
config: integrationConfig satisfies OpenApiIntegrationConfig as IntegrationConfig,
canRemove: true,
canRefresh:
specInputToSourceUrl(config.spec) != null ||
specInputToGoogleBundle(config.spec) != null,
});
yield* ctx.storage.putOperations(
config.slug,
storedOperationsFromCompiled(config.slug, compiled),
);
}),
);
return { slug, toolCount: compiled.definitions.length };
});
return {
previewSpec: (input: string | OpenApiPreviewInput) =>
Effect.gen(function* () {
const previewInput = typeof input === "string" ? { spec: input } : input;
const specText = isGoogleDiscoveryUrl(previewInput.spec)
? yield* fetchGoogleDiscoveryDocument(previewInput.spec).pipe(
Effect.provide(httpClientLayer),
Effect.flatMap((documentText) =>
convertGoogleDiscoveryToOpenApi({
discoveryUrl: previewInput.spec,
documentText,
}),
),
Effect.map((conversion) => conversion.specText),
)
: yield* resolveSpecText(previewInput.spec).pipe(Effect.provide(httpClientLayer));
return yield* previewSpec(specText).pipe(Effect.provide(httpClientLayer));
}),
addSpec,
removeSpec: (slug: string) =>
ctx.transaction(
Effect.gen(function* () {
yield* ctx.storage.removeOperations(slug);
yield* ctx.core.integrations
.remove(IntegrationSlug.make(slug))
.pipe(Effect.catchTag("IntegrationRemovalNotAllowedError", () => Effect.void));
}),
),
getIntegration: (slug: string) =>
ctx.core.integrations.get(IntegrationSlug.make(slug)).pipe(
Effect.map((record) =>
record
? ({
slug: record.slug,
description: record.description,
kind: record.kind,
canRemove: record.canRemove,
canRefresh: record.canRefresh,
} as Integration)
: null,
),
),
getConfig: (slug: string): Effect.Effect<OpenApiIntegrationConfig | null, StorageFailure> =>
ctx.core.integrations
.get(IntegrationSlug.make(slug))
.pipe(
Effect.map((record) =>
record ? decodeOpenApiIntegrationConfig(record.config) : null,
),
),
configure: (
slug: string,
input: OpenApiConfigureInput,
): Effect.Effect<readonly Authentication[], StorageFailure> =>
ctx.transaction(
Effect.gen(function* () {
const record = yield* ctx.core.integrations.get(IntegrationSlug.make(slug));
if (!record) return [] as readonly Authentication[];
const current = decodeOpenApiIntegrationConfig(record.config);
if (!current) return [] as readonly Authentication[];
const incoming = normalizeOpenApiAuthInputs(input.authenticationTemplate);
const merged =
input.mode === "replace"
? incoming
: mergeAuthTemplates(current.authenticationTemplate ?? [], incoming);
const next: OpenApiIntegrationConfig = {
...current,
authenticationTemplate: merged,
};
yield* ctx.core.integrations.update(IntegrationSlug.make(slug), {
config: next satisfies OpenApiIntegrationConfig as IntegrationConfig,
});
return merged;
}),
),
};
},
staticSources: (self: OpenApiPluginExtension) => [
{
id: "openapi",
kind: "executor",
name: "OpenAPI",
tools: [
tool({
name: "previewSpec",
description:
"Preview an OpenAPI document before adding it as an integration. Call this first when the user provides a spec URL/blob so you can inspect servers, auth schemes, operation count, and tags before `addSpec`. Do not collect API keys or OAuth client secrets in chat; use the connections tools for those values.",
inputSchema: PreviewSpecInputStandardSchema,
outputSchema: PreviewSpecOutputStandardSchema,
execute: (input: typeof PreviewSpecInputSchema.Type) =>
self.previewSpec(input).pipe(
Effect.map((preview) => ToolResult.ok(staticPreviewOutput(preview))),
Effect.catchTags({
OpenApiParseError: ({ message }: OpenApiParseError) =>
Effect.succeed(openApiToolFailure("openapi_parse_failed", message)),
OpenApiExtractionError: ({ message }: OpenApiExtractionError) =>
Effect.succeed(openApiToolFailure("openapi_extraction_failed", message)),
OpenApiOAuthError: ({ message }: OpenApiOAuthError) =>
Effect.succeed(openApiToolFailure("openapi_oauth_failed", message)),
}),
),
}),
tool({
name: "addSpec",
description:
"Add an OpenAPI integration to the catalog and persist its operations as tools. Recommended flow: call `previewSpec`, choose a `slug`, then create a connection for that integration with the user's API key or via `oauth.start`. When `baseUrl` is omitted it defaults to the spec's first server; when `authenticationTemplate` is omitted the auth methods are derived from the spec's declared security schemes (pass an explicit template to override how a credential is applied — apiKey header/query, or oauth bearer — or an empty array for no auth methods).",
annotations: {
requiresApproval: true,
approvalDescription: "Add an OpenAPI integration",
},
inputSchema: AddSourceInputStandardSchema,
outputSchema: AddSourceOutputStandardSchema,
execute: (input: typeof AddSourceInputSchema.Type) =>
self
.addSpec({
spec: input.spec,
slug: input.slug,
description: input.description,
baseUrl: input.baseUrl,
headers: input.headers,
queryParams: input.queryParams,
authenticationTemplate: input.authenticationTemplate as
| readonly AuthenticationInput[]
| undefined,
})
.pipe(
Effect.map((result) =>
ToolResult.ok({
slug: String(result.slug),
toolCount: result.toolCount,
}),
),
Effect.catchTags({
OpenApiParseError: ({ message }: OpenApiParseError) =>
Effect.succeed(openApiToolFailure("openapi_parse_failed", message)),
OpenApiExtractionError: ({ message }: OpenApiExtractionError) =>
Effect.succeed(openApiToolFailure("openapi_extraction_failed", message)),
OpenApiOAuthError: ({ message }: OpenApiOAuthError) =>
Effect.succeed(openApiToolFailure("openapi_oauth_failed", message)),
IntegrationAlreadyExistsError: ({ slug }: IntegrationAlreadyExistsError) =>
Effect.succeed(
openApiToolFailure(
"integration_already_exists",
`Integration ${slug} already exists; update it instead of re-adding.`,
),
),
}),
),
}),
],
},
],
describeAuthMethods: describeOpenApiAuthMethods,
describeIntegrationDisplay: describeOpenApiIntegrationDisplay,
// Produce one tool per spec operation. Spec-derived, identical for every
// connection on the integration — so `getValue` is never called here. The
// operation bindings invokeTool needs are persisted at addSpec time; this
// hook only shapes the per-connection ToolDefs from the spec blob the
// catalog config points at.
resolveTools: ({
config,
storage,
}: {
readonly integration: Integration;
readonly config: IntegrationConfig;
readonly storage: OpenapiStore;
}): Effect.Effect<ResolveToolsResult, StorageFailure> =>
Effect.gen(function* () {
const openApiConfig = decodeOpenApiIntegrationConfig(config);
if (!openApiConfig) return { tools: [], definitions: {} };
const specText = yield* loadSpecText(storage, openApiConfig);
if (specText == null) return { tools: [], definitions: {} };
const compiled = yield* compileSpec(specText).pipe(
Effect.catch(() => Effect.succeed(null)),
);
if (!compiled) return { tools: [], definitions: {} };
return {
tools: toolDefsFromCompiled(compiled),
definitions: compiled.hoistedDefs,
};
}),
invokeTool: ({