| |
| |
| |
| |
| |
| |
| |
| import type { Conversation } from "./chatStore"; |
|
|
| const PROXY = "/api/proxy/conversations"; |
|
|
| export type RemoteConversationsPayload = { |
| conversations: Record<string, Conversation>; |
| activeId: string | null; |
| updatedAt?: number; |
| version?: number; |
| }; |
|
|
| export async function loadConversations(): Promise<RemoteConversationsPayload | null> { |
| try { |
| const res = await fetch(PROXY, { |
| method: "GET", |
| headers: { "Content-Type": "application/json" }, |
| }); |
| if (!res.ok) { |
| console.warn("[sync] load failed", res.status); |
| return null; |
| } |
| const data = (await res.json()) as RemoteConversationsPayload; |
| return data; |
| } catch (err) { |
| console.warn("[sync] load error", err); |
| return null; |
| } |
| } |
|
|
| export async function saveConversations( |
| payload: RemoteConversationsPayload, |
| signal?: AbortSignal |
| ): Promise<boolean> { |
| try { |
| const res = await fetch(PROXY, { |
| method: "PUT", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify(payload), |
| signal, |
| }); |
| if (!res.ok) { |
| console.warn("[sync] save failed", res.status); |
| return false; |
| } |
| return true; |
| } catch (err) { |
| if ((err as { name?: string })?.name === "AbortError") return false; |
| console.warn("[sync] save error", err); |
| return false; |
| } |
| } |
|
|