|
| 1 | +import { createSupabaseRepositories } from '@liam-hq/agent' |
| 2 | +import { |
| 3 | + createClient, |
| 4 | + type SupabaseClientType, |
| 5 | + toResultAsync, |
| 6 | +} from '@liam-hq/db' |
| 7 | +import { err, ok, type Result, type ResultAsync } from 'neverthrow' |
| 8 | + |
| 9 | +type Repositories = ReturnType<typeof createSupabaseRepositories> |
| 10 | + |
| 11 | +type SetupResult = { |
| 12 | + repositories: Repositories |
| 13 | + organizationId: string |
| 14 | + buildingSchemaId: string |
| 15 | + designSessionId: string |
| 16 | + userId: string |
| 17 | +} |
| 18 | + |
| 19 | +let cachedSetup: SetupResult | null = null |
| 20 | + |
| 21 | +/** |
| 22 | + * Create Supabase client |
| 23 | + */ |
| 24 | +const createDatabaseConnection = (): Result<SupabaseClientType, Error> => { |
| 25 | + const supabaseUrl = process.env['NEXT_PUBLIC_SUPABASE_URL'] |
| 26 | + const supabaseKey = process.env['SUPABASE_SERVICE_ROLE_KEY'] |
| 27 | + |
| 28 | + if (!supabaseUrl || !supabaseKey) { |
| 29 | + return err( |
| 30 | + new Error( |
| 31 | + 'Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY', |
| 32 | + ), |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + const supabaseClient = createClient(supabaseUrl, supabaseKey) |
| 37 | + return ok(supabaseClient) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Get the first organization from database |
| 42 | + */ |
| 43 | +const getFirstOrganization = ( |
| 44 | + supabaseClient: SupabaseClientType, |
| 45 | +): ResultAsync<{ id: string }, Error> => { |
| 46 | + return toResultAsync( |
| 47 | + supabaseClient.from('organizations').select('id').limit(1).single(), |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Get the first user from database |
| 53 | + */ |
| 54 | +const getUser = ( |
| 55 | + supabaseClient: SupabaseClientType, |
| 56 | +): ResultAsync<{ id: string }, Error> => { |
| 57 | + return toResultAsync( |
| 58 | + supabaseClient.from('users').select('id').limit(1).single(), |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +const getDesignSession = ( |
| 63 | + supabaseClient: SupabaseClientType, |
| 64 | + organizationId: string, |
| 65 | +): ResultAsync<{ id: string }, Error> => { |
| 66 | + return toResultAsync( |
| 67 | + supabaseClient |
| 68 | + .from('design_sessions') |
| 69 | + .select('id') |
| 70 | + .eq('organization_id', organizationId) |
| 71 | + .limit(1) |
| 72 | + .single(), |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +const createDesignSession = ( |
| 77 | + supabaseClient: SupabaseClientType, |
| 78 | + organizationId: string, |
| 79 | + userId: string, |
| 80 | +): ResultAsync<{ id: string }, Error> => { |
| 81 | + return toResultAsync( |
| 82 | + supabaseClient |
| 83 | + .from('design_sessions') |
| 84 | + .insert({ |
| 85 | + name: 'Schema Benchmark Session', |
| 86 | + project_id: null, |
| 87 | + organization_id: organizationId, |
| 88 | + created_by_user_id: userId, |
| 89 | + parent_design_session_id: null, |
| 90 | + }) |
| 91 | + .select('id') |
| 92 | + .single(), |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Get or create a design session for benchmarking |
| 98 | + */ |
| 99 | +const getOrCreateDesignSession = ( |
| 100 | + supabaseClient: SupabaseClientType, |
| 101 | + organizationId: string, |
| 102 | + userId: string, |
| 103 | +): ResultAsync<{ id: string }, Error> => { |
| 104 | + return getDesignSession(supabaseClient, organizationId).orElse(() => |
| 105 | + createDesignSession(supabaseClient, organizationId, userId), |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +const getBuildingSchema = ( |
| 110 | + supabaseClient: SupabaseClientType, |
| 111 | + designSessionId: string, |
| 112 | +): ResultAsync<{ id: string }, Error> => { |
| 113 | + return toResultAsync( |
| 114 | + supabaseClient |
| 115 | + .from('building_schemas') |
| 116 | + .select('id') |
| 117 | + .eq('design_session_id', designSessionId) |
| 118 | + .limit(1) |
| 119 | + .single(), |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +const createBuildingSchema = ( |
| 124 | + supabaseClient: SupabaseClientType, |
| 125 | + designSessionId: string, |
| 126 | + organizationId: string, |
| 127 | +): ResultAsync<{ id: string }, Error> => { |
| 128 | + const initialSchema = { tables: {}, enums: {}, extensions: {} } |
| 129 | + return toResultAsync( |
| 130 | + supabaseClient |
| 131 | + .from('building_schemas') |
| 132 | + .insert({ |
| 133 | + design_session_id: designSessionId, |
| 134 | + organization_id: organizationId, |
| 135 | + schema: structuredClone(initialSchema), |
| 136 | + initial_schema_snapshot: structuredClone(initialSchema), |
| 137 | + schema_file_path: null, |
| 138 | + git_sha: null, |
| 139 | + }) |
| 140 | + .select('id') |
| 141 | + .single(), |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Get or create a building schema for the design session |
| 147 | + */ |
| 148 | +const getOrCreateBuildingSchema = ( |
| 149 | + supabaseClient: SupabaseClientType, |
| 150 | + designSessionId: string, |
| 151 | + organizationId: string, |
| 152 | +): ResultAsync<{ id: string }, Error> => { |
| 153 | + return getBuildingSchema(supabaseClient, designSessionId).orElse(() => |
| 154 | + createBuildingSchema(supabaseClient, designSessionId, organizationId), |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Setup Supabase repositories for schema benchmark |
| 160 | + * Uses singleton pattern to reuse connections across benchmark runs |
| 161 | + */ |
| 162 | +export const setupRepositories = async (): Promise< |
| 163 | + Result<SetupResult, Error> |
| 164 | +> => { |
| 165 | + // Return cached setup if available |
| 166 | + if (cachedSetup) { |
| 167 | + return ok(cachedSetup) |
| 168 | + } |
| 169 | + |
| 170 | + // Create database connection |
| 171 | + const connectionResult = createDatabaseConnection() |
| 172 | + if (connectionResult.isErr()) { |
| 173 | + return err(connectionResult.error) |
| 174 | + } |
| 175 | + const supabaseClient = connectionResult.value |
| 176 | + |
| 177 | + return await getFirstOrganization(supabaseClient) |
| 178 | + .andThen((organization) => |
| 179 | + getUser(supabaseClient).andThen((user) => |
| 180 | + getOrCreateDesignSession( |
| 181 | + supabaseClient, |
| 182 | + organization.id, |
| 183 | + user.id, |
| 184 | + ).andThen((designSession) => |
| 185 | + getOrCreateBuildingSchema( |
| 186 | + supabaseClient, |
| 187 | + designSession.id, |
| 188 | + organization.id, |
| 189 | + ).map((buildingSchema) => ({ |
| 190 | + organization, |
| 191 | + user, |
| 192 | + designSession, |
| 193 | + buildingSchema, |
| 194 | + })), |
| 195 | + ), |
| 196 | + ), |
| 197 | + ) |
| 198 | + .map(({ organization, user, designSession, buildingSchema }) => { |
| 199 | + const repositories = createSupabaseRepositories( |
| 200 | + supabaseClient, |
| 201 | + organization.id, |
| 202 | + ) |
| 203 | + |
| 204 | + cachedSetup = { |
| 205 | + repositories, |
| 206 | + organizationId: organization.id, |
| 207 | + buildingSchemaId: buildingSchema.id, |
| 208 | + designSessionId: designSession.id, |
| 209 | + userId: user.id, |
| 210 | + } |
| 211 | + |
| 212 | + return cachedSetup |
| 213 | + }) |
| 214 | +} |
0 commit comments