|
1 | | -import { auth, type User, websitesApi } from "@databuddy/auth"; |
| 1 | +import { auth, websitesApi } from "@databuddy/auth"; |
2 | 2 | import type { StreamingUpdate } from "@databuddy/shared/types/assistant"; |
| 3 | +import { record, setAttributes } from "@elysiajs/opentelemetry"; |
3 | 4 | import { Elysia } from "elysia"; |
4 | 5 | import { processAssistantRequest } from "../agent/processor"; |
5 | 6 | import { createStreamingResponse } from "../agent/utils/stream-utils"; |
6 | 7 | import { validateWebsite } from "../lib/website-utils"; |
7 | | -import { AssistantRequestSchema, type AssistantRequestType } from "../schemas"; |
| 8 | +import { AssistantRequestSchema } from "../schemas"; |
8 | 9 |
|
9 | 10 | function createErrorResponse(message: string): StreamingUpdate[] { |
10 | 11 | return [{ type: "error", content: message }]; |
@@ -36,59 +37,78 @@ export const assistant = new Elysia({ prefix: "/v1/assistant" }) |
36 | 37 | }) |
37 | 38 | .post( |
38 | 39 | "/stream", |
39 | | - async ({ |
40 | | - body, |
41 | | - user, |
42 | | - request, |
43 | | - }: { |
44 | | - body: AssistantRequestType; |
45 | | - user: User; |
46 | | - request: Request; |
47 | | - }) => { |
48 | | - try { |
49 | | - const websiteValidation = await validateWebsite(body.websiteId); |
50 | | - if (!websiteValidation.success) { |
51 | | - return createStreamingResponse( |
52 | | - createErrorResponse(websiteValidation.error || "Website not found") |
53 | | - ); |
54 | | - } |
| 40 | + function assistantStream({ body, user, request }) { |
| 41 | + return record("assistantStream", async () => { |
| 42 | + setAttributes({ |
| 43 | + "assistant.website_id": body.websiteId, |
| 44 | + "assistant.user_id": user?.id || "unknown", |
| 45 | + "assistant.message_count": body.messages.length, |
| 46 | + }); |
55 | 47 |
|
56 | | - const { website } = websiteValidation; |
57 | | - if (!website) { |
58 | | - return createStreamingResponse( |
59 | | - createErrorResponse("Website not found") |
60 | | - ); |
61 | | - } |
| 48 | + try { |
| 49 | + const websiteValidation = await validateWebsite(body.websiteId); |
| 50 | + if (!websiteValidation.success) { |
| 51 | + setAttributes({ "assistant.website_validation_failed": true }); |
| 52 | + return createStreamingResponse( |
| 53 | + createErrorResponse( |
| 54 | + websiteValidation.error || "Website not found" |
| 55 | + ) |
| 56 | + ); |
| 57 | + } |
62 | 58 |
|
63 | | - // Authorization: allow public websites, org members with permission, or the owner |
64 | | - let authorized = website.isPublic; |
65 | | - if (!authorized) { |
66 | | - if (website.organizationId) { |
67 | | - const { success } = await websitesApi.hasPermission({ |
68 | | - headers: request.headers, |
69 | | - body: { permissions: { website: ["read"] } }, |
70 | | - }); |
71 | | - authorized = success; |
72 | | - } else { |
73 | | - authorized = website.userId === user.id; |
| 59 | + const { website } = websiteValidation; |
| 60 | + if (!website) { |
| 61 | + setAttributes({ "assistant.website_not_found": true }); |
| 62 | + return createStreamingResponse( |
| 63 | + createErrorResponse("Website not found") |
| 64 | + ); |
74 | 65 | } |
75 | | - } |
76 | 66 |
|
77 | | - if (!authorized) { |
78 | | - return createStreamingResponse( |
79 | | - createErrorResponse( |
80 | | - "You do not have permission to access this website" |
81 | | - ) |
82 | | - ); |
83 | | - } |
| 67 | + // Authorization: allow public websites, org members with permission, or the owner |
| 68 | + let authorized = website.isPublic; |
| 69 | + if (!authorized) { |
| 70 | + if (website.organizationId) { |
| 71 | + const { success } = await websitesApi.hasPermission({ |
| 72 | + headers: request.headers, |
| 73 | + body: { permissions: { website: ["read"] } }, |
| 74 | + }); |
| 75 | + authorized = success; |
| 76 | + } else { |
| 77 | + authorized = website.userId === user?.id; |
| 78 | + } |
| 79 | + } |
84 | 80 |
|
85 | | - const updates = await processAssistantRequest(body, user, website); |
86 | | - return createStreamingResponse(updates); |
87 | | - } catch (error) { |
88 | | - const errorMessage = |
89 | | - error instanceof Error ? error.message : "Unknown error occurred"; |
90 | | - return createStreamingResponse(createErrorResponse(errorMessage)); |
91 | | - } |
| 81 | + if (!authorized) { |
| 82 | + setAttributes({ "assistant.unauthorized": true }); |
| 83 | + return createStreamingResponse( |
| 84 | + createErrorResponse( |
| 85 | + "You do not have permission to access this website" |
| 86 | + ) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + setAttributes({ |
| 91 | + "assistant.website_public": website.isPublic, |
| 92 | + "assistant.website_org": Boolean(website.organizationId), |
| 93 | + }); |
| 94 | + |
| 95 | + if (!user) { |
| 96 | + setAttributes({ "assistant.no_user": true }); |
| 97 | + return createStreamingResponse( |
| 98 | + createErrorResponse("User not found") |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + const updates = await processAssistantRequest(body, user as never, website); |
| 103 | + setAttributes({ "assistant.success": true }); |
| 104 | + return createStreamingResponse(updates); |
| 105 | + } catch (error) { |
| 106 | + setAttributes({ "assistant.error": true }); |
| 107 | + const errorMessage = |
| 108 | + error instanceof Error ? error.message : "Unknown error occurred"; |
| 109 | + return createStreamingResponse(createErrorResponse(errorMessage)); |
| 110 | + } |
| 111 | + }); |
92 | 112 | }, |
93 | 113 | { |
94 | 114 | body: AssistantRequestSchema, |
|
0 commit comments