|
1 | | -import type { z } from "zod"; |
2 | | -import type { AIResponseJsonSchema } from "../prompts/agent"; |
3 | | -import { validateSQL } from "../utils/sql-validator"; |
4 | | -import { executeQuery } from "../utils/query-executor"; |
5 | | -import type { StreamingUpdate } from "../utils/stream-utils"; |
| 1 | +import type { z } from 'zod'; |
| 2 | +import type { AIResponseJsonSchema } from '../prompts/agent'; |
| 3 | +import { executeQuery } from '../utils/query-executor'; |
| 4 | +import { validateSQL } from '../utils/sql-validator'; |
| 5 | +import type { StreamingUpdate } from '../utils/stream-utils'; |
6 | 6 |
|
7 | 7 | export interface MetricHandlerContext { |
8 | | - user: any; |
9 | | - website: any; |
10 | | - debugInfo: Record<string, unknown>; |
| 8 | + user: any; |
| 9 | + website: any; |
| 10 | + debugInfo: Record<string, unknown>; |
11 | 11 | } |
12 | 12 |
|
13 | 13 | export async function* handleMetricResponse( |
14 | | - parsedAiJson: z.infer<typeof AIResponseJsonSchema>, |
15 | | - context: MetricHandlerContext, |
| 14 | + parsedAiJson: z.infer<typeof AIResponseJsonSchema>, |
| 15 | + context: MetricHandlerContext |
16 | 16 | ): AsyncGenerator<StreamingUpdate> { |
17 | | - if (parsedAiJson.sql) { |
18 | | - if (!validateSQL(parsedAiJson.sql)) { |
19 | | - yield { |
20 | | - type: "error", |
21 | | - content: "Generated query failed security validation.", |
22 | | - debugInfo: |
23 | | - context.user.role === "ADMIN" ? context.debugInfo : undefined, |
24 | | - }; |
25 | | - return; |
26 | | - } |
| 17 | + if (parsedAiJson.sql) { |
| 18 | + if (!validateSQL(parsedAiJson.sql)) { |
| 19 | + yield { |
| 20 | + type: 'error', |
| 21 | + content: 'Generated query failed security validation.', |
| 22 | + debugInfo: |
| 23 | + context.user.role === 'ADMIN' ? context.debugInfo : undefined, |
| 24 | + }; |
| 25 | + return; |
| 26 | + } |
27 | 27 |
|
28 | | - try { |
29 | | - const queryResult = await executeQuery(parsedAiJson.sql); |
30 | | - const metricValue = extractMetricValue( |
31 | | - queryResult.data, |
32 | | - parsedAiJson.metric_value, |
33 | | - ); |
34 | | - yield* sendMetricResponse(parsedAiJson, metricValue, context); |
35 | | - } catch (queryError: unknown) { |
36 | | - console.error("❌ Metric SQL execution error", { |
37 | | - error: |
38 | | - queryError instanceof Error ? queryError.message : "Unknown error", |
39 | | - sql: parsedAiJson.sql, |
40 | | - }); |
41 | | - yield* sendMetricResponse( |
42 | | - parsedAiJson, |
43 | | - parsedAiJson.metric_value, |
44 | | - context, |
45 | | - ); |
46 | | - } |
47 | | - } else { |
48 | | - yield* sendMetricResponse(parsedAiJson, parsedAiJson.metric_value, context); |
49 | | - } |
| 28 | + try { |
| 29 | + const queryResult = await executeQuery(parsedAiJson.sql); |
| 30 | + const metricValue = extractMetricValue( |
| 31 | + queryResult.data, |
| 32 | + parsedAiJson.metric_value |
| 33 | + ); |
| 34 | + yield* sendMetricResponse(parsedAiJson, metricValue, context); |
| 35 | + } catch (queryError: unknown) { |
| 36 | + console.error('❌ Metric SQL execution error', { |
| 37 | + error: |
| 38 | + queryError instanceof Error ? queryError.message : 'Unknown error', |
| 39 | + sql: parsedAiJson.sql, |
| 40 | + }); |
| 41 | + yield* sendMetricResponse( |
| 42 | + parsedAiJson, |
| 43 | + parsedAiJson.metric_value, |
| 44 | + context |
| 45 | + ); |
| 46 | + } |
| 47 | + } else { |
| 48 | + yield* sendMetricResponse(parsedAiJson, parsedAiJson.metric_value, context); |
| 49 | + } |
50 | 50 | } |
51 | 51 |
|
52 | 52 | function extractMetricValue( |
53 | | - queryData: unknown[], |
54 | | - defaultValue: unknown, |
| 53 | + queryData: unknown[], |
| 54 | + defaultValue: unknown |
55 | 55 | ): unknown { |
56 | | - if (!queryData.length || !queryData[0]) return defaultValue; |
| 56 | + if (!(queryData.length && queryData[0])) return defaultValue; |
57 | 57 |
|
58 | | - const firstRow = queryData[0] as Record<string, unknown>; |
59 | | - const valueKey = |
60 | | - Object.keys(firstRow).find((key) => typeof firstRow[key] === "number") || |
61 | | - Object.keys(firstRow)[0]; |
| 58 | + const firstRow = queryData[0] as Record<string, unknown>; |
| 59 | + const valueKey = |
| 60 | + Object.keys(firstRow).find((key) => typeof firstRow[key] === 'number') || |
| 61 | + Object.keys(firstRow)[0]; |
62 | 62 |
|
63 | | - return valueKey ? firstRow[valueKey] : defaultValue; |
| 63 | + return valueKey ? firstRow[valueKey] : defaultValue; |
64 | 64 | } |
65 | 65 |
|
66 | 66 | async function* sendMetricResponse( |
67 | | - parsedAiJson: z.infer<typeof AIResponseJsonSchema>, |
68 | | - metricValue: unknown, |
69 | | - context: MetricHandlerContext, |
| 67 | + parsedAiJson: z.infer<typeof AIResponseJsonSchema>, |
| 68 | + metricValue: unknown, |
| 69 | + context: MetricHandlerContext |
70 | 70 | ): AsyncGenerator<StreamingUpdate> { |
71 | | - const formattedValue = |
72 | | - typeof metricValue === "number" |
73 | | - ? metricValue.toLocaleString() |
74 | | - : metricValue; |
| 71 | + const formattedValue = |
| 72 | + typeof metricValue === 'number' |
| 73 | + ? metricValue.toLocaleString() |
| 74 | + : metricValue; |
75 | 75 |
|
76 | | - yield { |
77 | | - type: "complete", |
78 | | - content: |
79 | | - parsedAiJson.text_response || |
80 | | - `${parsedAiJson.metric_label || "Result"}: ${formattedValue}`, |
81 | | - data: { |
82 | | - hasVisualization: false, |
83 | | - responseType: "metric", |
84 | | - metricValue: metricValue, |
85 | | - metricLabel: parsedAiJson.metric_label, |
86 | | - }, |
87 | | - debugInfo: context.user.role === "ADMIN" ? context.debugInfo : undefined, |
88 | | - }; |
| 76 | + yield { |
| 77 | + type: 'complete', |
| 78 | + content: |
| 79 | + parsedAiJson.text_response || |
| 80 | + `${parsedAiJson.metric_label || 'Result'}: ${formattedValue}`, |
| 81 | + data: { |
| 82 | + hasVisualization: false, |
| 83 | + responseType: 'metric', |
| 84 | + metricValue, |
| 85 | + metricLabel: parsedAiJson.metric_label, |
| 86 | + }, |
| 87 | + debugInfo: context.user.role === 'ADMIN' ? context.debugInfo : undefined, |
| 88 | + }; |
89 | 89 | } |
0 commit comments