Add automatic session migration when switching from SDK to Native mode
Some checks failed
Release Binaries / release (push) Has been cancelled
Some checks failed
Release Binaries / release (push) Has been cancelled
This commit is contained in:
@@ -158,6 +158,42 @@ export function registerNativeSessionsRoutes(app: FastifyInstance, deps: NativeS
|
||||
}
|
||||
})
|
||||
|
||||
// Import sessions from SDK mode - for migration when switching to native mode
|
||||
app.post<{
|
||||
Params: { workspaceId: string }
|
||||
Body: {
|
||||
sessions: Array<{
|
||||
id: string
|
||||
title?: string
|
||||
parentId?: string | null
|
||||
createdAt?: number
|
||||
updatedAt?: number
|
||||
model?: { providerId: string; modelId: string }
|
||||
agent?: string
|
||||
messages?: Array<{
|
||||
id: string
|
||||
role: "user" | "assistant" | "system" | "tool"
|
||||
content?: string
|
||||
createdAt?: number
|
||||
}>
|
||||
}>
|
||||
}
|
||||
}>("/api/native/workspaces/:workspaceId/sessions/import", async (request, reply) => {
|
||||
try {
|
||||
const result = await sessionManager.importSessions(
|
||||
request.params.workspaceId,
|
||||
request.body.sessions
|
||||
)
|
||||
logger.info({ workspaceId: request.params.workspaceId, ...result }, "Sessions imported from SDK mode")
|
||||
return { success: true, ...result }
|
||||
} catch (error) {
|
||||
logger.error({ error }, "Failed to import sessions")
|
||||
reply.code(500)
|
||||
return { error: "Failed to import sessions" }
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// Get messages for a session
|
||||
app.get<{ Params: { workspaceId: string; sessionId: string } }>("/api/native/workspaces/:workspaceId/sessions/:sessionId/messages", async (request, reply) => {
|
||||
try {
|
||||
|
||||
@@ -311,6 +311,74 @@ export class NativeSessionManager {
|
||||
const store = this.stores.get(workspaceId)
|
||||
return store ? Object.keys(store.sessions).length : 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Import sessions from SDK mode format - for migration when switching modes
|
||||
*/
|
||||
async importSessions(workspaceId: string, sessions: Array<{
|
||||
id: string
|
||||
title?: string
|
||||
parentId?: string | null
|
||||
createdAt?: number
|
||||
updatedAt?: number
|
||||
model?: { providerId: string; modelId: string }
|
||||
agent?: string
|
||||
messages?: Array<{
|
||||
id: string
|
||||
role: "user" | "assistant" | "system" | "tool"
|
||||
content?: string
|
||||
createdAt?: number
|
||||
}>
|
||||
}>): Promise<{ imported: number; skipped: number }> {
|
||||
const store = await this.loadStore(workspaceId)
|
||||
let imported = 0
|
||||
let skipped = 0
|
||||
|
||||
for (const sdkSession of sessions) {
|
||||
// Skip if session already exists
|
||||
if (store.sessions[sdkSession.id]) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
const session: Session = {
|
||||
id: sdkSession.id,
|
||||
workspaceId,
|
||||
title: sdkSession.title || "Imported Session",
|
||||
parentId: sdkSession.parentId ?? null,
|
||||
createdAt: sdkSession.createdAt || now,
|
||||
updatedAt: sdkSession.updatedAt || now,
|
||||
messageIds: [],
|
||||
model: sdkSession.model,
|
||||
agent: sdkSession.agent,
|
||||
}
|
||||
|
||||
// Import messages if provided
|
||||
if (sdkSession.messages && Array.isArray(sdkSession.messages)) {
|
||||
for (const msg of sdkSession.messages) {
|
||||
const message: SessionMessage = {
|
||||
id: msg.id,
|
||||
sessionId: sdkSession.id,
|
||||
role: msg.role,
|
||||
content: msg.content,
|
||||
createdAt: msg.createdAt || now,
|
||||
updatedAt: msg.createdAt || now,
|
||||
status: "completed"
|
||||
}
|
||||
store.messages[msg.id] = message
|
||||
session.messageIds.push(msg.id)
|
||||
}
|
||||
}
|
||||
|
||||
store.sessions[sdkSession.id] = session
|
||||
imported++
|
||||
}
|
||||
|
||||
await this.saveStore(workspaceId)
|
||||
log.info({ workspaceId, imported, skipped }, "Imported sessions from SDK mode")
|
||||
return { imported, skipped }
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
|
||||
Reference in New Issue
Block a user