Skip to content

Commit 405c7e8

Browse files
committed
Revert "fix: no more watches"
This reverts commit cba783a.
1 parent 9332211 commit 405c7e8

File tree

180 files changed

+910
-1172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+910
-1172
lines changed

apps/api/src/agent/handlers/chart-handler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export async function* handleChartResponse(
6060
},
6161
debugInfo: context.user.role === 'ADMIN' ? context.debugInfo : undefined,
6262
};
63-
} catch (_queryError: unknown) {
63+
} catch (queryError: unknown) {
64+
console.error('❌ SQL execution error', {
65+
error: queryError instanceof Error ? queryError.message : 'Unknown error',
66+
sql: parsedAiJson.sql,
67+
});
6468
yield {
6569
type: 'error',
6670
content: 'Database query failed. The data might not be available.',

apps/api/src/agent/handlers/metric-handler.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export async function* handleMetricResponse(
3232
parsedAiJson.metric_value
3333
);
3434
yield* sendMetricResponse(parsedAiJson, metricValue, context);
35-
} catch (_queryError: unknown) {
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+
});
3641
yield* sendMetricResponse(
3742
parsedAiJson,
3843
parsedAiJson.metric_value,
@@ -48,9 +53,7 @@ function extractMetricValue(
4853
queryData: unknown[],
4954
defaultValue: unknown
5055
): unknown {
51-
if (!(queryData.length && queryData[0])) {
52-
return defaultValue;
53-
}
56+
if (!(queryData.length && queryData[0])) return defaultValue;
5457

5558
const firstRow = queryData[0] as Record<string, unknown>;
5659
const valueKey =

apps/api/src/agent/processor.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export async function* processAssistantRequest(
3232
const startTime = Date.now();
3333

3434
try {
35+
console.info('✅ [Assistant Processor] Input validated', {
36+
message: request.message,
37+
website_id: request.website_id,
38+
website_hostname: request.website_hostname,
39+
});
40+
3541
if (context.user.role === 'ADMIN') {
3642
context.debugInfo.validatedInput = {
3743
message: request.message,
@@ -54,6 +60,11 @@ export async function* processAssistantRequest(
5460
const aiResponse = await getAICompletion({ prompt: fullPrompt });
5561
const aiTime = Date.now() - aiStart;
5662

63+
console.info('📝 [Assistant Processor] Raw AI response received', {
64+
timeTaken: `${aiTime}ms`,
65+
contentLength: aiResponse.content.length,
66+
});
67+
5768
const parsedResponse = parseAIResponse(aiResponse.content);
5869

5970
if (!parsedResponse.success) {
@@ -82,6 +93,11 @@ export async function* processAssistantRequest(
8293
};
8394
return;
8495
}
96+
console.info('✅ [Assistant Processor] AI response parsed', {
97+
responseType: aiJson.response_type,
98+
hasSQL: !!aiJson.sql,
99+
thinkingSteps: aiJson.thinking_steps?.length || 0,
100+
});
85101

86102
// Process thinking steps
87103
if (aiJson.thinking_steps?.length) {
@@ -133,6 +149,9 @@ export async function* processAssistantRequest(
133149
} catch (error: unknown) {
134150
const errorMessage =
135151
error instanceof Error ? error.message : 'Unknown error';
152+
console.error('💥 [Assistant Processor] Processing error', {
153+
error: errorMessage,
154+
});
136155

137156
yield {
138157
type: 'error',

apps/api/src/agent/prompts/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const comprehensiveUnifiedPrompt = (
4040
mode: 'analysis_only' | 'execute_chat' | 'execute_agent_step',
4141
previousMessages?: any[],
4242
agentToolResult?: any,
43-
_model?: 'chat' | 'agent' | 'agent-max'
43+
model?: 'chat' | 'agent' | 'agent-max'
4444
) => `
4545
<persona>
4646
You are Nova, a world-class, specialized AI analytics assistant for the website ${websiteHostname}. You are precise, analytical, and secure. Your sole purpose is to help users understand their website's analytics data by providing insights, generating SQL queries, and creating visualizations.

apps/api/src/agent/utils/ai-client.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,33 @@ export async function getAICompletion(
2727
request: AICompletionRequest
2828
): Promise<AICompletionResponse> {
2929
const startTime = Date.now();
30-
const completion = await openai.chat.completions.create({
31-
model: AI_MODEL,
32-
messages: [{ role: 'system', content: request.prompt }],
33-
temperature: request.temperature ?? 0.1,
34-
response_format: { type: 'json_object' },
35-
});
36-
37-
const content = completion.choices[0]?.message?.content || '';
38-
const _aiTime = Date.now() - startTime;
39-
40-
return {
41-
content,
42-
usage: completion.usage,
43-
};
30+
31+
try {
32+
const completion = await openai.chat.completions.create({
33+
model: AI_MODEL,
34+
messages: [{ role: 'system', content: request.prompt }],
35+
temperature: request.temperature ?? 0.1,
36+
response_format: { type: 'json_object' },
37+
});
38+
39+
const content = completion.choices[0]?.message?.content || '';
40+
const aiTime = Date.now() - startTime;
41+
42+
console.info('🤖 [AI Client] Completion completed', {
43+
timeTaken: `${aiTime}ms`,
44+
contentLength: content.length,
45+
usage: completion.usage,
46+
});
47+
48+
return {
49+
content,
50+
usage: completion.usage,
51+
};
52+
} catch (error) {
53+
console.error('❌ [AI Client] Completion failed', {
54+
error: error instanceof Error ? error.message : 'Unknown error',
55+
timeTaken: Date.now() - startTime,
56+
});
57+
throw error;
58+
}
4459
}

apps/api/src/agent/utils/query-executor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export async function executeQuery(sql: string): Promise<QueryResult> {
1111
const result = await chQuery(sql);
1212
const queryTime = Date.now() - queryStart;
1313

14+
console.info('🔍 [Query Executor] Query completed', {
15+
timeTaken: `${queryTime}ms`,
16+
resultCount: result.length,
17+
sql: sql.substring(0, 100) + (sql.length > 100 ? '...' : ''),
18+
});
19+
1420
return {
1521
data: result,
1622
executionTime: queryTime,

apps/api/src/agent/utils/response-parser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@ export function parseAIResponse(rawResponse: string): ParsedAIResponse {
1717

1818
const parsedData = AIResponseJsonSchema.parse(JSON.parse(cleanedResponse));
1919

20+
console.info('✅ [Response Parser] AI response parsed successfully', {
21+
responseType: parsedData.response_type,
22+
hasSQL: !!parsedData.sql,
23+
thinkingSteps: parsedData.thinking_steps?.length || 0,
24+
});
25+
2026
return {
2127
success: true,
2228
data: parsedData,
2329
};
2430
} catch (parseError) {
31+
console.error('❌ [Response Parser] AI response parsing failed', {
32+
error: parseError instanceof Error ? parseError.message : 'Unknown error',
33+
rawResponseLength: rawResponse.length,
34+
});
35+
2536
return {
2637
success: false,
2738
error:

apps/api/src/agent/utils/sql-validator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export function validateSQL(sql: string): boolean {
2222

2323
// Check for dangerous keyword patterns
2424
for (const keyword of FORBIDDEN_SQL_KEYWORDS) {
25-
if (upperSQL.includes(keyword)) {
26-
return false;
27-
}
25+
if (upperSQL.includes(keyword)) return false;
2826
}
2927

3028
// Must start with SELECT or WITH (for CTEs)

apps/api/src/middleware/logging.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

apps/api/src/middleware/rate-limit.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export function createRateLimitMiddleware(options: RateLimitOptions) {
2222
headers: request.headers,
2323
});
2424
userId = session?.user?.id;
25-
} catch (_error) {}
25+
} catch (error) {
26+
console.error('[Rate Limit] Auth error:', error);
27+
}
2628
}
2729

2830
const identifier = getRateLimitIdentifier(userId, request.headers);

0 commit comments

Comments
 (0)