(query: string, dataModel: DataModel)
| 63 | constructor(private readonly llmConfig: LLMConfig) {} |
| 64 | |
| 65 | async getDSL(query: string, dataModel: DataModel): Promise<TranslationResult> { |
| 66 | const { text, usage, finishReason } = await generateText({ |
| 67 | model: languageModel(this.llmConfig), |
| 68 | maxRetries: this.llmConfig.maxRetries ?? 3, |
| 69 | prompt: `You are a data analysis expert. Generate JavaScript code to answer the question. |
| 70 | |
| 71 | Dataset Context: |
| 72 | ${stringifyDataModel(dataModel)} |
| 73 | |
| 74 | User Query: ${query} |
| 75 | |
| 76 | ${this.rules} |
| 77 | |
| 78 | Return only executable JavaScript code, without explanation or Markdown.`, |
| 79 | }); |
| 80 | if (finishReason !== 'stop' && finishReason !== 'other') { |
| 81 | throw new Error(`JavaScript generation did not complete (${finishReason})`); |
| 82 | } |
| 83 | const trimmed = text.trim(); |
| 84 | const dsl = (trimmed.match(/^```(?:javascript|js)?[ \t]*\r?\n([\s\S]*?)\r?\n```$/i)?.[1] ?? trimmed).trim(); |
| 85 | if (!dsl) throw new Error('JavaScript generation returned empty code'); |
| 86 | return { dsl, usage }; |
| 87 | } |
| 88 | |
| 89 | async validateDSL(code: string): Promise<string> { |
| 90 | if (!code.trim()) throw new Error('JavaScript query must contain code'); |
nothing calls this directly
no test coverage detected