mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
chore: rebase local deployment features onto hapi v0.30.3
This commit is contained in:
@@ -0,0 +1,323 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { listLocalClaudeSessionSummaries, listLocalClaudeSessionsWithMessagesByIds } from './claudeSessions'
|
||||
|
||||
const SESSION_ID = '11111111-1111-4111-8111-111111111111'
|
||||
const CWD = '/tmp/claude-import-project'
|
||||
|
||||
function line(value: Record<string, unknown>): string {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
|
||||
describe('local Claude sessions', () => {
|
||||
let tempDir: string
|
||||
let previousConfigDir: string | undefined
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'hapi-claude-sessions-'))
|
||||
previousConfigDir = process.env.CLAUDE_CONFIG_DIR
|
||||
process.env.CLAUDE_CONFIG_DIR = tempDir
|
||||
mkdirSync(join(tempDir, 'projects', '-tmp-claude-import-project'), {
|
||||
recursive: true
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (previousConfigDir === undefined) delete process.env.CLAUDE_CONFIG_DIR
|
||||
else process.env.CLAUDE_CONFIG_DIR = previousConfigDir
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('lists main transcripts, converts visible history, and ignores subagent files', () => {
|
||||
const projectDir = join(tempDir, 'projects', '-tmp-claude-import-project')
|
||||
writeFileSync(
|
||||
join(projectDir, `${SESSION_ID}.jsonl`),
|
||||
[
|
||||
line({
|
||||
parentUuid: null,
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'First prompt' },
|
||||
uuid: 'user-1',
|
||||
timestamp: '2026-08-08T01:00:00.000Z'
|
||||
}),
|
||||
line({
|
||||
parentUuid: 'user-1',
|
||||
isSidechain: false,
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
model: 'claude-sonnet-4-5',
|
||||
content: [{ type: 'text', text: 'Answer' }]
|
||||
},
|
||||
uuid: 'assistant-1',
|
||||
timestamp: '2026-08-08T01:00:01.000Z'
|
||||
}),
|
||||
line({
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
isMeta: true,
|
||||
message: { role: 'user', content: 'hidden metadata' },
|
||||
uuid: 'meta-1',
|
||||
timestamp: '2026-08-08T01:00:02.000Z'
|
||||
}),
|
||||
line({
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'ai-title',
|
||||
aiTitle: 'Imported work'
|
||||
}),
|
||||
line({
|
||||
type: 'custom-title',
|
||||
customTitle: 'Renamed imported work',
|
||||
sessionId: SESSION_ID
|
||||
})
|
||||
].join('\n')
|
||||
)
|
||||
|
||||
const subagentDir = join(projectDir, SESSION_ID, 'subagents')
|
||||
mkdirSync(subagentDir, { recursive: true })
|
||||
writeFileSync(
|
||||
join(subagentDir, 'agent-child.jsonl'),
|
||||
line({
|
||||
cwd: CWD,
|
||||
sessionId: 'agent-child',
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'child prompt' },
|
||||
uuid: 'child-user'
|
||||
})
|
||||
)
|
||||
|
||||
expect(listLocalClaudeSessionSummaries()).toEqual([
|
||||
expect.objectContaining({
|
||||
id: SESSION_ID,
|
||||
title: 'Renamed imported work',
|
||||
lastUserMessage: 'First prompt',
|
||||
cwd: CWD,
|
||||
model: 'claude-sonnet-4-5',
|
||||
messageCount: 2
|
||||
})
|
||||
])
|
||||
|
||||
const full = listLocalClaudeSessionsWithMessagesByIds(new Set([SESSION_ID]))
|
||||
expect(full).toHaveLength(1)
|
||||
expect(full[0]?.messages).toEqual([
|
||||
expect.objectContaining({
|
||||
localId: `claude:${SESSION_ID}:user-1`,
|
||||
createdAt: Date.parse('2026-08-08T01:00:00.000Z'),
|
||||
content: expect.objectContaining({ role: 'user' })
|
||||
}),
|
||||
expect.objectContaining({
|
||||
localId: `claude:${SESSION_ID}:assistant-1`,
|
||||
createdAt: Date.parse('2026-08-08T01:00:01.000Z'),
|
||||
content: expect.objectContaining({ role: 'agent' })
|
||||
})
|
||||
])
|
||||
})
|
||||
|
||||
it('returns only requested session transcripts', () => {
|
||||
const projectDir = join(tempDir, 'projects', '-tmp-claude-import-project')
|
||||
for (const id of [SESSION_ID, '22222222-2222-4222-8222-222222222222']) {
|
||||
writeFileSync(
|
||||
join(projectDir, `${id}.jsonl`),
|
||||
line({
|
||||
parentUuid: null,
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: id,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: id },
|
||||
uuid: `user-${id}`,
|
||||
timestamp: '2026-08-08T01:00:00.000Z'
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const sessions = listLocalClaudeSessionsWithMessagesByIds(new Set([SESSION_ID]))
|
||||
expect(sessions.map((session) => session.id)).toEqual([SESSION_ID])
|
||||
})
|
||||
|
||||
it('imports only the active branch after a Claude rewind', () => {
|
||||
const projectDir = join(tempDir, 'projects', '-tmp-claude-import-project')
|
||||
const transcript = [
|
||||
{
|
||||
parentUuid: null,
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'Shared prompt' },
|
||||
uuid: 'user-1'
|
||||
},
|
||||
{
|
||||
parentUuid: 'user-1',
|
||||
isSidechain: false,
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: 'Shared answer' },
|
||||
{ type: 'tool_use', id: 'tool-active', name: 'Task', input: {} }
|
||||
]
|
||||
},
|
||||
uuid: 'assistant-1'
|
||||
},
|
||||
{
|
||||
parentUuid: null,
|
||||
isSidechain: true,
|
||||
parentToolUseId: 'tool-active',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: { role: 'assistant', content: [{ type: 'text', text: 'Active sidechain' }] },
|
||||
uuid: 'sidechain-active'
|
||||
},
|
||||
{
|
||||
parentUuid: 'assistant-1',
|
||||
isSidechain: false,
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'attachment',
|
||||
attachment: { filePath: '/tmp/context.txt' },
|
||||
uuid: 'attachment-common'
|
||||
},
|
||||
{
|
||||
parentUuid: 'attachment-common',
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'Abandoned prompt' },
|
||||
uuid: 'user-old'
|
||||
},
|
||||
{
|
||||
parentUuid: 'user-old',
|
||||
isSidechain: false,
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: 'Abandoned answer' },
|
||||
{ type: 'tool_use', id: 'tool-old', name: 'Task', input: {} }
|
||||
]
|
||||
},
|
||||
uuid: 'assistant-old'
|
||||
},
|
||||
{
|
||||
parentUuid: 'assistant-old',
|
||||
isSidechain: true,
|
||||
parentToolUseId: 'tool-old',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: { role: 'assistant', content: [{ type: 'text', text: 'Abandoned sidechain' }] },
|
||||
uuid: 'sidechain-old'
|
||||
},
|
||||
{
|
||||
parentUuid: 'attachment-common',
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'Replacement prompt' },
|
||||
uuid: 'user-new'
|
||||
},
|
||||
{
|
||||
parentUuid: 'user-new',
|
||||
isSidechain: false,
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: { role: 'assistant', content: [{ type: 'text', text: 'Replacement answer' }] },
|
||||
uuid: 'assistant-new'
|
||||
}
|
||||
]
|
||||
writeFileSync(join(projectDir, `${SESSION_ID}.jsonl`), transcript.map(line).join('\n'))
|
||||
|
||||
const [session] = listLocalClaudeSessionsWithMessagesByIds(new Set([SESSION_ID]))
|
||||
expect(session?.messages.map((message) => message.localId)).toEqual([
|
||||
`claude:${SESSION_ID}:user-1`,
|
||||
`claude:${SESSION_ID}:assistant-1`,
|
||||
`claude:${SESSION_ID}:sidechain-active`,
|
||||
`claude:${SESSION_ID}:user-new`,
|
||||
`claude:${SESSION_ID}:assistant-new`
|
||||
])
|
||||
expect(session).toMatchObject({
|
||||
lastUserMessage: 'Replacement prompt',
|
||||
messageCount: 5
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps linear history when legacy records have no parent links', () => {
|
||||
const projectDir = join(tempDir, 'projects', '-tmp-claude-import-project')
|
||||
writeFileSync(
|
||||
join(projectDir, `${SESSION_ID}.jsonl`),
|
||||
[
|
||||
line({
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: 'Legacy prompt' },
|
||||
uuid: 'legacy-user'
|
||||
}),
|
||||
line({
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'assistant',
|
||||
message: { role: 'assistant', content: [{ type: 'text', text: 'Legacy answer' }] },
|
||||
uuid: 'legacy-assistant'
|
||||
})
|
||||
].join('\n')
|
||||
)
|
||||
|
||||
const [session] = listLocalClaudeSessionsWithMessagesByIds(new Set([SESSION_ID]))
|
||||
expect(session?.messages.map((message) => message.localId)).toEqual([
|
||||
`claude:${SESSION_ID}:legacy-user`,
|
||||
`claude:${SESSION_ID}:legacy-assistant`
|
||||
])
|
||||
})
|
||||
|
||||
it('does not miss cwd when the first transcript record exceeds the old pre-read window', () => {
|
||||
const projectDir = join(tempDir, 'projects', '-tmp-claude-import-project')
|
||||
const longPrompt = `Start ${'x'.repeat(70 * 1024)}`
|
||||
writeFileSync(
|
||||
join(projectDir, `${SESSION_ID}.jsonl`),
|
||||
line({
|
||||
parentUuid: null,
|
||||
isSidechain: false,
|
||||
userType: 'external',
|
||||
cwd: CWD,
|
||||
sessionId: SESSION_ID,
|
||||
type: 'user',
|
||||
message: { role: 'user', content: longPrompt },
|
||||
uuid: 'long-user',
|
||||
timestamp: '2026-08-08T01:00:00.000Z'
|
||||
})
|
||||
)
|
||||
|
||||
expect(listLocalClaudeSessionSummaries()).toEqual([
|
||||
expect.objectContaining({
|
||||
id: SESSION_ID,
|
||||
cwd: CWD,
|
||||
messageCount: 1
|
||||
})
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,323 @@
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import { homedir } from 'node:os'
|
||||
import { basename, join } from 'node:path'
|
||||
import type {
|
||||
ClaudeImportedMessage,
|
||||
ClaudeImportedMessageContent,
|
||||
ClaudeLocalSessionSummary,
|
||||
ClaudeLocalSessionWithMessages
|
||||
} from '@hapi/protocol/apiTypes'
|
||||
import { isClaudeChatVisibleMessage } from '@hapi/protocol/messages'
|
||||
import { RawJSONLinesSchema, type RawJSONLines } from '@/claude/types'
|
||||
import { extractRawUserTextContent, isExternalUserMessage } from '@/claude/utils/transcriptMessages'
|
||||
|
||||
const DEFAULT_CLAUDE_SESSION_SCAN_LIMIT = 200
|
||||
|
||||
type SessionFileCandidate = {
|
||||
file: string
|
||||
modifiedAt: number
|
||||
discoveryIndex: number
|
||||
}
|
||||
|
||||
type ParsedClaudeSession = {
|
||||
summary: ClaudeLocalSessionSummary
|
||||
messages: ClaudeImportedMessage[]
|
||||
}
|
||||
|
||||
type ClaudeTranscriptRecord = {
|
||||
event: RawJSONLines | null
|
||||
uuid: string | null
|
||||
parentUuid: string | null
|
||||
isSidechain: boolean
|
||||
parentToolUseId: string | null
|
||||
customTitle: string | null
|
||||
}
|
||||
|
||||
function truncateText(value: string, maxLength: number): string {
|
||||
return value.length > maxLength ? `${value.slice(0, maxLength - 1)}…` : value
|
||||
}
|
||||
|
||||
function parseTimestamp(value: string | undefined, fallback: number): number {
|
||||
if (!value) return fallback
|
||||
const parsed = Date.parse(value)
|
||||
return Number.isFinite(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
export function getClaudeProjectsRoot(): string {
|
||||
const configDir = process.env.CLAUDE_CONFIG_DIR?.trim() || join(homedir(), '.claude')
|
||||
return join(configDir, 'projects')
|
||||
}
|
||||
|
||||
function collectClaudeSessionFiles(): SessionFileCandidate[] {
|
||||
let projectEntries: import('node:fs').Dirent[]
|
||||
try {
|
||||
projectEntries = readdirSync(getClaudeProjectsRoot(), {
|
||||
withFileTypes: true
|
||||
})
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
|
||||
const files: string[] = []
|
||||
for (const projectEntry of projectEntries) {
|
||||
if (!projectEntry.isDirectory()) continue
|
||||
const projectDir = join(getClaudeProjectsRoot(), projectEntry.name)
|
||||
let sessionEntries: import('node:fs').Dirent[]
|
||||
try {
|
||||
sessionEntries = readdirSync(projectDir, { withFileTypes: true })
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
for (const sessionEntry of sessionEntries) {
|
||||
if (sessionEntry.isFile() && sessionEntry.name.toLowerCase().endsWith('.jsonl')) {
|
||||
files.push(join(projectDir, sessionEntry.name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return files
|
||||
.flatMap((file, discoveryIndex) => {
|
||||
try {
|
||||
return [{ file, modifiedAt: statSync(file).mtimeMs, discoveryIndex }]
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
})
|
||||
.sort((a, b) => b.modifiedAt - a.modifiedAt || a.discoveryIndex - b.discoveryIndex)
|
||||
}
|
||||
|
||||
function importedUser(text: string): ClaudeImportedMessageContent {
|
||||
return {
|
||||
role: 'user',
|
||||
content: { type: 'text', text },
|
||||
meta: { sentFrom: 'cli' }
|
||||
}
|
||||
}
|
||||
|
||||
function importedAgent(data: RawJSONLines): ClaudeImportedMessageContent {
|
||||
return {
|
||||
role: 'agent',
|
||||
content: { type: 'output', data },
|
||||
meta: { sentFrom: 'cli' }
|
||||
}
|
||||
}
|
||||
|
||||
function parseTranscriptRecords(content: string): ClaudeTranscriptRecord[] {
|
||||
const records: ClaudeTranscriptRecord[] = []
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
if (!line.trim()) continue
|
||||
let raw: unknown
|
||||
try {
|
||||
raw = JSON.parse(line)
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) continue
|
||||
const rawRecord = raw as Record<string, unknown>
|
||||
const parsed = RawJSONLinesSchema.safeParse(raw)
|
||||
records.push({
|
||||
event: parsed.success ? parsed.data : null,
|
||||
uuid: typeof rawRecord.uuid === 'string' ? rawRecord.uuid : null,
|
||||
parentUuid: typeof rawRecord.parentUuid === 'string' ? rawRecord.parentUuid : null,
|
||||
isSidechain: rawRecord.isSidechain === true,
|
||||
parentToolUseId: typeof rawRecord.parentToolUseId === 'string' ? rawRecord.parentToolUseId : null,
|
||||
customTitle: rawRecord.type === 'custom-title' && typeof rawRecord.customTitle === 'string'
|
||||
? rawRecord.customTitle
|
||||
: null
|
||||
})
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
function isImportableConversationRecord(record: ClaudeTranscriptRecord): record is ClaudeTranscriptRecord & { event: RawJSONLines; uuid: string } {
|
||||
const event = record.event
|
||||
return Boolean(
|
||||
event &&
|
||||
record.uuid &&
|
||||
!event.isMeta &&
|
||||
!event.isCompactSummary &&
|
||||
isClaudeChatVisibleMessage(event)
|
||||
)
|
||||
}
|
||||
|
||||
function activeClaudeRecordIds(records: ClaudeTranscriptRecord[]): Set<string> | null {
|
||||
const topology = new Map<string, ClaudeTranscriptRecord>()
|
||||
for (const record of records) {
|
||||
if (record.uuid) topology.set(record.uuid, record)
|
||||
}
|
||||
let leaf: ClaudeTranscriptRecord | null = null
|
||||
for (let index = records.length - 1; index >= 0; index -= 1) {
|
||||
const record = records[index]!
|
||||
if (!record.isSidechain && isImportableConversationRecord(record)) {
|
||||
leaf = record
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!leaf?.uuid) return null
|
||||
|
||||
const activeMainIds = new Set<string>()
|
||||
const visited = new Set<string>()
|
||||
let currentUuid: string | null = leaf.uuid
|
||||
let followedKnownParent = false
|
||||
while (currentUuid && !visited.has(currentUuid)) {
|
||||
visited.add(currentUuid)
|
||||
activeMainIds.add(currentUuid)
|
||||
const current = topology.get(currentUuid)
|
||||
const parentUuid = current?.parentUuid ?? null
|
||||
if (parentUuid && topology.has(parentUuid)) followedKnownParent = true
|
||||
currentUuid = parentUuid
|
||||
}
|
||||
if (!followedKnownParent) return null
|
||||
|
||||
const activeToolUseIds = new Set<string>()
|
||||
for (const record of records) {
|
||||
if (!record.uuid || !activeMainIds.has(record.uuid) || record.event?.type !== 'assistant') continue
|
||||
const content = record.event.message?.content
|
||||
if (!Array.isArray(content)) continue
|
||||
for (const block of content) {
|
||||
if (block === null || typeof block !== 'object' || Array.isArray(block)) continue
|
||||
const toolUse = block as Record<string, unknown>
|
||||
if (toolUse.type === 'tool_use' && typeof toolUse.id === 'string') activeToolUseIds.add(toolUse.id)
|
||||
}
|
||||
}
|
||||
|
||||
const activeIds = new Set(activeMainIds)
|
||||
for (const record of records) {
|
||||
if (!record.uuid || !record.isSidechain || !isImportableConversationRecord(record)) continue
|
||||
const sidechainVisited = new Set<string>()
|
||||
let sidechainUuid: string | null = record.uuid
|
||||
while (sidechainUuid && !sidechainVisited.has(sidechainUuid)) {
|
||||
if (activeMainIds.has(sidechainUuid)) {
|
||||
activeIds.add(record.uuid)
|
||||
break
|
||||
}
|
||||
sidechainVisited.add(sidechainUuid)
|
||||
const current = topology.get(sidechainUuid)
|
||||
if (current && !current.isSidechain) break
|
||||
if (current?.parentToolUseId && activeToolUseIds.has(current.parentToolUseId)) {
|
||||
activeIds.add(record.uuid)
|
||||
break
|
||||
}
|
||||
sidechainUuid = current?.parentUuid ?? null
|
||||
}
|
||||
}
|
||||
return activeIds
|
||||
}
|
||||
|
||||
function parseClaudeLocalSession(filePath: string, knownModifiedAt?: number): ParsedClaudeSession | null {
|
||||
let content: string
|
||||
let modifiedAt: number
|
||||
try {
|
||||
content = readFileSync(filePath, 'utf-8')
|
||||
modifiedAt = knownModifiedAt ?? statSync(filePath).mtimeMs
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
const sessionId = basename(filePath, '.jsonl')
|
||||
if (!sessionId) return null
|
||||
|
||||
let cwd: string | null = null
|
||||
let customTitle: string | null = null
|
||||
let aiTitle: string | null = null
|
||||
let summary: string | null = null
|
||||
let firstUserMessage: string | null = null
|
||||
let lastUserMessage: string | null = null
|
||||
let model: string | null = null
|
||||
const messages: ClaudeImportedMessage[] = []
|
||||
const records = parseTranscriptRecords(content)
|
||||
const activeRecordIds = activeClaudeRecordIds(records)
|
||||
|
||||
for (const record of records) {
|
||||
if (record.customTitle !== null) {
|
||||
customTitle = record.customTitle.trim() || customTitle
|
||||
continue
|
||||
}
|
||||
const event = record.event
|
||||
if (!event) continue
|
||||
|
||||
cwd ??= event.cwd?.trim() || null
|
||||
if (event.type === 'ai-title') {
|
||||
aiTitle = event.aiTitle.trim() || aiTitle
|
||||
continue
|
||||
}
|
||||
if (event.type === 'summary') {
|
||||
summary = event.summary.trim() || summary
|
||||
continue
|
||||
}
|
||||
if (!isImportableConversationRecord(record)) continue
|
||||
|
||||
const uuid = record.uuid
|
||||
if (activeRecordIds && !activeRecordIds.has(uuid)) continue
|
||||
const createdAt = parseTimestamp(event.timestamp, modifiedAt)
|
||||
if (isExternalUserMessage(event)) {
|
||||
const text = extractRawUserTextContent(event.message.content)?.trim()
|
||||
if (!text) continue
|
||||
firstUserMessage ??= text
|
||||
lastUserMessage = text
|
||||
messages.push({
|
||||
localId: `claude:${sessionId}:${uuid}`,
|
||||
createdAt,
|
||||
content: importedUser(text)
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
if (event.type === 'assistant' && event.message?.model) model = event.message.model
|
||||
messages.push({
|
||||
localId: `claude:${sessionId}:${uuid}`,
|
||||
createdAt,
|
||||
content: importedAgent(event)
|
||||
})
|
||||
}
|
||||
|
||||
if (!cwd || messages.length === 0) return null
|
||||
const displayTitle =
|
||||
customTitle ?? aiTitle ?? (firstUserMessage ? truncateText(firstUserMessage, 80) : null) ?? summary ?? basename(cwd) ?? sessionId.slice(0, 8)
|
||||
|
||||
return {
|
||||
summary: {
|
||||
id: sessionId,
|
||||
title: displayTitle,
|
||||
lastUserMessage: lastUserMessage ? truncateText(lastUserMessage, 140) : null,
|
||||
cwd,
|
||||
file: filePath,
|
||||
modifiedAt,
|
||||
model,
|
||||
messageCount: messages.length
|
||||
},
|
||||
messages
|
||||
}
|
||||
}
|
||||
|
||||
export function listLocalClaudeSessionSummaries(limit = DEFAULT_CLAUDE_SESSION_SCAN_LIMIT): ClaudeLocalSessionSummary[] {
|
||||
if (limit <= 0) return []
|
||||
const summaries: ClaudeLocalSessionSummary[] = []
|
||||
const seenIds = new Set<string>()
|
||||
for (const candidate of collectClaudeSessionFiles()) {
|
||||
const sessionId = basename(candidate.file, '.jsonl')
|
||||
if (!sessionId || seenIds.has(sessionId)) continue
|
||||
const parsed = parseClaudeLocalSession(candidate.file, candidate.modifiedAt)
|
||||
if (!parsed) continue
|
||||
seenIds.add(sessionId)
|
||||
summaries.push(parsed.summary)
|
||||
if (summaries.length >= limit) break
|
||||
}
|
||||
return summaries
|
||||
}
|
||||
|
||||
export function listLocalClaudeSessionsWithMessagesByIds(ids: Set<string>): ClaudeLocalSessionWithMessages[] {
|
||||
if (ids.size === 0) return []
|
||||
const unresolved = new Set(ids)
|
||||
const sessions: ClaudeLocalSessionWithMessages[] = []
|
||||
for (const candidate of collectClaudeSessionFiles()) {
|
||||
const sessionId = basename(candidate.file, '.jsonl')
|
||||
if (!unresolved.has(sessionId)) continue
|
||||
const parsed = parseClaudeLocalSession(candidate.file, candidate.modifiedAt)
|
||||
if (!parsed) continue
|
||||
unresolved.delete(sessionId)
|
||||
sessions.push({ ...parsed.summary, messages: parsed.messages })
|
||||
if (unresolved.size === 0) break
|
||||
}
|
||||
return sessions
|
||||
}
|
||||
Reference in New Issue
Block a user