|
|
|
@ -28,6 +28,16 @@ export function useAgentSessions(agentSlug: string) { |
|
|
|
sessions.value.find((s) => s.id === currentSessionId.value) ?? null, |
|
|
|
); |
|
|
|
|
|
|
|
function syncSessionToUrl(id: string | null) { |
|
|
|
if (import.meta.server) return; |
|
|
|
const route = useRoute(); |
|
|
|
const router = useRouter(); |
|
|
|
const current = route.query.session; |
|
|
|
const target = id ?? undefined; |
|
|
|
if (current === target) return; |
|
|
|
router.replace({ query: { ...route.query, session: target } }); |
|
|
|
} |
|
|
|
|
|
|
|
async function fetchSessions() { |
|
|
|
loading.value = true; |
|
|
|
try { |
|
|
|
@ -62,17 +72,20 @@ export function useAgentSessions(agentSlug: string) { |
|
|
|
|
|
|
|
async function selectSession(id: string) { |
|
|
|
currentSessionId.value = id; |
|
|
|
syncSessionToUrl(id); |
|
|
|
} |
|
|
|
|
|
|
|
async function newSession(): Promise<AgentSessionItem | null> { |
|
|
|
const latest = sessions.value[0]; |
|
|
|
if (latest && latest.lastActiveAt === latest.createdAt) { |
|
|
|
currentSessionId.value = latest.id; |
|
|
|
syncSessionToUrl(latest.id); |
|
|
|
return latest; |
|
|
|
} |
|
|
|
const session = await createSession(); |
|
|
|
if (session) { |
|
|
|
currentSessionId.value = session.id; |
|
|
|
syncSessionToUrl(session.id); |
|
|
|
} |
|
|
|
return session; |
|
|
|
} |
|
|
|
@ -96,12 +109,15 @@ export function useAgentSessions(agentSlug: string) { |
|
|
|
if (currentSessionId.value === id) { |
|
|
|
if (sessions.value.length > 0) { |
|
|
|
currentSessionId.value = sessions.value[0]?.id ?? null; |
|
|
|
syncSessionToUrl(currentSessionId.value); |
|
|
|
} else { |
|
|
|
const session = await createSession(); |
|
|
|
if (session) { |
|
|
|
currentSessionId.value = session.id; |
|
|
|
syncSessionToUrl(session.id); |
|
|
|
} else { |
|
|
|
currentSessionId.value = null; |
|
|
|
syncSessionToUrl(null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -162,10 +178,16 @@ export function useAgentSessions(agentSlug: string) { |
|
|
|
if (initialized.value) return; |
|
|
|
await auth.refresh(); |
|
|
|
await fetchSessions(); |
|
|
|
if (sessions.value.length === 0) { |
|
|
|
const route = useRoute(); |
|
|
|
const urlSessionId = route.query.session; |
|
|
|
const urlSessionStr = typeof urlSessionId === "string" ? urlSessionId : null; |
|
|
|
if (urlSessionStr && sessions.value.some((s) => s.id === urlSessionStr)) { |
|
|
|
currentSessionId.value = urlSessionStr; |
|
|
|
} else if (sessions.value.length === 0) { |
|
|
|
await newSession(); |
|
|
|
} else { |
|
|
|
currentSessionId.value = sessions.value[0]?.id ?? null; |
|
|
|
syncSessionToUrl(currentSessionId.value); |
|
|
|
} |
|
|
|
initialized.value = true; |
|
|
|
} |
|
|
|
|