Add auto-sync of SDK sessions: directly read from OpenCode storage and import to Native mode
Some checks failed
Release Binaries / release (push) Has been cancelled

This commit is contained in:
Gemini AI
2025-12-27 11:46:42 +04:00
Unverified
parent eca090d360
commit 251fad85b1
4 changed files with 268 additions and 5 deletions

View File

@@ -192,6 +192,43 @@ export const nativeSessionApi = {
return response.json()
},
/**
* Sync sessions from SDK (OpenCode) to Native mode
* This reads sessions directly from OpenCode's storage
*/
async syncFromSdk(workspaceId: string, folderPath: string): Promise<{
success: boolean
imported: number
skipped: number
total?: number
message?: string
}> {
const response = await fetch(`${CODENOMAD_API_BASE}/api/native/workspaces/${encodeURIComponent(workspaceId)}/sync-sdk`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ folderPath })
})
if (!response.ok) throw new Error("Failed to sync SDK sessions")
return response.json()
},
/**
* Check if SDK sessions exist for a folder
*/
async checkSdkSessions(folderPath: string): Promise<{
found: boolean
count: number
sessions: Array<{ id: string; title: string; created: number }>
}> {
const response = await fetch(`${CODENOMAD_API_BASE}/api/native/check-sdk-sessions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ folderPath })
})
if (!response.ok) throw new Error("Failed to check SDK sessions")
return response.json()
},
/**
* Send a prompt to the session and get a streaming response

View File

@@ -390,15 +390,28 @@ async function fetchSessions(instanceId: string): Promise<void> {
let responseData: any[] = []
if (isNative) {
// Auto-import cached SDK sessions on native mode startup
// Auto-sync SDK sessions from OpenCode's storage on native mode startup
if (needsMigration(instanceId)) {
try {
const result = await autoImportCachedSessions(instanceId)
if (result.imported > 0) {
log.info({ instanceId, result }, "Auto-imported SDK sessions to native mode")
// First try to sync directly from OpenCode's storage (most reliable)
const folderPath = instance.folder
if (folderPath) {
log.info({ instanceId, folderPath }, "Syncing SDK sessions from OpenCode storage")
const syncResult = await nativeSessionApi.syncFromSdk(instanceId, folderPath)
if (syncResult.imported > 0) {
log.info({ instanceId, syncResult }, "Synced SDK sessions from OpenCode storage")
} else if (syncResult.message) {
log.info({ instanceId, message: syncResult.message }, "SDK sync info")
}
}
// Also try the localStorage cache as fallback
const cacheResult = await autoImportCachedSessions(instanceId)
if (cacheResult.imported > 0) {
log.info({ instanceId, cacheResult }, "Auto-imported cached SDK sessions")
}
} catch (error) {
log.error({ instanceId, error }, "Failed to auto-import SDK sessions")
log.error({ instanceId, error }, "Failed to sync SDK sessions")
markMigrated(instanceId) // Mark as migrated to prevent repeated failures
}
}