Route web/src/api modules through shared apiFetch so Bearer headers, typed error envelopes, and GET/HEAD transient retries live in one place without swallowing domain failures used by existing error cards.
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { ChatSearchHit, ChatSearchMode } from '../types/workbench';
|
|
import { mergeAuthHeaders } from './authHeaders';
|
|
import { apiFetch } from './client';
|
|
|
|
export interface SearchChatMessagesParams {
|
|
sessionId: string;
|
|
q: string;
|
|
mode?: ChatSearchMode;
|
|
category?: string;
|
|
dateFrom?: string;
|
|
dateTo?: string;
|
|
limit?: number;
|
|
}
|
|
|
|
export async function searchChatMessages(params: SearchChatMessagesParams): Promise<ChatSearchHit[]> {
|
|
const url = new URL('/api/v1/chat/search', window.location.origin);
|
|
url.searchParams.set('session_id', params.sessionId);
|
|
url.searchParams.set('q', params.q);
|
|
|
|
if (params.mode) {
|
|
url.searchParams.set('mode', params.mode);
|
|
}
|
|
if (params.category && params.category !== 'all') {
|
|
url.searchParams.set('category', params.category);
|
|
}
|
|
if (params.dateFrom) {
|
|
url.searchParams.set('date_from', params.dateFrom);
|
|
}
|
|
if (params.dateTo) {
|
|
url.searchParams.set('date_to', params.dateTo);
|
|
}
|
|
if (params.limit) {
|
|
url.searchParams.set('limit', String(params.limit));
|
|
}
|
|
|
|
const res = await apiFetch(url.toString(), {
|
|
method: 'GET',
|
|
headers: mergeAuthHeaders({ Accept: 'application/json' }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`searchChatMessages failed: HTTP ${res.status}`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
return Array.isArray(data) ? data : [];
|
|
}
|