refactor(shared): unify MESSAGE_STORAGE/PART_STORAGE constants into single source
- Add src/shared/opencode-storage-paths.ts with consolidated constants - Update imports in hook-message-injector and session-manager - Add src/shared/opencode-storage-detection.ts with isSqliteBackend() - Add OPENCODE_SQLITE_VERSION constant - Export all from shared/index.ts
This commit is contained in:
parent
c9c02e0525
commit
5eebef953b
@ -1,6 +1 @@
|
|||||||
import { join } from "node:path"
|
export { OPENCODE_STORAGE, MESSAGE_STORAGE, PART_STORAGE } from "../../shared"
|
||||||
import { getOpenCodeStorageDir } from "../../shared/data-path"
|
|
||||||
|
|
||||||
export const OPENCODE_STORAGE = getOpenCodeStorageDir()
|
|
||||||
export const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
|
|
||||||
export const PART_STORAGE = join(OPENCODE_STORAGE, "part")
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@ export type {
|
|||||||
OpenCodeConfigPaths,
|
OpenCodeConfigPaths,
|
||||||
} from "./opencode-config-dir-types"
|
} from "./opencode-config-dir-types"
|
||||||
export * from "./opencode-version"
|
export * from "./opencode-version"
|
||||||
|
export * from "./opencode-storage-detection"
|
||||||
export * from "./permission-compat"
|
export * from "./permission-compat"
|
||||||
export * from "./external-plugin-detector"
|
export * from "./external-plugin-detector"
|
||||||
export * from "./zip-extractor"
|
export * from "./zip-extractor"
|
||||||
@ -49,4 +50,5 @@ export * from "./port-utils"
|
|||||||
export * from "./git-worktree"
|
export * from "./git-worktree"
|
||||||
export * from "./safe-create-hook"
|
export * from "./safe-create-hook"
|
||||||
export * from "./truncate-description"
|
export * from "./truncate-description"
|
||||||
|
export * from "./opencode-storage-paths"
|
||||||
export * from "./opencode-message-dir"
|
export * from "./opencode-message-dir"
|
||||||
|
|||||||
94
src/shared/opencode-storage-detection.test.ts
Normal file
94
src/shared/opencode-storage-detection.test.ts
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import { describe, it, expect, beforeEach, afterEach, vi } from "bun:test"
|
||||||
|
import { existsSync } from "node:fs"
|
||||||
|
import { join } from "node:path"
|
||||||
|
import { isSqliteBackend, resetSqliteBackendCache } from "./opencode-storage-detection"
|
||||||
|
import { getDataDir } from "./data-path"
|
||||||
|
import { isOpenCodeVersionAtLeast, OPENCODE_SQLITE_VERSION } from "./opencode-version"
|
||||||
|
|
||||||
|
// Mock the dependencies
|
||||||
|
const mockExistsSync = vi.fn()
|
||||||
|
const mockGetDataDir = vi.fn()
|
||||||
|
const mockIsOpenCodeVersionAtLeast = vi.fn()
|
||||||
|
|
||||||
|
vi.mock("node:fs", () => ({
|
||||||
|
existsSync: mockExistsSync,
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock("./data-path", () => ({
|
||||||
|
getDataDir: mockGetDataDir,
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock("./opencode-version", () => ({
|
||||||
|
isOpenCodeVersionAtLeast: mockIsOpenCodeVersionAtLeast,
|
||||||
|
OPENCODE_SQLITE_VERSION: "1.1.53",
|
||||||
|
}))
|
||||||
|
|
||||||
|
describe("isSqliteBackend", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
// Reset the cached result
|
||||||
|
resetSqliteBackendCache()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns false when version is below threshold", () => {
|
||||||
|
// given
|
||||||
|
mockIsOpenCodeVersionAtLeast.mockReturnValue(false)
|
||||||
|
mockGetDataDir.mockReturnValue("/home/user/.local/share")
|
||||||
|
mockExistsSync.mockReturnValue(true)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = isSqliteBackend()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result).toBe(false)
|
||||||
|
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledWith(OPENCODE_SQLITE_VERSION)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns false when DB file does not exist", () => {
|
||||||
|
// given
|
||||||
|
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
|
||||||
|
mockGetDataDir.mockReturnValue("/home/user/.local/share")
|
||||||
|
mockExistsSync.mockReturnValue(false)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = isSqliteBackend()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result).toBe(false)
|
||||||
|
expect(mockExistsSync).toHaveBeenCalledWith(join("/home/user/.local/share", "opencode", "opencode.db"))
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns true when version is at or above threshold and DB exists", () => {
|
||||||
|
// given
|
||||||
|
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
|
||||||
|
mockGetDataDir.mockReturnValue("/home/user/.local/share")
|
||||||
|
mockExistsSync.mockReturnValue(true)
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = isSqliteBackend()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result).toBe(true)
|
||||||
|
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledWith(OPENCODE_SQLITE_VERSION)
|
||||||
|
expect(mockExistsSync).toHaveBeenCalledWith(join("/home/user/.local/share", "opencode", "opencode.db"))
|
||||||
|
})
|
||||||
|
|
||||||
|
it("caches the result and does not re-check on subsequent calls", () => {
|
||||||
|
// given
|
||||||
|
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
|
||||||
|
mockGetDataDir.mockReturnValue("/home/user/.local/share")
|
||||||
|
mockExistsSync.mockReturnValue(true)
|
||||||
|
|
||||||
|
// when
|
||||||
|
isSqliteBackend()
|
||||||
|
isSqliteBackend()
|
||||||
|
isSqliteBackend()
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledTimes(1)
|
||||||
|
expect(mockExistsSync).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
23
src/shared/opencode-storage-detection.ts
Normal file
23
src/shared/opencode-storage-detection.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { existsSync } from "node:fs"
|
||||||
|
import { join } from "node:path"
|
||||||
|
import { getDataDir } from "./data-path"
|
||||||
|
import { isOpenCodeVersionAtLeast, OPENCODE_SQLITE_VERSION } from "./opencode-version"
|
||||||
|
|
||||||
|
let cachedResult: boolean | null = null
|
||||||
|
|
||||||
|
export function isSqliteBackend(): boolean {
|
||||||
|
if (cachedResult !== null) {
|
||||||
|
return cachedResult
|
||||||
|
}
|
||||||
|
|
||||||
|
const versionOk = isOpenCodeVersionAtLeast(OPENCODE_SQLITE_VERSION)
|
||||||
|
const dbPath = join(getDataDir(), "opencode", "opencode.db")
|
||||||
|
const dbExists = existsSync(dbPath)
|
||||||
|
|
||||||
|
cachedResult = versionOk && dbExists
|
||||||
|
return cachedResult
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resetSqliteBackendCache(): void {
|
||||||
|
cachedResult = null
|
||||||
|
}
|
||||||
7
src/shared/opencode-storage-paths.ts
Normal file
7
src/shared/opencode-storage-paths.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { join } from "node:path"
|
||||||
|
import { getOpenCodeStorageDir } from "./data-path"
|
||||||
|
|
||||||
|
export const OPENCODE_STORAGE = getOpenCodeStorageDir()
|
||||||
|
export const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
|
||||||
|
export const PART_STORAGE = join(OPENCODE_STORAGE, "part")
|
||||||
|
export const SESSION_STORAGE = join(OPENCODE_STORAGE, "session")
|
||||||
@ -15,6 +15,12 @@ export const MINIMUM_OPENCODE_VERSION = "1.1.1"
|
|||||||
*/
|
*/
|
||||||
export const OPENCODE_NATIVE_AGENTS_INJECTION_VERSION = "1.1.37"
|
export const OPENCODE_NATIVE_AGENTS_INJECTION_VERSION = "1.1.37"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenCode version that introduced SQLite backend for storage.
|
||||||
|
* When this version is detected AND opencode.db exists, SQLite backend is used.
|
||||||
|
*/
|
||||||
|
export const OPENCODE_SQLITE_VERSION = "1.1.53"
|
||||||
|
|
||||||
const NOT_CACHED = Symbol("NOT_CACHED")
|
const NOT_CACHED = Symbol("NOT_CACHED")
|
||||||
let cachedVersion: string | null | typeof NOT_CACHED = NOT_CACHED
|
let cachedVersion: string | null | typeof NOT_CACHED = NOT_CACHED
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
import { getOpenCodeStorageDir } from "../../shared/data-path"
|
|
||||||
import { getClaudeConfigDir } from "../../shared"
|
import { getClaudeConfigDir } from "../../shared"
|
||||||
|
|
||||||
export const OPENCODE_STORAGE = getOpenCodeStorageDir()
|
export { OPENCODE_STORAGE, MESSAGE_STORAGE, PART_STORAGE, SESSION_STORAGE } from "../../shared"
|
||||||
export const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
|
|
||||||
export const PART_STORAGE = join(OPENCODE_STORAGE, "part")
|
|
||||||
export const SESSION_STORAGE = join(OPENCODE_STORAGE, "session")
|
|
||||||
export const TODO_DIR = join(getClaudeConfigDir(), "todos")
|
export const TODO_DIR = join(getClaudeConfigDir(), "todos")
|
||||||
export const TRANSCRIPT_DIR = join(getClaudeConfigDir(), "transcripts")
|
export const TRANSCRIPT_DIR = join(getClaudeConfigDir(), "transcripts")
|
||||||
export const SESSION_LIST_DESCRIPTION = `List all OpenCode sessions with optional filtering.
|
export const SESSION_LIST_DESCRIPTION = `List all OpenCode sessions with optional filtering.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user