| import type { |
| Conversation as DbConversation, |
| Message as DbMessage, |
| Folder as DbFolder, |
| User as DbUser, |
| } from "@workspace/db"; |
|
|
| export function serializeUser(u: DbUser) { |
| return { |
| id: u.id, |
| email: u.email, |
| username: u.username, |
| display_name: u.displayName, |
| avatar_url: u.avatarUrl, |
| role: u.role as "user" | "admin", |
| created_at: u.createdAt.toISOString(), |
| quota: { |
| plan: u.plan as "free" | "pro" | "team", |
| period_start: u.periodStart.toISOString(), |
| period_end: u.periodEnd.toISOString(), |
| tokens_limit: Number(u.tokensLimit), |
| tokens_used: Number(u.tokensUsed), |
| }, |
| }; |
| } |
|
|
| export function serializeConversation(c: DbConversation, shareUrl: string | null) { |
| return { |
| id: c.id, |
| title: c.title, |
| folder_id: c.folderId, |
| pinned: c.pinned, |
| archived: c.archived, |
| model_id: c.modelId, |
| message_count: c.messageCount, |
| preview: c.preview, |
| created_at: c.createdAt.toISOString(), |
| updated_at: c.updatedAt.toISOString(), |
| last_message_at: c.lastMessageAt.toISOString(), |
| system_prompt: c.systemPrompt, |
| share: |
| c.shareToken && shareUrl |
| ? { token: c.shareToken, url: shareUrl } |
| : null, |
| }; |
| } |
|
|
| export function serializeMessage(m: DbMessage) { |
| return { |
| id: m.id, |
| conversation_id: m.conversationId, |
| role: m.role as "user" | "assistant" | "system" | "tool", |
| status: m.status as "complete" | "streaming" | "stopped" | "error", |
| content: m.content, |
| parent_id: m.parentId, |
| model_id: m.modelId, |
| stop_reason: m.stopReason, |
| usage: |
| m.inputTokens != null && m.outputTokens != null |
| ? { input_tokens: m.inputTokens, output_tokens: m.outputTokens } |
| : null, |
| feedback: m.feedbackRating |
| ? { |
| rating: m.feedbackRating as "up" | "down", |
| reason: m.feedbackReason, |
| } |
| : null, |
| edited: m.edited, |
| created_at: m.createdAt.toISOString(), |
| }; |
| } |
|
|
| export function serializeFolder(f: DbFolder, conversationCount: number) { |
| return { |
| id: f.id, |
| name: f.name, |
| description: f.description, |
| color: f.color, |
| icon: f.icon, |
| system_prompt: f.systemPrompt, |
| conversation_count: conversationCount, |
| created_at: f.createdAt.toISOString(), |
| updated_at: f.updatedAt.toISOString(), |
| }; |
| } |
|
|
| export function shareUrlFor(token: string, req: { protocol: string; get(name: string): string | undefined }): string { |
| const explicit = process.env["DOATLAS_PUBLIC_BASE_URL"]; |
| if (explicit) return `${explicit.replace(/\/$/, "")}/share/${token}`; |
| const host = req.get("host") || "localhost"; |
| return `${req.protocol}://${host}/share/${token}`; |
| } |
|
|