Compare commits
2 Commits
610057c058
...
eaf93e2924
@@ -105,6 +105,42 @@ export function registerNativeSessionsRoutes(app: FastifyInstance, deps: NativeS
|
||||
}
|
||||
})
|
||||
|
||||
// Fork a session
|
||||
app.post<{
|
||||
Params: { workspaceId: string; sessionId: string }
|
||||
}>("/api/native/workspaces/:workspaceId/sessions/:sessionId/fork", async (request, reply) => {
|
||||
try {
|
||||
const session = await sessionManager.forkSession(
|
||||
request.params.workspaceId,
|
||||
request.params.sessionId
|
||||
)
|
||||
return { session }
|
||||
} catch (error) {
|
||||
logger.error({ error }, "Failed to fork session")
|
||||
reply.code(500)
|
||||
return { error: "Failed to fork session" }
|
||||
}
|
||||
})
|
||||
|
||||
// Revert a session
|
||||
app.post<{
|
||||
Params: { workspaceId: string; sessionId: string }
|
||||
Body: { messageId?: string }
|
||||
}>("/api/native/workspaces/:workspaceId/sessions/:sessionId/revert", async (request, reply) => {
|
||||
try {
|
||||
const session = await sessionManager.revert(
|
||||
request.params.workspaceId,
|
||||
request.params.sessionId,
|
||||
request.body.messageId
|
||||
)
|
||||
return { session }
|
||||
} catch (error) {
|
||||
logger.error({ error }, "Failed to revert session")
|
||||
reply.code(500)
|
||||
return { error: "Failed to revert session" }
|
||||
}
|
||||
})
|
||||
|
||||
// Delete a session
|
||||
app.delete<{ Params: { workspaceId: string; sessionId: string } }>("/api/native/workspaces/:workspaceId/sessions/:sessionId", async (request, reply) => {
|
||||
try {
|
||||
|
||||
@@ -200,6 +200,54 @@ export class NativeSessionManager {
|
||||
return true
|
||||
}
|
||||
|
||||
async forkSession(workspaceId: string, sessionId: string): Promise<Session> {
|
||||
const store = await this.loadStore(workspaceId)
|
||||
const original = store.sessions[sessionId]
|
||||
if (!original) throw new Error(`Session not found: ${sessionId}`)
|
||||
|
||||
const now = Date.now()
|
||||
const forked: Session = {
|
||||
...original,
|
||||
id: ulid(),
|
||||
title: original.title ? `${original.title} (fork)` : "Forked Session",
|
||||
parentId: original.parentId || original.id,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
messageIds: [...original.messageIds], // Shallow copy of message IDs
|
||||
}
|
||||
|
||||
store.sessions[forked.id] = forked
|
||||
await this.saveStore(workspaceId)
|
||||
return forked
|
||||
}
|
||||
|
||||
async revert(workspaceId: string, sessionId: string, messageId?: string): Promise<Session> {
|
||||
const store = await this.loadStore(workspaceId)
|
||||
const session = store.sessions[sessionId]
|
||||
if (!session) throw new Error(`Session not found: ${sessionId}`)
|
||||
|
||||
if (!messageId) {
|
||||
// Revert last message
|
||||
if (session.messageIds.length > 0) {
|
||||
const lastId = session.messageIds.pop()
|
||||
if (lastId) delete store.messages[lastId]
|
||||
}
|
||||
} else {
|
||||
// Revert to specific message
|
||||
const index = session.messageIds.indexOf(messageId)
|
||||
if (index !== -1) {
|
||||
const toDelete = session.messageIds.splice(index + 1)
|
||||
for (const id of toDelete) {
|
||||
delete store.messages[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
session.updatedAt = Date.now()
|
||||
await this.saveStore(workspaceId)
|
||||
return session
|
||||
}
|
||||
|
||||
// Message operations
|
||||
|
||||
async getSessionMessages(workspaceId: string, sessionId: string): Promise<SessionMessage[]> {
|
||||
|
||||
@@ -601,7 +601,7 @@ You are committed to excellence and take pride in delivering code that professio
|
||||
<div class="px-3 py-1.5 flex items-center justify-between border-t border-white/5 bg-zinc-950/30">
|
||||
<span class="text-[9px] font-bold text-zinc-500 uppercase tracking-widest">Saved Agents</span>
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); loadAgents(); fetchAgents(); }}
|
||||
onClick={(e) => { e.stopPropagation(); loadAgents(); fetchAgents(props.instanceId); }}
|
||||
class="p-1 hover:bg-white/5 rounded text-zinc-500 hover:text-zinc-300 transition-colors"
|
||||
title="Refresh agents"
|
||||
>
|
||||
|
||||
@@ -52,7 +52,7 @@ interface ToolCallProps {
|
||||
instanceId: string
|
||||
sessionId: string
|
||||
onContentRendered?: () => void
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -671,6 +671,7 @@ export default function ToolCall(props: ToolCallProps) {
|
||||
<Markdown
|
||||
part={markdownPart}
|
||||
isDark={isDark()}
|
||||
instanceId={props.instanceId}
|
||||
disableHighlight={disableHighlight}
|
||||
onRendered={handleMarkdownRendered}
|
||||
/>
|
||||
@@ -906,11 +907,11 @@ export default function ToolCall(props: ToolCallProps) {
|
||||
{expanded() && (
|
||||
<div class="tool-call-details">
|
||||
{renderToolBody()}
|
||||
|
||||
|
||||
{renderError()}
|
||||
|
||||
|
||||
{renderPermissionBlock()}
|
||||
|
||||
|
||||
<Show when={status() === "pending" && !pendingPermission()}>
|
||||
<div class="tool-call-pending-message">
|
||||
<span class="spinner-small"></span>
|
||||
@@ -919,7 +920,7 @@ export default function ToolCall(props: ToolCallProps) {
|
||||
</Show>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
<Show when={diagnosticsEntries().length}>
|
||||
|
||||
{renderDiagnosticsSection(
|
||||
|
||||
@@ -138,6 +138,26 @@ export const nativeSessionApi = {
|
||||
return response.ok || response.status === 204
|
||||
},
|
||||
|
||||
async forkSession(workspaceId: string, sessionId: string): Promise<NativeSession> {
|
||||
const response = await fetch(`${CODENOMAD_API_BASE}/api/native/workspaces/${encodeURIComponent(workspaceId)}/sessions/${encodeURIComponent(sessionId)}/fork`, {
|
||||
method: "POST"
|
||||
})
|
||||
if (!response.ok) throw new Error("Failed to fork session")
|
||||
const data = await response.json()
|
||||
return data.session
|
||||
},
|
||||
|
||||
async revertSession(workspaceId: string, sessionId: string, messageId?: string): Promise<NativeSession> {
|
||||
const response = await fetch(`${CODENOMAD_API_BASE}/api/native/workspaces/${encodeURIComponent(workspaceId)}/sessions/${encodeURIComponent(sessionId)}/revert`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ messageId })
|
||||
})
|
||||
if (!response.ok) throw new Error("Failed to revert session")
|
||||
const data = await response.json()
|
||||
return data.session
|
||||
},
|
||||
|
||||
async getMessages(workspaceId: string, sessionId: string): Promise<NativeMessage[]> {
|
||||
const response = await fetch(`${CODENOMAD_API_BASE}/api/native/workspaces/${encodeURIComponent(workspaceId)}/sessions/${encodeURIComponent(sessionId)}/messages`)
|
||||
if (!response.ok) throw new Error("Failed to get messages")
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
|
||||
import { createSignal, createMemo, batch } from "solid-js"
|
||||
import type { Session } from "../types/session"
|
||||
import type { Message, Part } from "../types/message"
|
||||
import { nativeSessionApi, isLiteMode, NativeSession, NativeMessage } from "../lib/lite-mode"
|
||||
import type { Message } from "../types/message"
|
||||
import { nativeSessionApi, isLiteMode } from "../lib/lite-mode"
|
||||
import type { NativeSession, NativeMessage } from "../lib/lite-mode"
|
||||
import { getLogger } from "../lib/logger"
|
||||
|
||||
const log = getLogger("native-sessions")
|
||||
@@ -53,24 +54,29 @@ export function forceLiteMode(enabled: boolean): void {
|
||||
}
|
||||
|
||||
// Convert native session to UI session format
|
||||
function nativeToUiSession(native: NativeSession): Session {
|
||||
function nativeToUiSession(native: NativeSession, workspaceId?: string): Session {
|
||||
return {
|
||||
id: native.id,
|
||||
title: native.title,
|
||||
parentId: native.parentId ?? undefined,
|
||||
createdAt: native.createdAt,
|
||||
updatedAt: native.updatedAt,
|
||||
agent: native.agent,
|
||||
instanceId: workspaceId || native.workspaceId,
|
||||
title: native.title || "",
|
||||
parentId: native.parentId ?? null,
|
||||
agent: native.agent || "Assistant",
|
||||
model: native.model ? {
|
||||
providerId: native.model.providerId,
|
||||
modelId: native.model.modelId,
|
||||
} : undefined,
|
||||
} : { providerId: "", modelId: "" },
|
||||
version: "0",
|
||||
time: {
|
||||
created: native.createdAt,
|
||||
updated: native.updatedAt
|
||||
},
|
||||
skills: []
|
||||
}
|
||||
}
|
||||
|
||||
// Convert native message to UI message format
|
||||
function nativeToUiMessage(native: NativeMessage): Message {
|
||||
const parts: Part[] = []
|
||||
const parts: any[] = []
|
||||
|
||||
if (native.content) {
|
||||
parts.push({
|
||||
@@ -82,19 +88,22 @@ function nativeToUiMessage(native: NativeMessage): Message {
|
||||
return {
|
||||
id: native.id,
|
||||
sessionId: native.sessionId,
|
||||
role: native.role,
|
||||
createdAt: native.createdAt,
|
||||
type: native.role === "user" ? "user" : "assistant",
|
||||
parts,
|
||||
timestamp: native.createdAt,
|
||||
status: native.status === "completed" ? "complete" : "streaming",
|
||||
version: 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetch sessions from native API
|
||||
*/
|
||||
export async function fetchNativeSessions(workspaceId: string): Promise<Session[]> {
|
||||
try {
|
||||
const sessions = await nativeSessionApi.listSessions(workspaceId)
|
||||
const uiSessions = sessions.map(nativeToUiSession)
|
||||
const uiSessions = sessions.map(s => nativeToUiSession(s, workspaceId))
|
||||
|
||||
// Update state
|
||||
setNativeSessions(prev => {
|
||||
@@ -227,9 +236,11 @@ export async function sendNativeMessage(
|
||||
const userMessage: Message = {
|
||||
id: `temp-${Date.now()}`,
|
||||
sessionId,
|
||||
role: "user",
|
||||
createdAt: Date.now(),
|
||||
parts: [{ type: "text", text: content }],
|
||||
type: "user",
|
||||
timestamp: Date.now(),
|
||||
parts: [{ type: "text", text: content } as any],
|
||||
status: "complete",
|
||||
version: 0
|
||||
}
|
||||
|
||||
const key = `${workspaceId}:${sessionId}`
|
||||
@@ -264,9 +275,11 @@ export async function sendNativeMessage(
|
||||
const assistantMessage: Message = {
|
||||
id: `msg-${Date.now()}`,
|
||||
sessionId,
|
||||
role: "assistant",
|
||||
createdAt: Date.now(),
|
||||
parts: [{ type: "text", text: fullContent }],
|
||||
type: "assistant",
|
||||
timestamp: Date.now(),
|
||||
parts: [{ type: "text", text: fullContent } as any],
|
||||
status: "complete",
|
||||
version: 0
|
||||
}
|
||||
|
||||
setNativeMessages(prev => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { instances, activeInstanceId } from "./instances"
|
||||
import { addTaskMessage } from "./task-actions"
|
||||
|
||||
import { addRecentModelPreference, setAgentModelPreference, getAgentModelPreference } from "./preferences"
|
||||
import { sessions, withSession, providers, setActiveParentSession, setActiveSession } from "./session-state"
|
||||
import { sessions, setSessions, withSession, providers, setActiveParentSession, setActiveSession } from "./session-state"
|
||||
import { getDefaultModel, isModelValid } from "./session-models"
|
||||
import { updateSessionInfo } from "./message-v2/session-info"
|
||||
import { messageStoreBus } from "./message-v2/bus"
|
||||
@@ -25,6 +25,8 @@ import { QwenOAuthManager } from "../lib/integrations/qwen-oauth"
|
||||
import { getUserScopedKey } from "../lib/user-storage"
|
||||
import { loadSkillDetails } from "./skills"
|
||||
import { serverApi } from "../lib/api-client"
|
||||
import { nativeSessionApi } from "../lib/lite-mode"
|
||||
import type { Session } from "../types/session"
|
||||
|
||||
const log = getLogger("actions")
|
||||
|
||||
@@ -1936,10 +1938,18 @@ async function updateSessionAgent(instanceId: string, sessionId: string, agent:
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
if (agent && shouldApplyModel && !agentModelPreference) {
|
||||
await setAgentModelPreference(instanceId, agent, nextModel)
|
||||
}
|
||||
|
||||
const instance = instances().get(instanceId)
|
||||
const isNative = instance?.binaryPath === "__nomadarch_native__"
|
||||
if (isNative) {
|
||||
await nativeSessionApi.updateSession(instanceId, sessionId, { agent })
|
||||
|
||||
}
|
||||
|
||||
if (shouldApplyModel) {
|
||||
updateSessionInfo(instanceId, sessionId)
|
||||
}
|
||||
@@ -1965,6 +1975,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 +2034,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 +2069,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 +2088,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 +2118,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
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Session, Provider, Model } from "../types/session"
|
||||
import type { Message } from "../types/message"
|
||||
|
||||
import { instances } from "./instances"
|
||||
import { nativeSessionApi } from "../lib/lite-mode"
|
||||
import { preferences, setAgentModelPreference, getAgentModelPreference } from "./preferences"
|
||||
import { setSessionCompactionState } from "./session-compaction"
|
||||
import {
|
||||
@@ -366,10 +367,16 @@ interface SessionForkResponse {
|
||||
|
||||
async function fetchSessions(instanceId: 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")
|
||||
}
|
||||
|
||||
setLoading((prev) => {
|
||||
const next = { ...prev }
|
||||
next.fetchingSessions.set(instanceId, true)
|
||||
@@ -377,13 +384,38 @@ async function fetchSessions(instanceId: string): Promise<void> {
|
||||
})
|
||||
|
||||
try {
|
||||
log.info("session.list", { instanceId })
|
||||
const response = await instance.client.session.list()
|
||||
log.info("session.list", { instanceId, isNative })
|
||||
|
||||
let responseData: any[] = []
|
||||
|
||||
if (isNative) {
|
||||
const nativeSessions = await nativeSessionApi.listSessions(instanceId)
|
||||
responseData = nativeSessions.map(s => ({
|
||||
id: s.id,
|
||||
title: s.title,
|
||||
parentID: s.parentId,
|
||||
version: "0",
|
||||
time: {
|
||||
created: s.createdAt,
|
||||
updated: s.updatedAt
|
||||
},
|
||||
model: s.model ? {
|
||||
providerID: s.model.providerId,
|
||||
modelID: s.model.modelId
|
||||
} : undefined,
|
||||
agent: s.agent
|
||||
}))
|
||||
} else {
|
||||
const response = await instance.client!.session.list()
|
||||
if (response.data && Array.isArray(response.data)) {
|
||||
responseData = response.data
|
||||
}
|
||||
}
|
||||
|
||||
const sessionMap = new Map<string, Session>()
|
||||
|
||||
if (!response.data || !Array.isArray(response.data)) {
|
||||
return
|
||||
if (responseData.length === 0 && !isNative) {
|
||||
// In SDK mode we still check response.data for empty
|
||||
}
|
||||
|
||||
const existingSessions = sessions().get(instanceId)
|
||||
@@ -394,13 +426,13 @@ async function fetchSessions(instanceId: string): Promise<void> {
|
||||
const sessionTasks = instanceData.sessionTasks || {}
|
||||
const sessionSkills = instanceData.sessionSkills || {}
|
||||
|
||||
for (const apiSession of response.data) {
|
||||
for (const apiSession of responseData) {
|
||||
const existingSession = existingSessions?.get(apiSession.id)
|
||||
|
||||
const existingModel = existingSession?.model ?? { providerId: "", modelId: "" }
|
||||
const hasUserSelectedModel = existingModel.providerId && existingModel.modelId
|
||||
const apiModel = (apiSession as any).model?.providerID && (apiSession as any).model?.modelID
|
||||
? { providerId: (apiSession as any).model.providerID, modelId: (apiSession as any).model.modelID }
|
||||
const apiModel = apiSession.model?.providerID && apiSession.model?.modelID
|
||||
? { providerId: apiSession.model.providerID, modelId: apiSession.model.modelID }
|
||||
: { providerId: "", modelId: "" }
|
||||
|
||||
sessionMap.set(apiSession.id, {
|
||||
@@ -408,7 +440,7 @@ async function fetchSessions(instanceId: string): Promise<void> {
|
||||
instanceId,
|
||||
title: apiSession.title || "Untitled",
|
||||
parentId: apiSession.parentID || null,
|
||||
agent: existingSession?.agent ?? (apiSession as any).agent ?? "",
|
||||
agent: existingSession?.agent ?? apiSession.agent ?? "",
|
||||
model: hasUserSelectedModel ? existingModel : apiModel,
|
||||
version: apiSession.version,
|
||||
time: {
|
||||
@@ -475,10 +507,15 @@ async function createSession(
|
||||
options?: { skipAutoCleanup?: boolean },
|
||||
): Promise<Session> {
|
||||
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 instanceAgents = agents().get(instanceId) || []
|
||||
const nonSubagents = instanceAgents.filter((a) => a.mode !== "subagent")
|
||||
const selectedAgent = agent || (nonSubagents.length > 0 ? nonSubagents[0].name : "")
|
||||
@@ -498,31 +535,57 @@ async function createSession(
|
||||
})
|
||||
|
||||
try {
|
||||
log.info(`[HTTP] POST /session.create for instance ${instanceId}`)
|
||||
const response = await instance.client.session.create()
|
||||
log.info(`[HTTP] POST session create for instance ${instanceId}, isNative: ${isNative}`)
|
||||
|
||||
if (!response.data) {
|
||||
let sessionData: any = null
|
||||
|
||||
if (isNative) {
|
||||
const native = await nativeSessionApi.createSession(instanceId, {
|
||||
agent: selectedAgent,
|
||||
model: sessionModel
|
||||
})
|
||||
sessionData = {
|
||||
id: native.id,
|
||||
title: native.title || "New Session",
|
||||
parentID: native.parentId,
|
||||
version: "0",
|
||||
time: {
|
||||
created: native.createdAt,
|
||||
updated: native.updatedAt
|
||||
},
|
||||
agent: native.agent,
|
||||
model: native.model ? {
|
||||
providerID: native.model.providerId,
|
||||
modelID: native.model.modelId
|
||||
} : undefined
|
||||
}
|
||||
} else {
|
||||
const response = await instance.client!.session.create()
|
||||
sessionData = response.data
|
||||
}
|
||||
|
||||
if (!sessionData) {
|
||||
throw new Error("Failed to create session: No data returned")
|
||||
}
|
||||
|
||||
const session: Session = {
|
||||
id: response.data.id,
|
||||
id: sessionData.id,
|
||||
instanceId,
|
||||
title: response.data.title || "New Session",
|
||||
title: sessionData.title || "New Session",
|
||||
parentId: null,
|
||||
agent: selectedAgent,
|
||||
model: sessionModel,
|
||||
skills: [],
|
||||
version: response.data.version,
|
||||
version: sessionData.version,
|
||||
time: {
|
||||
...response.data.time,
|
||||
...sessionData.time,
|
||||
},
|
||||
revert: response.data.revert
|
||||
revert: sessionData.revert
|
||||
? {
|
||||
messageID: response.data.revert.messageID,
|
||||
partID: response.data.revert.partID,
|
||||
snapshot: response.data.revert.snapshot,
|
||||
diff: response.data.revert.diff,
|
||||
messageID: sessionData.revert.messageID,
|
||||
partID: sessionData.revert.partID,
|
||||
snapshot: sessionData.revert.snapshot,
|
||||
diff: sessionData.revert.diff,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
@@ -683,9 +746,10 @@ async function forkSession(
|
||||
|
||||
async function deleteSession(instanceId: string, sessionId: string): Promise<void> {
|
||||
const instance = instances().get(instanceId)
|
||||
if (!instance || !instance.client) {
|
||||
throw new Error("Instance not ready")
|
||||
}
|
||||
if (!instance) return
|
||||
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (!isNative && !instance.client) return
|
||||
|
||||
setLoading((prev) => {
|
||||
const next = { ...prev }
|
||||
@@ -696,8 +760,13 @@ async function deleteSession(instanceId: string, sessionId: string): Promise<voi
|
||||
})
|
||||
|
||||
try {
|
||||
log.info(`[HTTP] DELETE /session.delete for instance ${instanceId}`, { sessionId })
|
||||
await instance.client.session.delete({ path: { id: sessionId } })
|
||||
log.info("session.delete", { instanceId, sessionId, isNative })
|
||||
|
||||
if (isNative) {
|
||||
await nativeSessionApi.deleteSession(instanceId, sessionId)
|
||||
} else {
|
||||
await instance.client!.session.delete({ path: { id: sessionId } })
|
||||
}
|
||||
|
||||
setSessions((prev) => {
|
||||
const next = new Map(prev)
|
||||
@@ -754,25 +823,42 @@ async function deleteSession(instanceId: string, sessionId: string): Promise<voi
|
||||
|
||||
async function fetchAgents(instanceId: 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")
|
||||
}
|
||||
|
||||
try {
|
||||
await ensureInstanceConfigLoaded(instanceId)
|
||||
log.info(`[HTTP] GET /app.agents for instance ${instanceId}`)
|
||||
const response = await instance.client.app.agents()
|
||||
const agentList = (response.data ?? []).map((agent) => ({
|
||||
name: agent.name,
|
||||
description: agent.description || "",
|
||||
mode: agent.mode,
|
||||
model: agent.model?.modelID
|
||||
? {
|
||||
providerId: agent.model.providerID || "",
|
||||
modelId: agent.model.modelID,
|
||||
}
|
||||
: undefined,
|
||||
}))
|
||||
log.info("agents.list", { instanceId, isNative })
|
||||
|
||||
let agentList: any[] = []
|
||||
|
||||
if (isNative) {
|
||||
// In native mode, we don't have agents from the SDK yet
|
||||
// We can return a default agent or common agents
|
||||
agentList = [{
|
||||
name: "Assistant",
|
||||
description: "Native assistant agent",
|
||||
mode: "native"
|
||||
}]
|
||||
} else {
|
||||
const response = await instance.client!.app.agents()
|
||||
agentList = (response.data || []).map((agent: any) => ({
|
||||
name: agent.name,
|
||||
description: agent.description || "",
|
||||
mode: agent.mode as "standard" | "subagent",
|
||||
model: agent.model
|
||||
? {
|
||||
providerId: agent.model.providerID || "",
|
||||
modelId: agent.model.modelID,
|
||||
}
|
||||
: undefined,
|
||||
}))
|
||||
}
|
||||
|
||||
const customAgents = getInstanceConfig(instanceId)?.customAgents ?? []
|
||||
const customList = customAgents.map((agent) => ({
|
||||
@@ -793,27 +879,43 @@ async function fetchAgents(instanceId: string): Promise<void> {
|
||||
|
||||
async function fetchProviders(instanceId: string): Promise<void> {
|
||||
const instance = instances().get(instanceId)
|
||||
if (!instance || !instance.client) {
|
||||
if (!instance) {
|
||||
throw new Error("Instance not ready")
|
||||
}
|
||||
|
||||
try {
|
||||
log.info(`[HTTP] GET /config.providers for instance ${instanceId}`)
|
||||
const response = await instance.client.config.providers()
|
||||
if (!response.data) return
|
||||
const isNative = instance.binaryPath === "__nomadarch_native__"
|
||||
if (!isNative && !instance.client) {
|
||||
throw new Error("Instance client not ready")
|
||||
}
|
||||
|
||||
const providerList = response.data.providers.map((provider) => ({
|
||||
id: provider.id,
|
||||
name: provider.name,
|
||||
defaultModelId: response.data?.default?.[provider.id],
|
||||
models: Object.entries(provider.models).map(([id, model]) => ({
|
||||
id,
|
||||
name: model.name,
|
||||
providerId: provider.id,
|
||||
limit: model.limit,
|
||||
cost: model.cost,
|
||||
})),
|
||||
}))
|
||||
try {
|
||||
log.info("config.providers", { instanceId, isNative })
|
||||
|
||||
let providerList: any[] = []
|
||||
let defaultProviders: any = {}
|
||||
|
||||
if (isNative) {
|
||||
// For native mode, we mainly rely on extra providers
|
||||
// but we could add "zen" (OpenCode Zen) if it's available via server API
|
||||
providerList = []
|
||||
} else {
|
||||
const response = await instance.client!.config.providers()
|
||||
if (response.data) {
|
||||
providerList = response.data.providers.map((provider) => ({
|
||||
id: provider.id,
|
||||
name: provider.name,
|
||||
defaultModelId: response.data?.default?.[provider.id],
|
||||
models: Object.entries(provider.models).map(([id, model]) => ({
|
||||
id,
|
||||
name: model.name,
|
||||
providerId: provider.id,
|
||||
limit: model.limit,
|
||||
cost: model.cost,
|
||||
})),
|
||||
}))
|
||||
defaultProviders = response.data.default || {}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter out Z.AI providers from SDK to use our custom routing with full message history
|
||||
const filteredBaseProviders = providerList.filter((provider) =>
|
||||
@@ -859,10 +961,15 @@ async function loadMessages(instanceId: string, sessionId: string, force = false
|
||||
}
|
||||
|
||||
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 instanceSessions = sessions().get(instanceId)
|
||||
const session = instanceSessions?.get(sessionId)
|
||||
if (!session) {
|
||||
@@ -878,15 +985,37 @@ async function loadMessages(instanceId: string, sessionId: string, force = false
|
||||
})
|
||||
|
||||
try {
|
||||
log.info(`[HTTP] GET /session.${"messages"} for instance ${instanceId}`, { sessionId })
|
||||
const response = await instance.client.session["messages"]({ path: { id: sessionId } })
|
||||
log.info("session.getMessages", { instanceId, sessionId, isNative })
|
||||
|
||||
if (!response.data || !Array.isArray(response.data)) {
|
||||
return
|
||||
let apiMessages: any[] = []
|
||||
let apiMessagesInfo: any = {}
|
||||
|
||||
if (isNative) {
|
||||
const nativeMessages = await nativeSessionApi.getMessages(instanceId, sessionId)
|
||||
apiMessages = nativeMessages.map(m => ({
|
||||
id: m.id,
|
||||
role: m.role,
|
||||
content: m.content || "",
|
||||
createdAt: m.createdAt,
|
||||
status: m.status,
|
||||
info: {
|
||||
id: m.id,
|
||||
role: m.role,
|
||||
time: { created: m.createdAt },
|
||||
// Add other native message properties to info if needed for later processing
|
||||
}
|
||||
}))
|
||||
} else {
|
||||
const response = await instance.client!.session.messages({ path: { id: sessionId } })
|
||||
if (!response.data || !Array.isArray(response.data)) {
|
||||
return
|
||||
}
|
||||
apiMessages = response.data || []
|
||||
apiMessagesInfo = (response as any).info || {} // Assuming 'info' might be on the response object itself for some cases
|
||||
}
|
||||
|
||||
const messagesInfo = new Map<string, any>()
|
||||
const messages: Message[] = response.data.map((apiMessage: any) => {
|
||||
const messages: Message[] = apiMessages.map((apiMessage: any) => {
|
||||
const info = apiMessage.info || apiMessage
|
||||
const role = info.role || "assistant"
|
||||
const messageId = info.id || String(Date.now())
|
||||
@@ -912,8 +1041,8 @@ async function loadMessages(instanceId: string, sessionId: string, force = false
|
||||
let providerID = ""
|
||||
let modelID = ""
|
||||
|
||||
for (let i = response.data.length - 1; i >= 0; i--) {
|
||||
const apiMessage = response.data[i]
|
||||
for (let i = apiMessages.length - 1; i >= 0; i--) {
|
||||
const apiMessage = apiMessages[i]
|
||||
const info = apiMessage.info || apiMessage
|
||||
|
||||
if (info.role === "assistant") {
|
||||
@@ -924,6 +1053,7 @@ async function loadMessages(instanceId: string, sessionId: string, force = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!agentName && !providerID && !modelID) {
|
||||
const defaultModel = await getDefaultModel(instanceId, session.agent)
|
||||
agentName = session.agent
|
||||
|
||||
Reference in New Issue
Block a user