Browse Source

feat: 在组件加载时自动获取代理列表,优化代理切换逻辑

feat/agent-independence
npmrun 2 months ago
parent
commit
80484260aa
  1. 4
      app/components/agent/AgentSwitcher.vue
  2. 24
      app/composables/useAgentSessions.ts
  3. BIN
      packages/drizzle-pkg/db.sqlite

4
app/components/agent/AgentSwitcher.vue

@ -36,9 +36,6 @@ async function loadAgents() {
} }
function toggle() { function toggle() {
if (!open.value && agents.value.length === 0) {
loadAgents();
}
open.value = !open.value; open.value = !open.value;
} }
@ -56,6 +53,7 @@ function handleClickOutside(e: MouseEvent) {
} }
onMounted(() => { onMounted(() => {
loadAgents();
document.addEventListener("click", handleClickOutside); document.addEventListener("click", handleClickOutside);
}); });

24
app/composables/useAgentSessions.ts

@ -28,6 +28,16 @@ export function useAgentSessions(agentSlug: string) {
sessions.value.find((s) => s.id === currentSessionId.value) ?? null, 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() { async function fetchSessions() {
loading.value = true; loading.value = true;
try { try {
@ -62,17 +72,20 @@ export function useAgentSessions(agentSlug: string) {
async function selectSession(id: string) { async function selectSession(id: string) {
currentSessionId.value = id; currentSessionId.value = id;
syncSessionToUrl(id);
} }
async function newSession(): Promise<AgentSessionItem | null> { async function newSession(): Promise<AgentSessionItem | null> {
const latest = sessions.value[0]; const latest = sessions.value[0];
if (latest && latest.lastActiveAt === latest.createdAt) { if (latest && latest.lastActiveAt === latest.createdAt) {
currentSessionId.value = latest.id; currentSessionId.value = latest.id;
syncSessionToUrl(latest.id);
return latest; return latest;
} }
const session = await createSession(); const session = await createSession();
if (session) { if (session) {
currentSessionId.value = session.id; currentSessionId.value = session.id;
syncSessionToUrl(session.id);
} }
return session; return session;
} }
@ -96,12 +109,15 @@ export function useAgentSessions(agentSlug: string) {
if (currentSessionId.value === id) { if (currentSessionId.value === id) {
if (sessions.value.length > 0) { if (sessions.value.length > 0) {
currentSessionId.value = sessions.value[0]?.id ?? null; currentSessionId.value = sessions.value[0]?.id ?? null;
syncSessionToUrl(currentSessionId.value);
} else { } else {
const session = await createSession(); const session = await createSession();
if (session) { if (session) {
currentSessionId.value = session.id; currentSessionId.value = session.id;
syncSessionToUrl(session.id);
} else { } else {
currentSessionId.value = null; currentSessionId.value = null;
syncSessionToUrl(null);
} }
} }
} }
@ -162,10 +178,16 @@ export function useAgentSessions(agentSlug: string) {
if (initialized.value) return; if (initialized.value) return;
await auth.refresh(); await auth.refresh();
await fetchSessions(); 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(); await newSession();
} else { } else {
currentSessionId.value = sessions.value[0]?.id ?? null; currentSessionId.value = sessions.value[0]?.id ?? null;
syncSessionToUrl(currentSessionId.value);
} }
initialized.value = true; initialized.value = true;
} }

BIN
packages/drizzle-pkg/db.sqlite

Binary file not shown.
Loading…
Cancel
Save