Skip to content

Commit 88c6029

Browse files
committed
fix: cors
1 parent 1d7dbc5 commit 88c6029

Some content is hidden

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

61 files changed

+2055
-1985
lines changed

apps/api/src/agent/core/ai-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { openrouter } from "@databuddy/shared/utils/openrouter";
12
import { generateObject } from "ai";
23
import type { z } from "zod";
34
import {
45
AIResponseJsonSchema,
56
comprehensiveSystemPrompt,
67
} from "../prompts/agent";
78
import type { AssistantSession } from "./assistant-session";
8-
import { openrouter } from "@databuddy/shared/utils/openrouter";
99

1010
const AI_MODEL = "google/gemini-2.5-flash-lite-preview-06-17";
1111

apps/api/src/agent_v2/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { openrouter } from "@databuddy/shared/utils/openrouter";
12
import { type ModelMessage, smoothStream, stepCountIs, streamText } from "ai";
23
import { chatPrompt } from "./prompts/chat-prompt";
34
import { tools } from "./tools";
4-
import { openrouter } from "@databuddy/shared/utils/openrouter";
55

66
export const modes = ["chat", "agent", "agent_max"] as const;
77
export type Mode = (typeof modes)[number];

apps/api/src/lib/api-key.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ const getCachedAccessEntries = cacheable(
3838
.from(apikeyAccess)
3939
.where(eq(apikeyAccess.apikeyId, keyId));
4040
} catch (error) {
41-
logger.error({ error, keyId }, "Failed to get API key access entries from cache");
41+
logger.error(
42+
{ error, keyId },
43+
"Failed to get API key access entries from cache"
44+
);
4245
return [] as InferSelectModel<typeof apikeyAccess>[];
4346
}
4447
},

