Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/providers/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ function getOmpSessionsDir(override?: string): string {
return override ?? join(homedir(), '.omp', 'agent', 'sessions')
}

async function readFirstEntry(filePath: string): Promise<PiEntry | null> {
async function readSessionEntry(filePath: string): Promise<PiEntry | null> {
const content = await readSessionFile(filePath)
if (content === null) return null
const line = content.split('\n')[0]
if (!line?.trim()) return null
try {
return JSON.parse(line) as PiEntry
} catch {
return null

for (const line of content.split('\n')) {
if (!line.trim()) continue
try {
const entry = JSON.parse(line) as PiEntry
if (entry?.type === 'session') return entry
} catch {
continue
}
}

return null
}

async function discoverSessionsInDir(sessionsDir: string, providerName: string): Promise<SessionSource[]> {
Expand Down Expand Up @@ -130,10 +135,10 @@ async function discoverSessionsInDir(sessionsDir: string, providerName: string):
const fileStat = await stat(filePath).catch(() => null)
if (!fileStat?.isFile()) continue

const first = await readFirstEntry(filePath)
if (!first || first.type !== 'session') continue
const entry = await readSessionEntry(filePath)
if (!entry) continue

const cwd = first.cwd ?? dirName
const cwd = entry.cwd ?? dirName
sources.push({ path: filePath, project: basename(cwd), provider: providerName })
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/providers/omp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,30 @@ describe('omp provider - session discovery', () => {
expect(sessions[0]!.project).toBe('myproject')
})

it('discovers title-first sessions', async () => {
const projectDir = join(tmpDir, '--Users-test-myproject--')
await writeSession(projectDir, 'title-first.jsonl', [
JSON.stringify({ type: 'title', title: 'OMP title-first session' }),
'',
sessionMeta({ cwd: '/Users/test/myproject' }),
assistantMessage({}),
])

const provider = createOmpProvider(tmpDir)
const sessions = await provider.discoverSessions()

expect(sessions).toHaveLength(1)
expect(sessions[0]!.provider).toBe('omp')
expect(sessions[0]!.project).toBe('myproject')
})

it('returns empty for non-existent directory', async () => {
const provider = createOmpProvider('/nonexistent/omp/path')
const sessions = await provider.discoverSessions()
expect(sessions).toEqual([])
})

it('skips files whose first line is not a session entry', async () => {
it('skips files without a session entry', async () => {
const projectDir = join(tmpDir, '--Users-test-myproject--')
await writeSession(projectDir, 'bad.jsonl', [
JSON.stringify({ type: 'message', id: 'x' }),
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/pi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('pi provider - session discovery', () => {
expect(sessions).toEqual([])
})

it('skips files whose first line is not a session entry', async () => {
it('skips files without a session entry', async () => {
const projectDir = join(tmpDir, '--Users-test-myproject--')
await writeSession(projectDir, 'bad.jsonl', [
JSON.stringify({ type: 'message', id: 'x' }),
Expand Down