Files
Agent-System/web/src/api/chatSessions.ts
T
weichenandCursor 47984f4863 fix(frontend): gate data fetches until session validated and improve workflow UX
Prevent login-page 401 noise by deferring project/chat API calls until auth is confirmed, use public-config for registration policy, and add client-side duty assignment checks before saving workflow plans.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 02:50:23 +00:00

57 lines
1.4 KiB
TypeScript

import { getBearerAuthHeaders } from './authHeaders';
export interface ChatSessionSummary {
session_id: string;
title: string;
user_id: number;
project_id?: number;
project_name?: string;
message_count: number;
last_message?: string;
updated_at: string;
}
export interface UpsertChatSessionInput {
session_id: string;
title: string;
user_id: number;
project_id: number;
}
const API_BASE = '';
export async function fetchChatSessions(
userId: number,
projectId?: number,
limit = 100
): Promise<ChatSessionSummary[]> {
const params = new URLSearchParams({
user_id: String(userId),
limit: String(limit),
});
if (projectId != null && projectId > 0) {
params.set('project_id', String(projectId));
}
const res = await fetch(`${API_BASE}/api/v1/chat/sessions?${params.toString()}`, {
headers: getBearerAuthHeaders(),
});
if (!res.ok) {
throw new Error(`获取会话列表失败: ${res.status}`);
}
const data = await res.json();
return Array.isArray(data) ? data : [];
}
export async function upsertChatSession(input: UpsertChatSessionInput): Promise<ChatSessionSummary> {
const res = await fetch(`${API_BASE}/api/v1/chat/sessions`, {
method: 'POST',
headers: getBearerAuthHeaders('application/json'),
body: JSON.stringify(input),
});
if (!res.ok) {
const text = await res.text();
throw new Error(text || `注册会话失败: ${res.status}`);
}
return res.json();
}