204 lines · 7.2 KB · TypeScript
RawDownload
import {  createToolCatalogFromTools,  makeToolInvokerFromTools,} from "@executor/codemode-core";import * as Context from "effect/Context";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer"; import type {  ExecutionEnvironment,  ResolveExecutionEnvironment,} from "../state";import {  createCodeExecutorForRuntime,  resolveConfiguredExecutionRuntime,} from "../runtime";import {  createScopeToolInvoker,} from "./tool-invoker";import {  RuntimeSourceAuthServiceTag,} from "../../sources/source-auth-service";import {  RuntimeSourceCatalogStoreService,} from "../../catalog/source/runtime";import {  RuntimeSourceAuthMaterialService,} from "../../auth/source-auth-material";import {  RuntimeSourceCatalogSyncService,} from "../../catalog/source/sync";import {  getRuntimeLocalScopeOption,} from "../../scope/runtime-context";import {  LocalInstanceConfigService,  SecretMaterialDeleterService,  SecretMaterialStorerService,  SecretMaterialUpdaterService,} from "../../scope/secret-material-providers";import {  LocalToolRuntimeLoaderService,  type LocalToolRuntimeLoaderShape,  type LocalToolRuntime,} from "../../local-tool-runtime";import {  InstallationStore,  type InstallationStoreShape,  SourceArtifactStore,  type SourceArtifactStoreShape,  ScopeConfigStore,  type ScopeConfigStoreShape,  ScopeStateStore,  type ScopeStateStoreShape,} from "../../scope/storage";import {  ExecutorStateStore,} from "../../executor-state-store";import {  RuntimeSourceStoreService,} from "../../sources/source-store";import type {  CreateScopeInternalToolMap,} from "./tool-invoker";export {  createCodeExecutorForRuntime,  resolveConfiguredExecutionRuntime,} from "../runtime"; const createEmptyLocalToolRuntime = (): LocalToolRuntime => ({  tools: {},  catalog: createToolCatalogFromTools({ tools: {} }),  toolInvoker: makeToolInvokerFromTools({ tools: {} }),  toolPaths: new Set<string>(),}); export const createScopeExecutionEnvironmentResolver =  (input: {    executorStateStore: Effect.Effect.Success<typeof ExecutorStateStore>;    sourceStore: Effect.Effect.Success<typeof RuntimeSourceStoreService>;    sourceCatalogSyncService: Effect.Effect.Success<      typeof RuntimeSourceCatalogSyncService    >;    sourceAuthMaterialService: Effect.Effect.Success<      typeof RuntimeSourceAuthMaterialService    >;    sourceAuthService: Effect.Effect.Success<      typeof RuntimeSourceAuthServiceTag    >;    sourceCatalogStore: Effect.Effect.Success<      typeof RuntimeSourceCatalogStoreService    >;    localToolRuntimeLoader: LocalToolRuntimeLoaderShape;    installationStore: InstallationStoreShape;    instanceConfigResolver: Effect.Effect.Success<typeof LocalInstanceConfigService>;    storeSecretMaterial: Effect.Effect.Success<typeof SecretMaterialStorerService>;    deleteSecretMaterial: Effect.Effect.Success<typeof SecretMaterialDeleterService>;    updateSecretMaterial: Effect.Effect.Success<typeof SecretMaterialUpdaterService>;    scopeConfigStore: ScopeConfigStoreShape;    scopeStateStore: ScopeStateStoreShape;    sourceArtifactStore: SourceArtifactStoreShape;    createInternalToolMap?: CreateScopeInternalToolMap;  }): ResolveExecutionEnvironment =>  ({ scopeId, actorScopeId, onElicitation }) =>    Effect.gen(function* () {      const runtimeLocalScope = yield* getRuntimeLocalScopeOption();      const loadedConfig =        runtimeLocalScope === null          ? null          : yield* input.scopeConfigStore.load();      const localToolRuntime =        runtimeLocalScope === null          ? createEmptyLocalToolRuntime()          : yield* input.localToolRuntimeLoader.load();      const { catalog, toolInvoker } = createScopeToolInvoker({        scopeId,        actorScopeId,        executorStateStore: input.executorStateStore,        sourceStore: input.sourceStore,        sourceCatalogSyncService: input.sourceCatalogSyncService,        sourceCatalogStore: input.sourceCatalogStore,        installationStore: input.installationStore,        instanceConfigResolver: input.instanceConfigResolver,        storeSecretMaterial: input.storeSecretMaterial,        deleteSecretMaterial: input.deleteSecretMaterial,        updateSecretMaterial: input.updateSecretMaterial,        scopeConfigStore: input.scopeConfigStore,        scopeStateStore: input.scopeStateStore,        sourceArtifactStore: input.sourceArtifactStore,        sourceAuthMaterialService: input.sourceAuthMaterialService,        sourceAuthService: input.sourceAuthService,        runtimeLocalScope,        localToolRuntime,        createInternalToolMap: input.createInternalToolMap,        onElicitation,      });       const executor = createCodeExecutorForRuntime(        resolveConfiguredExecutionRuntime(loadedConfig?.config),      );       return {        executor,        toolInvoker,        catalog,      } satisfies ExecutionEnvironment;    }); export class RuntimeExecutionResolverService extends Context.Tag(  "#runtime/RuntimeExecutionResolverService",)<  RuntimeExecutionResolverService,  ReturnType<typeof createScopeExecutionEnvironmentResolver>>() {} export const RuntimeExecutionResolverLive = (  input: {    executionResolver?: ResolveExecutionEnvironment;    createInternalToolMap?: CreateScopeInternalToolMap;  } = {},) =>  input.executionResolver    ? Layer.succeed(RuntimeExecutionResolverService, input.executionResolver)    : Layer.effect(        RuntimeExecutionResolverService,        Effect.gen(function* () {          const executorStateStore = yield* ExecutorStateStore;          const sourceStore = yield* RuntimeSourceStoreService;          const sourceCatalogSyncService =            yield* RuntimeSourceCatalogSyncService;          const sourceAuthMaterialService =            yield* RuntimeSourceAuthMaterialService;          const sourceAuthService = yield* RuntimeSourceAuthServiceTag;          const sourceCatalogStore = yield* RuntimeSourceCatalogStoreService;          const localToolRuntimeLoader = yield* LocalToolRuntimeLoaderService;          const installationStore = yield* InstallationStore;          const instanceConfigResolver = yield* LocalInstanceConfigService;          const storeSecretMaterial = yield* SecretMaterialStorerService;          const deleteSecretMaterial = yield* SecretMaterialDeleterService;          const updateSecretMaterial = yield* SecretMaterialUpdaterService;          const scopeConfigStore = yield* ScopeConfigStore;          const scopeStateStore = yield* ScopeStateStore;          const sourceArtifactStore = yield* SourceArtifactStore;           return createScopeExecutionEnvironmentResolver({            executorStateStore,            sourceStore,            sourceCatalogSyncService,            sourceAuthService,            sourceAuthMaterialService,            sourceCatalogStore,            localToolRuntimeLoader,            installationStore,            instanceConfigResolver,            storeSecretMaterial,            deleteSecretMaterial,            updateSecretMaterial,            scopeConfigStore,            scopeStateStore,            sourceArtifactStore,            createInternalToolMap: input.createInternalToolMap,          });        }),      );