import { ref, computed } from "vue"; import { useAuthSession } from "./useAuthSession"; export interface AgentSessionItem { id: string; userId: number | null; tempToken: string | null; title: string; modelId: number | null; enableThinking: number; enableTools: number; lastActiveAt: string; expiresAt: string | null; deletedAt: string | null; createdAt: string; updatedAt: string; } export function useAgentSessions() { const auth = useAuthSession(); const sessions = ref([]); const currentSessionId = ref(null); const loading = ref(false); const initialized = ref(false); const currentSession = computed(() => sessions.value.find((s) => s.id === currentSessionId.value) ?? null, ); async function fetchSessions() { loading.value = true; try { const res = await $fetch<{ code: number; data: { list: AgentSessionItem[]; total: number } }>( "/api/agent/sessions", { method: "GET" }, ); sessions.value = res.data?.list ?? []; } catch { sessions.value = []; } finally { loading.value = false; } } async function createSession(): Promise { try { const res = await $fetch<{ code: number; data: AgentSessionItem }>( "/api/agent/sessions", { method: "POST" }, ); const session = res.data; if (session) { sessions.value.unshift(session); return session; } return null; } catch { return null; } } async function selectSession(id: string) { currentSessionId.value = id; } async function newSession(): Promise { const session = await createSession(); if (session) { currentSessionId.value = session.id; } return session; } async function renameSession(id: string, title: string) { try { await $fetch(`/api/agent/sessions/${id}`, { method: "PUT", body: { title }, }); const s = sessions.value.find((x) => x.id === id); if (s) s.title = title; } catch { } } async function deleteSession(id: string) { try { await $fetch(`/api/agent/sessions/${id}`, { method: "DELETE" }); sessions.value = sessions.value.filter((s) => s.id !== id); if (currentSessionId.value === id) { currentSessionId.value = sessions.value[0]?.id ?? null; } } catch { } } async function updateSessionConfig( id: string, config: { modelId?: number | null; enableThinking?: boolean; enableTools?: boolean }, ) { try { await $fetch(`/api/agent/sessions/${id}/config`, { method: "PUT", body: config, }); const s = sessions.value.find((x) => x.id === id); if (s) { if (config.modelId !== undefined) s.modelId = config.modelId; if (config.enableThinking !== undefined) s.enableThinking = config.enableThinking ? 1 : 0; if (config.enableTools !== undefined) s.enableTools = config.enableTools ? 1 : 0; } } catch { } } async function refreshSessionTitle(id: string) { try { const res = await $fetch<{ code: number; data: { session: AgentSessionItem } }>( `/api/agent/sessions/${id}`, { method: "GET" }, ); if (res.data?.session) { const s = sessions.value.find((x) => x.id === id); if (s && s.title !== res.data.session.title) { s.title = res.data.session.title; } } } catch { } } function touchSessionOrder(id: string) { const idx = sessions.value.findIndex((s) => s.id === id); if (idx > 0) { const [s] = sessions.value.splice(idx, 1); if (s) { s.lastActiveAt = new Date().toISOString(); sessions.value.unshift(s); } } else if (idx === 0 && sessions.value[0]) { sessions.value[0].lastActiveAt = new Date().toISOString(); } } async function init() { if (initialized.value) return; await auth.refresh(); await fetchSessions(); if (sessions.value.length === 0) { await newSession(); } else { currentSessionId.value = sessions.value[0]?.id ?? null; } initialized.value = true; } return { sessions, currentSessionId, currentSession, loading, initialized, fetchSessions, createSession, newSession, selectSession, renameSession, deleteSession, updateSessionConfig, touchSessionOrder, refreshSessionTitle, init, }; }