mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-03 11:10:23 +02:00
Replace the floating chat FAB/window with a first-class Chat tab under Inbox, laid out as an IM-style two-pane surface (thread list + conversation). Highlights: - New Chat page (packages/views/chat/chat-page.tsx) with URL-addressable session selection; web + desktop routing wired up. Removes the old chat-fab / chat-window / resize-handles / context-items paths. - IM thread list: agent avatar + last-message preview + IM timestamp, red unread *count* badge (read-cursor model), presence-gated typing vs waiting. Rename lives only in the conversation header ⋯ menu (not the list hover). - Per-session conversation header (rename / view agent / delete), agent-aware empty state (avatar + name + description + starter prompts), and a deterministic clean-title derivation from the first message. - Server: read-cursor unread model (migration 145) and per-user pinned agents (migration 146, dedicated chat_pinned_agent table + handler/queries). New-agent welcome chat auto-enqueues a real agent run (LLM intro, no static template). - Design: fade the global --border token; borderless list headers on Chat/Inbox, kept (faded) on the conversation header. Verified: pnpm typecheck (all packages), go build ./..., go vet, gofmt. Co-authored-by: multica-agent <github@multica.ai>
194 lines
7.0 KiB
TypeScript
194 lines
7.0 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { api } from "../api";
|
|
import { useWorkspaceId } from "../hooks";
|
|
import { chatKeys } from "./queries";
|
|
import { createLogger } from "../logger";
|
|
import type { ChatSession, ChatPinnedAgent } from "../types";
|
|
|
|
const logger = createLogger("chat.mut");
|
|
|
|
/** Pin an agent to the quick-agent bar (optimistic append). */
|
|
export function usePinChatAgent() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (agentId: string) => api.pinChatAgent(agentId),
|
|
onMutate: async (agentId) => {
|
|
await qc.cancelQueries({ queryKey: chatKeys.pinnedAgents(wsId) });
|
|
const prev = qc.getQueryData<ChatPinnedAgent[]>(chatKeys.pinnedAgents(wsId));
|
|
qc.setQueryData<ChatPinnedAgent[]>(chatKeys.pinnedAgents(wsId), (old) => {
|
|
if (old?.some((p) => p.agent_id === agentId)) return old;
|
|
const maxPos = old?.reduce((m, p) => Math.max(m, p.position), 0) ?? 0;
|
|
return [...(old ?? []), { agent_id: agentId, position: maxPos + 1 }];
|
|
});
|
|
return { prev };
|
|
},
|
|
onError: (err, agentId, ctx) => {
|
|
logger.error("pinChatAgent.error.rollback", { agentId, err });
|
|
if (ctx?.prev) qc.setQueryData(chatKeys.pinnedAgents(wsId), ctx.prev);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: chatKeys.pinnedAgents(wsId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Unpin an agent from the quick-agent bar (optimistic removal). */
|
|
export function useUnpinChatAgent() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (agentId: string) => api.unpinChatAgent(agentId),
|
|
onMutate: async (agentId) => {
|
|
await qc.cancelQueries({ queryKey: chatKeys.pinnedAgents(wsId) });
|
|
const prev = qc.getQueryData<ChatPinnedAgent[]>(chatKeys.pinnedAgents(wsId));
|
|
qc.setQueryData<ChatPinnedAgent[]>(chatKeys.pinnedAgents(wsId), (old) =>
|
|
old?.filter((p) => p.agent_id !== agentId),
|
|
);
|
|
return { prev };
|
|
},
|
|
onError: (err, agentId, ctx) => {
|
|
logger.error("unpinChatAgent.error.rollback", { agentId, err });
|
|
if (ctx?.prev) qc.setQueryData(chatKeys.pinnedAgents(wsId), ctx.prev);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: chatKeys.pinnedAgents(wsId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateChatSession() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: { agent_id: string; title?: string }) => {
|
|
logger.info("createChatSession.start", { agent_id: data.agent_id, titleLength: data.title?.length ?? 0 });
|
|
return api.createChatSession(data);
|
|
},
|
|
onSuccess: (session) => {
|
|
logger.info("createChatSession.success", { sessionId: session.id, agentId: session.agent_id });
|
|
},
|
|
onError: (err) => {
|
|
logger.error("createChatSession.error", err);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Clears the session's unread state server-side. Optimistically flips
|
|
* has_unread to false in the cached list so the FAB badge drops
|
|
* immediately. The server broadcasts chat:session_read so other devices
|
|
* also sync.
|
|
*/
|
|
export function useMarkChatSessionRead() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (sessionId: string) => {
|
|
logger.info("markChatSessionRead.start", { sessionId });
|
|
return api.markChatSessionRead(sessionId);
|
|
},
|
|
onMutate: async (sessionId) => {
|
|
await qc.cancelQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
|
|
const prevSessions = qc.getQueryData<ChatSession[]>(chatKeys.sessions(wsId));
|
|
|
|
const clear = (old?: ChatSession[]) =>
|
|
old?.map((s) => (s.id === sessionId ? { ...s, has_unread: false, unread_count: 0 } : s));
|
|
qc.setQueryData<ChatSession[]>(chatKeys.sessions(wsId), clear);
|
|
|
|
return { prevSessions };
|
|
},
|
|
onError: (err, sessionId, ctx) => {
|
|
logger.error("markChatSessionRead.error.rollback", { sessionId, err });
|
|
if (ctx?.prevSessions) qc.setQueryData(chatKeys.sessions(wsId), ctx.prevSessions);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Renames a chat session. Optimistically swaps the title in the cached
|
|
* list so the dropdown reflects the new label immediately; rolls back on
|
|
* error. The matching `chat:session_updated` WS event keeps other
|
|
* tabs/devices in sync — see use-realtime-sync.ts.
|
|
*/
|
|
export function useUpdateChatSession() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: { sessionId: string; title: string }) => {
|
|
logger.info("updateChatSession.start", {
|
|
sessionId: data.sessionId,
|
|
titleLength: data.title.length,
|
|
});
|
|
return api.updateChatSession(data.sessionId, { title: data.title });
|
|
},
|
|
onMutate: async ({ sessionId, title }) => {
|
|
await qc.cancelQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
|
|
const prevSessions = qc.getQueryData<ChatSession[]>(chatKeys.sessions(wsId));
|
|
|
|
const patch = (old?: ChatSession[]) =>
|
|
old?.map((s) => (s.id === sessionId ? { ...s, title } : s));
|
|
qc.setQueryData<ChatSession[]>(chatKeys.sessions(wsId), patch);
|
|
|
|
return { prevSessions };
|
|
},
|
|
onError: (err, vars, ctx) => {
|
|
logger.error("updateChatSession.error.rollback", { sessionId: vars.sessionId, err });
|
|
if (ctx?.prevSessions) qc.setQueryData(chatKeys.sessions(wsId), ctx.prevSessions);
|
|
},
|
|
onSettled: () => {
|
|
qc.invalidateQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hard-deletes a chat session. Optimistically removes the row from the
|
|
* sessions list so the dropdown updates instantly; rolls back on error.
|
|
* The matching `chat:session_deleted` WS event keeps other tabs/devices
|
|
* in sync — see use-realtime-sync.ts.
|
|
*/
|
|
export function useDeleteChatSession() {
|
|
const qc = useQueryClient();
|
|
const wsId = useWorkspaceId();
|
|
|
|
return useMutation({
|
|
mutationFn: (sessionId: string) => {
|
|
logger.info("deleteChatSession.start", { sessionId });
|
|
return api.deleteChatSession(sessionId);
|
|
},
|
|
onMutate: async (sessionId) => {
|
|
await qc.cancelQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
|
|
const prevSessions = qc.getQueryData<ChatSession[]>(chatKeys.sessions(wsId));
|
|
|
|
const drop = (old?: ChatSession[]) => old?.filter((s) => s.id !== sessionId);
|
|
qc.setQueryData<ChatSession[]>(chatKeys.sessions(wsId), drop);
|
|
|
|
logger.debug("deleteChatSession.optimistic", { sessionId });
|
|
return { prevSessions };
|
|
},
|
|
onError: (err, sessionId, ctx) => {
|
|
logger.error("deleteChatSession.error.rollback", { sessionId, err });
|
|
if (ctx?.prevSessions) qc.setQueryData(chatKeys.sessions(wsId), ctx.prevSessions);
|
|
},
|
|
onSettled: (_data, _err, sessionId) => {
|
|
logger.debug("deleteChatSession.settled", { sessionId });
|
|
qc.invalidateQueries({ queryKey: chatKeys.sessions(wsId) });
|
|
},
|
|
});
|
|
}
|