~bigbes/lethe

ref: d6bca49b6562e12a3491b37bd464d667d306e696 lethe/web/src/api/adapters.ts -rw-r--r-- 5.3 KiB
d6bca49b — Eugene Blikh docs(lethe-web-ui-palette-savedsearch): plan + execute deviations a month ago
                                                                                
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
export type Tool = 'claude-code' | 'opencode' | 'crush' | 'pi' | 'kimi' | string
export type Host = 'laptop' | 'workpc' | string

export interface SessionDTO {
  owner: string
  tool: string
  host: string
  session_id: string
  started_at: number     // unix epoch seconds
  ended_at: number
  working_dir?: string
  source_file: string
  summary: string
  turn_count: number
  tokens_in_total: number
  tokens_out_total: number
  model?: string
  metadata?: unknown
}

export interface Session {
  id: string             // `${tool}/${host}/${session_id}`
  sessionId: string      // bare session_id (IV6)
  tool: string
  host: string
  cwd: string            // working_dir ?? ''
  model?: string
  started: string        // ISO 8601
  ended: string | null   // null if ended_at <= started_at
  summary: string
  turns: number
  tokensIn: number
  tokensOut: number
  hasError: boolean      // TODO: always false until error tracking is implemented
}

export function adaptSession(d: SessionDTO): Session {
  return {
    id: `${d.tool}/${d.host}/${d.session_id}`,
    sessionId: d.session_id,
    tool: d.tool,
    host: d.host,
    cwd: d.working_dir ?? '',
    model: d.model,
    started: new Date(d.started_at * 1000).toISOString(),
    ended: d.ended_at > d.started_at ? new Date(d.ended_at * 1000).toISOString() : null,
    summary: d.summary,
    turns: d.turn_count,
    tokensIn: d.tokens_in_total,
    tokensOut: d.tokens_out_total,
    hasError: false,
  }
}

// ── Projects ─────────────────────────────────────────────────────────────────

export interface ProjectDTO {
  cwd: string
  sessions: number
  turn_count: number
  tokens_in_total: number
  tokens_out_total: number
  last_active: number   // unix epoch seconds; 0 = never
  hosts: string[]
  tools: string[]
  top_tool: string
}

export interface Project {
  cwd: string
  sessions: number
  turns: number
  tokensIn: number
  tokensOut: number
  lastActive: string    // ISO 8601, or '' when last_active === 0
  hosts: string[]
  tools: string[]
  topTool: string
}

export function adaptProject(d: ProjectDTO): Project {
  return {
    cwd: d.cwd,
    sessions: d.sessions,
    turns: d.turn_count,
    tokensIn: d.tokens_in_total,
    tokensOut: d.tokens_out_total,
    lastActive: d.last_active === 0 ? '' : new Date(d.last_active * 1000).toISOString(),
    hosts: d.hosts,
    tools: d.tools,
    topTool: d.top_tool,
  }
}

// ── Stats ─────────────────────────────────────────────────────────────────────

export interface ToolRollupDTO {
  tool: string
  sessions: number
  turns: number
  tokens_in: number
  tokens_out: number
  daily_sparkline: number[]
}

export interface DailyBucketDTO {
  date_unix: number
  per_tool: Record<string, number>
}

export interface HeatmapCellDTO {
  date_unix: number
  count: number
}

export interface CwdRowDTO {
  cwd: string
  count: number
}

export interface HourBucketDTO {
  hour: number
  count: number
}

export interface HostRowDTO {
  host: string
  count: number
}

export interface StatsDTO {
  per_tool: ToolRollupDTO[]
  daily: DailyBucketDTO[]
  heatmap: HeatmapCellDTO[]
  top_cwd: CwdRowDTO[]
  hour_of_day: HourBucketDTO[]
  host_split: HostRowDTO[]
}

export interface ToolRollup {
  tool: string
  sessions: number
  turns: number
  tokensIn: number
  tokensOut: number
  dailySparkline: number[]
}

export interface DailyBucket {
  date: string             // ISO 8601
  perTool: Record<string, number>
}

export interface HeatmapCell {
  date: string             // ISO 8601
  count: number
}

export interface CwdRow {
  cwd: string
  count: number
}

export interface HourBucket {
  hour: number
  count: number
}

export interface HostRow {
  host: string
  count: number
}

export interface Stats {
  perTool: ToolRollup[]
  daily: DailyBucket[]
  heatmap: HeatmapCell[]
  topCwd: CwdRow[]
  hourOfDay: HourBucket[]
  hostSplit: HostRow[]
}

export function adaptStats(d: StatsDTO): Stats {
  return {
    perTool: d.per_tool.map(r => ({
      tool: r.tool,
      sessions: r.sessions,
      turns: r.turns,
      tokensIn: r.tokens_in,
      tokensOut: r.tokens_out,
      dailySparkline: r.daily_sparkline,
    })),
    daily: d.daily.map(b => ({
      date: new Date(b.date_unix * 1000).toISOString(),
      perTool: b.per_tool,
    })),
    heatmap: d.heatmap.map(c => ({
      date: new Date(c.date_unix * 1000).toISOString(),
      count: c.count,
    })),
    topCwd: d.top_cwd,
    hourOfDay: d.hour_of_day,
    hostSplit: d.host_split,
  }
}

// ── Saved searches ──────────────────────────────────────────────────────────

export interface SavedSearchDTO {
  name: string
  query: string
  created_at: number
  updated_at: number
}

export interface SavedSearch {
  name: string
  query: string
  createdAt: string   // ISO 8601
  updatedAt: string   // ISO 8601
}

export function adaptSavedSearch(d: SavedSearchDTO): SavedSearch {
  return {
    name: d.name,
    query: d.query,
    createdAt: new Date(d.created_at * 1000).toISOString(),
    updatedAt: new Date(d.updated_at * 1000).toISOString(),
  }
}