"use client"; import { useLayoutEffect, useRef } from "react"; import { ComposerPrimitive, ThreadPrimitive, useAuiState } from "@assistant-ui/react"; import { LexicalComposerInput } from "@assistant-ui/react-lexical"; import { ActionIcon, Box, Button, Group, Stack, Text, ThemeIcon, Tooltip } from "@mantine/core"; import { IconArrowUp, IconMessage, IconPaperclip, IconPlayerStop, IconQuote, IconX } from "@tabler/icons-react"; import { useI18n } from "@homarr/translation/client"; import classes from "./assistant-panel.module.css"; import { ComposerTriggers } from "./assistant-composer-triggers"; import type { AssistantConversationControls } from "./assistant-conversation-controls"; import { ConversationContext } from "./assistant-conversation-context"; import { useAssistantPreferences } from "./assistant-context"; import { Attachment, ContextDirectiveChip } from "./assistant-message-content"; import type { AssistantPendingAction } from "./assistant-pending-action"; import { isAssistantProviderUnavailable } from "./assistant-provider-quota"; import { HomarrProviderQuota, RuntimeControls } from "./assistant-runtime-controls"; type ComposerProps = AssistantConversationControls & { pendingAction: AssistantPendingAction | undefined }; export const Composer = (props: ComposerProps) => { const t = useI18n("assistant"); const preferences = useAssistantPreferences(); const running = useAuiState((state) => state.thread.isRunning); const hasPendingAction = props.pendingAction !== undefined; const providerUnavailable = isAssistantProviderUnavailable({ provider: preferences.provider, signedIn: preferences.providerUser !== null, remaining: preferences.quota?.remaining, }); const sendBlocked = hasPendingAction || providerUnavailable; let sendLabel = t("send"); if (providerUnavailable) sendLabel = t("providerQuota.unavailableDescription"); if (hasPendingAction) sendLabel = t("pendingAction.sendBlocked"); const composerInputRef = useRef(null); const composerLabel = t("composerPlaceholder"); useLayoutEffect(() => { const editor = composerInputRef.current?.querySelector(".aui-lexical-input"); if (!editor) return; editor.setAttribute("aria-label", composerLabel); return () => { if (editor.getAttribute("aria-label") === composerLabel) editor.removeAttribute("aria-label"); }; }, [composerLabel]); return ( { if (sendBlocked) event.preventDefault(); }} > {() => } {running ? ( ) : ( )} ); }; export const usePendingActionCopy = (action: AssistantPendingAction | undefined) => { const t = useI18n("assistant"); if (!action) return undefined; if (action.kind === "question") { return { title: t("pendingAction.answerTitle"), detail: action.detail ?? t("pendingAction.answerFallback") }; } if (action.kind === "form") { const isBoardSettings = action.toolName === "configure_board_settings"; return { title: isBoardSettings ? t("pendingAction.boardFormTitle") : t("pendingAction.formTitle"), detail: action.detail ?? (isBoardSettings ? t("configureBoardSettings.title") : t("configureApp.title")), }; } const tool = action.toolName.replaceAll("_", " "); return { title: t("activity.approval"), detail: action.detail ? `${tool} ยท ${action.detail}` : tool }; }; export const PendingActionBanner = ({ pendingAction }: { pendingAction: AssistantPendingAction | undefined }) => { const t = useI18n("assistant"); const copy = usePendingActionCopy(pendingAction); if (!copy || pendingAction?.kind === "question") return null; return ( {copy.title} {copy.detail} ); }; export const PendingQuestionDock = ({ pendingAction, setTarget, }: { pendingAction: AssistantPendingAction | undefined; setTarget: (target: HTMLDivElement | null) => void; }) => { const t = useI18n("assistant"); if (pendingAction?.kind !== "question") return null; return (
); };