Merge pull request #3796 from code-yeongyu/fix/bun-sqlite-dynamic-import

fix(ultrawork): lazy-load bun:sqlite to support Node/Electron runtime
This commit is contained in:
YeonGyu-Kim
2026-05-05 19:17:40 +09:00
committed by GitHub
2 changed files with 33 additions and 5 deletions
@@ -0,0 +1,20 @@
import { describe, expect, test } from "bun:test"
describe("scheduleDeferredModelOverride bun:sqlite unavailable", () => {
test("#given source code #when inspected #then bun:sqlite is loaded dynamically with an unavailable-runtime fallback", async () => {
//#given
const source = await Bun.file(new URL("./ultrawork-db-model-override.ts", import.meta.url)).text()
//#when
const hasStaticBunSqliteImport = source.includes('from "bun:sqlite"')
|| source.includes("from 'bun:sqlite'")
|| source.includes('import "bun:sqlite"')
|| source.includes("import 'bun:sqlite'")
//#then
expect(hasStaticBunSqliteImport).toBe(false)
expect(source).toContain('await import("bun:sqlite").catch(() => null)')
expect(source).toContain("bun:sqlite unavailable")
expect(source).toContain("return")
})
})
+13 -5
View File
@@ -1,9 +1,10 @@
import { Database } from "bun:sqlite"
import { join } from "node:path"
import { existsSync } from "node:fs"
import { getDataDir } from "../shared/data-path"
import { log } from "../shared"
type BunDatabase = import("bun:sqlite").Database
function getDbPath(): string {
return join(getDataDir(), "opencode", "opencode.db")
}
@@ -11,7 +12,7 @@ function getDbPath(): string {
const MAX_MICROTASK_RETRIES = 10
function tryUpdateMessageModel(
db: InstanceType<typeof Database>,
db: BunDatabase,
messageId: string,
targetModel: { providerID: string; modelID: string },
variant?: string,
@@ -30,7 +31,7 @@ function tryUpdateMessageModel(
}
function retryViaMicrotask(
db: InstanceType<typeof Database>,
db: BunDatabase,
messageId: string,
targetModel: { providerID: string; modelID: string },
variant: string | undefined,
@@ -112,14 +113,21 @@ export function scheduleDeferredModelOverride(
targetModel: { providerID: string; modelID: string },
variant?: string,
): void {
queueMicrotask(() => {
queueMicrotask(async () => {
const sqliteModule = await import("bun:sqlite").catch(() => null)
const Database = sqliteModule?.Database
if (typeof Database !== "function") {
log("[ultrawork-db-override] bun:sqlite unavailable, skipping deferred override", { messageId })
return
}
const dbPath = getDbPath()
if (!existsSync(dbPath)) {
log("[ultrawork-db-override] DB not found, skipping deferred override")
return
}
let db: InstanceType<typeof Database>
let db: BunDatabase
try {
db = new Database(dbPath)
} catch (error) {