-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex.ts
More file actions
180 lines (157 loc) · 5.39 KB
/
Copy pathcodex.ts
File metadata and controls
180 lines (157 loc) · 5.39 KB
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
import Database from 'better-sqlite3'
import { UnifiedSession } from '../../shared/types'
import type { ChatMessage } from './opencode'
import { homedir } from 'os'
import { join, basename } from 'path'
import { existsSync, readdirSync, readFileSync } from 'fs'
function extractProjectName(path: string | null): string | null {
if (!path) return null
if (path.startsWith('/private/var/folders/') || path.startsWith('/var/folders/')) return null
const parts = path.replace(/\/$/, '').split('/')
return parts[parts.length - 1] || null
}
function findJsonlPath(rolloutPath: string, sessionId: string): string | null {
if (existsSync(rolloutPath)) return rolloutPath
const sessionsDir = join(homedir(), '.codex', 'sessions')
const filename = basename(rolloutPath)
const searchDir = (dir: string): string | null => {
if (!existsSync(dir)) return null
const entries = readdirSync(dir, { withFileTypes: true })
for (const e of entries) {
const full = join(dir, e.name)
if (e.isDirectory()) {
const found = searchDir(full)
if (found) return found
} else if (e.name === filename || e.name === `${sessionId}.jsonl`) {
return full
}
}
return null
}
return searchDir(sessionsDir)
}
function countMessagesInJsonl(jsonlPath: string): number {
try {
const content = readFileSync(jsonlPath, 'utf-8')
let count = 0
for (const line of content.split('\n')) {
if (!line.trim()) continue
try {
const obj = JSON.parse(line)
if (obj.type === 'response_item') {
const msg = obj.payload || obj.item
if (msg?.content) count++
}
} catch {}
}
return count
} catch {
return 0
}
}
export async function scanCodex(): Promise<UnifiedSession[]> {
const dbPath = join(homedir(), '.codex', 'state_5.sqlite')
if (!existsSync(dbPath)) return []
const codexDb = new Database(dbPath, { readonly: true })
const sessions: UnifiedSession[] = []
try {
let tableExists = false
const tables = codexDb.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as Array<{ name: string }>
tableExists = tables.some(t => t.name === 'threads')
if (!tableExists) return []
const rows = codexDb.prepare(`
SELECT
id, title, cwd, model, tokens_used, first_user_message,
git_branch, created_at, updated_at, preview,
rollout_path
FROM threads t
ORDER BY updated_at DESC
`).all() as Array<{
id: string; title: string | null; cwd: string | null;
model: string | null; tokens_used: number | null;
first_user_message: string | null; git_branch: string | null;
created_at: number | null; updated_at: number | null;
preview: string | null; rollout_path: string | null
}>
for (const row of rows) {
let messageCount = 0
if (row.rollout_path) {
const jsonlPath = findJsonlPath(row.rollout_path, row.id)
if (jsonlPath) {
messageCount = countMessagesInJsonl(jsonlPath)
}
}
sessions.push({
id: `codex:${row.id}`,
tool: 'codex',
originalId: row.id,
projectPath: row.cwd,
projectName: extractProjectName(row.cwd),
title: row.first_user_message || row.title,
summary: row.preview || null,
model: row.model,
messageCount,
tokensTotal: row.tokens_used || 0,
cost: 0,
gitBranch: row.git_branch,
createdAt: row.created_at,
updatedAt: row.updated_at,
isActive: 0,
starred: 0,
tags: null,
archived: 0
})
}
} finally {
codexDb.close()
}
return sessions
}
export function getCodexMessages(sessionId: string): ChatMessage[] {
const dbPath = join(homedir(), '.codex', 'state_5.sqlite')
if (!existsSync(dbPath)) return []
const codexDb = new Database(dbPath, { readonly: true })
try {
const row = codexDb.prepare('SELECT rollout_path FROM threads WHERE id = ?').get(sessionId) as { rollout_path: string | null } | undefined
if (!row?.rollout_path) return []
const jsonlPath = findJsonlPath(row.rollout_path, sessionId)
if (!jsonlPath || !existsSync(jsonlPath)) return []
const content = readFileSync(jsonlPath, 'utf-8')
const lines = content.split('\n')
const messages: ChatMessage[] = []
for (const line of lines) {
if (!line.trim()) continue
try {
const obj = JSON.parse(line)
if (obj.type === 'response_item') {
const msg = obj.payload || obj.item
if (!msg?.content) continue
const role = msg.role
let text = ''
if (typeof msg.content === 'string') {
text = msg.content
} else if (Array.isArray(msg.content)) {
text = msg.content
.filter((b: { type: string }) => b.type === 'output_text' || b.type === 'input_text')
.map((b: { text: string }) => b.text)
.join('\n')
}
text = text.trim()
if (!text) continue
messages.push({
role: role === 'user' ? 'user' : 'assistant',
type: 'text',
content: text,
timestamp: null
})
}
} catch {}
}
return messages
} finally {
codexDb.close()
}
}
export function getCodexResumeCmd(originalId: string, projectPath: string): string {
return `cd "${projectPath}" && codex --resume ${originalId}`
}