History

Refactor plugin SDK and HTTP client boundaries

Rhys Sullivan committed 7 months ago

Commit 6f6f1b11 parent f25b091114 files · +2703 −9255Browse files

Unified · Split

114 files
  1. apps/executor/package.json +1 −0
  2. apps/executor/src/cli/main.ts +14 −13
  3. apps/executor/src/cli/pending-interaction-output.test.ts +0 −113
  4. apps/executor/src/cli/pending-interaction-output.ts +0 −4
  5. apps/web/package.json +1 −0
  6. apps/web/src/components/shell.tsx +5 −1
  7. apps/web/src/main.tsx +14 −16
  8. apps/web/src/source-plugins/index.ts +31 −0
  9. apps/web/src/source-plugins/pages.tsx +121 −0
  10. apps/web/src/source-plugins/types.tsx +109 −0
  11. apps/web/src/views/add-source.tsx +0 −19
  12. apps/web/src/views/home.tsx +21 −7
  13. apps/web/src/views/source-detail.tsx +0 −29
  14. apps/web/src/views/source-editor.tsx +0 −37
  15. bun.lock +68 −11
  16. examples/sqlite-sdk-consumer/src/sqlite-backend.ts +2 −465
  17. package.json +1 −0
  18. packages/clients/react/package.json +1 −1
  19. packages/clients/react/src/index.ts +386 −903
  20. packages/hosts/mcp/src/index.ts +3 −11
  21. packages/hosts/mcp/src/paused-result.test.ts +0 −101
  22. packages/kernel/core/src/tool-map.ts +3 −3
  23. packages/kernel/core/src/types.ts +6 −6
  24. packages/kernel/ir/src/catalog.ts +1 −1
  25. packages/kernel/ir/src/model.ts +2 −2
  26. packages/platform/api/package.json +1 −1
  27. packages/platform/api/src/api.ts +57 −3
  28. packages/platform/api/src/client.ts +0 −134
  29. packages/platform/api/src/effect.ts +7 −4
  30. packages/platform/api/src/http.ts +32 −10
  31. packages/platform/api/src/index.ts +8 −8
  32. packages/platform/api/src/plugins.ts +23 −0
  33. packages/platform/api/src/sources/api.ts +0 −56
  34. packages/platform/api/src/sources/http.ts +1 −581
  35. packages/platform/internal/src/tools/executor-admin.ts +0 −50
  36. packages/platform/sdk-file/src/capture-release-workspace-fixture.ts +1 −2
  37. packages/platform/sdk-file/src/executor-state-store.test.ts +0 −166
  38. packages/platform/sdk-file/src/executor-state-store.ts +0 −634
  39. packages/platform/sdk-file/src/index.ts +27 −20
  40. packages/platform/sdk/package.json +1 −1
  41. packages/platform/sdk/src/backend.ts +0 −71
  42. packages/platform/sdk/src/contracts.ts +0 −1
  43. packages/platform/sdk/src/executor-effect.ts +83 −126
  44. packages/platform/sdk/src/executor.ts +116 −36
  45. packages/platform/sdk/src/index.ts +1 −0
  46. packages/platform/sdk/src/local/operations.ts +2 −382
  47. packages/platform/sdk/src/plugins.ts +98 −0
  48. packages/platform/sdk/src/runtime/auth/auth-artifacts.ts +0 −514
  49. packages/platform/sdk/src/runtime/auth/auth-leases.ts +0 −507
  50. packages/platform/sdk/src/runtime/auth/mcp-auth-provider.ts +0 −262
  51. packages/platform/sdk/src/runtime/auth/provider-grant-lifecycle.ts +0 −94
  52. packages/platform/sdk/src/runtime/auth/source-auth-material.ts +0 −176
  53. packages/platform/sdk/src/runtime/catalog/source/reconcile.ts +3 −3
  54. packages/platform/sdk/src/runtime/catalog/source/runtime.ts +3 −5
  55. packages/platform/sdk/src/runtime/catalog/source/sync.ts +9 −36
  56. packages/platform/sdk/src/runtime/execution/ir-execution.ts +6 −11
  57. packages/platform/sdk/src/runtime/execution/live.ts +0 −6
  58. packages/platform/sdk/src/runtime/execution/scope/environment.ts +0 −19
  59. packages/platform/sdk/src/runtime/execution/scope/tool-invoker.ts +7 −26
  60. packages/platform/sdk/src/runtime/executor-state-store.ts +0 −120
  61. packages/platform/sdk/src/runtime/index.ts +8 −62
  62. packages/platform/sdk/src/runtime/sources/executor-tools.ts +22 −251
  63. packages/platform/sdk/src/runtime/sources/source-adapters/index.ts +0 −64
  64. packages/platform/sdk/src/runtime/sources/source-adapters/internal.ts +0 −131
  65. packages/platform/sdk/src/runtime/sources/source-auth-service.ts +0 −219
  66. packages/platform/sdk/src/runtime/sources/source-definitions.ts +54 −430
  67. packages/platform/sdk/src/runtime/sources/source-inspection.test.ts +1 −1
  68. packages/platform/sdk/src/runtime/sources/source-plugins/index.ts +41 −0
  69. packages/platform/sdk/src/runtime/sources/source-plugins/internal.ts +48 −0
  70. packages/platform/sdk/src/runtime/sources/source-store.ts +10 −18
  71. packages/platform/sdk/src/runtime/sources/source-store/auth.ts +0 −141
  72. packages/platform/sdk/src/runtime/sources/source-store/config.ts +2 −140
  73. packages/platform/sdk/src/runtime/sources/source-store/lifecycle.ts +2 −163
  74. packages/platform/sdk/src/runtime/sources/source-store/records.ts +16 −123
  75. packages/platform/sdk/src/schema/enums.ts +3 −22
  76. packages/platform/sdk/src/schema/ids.ts +0 −26
  77. packages/platform/sdk/src/schema/index.ts +1 −6
  78. packages/platform/sdk/src/schema/models/auth-artifact.ts +0 −375
  79. packages/platform/sdk/src/schema/models/auth-lease.ts +0 −54
  80. packages/platform/sdk/src/schema/models/local-config.ts +0 −6
  81. packages/platform/sdk/src/schema/models/provider-auth-grant.ts +0 −37
  82. packages/platform/sdk/src/schema/models/scope-oauth-client.ts +0 −26
  83. packages/platform/sdk/src/schema/models/secret-ref.ts +8 −0
  84. packages/platform/sdk/src/schema/models/source-auth-session.ts +0 −159
  85. packages/platform/sdk/src/schema/models/source-catalog.ts +3 −4
  86. packages/platform/sdk/src/schema/models/source-oauth-client.ts +0 −48
  87. packages/platform/sdk/src/schema/models/source.ts +0 −85
  88. packages/platform/sdk/src/sources/contracts.ts +0 −97
  89. packages/platform/sdk/src/sources/inspection.ts +2 −2
  90. packages/platform/sdk/src/sources/operations.ts +46 −117
  91. packages/platform/server/package.json +3 −0
  92. packages/platform/server/src/index.ts +28 −12
  93. packages/platform/server/src/openapi-source-storage.ts +92 −0
  94. packages/sources/builtins/src/index.ts +0 −3
  95. packages/sources/core/src/catalog-fragment.ts +1 −1
  96. packages/sources/core/src/catalog-shared.ts +3 −5
  97. packages/sources/core/src/composition.ts +0 −90
  98. packages/sources/core/src/index.ts +0 −2
  99. packages/sources/core/src/registry.ts +0 −44
  100. packages/sources/core/src/shared.ts +2 −235
  101. packages/sources/core/src/source-models.ts +0 −91
  102. packages/sources/core/src/types.ts +3 −151
  103. plugins/openapi/http/index.ts +113 −0
  104. plugins/openapi/http/package.json +26 −0
  105. plugins/openapi/http/tsconfig.json +2 −2
  106. plugins/openapi/react/index.tsx +460 −0
  107. plugins/openapi/react/package.json +27 −0
  108. plugins/openapi/react/tsconfig.json +16 −0
  109. plugins/openapi/sdk/index.ts +146 −0
  110. plugins/openapi/sdk/package.json +25 −0
  111. plugins/openapi/sdk/tsconfig.json +15 −0
  112. plugins/openapi/shared/index.ts +177 −0
  113. plugins/openapi/shared/package.json +6 −4
  114. plugins/openapi/shared/tsconfig.json +15 −0
apps/executor/package.json+1−0
@@ -38,6 +38,7 @@ · 37 unmodified lines
38 "@effect/platform-node": "catalog:",38 "@effect/platform-node": "catalog:",
39 "@executor/codemode-core": "workspace:*",39 "@executor/codemode-core": "workspace:*",
40 "@executor/executor-mcp": "workspace:*",40 "@executor/executor-mcp": "workspace:*",
41 "@executor/plugin-openapi-sdk": "workspace:*",
41 "@executor/platform-api": "workspace:*",42 "@executor/platform-api": "workspace:*",
42 "@executor/platform-internal": "workspace:*",43 "@executor/platform-internal": "workspace:*",
43 "@executor/platform-sdk": "workspace:*",44 "@executor/platform-sdk": "workspace:*",
apps/executor/src/cli/main.ts+14−13
@@ -14,11 +14,13 @@ import { · 13 unmodified lines
14 type ExecutorApiEffectClient as ExecutorApiClient,14 type ExecutorApiEffectClient as ExecutorApiClient,
15} from "@executor/platform-api/effect";15} from "@executor/platform-api/effect";
16import { runExecutorMcpStdioServer } from "@executor/executor-mcp";16import { runExecutorMcpStdioServer } from "@executor/executor-mcp";
17import {
18 openApiSdkPlugin,
19} from "@executor/plugin-openapi-sdk";
17import { createWorkspaceExecutorAdminToolMap } from "@executor/platform-internal";20import { createWorkspaceExecutorAdminToolMap } from "@executor/platform-internal";
18import {21import {
19 EXECUTOR_SOURCES_ADD_HELP_LINES,22 EXECUTOR_SOURCES_ADD_HELP_LINES,
20 RuntimeExecutionResolverService,23 RuntimeExecutionResolverService,
21 hasRegisteredExecutorAddableSourceAdapters,
22} from "@executor/platform-sdk/runtime";24} from "@executor/platform-sdk/runtime";
23import {25import {
24 createExecutorEffect,26 createExecutorEffect,
@@ -38,6 +40,7 @@ import * as Option from "effect/Option"; · 13 unmodified lines
38import * as Cause from "effect/Cause";40import * as Cause from "effect/Cause";
39 41
40import {42import {
43 createFileOpenApiSourceStorage,
41 createLocalExecutorServer,44 createLocalExecutorServer,
42 DEFAULT_SERVER_BASE_URL,45 DEFAULT_SERVER_BASE_URL,
43 DEFAULT_SERVER_HOST,46 DEFAULT_SERVER_HOST,
@@ -71,7 +74,6 @@ import { · 27 unmodified lines
71 74
72const toError = (cause: unknown): Error =>75const toError = (cause: unknown): Error =>
73 cause instanceof Error ? cause : new Error(String(cause));76 cause instanceof Error ? cause : new Error(String(cause));
74
75const sleep = (ms: number) =>77const sleep = (ms: number) =>
76 Effect.promise(() => new Promise<void>((resolve) => setTimeout(resolve, ms)));78 Effect.promise(() => new Promise<void>((resolve) => setTimeout(resolve, ms)));
77 79
@@ -214,16 +216,9 @@ const buildWorkflowText = (namespaces: readonly string[] = []): string => · 136 unmodified lines
214 '1) const matches = await tools.discover({ query: "<intent>", limit: 12 });',216 '1) const matches = await tools.discover({ query: "<intent>", limit: 12 });',
215 "2) const details = await tools.describe.tool({ path, includeSchemas: true });",217 "2) const details = await tools.describe.tool({ path, includeSchemas: true });",
216 "3) Call selected tools.<path>(input).",218 "3) Call selected tools.<path>(input).",
217 ...(hasRegisteredExecutorAddableSourceAdapters219 "4) Source plugins are not registered in this build.",
218 ? [220 ...EXECUTOR_SOURCES_ADD_HELP_LINES,
219 "4) To connect a source plugin, call tools.executor.sources.add(...).",221 "5) If execution pauses for interaction, resume it with `executor resume --execution-id ...`.",
220 ...EXECUTOR_SOURCES_ADD_HELP_LINES,
221 "5) If execution pauses for interaction, resume it with `executor resume --execution-id ...`.",
222 ]
223 : [
224 "4) Source plugins are not registered in this build.",
225 "5) If execution pauses for interaction, resume it with `executor resume --execution-id ...`.",
226 ]),
227 "Do not use fetch; use tools.* only.",222 "Do not use fetch; use tools.* only.",
228 ].join("\n");223 ].join("\n");
229 224
@@ -293,6 +288,13 @@ const loadRunWorkflowText = (): Effect.Effect<string, Error, never> => · 63 unmodified lines
293 backend: createLocalExecutorBackend({288 backend: createLocalExecutorBackend({
294 localDataDir: DEFAULT_LOCAL_DATA_DIR,289 localDataDir: DEFAULT_LOCAL_DATA_DIR,
295 }),290 }),
291 plugins: [
292 openApiSdkPlugin({
293 storage: createFileOpenApiSourceStorage({
294 rootDir: `${DEFAULT_LOCAL_DATA_DIR}/plugins/openapi/sources`,
295 }),
296 }),
297 ] as const,
296 createInternalToolMap: createWorkspaceExecutorAdminToolMap,298 createInternalToolMap: createWorkspaceExecutorAdminToolMap,
297 }).pipe(Effect.mapError(toError)),299 }).pipe(Effect.mapError(toError)),
298 (executor) =>300 (executor) =>
@@ -420,7 +422,6 @@ const getLocalAuthedClient = (baseUrl: string = DEFAULT_SERVER_BASE_URL) => · 121 unmodified lines
420 const installation = yield* bootstrapClient.local.installation({});422 const installation = yield* bootstrapClient.local.installation({});
421 const client = yield* createExecutorApiClient({423 const client = yield* createExecutorApiClient({
422 baseUrl,424 baseUrl,
423 accountId: installation.actorScopeId,
424 });425 });
425 426
426 return {427 return {
apps/executor/src/cli/pending-interaction-output.test.ts+0−113
@@ -1,113 +0,0 @@
1import { describe, expect, it } from "@effect/vitest";
2import type { ExecutionInteraction } from "@executor/platform-sdk/schema";
3
4import {
5 buildPausedExecutionOutput,
6 parseInteractionPayload,
7} from "./pending-interaction-output";
8
9const makeInteraction = (patch: Partial<ExecutionInteraction> = {}): ExecutionInteraction => ({
10 id: "exec_123:tool_execution_gate:call_1" as never,
11 executionId: "exec_123" as never,
12 status: "pending",
13 kind: "form",
14 purpose: "tool_execution_gate",
15 payloadJson: JSON.stringify({
16 elicitation: {
17 mode: "form",
18 message: "Allow DELETE /v2/domains/{domain}/records/{recordId}?",
19 requestedSchema: {
20 type: "object",
21 properties: {
22 approve: {
23 type: "boolean",
24 description: "Whether to approve the tool call",
25 },
26 },
27 required: ["approve"],
28 additionalProperties: false,
29 },
30 },
31 }),
32 responseJson: null,
33 responsePrivateJson: null,
34 createdAt: 1,
35 updatedAt: 1,
36 ...patch,
37});
38
39describe("pending-interaction-output", () => {
40 it("parses requested schema from interaction payload", () => {
41 const parsed = parseInteractionPayload(makeInteraction());
42
43 expect(parsed).not.toBeNull();
44 expect(parsed?.mode).toBe("form");
45 expect(parsed?.requestedSchema).toEqual({
46 type: "object",
47 properties: {
48 approve: {
49 type: "boolean",
50 description: "Whether to approve the tool call",
51 },
52 },
53 required: ["approve"],
54 additionalProperties: false,
55 });
56 });
57
58 it("builds a structured paused execution instruction with full resume command", () => {
59 const output = buildPausedExecutionOutput({
60 executionId: "exec_123",
61 interaction: makeInteraction(),
62 baseUrl: "http://127.0.0.1:8788",
63 shouldOpenUrls: false,
64 });
65
66 expect(output.resumeCommand).toBe(
67 "executor resume --execution-id exec_123 --base-url http://127.0.0.1:8788 --no-open",
68 );
69 expect(output.interaction.requestedSchema).toEqual({
70 type: "object",
71 properties: {
72 approve: {
73 type: "boolean",
74 description: "Whether to approve the tool call",
75 },
76 },
77 required: ["approve"],
78 additionalProperties: false,
79 });
80 expect(output.instruction).toContain("requires approval");
81 expect(output.instruction).toContain("Allow DELETE /v2/domains/{domain}/records/{recordId}?");
82 expect(output.instruction).toContain(output.resumeCommand);
83 expect(output.instruction).toContain("interaction.requestedSchema");
84 });
85
86 it("describes URL interactions with the next command to run", () => {
87 const output = buildPausedExecutionOutput({
88 executionId: "exec_456",
89 interaction: makeInteraction({
90 id: "exec_456:source_connect_oauth2:call_1" as never,
91 executionId: "exec_456" as never,
92 kind: "url",
93 purpose: "source_connect_oauth2",
94 payloadJson: JSON.stringify({
95 elicitation: {
96 mode: "url",
97 message: "Finish connecting Vercel",
98 url: "https://vercel.com/oauth/start",
99 },
100 }),
101 }),
102 baseUrl: "http://127.0.0.1:8788",
103 shouldOpenUrls: true,
104 });
105
106 expect(output.interaction.mode).toBe("url");
107 expect(output.interaction.url).toBe("https://vercel.com/oauth/start");
108 expect(output.instruction).toContain("https://vercel.com/oauth/start");
109 expect(output.instruction).toContain(
110 "executor resume --execution-id exec_456 --base-url http://127.0.0.1:8788",
111 );
112 });
113});
apps/executor/src/cli/pending-interaction-output.ts+0−4
@@ -55,10 +55,6 @@ const describePauseReason = (interaction: ExecutionInteraction): string => { · 54 unmodified lines
55 switch (interaction.purpose) {55 switch (interaction.purpose) {
56 case "tool_execution_gate":56 case "tool_execution_gate":
57 return "this tool call requires approval before executor can continue";57 return "this tool call requires approval before executor can continue";
58 case "source_connect_oauth2":
59 return "executor needs browser authentication to finish connecting the source";
60 case "source_connect_secret":
61 return "executor needs credentials to finish connecting the source";
62 default:58 default:
63 return "executor needs additional input before it can continue";59 return "executor needs additional input before it can continue";
64 }60 }
apps/web/package.json+1−0
@@ -9,6 +9,7 @@ · 8 unmodified lines
9 "start": "NODE_ENV=production bun src/server.ts"9 "start": "NODE_ENV=production bun src/server.ts"
10 },10 },
11 "dependencies": {11 "dependencies": {
12 "@executor/plugin-openapi-react": "workspace:*",
12 "@executor/react": "workspace:*",13 "@executor/react": "workspace:*",
13 "@executor/server": "workspace:*",14 "@executor/server": "workspace:*",
14 "@shikijs/langs": "^4.0.1",15 "@shikijs/langs": "^4.0.1",
apps/web/src/components/shell.tsx+5−1
@@ -257,7 +257,11 @@ export function AppShell() { · 256 unmodified lines
257 </div>257 </div>
258 <LoadableBlock loadable={sources} loading="Loading...">258 <LoadableBlock loadable={sources} loading="Loading...">
259 {(items) =>259 {(items) =>
260 items.length === 0 ? (260 !Array.isArray(items) ? (
261 <div className="px-2.5 py-2 text-[11px] leading-relaxed text-destructive">
262 Sources returned an unexpected payload.
263 </div>
264 ) : items.length === 0 ? (
261 <div className="px-2.5 py-2 text-[11px] leading-relaxed text-muted-foreground/40">265 <div className="px-2.5 py-2 text-[11px] leading-relaxed text-muted-foreground/40">
262 No sources yet266 No sources yet
263 </div>267 </div>
apps/web/src/main.tsx+14−16
@@ -12,21 +12,19 @@ import "./globals.css"; · 11 unmodified lines
12 12
13import { AppShell } from "./components/shell";13import { AppShell } from "./components/shell";
14import { HomePage } from "./views/home";14import { HomePage } from "./views/home";
15import { EditSourcePage, NewSourcePage } from "./views/source-editor";
16import { SourceDetailPage } from "./views/source-detail";
17import { SecretsPage } from "./views/secrets";15import { SecretsPage } from "./views/secrets";
18import { AddSourcePage } from "./views/add-source";16import {
17 SourcePluginAddPage,
18 SourcePluginCreatePage,
19 SourcePluginDetailPage,
20 SourcePluginEditPage,
21 type SourcePluginRouteSearch,
22} from "./source-plugins/pages";
19 23
20// ---------------------------------------------------------------------------24// ---------------------------------------------------------------------------
21// Route search schema25// Route search schema
22// ---------------------------------------------------------------------------26// ---------------------------------------------------------------------------
23 27
24type SourceRouteSearch = {
25 tab: "model" | "discover";
26 tool?: string;
27 query?: string;
28};
29
30const sourceTabs = ["model", "discover"] as const;28const sourceTabs = ["model", "discover"] as const;
31 29
32// ---------------------------------------------------------------------------30// ---------------------------------------------------------------------------
@@ -46,22 +44,22 @@ const homeRoute = createRoute({ · 13 unmodified lines
46const newSourceRoute = createRoute({44const newSourceRoute = createRoute({
47 getParentRoute: () => rootRoute,45 getParentRoute: () => rootRoute,
48 path: "/sources/new",46 path: "/sources/new",
49 component: NewSourcePage,47 component: SourcePluginCreatePage,
50});48});
51 49
52const addSourceRoute = createRoute({50const addSourceRoute = createRoute({
53 getParentRoute: () => rootRoute,51 getParentRoute: () => rootRoute,
54 path: "/sources/add",52 path: "/sources/add",
55 component: AddSourcePage,53 component: SourcePluginAddPage,
56});54});
57 55
58const sourceRoute = createRoute({56const sourceRoute = createRoute({
59 getParentRoute: () => rootRoute,57 getParentRoute: () => rootRoute,
60 path: "/sources/$sourceId",58 path: "/sources/$sourceId",
61 validateSearch: (search: Record<string, unknown>): SourceRouteSearch => ({59 validateSearch: (search: Record<string, unknown>): SourcePluginRouteSearch => ({
62 tab:60 tab:
63 typeof search.tab === "string" && sourceTabs.includes(search.tab as SourceRouteSearch["tab"])61 typeof search.tab === "string" && sourceTabs.includes(search.tab as SourcePluginRouteSearch["tab"])
64 ? (search.tab as SourceRouteSearch["tab"])62 ? (search.tab as SourcePluginRouteSearch["tab"])
65 : "model",63 : "model",
66 tool: typeof search.tool === "string" && search.tool.length > 0 ? search.tool : undefined,64 tool: typeof search.tool === "string" && search.tool.length > 0 ? search.tool : undefined,
67 query: typeof search.query === "string" ? search.query : undefined,65 query: typeof search.query === "string" ? search.query : undefined,
@@ -88,7 +86,7 @@ function SourceDetailPageWrapper() { · 20 unmodified lines
88 const navigate = useNavigate({ from: sourceRoute.fullPath });86 const navigate = useNavigate({ from: sourceRoute.fullPath });
89 87
90 return (88 return (
91 <SourceDetailPage89 <SourcePluginDetailPage
92 sourceId={sourceId}90 sourceId={sourceId}
93 search={search}91 search={search}
94 navigate={navigate as any}92 navigate={navigate as any}
@@ -98,7 +96,7 @@ function SourceDetailPageWrapper() { · 3 unmodified lines
98 96
99function EditSourcePageWrapper() {97function EditSourcePageWrapper() {
100 const { sourceId } = editSourceRoute.useParams();98 const { sourceId } = editSourceRoute.useParams();
101 return <EditSourcePage sourceId={sourceId} />;99 return <SourcePluginEditPage sourceId={sourceId} />;
102}100}
103 101
104// ---------------------------------------------------------------------------102// ---------------------------------------------------------------------------
apps/web/src/source-plugins/index.ts+31−0
@@ -0,0 +1,31 @@
1import {
2 OpenApiReactPlugin,
3} from "@executor/plugin-openapi-react";
4import type {
5 ExecutorFrontendPlugin,
6 FrontendSourceTypeDefinition,
7} from "./types";
8
9const frontendPlugins = [
10 OpenApiReactPlugin,
11] as const satisfies readonly ExecutorFrontendPlugin[];
12
13const sourceTypeDefinitions = new Map<string, FrontendSourceTypeDefinition>();
14
15for (const plugin of frontendPlugins) {
16 plugin.register({
17 sources: {
18 registerType(definition) {
19 sourceTypeDefinitions.set(definition.kind, definition);
20 },
21 },
22 });
23}
24
25export const registeredSourceFrontendTypes = [...sourceTypeDefinitions.values()];
26
27export const getSourceFrontendType = (kind: string) =>
28 sourceTypeDefinitions.get(kind) ?? null;
29
30export const getDefaultSourceFrontendType = () =>
31 registeredSourceFrontendTypes[0] ?? null;
apps/web/src/source-plugins/pages.tsx+121−0
@@ -0,0 +1,121 @@
1import { useSource } from "@executor/react";
2import { LoadableBlock } from "../components/loadable";
3import { SourcePluginsResetState } from "../components/source-plugins-reset-state";
4import {
5 getDefaultSourceFrontendType,
6 getSourceFrontendType,
7} from "./index";
8
9export type SourcePluginRouteSearch = {
10 tab: "model" | "discover";
11 tool?: string;
12 query?: string;
13};
14
15export function SourcePluginAddPage() {
16 const definition = getDefaultSourceFrontendType();
17 if (definition === null) {
18 return (
19 <SourcePluginsResetState
20 title="Add Source is intentionally blank"
21 message="No source plugins are registered in this build."
22 />
23 );
24 }
25
26 return (
27 <div className="mx-auto max-w-4xl px-6 py-10 lg:px-10 lg:py-14">
28 <div className="mb-8">
29 <div className="inline-flex rounded-full border border-border bg-muted px-3 py-1 text-[11px] font-medium uppercase tracking-[0.2em] text-muted-foreground">
30 Source Plugin
31 </div>
32 <h1 className="mt-5 font-display text-3xl tracking-tight text-foreground lg:text-4xl">
33 {definition.displayName}
34 </h1>
35 </div>
36 {definition.renderAddPage()}
37 </div>
38 );
39}
40
41export function SourcePluginCreatePage() {
42 const definition = getDefaultSourceFrontendType();
43 if (definition === null) {
44 return (
45 <SourcePluginsResetState
46 title="New source creation is disabled"
47 message="No source plugins are registered in this build."
48 />
49 );
50 }
51
52 return (
53 <div className="mx-auto max-w-4xl px-6 py-10 lg:px-10 lg:py-14">
54 {definition.renderAddPage()}
55 </div>
56 );
57}
58
59export function SourcePluginEditPage(input: {
60 sourceId: string;
61}) {
62 const source = useSource(input.sourceId);
63
64 return (
65 <LoadableBlock loadable={source} loading="Loading source...">
66 {(loadedSource) => {
67 const definition = getSourceFrontendType(loadedSource.kind);
68 if (definition?.renderEditPage === undefined) {
69 return (
70 <SourcePluginsResetState
71 title="Source editing is disabled"
72 message={`No frontend source plugin is registered for kind "${loadedSource.kind}".`}
73 />
74 );
75 }
76
77 return (
78 <div className="mx-auto max-w-4xl px-6 py-10 lg:px-10 lg:py-14">
79 {definition.renderEditPage({ source: loadedSource })}
80 </div>
81 );
82 }}
83 </LoadableBlock>
84 );
85}
86
87export function SourcePluginDetailPage(input: {
88 sourceId: string;
89 search: SourcePluginRouteSearch;
90 navigate: unknown;
91}) {
92 const source = useSource(input.sourceId);
93
94 return (
95 <LoadableBlock loadable={source} loading="Loading source...">
96 {(loadedSource) => {
97 const definition = getSourceFrontendType(loadedSource.kind);
98 if (definition?.renderDetailPage === undefined) {
99 return (
100 <SourcePluginsResetState
101 title="Source detail is disabled"
102 message={`No frontend source plugin is registered for kind "${loadedSource.kind}".`}
103 />
104 );
105 }
106
107 return (
108 <div className="mx-auto max-w-4xl px-6 py-10 lg:px-10 lg:py-14">
109 {definition.renderDetailPage({
110 source: loadedSource,
111 route: {
112 search: input.search,
113 navigate: input.navigate,
114 },
115 })}
116 </div>
117 );
118 }}
119 </LoadableBlock>
120 );
121}
apps/web/src/source-plugins/types.tsx+109−0
@@ -0,0 +1,109 @@
1import type { Source } from "@executor/react";
2import type { ComponentType, ReactNode } from "react";
3
4export type ExecutorFrontendPlugin = {
5 key: string;
6 register: (api: ExecutorFrontendPluginApi) => void;
7};
8
9export type ExecutorFrontendPluginApi = {
10 sources: {
11 registerType: (
12 definition: FrontendSourceTypeDefinition,
13 ) => void;
14 };
15};
16
17export type SourcePluginDetailRouteContext = {
18 search?: unknown;
19 navigate?: unknown;
20};
21
22export type SourcePluginAddScreenProps<Input, Client = unknown> = {
23 initialValue: Input;
24 client: Client;
25 onSubmit: (input: Input) => Promise<void>;
26};
27
28export type SourcePluginEditScreenProps<Config, Client = unknown> = {
29 sourceId: string;
30 config: Config;
31 client: Client;
32 onSubmit: (config: Config) => Promise<void>;
33};
34
35export type SourcePluginDetailScreenProps<Config, Client = unknown> = {
36 sourceId: string;
37 config: Config;
38 client: Client;
39};
40
41export type FrontendSourceTypeDefinition = {
42 kind: string;
43 displayName: string;
44 renderAddPage: () => ReactNode;
45 renderEditPage?: (input: { source: Source }) => ReactNode;
46 renderDetailPage?: (input: {
47 source: Source;
48 route: SourcePluginDetailRouteContext;
49 }) => ReactNode;
50};
51
52export const defineFrontendSourceType = <Input, Config, Client>(input: {
53 kind: string;
54 displayName: string;
55 createInitialInput: () => Input;
56 client: Client;
57 addScreen: ComponentType<SourcePluginAddScreenProps<Input, Client>>;
58 editScreen?: ComponentType<SourcePluginEditScreenProps<Config, Client>>;
59 detailScreen?: ComponentType<SourcePluginDetailScreenProps<Config, Client>>;
60 configFromSource?: (source: Source) => Config;
61}): FrontendSourceTypeDefinition => {
62 const renderEditPage = (() => {
63 if (input.editScreen === undefined || input.configFromSource === undefined) {
64 return undefined;
65 }
66
67 const EditScreen = input.editScreen;
68 const configFromSource = input.configFromSource;
69 return ({ source }: { source: Source }) => (
70 <EditScreen
71 sourceId={source.id}
72 config={configFromSource(source)}
73 client={input.client}
74 onSubmit={async () => {}}
75 />
76 );
77 })();
78
79 const renderDetailPage = (() => {
80 if (input.detailScreen === undefined || input.configFromSource === undefined) {
81 return undefined;
82 }
83
84 const DetailScreen = input.detailScreen;
85 const configFromSource = input.configFromSource;
86 return ({ source }: { source: Source }) => (
87 <DetailScreen
88 sourceId={source.id}
89 config={configFromSource(source)}
90 client={input.client}
91 />
92 );
93 })();
94
95 return {
96 kind: input.kind,
97 displayName: input.displayName,
98 renderAddPage: () =>
99 <input.addScreen
100 initialValue={input.createInitialInput()}
101 client={input.client}
102 onSubmit={async () => {}}
103 />,
104 renderEditPage,
105 renderDetailPage: renderDetailPage
106 ? ({ source }) => renderDetailPage({ source })
107 : undefined,
108 };
109};
apps/web/src/views/add-source.tsx+0−19
@@ -1,19 +0,0 @@
1import { Link } from "@tanstack/react-router";
2import { SourcePluginsResetState } from "../components/source-plugins-reset-state";
3import { Button } from "../components/ui/button";
4
5export function AddSourcePage() {
6 return (
7 <SourcePluginsResetState
8 title="Add Source is intentionally blank"
9 message="The previous OpenAPI, GraphQL, MCP, and Google Discovery source onboarding flows were removed so the product can be rebuilt around explicit source plugins."
10 action={(
11 <Link to="/">
12 <Button size="sm" variant="outline">
13 Back to sources
14 </Button>
15 </Link>
16 )}
17 />
18 );
19}
apps/web/src/views/home.tsx+21−7
@@ -1,6 +1,8 @@
1import { Link } from "@tanstack/react-router";
1import { useSources } from "@executor/react";2import { useSources } from "@executor/react";
2import { LoadableBlock } from "../components/loadable";3import { LoadableBlock } from "../components/loadable";
3import { Badge } from "../components/ui/badge";4import { Badge } from "../components/ui/badge";
5import { Button } from "../components/ui/button";
4import { SourcePluginsResetState } from "../components/source-plugins-reset-state";6import { SourcePluginsResetState } from "../components/source-plugins-reset-state";
5 7
6export function HomePage() {8export function HomePage() {
@@ -14,22 +16,34 @@ export function HomePage() { · 7 unmodified lines
14 Sources16 Sources
15 </div>17 </div>
16 <h1 className="mt-5 font-display text-3xl tracking-tight text-foreground lg:text-4xl">18 <h1 className="mt-5 font-display text-3xl tracking-tight text-foreground lg:text-4xl">
17 Source plugins have been removed from the product shell19 OpenAPI is now mounted as a source plugin
18 </h1>20 </h1>
19 <p className="mt-3 max-w-3xl text-sm leading-6 text-muted-foreground">21 <p className="mt-3 max-w-3xl text-sm leading-6 text-muted-foreground">
20 The built-in OpenAPI, GraphQL, MCP, and Google Discovery flows are22 The product shell is still in transition, but the first plugin-backed
21 intentionally stripped out. This leaves the workspace UI in a clean23 surface is wired in for OpenAPI so the frontend and backend boundaries
22 slate state while the plugin registration model is rebuilt.24 are visible in the app again.
23 </p>25 </p>
24 </div>26 </div>
25 27
26 <div className="mt-8">28 <div className="mt-8">
27 <LoadableBlock loadable={sources} loading="Loading sources...">29 <LoadableBlock loadable={sources} loading="Loading sources...">
28 {(items) =>30 {(items) =>
29 items.length === 0 ? (31 !Array.isArray(items) ? (
30 <SourcePluginsResetState32 <SourcePluginsResetState
31 title="No registered source plugins"33 title="Unexpected sources payload"
32 message="No source plugins are registered in this build, so new sources cannot be added from the UI yet."34 message="The sources list returned data in an unexpected shape."
35 />
36 ) : items.length === 0 ? (
37 <SourcePluginsResetState
38 title="No sources connected yet"
39 message="The OpenAPI source plugin is registered. Use the add flow to inspect the plugin-owned UI surface."
40 action={(
41 <Link to="/sources/add">
42 <Button size="sm" variant="outline">
43 Open OpenAPI Plugin
44 </Button>
45 </Link>
46 )}
33 />47 />
34 ) : (48 ) : (
35 <div className="grid gap-3">49 <div className="grid gap-3">
apps/web/src/views/source-detail.tsx+0−29
@@ -1,29 +0,0 @@
1import { Link } from "@tanstack/react-router";
2import { SourcePluginsResetState } from "../components/source-plugins-reset-state";
3import { Button } from "../components/ui/button";
4
5type SourceRouteSearch = {
6 tab: "model" | "discover";
7 tool?: string;
8 query?: string;
9};
10
11export function SourceDetailPage(_input: {
12 sourceId: string;
13 search: SourceRouteSearch;
14 navigate: unknown;
15}) {
16 return (
17 <SourcePluginsResetState
18 title="Source detail is disabled"
19 message="The source-specific detail experience was removed with the old built-in OpenAPI, GraphQL, MCP, and Google Discovery product wiring. This route remains only as a placeholder while plugin-backed detail modules are designed."
20 action={(
21 <Link to="/">
22 <Button size="sm" variant="outline">
23 Back to sources
24 </Button>
25 </Link>
26 )}
27 />
28 );
29}
apps/web/src/views/source-editor.tsx+0−37
@@ -1,37 +0,0 @@
1import { Link } from "@tanstack/react-router";
2import { SourcePluginsResetState } from "../components/source-plugins-reset-state";
3import { Button } from "../components/ui/button";
4
5export function NewSourcePage() {
6 return (
7 <SourcePluginsResetState
8 title="New source creation is disabled"
9 message="The old source creation form was removed as part of the clean-slate plugin reset. New source creation will come back through registered plugin modules instead of hard-coded source kinds."
10 action={(
11 <Link to="/">
12 <Button size="sm" variant="outline">
13 Back to sources
14 </Button>
15 </Link>
16 )}
17 />
18 );
19}
20
21export function EditSourcePage(_input: {
22 sourceId: string;
23}) {
24 return (
25 <SourcePluginsResetState
26 title="Source editing is disabled"
27 message="The previous source editor contained hard-coded OpenAPI, GraphQL, MCP, and Google Discovery behavior. That editor has been removed so a plugin-defined editor surface can replace it."
28 action={(
29 <Link to="/">
30 <Button size="sm" variant="outline">
31 Back to sources
32 </Button>
33 </Link>
34 )}
35 />
36 );
37}
bun.lock+68−11
@@ -32,6 +32,7 @@ · 31 unmodified lines
32 "@executor/platform-internal": "workspace:*",32 "@executor/platform-internal": "workspace:*",
33 "@executor/platform-sdk": "workspace:*",33 "@executor/platform-sdk": "workspace:*",
34 "@executor/platform-sdk-file": "workspace:*",34 "@executor/platform-sdk-file": "workspace:*",
35 "@executor/plugin-openapi-sdk": "workspace:*",
35 "@executor/server": "workspace:*",36 "@executor/server": "workspace:*",
36 "effect": "catalog:",37 "effect": "catalog:",
37 },38 },
@@ -51,6 +52,7 @@ · 13 unmodified lines
51 "name": "@executor/web",52 "name": "@executor/web",
52 "version": "0.0.0",53 "version": "0.0.0",
53 "dependencies": {54 "dependencies": {
55 "@executor/plugin-openapi-react": "workspace:*",
54 "@executor/react": "workspace:*",56 "@executor/react": "workspace:*",
55 "@executor/server": "workspace:*",57 "@executor/server": "workspace:*",
56 "@shikijs/langs": "^4.0.1",58 "@shikijs/langs": "^4.0.1",
@@ -343,7 +345,6 @@ · 286 unmodified lines
343 "@executor/runtime-deno-subprocess": "workspace:*",345 "@executor/runtime-deno-subprocess": "workspace:*",
344 "@executor/runtime-quickjs": "workspace:*",346 "@executor/runtime-quickjs": "workspace:*",
345 "@executor/runtime-ses": "workspace:*",347 "@executor/runtime-ses": "workspace:*",
346 "@executor/source-builtins": "workspace:*",
347 "@executor/source-core": "workspace:*",348 "@executor/source-core": "workspace:*",
348 "effect": "catalog:",349 "effect": "catalog:",
349 },350 },
@@ -393,6 +394,9 @@ · 43 unmodified lines
393 "@executor/platform-internal": "workspace:*",394 "@executor/platform-internal": "workspace:*",
394 "@executor/platform-sdk": "workspace:*",395 "@executor/platform-sdk": "workspace:*",
395 "@executor/platform-sdk-file": "workspace:*",396 "@executor/platform-sdk-file": "workspace:*",
397 "@executor/plugin-openapi-http": "workspace:*",
398 "@executor/plugin-openapi-sdk": "workspace:*",
399 "@executor/plugin-openapi-shared": "workspace:*",
396 "@opentelemetry/api": "^1.9.0",400 "@opentelemetry/api": "^1.9.0",
397 "@opentelemetry/exporter-trace-otlp-grpc": "^0.213.0",401 "@opentelemetry/exporter-trace-otlp-grpc": "^0.213.0",
398 "@opentelemetry/resources": "^2.6.0",402 "@opentelemetry/resources": "^2.6.0",
@@ -406,26 +410,73 @@ · 7 unmodified lines
406 "bun-types": "catalog:",410 "bun-types": "catalog:",
407 },411 },
408 },412 },
409 "packages/sources/builtins": {413 "packages/sources/core": {
410 "name": "@executor/source-builtins",414 "name": "@executor/source-core",
415 "version": "0.0.0",
416 "dependencies": {
417 "@executor/auth-oauth2": "workspace:*",
418 "@executor/codemode-core": "workspace:*",
419 "@executor/ir": "workspace:*",
420 "@modelcontextprotocol/sdk": "^1.26.0",
421 "effect": "catalog:",
422 },
423 "devDependencies": {
424 "@types/node": "catalog:",
425 "bun-types": "catalog:",
426 },
427 },
428 "plugins/openapi/http": {
429 "name": "@executor/plugin-openapi-http",
430 "version": "0.0.0",
431 "dependencies": {
432 "@effect/platform": "catalog:",
433 "@executor/platform-api": "workspace:*",
434 "@executor/platform-sdk": "workspace:*",
435 "@executor/plugin-openapi-shared": "workspace:*",
436 "effect": "catalog:",
437 },
438 "devDependencies": {
439 "@types/node": "catalog:",
440 "bun-types": "catalog:",
441 },
442 },
443 "plugins/openapi/react": {
444 "name": "@executor/plugin-openapi-react",
411 "version": "0.0.0",445 "version": "0.0.0",
412 "dependencies": {446 "dependencies": {
447 "@executor/plugin-openapi-http": "workspace:*",
448 "@executor/plugin-openapi-shared": "workspace:*",
449 "@executor/react": "workspace:*",
450 "@tanstack/react-router": "^1.132.7",
451 "react": "^19.2.0",
452 },
453 "devDependencies": {
454 "@types/node": "catalog:",
455 "@types/react": "^19.2.10",
456 "bun-types": "catalog:",
457 },
458 },
459 "plugins/openapi/sdk": {
460 "name": "@executor/plugin-openapi-sdk",
461 "version": "0.0.0",
462 "dependencies": {
463 "@executor/platform-sdk": "workspace:*",
464 "@executor/plugin-openapi-shared": "workspace:*",
413 "@executor/source-core": "workspace:*",465 "@executor/source-core": "workspace:*",
466 "effect": "catalog:",
414 },467 },
415 "devDependencies": {468 "devDependencies": {
416 "@types/node": "catalog:",469 "@types/node": "catalog:",
417 "bun-types": "catalog:",470 "bun-types": "catalog:",
418 },471 },
419 },472 },
420 "packages/sources/core": {473 "plugins/openapi/shared": {
421 "name": "@executor/source-core",474 "name": "@executor/plugin-openapi-shared",
422 "version": "0.0.0",475 "version": "0.0.0",
423 "dependencies": {476 "dependencies": {
424 "@executor/auth-oauth2": "workspace:*",477 "@executor/source-core": "workspace:*",
425 "@executor/codemode-core": "workspace:*",
426 "@executor/ir": "workspace:*",
427 "@modelcontextprotocol/sdk": "^1.26.0",
428 "effect": "catalog:",478 "effect": "catalog:",
479 "yaml": "^2.8.1",
429 },480 },
430 "devDependencies": {481 "devDependencies": {
431 "@types/node": "catalog:",482 "@types/node": "catalog:",
@@ -663,6 +714,14 @@ · 231 unmodified lines
663 714
664 "@executor/platform-sdk-file": ["@executor/platform-sdk-file@workspace:packages/platform/sdk-file"],715 "@executor/platform-sdk-file": ["@executor/platform-sdk-file@workspace:packages/platform/sdk-file"],
665 716
717 "@executor/plugin-openapi-http": ["@executor/plugin-openapi-http@workspace:plugins/openapi/http"],
718
719 "@executor/plugin-openapi-react": ["@executor/plugin-openapi-react@workspace:plugins/openapi/react"],
720
721 "@executor/plugin-openapi-sdk": ["@executor/plugin-openapi-sdk@workspace:plugins/openapi/sdk"],
722
723 "@executor/plugin-openapi-shared": ["@executor/plugin-openapi-shared@workspace:plugins/openapi/shared"],
724
666 "@executor/react": ["@executor/react@workspace:packages/clients/react"],725 "@executor/react": ["@executor/react@workspace:packages/clients/react"],
667 726
668 "@executor/runtime-deno-subprocess": ["@executor/runtime-deno-subprocess@workspace:packages/kernel/runtime-deno-subprocess"],727 "@executor/runtime-deno-subprocess": ["@executor/runtime-deno-subprocess@workspace:packages/kernel/runtime-deno-subprocess"],
@@ -675,8 +734,6 @@ · 6 unmodified lines
675 734
676 "@executor/server": ["@executor/server@workspace:packages/platform/server"],735 "@executor/server": ["@executor/server@workspace:packages/platform/server"],
677 736
678 "@executor/source-builtins": ["@executor/source-builtins@workspace:packages/sources/builtins"],
679
680 "@executor/source-core": ["@executor/source-core@workspace:packages/sources/core"],737 "@executor/source-core": ["@executor/source-core@workspace:packages/sources/core"],
681 738
682 "@executor/sqlite-sdk-consumer-example": ["@executor/sqlite-sdk-consumer-example@workspace:examples/sqlite-sdk-consumer"],739 "@executor/sqlite-sdk-consumer-example": ["@executor/sqlite-sdk-consumer-example@workspace:examples/sqlite-sdk-consumer"],
examples/sqlite-sdk-consumer/src/sqlite-backend.ts+2−465
@@ -20,19 +20,13 @@ import { · 19 unmodified lines
20 type ExecutorWorkspaceSourceArtifactRepository,20 type ExecutorWorkspaceSourceArtifactRepository,
21} from "@executor/platform-sdk";21} from "@executor/platform-sdk";
22import type {22import type {
23 AuthArtifact,
24 AuthLease,
25 Execution,23 Execution,
26 ExecutionInteraction,24 ExecutionInteraction,
27 ExecutionStep,25 ExecutionStep,
28 LocalExecutorConfig,26 LocalExecutorConfig,
29 LocalInstallation,27 LocalInstallation,
30 ProviderAuthGrant,
31 ScopeOauthClient,
32 ScopedSourceOauthClient,
33 SecretMaterial,28 SecretMaterial,
34 SecretRef,29 SecretRef,
35 SourceAuthSession,
36} from "@executor/platform-sdk/schema";30} from "@executor/platform-sdk/schema";
37import {31import {
38 ScopeIdSchema,32 ScopeIdSchema,
@@ -94,54 +88,6 @@ const sourceArtifacts = sqliteTable("source_artifacts", { · 55 unmodified lines
94 artifactJson: text("artifact_json").notNull(),88 artifactJson: text("artifact_json").notNull(),
95});89});
96 90
97const authArtifacts = sqliteTable("auth_artifacts", {
98 id: text("id").primaryKey(),
99 scopeId: text("scope_id").notNull(),
100 sourceId: text("source_id").notNull(),
101 actorScopeId: text("actor_scope_id"),
102 slot: text("slot").notNull(),
103 json: text("json").notNull(),
104});
105
106const authLeases = sqliteTable("auth_leases", {
107 authArtifactId: text("auth_artifact_id").primaryKey(),
108 json: text("json").notNull(),
109});
110
111const sourceOauthClients = sqliteTable("source_oauth_clients", {
112 id: text("id").primaryKey(),
113 scopeId: text("scope_id").notNull(),
114 sourceId: text("source_id").notNull(),
115 providerKey: text("provider_key").notNull(),
116 json: text("json").notNull(),
117});
118
119const scopeOauthClients = sqliteTable("scope_oauth_clients", {
120 id: text("id").primaryKey(),
121 scopeId: text("scope_id").notNull(),
122 providerKey: text("provider_key").notNull(),
123 json: text("json").notNull(),
124});
125
126const providerAuthGrants = sqliteTable("provider_auth_grants", {
127 id: text("id").primaryKey(),
128 scopeId: text("scope_id").notNull(),
129 actorScopeId: text("actor_scope_id"),
130 providerKey: text("provider_key").notNull(),
131 json: text("json").notNull(),
132});
133
134const sourceAuthSessions = sqliteTable("source_auth_sessions", {
135 id: text("id").primaryKey(),
136 scopeId: text("scope_id").notNull(),
137 sourceId: text("source_id").notNull(),
138 actorScopeId: text("actor_scope_id"),
139 state: text("state").notNull(),
140 status: text("status").notNull(),
141 credentialSlot: text("credential_slot"),
142 json: text("json").notNull(),
143});
144
145const secretMaterials = sqliteTable("secret_materials", {91const secretMaterials = sqliteTable("secret_materials", {
146 id: text("id").primaryKey(),92 id: text("id").primaryKey(),
147 providerId: text("provider_id").notNull(),93 providerId: text("provider_id").notNull(),
@@ -175,11 +121,6 @@ const executionSteps = sqliteTable("execution_steps", { · 27 unmodified lines
175const makeHash = (value: string): string =>121const makeHash = (value: string): string =>
176 createHash("sha256").update(value).digest("hex").slice(0, 24);122 createHash("sha256").update(value).digest("hex").slice(0, 24);
177 123
178const sameActor = (
179 left: string | null | undefined,
180 right: string | null | undefined,
181): boolean => (left ?? null) === (right ?? null);
182
183const parseJson = <T>(value: string): T => JSON.parse(value) as T;124const parseJson = <T>(value: string): T => JSON.parse(value) as T;
184 125
185const createSourceArtifact = (input: SourceArtifactBuildInput): SourceArtifact => {126const createSourceArtifact = (input: SourceArtifactBuildInput): SourceArtifact => {
@@ -190,10 +131,7 @@ const createSourceArtifact = (input: SourceArtifactBuildInput): SourceArtifact = · 4 unmodified lines
190 namespace: input.source.namespace,131 namespace: input.source.namespace,
191 name: input.source.name,132 name: input.source.name,
192 enabled: input.source.enabled,133 enabled: input.source.enabled,
193 binding: input.source.binding,134 sourceHash: input.source.sourceHash,
194 auth: input.source.auth,
195 importAuth: input.source.importAuth,
196 importAuthPolicy: input.source.importAuthPolicy,
197 });135 });
198 const importMetadataJson = JSON.stringify(snapshot.import);136 const importMetadataJson = JSON.stringify(snapshot.import);
199 const catalogId = SourceCatalogIdSchema.make(`src_catalog_${makeHash(sourceConfigJson)}`);137 const catalogId = SourceCatalogIdSchema.make(`src_catalog_${makeHash(sourceConfigJson)}`);
@@ -273,54 +211,6 @@ const openSqliteStore = (databasePath: string) => { · 73 unmodified lines
273 artifact_json TEXT NOT NULL211 artifact_json TEXT NOT NULL
274 );212 );
275 213
276 CREATE TABLE IF NOT EXISTS auth_artifacts (
277 id TEXT PRIMARY KEY NOT NULL,
278 scope_id TEXT NOT NULL,
279 source_id TEXT NOT NULL,
280 actor_scope_id TEXT,
281 slot TEXT NOT NULL,
282 json TEXT NOT NULL
283 );
284
285 CREATE TABLE IF NOT EXISTS auth_leases (
286 auth_artifact_id TEXT PRIMARY KEY NOT NULL,
287 json TEXT NOT NULL
288 );
289
290 CREATE TABLE IF NOT EXISTS source_oauth_clients (
291 id TEXT PRIMARY KEY NOT NULL,
292 scope_id TEXT NOT NULL,
293 source_id TEXT NOT NULL,
294 provider_key TEXT NOT NULL,
295 json TEXT NOT NULL
296 );
297
298 CREATE TABLE IF NOT EXISTS scope_oauth_clients (
299 id TEXT PRIMARY KEY NOT NULL,
300 scope_id TEXT NOT NULL,
301 provider_key TEXT NOT NULL,
302 json TEXT NOT NULL
303 );
304
305 CREATE TABLE IF NOT EXISTS provider_auth_grants (
306 id TEXT PRIMARY KEY NOT NULL,
307 scope_id TEXT NOT NULL,
308 actor_scope_id TEXT,
309 provider_key TEXT NOT NULL,
310 json TEXT NOT NULL
311 );
312
313 CREATE TABLE IF NOT EXISTS source_auth_sessions (
314 id TEXT PRIMARY KEY NOT NULL,
315 scope_id TEXT NOT NULL,
316 source_id TEXT NOT NULL,
317 actor_scope_id TEXT,
318 state TEXT NOT NULL,
319 status TEXT NOT NULL,
320 credential_slot TEXT,
321 json TEXT NOT NULL
322 );
323
324 CREATE TABLE IF NOT EXISTS secret_materials (214 CREATE TABLE IF NOT EXISTS secret_materials (
325 id TEXT PRIMARY KEY NOT NULL,215 id TEXT PRIMARY KEY NOT NULL,
326 provider_id TEXT NOT NULL,216 provider_id TEXT NOT NULL,
@@ -363,358 +253,6 @@ const openSqliteStore = (databasePath: string) => { · 36 unmodified lines
363type SqliteStore = ReturnType<typeof openSqliteStore>;253type SqliteStore = ReturnType<typeof openSqliteStore>;
364 254
365const createStorageDomains = (store: SqliteStore) => ({255const createStorageDomains = (store: SqliteStore) => ({
366 auth: {
367 artifacts: {
368 listByScopeId: (scopeId: AuthArtifact["scopeId"]) =>
369 store.db.select().from(authArtifacts).where(eq(authArtifacts.scopeId, scopeId)).all()
370 .map((row) => parseJson<AuthArtifact>(row.json)),
371 listByScopeAndSourceId: (input: {
372 scopeId: AuthArtifact["scopeId"];
373 sourceId: AuthArtifact["sourceId"];
374 }) =>
375 store.db.select().from(authArtifacts).where(
376 and(
377 eq(authArtifacts.scopeId, input.scopeId),
378 eq(authArtifacts.sourceId, input.sourceId),
379 ),
380 ).all().map((row) => parseJson<AuthArtifact>(row.json)),
381 getByScopeSourceAndActor: (input: {
382 scopeId: AuthArtifact["scopeId"];
383 sourceId: AuthArtifact["sourceId"];
384 actorScopeId: AuthArtifact["actorScopeId"];
385 slot: AuthArtifact["slot"];
386 }) =>
387 store.db.select().from(authArtifacts).where(
388 and(
389 eq(authArtifacts.scopeId, input.scopeId),
390 eq(authArtifacts.sourceId, input.sourceId),
391 eq(authArtifacts.slot, input.slot),
392 ),
393 ).all().map((row) => parseJson<AuthArtifact>(row.json))
394 .find((item) => sameActor(item.actorScopeId, input.actorScopeId)) ?? null,
395 upsert: (artifact: AuthArtifact) =>
396 store.db.insert(authArtifacts).values({
397 id: artifact.id,
398 scopeId: artifact.scopeId,
399 sourceId: artifact.sourceId,
400 actorScopeId: artifact.actorScopeId,
401 slot: artifact.slot,
402 json: JSON.stringify(artifact),
403 }).onConflictDoUpdate({
404 target: authArtifacts.id,
405 set: {
406 scopeId: artifact.scopeId,
407 sourceId: artifact.sourceId,
408 actorScopeId: artifact.actorScopeId,
409 slot: artifact.slot,
410 json: JSON.stringify(artifact),
411 },
412 }).run(),
413 removeByScopeSourceAndActor: (input: {
414 scopeId: AuthArtifact["scopeId"];
415 sourceId: AuthArtifact["sourceId"];
416 actorScopeId: AuthArtifact["actorScopeId"];
417 slot?: AuthArtifact["slot"];
418 }) => {
419 const rows = store.db.select().from(authArtifacts).where(
420 and(
421 eq(authArtifacts.scopeId, input.scopeId),
422 eq(authArtifacts.sourceId, input.sourceId),
423 ),
424 ).all();
425 const match = rows.find((row) => {
426 const item = parseJson<AuthArtifact>(row.json);
427 return sameActor(item.actorScopeId, input.actorScopeId)
428 && (input.slot === undefined || item.slot === input.slot);
429 });
430 if (!match) return false;
431 store.db.delete(authArtifacts).where(eq(authArtifacts.id, match.id)).run();
432 return true;
433 },
434 removeByScopeAndSourceId: (input: {
435 scopeId: AuthArtifact["scopeId"];
436 sourceId: AuthArtifact["sourceId"];
437 }) => {
438 const rows = store.db.select({ id: authArtifacts.id }).from(authArtifacts).where(
439 and(
440 eq(authArtifacts.scopeId, input.scopeId),
441 eq(authArtifacts.sourceId, input.sourceId),
442 ),
443 ).all();
444 store.db.delete(authArtifacts).where(
445 and(
446 eq(authArtifacts.scopeId, input.scopeId),
447 eq(authArtifacts.sourceId, input.sourceId),
448 ),
449 ).run();
450 return rows.length;
451 },
452 },
453 leases: {
454 listAll: () =>
455 store.db.select().from(authLeases).all().map((row) => parseJson<AuthLease>(row.json)),
456 getByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) => {
457 const row = store.db.select().from(authLeases).where(
458 eq(authLeases.authArtifactId, authArtifactId),
459 ).get();
460 return row ? parseJson<AuthLease>(row.json) : null;
461 },
462 upsert: (lease: AuthLease) =>
463 store.db.insert(authLeases).values({
464 authArtifactId: lease.authArtifactId,
465 json: JSON.stringify(lease),
466 }).onConflictDoUpdate({
467 target: authLeases.authArtifactId,
468 set: { json: JSON.stringify(lease) },
469 }).run(),
470 removeByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) => {
471 const row = store.db.select({ id: authLeases.authArtifactId }).from(authLeases).where(
472 eq(authLeases.authArtifactId, authArtifactId),
473 ).get();
474 if (!row) return false;
475 store.db.delete(authLeases).where(eq(authLeases.authArtifactId, authArtifactId)).run();
476 return true;
477 },
478 },
479 sourceOauthClients: {
480 getByScopeSourceAndProvider: (input: {
481 scopeId: ScopedSourceOauthClient["scopeId"];
482 sourceId: ScopedSourceOauthClient["sourceId"];
483 providerKey: string;
484 }) => {
485 const row = store.db.select().from(sourceOauthClients).where(
486 and(
487 eq(sourceOauthClients.scopeId, input.scopeId),
488 eq(sourceOauthClients.sourceId, input.sourceId),
489 eq(sourceOauthClients.providerKey, input.providerKey),
490 ),
491 ).get();
492 return row ? parseJson<ScopedSourceOauthClient>(row.json) : null;
493 },
494 upsert: (oauthClient: ScopedSourceOauthClient) =>
495 store.db.insert(sourceOauthClients).values({
496 id: oauthClient.id,
497 scopeId: oauthClient.scopeId,
498 sourceId: oauthClient.sourceId,
499 providerKey: oauthClient.providerKey,
500 json: JSON.stringify(oauthClient),
501 }).onConflictDoUpdate({
502 target: sourceOauthClients.id,
503 set: {
504 scopeId: oauthClient.scopeId,
505 sourceId: oauthClient.sourceId,
506 providerKey: oauthClient.providerKey,
507 json: JSON.stringify(oauthClient),
508 },
509 }).run(),
510 removeByScopeAndSourceId: (input: {
511 scopeId: ScopedSourceOauthClient["scopeId"];
512 sourceId: ScopedSourceOauthClient["sourceId"];
513 }) => {
514 const rows = store.db.select({ id: sourceOauthClients.id }).from(sourceOauthClients).where(
515 and(
516 eq(sourceOauthClients.scopeId, input.scopeId),
517 eq(sourceOauthClients.sourceId, input.sourceId),
518 ),
519 ).all();
520 store.db.delete(sourceOauthClients).where(
521 and(
522 eq(sourceOauthClients.scopeId, input.scopeId),
523 eq(sourceOauthClients.sourceId, input.sourceId),
524 ),
525 ).run();
526 return rows.length;
527 },
528 },
529 scopeOauthClients: {
530 listByScopeAndProvider: (input: {
531 scopeId: ScopeOauthClient["scopeId"];
532 providerKey: string;
533 }) =>
534 store.db.select().from(scopeOauthClients).where(
535 and(
536 eq(scopeOauthClients.scopeId, input.scopeId),
537 eq(scopeOauthClients.providerKey, input.providerKey),
538 ),
539 ).all().map((row) => parseJson<ScopeOauthClient>(row.json)),
540 getById: (id: ScopeOauthClient["id"]) => {
541 const row = store.db.select().from(scopeOauthClients).where(eq(scopeOauthClients.id, id)).get();
542 return row ? parseJson<ScopeOauthClient>(row.json) : null;
543 },
544 upsert: (oauthClient: ScopeOauthClient) =>
545 store.db.insert(scopeOauthClients).values({
546 id: oauthClient.id,
547 scopeId: oauthClient.scopeId,
548 providerKey: oauthClient.providerKey,
549 json: JSON.stringify(oauthClient),
550 }).onConflictDoUpdate({
551 target: scopeOauthClients.id,
552 set: {
553 scopeId: oauthClient.scopeId,
554 providerKey: oauthClient.providerKey,
555 json: JSON.stringify(oauthClient),
556 },
557 }).run(),
558 removeById: (id: ScopeOauthClient["id"]) => {
559 const row = store.db.select({ id: scopeOauthClients.id }).from(scopeOauthClients).where(
560 eq(scopeOauthClients.id, id),
561 ).get();
562 if (!row) return false;
563 store.db.delete(scopeOauthClients).where(eq(scopeOauthClients.id, id)).run();
564 return true;
565 },
566 },
567 providerGrants: {
568 listByScopeId: (scopeId: ProviderAuthGrant["scopeId"]) =>
569 store.db.select().from(providerAuthGrants).where(
570 eq(providerAuthGrants.scopeId, scopeId),
571 ).all().map((row) => parseJson<ProviderAuthGrant>(row.json)),
572 listByScopeActorAndProvider: (input: {
573 scopeId: ProviderAuthGrant["scopeId"];
574 actorScopeId: ProviderAuthGrant["actorScopeId"];
575 providerKey: string;
576 }) =>
577 store.db.select().from(providerAuthGrants).where(
578 and(
579 eq(providerAuthGrants.scopeId, input.scopeId),
580 eq(providerAuthGrants.providerKey, input.providerKey),
581 ),
582 ).all().map((row) => parseJson<ProviderAuthGrant>(row.json))
583 .filter((item) => sameActor(item.actorScopeId, input.actorScopeId)),
584 getById: (id: ProviderAuthGrant["id"]) => {
585 const row = store.db.select().from(providerAuthGrants).where(
586 eq(providerAuthGrants.id, id),
587 ).get();
588 return row ? parseJson<ProviderAuthGrant>(row.json) : null;
589 },
590 upsert: (grant: ProviderAuthGrant) =>
591 store.db.insert(providerAuthGrants).values({
592 id: grant.id,
593 scopeId: grant.scopeId,
594 actorScopeId: grant.actorScopeId,
595 providerKey: grant.providerKey,
596 json: JSON.stringify(grant),
597 }).onConflictDoUpdate({
598 target: providerAuthGrants.id,
599 set: {
600 scopeId: grant.scopeId,
601 actorScopeId: grant.actorScopeId,
602 providerKey: grant.providerKey,
603 json: JSON.stringify(grant),
604 },
605 }).run(),
606 removeById: (id: ProviderAuthGrant["id"]) => {
607 const row = store.db.select({ id: providerAuthGrants.id }).from(providerAuthGrants).where(
608 eq(providerAuthGrants.id, id),
609 ).get();
610 if (!row) return false;
611 store.db.delete(providerAuthGrants).where(eq(providerAuthGrants.id, id)).run();
612 return true;
613 },
614 },
615 sourceSessions: {
616 listAll: () =>
617 store.db.select().from(sourceAuthSessions).all().map((row) => parseJson<SourceAuthSession>(row.json)),
618 listByScopeId: (scopeId: SourceAuthSession["scopeId"]) =>
619 store.db.select().from(sourceAuthSessions).where(
620 eq(sourceAuthSessions.scopeId, scopeId),
621 ).all().map((row) => parseJson<SourceAuthSession>(row.json)),
622 getById: (id: SourceAuthSession["id"]) => {
623 const row = store.db.select().from(sourceAuthSessions).where(eq(sourceAuthSessions.id, id)).get();
624 return row ? parseJson<SourceAuthSession>(row.json) : null;
625 },
626 getByState: (state: SourceAuthSession["state"]) => {
627 const row = store.db.select().from(sourceAuthSessions).where(
628 eq(sourceAuthSessions.state, state),
629 ).get();
630 return row ? parseJson<SourceAuthSession>(row.json) : null;
631 },
632 getPendingByScopeSourceAndActor: (input: {
633 scopeId: SourceAuthSession["scopeId"];
634 sourceId: SourceAuthSession["sourceId"];
635 actorScopeId: SourceAuthSession["actorScopeId"];
636 credentialSlot?: SourceAuthSession["credentialSlot"];
637 }) =>
638 store.db.select().from(sourceAuthSessions).where(
639 and(
640 eq(sourceAuthSessions.scopeId, input.scopeId),
641 eq(sourceAuthSessions.sourceId, input.sourceId),
642 eq(sourceAuthSessions.status, "pending"),
643 ),
644 ).all().map((row) => parseJson<SourceAuthSession>(row.json))
645 .find((item) =>
646 sameActor(item.actorScopeId, input.actorScopeId)
647 && (input.credentialSlot === undefined || item.credentialSlot === input.credentialSlot)
648 ) ?? null,
649 insert: (session: SourceAuthSession) =>
650 store.db.insert(sourceAuthSessions).values({
651 id: session.id,
652 scopeId: session.scopeId,
653 sourceId: session.sourceId,
654 actorScopeId: session.actorScopeId,
655 state: session.state,
656 status: session.status,
657 credentialSlot: session.credentialSlot,
658 json: JSON.stringify(session),
659 }).run(),
660 update: (
661 id: SourceAuthSession["id"],
662 patch: Partial<Omit<SourceAuthSession, "id" | "scopeId" | "sourceId" | "createdAt">>,
663 ) => {
664 const current = store.db.select().from(sourceAuthSessions).where(eq(sourceAuthSessions.id, id)).get();
665 if (!current) return null;
666 const next = { ...parseJson<SourceAuthSession>(current.json), ...patch };
667 store.db.update(sourceAuthSessions).set({
668 state: next.state,
669 status: next.status,
670 credentialSlot: next.credentialSlot,
671 actorScopeId: next.actorScopeId,
672 json: JSON.stringify(next),
673 }).where(eq(sourceAuthSessions.id, id)).run();
674 return next;
675 },
676 upsert: (session: SourceAuthSession) =>
677 store.db.insert(sourceAuthSessions).values({
678 id: session.id,
679 scopeId: session.scopeId,
680 sourceId: session.sourceId,
681 actorScopeId: session.actorScopeId,
682 state: session.state,
683 status: session.status,
684 credentialSlot: session.credentialSlot,
685 json: JSON.stringify(session),
686 }).onConflictDoUpdate({
687 target: sourceAuthSessions.id,
688 set: {
689 scopeId: session.scopeId,
690 sourceId: session.sourceId,
691 actorScopeId: session.actorScopeId,
692 state: session.state,
693 status: session.status,
694 credentialSlot: session.credentialSlot,
695 json: JSON.stringify(session),
696 },
697 }).run(),
698 removeByScopeAndSourceId: (
699 scopeId: SourceAuthSession["scopeId"],
700 sourceId: SourceAuthSession["sourceId"],
701 ) => {
702 const rows = store.db.select({ id: sourceAuthSessions.id }).from(sourceAuthSessions).where(
703 and(
704 eq(sourceAuthSessions.scopeId, scopeId),
705 eq(sourceAuthSessions.sourceId, sourceId),
706 ),
707 ).all();
708 store.db.delete(sourceAuthSessions).where(
709 and(
710 eq(sourceAuthSessions.scopeId, scopeId),
711 eq(sourceAuthSessions.sourceId, sourceId),
712 ),
713 ).run();
714 return rows.length > 0;
715 },
716 },
717 },
718 secrets: {256 secrets: {
719 getById: (id: SecretMaterial["id"]) => {257 getById: (id: SecretMaterial["id"]) => {
720 const row = store.db.select().from(secretMaterials).where(eq(secretMaterials.id, id)).get();258 const row = store.db.select().from(secretMaterials).where(eq(secretMaterials.id, id)).get();
@@ -929,7 +467,7 @@ export const createSqliteExecutorBackend = ( · 208 unmodified lines
929 return createExecutorBackend({467 return createExecutorBackend({
930 loadRepositories: () => {468 loadRepositories: () => {
931 const store = openSqliteStore(databasePath);469 const store = openSqliteStore(databasePath);
932 const { auth, secrets, executions } = createStorageDomains(store);470 const { secrets, executions } = createStorageDomains(store);
933 471
934 return {472 return {
935 scope: {473 scope: {
@@ -1042,7 +580,6 @@ export const createSqliteExecutorBackend = ( · 106 unmodified lines
1042 store.db.delete(sourceArtifacts).where(eq(sourceArtifacts.sourceId, sourceId)).run();580 store.db.delete(sourceArtifacts).where(eq(sourceArtifacts.sourceId, sourceId)).run();
1043 },581 },
1044 },582 },
1045 sourceAuth: auth,
1046 },583 },
1047 secrets: {584 secrets: {
1048 ...secrets,585 ...secrets,
package.json+1−0
@@ -5,6 +5,7 @@ · 4 unmodified lines
5 "packageManager": "bun@1.2.22",5 "packageManager": "bun@1.2.22",
6 "workspaces": [6 "workspaces": [
7 "packages/*/*",7 "packages/*/*",
8 "plugins/*/*",
8 "apps/*",9 "apps/*",
9 "examples/*",10 "examples/*",
10 "apps/docs"11 "apps/docs"
packages/clients/react/package.json+1−1
@@ -13,6 +13,7 @@ · 12 unmodified lines
13 "dependencies": {13 "dependencies": {
14 "@effect-atom/atom": "0.5.3",14 "@effect-atom/atom": "0.5.3",
15 "@effect-atom/atom-react": "0.5.0",15 "@effect-atom/atom-react": "0.5.0",
16 "@effect/platform": "catalog:",
16 "@executor/platform-api": "workspace:*",17 "@executor/platform-api": "workspace:*",
17 "@executor/platform-sdk": "workspace:*",18 "@executor/platform-sdk": "workspace:*",
18 "effect": "catalog:"19 "effect": "catalog:"
@@ -21,7 +22,6 @@ · 2 unmodified lines
21 "react": ">=18 <20"22 "react": ">=18 <20"
22 },23 },
23 "devDependencies": {24 "devDependencies": {
24 "@effect/platform": "catalog:",
25 "@effect/platform-node": "catalog:",25 "@effect/platform-node": "catalog:",
26 "@effect/vitest": "catalog:",26 "@effect/vitest": "catalog:",
27 "@executor/effect-test-utils": "workspace:*",27 "@executor/effect-test-utils": "workspace:*",
packages/clients/react/src/index.ts+386−903
@@ -1,21 +1,30 @@
1import { Atom, Result } from "@effect-atom/atom";
2import type * as Registry from "@effect-atom/atom/Registry";
3import { RegistryContext, RegistryProvider, useAtomValue } from "@effect-atom/atom-react";
4import {1import {
5 createExecutorApiClient,2 Atom,
6 type ExecutorApiClient,3 AtomHttpApi,
7} from "@executor/platform-api/client";4 RegistryContext,
8import type {5 RegistryProvider,
9 CreateSecretPayload,6 Result,
10 CreateSecretResult,7 useAtomRefresh,
11 CreateSourcePayload,8 useAtomSet,
12 DeleteSecretResult,9 useAtomValue,
13 InstanceConfig,10} from "@effect-atom/atom-react";
14 LocalInstallation,11import {
15 SecretListItem,12 FetchHttpClient,
16 UpdateSecretPayload,13} from "@effect/platform";
17 UpdateSecretResult,14import type * as HttpApi from "@effect/platform/HttpApi";
18 UpdateSourcePayload,15import type * as HttpApiGroup from "@effect/platform/HttpApiGroup";
16import {
17 ExecutorApi,
18 createExecutorApi,
19 type CreateSecretPayload,
20 type CreateSecretResult,
21 type DeleteSecretResult,
22 type ExecutorHttpApiExtension,
23 type InstanceConfig,
24 type LocalInstallation,
25 type SecretListItem,
26 type UpdateSecretPayload,
27 type UpdateSecretResult,
19} from "@executor/platform-api";28} from "@executor/platform-api";
20import type {29import type {
21 Source,30 Source,
@@ -25,16 +34,14 @@ import type { · 3 unmodified lines
25} from "@executor/platform-sdk/schema";34} from "@executor/platform-sdk/schema";
26import * as Cause from "effect/Cause";35import * as Cause from "effect/Cause";
27import * as Effect from "effect/Effect";36import * as Effect from "effect/Effect";
28import * as Option from "effect/Option";
29import * as Runtime from "effect/Runtime";
30import * as React from "react";37import * as React from "react";
31 38
32const DEFAULT_EXECUTOR_API_BASE_URL = "http://127.0.0.1:8788";39const DEFAULT_EXECUTOR_API_BASE_URL = "http://127.0.0.1:8788";
33const PLACEHOLDER_WORKSPACE_ID = "ws_placeholder" as Source["scopeId"];40const PLACEHOLDER_WORKSPACE_ID = "ws_placeholder" as Source["scopeId"];
34const PLACEHOLDER_ACCOUNT_ID = "acc_placeholder";
35const PLACEHOLDER_SOURCE_ID = "src_placeholder" as Source["id"];
36 41
37type SourceMutationState<T> = {42type ReactivityKeys = Readonly<Record<string, ReadonlyArray<unknown>>>;
43
44type SecretMutationState<T> = {
38 status: "idle" | "pending" | "success" | "error";45 status: "idle" | "pending" | "success" | "error";
39 data: T | null;46 data: T | null;
40 error: Error | null;47 error: Error | null;
@@ -44,76 +51,14 @@ export type SourceRemoveResult = { · 3 unmodified lines
44 removed: boolean;51 removed: boolean;
45};52};
46 53
47type AtomKeyPart = string | number | boolean | null | undefined;54export type Loadable<T> =
48 55 | { status: "loading" }
49type SourcesKeyParts = readonly [boolean, Source["scopeId"], string];56 | { status: "error"; error: Error }
50type SourceKeyParts = readonly [boolean, Source["scopeId"], string, Source["id"]];57 | { status: "ready"; data: T };
51type SourceToolDetailKeyParts = readonly [
52 boolean,
53 Source["scopeId"],
54 string,
55 Source["id"],
56 string | null,
57];
58type SourceDiscoveryKeyParts = readonly [
59 boolean,
60 Source["scopeId"],
61 string,
62 Source["id"],
63 string,
64 number | null,
65];
66type InvalidationTarget = {
67 workspaceId?: Source["scopeId"];
68 accountId?: string;
69 sourceId?: Source["id"];
70};
71
72type ActiveQueryCollections = {
73 sourceLists: Set<string>;
74 sources: Set<string>;
75 inspections: Set<string>;
76 toolDetails: Set<string>;
77 discoveries: Set<string>;
78};
79
80type ExecutorQueryContextValue = {
81 registry: Registry.Registry;
82 activeQueries: ActiveQueryCollections;
83 invalidateQueries: (target?: InvalidationTarget) => void;
84};
85 58
86type MutationExecutionContext = {59type WorkspaceContext = {
60 installation: LocalInstallation;
87 workspaceId: Source["scopeId"];61 workspaceId: Source["scopeId"];
88 accountId: string;
89 registry: Registry.Registry;
90 invalidateQueries: (target?: InvalidationTarget) => void;
91};
92
93type OptimisticMutationResult<T> =
94 | void
95 | (() => void)
96 | {
97 rollback?: () => void;
98 value?: T;
99 };
100
101type MutationOptions<TInput, TOutput, TOptimistic = never> = {
102 optimisticUpdate?: (
103 context: MutationExecutionContext,
104 payload: TInput,
105 ) => OptimisticMutationResult<TOptimistic>;
106 onSuccess?: (
107 context: MutationExecutionContext,
108 payload: TInput,
109 data: TOutput,
110 optimisticValue: TOptimistic | undefined,
111 ) => void;
112};
113
114type InternalNode<A> = {
115 setValue: (value: A) => void;
116 valueOption?: () => Option.Option<A>;
117};62};
118 63
119let apiBaseUrl =64let apiBaseUrl =
@@ -121,266 +66,14 @@ let apiBaseUrl = · 1 unmodified line
121 ? window.location.origin66 ? window.location.origin
122 : DEFAULT_EXECUTOR_API_BASE_URL;67 : DEFAULT_EXECUTOR_API_BASE_URL;
123 68
124const ExecutorQueryContext = React.createContext<ExecutorQueryContextValue | null>(null);69const pendingResultAtom = Atom.make(
125 70 Effect.never as Effect.Effect<never, Error>,
126const encodeAtomKey = (parts: ReadonlyArray<AtomKeyPart>): string => JSON.stringify(parts);71).pipe(Atom.keepAlive);
127
128const decodeAtomKey = <T extends ReadonlyArray<AtomKeyPart>>(key: string): T => JSON.parse(key) as T;
129
130const encodeSourcesKey = (
131 enabled: boolean,
132 workspaceId: Source["scopeId"],
133 accountId: string,
134): string => encodeAtomKey([enabled, workspaceId, accountId] satisfies SourcesKeyParts);
135
136const encodeSourceKey = (
137 enabled: boolean,
138 workspaceId: Source["scopeId"],
139 accountId: string,
140 sourceId: Source["id"],
141): string => encodeAtomKey([enabled, workspaceId, accountId, sourceId] satisfies SourceKeyParts);
142
143const encodeToolDetailKey = (
144 enabled: boolean,
145 workspaceId: Source["scopeId"],
146 accountId: string,
147 sourceId: Source["id"],
148 toolPath: string | null,
149): string =>
150 encodeAtomKey([enabled, workspaceId, accountId, sourceId, toolPath] satisfies SourceToolDetailKeyParts);
151
152const encodeDiscoveryKey = (
153 enabled: boolean,
154 workspaceId: Source["scopeId"],
155 accountId: string,
156 sourceId: Source["id"],
157 query: string,
158 limit: number | null,
159): string =>
160 encodeAtomKey([enabled, workspaceId, accountId, sourceId, query, limit] satisfies SourceDiscoveryKeyParts);
161
162const toError = (cause: unknown): Error =>
163 cause instanceof Error ? cause : new Error(String(cause));
164 72
165const causeMessage = (cause: Cause.Cause<unknown>): Error =>73const causeMessage = (cause: Cause.Cause<unknown>): Error =>
166 new Error(Cause.pretty(cause));74 new Error(Cause.pretty(cause));
167 75
168const shouldLogExecutorDevErrors = (): boolean => {76const toLoadable = <T>(result: Result.Result<T, unknown>): Loadable<T> => {
169 if (typeof window === "undefined") {
170 return false;
171 }
172
173 const hostname = window.location.hostname;
174 return hostname === "localhost" || hostname === "127.0.0.1";
175};
176
177const describeExecutorDevError = (cause: unknown): Record<string, unknown> => {
178 if (Runtime.isFiberFailure(cause)) {
179 const inner = cause[Runtime.FiberFailureCauseId];
180 return {
181 name: cause.name,
182 message: cause.message,
183 cause: Cause.pretty(inner),
184 };
185 }
186
187 if (cause instanceof Error) {
188 return {
189 name: cause.name,
190 message: cause.message,
191 stack: cause.stack,
192 };
193 }
194
195 return {
196 message: String(cause),
197 };
198};
199
200const logExecutorDevError = (label: string, details: Record<string, unknown>): void => {
201 if (!shouldLogExecutorDevErrors()) {
202 return;
203 }
204
205 console.error(`[executor react] ${label}`, details);
206};
207
208const runControlPlane = async <A>(input: {
209 baseUrl?: string;
210 accountId?: string;
211 execute: (client: ExecutorApiClient) => Promise<A>;
212}): Promise<A> => {
213 const baseUrl = input.baseUrl ?? apiBaseUrl;
214 const accountId = input.accountId;
215
216 try {
217 const client = await createExecutorApiClient({
218 baseUrl,
219 ...(accountId !== undefined ? { accountId } : {}),
220 });
221 return await input.execute(client);
222 } catch (cause) {
223 const error = toError(cause);
224 logExecutorDevError("control-plane request failed", {
225 baseUrl,
226 accountId,
227 error: describeExecutorDevError(error),
228 });
229 throw error;
230 }
231};
232
233const controlPlaneRequest = <A>(input: {
234 baseUrl?: string;
235 accountId?: string;
236 execute: (client: ExecutorApiClient) => Promise<A>;
237}): Effect.Effect<A, Error> =>
238 Effect.tryPromise({
239 try: () => runControlPlane(input),
240 catch: toError,
241 });
242
243const localInstallationAtom = Atom.family((baseUrl: string) =>
244 Atom.make(
245 controlPlaneRequest({
246 baseUrl,
247 execute: (client) => client.local.installation({}),
248 }),
249 ).pipe(Atom.keepAlive),
250);
251
252const instanceConfigAtom = Atom.family((baseUrl: string) =>
253 Atom.make(
254 controlPlaneRequest({
255 baseUrl,
256 execute: (client) => client.local.config({}),
257 }),
258 ).pipe(Atom.keepAlive),
259);
260
261const secretsAtom = Atom.family((baseUrl: string) =>
262 Atom.make(
263 controlPlaneRequest({
264 baseUrl,
265 execute: (client) => client.local.listSecrets({}),
266 }),
267 ).pipe(Atom.keepAlive),
268);
269
270const sourcesAtom = Atom.family((key: string) => {
271 const [enabled, workspaceId, accountId] = decodeAtomKey<SourcesKeyParts>(key);
272
273 return Atom.make(
274 enabled
275 ? controlPlaneRequest({
276 accountId,
277 execute: (client) => client.sources.list({
278 path: {
279 workspaceId,
280 },
281 }),
282 })
283 : Effect.never,
284 ).pipe(Atom.keepAlive);
285});
286
287const sourceAtom = Atom.family((key: string) => {
288 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceKeyParts>(key);
289
290 return Atom.make(
291 enabled
292 ? controlPlaneRequest({
293 accountId,
294 execute: (client) => client.sources.get({
295 path: {
296 workspaceId,
297 sourceId,
298 },
299 }),
300 })
301 : Effect.never,
302 ).pipe(Atom.keepAlive);
303});
304
305const sourceInspectionAtom = Atom.family((key: string) => {
306 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceKeyParts>(key);
307
308 return Atom.make(
309 enabled
310 ? controlPlaneRequest({
311 accountId,
312 execute: (client) => client.sources.inspection({
313 path: {
314 workspaceId,
315 sourceId,
316 },
317 }),
318 })
319 : Effect.never,
320 ).pipe(Atom.keepAlive);
321});
322
323const sourceInspectionToolAtom = Atom.family((key: string) => {
324 const [enabled, workspaceId, accountId, sourceId, toolPath] = decodeAtomKey<SourceToolDetailKeyParts>(key);
325
326 return Atom.make(
327 enabled && toolPath
328 ? controlPlaneRequest({
329 accountId,
330 execute: (client) => client.sources.inspectionTool({
331 path: {
332 workspaceId,
333 sourceId,
334 toolPath,
335 },
336 }),
337 })
338 : Effect.succeed<SourceInspectionToolDetail | null>(null),
339 ).pipe(Atom.keepAlive);
340});
341
342const sourceDiscoveryAtom = Atom.family((key: string) => {
343 const [enabled, workspaceId, accountId, sourceId, query, limit] = decodeAtomKey<SourceDiscoveryKeyParts>(key);
344
345 return Atom.make(
346 !enabled
347 ? Effect.never
348 : query.trim().length === 0
349 ? Effect.succeed<SourceInspectionDiscoverResult>({
350 query: "",
351 queryTokens: [],
352 bestPath: null,
353 total: 0,
354 results: [],
355 })
356 : controlPlaneRequest({
357 accountId,
358 execute: (client) => client.sources.inspectionDiscover({
359 path: {
360 workspaceId,
361 sourceId,
362 },
363 payload: {
364 query,
365 ...(limit !== null ? { limit } : {}),
366 },
367 }),
368 }),
369 ).pipe(Atom.keepAlive);
370});
371
372export type Loadable<T> =
373 | { status: "loading" }
374 | { status: "error"; error: Error }
375 | { status: "ready"; data: T };
376
377type WorkspaceContext = {
378 installation: LocalInstallation;
379 workspaceId: Source["scopeId"];
380 accountId: string;
381};
382
383const toLoadable = <T>(result: Result.Result<T, Error>): Loadable<T> => {
384 if (Result.isSuccess(result)) {77 if (Result.isSuccess(result)) {
385 return {78 return {
386 status: "ready",79 status: "ready",
@@ -412,310 +105,298 @@ const pendingLoadable = <T>(workspace: Loadable<WorkspaceContext>): Loadable<T> · 25 unmodified lines
412 throw new Error("Expected workspace loadable to be pending or errored");105 throw new Error("Expected workspace loadable to be pending or errored");
413};106};
414 107
415const useLoadableAtom = <T>(atom: Atom.Atom<Result.Result<T, Error>>): Loadable<T> => {108const useLoadableAtom = <T>(atom: Atom.Atom<Result.Result<T, unknown>>): Loadable<T> => {
416 const result = useAtomValue(atom);109 const result = useAtomValue(atom);
417 return React.useMemo(() => toLoadable(result), [result]);110 return React.useMemo(() => toLoadable(result), [result]);
418};111};
419 112
420const useWorkspaceContext = (): Loadable<WorkspaceContext> => {113const disabledAtom = <T>() =>
421 const installation = useLoadableAtom(localInstallationAtom(apiBaseUrl));114 pendingResultAtom as unknown as Atom.Atom<Result.Result<T, unknown>>;
422 115
423 return React.useMemo(() => {116export const getExecutorApiBaseUrl = (): string => apiBaseUrl;
424 if (installation.status !== "ready") {
425 return installation;
426 }
427 117
428 return {118export const setExecutorApiBaseUrl = (baseUrl: string): void => {
429 status: "ready",119 apiBaseUrl = baseUrl;
430 data: {
431 installation: installation.data,
432 workspaceId: installation.data.scopeId,
433 accountId: installation.data.actorScopeId,
434 },
435 } satisfies Loadable<WorkspaceContext>;
436 }, [installation]);
437};120};
438 121
439const useWorkspaceRequestContext = () => {122export const defineExecutorHttpApiClient =
440 const workspace = useWorkspaceContext();123 <Self>() =>
441 const enabled = workspace.status === "ready";124 <
125 const Id extends string,
126 ApiId extends string,
127 Groups extends HttpApiGroup.HttpApiGroup.Any,
128 ApiE,
129 R,
130 >(
131 id: Id,
132 api: HttpApi.HttpApi<ApiId, Groups, ApiE, R>,
133 ) => {
134 const build = (baseUrl: string | URL) =>
135 AtomHttpApi.Tag<Self>()(id, {
136 api,
137 httpClient: FetchHttpClient.layer as any,
138 baseUrl,
139 });
442 140
443 const workspaceId = enabled141 const cache = new Map<string, ReturnType<typeof build>>();
444 ? workspace.data.workspaceId
445 : PLACEHOLDER_WORKSPACE_ID;
446 const accountId = enabled
447 ? workspace.data.accountId
448 : PLACEHOLDER_ACCOUNT_ID;
449 142
450 return React.useMemo(143 return (baseUrl: string | URL = getExecutorApiBaseUrl()) => {
451 () => ({144 const key = String(baseUrl);
452 workspace,145 const cached = cache.get(key);
453 enabled,146 if (cached !== undefined) {
454 workspaceId,147 return cached;
455 accountId,148 }
456 }),
457 [accountId, enabled, workspace, workspaceId],
458 );
459};
460 149
461const getCachedAtomValue = <A>(150 const client = build(baseUrl);
462 registry: Registry.Registry,151 cache.set(key, client);
463 atom: Atom.Atom<Result.Result<A, Error>>,152 return client;
464): A | undefined => {153 };
465 const node = (registry.getNodes().get(atom) ?? null) as InternalNode<Result.Result<A, Error>> | null;154 };
466 if (node === null || typeof node.valueOption !== "function") {
467 return undefined;
468 }
469 155
470 const option = node.valueOption();156export const defineExecutorPluginHttpApiClient =
471 if (Option.isNone(option)) {157 <Self>() =>
472 return undefined;158 <
473 }159 const Id extends string,
160 TExtensions extends readonly ExecutorHttpApiExtension[],
161 >(
162 id: Id,
163 extensions: TExtensions,
164 ) =>
165 defineExecutorHttpApiClient<Self>()(
166 id,
167 createExecutorApi({
168 plugins: extensions,
169 }),
170 );
171
172const getExecutorApiHttpClient = defineExecutorHttpApiClient<"ExecutorReactHttpClient">()(
173 "ExecutorReactHttpClient",
174 ExecutorApi,
175);
474 176
475 if (!Result.isSuccess(option.value)) {177const localInstallationReactivityKey = (): ReactivityKeys => ({
476 return undefined;178 localInstallation: [],
477 }179});
478 180
479 return option.value.value;181const instanceConfigReactivityKey = (): ReactivityKeys => ({
480};182 instanceConfig: [],
183});
481 184
482const setCachedAtomValue = <A>(185const secretsReactivityKey = (): ReactivityKeys => ({
483 registry: Registry.Registry,186 secrets: [],
484 atom: Atom.Atom<Result.Result<A, Error>>,187});
485 value: A,
486): void => {
487 const ensureNode = (registry as {
488 ensureNode?: (atom: Atom.Atom<Result.Result<A, Error>>) => InternalNode<Result.Result<A, Error>>;
489 }).ensureNode;
490 if (typeof ensureNode !== "function") {
491 return;
492 }
493 188
494 ensureNode.call(registry, atom).setValue(Result.success(value));189const sourcesReactivityKey = (workspaceId: Source["scopeId"]): ReactivityKeys => ({
495};190 sources: [workspaceId],
191});
496 192
497const createActiveQueryCollections = (): ActiveQueryCollections => ({193const sourceReactivityKey = (
498 sourceLists: new Set(),194 workspaceId: Source["scopeId"],
499 sources: new Set(),195 sourceId: Source["id"],
500 inspections: new Set(),196): ReactivityKeys => ({
501 toolDetails: new Set(),197 source: [workspaceId, sourceId],
502 discoveries: new Set(),
503});198});
504 199
505const targetMatches = (200const sourceInspectionReactivityKey = (
506 target: InvalidationTarget | undefined,
507 workspaceId: Source["scopeId"],201 workspaceId: Source["scopeId"],
508 accountId: string,202 sourceId: Source["id"],
509 sourceId?: Source["id"],203): ReactivityKeys => ({
510): boolean => {204 sourceInspection: [workspaceId, sourceId],
511 if (target?.workspaceId !== undefined && target.workspaceId !== workspaceId) {205});
512 return false;
513 }
514 if (target?.accountId !== undefined && target.accountId !== accountId) {
515 return false;
516 }
517 if (target?.sourceId !== undefined && target.sourceId !== sourceId) {
518 return false;
519 }
520 return true;
521};
522 206
523const invalidateTrackedQueries = (207const sourceInspectionToolReactivityKey = (
524 registry: Registry.Registry,208 workspaceId: Source["scopeId"],
525 activeQueries: ActiveQueryCollections,209 sourceId: Source["id"],
526 target?: InvalidationTarget,210 toolPath?: string | null,
527): void => {211): ReactivityKeys => ({
528 activeQueries.sourceLists.forEach((key) => {212 sourceInspectionTool:
529 const [enabled, workspaceId, accountId] = decodeAtomKey<SourcesKeyParts>(key);213 toolPath === undefined || toolPath === null
530 if (enabled && targetMatches(target, workspaceId, accountId)) {214 ? [workspaceId, sourceId]
531 registry.refresh(sourcesAtom(key));215 : [workspaceId, sourceId, toolPath],
532 }216});
533 });
534 217
535 activeQueries.sources.forEach((key) => {218const sourceDiscoveryReactivityKey = (
536 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceKeyParts>(key);219 workspaceId: Source["scopeId"],
537 if (enabled && targetMatches(target, workspaceId, accountId, sourceId)) {220 sourceId: Source["id"],
538 registry.refresh(sourceAtom(key));221 query?: string,
539 }222 limit?: number | null,
540 });223): ReactivityKeys => ({
224 sourceDiscovery:
225 query === undefined
226 ? [workspaceId, sourceId]
227 : [workspaceId, sourceId, query, limit ?? null],
228});
541 229
542 activeQueries.inspections.forEach((key) => {230const localInstallationAtom = (baseUrl: string) =>
543 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceKeyParts>(key);231 getExecutorApiHttpClient(baseUrl).query("local", "installation", {
544 if (enabled && targetMatches(target, workspaceId, accountId, sourceId)) {232 reactivityKeys: localInstallationReactivityKey(),
545 registry.refresh(sourceInspectionAtom(key));233 timeToLive: "5 minutes",
546 }
547 });234 });
548 235
549 activeQueries.toolDetails.forEach((key) => {236const instanceConfigAtom = (baseUrl: string) =>
550 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceToolDetailKeyParts>(key);237 getExecutorApiHttpClient(baseUrl).query("local", "config", {
551 if (enabled && targetMatches(target, workspaceId, accountId, sourceId)) {238 reactivityKeys: instanceConfigReactivityKey(),
552 registry.refresh(sourceInspectionToolAtom(key));239 timeToLive: "5 minutes",
553 }
554 });240 });
555 241
556 activeQueries.discoveries.forEach((key) => {242const secretsAtom = (baseUrl: string) =>
557 const [enabled, workspaceId, accountId, sourceId] = decodeAtomKey<SourceDiscoveryKeyParts>(key);243 getExecutorApiHttpClient(baseUrl).query("local", "listSecrets", {
558 if (enabled && targetMatches(target, workspaceId, accountId, sourceId)) {244 reactivityKeys: secretsReactivityKey(),
559 registry.refresh(sourceDiscoveryAtom(key));245 timeToLive: "1 minute",
560 }
561 });246 });
562};
563 247
564const useExecutorQueryContext = (): ExecutorQueryContextValue => {248const sourcesAtom = (workspaceId: Source["scopeId"]) =>
565 const context = React.useContext(ExecutorQueryContext);249 getExecutorApiHttpClient().query("sources", "list", {
566 if (context === null) {250 path: {
567 throw new Error("ExecutorReactProvider is missing from the React tree");251 workspaceId,
568 }252 },
569 return context;253 reactivityKeys: sourcesReactivityKey(workspaceId),
570};254 timeToLive: "30 seconds",
571 255 });
572const useTrackActiveKey = (
573 collection: keyof ActiveQueryCollections,
574 key: string,
575 enabled: boolean,
576): void => {
577 const { activeQueries } = useExecutorQueryContext();
578 256
579 React.useEffect(() => {257const sourceAtom = (
580 if (!enabled) {258 workspaceId: Source["scopeId"],
581 return;259 sourceId: Source["id"],
582 }260) =>
261 getExecutorApiHttpClient().query("sources", "get", {
262 path: {
263 workspaceId,
264 sourceId,
265 },
266 reactivityKeys: sourceReactivityKey(workspaceId, sourceId),
267 timeToLive: "30 seconds",
268 });
583 269
584 const bucket = activeQueries[collection];270const sourceInspectionAtom = (
585 bucket.add(key);271 workspaceId: Source["scopeId"],
586 return () => {272 sourceId: Source["id"],
587 bucket.delete(key);273) =>
588 };274 getExecutorApiHttpClient().query("sources", "inspection", {
589 }, [activeQueries, collection, enabled, key]);275 path: {
590};276 workspaceId,
277 sourceId,
278 },
279 reactivityKeys: sourceInspectionReactivityKey(workspaceId, sourceId),
280 timeToLive: "30 seconds",
281 });
591 282
592const upsertSourceInList = (283const sourceInspectionToolAtom = (
593 sources: ReadonlyArray<Source>,284 workspaceId: Source["scopeId"],
594 nextSource: Source,285 sourceId: Source["id"],
595): ReadonlyArray<Source> => {286 toolPath: string | null,
596 const index = sources.findIndex((source) => source.id === nextSource.id);287) =>
597 if (index === -1) {288 toolPath === null
598 return [nextSource, ...sources];289 ? disabledAtom<SourceInspectionToolDetail | null>()
599 }290 : getExecutorApiHttpClient().query("sources", "inspectionTool", {
291 path: {
292 workspaceId,
293 sourceId,
294 toolPath,
295 },
296 reactivityKeys: sourceInspectionToolReactivityKey(
297 workspaceId,
298 sourceId,
299 toolPath,
300 ),
301 timeToLive: "30 seconds",
302 });
600 303
601 const next = sources.slice();304const emptyDiscoveryResult: SourceInspectionDiscoverResult = {
602 next[index] = nextSource;305 query: "",
603 return next;306 queryTokens: [],
307 bestPath: null,
308 total: 0,
309 results: [],
604};310};
605 311
606const removeSourceFromList = (312const sourceDiscoveryAtom = (
607 sources: ReadonlyArray<Source>,313 workspaceId: Source["scopeId"],
608 sourceId: Source["id"],314 sourceId: Source["id"],
609): ReadonlyArray<Source> => sources.filter((source) => source.id !== sourceId);315 query: string,
316 limit: number | null,
317) =>
318 query.trim().length === 0
319 ? Atom.make(
320 Effect.succeed(emptyDiscoveryResult),
321 ).pipe(Atom.keepAlive)
322 : getExecutorApiHttpClient().query("sources", "inspectionDiscover", {
323 path: {
324 workspaceId,
325 sourceId,
326 },
327 payload: {
328 query,
329 ...(limit !== null ? { limit } : {}),
330 },
331 reactivityKeys: sourceDiscoveryReactivityKey(
332 workspaceId,
333 sourceId,
334 query,
335 limit,
336 ),
337 timeToLive: "15 seconds",
338 });
610 339
611const createOptimisticSource = (input: {340const useWorkspaceContext = (): Loadable<WorkspaceContext> => {
612 workspaceId: Source["scopeId"];341 const installation = useLoadableAtom(localInstallationAtom(getExecutorApiBaseUrl()));
613 payload: CreateSourcePayload;
614}): Source => {
615 const now = Date.now();
616 342
617 return {343 return React.useMemo(() => {
618 id: `src_optimistic_${crypto.randomUUID()}` as Source["id"],344 if (installation.status !== "ready") {
619 scopeId: input.workspaceId,345 return installation;
620 name: input.payload.name,346 }
621 kind: input.payload.kind,347
622 endpoint: input.payload.endpoint,348 return {
623 status: input.payload.status ?? "draft",349 status: "ready",
624 enabled: input.payload.enabled ?? true,350 data: {
625 namespace: input.payload.namespace ?? null,351 installation: installation.data,
626 bindingVersion: 1,352 workspaceId: installation.data.scopeId,
627 binding: input.payload.binding ?? {},353 },
628 importAuthPolicy: input.payload.importAuthPolicy ?? "reuse_runtime",354 } satisfies Loadable<WorkspaceContext>;
629 importAuth: input.payload.importAuth ?? { kind: "none" },355 }, [installation]);
630 auth: input.payload.auth ?? { kind: "none" },
631 sourceHash: input.payload.sourceHash ?? null,
632 lastError: input.payload.lastError ?? null,
633 createdAt: now,
634 updatedAt: now,
635 };
636};356};
637 357
638const applyUpdatePayloadToSource = (source: Source, payload: UpdateSourcePayload): Source => ({358const useWorkspaceRequestContext = () => {
639 ...source,359 const workspace = useWorkspaceContext();
640 name: payload.name ?? source.name,360 const enabled = workspace.status === "ready";
641 endpoint: payload.endpoint ?? source.endpoint,
642 status: payload.status ?? source.status,
643 enabled: payload.enabled ?? source.enabled,
644 namespace: payload.namespace !== undefined ? payload.namespace : source.namespace,
645 binding: payload.binding !== undefined ? payload.binding : source.binding,
646 auth: payload.auth !== undefined ? payload.auth : source.auth,
647 sourceHash: payload.sourceHash !== undefined ? payload.sourceHash : source.sourceHash,
648 lastError: payload.lastError !== undefined ? payload.lastError : source.lastError,
649 updatedAt: Date.now(),
650});
651 361
652const useSourceMutation = <TInput, TOutput, TOptimistic = never>(362 return React.useMemo(
653 execute: (input: {363 () => ({
654 workspaceId: Source["scopeId"];364 workspace,
655 accountId: string;365 enabled,
656 payload: TInput;366 workspaceId: enabled
657 }) => Promise<TOutput>,367 ? workspace.data.workspaceId
658 options?: MutationOptions<TInput, TOutput, TOptimistic>,368 : PLACEHOLDER_WORKSPACE_ID,
369 }),
370 [enabled, workspace],
371 );
372};
373
374export const useExecutorMutation = <TInput, TOutput>(
375 execute: (input: TInput) => Promise<TOutput>,
659) => {376) => {
660 const workspace = useWorkspaceRequestContext();377 const [state, setState] = React.useState<SecretMutationState<TOutput>>({
661 const { registry, invalidateQueries } = useExecutorQueryContext();
662 const [state, setState] = React.useState<SourceMutationState<TOutput>>({
663 status: "idle",378 status: "idle",
664 data: null,379 data: null,
665 error: null,380 error: null,
666 });381 });
667 382
668 const mutateAsync = React.useCallback(async (payload: TInput) => {383 const mutateAsync = React.useCallback(async (payload: TInput) => {
669 if (!workspace.enabled) {
670 const error = new Error("Executor workspace context is not ready");
671 setState({ status: "error", data: null, error });
672 throw error;
673 }
674
675 const executionContext: MutationExecutionContext = {
676 workspaceId: workspace.workspaceId,
677 accountId: workspace.accountId,
678 registry,
679 invalidateQueries,
680 };
681
682 setState((current) => ({384 setState((current) => ({
683 status: "pending",385 status: "pending",
684 data: current.data,386 data: current.data,
685 error: null,387 error: null,
686 }));388 }));
687 389
688 const optimistic = options?.optimisticUpdate?.(executionContext, payload);
689 const rollback = typeof optimistic === "function"
690 ? optimistic
691 : optimistic?.rollback;
692 const optimisticValue = typeof optimistic === "function"
693 ? undefined
694 : optimistic?.value;
695
696 try {390 try {
697 const data = await execute({391 const data = await execute(payload);
698 workspaceId: workspace.workspaceId,
699 accountId: workspace.accountId,
700 payload,
701 });
702 options?.onSuccess?.(executionContext, payload, data, optimisticValue);
703 setState({ status: "success", data, error: null });392 setState({ status: "success", data, error: null });
704 return data;393 return data;
705 } catch (cause) {394 } catch (cause) {
706 rollback?.();
707 logExecutorDevError("source mutation failed", {
708 workspaceId: workspace.workspaceId,
709 accountId: workspace.accountId,
710 payload,
711 error: describeExecutorDevError(cause),
712 cause,
713 });
714 const error = cause instanceof Error ? cause : new Error(String(cause));395 const error = cause instanceof Error ? cause : new Error(String(cause));
715 setState({ status: "error", data: null, error });396 setState({ status: "error", data: null, error });
716 throw error;397 throw error;
717 }398 }
718 }, [execute, invalidateQueries, options, registry, workspace.accountId, workspace.enabled, workspace.workspaceId]);399 }, [execute]);
719 400
720 const reset = React.useCallback(() => {401 const reset = React.useCallback(() => {
721 setState({ status: "idle", data: null, error: null });402 setState({ status: "idle", data: null, error: null });
@@ -731,184 +412,105 @@ const useSourceMutation = <TInput, TOutput, TOptimistic = never>( · 9 unmodified lines
731 );412 );
732};413};
733 414
734const ExecutorReactProviderInner = (props: React.PropsWithChildren) => {
735 const registry = React.useContext(RegistryContext);
736 const activeQueries = React.useMemo(createActiveQueryCollections, []);
737 const invalidateQueries = React.useCallback((target?: InvalidationTarget) => {
738 invalidateTrackedQueries(registry, activeQueries, target);
739 }, [activeQueries, registry]);
740
741 const value = React.useMemo<ExecutorQueryContextValue>(() => ({
742 registry,
743 activeQueries,
744 invalidateQueries,
745 }), [activeQueries, invalidateQueries, registry]);
746
747 return React.createElement(ExecutorQueryContext.Provider, { value }, props.children);
748};
749
750export const setExecutorApiBaseUrl = (baseUrl: string): void => {
751 apiBaseUrl = baseUrl;
752};
753
754export const ExecutorReactProvider = (props: React.PropsWithChildren) =>415export const ExecutorReactProvider = (props: React.PropsWithChildren) =>
755 React.createElement(416 React.createElement(RegistryProvider, null, props.children);
756 RegistryProvider,
757 null,
758 React.createElement(ExecutorReactProviderInner, null, props.children),
759 );
760
761export const useInvalidateExecutorQueries = (): (() => void) => {
762 const { invalidateQueries } = useExecutorQueryContext();
763 return React.useCallback(() => {
764 invalidateQueries();
765 }, [invalidateQueries]);
766};
767 417
768export const useLocalInstallation = (): Loadable<LocalInstallation> =>418export const useLocalInstallation = (): Loadable<LocalInstallation> =>
769 useLoadableAtom(localInstallationAtom(apiBaseUrl));419 useLoadableAtom(localInstallationAtom(getExecutorApiBaseUrl()));
770 420
771export const useInstanceConfig = (): Loadable<InstanceConfig> =>421export const useInstanceConfig = (): Loadable<InstanceConfig> =>
772 useLoadableAtom(instanceConfigAtom(apiBaseUrl));422 useLoadableAtom(instanceConfigAtom(getExecutorApiBaseUrl()));
773 423
774export const useSecrets = (): Loadable<ReadonlyArray<SecretListItem>> =>424export const useSecrets = (): Loadable<ReadonlyArray<SecretListItem>> =>
775 useLoadableAtom(secretsAtom(apiBaseUrl));425 useLoadableAtom(secretsAtom(getExecutorApiBaseUrl()));
776
777export const useRefreshSecrets = (): (() => void) => {
778 const registry = React.useContext(RegistryContext);
779 return React.useCallback(() => {
780 registry.refresh(secretsAtom(apiBaseUrl));
781 }, [registry]);
782};
783
784type SecretMutationState<T> = {
785 status: "idle" | "pending" | "success" | "error";
786 data: T | null;
787 error: Error | null;
788};
789
790const useSecretMutation = <TInput, TOutput>(
791 execute: (input: TInput) => Promise<TOutput>,
792) => {
793 const registry = React.useContext(RegistryContext);
794 const [state, setState] = React.useState<SecretMutationState<TOutput>>({
795 status: "idle",
796 data: null,
797 error: null,
798 });
799
800 const mutateAsync = React.useCallback(async (payload: TInput) => {
801 setState((current) => ({
802 status: "pending",
803 data: current.data,
804 error: null,
805 }));
806
807 try {
808 const data = await execute(payload);
809 registry.refresh(secretsAtom(apiBaseUrl));
810 setState({ status: "success", data, error: null });
811 return data;
812 } catch (cause) {
813 const error = cause instanceof Error ? cause : new Error(String(cause));
814 setState({ status: "error", data: null, error });
815 throw error;
816 }
817 }, [execute, registry]);
818 426
819 const reset = React.useCallback(() => {427export const useRefreshSecrets = (): (() => void) =>
820 setState({ status: "idle", data: null, error: null });428 useAtomRefresh(secretsAtom(getExecutorApiBaseUrl()));
821 }, []);
822 429
823 return React.useMemo(430export const useCreateSecret = () => {
824 () => ({431 const mutate = useAtomSet(
825 ...state,432 getExecutorApiHttpClient().mutation("local", "createSecret"),
826 mutateAsync,433 { mode: "promise" },
827 reset,
828 }),
829 [mutateAsync, reset, state],
830 );434 );
831};
832 435
833export const useCreateSecret = () =>436 return useExecutorMutation<CreateSecretPayload, CreateSecretResult>(
834 useSecretMutation<CreateSecretPayload, CreateSecretResult>(
835 React.useCallback(437 React.useCallback(
836 (payload) =>438 (payload) =>
837 runControlPlane({439 mutate({
838 execute: (client) => client.local.createSecret({440 payload,
839 payload,441 reactivityKeys: secretsReactivityKey(),
840 }),
841 }),442 }),
842 [],443 [mutate],
843 ),444 ),
844 );445 );
446};
845 447
846export const useUpdateSecret = () =>448export const useUpdateSecret = () => {
847 useSecretMutation<{ secretId: string; payload: UpdateSecretPayload }, UpdateSecretResult>(449 const mutate = useAtomSet(
450 getExecutorApiHttpClient().mutation("local", "updateSecret"),
451 { mode: "promise" },
452 );
453
454 return useExecutorMutation<
455 { secretId: string; payload: UpdateSecretPayload },
456 UpdateSecretResult
457 >(
848 React.useCallback(458 React.useCallback(
849 (input) =>459 (input) =>
850 runControlPlane({460 mutate({
851 execute: (client) => client.local.updateSecret({461 path: { secretId: input.secretId },
852 path: { secretId: input.secretId },462 payload: input.payload,
853 payload: input.payload,463 reactivityKeys: secretsReactivityKey(),
854 }),
855 }),464 }),
856 [],465 [mutate],
857 ),466 ),
858 );467 );
468};
859 469
860export const useDeleteSecret = () =>470export const useDeleteSecret = () => {
861 useSecretMutation<string, DeleteSecretResult>(471 const mutate = useAtomSet(
472 getExecutorApiHttpClient().mutation("local", "deleteSecret"),
473 { mode: "promise" },
474 );
475
476 return useExecutorMutation<string, DeleteSecretResult>(
862 React.useCallback(477 React.useCallback(
863 (secretId) =>478 (secretId) =>
864 runControlPlane({479 mutate({
865 execute: (client) => client.local.deleteSecret({480 path: { secretId },
866 path: { secretId },481 reactivityKeys: secretsReactivityKey(),
867 }),
868 }),482 }),
869 [],483 [mutate],
870 ),484 ),
871 );485 );
486};
872 487
873export const useSources = (): Loadable<ReadonlyArray<Source>> => {488export const useSources = (): Loadable<ReadonlyArray<Source>> => {
874 const workspace = useWorkspaceRequestContext();489 const workspace = useWorkspaceRequestContext();
875 const key = encodeSourcesKey(workspace.enabled, workspace.workspaceId, workspace.accountId);490 const atom = workspace.enabled
876 useTrackActiveKey("sourceLists", key, workspace.enabled);491 ? sourcesAtom(workspace.workspaceId)
877 const sources = useLoadableAtom(sourcesAtom(key));492 : disabledAtom<ReadonlyArray<Source>>();
493 const sources = useLoadableAtom(atom);
878 494
879 return workspace.enabled ? sources : pendingLoadable(workspace.workspace);495 return workspace.enabled ? sources : pendingLoadable(workspace.workspace);
880};496};
881 497
882export const useSource = (sourceId: string): Loadable<Source> => {498export const useSource = (sourceId: string): Loadable<Source> => {
883 const workspace = useWorkspaceRequestContext();499 const workspace = useWorkspaceRequestContext();
884 const requestedSourceId = workspace.enabled500 const atom = workspace.enabled
885 ? (sourceId as Source["id"])501 ? sourceAtom(workspace.workspaceId, sourceId as Source["id"])
886 : PLACEHOLDER_SOURCE_ID;502 : disabledAtom<Source>();
887 const key = encodeSourceKey(503 const source = useLoadableAtom(atom);
888 workspace.enabled,
889 workspace.workspaceId,
890 workspace.accountId,
891 requestedSourceId,
892 );
893 useTrackActiveKey("sources", key, workspace.enabled);
894 const source = useLoadableAtom(sourceAtom(key));
895 504
896 return workspace.enabled ? source : pendingLoadable(workspace.workspace);505 return workspace.enabled ? source : pendingLoadable(workspace.workspace);
897};506};
898 507
899export const useSourceInspection = (sourceId: string): Loadable<SourceInspection> => {508export const useSourceInspection = (sourceId: string): Loadable<SourceInspection> => {
900 const workspace = useWorkspaceRequestContext();509 const workspace = useWorkspaceRequestContext();
901 const requestedSourceId = workspace.enabled510 const atom = workspace.enabled
902 ? (sourceId as Source["id"])511 ? sourceInspectionAtom(workspace.workspaceId, sourceId as Source["id"])
903 : PLACEHOLDER_SOURCE_ID;512 : disabledAtom<SourceInspection>();
904 const key = encodeSourceKey(513 const inspection = useLoadableAtom(atom);
905 workspace.enabled,
906 workspace.workspaceId,
907 workspace.accountId,
908 requestedSourceId,
909 );
910 useTrackActiveKey("inspections", key, workspace.enabled);
911 const inspection = useLoadableAtom(sourceInspectionAtom(key));
912 514
913 return workspace.enabled ? inspection : pendingLoadable(workspace.workspace);515 return workspace.enabled ? inspection : pendingLoadable(workspace.workspace);
914};516};
@@ -918,18 +520,14 @@ export const useSourceToolDetail = ( · 3 unmodified lines
918 toolPath: string | null,520 toolPath: string | null,
919): Loadable<SourceInspectionToolDetail | null> => {521): Loadable<SourceInspectionToolDetail | null> => {
920 const workspace = useWorkspaceRequestContext();522 const workspace = useWorkspaceRequestContext();
921 const requestedSourceId = workspace.enabled523 const atom = workspace.enabled
922 ? (sourceId as Source["id"])524 ? sourceInspectionToolAtom(
923 : PLACEHOLDER_SOURCE_ID;525 workspace.workspaceId,
924 const key = encodeToolDetailKey(526 sourceId as Source["id"],
925 workspace.enabled,527 toolPath,
926 workspace.workspaceId,528 )
927 workspace.accountId,529 : disabledAtom<SourceInspectionToolDetail | null>();
928 requestedSourceId,530 const detail = useLoadableAtom(atom);
929 toolPath,
930 );
931 useTrackActiveKey("toolDetails", key, workspace.enabled && toolPath !== null);
932 const detail = useLoadableAtom(sourceInspectionToolAtom(key));
933 531
934 return workspace.enabled ? detail : pendingLoadable(workspace.workspace);532 return workspace.enabled ? detail : pendingLoadable(workspace.workspace);
935};533};
@@ -940,19 +538,15 @@ export const useSourceDiscovery = (input: { · 4 unmodified lines
940 limit?: number;538 limit?: number;
941}): Loadable<SourceInspectionDiscoverResult> => {539}): Loadable<SourceInspectionDiscoverResult> => {
942 const workspace = useWorkspaceRequestContext();540 const workspace = useWorkspaceRequestContext();
943 const requestedSourceId = workspace.enabled541 const atom = workspace.enabled
944 ? (input.sourceId as Source["id"])542 ? sourceDiscoveryAtom(
945 : PLACEHOLDER_SOURCE_ID;543 workspace.workspaceId,
946 const key = encodeDiscoveryKey(544 input.sourceId as Source["id"],
947 workspace.enabled,545 input.query,
948 workspace.workspaceId,546 input.limit ?? null,
949 workspace.accountId,547 )
950 requestedSourceId,548 : disabledAtom<SourceInspectionDiscoverResult>();
951 input.query,549 const results = useLoadableAtom(atom);
952 input.limit ?? null,
953 );
954 useTrackActiveKey("discoveries", key, workspace.enabled);
955 const results = useLoadableAtom(sourceDiscoveryAtom(key));
956 550
957 return workspace.enabled ? results : pendingLoadable(workspace.workspace);551 return workspace.enabled ? results : pendingLoadable(workspace.workspace);
958};552};
@@ -963,184 +557,74 @@ export const usePrefetchToolDetail = () => { · 4 unmodified lines
963 557
964 return React.useCallback(558 return React.useCallback(
965 (sourceId: string, toolPath: string): (() => void) => {559 (sourceId: string, toolPath: string): (() => void) => {
966 if (!workspace.enabled) return () => {};560 if (!workspace.enabled) {
967 const requestedSourceId = sourceId as Source["id"];561 return () => {};
968 const atom = sourceInspectionToolAtom(562 }
969 encodeToolDetailKey(563
970 workspace.enabled,564 return registry.mount(
565 sourceInspectionToolAtom(
971 workspace.workspaceId,566 workspace.workspaceId,
972 workspace.accountId,567 sourceId as Source["id"],
973 requestedSourceId,
974 toolPath,568 toolPath,
975 ),569 ),
976 );570 );
977 return registry.mount(atom);
978 },571 },
979 [registry, workspace.accountId, workspace.enabled, workspace.workspaceId],572 [registry, workspace.enabled, workspace.workspaceId],
980 );573 );
981};574};
982 575
983export const useCreateSource = () =>576export const useRemoveSource = () => {
984 useSourceMutation<CreateSourcePayload, Source, Source>(577 const workspace = useWorkspaceRequestContext();
985 React.useCallback(578 const mutate = useAtomSet(
986 ({ workspaceId, accountId, payload }) =>579 getExecutorApiHttpClient().mutation("sources", "remove"),
987 runControlPlane({580 { mode: "promise" },
988 accountId,
989 execute: (client) => client.sources.create({
990 path: {
991 workspaceId,
992 },
993 payload,
994 }),
995 }),
996 [],
997 ),
998 {
999 optimisticUpdate: (context, payload) => {
1000 const listAtom = sourcesAtom(encodeSourcesKey(true, context.workspaceId, context.accountId));
1001 const previousList = getCachedAtomValue(context.registry, listAtom);
1002 if (previousList === undefined) {
1003 return;
1004 }
1005
1006 const optimisticSource = createOptimisticSource({
1007 workspaceId: context.workspaceId,
1008 payload,
1009 });
1010 setCachedAtomValue(context.registry, listAtom, [optimisticSource, ...previousList]);
1011 return {
1012 value: optimisticSource,
1013 rollback: () => {
1014 setCachedAtomValue(context.registry, listAtom, previousList);
1015 },
1016 };
1017 },
1018 onSuccess: (context, _payload, source, optimisticSource) => {
1019 const listAtom = sourcesAtom(encodeSourcesKey(true, context.workspaceId, context.accountId));
1020 const currentList = getCachedAtomValue(context.registry, listAtom);
1021 if (currentList !== undefined) {
1022 const withoutOptimistic = optimisticSource
1023 ? currentList.filter((candidate) => candidate.id !== optimisticSource.id)
1024 : currentList;
1025 setCachedAtomValue(context.registry, listAtom, upsertSourceInList(withoutOptimistic, source));
1026 }
1027
1028 setCachedAtomValue(
1029 context.registry,
1030 sourceAtom(encodeSourceKey(true, context.workspaceId, context.accountId, source.id)),
1031 source,
1032 );
1033 context.invalidateQueries({
1034 workspaceId: context.workspaceId,
1035 accountId: context.accountId,
1036 });
1037 },
1038 },
1039 );581 );
1040 582
1041export const useUpdateSource = () =>583 return useExecutorMutation<Source["id"], SourceRemoveResult>(
1042 useSourceMutation<{ sourceId: Source["id"]; payload: UpdateSourcePayload }, Source>(
1043 React.useCallback(584 React.useCallback(
1044 ({ workspaceId, accountId, payload }) =>585 (sourceId) => {
1045 runControlPlane({586 if (!workspace.enabled) {
1046 accountId,587 return Promise.reject(
1047 execute: (client) => client.sources.update({588 new Error("Executor workspace context is not ready"),
1048 path: {589 );
1049 workspaceId,
1050 sourceId: payload.sourceId,
1051 },
1052 payload: payload.payload,
1053 }),
1054 }),
1055 [],
1056 ),
1057 {
1058 optimisticUpdate: (context, input) => {
1059 const listAtom = sourcesAtom(encodeSourcesKey(true, context.workspaceId, context.accountId));
1060 const detailAtom = sourceAtom(encodeSourceKey(true, context.workspaceId, context.accountId, input.sourceId));
1061 const previousList = getCachedAtomValue(context.registry, listAtom);
1062 const previousSource = getCachedAtomValue(context.registry, detailAtom)
1063 ?? previousList?.find((source) => source.id === input.sourceId);
1064 if (previousSource === undefined) {
1065 return;
1066 }590 }
1067 591
1068 const optimisticSource = applyUpdatePayloadToSource(previousSource, input.payload);592 return mutate({
1069 if (previousList !== undefined) {593 path: {
1070 setCachedAtomValue(context.registry, listAtom, upsertSourceInList(previousList, optimisticSource));594 workspaceId: workspace.workspaceId,
1071 }595 sourceId,
1072 setCachedAtomValue(context.registry, detailAtom, optimisticSource);596 },
1073 597 reactivityKeys: {
1074 return () => {598 ...sourcesReactivityKey(workspace.workspaceId),
1075 if (previousList !== undefined) {599 ...sourceReactivityKey(workspace.workspaceId, sourceId),
1076 setCachedAtomValue(context.registry, listAtom, previousList);600 ...sourceInspectionReactivityKey(workspace.workspaceId, sourceId),
1077 }601 ...sourceInspectionToolReactivityKey(workspace.workspaceId, sourceId),
1078 setCachedAtomValue(context.registry, detailAtom, previousSource);602 ...sourceDiscoveryReactivityKey(workspace.workspaceId, sourceId),
1079 };603 },
1080 },
1081 onSuccess: (context, input, source) => {
1082 const listAtom = sourcesAtom(encodeSourcesKey(true, context.workspaceId, context.accountId));
1083 const detailAtom = sourceAtom(encodeSourceKey(true, context.workspaceId, context.accountId, input.sourceId));
1084 const currentList = getCachedAtomValue(context.registry, listAtom);
1085 if (currentList !== undefined) {
1086 setCachedAtomValue(context.registry, listAtom, upsertSourceInList(currentList, source));
1087 }
1088 setCachedAtomValue(context.registry, detailAtom, source);
1089 context.invalidateQueries({
1090 workspaceId: context.workspaceId,
1091 accountId: context.accountId,
1092 sourceId: input.sourceId,
1093 });604 });
1094 },605 },
1095 },606 [mutate, workspace.enabled, workspace.workspaceId],
1096 );
1097
1098export const useRemoveSource = () =>
1099 useSourceMutation<Source["id"], SourceRemoveResult>(
1100 React.useCallback(
1101 ({ workspaceId, accountId, payload }) =>
1102 runControlPlane({
1103 accountId,
1104 execute: (client) => client.sources.remove({
1105 path: {
1106 workspaceId,
1107 sourceId: payload,
1108 },
1109 }),
1110 }),
1111 [],
1112 ),607 ),
1113 {
1114 optimisticUpdate: (context, sourceId) => {
1115 const listAtom = sourcesAtom(encodeSourcesKey(true, context.workspaceId, context.accountId));
1116 const previousList = getCachedAtomValue(context.registry, listAtom);
1117 if (previousList === undefined) {
1118 return;
1119 }
1120
1121 setCachedAtomValue(context.registry, listAtom, removeSourceFromList(previousList, sourceId));
1122 return () => {
1123 setCachedAtomValue(context.registry, listAtom, previousList);
1124 };
1125 },
1126 onSuccess: (context, sourceId) => {
1127 context.invalidateQueries({
1128 workspaceId: context.workspaceId,
1129 accountId: context.accountId,
1130 sourceId,
1131 });
1132 },
1133 },
1134 );608 );
609};
610
611export {
612 Atom,
613 AtomHttpApi,
614 RegistryContext,
615 RegistryProvider,
616 Result,
617 useAtomRefresh,
618 useAtomSet,
619 useAtomValue,
620};
1135 621
1136export type {622export type {
1137 CreateSecretPayload,623 CreateSecretPayload,
1138 CreateSecretResult,624 CreateSecretResult,
1139 CreateSourcePayload,
1140 DeleteSecretResult,625 DeleteSecretResult,
1141 InstanceConfig,626 InstanceConfig,
1142 LocalInstallation,627 LocalInstallation,
1143
1144 SecretListItem,628 SecretListItem,
1145 Source,629 Source,
1146 SourceInspection,630 SourceInspection,
@@ -1148,5 +632,4 @@ export type { · 1 unmodified line
1148 SourceInspectionToolDetail,632 SourceInspectionToolDetail,
1149 UpdateSecretPayload,633 UpdateSecretPayload,
1150 UpdateSecretResult,634 UpdateSecretResult,
1151 UpdateSourcePayload,
1152};635};
packages/hosts/mcp/src/index.ts+3−11
@@ -6,7 +6,6 @@ import { · 5 unmodified lines
6 RuntimeSourceCatalogStoreService,6 RuntimeSourceCatalogStoreService,
7 createExecution,7 createExecution,
8 getExecution,8 getExecution,
9 hasRegisteredExecutorAddableSourceAdapters,
10 resumeExecution,9 resumeExecution,
11 type ExecutorRuntime,10 type ExecutorRuntime,
12} from "@executor/platform-sdk/runtime";11} from "@executor/platform-sdk/runtime";
@@ -173,16 +172,9 @@ const buildExecuteWorkflowText = ( · 160 unmodified lines
173 '1) const matches = await tools.discover({ query: "<intent>", limit: 12 });',172 '1) const matches = await tools.discover({ query: "<intent>", limit: 12 });',
174 "2) const details = await tools.describe.tool({ path, includeSchemas: true });",173 "2) const details = await tools.describe.tool({ path, includeSchemas: true });",
175 "3) Call selected tools.<path>(input).",174 "3) Call selected tools.<path>(input).",
176 ...(hasRegisteredExecutorAddableSourceAdapters175 "4) Source plugins are not registered in this build.",
177 ? [176 ...EXECUTOR_SOURCES_ADD_HELP_LINES,
178 "4) To connect a source plugin, call tools.executor.sources.add(...).",177 "5) If execution pauses for interaction, resume it with the returned resumePayload or the available resume flow.",
179 ...EXECUTOR_SOURCES_ADD_HELP_LINES,
180 "5) If execution pauses for interaction, resume it with the returned resumePayload or the available resume flow.",
181 ]
182 : [
183 "4) Source plugins are not registered in this build.",
184 "5) If execution pauses for interaction, resume it with the returned resumePayload or the available resume flow.",
185 ]),
186 "Do not use fetch; use tools.* only.",178 "Do not use fetch; use tools.* only.",
187 ].join("\n");179 ].join("\n");
188 180
packages/hosts/mcp/src/paused-result.test.ts+0−101
@@ -1,101 +0,0 @@
1import { describe, expect, it } from "@effect/vitest";
2import type {
3 ExecutionEnvelope,
4 ExecutionInteraction,
5} from "@executor/platform-sdk/schema";
6
7import {
8 buildPausedResultText,
9 parseInteractionPayload,
10} from "./paused-result";
11
12const makeInteraction = (patch: Partial<ExecutionInteraction> = {}): ExecutionInteraction => ({
13 id: "exec_123:source_connect_oauth2:call_1" as never,
14 executionId: "exec_123" as never,
15 status: "pending",
16 kind: "url",
17 purpose: "source_connect_oauth2",
18 payloadJson: JSON.stringify({
19 elicitation: {
20 mode: "url",
21 message: "Finish connecting Axiom",
22 url: "https://mcp.axiom.co/authorize",
23 elicitationId: "oauth-session-123",
24 },
25 }),
26 responseJson: null,
27 responsePrivateJson: null,
28 createdAt: 1,
29 updatedAt: 1,
30 ...patch,
31});
32
33const makeEnvelope = (
34 patch: Partial<ExecutionEnvelope> = {},
35): ExecutionEnvelope => ({
36 execution: {
37 id: "exec_123" as never,
38 scopeId: "ws_123" as never,
39 createdByScopeId: "acct_123" as never,
40 status: "waiting_for_interaction",
41 code: "return await tools.executor.sources.add({ endpoint: 'https://mcp.axiom.co/mcp' });",
42 resultJson: null,
43 errorText: null,
44 logsJson: null,
45 startedAt: 1,
46 completedAt: null,
47 createdAt: 1,
48 updatedAt: 1,
49 },
50 pendingInteraction: makeInteraction(),
51 ...patch,
52});
53
54describe("paused-result", () => {
55 it("parses the browser URL from a paused interaction payload", () => {
56 const parsed = parseInteractionPayload(makeInteraction());
57
58 expect(parsed).not.toBeNull();
59 expect(parsed?.mode).toBe("url");
60 expect(parsed?.url).toBe("https://mcp.axiom.co/authorize");
61 expect(parsed?.elicitationId).toBe("oauth-session-123");
62 });
63
64 it("includes URL and resume instructions in the paused text for browser auth", () => {
65 const text = buildPausedResultText(makeEnvelope());
66
67 expect(text).toContain("Finish connecting Axiom");
68 expect(text).toContain("Open this URL in a browser:");
69 expect(text).toContain("https://mcp.axiom.co/authorize");
70 expect(text).toContain("executor.resume");
71 expect(text).toContain('"executionId": "exec_123"');
72 });
73
74 it("includes resume guidance for form interactions", () => {
75 const text = buildPausedResultText(
76 makeEnvelope({
77 pendingInteraction: makeInteraction({
78 kind: "form",
79 purpose: "tool_execution_gate",
80 payloadJson: JSON.stringify({
81 elicitation: {
82 mode: "form",
83 message: "Approve the tool call",
84 requestedSchema: {
85 type: "object",
86 properties: {
87 approve: { type: "boolean" },
88 },
89 required: ["approve"],
90 },
91 },
92 }),
93 }),
94 }),
95 );
96
97 expect(text).toContain("Approve the tool call");
98 expect(text).toContain("include a response object matching the requested schema");
99 expect(text).toContain('"executionId": "exec_123"');
100 });
101});
packages/kernel/core/src/tool-map.ts+3−3
@@ -448,9 +448,9 @@ export function toolDescriptorsFromTools(input: { · 447 unmodified lines
448 interaction: metadata?.interaction,448 interaction: metadata?.interaction,
449 elicitation: metadata?.elicitation,449 elicitation: metadata?.elicitation,
450 contract,450 contract,
451 ...(metadata?.providerKind ? { providerKind: metadata.providerKind } : {}),451 ...(metadata?.pluginKind ? { pluginKind: metadata.pluginKind } : {}),
452 ...(metadata?.providerData !== undefined452 ...(metadata?.pluginData !== undefined
453 ? { providerData: metadata.providerData }453 ? { pluginData: metadata.pluginData }
454 : {}),454 : {}),
455 } satisfies ToolDescriptor;455 } satisfies ToolDescriptor;
456 });456 });
packages/kernel/core/src/types.ts+6−6
@@ -98,8 +98,8 @@ export type ToolMetadata = { · 97 unmodified lines
98 elicitation?: ElicitationRequest;98 elicitation?: ElicitationRequest;
99 contract?: ToolContract;99 contract?: ToolContract;
100 sourceKey?: string;100 sourceKey?: string;
101 providerKind?: string;101 pluginKind?: string;
102 providerData?: unknown;102 pluginData?: unknown;
103};103};
104 104
105export type ToolExecutionContext = {105export type ToolExecutionContext = {
@@ -145,8 +145,8 @@ export type ToolDescriptor = { · 39 unmodified lines
145 interaction?: "auto" | "required";145 interaction?: "auto" | "required";
146 elicitation?: ElicitationRequest;146 elicitation?: ElicitationRequest;
147 contract?: ToolContract;147 contract?: ToolContract;
148 providerKind?: string;148 pluginKind?: string;
149 providerData?: unknown;149 pluginData?: unknown;
150};150};
151 151
152export const ToolDescriptorSchema = Schema.Struct({152export const ToolDescriptorSchema = Schema.Struct({
@@ -158,8 +158,8 @@ export const ToolDescriptorSchema = Schema.Struct({ · 5 unmodified lines
158 ),158 ),
159 elicitation: Schema.optional(Schema.Unknown),159 elicitation: Schema.optional(Schema.Unknown),
160 contract: Schema.optional(ToolContractSchema),160 contract: Schema.optional(ToolContractSchema),
161 providerKind: Schema.optional(Schema.String),161 pluginKind: Schema.optional(Schema.String),
162 providerData: Schema.optional(Schema.Unknown),162 pluginData: Schema.optional(Schema.Unknown),
163});163});
164 164
165export type ToolNamespace = {165export type ToolNamespace = {
packages/kernel/ir/src/catalog.ts+1−1
@@ -845,7 +845,7 @@ const searchDoc = ( · 844 unmodified lines
845 capability.executableIds845 capability.executableIds
846 .map((executableId: ExecutableId) =>846 .map((executableId: ExecutableId) =>
847 catalog.executables[executableId]?.display?.protocol847 catalog.executables[executableId]?.display?.protocol
848 ?? catalog.executables[executableId]?.adapterKey848 ?? catalog.executables[executableId]?.pluginKey
849 )849 )
850 .filter((protocol): protocol is string => protocol !== undefined),850 .filter((protocol): protocol is string => protocol !== undefined),
851 ),851 ),
packages/kernel/ir/src/model.ts+2−2
@@ -158,7 +158,7 @@ export const EntityBaseSchema = Schema.Struct({ · 157 unmodified lines
158 158
159export const ImportMetadataSchema = Schema.Struct({159export const ImportMetadataSchema = Schema.Struct({
160 sourceKind: SourceKindSchema,160 sourceKind: SourceKindSchema,
161 adapterKey: Schema.String,161 pluginKey: Schema.String,
162 importerVersion: Schema.String,162 importerVersion: Schema.String,
163 importedAt: Schema.String,163 importedAt: Schema.String,
164 sourceConfigHash: Schema.String,164 sourceConfigHash: Schema.String,
@@ -696,7 +696,7 @@ export const ExecutableSchema = Schema.extend( · 531 unmodified lines
696 id: ExecutableIdSchema,696 id: ExecutableIdSchema,
697 capabilityId: CapabilityIdSchema,697 capabilityId: CapabilityIdSchema,
698 scopeId: ScopeIdSchema,698 scopeId: ScopeIdSchema,
699 adapterKey: Schema.String,699 pluginKey: Schema.String,
700 bindingVersion: Schema.Number,700 bindingVersion: Schema.Number,
701 binding: Schema.Unknown,701 binding: Schema.Unknown,
702 projection: ExecutableProjectionSchema,702 projection: ExecutableProjectionSchema,
packages/platform/api/package.json+1−1
@@ -5,9 +5,9 @@ · 4 unmodified lines
5 "exports": {5 "exports": {
6 ".": "./src/index.ts",6 ".": "./src/index.ts",
7 "./api": "./src/api.ts",7 "./api": "./src/api.ts",
8 "./client": "./src/client.ts",
9 "./effect": "./src/effect.ts",8 "./effect": "./src/effect.ts",
10 "./http": "./src/http.ts",9 "./http": "./src/http.ts",
10 "./plugins": "./src/plugins.ts",
11 "./errors": "./src/errors.ts",11 "./errors": "./src/errors.ts",
12 "./local/api": "./src/local/api.ts",12 "./local/api": "./src/local/api.ts",
13 "./local/http": "./src/local/http.ts",13 "./local/http": "./src/local/http.ts",
packages/platform/api/src/api.ts+57−3
@@ -1,11 +1,17 @@
1import { HttpApi, OpenApi } from "@effect/platform";1import { HttpApi, OpenApi } from "@effect/platform";
2import type { HttpApiGroup } from "@effect/platform";
2 3
3import { ExecutionsApi } from "./executions/api";4import { ExecutionsApi } from "./executions/api";
4import { LocalApi } from "./local/api";5import { LocalApi } from "./local/api";
5import { PoliciesApi } from "./policies/api";6import { PoliciesApi } from "./policies/api";
6import { SourcesApi } from "./sources/api";7import { SourcesApi } from "./sources/api";
8import type {
9 ExecutorHttpApiExtension,
10 ExecutorHttpPlugin,
11 ExecutorHttpPluginGroups,
12} from "./plugins";
7 13
8export class ExecutorApi extends HttpApi.make("executor")14export const CoreExecutorApi = HttpApi.make("executor")
9 .add(LocalApi)15 .add(LocalApi)
10 .add(SourcesApi)16 .add(SourcesApi)
11 .add(PoliciesApi)17 .add(PoliciesApi)
@@ -15,6 +21,54 @@ export class ExecutorApi extends HttpApi.make("executor") · 3 unmodified lines
15 title: "Executor API",21 title: "Executor API",
16 description: "Local-first API for workspace sources, policies, auth, and execution",22 description: "Local-first API for workspace sources, policies, auth, and execution",
17 }),23 }),
18 ) {}24 );
19 25
20export const executorOpenApiSpec = OpenApi.fromApi(ExecutorApi);26type HttpApiGroupsOf<TApi> = TApi extends HttpApi.HttpApi<any, infer TGroups, any, any>
27 ? TGroups
28 : never;
29type HttpApiErrorOf<TApi> = TApi extends HttpApi.HttpApi<any, any, infer TError, any>
30 ? TError
31 : never;
32type HttpApiContextOf<TApi> = TApi extends HttpApi.HttpApi<any, any, any, infer TContext>
33 ? TContext
34 : never;
35
36export type CoreExecutorApiGroups = HttpApiGroupsOf<typeof CoreExecutorApi>;
37export type ExecutorApiWithPlugins<
38 TPlugins extends readonly ExecutorHttpApiExtension[] = [],
39> = HttpApi.HttpApi<
40 "executor",
41 CoreExecutorApiGroups | ExecutorHttpPluginGroups<TPlugins>,
42 HttpApiErrorOf<typeof CoreExecutorApi>,
43 HttpApiContextOf<typeof CoreExecutorApi>
44>;
45
46export const createExecutorApi = <
47 const TPlugins extends readonly ExecutorHttpApiExtension[] = [],
48>(
49 options: {
50 plugins?: TPlugins;
51 } = {},
52): ExecutorApiWithPlugins<TPlugins> =>
53 (options.plugins ?? []).reduce(
54 (api, plugin) =>
55 api.add(plugin.group as HttpApiGroup.HttpApiGroup.Any),
56 CoreExecutorApi as HttpApi.HttpApi<
57 "executor",
58 CoreExecutorApiGroups | ExecutorHttpPluginGroups<TPlugins>,
59 HttpApiErrorOf<typeof CoreExecutorApi>,
60 HttpApiContextOf<typeof CoreExecutorApi>
61 >,
62 );
63
64export const ExecutorApi = createExecutorApi();
65
66export const createExecutorOpenApiSpec = <
67 const TPlugins extends readonly ExecutorHttpPlugin[] = [],
68>(
69 options: {
70 plugins?: TPlugins;
71 } = {},
72) => OpenApi.fromApi(createExecutorApi(options));
73
74export const executorOpenApiSpec = createExecutorOpenApiSpec();
packages/platform/api/src/client.ts+0−134
@@ -1,134 +0,0 @@
1import * as Effect from "effect/Effect";
2
3import {
4 createExecutorApiEffectClient,
5 type ExecutorApiEffectClient,
6} from "./effect";
7
8type StripResponse<T> = T extends readonly [infer Value, unknown] ? Value : T;
9type RemoveWithResponse<T> = T extends object ? Omit<T, "withResponse"> : T;
10type MethodInput<F extends (input: any) => Effect.Effect<any, any, any>> = RemoveWithResponse<
11 Parameters<F>[0]
12>;
13type MethodOutput<F extends (input: any) => Effect.Effect<any, any, any>> = StripResponse<
14 Effect.Effect.Success<ReturnType<F>>
15>;
16type PromiseMethod<F extends (input: any) => Effect.Effect<any, any, any>> = (
17 input: MethodInput<F>,
18) => Promise<MethodOutput<F>>;
19
20export type ExecutorApiClient = {
21 local: {
22 installation: PromiseMethod<ExecutorApiEffectClient["local"]["installation"]>;
23 config: PromiseMethod<ExecutorApiEffectClient["local"]["config"]>;
24 listSecrets: PromiseMethod<ExecutorApiEffectClient["local"]["listSecrets"]>;
25 createSecret: PromiseMethod<ExecutorApiEffectClient["local"]["createSecret"]>;
26 updateSecret: PromiseMethod<ExecutorApiEffectClient["local"]["updateSecret"]>;
27 deleteSecret: PromiseMethod<ExecutorApiEffectClient["local"]["deleteSecret"]>;
28 };
29 policies: {
30 list: PromiseMethod<ExecutorApiEffectClient["policies"]["list"]>;
31 create: PromiseMethod<ExecutorApiEffectClient["policies"]["create"]>;
32 get: PromiseMethod<ExecutorApiEffectClient["policies"]["get"]>;
33 update: PromiseMethod<ExecutorApiEffectClient["policies"]["update"]>;
34 remove: PromiseMethod<ExecutorApiEffectClient["policies"]["remove"]>;
35 };
36 sources: {
37 list: PromiseMethod<ExecutorApiEffectClient["sources"]["list"]>;
38 create: PromiseMethod<ExecutorApiEffectClient["sources"]["create"]>;
39 get: PromiseMethod<ExecutorApiEffectClient["sources"]["get"]>;
40 update: PromiseMethod<ExecutorApiEffectClient["sources"]["update"]>;
41 remove: PromiseMethod<ExecutorApiEffectClient["sources"]["remove"]>;
42 inspection: PromiseMethod<ExecutorApiEffectClient["sources"]["inspection"]>;
43 inspectionTool: PromiseMethod<ExecutorApiEffectClient["sources"]["inspectionTool"]>;
44 inspectionDiscover: PromiseMethod<
45 ExecutorApiEffectClient["sources"]["inspectionDiscover"]
46 >;
47 credentialPage: PromiseMethod<ExecutorApiEffectClient["sources"]["credentialPage"]>;
48 credentialSubmit: PromiseMethod<ExecutorApiEffectClient["sources"]["credentialSubmit"]>;
49 };
50 executions: {
51 create: PromiseMethod<ExecutorApiEffectClient["executions"]["create"]>;
52 get: PromiseMethod<ExecutorApiEffectClient["executions"]["get"]>;
53 resume: PromiseMethod<ExecutorApiEffectClient["executions"]["resume"]>;
54 };
55};
56
57const isResponseTuple = <Value>(
58 value: Value | readonly [Value, unknown],
59): value is readonly [Value, unknown] =>
60 Array.isArray(value) && value.length === 2;
61
62function stripResponse<Value>(value: Value | readonly [Value, unknown]): Value {
63 return isResponseTuple(value) ? value[0] : value;
64}
65
66const wrapMethod = <
67 F extends (input: any) => Effect.Effect<any, unknown, never>,
68>(
69 effectClientPromise: Promise<ExecutorApiEffectClient>,
70 select: (client: ExecutorApiEffectClient) => F,
71): PromiseMethod<F> => {
72 const wrapped = (input: MethodInput<F>): Promise<MethodOutput<F>> =>
73 effectClientPromise
74 .then((client) => Effect.runPromise(select(client)(input)))
75 .then(stripResponse);
76
77 return wrapped;
78};
79
80export const createExecutorApiClient = async (input: {
81 baseUrl: string;
82 accountId?: string;
83}): Promise<ExecutorApiClient> => {
84 const effectClientPromise = Effect.runPromise(
85 createExecutorApiEffectClient(input),
86 );
87
88 return {
89 local: {
90 installation: wrapMethod(effectClientPromise, (client) => client.local.installation),
91 config: wrapMethod(effectClientPromise, (client) => client.local.config),
92 listSecrets: wrapMethod(effectClientPromise, (client) => client.local.listSecrets),
93 createSecret: wrapMethod(effectClientPromise, (client) => client.local.createSecret),
94 updateSecret: wrapMethod(effectClientPromise, (client) => client.local.updateSecret),
95 deleteSecret: wrapMethod(effectClientPromise, (client) => client.local.deleteSecret),
96 },
97 policies: {
98 list: wrapMethod(effectClientPromise, (client) => client.policies.list),
99 create: wrapMethod(effectClientPromise, (client) => client.policies.create),
100 get: wrapMethod(effectClientPromise, (client) => client.policies.get),
101 update: wrapMethod(effectClientPromise, (client) => client.policies.update),
102 remove: wrapMethod(effectClientPromise, (client) => client.policies.remove),
103 },
104 sources: {
105 list: wrapMethod(effectClientPromise, (client) => client.sources.list),
106 create: wrapMethod(effectClientPromise, (client) => client.sources.create),
107 get: wrapMethod(effectClientPromise, (client) => client.sources.get),
108 update: wrapMethod(effectClientPromise, (client) => client.sources.update),
109 remove: wrapMethod(effectClientPromise, (client) => client.sources.remove),
110 inspection: wrapMethod(effectClientPromise, (client) => client.sources.inspection),
111 inspectionTool: wrapMethod(
112 effectClientPromise,
113 (client) => client.sources.inspectionTool,
114 ),
115 inspectionDiscover: wrapMethod(
116 effectClientPromise,
117 (client) => client.sources.inspectionDiscover,
118 ),
119 credentialPage: wrapMethod(
120 effectClientPromise,
121 (client) => client.sources.credentialPage,
122 ),
123 credentialSubmit: wrapMethod(
124 effectClientPromise,
125 (client) => client.sources.credentialSubmit,
126 ),
127 },
128 executions: {
129 create: wrapMethod(effectClientPromise, (client) => client.executions.create),
130 get: wrapMethod(effectClientPromise, (client) => client.executions.get),
131 resume: wrapMethod(effectClientPromise, (client) => client.executions.resume),
132 },
133 };
134};
packages/platform/api/src/effect.ts+7−4
@@ -4,13 +4,16 @@ import { · 3 unmodified lines
4} from "@effect/platform";4} from "@effect/platform";
5import * as Effect from "effect/Effect";5import * as Effect from "effect/Effect";
6 6
7import { ExecutorApi } from "./api";7import { createExecutorApi } from "./api";
8import type { ExecutorHttpPlugin } from "./plugins";
8 9
9export const createExecutorApiEffectClient = (input: {10export const createExecutorApiEffectClient = <
11 const TPlugins extends readonly ExecutorHttpPlugin[] = [],
12>(input: {
10 baseUrl: string;13 baseUrl: string;
11 accountId?: string;14 plugins?: TPlugins;
12}) =>15}) =>
13 HttpApiClient.make(ExecutorApi, {16 HttpApiClient.make(createExecutorApi({ plugins: input.plugins }), {
14 baseUrl: input.baseUrl,17 baseUrl: input.baseUrl,
15 }).pipe(Effect.provide(FetchHttpClient.layer));18 }).pipe(Effect.provide(FetchHttpClient.layer));
16 19
packages/platform/api/src/http.ts+32−10
@@ -2,24 +2,46 @@ import { HttpApiBuilder } from "@effect/platform"; · 1 unmodified line
2import * as Layer from "effect/Layer";2import * as Layer from "effect/Layer";
3import type { ExecutorEffect } from "@executor/platform-sdk/effect";3import type { ExecutorEffect } from "@executor/platform-sdk/effect";
4 4
5import { ExecutorApi } from "./api";5import { createExecutorApi } from "./api";
6import { createControlPlaneExecutorLayer } from "./executor-context";6import { createControlPlaneExecutorLayer } from "./executor-context";
7import { ExecutorExecutionsLive } from "./executions/http";7import { ExecutorExecutionsLive } from "./executions/http";
8import { ExecutorLocalLive } from "./local/http";8import { ExecutorLocalLive } from "./local/http";
9import { ExecutorPoliciesLive } from "./policies/http";9import { ExecutorPoliciesLive } from "./policies/http";
10import type { ExecutorHttpPlugin } from "./plugins";
10import { ExecutorSourcesLive } from "./sources/http";11import { ExecutorSourcesLive } from "./sources/http";
11 12
12export const ExecutorApiLive = HttpApiBuilder.api(ExecutorApi).pipe(13const createExecutorApiLive = (
13 Layer.provide(ExecutorLocalLive),14 executor: ExecutorEffect,
14 Layer.provide(ExecutorSourcesLive),15 plugins: readonly ExecutorHttpPlugin[] = [],
15 Layer.provide(ExecutorPoliciesLive),16) => {
16 Layer.provide(ExecutorExecutionsLive),17 const api = createExecutorApi({ plugins });
17);18 let live: Layer.Layer<any, any, any> = HttpApiBuilder.api(api as any).pipe(
19 Layer.provide(ExecutorLocalLive),
20 Layer.provide(ExecutorSourcesLive),
21 Layer.provide(ExecutorPoliciesLive),
22 Layer.provide(ExecutorExecutionsLive),
23 );
24
25 for (const plugin of plugins) {
26 live = live.pipe(
27 Layer.provide(
28 plugin.build({
29 executor,
30 }),
31 ),
32 );
33 }
18 34
19export type ExecutorApiRuntimeContext = Layer.Layer.Context<typeof ExecutorApiLive>;35 return live;
36};
20 37
21export const createExecutorApiLayer = (executor: ExecutorEffect) =>38export const createExecutorApiLayer = (
22 ExecutorApiLive.pipe(39 executor: ExecutorEffect,
40 options: {
41 plugins?: readonly ExecutorHttpPlugin[];
42 } = {},
43) =>
44 createExecutorApiLive(executor, options.plugins).pipe(
23 Layer.provide(createControlPlaneExecutorLayer(executor)),45 Layer.provide(createControlPlaneExecutorLayer(executor)),
24 );46 );
25 47
packages/platform/api/src/index.ts+8−8
@@ -1,11 +1,15 @@
1export {1export {
2 CoreExecutorApi,
3 createExecutorApi,
4 createExecutorOpenApiSpec,
2 ExecutorApi,5 ExecutorApi,
3 executorOpenApiSpec,6 executorOpenApiSpec,
4} from "./api";7} from "./api";
5export {8export type {
6 createExecutorApiClient,9 ExecutorHttpApiExtension,
7 type ExecutorApiClient,10 ExecutorHttpPlugin,
8} from "./client";11 ExecutorHttpPluginGroups,
12} from "./plugins";
9 13
10export type { LocalInstallation } from "@executor/platform-sdk/schema";14export type { LocalInstallation } from "@executor/platform-sdk/schema";
11 15
@@ -38,11 +42,7 @@ export { · 26 unmodified lines
38} from "./local/api";42} from "./local/api";
39 43
40export {44export {
41 CreateSourcePayloadSchema,
42 SourcesApi,45 SourcesApi,
43 UpdateSourcePayloadSchema,
44 type CreateSourcePayload,
45 type UpdateSourcePayload,
46} from "./sources/api";46} from "./sources/api";
47 47
48export {48export {
packages/platform/api/src/plugins.ts+23−0
@@ -0,0 +1,23 @@
1import type { HttpApiGroup } from "@effect/platform";
2import type * as Layer from "effect/Layer";
3import type { ExecutorEffect } from "@executor/platform-sdk/effect";
4
5export type ExecutorHttpApiExtension<
6 TGroup extends HttpApiGroup.HttpApiGroup.Any = HttpApiGroup.HttpApiGroup.Any,
7> = {
8 key: string;
9 group: TGroup;
10};
11
12export type ExecutorHttpPlugin<
13 TGroup extends HttpApiGroup.HttpApiGroup.Any = HttpApiGroup.HttpApiGroup.Any,
14 TExecutorExtension extends object = {},
15> = ExecutorHttpApiExtension<TGroup> & {
16 build(input: {
17 executor: ExecutorEffect & TExecutorExtension;
18 }): Layer.Layer<any, any, any>;
19};
20
21export type ExecutorHttpPluginGroups<
22 TPlugins extends readonly ExecutorHttpApiExtension[],
23> = TPlugins[number]["group"];
packages/platform/api/src/sources/api.ts+0−56
@@ -1,10 +1,4 @@
1import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "@effect/platform";1import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "@effect/platform";
2import {
3 CreateSourcePayloadSchema,
4 CredentialPageUrlParamsSchema,
5 CredentialSubmitPayloadSchema,
6 UpdateSourcePayloadSchema,
7} from "@executor/platform-sdk/contracts";
8import {2import {
9 ControlPlaneBadRequestError,3 ControlPlaneBadRequestError,
10 ControlPlaneForbiddenError,4 ControlPlaneForbiddenError,
@@ -23,24 +17,10 @@ import { · 12 unmodified lines
23} from "@executor/platform-sdk/schema";17} from "@executor/platform-sdk/schema";
24import * as Schema from "effect/Schema";18import * as Schema from "effect/Schema";
25 19
26export type {
27 CreateSourcePayload,
28 UpdateSourcePayload,
29} from "@executor/platform-sdk/contracts";
30
31export {
32 CreateSourcePayloadSchema,
33 UpdateSourcePayloadSchema,
34};
35
36const workspaceIdParam = HttpApiSchema.param("workspaceId", WorkspaceIdSchema);20const workspaceIdParam = HttpApiSchema.param("workspaceId", WorkspaceIdSchema);
37const sourceIdParam = HttpApiSchema.param("sourceId", SourceIdSchema);21const sourceIdParam = HttpApiSchema.param("sourceId", SourceIdSchema);
38const toolPathParam = HttpApiSchema.param("toolPath", Schema.String);22const toolPathParam = HttpApiSchema.param("toolPath", Schema.String);
39 23
40const HtmlSchema = HttpApiSchema.Text({
41 contentType: "text/html",
42});
43
44export class SourcesApi extends HttpApiGroup.make("sources")24export class SourcesApi extends HttpApiGroup.make("sources")
45 .add(25 .add(
46 HttpApiEndpoint.get("list")`/workspaces/${workspaceIdParam}/sources`26 HttpApiEndpoint.get("list")`/workspaces/${workspaceIdParam}/sources`
@@ -50,15 +30,6 @@ export class SourcesApi extends HttpApiGroup.make("sources") · 3 unmodified lines
50 .addError(ControlPlaneForbiddenError)30 .addError(ControlPlaneForbiddenError)
51 .addError(ControlPlaneStorageError),31 .addError(ControlPlaneStorageError),
52 )32 )
53 .add(
54 HttpApiEndpoint.post("create")`/workspaces/${workspaceIdParam}/sources`
55 .setPayload(CreateSourcePayloadSchema)
56 .addSuccess(SourceSchema)
57 .addError(ControlPlaneBadRequestError)
58 .addError(ControlPlaneUnauthorizedError)
59 .addError(ControlPlaneForbiddenError)
60 .addError(ControlPlaneStorageError),
61 )
62 .add(33 .add(
63 HttpApiEndpoint.get("get")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}`34 HttpApiEndpoint.get("get")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}`
64 .addSuccess(SourceSchema)35 .addSuccess(SourceSchema)
@@ -68,16 +39,6 @@ export class SourcesApi extends HttpApiGroup.make("sources") · 3 unmodified lines
68 .addError(ControlPlaneNotFoundError)39 .addError(ControlPlaneNotFoundError)
69 .addError(ControlPlaneStorageError),40 .addError(ControlPlaneStorageError),
70 )41 )
71 .add(
72 HttpApiEndpoint.patch("update")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}`
73 .setPayload(UpdateSourcePayloadSchema)
74 .addSuccess(SourceSchema)
75 .addError(ControlPlaneBadRequestError)
76 .addError(ControlPlaneUnauthorizedError)
77 .addError(ControlPlaneForbiddenError)
78 .addError(ControlPlaneNotFoundError)
79 .addError(ControlPlaneStorageError),
80 )
81 .add(42 .add(
82 HttpApiEndpoint.del("remove")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}`43 HttpApiEndpoint.del("remove")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}`
83 .addSuccess(Schema.Struct({ removed: Schema.Boolean }))44 .addSuccess(Schema.Struct({ removed: Schema.Boolean }))
@@ -86,23 +47,6 @@ export class SourcesApi extends HttpApiGroup.make("sources") · 2 unmodified lines
86 .addError(ControlPlaneForbiddenError)47 .addError(ControlPlaneForbiddenError)
87 .addError(ControlPlaneStorageError),48 .addError(ControlPlaneStorageError),
88 )49 )
89 .add(
90 HttpApiEndpoint.get("credentialPage")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/credentials`
91 .setUrlParams(CredentialPageUrlParamsSchema)
92 .addSuccess(HtmlSchema)
93 .addError(ControlPlaneBadRequestError)
94 .addError(ControlPlaneNotFoundError)
95 .addError(ControlPlaneStorageError),
96 )
97 .add(
98 HttpApiEndpoint.post("credentialSubmit")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/credentials`
99 .setUrlParams(CredentialPageUrlParamsSchema)
100 .setPayload(CredentialSubmitPayloadSchema)
101 .addSuccess(HtmlSchema)
102 .addError(ControlPlaneBadRequestError)
103 .addError(ControlPlaneNotFoundError)
104 .addError(ControlPlaneStorageError),
105 )
106 .add(50 .add(
107 HttpApiEndpoint.get("inspection")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/inspection`51 HttpApiEndpoint.get("inspection")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/inspection`
108 .addSuccess(SourceInspectionSchema)52 .addSuccess(SourceInspectionSchema)
packages/platform/api/src/sources/http.ts+1−581
@@ -1,7 +1,4 @@
1import {1import { HttpApiBuilder } from "@effect/platform";
2 HttpApiBuilder,
3 HttpServerResponse,
4} from "@effect/platform";
5import type {2import type {
6 ExecutionInteraction,3 ExecutionInteraction,
7 Source,4 Source,
@@ -29,453 +26,6 @@ const toBadRequestError = (operation: string, cause: unknown) => { · 21 unmodified lines
29 });26 });
30};27};
31 28
32const escapeHtml = (value: string): string =>
33 value
34 .replaceAll("&", "&amp;")
35 .replaceAll("<", "&lt;")
36 .replaceAll(">", "&gt;")
37 .replaceAll('"', "&quot;")
38 .replaceAll("'", "&#39;");
39
40const credentialPageDocument = (input: {
41 title: string;
42 eyebrow: string;
43 message: string;
44 sourceLabel: string;
45 endpoint: string;
46 interactionId: string;
47 error?: string;
48 state?: "pending" | "stored" | "continued" | "cancelled" | "inactive";
49}) => `<!doctype html>
50<html lang="en">
51 <head>
52 <meta charset="utf-8" />
53 <meta name="viewport" content="width=device-width, initial-scale=1" />
54 <title>${escapeHtml(input.title)}</title>
55 <link rel="preconnect" href="https://fonts.googleapis.com" />
56 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
57 <link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;1,9..40,400&family=DM+Mono:wght@400;500&display=swap" rel="stylesheet" />
58 <style>
59 :root {
60 --bg: #fafaf9;
61 --surface: #ffffff;
62 --text: #1c1917;
63 --text-secondary: #78716c;
64 --text-tertiary: #a8a29e;
65 --border: #e7e5e4;
66 --accent: #1d4ed8;
67 --accent-light: #eff6ff;
68 --green: #16a34a;
69 --green-light: #f0fdf4;
70 --green-border: #bbf7d0;
71 --red: #dc2626;
72 --red-light: #fef2f2;
73 --red-border: #fecaca;
74 --amber: #d97706;
75 --amber-light: #fffbeb;
76 --amber-border: #fde68a;
77 }
78
79 * { box-sizing: border-box; margin: 0; padding: 0; }
80
81 body {
82 min-height: 100vh;
83 background: var(--bg);
84 color: var(--text);
85 font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
86 display: flex;
87 justify-content: center;
88 padding: 80px 24px 48px;
89 -webkit-font-smoothing: antialiased;
90 }
91
92 .page {
93 width: 100%;
94 max-width: 540px;
95 }
96
97 /* Status pill at the very top */
98 .status-pill {
99 display: inline-flex;
100 align-items: center;
101 gap: 7px;
102 padding: 5px 14px 5px 10px;
103 border-radius: 999px;
104 font-family: 'DM Mono', monospace;
105 font-size: 12px;
106 font-weight: 500;
107 letter-spacing: 0.01em;
108 margin-bottom: 28px;
109 }
110
111 .status-pill::before {
112 content: '';
113 width: 7px;
114 height: 7px;
115 border-radius: 50%;
116 flex-shrink: 0;
117 }
118
119 .status-pill[data-state='pending'] {
120 background: var(--amber-light);
121 color: var(--amber);
122 border: 1px solid var(--amber-border);
123 }
124 .status-pill[data-state='pending']::before { background: var(--amber); }
125
126 .status-pill[data-state='stored'] {
127 background: var(--green-light);
128 color: var(--green);
129 border: 1px solid var(--green-border);
130 }
131 .status-pill[data-state='stored']::before { background: var(--green); }
132
133 .status-pill[data-state='continued'] {
134 background: var(--green-light);
135 color: var(--green);
136 border: 1px solid var(--green-border);
137 }
138 .status-pill[data-state='continued']::before { background: var(--green); }
139
140 .status-pill[data-state='cancelled'],
141 .status-pill[data-state='inactive'] {
142 background: #f5f5f4;
143 color: var(--text-secondary);
144 border: 1px solid var(--border);
145 }
146 .status-pill[data-state='cancelled']::before,
147 .status-pill[data-state='inactive']::before { background: var(--text-tertiary); }
148
149 /* Title area */
150 h1 {
151 font-size: 28px;
152 font-weight: 600;
153 letter-spacing: -0.025em;
154 line-height: 1.2;
155 margin-bottom: 10px;
156 }
157
158 .subtitle {
159 font-size: 15px;
160 line-height: 1.6;
161 color: var(--text-secondary);
162 }
163
164 /* Divider */
165 hr {
166 border: none;
167 height: 1px;
168 background: var(--border);
169 margin: 36px 0;
170 }
171
172 /* Source detail rows */
173 .detail {
174 display: flex;
175 flex-direction: column;
176 gap: 6px;
177 margin-bottom: 24px;
178 }
179
180 .detail-label {
181 font-size: 13px;
182 font-weight: 500;
183 color: var(--text-secondary);
184 }
185
186 .detail-value {
187 font-size: 15px;
188 line-height: 1.5;
189 word-break: break-word;
190 }
191
192 .detail-mono {
193 font-family: 'DM Mono', monospace;
194 font-size: 13px;
195 line-height: 1.5;
196 padding: 10px 14px;
197 background: #f5f5f4;
198 border: 1px solid var(--border);
199 border-radius: 8px;
200 word-break: break-all;
201 color: var(--text);
202 }
203
204 .detail-id {
205 font-family: 'DM Mono', monospace;
206 font-size: 12px;
207 color: var(--text-tertiary);
208 }
209
210 /* Result card for stored/cancelled/inactive */
211 .result-card {
212 padding: 32px;
213 border-radius: 12px;
214 text-align: center;
215 }
216
217 .result-card.success {
218 background: var(--green-light);
219 border: 1px solid var(--green-border);
220 }
221
222 .result-card.neutral {
223 background: #f5f5f4;
224 border: 1px solid var(--border);
225 }
226
227 .result-card p {
228 font-size: 15px;
229 line-height: 1.6;
230 color: var(--text-secondary);
231 }
232
233 .result-card .result-heading {
234 font-weight: 600;
235 font-size: 15px;
236 color: var(--text);
237 margin-bottom: 6px;
238 }
239
240 /* Form */
241 .alert {
242 padding: 12px 14px;
243 border-radius: 8px;
244 background: var(--red-light);
245 border: 1px solid var(--red-border);
246 color: var(--red);
247 font-size: 14px;
248 line-height: 1.5;
249 margin-bottom: 20px;
250 }
251
252 .field { margin-bottom: 20px; }
253
254 label {
255 display: block;
256 font-size: 14px;
257 font-weight: 500;
258 margin-bottom: 6px;
259 }
260
261 input[type='password'] {
262 width: 100%;
263 padding: 10px 14px;
264 background: var(--surface);
265 border: 1px solid var(--border);
266 border-radius: 8px;
267 font-family: 'DM Mono', monospace;
268 font-size: 14px;
269 color: var(--text);
270 transition: border-color 0.15s, box-shadow 0.15s;
271 }
272
273 input[type='password']::placeholder { color: var(--text-tertiary); }
274
275 input[type='password']:focus {
276 outline: none;
277 border-color: var(--accent);
278 box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.1);
279 }
280
281 .hint {
282 margin-top: 8px;
283 font-size: 13px;
284 line-height: 1.5;
285 color: var(--text-tertiary);
286 }
287
288 .actions {
289 display: flex;
290 gap: 10px;
291 margin-top: 24px;
292 }
293
294 button {
295 appearance: none;
296 border: none;
297 border-radius: 8px;
298 padding: 10px 20px;
299 font-family: inherit;
300 font-size: 14px;
301 font-weight: 500;
302 cursor: pointer;
303 transition: background 0.15s;
304 }
305
306 .primary {
307 background: var(--text);
308 color: var(--surface);
309 }
310 .primary:hover { background: #292524; }
311
312 .secondary {
313 background: transparent;
314 color: var(--text-secondary);
315 border: 1px solid var(--border);
316 }
317 .secondary:hover { background: #f5f5f4; }
318
319 @media (max-width: 540px) {
320 body { padding: 48px 20px 32px; }
321 h1 { font-size: 24px; }
322 }
323 </style>
324 </head>
325 <body>
326 <main class="page">
327 <div class="status-pill" data-state="${escapeHtml(input.state ?? "pending")}">${escapeHtml(input.state ?? "pending")}</div>
328
329 <h1>${escapeHtml(input.title)}</h1>
330 <p class="subtitle">${escapeHtml(input.message)}</p>
331
332 <hr />
333
334 <div class="detail">
335 <span class="detail-label">Source</span>
336 <span class="detail-value">${escapeHtml(input.sourceLabel)}</span>
337 </div>
338
339 <div class="detail">
340 <span class="detail-label">API endpoint</span>
341 <div class="detail-mono">${escapeHtml(input.endpoint)}</div>
342 </div>
343
344 <div class="detail">
345 <span class="detail-label">Interaction</span>
346 <span class="detail-id">${escapeHtml(input.interactionId)}</span>
347 </div>
348
349 <hr />
350
351 ${input.error ? `<p class="alert">${escapeHtml(input.error)}</p>` : ""}
352 ${
353 input.state === "stored"
354 ? `<div class="result-card success">
355 <p class="result-heading">Credential stored</p>
356 <p>Executor resumed the source connection request. You can close this window.</p>
357 </div>`
358 : input.state === "continued"
359 ? `<div class="result-card success">
360 <p class="result-heading">Continuing without auth</p>
361 <p>Executor resumed the source connection without stored credentials. You can close this window.</p>
362 </div>`
363 : input.state === "cancelled"
364 ? `<div class="result-card neutral">
365 <p class="result-heading">Request cancelled</p>
366 <p>Executor was notified. You can close this window.</p>
367 </div>`
368 : input.state === "inactive"
369 ? `<div class="result-card neutral">
370 <p class="result-heading">No longer active</p>
371 <p>This credential request has expired. Start a new source connection flow if needed.</p>
372 </div>`
373 : `<form method="post">
374 <div class="field">
375 <label for="token">Personal access token</label>
376 <input
377 id="token"
378 name="token"
379 type="password"
380 autocomplete="off"
381 spellcheck="false"
382 placeholder="ghp_..."
383 />
384 <p class="hint">
385 Add a token to enable authenticated requests, or continue without auth if this API supports public access.
386 </p>
387 </div>
388 <div class="actions">
389 <button class="primary" type="submit" name="action" value="submit">Connect token</button>
390 <button class="secondary" type="submit" name="action" value="continue" formnovalidate>Continue without auth</button>
391 <button class="secondary" type="submit" name="action" value="cancel" formnovalidate>Cancel</button>
392 </div>
393 </form>`
394 }
395 </main>
396 </body>
397</html>`;
398
399const htmlResponse = (
400 html: string,
401 status = 200,
402): ReturnType<typeof HttpServerResponse.html> =>
403 HttpServerResponse.html(html).pipe(
404 HttpServerResponse.setStatus(status),
405 ) as ReturnType<typeof HttpServerResponse.html>;
406
407const credentialErrorResponse = (input: {
408 title: string;
409 message: string;
410 status: number;
411}) =>
412 htmlResponse(
413 credentialPageDocument({
414 title: input.title,
415 eyebrow: "Executor",
416 message: input.message,
417 sourceLabel: "Unavailable",
418 endpoint: "Unavailable",
419 interactionId: "unavailable",
420 state: "inactive",
421 error: input.message,
422 }),
423 input.status,
424 );
425
426const credentialErrorStatus = (
427 error:
428 | ControlPlaneBadRequestError
429 | ControlPlaneNotFoundError
430 | ControlPlaneStorageError,
431) =>
432 error instanceof ControlPlaneNotFoundError
433 ? 404
434 : error instanceof ControlPlaneBadRequestError
435 ? 400
436 : 500;
437
438const credentialSubmitErrorResponse = (input: {
439 workspaceId: WorkspaceId;
440 sourceId: Source["id"];
441 interactionId: ExecutionInteraction["id"];
442 message: string;
443 status: number;
444}) =>
445 resolveRequestedLocalWorkspace(
446 "sources.credentialSubmit",
447 input.workspaceId,
448 ).pipe(
449 Effect.flatMap((executor) =>
450 executor.local.credentials.get({
451 sourceId: input.sourceId,
452 interactionId: input.interactionId,
453 }),
454 ),
455 Effect.match({
456 onFailure: () =>
457 credentialErrorResponse({
458 title: "Source Credential Request Failed",
459 message: input.message,
460 status: input.status,
461 }),
462 onSuccess: (interaction) =>
463 htmlResponse(
464 credentialPageDocument({
465 title: "Configure Source Access",
466 eyebrow: "Executor",
467 message: interaction.message,
468 sourceLabel: interaction.sourceLabel,
469 endpoint: interaction.endpoint,
470 interactionId: interaction.interactionId,
471 error: input.message,
472 state: "pending",
473 }),
474 input.status,
475 ),
476 }),
477 );
478
479export const ExecutorSourcesLive = HttpApiBuilder.group(29export const ExecutorSourcesLive = HttpApiBuilder.group(
480 ExecutorApi,30 ExecutorApi,
481 "sources",31 "sources",
@@ -486,11 +36,6 @@ export const ExecutorSourcesLive = HttpApiBuilder.group( · 4 unmodified lines
486 Effect.flatMap((executor) => executor.sources.list()),36 Effect.flatMap((executor) => executor.sources.list()),
487 ),37 ),
488 )38 )
489 .handle("create", ({ path, payload }) =>
490 resolveRequestedLocalWorkspace("sources.create", path.workspaceId).pipe(
491 Effect.flatMap((executor) => executor.sources.create(payload)),
492 ),
493 )
494 .handle("get", ({ path }) =>39 .handle("get", ({ path }) =>
495 resolveRequestedLocalWorkspace("sources.get", path.workspaceId).pipe(40 resolveRequestedLocalWorkspace("sources.get", path.workspaceId).pipe(
496 Effect.flatMap((executor) => executor.sources.get(path.sourceId)),41 Effect.flatMap((executor) => executor.sources.get(path.sourceId)),
@@ -530,135 +75,10 @@ export const ExecutorSourcesLive = HttpApiBuilder.group( · 33 unmodified lines
530 ),75 ),
531 ),76 ),
532 )77 )
533 .handle("update", ({ path, payload }) =>
534 resolveRequestedLocalWorkspace("sources.update", path.workspaceId).pipe(
535 Effect.flatMap((executor) => executor.sources.update(path.sourceId, payload)),
536 ),
537 )
538 .handle("remove", ({ path }) =>78 .handle("remove", ({ path }) =>
539 resolveRequestedLocalWorkspace("sources.remove", path.workspaceId).pipe(79 resolveRequestedLocalWorkspace("sources.remove", path.workspaceId).pipe(
540 Effect.flatMap((executor) => executor.sources.remove(path.sourceId)),80 Effect.flatMap((executor) => executor.sources.remove(path.sourceId)),
541 Effect.map((removed) => ({ removed })),81 Effect.map((removed) => ({ removed })),
542 ),82 ),
543 )83 )
544 .handle("credentialPage", ({ path, urlParams }) =>
545 resolveRequestedLocalWorkspace(
546 "sources.credentialPage",
547 path.workspaceId,
548 ).pipe(
549 Effect.flatMap((executor) =>
550 executor.local.credentials.get({
551 sourceId: path.sourceId,
552 interactionId: urlParams.interactionId,
553 }),
554 ),
555 Effect.map((interaction) =>
556 htmlResponse(
557 credentialPageDocument({
558 title: "Configure Source Access",
559 eyebrow: "Executor",
560 message: interaction.message,
561 sourceLabel: interaction.sourceLabel,
562 endpoint: interaction.endpoint,
563 interactionId: interaction.interactionId,
564 state:
565 interaction.status === "pending" ? "pending" : "inactive",
566 }),
567 ),
568 ),
569 Effect.catchAll((error) =>
570 Effect.succeed(
571 credentialErrorResponse({
572 title:
573 error instanceof ControlPlaneNotFoundError
574 ? "Source Credential Request Unavailable"
575 : "Source Credential Request Failed",
576 message:
577 error instanceof Error ? error.message : "Source credential request failed",
578 status:
579 error instanceof ControlPlaneNotFoundError
580 || error instanceof ControlPlaneStorageError
581 || error instanceof ControlPlaneBadRequestError
582 ? credentialErrorStatus(error)
583 : 500,
584 }),
585 ),
586 ),
587 ),
588 )
589 .handle("credentialSubmit", ({ path, urlParams, payload }) =>
590 resolveRequestedLocalWorkspace(
591 "sources.credentialSubmit",
592 path.workspaceId,
593 ).pipe(
594 Effect.flatMap((executor) =>
595 executor.local.credentials.submit({
596 sourceId: path.sourceId,
597 interactionId: urlParams.interactionId,
598 action:
599 payload.action === "cancel"
600 ? "cancel"
601 : payload.action === "continue"
602 ? "continue"
603 : "submit",
604 token: payload.token,
605 }),
606 ),
607 Effect.map((result) =>
608 htmlResponse(
609 credentialPageDocument({
610 title:
611 result.kind === "stored"
612 ? "Source Credential Stored"
613 : result.kind === "continued"
614 ? "Source Access Updated"
615 : "Source Credential Request Cancelled",
616 eyebrow: "Executor",
617 message:
618 result.kind === "stored"
619 ? "Executor stored the credential and resumed the source connection."
620 : result.kind === "continued"
621 ? "Executor resumed the source connection without stored credentials."
622 : "Executor cancelled the source credential request.",
623 sourceLabel: result.sourceLabel,
624 endpoint: result.endpoint,
625 interactionId: urlParams.interactionId,
626 state:
627 result.kind === "stored"
628 ? "stored"
629 : result.kind === "continued"
630 ? "continued"
631 : "cancelled",
632 }),
633 ),
634 ),
635 Effect.catchAll((error) => {
636 if (error instanceof ControlPlaneBadRequestError) {
637 return credentialSubmitErrorResponse({
638 workspaceId: path.workspaceId,
639 sourceId: path.sourceId,
640 interactionId: urlParams.interactionId,
641 message: error.message,
642 status: credentialErrorStatus(error),
643 });
644 }
645
646 return Effect.succeed(
647 credentialErrorResponse({
648 title:
649 error instanceof ControlPlaneNotFoundError
650 ? "Source Credential Request Unavailable"
651 : "Source Credential Request Failed",
652 message:
653 error instanceof Error ? error.message : "Source credential request failed",
654 status:
655 error instanceof ControlPlaneNotFoundError
656 || error instanceof ControlPlaneStorageError
657 ? credentialErrorStatus(error)
658 : 500,
659 }),
660 );
661 }),
662 ),
663 )
664);84);
packages/platform/internal/src/tools/executor-admin.ts+0−50
@@ -24,7 +24,6 @@ import { · 23 unmodified lines
24 SecretMaterialUpdaterService,24 SecretMaterialUpdaterService,
25 type ExecutorStateStoreShape,25 type ExecutorStateStoreShape,
26 type ScopeInternalToolContext as WorkspaceInternalToolContext,26 type ScopeInternalToolContext as WorkspaceInternalToolContext,
27 RuntimeSourceAuthService,
28 RuntimeSourceCatalogSyncService,27 RuntimeSourceCatalogSyncService,
29 RuntimeSourceStore,28 RuntimeSourceStore,
30 RuntimeSourceStoreService,29 RuntimeSourceStoreService,
@@ -69,7 +68,6 @@ import { · 38 unmodified lines
69 removeSource,68 removeSource,
70 updateLocalSecret,69 updateLocalSecret,
71 updatePolicy,70 updatePolicy,
72 updateSource,
73} from "@executor/platform-sdk/operations";71} from "@executor/platform-sdk/operations";
74import {72import {
75 CreatePolicyPayloadSchema,73 CreatePolicyPayloadSchema,
@@ -77,10 +75,6 @@ import { · 1 unmodified line
77 type UpdatePolicyPayload,75 type UpdatePolicyPayload,
78 UpdatePolicyPayloadSchema,76 UpdatePolicyPayloadSchema,
79} from "@executor/platform-sdk/contracts";77} from "@executor/platform-sdk/contracts";
80import {
81 UpdateSourcePayloadSchema,
82} from "@executor/platform-sdk/contracts";
83
84const emptyInputSchema = Schema.standardSchemaV1(Schema.Struct({}));78const emptyInputSchema = Schema.standardSchemaV1(Schema.Struct({}));
85const localInstallationOutputSchema = Schema.standardSchemaV1(79const localInstallationOutputSchema = Schema.standardSchemaV1(
86 LocalInstallationSchema,80 LocalInstallationSchema,
@@ -117,12 +111,6 @@ const getSourceInputSchema = Schema.standardSchemaV1( · 30 unmodified lines
117 sourceId: SourceIdSchema,111 sourceId: SourceIdSchema,
118 }),112 }),
119);113);
120const updateSourceInputSchema = Schema.standardSchemaV1(
121 Schema.Struct({
122 sourceId: SourceIdSchema,
123 payload: UpdateSourcePayloadSchema,
124 }),
125);
126const inspectToolInputSchema = Schema.standardSchemaV1(114const inspectToolInputSchema = Schema.standardSchemaV1(
127 Schema.Struct({115 Schema.Struct({
128 sourceId: SourceIdSchema,116 sourceId: SourceIdSchema,
@@ -386,28 +374,6 @@ export const createWorkspaceExecutorAdminToolMap = ( · 257 unmodified lines
386 },374 },
387 metadata,375 metadata,
388 }),376 }),
389 "executor.sources.update": toTool({
390 tool: {
391 description: "Update a source definition in the current workspace.",
392 inputSchema: updateSourceInputSchema,
393 outputSchema: sourceOutputSchema,
394 execute: (payload: {
395 sourceId: string;
396 payload: Record<string, unknown>;
397 }) =>
398 runRuntimeEffect({
399 effect: updateSource({
400 scopeId: input.scopeId,
401 sourceId: payload.sourceId as never,
402 actorScopeId: input.actorScopeId as never,
403 payload: payload.payload as never,
404 }),
405 runtimeLayer,
406 runtimeLocalScope: input.runtimeLocalScope,
407 }),
408 },
409 metadata,
410 }),
411 "executor.sources.remove": toTool({377 "executor.sources.remove": toTool({
412 tool: {378 tool: {
413 description: "Remove a source from the current workspace.",379 description: "Remove a source from the current workspace.",
@@ -674,22 +640,6 @@ export const createExecutorAdminToolMap = (input: { · 260 unmodified lines
674 },640 },
675 metadata,641 metadata,
676 }),642 }),
677 "executor.sources.update": toTool({
678 tool: {
679 description: "Update a source definition in the current workspace.",
680 inputSchema: updateSourceInputSchema,
681 outputSchema: sourceOutputSchema,
682 execute: (payload: {
683 sourceId: string;
684 payload: Record<string, unknown>;
685 }) =>
686 input.executor.sources.update(
687 payload.sourceId as never,
688 payload.payload as never,
689 ),
690 },
691 metadata,
692 }),
693 "executor.sources.remove": toTool({643 "executor.sources.remove": toTool({
694 tool: {644 tool: {
695 description: "Remove a source from the current workspace.",645 description: "Remove a source from the current workspace.",
packages/platform/sdk-file/src/capture-release-workspace-fixture.ts+1−2
@@ -188,10 +188,9 @@ const copyRecursive = ( · 187 unmodified lines
188const sanitizeSourceConfig = (188const sanitizeSourceConfig = (
189 sourceConfig: NonNullable<LocalExecutorConfig["sources"]>[string],189 sourceConfig: NonNullable<LocalExecutorConfig["sources"]>[string],
190): NonNullable<LocalExecutorConfig["sources"]>[string] => {190): NonNullable<LocalExecutorConfig["sources"]>[string] => {
191 const { auth: _ignoredAuth, ...connection } = sourceConfig.connection;
192 return {191 return {
193 ...sourceConfig,192 ...sourceConfig,
194 connection,193 connection: sourceConfig.connection,
195 };194 };
196};195};
197 196
packages/platform/sdk-file/src/executor-state-store.test.ts+0−166
@@ -1,166 +0,0 @@
1import { tmpdir } from "node:os";
2import { dirname, join } from "node:path";
3import { FileSystem } from "@effect/platform";
4import { NodeFileSystem } from "@effect/platform-node";
5import { describe, expect, it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7
8import type { ResolvedLocalWorkspaceContext } from "./config";
9import {
10 loadLocalExecutorStateSnapshot,
11 localExecutorStatePath,
12 writeLocalExecutorStateSnapshot,
13} from "./executor-state-store";
14
15const makeContext = (): Effect.Effect<
16 ResolvedLocalWorkspaceContext,
17 never,
18 FileSystem.FileSystem
19> =>
20 Effect.gen(function* () {
21 const fs = yield* FileSystem.FileSystem;
22 const workspaceRoot = yield* fs.makeTempDirectory({
23 directory: tmpdir(),
24 prefix: "executor-state-store-",
25 }).pipe(Effect.orDie);
26
27 return {
28 cwd: workspaceRoot,
29 workspaceRoot,
30 workspaceName: "executor-state-store",
31 configDirectory: join(workspaceRoot, ".executor"),
32 projectConfigPath: join(workspaceRoot, ".executor", "executor.jsonc"),
33 homeConfigPath: join(workspaceRoot, ".executor-home.jsonc"),
34 homeStateDirectory: join(workspaceRoot, ".executor-home-state"),
35 artifactsDirectory: join(workspaceRoot, ".executor", "artifacts"),
36 stateDirectory: join(workspaceRoot, ".executor", "state"),
37 };
38 });
39
40describe("local-executor-state-store", () => {
41 it.effect("stores secret-bearing executor state outside the workspace", () =>
42 Effect.gen(function* () {
43 const fs = yield* FileSystem.FileSystem;
44 const context = yield* makeContext();
45 const expectedPath = localExecutorStatePath(context);
46 const workspacePath = join(context.stateDirectory, "executor-state.json");
47
48 yield* writeLocalExecutorStateSnapshot({
49 context,
50 state: {
51 version: 1,
52 authArtifacts: [],
53 authLeases: [],
54 sourceOauthClients: [],
55 scopeOauthClients: [],
56 providerAuthGrants: [],
57 sourceAuthSessions: [],
58 secretMaterials: [],
59 executions: [],
60 executionInteractions: [],
61 executionSteps: [],
62 },
63 });
64
65 expect(expectedPath.startsWith(context.homeStateDirectory)).toBe(true);
66 expect(yield* fs.exists(expectedPath)).toBe(true);
67 expect(yield* fs.exists(workspacePath)).toBe(false);
68
69 const loaded = yield* loadLocalExecutorStateSnapshot(context);
70 expect(loaded.version).toBe(1);
71 expect(loaded.secretMaterials).toEqual([]);
72
73 if (process.platform !== "win32") {
74 expect((yield* fs.stat(expectedPath)).mode & 0o777).toBe(0o600);
75 }
76 }).pipe(Effect.provide(NodeFileSystem.layer)),
77 );
78
79 it.effect("migrates legacy workspace-shaped executor state on load", () =>
80 Effect.gen(function* () {
81 const fs = yield* FileSystem.FileSystem;
82 const context = yield* makeContext();
83 const path = localExecutorStatePath(context);
84
85 yield* fs.makeDirectory(dirname(path), { recursive: true }).pipe(
86 Effect.orDie,
87 );
88 yield* fs.writeFileString(
89 path,
90 `${JSON.stringify({
91 version: 1,
92 authArtifacts: [
93 {
94 id: "auth_art_test",
95 workspaceId: "ws_test",
96 sourceId: "github",
97 actorAccountId: "acc_test",
98 slot: "runtime",
99 artifactKind: "static_bearer",
100 configJson:
101 "{\"headerName\":\"Authorization\",\"prefix\":\"Bearer \",\"token\":{\"providerId\":\"test\",\"handle\":\"secret_test\"}}",
102 grantSetJson: null,
103 createdAt: 1,
104 updatedAt: 1,
105 },
106 ],
107 authLeases: [],
108 sourceOauthClients: [],
109 workspaceOauthClients: [],
110 providerAuthGrants: [],
111 sourceAuthSessions: [],
112 secretMaterials: [
113 {
114 id: "secret_test",
115 providerId: "test",
116 handle: "secret_test",
117 name: null,
118 purpose: "auth_material",
119 value: null,
120 createdAt: 1,
121 updatedAt: 1,
122 },
123 ],
124 executions: [
125 {
126 id: "exec_test",
127 workspaceId: "ws_test",
128 createdByAccountId: "acc_test",
129 status: "completed",
130 code: "return 1;",
131 resultJson: "1",
132 errorText: null,
133 logsJson: null,
134 startedAt: 1,
135 completedAt: 1,
136 createdAt: 1,
137 updatedAt: 1,
138 },
139 ],
140 executionInteractions: [],
141 executionSteps: [],
142 }, null, 2)}\n`,
143 { mode: 0o600 },
144 ).pipe(Effect.orDie);
145
146 const loaded = yield* loadLocalExecutorStateSnapshot(context);
147 expect(loaded.authArtifacts[0]?.scopeId).toBe("ws_test");
148 expect(loaded.authArtifacts[0]?.actorScopeId).toBe("acc_test");
149 expect(loaded.executions[0]?.scopeId).toBe("ws_test");
150 expect(loaded.executions[0]?.createdByScopeId).toBe("acc_test");
151 expect(loaded.scopeOauthClients).toEqual([]);
152
153 const migratedContent = yield* fs.readFileString(path, "utf8").pipe(
154 Effect.orDie,
155 );
156 expect(migratedContent.includes("\"workspaceId\"")).toBe(false);
157 expect(migratedContent.includes("\"actorAccountId\"")).toBe(false);
158 expect(migratedContent.includes("\"createdByAccountId\"")).toBe(false);
159 expect(migratedContent.includes("\"workspaceOauthClients\"")).toBe(false);
160 expect(migratedContent.includes("\"scopeId\"")).toBe(true);
161 expect(migratedContent.includes("\"actorScopeId\"")).toBe(true);
162 expect(migratedContent.includes("\"createdByScopeId\"")).toBe(true);
163 expect(migratedContent.includes("\"scopeOauthClients\"")).toBe(true);
164 }).pipe(Effect.provide(NodeFileSystem.layer)),
165 );
166});
packages/platform/sdk-file/src/executor-state-store.ts+0−634
@@ -4,26 +4,14 @@ import { FileSystem } from "@effect/platform"; · 3 unmodified lines
4import { NodeFileSystem } from "@effect/platform-node";4import { NodeFileSystem } from "@effect/platform-node";
5 5
6import {6import {
7 AuthArtifactSchema,
8 AuthLeaseSchema,
9 type AuthArtifact,
10 type AuthLease,
11 type Execution,7 type Execution,
12 type ExecutionInteraction,8 type ExecutionInteraction,
13 type ExecutionStep,9 type ExecutionStep,
14 ExecutionInteractionSchema,10 ExecutionInteractionSchema,
15 ExecutionSchema,11 ExecutionSchema,
16 ExecutionStepSchema,12 ExecutionStepSchema,
17 type ProviderAuthGrant,
18 ProviderAuthGrantSchema,
19 SecretMaterialSchema,13 SecretMaterialSchema,
20 type SecretMaterial,14 type SecretMaterial,
21 type SourceAuthSession,
22 SourceAuthSessionSchema,
23 type ScopeOauthClient,
24 ScopeOauthClientSchema,
25 type ScopedSourceOauthClient,
26 ScopedSourceOauthClientSchema,
27} from "@executor/platform-sdk/schema";15} from "@executor/platform-sdk/schema";
28import * as Effect from "effect/Effect";16import * as Effect from "effect/Effect";
29import * as Option from "effect/Option";17import * as Option from "effect/Option";
@@ -41,12 +29,6 @@ const LOCAL_EXECUTOR_STATE_BASENAME = "executor-state.json"; · 11 unmodified lines
41 29
42const LocalExecutorStateSnapshotSchema = Schema.Struct({30const LocalExecutorStateSnapshotSchema = Schema.Struct({
43 version: Schema.Literal(LOCAL_EXECUTOR_STATE_VERSION),31 version: Schema.Literal(LOCAL_EXECUTOR_STATE_VERSION),
44 authArtifacts: Schema.Array(AuthArtifactSchema),
45 authLeases: Schema.Array(AuthLeaseSchema),
46 sourceOauthClients: Schema.Array(ScopedSourceOauthClientSchema),
47 scopeOauthClients: Schema.Array(ScopeOauthClientSchema),
48 providerAuthGrants: Schema.Array(ProviderAuthGrantSchema),
49 sourceAuthSessions: Schema.Array(SourceAuthSessionSchema),
50 secretMaterials: Schema.Array(SecretMaterialSchema),32 secretMaterials: Schema.Array(SecretMaterialSchema),
51 executions: Schema.Array(ExecutionSchema),33 executions: Schema.Array(ExecutionSchema),
52 executionInteractions: Schema.Array(ExecutionInteractionSchema),34 executionInteractions: Schema.Array(ExecutionInteractionSchema),
@@ -66,12 +48,6 @@ const decodeLocalExecutorStateSnapshot = Schema.decodeUnknown( · 13 unmodified lines
66 48
67const defaultLocalExecutorStateSnapshot = (): LocalExecutorStateSnapshot => ({49const defaultLocalExecutorStateSnapshot = (): LocalExecutorStateSnapshot => ({
68 version: LOCAL_EXECUTOR_STATE_VERSION,50 version: LOCAL_EXECUTOR_STATE_VERSION,
69 authArtifacts: [],
70 authLeases: [],
71 sourceOauthClients: [],
72 scopeOauthClients: [],
73 providerAuthGrants: [],
74 sourceAuthSessions: [],
75 secretMaterials: [],51 secretMaterials: [],
76 executions: [],52 executions: [],
77 executionInteractions: [],53 executionInteractions: [],
@@ -144,18 +120,6 @@ const mapFileSystemError = (path: string, action: string) => (cause: unknown) => · 66 unmodified lines
144 details: unknownLocalErrorDetails(cause),120 details: unknownLocalErrorDetails(cause),
145 });121 });
146 122
147const actorEquals = (
148 left: string | null | undefined,
149 right: string | null | undefined,
150): boolean => (left ?? null) === (right ?? null);
151
152const sortByUpdatedAtAndIdAsc = <T extends { updatedAt: number; id: string }>(
153 values: readonly T[],
154): T[] =>
155 [...values].sort((left, right) =>
156 left.updatedAt - right.updatedAt || left.id.localeCompare(right.id),
157 );
158
159const sortByUpdatedAtAndIdDesc = <T extends { updatedAt: number; id: string }>(123const sortByUpdatedAtAndIdDesc = <T extends { updatedAt: number; id: string }>(
160 values: readonly T[],124 values: readonly T[],
161): T[] =>125): T[] =>
@@ -264,140 +228,11 @@ const mergeById = <T extends { id: string }>( · 102 unmodified lines
264 return [...merged.values()];228 return [...merged.values()];
265};229};
266 230
267const mergeAuthLeases = (
268 current: readonly AuthLease[],
269 imported: readonly AuthLease[],
270): AuthLease[] => {
271 const merged = new Map<string, AuthLease>();
272
273 for (const lease of imported) {
274 merged.set(lease.authArtifactId, cloneValue(lease));
275 }
276
277 for (const lease of current) {
278 merged.set(lease.authArtifactId, cloneValue(lease));
279 }
280
281 return [...merged.values()];
282};
283
284const mergeAuthArtifacts = (
285 current: readonly AuthArtifact[],
286 imported: readonly AuthArtifact[],
287): AuthArtifact[] => {
288 const merged = new Map<string, AuthArtifact>();
289
290 for (const artifact of imported) {
291 merged.set(
292 [
293 artifact.scopeId,
294 artifact.sourceId,
295 artifact.actorScopeId ?? "",
296 artifact.slot,
297 ].join("::"),
298 cloneValue(artifact),
299 );
300 }
301
302 for (const artifact of current) {
303 merged.set(
304 [
305 artifact.scopeId,
306 artifact.sourceId,
307 artifact.actorScopeId ?? "",
308 artifact.slot,
309 ].join("::"),
310 cloneValue(artifact),
311 );
312 }
313
314 return [...merged.values()];
315};
316
317const mergeSourceOauthClients = (
318 current: readonly ScopedSourceOauthClient[],
319 imported: readonly ScopedSourceOauthClient[],
320): ScopedSourceOauthClient[] => {
321 const merged = new Map<string, ScopedSourceOauthClient>();
322
323 for (const oauthClient of imported) {
324 merged.set(
325 [oauthClient.scopeId, oauthClient.sourceId, oauthClient.providerKey].join(
326 "::",
327 ),
328 cloneValue(oauthClient),
329 );
330 }
331
332 for (const oauthClient of current) {
333 merged.set(
334 [oauthClient.scopeId, oauthClient.sourceId, oauthClient.providerKey].join(
335 "::",
336 ),
337 cloneValue(oauthClient),
338 );
339 }
340
341 return [...merged.values()];
342};
343
344const mergeWorkspaceOauthClients = (
345 current: readonly ScopeOauthClient[],
346 imported: readonly ScopeOauthClient[],
347): ScopeOauthClient[] => {
348 const merged = new Map<string, ScopeOauthClient>();
349
350 for (const oauthClient of imported) {
351 merged.set(
352 [oauthClient.scopeId, oauthClient.providerKey, oauthClient.id].join("::"),
353 cloneValue(oauthClient),
354 );
355 }
356
357 for (const oauthClient of current) {
358 merged.set(
359 [oauthClient.scopeId, oauthClient.providerKey, oauthClient.id].join("::"),
360 cloneValue(oauthClient),
361 );
362 }
363
364 return [...merged.values()];
365};
366
367const mergeProviderAuthGrants = (
368 current: readonly ProviderAuthGrant[],
369 imported: readonly ProviderAuthGrant[],
370): ProviderAuthGrant[] => mergeById(current, imported);
371
372export const mergeImportedLocalExecutorStateSnapshot = (input: {231export const mergeImportedLocalExecutorStateSnapshot = (input: {
373 current: LocalExecutorStateSnapshot;232 current: LocalExecutorStateSnapshot;
374 imported: Partial<Omit<LocalExecutorStateSnapshot, "version">>;233 imported: Partial<Omit<LocalExecutorStateSnapshot, "version">>;
375}): LocalExecutorStateSnapshot => ({234}): LocalExecutorStateSnapshot => ({
376 version: LOCAL_EXECUTOR_STATE_VERSION,235 version: LOCAL_EXECUTOR_STATE_VERSION,
377 authArtifacts: mergeAuthArtifacts(
378 input.current.authArtifacts,
379 input.imported.authArtifacts ?? [],
380 ),
381 authLeases: mergeAuthLeases(
382 input.current.authLeases,
383 input.imported.authLeases ?? [],
384 ),
385 sourceOauthClients: mergeSourceOauthClients(
386 input.current.sourceOauthClients,
387 input.imported.sourceOauthClients ?? [],
388 ),
389 scopeOauthClients: mergeWorkspaceOauthClients(
390 input.current.scopeOauthClients,
391 input.imported.scopeOauthClients ?? [],
392 ),
393 providerAuthGrants: mergeProviderAuthGrants(
394 input.current.providerAuthGrants,
395 input.imported.providerAuthGrants ?? [],
396 ),
397 sourceAuthSessions: mergeById(
398 input.current.sourceAuthSessions,
399 input.imported.sourceAuthSessions ?? [],
400 ),
401 secretMaterials: mergeById(236 secretMaterials: mergeById(
402 input.current.secretMaterials,237 input.current.secretMaterials,
403 input.imported.secretMaterials ?? [],238 input.imported.secretMaterials ?? [],
@@ -499,474 +334,6 @@ export const createLocalExecutorStateStore = ( · 95 unmodified lines
499 const stateManager = createStateManager(context, fileSystem);334 const stateManager = createStateManager(context, fileSystem);
500 335
501 return {336 return {
502 authArtifacts: {
503 listByScopeId: (scopeId: AuthArtifact["scopeId"]) =>
504 stateManager.read((state) =>
505 sortByUpdatedAtAndIdAsc(
506 state.authArtifacts.filter((artifact) => artifact.scopeId === scopeId),
507 ),
508 ),
509
510 listByScopeAndSourceId: (input: {
511 scopeId: AuthArtifact["scopeId"];
512 sourceId: AuthArtifact["sourceId"];
513 }) =>
514 stateManager.read((state) =>
515 sortByUpdatedAtAndIdAsc(
516 state.authArtifacts.filter(
517 (artifact) =>
518 artifact.scopeId === input.scopeId
519 && artifact.sourceId === input.sourceId,
520 ),
521 ),
522 ),
523
524 getByScopeSourceAndActor: (input: {
525 scopeId: AuthArtifact["scopeId"];
526 sourceId: AuthArtifact["sourceId"];
527 actorScopeId: AuthArtifact["actorScopeId"];
528 slot: AuthArtifact["slot"];
529 }) =>
530 stateManager.read((state) => {
531 const artifact = state.authArtifacts.find(
532 (candidate) =>
533 candidate.scopeId === input.scopeId
534 && candidate.sourceId === input.sourceId
535 && candidate.slot === input.slot
536 && actorEquals(candidate.actorScopeId, input.actorScopeId),
537 );
538
539 return artifact ? Option.some(cloneValue(artifact)) : Option.none<AuthArtifact>();
540 }),
541
542 upsert: (artifact: AuthArtifact) =>
543 stateManager.mutate((state) => {
544 const nextArtifacts = state.authArtifacts.filter(
545 (candidate) =>
546 !(
547 candidate.scopeId === artifact.scopeId
548 && candidate.sourceId === artifact.sourceId
549 && candidate.slot === artifact.slot
550 && actorEquals(candidate.actorScopeId, artifact.actorScopeId)
551 ),
552 );
553 nextArtifacts.push(cloneValue(artifact));
554
555 return {
556 state: {
557 ...state,
558 authArtifacts: nextArtifacts,
559 },
560 value: undefined,
561 } satisfies StateMutationResult<void>;
562 }),
563
564 removeByScopeSourceAndActor: (input: {
565 scopeId: AuthArtifact["scopeId"];
566 sourceId: AuthArtifact["sourceId"];
567 actorScopeId: AuthArtifact["actorScopeId"];
568 slot?: AuthArtifact["slot"];
569 }) =>
570 stateManager.mutate((state) => {
571 const nextArtifacts = state.authArtifacts.filter(
572 (candidate) =>
573 candidate.scopeId !== input.scopeId
574 || candidate.sourceId !== input.sourceId
575 || !actorEquals(candidate.actorScopeId, input.actorScopeId)
576 || (input.slot !== undefined && candidate.slot !== input.slot),
577 );
578
579 return {
580 state: {
581 ...state,
582 authArtifacts: nextArtifacts,
583 },
584 value: nextArtifacts.length !== state.authArtifacts.length,
585 } satisfies StateMutationResult<boolean>;
586 }),
587
588 removeByScopeAndSourceId: (input: {
589 scopeId: AuthArtifact["scopeId"];
590 sourceId: AuthArtifact["sourceId"];
591 }) =>
592 stateManager.mutate((state) => {
593 const nextArtifacts = state.authArtifacts.filter(
594 (candidate) =>
595 candidate.scopeId !== input.scopeId
596 || candidate.sourceId !== input.sourceId,
597 );
598
599 return {
600 state: {
601 ...state,
602 authArtifacts: nextArtifacts,
603 },
604 value: state.authArtifacts.length - nextArtifacts.length,
605 } satisfies StateMutationResult<number>;
606 }),
607 },
608
609 authLeases: {
610 listAll: () =>
611 stateManager.read((state) => sortByUpdatedAtAndIdAsc(state.authLeases)),
612
613 getByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) =>
614 stateManager.read((state) => {
615 const lease = state.authLeases.find(
616 (candidate) => candidate.authArtifactId === authArtifactId,
617 );
618 return lease ? Option.some(cloneValue(lease)) : Option.none<AuthLease>();
619 }),
620
621 upsert: (lease: AuthLease) =>
622 stateManager.mutate((state) => {
623 const nextLeases = state.authLeases.filter(
624 (candidate) => candidate.authArtifactId !== lease.authArtifactId,
625 );
626 nextLeases.push(cloneValue(lease));
627
628 return {
629 state: {
630 ...state,
631 authLeases: nextLeases,
632 },
633 value: undefined,
634 } satisfies StateMutationResult<void>;
635 }),
636
637 removeByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) =>
638 stateManager.mutate((state) => {
639 const nextLeases = state.authLeases.filter(
640 (candidate) => candidate.authArtifactId !== authArtifactId,
641 );
642
643 return {
644 state: {
645 ...state,
646 authLeases: nextLeases,
647 },
648 value: nextLeases.length !== state.authLeases.length,
649 } satisfies StateMutationResult<boolean>;
650 }),
651 },
652
653 sourceOauthClients: {
654 getByScopeSourceAndProvider: (input: {
655 scopeId: ScopedSourceOauthClient["scopeId"];
656 sourceId: ScopedSourceOauthClient["sourceId"];
657 providerKey: string;
658 }) =>
659 stateManager.read((state) => {
660 const oauthClient = state.sourceOauthClients.find(
661 (candidate) =>
662 candidate.scopeId === input.scopeId
663 && candidate.sourceId === input.sourceId
664 && candidate.providerKey === input.providerKey,
665 );
666
667 return oauthClient
668 ? Option.some(cloneValue(oauthClient))
669 : Option.none<ScopedSourceOauthClient>();
670 }),
671
672 upsert: (oauthClient: ScopedSourceOauthClient) =>
673 stateManager.mutate((state) => {
674 const nextOauthClients = state.sourceOauthClients.filter(
675 (candidate) =>
676 !(
677 candidate.scopeId === oauthClient.scopeId
678 && candidate.sourceId === oauthClient.sourceId
679 && candidate.providerKey === oauthClient.providerKey
680 ),
681 );
682 nextOauthClients.push(cloneValue(oauthClient));
683
684 return {
685 state: {
686 ...state,
687 sourceOauthClients: nextOauthClients,
688 },
689 value: undefined,
690 } satisfies StateMutationResult<void>;
691 }),
692
693 removeByScopeAndSourceId: (input: {
694 scopeId: ScopedSourceOauthClient["scopeId"];
695 sourceId: ScopedSourceOauthClient["sourceId"];
696 }) =>
697 stateManager.mutate((state) => {
698 const nextOauthClients = state.sourceOauthClients.filter(
699 (candidate) =>
700 candidate.scopeId !== input.scopeId
701 || candidate.sourceId !== input.sourceId,
702 );
703
704 return {
705 state: {
706 ...state,
707 sourceOauthClients: nextOauthClients,
708 },
709 value: state.sourceOauthClients.length - nextOauthClients.length,
710 } satisfies StateMutationResult<number>;
711 }),
712 },
713
714 scopeOauthClients: {
715 listByScopeAndProvider: (input: {
716 scopeId: ScopeOauthClient["scopeId"];
717 providerKey: string;
718 }) =>
719 stateManager.read((state) =>
720 sortByUpdatedAtAndIdAsc(
721 state.scopeOauthClients.filter(
722 (candidate) =>
723 candidate.scopeId === input.scopeId
724 && candidate.providerKey === input.providerKey,
725 ),
726 ),
727 ),
728
729 getById: (id: ScopeOauthClient["id"]) =>
730 stateManager.read((state) => {
731 const oauthClient = state.scopeOauthClients.find(
732 (candidate) => candidate.id === id,
733 );
734
735 return oauthClient
736 ? Option.some(cloneValue(oauthClient))
737 : Option.none<ScopeOauthClient>();
738 }),
739
740 upsert: (oauthClient: ScopeOauthClient) =>
741 stateManager.mutate((state) => {
742 const nextOauthClients = state.scopeOauthClients.filter(
743 (candidate) => candidate.id !== oauthClient.id,
744 );
745 nextOauthClients.push(cloneValue(oauthClient));
746
747 return {
748 state: {
749 ...state,
750 scopeOauthClients: nextOauthClients,
751 },
752 value: undefined,
753 } satisfies StateMutationResult<void>;
754 }),
755
756 removeById: (id: ScopeOauthClient["id"]) =>
757 stateManager.mutate((state) => {
758 const nextOauthClients = state.scopeOauthClients.filter(
759 (candidate) => candidate.id !== id,
760 );
761
762 return {
763 state: {
764 ...state,
765 scopeOauthClients: nextOauthClients,
766 },
767 value: nextOauthClients.length !== state.scopeOauthClients.length,
768 } satisfies StateMutationResult<boolean>;
769 }),
770 },
771
772 providerAuthGrants: {
773 listByScopeId: (scopeId: ProviderAuthGrant["scopeId"]) =>
774 stateManager.read((state) =>
775 sortByUpdatedAtAndIdAsc(
776 state.providerAuthGrants.filter(
777 (grant) => grant.scopeId === scopeId,
778 ),
779 ),
780 ),
781
782 listByScopeActorAndProvider: (input: {
783 scopeId: ProviderAuthGrant["scopeId"];
784 actorScopeId: ProviderAuthGrant["actorScopeId"];
785 providerKey: string;
786 }) =>
787 stateManager.read((state) =>
788 sortByUpdatedAtAndIdAsc(
789 state.providerAuthGrants.filter(
790 (grant) =>
791 grant.scopeId === input.scopeId
792 && grant.providerKey === input.providerKey
793 && actorEquals(grant.actorScopeId, input.actorScopeId),
794 ),
795 ),
796 ),
797
798 getById: (id: ProviderAuthGrant["id"]) =>
799 stateManager.read((state) => {
800 const grant = state.providerAuthGrants.find(
801 (candidate) => candidate.id === id,
802 );
803
804 return grant
805 ? Option.some(cloneValue(grant))
806 : Option.none<ProviderAuthGrant>();
807 }),
808
809 upsert: (grant: ProviderAuthGrant) =>
810 stateManager.mutate((state) => {
811 const nextGrants = state.providerAuthGrants.filter(
812 (candidate) => candidate.id !== grant.id,
813 );
814 nextGrants.push(cloneValue(grant));
815
816 return {
817 state: {
818 ...state,
819 providerAuthGrants: nextGrants,
820 },
821 value: undefined,
822 } satisfies StateMutationResult<void>;
823 }),
824
825 removeById: (id: ProviderAuthGrant["id"]) =>
826 stateManager.mutate((state) => {
827 const nextGrants = state.providerAuthGrants.filter(
828 (candidate) => candidate.id !== id,
829 );
830
831 return {
832 state: {
833 ...state,
834 providerAuthGrants: nextGrants,
835 },
836 value: nextGrants.length !== state.providerAuthGrants.length,
837 } satisfies StateMutationResult<boolean>;
838 }),
839 },
840
841 sourceAuthSessions: {
842 listAll: () =>
843 stateManager.read((state) => sortByUpdatedAtAndIdAsc(state.sourceAuthSessions)),
844
845 listByScopeId: (scopeId: SourceAuthSession["scopeId"]) =>
846 stateManager.read((state) =>
847 sortByUpdatedAtAndIdAsc(
848 state.sourceAuthSessions.filter(
849 (session) => session.scopeId === scopeId,
850 ),
851 ),
852 ),
853
854 getById: (id: SourceAuthSession["id"]) =>
855 stateManager.read((state) => {
856 const session = state.sourceAuthSessions.find(
857 (candidate) => candidate.id === id,
858 );
859 return session
860 ? Option.some(cloneValue(session))
861 : Option.none<SourceAuthSession>();
862 }),
863
864 getByState: (stateValue: SourceAuthSession["state"]) =>
865 stateManager.read((state) => {
866 const session = state.sourceAuthSessions.find(
867 (candidate) => candidate.state === stateValue,
868 );
869 return session
870 ? Option.some(cloneValue(session))
871 : Option.none<SourceAuthSession>();
872 }),
873
874 getPendingByScopeSourceAndActor: (input: {
875 scopeId: SourceAuthSession["scopeId"];
876 sourceId: SourceAuthSession["sourceId"];
877 actorScopeId: SourceAuthSession["actorScopeId"];
878 credentialSlot?: SourceAuthSession["credentialSlot"];
879 }) =>
880 stateManager.read((state) => {
881 const session = sortByUpdatedAtAndIdAsc(
882 state.sourceAuthSessions.filter(
883 (candidate) =>
884 candidate.scopeId === input.scopeId
885 && candidate.sourceId === input.sourceId
886 && candidate.status === "pending"
887 && actorEquals(candidate.actorScopeId, input.actorScopeId)
888 && (input.credentialSlot === undefined
889 || candidate.credentialSlot === input.credentialSlot),
890 ),
891 )[0] ?? null;
892
893 return session
894 ? Option.some(cloneValue(session))
895 : Option.none<SourceAuthSession>();
896 }),
897
898 insert: (session: SourceAuthSession) =>
899 stateManager.mutate((state) => ({
900 state: {
901 ...state,
902 sourceAuthSessions: [...state.sourceAuthSessions, cloneValue(session)],
903 },
904 value: undefined,
905 } satisfies StateMutationResult<void>)),
906
907 update: (
908 id: SourceAuthSession["id"],
909 patch: Partial<Omit<SourceAuthSession, "id" | "scopeId" | "sourceId" | "createdAt">>,
910 ) =>
911 stateManager.mutate((state) => {
912 let updated: SourceAuthSession | null = null;
913 const nextSessions = state.sourceAuthSessions.map((session) => {
914 if (session.id !== id) {
915 return session;
916 }
917
918 updated = {
919 ...session,
920 ...cloneValue(patch),
921 } satisfies SourceAuthSession;
922 return updated;
923 });
924
925 return {
926 state: {
927 ...state,
928 sourceAuthSessions: nextSessions,
929 },
930 value: updated ? Option.some(cloneValue(updated)) : Option.none<SourceAuthSession>(),
931 } satisfies StateMutationResult<Option.Option<SourceAuthSession>>;
932 }),
933
934 upsert: (session: SourceAuthSession) =>
935 stateManager.mutate((state) => {
936 const nextSessions = state.sourceAuthSessions.filter(
937 (candidate) => candidate.id !== session.id,
938 );
939 nextSessions.push(cloneValue(session));
940
941 return {
942 state: {
943 ...state,
944 sourceAuthSessions: nextSessions,
945 },
946 value: undefined,
947 } satisfies StateMutationResult<void>;
948 }),
949
950 removeByScopeAndSourceId: (
951 scopeId: SourceAuthSession["scopeId"],
952 sourceId: SourceAuthSession["sourceId"],
953 ) =>
954 stateManager.mutate((state) => {
955 const nextSessions = state.sourceAuthSessions.filter(
956 (candidate) =>
957 candidate.scopeId !== scopeId || candidate.sourceId !== sourceId,
958 );
959
960 return {
961 state: {
962 ...state,
963 sourceAuthSessions: nextSessions,
964 },
965 value: nextSessions.length !== state.sourceAuthSessions.length,
966 } satisfies StateMutationResult<boolean>;
967 }),
968 },
969
970 secretMaterials: {337 secretMaterials: {
971 getById: (id: SecretMaterial["id"]) =>338 getById: (id: SecretMaterial["id"]) =>
972 stateManager.read((state) => {339 stateManager.read((state) => {
@@ -1041,7 +408,6 @@ export const createLocalExecutorStateStore = ( · 68 unmodified lines
1041 } satisfies StateMutationResult<boolean>;408 } satisfies StateMutationResult<boolean>;
1042 }),409 }),
1043 },410 },
1044
1045 executions: {411 executions: {
1046 getById: (executionId: Execution["id"]) =>412 getById: (executionId: Execution["id"]) =>
1047 stateManager.read((state) => {413 stateManager.read((state) => {
packages/platform/sdk-file/src/index.ts+27−20
@@ -12,9 +12,13 @@ import { · 11 unmodified lines
12} from "@executor/platform-sdk";12} from "@executor/platform-sdk";
13import {13import {
14 createExecutorEffect,14 createExecutorEffect,
15 type CreateExecutorEffectOptions,
15 type ExecutorEffect,16 type ExecutorEffect,
16 type CreateExecutorEffectOptions as CreateExecutorOptions,
17} from "@executor/platform-sdk/effect";17} from "@executor/platform-sdk/effect";
18import type {
19 ExecutorSdkPlugin,
20 ExecutorSdkPluginExtensions,
21} from "@executor/platform-sdk/plugins";
18import type {22import type {
19 LocalExecutorConfig,23 LocalExecutorConfig,
20 LocalInstallation,24 LocalInstallation,
@@ -263,18 +267,6 @@ export const createLocalExecutorRepositoriesEffect = ( · 242 unmodified lines
263 workspaceContext,267 workspaceContext,
264 fileSystem,268 fileSystem,
265 ),269 ),
266 sourceAuth: {
267 artifacts: executorStateStorage.executorState.authArtifacts,
268 leases: executorStateStorage.executorState.authLeases,
269 sourceOauthClients:
270 executorStateStorage.executorState.sourceOauthClients,
271 scopeOauthClients:
272 executorStateStorage.executorState.scopeOauthClients,
273 providerGrants:
274 executorStateStorage.executorState.providerAuthGrants,
275 sourceSessions:
276 executorStateStorage.executorState.sourceAuthSessions,
277 },
278 localTools: createBoundLocalToolRuntimeLoader(270 localTools: createBoundLocalToolRuntimeLoader(
279 workspaceContext,271 workspaceContext,
280 fileSystem,272 fileSystem,
@@ -326,22 +318,37 @@ export const createLocalExecutorRuntime = ( · 45 unmodified lines
326 getLocalServerBaseUrl: options.getLocalServerBaseUrl,318 getLocalServerBaseUrl: options.getLocalServerBaseUrl,
327 });319 });
328 320
329export const createLocalExecutorEffect = (321type CreateLocalExecutorOptions<
330 options: CreateLocalExecutorBackendOptions & ExecutorRuntimeOptions = {},322 TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
331): Effect.Effect<ExecutorEffect, Error> =>323> = CreateLocalExecutorBackendOptions & ExecutorRuntimeOptions & {
324 plugins?: TPlugins;
325};
326
327export const createLocalExecutorEffect = <
328 const TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
329>(
330 options: CreateLocalExecutorOptions<TPlugins> = {},
331): Effect.Effect<
332 ExecutorEffect & ExecutorSdkPluginExtensions<TPlugins>,
333 Error
334> =>
332 createExecutorEffect({335 createExecutorEffect({
333 backend: createLocalExecutorBackend(options),336 backend: createLocalExecutorBackend(options),
337 plugins: options.plugins,
334 executionResolver: options.executionResolver,338 executionResolver: options.executionResolver,
335 createInternalToolMap: options.createInternalToolMap,339 createInternalToolMap: options.createInternalToolMap,
336 resolveSecretMaterial: options.resolveSecretMaterial,340 resolveSecretMaterial: options.resolveSecretMaterial,
337 getLocalServerBaseUrl: options.getLocalServerBaseUrl,341 getLocalServerBaseUrl: options.getLocalServerBaseUrl,
338 } satisfies CreateExecutorOptions);342 } satisfies CreateExecutorEffectOptions & { plugins?: TPlugins });
339 343
340export const createLocalExecutor = (344export const createLocalExecutor = <
341 options: CreateLocalExecutorBackendOptions & ExecutorRuntimeOptions = {},345 const TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
342): Promise<Executor> =>346>(
347 options: CreateLocalExecutorOptions<TPlugins> = {},
348): Promise<Executor<TPlugins>> =>
343 createExecutor({349 createExecutor({
344 backend: createLocalExecutorBackend(options),350 backend: createLocalExecutorBackend(options),
351 plugins: options.plugins,
345 executionResolver: options.executionResolver,352 executionResolver: options.executionResolver,
346 createInternalToolMap: options.createInternalToolMap,353 createInternalToolMap: options.createInternalToolMap,
347 resolveSecretMaterial: options.resolveSecretMaterial,354 resolveSecretMaterial: options.resolveSecretMaterial,
packages/platform/sdk/package.json+1−1
@@ -10,6 +10,7 @@ · 9 unmodified lines
10 "./executor": "./src/executor.ts",10 "./executor": "./src/executor.ts",
11 "./errors": "./src/errors.ts",11 "./errors": "./src/errors.ts",
12 "./operations": "./src/operations.ts",12 "./operations": "./src/operations.ts",
13 "./plugins": "./src/plugins.ts",
13 "./string-schemas": "./src/string-schemas.ts",14 "./string-schemas": "./src/string-schemas.ts",
14 "./schema": "./src/schema/index.ts",15 "./schema": "./src/schema/index.ts",
15 "./runtime": "./src/runtime/index.ts"16 "./runtime": "./src/runtime/index.ts"
@@ -34,7 +35,6 @@ · 18 unmodified lines
34 "@executor/runtime-quickjs": "workspace:*",35 "@executor/runtime-quickjs": "workspace:*",
35 "@executor/runtime-ses": "workspace:*",36 "@executor/runtime-ses": "workspace:*",
36 "@executor/source-core": "workspace:*",37 "@executor/source-core": "workspace:*",
37 "@executor/source-builtins": "workspace:*",
38 "effect": "catalog:"38 "effect": "catalog:"
39 },39 },
40 "devDependencies": {40 "devDependencies": {
packages/platform/sdk/src/backend.ts+0−71
@@ -11,7 +11,6 @@ import { · 10 unmodified lines
11 type BoundScopeStateStore,11 type BoundScopeStateStore,
12 type ExecutorRuntime,12 type ExecutorRuntime,
13 type ExecutorRuntimeOptions,13 type ExecutorRuntimeOptions,
14 type RuntimeAuthStorageServices,
15 type RuntimeExecutionStorageServices,14 type RuntimeExecutionStorageServices,
16 type RuntimeInstanceConfigService,15 type RuntimeInstanceConfigService,
17 type RuntimeSecretsStorageServices,16 type RuntimeSecretsStorageServices,
@@ -59,14 +58,6 @@ export type ExecutorWorkspaceLocalToolRepository = PublicizeObject< · 41 unmodified lines
59export type ExecutorWorkspaceSourceTypeDeclarationsRepository = PublicizeObject<58export type ExecutorWorkspaceSourceTypeDeclarationsRepository = PublicizeObject<
60 BoundSourceTypeDeclarationsRefresher59 BoundSourceTypeDeclarationsRefresher
61>;60>;
62export type ExecutorWorkspaceSourceAuthRepository = {
63 artifacts: PublicizeObject<RuntimeAuthStorageServices["artifacts"]>;
64 leases: PublicizeObject<RuntimeAuthStorageServices["leases"]>;
65 sourceOauthClients: PublicizeObject<RuntimeAuthStorageServices["sourceOauthClients"]>;
66 scopeOauthClients: PublicizeObject<RuntimeAuthStorageServices["scopeOauthClients"]>;
67 providerGrants: PublicizeObject<RuntimeAuthStorageServices["providerGrants"]>;
68 sourceSessions: PublicizeObject<RuntimeAuthStorageServices["sourceSessions"]>;
69};
70export type ExecutorSecretRepository = PublicizeObject<RuntimeSecretsStorageServices>;61export type ExecutorSecretRepository = PublicizeObject<RuntimeSecretsStorageServices>;
71export type ExecutorExecutionRepository = {62export type ExecutorExecutionRepository = {
72 runs: PublicizeObject<RuntimeExecutionStorageServices["runs"]>;63 runs: PublicizeObject<RuntimeExecutionStorageServices["runs"]>;
@@ -81,7 +72,6 @@ export type ExecutorWorkspaceRepository = { · 8 unmodified lines
81 config: ExecutorWorkspaceConfigRepository;72 config: ExecutorWorkspaceConfigRepository;
82 state: ExecutorWorkspaceStateRepository;73 state: ExecutorWorkspaceStateRepository;
83 sourceArtifacts: ExecutorWorkspaceSourceArtifactRepository;74 sourceArtifacts: ExecutorWorkspaceSourceArtifactRepository;
84 sourceAuth: ExecutorWorkspaceSourceAuthRepository;
85 localTools?: ExecutorWorkspaceLocalToolRepository;75 localTools?: ExecutorWorkspaceLocalToolRepository;
86 sourceTypeDeclarations?: ExecutorWorkspaceSourceTypeDeclarationsRepository;76 sourceTypeDeclarations?: ExecutorWorkspaceSourceTypeDeclarationsRepository;
87};77};
@@ -175,66 +165,6 @@ const toSourceTypeDeclarationsBackend = ( · 87 unmodified lines
175 toEffect(input.refreshSourceInBackground(payload)).pipe(Effect.orDie),165 toEffect(input.refreshSourceInBackground(payload)).pipe(Effect.orDie),
176});166});
177 167
178const toAuthBackend = (
179 input: ExecutorWorkspaceSourceAuthRepository,
180): RuntimeAuthStorageServices => ({
181 artifacts: {
182 listByScopeId: (scopeId) => toEffect(input.artifacts.listByScopeId(scopeId)),
183 listByScopeAndSourceId: (payload) =>
184 toEffect(input.artifacts.listByScopeAndSourceId(payload)),
185 getByScopeSourceAndActor: (payload) =>
186 toOptionEffect(input.artifacts.getByScopeSourceAndActor(payload)),
187 upsert: (artifact) => toEffect(input.artifacts.upsert(artifact)),
188 removeByScopeSourceAndActor: (payload) =>
189 toEffect(input.artifacts.removeByScopeSourceAndActor(payload)),
190 removeByScopeAndSourceId: (payload) =>
191 toEffect(input.artifacts.removeByScopeAndSourceId(payload)),
192 },
193 leases: {
194 listAll: () => toEffect(input.leases.listAll()),
195 getByAuthArtifactId: (authArtifactId) =>
196 toOptionEffect(input.leases.getByAuthArtifactId(authArtifactId)),
197 upsert: (lease) => toEffect(input.leases.upsert(lease)),
198 removeByAuthArtifactId: (authArtifactId) =>
199 toEffect(input.leases.removeByAuthArtifactId(authArtifactId)),
200 },
201 sourceOauthClients: {
202 getByScopeSourceAndProvider: (payload) =>
203 toOptionEffect(input.sourceOauthClients.getByScopeSourceAndProvider(payload)),
204 upsert: (oauthClient) => toEffect(input.sourceOauthClients.upsert(oauthClient)),
205 removeByScopeAndSourceId: (payload) =>
206 toEffect(input.sourceOauthClients.removeByScopeAndSourceId(payload)),
207 },
208 scopeOauthClients: {
209 listByScopeAndProvider: (payload) =>
210 toEffect(input.scopeOauthClients.listByScopeAndProvider(payload)),
211 getById: (id) => toOptionEffect(input.scopeOauthClients.getById(id)),
212 upsert: (oauthClient) => toEffect(input.scopeOauthClients.upsert(oauthClient)),
213 removeById: (id) => toEffect(input.scopeOauthClients.removeById(id)),
214 },
215 providerGrants: {
216 listByScopeId: (scopeId) => toEffect(input.providerGrants.listByScopeId(scopeId)),
217 listByScopeActorAndProvider: (payload) =>
218 toEffect(input.providerGrants.listByScopeActorAndProvider(payload)),
219 getById: (id) => toOptionEffect(input.providerGrants.getById(id)),
220 upsert: (grant) => toEffect(input.providerGrants.upsert(grant)),
221 removeById: (id) => toEffect(input.providerGrants.removeById(id)),
222 },
223 sourceSessions: {
224 listAll: () => toEffect(input.sourceSessions.listAll()),
225 listByScopeId: (scopeId) => toEffect(input.sourceSessions.listByScopeId(scopeId)),
226 getById: (id) => toOptionEffect(input.sourceSessions.getById(id)),
227 getByState: (state) => toOptionEffect(input.sourceSessions.getByState(state)),
228 getPendingByScopeSourceAndActor: (payload) =>
229 toOptionEffect(input.sourceSessions.getPendingByScopeSourceAndActor(payload)),
230 insert: (session) => toEffect(input.sourceSessions.insert(session)),
231 update: (id, patch) => toOptionEffect(input.sourceSessions.update(id, patch)),
232 upsert: (session) => toEffect(input.sourceSessions.upsert(session)),
233 removeByScopeAndSourceId: (scopeId, sourceId) =>
234 toEffect(input.sourceSessions.removeByScopeAndSourceId(scopeId, sourceId)),
235 },
236});
237
238const toSecretsBackend = (168const toSecretsBackend = (
239 input: ExecutorSecretRepository,169 input: ExecutorSecretRepository,
240): RuntimeSecretsStorageServices => ({170): RuntimeSecretsStorageServices => ({
@@ -308,7 +238,6 @@ export const createExecutorBackend = (input: { · 67 unmodified lines
308 sourceArtifacts: toSourceArtifactBackend(238 sourceArtifacts: toSourceArtifactBackend(
309 repositories.workspace.sourceArtifacts,239 repositories.workspace.sourceArtifacts,
310 ),240 ),
311 auth: toAuthBackend(repositories.workspace.sourceAuth),
312 secrets: toSecretsBackend(repositories.secrets),241 secrets: toSecretsBackend(repositories.secrets),
313 executions: toExecutionsBackend(repositories.executions),242 executions: toExecutionsBackend(repositories.executions),
314 close: repositories.close,243 close: repositories.close,
packages/platform/sdk/src/contracts.ts+0−1
@@ -1,4 +1,3 @@
1export * from "./executions/contracts";1export * from "./executions/contracts";
2export * from "./local/contracts";2export * from "./local/contracts";
3export * from "./policies/contracts";3export * from "./policies/contracts";
4export * from "./sources/contracts";
packages/platform/sdk/src/executor-effect.ts+83−126
@@ -4,12 +4,10 @@ import type { · 3 unmodified lines
4 ScopeId,4 ScopeId,
5 Execution,5 Execution,
6 ExecutionEnvelope,6 ExecutionEnvelope,
7 ExecutionInteraction,
8 LocalInstallation,7 LocalInstallation,
9 LocalScopePolicy,8 LocalScopePolicy,
10 Source,9 Source,
11} from "./schema";10} from "./schema";
12import { ExecutionIdSchema } from "./schema";
13import type {11import type {
14 CreateExecutionPayload,12 CreateExecutionPayload,
15 ResumeExecutionPayload,13 ResumeExecutionPayload,
@@ -24,10 +22,7 @@ import type { · 8 unmodified lines
24 UpdateSecretResult,22 UpdateSecretResult,
25} from "./local/contracts";23} from "./local/contracts";
26import {24import {
27 completeSourceCredentialSetup,
28 getLocalInstallation,25 getLocalInstallation,
29 getSourceCredentialInteraction,
30 submitSourceCredentialInteraction,
31} from "./local/operations";26} from "./local/operations";
32import {27import {
33 createLocalSecret,28 createLocalSecret,
@@ -47,21 +42,17 @@ import { · 13 unmodified lines
47 removePolicy,42 removePolicy,
48 updatePolicy,43 updatePolicy,
49} from "./policies/operations";44} from "./policies/operations";
50import type {
51 CreateSourcePayload,
52 UpdateSourcePayload,
53} from "./sources/contracts";
54import {45import {
55 discoverSourceInspectionTools,46 discoverSourceInspectionTools,
56 getSourceInspection,47 getSourceInspection,
57 getSourceInspectionToolDetail,48 getSourceInspectionToolDetail,
58} from "./sources/inspection";49} from "./sources/inspection";
59import {50import {
60 createSource,51 createManagedSource,
61 getSource,52 getSource,
62 listSources,53 listSources,
63 removeSource,54 removeSource,
64 updateSource,55 saveManagedSource,
65} from "./sources/operations";56} from "./sources/operations";
66import type { ExecutorBackend } from "./backend";57import type { ExecutorBackend } from "./backend";
67import {58import {
@@ -71,7 +62,6 @@ import { · 3 unmodified lines
71 type ExecutorRuntimeOptions,62 type ExecutorRuntimeOptions,
72 type ResolveExecutionEnvironment,63 type ResolveExecutionEnvironment,
73 type ResolveSecretMaterial,64 type ResolveSecretMaterial,
74 RuntimeSourceAuthServiceTag,
75} from "./runtime";65} from "./runtime";
76import {66import {
77 createExecution,67 createExecution,
@@ -79,14 +69,14 @@ import { · 1 unmodified line
79 resumeExecution,69 resumeExecution,
80} from "./runtime/execution/service";70} from "./runtime/execution/service";
81import type {71import type {
82 CompleteSourceCredentialSetupResult,72 ExecutorSdkPluginHost,
83 ExecutorAddSourceInput,73 ExecutorSdkPlugin,
84 ExecutorSourceAddResult,74 ExecutorSdkPluginExtensions,
85} from "./runtime/sources/source-auth-service";75} from "./plugins";
76import {
77 configureExecutorSourcePlugins,
78} from "./runtime/sources/source-plugins";
86 79
87type DistributiveOmit<T, Keys extends PropertyKey> = T extends unknown
88 ? Omit<T, Keys>
89 : never;
90type ProvidedEffect<T extends Effect.Effect<any, any, any>> = Effect.Effect<80type ProvidedEffect<T extends Effect.Effect<any, any, any>> = Effect.Effect<
91 Effect.Effect.Success<T>,81 Effect.Effect.Success<T>,
92 Effect.Effect.Error<T>,82 Effect.Effect.Error<T>,
@@ -97,11 +87,6 @@ type MappedProvidedEffect< · 4 unmodified lines
97 A,87 A,
98> = Effect.Effect<A, Effect.Effect.Error<T>, never>;88> = Effect.Effect<A, Effect.Effect.Error<T>, never>;
99 89
100export type ExecutorSourceInput = DistributiveOmit<
101 ExecutorAddSourceInput,
102 "scopeId" | "actorScopeId" | "executionId" | "interactionId"
103>;
104
105export type ExecutorEffect = {90export type ExecutorEffect = {
106 runtime: ExecutorRuntime;91 runtime: ExecutorRuntime;
107 installation: LocalInstallation;92 installation: LocalInstallation;
@@ -112,30 +97,6 @@ export type ExecutorEffect = { · 4 unmodified lines
112 local: {97 local: {
113 installation: () => ProvidedEffect<ReturnType<typeof getLocalInstallation>>;98 installation: () => ProvidedEffect<ReturnType<typeof getLocalInstallation>>;
114 config: () => ProvidedEffect<ReturnType<typeof getLocalInstanceConfig>>;99 config: () => ProvidedEffect<ReturnType<typeof getLocalInstanceConfig>>;
115 credentials: {
116 get: (input: {
117 sourceId: Source["id"];
118 interactionId: ExecutionInteraction["id"];
119 }) => ProvidedEffect<ReturnType<typeof getSourceCredentialInteraction>>;
120 submit: (input: {
121 sourceId: Source["id"];
122 interactionId: ExecutionInteraction["id"];
123 action: "submit" | "continue" | "cancel";
124 token?: string | null;
125 }) => ProvidedEffect<
126 ReturnType<typeof submitSourceCredentialInteraction>
127 >;
128 complete: (input: {
129 sourceId: Source["id"];
130 state: string;
131 code?: string | null;
132 error?: string | null;
133 errorDescription?: string | null;
134 }) => MappedProvidedEffect<
135 ReturnType<typeof completeSourceCredentialSetup>,
136 CompleteSourceCredentialSetupResult
137 >;
138 };
139 };100 };
140 secrets: {101 secrets: {
141 list: () => ProvidedEffect<ReturnType<typeof listLocalSecrets>>;102 list: () => ProvidedEffect<ReturnType<typeof listLocalSecrets>>;
@@ -168,21 +129,8 @@ export type ExecutorEffect = { · 26 unmodified lines
168 ) => MappedProvidedEffect<ReturnType<typeof removePolicy>, boolean>;129 ) => MappedProvidedEffect<ReturnType<typeof removePolicy>, boolean>;
169 };130 };
170 sources: {131 sources: {
171 add: (
172 input: ExecutorSourceInput,
173 options?: {
174 baseUrl?: string | null;
175 },
176 ) => Effect.Effect<ExecutorSourceAddResult, Error, never>;
177 list: () => ProvidedEffect<ReturnType<typeof listSources>>;132 list: () => ProvidedEffect<ReturnType<typeof listSources>>;
178 create: (
179 payload: CreateSourcePayload,
180 ) => ProvidedEffect<ReturnType<typeof createSource>>;
181 get: (sourceId: Source["id"]) => ProvidedEffect<ReturnType<typeof getSource>>;133 get: (sourceId: Source["id"]) => ProvidedEffect<ReturnType<typeof getSource>>;
182 update: (
183 sourceId: Source["id"],
184 payload: UpdateSourcePayload,
185 ) => ProvidedEffect<ReturnType<typeof updateSource>>;
186 remove: (134 remove: (
187 sourceId: Source["id"],135 sourceId: Source["id"],
188 ) => MappedProvidedEffect<ReturnType<typeof removeSource>, boolean>;136 ) => MappedProvidedEffect<ReturnType<typeof removeSource>, boolean>;
@@ -220,24 +168,18 @@ export type CreateExecutorEffectOptions = ExecutorRuntimeOptions & { · 31 unmodified lines
220 backend: ExecutorBackend;168 backend: ExecutorBackend;
221};169};
222 170
171type CreateExecutorEffectOptionsWithPlugins<
172 TPlugins extends readonly ExecutorSdkPlugin<any, any>[],
173> = CreateExecutorEffectOptions & {
174 plugins?: TPlugins;
175};
176
223const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => {177const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => {
224 const installation = runtime.localInstallation;178 const installation = runtime.localInstallation;
225 const scopeId = installation.scopeId;179 const scopeId = installation.scopeId;
226 const actorScopeId = installation.actorScopeId;180 const actorScopeId = installation.actorScopeId;
227 const provide = <A, E, R>(effect: Effect.Effect<A, E, R>) =>181 const provide = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
228 provideExecutorRuntime(effect, runtime);182 provideExecutorRuntime(effect, runtime);
229 const provideSourceAuth = <A, E>(
230 execute: (
231 service: Effect.Effect.Success<typeof RuntimeSourceAuthServiceTag>,
232 ) => Effect.Effect<A, E, any>,
233 ) => provide(Effect.flatMap(RuntimeSourceAuthServiceTag, execute));
234 const createSdkSourceSession = () => {
235 const id = crypto.randomUUID();
236 return {
237 executionId: ExecutionIdSchema.make(`exec_sdk_${id}`),
238 interactionId: `executor.sdk.${id}` as never,
239 };
240 };
241 183
242 return {184 return {
243 runtime,185 runtime,
@@ -249,37 +191,6 @@ const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => { · 5 unmodified lines
249 local: {191 local: {
250 installation: () => provide(getLocalInstallation()),192 installation: () => provide(getLocalInstallation()),
251 config: () => provide(getLocalInstanceConfig()),193 config: () => provide(getLocalInstanceConfig()),
252 credentials: {
253 get: ({ sourceId, interactionId }) =>
254 provide(
255 getSourceCredentialInteraction({
256 scopeId,
257 sourceId,
258 interactionId,
259 }),
260 ),
261 submit: ({ sourceId, interactionId, action, token }) =>
262 provide(
263 submitSourceCredentialInteraction({
264 scopeId,
265 sourceId,
266 interactionId,
267 action,
268 token,
269 }),
270 ),
271 complete: ({ sourceId, state, code, error, errorDescription }) =>
272 provide(
273 completeSourceCredentialSetup({
274 scopeId,
275 sourceId,
276 state,
277 code,
278 error,
279 errorDescription,
280 }),
281 ),
282 },
283 },194 },
284 secrets: {195 secrets: {
285 list: () => provide(listLocalSecrets()),196 list: () => provide(listLocalSecrets()),
@@ -300,25 +211,8 @@ const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => { · 14 unmodified lines
300 ),211 ),
301 },212 },
302 sources: {213 sources: {
303 add: (input, options) =>
304 provideSourceAuth((service) => {
305 const session = createSdkSourceSession();
306 return service.addExecutorSource(
307 {
308 ...input,
309 scopeId,
310 actorScopeId,
311 executionId: session.executionId,
312 interactionId: session.interactionId,
313 },
314 options,
315 );
316 }),
317 list: () => provide(listSources({ scopeId, actorScopeId })),214 list: () => provide(listSources({ scopeId, actorScopeId })),
318 create: (payload) => provide(createSource({ scopeId, actorScopeId, payload })),
319 get: (sourceId) => provide(getSource({ scopeId, sourceId, actorScopeId })),215 get: (sourceId) => provide(getSource({ scopeId, sourceId, actorScopeId })),
320 update: (sourceId, payload) =>
321 provide(updateSource({ scopeId, sourceId, actorScopeId, payload })),
322 remove: (sourceId) =>216 remove: (sourceId) =>
323 provide(removeSource({ scopeId, sourceId })).pipe(217 provide(removeSource({ scopeId, sourceId })).pipe(
324 Effect.map((result) => result.removed),218 Effect.map((result) => result.removed),
@@ -366,15 +260,78 @@ const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => { · 41 unmodified lines
366 };260 };
367};261};
368 262
369export const createExecutorEffect = (263export const createExecutorEffect = <
370 options: CreateExecutorEffectOptions,264 const TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
371): Effect.Effect<ExecutorEffect, Error> =>265>(
372 Effect.map(266 options: CreateExecutorEffectOptionsWithPlugins<TPlugins>,
267): Effect.Effect<ExecutorEffect & ExecutorSdkPluginExtensions<TPlugins>, Error> => {
268 configureExecutorSourcePlugins(options.plugins ?? []);
269
270 return Effect.map(
373 options.backend.createRuntime({271 options.backend.createRuntime({
374 executionResolver: options.executionResolver,272 executionResolver: options.executionResolver,
375 createInternalToolMap: options.createInternalToolMap,273 createInternalToolMap: options.createInternalToolMap,
376 resolveSecretMaterial: options.resolveSecretMaterial,274 resolveSecretMaterial: options.resolveSecretMaterial,
377 getLocalServerBaseUrl: options.getLocalServerBaseUrl,275 getLocalServerBaseUrl: options.getLocalServerBaseUrl,
378 }),276 }),
379 fromRuntime,277 (runtime) => {
278 const executor = fromRuntime(runtime);
279 const providePluginHostEffect = <A>(
280 effect: Effect.Effect<A, unknown, any>,
281 ): Effect.Effect<A, Error, never> =>
282 provideExecutorRuntime(effect, runtime).pipe(
283 Effect.mapError((cause) =>
284 cause instanceof Error ? cause : new Error(String(cause)),
285 ),
286 );
287 const host: ExecutorSdkPluginHost = {
288 sources: {
289 create: ({ source }) =>
290 providePluginHostEffect(
291 createManagedSource({
292 scopeId: executor.scopeId,
293 actorScopeId: executor.actorScopeId,
294 source,
295 }),
296 ),
297 get: (sourceId) =>
298 providePluginHostEffect(
299 getSource({
300 scopeId: executor.scopeId,
301 sourceId,
302 actorScopeId: executor.actorScopeId,
303 }),
304 ),
305 save: (source) =>
306 providePluginHostEffect(
307 saveManagedSource({
308 actorScopeId: executor.actorScopeId,
309 source,
310 }),
311 ),
312 remove: (sourceId) =>
313 providePluginHostEffect(
314 removeSource({
315 scopeId: executor.scopeId,
316 sourceId,
317 }).pipe(Effect.map((result) => result.removed)),
318 ),
319 },
320 };
321 const extensions = Object.fromEntries(
322 (options.plugins ?? []).map((plugin) => [
323 plugin.key,
324 plugin.extendExecutor?.({
325 executor,
326 host,
327 }) ?? {},
328 ]),
329 );
330
331 return Object.assign(
332 executor,
333 extensions,
334 ) as ExecutorEffect & ExecutorSdkPluginExtensions<TPlugins>;
335 },
380 );336 );
337};
packages/platform/sdk/src/executor.ts+116−36
@@ -4,8 +4,11 @@ import { · 3 unmodified lines
4 createExecutorEffect,4 createExecutorEffect,
5 type CreateExecutorEffectOptions,5 type CreateExecutorEffectOptions,
6 type ExecutorEffect,6 type ExecutorEffect,
7 type ExecutorSourceInput,
8} from "./executor-effect";7} from "./executor-effect";
8import type {
9 ExecutorSdkPlugin,
10 ExecutorSdkPluginExtensions,
11} from "./plugins";
9 12
10type Promiseify<T> = T extends Effect.Effect<infer A, any, any>13type Promiseify<T> = T extends Effect.Effect<infer A, any, any>
11 ? Promise<A>14 ? Promise<A>
@@ -17,17 +20,56 @@ type Promiseify<T> = T extends Effect.Effect<infer A, any, any> · 5 unmodified lines
17 ? { [Key in keyof T]: Promiseify<T[Key]> }20 ? { [Key in keyof T]: Promiseify<T[Key]> }
18 : T;21 : T;
19 22
20export type Executor = Omit<Promiseify<ExecutorEffect>, "runtime">;23export type Executor<
21export type CreateExecutorOptions = CreateExecutorEffectOptions;24 TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
22export type {25> = Omit<
23 ExecutorSourceInput,26 Promiseify<ExecutorEffect & ExecutorSdkPluginExtensions<TPlugins>>,
27 "runtime"
28>;
29export type CreateExecutorOptions<
30 TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
31> = CreateExecutorEffectOptions & {
32 plugins?: TPlugins;
24};33};
25 34
26const toPromiseExecutor = (executor: ExecutorEffect): Executor => {35const toPromiseExecutor = <
27 const run = <A, E>(effect: Effect.Effect<A, E, never>) =>36 TPlugins extends readonly ExecutorSdkPlugin<any, any>[],
28 Effect.runPromise(effect);37>(
38 executor: ExecutorEffect & ExecutorSdkPluginExtensions<TPlugins>,
39): Executor<TPlugins> => {
40 const run = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
41 Effect.runPromise(effect as Effect.Effect<A, E, never>);
42 const promiseifyValue = (value: unknown): unknown => {
43 if (Effect.isEffect(value)) {
44 return run(value);
45 }
29 46
30 return {47 if (value instanceof Promise) {
48 return value;
49 }
50
51 if (typeof value === "function") {
52 return (...args: Array<unknown>) =>
53 promiseifyValue(value(...args));
54 }
55
56 if (Array.isArray(value)) {
57 return value.map(promiseifyValue);
58 }
59
60 if (value !== null && typeof value === "object") {
61 return Object.fromEntries(
62 Object.entries(value).map(([key, nestedValue]) => [
63 key,
64 promiseifyValue(nestedValue),
65 ]),
66 );
67 }
68
69 return value;
70 };
71
72 const base = {
31 installation: executor.installation,73 installation: executor.installation,
32 scopeId: executor.scopeId,74 scopeId: executor.scopeId,
33 actorScopeId: executor.actorScopeId,75 actorScopeId: executor.actorScopeId,
@@ -36,49 +78,87 @@ const toPromiseExecutor = (executor: ExecutorEffect): Executor => { · 2 unmodified lines
36 local: {78 local: {
37 installation: () => run(executor.local.installation()),79 installation: () => run(executor.local.installation()),
38 config: () => run(executor.local.config()),80 config: () => run(executor.local.config()),
39 credentials: {
40 get: (input) => run(executor.local.credentials.get(input)),
41 submit: (input) => run(executor.local.credentials.submit(input)),
42 complete: (input) => run(executor.local.credentials.complete(input)),
43 },
44 },81 },
45 secrets: {82 secrets: {
46 list: () => run(executor.secrets.list()),83 list: () => run(executor.secrets.list()),
47 create: (payload) => run(executor.secrets.create(payload)),84 create: (payload: Parameters<typeof executor.secrets.create>[0]) =>
48 update: (input) => run(executor.secrets.update(input)),85 run(executor.secrets.create(payload)),
49 remove: (secretId) => run(executor.secrets.remove(secretId)),86 update: (input: Parameters<typeof executor.secrets.update>[0]) =>
87 run(executor.secrets.update(input)),
88 remove: (secretId: Parameters<typeof executor.secrets.remove>[0]) =>
89 run(executor.secrets.remove(secretId)),
50 },90 },
51 policies: {91 policies: {
52 list: () => run(executor.policies.list()),92 list: () => run(executor.policies.list()),
53 create: (payload) => run(executor.policies.create(payload)),93 create: (payload: Parameters<typeof executor.policies.create>[0]) =>
54 get: (policyId) => run(executor.policies.get(policyId)),94 run(executor.policies.create(payload)),
55 update: (policyId, payload) =>95 get: (policyId: Parameters<typeof executor.policies.get>[0]) =>
96 run(executor.policies.get(policyId)),
97 update: (
98 policyId: Parameters<typeof executor.policies.update>[0],
99 payload: Parameters<typeof executor.policies.update>[1],
100 ) =>
56 run(executor.policies.update(policyId, payload)),101 run(executor.policies.update(policyId, payload)),
57 remove: (policyId) => run(executor.policies.remove(policyId)),102 remove: (policyId: Parameters<typeof executor.policies.remove>[0]) =>
103 run(executor.policies.remove(policyId)),
58 },104 },
59 sources: {105 sources: {
60 add: (input, options) => run(executor.sources.add(input, options)),
61 list: () => run(executor.sources.list()),106 list: () => run(executor.sources.list()),
62 create: (payload) => run(executor.sources.create(payload)),107 get: (sourceId: Parameters<typeof executor.sources.get>[0]) =>
63 get: (sourceId) => run(executor.sources.get(sourceId)),108 run(executor.sources.get(sourceId)),
64 update: (sourceId, payload) =>109 remove: (sourceId: Parameters<typeof executor.sources.remove>[0]) =>
65 run(executor.sources.update(sourceId, payload)),110 run(executor.sources.remove(sourceId)),
66 remove: (sourceId) => run(executor.sources.remove(sourceId)),
67 inspection: {111 inspection: {
68 get: (sourceId) => run(executor.sources.inspection.get(sourceId)),112 get: (sourceId: Parameters<typeof executor.sources.inspection.get>[0]) =>
69 tool: (input) => run(executor.sources.inspection.tool(input)),113 run(executor.sources.inspection.get(sourceId)),
70 discover: (input) => run(executor.sources.inspection.discover(input)),114 tool: (input: Parameters<typeof executor.sources.inspection.tool>[0]) =>
115 run(executor.sources.inspection.tool(input)),
116 discover: (
117 input: Parameters<typeof executor.sources.inspection.discover>[0],
118 ) => run(executor.sources.inspection.discover(input)),
71 },119 },
72 },120 },
73 executions: {121 executions: {
74 create: (payload) => run(executor.executions.create(payload)),122 create: (payload: Parameters<typeof executor.executions.create>[0]) =>
75 get: (executionId) => run(executor.executions.get(executionId)),123 run(executor.executions.create(payload)),
76 resume: (executionId, payload) =>124 get: (executionId: Parameters<typeof executor.executions.get>[0]) =>
125 run(executor.executions.get(executionId)),
126 resume: (
127 executionId: Parameters<typeof executor.executions.resume>[0],
128 payload: Parameters<typeof executor.executions.resume>[1],
129 ) =>
77 run(executor.executions.resume(executionId, payload)),130 run(executor.executions.resume(executionId, payload)),
78 },131 },
79 };132 };
133
134 const coreKeys = new Set([
135 "runtime",
136 "installation",
137 "scopeId",
138 "actorScopeId",
139 "resolutionScopeIds",
140 "close",
141 "local",
142 "secrets",
143 "policies",
144 "sources",
145 "executions",
146 ]);
147
148 const pluginExtensions = Object.fromEntries(
149 Object.entries(executor)
150 .filter(([key]) => !coreKeys.has(key))
151 .map(([key, value]) => [key, promiseifyValue(value)]),
152 );
153
154 return Object.assign(base, pluginExtensions) as unknown as Executor<TPlugins>;
80};155};
81 156
82export const createExecutor = async (157export const createExecutor = async <
83 options: CreateExecutorOptions,158 const TPlugins extends readonly ExecutorSdkPlugin<any, any>[] = [],
84): Promise<Executor> => toPromiseExecutor(await Effect.runPromise(createExecutorEffect(options)));159>(
160 options: CreateExecutorOptions<TPlugins>,
161): Promise<Executor<TPlugins>> =>
162 toPromiseExecutor(
163 await Effect.runPromise(createExecutorEffect(options)),
164 );
packages/platform/sdk/src/index.ts+1−0
@@ -1,2 +1,3 @@
1export * from "./backend";1export * from "./backend";
2export * from "./executor";2export * from "./executor";
3export * from "./plugins";
packages/platform/sdk/src/local/operations.ts+2−382
@@ -1,200 +1,16 @@
1import type {
2 Execution,
3 ExecutionInteraction,
4 Source,
5 ScopeId,
6} from "../schema";
7import {
8 SourceIdSchema,
9 ScopeIdSchema,
10} from "../schema";
11import * as Effect from "effect/Effect";1import * as Effect from "effect/Effect";
12import * as Option from "effect/Option";
13 2
14import {
15 submitExecutionInteractionResponse,
16} from "../runtime/execution/service";
17import {
18 LiveExecutionManagerService,
19} from "../runtime/execution/live";
20import {
21 operationErrors,
22} from "../runtime/policy/operation-errors";
23import {3import {
24 InstallationStore,4 InstallationStore,
25} from "../runtime/scope/storage";5} from "../runtime/scope/storage";
26import {6import {
27 RuntimeSourceAuthServiceTag,7 operationErrors,
28} from "../runtime/sources/source-auth-service";8} from "../runtime/policy/operation-errors";
29import {
30 createSourceCredentialSelectionBearerContent,
31 createSourceCredentialSelectionNoneContent,
32} from "../runtime/sources/source-credential-interactions";
33import {
34 ExecutorStateStore,
35} from "../runtime/executor-state-store";
36 9
37const localOps = {10const localOps = {
38 installation: operationErrors("local.installation.get"),11 installation: operationErrors("local.installation.get"),
39 sourceCredentialComplete: operationErrors("sources.credentials.complete"),
40 sourceCredentialPage: operationErrors("sources.credentials.page"),
41 sourceCredentialSubmit: operationErrors("sources.credentials.submit"),
42} as const;12} as const;
43 13
44type SourceCredentialInteraction = {
45 interactionId: ExecutionInteraction["id"];
46 executionId: Execution["id"];
47 status: ExecutionInteraction["status"];
48 message: string;
49 scopeId: ScopeId;
50 sourceId: Source["id"];
51 sourceLabel: string;
52 endpoint: string;
53};
54
55const isRecord = (value: unknown): value is Record<string, unknown> =>
56 typeof value === "object" && value !== null;
57
58const asString = (value: unknown): string | null =>
59 typeof value === "string" ? value : null;
60
61const trimOrNull = (value: string | null | undefined): string | null => {
62 if (value === null || value === undefined) {
63 return null;
64 }
65
66 const trimmed = value.trim();
67 return trimmed.length > 0 ? trimmed : null;
68};
69
70export const decodeSourceCredentialInteraction = (
71 interaction: ExecutionInteraction,
72): Omit<SourceCredentialInteraction, "sourceLabel" | "endpoint"> | null => {
73 try {
74 if (
75 interaction.purpose !== "source_connect_oauth2" &&
76 interaction.purpose !== "source_connect_secret" &&
77 interaction.purpose !== "elicitation"
78 ) {
79 return null;
80 }
81
82 const payload = JSON.parse(interaction.payloadJson) as unknown;
83 if (!isRecord(payload)) {
84 return null;
85 }
86
87 const args = payload.args;
88 const elicitation = payload.elicitation;
89 if (!isRecord(args) || !isRecord(elicitation)) {
90 return null;
91 }
92
93 if (payload.path !== "executor.sources.add") {
94 return null;
95 }
96 const effectivePurpose =
97 interaction.purpose === "elicitation"
98 ? elicitation.mode === "url"
99 ? "source_connect_oauth2"
100 : "source_connect_secret"
101 : interaction.purpose;
102 if (
103 effectivePurpose === "source_connect_oauth2" &&
104 elicitation.mode !== "url"
105 ) {
106 return null;
107 }
108 if (
109 effectivePurpose === "source_connect_secret" &&
110 elicitation.mode !== "form"
111 ) {
112 return null;
113 }
114
115 const scopeId = trimOrNull(asString(args.scopeId));
116 const sourceId = trimOrNull(asString(args.sourceId));
117 const message = trimOrNull(asString(elicitation.message));
118 if (scopeId === null || sourceId === null || message === null) {
119 return null;
120 }
121
122 return {
123 interactionId: interaction.id,
124 executionId: interaction.executionId,
125 status: interaction.status,
126 message,
127 scopeId: ScopeIdSchema.make(scopeId),
128 sourceId: SourceIdSchema.make(sourceId),
129 };
130 } catch {
131 return null;
132 }
133};
134
135const loadSourceCredentialInteraction = (input: {
136 scopeId: ScopeId;
137 sourceId: Source["id"];
138 interactionId: ExecutionInteraction["id"];
139 operation:
140 | typeof localOps.sourceCredentialPage
141 | typeof localOps.sourceCredentialSubmit;
142}) =>
143 Effect.gen(function* () {
144 const store = yield* ExecutorStateStore;
145 const sourceAuthService = yield* RuntimeSourceAuthServiceTag;
146
147 const stored = yield* store.executionInteractions
148 .getById(input.interactionId)
149 .pipe(
150 Effect.mapError((error) =>
151 input.operation.unknownStorage(
152 error,
153 `Failed loading execution interaction ${input.interactionId}`,
154 ),
155 ),
156 );
157
158 if (Option.isNone(stored)) {
159 return yield* input.operation.notFound(
160 "Source credential request not found",
161 `interactionId=${input.interactionId}`,
162 );
163 }
164
165 const decoded = decodeSourceCredentialInteraction(stored.value);
166 if (
167 decoded === null ||
168 decoded.scopeId !== input.scopeId ||
169 decoded.sourceId !== input.sourceId
170 ) {
171 return yield* input.operation.notFound(
172 "Source credential request not found",
173 `scopeId=${input.scopeId} sourceId=${input.sourceId} interactionId=${input.interactionId}`,
174 );
175 }
176
177 const source = yield* sourceAuthService
178 .getSourceById({
179 scopeId: input.scopeId,
180 sourceId: input.sourceId,
181 })
182 .pipe(
183 Effect.mapError((error) =>
184 input.operation.unknownStorage(
185 error,
186 `Failed loading source ${input.sourceId}`,
187 ),
188 ),
189 );
190
191 return {
192 ...decoded,
193 sourceLabel: source.name,
194 endpoint: source.endpoint,
195 } satisfies SourceCredentialInteraction;
196 });
197
198export const getLocalInstallation = () =>14export const getLocalInstallation = () =>
199 Effect.gen(function* () {15 Effect.gen(function* () {
200 const installationStore = yield* InstallationStore;16 const installationStore = yield* InstallationStore;
@@ -207,199 +23,3 @@ export const getLocalInstallation = () => · 6 unmodified lines
207 ),23 ),
208 );24 );
209 });25 });
210
211export const getSourceCredentialInteraction = (input: {
212 scopeId: ScopeId;
213 sourceId: Source["id"];
214 interactionId: ExecutionInteraction["id"];
215}) =>
216 loadSourceCredentialInteraction({
217 ...input,
218 operation: localOps.sourceCredentialPage,
219 });
220
221export const submitSourceCredentialInteraction = (input: {
222 scopeId: ScopeId;
223 sourceId: Source["id"];
224 interactionId: ExecutionInteraction["id"];
225 action: "submit" | "continue" | "cancel";
226 token?: string | null;
227}) =>
228 Effect.gen(function* () {
229 const interaction = yield* loadSourceCredentialInteraction({
230 scopeId: input.scopeId,
231 sourceId: input.sourceId,
232 interactionId: input.interactionId,
233 operation: localOps.sourceCredentialSubmit,
234 });
235
236 if (interaction.status !== "pending") {
237 return yield* localOps.sourceCredentialSubmit.badRequest(
238 "Source credential request is no longer active",
239 `interactionId=${interaction.interactionId} status=${interaction.status}`,
240 );
241 }
242
243 const liveExecutionManager = yield* LiveExecutionManagerService;
244
245 if (input.action === "cancel") {
246 const resumed = yield* liveExecutionManager.resolveInteraction({
247 executionId: interaction.executionId,
248 response: {
249 action: "cancel",
250 },
251 });
252
253 if (!resumed) {
254 const persisted = yield* submitExecutionInteractionResponse({
255 executionId: interaction.executionId,
256 response: {
257 action: "cancel",
258 },
259 }).pipe(
260 Effect.mapError((error) =>
261 localOps.sourceCredentialSubmit.unknownStorage(
262 error,
263 `Failed resuming execution for interaction ${interaction.interactionId}`,
264 ),
265 ),
266 );
267
268 if (!persisted) {
269 return yield* localOps.sourceCredentialSubmit.badRequest(
270 "Source credential request is no longer resumable",
271 `interactionId=${interaction.interactionId}`,
272 );
273 }
274 }
275
276 return {
277 kind: "cancelled" as const,
278 sourceLabel: interaction.sourceLabel,
279 endpoint: interaction.endpoint,
280 };
281 }
282
283 if (input.action === "continue") {
284 const resumed = yield* liveExecutionManager.resolveInteraction({
285 executionId: interaction.executionId,
286 response: {
287 action: "accept",
288 content: createSourceCredentialSelectionNoneContent(),
289 },
290 });
291
292 if (!resumed) {
293 const persisted = yield* submitExecutionInteractionResponse({
294 executionId: interaction.executionId,
295 response: {
296 action: "accept",
297 content: createSourceCredentialSelectionNoneContent(),
298 },
299 }).pipe(
300 Effect.mapError((error) =>
301 localOps.sourceCredentialSubmit.unknownStorage(
302 error,
303 `Failed resuming execution for interaction ${interaction.interactionId}`,
304 ),
305 ),
306 );
307
308 if (!persisted) {
309 return yield* localOps.sourceCredentialSubmit.badRequest(
310 "Source credential request is no longer resumable",
311 `interactionId=${interaction.interactionId}`,
312 );
313 }
314 }
315
316 return {
317 kind: "continued" as const,
318 sourceLabel: interaction.sourceLabel,
319 endpoint: interaction.endpoint,
320 };
321 }
322
323 const token = trimOrNull(input.token);
324 if (token === null) {
325 return yield* localOps.sourceCredentialSubmit.badRequest(
326 "Missing token",
327 `interactionId=${interaction.interactionId}`,
328 );
329 }
330
331 const sourceAuthService = yield* RuntimeSourceAuthServiceTag;
332 const tokenRef = yield* sourceAuthService
333 .storeSecretMaterial({
334 purpose: "auth_material",
335 value: token,
336 })
337 .pipe(
338 Effect.mapError((error) =>
339 localOps.sourceCredentialSubmit.unknownStorage(
340 error,
341 `Failed storing credential material for interaction ${interaction.interactionId}`,
342 ),
343 ),
344 );
345 const resumed = yield* liveExecutionManager.resolveInteraction({
346 executionId: interaction.executionId,
347 response: {
348 action: "accept",
349 content: createSourceCredentialSelectionBearerContent(tokenRef),
350 },
351 });
352
353 if (!resumed) {
354 const persisted = yield* submitExecutionInteractionResponse({
355 executionId: interaction.executionId,
356 response: {
357 action: "accept",
358 content: createSourceCredentialSelectionBearerContent(tokenRef),
359 },
360 }).pipe(
361 Effect.mapError((error) =>
362 localOps.sourceCredentialSubmit.unknownStorage(
363 error,
364 `Failed resuming execution for interaction ${interaction.interactionId}`,
365 ),
366 ),
367 );
368
369 if (!persisted) {
370 return yield* localOps.sourceCredentialSubmit.badRequest(
371 "Source credential request is no longer resumable",
372 `interactionId=${interaction.interactionId}`,
373 );
374 }
375 }
376
377 return {
378 kind: "stored" as const,
379 sourceLabel: interaction.sourceLabel,
380 endpoint: interaction.endpoint,
381 };
382 });
383
384export const completeSourceCredentialSetup = (input: {
385 scopeId: ScopeId;
386 sourceId: Source["id"];
387 state: string;
388 code?: string | null;
389 error?: string | null;
390 errorDescription?: string | null;
391}) =>
392 Effect.gen(function* () {
393 const sourceAuthService = yield* RuntimeSourceAuthServiceTag;
394
395 return yield* sourceAuthService
396 .completeSourceCredentialSetup(input)
397 .pipe(
398 Effect.mapError((error) =>
399 localOps.sourceCredentialComplete.unknownStorage(
400 error,
401 "Failed completing source credential setup",
402 ),
403 ),
404 );
405 });
packages/platform/sdk/src/plugins.ts+98−0
@@ -0,0 +1,98 @@
1import type { SourceCatalogSyncResult } from "@executor/source-core";
2import type {
3 Source,
4 SourceCatalogKind,
5} from "@executor/source-core";
6import type { ExecutorEffect } from "./executor-effect";
7import type { Source as ExecutorSource } from "./schema";
8import type {
9 SourceInvokeInput,
10 SourceInvokeResult,
11 SourceSyncInput,
12} from "@executor/source-core";
13import * as Effect from "effect/Effect";
14
15export type ExecutorSdkPlugin<
16 TKey extends string = string,
17 TExtension extends object = {},
18> = {
19 key: TKey;
20 sources?: readonly SourcePluginRuntime[];
21 extendExecutor?: (input: {
22 executor: ExecutorEffect & Record<string, unknown>;
23 host: ExecutorSdkPluginHost;
24 }) => TExtension;
25};
26
27export type ExecutorSdkPluginHost = {
28 sources: {
29 create: (input: {
30 source: Omit<
31 ExecutorSource,
32 "id" | "scopeId" | "createdAt" | "updatedAt"
33 >;
34 }) => Effect.Effect<ExecutorSource, Error, never>;
35 get: (sourceId: ExecutorSource["id"]) => Effect.Effect<ExecutorSource, Error, never>;
36 save: (source: ExecutorSource) => Effect.Effect<ExecutorSource, Error, never>;
37 remove: (sourceId: ExecutorSource["id"]) => Effect.Effect<boolean, Error, never>;
38 };
39};
40
41export type ExecutorSdkPluginExtensions<
42 TPlugins extends readonly ExecutorSdkPlugin<any, any>[],
43> = {
44 [TPlugin in TPlugins[number] as TPlugin["key"]]:
45 TPlugin extends ExecutorSdkPlugin<any, infer TExtension>
46 ? TExtension
47 : never;
48};
49
50export type SourcePluginRuntime = {
51 kind: string;
52 displayName: string;
53 catalogKind: SourceCatalogKind;
54 catalogIdentity?: (input: {
55 source: Source;
56 }) => Record<string, unknown>;
57 getIrModel: (
58 input: SourceSyncInput,
59 ) => Effect.Effect<SourceCatalogSyncResult, Error, never>;
60 invoke: (
61 input: SourceInvokeInput,
62 ) => Effect.Effect<SourceInvokeResult, Error, never>;
63};
64
65export const registerExecutorSdkPlugins = (
66 plugins: readonly ExecutorSdkPlugin[],
67) => {
68 const sourcePlugins = new Map<string, SourcePluginRuntime>();
69
70 for (const plugin of plugins) {
71 for (const source of plugin.sources ?? []) {
72 sourcePlugins.set(source.kind, source);
73 }
74 }
75
76 const getSourcePlugin = (kind: string) => {
77 const definition = sourcePlugins.get(kind);
78 if (!definition) {
79 throw new Error(`Unsupported source plugin: ${kind}`);
80 }
81
82 return definition;
83 };
84
85 const getSourcePluginForSource = (source: Pick<Source, "kind">) =>
86 getSourcePlugin(source.kind);
87
88 return {
89 plugins,
90 sourcePlugins: [...sourcePlugins.values()],
91 getSourcePlugin,
92 getSourcePluginForSource,
93 sourcePluginCatalogKind: (kind: string): SourceCatalogKind =>
94 getSourcePlugin(kind).catalogKind,
95 isInternalSourcePluginKind: (kind: string): boolean =>
96 getSourcePlugin(kind).catalogKind === "internal",
97 };
98};
packages/platform/sdk/src/runtime/auth/auth-artifacts.ts+0−514
@@ -1,514 +0,0 @@
1import type {
2 OAuthClientProvider,
3} from "@modelcontextprotocol/sdk/client/auth.js";
4import {
5 type ScopeId,
6 AuthGrantSetJsonSchema,
7 McpOAuthAuthArtifactConfigJsonSchema,
8 McpOAuthAuthArtifactKind,
9 ProviderGrantRefAuthArtifactConfigJsonSchema,
10 ProviderGrantRefAuthArtifactKind,
11 RequestPlacementTemplatesJsonSchema,
12 RefreshableOAuth2AuthorizedUserAuthArtifactConfigJsonSchema,
13 RefreshableOAuth2AuthorizedUserAuthArtifactKind,
14 StaticBearerAuthArtifactConfigJsonSchema,
15 StaticBearerAuthArtifactKind,
16 StaticOAuth2AuthArtifactConfigJsonSchema,
17 StaticOAuth2AuthArtifactKind,
18 StaticPlacementsAuthArtifactConfigJsonSchema,
19 StaticPlacementsAuthArtifactKind,
20 authArtifactSecretRefs,
21 decodeAuthGrantSet,
22 decodeBuiltInAuthArtifactConfig,
23 decodeMcpOAuthAuthArtifactConfig,
24 decodeProviderGrantRefAuthArtifactConfig,
25 type AuthArtifact,
26 type CredentialSlot,
27 type RequestPlacement,
28 type RequestPlacementPart,
29 type SecretRef,
30 type Source,
31 type SourceAuth,
32} from "#schema";
33import * as Either from "effect/Either";
34import * as Effect from "effect/Effect";
35import * as Schema from "effect/Schema";
36
37import type {
38 ResolveSecretMaterial,
39 SecretMaterialResolveContext,
40} from "../scope/secret-material-providers";
41import {
42 runtimeEffectError,
43} from "../effect-errors";
44
45export type ResolvedSourceAuthMaterial = {
46 placements: ReadonlyArray<RequestPlacement>;
47 headers: Readonly<Record<string, string>>;
48 queryParams: Readonly<Record<string, string>>;
49 cookies: Readonly<Record<string, string>>;
50 bodyValues: Readonly<Record<string, string>>;
51 expiresAt: number | null;
52 refreshAfter: number | null;
53 authProvider?: OAuthClientProvider | undefined;
54};
55
56const emptyResolvedAuthMaterial: ResolvedSourceAuthMaterial = {
57 placements: [],
58 headers: {},
59 queryParams: {},
60 cookies: {},
61 bodyValues: {},
62 expiresAt: null,
63 refreshAfter: null,
64 authProvider: undefined,
65};
66
67const encodeStaticBearerAuthArtifactConfig = Schema.encodeSync(
68 StaticBearerAuthArtifactConfigJsonSchema,
69);
70
71const encodeStaticOAuth2AuthArtifactConfig = Schema.encodeSync(
72 StaticOAuth2AuthArtifactConfigJsonSchema,
73);
74
75const encodeStaticPlacementsAuthArtifactConfig = Schema.encodeSync(
76 StaticPlacementsAuthArtifactConfigJsonSchema,
77);
78const encodeRefreshableOAuth2AuthorizedUserAuthArtifactConfig = Schema.encodeSync(
79 RefreshableOAuth2AuthorizedUserAuthArtifactConfigJsonSchema,
80);
81const encodeProviderGrantRefAuthArtifactConfig = Schema.encodeSync(
82 ProviderGrantRefAuthArtifactConfigJsonSchema,
83);
84const encodeMcpOAuthAuthArtifactConfig = Schema.encodeSync(
85 McpOAuthAuthArtifactConfigJsonSchema,
86);
87
88const decodeRequestPlacementTemplates = Schema.decodeUnknownEither(
89 RequestPlacementTemplatesJsonSchema,
90);
91const encodeAuthGrantSet = Schema.encodeSync(AuthGrantSetJsonSchema);
92
93const mergePlacement = (acc: {
94 headers: Record<string, string>;
95 queryParams: Record<string, string>;
96 cookies: Record<string, string>;
97 bodyValues: Record<string, string>;
98}, placement: RequestPlacement) => {
99 switch (placement.location) {
100 case "header":
101 acc.headers[placement.name] = placement.value;
102 break;
103 case "query":
104 acc.queryParams[placement.name] = placement.value;
105 break;
106 case "cookie":
107 acc.cookies[placement.name] = placement.value;
108 break;
109 case "body":
110 acc.bodyValues[placement.path] = placement.value;
111 break;
112 }
113};
114
115const summarizeResolvedPlacements = (placements: ReadonlyArray<RequestPlacement>, input: {
116 expiresAt?: number | null;
117 refreshAfter?: number | null;
118} = {}): ResolvedSourceAuthMaterial => {
119 const acc = {
120 headers: {} as Record<string, string>,
121 queryParams: {} as Record<string, string>,
122 cookies: {} as Record<string, string>,
123 bodyValues: {} as Record<string, string>,
124 };
125
126 for (const placement of placements) {
127 mergePlacement(acc, placement);
128 }
129
130 return {
131 placements,
132 headers: acc.headers,
133 queryParams: acc.queryParams,
134 cookies: acc.cookies,
135 bodyValues: acc.bodyValues,
136 expiresAt: input.expiresAt ?? null,
137 refreshAfter: input.refreshAfter ?? null,
138 };
139};
140
141const resolvePlacementParts = (input: {
142 parts: ReadonlyArray<RequestPlacementPart>;
143 resolveSecretMaterial: ResolveSecretMaterial;
144 context?: SecretMaterialResolveContext;
145}): Effect.Effect<string, Error, never> =>
146 Effect.map(
147 Effect.forEach(input.parts, (part) =>
148 part.kind === "literal"
149 ? Effect.succeed(part.value)
150 : input.resolveSecretMaterial({
151 ref: part.ref,
152 context: input.context,
153 }), { discard: false }),
154 (values) => values.join(""),
155 );
156
157export const authArtifactFromSourceAuth = (input: {
158 source: Source;
159 auth: SourceAuth;
160 slot: CredentialSlot;
161 actorScopeId?: ScopeId | null;
162 existingAuthArtifactId?: AuthArtifact["id"] | null;
163}): AuthArtifact | null => {
164 if (input.auth.kind === "none") {
165 return null;
166 }
167
168 const id = input.existingAuthArtifactId ?? `auth_art_${crypto.randomUUID()}` as AuthArtifact["id"];
169 const grantSetJson =
170 input.auth.kind === "oauth2_authorized_user"
171 ? encodeAuthGrantSetJson(input.auth.grantSet)
172 : null;
173
174 if (input.auth.kind === "bearer") {
175 return {
176 id,
177 scopeId: input.source.scopeId,
178 sourceId: input.source.id,
179 actorScopeId: input.actorScopeId ?? null,
180 slot: input.slot,
181 artifactKind: StaticBearerAuthArtifactKind,
182 configJson: encodeStaticBearerAuthArtifactConfig({
183 headerName: input.auth.headerName,
184 prefix: input.auth.prefix,
185 token: input.auth.token,
186 }),
187 grantSetJson,
188 createdAt: input.source.createdAt,
189 updatedAt: input.source.updatedAt,
190 };
191 }
192
193 if (input.auth.kind === "oauth2_authorized_user") {
194 return {
195 id,
196 scopeId: input.source.scopeId,
197 sourceId: input.source.id,
198 actorScopeId: input.actorScopeId ?? null,
199 slot: input.slot,
200 artifactKind: RefreshableOAuth2AuthorizedUserAuthArtifactKind,
201 configJson: encodeRefreshableOAuth2AuthorizedUserAuthArtifactConfig({
202 headerName: input.auth.headerName,
203 prefix: input.auth.prefix,
204 tokenEndpoint: input.auth.tokenEndpoint,
205 clientId: input.auth.clientId,
206 clientAuthentication: input.auth.clientAuthentication,
207 clientSecret: input.auth.clientSecret,
208 refreshToken: input.auth.refreshToken,
209 }),
210 grantSetJson,
211 createdAt: input.source.createdAt,
212 updatedAt: input.source.updatedAt,
213 };
214 }
215
216 if (input.auth.kind === "provider_grant_ref") {
217 return {
218 id,
219 scopeId: input.source.scopeId,
220 sourceId: input.source.id,
221 actorScopeId: input.actorScopeId ?? null,
222 slot: input.slot,
223 artifactKind: ProviderGrantRefAuthArtifactKind,
224 configJson: encodeProviderGrantRefAuthArtifactConfig({
225 grantId: input.auth.grantId,
226 providerKey: input.auth.providerKey,
227 requiredScopes: [...input.auth.requiredScopes],
228 headerName: input.auth.headerName,
229 prefix: input.auth.prefix,
230 }),
231 grantSetJson,
232 createdAt: input.source.createdAt,
233 updatedAt: input.source.updatedAt,
234 };
235 }
236
237 if (input.auth.kind === "mcp_oauth") {
238 return {
239 id,
240 scopeId: input.source.scopeId,
241 sourceId: input.source.id,
242 actorScopeId: input.actorScopeId ?? null,
243 slot: input.slot,
244 artifactKind: McpOAuthAuthArtifactKind,
245 configJson: encodeMcpOAuthAuthArtifactConfig({
246 redirectUri: input.auth.redirectUri,
247 accessToken: input.auth.accessToken,
248 refreshToken: input.auth.refreshToken,
249 tokenType: input.auth.tokenType,
250 expiresIn: input.auth.expiresIn,
251 scope: input.auth.scope,
252 resourceMetadataUrl: input.auth.resourceMetadataUrl,
253 authorizationServerUrl: input.auth.authorizationServerUrl,
254 resourceMetadataJson: input.auth.resourceMetadataJson,
255 authorizationServerMetadataJson: input.auth.authorizationServerMetadataJson,
256 clientInformationJson: input.auth.clientInformationJson,
257 }),
258 grantSetJson,
259 createdAt: input.source.createdAt,
260 updatedAt: input.source.updatedAt,
261 };
262 }
263
264 return {
265 id,
266 scopeId: input.source.scopeId,
267 sourceId: input.source.id,
268 actorScopeId: input.actorScopeId ?? null,
269 slot: input.slot,
270 artifactKind: StaticOAuth2AuthArtifactKind,
271 configJson: encodeStaticOAuth2AuthArtifactConfig({
272 headerName: input.auth.headerName,
273 prefix: input.auth.prefix,
274 accessToken: input.auth.accessToken,
275 refreshToken: input.auth.refreshToken,
276 }),
277 grantSetJson,
278 createdAt: input.source.createdAt,
279 updatedAt: input.source.updatedAt,
280 };
281};
282
283export const sourceAuthFromAuthArtifact = (artifact: AuthArtifact | null): SourceAuth => {
284 if (artifact === null) {
285 return { kind: "none" };
286 }
287
288 const providerGrantRefConfig = decodeProviderGrantRefAuthArtifactConfig(artifact);
289 if (providerGrantRefConfig !== null) {
290 return {
291 kind: "provider_grant_ref",
292 grantId: providerGrantRefConfig.grantId,
293 providerKey: providerGrantRefConfig.providerKey,
294 requiredScopes: [...providerGrantRefConfig.requiredScopes],
295 headerName: providerGrantRefConfig.headerName,
296 prefix: providerGrantRefConfig.prefix,
297 };
298 }
299
300 const mcpOAuthConfig = decodeMcpOAuthAuthArtifactConfig(artifact);
301 if (mcpOAuthConfig !== null) {
302 return {
303 kind: "mcp_oauth",
304 redirectUri: mcpOAuthConfig.redirectUri,
305 accessToken: mcpOAuthConfig.accessToken,
306 refreshToken: mcpOAuthConfig.refreshToken,
307 tokenType: mcpOAuthConfig.tokenType,
308 expiresIn: mcpOAuthConfig.expiresIn,
309 scope: mcpOAuthConfig.scope,
310 resourceMetadataUrl: mcpOAuthConfig.resourceMetadataUrl,
311 authorizationServerUrl: mcpOAuthConfig.authorizationServerUrl,
312 resourceMetadataJson: mcpOAuthConfig.resourceMetadataJson,
313 authorizationServerMetadataJson: mcpOAuthConfig.authorizationServerMetadataJson,
314 clientInformationJson: mcpOAuthConfig.clientInformationJson,
315 };
316 }
317
318 const decoded = decodeBuiltInAuthArtifactConfig(artifact);
319 if (decoded === null) {
320 return { kind: "none" };
321 }
322
323 switch (decoded.artifactKind) {
324 case StaticBearerAuthArtifactKind:
325 return {
326 kind: "bearer",
327 headerName: decoded.config.headerName,
328 prefix: decoded.config.prefix,
329 token: decoded.config.token,
330 };
331 case StaticOAuth2AuthArtifactKind:
332 return {
333 kind: "oauth2",
334 headerName: decoded.config.headerName,
335 prefix: decoded.config.prefix,
336 accessToken: decoded.config.accessToken,
337 refreshToken: decoded.config.refreshToken,
338 };
339 case StaticPlacementsAuthArtifactKind:
340 return { kind: "none" };
341 case RefreshableOAuth2AuthorizedUserAuthArtifactKind:
342 return {
343 kind: "oauth2_authorized_user",
344 headerName: decoded.config.headerName,
345 prefix: decoded.config.prefix,
346 tokenEndpoint: decoded.config.tokenEndpoint,
347 clientId: decoded.config.clientId,
348 clientAuthentication: decoded.config.clientAuthentication,
349 clientSecret: decoded.config.clientSecret,
350 refreshToken: decoded.config.refreshToken,
351 grantSet: decodeAuthGrantSet(artifact.grantSetJson),
352 };
353 }
354};
355
356export const authArtifactGrantSet = (artifact: Pick<AuthArtifact, "grantSetJson">): ReadonlyArray<string> | null =>
357 decodeAuthGrantSet(artifact.grantSetJson);
358
359export const authArtifactSecretMaterialRefs = (
360 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
361): ReadonlyArray<SecretRef> => authArtifactSecretRefs(artifact);
362
363export const encodeStaticPlacementsArtifactConfig = (placements: ReadonlyArray<{
364 location: "header" | "query" | "cookie" | "body";
365 name?: string;
366 path?: string;
367 parts: ReadonlyArray<RequestPlacementPart>;
368}>): string =>
369 encodeStaticPlacementsAuthArtifactConfig({
370 placements: placements.map((placement) =>
371 placement.location === "body"
372 ? {
373 location: "body" as const,
374 path: placement.path ?? "",
375 parts: [...placement.parts],
376 }
377 : {
378 location: placement.location,
379 name: placement.name ?? "",
380 parts: [...placement.parts],
381 }),
382 });
383
384export const encodeAuthGrantSetJson = (grantSet: ReadonlyArray<string> | null): string | null =>
385 grantSet === null ? null : encodeAuthGrantSet([...grantSet]);
386
387export const resolveAuthArtifactMaterial = (input: {
388 artifact: AuthArtifact | null;
389 lease?: {
390 placementsTemplateJson: string;
391 expiresAt: number | null;
392 refreshAfter: number | null;
393 } | null;
394 resolveSecretMaterial: ResolveSecretMaterial;
395 context?: SecretMaterialResolveContext;
396}): Effect.Effect<ResolvedSourceAuthMaterial, Error, never> =>
397 Effect.gen(function* () {
398 if (input.artifact === null) {
399 return emptyResolvedAuthMaterial;
400 }
401
402 const decoded = decodeBuiltInAuthArtifactConfig(input.artifact);
403 if (decoded !== null) {
404 switch (decoded.artifactKind) {
405 case StaticBearerAuthArtifactKind: {
406 const token = yield* input.resolveSecretMaterial({
407 ref: decoded.config.token,
408 context: input.context,
409 });
410
411 return summarizeResolvedPlacements([
412 {
413 location: "header",
414 name: decoded.config.headerName,
415 value: `${decoded.config.prefix}${token}`,
416 },
417 ]);
418 }
419 case StaticOAuth2AuthArtifactKind: {
420 const accessToken = yield* input.resolveSecretMaterial({
421 ref: decoded.config.accessToken,
422 context: input.context,
423 });
424
425 return summarizeResolvedPlacements([
426 {
427 location: "header",
428 name: decoded.config.headerName,
429 value: `${decoded.config.prefix}${accessToken}`,
430 },
431 ]);
432 }
433 case StaticPlacementsAuthArtifactKind: {
434 const placements = yield* Effect.forEach(decoded.config.placements, (placement) =>
435 Effect.map(
436 resolvePlacementParts({
437 parts: placement.parts,
438 resolveSecretMaterial: input.resolveSecretMaterial,
439 context: input.context,
440 }),
441 (value): RequestPlacement =>
442 placement.location === "body"
443 ? {
444 location: "body",
445 path: placement.path,
446 value,
447 }
448 : {
449 location: placement.location,
450 name: placement.name,
451 value,
452 },
453 ), { discard: false });
454
455 return summarizeResolvedPlacements(placements);
456 }
457 case RefreshableOAuth2AuthorizedUserAuthArtifactKind:
458 break;
459 }
460 }
461
462 const providerGrantRefConfig = decodeProviderGrantRefAuthArtifactConfig(input.artifact);
463 if (providerGrantRefConfig !== null) {
464 if (input.lease === null || input.lease === undefined) {
465 return yield* runtimeEffectError("auth/auth-artifacts", `Provider grant auth artifact ${input.artifact.id} requires a lease`);
466 }
467 }
468
469 const mcpOAuthConfig = decodeMcpOAuthAuthArtifactConfig(input.artifact);
470 if (mcpOAuthConfig !== null) {
471 return {
472 ...emptyResolvedAuthMaterial,
473 };
474 }
475
476 if (input.lease === null || input.lease === undefined) {
477 return yield* runtimeEffectError("auth/auth-artifacts", `Unsupported auth artifact kind: ${input.artifact.artifactKind}`);
478 }
479
480 const templatesEither = decodeRequestPlacementTemplates(
481 input.lease.placementsTemplateJson,
482 );
483 if (Either.isLeft(templatesEither)) {
484 return yield* runtimeEffectError("auth/auth-artifacts",
485 `Invalid auth lease placements for artifact ${input.artifact.id}`,
486 );
487 }
488
489 const placements = yield* Effect.forEach(templatesEither.right, (placement) =>
490 Effect.map(
491 resolvePlacementParts({
492 parts: placement.parts,
493 resolveSecretMaterial: input.resolveSecretMaterial,
494 context: input.context,
495 }),
496 (value): RequestPlacement =>
497 placement.location === "body"
498 ? {
499 location: "body",
500 path: placement.path,
501 value,
502 }
503 : {
504 location: placement.location,
505 name: placement.name,
506 value,
507 },
508 ), { discard: false });
509
510 return summarizeResolvedPlacements(placements, {
511 expiresAt: input.lease.expiresAt,
512 refreshAfter: input.lease.refreshAfter,
513 });
514 });
packages/platform/sdk/src/runtime/auth/auth-leases.ts+0−507
@@ -1,507 +0,0 @@
1import type {
2 AuthArtifact,
3 AuthLease,
4 ProviderAuthGrant,
5 RequestPlacementTemplate,
6 SecretRef,
7} from "#schema";
8import {
9 AuthLeaseIdSchema,
10 decodeMcpOAuthAuthArtifactConfig,
11 decodeProviderGrantRefAuthArtifactConfig,
12 RefreshableOAuth2AuthorizedUserAuthArtifactKind,
13 authLeaseSecretRefs,
14 decodeBuiltInAuthArtifactConfig,
15} from "#schema";
16import * as Effect from "effect/Effect";
17import * as Option from "effect/Option";
18
19import type {
20 ExecutorStateStoreShape,
21} from "../executor-state-store";
22import {
23 resolveAuthArtifactMaterial,
24 type ResolvedSourceAuthMaterial,
25} from "./auth-artifacts";
26import {
27 type DeleteSecretMaterial,
28 type ResolveSecretMaterial,
29 type SecretMaterialResolveContext,
30 type StoreSecretMaterial,
31} from "../scope/secret-material-providers";
32import {
33 refreshOAuth2AccessToken,
34} from "./oauth2-pkce";
35import type {
36 OAuth2TokenResponse,
37} from "./oauth2-pkce";
38import {
39 createPersistedMcpAuthProvider,
40} from "./mcp-auth-provider";
41import {
42 runtimeEffectError,
43} from "../effect-errors";
44
45const LEASE_REFRESH_SKEW_MS = 60_000;
46
47const encodeLeasePlacementTemplatesJson = (templates: ReadonlyArray<RequestPlacementTemplate>): string =>
48 JSON.stringify(templates);
49
50const secretRefKey = (ref: SecretRef): string =>
51 `${ref.providerId}:${ref.handle}`;
52
53const cleanupAuthLeaseSecretRefs = (
54 executorState: ExecutorStateStoreShape,
55 input: {
56 previous: Pick<AuthLease, "placementsTemplateJson"> | null;
57 next: Pick<AuthLease, "placementsTemplateJson"> | null;
58 },
59 deleteSecretMaterial: DeleteSecretMaterial,
60) =>
61 Effect.gen(function* () {
62 if (input.previous === null) {
63 return;
64 }
65 const nextRefKeys = new Set(
66 (input.next === null ? [] : authLeaseSecretRefs(input.next)).map(secretRefKey),
67 );
68 const refsToDelete = authLeaseSecretRefs(input.previous).filter(
69 (ref) => !nextRefKeys.has(secretRefKey(ref)),
70 );
71
72 yield* Effect.forEach(
73 refsToDelete,
74 (ref) => Effect.either(deleteSecretMaterial(ref)),
75 { discard: true },
76 );
77 });
78
79const leaseIsFresh = (lease: AuthLease | null, now: number): boolean => {
80 if (lease === null) {
81 return false;
82 }
83
84 if (lease.refreshAfter !== null && now >= lease.refreshAfter) {
85 return false;
86 }
87
88 if (lease.expiresAt !== null && now >= lease.expiresAt - LEASE_REFRESH_SKEW_MS) {
89 return false;
90 }
91
92 return true;
93};
94
95const refreshRefreshableOauth2AuthorizedUserArtifact = (input: {
96 executorState: ExecutorStateStoreShape;
97 artifact: AuthArtifact;
98 lease: AuthLease | null;
99 resolveSecretMaterial: ResolveSecretMaterial;
100 storeSecretMaterial: StoreSecretMaterial;
101 deleteSecretMaterial: DeleteSecretMaterial;
102 context?: SecretMaterialResolveContext;
103}): Effect.Effect<AuthLease, Error, never> =>
104 Effect.gen(function* () {
105 const decoded = decodeBuiltInAuthArtifactConfig(input.artifact);
106 if (
107 decoded === null
108 || decoded.artifactKind !== RefreshableOAuth2AuthorizedUserAuthArtifactKind
109 ) {
110 return yield* runtimeEffectError("auth/auth-leases", `Unsupported auth artifact kind: ${input.artifact.artifactKind}`);
111 }
112
113 const refreshToken = yield* input.resolveSecretMaterial({
114 ref: decoded.config.refreshToken,
115 context: input.context,
116 });
117 let clientSecret: string | null = null;
118 if (decoded.config.clientSecret) {
119 clientSecret = yield* input.resolveSecretMaterial({
120 ref: decoded.config.clientSecret,
121 context: input.context,
122 });
123 }
124
125 const tokenResponse = yield* refreshOAuth2AccessToken({
126 tokenEndpoint: decoded.config.tokenEndpoint,
127 clientId: decoded.config.clientId,
128 clientAuthentication: decoded.config.clientAuthentication,
129 clientSecret,
130 refreshToken,
131 });
132
133 const accessTokenRef = yield* input.storeSecretMaterial({
134 purpose: "oauth_access_token",
135 value: tokenResponse.access_token,
136 name: `${decoded.config.clientId} Access Token`,
137 });
138 const now = Date.now();
139 const expiresInMs =
140 typeof tokenResponse.expires_in === "number" && Number.isFinite(tokenResponse.expires_in)
141 ? Math.max(0, tokenResponse.expires_in * 1000)
142 : null;
143 const expiresAt = expiresInMs === null ? null : now + expiresInMs;
144 const refreshAfter =
145 expiresAt === null ? null : Math.max(now, expiresAt - LEASE_REFRESH_SKEW_MS);
146
147 const nextLease: AuthLease = {
148 id: input.lease?.id ?? AuthLeaseIdSchema.make(`auth_lease_${crypto.randomUUID()}`),
149 authArtifactId: input.artifact.id,
150 scopeId: input.artifact.scopeId,
151 sourceId: input.artifact.sourceId,
152 actorScopeId: input.artifact.actorScopeId,
153 slot: input.artifact.slot,
154 placementsTemplateJson: encodeLeasePlacementTemplatesJson([
155 {
156 location: "header",
157 name: decoded.config.headerName,
158 parts: [
159 {
160 kind: "literal",
161 value: decoded.config.prefix,
162 },
163 {
164 kind: "secret_ref",
165 ref: accessTokenRef,
166 },
167 ],
168 },
169 ]),
170 expiresAt,
171 refreshAfter,
172 createdAt: input.lease?.createdAt ?? now,
173 updatedAt: now,
174 };
175
176 yield* input.executorState.authLeases.upsert(nextLease);
177 yield* cleanupAuthLeaseSecretRefs(input.executorState, {
178 previous: input.lease,
179 next: nextLease,
180 }, input.deleteSecretMaterial);
181
182 return nextLease;
183 });
184
185const scopeOauthClientSecretRef = (input: {
186 clientSecretProviderId: string | null;
187 clientSecretHandle: string | null;
188}): SecretRef | null =>
189 input.clientSecretProviderId && input.clientSecretHandle
190 ? {
191 providerId: input.clientSecretProviderId,
192 handle: input.clientSecretHandle,
193 }
194 : null;
195
196const cleanupProviderGrantRefreshToken = (executorState: ExecutorStateStoreShape, input: {
197 previous: SecretRef;
198 next: SecretRef;
199}, deleteSecretMaterial: DeleteSecretMaterial) =>
200 secretRefKey(input.previous) === secretRefKey(input.next)
201 ? Effect.void
202 : deleteSecretMaterial(input.previous).pipe(
203 Effect.either,
204 Effect.ignore,
205 );
206
207const refreshProviderGrantRefArtifact = (input: {
208 executorState: ExecutorStateStoreShape;
209 artifact: AuthArtifact;
210 lease: AuthLease | null;
211 resolveSecretMaterial: ResolveSecretMaterial;
212 storeSecretMaterial: StoreSecretMaterial;
213 deleteSecretMaterial: DeleteSecretMaterial;
214 context?: SecretMaterialResolveContext;
215}): Effect.Effect<AuthLease, Error, never> =>
216 Effect.gen(function* () {
217 const config = decodeProviderGrantRefAuthArtifactConfig(input.artifact);
218 if (config === null) {
219 return yield* runtimeEffectError("auth/auth-leases", `Unsupported auth artifact kind: ${input.artifact.artifactKind}`);
220 }
221
222 const grantOption = yield* input.executorState.providerAuthGrants.getById(config.grantId);
223 if (Option.isNone(grantOption)) {
224 return yield* runtimeEffectError("auth/auth-leases", `Provider auth grant not found: ${config.grantId}`);
225 }
226 const grant = grantOption.value;
227
228 const oauthClientOption = yield* input.executorState.scopeOauthClients.getById(grant.oauthClientId);
229 if (Option.isNone(oauthClientOption)) {
230 return yield* runtimeEffectError("auth/auth-leases", `Scope OAuth client not found: ${grant.oauthClientId}`);
231 }
232 const oauthClient = oauthClientOption.value;
233
234 const refreshToken = yield* input.resolveSecretMaterial({
235 ref: grant.refreshToken,
236 context: input.context,
237 });
238 const clientSecretRef = scopeOauthClientSecretRef(oauthClient);
239 const clientSecret = clientSecretRef
240 ? yield* input.resolveSecretMaterial({
241 ref: clientSecretRef,
242 context: input.context,
243 })
244 : null;
245
246 const tokenResponse = yield* refreshOAuth2AccessToken({
247 tokenEndpoint: grant.tokenEndpoint,
248 clientId: oauthClient.clientId,
249 clientAuthentication: grant.clientAuthentication,
250 clientSecret,
251 refreshToken,
252 scopes: config.requiredScopes.length > 0 ? config.requiredScopes : null,
253 });
254
255 const accessTokenRef = yield* input.storeSecretMaterial({
256 purpose: "oauth_access_token",
257 value: tokenResponse.access_token,
258 name: `${grant.providerKey} Access Token`,
259 });
260
261 const rotatedRefreshTokenRef = tokenResponse.refresh_token
262 ? yield* input.storeSecretMaterial({
263 purpose: "oauth_refresh_token",
264 value: tokenResponse.refresh_token,
265 name: `${grant.providerKey} Refresh Token`,
266 })
267 : grant.refreshToken;
268
269 const now = Date.now();
270 const nextGrant: ProviderAuthGrant = {
271 ...grant,
272 refreshToken: rotatedRefreshTokenRef,
273 lastRefreshedAt: now,
274 updatedAt: now,
275 };
276 yield* input.executorState.providerAuthGrants.upsert(nextGrant);
277 yield* cleanupProviderGrantRefreshToken(input.executorState, {
278 previous: grant.refreshToken,
279 next: rotatedRefreshTokenRef,
280 }, input.deleteSecretMaterial);
281
282 const expiresInMs =
283 typeof tokenResponse.expires_in === "number" && Number.isFinite(tokenResponse.expires_in)
284 ? Math.max(0, tokenResponse.expires_in * 1000)
285 : null;
286 const expiresAt = expiresInMs === null ? null : now + expiresInMs;
287 const refreshAfter =
288 expiresAt === null ? null : Math.max(now, expiresAt - LEASE_REFRESH_SKEW_MS);
289
290 const nextLease: AuthLease = {
291 id: input.lease?.id ?? AuthLeaseIdSchema.make(`auth_lease_${crypto.randomUUID()}`),
292 authArtifactId: input.artifact.id,
293 scopeId: input.artifact.scopeId,
294 sourceId: input.artifact.sourceId,
295 actorScopeId: input.artifact.actorScopeId,
296 slot: input.artifact.slot,
297 placementsTemplateJson: encodeLeasePlacementTemplatesJson([
298 {
299 location: "header",
300 name: config.headerName,
301 parts: [
302 {
303 kind: "literal",
304 value: config.prefix,
305 },
306 {
307 kind: "secret_ref",
308 ref: accessTokenRef,
309 },
310 ],
311 },
312 ]),
313 expiresAt,
314 refreshAfter,
315 createdAt: input.lease?.createdAt ?? now,
316 updatedAt: now,
317 };
318
319 yield* input.executorState.authLeases.upsert(nextLease);
320 yield* cleanupAuthLeaseSecretRefs(input.executorState, {
321 previous: input.lease,
322 next: nextLease,
323 }, input.deleteSecretMaterial);
324
325 return nextLease;
326 });
327
328export const removeAuthLeaseAndSecrets = (executorState: ExecutorStateStoreShape, input: {
329 authArtifactId: AuthArtifact["id"];
330}, deleteSecretMaterial: DeleteSecretMaterial): Effect.Effect<void, Error, never> =>
331 Effect.gen(function* () {
332 const existingLease = yield* executorState.authLeases.getByAuthArtifactId(input.authArtifactId);
333 if (Option.isNone(existingLease)) {
334 return;
335 }
336
337 yield* executorState.authLeases.removeByAuthArtifactId(input.authArtifactId);
338 yield* cleanupAuthLeaseSecretRefs(executorState, {
339 previous: existingLease.value,
340 next: null,
341 }, deleteSecretMaterial);
342 });
343
344export const upsertOauth2AuthorizedUserLeaseFromTokenResponse = (input: {
345 executorState: ExecutorStateStoreShape;
346 artifact: AuthArtifact;
347 tokenResponse: OAuth2TokenResponse;
348 storeSecretMaterial: StoreSecretMaterial;
349 deleteSecretMaterial: DeleteSecretMaterial;
350}): Effect.Effect<void, Error, never> =>
351 Effect.gen(function* () {
352 const decoded = decodeBuiltInAuthArtifactConfig(input.artifact);
353 if (
354 decoded === null
355 || decoded.artifactKind !== RefreshableOAuth2AuthorizedUserAuthArtifactKind
356 ) {
357 return yield* runtimeEffectError("auth/auth-leases", `Unsupported auth artifact kind: ${input.artifact.artifactKind}`);
358 }
359
360 const existingLeaseOption = yield* input.executorState.authLeases.getByAuthArtifactId(input.artifact.id);
361 const existingLease = Option.isSome(existingLeaseOption) ? existingLeaseOption.value : null;
362 const accessTokenRef = yield* input.storeSecretMaterial({
363 purpose: "oauth_access_token",
364 value: input.tokenResponse.access_token,
365 name: `${decoded.config.clientId} Access Token`,
366 });
367 const now = Date.now();
368 const expiresInMs =
369 typeof input.tokenResponse.expires_in === "number" && Number.isFinite(input.tokenResponse.expires_in)
370 ? Math.max(0, input.tokenResponse.expires_in * 1000)
371 : null;
372 const expiresAt = expiresInMs === null ? null : now + expiresInMs;
373 const refreshAfter =
374 expiresAt === null ? null : Math.max(now, expiresAt - LEASE_REFRESH_SKEW_MS);
375
376 const nextLease: AuthLease = {
377 id: existingLease?.id ?? AuthLeaseIdSchema.make(`auth_lease_${crypto.randomUUID()}`),
378 authArtifactId: input.artifact.id,
379 scopeId: input.artifact.scopeId,
380 sourceId: input.artifact.sourceId,
381 actorScopeId: input.artifact.actorScopeId,
382 slot: input.artifact.slot,
383 placementsTemplateJson: encodeLeasePlacementTemplatesJson([
384 {
385 location: "header",
386 name: decoded.config.headerName,
387 parts: [
388 {
389 kind: "literal",
390 value: decoded.config.prefix,
391 },
392 {
393 kind: "secret_ref",
394 ref: accessTokenRef,
395 },
396 ],
397 },
398 ]),
399 expiresAt,
400 refreshAfter,
401 createdAt: existingLease?.createdAt ?? now,
402 updatedAt: now,
403 };
404
405 yield* input.executorState.authLeases.upsert(nextLease);
406 yield* cleanupAuthLeaseSecretRefs(input.executorState, {
407 previous: existingLease,
408 next: nextLease,
409 }, input.deleteSecretMaterial);
410 });
411
412export const resolveAuthArtifactMaterialWithLeases = (input: {
413 executorState: ExecutorStateStoreShape;
414 artifact: AuthArtifact | null;
415 resolveSecretMaterial: ResolveSecretMaterial;
416 storeSecretMaterial: StoreSecretMaterial;
417 deleteSecretMaterial: DeleteSecretMaterial;
418 context?: SecretMaterialResolveContext;
419}): Effect.Effect<ResolvedSourceAuthMaterial, Error, never> =>
420 Effect.gen(function* () {
421 if (input.artifact === null) {
422 return yield* resolveAuthArtifactMaterial({
423 artifact: null,
424 resolveSecretMaterial: input.resolveSecretMaterial,
425 context: input.context,
426 });
427 }
428
429 const existingLeaseOption = yield* input.executorState.authLeases.getByAuthArtifactId(input.artifact.id);
430 const existingLease = Option.isSome(existingLeaseOption) ? existingLeaseOption.value : null;
431 const decoded = decodeBuiltInAuthArtifactConfig(input.artifact);
432 const providerGrantConfig = decodeProviderGrantRefAuthArtifactConfig(input.artifact);
433 const mcpOAuthConfig = decodeMcpOAuthAuthArtifactConfig(input.artifact);
434
435 if (
436 decoded !== null
437 && decoded.artifactKind === RefreshableOAuth2AuthorizedUserAuthArtifactKind
438 ) {
439 const lease = leaseIsFresh(existingLease, Date.now())
440 ? existingLease
441 : yield* refreshRefreshableOauth2AuthorizedUserArtifact({
442 executorState: input.executorState,
443 artifact: input.artifact,
444 lease: existingLease,
445 resolveSecretMaterial: input.resolveSecretMaterial,
446 storeSecretMaterial: input.storeSecretMaterial,
447 deleteSecretMaterial: input.deleteSecretMaterial,
448 context: input.context,
449 });
450
451 return yield* resolveAuthArtifactMaterial({
452 artifact: input.artifact,
453 lease,
454 resolveSecretMaterial: input.resolveSecretMaterial,
455 context: input.context,
456 });
457 }
458
459 if (providerGrantConfig !== null) {
460 const lease = leaseIsFresh(existingLease, Date.now())
461 ? existingLease
462 : yield* refreshProviderGrantRefArtifact({
463 executorState: input.executorState,
464 artifact: input.artifact,
465 lease: existingLease,
466 resolveSecretMaterial: input.resolveSecretMaterial,
467 storeSecretMaterial: input.storeSecretMaterial,
468 deleteSecretMaterial: input.deleteSecretMaterial,
469 context: input.context,
470 });
471
472 return yield* resolveAuthArtifactMaterial({
473 artifact: input.artifact,
474 lease,
475 resolveSecretMaterial: input.resolveSecretMaterial,
476 context: input.context,
477 });
478 }
479
480 if (mcpOAuthConfig !== null) {
481 const material = yield* resolveAuthArtifactMaterial({
482 artifact: input.artifact,
483 resolveSecretMaterial: input.resolveSecretMaterial,
484 context: input.context,
485 });
486
487 return {
488 ...material,
489 authProvider: createPersistedMcpAuthProvider({
490 executorState: input.executorState,
491 artifact: input.artifact,
492 config: mcpOAuthConfig,
493 resolveSecretMaterial: input.resolveSecretMaterial,
494 storeSecretMaterial: input.storeSecretMaterial,
495 deleteSecretMaterial: input.deleteSecretMaterial,
496 context: input.context,
497 }),
498 };
499 }
500
501 return yield* resolveAuthArtifactMaterial({
502 artifact: input.artifact,
503 lease: existingLease,
504 resolveSecretMaterial: input.resolveSecretMaterial,
505 context: input.context,
506 });
507 });
packages/platform/sdk/src/runtime/auth/mcp-auth-provider.ts+0−262
@@ -1,262 +0,0 @@
1import type {
2 OAuthClientInformationMixed,
3 OAuthTokens,
4} from "@modelcontextprotocol/sdk/shared/auth.js";
5import type {
6 OAuthClientMetadata,
7} from "@modelcontextprotocol/sdk/shared/auth.js";
8import type {
9 OAuthClientProvider,
10 OAuthDiscoveryState,
11} from "@modelcontextprotocol/sdk/client/auth.js";
12import type {
13 AuthArtifact,
14 McpOAuthAuthArtifactConfig,
15 SecretRef,
16} from "#schema";
17import {
18 McpOAuthAuthArtifactConfigJsonSchema,
19} from "#schema";
20import * as Effect from "effect/Effect";
21import * as Schema from "effect/Schema";
22
23import type {
24 ExecutorStateStoreShape,
25} from "../executor-state-store";
26import {
27 type DeleteSecretMaterial,
28 type ResolveSecretMaterial,
29 type SecretMaterialResolveContext,
30 type StoreSecretMaterial,
31} from "../scope/secret-material-providers";
32
33const encodeMcpOAuthAuthArtifactConfig = Schema.encodeSync(
34 McpOAuthAuthArtifactConfigJsonSchema,
35);
36
37const parseJsonObject = <T>(value: string | null): T | undefined => {
38 if (value === null) {
39 return undefined;
40 }
41
42 try {
43 const parsed = JSON.parse(value) as unknown;
44 return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
45 ? parsed as T
46 : undefined;
47 } catch {
48 return undefined;
49 }
50};
51
52const stringifyJsonObject = (value: unknown): string | null =>
53 value === null || value === undefined ? null : JSON.stringify(value);
54
55const createClientMetadata = (redirectUrl: string): OAuthClientMetadata => ({
56 redirect_uris: [redirectUrl],
57 grant_types: ["authorization_code", "refresh_token"],
58 response_types: ["code"],
59 token_endpoint_auth_method: "none",
60 client_name: "Executor Local",
61});
62
63const secretRefEquals = (left: SecretRef | null, right: SecretRef | null): boolean =>
64 (left?.providerId ?? null) === (right?.providerId ?? null)
65 && (left?.handle ?? null) === (right?.handle ?? null);
66
67const cleanupReplacedSecretRefs = (input: {
68 deleteSecretMaterial: DeleteSecretMaterial;
69 previousAccessToken: SecretRef;
70 nextAccessToken: SecretRef;
71 previousRefreshToken: SecretRef | null;
72 nextRefreshToken: SecretRef | null;
73}) =>
74 Effect.gen(function* () {
75 if (!secretRefEquals(input.previousAccessToken, input.nextAccessToken)) {
76 yield* Effect.either(input.deleteSecretMaterial(input.previousAccessToken));
77 }
78
79 if (
80 input.previousRefreshToken !== null
81 && !secretRefEquals(input.previousRefreshToken, input.nextRefreshToken)
82 ) {
83 yield* Effect.either(input.deleteSecretMaterial(input.previousRefreshToken));
84 }
85 });
86
87export const createPersistedMcpOAuthSourceAuth = (input: {
88 redirectUri: string;
89 accessToken: SecretRef;
90 refreshToken: SecretRef | null;
91 tokenType: string;
92 expiresIn: number | null;
93 scope: string | null;
94 resourceMetadataUrl: string | null;
95 authorizationServerUrl: string | null;
96 resourceMetadata: unknown;
97 authorizationServerMetadata: unknown;
98 clientInformation: unknown;
99}) => ({
100 kind: "mcp_oauth" as const,
101 redirectUri: input.redirectUri,
102 accessToken: input.accessToken,
103 refreshToken: input.refreshToken,
104 tokenType: input.tokenType,
105 expiresIn: input.expiresIn,
106 scope: input.scope,
107 resourceMetadataUrl: input.resourceMetadataUrl,
108 authorizationServerUrl: input.authorizationServerUrl,
109 resourceMetadataJson: stringifyJsonObject(input.resourceMetadata),
110 authorizationServerMetadataJson: stringifyJsonObject(input.authorizationServerMetadata),
111 clientInformationJson: stringifyJsonObject(input.clientInformation),
112});
113
114export const createPersistedMcpAuthProvider = (input: {
115 executorState: ExecutorStateStoreShape;
116 artifact: AuthArtifact;
117 config: McpOAuthAuthArtifactConfig;
118 resolveSecretMaterial: ResolveSecretMaterial;
119 storeSecretMaterial: StoreSecretMaterial;
120 deleteSecretMaterial: DeleteSecretMaterial;
121 context?: SecretMaterialResolveContext;
122}): OAuthClientProvider => {
123 let currentArtifact = input.artifact;
124 let currentConfig = input.config;
125
126 const persistConfig = (nextConfig: McpOAuthAuthArtifactConfig) =>
127 Effect.gen(function* () {
128 const nextArtifact: AuthArtifact = {
129 ...currentArtifact,
130 configJson: encodeMcpOAuthAuthArtifactConfig(nextConfig),
131 updatedAt: Date.now(),
132 };
133 yield* input.executorState.authArtifacts.upsert(nextArtifact);
134 currentArtifact = nextArtifact;
135 currentConfig = nextConfig;
136 });
137
138 return {
139 get redirectUrl() {
140 return currentConfig.redirectUri;
141 },
142
143 get clientMetadata() {
144 return createClientMetadata(currentConfig.redirectUri);
145 },
146
147 clientInformation: () =>
148 parseJsonObject<OAuthClientInformationMixed>(currentConfig.clientInformationJson),
149
150 saveClientInformation: (clientInformation) =>
151 Effect.runPromise(
152 persistConfig({
153 ...currentConfig,
154 clientInformationJson: stringifyJsonObject(clientInformation),
155 }),
156 ).then(() => undefined),
157
158 tokens: async () => {
159 const accessToken = await Effect.runPromise(
160 input.resolveSecretMaterial({
161 ref: currentConfig.accessToken,
162 context: input.context,
163 }),
164 );
165 const refreshToken = currentConfig.refreshToken
166 ? await Effect.runPromise(
167 input.resolveSecretMaterial({
168 ref: currentConfig.refreshToken,
169 context: input.context,
170 }),
171 )
172 : undefined;
173
174 return {
175 access_token: accessToken,
176 token_type: currentConfig.tokenType,
177 ...(typeof currentConfig.expiresIn === "number"
178 ? { expires_in: currentConfig.expiresIn }
179 : {}),
180 ...(currentConfig.scope ? { scope: currentConfig.scope } : {}),
181 ...(refreshToken ? { refresh_token: refreshToken } : {}),
182 } satisfies OAuthTokens;
183 },
184
185 saveTokens: (tokens) =>
186 Effect.runPromise(Effect.gen(function* () {
187 const previousConfig = currentConfig;
188 const accessTokenRef = yield* input.storeSecretMaterial({
189 purpose: "oauth_access_token",
190 value: tokens.access_token,
191 name: `${currentArtifact.sourceId} MCP Access Token`,
192 });
193 const refreshTokenRef = tokens.refresh_token
194 ? yield* input.storeSecretMaterial({
195 purpose: "oauth_refresh_token",
196 value: tokens.refresh_token,
197 name: `${currentArtifact.sourceId} MCP Refresh Token`,
198 })
199 : currentConfig.refreshToken;
200
201 const nextConfig: McpOAuthAuthArtifactConfig = {
202 ...currentConfig,
203 accessToken: accessTokenRef,
204 refreshToken: refreshTokenRef,
205 tokenType: tokens.token_type ?? currentConfig.tokenType,
206 expiresIn:
207 typeof tokens.expires_in === "number" && Number.isFinite(tokens.expires_in)
208 ? tokens.expires_in
209 : currentConfig.expiresIn,
210 scope: tokens.scope ?? currentConfig.scope,
211 };
212
213 yield* persistConfig(nextConfig);
214 yield* cleanupReplacedSecretRefs({
215 deleteSecretMaterial: input.deleteSecretMaterial,
216 previousAccessToken: previousConfig.accessToken,
217 nextAccessToken: accessTokenRef,
218 previousRefreshToken: previousConfig.refreshToken,
219 nextRefreshToken: refreshTokenRef,
220 });
221 })).then(() => undefined),
222
223 redirectToAuthorization: async (authorizationUrl) => {
224 throw new Error(
225 `MCP OAuth re-authorization is required for ${currentArtifact.sourceId}: ${authorizationUrl.toString()}`,
226 );
227 },
228
229 saveCodeVerifier: () => undefined,
230
231 codeVerifier: () => {
232 throw new Error("Persisted MCP OAuth sessions do not retain an active PKCE verifier");
233 },
234
235 saveDiscoveryState: (state) =>
236 Effect.runPromise(
237 persistConfig({
238 ...currentConfig,
239 resourceMetadataUrl: state.resourceMetadataUrl ?? null,
240 authorizationServerUrl: state.authorizationServerUrl ?? null,
241 resourceMetadataJson: stringifyJsonObject(state.resourceMetadata),
242 authorizationServerMetadataJson: stringifyJsonObject(
243 state.authorizationServerMetadata,
244 ),
245 }),
246 ).then(() => undefined),
247
248 discoveryState: () =>
249 currentConfig.authorizationServerUrl === null
250 ? undefined
251 : {
252 resourceMetadataUrl: currentConfig.resourceMetadataUrl ?? undefined,
253 authorizationServerUrl: currentConfig.authorizationServerUrl,
254 resourceMetadata: parseJsonObject<OAuthDiscoveryState["resourceMetadata"]>(
255 currentConfig.resourceMetadataJson,
256 ),
257 authorizationServerMetadata: parseJsonObject<
258 OAuthDiscoveryState["authorizationServerMetadata"]
259 >(currentConfig.authorizationServerMetadataJson),
260 },
261 };
262};
packages/platform/sdk/src/runtime/auth/provider-grant-lifecycle.ts+0−94
@@ -1,94 +0,0 @@
1import type {
2 AuthArtifact,
3 ProviderAuthGrant,
4 ScopeId,
5} from "#schema";
6import {
7 decodeProviderGrantRefAuthArtifactConfig,
8} from "#schema";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11
12import {
13 type DeleteSecretMaterial,
14} from "../scope/secret-material-providers";
15import type {
16 ExecutorStateStoreShape,
17} from "../executor-state-store";
18
19const providerGrantRefFromArtifact = (
20 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
21) => decodeProviderGrantRefAuthArtifactConfig(artifact);
22
23export const providerGrantIdFromArtifact = (
24 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
25): ProviderAuthGrant["id"] | null =>
26 providerGrantRefFromArtifact(artifact)?.grantId ?? null;
27
28export const listProviderGrantRefArtifacts = (executorState: ExecutorStateStoreShape, input: {
29 scopeId: ScopeId;
30 grantId?: ProviderAuthGrant["id"] | null;
31}): Effect.Effect<readonly AuthArtifact[], Error, never> =>
32 Effect.map(
33 executorState.authArtifacts.listByScopeId(input.scopeId),
34 (artifacts) =>
35 artifacts.filter((artifact) => {
36 const grantId = providerGrantIdFromArtifact(artifact);
37 return grantId !== null && (input.grantId == null || grantId === input.grantId);
38 }),
39 );
40
41export const clearProviderGrantOrphanedAt = (executorState: ExecutorStateStoreShape, input: {
42 grantId: ProviderAuthGrant["id"];
43}): Effect.Effect<boolean, Error, never> =>
44 Effect.gen(function* () {
45 const grantOption = yield* executorState.providerAuthGrants.getById(input.grantId);
46 if (Option.isNone(grantOption) || grantOption.value.orphanedAt === null) {
47 return false;
48 }
49
50 yield* executorState.providerAuthGrants.upsert({
51 ...grantOption.value,
52 orphanedAt: null,
53 updatedAt: Date.now(),
54 });
55 return true;
56 });
57
58export const markProviderGrantOrphanedIfUnused = (executorState: ExecutorStateStoreShape, input: {
59 scopeId: ScopeId;
60 grantId: ProviderAuthGrant["id"];
61}): Effect.Effect<boolean, Error, never> =>
62 Effect.gen(function* () {
63 const references = yield* listProviderGrantRefArtifacts(executorState, input);
64 if (references.length > 0) {
65 return false;
66 }
67
68 const grantOption = yield* executorState.providerAuthGrants.getById(input.grantId);
69 if (Option.isNone(grantOption) || grantOption.value.scopeId !== input.scopeId) {
70 return false;
71 }
72
73 const grant = grantOption.value;
74 if (grant.orphanedAt !== null) {
75 return false;
76 }
77
78 yield* executorState.providerAuthGrants.upsert({
79 ...grant,
80 orphanedAt: Date.now(),
81 updatedAt: Date.now(),
82 });
83 return true;
84 });
85
86export const removeProviderAuthGrantSecret = (_executorState: ExecutorStateStoreShape, input: {
87 grant: ProviderAuthGrant;
88}, deleteSecretMaterial: DeleteSecretMaterial): Effect.Effect<void, Error, never> =>
89 Effect.gen(function* () {
90 yield* deleteSecretMaterial(input.grant.refreshToken).pipe(
91 Effect.either,
92 Effect.ignore,
93 );
94 });
packages/platform/sdk/src/runtime/auth/source-auth-material.ts+0−176
@@ -1,176 +0,0 @@
1import type {
2 ScopeId,
3 CredentialSlot,
4 Source,
5 SourceAuth,
6} from "#schema";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11
12import {
13 ExecutorStateStore,
14 type ExecutorStateStoreShape,
15} from "../executor-state-store";
16import {
17 authArtifactFromSourceAuth,
18 resolveAuthArtifactMaterial,
19 type ResolvedSourceAuthMaterial,
20} from "./auth-artifacts";
21import {
22 resolveAuthArtifactMaterialWithLeases,
23} from "./auth-leases";
24import type {
25 DeleteSecretMaterial,
26 ResolveSecretMaterial,
27 SecretMaterialResolveContext,
28 StoreSecretMaterial,
29} from "../scope/secret-material-providers";
30import {
31 SecretMaterialDeleterService,
32 SecretMaterialResolverService,
33 SecretMaterialStorerService,
34} from "../scope/secret-material-providers";
35import {
36 runtimeEffectError,
37} from "../effect-errors";
38
39const authForSlot = (input: {
40 source: Source;
41 slot: CredentialSlot;
42}): SourceAuth => {
43 if (input.slot === "runtime") {
44 return input.source.auth;
45 }
46
47 if (input.source.importAuthPolicy === "reuse_runtime") {
48 return input.source.auth;
49 }
50
51 if (input.source.importAuthPolicy === "none") {
52 return { kind: "none" };
53 }
54
55 return input.source.importAuth;
56};
57
58export type RuntimeSourceAuthMaterialShape = {
59 resolve: (input: {
60 source: Source;
61 slot?: CredentialSlot;
62 actorScopeId?: ScopeId | null;
63 context?: SecretMaterialResolveContext;
64 }) => Effect.Effect<ResolvedSourceAuthMaterial, Error, never>;
65};
66
67export class RuntimeSourceAuthMaterialService extends Context.Tag(
68 "#runtime/RuntimeSourceAuthMaterialService",
69)<RuntimeSourceAuthMaterialService, RuntimeSourceAuthMaterialShape>() {}
70
71export const resolveSourceAuthMaterialWithDeps = (input: {
72 source: Source;
73 slot?: CredentialSlot;
74 actorScopeId?: ScopeId | null;
75 executorState?: ExecutorStateStoreShape;
76 resolveSecretMaterial: ResolveSecretMaterial;
77 storeSecretMaterial?: StoreSecretMaterial;
78 deleteSecretMaterial?: DeleteSecretMaterial;
79 context?: SecretMaterialResolveContext;
80}): Effect.Effect<ResolvedSourceAuthMaterial, Error, never> =>
81 Effect.gen(function* () {
82 const slot = input.slot ?? "runtime";
83
84 if (input.executorState !== undefined) {
85 const candidateSlots =
86 slot === "import" && input.source.importAuthPolicy === "reuse_runtime"
87 ? ["import", "runtime"] satisfies ReadonlyArray<CredentialSlot>
88 : [slot] satisfies ReadonlyArray<CredentialSlot>;
89
90 for (const candidateSlot of candidateSlots) {
91 const artifactOption = yield* input.executorState.authArtifacts.getByScopeSourceAndActor({
92 scopeId: input.source.scopeId,
93 sourceId: input.source.id,
94 actorScopeId: input.actorScopeId ?? null,
95 slot: candidateSlot,
96 });
97
98 if (Option.isSome(artifactOption)) {
99 return yield* resolveAuthArtifactMaterialWithLeases({
100 executorState: input.executorState,
101 artifact: artifactOption.value,
102 resolveSecretMaterial: input.resolveSecretMaterial,
103 storeSecretMaterial: input.storeSecretMaterial ?? (() => Effect.dieMessage("storeSecretMaterial unavailable")),
104 deleteSecretMaterial: input.deleteSecretMaterial ?? (() => Effect.dieMessage("deleteSecretMaterial unavailable")),
105 context: input.context,
106 });
107 }
108 }
109 }
110
111 const artifact = authArtifactFromSourceAuth({
112 source: input.source,
113 auth: authForSlot({
114 source: input.source,
115 slot,
116 }),
117 slot,
118 actorScopeId: input.actorScopeId ?? null,
119 });
120
121 if (input.executorState !== undefined) {
122 return yield* resolveAuthArtifactMaterialWithLeases({
123 executorState: input.executorState,
124 artifact,
125 resolveSecretMaterial: input.resolveSecretMaterial,
126 storeSecretMaterial: input.storeSecretMaterial ?? (() => Effect.dieMessage("storeSecretMaterial unavailable")),
127 deleteSecretMaterial: input.deleteSecretMaterial ?? (() => Effect.dieMessage("deleteSecretMaterial unavailable")),
128 context: input.context,
129 });
130 }
131
132 if (
133 artifact?.artifactKind === "oauth2_authorized_user"
134 || artifact?.artifactKind === "provider_grant_ref"
135 || artifact?.artifactKind === "mcp_oauth"
136 ) {
137 return yield* runtimeEffectError("auth/source-auth-material", "Dynamic auth artifacts require persistence-backed lease resolution");
138 }
139
140 return yield* resolveAuthArtifactMaterial({
141 artifact,
142 resolveSecretMaterial: input.resolveSecretMaterial,
143 context: input.context,
144 });
145 });
146
147export const resolveSourceAuthMaterial = (input: {
148 source: Source;
149 slot?: CredentialSlot;
150 actorScopeId?: ScopeId | null;
151 context?: SecretMaterialResolveContext;
152}): Effect.Effect<ResolvedSourceAuthMaterial, Error, RuntimeSourceAuthMaterialService> =>
153 Effect.flatMap(RuntimeSourceAuthMaterialService, (service) => service.resolve(input));
154
155export const RuntimeSourceAuthMaterialLive = Layer.effect(
156 RuntimeSourceAuthMaterialService,
157 Effect.gen(function* () {
158 const executorState = yield* ExecutorStateStore;
159 const resolveSecretMaterial = yield* SecretMaterialResolverService;
160 const storeSecretMaterial = yield* SecretMaterialStorerService;
161 const deleteSecretMaterial = yield* SecretMaterialDeleterService;
162
163 return RuntimeSourceAuthMaterialService.of({
164 resolve: (input) =>
165 resolveSourceAuthMaterialWithDeps({
166 ...input,
167 executorState,
168 resolveSecretMaterial,
169 storeSecretMaterial,
170 deleteSecretMaterial,
171 }),
172 });
173 }),
174);
175
176export type { ResolvedSourceAuthMaterial } from "./auth-artifacts";
packages/platform/sdk/src/runtime/catalog/source/reconcile.ts+3−3
@@ -11,8 +11,8 @@ import { · 10 unmodified lines
11 SourceArtifactStore,11 SourceArtifactStore,
12} from "../../scope/storage";12} from "../../scope/storage";
13import {13import {
14 getSourceAdapterForSource,14 getSourcePluginForSource,
15} from "../../sources/source-adapters";15} from "../../sources/source-plugins";
16import {16import {
17 RuntimeSourceStoreService,17 RuntimeSourceStoreService,
18} from "../../sources/source-store";18} from "../../sources/source-store";
@@ -23,7 +23,7 @@ import { · 4 unmodified lines
23const shouldReconcileSource = (source: Source): boolean =>23const shouldReconcileSource = (source: Source): boolean =>
24 source.enabled24 source.enabled
25 && source.status === "connected"25 && source.status === "connected"
26 && getSourceAdapterForSource(source).catalogKind !== "internal";26 && getSourcePluginForSource(source).catalogKind !== "internal";
27 27
28export const reconcileMissingSourceCatalogArtifacts = (input: {28export const reconcileMissingSourceCatalogArtifacts = (input: {
29 scopeId: ScopeId;29 scopeId: ScopeId;
packages/platform/sdk/src/runtime/catalog/source/runtime.ts+3−5
@@ -597,11 +597,11 @@ const codemodeDescriptorFromCapability = (input: { · 596 unmodified lines
597 ...(inputSchema !== undefined ? { inputSchema } : {}),597 ...(inputSchema !== undefined ? { inputSchema } : {}),
598 ...(outputSchema !== undefined ? { outputSchema } : {}),598 ...(outputSchema !== undefined ? { outputSchema } : {}),
599 },599 },
600 providerKind: input.executable.adapterKey,600 pluginKind: input.executable.pluginKey,
601 providerData: {601 pluginData: {
602 capabilityId: input.capability.id,602 capabilityId: input.capability.id,
603 executableId: input.executable.id,603 executableId: input.executable.id,
604 adapterKey: input.executable.adapterKey,604 pluginKey: input.executable.pluginKey,
605 display: input.executable.display,605 display: input.executable.display,
606 },606 },
607 };607 };
@@ -679,8 +679,6 @@ const sourceRecordFromCatalogArtifact = (input: { · 71 unmodified lines
679 status: input.source.status,679 status: input.source.status,
680 enabled: input.source.enabled,680 enabled: input.source.enabled,
681 namespace: input.source.namespace,681 namespace: input.source.namespace,
682 importAuthPolicy: input.source.importAuthPolicy,
683 bindingConfigJson: JSON.stringify(input.source.binding),
684 sourceHash: input.source.sourceHash,682 sourceHash: input.source.sourceHash,
685 lastError: input.source.lastError,683 lastError: input.source.lastError,
686 createdAt: input.source.createdAt,684 createdAt: input.source.createdAt,
packages/platform/sdk/src/runtime/catalog/source/sync.ts+9−36
@@ -21,14 +21,8 @@ import { · 20 unmodified lines
21 type LocalScopeState,21 type LocalScopeState,
22} from "../../scope-state";22} from "../../scope-state";
23import {23import {
24 RuntimeSourceAuthMaterialService,24 getSourcePluginForSource,
25} from "../../auth/source-auth-material";25} from "../../sources/source-plugins";
26import {
27 getSourceAdapterForSource,
28} from "../../sources/source-adapters";
29import {
30 SecretMaterialResolverService,
31} from "../../scope/secret-material-providers";
32import {26import {
33 snapshotFromSourceCatalogSyncResult,27 snapshotFromSourceCatalogSyncResult,
34} from "@executor/source-core";28} from "@executor/source-core";
@@ -43,24 +37,20 @@ import { · 8 unmodified lines
43const shouldIndexSource = (source: Source): boolean =>37const shouldIndexSource = (source: Source): boolean =>
44 source.enabled38 source.enabled
45 && source.status === "connected"39 && source.status === "connected"
46 && getSourceAdapterForSource(source).catalogKind !== "internal";40 && getSourcePluginForSource(source).catalogKind !== "internal";
47 41
48type RuntimeSourceCatalogSyncDeps = {42type RuntimeSourceCatalogSyncDeps = {
49 runtimeLocalScope: RuntimeLocalScopeState;43 runtimeLocalScope: RuntimeLocalScopeState;
50 scopeStateStore: ScopeStateStoreShape;44 scopeStateStore: ScopeStateStoreShape;
51 sourceArtifactStore: SourceArtifactStoreShape;45 sourceArtifactStore: SourceArtifactStoreShape;
52 sourceTypeDeclarationsRefresher: SourceTypeDeclarationsRefresherShape;46 sourceTypeDeclarationsRefresher: SourceTypeDeclarationsRefresherShape;
53 resolveSecretMaterial: Effect.Effect.Success<typeof SecretMaterialResolverService>;
54 sourceAuthMaterialService: Effect.Effect.Success<typeof RuntimeSourceAuthMaterialService>;
55};47};
56 48
57type SourceCatalogSyncServices =49type SourceCatalogSyncServices =
58 | RuntimeLocalScopeService50 | RuntimeLocalScopeService
59 | ScopeStateStore51 | ScopeStateStore
60 | SourceArtifactStore52 | SourceArtifactStore
61 | SourceTypeDeclarationsRefresherService53 | SourceTypeDeclarationsRefresherService;
62 | RuntimeSourceAuthMaterialService
63 | SecretMaterialResolverService;
64 54
65export type RuntimeSourceCatalogSyncShape = {55export type RuntimeSourceCatalogSyncShape = {
66 sync: (input: {56 sync: (input: {
@@ -123,23 +113,16 @@ const syncSourceCatalogWithDeps = ( · 56 unmodified lines
123 return;113 return;
124 }114 }
125 115
126 const adapter = getSourceAdapterForSource(input.source);116 const definition = getSourcePluginForSource(input.source);
127 const syncResult = yield* adapter.syncCatalog({117 const irModel = yield* definition.getIrModel({
128 source: input.source,118 source: input.source,
129 resolveSecretMaterial: deps.resolveSecretMaterial,
130 resolveAuthMaterialForSlot: (slot) =>
131 deps.sourceAuthMaterialService.resolve({
132 source: input.source,
133 slot,
134 actorScopeId: input.actorScopeId,
135 }),
136 });119 });
137 const snapshot = snapshotFromSourceCatalogSyncResult(syncResult);120 const snapshot = snapshotFromSourceCatalogSyncResult(irModel);
138 yield* deps.sourceArtifactStore.write({121 yield* deps.sourceArtifactStore.write({
139 sourceId: input.source.id,122 sourceId: input.source.id,
140 artifact: deps.sourceArtifactStore.build({123 artifact: deps.sourceArtifactStore.build({
141 source: input.source,124 source: input.source,
142 syncResult,125 syncResult: irModel,
143 }),126 }),
144 });127 });
145 128
@@ -152,7 +135,7 @@ const syncSourceCatalogWithDeps = ( · 6 unmodified lines
152 [input.source.id]: {135 [input.source.id]: {
153 status: "connected",136 status: "connected",
154 lastError: null,137 lastError: null,
155 sourceHash: syncResult.sourceHash,138 sourceHash: irModel.sourceHash,
156 createdAt: existingSourceState?.createdAt ?? input.source.createdAt,139 createdAt: existingSourceState?.createdAt ?? input.source.createdAt,
157 updatedAt: Date.now(),140 updatedAt: Date.now(),
158 },141 },
@@ -187,17 +170,12 @@ export const syncSourceCatalog = (input: { · 28 unmodified lines
187 const sourceArtifactStore = yield* SourceArtifactStore;170 const sourceArtifactStore = yield* SourceArtifactStore;
188 const sourceTypeDeclarationsRefresher =171 const sourceTypeDeclarationsRefresher =
189 yield* SourceTypeDeclarationsRefresherService;172 yield* SourceTypeDeclarationsRefresherService;
190 const resolveSecretMaterial = yield* SecretMaterialResolverService;
191 const sourceAuthMaterialService = yield* RuntimeSourceAuthMaterialService;
192
193 return yield* syncSourceCatalogWithDeps(173 return yield* syncSourceCatalogWithDeps(
194 {174 {
195 runtimeLocalScope,175 runtimeLocalScope,
196 scopeStateStore,176 scopeStateStore,
197 sourceArtifactStore,177 sourceArtifactStore,
198 sourceTypeDeclarationsRefresher,178 sourceTypeDeclarationsRefresher,
199 resolveSecretMaterial,
200 sourceAuthMaterialService,
201 },179 },
202 {180 {
203 source: input.source,181 source: input.source,
@@ -214,16 +192,11 @@ export const RuntimeSourceCatalogSyncLive = Layer.effect( · 10 unmodified lines
214 const sourceArtifactStore = yield* SourceArtifactStore;192 const sourceArtifactStore = yield* SourceArtifactStore;
215 const sourceTypeDeclarationsRefresher =193 const sourceTypeDeclarationsRefresher =
216 yield* SourceTypeDeclarationsRefresherService;194 yield* SourceTypeDeclarationsRefresherService;
217 const resolveSecretMaterial = yield* SecretMaterialResolverService;
218 const sourceAuthMaterialService = yield* RuntimeSourceAuthMaterialService;
219
220 const deps: RuntimeSourceCatalogSyncDeps = {195 const deps: RuntimeSourceCatalogSyncDeps = {
221 runtimeLocalScope,196 runtimeLocalScope,
222 scopeStateStore,197 scopeStateStore,
223 sourceArtifactStore,198 sourceArtifactStore,
224 sourceTypeDeclarationsRefresher,199 sourceTypeDeclarationsRefresher,
225 resolveSecretMaterial,
226 sourceAuthMaterialService,
227 };200 };
228 201
229 return RuntimeSourceCatalogSyncService.of({202 return RuntimeSourceCatalogSyncService.of({
packages/platform/sdk/src/runtime/execution/ir-execution.ts+6−11
@@ -10,12 +10,9 @@ import * as Effect from "effect/Effect"; · 9 unmodified lines
10import type {10import type {
11 LoadedSourceCatalogToolIndexEntry,11 LoadedSourceCatalogToolIndexEntry,
12} from "../catalog/source/runtime";12} from "../catalog/source/runtime";
13import type {
14 ResolvedSourceAuthMaterial,
15} from "../auth/source-auth-material";
16import {13import {
17 getSourceAdapter,14 getSourcePlugin,
18} from "../sources/source-adapters";15} from "../sources/source-plugins";
19import {16import {
20 runtimeEffectError,17 runtimeEffectError,
21} from "../effect-errors";18} from "../effect-errors";
@@ -55,28 +52,26 @@ export const invokeIrTool = (input: { · 33 unmodified lines
55 scopeId: Source["scopeId"];52 scopeId: Source["scopeId"];
56 actorScopeId: ScopeId;53 actorScopeId: ScopeId;
57 tool: LoadedSourceCatalogToolIndexEntry;54 tool: LoadedSourceCatalogToolIndexEntry;
58 auth: ResolvedSourceAuthMaterial;
59 args: unknown;55 args: unknown;
60 onElicitation?: OnElicitation;56 onElicitation?: OnElicitation;
61 context?: Record<string, unknown>;57 context?: Record<string, unknown>;
62}) => {58}) => {
63 const adapter = getSourceAdapter(input.tool.executable.adapterKey);59 const definition = getSourcePlugin(input.tool.executable.pluginKey);
64 if (adapter.key !== input.tool.source.kind) {60 if (definition.kind !== input.tool.source.kind) {
65 return Effect.fail(61 return Effect.fail(
66 runtimeEffectError("execution/ir-execution", 62 runtimeEffectError("execution/ir-execution",
67 `Executable ${input.tool.executable.id} expects adapter ${adapter.key}, but source ${input.tool.source.id} is ${input.tool.source.kind}`,63 `Executable ${input.tool.executable.id} expects source type ${definition.kind}, but source ${input.tool.source.id} is ${input.tool.source.kind}`,
68 ),64 ),
69 );65 );
70 }66 }
71 67
72 return adapter.invoke({68 return definition.invoke({
73 source: input.tool.source,69 source: input.tool.source,
74 capability: input.tool.capability,70 capability: input.tool.capability,
75 executable: input.tool.executable,71 executable: input.tool.executable,
76 descriptor: input.tool.descriptor,72 descriptor: input.tool.descriptor,
77 catalog: input.tool.projectedCatalog,73 catalog: input.tool.projectedCatalog,
78 args: input.args,74 args: input.args,
79 auth: input.auth,
80 onElicitation: input.onElicitation,75 onElicitation: input.onElicitation,
81 context: input.context,76 context: input.context,
82 });77 });
packages/platform/sdk/src/runtime/execution/live.ts+0−6
@@ -111,12 +111,6 @@ const interactionPurposeFromInput = (input: Parameters<OnElicitation>[0]): strin · 110 unmodified lines
111 return explicitPurpose;111 return explicitPurpose;
112 }112 }
113 113
114 if (input.path === "executor.sources.add") {
115 return input.elicitation.mode === "url"
116 ? "source_connect_oauth2"
117 : "source_connect_secret";
118 }
119
120 return "elicitation";114 return "elicitation";
121};115};
122 116
packages/platform/sdk/src/runtime/execution/scope/environment.ts+0−19
@@ -17,15 +17,9 @@ import { · 16 unmodified lines
17import {17import {
18 createScopeToolInvoker,18 createScopeToolInvoker,
19} from "./tool-invoker";19} from "./tool-invoker";
20import {
21 RuntimeSourceAuthServiceTag,
22} from "../../sources/source-auth-service";
23import {20import {
24 RuntimeSourceCatalogStoreService,21 RuntimeSourceCatalogStoreService,
25} from "../../catalog/source/runtime";22} from "../../catalog/source/runtime";
26import {
27 RuntimeSourceAuthMaterialService,
28} from "../../auth/source-auth-material";
29import {23import {
30 RuntimeSourceCatalogSyncService,24 RuntimeSourceCatalogSyncService,
31} from "../../catalog/source/sync";25} from "../../catalog/source/sync";
@@ -81,12 +75,6 @@ export const createScopeExecutionEnvironmentResolver = · 49 unmodified lines
81 sourceCatalogSyncService: Effect.Effect.Success<75 sourceCatalogSyncService: Effect.Effect.Success<
82 typeof RuntimeSourceCatalogSyncService76 typeof RuntimeSourceCatalogSyncService
83 >;77 >;
84 sourceAuthMaterialService: Effect.Effect.Success<
85 typeof RuntimeSourceAuthMaterialService
86 >;
87 sourceAuthService: Effect.Effect.Success<
88 typeof RuntimeSourceAuthServiceTag
89 >;
90 sourceCatalogStore: Effect.Effect.Success<78 sourceCatalogStore: Effect.Effect.Success<
91 typeof RuntimeSourceCatalogStoreService79 typeof RuntimeSourceCatalogStoreService
92 >;80 >;
@@ -127,8 +115,6 @@ export const createScopeExecutionEnvironmentResolver = · 34 unmodified lines
127 scopeConfigStore: input.scopeConfigStore,115 scopeConfigStore: input.scopeConfigStore,
128 scopeStateStore: input.scopeStateStore,116 scopeStateStore: input.scopeStateStore,
129 sourceArtifactStore: input.sourceArtifactStore,117 sourceArtifactStore: input.sourceArtifactStore,
130 sourceAuthMaterialService: input.sourceAuthMaterialService,
131 sourceAuthService: input.sourceAuthService,
132 runtimeLocalScope,118 runtimeLocalScope,
133 localToolRuntime,119 localToolRuntime,
134 createInternalToolMap: input.createInternalToolMap,120 createInternalToolMap: input.createInternalToolMap,
@@ -168,9 +154,6 @@ export const RuntimeExecutionResolverLive = ( · 33 unmodified lines
168 const sourceStore = yield* RuntimeSourceStoreService;154 const sourceStore = yield* RuntimeSourceStoreService;
169 const sourceCatalogSyncService =155 const sourceCatalogSyncService =
170 yield* RuntimeSourceCatalogSyncService;156 yield* RuntimeSourceCatalogSyncService;
171 const sourceAuthMaterialService =
172 yield* RuntimeSourceAuthMaterialService;
173 const sourceAuthService = yield* RuntimeSourceAuthServiceTag;
174 const sourceCatalogStore = yield* RuntimeSourceCatalogStoreService;157 const sourceCatalogStore = yield* RuntimeSourceCatalogStoreService;
175 const localToolRuntimeLoader = yield* LocalToolRuntimeLoaderService;158 const localToolRuntimeLoader = yield* LocalToolRuntimeLoaderService;
176 const installationStore = yield* InstallationStore;159 const installationStore = yield* InstallationStore;
@@ -186,8 +169,6 @@ export const RuntimeExecutionResolverLive = ( · 9 unmodified lines
186 executorStateStore,169 executorStateStore,
187 sourceStore,170 sourceStore,
188 sourceCatalogSyncService,171 sourceCatalogSyncService,
189 sourceAuthService,
190 sourceAuthMaterialService,
191 sourceCatalogStore,172 sourceCatalogStore,
192 localToolRuntimeLoader,173 localToolRuntimeLoader,
193 installationStore,174 installationStore,
packages/platform/sdk/src/runtime/execution/scope/tool-invoker.ts+7−26
@@ -14,9 +14,6 @@ import type { · 13 unmodified lines
14} from "#schema";14} from "#schema";
15import * as Effect from "effect/Effect";15import * as Effect from "effect/Effect";
16 16
17import {
18 RuntimeSourceAuthMaterialService,
19} from "../../auth/source-auth-material";
20import {17import {
21 RuntimeSourceCatalogStoreService,18 RuntimeSourceCatalogStoreService,
22} from "../../catalog/source/runtime";19} from "../../catalog/source/runtime";
@@ -42,9 +39,6 @@ import type { · 19 unmodified lines
42import type {39import type {
43 ExecutorStateStoreShape,40 ExecutorStateStoreShape,
44} from "../../executor-state-store";41} from "../../executor-state-store";
45import {
46 RuntimeSourceAuthService,
47} from "../../sources/source-auth-service";
48import {42import {
49 type RuntimeSourceStore,43 type RuntimeSourceStore,
50} from "../../sources/source-store";44} from "../../sources/source-store";
@@ -80,7 +74,6 @@ export type ScopeInternalToolContext = { · 29 unmodified lines
80 sourceCatalogSyncService: Effect.Effect.Success<74 sourceCatalogSyncService: Effect.Effect.Success<
81 typeof RuntimeSourceCatalogSyncService75 typeof RuntimeSourceCatalogSyncService
82 >;76 >;
83 sourceAuthService: RuntimeSourceAuthService;
84 installationStore: InstallationStoreShape;77 installationStore: InstallationStoreShape;
85 instanceConfigResolver: ResolveInstanceConfig;78 instanceConfigResolver: ResolveInstanceConfig;
86 storeSecretMaterial: StoreSecretMaterial;79 storeSecretMaterial: StoreSecretMaterial;
@@ -115,10 +108,6 @@ export const createScopeToolInvoker = (input: { · 28 unmodified lines
115 scopeConfigStore: ScopeConfigStoreShape;108 scopeConfigStore: ScopeConfigStoreShape;
116 scopeStateStore: ScopeStateStoreShape;109 scopeStateStore: ScopeStateStoreShape;
117 sourceArtifactStore: SourceArtifactStoreShape;110 sourceArtifactStore: SourceArtifactStoreShape;
118 sourceAuthMaterialService: Effect.Effect.Success<
119 typeof RuntimeSourceAuthMaterialService
120 >;
121 sourceAuthService: RuntimeSourceAuthService;
122 runtimeLocalScope: RuntimeLocalScopeState | null;111 runtimeLocalScope: RuntimeLocalScopeState | null;
123 localToolRuntime: LocalToolRuntime;112 localToolRuntime: LocalToolRuntime;
124 createInternalToolMap?: CreateScopeInternalToolMap;113 createInternalToolMap?: CreateScopeInternalToolMap;
@@ -140,7 +129,6 @@ export const createScopeToolInvoker = (input: { · 15 unmodified lines
140 const executorTools = createExecutorToolMap({129 const executorTools = createExecutorToolMap({
141 scopeId: input.scopeId,130 scopeId: input.scopeId,
142 actorScopeId: input.actorScopeId,131 actorScopeId: input.actorScopeId,
143 sourceAuthService: input.sourceAuthService,
144 installationStore: input.installationStore,132 installationStore: input.installationStore,
145 scopeConfigStore: input.scopeConfigStore,133 scopeConfigStore: input.scopeConfigStore,
146 scopeStateStore: input.scopeStateStore,134 scopeStateStore: input.scopeStateStore,
@@ -154,7 +142,6 @@ export const createScopeToolInvoker = (input: { · 7 unmodified lines
154 executorStateStore: input.executorStateStore,142 executorStateStore: input.executorStateStore,
155 sourceStore: input.sourceStore,143 sourceStore: input.sourceStore,
156 sourceCatalogSyncService: input.sourceCatalogSyncService,144 sourceCatalogSyncService: input.sourceCatalogSyncService,
157 sourceAuthService: input.sourceAuthService,
158 installationStore: input.installationStore,145 installationStore: input.installationStore,
159 instanceConfigResolver: input.instanceConfigResolver,146 instanceConfigResolver: input.instanceConfigResolver,
160 storeSecretMaterial: input.storeSecretMaterial,147 storeSecretMaterial: input.storeSecretMaterial,
@@ -232,16 +219,10 @@ export const createScopeToolInvoker = (input: { · 71 unmodified lines
232 onElicitation: input.onElicitation,219 onElicitation: input.onElicitation,
233 });220 });
234 221
235 const auth = yield* input.sourceAuthMaterialService.resolve({
236 source: catalogTool.source,
237 actorScopeId: input.actorScopeId,
238 context: toSecretResolutionContext(invocation.context),
239 });
240 return yield* invokeIrTool({222 return yield* invokeIrTool({
241 scopeId: input.scopeId,223 scopeId: input.scopeId,
242 actorScopeId: input.actorScopeId,224 actorScopeId: input.actorScopeId,
243 tool: catalogTool,225 tool: catalogTool,
244 auth,
245 args: invocation.args,226 args: invocation.args,
246 onElicitation: input.onElicitation,227 onElicitation: input.onElicitation,
247 context: invocation.context,228 context: invocation.context,
@@ -254,13 +235,13 @@ export const createScopeToolInvoker = (input: { · 6 unmodified lines
254 return {235 return {
255 catalog,236 catalog,
256 toolInvoker: {237 toolInvoker: {
257 invoke: ({ path, args, context }) =>238 invoke: ({ path, args, context }) => {
258 provideRuntimeLocalScope(239 const effect: Effect.Effect<unknown, unknown, never> = authoredToolPaths.has(path)
259 authoredToolPaths.has(path)240 ? authoredInvoker.invoke({ path, args, context }) as Effect.Effect<unknown, unknown, never>
260 ? authoredInvoker.invoke({ path, args, context })241 : invokePersistedTool({ path, args, context });
261 : invokePersistedTool({ path, args, context }),242
262 input.runtimeLocalScope,243 return provideRuntimeLocalScope(effect, input.runtimeLocalScope);
263 ),244 },
264 },245 },
265 };246 };
266};247};
packages/platform/sdk/src/runtime/executor-state-store.ts+0−120
@@ -2,16 +2,10 @@ import * as Context from "effect/Context"; · 1 unmodified line
2import type * as Effect from "effect/Effect";2import type * as Effect from "effect/Effect";
3 3
4import type {4import type {
5 AuthArtifact,
6 AuthLease,
7 Execution,5 Execution,
8 ExecutionInteraction,6 ExecutionInteraction,
9 ExecutionStep,7 ExecutionStep,
10 ProviderAuthGrant,
11 SecretMaterial,8 SecretMaterial,
12 SourceAuthSession,
13 ScopeOauthClient,
14 ScopedSourceOauthClient,
15} from "#schema";9} from "#schema";
16 10
17type SecretMaterialSummary = {11type SecretMaterialSummary = {
@@ -24,120 +18,6 @@ type SecretMaterialSummary = { · 6 unmodified lines
24};18};
25 19
26export type ExecutorStateStoreShape = {20export type ExecutorStateStoreShape = {
27 authArtifacts: {
28 listByScopeId: (
29 scopeId: AuthArtifact["scopeId"],
30 ) => Effect.Effect<readonly AuthArtifact[], Error, never>;
31 listByScopeAndSourceId: (input: {
32 scopeId: AuthArtifact["scopeId"];
33 sourceId: AuthArtifact["sourceId"];
34 }) => Effect.Effect<readonly AuthArtifact[], Error, never>;
35 getByScopeSourceAndActor: (input: {
36 scopeId: AuthArtifact["scopeId"];
37 sourceId: AuthArtifact["sourceId"];
38 actorScopeId: AuthArtifact["actorScopeId"];
39 slot: AuthArtifact["slot"];
40 }) => Effect.Effect<import("effect/Option").Option<AuthArtifact>, Error, never>;
41 upsert: (artifact: AuthArtifact) => Effect.Effect<void, Error, never>;
42 removeByScopeSourceAndActor: (input: {
43 scopeId: AuthArtifact["scopeId"];
44 sourceId: AuthArtifact["sourceId"];
45 actorScopeId: AuthArtifact["actorScopeId"];
46 slot?: AuthArtifact["slot"];
47 }) => Effect.Effect<boolean, Error, never>;
48 removeByScopeAndSourceId: (input: {
49 scopeId: AuthArtifact["scopeId"];
50 sourceId: AuthArtifact["sourceId"];
51 }) => Effect.Effect<number, Error, never>;
52 };
53 authLeases: {
54 listAll: () => Effect.Effect<readonly AuthLease[], Error, never>;
55 getByAuthArtifactId: (
56 authArtifactId: AuthLease["authArtifactId"],
57 ) => Effect.Effect<import("effect/Option").Option<AuthLease>, Error, never>;
58 upsert: (lease: AuthLease) => Effect.Effect<void, Error, never>;
59 removeByAuthArtifactId: (
60 authArtifactId: AuthLease["authArtifactId"],
61 ) => Effect.Effect<boolean, Error, never>;
62 };
63 sourceOauthClients: {
64 getByScopeSourceAndProvider: (input: {
65 scopeId: ScopedSourceOauthClient["scopeId"];
66 sourceId: ScopedSourceOauthClient["sourceId"];
67 providerKey: string;
68 }) => Effect.Effect<
69 import("effect/Option").Option<ScopedSourceOauthClient>,
70 Error,
71 never
72 >;
73 upsert: (
74 oauthClient: ScopedSourceOauthClient,
75 ) => Effect.Effect<void, Error, never>;
76 removeByScopeAndSourceId: (input: {
77 scopeId: ScopedSourceOauthClient["scopeId"];
78 sourceId: ScopedSourceOauthClient["sourceId"];
79 }) => Effect.Effect<number, Error, never>;
80 };
81 scopeOauthClients: {
82 listByScopeAndProvider: (input: {
83 scopeId: ScopeOauthClient["scopeId"];
84 providerKey: string;
85 }) => Effect.Effect<readonly ScopeOauthClient[], Error, never>;
86 getById: (
87 id: ScopeOauthClient["id"],
88 ) => Effect.Effect<import("effect/Option").Option<ScopeOauthClient>, Error, never>;
89 upsert: (oauthClient: ScopeOauthClient) => Effect.Effect<void, Error, never>;
90 removeById: (id: ScopeOauthClient["id"]) => Effect.Effect<boolean, Error, never>;
91 };
92 providerAuthGrants: {
93 listByScopeId: (
94 scopeId: ProviderAuthGrant["scopeId"],
95 ) => Effect.Effect<readonly ProviderAuthGrant[], Error, never>;
96 listByScopeActorAndProvider: (input: {
97 scopeId: ProviderAuthGrant["scopeId"];
98 actorScopeId: ProviderAuthGrant["actorScopeId"];
99 providerKey: string;
100 }) => Effect.Effect<readonly ProviderAuthGrant[], Error, never>;
101 getById: (
102 id: ProviderAuthGrant["id"],
103 ) => Effect.Effect<import("effect/Option").Option<ProviderAuthGrant>, Error, never>;
104 upsert: (grant: ProviderAuthGrant) => Effect.Effect<void, Error, never>;
105 removeById: (id: ProviderAuthGrant["id"]) => Effect.Effect<boolean, Error, never>;
106 };
107 sourceAuthSessions: {
108 listAll: () => Effect.Effect<readonly SourceAuthSession[], Error, never>;
109 listByScopeId: (
110 scopeId: SourceAuthSession["scopeId"],
111 ) => Effect.Effect<readonly SourceAuthSession[], Error, never>;
112 getById: (
113 id: SourceAuthSession["id"],
114 ) => Effect.Effect<import("effect/Option").Option<SourceAuthSession>, Error, never>;
115 getByState: (
116 state: SourceAuthSession["state"],
117 ) => Effect.Effect<import("effect/Option").Option<SourceAuthSession>, Error, never>;
118 getPendingByScopeSourceAndActor: (input: {
119 scopeId: SourceAuthSession["scopeId"];
120 sourceId: SourceAuthSession["sourceId"];
121 actorScopeId: SourceAuthSession["actorScopeId"];
122 credentialSlot?: SourceAuthSession["credentialSlot"];
123 }) => Effect.Effect<
124 import("effect/Option").Option<SourceAuthSession>,
125 Error,
126 never
127 >;
128 insert: (session: SourceAuthSession) => Effect.Effect<void, Error, never>;
129 update: (
130 id: SourceAuthSession["id"],
131 patch: Partial<
132 Omit<SourceAuthSession, "id" | "scopeId" | "sourceId" | "createdAt">
133 >,
134 ) => Effect.Effect<import("effect/Option").Option<SourceAuthSession>, Error, never>;
135 upsert: (session: SourceAuthSession) => Effect.Effect<void, Error, never>;
136 removeByScopeAndSourceId: (
137 scopeId: SourceAuthSession["scopeId"],
138 sourceId: SourceAuthSession["sourceId"],
139 ) => Effect.Effect<boolean, Error, never>;
140 };
141 secretMaterials: {21 secretMaterials: {
142 getById: (22 getById: (
143 id: SecretMaterial["id"],23 id: SecretMaterial["id"],
packages/platform/sdk/src/runtime/index.ts+8−62
@@ -10,9 +10,6 @@ import * as Effect from "effect/Effect"; · 9 unmodified lines
10import * as Layer from "effect/Layer";10import * as Layer from "effect/Layer";
11import * as ManagedRuntime from "effect/ManagedRuntime";11import * as ManagedRuntime from "effect/ManagedRuntime";
12 12
13import {
14 RuntimeSourceAuthMaterialLive,
15} from "./auth/source-auth-material";
16import {13import {
17 RuntimeSourceCatalogStoreService,14 RuntimeSourceCatalogStoreService,
18 RuntimeSourceCatalogStoreLive,15 RuntimeSourceCatalogStoreLive,
@@ -92,9 +89,6 @@ import { · 73 unmodified lines
92 ExecutorStateStore,89 ExecutorStateStore,
93 type ExecutorStateStoreShape,90 type ExecutorStateStoreShape,
94} from "./executor-state-store";91} from "./executor-state-store";
95import {
96 RuntimeSourceAuthServiceLive,
97} from "./sources/source-auth-service";
98import {92import {
99 RuntimeSourceStoreLive,93 RuntimeSourceStoreLive,
100} from "./sources/source-store";94} from "./sources/source-store";
@@ -105,7 +99,6 @@ export * from "./execution/live"; · 4 unmodified lines
105export * from "./catalog/schema-type-signature";99export * from "./catalog/schema-type-signature";
106export * from "./catalog/source/runtime";100export * from "./catalog/source/runtime";
107export * from "./catalog/source/sync";101export * from "./catalog/source/sync";
108export * from "./sources/source-auth-service";
109export * from "./sources/source-credential-interactions";102export * from "./sources/source-credential-interactions";
110export * from "./sources/source-store";103export * from "./sources/source-store";
111export * from "./executor-state-store";104export * from "./executor-state-store";
@@ -131,22 +124,13 @@ export type { · 19 unmodified lines
131 UpdateSecretMaterial,124 UpdateSecretMaterial,
132} from "./scope/secret-material-providers";125} from "./scope/secret-material-providers";
133export {126export {
134 builtInSourceAdapters,127 registeredSourcePlugins,
135 connectableSourceAdapters,128 hasRegisteredExternalSourcePlugins,
136 executorAddableSourceAdapters,129 getSourcePlugin,
137 localConfigurableSourceAdapters,130 getSourcePluginForSource,
138 hasRegisteredConnectableSourceAdapters,131 sourcePluginCatalogKind,
139 hasRegisteredExecutorAddableSourceAdapters,132 isInternalSourcePluginKind,
140 hasRegisteredExternalSourceAdapters,133} from "./sources/source-plugins";
141 getSourceAdapter,
142 getSourceAdapterForSource,
143 findSourceAdapterByProviderKey,
144 sourceBindingStateFromSource,
145 sourceAdapterCatalogKind,
146 sourceAdapterRequiresInteractiveConnect,
147 sourceAdapterUsesCredentialManagedAuth,
148 isInternalSourceAdapter,
149} from "./sources/source-adapters";
150 134
151export type ExecutorRuntimeOptions = {135export type ExecutorRuntimeOptions = {
152 executionResolver?: ResolveExecutionEnvironment;136 executionResolver?: ResolveExecutionEnvironment;
@@ -213,15 +197,6 @@ export type RuntimeSecretMaterialServices = { · 60 unmodified lines
213 update: UpdateSecretMaterial;197 update: UpdateSecretMaterial;
214};198};
215 199
216export type RuntimeAuthStorageServices = {
217 artifacts: ExecutorStateStoreShape["authArtifacts"];
218 leases: ExecutorStateStoreShape["authLeases"];
219 sourceOauthClients: ExecutorStateStoreShape["sourceOauthClients"];
220 scopeOauthClients: ExecutorStateStoreShape["scopeOauthClients"];
221 providerGrants: ExecutorStateStoreShape["providerAuthGrants"];
222 sourceSessions: ExecutorStateStoreShape["sourceAuthSessions"];
223};
224
225export const prewarmWorkspaceSourceCatalogToolIndex = (input: {200export const prewarmWorkspaceSourceCatalogToolIndex = (input: {
226 scopeId: LocalInstallation["scopeId"];201 scopeId: LocalInstallation["scopeId"];
227 actorScopeId: LocalInstallation["actorScopeId"];202 actorScopeId: LocalInstallation["actorScopeId"];
@@ -253,7 +228,6 @@ export type RuntimeStorageServices = { · 25 unmodified lines
253 scopeConfig: BoundScopeConfigStore;228 scopeConfig: BoundScopeConfigStore;
254 scopeState: BoundScopeStateStore;229 scopeState: BoundScopeStateStore;
255 sourceArtifacts: BoundSourceArtifactStore;230 sourceArtifacts: BoundSourceArtifactStore;
256 auth: RuntimeAuthStorageServices;
257 secrets: RuntimeSecretsStorageServices;231 secrets: RuntimeSecretsStorageServices;
258 executions: RuntimeExecutionStorageServices;232 executions: RuntimeExecutionStorageServices;
259 close?: () => Promise<void>;233 close?: () => Promise<void>;
@@ -327,12 +301,6 @@ const makeInstanceConfigLayer = (input: RuntimeInstanceConfigService) => · 67 unmodified lines
327const toExecutorStateStoreShape = (301const toExecutorStateStoreShape = (
328 input: RuntimeStorageServices,302 input: RuntimeStorageServices,
329): ExecutorStateStoreShape => ({303): ExecutorStateStoreShape => ({
330 authArtifacts: input.auth.artifacts,
331 authLeases: input.auth.leases,
332 sourceOauthClients: input.auth.sourceOauthClients,
333 scopeOauthClients: input.auth.scopeOauthClients,
334 providerAuthGrants: input.auth.providerGrants,
335 sourceAuthSessions: input.auth.sourceSessions,
336 secretMaterials: input.secrets,304 secretMaterials: input.secrets,
337 executions: input.executions.runs,305 executions: input.executions.runs,
338 executionInteractions: input.executions.interactions,306 executionInteractions: input.executions.interactions,
@@ -387,27 +355,9 @@ export const createExecutorRuntimeLayer = ( · 48 unmodified lines
387 Layer.provide(Layer.mergeAll(baseLayer, sourceStoreLayer)),355 Layer.provide(Layer.mergeAll(baseLayer, sourceStoreLayer)),
388 );356 );
389 357
390 const sourceAuthMaterialLayer = RuntimeSourceAuthMaterialLive.pipe(
391 Layer.provide(Layer.mergeAll(baseLayer, secretMaterialLayer)),
392 );
393
394 const sourceCatalogSyncLayer = RuntimeSourceCatalogSyncLive.pipe(358 const sourceCatalogSyncLayer = RuntimeSourceCatalogSyncLive.pipe(
395 Layer.provide(359 Layer.provide(
396 Layer.mergeAll(baseLayer, secretMaterialLayer, sourceAuthMaterialLayer),360 Layer.mergeAll(baseLayer, secretMaterialLayer),
397 ),
398 );
399
400 const sourceAuthLayer = RuntimeSourceAuthServiceLive({
401 getLocalServerBaseUrl: input.getLocalServerBaseUrl,
402 }).pipe(
403 Layer.provide(
404 Layer.mergeAll(
405 baseLayer,
406 instanceConfigLayer,
407 secretMaterialLayer,
408 sourceStoreLayer,
409 sourceCatalogSyncLayer,
410 ),
411 ),361 ),
412 );362 );
413 363
@@ -421,9 +371,7 @@ export const createExecutorRuntimeLayer = ( · 7 unmodified lines
421 instanceConfigLayer,371 instanceConfigLayer,
422 secretMaterialLayer,372 secretMaterialLayer,
423 sourceStoreLayer,373 sourceStoreLayer,
424 sourceAuthMaterialLayer,
425 sourceCatalogSyncLayer,374 sourceCatalogSyncLayer,
426 sourceAuthLayer,
427 sourceCatalogStoreLayer,375 sourceCatalogStoreLayer,
428 localToolRuntimeLayer,376 localToolRuntimeLayer,
429 ),377 ),
@@ -435,11 +383,9 @@ export const createExecutorRuntimeLayer = ( · 5 unmodified lines
435 instanceConfigLayer,383 instanceConfigLayer,
436 secretMaterialLayer,384 secretMaterialLayer,
437 sourceStoreLayer,385 sourceStoreLayer,
438 sourceAuthMaterialLayer,
439 sourceCatalogSyncLayer,386 sourceCatalogSyncLayer,
440 sourceCatalogStoreLayer,387 sourceCatalogStoreLayer,
441 localToolRuntimeLayer,388 localToolRuntimeLayer,
442 sourceAuthLayer,
443 executionResolverLayer,389 executionResolverLayer,
444 ) as ExecutorRuntimeLayer;390 ) as ExecutorRuntimeLayer;
445};391};
packages/platform/sdk/src/runtime/sources/executor-tools.ts+22−251
@@ -1,276 +1,47 @@
1import {1import type { ToolMap } from "@executor/codemode-core";
2 type ElicitationResponse,2import { SourceSchema } from "#schema";
3 toTool,
4 type ToolMap,
5 type ToolPath,
6} from "@executor/codemode-core";
7import {
8 type ScopeId,
9 ExecutionIdSchema,
10 ExecutionInteractionIdSchema,
11 SourceSchema,
12 type Source,
13} from "#schema";
14import * as Effect from "effect/Effect";
15import * as Cause from "effect/Cause";
16import * as Exit from "effect/Exit";
17import * as Schema from "effect/Schema";
18import type {
19 RuntimeLocalScopeState,
20} from "../scope/runtime-context";
21import {
22 type LocalStorageServices,
23 type InstallationStoreShape,
24 type SourceArtifactStoreShape,
25 type ScopeConfigStoreShape,
26 type ScopeStateStoreShape,
27 makeLocalStorageLayer,
28} from "../scope/storage";
29import {
30 provideOptionalRuntimeLocalScope,
31} from "../scope/runtime-context";
32
33/** Run an Effect as a Promise, preserving the original error (not FiberFailure). */
34const runEffect = async <A>(
35 effect: Effect.Effect<A, unknown, LocalStorageServices>,
36 storage: {
37 installationStore: InstallationStoreShape;
38 scopeConfigStore: ScopeConfigStoreShape;
39 scopeStateStore: ScopeStateStoreShape;
40 sourceArtifactStore: SourceArtifactStoreShape;
41 },
42 runtimeLocalScope: RuntimeLocalScopeState | null = null,
43): Promise<A> => {
44 const baseLayer = makeLocalStorageLayer(storage);
45 const exit = await Effect.runPromiseExit(
46 provideOptionalRuntimeLocalScope(
47 effect.pipe(Effect.provide(baseLayer)),
48 runtimeLocalScope,
49 ),
50 );
51 if (Exit.isSuccess(exit)) return exit.value;
52 throw Cause.squash(exit.cause);
53};
54 3
55import {
56 type ExecutorAddSourceInput,
57 type RuntimeSourceAuthService,
58} from "./source-auth-service";
59import {4import {
60 deriveSchemaJson,5 deriveSchemaJson,
61 deriveSchemaTypeSignature,6 deriveSchemaTypeSignature,
62} from "../catalog/schema-type-signature";7} from "../catalog/schema-type-signature";
63import {8import type {
64 ExecutorAddSourceInputSchema,9 InstallationStoreShape,
65 executorAddableSourceAdapters,10 SourceArtifactStoreShape,
66 hasRegisteredExecutorAddableSourceAdapters,11 ScopeConfigStoreShape,
67} from "./source-adapters";12 ScopeStateStoreShape,
68 13} from "../scope/storage";
69const ExecutorSourcesAddInputSchema = Schema.standardSchemaV1(
70 ExecutorAddSourceInputSchema,
71);
72
73const ExecutorSourcesAddOutputSchema = Schema.standardSchemaV1(SourceSchema);
74 14
75export const EXECUTOR_SOURCES_ADD_INPUT_HINT = hasRegisteredExecutorAddableSourceAdapters15export const EXECUTOR_SOURCES_ADD_INPUT_HINT =
76 ? deriveSchemaTypeSignature(16 "Source plugins are not registered in this build.";
77 ExecutorAddSourceInputSchema,
78 320,
79 )
80 : "Source plugins are not registered in this build.";
81 17
82export const EXECUTOR_SOURCES_ADD_OUTPUT_SIGNATURE = deriveSchemaTypeSignature(18export const EXECUTOR_SOURCES_ADD_OUTPUT_SIGNATURE = deriveSchemaTypeSignature(
83 SourceSchema,19 SourceSchema,
84 260,20 260,
85);21);
86 22
87export const EXECUTOR_SOURCES_ADD_INPUT_SCHEMA = hasRegisteredExecutorAddableSourceAdapters23export const EXECUTOR_SOURCES_ADD_INPUT_SCHEMA = {};
88 ? deriveSchemaJson(
89 ExecutorAddSourceInputSchema,
90 ) ?? {}
91 : {};
92 24
93export const EXECUTOR_SOURCES_ADD_OUTPUT_SCHEMA = deriveSchemaJson(25export const EXECUTOR_SOURCES_ADD_OUTPUT_SCHEMA = deriveSchemaJson(
94 SourceSchema,26 SourceSchema,
95) ?? {};27) ?? {};
96 28
97export const EXECUTOR_SOURCES_ADD_HELP_LINES = hasRegisteredExecutorAddableSourceAdapters29export const EXECUTOR_SOURCES_ADD_HELP_LINES = [
98 ? [30 "Source plugins are not registered in this build.",
99 "Source add input shapes:",31] as const;
100 ...executorAddableSourceAdapters.flatMap((adapter) =>
101 adapter.executorAddInputSchema
102 && adapter.executorAddInputSignatureWidth !== null
103 && adapter.executorAddHelpText
104 ? [
105 `- ${adapter.displayName}: ${deriveSchemaTypeSignature(adapter.executorAddInputSchema, adapter.executorAddInputSignatureWidth)}`,
106 ...adapter.executorAddHelpText.map((line) => ` ${line}`),
107 ]
108 : [],
109 ),
110 " executor handles the credential setup for you.",
111 ] as const
112 : [
113 "Source plugins are not registered in this build.",
114 ] as const;
115 32
116export const buildExecutorSourcesAddDescription = (): string =>33export const buildExecutorSourcesAddDescription = (): string =>
117 hasRegisteredExecutorAddableSourceAdapters34 "Source plugins are not registered in this build.";
118 ? [
119 "Add a registered source plugin to the current scope.",
120 ...EXECUTOR_SOURCES_ADD_HELP_LINES,
121 ].join("\n")
122 : "Source plugins are not registered in this build.";
123
124const toExecutionId = (value: unknown) => {
125 if (typeof value !== "string" || value.trim().length === 0) {
126 throw new Error("Missing execution run id for executor.sources.add");
127 }
128
129 return ExecutionIdSchema.make(value);
130};
131
132const asToolPath = (value: string): ToolPath => value as ToolPath;
133
134const toSerializableValue = <A>(value: A): A =>
135 JSON.parse(JSON.stringify(value)) as A;
136
137type ExecutorSourcesAddToolArgs = Omit<
138 ExecutorAddSourceInput,
139 "scopeId" | "actorScopeId" | "executionId" | "interactionId"
140>;
141
142const prepareExecutorAddSourceInput = (input: {
143 args: ExecutorSourcesAddToolArgs;
144 scopeId: ScopeId;
145 actorScopeId: ScopeId;
146 executionId: ReturnType<typeof toExecutionId>;
147 interactionId: ReturnType<typeof ExecutionInteractionIdSchema.make>;
148}): ExecutorAddSourceInput => ({
149 ...input.args,
150 scopeId: input.scopeId,
151 actorScopeId: input.actorScopeId,
152 executionId: input.executionId,
153 interactionId: input.interactionId,
154 } as ExecutorAddSourceInput);
155 35
156export const createExecutorToolMap = (input: {36export const createExecutorToolMap = (input: {
157 scopeId: ScopeId;37 scopeId?: string;
158 actorScopeId: ScopeId;38 actorScopeId?: string;
159 sourceAuthService: RuntimeSourceAuthService;
160 installationStore: InstallationStoreShape;39 installationStore: InstallationStoreShape;
161 scopeConfigStore: ScopeConfigStoreShape;40 scopeConfigStore: ScopeConfigStoreShape;
162 scopeStateStore: ScopeStateStoreShape;41 scopeStateStore: ScopeStateStoreShape;
163 sourceArtifactStore: SourceArtifactStoreShape;42 sourceArtifactStore: SourceArtifactStoreShape;
164 runtimeLocalScope: RuntimeLocalScopeState | null;43 runtimeLocalScope?: unknown;
165}): ToolMap =>44}): ToolMap => {
166 !hasRegisteredExecutorAddableSourceAdapters45 void input;
167 ? {}46 return {};
168 : ({47};
169 "executor.sources.add": toTool({
170 tool: {
171 description: buildExecutorSourcesAddDescription(),
172 inputSchema: ExecutorSourcesAddInputSchema,
173 outputSchema: ExecutorSourcesAddOutputSchema,
174 execute: async (args: ExecutorSourcesAddToolArgs, context): Promise<Source> => {
175 const executionId = toExecutionId(context?.invocation?.runId);
176 const interactionId = ExecutionInteractionIdSchema.make(
177 `executor.sources.add:${crypto.randomUUID()}`,
178 );
179 const preparedArgs = prepareExecutorAddSourceInput({
180 args,
181 scopeId: input.scopeId,
182 actorScopeId: input.actorScopeId,
183 executionId,
184 interactionId,
185 });
186 const result = await runEffect(
187 input.sourceAuthService.addExecutorSource(
188 preparedArgs,
189 context?.onElicitation
190 ? {
191 mcpDiscoveryElicitation: {
192 onElicitation: context.onElicitation,
193 path: context.path ?? asToolPath("executor.sources.add"),
194 sourceKey: context.sourceKey,
195 args,
196 metadata: context.metadata,
197 invocation: context.invocation,
198 },
199 }
200 : undefined,
201 ),
202 {
203 installationStore: input.installationStore,
204 scopeConfigStore: input.scopeConfigStore,
205 scopeStateStore: input.scopeStateStore,
206 sourceArtifactStore: input.sourceArtifactStore,
207 },
208 input.runtimeLocalScope,
209 );
210
211 if (result.kind === "connected") {
212 return toSerializableValue(result.source);
213 }
214 if (!context?.onElicitation || result.kind !== "oauth_required") {
215 throw new Error(
216 `executor.sources.add requires plugin-managed continuation support for ${result.source.id}`,
217 );
218 }
219
220 const response: ElicitationResponse = await runEffect(
221 context.onElicitation({
222 interactionId,
223 path: context.path ?? asToolPath("executor.sources.add"),
224 sourceKey: context.sourceKey,
225 args: preparedArgs,
226 metadata: context.metadata,
227 context: context.invocation,
228 elicitation: {
229 mode: "url",
230 message: `Open the provider sign-in page to connect ${result.source.name}`,
231 url: result.authorizationUrl,
232 elicitationId: result.sessionId,
233 },
234 }),
235 {
236 installationStore: input.installationStore,
237 scopeConfigStore: input.scopeConfigStore,
238 scopeStateStore: input.scopeStateStore,
239 sourceArtifactStore: input.sourceArtifactStore,
240 },
241 input.runtimeLocalScope,
242 );
243
244 if (response.action !== "accept") {
245 throw new Error(`Source add was not completed for ${result.source.id}`);
246 }
247
248 const connected = await runEffect(
249 input.sourceAuthService.getSourceById({
250 scopeId: input.scopeId,
251 sourceId: result.source.id,
252 actorScopeId: input.actorScopeId,
253 }),
254 {
255 installationStore: input.installationStore,
256 scopeConfigStore: input.scopeConfigStore,
257 scopeStateStore: input.scopeStateStore,
258 sourceArtifactStore: input.sourceArtifactStore,
259 },
260 input.runtimeLocalScope,
261 );
262 return toSerializableValue(connected);
263 },
264 },
265 metadata: {
266 contract: {
267 inputTypePreview: EXECUTOR_SOURCES_ADD_INPUT_HINT,
268 outputTypePreview: EXECUTOR_SOURCES_ADD_OUTPUT_SIGNATURE,
269 inputSchema: EXECUTOR_SOURCES_ADD_INPUT_SCHEMA,
270 outputSchema: EXECUTOR_SOURCES_ADD_OUTPUT_SCHEMA,
271 },
272 sourceKey: "executor",
273 interaction: "auto",
274 },
275 }),
276 });
packages/platform/sdk/src/runtime/sources/source-adapters/index.ts+0−64
@@ -1,64 +0,0 @@
1import * as Schema from "effect/Schema";
2
3import {
4 createSourceAdapterComposition,
5 type SourceAdapter,
6} from "@executor/source-core";
7import {
8 externalSourceAdapters,
9} from "@executor/source-builtins";
10
11import {
12 internalSourceAdapter,
13} from "./internal";
14
15type DisabledSourcePluginPayload = {
16 __sourcePluginRegistrationRequired?: true;
17 [key: string]: unknown;
18};
19
20export type * from "@executor/source-core";
21
22export const builtInSourceAdapters = [
23 ...externalSourceAdapters,
24 internalSourceAdapter,
25] as const satisfies readonly SourceAdapter[];
26const composition = createSourceAdapterComposition(builtInSourceAdapters);
27
28export const connectableSourceAdapters = composition.connectableSourceAdapters;
29export const ConnectSourcePayloadSchema =
30 composition.connectPayloadSchema as unknown as Schema.Schema<
31 typeof composition.connectPayloadSchema.Type | DisabledSourcePluginPayload,
32 any,
33 never
34 >;
35export type ConnectSourcePayload = typeof ConnectSourcePayloadSchema.Type;
36
37export const executorAddableSourceAdapters = composition.executorAddableSourceAdapters;
38export const ExecutorAddSourceInputSchema =
39 composition.executorAddInputSchema as unknown as Schema.Schema<
40 typeof composition.executorAddInputSchema.Type | DisabledSourcePluginPayload,
41 any,
42 never
43 >;
44export type ExecutorAddSourceInput = typeof ExecutorAddSourceInputSchema.Type;
45
46export const localConfigurableSourceAdapters = composition.localConfigurableSourceAdapters;
47export const hasRegisteredConnectableSourceAdapters =
48 connectableSourceAdapters.length > 0;
49export const hasRegisteredExecutorAddableSourceAdapters =
50 executorAddableSourceAdapters.length > 0;
51export const hasRegisteredExternalSourceAdapters =
52 hasRegisteredConnectableSourceAdapters
53 || hasRegisteredExecutorAddableSourceAdapters;
54
55export const getSourceAdapter = composition.getSourceAdapter;
56export const getSourceAdapterForSource = composition.getSourceAdapterForSource;
57export const findSourceAdapterByProviderKey = composition.findSourceAdapterByProviderKey;
58export const sourceBindingStateFromSource = composition.sourceBindingStateFromSource;
59export const sourceAdapterCatalogKind = composition.sourceAdapterCatalogKind;
60export const sourceAdapterRequiresInteractiveConnect =
61 composition.sourceAdapterRequiresInteractiveConnect;
62export const sourceAdapterUsesCredentialManagedAuth =
63 composition.sourceAdapterUsesCredentialManagedAuth;
64export const isInternalSourceAdapter = composition.isInternalSourceAdapter;
packages/platform/sdk/src/runtime/sources/source-adapters/internal.ts+0−131
@@ -1,131 +0,0 @@
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import {
4 createSourceCatalogSyncResult,
5 createCatalogImportMetadata,
6 decodeBindingConfig,
7 decodeSourceBindingPayload,
8 emptySourceBindingState,
9 encodeBindingConfig,
10 type Source,
11 type SourceAdapter,
12} from "@executor/source-core";
13import {
14 runtimeEffectError,
15} from "../../effect-errors";
16
17const InternalBindingConfigSchema = Schema.Struct({});
18
19const INTERNAL_BINDING_CONFIG_VERSION = 1;
20
21const bindingHasAnyField = (
22 value: unknown,
23 fields: readonly string[],
24): boolean =>
25 value !== null &&
26 typeof value === "object" &&
27 !Array.isArray(value) &&
28 fields.some((field) => Object.prototype.hasOwnProperty.call(value, field));
29
30const internalBindingConfigFromSource = (
31 source: Pick<Source, "id" | "bindingVersion" | "binding">,
32) =>
33 Effect.gen(function* () {
34 if (
35 bindingHasAnyField(source.binding, [
36 "specUrl",
37 "defaultHeaders",
38 "transport",
39 "queryParams",
40 "headers",
41 ])
42 ) {
43 return yield* runtimeEffectError("sources/source-adapters/internal", "internal sources cannot define HTTP source settings");
44 }
45
46 return yield* decodeSourceBindingPayload({
47 sourceId: source.id,
48 label: "internal",
49 version: source.bindingVersion,
50 expectedVersion: INTERNAL_BINDING_CONFIG_VERSION,
51 schema: InternalBindingConfigSchema,
52 value: source.binding,
53 allowedKeys: [],
54 });
55 });
56
57export const internalSourceAdapter = {
58 key: "internal",
59 displayName: "Internal",
60 catalogKind: "internal",
61 connectStrategy: "none",
62 credentialStrategy: "none",
63 bindingConfigVersion: INTERNAL_BINDING_CONFIG_VERSION,
64 providerKey: "generic_internal",
65 defaultImportAuthPolicy: "none",
66 connectPayloadSchema: null,
67 executorAddInputSchema: null,
68 executorAddHelpText: null,
69 executorAddInputSignatureWidth: null,
70 localConfigBindingSchema: null,
71 localConfigBindingFromSource: () => null,
72 serializeBindingConfig: (source) =>
73 encodeBindingConfig({
74 adapterKey: source.kind,
75 version: INTERNAL_BINDING_CONFIG_VERSION,
76 payloadSchema: InternalBindingConfigSchema,
77 payload: Effect.runSync(internalBindingConfigFromSource(source)),
78 }),
79 deserializeBindingConfig: ({ id, bindingConfigJson }) =>
80 Effect.map(
81 decodeBindingConfig({
82 sourceId: id,
83 label: "internal",
84 adapterKey: "internal",
85 version: INTERNAL_BINDING_CONFIG_VERSION,
86 payloadSchema: InternalBindingConfigSchema,
87 value: bindingConfigJson,
88 }),
89 ({ version, payload }) => ({
90 version,
91 payload,
92 }),
93 ),
94 bindingStateFromSource: () => Effect.succeed(emptySourceBindingState),
95 sourceConfigFromSource: (source) => ({
96 kind: "internal",
97 endpoint: source.endpoint,
98 }),
99 validateSource: (source) =>
100 Effect.gen(function* () {
101 yield* internalBindingConfigFromSource(source);
102
103 return {
104 ...source,
105 bindingVersion: INTERNAL_BINDING_CONFIG_VERSION,
106 binding: {},
107 };
108 }),
109 shouldAutoProbe: () => false,
110 syncCatalog: ({ source }) =>
111 Effect.succeed(
112 createSourceCatalogSyncResult({
113 fragment: {
114 version: "ir.v1.fragment",
115 },
116 importMetadata: {
117 ...createCatalogImportMetadata({
118 source,
119 adapterKey: "internal",
120 }),
121 importerVersion: "ir.v1.internal",
122 sourceConfigHash: "internal",
123 },
124 sourceHash: null,
125 }),
126 ),
127 invoke: () =>
128 Effect.fail(
129 runtimeEffectError("sources/source-adapters/internal", "Internal sources do not support persisted adapter invocation"),
130 ),
131} satisfies SourceAdapter;
packages/platform/sdk/src/runtime/sources/source-auth-service.ts+0−219
@@ -1,219 +0,0 @@
1import type {
2 CredentialSlot,
3 SecretRef,
4} from "@executor/source-core";
5import {
6 type ScopeId,
7 type Source,
8 type SourceAuthSession,
9 SourceAuthSessionIdSchema,
10 SourceSchema,
11} from "#schema";
12import * as Context from "effect/Context";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import * as Schema from "effect/Schema";
16
17import {
18 LiveExecutionManagerService,
19 type LiveExecutionManager,
20} from "../execution/live";
21import {
22 getRuntimeLocalScopeOption,
23 provideOptionalRuntimeLocalScope,
24 type RuntimeLocalScopeState,
25} from "../scope/runtime-context";
26import {
27 type DeleteSecretMaterial,
28 type ResolveSecretMaterial,
29 SecretMaterialDeleterService,
30 SecretMaterialResolverService,
31 SecretMaterialStorerService,
32 type StoreSecretMaterial,
33} from "../scope/secret-material-providers";
34import {
35 RuntimeSourceCatalogSyncService,
36 type RuntimeSourceCatalogSyncShape,
37} from "../catalog/source/sync";
38import {
39 type RuntimeSourceStore,
40 RuntimeSourceStoreService,
41} from "./source-store";
42import type {
43 ScopeStorageServices,
44} from "../scope/storage";
45import {
46 ExecutorStateStore,
47 type ExecutorStateStoreShape,
48} from "../executor-state-store";
49import {
50 runtimeEffectError,
51} from "../effect-errors";
52import type {
53 ExecutorAddSourceInput as RegisteredExecutorAddSourceInput,
54} from "./source-adapters";
55
56const SOURCE_PLUGINS_REMOVED_MESSAGE =
57 "Source plugins and legacy source adapters have been removed from this build.";
58
59const sourcePluginsRemovedError = (
60 operation: string,
61) => runtimeEffectError("sources/source-auth-service", `${operation}: ${SOURCE_PLUGINS_REMOVED_MESSAGE}`);
62
63const disabledSourcePlugins = <A>(
64 operation: string,
65): Effect.Effect<A, Error, never> =>
66 Effect.fail(sourcePluginsRemovedError(operation));
67
68export type ExecutorSourceAddResult =
69 | {
70 kind: "connected";
71 source: Source;
72 }
73 | {
74 kind: "credential_required";
75 source: Source;
76 credentialSlot: CredentialSlot;
77 }
78 | {
79 kind: "oauth_required";
80 source: Source;
81 sessionId: SourceAuthSession["id"];
82 authorizationUrl: string;
83 };
84
85export type ExecutorAddSourceInput = RegisteredExecutorAddSourceInput & {
86 scopeId: ScopeId;
87 actorScopeId?: ScopeId | null;
88 executionId: SourceAuthSession["executionId"];
89 interactionId: SourceAuthSession["interactionId"];
90};
91
92export type CompleteSourceCredentialSetupResult = {
93 sessionId: SourceAuthSession["id"];
94 source: Source;
95};
96
97type RuntimeSourceAuthServiceShape = {
98 getSourceById: (input: {
99 scopeId: ScopeId;
100 sourceId: Source["id"];
101 actorScopeId?: ScopeId | null;
102 }) => Effect.Effect<Source, Error, ScopeStorageServices>;
103 getLocalServerBaseUrl: () => string | null;
104 storeSecretMaterial: (input: {
105 purpose: Parameters<StoreSecretMaterial>[0]["purpose"];
106 value: string;
107 }) => Effect.Effect<SecretRef, Error, never>;
108 addExecutorSource: (
109 input: ExecutorAddSourceInput,
110 options?: {
111 mcpDiscoveryElicitation?: unknown;
112 baseUrl?: string | null;
113 },
114 ) => Effect.Effect<ExecutorSourceAddResult, Error, ScopeStorageServices>;
115 completeSourceCredentialSetup: (input: {
116 scopeId: ScopeId;
117 sourceId: Source["id"];
118 actorScopeId?: ScopeId | null;
119 state: string;
120 code?: string | null;
121 error?: string | null;
122 errorDescription?: string | null;
123 }) => Effect.Effect<CompleteSourceCredentialSetupResult, Error, ScopeStorageServices>;
124};
125
126type RuntimeSourceAuthDependencies = {
127 executorState: ExecutorStateStoreShape;
128 liveExecutionManager: LiveExecutionManager;
129 sourceStore: RuntimeSourceStore;
130 sourceCatalogSync: RuntimeSourceCatalogSyncShape;
131 resolveSecretMaterial: ResolveSecretMaterial;
132 storeSecretMaterial: StoreSecretMaterial;
133 deleteSecretMaterial: DeleteSecretMaterial;
134 getLocalServerBaseUrl?: () => string | undefined;
135 localScopeState?: RuntimeLocalScopeState;
136};
137
138export const createRuntimeSourceAuthService = (
139 input: RuntimeSourceAuthDependencies,
140): RuntimeSourceAuthService => {
141 const provideLocalWorkspace = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
142 provideOptionalRuntimeLocalScope(effect, input.localScopeState);
143
144 return {
145 getSourceById: ({ scopeId, sourceId, actorScopeId }) =>
146 provideLocalWorkspace(
147 input.sourceStore.loadSourceById({
148 scopeId,
149 sourceId,
150 actorScopeId,
151 }),
152 ),
153 getLocalServerBaseUrl: () => input.getLocalServerBaseUrl?.() ?? null,
154 storeSecretMaterial: ({ purpose, value }) =>
155 input.storeSecretMaterial({
156 purpose,
157 value,
158 }),
159 addExecutorSource: () =>
160 provideLocalWorkspace(disabledSourcePlugins("addExecutorSource")),
161 completeSourceCredentialSetup: () =>
162 provideLocalWorkspace(disabledSourcePlugins("completeSourceCredentialSetup")),
163 };
164};
165
166export type RuntimeSourceAuthService = RuntimeSourceAuthServiceShape;
167
168export class RuntimeSourceAuthServiceTag extends Context.Tag(
169 "#runtime/RuntimeSourceAuthServiceTag",
170)<RuntimeSourceAuthServiceTag, RuntimeSourceAuthService>() {}
171
172export const RuntimeSourceAuthServiceLive = (input: {
173 getLocalServerBaseUrl?: () => string | undefined;
174} = {}) =>
175 Layer.effect(
176 RuntimeSourceAuthServiceTag,
177 Effect.gen(function* () {
178 const executorState = yield* ExecutorStateStore;
179 const liveExecutionManager = yield* LiveExecutionManagerService;
180 const sourceStore = yield* RuntimeSourceStoreService;
181 const sourceCatalogSync = yield* RuntimeSourceCatalogSyncService;
182 const resolveSecretMaterial = yield* SecretMaterialResolverService;
183 const storeSecretMaterial = yield* SecretMaterialStorerService;
184 const deleteSecretMaterial = yield* SecretMaterialDeleterService;
185 const runtimeLocalScope = yield* getRuntimeLocalScopeOption();
186
187 return createRuntimeSourceAuthService({
188 executorState,
189 liveExecutionManager,
190 sourceStore,
191 sourceCatalogSync,
192 resolveSecretMaterial,
193 storeSecretMaterial,
194 deleteSecretMaterial,
195 getLocalServerBaseUrl: input.getLocalServerBaseUrl,
196 localScopeState: runtimeLocalScope ?? undefined,
197 });
198 }),
199 );
200
201export const ExecutorAddSourceResultSchema = Schema.Union(
202 Schema.Struct({
203 kind: Schema.Literal("connected"),
204 source: SourceSchema,
205 }),
206 Schema.Struct({
207 kind: Schema.Literal("credential_required"),
208 source: SourceSchema,
209 credentialSlot: Schema.Literal("runtime", "import"),
210 }),
211 Schema.Struct({
212 kind: Schema.Literal("oauth_required"),
213 source: SourceSchema,
214 sessionId: SourceAuthSessionIdSchema,
215 authorizationUrl: Schema.String,
216 }),
217);
218
219export type ExecutorAddSourceResult = typeof ExecutorAddSourceResultSchema.Type;
packages/platform/sdk/src/runtime/sources/source-definitions.ts+54−430
@@ -1,44 +1,22 @@
1import {1import { sha256Hex } from "@executor/codemode-core";
2 sha256Hex,
3} from "@executor/codemode-core";
4 2
5import type {
6 CreateSourcePayload,
7 UpdateSourcePayload,
8} from "../../sources/contracts";
9import type {3import type {
10 ScopeId,4 ScopeId,
11 AuthArtifact,
12 Source,5 Source,
13 SourceAuth,
14 SourceImportAuthPolicy,
15 SourceCatalogAdapterKey,
16 SourceCatalogId,6 SourceCatalogId,
17 SourceCatalogKind,7 SourceCatalogKind,
8 SourceCatalogPluginKey,
18 SourceCatalogRevisionId,9 SourceCatalogRevisionId,
19 StoredSourceRecord,
20 StoredSourceCatalogRecord,10 StoredSourceCatalogRecord,
21 StoredSourceCatalogRevisionRecord,11 StoredSourceCatalogRevisionRecord,
22} from "#schema";12} from "#schema";
23import {13import {
24 AuthArtifactIdSchema,
25 ProviderAuthGrantIdSchema,
26 SourceCatalogIdSchema,14 SourceCatalogIdSchema,
27 SourceCatalogRevisionIdSchema,15 SourceCatalogRevisionIdSchema,
28} from "#schema";16} from "#schema";
29import * as Effect from "effect/Effect";17import * as Effect from "effect/Effect";
30 18
31import {19import { getSourcePluginForSource } from "./source-plugins";
32 getSourceAdapter,
33 getSourceAdapterForSource,
34} from "./source-adapters";
35import {
36 authArtifactFromSourceAuth,
37 sourceAuthFromAuthArtifact,
38} from "../auth/auth-artifacts";
39import {
40 runtimeEffectError,
41} from "../effect-errors";
42 20
43const trimOrNull = (value: string | null | undefined): string | null => {21const trimOrNull = (value: string | null | undefined): string | null => {
44 if (value === null || value === undefined) {22 if (value === null || value === undefined) {
@@ -52,20 +30,19 @@ const trimOrNull = (value: string | null | undefined): string | null => { · 7 unmodified lines
52type SourceCatalogSourceConfig = Record<string, unknown>;30type SourceCatalogSourceConfig = Record<string, unknown>;
53 31
54const sourceConfigFromSource = (source: Source): SourceCatalogSourceConfig =>32const sourceConfigFromSource = (source: Source): SourceCatalogSourceConfig =>
55 getSourceAdapterForSource(source).sourceConfigFromSource(source);33 getSourcePluginForSource(source).catalogIdentity?.({
34 source,
35 }) ?? {
36 kind: source.kind,
37 endpoint: source.endpoint,
38 namespace: source.namespace,
39 };
56 40
57const sourceCatalogKindFromSource = (source: Source): SourceCatalogKind => {41const sourceCatalogKindFromSource = (source: Source): SourceCatalogKind =>
58 const adapter = getSourceAdapterForSource(source);42 getSourcePluginForSource(source).catalogKind;
59 return adapter.catalogKind;
60};
61 43
62const sourceCatalogAdapterKeyFromSource = (source: Source): SourceCatalogAdapterKey => {44const sourceCatalogPluginKeyFromSource = (source: Source): SourceCatalogPluginKey =>
63 return getSourceAdapterForSource(source).key;45 getSourcePluginForSource(source).kind;
64};
65
66const sourceCatalogProviderKeyFromSource = (source: Source): string => {
67 return getSourceAdapterForSource(source).providerKey;
68};
69 46
70const stableHash = (value: string): string =>47const stableHash = (value: string): string =>
71 sha256Hex(value).slice(0, 24);48 sha256Hex(value).slice(0, 24);
@@ -73,13 +50,21 @@ const stableHash = (value: string): string => · 1 unmodified line
73const sourceCatalogSignature = (source: Source): string =>50const sourceCatalogSignature = (source: Source): string =>
74 JSON.stringify({51 JSON.stringify({
75 catalogKind: sourceCatalogKindFromSource(source),52 catalogKind: sourceCatalogKindFromSource(source),
76 adapterKey: sourceCatalogAdapterKeyFromSource(source),53 pluginKey: sourceCatalogPluginKeyFromSource(source),
77 providerKey: sourceCatalogProviderKeyFromSource(source),54 sourceId: source.id,
78 sourceConfig: sourceConfigFromSource(source),55 sourceConfig: sourceConfigFromSource(source),
79 });56 });
80 57
81export const sourceConfigSignature = (source: Source): string =>58export const sourceConfigSignature = (source: Source): string =>
82 JSON.stringify(sourceConfigFromSource(source));59 JSON.stringify({
60 sourceId: source.id,
61 kind: source.kind,
62 name: source.name,
63 endpoint: source.endpoint,
64 namespace: source.namespace,
65 enabled: source.enabled,
66 updatedAt: source.updatedAt,
67 });
83 68
84export const stableSourceCatalogId = (source: Source): SourceCatalogId =>69export const stableSourceCatalogId = (source: Source): SourceCatalogId =>
85 SourceCatalogIdSchema.make(`src_catalog_${stableHash(sourceCatalogSignature(source))}`);70 SourceCatalogIdSchema.make(`src_catalog_${stableHash(sourceCatalogSignature(source))}`);
@@ -89,280 +74,45 @@ export const stableSourceCatalogRevisionId = ( · 3 unmodified lines
89): SourceCatalogRevisionId =>74): SourceCatalogRevisionId =>
90 SourceCatalogRevisionIdSchema.make(`src_catalog_rev_${stableHash(sourceConfigSignature(source))}`);75 SourceCatalogRevisionIdSchema.make(`src_catalog_rev_${stableHash(sourceConfigSignature(source))}`);
91 76
92const normalizeAuth = (
93 auth: SourceAuth | undefined,
94): Effect.Effect<SourceAuth, Error, never> =>
95 Effect.gen(function* () {
96 if (auth === undefined || auth.kind === "none") {
97 return { kind: "none" } satisfies SourceAuth;
98 }
99
100 if (auth.kind === "bearer") {
101 const headerName = trimOrNull(auth.headerName) ?? "Authorization";
102 const prefix = auth.prefix ?? "Bearer ";
103 const providerId = trimOrNull(auth.token.providerId);
104 const handle = trimOrNull(auth.token.handle);
105 if (providerId === null || handle === null) {
106 return yield* runtimeEffectError("sources/source-definitions", "Bearer auth requires a token secret ref");
107 }
108
109 return {
110 kind: "bearer",
111 headerName,
112 prefix,
113 token: {
114 providerId,
115 handle,
116 },
117 } satisfies SourceAuth;
118 }
119
120 if (auth.kind === "oauth2_authorized_user") {
121 const headerName = trimOrNull(auth.headerName) ?? "Authorization";
122 const prefix = auth.prefix ?? "Bearer ";
123 const refreshProviderId = trimOrNull(auth.refreshToken.providerId);
124 const refreshHandle = trimOrNull(auth.refreshToken.handle);
125 if (refreshProviderId === null || refreshHandle === null) {
126 return yield* runtimeEffectError("sources/source-definitions", "OAuth2 authorized-user auth requires a refresh token secret ref");
127 }
128
129 let clientSecret: { providerId: string; handle: string } | null = null;
130 if (auth.clientSecret !== null) {
131 const clientSecretProviderId = trimOrNull(auth.clientSecret.providerId);
132 const clientSecretHandle = trimOrNull(auth.clientSecret.handle);
133 if (clientSecretProviderId === null || clientSecretHandle === null) {
134 return yield* runtimeEffectError("sources/source-definitions", "OAuth2 authorized-user client secret ref must include providerId and handle");
135 }
136 clientSecret = {
137 providerId: clientSecretProviderId,
138 handle: clientSecretHandle,
139 };
140 }
141
142 const tokenEndpoint = trimOrNull(auth.tokenEndpoint);
143 const clientId = trimOrNull(auth.clientId);
144 if (tokenEndpoint === null || clientId === null) {
145 return yield* runtimeEffectError("sources/source-definitions", "OAuth2 authorized-user auth requires tokenEndpoint and clientId");
146 }
147
148 return {
149 kind: "oauth2_authorized_user",
150 headerName,
151 prefix,
152 tokenEndpoint,
153 clientId,
154 clientAuthentication: auth.clientAuthentication,
155 clientSecret,
156 refreshToken: {
157 providerId: refreshProviderId,
158 handle: refreshHandle,
159 },
160 grantSet: auth.grantSet ?? null,
161 } satisfies SourceAuth;
162 }
163
164 if (auth.kind === "provider_grant_ref") {
165 const headerName = trimOrNull(auth.headerName) ?? "Authorization";
166 const prefix = auth.prefix ?? "Bearer ";
167 const grantId = trimOrNull(auth.grantId);
168 if (grantId === null) {
169 return yield* runtimeEffectError("sources/source-definitions", "Provider grant auth requires a grantId");
170 }
171
172 return {
173 kind: "provider_grant_ref",
174 grantId: ProviderAuthGrantIdSchema.make(grantId),
175 providerKey: trimOrNull(auth.providerKey) ?? "",
176 requiredScopes: auth.requiredScopes
177 .map((scope) => scope.trim())
178 .filter((scope) => scope.length > 0),
179 headerName,
180 prefix,
181 } satisfies SourceAuth;
182 }
183
184 if (auth.kind === "mcp_oauth") {
185 const redirectUri = trimOrNull(auth.redirectUri);
186 const accessProviderId = trimOrNull(auth.accessToken.providerId);
187 const accessHandle = trimOrNull(auth.accessToken.handle);
188 if (redirectUri === null || accessProviderId === null || accessHandle === null) {
189 return yield* runtimeEffectError("sources/source-definitions", "MCP OAuth auth requires redirectUri and access token secret ref");
190 }
191
192 let refreshToken: { providerId: string; handle: string } | null = null;
193 if (auth.refreshToken !== null) {
194 const refreshProviderId = trimOrNull(auth.refreshToken.providerId);
195 const refreshHandle = trimOrNull(auth.refreshToken.handle);
196 if (refreshProviderId === null || refreshHandle === null) {
197 return yield* runtimeEffectError("sources/source-definitions", "MCP OAuth refresh token ref must include providerId and handle");
198 }
199
200 refreshToken = {
201 providerId: refreshProviderId,
202 handle: refreshHandle,
203 };
204 }
205
206 const tokenType = trimOrNull(auth.tokenType) ?? "Bearer";
207
208 return {
209 kind: "mcp_oauth",
210 redirectUri,
211 accessToken: {
212 providerId: accessProviderId,
213 handle: accessHandle,
214 },
215 refreshToken,
216 tokenType,
217 expiresIn: auth.expiresIn ?? null,
218 scope: trimOrNull(auth.scope),
219 resourceMetadataUrl: trimOrNull(auth.resourceMetadataUrl),
220 authorizationServerUrl: trimOrNull(auth.authorizationServerUrl),
221 resourceMetadataJson: trimOrNull(auth.resourceMetadataJson),
222 authorizationServerMetadataJson: trimOrNull(auth.authorizationServerMetadataJson),
223 clientInformationJson: trimOrNull(auth.clientInformationJson),
224 } satisfies SourceAuth;
225 }
226
227 const headerName = trimOrNull(auth.headerName) ?? "Authorization";
228 const prefix = auth.prefix ?? "Bearer ";
229 const accessProviderId = trimOrNull(auth.accessToken.providerId);
230 const accessHandle = trimOrNull(auth.accessToken.handle);
231 if (accessProviderId === null || accessHandle === null) {
232 return yield* runtimeEffectError("sources/source-definitions", "OAuth2 auth requires an access token secret ref");
233 }
234
235 let refreshToken: { providerId: string; handle: string } | null = null;
236 if (auth.refreshToken !== null) {
237 const refreshProviderId = trimOrNull(auth.refreshToken.providerId);
238 const refreshHandle = trimOrNull(auth.refreshToken.handle);
239 if (refreshProviderId === null || refreshHandle === null) {
240 return yield* runtimeEffectError("sources/source-definitions", "OAuth2 refresh token ref must include providerId and handle");
241 }
242
243 refreshToken = {
244 providerId: refreshProviderId,
245 handle: refreshHandle,
246 };
247 }
248
249 return {
250 kind: "oauth2",
251 headerName,
252 prefix,
253 accessToken: {
254 providerId: accessProviderId,
255 handle: accessHandle,
256 },
257 refreshToken,
258 } satisfies SourceAuth;
259 });
260
261const normalizeImportAuthPolicy = (
262 sourceKind: Source["kind"],
263 policy: SourceImportAuthPolicy | undefined,
264): SourceImportAuthPolicy => policy ?? getSourceAdapter(sourceKind).defaultImportAuthPolicy;
265
266const validateSourceImportAuth = (source: Source): Effect.Effect<Source, Error, never> =>
267 Effect.gen(function* () {
268 if (source.importAuthPolicy !== "separate" && source.importAuth.kind !== "none") {
269 return yield* runtimeEffectError("sources/source-definitions", "importAuth must be none unless importAuthPolicy is separate");
270 }
271
272 return source;
273 });
274
275const validateSourceByKind = (source: Source): Effect.Effect<Source, Error, never> =>77const validateSourceByKind = (source: Source): Effect.Effect<Source, Error, never> =>
276 Effect.flatMap(78 Effect.succeed(source);
277 validateSourceImportAuth(source),
278 (validated) =>
279 Effect.map(
280 getSourceAdapterForSource(validated).validateSource(validated),
281 (result) => result as Source,
282 ),
283 );
284 79
285export const createSourceFromPayload = (input: {80export const normalizeSourceForCreate = (input: {
286 scopeId: ScopeId;81 scopeId: ScopeId;
287 sourceId: Source["id"];82 sourceId: Source["id"];
288 payload: CreateSourcePayload;83 source: Omit<
84 Source,
85 "id" | "scopeId" | "createdAt" | "updatedAt"
86 >;
289 now: number;87 now: number;
290}): Effect.Effect<Source, Error, never> =>88}): Effect.Effect<Source, Error, never> =>
291 Effect.gen(function* () {89 validateSourceByKind({
292 const auth = yield* normalizeAuth(input.payload.auth);90 id: input.sourceId,
293 const importAuth = yield* normalizeAuth(input.payload.importAuth);91 scopeId: input.scopeId,
294 const importAuthPolicy = normalizeImportAuthPolicy(92 name: input.source.name.trim(),
295 input.payload.kind,93 kind: input.source.kind,
296 input.payload.importAuthPolicy,94 endpoint: input.source.endpoint.trim(),
297 );95 status: input.source.status,
298 96 enabled: input.source.enabled,
299 return yield* validateSourceByKind({97 namespace: trimOrNull(input.source.namespace),
300 id: input.sourceId,98 sourceHash: trimOrNull(input.source.sourceHash),
301 scopeId: input.scopeId,99 lastError: trimOrNull(input.source.lastError),
302 name: input.payload.name.trim(),100 createdAt: input.now,
303 kind: input.payload.kind,101 updatedAt: input.now,
304 endpoint: input.payload.endpoint.trim(),
305 status: input.payload.status ?? "draft",
306 enabled: input.payload.enabled ?? true,
307 namespace: trimOrNull(input.payload.namespace),
308 bindingVersion: getSourceAdapter(input.payload.kind).bindingConfigVersion,
309 binding: input.payload.binding ?? {},
310 importAuthPolicy,
311 importAuth,
312 auth,
313 sourceHash: trimOrNull(input.payload.sourceHash),
314 lastError: trimOrNull(input.payload.lastError),
315 createdAt: input.now,
316 updatedAt: input.now,
317 });
318 });102 });
319 103
320export const updateSourceFromPayload = (input: {104export const normalizeSourceForSave = (input: {
321 source: Source;105 source: Source;
322 payload: UpdateSourcePayload;
323 now: number;106 now: number;
324}): Effect.Effect<Source, Error, never> =>107}): Effect.Effect<Source, Error, never> =>
325 Effect.gen(function* () {108 validateSourceByKind({
326 const nextAuth = input.payload.auth === undefined109 ...input.source,
327 ? input.source.auth110 name: input.source.name.trim(),
328 : yield* normalizeAuth(input.payload.auth);111 endpoint: input.source.endpoint.trim(),
329 const nextImportAuth = input.payload.importAuth === undefined112 namespace: trimOrNull(input.source.namespace),
330 ? input.source.importAuth113 sourceHash: trimOrNull(input.source.sourceHash),
331 : yield* normalizeAuth(input.payload.importAuth);114 lastError: trimOrNull(input.source.lastError),
332 const nextImportAuthPolicy = normalizeImportAuthPolicy(115 updatedAt: input.now,
333 input.source.kind,
334 input.payload.importAuthPolicy ?? input.source.importAuthPolicy,
335 );
336
337 return yield* validateSourceByKind({
338 ...input.source,
339 name: input.payload.name !== undefined ? input.payload.name.trim() : input.source.name,
340 endpoint:
341 input.payload.endpoint !== undefined
342 ? input.payload.endpoint.trim()
343 : input.source.endpoint,
344 status: input.payload.status ?? input.source.status,
345 enabled: input.payload.enabled ?? input.source.enabled,
346 namespace: input.payload.namespace !== undefined
347 ? trimOrNull(input.payload.namespace)
348 : input.source.namespace,
349 bindingVersion: input.payload.binding !== undefined
350 ? getSourceAdapter(input.source.kind).bindingConfigVersion
351 : input.source.bindingVersion,
352 binding: input.payload.binding !== undefined
353 ? input.payload.binding
354 : input.source.binding,
355 importAuthPolicy: nextImportAuthPolicy,
356 importAuth: nextImportAuth,
357 auth: nextAuth,
358 sourceHash: input.payload.sourceHash !== undefined
359 ? trimOrNull(input.payload.sourceHash)
360 : input.source.sourceHash,
361 lastError: input.payload.lastError !== undefined
362 ? trimOrNull(input.payload.lastError)
363 : input.source.lastError,
364 updatedAt: input.now,
365 });
366 });116 });
367 117
368export const createSourceCatalogRecord = (input: {118export const createSourceCatalogRecord = (input: {
@@ -372,8 +122,7 @@ export const createSourceCatalogRecord = (input: { · 3 unmodified lines
372}): StoredSourceCatalogRecord => ({122}): StoredSourceCatalogRecord => ({
373 id: input.catalogId ?? stableSourceCatalogId(input.source),123 id: input.catalogId ?? stableSourceCatalogId(input.source),
374 kind: sourceCatalogKindFromSource(input.source),124 kind: sourceCatalogKindFromSource(input.source),
375 adapterKey: sourceCatalogAdapterKeyFromSource(input.source),125 pluginKey: sourceCatalogPluginKeyFromSource(input.source),
376 providerKey: sourceCatalogProviderKeyFromSource(input.source),
377 name: input.source.name,126 name: input.source.name,
378 summary: null,127 summary: null,
379 visibility: "scope",128 visibility: "scope",
@@ -403,128 +152,3 @@ export const createSourceCatalogRevisionRecord = (input: { · 23 unmodified lines
403 createdAt: input.source.createdAt,152 createdAt: input.source.createdAt,
404 updatedAt: input.source.updatedAt,153 updatedAt: input.source.updatedAt,
405});154});
406
407export const splitSourceForStorage = (input: {
408 source: Source;
409 catalogId: SourceCatalogId;
410 catalogRevisionId: SourceCatalogRevisionId;
411 actorScopeId?: ScopeId | null;
412 existingRuntimeAuthArtifactId?: AuthArtifact["id"] | null;
413 existingImportAuthArtifactId?: AuthArtifact["id"] | null;
414}): {
415 sourceRecord: StoredSourceRecord;
416 runtimeAuthArtifact: AuthArtifact | null;
417 importAuthArtifact: AuthArtifact | null;
418} => {
419 const sourceRecord: StoredSourceRecord = {
420 id: input.source.id,
421 scopeId: input.source.scopeId,
422 catalogId: input.catalogId,
423 catalogRevisionId: input.catalogRevisionId,
424 name: input.source.name,
425 kind: input.source.kind,
426 endpoint: input.source.endpoint,
427 status: input.source.status,
428 enabled: input.source.enabled,
429 namespace: input.source.namespace,
430 importAuthPolicy: input.source.importAuthPolicy,
431 bindingConfigJson: getSourceAdapterForSource(input.source).serializeBindingConfig(input.source),
432 sourceHash: input.source.sourceHash,
433 lastError: input.source.lastError,
434 createdAt: input.source.createdAt,
435 updatedAt: input.source.updatedAt,
436 };
437
438 return {
439 sourceRecord,
440 runtimeAuthArtifact: authArtifactFromSourceAuth({
441 source: input.source,
442 auth: input.source.auth,
443 slot: "runtime",
444 actorScopeId: input.actorScopeId,
445 existingAuthArtifactId: input.existingRuntimeAuthArtifactId
446 ?? AuthArtifactIdSchema.make(`auth_art_${crypto.randomUUID()}`),
447 }),
448 importAuthArtifact: input.source.importAuthPolicy === "separate"
449 ? authArtifactFromSourceAuth({
450 source: input.source,
451 auth: input.source.importAuth,
452 slot: "import",
453 actorScopeId: input.actorScopeId,
454 existingAuthArtifactId: input.existingImportAuthArtifactId
455 ?? AuthArtifactIdSchema.make(`auth_art_${crypto.randomUUID()}`),
456 })
457 : null,
458 };
459};
460
461export const projectSourceFromStorage = (input: {
462 sourceRecord: StoredSourceRecord;
463 runtimeAuthArtifact: AuthArtifact | null;
464 importAuthArtifact: AuthArtifact | null;
465}): Effect.Effect<Source, Error, never> =>
466 Effect.gen(function* () {
467 const adapter = getSourceAdapter(input.sourceRecord.kind);
468 const bindingConfig = yield* adapter.deserializeBindingConfig({
469 id: input.sourceRecord.id,
470 bindingConfigJson: input.sourceRecord.bindingConfigJson,
471 });
472
473 return {
474 id: input.sourceRecord.id,
475 scopeId: input.sourceRecord.scopeId,
476 name: input.sourceRecord.name,
477 kind: input.sourceRecord.kind,
478 endpoint: input.sourceRecord.endpoint,
479 status: input.sourceRecord.status,
480 enabled: input.sourceRecord.enabled,
481 namespace: input.sourceRecord.namespace,
482 bindingVersion: bindingConfig.version,
483 binding: bindingConfig.payload,
484 importAuthPolicy: input.sourceRecord.importAuthPolicy,
485 importAuth:
486 input.sourceRecord.importAuthPolicy === "separate"
487 ? sourceAuthFromAuthArtifact(input.importAuthArtifact)
488 : { kind: "none" },
489 auth: sourceAuthFromAuthArtifact(input.runtimeAuthArtifact),
490 sourceHash: input.sourceRecord.sourceHash,
491 lastError: input.sourceRecord.lastError,
492 createdAt: input.sourceRecord.createdAt,
493 updatedAt: input.sourceRecord.updatedAt,
494 } satisfies Source;
495 }).pipe(
496 Effect.mapError((cause) =>
497 cause instanceof Error ? cause : new Error(String(cause)),
498 ),
499 );
500
501export const projectSourcesFromStorage = (input: {
502 sourceRecords: ReadonlyArray<StoredSourceRecord>;
503 authArtifacts: ReadonlyArray<AuthArtifact>;
504}): Effect.Effect<ReadonlyArray<Source>, Error, never> => {
505 const authArtifactsBySourceId = new Map<string, {
506 runtime: AuthArtifact | null;
507 import: AuthArtifact | null;
508 }>();
509
510 for (const authArtifact of input.authArtifacts) {
511 const existing = authArtifactsBySourceId.get(authArtifact.sourceId) ?? {
512 runtime: null,
513 import: null,
514 };
515 const current = authArtifact.slot === "runtime" ? existing.runtime : existing.import;
516 if (current === null || (current.actorScopeId === null && authArtifact.actorScopeId !== null)) {
517 authArtifactsBySourceId.set(authArtifact.sourceId, {
518 ...existing,
519 [authArtifact.slot]: authArtifact,
520 });
521 }
522 }
523
524 return Effect.forEach(input.sourceRecords, (sourceRecord) =>
525 projectSourceFromStorage({
526 sourceRecord,
527 runtimeAuthArtifact: authArtifactsBySourceId.get(sourceRecord.id)?.runtime ?? null,
528 importAuthArtifact: authArtifactsBySourceId.get(sourceRecord.id)?.import ?? null,
529 }));
530};
packages/platform/sdk/src/runtime/sources/source-inspection.test.ts+1−1
@@ -31,7 +31,7 @@ describe("source inspection", () => { · 30 unmodified lines
31 },31 },
32 executable: {32 executable: {
33 id: "exec_graphql_administrableTeams",33 id: "exec_graphql_administrableTeams",
34 adapterKey: "graphql",34 pluginKey: "graphql",
35 bindingVersion: 1,35 bindingVersion: 1,
36 binding: {36 binding: {
37 operationKind: "query",37 operationKind: "query",
packages/platform/sdk/src/runtime/sources/source-plugins/index.ts+41−0
@@ -0,0 +1,41 @@
1import {
2 registerExecutorSdkPlugins,
3 type ExecutorSdkPlugin,
4} from "../../../plugins";
5
6import {
7 InternalSourceSdkPlugin,
8} from "./internal";
9
10let configuredExternalSourcePlugins: readonly ExecutorSdkPlugin[] = [];
11let registry = registerExecutorSdkPlugins([
12 InternalSourceSdkPlugin,
13]);
14
15const refreshRegistry = () => {
16 registry = registerExecutorSdkPlugins([
17 ...configuredExternalSourcePlugins,
18 InternalSourceSdkPlugin,
19 ]);
20};
21
22export const configureExecutorSourcePlugins = (
23 plugins: readonly ExecutorSdkPlugin[],
24): void => {
25 configuredExternalSourcePlugins = plugins;
26 refreshRegistry();
27};
28
29export const registeredSourcePlugins = () => registry.sourcePlugins;
30
31export const getSourcePlugin = (kind: string) => registry.getSourcePlugin(kind);
32export const getSourcePluginForSource = (
33 source: Parameters<typeof registry.getSourcePluginForSource>[0],
34) => registry.getSourcePluginForSource(source);
35export const sourcePluginCatalogKind = (kind: string) =>
36 registry.sourcePluginCatalogKind(kind);
37export const isInternalSourcePluginKind = (kind: string) =>
38 registry.isInternalSourcePluginKind(kind);
39
40export const hasRegisteredExternalSourcePlugins = () =>
41 configuredExternalSourcePlugins.length > 0;
packages/platform/sdk/src/runtime/sources/source-plugins/internal.ts+48−0
@@ -0,0 +1,48 @@
1import * as Effect from "effect/Effect";
2
3import {
4 createCatalogImportMetadata,
5 createSourceCatalogSyncResult,
6} from "@executor/source-core";
7import type { ExecutorSdkPlugin } from "../../../plugins";
8
9import { runtimeEffectError } from "../../effect-errors";
10
11export const InternalSourceSdkPlugin = {
12 key: "internal",
13 sources: [
14 {
15 kind: "internal",
16 displayName: "Internal",
17 catalogKind: "internal",
18 catalogIdentity: ({ source }) => ({
19 kind: "internal",
20 endpoint: source.endpoint,
21 }),
22 getIrModel: ({ source }) =>
23 Effect.succeed(
24 createSourceCatalogSyncResult({
25 fragment: {
26 version: "ir.v1.fragment",
27 },
28 importMetadata: {
29 ...createCatalogImportMetadata({
30 source,
31 pluginKey: "internal",
32 }),
33 importerVersion: "ir.v1.internal",
34 sourceConfigHash: "internal",
35 },
36 sourceHash: null,
37 }),
38 ),
39 invoke: () =>
40 Effect.fail(
41 runtimeEffectError(
42 "sources/source-plugins/internal",
43 "Internal sources do not support persisted plugin invocation",
44 ),
45 ),
46 },
47 ],
48} satisfies ExecutorSdkPlugin<"internal">;
packages/platform/sdk/src/runtime/sources/source-store.ts+10−18
@@ -15,9 +15,6 @@ import { · 14 unmodified lines
15 ScopeConfigStore,15 ScopeConfigStore,
16 ScopeStateStore,16 ScopeStateStore,
17} from "../scope/storage";17} from "../scope/storage";
18import {
19 SecretMaterialDeleterService,
20} from "../scope/secret-material-providers";
21import {18import {
22 RuntimeLocalScopeService,19 RuntimeLocalScopeService,
23} from "../scope/runtime-context";20} from "../scope/runtime-context";
@@ -128,13 +125,11 @@ export const removeSourceById = ( · 104 unmodified lines
128 Error,125 Error,
129 | ScopeStorageServices126 | ScopeStorageServices
130 | SourceTypeDeclarationsRefresherService127 | SourceTypeDeclarationsRefresherService
131 | SecretMaterialDeleterService
132> =>128> =>
133 Effect.gen(function* () {129 Effect.flatMap(
134 const deps = yield* loadRuntimeSourceStoreDeps(executorState, input.scopeId);130 loadRuntimeSourceStoreDeps(executorState, input.scopeId),
135 const deleteSecretMaterial = yield* SecretMaterialDeleterService;131 (deps) => removeSourceByIdWithDeps(deps, input),
136 return yield* removeSourceByIdWithDeps(deps, input, deleteSecretMaterial);132 );
137 });
138 133
139export const persistSource = (134export const persistSource = (
140 executorState: ExecutorStateStoreShape,135 executorState: ExecutorStateStoreShape,
@@ -147,13 +142,11 @@ export const persistSource = ( · 6 unmodified lines
147 Error,142 Error,
148 | ScopeStorageServices143 | ScopeStorageServices
149 | SourceTypeDeclarationsRefresherService144 | SourceTypeDeclarationsRefresherService
150 | SecretMaterialDeleterService
151> =>145> =>
152 Effect.gen(function* () {146 Effect.flatMap(
153 const deps = yield* loadRuntimeSourceStoreDeps(executorState, source.scopeId);147 loadRuntimeSourceStoreDeps(executorState, source.scopeId),
154 const deleteSecretMaterial = yield* SecretMaterialDeleterService;148 (deps) => persistSourceWithDeps(deps, source, options),
155 return yield* persistSourceWithDeps(deps, source, options, deleteSecretMaterial);149 );
156 });
157 150
158export class RuntimeSourceStoreService extends Context.Tag(151export class RuntimeSourceStoreService extends Context.Tag(
159 "#runtime/RuntimeSourceStoreService",152 "#runtime/RuntimeSourceStoreService",
@@ -169,7 +162,6 @@ export const RuntimeSourceStoreLive = Layer.effect( · 9 unmodified lines
169 const sourceArtifactStore = yield* SourceArtifactStore;162 const sourceArtifactStore = yield* SourceArtifactStore;
170 const sourceTypeDeclarationsRefresher =163 const sourceTypeDeclarationsRefresher =
171 yield* SourceTypeDeclarationsRefresherService;164 yield* SourceTypeDeclarationsRefresherService;
172 const deleteSecretMaterial = yield* SecretMaterialDeleterService;
173 165
174 const deps: RuntimeSourceStoreDeps = {166 const deps: RuntimeSourceStoreDeps = {
175 executorState,167 executorState,
@@ -188,9 +180,9 @@ export const RuntimeSourceStoreLive = Layer.effect( · 12 unmodified lines
188 loadSourceById: (input) =>180 loadSourceById: (input) =>
189 loadSourceByIdWithDeps(deps, input),181 loadSourceByIdWithDeps(deps, input),
190 removeSourceById: (input) =>182 removeSourceById: (input) =>
191 removeSourceByIdWithDeps(deps, input, deleteSecretMaterial),183 removeSourceByIdWithDeps(deps, input),
192 persistSource: (source, options = {}) =>184 persistSource: (source, options = {}) =>
193 persistSourceWithDeps(deps, source, options, deleteSecretMaterial),185 persistSourceWithDeps(deps, source, options),
194 });186 });
195 }),187 }),
196);188);
packages/platform/sdk/src/runtime/sources/source-store/auth.ts+0−141
@@ -1,141 +0,0 @@
1import type {
2 ScopeId,
3 AuthArtifact,
4 CredentialSlot,
5 ProviderAuthGrant,
6 Source,
7} from "#schema";
8import {
9 decodeProviderGrantRefAuthArtifactConfig,
10} from "#schema";
11import * as Effect from "effect/Effect";
12
13import {
14 authArtifactSecretMaterialRefs,
15} from "../../auth/auth-artifacts";
16import {
17 removeAuthLeaseAndSecrets,
18} from "../../auth/auth-leases";
19import type {
20 DeleteSecretMaterial,
21} from "../../scope/secret-material-providers";
22import type {
23 ExecutorStateStoreShape,
24} from "../../executor-state-store";
25
26const secretRefKey = (ref: { providerId: string; handle: string }): string =>
27 `${ref.providerId}:${ref.handle}`;
28
29export const cleanupAuthArtifactSecretRefs = (
30 executorState: ExecutorStateStoreShape,
31 input: {
32 previous: AuthArtifact | null;
33 next: AuthArtifact | null;
34 },
35 deleteSecretMaterial: DeleteSecretMaterial,
36) =>
37 Effect.gen(function* () {
38 if (input.previous === null) {
39 return;
40 }
41 const nextRefKeys = new Set(
42 (input.next === null ? [] : authArtifactSecretMaterialRefs(input.next)).map(
43 secretRefKey,
44 ),
45 );
46 const refsToDelete = authArtifactSecretMaterialRefs(input.previous).filter(
47 (ref) => !nextRefKeys.has(secretRefKey(ref)),
48 );
49
50 yield* Effect.forEach(
51 refsToDelete,
52 (ref) => Effect.either(deleteSecretMaterial(ref)),
53 { discard: true },
54 );
55 });
56
57export const providerGrantIdsFromArtifacts = (
58 artifacts: ReadonlyArray<
59 Pick<AuthArtifact, "artifactKind" | "configJson"> | null
60 >,
61): ReadonlySet<ProviderAuthGrant["id"]> =>
62 new Set(
63 artifacts
64 .flatMap((artifact) =>
65 artifact ? [decodeProviderGrantRefAuthArtifactConfig(artifact)] : []
66 )
67 .flatMap((config) => (config ? [config.grantId] : [])),
68 );
69
70export const selectPreferredAuthArtifact = (input: {
71 authArtifacts: ReadonlyArray<AuthArtifact>;
72 actorScopeId?: ScopeId | null;
73 slot: CredentialSlot;
74}): AuthArtifact | null => {
75 const matchingSlot = input.authArtifacts.filter(
76 (artifact) => artifact.slot === input.slot,
77 );
78
79 if (input.actorScopeId !== undefined) {
80 const exact = matchingSlot.find(
81 (artifact) => artifact.actorScopeId === input.actorScopeId,
82 );
83 if (exact) {
84 return exact;
85 }
86 }
87
88 return matchingSlot.find((artifact) => artifact.actorScopeId === null) ?? null;
89};
90
91export const selectExactAuthArtifact = (input: {
92 authArtifacts: ReadonlyArray<AuthArtifact>;
93 actorScopeId?: ScopeId | null;
94 slot: CredentialSlot;
95}): AuthArtifact | null =>
96 input.authArtifacts.find(
97 (artifact) =>
98 artifact.slot === input.slot &&
99 artifact.actorScopeId === (input.actorScopeId ?? null),
100 ) ?? null;
101
102export const removeAuthArtifactsForSource = (
103 executorState: ExecutorStateStoreShape,
104 input: {
105 scopeId: ScopeId;
106 sourceId: Source["id"];
107 },
108 deleteSecretMaterial: DeleteSecretMaterial,
109) =>
110 Effect.gen(function* () {
111 const existingAuthArtifacts = yield* executorState.authArtifacts.listByScopeAndSourceId({
112 scopeId: input.scopeId,
113 sourceId: input.sourceId,
114 });
115
116 yield* executorState.authArtifacts.removeByScopeAndSourceId({
117 scopeId: input.scopeId,
118 sourceId: input.sourceId,
119 });
120
121 yield* Effect.forEach(
122 existingAuthArtifacts,
123 (artifact) =>
124 removeAuthLeaseAndSecrets(executorState, {
125 authArtifactId: artifact.id,
126 }, deleteSecretMaterial),
127 { discard: true },
128 );
129
130 yield* Effect.forEach(
131 existingAuthArtifacts,
132 (artifact) =>
133 cleanupAuthArtifactSecretRefs(executorState, {
134 previous: artifact,
135 next: null,
136 }, deleteSecretMaterial),
137 { discard: true },
138 );
139
140 return existingAuthArtifacts.length;
141 });
packages/platform/sdk/src/runtime/sources/source-store/config.ts+2−140
@@ -1,5 +1,4 @@
1import type {1import type {
2 LocalConfigSecretInput,
3 LocalConfigSource,2 LocalConfigSource,
4 Source,3 Source,
5 SourceId,4 SourceId,
@@ -8,19 +7,6 @@ import { · 2 unmodified lines
8 SourceIdSchema,7 SourceIdSchema,
9} from "#schema";8} from "#schema";
10 9
11import type {
12 LoadedLocalExecutorConfig,
13} from "../../scope-config";
14import {
15 LocalUnsupportedSourceKindError,
16} from "../../scope-errors";
17import {
18 fromConfigSecretProviderId,
19 toConfigSecretProviderId,
20} from "../../scope/config-secrets";
21import {
22 getSourceAdapterForSource,
23} from "../source-adapters";
24import {10import {
25 slugify,11 slugify,
26} from "../slug";12} from "../slug";
@@ -52,120 +38,11 @@ export const deriveLocalSourceId = ( · 25 unmodified lines
52 return SourceIdSchema.make(candidate);38 return SourceIdSchema.make(candidate);
53};39};
54 40
55const resolveLocalConfigSecretProviderAlias = (
56 config: LoadedLocalExecutorConfig["config"],
57): string | null => {
58 const defaultAlias = trimOrNull(config?.secrets?.defaults?.env);
59 if (defaultAlias !== null && config?.secrets?.providers?.[defaultAlias]) {
60 return defaultAlias;
61 }
62
63 return config?.secrets?.providers?.default ? "default" : null;
64};
65
66export const sourceAuthFromConfigInput = (input: {
67 auth: unknown;
68 config: LoadedLocalExecutorConfig["config"];
69 existing: Source["auth"] | null;
70}): Source["auth"] => {
71 if (input.auth === undefined) {
72 return input.existing ?? { kind: "none" };
73 }
74
75 if (typeof input.auth === "string") {
76 const providerAlias = resolveLocalConfigSecretProviderAlias(input.config);
77 return {
78 kind: "bearer",
79 headerName: "Authorization",
80 prefix: "Bearer ",
81 token: {
82 providerId: providerAlias ? toConfigSecretProviderId(providerAlias) : "env",
83 handle: input.auth,
84 },
85 };
86 }
87
88 if (typeof input.auth === "object" && input.auth !== null) {
89 const explicit = input.auth as {
90 source?: string;
91 provider?: string;
92 id?: string;
93 };
94 const providerAlias = trimOrNull(explicit.provider);
95 const providerId = providerAlias
96 ? providerAlias === "params"
97 ? "params"
98 : toConfigSecretProviderId(providerAlias)
99 : explicit.source === "env"
100 ? "env"
101 : explicit.source === "params"
102 ? "params"
103 : null;
104 const handle = trimOrNull(explicit.id);
105 if (providerId && handle) {
106 return {
107 kind: "bearer",
108 headerName: "Authorization",
109 prefix: "Bearer ",
110 token: {
111 providerId,
112 handle,
113 },
114 };
115 }
116 }
117
118 return input.existing ?? { kind: "none" };
119};
120
121const configAuthFromSource = (input: {
122 source: Source;
123 existingConfigAuth: LocalConfigSecretInput | undefined;
124 config: LoadedLocalExecutorConfig["config"];
125}): LocalConfigSecretInput | undefined => {
126 if (input.source.auth.kind !== "bearer") {
127 return input.existingConfigAuth;
128 }
129
130 if (input.source.auth.token.providerId === "env") {
131 return input.source.auth.token.handle;
132 }
133
134 if (input.source.auth.token.providerId === "params") {
135 return {
136 source: "params",
137 provider: "params",
138 id: input.source.auth.token.handle,
139 };
140 }
141
142 const provider = fromConfigSecretProviderId(input.source.auth.token.providerId);
143 if (provider !== null) {
144 const configuredProvider = input.config?.secrets?.providers?.[provider];
145 if (configuredProvider) {
146 return {
147 source: configuredProvider.source,
148 provider,
149 id: input.source.auth.token.handle,
150 };
151 }
152 }
153
154 return input.existingConfigAuth;
155};
156
157export const configSourceFromLocalSource = (input: {41export const configSourceFromLocalSource = (input: {
158 source: Source;42 source: Source;
159 existingConfigAuth: LocalConfigSecretInput | undefined;
160 config: LoadedLocalExecutorConfig["config"];
161}): LocalConfigSource => {43}): LocalConfigSource => {
162 const auth = configAuthFromSource({44 return {
163 source: input.source,45 kind: input.source.kind as LocalConfigSource["kind"],
164 existingConfigAuth: input.existingConfigAuth,
165 config: input.config,
166 });
167
168 const common = {
169 ...(trimOrNull(input.source.name) !== trimOrNull(input.source.id)46 ...(trimOrNull(input.source.name) !== trimOrNull(input.source.id)
170 ? { name: input.source.name }47 ? { name: input.source.name }
171 : {}),48 : {}),
@@ -175,21 +52,6 @@ export const configSourceFromLocalSource = (input: { · 3 unmodified lines
175 ...(input.source.enabled === false ? { enabled: false } : {}),52 ...(input.source.enabled === false ? { enabled: false } : {}),
176 connection: {53 connection: {
177 endpoint: input.source.endpoint,54 endpoint: input.source.endpoint,
178 ...(auth !== undefined ? { auth } : {}),
179 },55 },
180 };
181
182 const adapter = getSourceAdapterForSource(input.source);
183 if (adapter.localConfigBindingSchema === null) {
184 throw new LocalUnsupportedSourceKindError({
185 message: `Unsupported source kind for local config: ${input.source.kind}`,
186 kind: input.source.kind,
187 });
188 }
189
190 return {
191 kind: input.source.kind as LocalConfigSource["kind"],
192 ...common,
193 binding: cloneJson(adapter.localConfigBindingFromSource(input.source)),
194 } as LocalConfigSource;56 } as LocalConfigSource;
195};57};
packages/platform/sdk/src/runtime/sources/source-store/lifecycle.ts+2−163
@@ -4,30 +4,7 @@ import type { · 3 unmodified lines
4} from "#schema";4} from "#schema";
5import * as Effect from "effect/Effect";5import * as Effect from "effect/Effect";
6 6
7import {7import type { LocalScopeState } from "../../scope-state";
8 removeAuthLeaseAndSecrets,
9} from "../../auth/auth-leases";
10import {
11 clearProviderGrantOrphanedAt,
12 markProviderGrantOrphanedIfUnused,
13} from "../../auth/provider-grant-lifecycle";
14import type {
15 DeleteSecretMaterial,
16} from "../../scope/secret-material-providers";
17import type {
18 LocalScopeState,
19} from "../../scope-state";
20import {
21 stableSourceCatalogId,
22 stableSourceCatalogRevisionId,
23 splitSourceForStorage,
24} from "../source-definitions";
25import {
26 cleanupAuthArtifactSecretRefs,
27 providerGrantIdsFromArtifacts,
28 removeAuthArtifactsForSource,
29 selectExactAuthArtifact,
30} from "./auth";
31import {8import {
32 configSourceFromLocalSource,9 configSourceFromLocalSource,
33 cloneJson,10 cloneJson,
@@ -49,7 +26,6 @@ export const removeSourceByIdWithDeps = ( · 15 unmodified lines
49 scopeId: ScopeId;26 scopeId: ScopeId;
50 sourceId: Source["id"];27 sourceId: Source["id"];
51 },28 },
52 deleteSecretMaterial: DeleteSecretMaterial,
53): Effect.Effect<boolean, Error, never> =>29): Effect.Effect<boolean, Error, never> =>
54 Effect.gen(function* () {30 Effect.gen(function* () {
55 const localScope = yield* resolveRuntimeLocalScopeFromDeps(31 const localScope = yield* resolveRuntimeLocalScopeFromDeps(
@@ -84,31 +60,6 @@ export const removeSourceByIdWithDeps = ( · 28 unmodified lines
84 yield* localScope.sourceArtifactStore.remove({60 yield* localScope.sourceArtifactStore.remove({
85 sourceId: input.sourceId,61 sourceId: input.sourceId,
86 });62 });
87 const existingAuthArtifacts =
88 yield* deps.executorState.authArtifacts.listByScopeAndSourceId({
89 scopeId: input.scopeId,
90 sourceId: input.sourceId,
91 });
92 const removedGrantIds = providerGrantIdsFromArtifacts(existingAuthArtifacts);
93
94 yield* deps.executorState.sourceAuthSessions.removeByScopeAndSourceId(
95 input.scopeId,
96 input.sourceId,
97 );
98 yield* deps.executorState.sourceOauthClients.removeByScopeAndSourceId({
99 scopeId: input.scopeId,
100 sourceId: input.sourceId,
101 });
102 yield* removeAuthArtifactsForSource(deps.executorState, input, deleteSecretMaterial);
103 yield* Effect.forEach(
104 [...removedGrantIds],
105 (grantId) =>
106 markProviderGrantOrphanedIfUnused(deps.executorState, {
107 scopeId: input.scopeId,
108 grantId,
109 }),
110 { discard: true },
111 );
112 yield* syncScopeSourceTypeDeclarationsWithDeps(deps, input.scopeId);63 yield* syncScopeSourceTypeDeclarationsWithDeps(deps, input.scopeId);
113 64
114 return true;65 return true;
@@ -120,7 +71,6 @@ export const persistSourceWithDeps = ( · 5 unmodified lines
120 options: {71 options: {
121 actorScopeId?: ScopeId | null;72 actorScopeId?: ScopeId | null;
122 } = {},73 } = {},
123 deleteSecretMaterial: DeleteSecretMaterial,
124): Effect.Effect<Source, Error, never> =>74): Effect.Effect<Source, Error, never> =>
125 Effect.gen(function* () {75 Effect.gen(function* () {
126 const localScope = yield* resolveRuntimeLocalScopeFromDeps(76 const localScope = yield* resolveRuntimeLocalScopeFromDeps(
@@ -138,30 +88,13 @@ export const persistSourceWithDeps = ( · 11 unmodified lines
138 new Set(Object.keys(localScope.loadedConfig.config?.sources ?? {})),88 new Set(Object.keys(localScope.loadedConfig.config?.sources ?? {})),
139 ),89 ),
140 } satisfies Source;90 } satisfies Source;
141 const existingAuthArtifacts =91
142 yield* deps.executorState.authArtifacts.listByScopeAndSourceId({
143 scopeId: nextSource.scopeId,
144 sourceId: nextSource.id,
145 });
146 const existingRuntimeAuthArtifact = selectExactAuthArtifact({
147 authArtifacts: existingAuthArtifacts,
148 actorScopeId: options.actorScopeId,
149 slot: "runtime",
150 });
151 const existingImportAuthArtifact = selectExactAuthArtifact({
152 authArtifacts: existingAuthArtifacts,
153 actorScopeId: options.actorScopeId,
154 slot: "import",
155 });
156 const projectConfig = cloneJson(localScope.loadedConfig.projectConfig ?? {});92 const projectConfig = cloneJson(localScope.loadedConfig.projectConfig ?? {});
157 const sources = {93 const sources = {
158 ...projectConfig.sources,94 ...projectConfig.sources,
159 };95 };
160 const existingConfigSource = sources[nextSource.id];
161 sources[nextSource.id] = configSourceFromLocalSource({96 sources[nextSource.id] = configSourceFromLocalSource({
162 source: nextSource,97 source: nextSource,
163 existingConfigAuth: existingConfigSource?.connection.auth,
164 config: localScope.loadedConfig.config,
165 });98 });
166 yield* localScope.scopeConfigStore.writeProject({99 yield* localScope.scopeConfigStore.writeProject({
167 config: {100 config: {
@@ -170,100 +103,6 @@ export const persistSourceWithDeps = ( · 2 unmodified lines
170 },103 },
171 });104 });
172 105
173 const { runtimeAuthArtifact, importAuthArtifact } = splitSourceForStorage({
174 source: nextSource,
175 catalogId: stableSourceCatalogId(nextSource),
176 catalogRevisionId: stableSourceCatalogRevisionId(nextSource),
177 actorScopeId: options.actorScopeId,
178 existingRuntimeAuthArtifactId: existingRuntimeAuthArtifact?.id ?? null,
179 existingImportAuthArtifactId: existingImportAuthArtifact?.id ?? null,
180 });
181
182 if (runtimeAuthArtifact === null) {
183 if (existingRuntimeAuthArtifact !== null) {
184 yield* removeAuthLeaseAndSecrets(deps.executorState, {
185 authArtifactId: existingRuntimeAuthArtifact.id,
186 }, deleteSecretMaterial);
187 }
188 yield* deps.executorState.authArtifacts.removeByScopeSourceAndActor({
189 scopeId: nextSource.scopeId,
190 sourceId: nextSource.id,
191 actorScopeId: options.actorScopeId ?? null,
192 slot: "runtime",
193 });
194 } else {
195 yield* deps.executorState.authArtifacts.upsert(runtimeAuthArtifact);
196 if (
197 existingRuntimeAuthArtifact !== null &&
198 existingRuntimeAuthArtifact.id !== runtimeAuthArtifact.id
199 ) {
200 yield* removeAuthLeaseAndSecrets(deps.executorState, {
201 authArtifactId: existingRuntimeAuthArtifact.id,
202 }, deleteSecretMaterial);
203 }
204 }
205
206 yield* cleanupAuthArtifactSecretRefs(deps.executorState, {
207 previous: existingRuntimeAuthArtifact ?? null,
208 next: runtimeAuthArtifact,
209 }, deleteSecretMaterial);
210
211 if (importAuthArtifact === null) {
212 if (existingImportAuthArtifact !== null) {
213 yield* removeAuthLeaseAndSecrets(deps.executorState, {
214 authArtifactId: existingImportAuthArtifact.id,
215 }, deleteSecretMaterial);
216 }
217 yield* deps.executorState.authArtifacts.removeByScopeSourceAndActor({
218 scopeId: nextSource.scopeId,
219 sourceId: nextSource.id,
220 actorScopeId: options.actorScopeId ?? null,
221 slot: "import",
222 });
223 } else {
224 yield* deps.executorState.authArtifacts.upsert(importAuthArtifact);
225 if (
226 existingImportAuthArtifact !== null &&
227 existingImportAuthArtifact.id !== importAuthArtifact.id
228 ) {
229 yield* removeAuthLeaseAndSecrets(deps.executorState, {
230 authArtifactId: existingImportAuthArtifact.id,
231 }, deleteSecretMaterial);
232 }
233 }
234
235 yield* cleanupAuthArtifactSecretRefs(deps.executorState, {
236 previous: existingImportAuthArtifact ?? null,
237 next: importAuthArtifact,
238 }, deleteSecretMaterial);
239
240 const previousGrantIds = providerGrantIdsFromArtifacts([
241 existingRuntimeAuthArtifact,
242 existingImportAuthArtifact,
243 ]);
244 const nextGrantIds = providerGrantIdsFromArtifacts([
245 runtimeAuthArtifact,
246 importAuthArtifact,
247 ]);
248
249 yield* Effect.forEach(
250 [...nextGrantIds],
251 (grantId) =>
252 clearProviderGrantOrphanedAt(deps.executorState, {
253 grantId,
254 }),
255 { discard: true },
256 );
257 yield* Effect.forEach(
258 [...previousGrantIds].filter((grantId) => !nextGrantIds.has(grantId)),
259 (grantId) =>
260 markProviderGrantOrphanedIfUnused(deps.executorState, {
261 scopeId: nextSource.scopeId,
262 grantId,
263 }),
264 { discard: true },
265 );
266
267 const existingSourceState = localScope.scopeState.sources[nextSource.id];106 const existingSourceState = localScope.scopeState.sources[nextSource.id];
268 const scopeState: LocalScopeState = {107 const scopeState: LocalScopeState = {
269 ...localScope.scopeState,108 ...localScope.scopeState,
packages/platform/sdk/src/runtime/sources/source-store/records.ts+16−123
@@ -1,53 +1,29 @@
1import type {1import type {
2 ScopeId,2 ScopeId,
3 AuthArtifact,
4 Source,3 Source,
5 SourceId,4 SourceId,
6} from "#schema";5} from "#schema";
7import {6import { SourceIdSchema } from "#schema";
8 SourceIdSchema,
9} from "#schema";
10import * as Effect from "effect/Effect";7import * as Effect from "effect/Effect";
11 8
12import {9import type { LoadedLocalExecutorConfig } from "../../scope-config";
13 sourceAuthFromAuthArtifact,
14} from "../../auth/auth-artifacts";
15import {
16 authArtifactSecretMaterialRefs,
17} from "../../auth/auth-artifacts";
18import type {
19 LoadedLocalExecutorConfig,
20} from "../../scope-config";
21import {10import {
22 LocalConfiguredSourceNotFoundError,11 LocalConfiguredSourceNotFoundError,
23 RuntimeLocalScopeMismatchError,12 RuntimeLocalScopeMismatchError,
24 RuntimeLocalScopeUnavailableError,13 RuntimeLocalScopeUnavailableError,
25} from "../../scope-errors";14} from "../../scope-errors";
26import type {15import type { LocalScopeState } from "../../scope-state";
27 LocalScopeState,
28} from "../../scope-state";
29import {
30 getSourceAdapter,
31} from "../source-adapters";
32import {16import {
33 resolveRuntimeLocalScopeFromDeps,17 resolveRuntimeLocalScopeFromDeps,
34 type RuntimeSourceStoreDeps,18 type RuntimeSourceStoreDeps,
35} from "./deps";19} from "./deps";
36import {20import { trimOrNull } from "./config";
37 selectPreferredAuthArtifact,
38} from "./auth";
39import {
40 sourceAuthFromConfigInput,
41 trimOrNull,
42} from "./config";
43 21
44export const buildLocalSourceRecord = (input: {22export const buildLocalSourceRecord = (input: {
45 scopeId: ScopeId;23 scopeId: ScopeId;
46 loadedConfig: LoadedLocalExecutorConfig;24 loadedConfig: LoadedLocalExecutorConfig;
47 scopeState: LocalScopeState;25 scopeState: LocalScopeState;
48 sourceId: SourceId;26 sourceId: SourceId;
49 actorScopeId?: ScopeId | null;
50 authArtifacts: ReadonlyArray<AuthArtifact>;
51}): Effect.Effect<27}): Effect.Effect<
52 {28 {
53 source: Source;29 source: Source;
@@ -60,14 +36,13 @@ export const buildLocalSourceRecord = (input: { · 6 unmodified lines
60 const sourceConfig = input.loadedConfig.config?.sources?.[input.sourceId];36 const sourceConfig = input.loadedConfig.config?.sources?.[input.sourceId];
61 if (!sourceConfig) {37 if (!sourceConfig) {
62 return yield* new LocalConfiguredSourceNotFoundError({38 return yield* new LocalConfiguredSourceNotFoundError({
63 message: `Configured source not found for id ${input.sourceId}`,39 message: `Configured source not found for id ${input.sourceId}`,
64 sourceId: input.sourceId,40 sourceId: input.sourceId,
65 });41 });
66 }42 }
67 43
68 const existingState = input.scopeState.sources[input.sourceId];44 const existingState = input.scopeState.sources[input.sourceId];
69 const adapter = getSourceAdapter(sourceConfig.kind);45 const source: Source = {
70 const baseSource = (yield* adapter.validateSource({
71 id: SourceIdSchema.make(input.sourceId),46 id: SourceIdSchema.make(input.sourceId),
72 scopeId: input.scopeId,47 scopeId: input.scopeId,
73 name: trimOrNull(sourceConfig.name) ?? input.sourceId,48 name: trimOrNull(sourceConfig.name) ?? input.sourceId,
@@ -78,48 +53,10 @@ export const buildLocalSourceRecord = (input: { · 4 unmodified lines
78 (sourceConfig.enabled ?? true ? "connected" : "draft"),53 (sourceConfig.enabled ?? true ? "connected" : "draft"),
79 enabled: sourceConfig.enabled ?? true,54 enabled: sourceConfig.enabled ?? true,
80 namespace: trimOrNull(sourceConfig.namespace) ?? input.sourceId,55 namespace: trimOrNull(sourceConfig.namespace) ?? input.sourceId,
81 bindingVersion: adapter.bindingConfigVersion,
82 binding: sourceConfig.binding,
83 importAuthPolicy: adapter.defaultImportAuthPolicy,
84 importAuth: { kind: "none" },
85 auth: sourceAuthFromConfigInput({
86 auth: sourceConfig.connection.auth,
87 config: input.loadedConfig.config,
88 existing: null,
89 }),
90 sourceHash: existingState?.sourceHash ?? null,56 sourceHash: existingState?.sourceHash ?? null,
91 lastError: existingState?.lastError ?? null,57 lastError: existingState?.lastError ?? null,
92 createdAt: existingState?.createdAt ?? Date.now(),58 createdAt: existingState?.createdAt ?? Date.now(),
93 updatedAt: existingState?.updatedAt ?? Date.now(),59 updatedAt: existingState?.updatedAt ?? Date.now(),
94 })) as Source;
95
96 const runtimeAuthArtifact = selectPreferredAuthArtifact({
97 authArtifacts: input.authArtifacts.filter(
98 (artifactItem) => artifactItem.sourceId === baseSource.id,
99 ),
100 actorScopeId: input.actorScopeId,
101 slot: "runtime",
102 });
103 const importAuthArtifact = selectPreferredAuthArtifact({
104 authArtifacts: input.authArtifacts.filter(
105 (artifactItem) => artifactItem.sourceId === baseSource.id,
106 ),
107 actorScopeId: input.actorScopeId,
108 slot: "import",
109 });
110
111 const source: Source = {
112 ...baseSource,
113 auth:
114 runtimeAuthArtifact === null
115 ? baseSource.auth
116 : sourceAuthFromAuthArtifact(runtimeAuthArtifact),
117 importAuth:
118 baseSource.importAuthPolicy === "separate"
119 ? importAuthArtifact === null
120 ? baseSource.importAuth
121 : sourceAuthFromAuthArtifact(importAuthArtifact)
122 : { kind: "none" },
123 };60 };
124 61
125 return {62 return {
@@ -131,7 +68,7 @@ export const buildLocalSourceRecord = (input: { · 5 unmodified lines
131export const loadSourcesInWorkspaceWithDeps = (68export const loadSourcesInWorkspaceWithDeps = (
132 deps: RuntimeSourceStoreDeps,69 deps: RuntimeSourceStoreDeps,
133 scopeId: ScopeId,70 scopeId: ScopeId,
134 options: {71 _options: {
135 actorScopeId?: ScopeId | null;72 actorScopeId?: ScopeId | null;
136 } = {},73 } = {},
137): Effect.Effect<74): Effect.Effect<
@@ -147,9 +84,6 @@ export const loadSourcesInWorkspaceWithDeps = ( · 9 unmodified lines
147 deps,84 deps,
148 scopeId,85 scopeId,
149 );86 );
150 const authArtifacts = yield* deps.executorState.authArtifacts.listByScopeId(
151 scopeId,
152 );
153 const sources = yield* Effect.forEach(87 const sources = yield* Effect.forEach(
154 Object.keys(localScope.loadedConfig.config?.sources ?? {}),88 Object.keys(localScope.loadedConfig.config?.sources ?? {}),
155 (sourceId) =>89 (sourceId) =>
@@ -159,8 +93,6 @@ export const loadSourcesInWorkspaceWithDeps = ( · 3 unmodified lines
159 loadedConfig: localScope.loadedConfig,93 loadedConfig: localScope.loadedConfig,
160 scopeState: localScope.scopeState,94 scopeState: localScope.scopeState,
161 sourceId: SourceIdSchema.make(sourceId),95 sourceId: SourceIdSchema.make(sourceId),
162 actorScopeId: options.actorScopeId,
163 authArtifacts,
164 }),96 }),
165 ({ source }) => source,97 ({ source }) => source,
166 ),98 ),
@@ -241,44 +173,10 @@ export const listLinkedSecretSourcesInWorkspaceWithDeps = ( · 74 unmodified lines
241 never173 never
242> =>174> =>
243 Effect.gen(function* () {175 Effect.gen(function* () {
244 const [sources, authArtifacts, materialIds] = yield* Effect.all([176 yield* loadSourcesInWorkspaceWithDeps(deps, scopeId, {
245 loadSourcesInWorkspaceWithDeps(deps, scopeId, {177 actorScopeId: options.actorScopeId,
246 actorScopeId: options.actorScopeId,178 });
247 }),179 return new Map<string, Array<{ sourceId: string; sourceName: string }>>();
248 deps.executorState.authArtifacts.listByScopeId(scopeId),
249 deps.executorState.secretMaterials.listAll().pipe(
250 Effect.map(
251 (materials) => new Set(materials.map((material) => String(material.id))),
252 ),
253 ),
254 ]);
255
256 const sourceNames = new Map(
257 sources.map((source) => [source.id, source.name] as const),
258 );
259 const linkedSources = new Map<
260 string,
261 Array<{ sourceId: string; sourceName: string }>
262 >();
263
264 for (const artifact of authArtifacts) {
265 for (const ref of authArtifactSecretMaterialRefs(artifact)) {
266 if (!materialIds.has(ref.handle)) {
267 continue;
268 }
269
270 const existing = linkedSources.get(ref.handle) ?? [];
271 if (!existing.some((link) => link.sourceId === artifact.sourceId)) {
272 existing.push({
273 sourceId: artifact.sourceId,
274 sourceName: sourceNames.get(artifact.sourceId) ?? artifact.sourceId,
275 });
276 linkedSources.set(ref.handle, existing);
277 }
278 }
279 }
280
281 return linkedSources;
282 });180 });
283 181
284export const loadSourceByIdWithDeps = (182export const loadSourceByIdWithDeps = (
@@ -301,14 +199,11 @@ export const loadSourceByIdWithDeps = ( · 16 unmodified lines
301 deps,199 deps,
302 input.scopeId,200 input.scopeId,
303 );201 );
304 const authArtifacts = yield* deps.executorState.authArtifacts.listByScopeId(
305 input.scopeId,
306 );
307 if (!localScope.loadedConfig.config?.sources?.[input.sourceId]) {202 if (!localScope.loadedConfig.config?.sources?.[input.sourceId]) {
308 return yield* new LocalConfiguredSourceNotFoundError({203 return yield* new LocalConfiguredSourceNotFoundError({
309 message: `Source not found: scopeId=${input.scopeId} sourceId=${input.sourceId}`,204 message: `Source not found: scopeId=${input.scopeId} sourceId=${input.sourceId}`,
310 sourceId: input.sourceId,205 sourceId: input.sourceId,
311 });206 });
312 }207 }
313 208
314 const localSource = yield* buildLocalSourceRecord({209 const localSource = yield* buildLocalSourceRecord({
@@ -316,8 +211,6 @@ export const loadSourceByIdWithDeps = ( · 1 unmodified line
316 loadedConfig: localScope.loadedConfig,211 loadedConfig: localScope.loadedConfig,
317 scopeState: localScope.scopeState,212 scopeState: localScope.scopeState,
318 sourceId: input.sourceId,213 sourceId: input.sourceId,
319 actorScopeId: input.actorScopeId,
320 authArtifacts,
321 });214 });
322 215
323 return localSource.source;216 return localSource.source;
packages/platform/sdk/src/schema/enums.ts+3−22
@@ -1,12 +1,7 @@
1export {1export {
2 SourceAuthSchema,
3 SourceBindingSchema,
4 SourceBindingVersionSchema,
5 SourceKindSchema,2 SourceKindSchema,
6 SourceStatusSchema,3 SourceStatusSchema,
7 SourceTransportSchema,4 SourceTransportSchema,
8 type SourceAuth,
9 type SourceBinding,
10 type SourceKind,5 type SourceKind,
11 type SourceStatus,6 type SourceStatus,
12 type SourceTransport,7 type SourceTransport,
@@ -14,29 +9,15 @@ export { · 1 unmodified line
14export {9export {
15 SecretRefSchema,10 SecretRefSchema,
16 type SecretRef,11 type SecretRef,
17} from "./models/auth-artifact";12} from "./models/secret-ref";
18export {13export {
19 SourceCatalogAdapterKeySchema,14 SourceCatalogPluginKeySchema,
20 SourceCatalogKindSchema,15 SourceCatalogKindSchema,
21 SourceCatalogVisibilitySchema,16 SourceCatalogVisibilitySchema,
22 type SourceCatalogAdapterKey,17 type SourceCatalogPluginKey,
23 type SourceCatalogKind,18 type SourceCatalogKind,
24 type SourceCatalogVisibility,19 type SourceCatalogVisibility,
25} from "./models/source-catalog";20} from "./models/source-catalog";
26export {
27 AuthArtifactKindSchema,
28 AuthArtifactSlotSchema,
29 BuiltInAuthArtifactKindSchema,
30 type AuthArtifactKind,
31 type AuthArtifactSlot,
32 type BuiltInAuthArtifactKind,
33} from "./models/auth-artifact";
34export {
35 SourceAuthSessionProviderKindSchema,
36 SourceAuthSessionStatusSchema,
37 type SourceAuthSessionProviderKind,
38 type SourceAuthSessionStatus,
39} from "./models/source-auth-session";
40export {21export {
41 LocalScopePolicyApprovalModeSchema,22 LocalScopePolicyApprovalModeSchema,
42 LocalScopePolicyEffectSchema,23 LocalScopePolicyEffectSchema,
packages/platform/sdk/src/schema/ids.ts+0−26
@@ -8,25 +8,6 @@ export const SourceCatalogIdSchema = Schema.String.pipe(Schema.brand("SourceCata · 7 unmodified lines
8export const SourceCatalogRevisionIdSchema = Schema.String.pipe(8export const SourceCatalogRevisionIdSchema = Schema.String.pipe(
9 Schema.brand("SourceCatalogRevisionId"),9 Schema.brand("SourceCatalogRevisionId"),
10);10);
11export const SourceAuthSessionIdSchema = Schema.String.pipe(
12 Schema.brand("SourceAuthSessionId"),
13);
14export const AuthArtifactIdSchema = Schema.String.pipe(
15 Schema.brand("AuthArtifactId"),
16);
17export const AuthLeaseIdSchema = Schema.String.pipe(
18 Schema.brand("AuthLeaseId"),
19);
20export const CredentialIdSchema = AuthArtifactIdSchema;
21export const ScopedSourceOauthClientIdSchema = Schema.String.pipe(
22 Schema.brand("ScopedSourceOauthClientId"),
23);
24export const ScopeOauthClientIdSchema = Schema.String.pipe(
25 Schema.brand("ScopeOauthClientId"),
26);
27export const ProviderAuthGrantIdSchema = Schema.String.pipe(
28 Schema.brand("ProviderAuthGrantId"),
29);
30export const SecretMaterialIdSchema = Schema.String.pipe(11export const SecretMaterialIdSchema = Schema.String.pipe(
31 Schema.brand("SecretMaterialId"),12 Schema.brand("SecretMaterialId"),
32);13);
@@ -43,13 +24,6 @@ export type ScopeId = typeof ScopeIdSchema.Type; · 10 unmodified lines
43export type SourceId = typeof SourceIdSchema.Type;24export type SourceId = typeof SourceIdSchema.Type;
44export type SourceCatalogId = typeof SourceCatalogIdSchema.Type;25export type SourceCatalogId = typeof SourceCatalogIdSchema.Type;
45export type SourceCatalogRevisionId = typeof SourceCatalogRevisionIdSchema.Type;26export type SourceCatalogRevisionId = typeof SourceCatalogRevisionIdSchema.Type;
46export type SourceAuthSessionId = typeof SourceAuthSessionIdSchema.Type;
47export type AuthArtifactId = typeof AuthArtifactIdSchema.Type;
48export type AuthLeaseId = typeof AuthLeaseIdSchema.Type;
49export type CredentialId = typeof CredentialIdSchema.Type;
50export type ScopedSourceOauthClientId = typeof ScopedSourceOauthClientIdSchema.Type;
51export type ScopeOauthClientId = typeof ScopeOauthClientIdSchema.Type;
52export type ProviderAuthGrantId = typeof ProviderAuthGrantIdSchema.Type;
53export type SecretMaterialId = typeof SecretMaterialIdSchema.Type;27export type SecretMaterialId = typeof SecretMaterialIdSchema.Type;
54export type PolicyId = typeof PolicyIdSchema.Type;28export type PolicyId = typeof PolicyIdSchema.Type;
55export type ExecutionId = typeof ExecutionIdSchema.Type;29export type ExecutionId = typeof ExecutionIdSchema.Type;
packages/platform/sdk/src/schema/index.ts+1−6
@@ -4,13 +4,8 @@ export * from "./ids"; · 3 unmodified lines
4 4
5export * from "./models/source";5export * from "./models/source";
6export * from "./models/source-catalog";6export * from "./models/source-catalog";
7export * from "./models/auth-artifact";7export * from "./models/secret-ref";
8export * from "./models/auth-lease";
9export * from "./models/source-oauth-client";
10export * from "./models/scope-oauth-client";
11export * from "./models/provider-auth-grant";
12export * from "./models/secret-material";8export * from "./models/secret-material";
13export * from "./models/source-auth-session";
14export * from "./models/policy";9export * from "./models/policy";
15export * from "./models/local-installation";10export * from "./models/local-installation";
16export * from "./models/local-config";11export * from "./models/local-config";
packages/platform/sdk/src/schema/models/auth-artifact.ts+0−375
@@ -1,375 +0,0 @@
1import {
2 Schema,
3} from "effect";
4import * as Option from "effect/Option";
5
6import {
7 TimestampMsSchema,
8} from "../common";
9import {
10 ScopeIdSchema,
11 AuthArtifactIdSchema,
12 ProviderAuthGrantIdSchema,
13 SourceIdSchema,
14} from "../ids";
15
16export const SecretRefSchema = Schema.Struct({
17 providerId: Schema.String,
18 handle: Schema.String,
19});
20
21export const AuthArtifactSlotSchema = Schema.Literal("runtime", "import");
22export const CredentialSlotSchema = AuthArtifactSlotSchema;
23
24export const AuthArtifactKindSchema = Schema.String;
25export const AuthGrantSetSchema = Schema.Array(Schema.String);
26
27export const RequestPlacementPartSchema = Schema.Union(
28 Schema.Struct({
29 kind: Schema.Literal("literal"),
30 value: Schema.String,
31 }),
32 Schema.Struct({
33 kind: Schema.Literal("secret_ref"),
34 ref: SecretRefSchema,
35 }),
36);
37
38export const RequestPlacementTemplateSchema = Schema.Union(
39 Schema.Struct({
40 location: Schema.Literal("header"),
41 name: Schema.String,
42 parts: Schema.Array(RequestPlacementPartSchema),
43 }),
44 Schema.Struct({
45 location: Schema.Literal("query"),
46 name: Schema.String,
47 parts: Schema.Array(RequestPlacementPartSchema),
48 }),
49 Schema.Struct({
50 location: Schema.Literal("cookie"),
51 name: Schema.String,
52 parts: Schema.Array(RequestPlacementPartSchema),
53 }),
54 Schema.Struct({
55 location: Schema.Literal("body"),
56 path: Schema.String,
57 parts: Schema.Array(RequestPlacementPartSchema),
58 }),
59);
60
61export const RequestPlacementSchema = Schema.Union(
62 Schema.Struct({
63 location: Schema.Literal("header"),
64 name: Schema.String,
65 value: Schema.String,
66 }),
67 Schema.Struct({
68 location: Schema.Literal("query"),
69 name: Schema.String,
70 value: Schema.String,
71 }),
72 Schema.Struct({
73 location: Schema.Literal("cookie"),
74 name: Schema.String,
75 value: Schema.String,
76 }),
77 Schema.Struct({
78 location: Schema.Literal("body"),
79 path: Schema.String,
80 value: Schema.String,
81 }),
82);
83
84export const RequestPlacementTemplatesJsonSchema = Schema.parseJson(
85 Schema.Array(RequestPlacementTemplateSchema),
86);
87
88export const StaticBearerAuthArtifactKind = "static_bearer" as const;
89export const StaticOAuth2AuthArtifactKind = "static_oauth2" as const;
90export const StaticPlacementsAuthArtifactKind = "static_placements" as const;
91export const RefreshableOAuth2AuthorizedUserAuthArtifactKind =
92 "oauth2_authorized_user" as const;
93export const ProviderGrantRefAuthArtifactKind = "provider_grant_ref" as const;
94export const McpOAuthAuthArtifactKind = "mcp_oauth" as const;
95
96export const OAuth2ClientAuthenticationMethodSchema = Schema.Literal(
97 "none",
98 "client_secret_post",
99);
100
101export const BuiltInAuthArtifactKindSchema = Schema.Literal(
102 StaticBearerAuthArtifactKind,
103 StaticOAuth2AuthArtifactKind,
104 StaticPlacementsAuthArtifactKind,
105 RefreshableOAuth2AuthorizedUserAuthArtifactKind,
106);
107
108export const StaticBearerAuthArtifactConfigSchema = Schema.Struct({
109 headerName: Schema.String,
110 prefix: Schema.String,
111 token: SecretRefSchema,
112});
113
114export const StaticOAuth2AuthArtifactConfigSchema = Schema.Struct({
115 headerName: Schema.String,
116 prefix: Schema.String,
117 accessToken: SecretRefSchema,
118 refreshToken: Schema.NullOr(SecretRefSchema),
119});
120
121export const StaticPlacementsAuthArtifactConfigSchema = Schema.Struct({
122 placements: Schema.Array(RequestPlacementTemplateSchema),
123});
124
125export const RefreshableOAuth2AuthorizedUserAuthArtifactConfigSchema = Schema.Struct({
126 headerName: Schema.String,
127 prefix: Schema.String,
128 tokenEndpoint: Schema.String,
129 clientId: Schema.String,
130 clientAuthentication: OAuth2ClientAuthenticationMethodSchema,
131 clientSecret: Schema.NullOr(SecretRefSchema),
132 refreshToken: SecretRefSchema,
133});
134
135export const ProviderGrantRefAuthArtifactConfigSchema = Schema.Struct({
136 grantId: ProviderAuthGrantIdSchema,
137 providerKey: Schema.String,
138 requiredScopes: Schema.Array(Schema.String),
139 headerName: Schema.String,
140 prefix: Schema.String,
141});
142
143export const McpOAuthAuthArtifactConfigSchema = Schema.Struct({
144 redirectUri: Schema.String,
145 accessToken: SecretRefSchema,
146 refreshToken: Schema.NullOr(SecretRefSchema),
147 tokenType: Schema.String,
148 expiresIn: Schema.NullOr(Schema.Number),
149 scope: Schema.NullOr(Schema.String),
150 resourceMetadataUrl: Schema.NullOr(Schema.String),
151 authorizationServerUrl: Schema.NullOr(Schema.String),
152 resourceMetadataJson: Schema.NullOr(Schema.String),
153 authorizationServerMetadataJson: Schema.NullOr(Schema.String),
154 clientInformationJson: Schema.NullOr(Schema.String),
155});
156
157export const StaticBearerAuthArtifactConfigJsonSchema = Schema.parseJson(
158 StaticBearerAuthArtifactConfigSchema,
159);
160
161export const StaticOAuth2AuthArtifactConfigJsonSchema = Schema.parseJson(
162 StaticOAuth2AuthArtifactConfigSchema,
163);
164
165export const StaticPlacementsAuthArtifactConfigJsonSchema = Schema.parseJson(
166 StaticPlacementsAuthArtifactConfigSchema,
167);
168
169export const RefreshableOAuth2AuthorizedUserAuthArtifactConfigJsonSchema = Schema.parseJson(
170 RefreshableOAuth2AuthorizedUserAuthArtifactConfigSchema,
171);
172
173export const ProviderGrantRefAuthArtifactConfigJsonSchema = Schema.parseJson(
174 ProviderGrantRefAuthArtifactConfigSchema,
175);
176
177export const McpOAuthAuthArtifactConfigJsonSchema = Schema.parseJson(
178 McpOAuthAuthArtifactConfigSchema,
179);
180
181export const RequestPlacementsJsonSchema = Schema.parseJson(
182 Schema.Array(RequestPlacementSchema),
183);
184
185export const AuthGrantSetJsonSchema = Schema.parseJson(AuthGrantSetSchema);
186
187const decodeStaticBearerAuthArtifactConfigOption = Schema.decodeUnknownOption(
188 StaticBearerAuthArtifactConfigJsonSchema,
189);
190
191const decodeStaticOAuth2AuthArtifactConfigOption = Schema.decodeUnknownOption(
192 StaticOAuth2AuthArtifactConfigJsonSchema,
193);
194
195const decodeStaticPlacementsAuthArtifactConfigOption = Schema.decodeUnknownOption(
196 StaticPlacementsAuthArtifactConfigJsonSchema,
197);
198
199const decodeRefreshableOAuth2AuthorizedUserAuthArtifactConfigOption = Schema.decodeUnknownOption(
200 RefreshableOAuth2AuthorizedUserAuthArtifactConfigJsonSchema,
201);
202const decodeProviderGrantRefAuthArtifactConfigOption = Schema.decodeUnknownOption(
203 ProviderGrantRefAuthArtifactConfigJsonSchema,
204);
205const decodeMcpOAuthAuthArtifactConfigOption = Schema.decodeUnknownOption(
206 McpOAuthAuthArtifactConfigJsonSchema,
207);
208
209const decodeAuthGrantSetOption = Schema.decodeUnknownOption(AuthGrantSetJsonSchema);
210
211export const AuthArtifactSchema = Schema.Struct({
212 id: AuthArtifactIdSchema,
213 scopeId: ScopeIdSchema,
214 sourceId: SourceIdSchema,
215 actorScopeId: Schema.NullOr(ScopeIdSchema),
216 slot: AuthArtifactSlotSchema,
217 artifactKind: AuthArtifactKindSchema,
218 configJson: Schema.String,
219 grantSetJson: Schema.NullOr(Schema.String),
220 createdAt: TimestampMsSchema,
221 updatedAt: TimestampMsSchema,
222});
223
224export type SecretRef = typeof SecretRefSchema.Type;
225export type AuthArtifactSlot = typeof AuthArtifactSlotSchema.Type;
226export type CredentialSlot = typeof CredentialSlotSchema.Type;
227export type AuthArtifactKind = typeof AuthArtifactKindSchema.Type;
228export type AuthGrantSet = typeof AuthGrantSetSchema.Type;
229export type RequestPlacementPart = typeof RequestPlacementPartSchema.Type;
230export type RequestPlacementTemplate = typeof RequestPlacementTemplateSchema.Type;
231export type RequestPlacement = typeof RequestPlacementSchema.Type;
232export type OAuth2ClientAuthenticationMethod =
233 typeof OAuth2ClientAuthenticationMethodSchema.Type;
234export type BuiltInAuthArtifactKind = typeof BuiltInAuthArtifactKindSchema.Type;
235export type StaticBearerAuthArtifactConfig = typeof StaticBearerAuthArtifactConfigSchema.Type;
236export type StaticOAuth2AuthArtifactConfig = typeof StaticOAuth2AuthArtifactConfigSchema.Type;
237export type StaticPlacementsAuthArtifactConfig = typeof StaticPlacementsAuthArtifactConfigSchema.Type;
238export type RefreshableOAuth2AuthorizedUserAuthArtifactConfig =
239 typeof RefreshableOAuth2AuthorizedUserAuthArtifactConfigSchema.Type;
240export type ProviderGrantRefAuthArtifactConfig =
241 typeof ProviderGrantRefAuthArtifactConfigSchema.Type;
242export type McpOAuthAuthArtifactConfig = typeof McpOAuthAuthArtifactConfigSchema.Type;
243export type AuthArtifact = typeof AuthArtifactSchema.Type;
244
245export type DecodedBuiltInAuthArtifactConfig =
246 | {
247 artifactKind: typeof StaticBearerAuthArtifactKind;
248 config: StaticBearerAuthArtifactConfig;
249 }
250 | {
251 artifactKind: typeof StaticOAuth2AuthArtifactKind;
252 config: StaticOAuth2AuthArtifactConfig;
253 }
254 | {
255 artifactKind: typeof StaticPlacementsAuthArtifactKind;
256 config: StaticPlacementsAuthArtifactConfig;
257 }
258 | {
259 artifactKind: typeof RefreshableOAuth2AuthorizedUserAuthArtifactKind;
260 config: RefreshableOAuth2AuthorizedUserAuthArtifactConfig;
261 };
262
263export const decodeProviderGrantRefAuthArtifactConfig = (
264 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
265): ProviderGrantRefAuthArtifactConfig | null => {
266 if (artifact.artifactKind !== ProviderGrantRefAuthArtifactKind) {
267 return null;
268 }
269
270 const decoded = decodeProviderGrantRefAuthArtifactConfigOption(artifact.configJson);
271 return Option.isSome(decoded) ? decoded.value : null;
272};
273
274export const decodeMcpOAuthAuthArtifactConfig = (
275 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
276): McpOAuthAuthArtifactConfig | null => {
277 if (artifact.artifactKind !== McpOAuthAuthArtifactKind) {
278 return null;
279 }
280
281 const decoded = decodeMcpOAuthAuthArtifactConfigOption(artifact.configJson);
282 return Option.isSome(decoded) ? decoded.value : null;
283};
284
285export const decodeBuiltInAuthArtifactConfig = (
286 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
287): DecodedBuiltInAuthArtifactConfig | null => {
288 switch (artifact.artifactKind) {
289 case StaticBearerAuthArtifactKind: {
290 const decoded = decodeStaticBearerAuthArtifactConfigOption(artifact.configJson);
291 return Option.isSome(decoded)
292 ? { artifactKind: StaticBearerAuthArtifactKind, config: decoded.value }
293 : null;
294 }
295 case StaticOAuth2AuthArtifactKind: {
296 const decoded = decodeStaticOAuth2AuthArtifactConfigOption(artifact.configJson);
297 return Option.isSome(decoded)
298 ? { artifactKind: StaticOAuth2AuthArtifactKind, config: decoded.value }
299 : null;
300 }
301 case StaticPlacementsAuthArtifactKind: {
302 const decoded = decodeStaticPlacementsAuthArtifactConfigOption(artifact.configJson);
303 return Option.isSome(decoded)
304 ? { artifactKind: StaticPlacementsAuthArtifactKind, config: decoded.value }
305 : null;
306 }
307 case RefreshableOAuth2AuthorizedUserAuthArtifactKind: {
308 const decoded = decodeRefreshableOAuth2AuthorizedUserAuthArtifactConfigOption(
309 artifact.configJson,
310 );
311 return Option.isSome(decoded)
312 ? {
313 artifactKind: RefreshableOAuth2AuthorizedUserAuthArtifactKind,
314 config: decoded.value,
315 }
316 : null;
317 }
318 default:
319 return null;
320 }
321};
322
323export const decodeAuthArtifactGrantSet = (
324 artifact: Pick<AuthArtifact, "grantSetJson"> | string | null,
325): AuthGrantSet | null => {
326 const grantSetJson = artifact !== null && typeof artifact === "object"
327 ? artifact.grantSetJson
328 : artifact;
329
330 if (grantSetJson === null) {
331 return null;
332 }
333
334 const decoded = decodeAuthGrantSetOption(grantSetJson);
335 return Option.isSome(decoded) ? decoded.value : null;
336};
337
338export const decodeAuthGrantSet = decodeAuthArtifactGrantSet;
339
340export const authArtifactSecretRefs = (
341 artifact: Pick<AuthArtifact, "artifactKind" | "configJson">,
342): ReadonlyArray<SecretRef> => {
343 const decoded = decodeBuiltInAuthArtifactConfig(artifact);
344 if (decoded !== null) {
345 switch (decoded.artifactKind) {
346 case StaticBearerAuthArtifactKind:
347 return [decoded.config.token];
348 case StaticOAuth2AuthArtifactKind:
349 return decoded.config.refreshToken
350 ? [decoded.config.accessToken, decoded.config.refreshToken]
351 : [decoded.config.accessToken];
352 case StaticPlacementsAuthArtifactKind:
353 return decoded.config.placements.flatMap((placement) =>
354 placement.parts.flatMap((part) =>
355 part.kind === "secret_ref" ? [part.ref] : [],
356 )
357 );
358 case RefreshableOAuth2AuthorizedUserAuthArtifactKind:
359 return decoded.config.clientSecret
360 ? [decoded.config.refreshToken, decoded.config.clientSecret]
361 : [decoded.config.refreshToken];
362 }
363 }
364
365 const mcpOAuthConfig = decodeMcpOAuthAuthArtifactConfig(artifact);
366 if (mcpOAuthConfig !== null) {
367 return mcpOAuthConfig.refreshToken
368 ? [mcpOAuthConfig.accessToken, mcpOAuthConfig.refreshToken]
369 : [mcpOAuthConfig.accessToken];
370 }
371
372 return [];
373};
374
375export const authArtifactSecretMaterialRefs = authArtifactSecretRefs;
packages/platform/sdk/src/schema/models/auth-lease.ts+0−54
@@ -1,54 +0,0 @@
1import {
2 Schema,
3} from "effect";
4import * as Option from "effect/Option";
5
6import {
7 TimestampMsSchema,
8} from "../common";
9import {
10 ScopeIdSchema,
11 AuthArtifactIdSchema,
12 AuthLeaseIdSchema,
13 SourceIdSchema,
14} from "../ids";
15
16import {
17 AuthArtifactSlotSchema,
18 RequestPlacementTemplatesJsonSchema,
19 type RequestPlacementTemplate,
20} from "./auth-artifact";
21
22export const AuthLeaseSchema = Schema.Struct({
23 id: AuthLeaseIdSchema,
24 authArtifactId: AuthArtifactIdSchema,
25 scopeId: ScopeIdSchema,
26 sourceId: SourceIdSchema,
27 actorScopeId: Schema.NullOr(ScopeIdSchema),
28 slot: AuthArtifactSlotSchema,
29 placementsTemplateJson: Schema.String,
30 expiresAt: Schema.NullOr(TimestampMsSchema),
31 refreshAfter: Schema.NullOr(TimestampMsSchema),
32 createdAt: TimestampMsSchema,
33 updatedAt: TimestampMsSchema,
34});
35
36export type AuthLease = typeof AuthLeaseSchema.Type;
37
38const decodeLeasePlacementTemplatesOption = Schema.decodeUnknownOption(
39 RequestPlacementTemplatesJsonSchema,
40);
41
42export const decodeAuthLeasePlacementTemplates = (
43 lease: Pick<AuthLease, "placementsTemplateJson">,
44): ReadonlyArray<RequestPlacementTemplate> | null => {
45 const decoded = decodeLeasePlacementTemplatesOption(lease.placementsTemplateJson);
46 return Option.isSome(decoded) ? decoded.value : null;
47};
48
49export const authLeaseSecretRefs = (
50 lease: Pick<AuthLease, "placementsTemplateJson">,
51) =>
52 (decodeAuthLeasePlacementTemplates(lease) ?? []).flatMap((placement) =>
53 placement.parts.flatMap((part) => (part.kind === "secret_ref" ? [part.ref] : [])),
54 );
packages/platform/sdk/src/schema/models/local-config.ts+0−6
@@ -2,10 +2,6 @@ import { · 1 unmodified line
2 Schema,2 Schema,
3} from "effect";3} from "effect";
4 4
5import {
6 JsonObjectSchema,
7} from "@executor/source-core";
8
9export const LocalExecutorRuntimeSchema = Schema.Literal(5export const LocalExecutorRuntimeSchema = Schema.Literal(
10 "quickjs",6 "quickjs",
11 "ses",7 "ses",
@@ -62,7 +58,6 @@ export const LocalConfigSecretInputSchema = Schema.Union( · 50 unmodified lines
62 58
63export const LocalConfigSourceConnectionSchema = Schema.Struct({59export const LocalConfigSourceConnectionSchema = Schema.Struct({
64 endpoint: Schema.String,60 endpoint: Schema.String,
65 auth: Schema.optional(LocalConfigSecretInputSchema),
66});61});
67 62
68const LocalConfigSourceEntryBaseSchema = Schema.Struct({63const LocalConfigSourceEntryBaseSchema = Schema.Struct({
@@ -76,7 +71,6 @@ export const LocalConfigSourceSchema = Schema.extend( · 7 unmodified lines
76 LocalConfigSourceEntryBaseSchema,71 LocalConfigSourceEntryBaseSchema,
77 Schema.Struct({72 Schema.Struct({
78 kind: Schema.String,73 kind: Schema.String,
79 binding: JsonObjectSchema,
80 }),74 }),
81);75);
82 76
packages/platform/sdk/src/schema/models/provider-auth-grant.ts+0−37
@@ -1,37 +0,0 @@
1import {
2 Schema,
3} from "effect";
4
5import {
6 TimestampMsSchema,
7} from "../common";
8import {
9 ScopeIdSchema,
10 ProviderAuthGrantIdSchema,
11 ScopeOauthClientIdSchema,
12} from "../ids";
13
14import {
15 OAuth2ClientAuthenticationMethodSchema,
16 SecretRefSchema,
17} from "./auth-artifact";
18
19export const ProviderAuthGrantSchema = Schema.Struct({
20 id: ProviderAuthGrantIdSchema,
21 scopeId: ScopeIdSchema,
22 actorScopeId: Schema.NullOr(ScopeIdSchema),
23 providerKey: Schema.String,
24 oauthClientId: ScopeOauthClientIdSchema,
25 tokenEndpoint: Schema.String,
26 clientAuthentication: OAuth2ClientAuthenticationMethodSchema,
27 headerName: Schema.String,
28 prefix: Schema.String,
29 refreshToken: SecretRefSchema,
30 grantedScopes: Schema.Array(Schema.String),
31 lastRefreshedAt: Schema.NullOr(TimestampMsSchema),
32 orphanedAt: Schema.NullOr(TimestampMsSchema),
33 createdAt: TimestampMsSchema,
34 updatedAt: TimestampMsSchema,
35});
36
37export type ProviderAuthGrant = typeof ProviderAuthGrantSchema.Type;
packages/platform/sdk/src/schema/models/scope-oauth-client.ts+0−26
@@ -1,26 +0,0 @@
1import {
2 Schema,
3} from "effect";
4
5import {
6 TimestampMsSchema,
7} from "../common";
8import {
9 ScopeIdSchema,
10 ScopeOauthClientIdSchema,
11} from "../ids";
12
13export const ScopeOauthClientSchema = Schema.Struct({
14 id: ScopeOauthClientIdSchema,
15 scopeId: ScopeIdSchema,
16 providerKey: Schema.String,
17 label: Schema.NullOr(Schema.String),
18 clientId: Schema.String,
19 clientSecretProviderId: Schema.NullOr(Schema.String),
20 clientSecretHandle: Schema.NullOr(Schema.String),
21 clientMetadataJson: Schema.NullOr(Schema.String),
22 createdAt: TimestampMsSchema,
23 updatedAt: TimestampMsSchema,
24});
25
26export type ScopeOauthClient = typeof ScopeOauthClientSchema.Type;
packages/platform/sdk/src/schema/models/secret-ref.ts+8−0
@@ -0,0 +1,8 @@
1import * as Schema from "effect/Schema";
2
3export const SecretRefSchema = Schema.Struct({
4 providerId: Schema.String,
5 handle: Schema.String,
6});
7
8export type SecretRef = typeof SecretRefSchema.Type;
packages/platform/sdk/src/schema/models/source-auth-session.ts+0−159
@@ -1,159 +0,0 @@
1import {
2 Schema,
3} from "effect";
4
5import {
6 TimestampMsSchema,
7} from "../common";
8import {
9 ScopeIdSchema,
10 ExecutionIdSchema,
11 ExecutionInteractionIdSchema,
12 SourceIdSchema,
13 SourceAuthSessionIdSchema,
14 ScopeOauthClientIdSchema,
15} from "../ids";
16import {
17 CredentialSlotSchema,
18 OAuth2ClientAuthenticationMethodSchema,
19 SecretRefSchema,
20} from "./auth-artifact";
21
22export const SourceAuthSessionProviderKindSchema = Schema.String;
23
24export const SourceAuthSessionStatusSchema = Schema.Literal(
25 "pending",
26 "completed",
27 "failed",
28 "cancelled",
29);
30
31export type JsonValue =
32 | string
33 | number
34 | boolean
35 | null
36 | JsonObject
37 | ReadonlyArray<JsonValue>;
38
39export type JsonObject = {
40 readonly [key: string]: JsonValue;
41};
42
43export const JsonValueSchema: Schema.Schema<JsonValue> = Schema.suspend(() =>
44 Schema.Union(
45 Schema.String,
46 Schema.Number,
47 Schema.Boolean,
48 Schema.Null,
49 Schema.Array(JsonValueSchema),
50 Schema.Record({
51 key: Schema.String,
52 value: JsonValueSchema,
53 }),
54 )
55).annotations({ identifier: "JsonValue" });
56
57export const JsonObjectSchema = Schema.Record({
58 key: Schema.String,
59 value: JsonValueSchema,
60}).annotations({ identifier: "JsonObject" });
61
62export const McpSourceAuthSessionDataSchema = Schema.Struct({
63 kind: Schema.Literal("mcp_oauth"),
64 endpoint: Schema.String,
65 redirectUri: Schema.String,
66 scope: Schema.NullOr(Schema.String),
67 resourceMetadataUrl: Schema.NullOr(Schema.String),
68 authorizationServerUrl: Schema.NullOr(Schema.String),
69 resourceMetadata: Schema.NullOr(JsonObjectSchema),
70 authorizationServerMetadata: Schema.NullOr(JsonObjectSchema),
71 clientInformation: Schema.NullOr(JsonObjectSchema),
72 codeVerifier: Schema.NullOr(Schema.String),
73 authorizationUrl: Schema.NullOr(Schema.String),
74});
75
76export const McpSourceAuthSessionDataJsonSchema = Schema.parseJson(
77 McpSourceAuthSessionDataSchema,
78);
79
80export const OAuth2PkceSourceAuthSessionDataSchema = Schema.Struct({
81 kind: Schema.Literal("oauth2_pkce"),
82 providerKey: Schema.String,
83 authorizationEndpoint: Schema.String,
84 tokenEndpoint: Schema.String,
85 redirectUri: Schema.String,
86 clientId: Schema.String,
87 clientAuthentication: OAuth2ClientAuthenticationMethodSchema,
88 clientSecret: Schema.NullOr(SecretRefSchema),
89 scopes: Schema.Array(Schema.String),
90 headerName: Schema.String,
91 prefix: Schema.String,
92 authorizationParams: Schema.Record({
93 key: Schema.String,
94 value: Schema.String,
95 }),
96 codeVerifier: Schema.NullOr(Schema.String),
97 authorizationUrl: Schema.NullOr(Schema.String),
98});
99
100export const OAuth2PkceSourceAuthSessionDataJsonSchema = Schema.parseJson(
101 OAuth2PkceSourceAuthSessionDataSchema,
102);
103
104export const ProviderOauthTargetSourceSchema = Schema.Struct({
105 sourceId: SourceIdSchema,
106 requiredScopes: Schema.Array(Schema.String),
107});
108
109export const ProviderOauthBatchSourceAuthSessionDataSchema = Schema.Struct({
110 kind: Schema.Literal("provider_oauth_batch"),
111 providerKey: Schema.String,
112 authorizationEndpoint: Schema.String,
113 tokenEndpoint: Schema.String,
114 redirectUri: Schema.String,
115 oauthClientId: ScopeOauthClientIdSchema,
116 clientAuthentication: OAuth2ClientAuthenticationMethodSchema,
117 scopes: Schema.Array(Schema.String),
118 headerName: Schema.String,
119 prefix: Schema.String,
120 authorizationParams: Schema.Record({
121 key: Schema.String,
122 value: Schema.String,
123 }),
124 targetSources: Schema.Array(ProviderOauthTargetSourceSchema),
125 codeVerifier: Schema.NullOr(Schema.String),
126 authorizationUrl: Schema.NullOr(Schema.String),
127});
128
129export const ProviderOauthBatchSourceAuthSessionDataJsonSchema = Schema.parseJson(
130 ProviderOauthBatchSourceAuthSessionDataSchema,
131);
132
133export const SourceAuthSessionSchema = Schema.Struct({
134 id: SourceAuthSessionIdSchema,
135 scopeId: ScopeIdSchema,
136 sourceId: SourceIdSchema,
137 actorScopeId: Schema.NullOr(ScopeIdSchema),
138 credentialSlot: CredentialSlotSchema,
139 executionId: Schema.NullOr(ExecutionIdSchema),
140 interactionId: Schema.NullOr(ExecutionInteractionIdSchema),
141 providerKind: SourceAuthSessionProviderKindSchema,
142 status: SourceAuthSessionStatusSchema,
143 state: Schema.String,
144 sessionDataJson: Schema.String,
145 errorText: Schema.NullOr(Schema.String),
146 completedAt: Schema.NullOr(TimestampMsSchema),
147 createdAt: TimestampMsSchema,
148 updatedAt: TimestampMsSchema,
149});
150
151export type SourceAuthSessionProviderKind = typeof SourceAuthSessionProviderKindSchema.Type;
152export type SourceAuthSessionStatus = typeof SourceAuthSessionStatusSchema.Type;
153export type McpSourceAuthSessionData = typeof McpSourceAuthSessionDataSchema.Type;
154export type OAuth2PkceSourceAuthSessionData =
155 typeof OAuth2PkceSourceAuthSessionDataSchema.Type;
156export type ProviderOauthTargetSource = typeof ProviderOauthTargetSourceSchema.Type;
157export type ProviderOauthBatchSourceAuthSessionData =
158 typeof ProviderOauthBatchSourceAuthSessionDataSchema.Type;
159export type SourceAuthSession = typeof SourceAuthSessionSchema.Type;
packages/platform/sdk/src/schema/models/source-catalog.ts+3−4
@@ -15,7 +15,7 @@ export const SourceCatalogKindSchema = Schema.Literal( · 14 unmodified lines
15 "internal",15 "internal",
16);16);
17 17
18export const SourceCatalogAdapterKeySchema = Schema.String;18export const SourceCatalogPluginKeySchema = Schema.String;
19 19
20export const SourceCatalogVisibilitySchema = Schema.Literal(20export const SourceCatalogVisibilitySchema = Schema.Literal(
21 "private",21 "private",
@@ -27,8 +27,7 @@ export const SourceCatalogVisibilitySchema = Schema.Literal( · 5 unmodified lines
27export const StoredSourceCatalogRecordSchema = Schema.Struct({27export const StoredSourceCatalogRecordSchema = Schema.Struct({
28 id: SourceCatalogIdSchema,28 id: SourceCatalogIdSchema,
29 kind: SourceCatalogKindSchema,29 kind: SourceCatalogKindSchema,
30 adapterKey: SourceCatalogAdapterKeySchema,30 pluginKey: SourceCatalogPluginKeySchema,
31 providerKey: Schema.String,
32 name: Schema.String,31 name: Schema.String,
33 summary: Schema.NullOr(Schema.String),32 summary: Schema.NullOr(Schema.String),
34 visibility: SourceCatalogVisibilitySchema,33 visibility: SourceCatalogVisibilitySchema,
@@ -50,7 +49,7 @@ export const StoredSourceCatalogRevisionRecordSchema = Schema.Struct({ · 15 unmodified lines
50});49});
51 50
52export type SourceCatalogKind = typeof SourceCatalogKindSchema.Type;51export type SourceCatalogKind = typeof SourceCatalogKindSchema.Type;
53export type SourceCatalogAdapterKey = typeof SourceCatalogAdapterKeySchema.Type;52export type SourceCatalogPluginKey = typeof SourceCatalogPluginKeySchema.Type;
54export type SourceCatalogVisibility = typeof SourceCatalogVisibilitySchema.Type;53export type SourceCatalogVisibility = typeof SourceCatalogVisibilitySchema.Type;
55export type StoredSourceCatalogRecord = typeof StoredSourceCatalogRecordSchema.Type;54export type StoredSourceCatalogRecord = typeof StoredSourceCatalogRecordSchema.Type;
56export type StoredSourceCatalogRevisionRecord = typeof StoredSourceCatalogRevisionRecordSchema.Type;55export type StoredSourceCatalogRevisionRecord = typeof StoredSourceCatalogRevisionRecordSchema.Type;
packages/platform/sdk/src/schema/models/source-oauth-client.ts+0−48
@@ -1,48 +0,0 @@
1import {
2 Schema,
3} from "effect";
4export {
5 SourceOauthClientInputSchema,
6 ScopedSourceOauthClientRedirectModeSchema,
7} from "@executor/source-core";
8import {
9 SourceOauthClientInputSchema,
10 ScopedSourceOauthClientRedirectModeSchema,
11} from "@executor/source-core";
12
13import {
14 TimestampMsSchema,
15} from "../common";
16import {
17 SourceIdSchema,
18 ScopeIdSchema,
19 ScopedSourceOauthClientIdSchema,
20} from "../ids";
21
22export const ScopedSourceOauthClientMetadataSchema = Schema.Struct({
23 redirectMode: Schema.optional(ScopedSourceOauthClientRedirectModeSchema),
24});
25
26export const ScopedSourceOauthClientMetadataJsonSchema = Schema.parseJson(
27 ScopedSourceOauthClientMetadataSchema,
28);
29
30export const ScopedSourceOauthClientSchema = Schema.Struct({
31 id: ScopedSourceOauthClientIdSchema,
32 scopeId: ScopeIdSchema,
33 sourceId: SourceIdSchema,
34 providerKey: Schema.String,
35 clientId: Schema.String,
36 clientSecretProviderId: Schema.NullOr(Schema.String),
37 clientSecretHandle: Schema.NullOr(Schema.String),
38 clientMetadataJson: Schema.NullOr(Schema.String),
39 createdAt: TimestampMsSchema,
40 updatedAt: TimestampMsSchema,
41});
42
43export type ScopedSourceOauthClient = typeof ScopedSourceOauthClientSchema.Type;
44export type ScopedSourceOauthClientRedirectMode =
45 typeof ScopedSourceOauthClientRedirectModeSchema.Type;
46export type ScopedSourceOauthClientMetadata =
47 typeof ScopedSourceOauthClientMetadataSchema.Type;
48export type SourceOauthClientInput = typeof SourceOauthClientInputSchema.Type;
packages/platform/sdk/src/schema/models/source.ts+0−85
@@ -2,12 +2,10 @@ import { · 1 unmodified line
2 Schema,2 Schema,
3} from "effect";3} from "effect";
4export {4export {
5 SourceImportAuthPolicySchema,
6 SourceTransportSchema,5 SourceTransportSchema,
7 StringMapSchema,6 StringMapSchema,
8} from "@executor/source-core";7} from "@executor/source-core";
9import {8import {
10 SourceImportAuthPolicySchema,
11 SourceTransportSchema,9 SourceTransportSchema,
12 StringMapSchema,10 StringMapSchema,
13} from "@executor/source-core";11} from "@executor/source-core";
@@ -16,19 +14,11 @@ import { · 2 unmodified lines
16 TimestampMsSchema,14 TimestampMsSchema,
17} from "../common";15} from "../common";
18import {16import {
19 ProviderAuthGrantIdSchema,
20 SourceIdSchema,17 SourceIdSchema,
21 SourceCatalogIdSchema,18 SourceCatalogIdSchema,
22 SourceCatalogRevisionIdSchema,19 SourceCatalogRevisionIdSchema,
23 ScopeIdSchema,20 ScopeIdSchema,
24} from "../ids";21} from "../ids";
25import {
26 SecretRefSchema,
27} from "./auth-artifact";
28import {
29 JsonObjectSchema,
30} from "./source-auth-session";
31
32export const SourceKindSchema = Schema.String;22export const SourceKindSchema = Schema.String;
33 23
34export const SourceStatusSchema = Schema.Literal(24export const SourceStatusSchema = Schema.Literal(
@@ -39,65 +29,6 @@ export const SourceStatusSchema = Schema.Literal( · 4 unmodified lines
39 "error",29 "error",
40);30);
41 31
42export const SourceAuthSchema = Schema.Union(
43 Schema.Struct({
44 kind: Schema.Literal("none"),
45 }),
46 Schema.Struct({
47 kind: Schema.Literal("bearer"),
48 headerName: Schema.String,
49 prefix: Schema.String,
50 token: SecretRefSchema,
51 }),
52 Schema.Struct({
53 kind: Schema.Literal("oauth2"),
54 headerName: Schema.String,
55 prefix: Schema.String,
56 accessToken: SecretRefSchema,
57 refreshToken: Schema.NullOr(SecretRefSchema),
58 }),
59 Schema.Struct({
60 kind: Schema.Literal("oauth2_authorized_user"),
61 headerName: Schema.String,
62 prefix: Schema.String,
63 tokenEndpoint: Schema.String,
64 clientId: Schema.String,
65 clientAuthentication: Schema.Literal("none", "client_secret_post"),
66 clientSecret: Schema.NullOr(SecretRefSchema),
67 refreshToken: SecretRefSchema,
68 grantSet: Schema.NullOr(Schema.Array(Schema.String)),
69 }),
70 Schema.Struct({
71 kind: Schema.Literal("provider_grant_ref"),
72 grantId: ProviderAuthGrantIdSchema,
73 providerKey: Schema.String,
74 requiredScopes: Schema.Array(Schema.String),
75 headerName: Schema.String,
76 prefix: Schema.String,
77 }),
78 Schema.Struct({
79 kind: Schema.Literal("mcp_oauth"),
80 redirectUri: Schema.String,
81 accessToken: SecretRefSchema,
82 refreshToken: Schema.NullOr(SecretRefSchema),
83 tokenType: Schema.String,
84 expiresIn: Schema.NullOr(Schema.Number),
85 scope: Schema.NullOr(Schema.String),
86 resourceMetadataUrl: Schema.NullOr(Schema.String),
87 authorizationServerUrl: Schema.NullOr(Schema.String),
88 resourceMetadataJson: Schema.NullOr(Schema.String),
89 authorizationServerMetadataJson: Schema.NullOr(Schema.String),
90 clientInformationJson: Schema.NullOr(Schema.String),
91 }),
92);
93
94export const SourceBindingVersionSchema = Schema.Number;
95
96export const SourceBindingSchema = Schema.Struct({
97 version: SourceBindingVersionSchema,
98 payload: JsonObjectSchema,
99});
100
101const SourceStorageRowSchema = Schema.Struct({32const SourceStorageRowSchema = Schema.Struct({
102 scopeId: ScopeIdSchema,33 scopeId: ScopeIdSchema,
103 sourceId: SourceIdSchema,34 sourceId: SourceIdSchema,
@@ -109,8 +40,6 @@ const SourceStorageRowSchema = Schema.Struct({ · 5 unmodified lines
109 status: SourceStatusSchema,40 status: SourceStatusSchema,
110 enabled: Schema.Boolean,41 enabled: Schema.Boolean,
111 namespace: Schema.NullOr(Schema.String),42 namespace: Schema.NullOr(Schema.String),
112 importAuthPolicy: SourceImportAuthPolicySchema,
113 bindingConfigJson: Schema.String,
114 sourceHash: Schema.NullOr(Schema.String),43 sourceHash: Schema.NullOr(Schema.String),
115 lastError: Schema.NullOr(Schema.String),44 lastError: Schema.NullOr(Schema.String),
116 createdAt: TimestampMsSchema,45 createdAt: TimestampMsSchema,
@@ -130,8 +59,6 @@ export const StoredSourceRecordSchema = Schema.transform( · 13 unmodified lines
130 status: SourceStatusSchema,59 status: SourceStatusSchema,
131 enabled: Schema.Boolean,60 enabled: Schema.Boolean,
132 namespace: Schema.NullOr(Schema.String),61 namespace: Schema.NullOr(Schema.String),
133 importAuthPolicy: SourceImportAuthPolicySchema,
134 bindingConfigJson: Schema.String,
135 sourceHash: Schema.NullOr(Schema.String),62 sourceHash: Schema.NullOr(Schema.String),
136 lastError: Schema.NullOr(Schema.String),63 lastError: Schema.NullOr(Schema.String),
137 createdAt: TimestampMsSchema,64 createdAt: TimestampMsSchema,
@@ -150,8 +77,6 @@ export const StoredSourceRecordSchema = Schema.transform( · 12 unmodified lines
150 status: row.status,77 status: row.status,
151 enabled: row.enabled,78 enabled: row.enabled,
152 namespace: row.namespace,79 namespace: row.namespace,
153 importAuthPolicy: row.importAuthPolicy,
154 bindingConfigJson: row.bindingConfigJson,
155 sourceHash: row.sourceHash,80 sourceHash: row.sourceHash,
156 lastError: row.lastError,81 lastError: row.lastError,
157 createdAt: row.createdAt,82 createdAt: row.createdAt,
@@ -168,8 +93,6 @@ export const StoredSourceRecordSchema = Schema.transform( · 10 unmodified lines
168 status: source.status,93 status: source.status,
169 enabled: source.enabled,94 enabled: source.enabled,
170 namespace: source.namespace,95 namespace: source.namespace,
171 importAuthPolicy: source.importAuthPolicy,
172 bindingConfigJson: source.bindingConfigJson,
173 sourceHash: source.sourceHash,96 sourceHash: source.sourceHash,
174 lastError: source.lastError,97 lastError: source.lastError,
175 createdAt: source.createdAt,98 createdAt: source.createdAt,
@@ -187,11 +110,6 @@ export const SourceSchema = Schema.Struct({ · 11 unmodified lines
187 status: SourceStatusSchema,110 status: SourceStatusSchema,
188 enabled: Schema.Boolean,111 enabled: Schema.Boolean,
189 namespace: Schema.NullOr(Schema.String),112 namespace: Schema.NullOr(Schema.String),
190 bindingVersion: SourceBindingVersionSchema,
191 binding: JsonObjectSchema,
192 importAuthPolicy: SourceImportAuthPolicySchema,
193 importAuth: SourceAuthSchema,
194 auth: SourceAuthSchema,
195 sourceHash: Schema.NullOr(Schema.String),113 sourceHash: Schema.NullOr(Schema.String),
196 lastError: Schema.NullOr(Schema.String),114 lastError: Schema.NullOr(Schema.String),
197 createdAt: TimestampMsSchema,115 createdAt: TimestampMsSchema,
@@ -201,9 +119,6 @@ export const SourceSchema = Schema.Struct({ · 3 unmodified lines
201export type SourceKind = typeof SourceKindSchema.Type;119export type SourceKind = typeof SourceKindSchema.Type;
202export type SourceStatus = typeof SourceStatusSchema.Type;120export type SourceStatus = typeof SourceStatusSchema.Type;
203export type SourceTransport = typeof SourceTransportSchema.Type;121export type SourceTransport = typeof SourceTransportSchema.Type;
204export type SourceImportAuthPolicy = typeof SourceImportAuthPolicySchema.Type;
205export type SourceAuth = typeof SourceAuthSchema.Type;
206export type SourceBinding = typeof SourceBindingSchema.Type;
207export type StoredSourceRecord = typeof StoredSourceRecordSchema.Type;122export type StoredSourceRecord = typeof StoredSourceRecordSchema.Type;
208export type StringMap = typeof StringMapSchema.Type;123export type StringMap = typeof StringMapSchema.Type;
209export type Source = typeof SourceSchema.Type;124export type Source = typeof SourceSchema.Type;
packages/platform/sdk/src/sources/contracts.ts+0−97
@@ -1,97 +0,0 @@
1import {
2 ExecutionInteractionIdSchema,
3 JsonObjectSchema,
4 ProviderAuthGrantIdSchema,
5 SourceAuthSchema,
6 SourceImportAuthPolicySchema,
7 SourceKindSchema,
8 SourceSchema,
9 SourceStatusSchema,
10 SourceOauthClientInputSchema,
11 ScopeIdSchema,
12 ScopeOauthClientIdSchema,
13 ScopeOauthClientSchema,
14} from "../schema";
15import * as Schema from "effect/Schema";
16import {
17 OptionalTrimmedNonEmptyStringSchema,
18 TrimmedNonEmptyStringSchema,
19} from "../string-schemas";
20
21const createSourcePayloadRequiredSchema = Schema.Struct({
22 name: TrimmedNonEmptyStringSchema,
23 kind: SourceKindSchema,
24 endpoint: TrimmedNonEmptyStringSchema,
25});
26
27const createSourcePayloadOptionalSchema = Schema.Struct({
28 status: Schema.optional(SourceStatusSchema),
29 enabled: Schema.optional(Schema.Boolean),
30 namespace: Schema.optional(Schema.NullOr(Schema.String)),
31 binding: Schema.optional(JsonObjectSchema),
32 importAuthPolicy: Schema.optional(SourceImportAuthPolicySchema),
33 importAuth: Schema.optional(SourceAuthSchema),
34 auth: Schema.optional(SourceAuthSchema),
35 sourceHash: Schema.optional(Schema.NullOr(Schema.String)),
36 lastError: Schema.optional(Schema.NullOr(Schema.String)),
37});
38
39export const CreateSourcePayloadSchema = Schema.extend(
40 createSourcePayloadRequiredSchema,
41 createSourcePayloadOptionalSchema,
42);
43
44export type CreateSourcePayload = typeof CreateSourcePayloadSchema.Type;
45
46export const UpdateSourcePayloadSchema = Schema.Struct({
47 name: OptionalTrimmedNonEmptyStringSchema,
48 endpoint: OptionalTrimmedNonEmptyStringSchema,
49 status: Schema.optional(SourceStatusSchema),
50 enabled: Schema.optional(Schema.Boolean),
51 namespace: Schema.optional(Schema.NullOr(Schema.String)),
52 binding: Schema.optional(JsonObjectSchema),
53 importAuthPolicy: Schema.optional(SourceImportAuthPolicySchema),
54 importAuth: Schema.optional(SourceAuthSchema),
55 auth: Schema.optional(SourceAuthSchema),
56 sourceHash: Schema.optional(Schema.NullOr(Schema.String)),
57 lastError: Schema.optional(Schema.NullOr(Schema.String)),
58});
59
60export type UpdateSourcePayload = typeof UpdateSourcePayloadSchema.Type;
61
62export const CredentialPageUrlParamsSchema = Schema.Struct({
63 interactionId: ExecutionInteractionIdSchema,
64});
65
66export const CredentialSubmitPayloadSchema = Schema.Struct({
67 action: Schema.optional(Schema.Literal("submit", "continue", "cancel")),
68 token: Schema.optional(Schema.String),
69});
70
71export const CredentialOauthCompleteUrlParamsSchema = Schema.Struct({
72 state: Schema.String,
73 code: Schema.optional(Schema.String),
74 error: Schema.optional(Schema.String),
75 error_description: Schema.optional(Schema.String),
76});
77
78export const ScopeOauthClientQuerySchema = Schema.Struct({
79 providerKey: Schema.String,
80});
81
82export const CreateScopeOauthClientPayloadSchema = Schema.Struct({
83 providerKey: Schema.String,
84 label: Schema.optional(Schema.NullOr(Schema.String)),
85 oauthClient: SourceOauthClientInputSchema,
86});
87
88export type CreateScopeOauthClientPayload =
89 typeof CreateScopeOauthClientPayloadSchema.Type;
90
91export const oauthClientIdParam = ScopeOauthClientIdSchema;
92export const grantIdParam = ProviderAuthGrantIdSchema;
93
94export {
95 ScopeIdSchema,
96 ScopeOauthClientSchema,
97};
packages/platform/sdk/src/sources/inspection.ts+2−2
@@ -101,7 +101,7 @@ const loadSourceCatalogOrEmpty = (input: { · 100 unmodified lines
101const executableDetails = (tool: LoadedSourceCatalogTool) => {101const executableDetails = (tool: LoadedSourceCatalogTool) => {
102 const display = tool.executable.display ?? {};102 const display = tool.executable.display ?? {};
103 return {103 return {
104 protocol: display.protocol ?? tool.executable.adapterKey,104 protocol: display.protocol ?? tool.executable.pluginKey,
105 method: display.method ?? null,105 method: display.method ?? null,
106 pathTemplate: display.pathTemplate ?? null,106 pathTemplate: display.pathTemplate ?? null,
107 rawToolId: display.rawToolId ?? tool.path.split(".").at(-1) ?? null,107 rawToolId: display.rawToolId ?? tool.path.split(".").at(-1) ?? null,
@@ -255,7 +255,7 @@ export const inspectionToolDetailFromTool = ( · 147 unmodified lines
255 jsonSection("Capability", tool.capability),255 jsonSection("Capability", tool.capability),
256 jsonSection("Executable", {256 jsonSection("Executable", {
257 id: tool.executable.id,257 id: tool.executable.id,
258 adapterKey: tool.executable.adapterKey,258 pluginKey: tool.executable.pluginKey,
259 bindingVersion: tool.executable.bindingVersion,259 bindingVersion: tool.executable.bindingVersion,
260 binding: tool.executable.binding,260 binding: tool.executable.binding,
261 projection: tool.executable.projection,261 projection: tool.executable.projection,
packages/platform/sdk/src/sources/operations.ts+46−117
@@ -1,7 +1,3 @@
1import type {
2 CreateSourcePayload,
3 UpdateSourcePayload,
4} from "./contracts";
5import {1import {
6 type ScopeId,2 type ScopeId,
7 SourceIdSchema,3 SourceIdSchema,
@@ -12,12 +8,9 @@ import * as Either from "effect/Either"; · 4 unmodified lines
12import * as Effect from "effect/Effect";8import * as Effect from "effect/Effect";
13 9
14import {10import {
15 createSourceFromPayload,11 normalizeSourceForCreate,
16 updateSourceFromPayload,12 normalizeSourceForSave,
17} from "../runtime/sources/source-definitions";13} from "../runtime/sources/source-definitions";
18import {
19 getSourceAdapterForSource,
20} from "../runtime/sources/source-adapters";
21import {14import {
22 mapPersistenceError,15 mapPersistenceError,
23} from "../runtime/policy/operations-shared";16} from "../runtime/policy/operations-shared";
@@ -43,9 +36,6 @@ const sourceOps = { · 19 unmodified lines
43 remove: operationErrors("sources.remove"),36 remove: operationErrors("sources.remove"),
44} as const;37} as const;
45 38
46const shouldAutoProbeSource = (source: Source): boolean =>
47 getSourceAdapterForSource(source).shouldAutoProbe(source);
48
49const syncArtifactsForSource = (input: {39const syncArtifactsForSource = (input: {
50 store: ExecutorStateStoreShape;40 store: ExecutorStateStoreShape;
51 sourceStore: Effect.Effect.Success<typeof RuntimeSourceStoreService>;41 sourceStore: Effect.Effect.Success<typeof RuntimeSourceStoreService>;
@@ -56,57 +46,21 @@ const syncArtifactsForSource = (input: { · 4 unmodified lines
56 Effect.gen(function* () {46 Effect.gen(function* () {
57 const catalogSyncService = yield* RuntimeSourceCatalogSyncService;47 const catalogSyncService = yield* RuntimeSourceCatalogSyncService;
58 48
59 // For HTTP-backed source kinds that can validate themselves from a remote
60 // document, automatically attempt to probe and connect. This mirrors the
61 // addExecutorSource flow by overriding status to "connected" so the sync
62 // guard passes.
63 const autoProbe = shouldAutoProbeSource(input.source);
64 const sourceForSync = autoProbe
65 ? { ...input.source, status: "connected" as const }
66 : input.source;
67
68 const synced = yield* Effect.either(49 const synced = yield* Effect.either(
69 catalogSyncService.sync({50 catalogSyncService.sync({
70 source: sourceForSync,51 source: input.source,
71 actorScopeId: input.actorScopeId,52 actorScopeId: input.actorScopeId,
72 }),53 }),
73 );54 );
74 55
75 return yield* Either.match(synced, {56 return yield* Either.match(synced, {
76 onRight: () =>57 onRight: () => Effect.succeed(input.source),
77 Effect.gen(function* () {
78 if (autoProbe) {
79 const connectedSource = yield* updateSourceFromPayload({
80 source: input.source,
81 payload: { status: "connected", lastError: null },
82 now: Date.now(),
83 }).pipe(
84 Effect.mapError((cause) =>
85 input.operation.badRequest(
86 "Failed updating source status",
87 cause instanceof Error ? cause.message : String(cause),
88 ),
89 ),
90 );
91 yield* mapPersistenceError(
92 input.operation.child("source_connected"),
93 input.sourceStore.persistSource(connectedSource, {
94 actorScopeId: input.actorScopeId,
95 }),
96 );
97 return connectedSource;
98 }
99 return input.source;
100 }),
101 onLeft: (error) =>58 onLeft: (error) =>
102 Effect.gen(function* () {59 Effect.gen(function* () {
103 if (60 if (input.source.enabled && input.source.status === "connected") {
104 autoProbe ||61 const erroredSource = yield* normalizeSourceForSave({
105 (input.source.enabled && input.source.status === "connected")62 source: {
106 ) {63 ...input.source,
107 const erroredSource = yield* updateSourceFromPayload({
108 source: input.source,
109 payload: {
110 status: "error",64 status: "error",
111 lastError: error.message,65 lastError: error.message,
112 },66 },
@@ -144,35 +98,36 @@ export const listSources = (input: { · 31 unmodified lines
144 Effect.gen(function* () {98 Effect.gen(function* () {
145 const sourceStore = yield* RuntimeSourceStoreService;99 const sourceStore = yield* RuntimeSourceStoreService;
146 100
147 return yield* sourceStore101 return yield* sourceStore.loadSourcesInScope(input.scopeId, {
148 .loadSourcesInScope(input.scopeId, {102 actorScopeId: input.actorScopeId,
149 actorScopeId: input.actorScopeId,103 }).pipe(
150 })104 Effect.mapError((error) =>
151 .pipe(105 sourceOps.list.unknownStorage(
152 Effect.mapError((error) =>106 error,
153 sourceOps.list.unknownStorage(107 "Failed projecting stored sources",
154 error,
155 "Failed projecting stored sources",
156 ),
157 ),108 ),
158 );109 ),
110 );
159 }),111 }),
160 );112 );
161 113
162export const createSource = (input: {114export const createManagedSource = (input: {
163 scopeId: ScopeId;115 scopeId: ScopeId;
164 actorScopeId: ScopeId;116 actorScopeId: ScopeId;
165 payload: CreateSourcePayload;117 source: Omit<
118 Source,
119 "id" | "scopeId" | "createdAt" | "updatedAt"
120 >;
166}) =>121}) =>
167 Effect.flatMap(ExecutorStateStore, (store) =>122 Effect.flatMap(ExecutorStateStore, (store) =>
168 Effect.gen(function* () {123 Effect.gen(function* () {
169 const sourceStore = yield* RuntimeSourceStoreService;124 const sourceStore = yield* RuntimeSourceStoreService;
170 const now = Date.now();125 const now = Date.now();
171 126
172 const source = yield* createSourceFromPayload({127 const source = yield* normalizeSourceForCreate({
173 scopeId: input.scopeId,128 scopeId: input.scopeId,
174 sourceId: SourceIdSchema.make(`src_${crypto.randomUUID()}`),129 sourceId: SourceIdSchema.make(`src_${crypto.randomUUID()}`),
175 payload: input.payload,130 source: input.source,
176 now,131 now,
177 }).pipe(132 }).pipe(
178 Effect.mapError((cause) =>133 Effect.mapError((cause) =>
@@ -211,62 +166,36 @@ export const getSource = (input: { · 32 unmodified lines
211 Effect.gen(function* () {166 Effect.gen(function* () {
212 const sourceStore = yield* RuntimeSourceStoreService;167 const sourceStore = yield* RuntimeSourceStoreService;
213 168
214 return yield* sourceStore169 return yield* sourceStore.loadSourceById({
215 .loadSourceById({170 scopeId: input.scopeId,
216 scopeId: input.scopeId,171 sourceId: input.sourceId,
217 sourceId: input.sourceId,172 actorScopeId: input.actorScopeId,
218 actorScopeId: input.actorScopeId,173 }).pipe(
219 })174 Effect.mapError((cause) =>
220 .pipe(175 cause instanceof Error &&
221 Effect.mapError((cause) =>176 cause.message.startsWith("Source not found:")
222 cause instanceof Error &&177 ? sourceOps.get.notFound(
223 cause.message.startsWith("Source not found:")178 "Source not found",
224 ? sourceOps.get.notFound(179 `scopeId=${input.scopeId} sourceId=${input.sourceId}`,
225 "Source not found",180 )
226 `scopeId=${input.scopeId} sourceId=${input.sourceId}`,181 : sourceOps.get.unknownStorage(
227 )182 cause,
228 : sourceOps.get.unknownStorage(183 "Failed projecting stored source",
229 cause,184 ),
230 "Failed projecting stored source",185 ),
231 ),186 );
232 ),
233 );
234 }),187 }),
235 );188 );
236 189
237export const updateSource = (input: {190export const saveManagedSource = (input: {
238 scopeId: ScopeId;
239 sourceId: SourceId;
240 actorScopeId: ScopeId;191 actorScopeId: ScopeId;
241 payload: UpdateSourcePayload;192 source: Source;
242}) =>193}) =>
243 Effect.flatMap(ExecutorStateStore, (store) =>194 Effect.flatMap(ExecutorStateStore, (store) =>
244 Effect.gen(function* () {195 Effect.gen(function* () {
245 const sourceStore = yield* RuntimeSourceStoreService;196 const sourceStore = yield* RuntimeSourceStoreService;
246 const existingSource = yield* sourceStore197 const updatedSource = yield* normalizeSourceForSave({
247 .loadSourceById({198 source: input.source,
248 scopeId: input.scopeId,
249 sourceId: input.sourceId,
250 actorScopeId: input.actorScopeId,
251 })
252 .pipe(
253 Effect.mapError((cause) =>
254 cause instanceof Error &&
255 cause.message.startsWith("Source not found:")
256 ? sourceOps.update.notFound(
257 "Source not found",
258 `scopeId=${input.scopeId} sourceId=${input.sourceId}`,
259 )
260 : sourceOps.update.unknownStorage(
261 cause,
262 "Failed projecting stored source",
263 ),
264 ),
265 );
266
267 const updatedSource = yield* updateSourceFromPayload({
268 source: existingSource,
269 payload: input.payload,
270 now: Date.now(),199 now: Date.now(),
271 }).pipe(200 }).pipe(
272 Effect.mapError((cause) =>201 Effect.mapError((cause) =>
packages/platform/server/package.json+3−0
@@ -15,6 +15,9 @@ · 14 unmodified lines
15 "@effect/platform": "catalog:",15 "@effect/platform": "catalog:",
16 "@effect/platform-node": "catalog:",16 "@effect/platform-node": "catalog:",
17 "@executor/executor-mcp": "workspace:*",17 "@executor/executor-mcp": "workspace:*",
18 "@executor/plugin-openapi-http": "workspace:*",
19 "@executor/plugin-openapi-shared": "workspace:*",
20 "@executor/plugin-openapi-sdk": "workspace:*",
18 "@executor/platform-api": "workspace:*",21 "@executor/platform-api": "workspace:*",
19 "@executor/platform-internal": "workspace:*",22 "@executor/platform-internal": "workspace:*",
20 "@executor/platform-sdk": "workspace:*",23 "@executor/platform-sdk": "workspace:*",
packages/platform/server/src/index.ts+28−12
@@ -8,6 +8,10 @@ import { dirname, extname, resolve } from "node:path"; · 7 unmodified lines
8import { Readable } from "node:stream";8import { Readable } from "node:stream";
9import { FileSystem, HttpApiBuilder, HttpServer } from "@effect/platform";9import { FileSystem, HttpApiBuilder, HttpServer } from "@effect/platform";
10import { NodeFileSystem } from "@effect/platform-node";10import { NodeFileSystem } from "@effect/platform-node";
11import { openApiHttpPlugin } from "@executor/plugin-openapi-http";
12import {
13 openApiSdkPlugin,
14} from "@executor/plugin-openapi-sdk";
11import {15import {
12 createExecutorApiLayer,16 createExecutorApiLayer,
13} from "@executor/platform-api/http";17} from "@executor/platform-api/http";
@@ -44,6 +48,9 @@ import { · 30 unmodified lines
44 tracingSearchUrl,48 tracingSearchUrl,
45} from "./tracing";49} from "./tracing";
46import { platformServerEffectError } from "./effect-errors";50import { platformServerEffectError } from "./effect-errors";
51import { createFileOpenApiSourceStorage } from "./openapi-source-storage";
52
53export { createFileOpenApiSourceStorage } from "./openapi-source-storage";
47 54
48export {55export {
49 DEFAULT_EXECUTOR_DATA_DIR,56 DEFAULT_EXECUTOR_DATA_DIR,
@@ -107,6 +114,7 @@ type ExecutorMcpHandler = ReturnType<typeof createExecutorMcpRequestHandler>; · 57 unmodified lines
107const EXECUTOR_NPM_DIST_TAGS_PATHNAME = "/v1/app/npm/dist-tags";114const EXECUTOR_NPM_DIST_TAGS_PATHNAME = "/v1/app/npm/dist-tags";
108const EXECUTOR_NPM_DIST_TAGS_URL =115const EXECUTOR_NPM_DIST_TAGS_URL =
109 "https://registry.npmjs.org/-/package/executor/dist-tags";116 "https://registry.npmjs.org/-/package/executor/dist-tags";
117const executorHttpPlugins = [openApiHttpPlugin()] as const;
110 118
111const disposeExecutor = (executor: Executor) =>119const disposeExecutor = (executor: Executor) =>
112 Effect.tryPromise({120 Effect.tryPromise({
@@ -125,6 +133,13 @@ const createExecutorRuntime = ( · 12 unmodified lines
125 workspaceRoot: options.workspaceRoot,133 workspaceRoot: options.workspaceRoot,
126 localDataDir,134 localDataDir,
127 }),135 }),
136 plugins: [
137 openApiSdkPlugin({
138 storage: createFileOpenApiSourceStorage({
139 rootDir: resolve(localDataDir, "plugins", "openapi", "sources"),
140 }),
141 }),
142 ] as const,
128 executionResolver: options.executionResolver,143 executionResolver: options.executionResolver,
129 createInternalToolMap: createWorkspaceExecutorAdminToolMap,144 createInternalToolMap: createWorkspaceExecutorAdminToolMap,
130 resolveSecretMaterial: options.resolveSecretMaterial,145 resolveSecretMaterial: options.resolveSecretMaterial,
@@ -140,20 +155,21 @@ const createExecutorApiWebHandler = ( · 9 unmodified lines
140 tracingRuntime: ReturnType<typeof createLocalTracingRuntimeFromEnv>,155 tracingRuntime: ReturnType<typeof createLocalTracingRuntimeFromEnv>,
141) =>156) =>
142 Effect.acquireRelease(157 Effect.acquireRelease(
143 Effect.sync(() =>158 Effect.sync(() => {
144 HttpApiBuilder.toWebHandler(159 const apiLayer: any = HttpApiBuilder.middlewareOpenApi({
145 Layer.merge(160 path: "/v1/openapi.json",
146 HttpApiBuilder.middlewareOpenApi({ path: "/v1/openapi.json" }).pipe(161 }).pipe(
147 Layer.provideMerge(162 Layer.provideMerge(
148 createExecutorApiLayer(executor).pipe(163 createExecutorApiLayer(executor, {
149 Layer.provideMerge(tracingRuntime?.layer ?? Layer.empty),164 plugins: executorHttpPlugins,
150 ),165 }).pipe(
151 )166 Layer.provideMerge(tracingRuntime?.layer ?? Layer.empty),
152 ),167 ),
153 HttpServer.layerContext,
154 ),168 ),
155 ),169 );
156 ),170 const webLayer: any = Layer.merge(apiLayer, HttpServer.layerContext);
171 return HttpApiBuilder.toWebHandler(webLayer);
172 }),
157 (handler: ExecutorApiWebHandler) => Effect.tryPromise({ try: () => handler.dispose(), catch: (cause) => cause instanceof Error ? cause : new Error(String(cause ?? "web handler dispose failed")) }).pipe(Effect.orDie),173 (handler: ExecutorApiWebHandler) => Effect.tryPromise({ try: () => handler.dispose(), catch: (cause) => cause instanceof Error ? cause : new Error(String(cause ?? "web handler dispose failed")) }).pipe(Effect.orDie),
158 );174 );
159 175
packages/platform/server/src/openapi-source-storage.ts+92−0
@@ -0,0 +1,92 @@
1import { join } from "node:path";
2import { FileSystem } from "@effect/platform";
3import { NodeFileSystem } from "@effect/platform-node";
4import type { OpenApiSourceStorage } from "@executor/plugin-openapi-sdk";
5import {
6 OpenApiStoredSourceDataSchema,
7 type OpenApiStoredSourceData,
8} from "@executor/plugin-openapi-shared";
9import * as Effect from "effect/Effect";
10import * as Schema from "effect/Schema";
11
12const decodeStoredSourceData = Schema.decodeUnknownSync(OpenApiStoredSourceDataSchema);
13const encodeStoredSourceData = Schema.encodeSync(OpenApiStoredSourceDataSchema);
14
15const storagePath = (input: {
16 rootDir: string;
17 scopeId: string;
18 sourceId: string;
19}) => join(input.rootDir, input.scopeId, `${input.sourceId}.json`);
20
21const toError = (cause: unknown): Error =>
22 cause instanceof Error ? cause : new Error(String(cause));
23
24const bindNodeFileSystem = <A, E>(
25 effect: Effect.Effect<A, E, FileSystem.FileSystem>,
26): Effect.Effect<A, E, never> =>
27 effect.pipe(Effect.provide(NodeFileSystem.layer));
28
29const readStoredSourceData = (
30 path: string,
31): Effect.Effect<OpenApiStoredSourceData | null, Error, never> =>
32 bindNodeFileSystem(
33 Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;
35 const exists = yield* fileSystem.exists(path).pipe(Effect.mapError(toError));
36 if (!exists) {
37 return null;
38 }
39
40 const contents = yield* fileSystem.readFileString(path, "utf8").pipe(
41 Effect.mapError(toError),
42 );
43
44 return decodeStoredSourceData(JSON.parse(contents));
45 }),
46 );
47
48export const createFileOpenApiSourceStorage = (input: {
49 rootDir: string;
50}): OpenApiSourceStorage => ({
51 get: ({ scopeId, sourceId }) =>
52 readStoredSourceData(
53 storagePath({
54 rootDir: input.rootDir,
55 scopeId,
56 sourceId,
57 }),
58 ),
59 put: ({ scopeId, sourceId, value }) =>
60 bindNodeFileSystem(
61 Effect.gen(function* () {
62 const fileSystem = yield* FileSystem.FileSystem;
63 const path = storagePath({
64 rootDir: input.rootDir,
65 scopeId,
66 sourceId,
67 });
68 yield* fileSystem.makeDirectory(join(input.rootDir, scopeId), {
69 recursive: true,
70 }).pipe(Effect.mapError(toError));
71 yield* fileSystem.writeFileString(
72 path,
73 `${JSON.stringify(encodeStoredSourceData(value), null, 2)}\n`,
74 ).pipe(Effect.mapError(toError));
75 }),
76 ),
77 remove: ({ scopeId, sourceId }) =>
78 bindNodeFileSystem(
79 Effect.gen(function* () {
80 const fileSystem = yield* FileSystem.FileSystem;
81 const path = storagePath({
82 rootDir: input.rootDir,
83 scopeId,
84 sourceId,
85 });
86 const exists = yield* fileSystem.exists(path).pipe(Effect.mapError(toError));
87 if (exists) {
88 yield* fileSystem.remove(path).pipe(Effect.mapError(toError));
89 }
90 }),
91 ),
92});
packages/sources/builtins/src/index.ts+0−3
@@ -1,3 +0,0 @@
1import type { SourceAdapter } from "@executor/source-core";
2
3export const externalSourceAdapters = [] as const satisfies readonly SourceAdapter[];
packages/sources/core/src/catalog-fragment.ts+1−1
@@ -117,7 +117,7 @@ const createServiceScope = (input: { · 116 unmodified lines
117export const buildCatalogFragment = (input: {117export const buildCatalogFragment = (input: {
118 source: Pick<118 source: Pick<
119 Source,119 Source,
120 "id" | "kind" | "name" | "endpoint" | "namespace" | "binding" | "auth"120 "id" | "kind" | "name" | "endpoint" | "namespace"
121 >;121 >;
122 documents: readonly CatalogSourceDocumentInput[];122 documents: readonly CatalogSourceDocumentInput[];
123 serviceScopeDefaults?: Scope["defaults"];123 serviceScopeDefaults?: Scope["defaults"];
packages/sources/core/src/catalog-shared.ts+3−5
@@ -120,19 +120,17 @@ export const interactionForEffect = ( · 119 unmodified lines
120});120});
121 121
122export const createCatalogImportMetadata = (input: {122export const createCatalogImportMetadata = (input: {
123 source: Pick<Source, "kind" | "endpoint" | "sourceHash" | "binding" | "auth">;123 source: Pick<Source, "kind" | "endpoint" | "sourceHash">;
124 adapterKey: string;124 pluginKey: string;
125}): ImportMetadata => ({125}): ImportMetadata => ({
126 sourceKind: sourceKindFromSource(input.source),126 sourceKind: sourceKindFromSource(input.source),
127 adapterKey: input.adapterKey,127 pluginKey: input.pluginKey,
128 importerVersion: "ir.v1.snapshot_builder",128 importerVersion: "ir.v1.snapshot_builder",
129 importedAt: new Date().toISOString(),129 importedAt: new Date().toISOString(),
130 sourceConfigHash:130 sourceConfigHash:
131 input.source.sourceHash ??131 input.source.sourceHash ??
132 stableHash({132 stableHash({
133 endpoint: input.source.endpoint,133 endpoint: input.source.endpoint,
134 binding: input.source.binding,
135 auth: input.source.auth?.kind ?? null,
136 }),134 }),
137});135});
138 136
packages/sources/core/src/composition.ts+0−90
@@ -1,90 +0,0 @@
1import * as Schema from "effect/Schema";
2
3import { createSourceAdapterRegistry } from "./registry";
4import type { SourceAdapter, SourceAdapterInputSchema } from "./types";
5
6type SchemaTypeOf<TSchema> = TSchema extends { readonly Type: infer T } ? T : never;
7type AdapterUnion<TAdapters extends readonly SourceAdapter[]> = TAdapters[number];
8type ConnectPayloadSchema<TAdapters extends readonly SourceAdapter[]> =
9 NonNullable<AdapterUnion<TAdapters>["connectPayloadSchema"]>;
10type ExecutorAddInputSchema<TAdapters extends readonly SourceAdapter[]> =
11 NonNullable<AdapterUnion<TAdapters>["executorAddInputSchema"]>;
12
13export type ConnectableSourceAdapter<TAdapter extends SourceAdapter = SourceAdapter> = TAdapter & {
14 connectPayloadSchema: SourceAdapterInputSchema;
15};
16
17export type ExecutorAddableSourceAdapter<TAdapter extends SourceAdapter = SourceAdapter> = TAdapter & {
18 executorAddInputSchema: SourceAdapterInputSchema;
19};
20
21export type LocalConfigurableSourceAdapter<TAdapter extends SourceAdapter = SourceAdapter> = TAdapter & {
22 localConfigBindingSchema: SourceAdapterInputSchema;
23};
24
25const isConnectableSourceAdapter = <TAdapter extends SourceAdapter>(
26 adapter: TAdapter,
27): adapter is ConnectableSourceAdapter<TAdapter> =>
28 adapter.connectPayloadSchema !== null;
29
30const isExecutorAddableSourceAdapter = <TAdapter extends SourceAdapter>(
31 adapter: TAdapter,
32): adapter is ExecutorAddableSourceAdapter<TAdapter> =>
33 adapter.executorAddInputSchema !== null;
34
35const isLocalConfigurableSourceAdapter = <TAdapter extends SourceAdapter>(
36 adapter: TAdapter,
37): adapter is LocalConfigurableSourceAdapter<TAdapter> =>
38 adapter.localConfigBindingSchema !== null;
39
40const asSchemaTuple = (
41 schemas: ReadonlyArray<Schema.Schema<any, any, never>>,
42): [
43 Schema.Schema<any, any, never>,
44 Schema.Schema<any, any, never>,
45 ...Array<Schema.Schema<any, any, never>>,
46] => schemas as [
47 Schema.Schema<any, any, never>,
48 Schema.Schema<any, any, never>,
49 ...Array<Schema.Schema<any, any, never>>,
50];
51
52const DisabledSourceAdapterSchema = Schema.Struct({
53 __sourcePluginRegistrationRequired: Schema.Literal(true),
54}) as unknown as SourceAdapterInputSchema;
55
56const createSchemaFromAdapters = <TSchema extends SourceAdapterInputSchema>(
57 schemas: ReadonlyArray<TSchema>,
58 _label: string,
59): TSchema =>
60 (schemas.length === 0
61 ? DisabledSourceAdapterSchema
62 : schemas.length === 1
63 ? schemas[0]!
64 : Schema.Union(...asSchemaTuple(schemas))) as TSchema;
65
66export const createSourceAdapterComposition = <const TAdapters extends readonly SourceAdapter[]>(
67 adapters: TAdapters,
68) => {
69 const connectableSourceAdapters = adapters.filter(isConnectableSourceAdapter);
70 const executorAddableSourceAdapters = adapters.filter(isExecutorAddableSourceAdapter);
71 const localConfigurableSourceAdapters = adapters.filter(isLocalConfigurableSourceAdapter);
72 const registry = createSourceAdapterRegistry(adapters);
73
74 return {
75 connectableSourceAdapters,
76 connectPayloadSchema: createSchemaFromAdapters(
77 connectableSourceAdapters.map((adapter) => adapter.connectPayloadSchema),
78 "connect payload schema",
79 ) as Schema.Schema<SchemaTypeOf<ConnectPayloadSchema<TAdapters>>, any, never>,
80 executorAddableSourceAdapters,
81 executorAddInputSchema: createSchemaFromAdapters(
82 executorAddableSourceAdapters.map((adapter) => adapter.executorAddInputSchema),
83 "executor add input schema",
84 ) as Schema.Schema<SchemaTypeOf<ExecutorAddInputSchema<TAdapters>>, any, never>,
85 localConfigurableSourceAdapters,
86 ...registry,
87 };
88};
89
90export type SourceAdapterComposition = ReturnType<typeof createSourceAdapterComposition>;
packages/sources/core/src/index.ts+0−2
@@ -1,10 +1,8 @@
1export * from "./catalog";1export * from "./catalog";
2export * from "./catalog-sync-result";2export * from "./catalog-sync-result";
3export * from "./composition";
4export * from "./discovery";3export * from "./discovery";
5export * from "./discovery-models";4export * from "./discovery-models";
6export * from "./effect-errors";5export * from "./effect-errors";
7export * from "./registry";
8export * from "./shared";6export * from "./shared";
9export * from "./source-models";7export * from "./source-models";
10export * from "./types";8export * from "./types";
packages/sources/core/src/registry.ts+0−44
@@ -1,44 +0,0 @@
1import type { Source, SourceCatalogKind } from "./source-models";
2import type { SourceAdapter } from "./types";
3
4export const createSourceAdapterRegistry = <const TAdapters extends readonly SourceAdapter[]>(
5 adapters: TAdapters,
6) => {
7 const adaptersByKey = new Map<string, SourceAdapter>(
8 adapters.map((adapter) => [adapter.key, adapter]),
9 );
10
11 const getSourceAdapter = (key: string): SourceAdapter => {
12 const adapter = adaptersByKey.get(key);
13 if (!adapter) {
14 throw new Error(`Unsupported source adapter: ${key}`);
15 }
16
17 return adapter;
18 };
19
20 const getSourceAdapterForSource = (source: Pick<Source, "kind">): SourceAdapter =>
21 getSourceAdapter(source.kind);
22
23 const findSourceAdapterByProviderKey = (providerKey: string): SourceAdapter | null =>
24 adapters.find((adapter) => adapter.providerKey === providerKey) ?? null;
25
26 return {
27 adapters,
28 getSourceAdapter,
29 getSourceAdapterForSource,
30 findSourceAdapterByProviderKey,
31 sourceBindingStateFromSource: (source: Source) =>
32 getSourceAdapterForSource(source).bindingStateFromSource(source),
33 sourceAdapterCatalogKind: (key: string): SourceCatalogKind =>
34 getSourceAdapter(key).catalogKind,
35 sourceAdapterRequiresInteractiveConnect: (key: string): boolean =>
36 getSourceAdapter(key).connectStrategy === "interactive",
37 sourceAdapterUsesCredentialManagedAuth: (key: string): boolean =>
38 getSourceAdapter(key).credentialStrategy === "credential_managed",
39 isInternalSourceAdapter: (key: string): boolean =>
40 getSourceAdapter(key).catalogKind === "internal",
41 };
42};
43
44export type SourceAdapterRegistry = ReturnType<typeof createSourceAdapterRegistry>;
packages/sources/core/src/shared.ts+2−235
@@ -1,84 +1,5 @@
1import * as Data from "effect/Data";1import * as Data from "effect/Data";
2import * as Effect from "effect/Effect";2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4
5import { sourceCoreEffectError } from "./effect-errors";
6import {
7 SecretRefSchema,
8 SourceBindingVersionSchema,
9 SourceImportAuthPolicySchema,
10 SourceOauthClientInputSchema,
11 StringArraySchema,
12 SourceTransportSchema,
13 StringMapSchema,
14 type CredentialSlot,
15 type SecretRef,
16 type SourceBinding,
17 type StringArray,
18 type SourceTransport,
19 type StringMap,
20} from "./source-models";
21
22const TrimmedNonEmptyStringSchema = Schema.Trim.pipe(Schema.nonEmptyString());
23
24export const OptionalNullableStringSchema = Schema.optional(
25 Schema.NullOr(Schema.String),
26);
27
28export const ConnectBearerAuthSchema = Schema.Struct({
29 kind: Schema.Literal("bearer"),
30 headerName: OptionalNullableStringSchema,
31 prefix: OptionalNullableStringSchema,
32 token: OptionalNullableStringSchema,
33 tokenRef: Schema.optional(
34 Schema.NullOr(SecretRefSchema as Schema.Schema<SecretRef, SecretRef, never>),
35 ),
36});
37
38export const ConnectOAuth2AuthSchema = Schema.Struct({
39 kind: Schema.Literal("oauth2"),
40 headerName: OptionalNullableStringSchema,
41 prefix: OptionalNullableStringSchema,
42 accessToken: OptionalNullableStringSchema,
43 accessTokenRef: Schema.optional(
44 Schema.NullOr(SecretRefSchema as Schema.Schema<SecretRef, SecretRef, never>),
45 ),
46 refreshToken: OptionalNullableStringSchema,
47 refreshTokenRef: Schema.optional(
48 Schema.NullOr(SecretRefSchema as Schema.Schema<SecretRef, SecretRef, never>),
49 ),
50});
51
52export const ConnectHttpAuthSchema = Schema.Union(
53 Schema.Struct({ kind: Schema.Literal("none") }),
54 ConnectBearerAuthSchema,
55 ConnectOAuth2AuthSchema,
56);
57
58export const ConnectHttpImportAuthSchema = Schema.Struct({
59 importAuthPolicy: Schema.optional(SourceImportAuthPolicySchema),
60 importAuth: Schema.optional(ConnectHttpAuthSchema),
61});
62
63export const ConnectOauthClientSchema = Schema.optional(
64 Schema.NullOr(SourceOauthClientInputSchema),
65);
66
67export const SourceConnectCommonFieldsSchema = Schema.Struct({
68 endpoint: TrimmedNonEmptyStringSchema,
69 name: OptionalNullableStringSchema,
70 namespace: OptionalNullableStringSchema,
71});
72
73export const McpConnectFieldsSchema = Schema.Struct({
74 transport: Schema.optional(Schema.NullOr(SourceTransportSchema)),
75 queryParams: Schema.optional(Schema.NullOr(StringMapSchema)),
76 headers: Schema.optional(Schema.NullOr(StringMapSchema)),
77 command: Schema.optional(Schema.NullOr(Schema.String)),
78 args: Schema.optional(Schema.NullOr(StringArraySchema)),
79 env: Schema.optional(Schema.NullOr(StringMapSchema)),
80 cwd: Schema.optional(Schema.NullOr(Schema.String)),
81});
82 3
83export const parseJsonValue = <T>(input: {4export const parseJsonValue = <T>(input: {
84 label: string;5 label: string;
@@ -97,11 +18,11 @@ export const parseJsonValue = <T>(input: { · 12 unmodified lines
97export class SourceCredentialRequiredError extends Data.TaggedError(18export class SourceCredentialRequiredError extends Data.TaggedError(
98 "SourceCredentialRequiredError",19 "SourceCredentialRequiredError",
99)<{20)<{
100 readonly slot: CredentialSlot;21 readonly slot: "runtime" | "import";
101 readonly message: string;22 readonly message: string;
102}> {23}> {
103 constructor(24 constructor(
104 slot: CredentialSlot,25 slot: "runtime" | "import",
105 message: string,26 message: string,
106 ) {27 ) {
107 super({ slot, message });28 super({ slot, message });
@@ -112,157 +33,3 @@ export const isSourceCredentialRequiredError = ( · 4 unmodified lines
112 error: unknown,33 error: unknown,
113): error is SourceCredentialRequiredError =>34): error is SourceCredentialRequiredError =>
114 error instanceof SourceCredentialRequiredError;35 error instanceof SourceCredentialRequiredError;
115
116export const emptySourceBindingState = {
117 transport: null,
118 queryParams: null,
119 headers: null,
120 command: null,
121 args: null,
122 env: null,
123 cwd: null,
124 specUrl: null,
125 defaultHeaders: null,
126} satisfies {
127 transport: SourceTransport | null;
128 queryParams: StringMap | null;
129 headers: StringMap | null;
130 command: string | null;
131 args: StringArray | null;
132 env: StringMap | null;
133 cwd: string | null;
134 specUrl: string | null;
135 defaultHeaders: StringMap | null;
136};
137
138const BindingConfigEnvelopeSchema = <A, I>(
139 adapterKey: string,
140 payloadSchema: Schema.Schema<A, I, never>,
141) =>
142 Schema.Struct({
143 adapterKey: Schema.Literal(adapterKey),
144 version: SourceBindingVersionSchema,
145 payload: payloadSchema,
146 });
147
148export const encodeBindingConfig = <A, I>(input: {
149 adapterKey: string;
150 version: number;
151 payloadSchema: Schema.Schema<A, I, never>;
152 payload: A;
153}): string =>
154 Schema.encodeSync(
155 Schema.parseJson(BindingConfigEnvelopeSchema(input.adapterKey, input.payloadSchema)),
156 )({
157 adapterKey: input.adapterKey,
158 version: input.version,
159 payload: input.payload,
160 });
161
162export const decodeBindingConfig = <A>(input: {
163 sourceId: string;
164 label: string;
165 adapterKey: string;
166 version: number;
167 payloadSchema: Schema.Schema<A, any, never>;
168 value: string | null;
169}): Effect.Effect<SourceBinding & { payload: A }, Error, never> => {
170 if (input.value === null) {
171 return Effect.fail(
172 sourceCoreEffectError("core/shared", `Missing ${input.label} binding config for ${input.sourceId}`),
173 );
174 }
175
176 return Effect.try({
177 try: () =>
178 Schema.decodeUnknownSync(
179 Schema.parseJson(BindingConfigEnvelopeSchema(input.adapterKey, input.payloadSchema)),
180 )(input.value),
181 catch: (cause) => {
182 const message = cause instanceof Error ? cause.message : String(cause);
183 return new Error(
184 `Invalid ${input.label} binding config for ${input.sourceId}: ${message}`,
185 );
186 },
187 }).pipe(
188 Effect.flatMap((decoded) =>
189 decoded.version === input.version
190 ? Effect.succeed({
191 version: decoded.version,
192 payload: decoded.payload,
193 } as SourceBinding & { payload: A })
194 : Effect.fail(
195 sourceCoreEffectError("core/shared",
196 `Unsupported ${input.label} binding config version ${decoded.version} for ${input.sourceId}; expected ${input.version}`,
197 ),
198 ),
199 ),
200 );
201};
202
203export const decodeSourceBindingPayload = <A>(input: {
204 sourceId: string;
205 label: string;
206 version: number;
207 expectedVersion: number;
208 schema: Schema.Schema<A, any, never>;
209 value: unknown;
210 allowedKeys?: readonly string[] | undefined;
211}): Effect.Effect<A, Error, never> =>
212 input.version !== input.expectedVersion
213 ? Effect.fail(
214 sourceCoreEffectError("core/shared",
215 `Unsupported ${input.label} binding version ${input.version} for ${input.sourceId}; expected ${input.expectedVersion}`,
216 ),
217 )
218 : Effect.try({
219 try: () => {
220 if (
221 input.allowedKeys
222 && input.value !== null
223 && typeof input.value === "object"
224 && !Array.isArray(input.value)
225 ) {
226 const extraKeys = Object.keys(input.value as Record<string, unknown>).filter(
227 (key) => !input.allowedKeys!.includes(key),
228 );
229 if (extraKeys.length > 0) {
230 throw new Error(
231 `Unsupported fields: ${extraKeys.join(", ")}`,
232 );
233 }
234 }
235
236 return Schema.decodeUnknownSync(input.schema)(input.value);
237 },
238 catch: (cause) => {
239 const message = cause instanceof Error ? cause.message : String(cause);
240 return new Error(
241 `Invalid ${input.label} binding payload for ${input.sourceId}: ${message}`,
242 );
243 },
244 });
245
246export const decodeExecutableBindingPayload = <A>(input: {
247 executableId: string;
248 label: string;
249 version: number;
250 expectedVersion: number;
251 schema: Schema.Schema<A, any, never>;
252 value: unknown;
253}): A => {
254 if (input.version !== input.expectedVersion) {
255 throw new Error(
256 `Unsupported ${input.label} executable binding version ${input.version} for ${input.executableId}; expected ${input.expectedVersion}`,
257 );
258 }
259
260 try {
261 return Schema.decodeUnknownSync(input.schema)(input.value);
262 } catch (cause) {
263 const message = cause instanceof Error ? cause.message : String(cause);
264 throw new Error(
265 `Invalid ${input.label} executable binding for ${input.executableId}: ${message}`,
266 );
267 }
268};
packages/sources/core/src/source-models.ts+0−91
@@ -29,12 +29,6 @@ export const SourceTransportSchema = Schema.Literal( · 28 unmodified lines
29 "stdio",29 "stdio",
30);30);
31 31
32export const SourceImportAuthPolicySchema = Schema.Literal(
33 "none",
34 "reuse_runtime",
35 "separate",
36);
37
38export const StringMapSchema = Schema.Record({32export const StringMapSchema = Schema.Record({
39 key: Schema.String,33 key: Schema.String,
40 value: Schema.String,34 value: Schema.String,
@@ -73,64 +67,6 @@ export const JsonObjectSchema = Schema.Record({ · 32 unmodified lines
73 value: JsonValueSchema,67 value: JsonValueSchema,
74}).annotations({ identifier: "JsonObject" });68}).annotations({ identifier: "JsonObject" });
75 69
76export const SourceAuthSchema = Schema.Union(
77 Schema.Struct({
78 kind: Schema.Literal("none"),
79 }),
80 Schema.Struct({
81 kind: Schema.Literal("bearer"),
82 headerName: Schema.String,
83 prefix: Schema.String,
84 token: SecretRefSchema,
85 }),
86 Schema.Struct({
87 kind: Schema.Literal("oauth2"),
88 headerName: Schema.String,
89 prefix: Schema.String,
90 accessToken: SecretRefSchema,
91 refreshToken: Schema.NullOr(SecretRefSchema),
92 }),
93 Schema.Struct({
94 kind: Schema.Literal("oauth2_authorized_user"),
95 headerName: Schema.String,
96 prefix: Schema.String,
97 tokenEndpoint: Schema.String,
98 clientId: Schema.String,
99 clientAuthentication: Schema.Literal("none", "client_secret_post"),
100 clientSecret: Schema.NullOr(SecretRefSchema),
101 refreshToken: SecretRefSchema,
102 grantSet: Schema.NullOr(Schema.Array(Schema.String)),
103 }),
104 Schema.Struct({
105 kind: Schema.Literal("provider_grant_ref"),
106 grantId: Schema.String,
107 providerKey: Schema.String,
108 requiredScopes: Schema.Array(Schema.String),
109 headerName: Schema.String,
110 prefix: Schema.String,
111 }),
112 Schema.Struct({
113 kind: Schema.Literal("mcp_oauth"),
114 redirectUri: Schema.String,
115 accessToken: SecretRefSchema,
116 refreshToken: Schema.NullOr(SecretRefSchema),
117 tokenType: Schema.String,
118 expiresIn: Schema.NullOr(Schema.Number),
119 scope: Schema.NullOr(Schema.String),
120 resourceMetadataUrl: Schema.NullOr(Schema.String),
121 authorizationServerUrl: Schema.NullOr(Schema.String),
122 resourceMetadataJson: Schema.NullOr(Schema.String),
123 authorizationServerMetadataJson: Schema.NullOr(Schema.String),
124 clientInformationJson: Schema.NullOr(Schema.String),
125 }),
126);
127
128export const SourceBindingVersionSchema = Schema.Number;
129
130export const SourceBindingSchema = Schema.Struct({
131 version: SourceBindingVersionSchema,
132 payload: JsonObjectSchema,
133});
134 70
135export const SourceSchema = Schema.Struct({71export const SourceSchema = Schema.Struct({
136 id: Schema.String,72 id: Schema.String,
@@ -141,11 +77,6 @@ export const SourceSchema = Schema.Struct({ · 4 unmodified lines
141 status: SourceStatusSchema,77 status: SourceStatusSchema,
142 enabled: Schema.Boolean,78 enabled: Schema.Boolean,
143 namespace: Schema.NullOr(Schema.String),79 namespace: Schema.NullOr(Schema.String),
144 bindingVersion: SourceBindingVersionSchema,
145 binding: JsonObjectSchema,
146 importAuthPolicy: SourceImportAuthPolicySchema,
147 importAuth: SourceAuthSchema,
148 auth: SourceAuthSchema,
149 sourceHash: Schema.NullOr(Schema.String),80 sourceHash: Schema.NullOr(Schema.String),
150 lastError: Schema.NullOr(Schema.String),81 lastError: Schema.NullOr(Schema.String),
151 createdAt: Schema.Number,82 createdAt: Schema.Number,
@@ -154,22 +85,6 @@ export const SourceSchema = Schema.Struct({ · 2 unmodified lines
154 85
155export const StoredSourceRecordSchema = Schema.Struct({86export const StoredSourceRecordSchema = Schema.Struct({
156 id: Schema.String,87 id: Schema.String,
157 bindingConfigJson: Schema.NullOr(Schema.String),
158});
159
160export const ScopedSourceOauthClientRedirectModeSchema = Schema.Literal(
161 "app_callback",
162 "loopback",
163);
164
165export const ScopeOauthClientIdSchema = Schema.String;
166
167export const SourceOauthClientInputSchema = Schema.Struct({
168 clientId: Schema.Trim.pipe(Schema.nonEmptyString()),
169 clientSecret: Schema.optional(
170 Schema.NullOr(Schema.Trim.pipe(Schema.nonEmptyString())),
171 ),
172 redirectMode: Schema.optional(ScopedSourceOauthClientRedirectModeSchema),
173});88});
174 89
175export type SecretRef = typeof SecretRefSchema.Type;90export type SecretRef = typeof SecretRefSchema.Type;
@@ -178,13 +93,7 @@ export type SourceCatalogKind = typeof SourceCatalogKindSchema.Type; · 2 unmodified lines
178export type SourceKind = typeof SourceKindSchema.Type;93export type SourceKind = typeof SourceKindSchema.Type;
179export type SourceStatus = typeof SourceStatusSchema.Type;94export type SourceStatus = typeof SourceStatusSchema.Type;
180export type SourceTransport = typeof SourceTransportSchema.Type;95export type SourceTransport = typeof SourceTransportSchema.Type;
181export type SourceImportAuthPolicy = typeof SourceImportAuthPolicySchema.Type;
182export type StringMap = typeof StringMapSchema.Type;96export type StringMap = typeof StringMapSchema.Type;
183export type StringArray = typeof StringArraySchema.Type;97export type StringArray = typeof StringArraySchema.Type;
184export type SourceAuth = typeof SourceAuthSchema.Type;
185export type SourceBinding = typeof SourceBindingSchema.Type;
186export type Source = typeof SourceSchema.Type;98export type Source = typeof SourceSchema.Type;
187export type StoredSourceRecord = typeof StoredSourceRecordSchema.Type;99export type StoredSourceRecord = typeof StoredSourceRecordSchema.Type;
188export type ScopedSourceOauthClientRedirectMode =
189 typeof ScopedSourceOauthClientRedirectModeSchema.Type;
190export type SourceOauthClientInput = typeof SourceOauthClientInputSchema.Type;
packages/sources/core/src/types.ts+3−151
@@ -1,179 +1,31 @@
1import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js";
2import type {1import type {
3 OnElicitation,2 OnElicitation,
4 ToolDescriptor as CatalogToolDescriptor,3 ToolDescriptor as CatalogToolDescriptor,
5} from "@executor/codemode-core";4} from "@executor/codemode-core";
6import type { Capability, CatalogV1, Executable } from "@executor/ir/model";5import type { Capability, CatalogV1, Executable } from "@executor/ir/model";
7import * as Effect from "effect/Effect";
8import type { Schema } from "effect";
9
10import type { OAuth2ClientAuthenticationMethod } from "@executor/auth-oauth2";
11import type { SourceCatalogSyncResult } from "./catalog-sync-result";6import type { SourceCatalogSyncResult } from "./catalog-sync-result";
12import type { SourceDiscoveryResult } from "./discovery-models";
13import type {7import type {
14 CredentialSlot,
15 SecretRef,
16 Source,8 Source,
17 SourceBinding,
18 SourceCatalogKind,
19 SourceImportAuthPolicy,
20 SourceOauthClientInput,
21 StringArray,
22 SourceTransport,
23 StoredSourceRecord,
24 StringMap,
25} from "./source-models";9} from "./source-models";
26 10
27export type RequestPlacement =11export type SourceSyncInput = {
28 | {
29 location: "header";
30 name: string;
31 value: string;
32 }
33 | {
34 location: "query";
35 name: string;
36 value: string;
37 }
38 | {
39 location: "cookie";
40 name: string;
41 value: string;
42 }
43 | {
44 location: "body";
45 path: string;
46 value: string;
47 };
48
49export type ResolvedSourceAuthMaterial = {
50 placements: ReadonlyArray<RequestPlacement>;
51 headers: Readonly<Record<string, string>>;
52 queryParams: Readonly<Record<string, string>>;
53 cookies: Readonly<Record<string, string>>;
54 bodyValues: Readonly<Record<string, string>>;
55 expiresAt: number | null;
56 refreshAfter: number | null;
57 authProvider?: OAuthClientProvider | undefined;
58};
59
60export type SecretMaterialResolveContext = {
61 params?: Readonly<Record<string, string | undefined>>;
62};
63
64export type ResolveSecretMaterial = (input: {
65 ref: SecretRef;
66 context?: SecretMaterialResolveContext;
67}) => Effect.Effect<string, Error, never>;
68
69export type SourceAdapterConnectStrategy = "direct" | "interactive" | "none";
70
71export type SourceAdapterCredentialStrategy =
72 | "credential_managed"
73 | "adapter_defined"
74 | "none";
75
76export type SourceAdapterInputSchema = Schema.Schema<any, any, never>;
77
78export type SourceBindingState = {
79 transport: SourceTransport | null;
80 queryParams: StringMap | null;
81 headers: StringMap | null;
82 command: string | null;
83 args: StringArray | null;
84 env: StringMap | null;
85 cwd: string | null;
86 specUrl: string | null;
87 defaultHeaders: StringMap | null;
88};
89
90export type StoredSourceBindingConfig = Pick<SourceBinding, "version" | "payload">;
91
92export type SourceAdapterSyncInput = {
93 source: Source;12 source: Source;
94 resolveSecretMaterial: ResolveSecretMaterial;
95 resolveAuthMaterialForSlot: (slot: CredentialSlot) => Effect.Effect<
96 ResolvedSourceAuthMaterial,
97 Error,
98 never
99 >;
100};
101
102export type SourceAdapterOauth2SetupConfig = {
103 providerKey: string;
104 authorizationEndpoint: string;
105 tokenEndpoint: string;
106 scopes: readonly string[];
107 headerName: string;
108 prefix: string;
109 clientAuthentication: OAuth2ClientAuthenticationMethod;
110 authorizationParams?: Readonly<Record<string, string>>;
111};13};
112 14
113export type SourceAdapterInvokeResult = {15export type SourceInvokeResult = {
114 data: unknown;16 data: unknown;
115 error: unknown;17 error: unknown;
116 headers: Record<string, string>;18 headers: Record<string, string>;
117 status: number | null;19 status: number | null;
118};20};
119 21
120export type SourceAdapterInvokeInput = {22export type SourceInvokeInput = {
121 source: Source;23 source: Source;
122 capability: Capability;24 capability: Capability;
123 executable: Executable;25 executable: Executable;
124 descriptor: CatalogToolDescriptor;26 descriptor: CatalogToolDescriptor;
125 catalog: CatalogV1;27 catalog: CatalogV1;
126 args: unknown;28 args: unknown;
127 auth: ResolvedSourceAuthMaterial;
128 onElicitation?: OnElicitation;29 onElicitation?: OnElicitation;
129 context?: Record<string, unknown>;30 context?: Record<string, unknown>;
130};31};
131
132export type SourceAdapterDiscoveryInput = {
133 normalizedUrl: string;
134 headers: Readonly<Record<string, string>>;
135};
136
137export type SourceAdapter = {
138 key: string;
139 displayName: string;
140 catalogKind: SourceCatalogKind;
141 connectStrategy: SourceAdapterConnectStrategy;
142 credentialStrategy: SourceAdapterCredentialStrategy;
143 bindingConfigVersion: number;
144 providerKey: string;
145 defaultImportAuthPolicy: SourceImportAuthPolicy;
146 connectPayloadSchema: SourceAdapterInputSchema | null;
147 executorAddInputSchema: SourceAdapterInputSchema | null;
148 executorAddHelpText: readonly string[] | null;
149 executorAddInputSignatureWidth: number | null;
150 localConfigBindingSchema: SourceAdapterInputSchema | null;
151 localConfigBindingFromSource: (source: Source) => unknown;
152 serializeBindingConfig: (source: Source) => string;
153 deserializeBindingConfig: (
154 input: Pick<StoredSourceRecord, "id" | "bindingConfigJson">,
155 ) => Effect.Effect<StoredSourceBindingConfig, Error, never>;
156 bindingStateFromSource: (source: Source) => Effect.Effect<SourceBindingState, Error, never>;
157 sourceConfigFromSource: (source: Source) => Record<string, unknown>;
158 validateSource: (source: Source) => Effect.Effect<Source, Error, never>;
159 shouldAutoProbe: (source: Source) => boolean;
160 discoveryPriority?: (input: {
161 normalizedUrl: string;
162 }) => number;
163 detectSource?: (
164 input: SourceAdapterDiscoveryInput,
165 ) => Effect.Effect<SourceDiscoveryResult | null, Error, never>;
166 syncCatalog: (
167 input: SourceAdapterSyncInput,
168 ) => Effect.Effect<SourceCatalogSyncResult, Error, never>;
169 invoke: (
170 input: SourceAdapterInvokeInput,
171 ) => Effect.Effect<SourceAdapterInvokeResult, Error, never>;
172 getOauth2SetupConfig?: (input: {
173 source: Source;
174 slot: CredentialSlot;
175 }) => Effect.Effect<SourceAdapterOauth2SetupConfig | null, Error, never>;
176 normalizeOauthClientInput?: (
177 input: SourceOauthClientInput,
178 ) => Effect.Effect<SourceOauthClientInput, Error, never>;
179};
plugins/openapi/http/index.ts+113−0
@@ -0,0 +1,113 @@
1import {
2 HttpApi,
3 HttpApiBuilder,
4 HttpApiEndpoint,
5 HttpApiGroup,
6 HttpApiSchema,
7} from "@effect/platform";
8import {
9 ControlPlaneBadRequestError,
10 ControlPlaneForbiddenError,
11 ControlPlaneStorageError,
12 type ExecutorHttpApiExtension,
13 type ExecutorHttpPlugin,
14} from "@executor/platform-api";
15import { resolveRequestedLocalWorkspace } from "@executor/platform-api/local-context";
16import {
17 ScopeIdSchema,
18 SourceSchema,
19 type Source,
20} from "@executor/platform-sdk/schema";
21import * as Effect from "effect/Effect";
22
23import {
24 OpenApiConnectInputSchema,
25 OpenApiPreviewRequestSchema,
26 OpenApiPreviewResponseSchema,
27 type OpenApiConnectInput,
28 type OpenApiPreviewRequest,
29 type OpenApiPreviewResponse,
30} from "@executor/plugin-openapi-shared";
31
32type OpenApiExecutorExtension = {
33 openapi: {
34 previewDocument: (
35 input: OpenApiPreviewRequest,
36 ) => Effect.Effect<OpenApiPreviewResponse, Error, never>;
37 createSource: (
38 input: OpenApiConnectInput,
39 ) => Effect.Effect<Source, Error, never>;
40 };
41};
42
43const workspaceIdParam = HttpApiSchema.param("workspaceId", ScopeIdSchema);
44
45export const OpenApiHttpGroup = HttpApiGroup.make("openapi")
46 .add(
47 HttpApiEndpoint.post("previewDocument")`/workspaces/${workspaceIdParam}/plugins/openapi/preview`
48 .setPayload(OpenApiPreviewRequestSchema)
49 .addSuccess(OpenApiPreviewResponseSchema)
50 .addError(ControlPlaneBadRequestError)
51 .addError(ControlPlaneForbiddenError)
52 .addError(ControlPlaneStorageError),
53 )
54 .add(
55 HttpApiEndpoint.post("createSource")`/workspaces/${workspaceIdParam}/plugins/openapi/sources`
56 .setPayload(OpenApiConnectInputSchema)
57 .addSuccess(SourceSchema)
58 .addError(ControlPlaneBadRequestError)
59 .addError(ControlPlaneForbiddenError)
60 .addError(ControlPlaneStorageError),
61 )
62 .prefix("/v1");
63
64export const OpenApiHttpApi = HttpApi.make("executor").add(OpenApiHttpGroup);
65
66export const openApiHttpApiExtension = {
67 key: "openapi",
68 group: OpenApiHttpGroup,
69} satisfies ExecutorHttpApiExtension<typeof OpenApiHttpGroup>;
70
71const toBadRequestError = (operation: string) => (cause: unknown) =>
72 new ControlPlaneBadRequestError({
73 operation,
74 message: cause instanceof Error ? cause.message : String(cause),
75 details: cause instanceof Error ? cause.stack ?? cause.message : String(cause),
76 });
77
78const toStorageError = (operation: string) => (cause: unknown) =>
79 new ControlPlaneStorageError({
80 operation,
81 message: cause instanceof Error ? cause.message : String(cause),
82 details: cause instanceof Error ? cause.stack ?? cause.message : String(cause),
83 });
84
85export const openApiHttpPlugin = (): ExecutorHttpPlugin<
86 typeof OpenApiHttpGroup,
87 OpenApiExecutorExtension
88> => ({
89 key: "openapi",
90 group: OpenApiHttpGroup,
91 build: ({ executor }) =>
92 HttpApiBuilder.group(OpenApiHttpApi, "openapi", (handlers) =>
93 handlers
94 .handle("previewDocument", ({ path, payload }) =>
95 resolveRequestedLocalWorkspace(
96 "openapi.previewDocument",
97 path.workspaceId,
98 ).pipe(
99 Effect.flatMap(() => executor.openapi.previewDocument(payload)),
100 Effect.mapError(toBadRequestError("openapi.previewDocument")),
101 )
102 )
103 .handle("createSource", ({ path, payload }) =>
104 resolveRequestedLocalWorkspace(
105 "openapi.createSource",
106 path.workspaceId,
107 ).pipe(
108 Effect.flatMap(() => executor.openapi.createSource(payload)),
109 Effect.mapError(toStorageError("openapi.createSource")),
110 )
111 )
112 ),
113});
plugins/openapi/http/package.json+26−0
@@ -0,0 +1,26 @@
1{
2 "name": "@executor/plugin-openapi-http",
3 "private": true,
4 "type": "module",
5 "exports": {
6 ".": "./index.ts"
7 },
8 "files": [
9 "index.ts"
10 ],
11 "scripts": {
12 "typecheck": "bunx tsc --noEmit -p tsconfig.json"
13 },
14 "dependencies": {
15 "@effect/platform": "catalog:",
16 "@executor/platform-api": "workspace:*",
17 "@executor/platform-sdk": "workspace:*",
18 "@executor/plugin-openapi-shared": "workspace:*",
19 "effect": "catalog:"
20 },
21 "devDependencies": {
22 "@types/node": "catalog:",
23 "bun-types": "catalog:"
24 },
25 "version": "0.0.0"
26}
plugins/openapi/http/tsconfig.jsonrenamed from packages/sources/builtins/tsconfig.json+2−2
@@ -6,10 +6,10 @@ · 5 unmodified lines
6 "strict": true,6 "strict": true,
7 "skipLibCheck": true,7 "skipLibCheck": true,
8 "noEmit": true,8 "noEmit": true,
9 "lib": ["ES2022"],9 "lib": ["ES2022", "DOM"],
10 "types": ["bun-types", "node"]10 "types": ["bun-types", "node"]
11 },11 },
12 "include": [12 "include": [
13 "src/**/*.ts"13 "index.ts"
14 ]14 ]
15}15}
plugins/openapi/react/index.tsx+460−0
@@ -0,0 +1,460 @@
1import type { Source } from "@executor/react";
2import {
3 Result,
4 defineExecutorPluginHttpApiClient,
5 useAtomValue,
6 useAtomSet,
7 useExecutorMutation,
8 useLocalInstallation,
9} from "@executor/react";
10import {
11 openApiHttpApiExtension,
12} from "@executor/plugin-openapi-http";
13import type {
14 OpenApiConnectInput,
15 OpenApiPreviewRequest,
16 OpenApiPreviewResponse,
17} from "@executor/plugin-openapi-shared";
18import { useNavigate } from "@tanstack/react-router";
19import { startTransition, useState, type ReactNode } from "react";
20
21type FrontendSourceTypeDefinition = {
22 kind: string;
23 displayName: string;
24 renderAddPage: () => ReactNode;
25 renderEditPage?: (input: { source: Source }) => ReactNode;
26 renderDetailPage?: (input: {
27 source: Source;
28 route: {
29 search?: unknown;
30 navigate?: unknown;
31 };
32 }) => ReactNode;
33};
34
35type FrontendPluginRegisterApi = {
36 sources: {
37 registerType: (definition: FrontendSourceTypeDefinition) => void;
38 };
39};
40
41const defaultOpenApiInput = (): OpenApiConnectInput => ({
42 name: "My OpenAPI Source",
43 specUrl: "https://example.com/openapi.json",
44 baseUrl: null,
45 auth: {
46 kind: "none",
47 },
48});
49
50const getOpenApiHttpClient = defineExecutorPluginHttpApiClient<"OpenApiReactHttpClient">()(
51 "OpenApiReactHttpClient",
52 [openApiHttpApiExtension] as const,
53);
54
55
56const Section = (props: {
57 title: string;
58 children: ReactNode;
59}) => (
60 <section className="rounded-xl border border-border/70 bg-card/40 p-4">
61 <div className="mb-3 flex items-center gap-2">
62 <h2 className="text-sm font-semibold">{props.title}</h2>
63 <span className="inline-flex items-center rounded-full border border-transparent bg-secondary px-2 py-0.5 text-[10px] font-medium uppercase tracking-wide text-secondary-foreground">
64 Plugin
65 </span>
66 </div>
67 {props.children}
68 </section>
69);
70
71function OpenApiAddSourcePage(props: {
72 initialValue: OpenApiConnectInput;
73}) {
74 const navigate = useNavigate();
75 const installation = useLocalInstallation();
76 const [name, setName] = useState(props.initialValue.name);
77 const [specUrl, setSpecUrl] = useState(props.initialValue.specUrl);
78 const [baseUrl, setBaseUrl] = useState(props.initialValue.baseUrl ?? "");
79 const [error, setError] = useState<string | null>(null);
80 const [preview, setPreview] = useState<OpenApiPreviewResponse | null>(null);
81 const [nameEdited, setNameEdited] = useState(false);
82 const [baseUrlEdited, setBaseUrlEdited] = useState(false);
83 const openApiHttpClient = getOpenApiHttpClient();
84 const secretsResult = useAtomValue(
85 openApiHttpClient.query("local", "listSecrets", {
86 reactivityKeys: {
87 secrets: [],
88 },
89 timeToLive: "1 minute",
90 }),
91 );
92 const availableSecrets = Result.isSuccess(secretsResult)
93 ? secretsResult.value
94 : [];
95 const [authKind, setAuthKind] = useState<OpenApiConnectInput["auth"]["kind"]>(
96 props.initialValue.auth.kind,
97 );
98 const [tokenSecretRef, setTokenSecretRef] = useState(
99 props.initialValue.auth.kind === "bearer"
100 ? props.initialValue.auth.tokenSecretRef
101 : "",
102 );
103 const previewDocument = useAtomSet(
104 openApiHttpClient.mutation("openapi", "previewDocument"),
105 { mode: "promise" },
106 );
107 const createSource = useAtomSet(
108 openApiHttpClient.mutation("openapi", "createSource"),
109 { mode: "promise" },
110 );
111
112 const requireWorkspaceId = (): string => {
113 if (installation.status === "ready") {
114 return installation.data.scopeId;
115 }
116
117 if (installation.status === "error") {
118 throw installation.error;
119 }
120
121 throw new Error("Workspace is still loading.");
122 };
123
124 const previewMutation = useExecutorMutation<
125 OpenApiPreviewRequest,
126 OpenApiPreviewResponse
127 >(async (payload) =>
128 previewDocument({
129 path: {
130 workspaceId: requireWorkspaceId() as Source["scopeId"],
131 },
132 payload,
133 })
134 );
135
136 const createMutation = useExecutorMutation<
137 OpenApiConnectInput,
138 Source
139 >(async (payload) =>
140 createSource({
141 path: {
142 workspaceId: requireWorkspaceId() as Source["scopeId"],
143 },
144 payload,
145 reactivityKeys: {
146 sources: [requireWorkspaceId()],
147 },
148 })
149 );
150
151 const handlePreview = async () => {
152 setError(null);
153 const trimmedSpecUrl = specUrl.trim();
154 if (!trimmedSpecUrl) {
155 setError("Spec URL is required.");
156 return;
157 }
158
159 if (authKind === "bearer" && tokenSecretRef.trim().length === 0) {
160 setError("Select a secret for bearer auth.");
161 return;
162 }
163
164 try {
165 const result = await previewMutation.mutateAsync({
166 specUrl: trimmedSpecUrl,
167 });
168 setPreview({
169 ...result,
170 warnings: [...result.warnings],
171 });
172
173 if (!nameEdited && result.title) {
174 setName(result.title);
175 }
176
177 if (!baseUrlEdited && result.baseUrl) {
178 setBaseUrl(result.baseUrl);
179 }
180 } catch (cause) {
181 setError(cause instanceof Error ? cause.message : "Failed previewing document.");
182 setPreview(null);
183 }
184 };
185
186 const handleSubmit = async () => {
187 setError(null);
188
189 const trimmedName = name.trim();
190 const trimmedSpecUrl = specUrl.trim();
191 const trimmedBaseUrl = baseUrl.trim();
192
193 if (!trimmedName) {
194 setError("Name is required.");
195 return;
196 }
197
198 if (!trimmedSpecUrl) {
199 setError("Spec URL is required.");
200 return;
201 }
202
203 try {
204 const source = await createMutation.mutateAsync({
205 name: trimmedName,
206 specUrl: trimmedSpecUrl,
207 baseUrl: trimmedBaseUrl || null,
208 auth:
209 authKind === "bearer"
210 ? {
211 kind: "bearer",
212 tokenSecretRef,
213 }
214 : {
215 kind: "none",
216 },
217 });
218
219 startTransition(() => {
220 void navigate({
221 to: "/sources/$sourceId",
222 params: {
223 sourceId: source.id,
224 },
225 search: {
226 tab: "model",
227 },
228 });
229 });
230 } catch (cause) {
231 setError(cause instanceof Error ? cause.message : "Failed creating source.");
232 }
233 };
234
235 return (
236 <div className="space-y-4">
237 <Section title="Connection">
238 <p className="text-sm text-muted-foreground">
239 This plugin owns its typed HTTP client and source payload shape. The app shell only
240 mounts the registered page.
241 </p>
242 <div className="mt-4 grid gap-4">
243 <label className="grid gap-1.5">
244 <span className="text-[12px] font-medium text-foreground">Name</span>
245 <input
246 value={name}
247 onChange={(event) => {
248 setNameEdited(true);
249 setName(event.target.value);
250 }}
251 placeholder="GitHub REST"
252 className="h-10 w-full rounded-lg border border-input bg-background px-3 text-[13px] text-foreground outline-none transition-colors placeholder:text-muted-foreground/35 focus:border-ring focus:ring-1 focus:ring-ring/25"
253 />
254 </label>
255
256 <label className="grid gap-1.5">
257 <span className="text-[12px] font-medium text-foreground">Spec URL</span>
258 <input
259 value={specUrl}
260 onChange={(event) => setSpecUrl(event.target.value)}
261 placeholder="https://example.com/openapi.json"
262 className="h-10 w-full rounded-lg border border-input bg-background px-3 font-mono text-[12px] text-foreground outline-none transition-colors placeholder:text-muted-foreground/35 focus:border-ring focus:ring-1 focus:ring-ring/25"
263 />
264 </label>
265
266 <label className="grid gap-1.5">
267 <span className="text-[12px] font-medium text-foreground">Base URL</span>
268 <input
269 value={baseUrl}
270 onChange={(event) => {
271 setBaseUrlEdited(true);
272 setBaseUrl(event.target.value);
273 }}
274 placeholder="https://api.example.com"
275 className="h-10 w-full rounded-lg border border-input bg-background px-3 font-mono text-[12px] text-foreground outline-none transition-colors placeholder:text-muted-foreground/35 focus:border-ring focus:ring-1 focus:ring-ring/25"
276 />
277 </label>
278
279 <label className="grid gap-1.5">
280 <span className="text-[12px] font-medium text-foreground">Auth</span>
281 <select
282 value={authKind}
283 onChange={(event) =>
284 setAuthKind(event.target.value as OpenApiConnectInput["auth"]["kind"])}
285 className="h-10 w-full rounded-lg border border-input bg-background px-3 text-[13px] text-foreground outline-none transition-colors focus:border-ring focus:ring-1 focus:ring-ring/25"
286 >
287 <option value="none">None</option>
288 <option value="bearer">Bearer Secret</option>
289 </select>
290 </label>
291
292 {authKind === "bearer" && (
293 <label className="grid gap-1.5">
294 <span className="text-[12px] font-medium text-foreground">Secret</span>
295 <select
296 value={tokenSecretRef}
297 onChange={(event) => setTokenSecretRef(event.target.value)}
298 className="h-10 w-full rounded-lg border border-input bg-background px-3 text-[13px] text-foreground outline-none transition-colors focus:border-ring focus:ring-1 focus:ring-ring/25"
299 >
300 <option value="">Select a secret</option>
301 {availableSecrets.map((secret) => (
302 <option key={secret.id} value={secret.id}>
303 {secret.name || secret.id}
304 </option>
305 ))}
306 </select>
307 </label>
308 )}
309 </div>
310 </Section>
311
312 <Section title="Preview">
313 <p className="text-sm text-muted-foreground">
314 Preview introspects the document and can pull out defaults like title and base URL from
315 the OpenAPI spec.
316 </p>
317 <div className="mt-3 flex items-center gap-3">
318 <button
319 type="button"
320 onClick={() => {
321 void handlePreview();
322 }}
323 disabled={previewMutation.status === "pending" || createMutation.status === "pending"}
324 className="inline-flex h-7 items-center justify-center rounded-md border border-input bg-transparent px-2.5 text-xs font-medium text-foreground disabled:pointer-events-none disabled:opacity-50"
325 >
326 {previewMutation.status === "pending" ? "Previewing..." : "Preview OpenAPI Document"}
327 </button>
328 {preview && (
329 <div className="text-xs text-muted-foreground">
330 {preview.operationCount} operations
331 {preview.version ? ` · v${preview.version}` : ""}
332 </div>
333 )}
334 </div>
335 {preview && (
336 <div className="mt-4 rounded-lg border border-border/70 bg-background/60 p-4 text-sm">
337 <div className="grid gap-2">
338 <div>
339 <span className="font-medium text-foreground">Title:</span>{" "}
340 <span className="text-muted-foreground">{preview.title ?? "Unknown"}</span>
341 </div>
342 <div>
343 <span className="font-medium text-foreground">Version:</span>{" "}
344 <span className="text-muted-foreground">{preview.version ?? "Unknown"}</span>
345 </div>
346 <div>
347 <span className="font-medium text-foreground">Base URL:</span>{" "}
348 <span className="text-muted-foreground">{preview.baseUrl ?? "Not declared"}</span>
349 </div>
350 </div>
351 {preview.warnings.length > 0 && (
352 <div className="mt-3 rounded-md border border-amber-300/40 bg-amber-100/20 px-3 py-2 text-xs text-amber-800">
353 {preview.warnings.join(" ")}
354 </div>
355 )}
356 </div>
357 )}
358 </Section>
359
360 <Section title="Submit">
361 <p className="text-sm text-muted-foreground">
362 This plugin can use its own HTTP endpoints and the core API from one merged client. That
363 is how the bearer-secret selector is loaded without a separate app-owned client path.
364 </p>
365 {error && (
366 <div className="mb-3 rounded-lg border border-destructive/30 bg-destructive/8 px-4 py-2.5 text-[13px] text-destructive">
367 {error}
368 </div>
369 )}
370 <div className="flex items-center gap-3">
371 <button
372 type="button"
373 onClick={() => {
374 void handleSubmit();
375 }}
376 disabled={createMutation.status === "pending" || installation.status === "loading"}
377 className="inline-flex h-7 items-center justify-center rounded-md bg-primary px-2.5 text-xs font-medium text-primary-foreground disabled:pointer-events-none disabled:opacity-50"
378 >
379 {createMutation.status === "pending" ? "Creating..." : "Create Source"}
380 </button>
381 <div className="text-xs text-muted-foreground">
382 Creates a real <code>kind: &quot;openapi&quot;</code> source.
383 </div>
384 </div>
385 </Section>
386 </div>
387 );
388}
389
390function OpenApiEditSourcePage(props: {
391 source: Source;
392}) {
393 return (
394 <Section title="OpenAPI Plugin Editor">
395 <p className="text-sm text-muted-foreground">
396 Plugin-specific configuration is stored behind the OpenAPI plugin boundary, not on the
397 shared source row.
398 </p>
399 <pre className="mt-3 overflow-x-auto rounded-md bg-muted/60 p-3 text-xs">
400 {JSON.stringify({
401 id: props.source.id,
402 kind: props.source.kind,
403 endpoint: props.source.endpoint,
404 sourceHash: props.source.sourceHash,
405 }, null, 2)}
406 </pre>
407 </Section>
408 );
409}
410
411function OpenApiSourceDetailPage(props: {
412 source: Source;
413}) {
414 return (
415 <div className="space-y-4">
416 <Section title="Source Detail">
417 <p className="text-sm text-muted-foreground">
418 OpenAPI-specific configuration and auth live in plugin storage. The shared source record
419 only carries generic routing and status metadata.
420 </p>
421 <pre className="mt-3 overflow-x-auto rounded-md bg-muted/60 p-3 text-xs">
422 {JSON.stringify({
423 id: props.source.id,
424 name: props.source.name,
425 kind: props.source.kind,
426 endpoint: props.source.endpoint,
427 status: props.source.status,
428 sourceHash: props.source.sourceHash,
429 }, null, 2)}
430 </pre>
431 </Section>
432
433 <Section title="Plugin Boundary">
434 <p className="text-sm text-muted-foreground">
435 The shell only knows there is a registered source plugin named <code>openapi</code>.
436 Everything specific stays inside this plugin.
437 </p>
438 </Section>
439 </div>
440 );
441}
442
443export const OpenApiReactPlugin = {
444 key: "openapi",
445 register(api: FrontendPluginRegisterApi) {
446 api.sources.registerType({
447 kind: "openapi",
448 displayName: "OpenAPI",
449 renderAddPage: () => (
450 <OpenApiAddSourcePage initialValue={defaultOpenApiInput()} />
451 ),
452 renderEditPage: ({ source }) => (
453 <OpenApiEditSourcePage source={source} />
454 ),
455 renderDetailPage: ({ source }) => (
456 <OpenApiSourceDetailPage source={source} />
457 ),
458 });
459 },
460};
plugins/openapi/react/package.json+27−0
@@ -0,0 +1,27 @@
1{
2 "name": "@executor/plugin-openapi-react",
3 "private": true,
4 "type": "module",
5 "exports": {
6 ".": "./index.tsx"
7 },
8 "files": [
9 "index.tsx"
10 ],
11 "scripts": {
12 "typecheck": "bunx tsc --noEmit -p tsconfig.json"
13 },
14 "dependencies": {
15 "@executor/plugin-openapi-http": "workspace:*",
16 "@executor/plugin-openapi-shared": "workspace:*",
17 "@executor/react": "workspace:*",
18 "@tanstack/react-router": "^1.132.7",
19 "react": "^19.2.0"
20 },
21 "devDependencies": {
22 "@types/node": "catalog:",
23 "@types/react": "^19.2.10",
24 "bun-types": "catalog:"
25 },
26 "version": "0.0.0"
27}
plugins/openapi/react/tsconfig.json+16−0
@@ -0,0 +1,16 @@
1{
2 "compilerOptions": {
3 "target": "ES2022",
4 "module": "ESNext",
5 "moduleResolution": "Bundler",
6 "strict": true,
7 "skipLibCheck": true,
8 "noEmit": true,
9 "lib": ["ES2022", "DOM", "DOM.Iterable"],
10 "jsx": "react-jsx",
11 "types": ["bun-types", "node"]
12 },
13 "include": [
14 "index.tsx"
15 ]
16}
plugins/openapi/sdk/index.ts+146−0
@@ -0,0 +1,146 @@
1import { createHash } from "node:crypto";
2import * as Effect from "effect/Effect";
3
4import {
5 createCatalogImportMetadata,
6 createSourceCatalogSyncResult,
7} from "@executor/source-core";
8import type { Source } from "@executor/platform-sdk/schema";
9import type {
10 ExecutorSdkPlugin,
11 SourcePluginRuntime,
12} from "@executor/platform-sdk/plugins";
13import {
14 previewOpenApiDocument,
15 type OpenApiConnectInput,
16 type OpenApiPreviewRequest,
17 type OpenApiPreviewResponse,
18 type OpenApiStoredSourceData,
19} from "@executor/plugin-openapi-shared";
20
21const stableSourceHash = (value: OpenApiStoredSourceData): string =>
22 createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 24);
23
24export type OpenApiSourceStorage = {
25 get: (input: {
26 scopeId: string;
27 sourceId: string;
28 }) => Effect.Effect<OpenApiStoredSourceData | null, Error, never>;
29 put: (input: {
30 scopeId: string;
31 sourceId: string;
32 value: OpenApiStoredSourceData;
33 }) => Effect.Effect<void, Error, never>;
34 remove?: (input: {
35 scopeId: string;
36 sourceId: string;
37 }) => Effect.Effect<void, Error, never>;
38};
39
40export type OpenApiSecrets = {
41 resolve: (input: {
42 ref: string;
43 }) => Effect.Effect<string, Error, never>;
44};
45
46export type OpenApiSdk = {
47 previewDocument: (
48 input: OpenApiPreviewRequest,
49 ) => Effect.Effect<OpenApiPreviewResponse, Error, never>;
50 createSource: (
51 input: OpenApiConnectInput,
52 ) => Effect.Effect<Source, Error, never>;
53};
54
55export type OpenApiSdkPluginOptions = {
56 storage: OpenApiSourceStorage;
57 secrets?: OpenApiSecrets;
58};
59
60const createStoredSourceData = (
61 input: OpenApiConnectInput,
62): OpenApiStoredSourceData => ({
63 specUrl: input.specUrl.trim(),
64 baseUrl: input.baseUrl?.trim() || null,
65 auth: input.auth,
66 defaultHeaders: null,
67 etag: null,
68 lastSyncAt: null,
69});
70
71const createOpenApiSourceRuntime = (
72 options: OpenApiSdkPluginOptions,
73): SourcePluginRuntime => ({
74 kind: "openapi",
75 displayName: "OpenAPI",
76 catalogKind: "imported",
77 catalogIdentity: ({ source }) => ({
78 kind: "openapi",
79 endpoint: source.endpoint,
80 }),
81 getIrModel: ({ source }) =>
82 Effect.gen(function* () {
83 const stored = yield* options.storage.get({
84 scopeId: source.scopeId,
85 sourceId: source.id,
86 });
87
88 return createSourceCatalogSyncResult({
89 fragment: {
90 version: "ir.v1.fragment",
91 },
92 importMetadata: {
93 ...createCatalogImportMetadata({
94 source,
95 pluginKey: "openapi",
96 }),
97 importerVersion: "ir.v1.openapi",
98 sourceConfigHash:
99 stored === null ? source.sourceHash ?? "openapi" : stableSourceHash(stored),
100 },
101 sourceHash:
102 stored === null ? source.sourceHash ?? null : stableSourceHash(stored),
103 });
104 }),
105 invoke: () =>
106 Effect.fail(new Error("OpenAPI plugin invocation is not implemented.")),
107});
108
109export const openApiSdkPlugin = (
110 options: OpenApiSdkPluginOptions,
111): ExecutorSdkPlugin<"openapi", OpenApiSdk> => ({
112 key: "openapi",
113 sources: [createOpenApiSourceRuntime(options)],
114 extendExecutor: ({ host }) => ({
115 previewDocument: (input) =>
116 Effect.tryPromise({
117 try: () => previewOpenApiDocument(input),
118 catch: (cause) =>
119 cause instanceof Error ? cause : new Error(String(cause)),
120 }),
121 createSource: (input) =>
122 Effect.gen(function* () {
123 const stored = createStoredSourceData(input);
124 const source = yield* host.sources.create({
125 source: {
126 name: input.name.trim(),
127 kind: "openapi",
128 endpoint: stored.baseUrl ?? stored.specUrl,
129 status: "connected",
130 enabled: true,
131 namespace: null,
132 sourceHash: stableSourceHash(stored),
133 lastError: null,
134 },
135 });
136
137 yield* options.storage.put({
138 scopeId: source.scopeId,
139 sourceId: source.id,
140 value: stored,
141 });
142
143 return source;
144 }),
145 }),
146});
plugins/openapi/sdk/package.json+25−0
@@ -0,0 +1,25 @@
1{
2 "name": "@executor/plugin-openapi-sdk",
3 "private": true,
4 "type": "module",
5 "exports": {
6 ".": "./index.ts"
7 },
8 "files": [
9 "index.ts"
10 ],
11 "scripts": {
12 "typecheck": "bunx tsc --noEmit -p tsconfig.json"
13 },
14 "dependencies": {
15 "@executor/platform-sdk": "workspace:*",
16 "@executor/plugin-openapi-shared": "workspace:*",
17 "@executor/source-core": "workspace:*",
18 "effect": "catalog:"
19 },
20 "devDependencies": {
21 "@types/node": "catalog:",
22 "bun-types": "catalog:"
23 },
24 "version": "0.0.0"
25}
plugins/openapi/sdk/tsconfig.json+15−0
@@ -0,0 +1,15 @@
1{
2 "compilerOptions": {
3 "target": "ES2022",
4 "module": "ESNext",
5 "moduleResolution": "Bundler",
6 "strict": true,
7 "skipLibCheck": true,
8 "noEmit": true,
9 "lib": ["ES2022", "DOM"],
10 "types": ["bun-types", "node"]
11 },
12 "include": [
13 "index.ts"
14 ]
15}
plugins/openapi/shared/index.ts+177−0
@@ -0,0 +1,177 @@
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import YAML from "yaml";
4
5import {
6 StringMapSchema,
7} from "@executor/source-core";
8
9export const OpenApiConnectionAuthSchema = Schema.Union(
10 Schema.Struct({
11 kind: Schema.Literal("none"),
12 }),
13 Schema.Struct({
14 kind: Schema.Literal("bearer"),
15 tokenSecretRef: Schema.String,
16 }),
17);
18
19export const OpenApiConnectInputSchema = Schema.Struct({
20 name: Schema.String,
21 specUrl: Schema.String,
22 baseUrl: Schema.NullOr(Schema.String),
23 auth: OpenApiConnectionAuthSchema,
24});
25
26export const OpenApiSourceConfigSchema = Schema.Struct({
27 specUrl: Schema.String,
28 baseUrl: Schema.NullOr(Schema.String),
29 authStrategy: Schema.Literal("none", "bearer"),
30 documentHash: Schema.String,
31});
32
33export const OpenApiStoredSourceDataSchema = Schema.Struct({
34 specUrl: Schema.String,
35 baseUrl: Schema.NullOr(Schema.String),
36 auth: OpenApiConnectionAuthSchema,
37 defaultHeaders: Schema.NullOr(StringMapSchema),
38 etag: Schema.NullOr(Schema.String),
39 lastSyncAt: Schema.NullOr(Schema.Number),
40});
41
42export const OpenApiPreviewRequestSchema = Schema.Struct({
43 specUrl: Schema.String,
44});
45
46export const OpenApiPreviewResponseSchema = Schema.Struct({
47 title: Schema.NullOr(Schema.String),
48 version: Schema.NullOr(Schema.String),
49 baseUrl: Schema.NullOr(Schema.String),
50 operationCount: Schema.Number,
51 warnings: Schema.Array(Schema.String),
52});
53
54export type OpenApiConnectionAuth = typeof OpenApiConnectionAuthSchema.Type;
55export type OpenApiConnectInput = typeof OpenApiConnectInputSchema.Type;
56export type OpenApiSourceConfig = typeof OpenApiSourceConfigSchema.Type;
57export type OpenApiStoredSourceData = typeof OpenApiStoredSourceDataSchema.Type;
58export type OpenApiPreviewRequest = typeof OpenApiPreviewRequestSchema.Type;
59export type OpenApiPreviewResponse = typeof OpenApiPreviewResponseSchema.Type;
60
61type JsonRecord = Record<string, unknown>;
62
63const asRecord = (value: unknown): JsonRecord | null =>
64 value !== null && typeof value === "object" && !Array.isArray(value)
65 ? value as JsonRecord
66 : null;
67
68const asString = (value: unknown): string | null =>
69 typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
70
71const parseDocument = (text: string): unknown => {
72 const trimmed = text.trim();
73 if (!trimmed) {
74 throw new Error("The OpenAPI document is empty.");
75 }
76
77 if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
78 return JSON.parse(trimmed);
79 }
80
81 return YAML.parse(trimmed);
82};
83
84const deriveBaseUrl = (document: JsonRecord, specUrl: string): string | null => {
85 const servers = Array.isArray(document.servers) ? document.servers : [];
86 const firstServer = asRecord(servers[0]);
87 const rawUrl = asString(firstServer?.url);
88 if (rawUrl === null) {
89 return null;
90 }
91
92 try {
93 return new URL(rawUrl, specUrl).toString();
94 } catch {
95 return rawUrl;
96 }
97};
98
99const countOperations = (document: JsonRecord): number => {
100 const paths = asRecord(document.paths);
101 if (paths === null) {
102 return 0;
103 }
104
105 const methodNames = new Set([
106 "get",
107 "post",
108 "put",
109 "patch",
110 "delete",
111 "head",
112 "options",
113 "trace",
114 ]);
115
116 let count = 0;
117 for (const pathItem of Object.values(paths)) {
118 const pathRecord = asRecord(pathItem);
119 if (pathRecord === null) {
120 continue;
121 }
122
123 for (const key of Object.keys(pathRecord)) {
124 if (methodNames.has(key.toLowerCase())) {
125 count += 1;
126 }
127 }
128 }
129
130 return count;
131};
132
133export const previewOpenApiDocument = async (
134 input: OpenApiPreviewRequest,
135): Promise<OpenApiPreviewResponse> => {
136 const response = await fetch(input.specUrl);
137 if (!response.ok) {
138 throw new Error(`Failed fetching spec: ${response.status} ${response.statusText}`);
139 }
140
141 const text = await response.text();
142 const parsed = parseDocument(text);
143 const document = asRecord(parsed);
144 if (document === null) {
145 throw new Error("The fetched document is not a valid OpenAPI object.");
146 }
147
148 const info = asRecord(document.info);
149 const warnings: string[] = [];
150
151 const openapiVersion = asString(document.openapi) ?? asString(document.swagger);
152 if (openapiVersion === null) {
153 warnings.push("The document does not declare an OpenAPI/Swagger version.");
154 }
155
156 const baseUrl = deriveBaseUrl(document, input.specUrl);
157 if (baseUrl === null) {
158 warnings.push("No server URL was found in the document.");
159 }
160
161 return {
162 title: asString(info?.title),
163 version: asString(info?.version),
164 baseUrl,
165 operationCount: countOperations(document),
166 warnings,
167 };
168};
169
170export const decodeOpenApiStoredSourceData = (
171 value: unknown,
172): Effect.Effect<OpenApiStoredSourceData, Error, never> =>
173 Effect.try({
174 try: () => Schema.decodeUnknownSync(OpenApiStoredSourceDataSchema)(value),
175 catch: (cause) =>
176 cause instanceof Error ? cause : new Error(String(cause)),
177 });
plugins/openapi/shared/package.jsonrenamed from packages/sources/builtins/package.json+6−4
@@ -1,18 +1,20 @@
1{1{
2 "name": "@executor/source-builtins",2 "name": "@executor/plugin-openapi-shared",
3 "private": true,3 "private": true,
4 "type": "module",4 "type": "module",
5 "exports": {5 "exports": {
6 ".": "./src/index.ts"6 ".": "./index.ts"
7 },7 },
8 "files": [8 "files": [
9 "src"9 "index.ts"
10 ],10 ],
11 "scripts": {11 "scripts": {
12 "typecheck": "bunx tsc --noEmit -p tsconfig.json"12 "typecheck": "bunx tsc --noEmit -p tsconfig.json"
13 },13 },
14 "dependencies": {14 "dependencies": {
15 "@executor/source-core": "workspace:*"15 "@executor/source-core": "workspace:*",
16 "effect": "catalog:",
17 "yaml": "^2.8.1"
16 },18 },
17 "devDependencies": {19 "devDependencies": {
18 "@types/node": "catalog:",20 "@types/node": "catalog:",
plugins/openapi/shared/tsconfig.json+15−0
@@ -0,0 +1,15 @@
1{
2 "compilerOptions": {
3 "target": "ES2022",
4 "module": "ESNext",
5 "moduleResolution": "Bundler",
6 "strict": true,
7 "skipLibCheck": true,
8 "noEmit": true,
9 "lib": ["ES2022", "DOM"],
10 "types": ["bun-types", "node"]
11 },
12 "include": [
13 "index.ts"
14 ]
15}

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