46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
/**
|
|
* features/chat/composables/useChatHistory.ts
|
|
* 跨 Agent 聊天历史列表(工作台 History 页 / 移动端 Tab)。
|
|
*/
|
|
import { ref } from 'vue';
|
|
import { getAuthHeaders } from '@/shared/utils/auth';
|
|
import { requestJson } from '@/shared/api/client';
|
|
import type { ChatCrossAgentHistoryItem } from '../types/chat.types';
|
|
|
|
export function useChatHistory() {
|
|
const chatHistories = ref<ChatCrossAgentHistoryItem[]>([]);
|
|
const isLoadingHistories = ref(false);
|
|
|
|
const loadChatHistories = async () => {
|
|
isLoadingHistories.value = true;
|
|
try {
|
|
const agentList = await requestJson<Array<{ id: number; name: string }>>('/api/agents', {
|
|
headers: getAuthHeaders(),
|
|
});
|
|
const allHistories: ChatCrossAgentHistoryItem[] = [];
|
|
await Promise.all(
|
|
(Array.isArray(agentList) ? agentList : []).map(async (agent) => {
|
|
try {
|
|
const sessions = await requestJson<ChatCrossAgentHistoryItem[]>(
|
|
`/api/chat/sessions?agent_id=${agent.id}`,
|
|
{ headers: getAuthHeaders() },
|
|
);
|
|
for (const s of Array.isArray(sessions) ? sessions : []) {
|
|
allHistories.push({ ...s, agent_id: agent.id, agent_name: agent.name });
|
|
}
|
|
} catch {
|
|
// ignore per-agent failures
|
|
}
|
|
}),
|
|
);
|
|
allHistories.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
|
|
chatHistories.value = allHistories;
|
|
} catch {
|
|
// ignore
|
|
}
|
|
isLoadingHistories.value = false;
|
|
};
|
|
|
|
return { chatHistories, isLoadingHistories, loadChatHistories };
|
|
}
|