You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
4.9 KiB
188 lines
4.9 KiB
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<AgentSessionItem[]>([]);
|
|
const currentSessionId = ref<string | null>(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<AgentSessionItem | null> {
|
|
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<AgentSessionItem | null> {
|
|
const latest = sessions.value[0];
|
|
if (latest && latest.lastActiveAt === latest.createdAt) {
|
|
currentSessionId.value = latest.id;
|
|
return latest;
|
|
}
|
|
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) {
|
|
if (sessions.value.length > 0) {
|
|
currentSessionId.value = sessions.value[0]?.id ?? null;
|
|
} else {
|
|
const session = await createSession();
|
|
if (session) {
|
|
currentSessionId.value = session.id;
|
|
} else {
|
|
currentSessionId.value = 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,
|
|
};
|
|
}
|
|
|