forked from shareAI-lab/Kode-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkodeAgentSessionLoad.ts
More file actions
249 lines (216 loc) · 6.54 KB
/
kodeAgentSessionLoad.ts
File metadata and controls
249 lines (216 loc) · 6.54 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { existsSync, readdirSync, readFileSync, statSync } from 'fs'
import { basename, join } from 'path'
import type { Message } from '@query'
import type {
Message as APIMessage,
MessageParam,
} from '@anthropic-ai/sdk/resources/index.mjs'
import { getSessionProjectDir } from './kodeAgentSessionLog'
import { isUuid } from '@utils/text/uuid'
type JsonlUserEntry = {
type: 'user'
sessionId?: string
uuid?: string
message?: MessageParam
isApiErrorMessage?: boolean
toolUseResult?: unknown
}
type JsonlAssistantEntry = {
type: 'assistant'
sessionId?: string
uuid?: string
message?: APIMessage
isApiErrorMessage?: boolean
requestId?: string
}
type JsonlSummaryEntry = {
type: 'summary'
summary?: string
leafUuid?: string
}
type JsonlCustomTitleEntry = {
type: 'custom-title'
sessionId?: string
customTitle?: string
}
type JsonlTagEntry = {
type: 'tag'
sessionId?: string
tag?: string
}
type JsonlFileHistorySnapshotEntry = {
type: 'file-history-snapshot'
messageId?: string
snapshot?: unknown
isSnapshotUpdate?: boolean
}
type JsonlEntry =
| JsonlUserEntry
| JsonlAssistantEntry
| JsonlSummaryEntry
| JsonlCustomTitleEntry
| JsonlTagEntry
| JsonlFileHistorySnapshotEntry
| Record<string, unknown>
function safeParseJson(line: string): unknown | null {
try {
return JSON.parse(line)
} catch {
return null
}
}
function isUserEntry(entry: JsonlEntry): entry is JsonlUserEntry {
return (
typeof (entry as any)?.type === 'string' && (entry as any).type === 'user'
)
}
function isAssistantEntry(entry: JsonlEntry): entry is JsonlAssistantEntry {
return (
typeof (entry as any)?.type === 'string' &&
(entry as any).type === 'assistant'
)
}
function isSummaryEntry(entry: JsonlEntry): entry is JsonlSummaryEntry {
return (
typeof (entry as any)?.type === 'string' &&
(entry as any).type === 'summary'
)
}
function isCustomTitleEntry(entry: JsonlEntry): entry is JsonlCustomTitleEntry {
return (
typeof (entry as any)?.type === 'string' &&
(entry as any).type === 'custom-title'
)
}
function isTagEntry(entry: JsonlEntry): entry is JsonlTagEntry {
return (
typeof (entry as any)?.type === 'string' && (entry as any).type === 'tag'
)
}
function isFileHistorySnapshotEntry(
entry: JsonlEntry,
): entry is JsonlFileHistorySnapshotEntry {
return (
typeof (entry as any)?.type === 'string' &&
(entry as any).type === 'file-history-snapshot'
)
}
function normalizeLoadedUser(entry: JsonlUserEntry): Message | null {
if (!entry.uuid || !entry.message) return null
return {
type: 'user',
uuid: entry.uuid as any,
message: entry.message as any,
...(entry.toolUseResult !== undefined
? { toolUseResult: { data: entry.toolUseResult, resultForAssistant: '' } }
: {}),
}
}
function normalizeLoadedAssistant(entry: JsonlAssistantEntry): Message | null {
if (!entry.uuid || !entry.message) return null
return {
type: 'assistant',
uuid: entry.uuid as any,
costUSD: 0,
durationMs: 0,
message: entry.message as any,
...(entry.isApiErrorMessage ? { isApiErrorMessage: true } : {}),
...(typeof entry.requestId === 'string'
? { requestId: entry.requestId }
: {}),
} as any
}
export type KodeAgentSessionLogData = {
messages: Message[]
summaries: Map<string, string>
customTitles: Map<string, string>
tags: Map<string, string>
fileHistorySnapshots: Map<string, JsonlFileHistorySnapshotEntry>
}
export function loadKodeAgentSessionLogData(args: {
cwd: string
sessionId: string
}): KodeAgentSessionLogData {
const { cwd, sessionId } = args
const projectDir = getSessionProjectDir(cwd)
const filePath = join(projectDir, `${sessionId}.jsonl`)
if (!existsSync(filePath)) {
throw new Error(`No conversation found with session ID: ${sessionId}`)
}
const lines = readFileSync(filePath, 'utf8').split('\n')
const messages: Message[] = []
const summaries = new Map<string, string>()
const customTitles = new Map<string, string>()
const tags = new Map<string, string>()
const fileHistorySnapshots = new Map<string, JsonlFileHistorySnapshotEntry>()
for (const line of lines) {
const raw = safeParseJson(line.trim())
if (!raw || typeof raw !== 'object') continue
const entry = raw as JsonlEntry
if (isUserEntry(entry)) {
if (entry.sessionId && entry.sessionId !== sessionId) continue
const msg = normalizeLoadedUser(entry)
if (msg) messages.push(msg)
continue
}
if (isAssistantEntry(entry)) {
if (entry.sessionId && entry.sessionId !== sessionId) continue
const msg = normalizeLoadedAssistant(entry)
if (msg) messages.push(msg)
continue
}
if (isSummaryEntry(entry)) {
const leafUuid = typeof entry.leafUuid === 'string' ? entry.leafUuid : ''
const summary = typeof entry.summary === 'string' ? entry.summary : ''
if (leafUuid && summary) summaries.set(leafUuid, summary)
continue
}
if (isCustomTitleEntry(entry)) {
const id = typeof entry.sessionId === 'string' ? entry.sessionId : ''
const title =
typeof entry.customTitle === 'string' ? entry.customTitle : ''
if (id && title) customTitles.set(id, title)
continue
}
if (isTagEntry(entry)) {
const id = typeof entry.sessionId === 'string' ? entry.sessionId : ''
const tag = typeof entry.tag === 'string' ? entry.tag : ''
if (id && tag) tags.set(id, tag)
continue
}
if (isFileHistorySnapshotEntry(entry)) {
const messageId =
typeof entry.messageId === 'string' ? entry.messageId : ''
if (messageId) fileHistorySnapshots.set(messageId, entry)
continue
}
}
return { messages, summaries, customTitles, tags, fileHistorySnapshots }
}
export function loadKodeAgentSessionMessages(args: {
cwd: string
sessionId: string
}): Message[] {
return loadKodeAgentSessionLogData(args).messages
}
export function findMostRecentKodeAgentSessionId(cwd: string): string | null {
const projectDir = getSessionProjectDir(cwd)
if (!existsSync(projectDir)) return null
const candidates = readdirSync(projectDir)
.filter(name => name.endsWith('.jsonl'))
.filter(name => !name.startsWith('agent-'))
.map(name => ({
sessionId: basename(name, '.jsonl'),
path: join(projectDir, name),
}))
.filter(c => isUuid(c.sessionId))
if (candidates.length === 0) return null
candidates.sort((a, b) => {
try {
return statSync(b.path).mtimeMs - statSync(a.path).mtimeMs
} catch {
return 0
}
})
return candidates[0]?.sessionId ?? null
}