| 30 | const DEFAULT_QUERY_TIMEOUT_MS = 30_000; |
| 31 | |
| 32 | export class PythonEngine implements AnalysisEngine { |
| 33 | readonly dialect: PythonDialect; |
| 34 | |
| 35 | private source: DataSourceConfig | null = null; |
| 36 | private readonly timeoutMs: number; |
| 37 | private readonly executor: ReturnType<typeof createPythonExecutor>; |
| 38 | |
| 39 | constructor(llmConfig: LLMConfig, engineOptions: PythonEngineOptions = {}) { |
| 40 | if (engineOptions.execute !== undefined && typeof engineOptions.execute !== 'function') { |
| 41 | throw new Error('Python execute must be a function'); |
| 42 | } |
| 43 | this.timeoutMs = z |
| 44 | .number() |
| 45 | .int() |
| 46 | .positive() |
| 47 | .max(2_147_483_647) |
| 48 | .parse(engineOptions.queryTimeoutMs ?? DEFAULT_QUERY_TIMEOUT_MS); |
| 49 | this.executor = engineOptions.execute ?? createPythonExecutor(this.timeoutMs); |
| 50 | this.dialect = new PythonDialect(llmConfig, (script) => |
| 51 | runWithTimeout(this.executor(script, 64 * 1024), this.timeoutMs, { |
| 52 | onTimeout: () => new Error(`Python validation timed out after ${this.timeoutMs}ms`), |
| 53 | }) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | async load(configs: DataSourceConfig | DataSourceConfig[]): Promise<void> { |
| 58 | const sources = Array.isArray(configs) ? configs : [configs]; |
| 59 | if (sources.length !== 1) { |
| 60 | throw new Error(`PythonEngine currently supports exactly one data source, received ${sources.length}`); |
| 61 | } |
| 62 | const config = sources[0]; |
| 63 | this.source = null; |
| 64 | this.source = sourceSchema.parse(config); |
| 65 | } |
| 66 | |
| 67 | async schema(): Promise<Schema> { |
| 68 | if (!this.source) throw new Error('No data loaded. Please call source() first.'); |
| 69 | |
| 70 | const tables = z.array(tableSchema).parse(await this.metadata(SCHEMA_CODE)); |
| 71 | if (new Set(tables.map((table) => table.name)).size !== tables.length) { |
| 72 | throw new Error('Python source returned duplicate table names'); |
| 73 | } |
| 74 | return { models: tables } as Schema; |
| 75 | } |
| 76 | |
| 77 | async getDSL(query: string, dataModel: DataModel): Promise<TranslationResult> { |
| 78 | if (!this.source) throw new Error('No data loaded. Please call source() first.'); |
| 79 | |
| 80 | return this.dialect.getDSL(query, dataModel); |
| 81 | } |
| 82 | |
| 83 | async profile(schema: Schema, options: ProfileOptions = {}): Promise<Profile> { |
| 84 | if (!this.source) throw new Error('No data loaded. Please call source() first.'); |
| 85 | |
| 86 | const tables = await this.metadata(profileCode(parseProfileOptions(options, { metrics: DEFAULT_METRICS }), schema)); |
| 87 | return profileOutputSchema.parse({ models: tables }); |
| 88 | } |
| 89 |
nothing calls this directly
no outgoing calls
no test coverage detected