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