| 31 | } from '../../types'; |
| 32 | |
| 33 | export class JavaScriptEngine implements AnalysisEngine { |
| 34 | readonly dialect: JavaScriptDialect; |
| 35 | |
| 36 | private data: any[] | null = null; |
| 37 | |
| 38 | constructor(private readonly llmConfig: LLMConfig) { |
| 39 | this.dialect = new JavaScriptDialect(llmConfig); |
| 40 | } |
| 41 | |
| 42 | async load(configs: DataSourceConfig | DataSourceConfig[]): Promise<void> { |
| 43 | const sources = Array.isArray(configs) ? configs : [configs]; |
| 44 | if (sources.length !== 1) { |
| 45 | throw new Error(`JavaScriptEngine currently supports exactly one data source, received ${sources.length}`); |
| 46 | } |
| 47 | this.data = null; |
| 48 | const config = sourceInputSchema.parse(sources[0]); |
| 49 | if (config.type !== 'csv' && config.type !== 'json' && config.type !== 'text') { |
| 50 | throw new Error(`JavaScriptEngine only supports csv/json/text sources, got: ${config.type}`); |
| 51 | } |
| 52 | |
| 53 | this.data = await loadSource(config, this.llmConfig); |
| 54 | } |
| 55 | |
| 56 | async schema(): Promise<Schema> { |
| 57 | if (!this.data) throw new Error('No data loaded. Please call source() first.'); |
| 58 | return extractDataSchema(this.data); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Return the loaded in-memory rows (null when nothing is loaded). |
| 63 | * Only the JavaScript interpreter engine keeps data as a plain single-table array, |
| 64 | * so it is the only engine that can expose it directly. |
| 65 | */ |
| 66 | getData(): any[] | null { |
| 67 | return this.data; |
| 68 | } |
| 69 | |
| 70 | async profile(schema: Schema, options: ProfileOptions = {}): Promise<Profile> { |
| 71 | if (!this.data) throw new Error('No data loaded. Please call source() first.'); |
| 72 | return profileTables(this.data, schema, parseProfileOptions(options, { metrics: DEFAULT_METRICS })); |
| 73 | } |
| 74 | |
| 75 | async getDSL(query: string, dataModel: DataModel, signal?: AbortSignal): Promise<TranslationResult> { |
| 76 | if (!this.data) { |
| 77 | throw new Error('No data loaded. Please call source() first.'); |
| 78 | } |
| 79 | |
| 80 | return signal ? this.dialect.getDSL(query, dataModel, signal) : this.dialect.getDSL(query, dataModel); |
| 81 | } |
| 82 | |
| 83 | async execute<T = Record<string, unknown>>( |
| 84 | code: string, |
| 85 | options?: ExecutionOptions, |
| 86 | signal?: AbortSignal |
| 87 | ): Promise<ExecutionResult<T>> { |
| 88 | if (!this.data) { |
| 89 | throw new Error('No data loaded. Please call source() first.'); |
| 90 | } |
nothing calls this directly
no outgoing calls
no test coverage detected