Fix Native Mode Sessions: implemented fork, revert, and sync for native sessions
This commit is contained in:
@@ -1940,6 +1940,11 @@ async function updateSessionAgent(instanceId: string, sessionId: string, agent:
|
||||
await setAgentModelPreference(instanceId, agent, nextModel)
|
||||
}
|
||||
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (isNative) {
|
||||
await nativeSessionApi.updateSession(instanceId, sessionId, { agent })
|
||||
}
|
||||
|
||||
if (shouldApplyModel) {
|
||||
updateSessionInfo(instanceId, sessionId)
|
||||
}
|
||||
@@ -1965,6 +1970,16 @@ async function updateSessionModel(
|
||||
current.model = model
|
||||
})
|
||||
|
||||
const instance = instances().get(instanceId)
|
||||
if (instance?.binaryPath === "__nomadarch_native__") {
|
||||
await nativeSessionApi.updateSession(instanceId, sessionId, {
|
||||
model: {
|
||||
providerId: model.providerId,
|
||||
modelId: model.modelId
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const propagateModel = (targetSessionId?: string | null) => {
|
||||
if (!targetSessionId || targetSessionId === sessionId) return
|
||||
withSession(instanceId, targetSessionId, (current) => {
|
||||
@@ -2014,16 +2029,31 @@ async function updateSessionModelForSession(
|
||||
current.model = model
|
||||
})
|
||||
|
||||
const instance = instances().get(instanceId)
|
||||
if (instance?.binaryPath === "__nomadarch_native__") {
|
||||
await nativeSessionApi.updateSession(instanceId, sessionId, {
|
||||
model: {
|
||||
providerId: model.providerId,
|
||||
modelId: model.modelId
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
addRecentModelPreference(model)
|
||||
updateSessionInfo(instanceId, sessionId)
|
||||
}
|
||||
|
||||
async function renameSession(instanceId: string, sessionId: string, nextTitle: string): Promise<void> {
|
||||
const instance = instances().get(instanceId)
|
||||
if (!instance || !instance.client) {
|
||||
if (!instance) {
|
||||
throw new Error("Instance not ready")
|
||||
}
|
||||
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (!isNative && !instance.client) {
|
||||
throw new Error("Instance client not ready")
|
||||
}
|
||||
|
||||
const session = sessions().get(instanceId)?.get(sessionId)
|
||||
if (!session) {
|
||||
throw new Error("Session not found")
|
||||
@@ -2034,10 +2064,14 @@ async function renameSession(instanceId: string, sessionId: string, nextTitle: s
|
||||
throw new Error("Session title is required")
|
||||
}
|
||||
|
||||
await instance.client.session.update({
|
||||
path: { id: sessionId },
|
||||
body: { title: trimmedTitle },
|
||||
})
|
||||
if (isNative) {
|
||||
await nativeSessionApi.updateSession(instanceId, sessionId, { title: trimmedTitle })
|
||||
} else {
|
||||
await instance.client!.session.update({
|
||||
path: { id: sessionId },
|
||||
body: { title: trimmedTitle },
|
||||
})
|
||||
}
|
||||
|
||||
withSession(instanceId, sessionId, (current) => {
|
||||
current.title = trimmedTitle
|
||||
@@ -2049,19 +2083,28 @@ async function renameSession(instanceId: string, sessionId: string, nextTitle: s
|
||||
|
||||
async function revertSession(instanceId: string, sessionId: string): Promise<void> {
|
||||
const instance = instances().get(instanceId)
|
||||
if (!instance || !instance.client) {
|
||||
if (!instance) {
|
||||
throw new Error("Instance not ready")
|
||||
}
|
||||
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (!isNative && !instance.client) {
|
||||
throw new Error("Instance client not ready")
|
||||
}
|
||||
|
||||
const session = sessions().get(instanceId)?.get(sessionId)
|
||||
if (!session) {
|
||||
throw new Error("Session not found")
|
||||
}
|
||||
|
||||
try {
|
||||
await instance.client.session.revert({
|
||||
path: { id: sessionId },
|
||||
})
|
||||
if (isNative) {
|
||||
await nativeSessionApi.revertSession(instanceId, sessionId)
|
||||
} else {
|
||||
await instance.client!.session.revert({
|
||||
path: { id: sessionId },
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
log.error("Failed to revert session", error)
|
||||
throw error
|
||||
@@ -2070,30 +2113,76 @@ async function revertSession(instanceId: string, sessionId: string): Promise<voi
|
||||
|
||||
async function forkSession(instanceId: string, sessionId: string): Promise<string> {
|
||||
const instance = instances().get(instanceId)
|
||||
if (!instance || !instance.client) {
|
||||
if (!instance) {
|
||||
throw new Error("Instance not ready")
|
||||
}
|
||||
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (!isNative && !instance.client) {
|
||||
throw new Error("Instance client not ready")
|
||||
}
|
||||
|
||||
const session = sessions().get(instanceId)?.get(sessionId)
|
||||
if (!session) {
|
||||
throw new Error("Session not found")
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await instance.client.session.fork({
|
||||
path: { id: sessionId },
|
||||
})
|
||||
let forkedId: string = ""
|
||||
let forkedVersion: string = "0"
|
||||
let forkedTime: any = { created: Date.now(), updated: Date.now() }
|
||||
let forkedRevert: any = undefined
|
||||
|
||||
if (response.error) {
|
||||
throw new Error(JSON.stringify(response.error) || "Failed to fork session")
|
||||
if (isNative) {
|
||||
const response = await nativeSessionApi.forkSession(instanceId, sessionId)
|
||||
forkedId = response.id
|
||||
forkedTime = { created: response.createdAt, updated: response.updatedAt }
|
||||
} else {
|
||||
const response = await instance.client!.session.fork({
|
||||
path: { id: sessionId },
|
||||
})
|
||||
if (!response.data) {
|
||||
throw new Error("Failed to fork session: No data returned")
|
||||
}
|
||||
forkedId = response.data.id
|
||||
forkedVersion = response.data.version
|
||||
forkedTime = response.data.time
|
||||
forkedRevert = response.data.revert
|
||||
? {
|
||||
messageID: response.data.revert.messageID,
|
||||
partID: response.data.revert.partID,
|
||||
snapshot: response.data.revert.snapshot,
|
||||
diff: response.data.revert.diff,
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
|
||||
const newSessionId = response.data?.id
|
||||
if (!newSessionId) {
|
||||
if (!forkedId) {
|
||||
throw new Error("No session ID returned from fork operation")
|
||||
}
|
||||
|
||||
return newSessionId
|
||||
const forkedSession: Session = {
|
||||
id: forkedId,
|
||||
instanceId,
|
||||
title: session.title ? `${session.title} (fork)` : "Forked Session",
|
||||
parentId: session.parentId || session.id,
|
||||
agent: session.agent,
|
||||
model: session.model,
|
||||
skills: [...session.skills],
|
||||
version: forkedVersion,
|
||||
time: forkedTime,
|
||||
revert: forkedRevert
|
||||
}
|
||||
|
||||
setSessions((prev) => {
|
||||
const next = new Map(prev)
|
||||
const instanceSessions = new Map(next.get(instanceId) || [])
|
||||
instanceSessions.set(forkedSession.id, forkedSession)
|
||||
next.set(instanceId, instanceSessions)
|
||||
return next
|
||||
})
|
||||
|
||||
return forkedId
|
||||
} catch (error) {
|
||||
log.error("Failed to fork session", error)
|
||||
throw error
|
||||
|
||||
Reference in New Issue
Block a user