~bigbes/lethe

ref: 346e6a81d673e2daf06604b9a838b08ba9dbdbef lethe/web/src/api/adapters.ts -rw-r--r-- 2.4 KiB
346e6a81 — Eugene Blikh web: project detail route scoped via ?cwd= sessions filter 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
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}`
  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}`,
    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,
  }
}