apps/api/src/lib/export/data-fetcher.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ export async function fetchExportData(
4848
// Execute queries in parallel with secure parameters
4949
const [events, errors, webVitals] = await Promise.all([
5050
chQuery<SanitizedEvent>(eventsQuery, queryParams).catch((error) => {
51-
logger.error(
52-
{ error, websiteId },
53-
"Failed to fetch events for export"
54-
);
51+
logger.error({ error, websiteId }, "Failed to fetch events for export");
5552
throw new Error(
5653
`Failed to fetch events: ${error instanceof Error ? error.message : String(error)}`
5754
);
5855
}),
5956
chQuery<SanitizedError>(errorsQuery, queryParams).catch((error) => {
60-
logger.error(
61-
{ error, websiteId },
62-
"Failed to fetch errors for export"
63-
);
57+
logger.error({ error, websiteId }, "Failed to fetch errors for export");
6458
throw new Error(
6559
`Failed to fetch errors: ${error instanceof Error ? error.message : String(error)}`
6660
);

apps/api/src/lib/export/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type ExportResult = {
1919
totalRecords: number;
2020
fileSize: number;
2121
};
22-
}
22+
};
2323

2424
export async function processExport(
2525
request: ExportRequest

apps/api/src/lib/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { logger as sharedLogger } from "@databuddy/shared/logger";
22

33
export const logger = sharedLogger.child({
44
service: "api",
5-
});
5+
});

apps/api/src/lib/tracing.ts

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { resourceFromAttributes } from "@opentelemetry/resources";
77
import { NodeSDK } from "@opentelemetry/sdk-node";
88
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-node";
99
import {
10-
ATTR_SERVICE_NAME,
11-
ATTR_SERVICE_VERSION,
10+
ATTR_SERVICE_NAME,
11+
ATTR_SERVICE_VERSION,
1212
} from "@opentelemetry/semantic-conventions";
1313
import pkg from "../../package.json";
1414

@@ -18,135 +18,135 @@ let sdk: NodeSDK | null = null;
1818
* Initialize OpenTelemetry
1919
*/
2020
export function initTracing(): void {
21-
if (sdk) {
22-
return;
23-
}
21+
if (sdk) {
22+
return;
23+
}
2424

25-
const exporter = new OTLPTraceExporter({
26-
url: "https://api.axiom.co/v1/traces",
27-
headers: {
28-
Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
29-
"X-Axiom-Dataset": process.env.AXIOM_DATASET ?? "api",
30-
},
31-
});
25+
const exporter = new OTLPTraceExporter({
26+
url: "https://api.axiom.co/v1/traces",
27+
headers: {
28+
Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
29+
"X-Axiom-Dataset": process.env.AXIOM_DATASET ?? "api",
30+
},
31+
});
3232

33-
sdk = new NodeSDK({
34-
resource: resourceFromAttributes({
35-
[ATTR_SERVICE_NAME]: "api",
36-
[ATTR_SERVICE_VERSION]: pkg.version,
37-
}),
38-
spanProcessor: new BatchSpanProcessor(exporter, {
39-
scheduledDelayMillis: 1000,
40-
exportTimeoutMillis: 30_000,
41-
maxExportBatchSize: 512,
42-
maxQueueSize: 2048,
43-
}),
44-
instrumentations: [
45-
new HttpInstrumentation({
46-
ignoreIncomingRequestHook: (req: { url?: string }) => {
47-
// Don't trace health checks
48-
return req.url?.includes("/health") ?? false;
49-
},
50-
}),
51-
new PgInstrumentation(),
52-
createORPCInstrumentation(),
53-
],
54-
});
33+
sdk = new NodeSDK({
34+
resource: resourceFromAttributes({
35+
[ATTR_SERVICE_NAME]: "api",
36+
[ATTR_SERVICE_VERSION]: pkg.version,
37+
}),
38+
spanProcessor: new BatchSpanProcessor(exporter, {
39+
scheduledDelayMillis: 1000,
40+
exportTimeoutMillis: 30_000,
41+
maxExportBatchSize: 512,
42+
maxQueueSize: 2048,
43+
}),
44+
instrumentations: [
45+
new HttpInstrumentation({
46+
ignoreIncomingRequestHook: (req: { url?: string }) => {
47+
// Don't trace health checks
48+
return req.url?.includes("/health") ?? false;
49+
},
50+
}),
51+
new PgInstrumentation(),
52+
createORPCInstrumentation(),
53+
],
54+
});
5555

56-
sdk.start();
56+
sdk.start();
5757
}
5858

5959
export async function shutdownTracing(): Promise<void> {
60-
if (sdk) {
61-
await sdk.shutdown();
62-
sdk = null;
63-
}
60+
if (sdk) {
61+
await sdk.shutdown();
62+
sdk = null;
63+
}
6464
}
6565

6666
/**
6767
* Get tracer
6868
*/
6969
function getTracer() {
70-
return trace.getTracer("api");
70+
return trace.getTracer("api");
7171
}
7272

7373
/**
7474
* Create a span - replaces @elysiajs/opentelemetry record
7575
*/
7676
export function record<T>(name: string, fn: () => Promise<T> | T): Promise<T> {
77-
const tracer = getTracer();
78-
return tracer.startActiveSpan(name, async (span: Span) => {
79-
try {
80-
const result = await fn();
81-
span.setStatus({ code: SpanStatusCode.OK });
82-
return result;
83-
} catch (error) {
84-
span.setStatus({
85-
code: SpanStatusCode.ERROR,
86-
message: error instanceof Error ? error.message : String(error),
87-
});
88-
span.recordException(
89-
error instanceof Error ? error : new Error(String(error))
90-
);
91-
throw error;
92-
} finally {
93-
span.end();
94-
}
95-
});
77+
const tracer = getTracer();
78+
return tracer.startActiveSpan(name, async (span: Span) => {
79+
try {
80+
const result = await fn();
81+
span.setStatus({ code: SpanStatusCode.OK });
82+
return result;
83+
} catch (error) {
84+
span.setStatus({
85+
code: SpanStatusCode.ERROR,
86+
message: error instanceof Error ? error.message : String(error),
87+
});
88+
span.recordException(
89+
error instanceof Error ? error : new Error(String(error))
90+
);
91+
throw error;
92+
} finally {
93+
span.end();
94+
}
95+
});
9696
}
9797

9898
/**
9999
* Set attributes on active span - replaces @elysiajs/opentelemetry setAttributes
100100
*/
101101
export function setAttributes(
102-
attributes: Record<string, string | number | boolean>
102+
attributes: Record<string, string | number | boolean>
103103
): void {
104-
const span = trace.getActiveSpan();
105-
if (span) {
106-
for (const [key, value] of Object.entries(attributes)) {
107-
span.setAttribute(key, value);
108-
}
109-
}
104+
const span = trace.getActiveSpan();
105+
if (span) {
106+
for (const [key, value] of Object.entries(attributes)) {
107+
span.setAttribute(key, value);
108+
}
109+
}
110110
}
111111

112112
/**
113113
* Start HTTP request span and set it as active
114114
* Returns both the span and the context with the span set as active
115115
*/
116116
export function startRequestSpan(
117-
method: string,
118-
path: string,
119-
route?: string
117+
method: string,
118+
path: string,
119+
route?: string
120120
): { span: Span; activeContext: ReturnType<typeof context.active> } {
121-
const tracer = getTracer();
122-
const span = tracer.startSpan(`${method} ${route ?? path}`, {
123-
kind: 1, // SERVER
124-
attributes: {
125-
"http.method": method,
126-
"http.route": route ?? path,
127-
"http.target": path,
128-
},
129-
});
121+
const tracer = getTracer();
122+
const span = tracer.startSpan(`${method} ${route ?? path}`, {
123+
kind: 1, // SERVER
124+
attributes: {
125+
"http.method": method,
126+
"http.route": route ?? path,
127+
"http.target": path,
128+
},
129+
});
130130

131-
// Create context with this span as active
132-
const activeContext = trace.setSpan(context.active(), span);
131+
// Create context with this span as active
132+
const activeContext = trace.setSpan(context.active(), span);
133133

134-
return { span, activeContext };
134+
return { span, activeContext };
135135
}
136136

137137
/**
138138
* End HTTP request span
139139
*/
140140
export function endRequestSpan(
141-
span: Span,
142-
statusCode: number,
143-
startTime: number
141+
span: Span,
142+
statusCode: number,
143+
startTime: number
144144
): void {
145-
span.setAttribute("http.status_code", statusCode);
146-
span.setAttribute("http.response.duration_ms", Date.now() - startTime);
147-
span.setStatus({
148-
code: statusCode >= 400 ? SpanStatusCode.ERROR : SpanStatusCode.OK,
149-
message: statusCode >= 400 ? `HTTP ${statusCode}` : undefined,
150-
});
151-
span.end();
145+
span.setAttribute("http.status_code", statusCode);
146+
span.setAttribute("http.response.duration_ms", Date.now() - startTime);
147+
span.setStatus({
148+
code: statusCode >= 400 ? SpanStatusCode.ERROR : SpanStatusCode.OK,
149+
message: statusCode >= 400 ? `HTTP ${statusCode}` : undefined,
150+
});
151+
span.end();
152152
}

apps/api/src/query/builders/engagement.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ export const EngagementBuilders: Record<string, SimpleQueryConfig> = {
8080
table: Analytics.events,
8181
fields: [
8282
"CASE " +
83-
'WHEN scroll_depth < 0.25 THEN "0-25%" ' +
84-
'WHEN scroll_depth < 0.5 THEN "25-50%" ' +
85-
'WHEN scroll_depth < 0.75 THEN "50-75%" ' +
86-
'WHEN scroll_depth < 1.0 THEN "75-100%" ' +
87-
'ELSE "100%" ' +
88-
"END as depth_range",
83+
'WHEN scroll_depth < 0.25 THEN "0-25%" ' +
84+
'WHEN scroll_depth < 0.5 THEN "25-50%" ' +
85+
'WHEN scroll_depth < 0.75 THEN "50-75%" ' +
86+
'WHEN scroll_depth < 1.0 THEN "75-100%" ' +
87+
'ELSE "100%" ' +
88+
"END as depth_range",
8989
"COUNT(DISTINCT anonymous_id) as visitors",
9090
"COUNT(DISTINCT session_id) as sessions",
9191
"ROUND((COUNT(DISTINCT session_id) / SUM(COUNT(DISTINCT session_id)) OVER()) * 100, 2) as percentage",

apps/api/src/routes/assistant.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ export const assistant = new Elysia({ prefix: "/v1/assistant" })
9999
);
100100
}
101101

102-
const updates = await processAssistantRequest(body, user as never, website);
102+
const updates = await processAssistantRequest(
103+
body,
104+
user as never,
105+
website
106+
);
103107
setAttributes({ "assistant.success": true });
104108
return createStreamingResponse(updates);
105109
} catch (error) {

0 commit comments

Comments
 (0)