import * as Effect from "effect/Effect"; import type { ScopeId, Execution, ExecutionEnvelope, ExecutionInteraction, LocalInstallation, LocalScopePolicy, ProviderAuthGrant, ScopeOauthClient, Source, } from "./schema"; import { ExecutionIdSchema } from "./schema"; import type { CreateExecutionPayload, ResumeExecutionPayload, } from "./executions/contracts"; import type { CreateSecretPayload, CreateSecretResult, DeleteSecretResult, InstanceConfig, SecretListItem, UpdateSecretPayload, UpdateSecretResult, } from "./local/contracts"; import { completeSourceCredentialSetup, getLocalInstallation, getSourceCredentialInteraction, submitSourceCredentialInteraction, } from "./local/operations"; import { createLocalSecret, deleteLocalSecret, getLocalInstanceConfig, listLocalSecrets, updateLocalSecret, } from "./local/secrets"; import type { CreatePolicyPayload, UpdatePolicyPayload, } from "./policies/contracts"; import { createPolicy, getPolicy, listPolicies, removePolicy, updatePolicy, } from "./policies/operations"; import type { CreateScopeOauthClientPayload, CreateSourcePayload, UpdateSourcePayload, } from "./sources/contracts"; import { discoverSource } from "./sources/discovery"; import { discoverSourceInspectionTools, getSourceInspection, getSourceInspectionToolDetail, } from "./sources/inspection"; import { createSource, getSource, listSources, removeSource, updateSource, } from "./sources/operations"; import type { ExecutorBackend } from "./backend"; import { provideExecutorRuntime, type CreateScopeInternalToolMap, type ExecutorRuntime, type ExecutorRuntimeOptions, type ResolveExecutionEnvironment, type ResolveSecretMaterial, RuntimeSourceAuthServiceTag, } from "./runtime"; import { createExecution, getExecution, resumeExecution, } from "./runtime/execution/service"; import type { CompleteProviderOauthCallbackResult, CompleteSourceCredentialSetupResult, CompleteSourceOAuthSessionResult, ConnectGoogleDiscoveryBatchInput, ConnectGoogleDiscoveryBatchResult, ConnectMcpSourceInput, ExecutorAddSourceInput, ExecutorSourceAddResult, McpSourceConnectResult, StartSourceOAuthSessionInput, StartSourceOAuthSessionResult, } from "./runtime/sources/source-auth-service"; type DistributiveOmit = T extends unknown ? Omit : never; type ProvidedEffect> = Effect.Effect< Effect.Effect.Success, Effect.Effect.Error, never >; type MappedProvidedEffect< T extends Effect.Effect, A, > = Effect.Effect, never>; export type ExecutorSourceInput = DistributiveOmit< ExecutorAddSourceInput, "scopeId" | "actorScopeId" | "executionId" | "interactionId" >; export type ExecutorSourceBatchInput = DistributiveOmit< ConnectGoogleDiscoveryBatchInput, "scopeId" | "actorScopeId" | "executionId" | "interactionId" >; export type ExecutorMcpSourceInput = DistributiveOmit< ConnectMcpSourceInput, "scopeId" | "actorScopeId" >; export type ExecutorSourceOAuthInput = DistributiveOmit< StartSourceOAuthSessionInput, "scopeId" | "actorScopeId" >; export type ExecutorEffect = { runtime: ExecutorRuntime; installation: LocalInstallation; scopeId: ScopeId; actorScopeId: ScopeId; resolutionScopeIds: ReadonlyArray; close: () => Promise; local: { installation: () => ProvidedEffect>; config: () => ProvidedEffect>; credentials: { get: (input: { sourceId: Source["id"]; interactionId: ExecutionInteraction["id"]; }) => ProvidedEffect>; submit: (input: { sourceId: Source["id"]; interactionId: ExecutionInteraction["id"]; action: "submit" | "continue" | "cancel"; token?: string | null; }) => ProvidedEffect< ReturnType >; complete: (input: { sourceId: Source["id"]; state: string; code?: string | null; error?: string | null; errorDescription?: string | null; }) => MappedProvidedEffect< ReturnType, CompleteSourceCredentialSetupResult >; }; }; secrets: { list: () => ProvidedEffect>; create: ( payload: CreateSecretPayload, ) => ProvidedEffect>; update: (input: { secretId: string; payload: UpdateSecretPayload; }) => ProvidedEffect>; remove: ( secretId: string, ) => MappedProvidedEffect< ReturnType, DeleteSecretResult >; }; policies: { list: () => ProvidedEffect>; create: ( payload: CreatePolicyPayload, ) => ProvidedEffect>; get: (policyId: string) => ProvidedEffect>; update: ( policyId: string, payload: UpdatePolicyPayload, ) => ProvidedEffect>; remove: ( policyId: string, ) => MappedProvidedEffect, boolean>; }; sources: { add: ( input: ExecutorSourceInput, options?: { baseUrl?: string | null; }, ) => Effect.Effect; connect: ( payload: ExecutorMcpSourceInput, ) => Effect.Effect; connectBatch: ( payload: ExecutorSourceBatchInput, ) => Effect.Effect; discover: (input: { url: string; probeAuth?: Parameters[0]["probeAuth"]; }) => ProvidedEffect>; list: () => ProvidedEffect>; create: ( payload: CreateSourcePayload, ) => ProvidedEffect>; get: (sourceId: Source["id"]) => ProvidedEffect>; update: ( sourceId: Source["id"], payload: UpdateSourcePayload, ) => ProvidedEffect>; remove: ( sourceId: Source["id"], ) => MappedProvidedEffect, boolean>; inspection: { get: ( sourceId: Source["id"], ) => ProvidedEffect>; tool: (input: { sourceId: Source["id"]; toolPath: string; }) => ProvidedEffect>; discover: (input: { sourceId: Source["id"]; payload: Parameters[0]["payload"]; }) => ProvidedEffect< ReturnType >; }; oauthClients: { list: ( providerKey: string, ) => Effect.Effect, Error, never>; create: ( payload: CreateScopeOauthClientPayload, ) => Effect.Effect; remove: ( oauthClientId: ScopeOauthClient["id"], ) => Effect.Effect; }; providerGrants: { remove: ( grantId: ProviderAuthGrant["id"], ) => Effect.Effect; }; }; oauth: { startSourceAuth: ( input: ExecutorSourceOAuthInput, ) => Effect.Effect; completeSourceAuth: (input: { state: string; code?: string | null; error?: string | null; errorDescription?: string | null; }) => Effect.Effect; completeProviderCallback: (input: { scopeId?: ScopeId; actorScopeId?: ScopeId | null; state: string; code?: string | null; error?: string | null; errorDescription?: string | null; }) => Effect.Effect; }; executions: { create: ( payload: CreateExecutionPayload, ) => ProvidedEffect>; get: ( executionId: Execution["id"], ) => ProvidedEffect>; resume: ( executionId: Execution["id"], payload: ResumeExecutionPayload, ) => ProvidedEffect>; }; }; export type CreateExecutorEffectOptions = ExecutorRuntimeOptions & { backend: ExecutorBackend; }; const fromRuntime = (runtime: ExecutorRuntime): ExecutorEffect => { const installation = runtime.localInstallation; const scopeId = installation.scopeId; const actorScopeId = installation.actorScopeId; const provide = (effect: Effect.Effect) => provideExecutorRuntime(effect, runtime); const provideSourceAuth = ( execute: ( service: Effect.Effect.Success, ) => Effect.Effect, ) => provide(Effect.flatMap(RuntimeSourceAuthServiceTag, execute)); const createSdkSourceSession = () => { const id = crypto.randomUUID(); return { executionId: ExecutionIdSchema.make(`exec_sdk_${id}`), interactionId: `executor.sdk.${id}` as never, }; }; return { runtime, installation, scopeId, actorScopeId, resolutionScopeIds: installation.resolutionScopeIds, close: () => runtime.close(), local: { installation: () => provide(getLocalInstallation()), config: () => provide(getLocalInstanceConfig()), credentials: { get: ({ sourceId, interactionId }) => provide( getSourceCredentialInteraction({ scopeId, sourceId, interactionId, }), ), submit: ({ sourceId, interactionId, action, token }) => provide( submitSourceCredentialInteraction({ scopeId, sourceId, interactionId, action, token, }), ), complete: ({ sourceId, state, code, error, errorDescription }) => provide( completeSourceCredentialSetup({ scopeId, sourceId, state, code, error, errorDescription, }), ), }, }, secrets: { list: () => provide(listLocalSecrets()), create: (payload) => provide(createLocalSecret(payload)), update: (input) => provide(updateLocalSecret(input)), remove: (secretId) => provide(deleteLocalSecret(secretId)), }, policies: { list: () => provide(listPolicies(scopeId)), create: (payload) => provide(createPolicy({ scopeId, payload })), get: (policyId) => provide(getPolicy({ scopeId, policyId: policyId as never })), update: (policyId, payload) => provide(updatePolicy({ scopeId, policyId: policyId as never, payload })), remove: (policyId) => provide(removePolicy({ scopeId, policyId: policyId as never })).pipe( Effect.map((result) => result.removed), ), }, sources: { add: (input, options) => provideSourceAuth((service) => { const session = createSdkSourceSession(); return service.addExecutorSource( { ...input, scopeId, actorScopeId, executionId: session.executionId, interactionId: session.interactionId, }, options, ); }), connect: (payload) => provideSourceAuth((service) => service.connectMcpSource({ ...payload, scopeId, actorScopeId, }), ), connectBatch: (payload) => provideSourceAuth((service) => { const session = createSdkSourceSession(); return service.connectGoogleDiscoveryBatch({ ...payload, scopeId, actorScopeId, executionId: session.executionId, interactionId: session.interactionId, }); }), discover: (input) => provide(discoverSource(input)), list: () => provide(listSources({ scopeId, actorScopeId })), create: (payload) => provide(createSource({ scopeId, actorScopeId, payload })), get: (sourceId) => provide(getSource({ scopeId, sourceId, actorScopeId })), update: (sourceId, payload) => provide(updateSource({ scopeId, sourceId, actorScopeId, payload })), remove: (sourceId) => provide(removeSource({ scopeId, sourceId })).pipe( Effect.map((result) => result.removed), ), inspection: { get: (sourceId) => provide(getSourceInspection({ scopeId, sourceId })), tool: ({ sourceId, toolPath }) => provide( getSourceInspectionToolDetail({ scopeId, sourceId, toolPath, }), ), discover: ({ sourceId, payload }) => provide( discoverSourceInspectionTools({ scopeId, sourceId, payload, }), ), }, oauthClients: { list: (providerKey) => provideSourceAuth((service) => service.listScopeOauthClients({ scopeId, providerKey, }), ), create: (payload) => provideSourceAuth((service) => service.createScopeOauthClient({ scopeId, providerKey: payload.providerKey, label: payload.label, oauthClient: payload.oauthClient, }), ), remove: (oauthClientId) => provideSourceAuth((service) => service.removeScopeOauthClient({ scopeId, oauthClientId, }), ), }, providerGrants: { remove: (grantId) => provideSourceAuth((service) => service.removeProviderAuthGrant({ scopeId, grantId, }), ), }, }, oauth: { startSourceAuth: (input) => provideSourceAuth((service) => service.startSourceOAuthSession({ ...input, scopeId, actorScopeId, }), ), completeSourceAuth: ({ state, code, error, errorDescription }) => provideSourceAuth((service) => service.completeSourceOAuthSession({ state, code, error, errorDescription, }), ), completeProviderCallback: (input) => provideSourceAuth((service) => service.completeProviderOauthCallback({ ...input, scopeId: input.scopeId ?? scopeId, actorScopeId: input.actorScopeId ?? actorScopeId, }), ), }, executions: { create: (payload) => provide( createExecution({ scopeId, payload, createdByScopeId: actorScopeId, }), ), get: (executionId) => provide(getExecution({ scopeId, executionId })), resume: (executionId, payload) => provide( resumeExecution({ scopeId, executionId, payload, resumedByScopeId: actorScopeId, }), ), }, }; }; export const createExecutorEffect = ( options: CreateExecutorEffectOptions, ): Effect.Effect => Effect.map( options.backend.createRuntime({ executionResolver: options.executionResolver, createInternalToolMap: options.createInternalToolMap, resolveSecretMaterial: options.resolveSecretMaterial, getLocalServerBaseUrl: options.getLocalServerBaseUrl, }), fromRuntime, );