File size: 2,669 Bytes
5871090
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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}`;
}