// A composer pill per chat whose reply queue has items: open it to review the // queued parts, then send them or clear them. import type { PluginClientContext } from "@getpaseo/plugin/client"; import type { PluginButtonContentProps, PluginButtonRegistration } from "@getpaseo/plugin/client"; import { Icon } from "@getpaseo/plugin/client/react-native"; import React from "react"; import { Pressable, StyleSheet, Text, View } from "react-native"; import { composeReply } from "../shared/logic/reply"; import { clearQueue, knownQueueAgents, queueOf, setQueued, subscribeQueues, useReplyQueue } from "./reply-queue"; export function registerQueuePills(client: PluginClientContext): () => void { const pills = new Map(); const pending = new Set(); const label = (count: number) => `${count} queued`; const sync = () => { for (const agentId of knownQueueAgents()) { if (agentId === "local") continue; const count = queueOf(agentId).length; const pill = pills.get(agentId); if (count === 0) { pill?.remove(); pills.delete(agentId); } else if (pill) { pill.update({ label: label(count) }); } else if (!pending.has(agentId)) { pending.add(agentId); void register(agentId).finally(() => pending.delete(agentId)); } } }; const register = async (agentId: string) => { const agent = client.paseo.agents.ref(agentId); if (agent.workspaceId === null) await agent.refresh(); const workspaceId = agent.workspaceId; const count = queueOf(agentId).length; if (workspaceId === null || count === 0 || pills.has(agentId)) return; pills.set( agentId, client.addComposerPill({ id: `reply-queue-${agentId}`, workspaceId, agentId, button: { title: "Queued reply", label: label(count), icon: "MessageSquare", behavior: { kind: "popover", Content: (props) => ( client.paseo.agents.ref(agentId).send(text)} /> ), }, }, }), ); }; const unsubscribe = subscribeQueues(sync); sync(); return () => { unsubscribe(); for (const pill of pills.values()) pill.remove(); pills.clear(); }; } function QueuePopover({ theme, close, send, ...context }: PluginButtonContentProps & { send(text: string): Promise }) { const agentId = context.context === "agent" ? context.agentId : null; const queue = useReplyQueue(agentId); if (agentId === null) return null; return ( {queue.map((item) => ( {item.text} setQueued(agentId, item.id, null)}> ))} { const text = composeReply(queueOf(agentId).map((item) => item.text), null); if (text === "") return; void send(text).then(() => clearQueue(agentId)); close(); }} style={{ paddingHorizontal: 12, paddingVertical: 6, borderRadius: 8, backgroundColor: theme.colors.accent }} > Send now { clearQueue(agentId); close(); }} style={{ paddingHorizontal: 12, paddingVertical: 6, borderRadius: 8, backgroundColor: theme.colors.surface2, borderWidth: StyleSheet.hairlineWidth, borderColor: theme.colors.border, }} > Clear ); }