Rhys Sullivanrework naminge5818557 months agoHistory
1110 lines · 37.9 KB · TypeScript
RawDownload
import { createHash, randomUUID } from "node:crypto";import { mkdirSync } from "node:fs";import { dirname, resolve as resolvePath } from "node:path"; import { Database } from "bun:sqlite";import { and, eq } from "drizzle-orm";import { drizzle } from "drizzle-orm/bun-sqlite";import {  integer,  sqliteTable,  text,} from "drizzle-orm/sqlite-core"; import {  createExecutorBackend,  type ExecutorBackend,  type ExecutorBackendRepositories,  type ExecutorWorkspaceConfigRepository,  type ExecutorWorkspaceStateRepository,  type ExecutorWorkspaceSourceArtifactRepository,} from "@executor/platform-sdk";import type {  AuthArtifact,  AuthLease,  Execution,  ExecutionInteraction,  ExecutionStep,  LocalExecutorConfig,  LocalInstallation,  ProviderAuthGrant,  ScopeOauthClient,  ScopedSourceOauthClient,  SecretMaterial,  SecretRef,  SourceAuthSession,} from "@executor/platform-sdk/schema";import {  ScopeIdSchema,  SecretMaterialIdSchema,  SourceCatalogIdSchema,  SourceCatalogRevisionIdSchema,} from "@executor/platform-sdk/schema";import {  contentHash,  snapshotFromSourceCatalogSyncResult,} from "@executor/source-core"; export type CreateSqliteExecutorBackendOptions = {  databasePath?: string;  scopeName?: string;  scopeRoot?: string | null;  scopeId?: string;  actorScopeId?: string;}; type ScopeConfig = Awaited<ReturnType<ExecutorWorkspaceConfigRepository["load"]>>;type ScopeState = Awaited<ReturnType<ExecutorWorkspaceStateRepository["load"]>>;type SourceArtifact =  ReturnType<ExecutorWorkspaceSourceArtifactRepository["build"]>;type SourceArtifactBuildInput = Parameters<  ExecutorWorkspaceSourceArtifactRepository["build"]>[0]; type SecretMaterialSummary = {  id: string;  providerId: string;  name: string | null;  purpose: string;  createdAt: number;  updatedAt: number;}; const SQLITE_SECRET_PROVIDER_ID = "sqlite"; const installations = sqliteTable("installations", {  key: text("key").primaryKey(),  scopeId: text("scope_id").notNull(),  actorScopeId: text("actor_scope_id").notNull(),  resolutionScopeIdsJson: text("resolution_scope_ids_json").notNull(),}); const scopeConfigs = sqliteTable("scope_configs", {  key: text("key").primaryKey(),  projectConfigJson: text("project_config_json").notNull(),}); const scopeStates = sqliteTable("scope_states", {  key: text("key").primaryKey(),  stateJson: text("state_json").notNull(),}); const sourceArtifacts = sqliteTable("source_artifacts", {  sourceId: text("source_id").primaryKey(),  artifactJson: text("artifact_json").notNull(),}); const authArtifacts = sqliteTable("auth_artifacts", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  sourceId: text("source_id").notNull(),  actorScopeId: text("actor_scope_id"),  slot: text("slot").notNull(),  json: text("json").notNull(),}); const authLeases = sqliteTable("auth_leases", {  authArtifactId: text("auth_artifact_id").primaryKey(),  json: text("json").notNull(),}); const sourceOauthClients = sqliteTable("source_oauth_clients", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  sourceId: text("source_id").notNull(),  providerKey: text("provider_key").notNull(),  json: text("json").notNull(),}); const scopeOauthClients = sqliteTable("scope_oauth_clients", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  providerKey: text("provider_key").notNull(),  json: text("json").notNull(),}); const providerAuthGrants = sqliteTable("provider_auth_grants", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  actorScopeId: text("actor_scope_id"),  providerKey: text("provider_key").notNull(),  json: text("json").notNull(),}); const sourceAuthSessions = sqliteTable("source_auth_sessions", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  sourceId: text("source_id").notNull(),  actorScopeId: text("actor_scope_id"),  state: text("state").notNull(),  status: text("status").notNull(),  credentialSlot: text("credential_slot"),  json: text("json").notNull(),}); const secretMaterials = sqliteTable("secret_materials", {  id: text("id").primaryKey(),  providerId: text("provider_id").notNull(),  name: text("name"),  purpose: text("purpose").notNull(),  createdAt: integer("created_at").notNull(),  updatedAt: integer("updated_at").notNull(),  json: text("json").notNull(),}); const executions = sqliteTable("executions", {  id: text("id").primaryKey(),  scopeId: text("scope_id").notNull(),  json: text("json").notNull(),}); const executionInteractions = sqliteTable("execution_interactions", {  id: text("id").primaryKey(),  executionId: text("execution_id").notNull(),  status: text("status").notNull(),  json: text("json").notNull(),}); const executionSteps = sqliteTable("execution_steps", {  id: text("id").primaryKey(),  executionId: text("execution_id").notNull(),  sequence: integer("sequence").notNull(),  json: text("json").notNull(),}); const makeHash = (value: string): string =>  createHash("sha256").update(value).digest("hex").slice(0, 24); const sameActor = (  left: string | null | undefined,  right: string | null | undefined,): boolean => (left ?? null) === (right ?? null); const parseJson = <T>(value: string): T => JSON.parse(value) as T; const createSourceArtifact = (input: SourceArtifactBuildInput): SourceArtifact => {  const snapshot = snapshotFromSourceCatalogSyncResult(input.syncResult);  const sourceConfigJson = JSON.stringify({    kind: input.source.kind,    endpoint: input.source.endpoint,    namespace: input.source.namespace,    name: input.source.name,    enabled: input.source.enabled,    binding: input.source.binding,    auth: input.source.auth,    importAuth: input.source.importAuth,    importAuthPolicy: input.source.importAuthPolicy,  });  const importMetadataJson = JSON.stringify(snapshot.import);  const catalogId = SourceCatalogIdSchema.make(`src_catalog_${makeHash(sourceConfigJson)}`);  const revisionId = SourceCatalogRevisionIdSchema.make(    `src_catalog_rev_${makeHash(sourceConfigJson)}`,  );   return {    version: 4,    sourceId: input.source.id,    catalogId,    generatedAt: Date.now(),    revision: {      id: revisionId,      catalogId,      revisionNumber: 1,      sourceConfigJson,      importMetadataJson,      importMetadataHash: contentHash(importMetadataJson),      snapshotHash: contentHash(JSON.stringify(snapshot)),      createdAt: input.source.createdAt,      updatedAt: input.source.updatedAt,    },    snapshot,  };}; const defaultScopeState = (): ScopeState => ({  version: 1,  sources: {},  policies: {},}); const createInstallation = (  options: CreateSqliteExecutorBackendOptions,): LocalInstallation => {  const scopeId = ScopeIdSchema.make(options.scopeId ?? "scope_sqlite_example");  const actorScopeId = ScopeIdSchema.make(    options.actorScopeId ?? "account_sqlite_example",  );   return {    scopeId,    actorScopeId,    resolutionScopeIds: [scopeId, actorScopeId],  };}; const openSqliteStore = (databasePath: string) => {  if (databasePath !== ":memory:") {    mkdirSync(dirname(databasePath), { recursive: true });  }   const sqlite = new Database(databasePath, { create: true, strict: true });  const db = drizzle(sqlite);   sqlite.exec(`      CREATE TABLE IF NOT EXISTS installations (        key TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        actor_scope_id TEXT NOT NULL,        resolution_scope_ids_json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS scope_configs (        key TEXT PRIMARY KEY NOT NULL,        project_config_json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS scope_states (        key TEXT PRIMARY KEY NOT NULL,        state_json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS source_artifacts (        source_id TEXT PRIMARY KEY NOT NULL,        artifact_json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS auth_artifacts (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        source_id TEXT NOT NULL,        actor_scope_id TEXT,        slot TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS auth_leases (        auth_artifact_id TEXT PRIMARY KEY NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS source_oauth_clients (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        source_id TEXT NOT NULL,        provider_key TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS scope_oauth_clients (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        provider_key TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS provider_auth_grants (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        actor_scope_id TEXT,        provider_key TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS source_auth_sessions (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        source_id TEXT NOT NULL,        actor_scope_id TEXT,        state TEXT NOT NULL,        status TEXT NOT NULL,        credential_slot TEXT,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS secret_materials (        id TEXT PRIMARY KEY NOT NULL,        provider_id TEXT NOT NULL,        name TEXT,        purpose TEXT NOT NULL,        created_at INTEGER NOT NULL,        updated_at INTEGER NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS executions (        id TEXT PRIMARY KEY NOT NULL,        scope_id TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS execution_interactions (        id TEXT PRIMARY KEY NOT NULL,        execution_id TEXT NOT NULL,        status TEXT NOT NULL,        json TEXT NOT NULL      );       CREATE TABLE IF NOT EXISTS execution_steps (        id TEXT PRIMARY KEY NOT NULL,        execution_id TEXT NOT NULL,        sequence INTEGER NOT NULL,        json TEXT NOT NULL      );    `);  return {    sqlite,    db,    close: () => {      sqlite.close();    },  };}; type SqliteStore = ReturnType<typeof openSqliteStore>; const createStorageDomains = (store: SqliteStore) => ({  auth: {    artifacts: {    listByScopeId: (scopeId: AuthArtifact["scopeId"]) =>      store.db.select().from(authArtifacts).where(eq(authArtifacts.scopeId, scopeId)).all()        .map((row) => parseJson<AuthArtifact>(row.json)),    listByScopeAndSourceId: (input: {      scopeId: AuthArtifact["scopeId"];      sourceId: AuthArtifact["sourceId"];    }) =>      store.db.select().from(authArtifacts).where(        and(          eq(authArtifacts.scopeId, input.scopeId),          eq(authArtifacts.sourceId, input.sourceId),        ),      ).all().map((row) => parseJson<AuthArtifact>(row.json)),    getByScopeSourceAndActor: (input: {      scopeId: AuthArtifact["scopeId"];      sourceId: AuthArtifact["sourceId"];      actorScopeId: AuthArtifact["actorScopeId"];      slot: AuthArtifact["slot"];    }) =>      store.db.select().from(authArtifacts).where(        and(          eq(authArtifacts.scopeId, input.scopeId),          eq(authArtifacts.sourceId, input.sourceId),          eq(authArtifacts.slot, input.slot),        ),      ).all().map((row) => parseJson<AuthArtifact>(row.json))        .find((item) => sameActor(item.actorScopeId, input.actorScopeId)) ?? null,    upsert: (artifact: AuthArtifact) =>      store.db.insert(authArtifacts).values({        id: artifact.id,        scopeId: artifact.scopeId,        sourceId: artifact.sourceId,        actorScopeId: artifact.actorScopeId,        slot: artifact.slot,        json: JSON.stringify(artifact),      }).onConflictDoUpdate({        target: authArtifacts.id,        set: {          scopeId: artifact.scopeId,          sourceId: artifact.sourceId,          actorScopeId: artifact.actorScopeId,          slot: artifact.slot,          json: JSON.stringify(artifact),        },      }).run(),    removeByScopeSourceAndActor: (input: {      scopeId: AuthArtifact["scopeId"];      sourceId: AuthArtifact["sourceId"];      actorScopeId: AuthArtifact["actorScopeId"];      slot?: AuthArtifact["slot"];    }) => {      const rows = store.db.select().from(authArtifacts).where(        and(          eq(authArtifacts.scopeId, input.scopeId),          eq(authArtifacts.sourceId, input.sourceId),        ),      ).all();      const match = rows.find((row) => {        const item = parseJson<AuthArtifact>(row.json);        return sameActor(item.actorScopeId, input.actorScopeId)          && (input.slot === undefined || item.slot === input.slot);      });      if (!match) return false;      store.db.delete(authArtifacts).where(eq(authArtifacts.id, match.id)).run();      return true;    },    removeByScopeAndSourceId: (input: {      scopeId: AuthArtifact["scopeId"];      sourceId: AuthArtifact["sourceId"];    }) => {      const rows = store.db.select({ id: authArtifacts.id }).from(authArtifacts).where(        and(          eq(authArtifacts.scopeId, input.scopeId),          eq(authArtifacts.sourceId, input.sourceId),        ),      ).all();      store.db.delete(authArtifacts).where(        and(          eq(authArtifacts.scopeId, input.scopeId),          eq(authArtifacts.sourceId, input.sourceId),        ),      ).run();      return rows.length;    },  },    leases: {    listAll: () =>      store.db.select().from(authLeases).all().map((row) => parseJson<AuthLease>(row.json)),    getByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) => {      const row = store.db.select().from(authLeases).where(        eq(authLeases.authArtifactId, authArtifactId),      ).get();      return row ? parseJson<AuthLease>(row.json) : null;    },    upsert: (lease: AuthLease) =>      store.db.insert(authLeases).values({        authArtifactId: lease.authArtifactId,        json: JSON.stringify(lease),      }).onConflictDoUpdate({        target: authLeases.authArtifactId,        set: { json: JSON.stringify(lease) },      }).run(),    removeByAuthArtifactId: (authArtifactId: AuthLease["authArtifactId"]) => {      const row = store.db.select({ id: authLeases.authArtifactId }).from(authLeases).where(        eq(authLeases.authArtifactId, authArtifactId),      ).get();      if (!row) return false;      store.db.delete(authLeases).where(eq(authLeases.authArtifactId, authArtifactId)).run();      return true;    },  },    sourceOauthClients: {    getByScopeSourceAndProvider: (input: {      scopeId: ScopedSourceOauthClient["scopeId"];      sourceId: ScopedSourceOauthClient["sourceId"];      providerKey: string;    }) => {      const row = store.db.select().from(sourceOauthClients).where(        and(          eq(sourceOauthClients.scopeId, input.scopeId),          eq(sourceOauthClients.sourceId, input.sourceId),          eq(sourceOauthClients.providerKey, input.providerKey),        ),      ).get();      return row ? parseJson<ScopedSourceOauthClient>(row.json) : null;    },    upsert: (oauthClient: ScopedSourceOauthClient) =>      store.db.insert(sourceOauthClients).values({        id: oauthClient.id,        scopeId: oauthClient.scopeId,        sourceId: oauthClient.sourceId,        providerKey: oauthClient.providerKey,        json: JSON.stringify(oauthClient),

Lines 1–500 of 1110.