feat(ai-vault): support OpenCode SQLite session storage (#5925)

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Orca <help@stably.ai>
Co-authored-by: brennanb2025 <brennankbenson@gmail.com>
This commit is contained in:
Salah Alkhwlani
2026-06-23 11:18:17 -07:00
committed by GitHub
co-authored by Devin Orca brennanb2025
parent 8c4f57e8ef
commit 03fd869b92
15 changed files with 1473 additions and 34 deletions
+1 -12
View File
@@ -6,6 +6,7 @@ import { isAbsolute, join, posix, win32 } from 'path'
import type { Repo } from '../../shared/types'
import { areWorktreePathsEqual } from '../ipc/worktree-logic'
import Database from '../sqlite/sync-database'
import { columnExists, tableExists } from './schema-helpers'
import { canonicalizeUsageWorktreePaths } from '../usage-worktree-canonicalizer'
import type {
OpenCodeUsageAttributedEvent,
@@ -135,18 +136,6 @@ async function yieldToEventLoop(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 0))
}
function tableExists(db: Database.Database, tableName: string): boolean {
const row = db
.prepare("SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = ?")
.get(tableName) as { found?: number } | undefined
return row?.found === 1
}
function columnExists(db: Database.Database, tableName: string, columnName: string): boolean {
const rows = db.prepare(`PRAGMA table_info(${tableName})`).all() as { name?: string }[]
return rows.some((row) => row.name === columnName)
}
function getProjectJoin(db: Database.Database): string {
return tableExists(db, 'project') && columnExists(db, 'session', 'project_id')
? 'LEFT JOIN project p ON p.id = s.project_id'
+31
View File
@@ -0,0 +1,31 @@
import type SyncDatabase from '../sqlite/sync-database'
// Why: OpenCode's usage scanner and the AI Vault session scanner both need to
// probe the opencode.db schema shape across multiple DB generations. Centralizing
// the probes here avoids two private copies and keeps the contract testable.
type Database = SyncDatabase.Database
/**
* Check whether a table exists in the given SQLite database.
* @param db - A readonly or read-write SyncDatabase instance.
* @param tableName - The table name to look up in sqlite_master.
* @returns `true` if the table exists, `false` otherwise.
*/
export function tableExists(db: Database, tableName: string): boolean {
const row = db
.prepare("SELECT 1 AS found FROM sqlite_master WHERE type = 'table' AND name = ?")
.get(tableName) as { found?: number } | undefined
return row?.found === 1
}
/**
* Check whether a column exists on a table in the given SQLite database.
* @param db - A readonly or read-write SyncDatabase instance.
* @param tableName - The table to inspect via PRAGMA table_info.
* @param columnName - The column name to find.
* @returns `true` if the column exists on the table, `false` otherwise.
*/
export function columnExists(db: Database, tableName: string, columnName: string): boolean {
const rows = db.prepare(`PRAGMA table_info(${tableName})`).all() as { name?: string }[]
return rows.some((row) => row.name === columnName)
}