File size: 3,965 Bytes
64648ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { and, eq, sql } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import { agentProjectSessions, agents } from "@paperclipai/db";

export function sessionResolverService(db: Db) {
  return {
    async resolveSession(agentId: string, adapterType: string, projectId: string | null) {
      const conditions = [
        eq(agentProjectSessions.agentId, agentId),
        eq(agentProjectSessions.adapterType, adapterType),
      ];
      if (projectId) {
        conditions.push(eq(agentProjectSessions.projectId, projectId));
      }

      const [existing] = await db
        .select()
        .from(agentProjectSessions)
        .where(and(...conditions))
        .limit(1);

      if (existing) {
        return existing;
      }

      // Create new session — need companyId from agent
      const [agent] = await db
        .select({ companyId: agents.companyId })
        .from(agents)
        .where(eq(agents.id, agentId))
        .limit(1);

      if (!agent) return null;

      const [session] = await db
        .insert(agentProjectSessions)
        .values({
          agentId,
          adapterType,
          projectId,
          companyId: agent.companyId,
        })
        .returning();

      return session;
    },

    async updateSession(
      agentId: string,
      adapterType: string,
      projectId: string | null,
      updates: {
        sessionParamsJson?: Record<string, unknown> | null;
        sessionDisplayId?: string | null;
        lastRunId?: string | null;
        runCount?: number;
        inputTokens?: number;
        outputTokens?: number;
      },
    ) {
      const conditions = [
        eq(agentProjectSessions.agentId, agentId),
        eq(agentProjectSessions.adapterType, adapterType),
      ];
      if (projectId) {
        conditions.push(eq(agentProjectSessions.projectId, projectId));
      }

      const [existing] = await db
        .select()
        .from(agentProjectSessions)
        .where(and(...conditions))
        .limit(1);

      if (existing) {
        const [updated] = await db
          .update(agentProjectSessions)
          .set({
            sessionParams: updates.sessionParamsJson ?? existing.sessionParams,
            sessionDisplayId: updates.sessionDisplayId ?? existing.sessionDisplayId,
            lastRunId: updates.lastRunId ?? existing.lastRunId,
            runCount: existing.runCount + (updates.runCount ?? 0),
            totalInputTokens: existing.totalInputTokens + (updates.inputTokens ?? 0),
            totalOutputTokens: existing.totalOutputTokens + (updates.outputTokens ?? 0),
            updatedAt: new Date(),
          })
          .where(eq(agentProjectSessions.id, existing.id))
          .returning();
        return updated;
      }

      // Create if not exists
      const [agent] = await db
        .select({ companyId: agents.companyId })
        .from(agents)
        .where(eq(agents.id, agentId))
        .limit(1);
      if (!agent) return null;

      const [created] = await db
        .insert(agentProjectSessions)
        .values({
          agentId,
          adapterType,
          projectId,
          companyId: agent.companyId,
          sessionParams: updates.sessionParamsJson,
          sessionDisplayId: updates.sessionDisplayId,
          lastRunId: updates.lastRunId,
          runCount: updates.runCount ?? 1,
          totalInputTokens: updates.inputTokens ?? 0,
          totalOutputTokens: updates.outputTokens ?? 0,
        })
        .returning();
      return created;
    },

    async getSession(sessionId: string) {
      const [session] = await db
        .select()
        .from(agentProjectSessions)
        .where(eq(agentProjectSessions.id, sessionId))
        .limit(1);
      return session ?? null;
    },

    async listSessions(agentId: string) {
      return db
        .select()
        .from(agentProjectSessions)
        .where(eq(agentProjectSessions.agentId, agentId));
    },
  };
}