TypeScript 97.4%
SQL 1%
JavaScript 0.9%
CSS 0.6%
1"use client";2import { api } from "@/lib/client/api";3import { PENDING_ATTACHMENTS_KEY } from "@/components/prompts/insert";4import type { PendingAttachment } from "@/lib/client/types";56/**7 * Library files → new chat hand-off (consumed by the Chat workstream).8 *9 * `useFilesInNewChat(fileIds)` copies the files into pending `message_attachments` (POST /api/library/files/attach),10 * stashes the descriptors in `sessionStorage["polyllm:pending-attachments"]` and returns the href11 * `/app/chat?attachments=<id,id>` to navigate to. A new chat view should, on mount, read `?attachments=` and12 * `consumePendingAttachments()` to seed the composer chips (the ids go straight into `message.attachmentIds`).13 */14export async function prepareFilesForNewChat(fileIds: string[]): Promise<{ href: string; attachments: PendingAttachment[] }> {15 const res = await api<{ attachments: PendingAttachment[] }>("/api/library/files/attach", { method: "POST", json: { fileIds } });16 try {17 window.sessionStorage.setItem(PENDING_ATTACHMENTS_KEY, JSON.stringify({ at: Date.now(), attachments: res.attachments }));18 } catch {19 /* ignore */20 }21 return { href: `/app/chat?attachments=${encodeURIComponent(res.attachments.map((a) => a.id).join(","))}`, attachments: res.attachments };22}2324/** Reads and clears stashed pending attachments (null when absent or older than 5 minutes). */25export function consumePendingAttachments(): PendingAttachment[] | null {26 try {27 const raw = window.sessionStorage.getItem(PENDING_ATTACHMENTS_KEY);28 if (!raw) return null;29 window.sessionStorage.removeItem(PENDING_ATTACHMENTS_KEY);30 const d = JSON.parse(raw) as { at: number; attachments: PendingAttachment[] };31 if (!Array.isArray(d?.attachments) || Date.now() - (d.at ?? 0) > 5 * 60_000) return null;32 return d.attachments;33 } catch {34 return null;35 }36}37