MCPcopy Create free account
hub / github.com/antvis/AVA / PythonEngine

Class PythonEngine

src/interpreter/python/engine.ts:33–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected