nexus/ui/src/api/chat.ts
Mikkel Georgsen 2a0724839b feat(21-03): add chat API client, ChatPanelProvider, and TanStack Query hooks
- Create chatApi with full CRUD + archive/pin/message endpoints
- Create ChatPanelContext with localStorage persistence (nexus:chat-panel-open)
- Create useChatConversations with useInfiniteQuery and conversation mutations
- Create useChatMessages with useInfiniteQuery and useSendMessage
- Wrap app tree with ChatPanelProvider in main.tsx
2026-04-01 13:08:27 +02:00

37 lines
2 KiB
TypeScript

import type { ChatConversation, ChatConversationListResponse, ChatMessage } from "@paperclipai/shared";
import { api } from "./client";
export const chatApi = {
listConversations: (companyId: string, opts?: { cursor?: string; limit?: number }) => {
const params = new URLSearchParams();
if (opts?.cursor) params.set("cursor", opts.cursor);
if (opts?.limit) params.set("limit", String(opts.limit));
const qs = params.toString();
return api.get<ChatConversationListResponse>(`/api/companies/${companyId}/conversations${qs ? `?${qs}` : ""}`);
},
createConversation: (companyId: string, data?: { title?: string }) =>
api.post<ChatConversation>(`/api/companies/${companyId}/conversations`, data ?? {}),
getConversation: (id: string) =>
api.get<ChatConversation>(`/api/conversations/${id}`),
updateConversation: (id: string, data: { title?: string }) =>
api.patch<ChatConversation>(`/api/conversations/${id}`, data),
deleteConversation: (id: string) =>
api.delete(`/api/conversations/${id}`),
archiveConversation: (id: string) =>
api.post<ChatConversation>(`/api/conversations/${id}/archive`, {}),
unarchiveConversation: (id: string) =>
api.post<ChatConversation>(`/api/conversations/${id}/unarchive`, {}),
pinConversation: (id: string) =>
api.post<ChatConversation>(`/api/conversations/${id}/pin`, {}),
unpinConversation: (id: string) =>
api.post<ChatConversation>(`/api/conversations/${id}/unpin`, {}),
listMessages: (conversationId: string, opts?: { cursor?: string; limit?: number }) => {
const params = new URLSearchParams();
if (opts?.cursor) params.set("cursor", opts.cursor);
if (opts?.limit) params.set("limit", String(opts.limit));
const qs = params.toString();
return api.get<{ items: ChatMessage[]; hasMore: boolean }>(`/api/conversations/${conversationId}/messages${qs ? `?${qs}` : ""}`);
},
sendMessage: (conversationId: string, data: { role: string; content: string; agentId?: string | null }) =>
api.post<ChatMessage>(`/api/conversations/${conversationId}/messages`, data),
};