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() {
if (!open.value && agents.value.length === 0) {
loadAgents();
}
open.value = !open.value;
}
@ -56,6 +53,7 @@ function handleClickOutside(e: MouseEvent) {
}
onMounted(() => {
loadAgents();
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,
);
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;
}

BIN
packages/drizzle-pkg/db.sqlite

Binary file not shown.
Loading…
Cancel
Save