feat: implement manual SDK session sync and fix UI crash
Some checks failed
Release Binaries / release (push) Has been cancelled

This commit is contained in:
Gemini AI
2025-12-27 12:11:08 +04:00
Unverified
parent 251fad85b1
commit 0e5059fc88
5 changed files with 127 additions and 33 deletions

12
.gitignore vendored
View File

@@ -41,7 +41,7 @@ install.log
# ===================== OS Generated Files ===============
Thumbs.db
ehthumbs.db
Desktop.ini
44: Desktop.ini
# ===================== Temporary Files ==================
*.tmp
@@ -101,3 +101,13 @@ packages/server/dist/
*.backup
*_backup*
_backup_original/
# ===================== NomadArch Specific Data ============
.codenomad-data/
**/logs/
**/.codenomad-data/
sdk-sync-debug.log
**/sessions.json
**/messages.json
**/workspaces.json
*.json.bak

View File

@@ -9,10 +9,9 @@
*/
import { FastifyInstance } from "fastify"
import { readdir, readFile } from "fs/promises"
import { readdir, readFile, appendFile } from "fs/promises"
import { existsSync } from "fs"
import { join } from "path"
import { createHash } from "crypto"
import { homedir } from "os"
import { Logger } from "../../logger"
import { getSessionManager } from "../../storage/session-store"
@@ -59,53 +58,91 @@ function getOpenCodeStorageDir(): string {
return join(homeDir, ".local", "share", "opencode", "storage")
}
/**
* Generate the project ID hash that OpenCode uses
* OpenCode uses a SHA1 hash of the folder path
*/
function generateProjectId(folderPath: string): string {
return createHash("sha1").update(folderPath).digest("hex")
}
/**
* Read all sessions for a project from OpenCode's storage
*/
async function readOpenCodeSessions(folderPath: string, logger: Logger): Promise<OpenCodeSession[]> {
const storageDir = getOpenCodeStorageDir()
const projectId = generateProjectId(folderPath)
const sessionDir = join(storageDir, "session", projectId)
const sessionBaseDir = join(storageDir, "session")
const debugLogPath = join(process.cwd(), "sdk-sync-debug.log")
logger.info({ folderPath, projectId, sessionDir }, "Looking for OpenCode sessions")
const logDebug = async (msg: string, obj?: any) => {
const line = `[${new Date().toISOString()}] ${msg}${obj ? ' ' + JSON.stringify(obj) : ''}\n`
await appendFile(debugLogPath, line).catch(() => { })
logger.info(obj || {}, msg)
}
if (!existsSync(sessionDir)) {
logger.info({ sessionDir }, "OpenCode session directory not found")
// Normalize target folder path for comparison
const targetPath = folderPath.replace(/\\/g, '/').toLowerCase().trim()
await logDebug("Starting SDK session search", { folderPath, targetPath, sessionBaseDir })
if (!existsSync(sessionBaseDir)) {
await logDebug("OpenCode session base directory not found", { sessionBaseDir })
return []
}
const sessions: OpenCodeSession[] = []
try {
const projectDirs = await readdir(sessionBaseDir, { withFileTypes: true })
const dirs = projectDirs.filter(d => d.isDirectory()).map(d => d.name)
await logDebug("Scanning project directories", { count: dirs.length })
for (const projectId of dirs) {
const sessionDir = join(sessionBaseDir, projectId)
try {
const files = await readdir(sessionDir)
const sessionFiles = files.filter(f => f.startsWith("ses_") && f.endsWith(".json"))
const firstSessionFile = files.find(f => f.startsWith("ses_") && f.endsWith(".json"))
logger.info({ count: sessionFiles.length }, "Found OpenCode session files")
if (firstSessionFile) {
const content = await readFile(join(sessionDir, firstSessionFile), "utf-8")
const sessionData = JSON.parse(content) as OpenCodeSession
for (const file of sessionFiles) {
if (!sessionData.directory) {
await logDebug("Session file missing directory field", { projectId, firstSessionFile })
continue
}
const sessionPath = sessionData.directory.replace(/\\/g, '/').toLowerCase().trim()
if (sessionPath === targetPath) {
await logDebug("MATCH FOUND!", { projectId, sessionPath })
// This is the correct directory, read all sessions
const sessions: OpenCodeSession[] = [sessionData]
const otherFiles = files.filter(f => f !== firstSessionFile && f.startsWith("ses_") && f.endsWith(".json"))
for (const file of otherFiles) {
try {
const filePath = join(sessionDir, file)
const content = await readFile(filePath, "utf-8")
const session = JSON.parse(content) as OpenCodeSession
sessions.push(session)
} catch (error) {
logger.warn({ file, error }, "Failed to read session file")
const fileContent = await readFile(join(sessionDir, file), "utf-8")
sessions.push(JSON.parse(fileContent) as OpenCodeSession)
} catch (e) {
logger.warn({ file, error: e }, "Failed to read session file")
}
}
} catch (error) {
logger.error({ error }, "Failed to read OpenCode sessions directory")
}
await logDebug("Read sessions count", { count: sessions.length })
return sessions
} else {
// Just log a few mismatches to avoid bloating
// await logDebug("Mismatch", { sessionPath, targetPath })
}
}
} catch (e) {
await logDebug("Error scanning project directory", { projectId, error: String(e) })
}
}
} catch (error) {
await logDebug("Failed to scan OpenCode sessions directory", { error: String(error) })
}
await logDebug("No sessions found after scan")
return []
}
export function registerSdkSyncRoutes(app: FastifyInstance, deps: SdkSyncRouteDeps) {
const logger = deps.logger.child({ component: "sdk-sync" })

View File

@@ -7,6 +7,8 @@ import {
Settings,
Plug,
Sparkles,
RefreshCw,
Download,
ChevronRight,
ChevronDown,
Folder,
@@ -21,6 +23,7 @@ import InstanceServiceStatus from "../instance-service-status"
import McpManager from "../mcp-manager"
import { catalog, catalogLoading, catalogError, loadCatalog } from "../../stores/skills"
import { getSessionSkills, setSessionSkills } from "../../stores/session-state"
import { syncSessionsFromSdk } from "../../stores/session-api"
export interface FileNode {
name: string
@@ -132,6 +135,7 @@ export const Sidebar: Component<SidebarProps> = (props) => {
const [rootFiles, setRootFiles] = createSignal<FileNode[]>([])
const [lastRequestedTab, setLastRequestedTab] = createSignal<string | null>(null)
const [searchQuery, setSearchQuery] = createSignal("")
const [syncing, setSyncing] = createSignal(false)
const [searchResults, setSearchResults] = createSignal<FileNode[]>([])
const [searchLoading, setSearchLoading] = createSignal(false)
const [gitStatus, setGitStatus] = createSignal<{
@@ -322,6 +326,25 @@ export const Sidebar: Component<SidebarProps> = (props) => {
</Show>
<Show when={activeTab() === "sessions"}>
<div class="flex flex-col gap-1">
<div class="px-2 mb-2">
<button
onClick={async () => {
setSyncing(true)
try {
await syncSessionsFromSdk(props.instanceId)
} finally {
setSyncing(false)
}
}}
disabled={syncing()}
class="w-full flex items-center justify-center gap-2 px-3 py-2 text-xs font-semibold uppercase tracking-wide rounded-md bg-blue-500/10 text-blue-400 border border-blue-500/20 hover:bg-blue-500/20 disabled:opacity-50 transition-all"
>
<Show when={syncing()} fallback={<Download size={14} />}>
<RefreshCw size={14} class="animate-spin" />
</Show>
{syncing() ? "Syncing..." : "Sync SDK Sessions"}
</button>
</div>
<For each={props.sessions}>
{(session) => (
<div

View File

@@ -1153,6 +1153,27 @@ async function loadMessages(instanceId: string, sessionId: string, force = false
updateSessionInfo(instanceId, sessionId)
}
async function syncSessionsFromSdk(instanceId: string): Promise<void> {
const instance = instances().get(instanceId)
if (!instance) throw new Error("Instance not ready")
const folderPath = instance.folder
if (!folderPath) throw new Error("No folder path for instance")
log.info({ instanceId, folderPath }, "Manual SDK sync requested")
try {
const result = await nativeSessionApi.syncFromSdk(instanceId, folderPath)
log.info({ instanceId, result }, "Manual SDK sync result")
// Refresh sessions after sync
await fetchSessions(instanceId)
} catch (error) {
log.error({ instanceId, error }, "Manual SDK sync failed")
throw error
}
}
export {
createSession,
deleteSession,
@@ -1160,6 +1181,7 @@ export {
fetchProviders,
fetchSessions,
syncSessionsFromSdk,
forkSession,
loadMessages,
}

View File

@@ -35,6 +35,7 @@ import {
fetchAgents,
fetchProviders,
fetchSessions,
syncSessionsFromSdk,
forkSession,
loadMessages,
} from "./session-api"
@@ -88,6 +89,7 @@ export {
fetchAgents,
fetchProviders,
fetchSessions,
syncSessionsFromSdk,
forkSession,
getActiveParentSession,
getActiveSession,