mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor: persist assistant repo in database with ID migration (#271)
This commit is contained in:
1 parent
0d384d6cf0
commit
8133b655b0
12 files changed
+188
-112
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Repo, CreateRepoInput } from '../types/repo'
|
||||
import { getReposPath } from '@opencode-manager/shared/config/env'
|
||||
import { ASSISTANT_REPO_ID, ASSISTANT_REPO_PATH } from '@opencode-manager/shared/utils'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
import path from 'path'
|
||||
|
||||
@@ -41,6 +42,13 @@ function rowToRepo(row: RepoRow): Repo {
|
||||
}
|
||||
}
|
||||
|
||||
const TABLES_WITH_REPO_ID = ['schedule_jobs', 'schedule_runs', 'repo_settings'] as const
|
||||
type RepoIdTable = typeof TABLES_WITH_REPO_ID[number]
|
||||
|
||||
function updateRepoIdReference(db: Database, tableName: RepoIdTable, fromRepoId: number, toRepoId: number): void {
|
||||
db.prepare(`UPDATE ${tableName} SET repo_id = ? WHERE repo_id = ?`).run(toRepoId, fromRepoId)
|
||||
}
|
||||
|
||||
export function getRepoById(db: Database, id: number): Repo | null {
|
||||
const stmt = db.prepare('SELECT * FROM repos WHERE id = ?')
|
||||
const row = stmt.get(id) as RepoRow | undefined
|
||||
@@ -48,6 +56,70 @@ export function getRepoById(db: Database, id: number): Repo | null {
|
||||
return row ? rowToRepo(row) : null
|
||||
}
|
||||
|
||||
export function ensureAssistantRepo(db: Database): Repo {
|
||||
const now = Date.now()
|
||||
|
||||
const syncAssistantRepo = db.transaction(() => {
|
||||
const existingAssistantPathRow = db.prepare('SELECT id FROM repos WHERE local_path = ? AND id != ?')
|
||||
.get(ASSISTANT_REPO_PATH, ASSISTANT_REPO_ID) as { id: number } | undefined
|
||||
|
||||
if (existingAssistantPathRow) {
|
||||
for (const table of TABLES_WITH_REPO_ID) {
|
||||
updateRepoIdReference(db, table, existingAssistantPathRow.id, ASSISTANT_REPO_ID)
|
||||
}
|
||||
db.prepare('DELETE FROM repos WHERE id = ?').run(existingAssistantPathRow.id)
|
||||
}
|
||||
|
||||
db.prepare(`
|
||||
INSERT INTO repos (
|
||||
id,
|
||||
repo_url,
|
||||
local_path,
|
||||
source_path,
|
||||
branch,
|
||||
default_branch,
|
||||
clone_status,
|
||||
cloned_at,
|
||||
last_accessed_at,
|
||||
is_worktree,
|
||||
is_local
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
repo_url = excluded.repo_url,
|
||||
local_path = excluded.local_path,
|
||||
source_path = excluded.source_path,
|
||||
branch = excluded.branch,
|
||||
default_branch = excluded.default_branch,
|
||||
clone_status = excluded.clone_status,
|
||||
last_accessed_at = excluded.last_accessed_at,
|
||||
is_worktree = excluded.is_worktree,
|
||||
is_local = excluded.is_local
|
||||
`).run(
|
||||
ASSISTANT_REPO_ID,
|
||||
null,
|
||||
ASSISTANT_REPO_PATH,
|
||||
null,
|
||||
null,
|
||||
'main',
|
||||
'ready',
|
||||
now,
|
||||
now,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
})
|
||||
|
||||
syncAssistantRepo()
|
||||
|
||||
const repo = getRepoById(db, ASSISTANT_REPO_ID)
|
||||
if (!repo) {
|
||||
throw new Error('Failed to sync Assistant repository')
|
||||
}
|
||||
|
||||
return repo
|
||||
}
|
||||
|
||||
export function createRepo(db: Database, repo: CreateRepoInput): Repo {
|
||||
const normalizedPath = repo.localPath.trim().replace(/\/+$/, '')
|
||||
|
||||
@@ -209,8 +281,9 @@ export function updateRepoBranch(db: Database, id: number, branch: string): void
|
||||
}
|
||||
|
||||
export function deleteRepo(db: Database, id: number): void {
|
||||
db.prepare('DELETE FROM schedule_runs WHERE repo_id = ?').run(id)
|
||||
db.prepare('DELETE FROM schedule_jobs WHERE repo_id = ?').run(id)
|
||||
for (const table of TABLES_WITH_REPO_ID) {
|
||||
db.prepare(`DELETE FROM ${table} WHERE repo_id = ?`).run(id)
|
||||
}
|
||||
const stmt = db.prepare('DELETE FROM repos WHERE id = ?')
|
||||
stmt.run(id)
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getErrorMessage, getStatusCode } from '../utils/error-utils'
|
||||
import { getOpenCodeConfigFilePath } from '@opencode-manager/shared/config/env'
|
||||
import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
|
||||
import { createRepoGitRoutes } from './repo-git'
|
||||
import { createScheduleRoutes } from './schedules'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
@@ -31,6 +32,10 @@ async function restartOpenCode(openCodeSupervisor?: OpenCodeSupervisor): Promise
|
||||
await opencodeServerManager.restart()
|
||||
}
|
||||
|
||||
function resolveRepo(database: Database, id: number): Repo | null {
|
||||
return getRepoById(database, id) ?? (id === ASSISTANT_REPO_ID ? buildAssistantRepo() : null)
|
||||
}
|
||||
|
||||
export function createRepoRoutes(
|
||||
database: Database,
|
||||
gitAuthService: GitAuthService,
|
||||
@@ -122,7 +127,7 @@ app.get('/', async (c) => {
|
||||
const reposWithCurrentBranch = await Promise.all(
|
||||
repos.map(async (repo) => {
|
||||
const env = gitAuthService.getGitEnvironment()
|
||||
const currentBranch = await repoService.getCurrentBranch(repo, env)
|
||||
const currentBranch = repo.id === ASSISTANT_REPO_ID ? undefined : await repoService.getCurrentBranch(repo, env)
|
||||
return { ...repo, currentBranch }
|
||||
})
|
||||
)
|
||||
@@ -157,14 +162,13 @@ app.get('/', async (c) => {
|
||||
try {
|
||||
const id = parseInt(c.req.param('id'))
|
||||
|
||||
const isAssistant = id === 0
|
||||
const repo: Repo | null = isAssistant ? buildAssistantRepo() : getRepoById(database, id)
|
||||
const repo: Repo | null = resolveRepo(database, id)
|
||||
|
||||
if (!repo) {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
}
|
||||
|
||||
const currentBranch = isAssistant ? undefined : await repoService.getCurrentBranch(repo, gitAuthService.getGitEnvironment())
|
||||
const currentBranch = id === ASSISTANT_REPO_ID ? undefined : await repoService.getCurrentBranch(repo, gitAuthService.getGitEnvironment())
|
||||
|
||||
return c.json({ ...repo, currentBranch })
|
||||
} catch (error: unknown) {
|
||||
@@ -267,6 +271,11 @@ app.get('/', async (c) => {
|
||||
app.delete('/:id', async (c) => {
|
||||
try {
|
||||
const id = parseInt(c.req.param('id'))
|
||||
|
||||
if (id === ASSISTANT_REPO_ID) {
|
||||
return c.json({ error: 'Cannot delete the assistant repository' }, 403)
|
||||
}
|
||||
|
||||
const repo = getRepoById(database, id)
|
||||
|
||||
if (!repo) {
|
||||
@@ -480,7 +489,7 @@ app.get('/', async (c) => {
|
||||
try {
|
||||
const id = parseInt(c.req.param('id'))
|
||||
|
||||
const repo: Repo | null = id === 0 ? buildAssistantRepo() : getRepoById(database, id)
|
||||
const repo: Repo | null = resolveRepo(database, id)
|
||||
|
||||
if (!repo) {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
@@ -498,7 +507,7 @@ app.get('/', async (c) => {
|
||||
try {
|
||||
const id = parseInt(c.req.param('id'))
|
||||
|
||||
const repo: Repo | null = id === 0 ? buildAssistantRepo() : getRepoById(database, id)
|
||||
const repo: Repo | null = resolveRepo(database, id)
|
||||
|
||||
if (!repo) {
|
||||
return c.json({ error: 'Repo not found' }, 404)
|
||||
|
||||
@@ -17,6 +17,7 @@ import { ASSISTANT_REPO_ID, ASSISTANT_REPO_PATH } from '@opencode-manager/shared
|
||||
import { getReposPath, ENV } from '@opencode-manager/shared/config/env'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { getOrCreateInternalToken } from './internal-token'
|
||||
import { ensureAssistantRepo } from '../db/queries'
|
||||
|
||||
|
||||
const ASSISTANT_MODE_DIR = ASSISTANT_REPO_PATH
|
||||
@@ -1081,7 +1082,9 @@ export async function installAssistantWorkspace(deps: {
|
||||
db: Database
|
||||
apiBaseUrl: string
|
||||
}): Promise<AssistantModeStatus> {
|
||||
return ensureAssistantMode(buildAssistantRepo(), {
|
||||
const assistantRepo = ensureAssistantRepo(deps.db)
|
||||
|
||||
return ensureAssistantMode(assistantRepo, {
|
||||
db: deps.db,
|
||||
apiBaseUrl: deps.apiBaseUrl,
|
||||
})
|
||||
|
||||
@@ -1080,6 +1080,8 @@ export class ScheduleService {
|
||||
|
||||
private assertRepo(repoId: number) {
|
||||
if (repoId === ASSISTANT_REPO_ID) {
|
||||
const repo = getRepoById(this.db, ASSISTANT_REPO_ID)
|
||||
if (repo) return repo
|
||||
return { ...buildAssistantRepo(), lastAccessedAt: Date.now(), isLocal: true, currentBranch: undefined }
|
||||
}
|
||||
const repo = getRepoById(this.db, repoId)
|
||||
|
||||
Reference in new issue
Block a user