Rhys Sullivanbackupa3147538 months agoHistory
1658 lines · 48.9 KB · TypeScript
RawDownload
import { existsSync, readFileSync } from "node:fs";import { mkdir, rename, unlink } from "node:fs/promises";import { join, resolve } from "node:path"; import { FileSystem } from "@effect/platform";import { PGlite } from "@electric-sql/pglite";import {  buildOpenApiToolPresentation,  compileOpenApiToolDefinitions,  extractOpenApiManifest,  type OpenApiRefHintTable,  type OpenApiToolProviderData,} from "@executor/codemode-openapi";import {  AuthArtifactIdSchema,  type JsonObject,  McpSourceAuthSessionDataJsonSchema,  type Execution,  type ExecutionInteraction,  type LocalExecutorConfig,  type SecretMaterial,  type Source,  type SourceAuth,  type SourceTransport,  type SourceAuthSession,} from "#schema";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";import postgres from "postgres"; import {  buildGraphqlToolPresentation,  compileGraphqlToolDefinitions,  extractGraphqlManifest,  type GraphqlToolProviderData,} from "./graphql-tools";import {  type LocalControlPlaneState,  writeLocalControlPlaneState,} from "./local-control-plane-store";import {  type ResolvedLocalWorkspaceContext,  writeProjectLocalExecutorConfig,} from "./local-config";import { deriveLocalInstallation } from "./local-installation";import { derivePolicyConfigKey } from "./local-workspace-sync";import {  type LocalWorkspaceState,  writeLocalWorkspaceState,} from "./local-workspace-state";import {  buildLocalSourceArtifact,  writeLocalSourceArtifact,  type LocalSourceArtifact,} from "./local-source-artifacts";import { namespaceFromSourceName } from "./source-names";import {  createCatalogImportMetadata,  createGraphqlCatalogFragment,  createMcpCatalogFragment,  createOpenApiCatalogFragment,  type GraphqlCatalogOperationInput,  type McpCatalogOperationInput,  type OpenApiCatalogOperationInput,} from "./source-catalog-snapshot";import { authArtifactFromSourceAuth } from "./auth-artifacts";import { createSourceCatalogSyncResult, contentHash } from "./source-catalog-support";import { getSourceAdapter } from "./source-adapters"; const LOCAL_CONTROL_PLANE_STATE_BASENAME = "control-plane-state.json";const WORKSPACE_STATE_BASENAME = "workspace-state.json";const LEGACY_LOCAL_INSTALLATION_ID = "local_default";const LEGACY_POSTGRES_SECRET_PROVIDER_ID = "postgres";const LOCAL_SECRET_PROVIDER_ID = "local";const MIGRATABLE_LEGACY_SOURCE_KINDS = ["mcp", "openapi", "graphql"] as const; type MigratableLegacySourceKind = (typeof MIGRATABLE_LEGACY_SOURCE_KINDS)[number];type LocalConfigSourceEntry = NonNullable<LocalExecutorConfig["sources"]>[string];type LocalWorkspaceSourceState = LocalWorkspaceState["sources"][string]; type LegacyRow = Record<string, unknown>; type LegacyLocalInstallationRow = {  id: string;  account_id: string;  workspace_id: string;}; type LegacyWorkspaceRow = {  id: string;  name: string | null;}; type LegacySourceRow = {  workspace_id: string;  source_id: string;  name: string;  kind: string;  endpoint: string;  status: string;  enabled: boolean;  namespace: string | null;  transport: string | null;  query_params_json: string | null;  headers_json: string | null;  spec_url: string | null;  default_headers_json: string | null;  source_hash: string | null;  source_document_text: string | null;  last_error: string | null;  created_at: number;  updated_at: number;}; type LegacyCredentialRow = {  id: string;  workspace_id: string;  auth_kind: string;  auth_header_name: string;  auth_prefix: string;  token_provider_id: string;  token_handle: string;  refresh_token_provider_id: string | null;  refresh_token_handle: string | null;  created_at: number;  updated_at: number;}; type LegacySourceCredentialBindingRow = {  id: string;  workspace_id: string;  source_id: string;  credential_id: string;}; type LegacySecretMaterialRow = {  id: string;  name: string | null;  purpose: SecretMaterial["purpose"];  value: string;  created_at: number;  updated_at: number;}; type LegacyPolicyRow = {  id: string;  scope_type: string;  workspace_id: string | null;  target_account_id: string | null;  client_id: string | null;  resource_type: string;  resource_pattern: string;  match_type: string;  effect: string;  approval_mode: string;  priority: number;  enabled: boolean;  argument_conditions_json: string | null;  created_at: number;  updated_at: number;}; type LegacyExecutionRow = {  id: string;  workspace_id: string;  created_by_account_id: string;  status: Execution["status"];  code: string;  result_json: string | null;  error_text: string | null;  logs_json: string | null;  started_at: number | null;  completed_at: number | null;  created_at: number;  updated_at: number;}; type LegacyExecutionInteractionRow = {  id: string;  execution_id: string;  status: ExecutionInteraction["status"];  kind: string;  purpose: string;  payload_json: string;  response_json: string | null;  created_at: number;  updated_at: number;}; type LegacySourceAuthSessionRow = {  id: string;  workspace_id: string;  source_id: string;  execution_id: string | null;  interaction_id: string | null;  strategy: string;  status: SourceAuthSession["status"];  endpoint: string;  state: string;  redirect_uri: string;  scope: string | null;  resource_metadata_url: string | null;  authorization_server_url: string | null;  resource_metadata_json: string | null;  authorization_server_metadata_json: string | null;  client_information_json: string | null;  code_verifier: string | null;  authorization_url: string | null;  error_text: string | null;  completed_at: number | null;  created_at: number;  updated_at: number;}; type LegacyToolArtifactRow = {  workspace_id: string;  path: string;  tool_id: string;  source_id: string;  title: string | null;  description: string | null;  search_namespace: string;  search_text: string;  input_schema_json: string | null;  output_schema_json: string | null;  provider_kind: string;  mcp_tool_name: string | null;  created_at: number;  updated_at: number;}; type LegacyWorkspaceSnapshot = {  installation: LegacyLocalInstallationRow;  workspace: LegacyWorkspaceRow | null;  sources: LegacySourceRow[];  credentials: LegacyCredentialRow[];  sourceCredentialBindings: LegacySourceCredentialBindingRow[];  secretMaterials: LegacySecretMaterialRow[];  policies: LegacyPolicyRow[];  executions: LegacyExecutionRow[];  executionInteractions: LegacyExecutionInteractionRow[];  sourceAuthSessions: LegacySourceAuthSessionRow[];  toolArtifacts: LegacyToolArtifactRow[];}; type LegacySnapshotSource =  | {      kind: "pglite";      localDataDir: string;    }  | {      kind: "postgres";      databaseUrl: string;    }; type LoadedLegacyWorkspaceSnapshot = {  snapshot: LegacyWorkspaceSnapshot;  source: LegacySnapshotSource;}; type LegacyQueryClient = {  label: string;  query: (sql: string) => Promise<LegacyRow[]>;  close: () => Promise<void>;}; type MigratedPolicyEntry = {  key: string;  config: NonNullable<LocalExecutorConfig["policies"]>[string];  state: LocalWorkspaceState["policies"][string];}; type MigratedWorkspace = {  projectConfig: LocalExecutorConfig;  workspaceState: LocalWorkspaceState;  controlPlaneState: LocalControlPlaneState;  sourceArtifacts: Array<{    sourceId: Source["id"];    artifact: LocalSourceArtifact;  }>;  sourceCount: number;  policyCount: number;}; const encodeMcpSourceAuthSessionData = Schema.encodeSync(  McpSourceAuthSessionDataJsonSchema,); const trimOrNull = (value: unknown): string | null => {  if (typeof value !== "string") {    return null;  }   const trimmed = value.trim();  return trimmed.length > 0 ? trimmed : null;}; const toError = (cause: unknown): Error =>  cause instanceof Error ? cause : new Error(String(cause)); const asRecord = (value: unknown): Record<string, unknown> =>  value !== null && typeof value === "object" && !Array.isArray(value)    ? (value as Record<string, unknown>)    : {}; const asString = (value: unknown): string => {  const trimmed = trimOrNull(value);  return trimmed ?? "";}; const asNullableString = (value: unknown): string | null => trimOrNull(value); const asNumber = (value: unknown): number => {  if (typeof value === "number" && Number.isFinite(value)) {    return value;  }  if (typeof value === "bigint") {    return Number(value);  }  if (typeof value === "string" && value.trim().length > 0) {    const parsed = Number(value);    return Number.isFinite(parsed) ? parsed : 0;  }  return 0;}; const asNullableNumber = (value: unknown): number | null => {  if (value === null || value === undefined) {    return null;  }  return asNumber(value);}; const asBoolean = (value: unknown): boolean => {  if (typeof value === "boolean") {    return value;  }  if (typeof value === "number") {    return value !== 0;  }  if (typeof value === "string") {    const normalized = value.trim().toLowerCase();    return normalized === "true" || normalized === "t" || normalized === "1";  }  return false;}; const asStringMap = (value: string | null): Record<string, string> | null => {  if (value === null) {    return null;  }   try {    const parsed = JSON.parse(value) as unknown;    const record = asRecord(parsed);    const normalized: Record<string, string> = {};    for (const [key, entry] of Object.entries(record)) {      if (typeof entry === "string") {        normalized[key] = entry;      }    }    return normalized;  } catch {    return null;  }}; const safeJsonParse = <T>(value: string | null): T | undefined => {  if (value === null) {    return undefined;  }   try {    return JSON.parse(value) as T;  } catch {    return undefined;  }}; const stableSourceNamespace = (row: Pick<LegacySourceRow, "name" | "namespace">): string =>  trimOrNull(row.namespace) ?? namespaceFromSourceName(row.name); const normalizeLegacyRows = (value: unknown): LegacyRow[] => {  if (Array.isArray(value)) {    return value.map(asRecord);  }   const record = asRecord(value);  if (Array.isArray(record.rows)) {    return record.rows.map(asRecord);  }   return [];}; const cloneJson = <T>(value: T): T =>  JSON.parse(JSON.stringify(value)) as T; const isMigratableLegacySourceKind = (  value: string,): value is MigratableLegacySourceKind =>  (MIGRATABLE_LEGACY_SOURCE_KINDS as readonly string[]).includes(value); const decodeLegacyLocalInstallationRow = (row: LegacyRow): LegacyLocalInstallationRow => ({  id: asString(row.id),  account_id: asString(row.account_id),  workspace_id: asString(row.workspace_id),}); const decodeLegacyWorkspaceRow = (row: LegacyRow): LegacyWorkspaceRow => ({  id: asString(row.id),  name: asNullableString(row.name),}); const decodeLegacySourceRow = (row: LegacyRow): LegacySourceRow => ({  workspace_id: asString(row.workspace_id),  source_id: asString(row.source_id),  name: asString(row.name),  kind: asString(row.kind),  endpoint: asString(row.endpoint),  status: asString(row.status),  enabled: asBoolean(row.enabled),  namespace: asNullableString(row.namespace),  transport: asNullableString(row.transport),  query_params_json: asNullableString(row.query_params_json),  headers_json: asNullableString(row.headers_json),  spec_url: asNullableString(row.spec_url),  default_headers_json: asNullableString(row.default_headers_json),  source_hash: asNullableString(row.source_hash),  source_document_text: asNullableString(row.source_document_text),  last_error: asNullableString(row.last_error),  created_at: asNumber(row.created_at),  updated_at: asNumber(row.updated_at),}); const decodeLegacyCredentialRow = (row: LegacyRow): LegacyCredentialRow => ({  id: asString(row.id),  workspace_id: asString(row.workspace_id),  auth_kind: asString(row.auth_kind),  auth_header_name: asString(row.auth_header_name),  auth_prefix: asString(row.auth_prefix),  token_provider_id: asString(row.token_provider_id),  token_handle: asString(row.token_handle),  refresh_token_provider_id: asNullableString(row.refresh_token_provider_id),  refresh_token_handle: asNullableString(row.refresh_token_handle),  created_at: asNumber(row.created_at),  updated_at: asNumber(row.updated_at),}); const decodeLegacySourceCredentialBindingRow = (  row: LegacyRow,): LegacySourceCredentialBindingRow => ({  id: asString(row.id),  workspace_id: asString(row.workspace_id),  source_id: asString(row.source_id),  credential_id: asString(row.credential_id),}); const decodeLegacySecretMaterialRow = (row: LegacyRow): LegacySecretMaterialRow => ({  id: asString(row.id),  name: asNullableString(row.name),  purpose:    asString(row.purpose) as LegacySecretMaterialRow["purpose"],  value: asString(row.value),  created_at: asNumber(row.created_at),  updated_at: asNumber(row.updated_at),}); const decodeLegacyPolicyRow = (row: LegacyRow): LegacyPolicyRow => ({  id: asString(row.id),  scope_type: asString(row.scope_type),  workspace_id: asNullableString(row.workspace_id),  target_account_id: asNullableString(row.target_account_id),  client_id: asNullableString(row.client_id),  resource_type: asString(row.resource_type),  resource_pattern: asString(row.resource_pattern),  match_type: asString(row.match_type),  effect: asString(row.effect),  approval_mode: asString(row.approval_mode),  priority: asNumber(row.priority),  enabled: row.enabled === undefined ? true : asBoolean(row.enabled),  argument_conditions_json: asNullableString(row.argument_conditions_json),  created_at: asNumber(row.created_at),  updated_at: asNumber(row.updated_at),}); const decodeLegacyExecutionRow = (row: LegacyRow): LegacyExecutionRow => ({  id: asString(row.id),  workspace_id: asString(row.workspace_id),  created_by_account_id: asString(row.created_by_account_id),  status: asString(row.status) as LegacyExecutionRow["status"],  code: asString(row.code),  result_json: asNullableString(row.result_json),  error_text: asNullableString(row.error_text),  logs_json: asNullableString(row.logs_json),  started_at: asNullableNumber(row.started_at),  completed_at: asNullableNumber(row.completed_at),  created_at: asNumber(row.created_at),  updated_at: asNumber(row.updated_at),});

Lines 1–500 of 1658.