Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

41.0 KB
Raw Blame
"use client";

import type { PropsWithChildren } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import type {
  AttachmentAdapter,
  MessageFormatAdapter,
  RemoteThreadListAdapter,
  SpeechSynthesisAdapter,
  ThreadHistoryAdapter,
  ThreadMessage,
  Toolkit,
} from "@assistant-ui/react";
import {
  defineToolkit,
  useAui,
  useAuiEvent,
  useAuiState,
  useRemoteThreadListRuntime,
  WebSpeechSynthesisAdapter,
  AuiProvider,
  AuiConfig,
} from "@assistant-ui/react";
import { useChat } from "@ai-sdk/react";
import { AssistantChatTransport, useAISDKRuntime } from "@assistant-ui/react-ai-sdk";
import { useCallbackRef } from "@mantine/hooks";
import { createAssistantStream } from "assistant-stream";

import { clientApi, fetchApi } from "@homarr/api/client";
import { createId } from "@homarr/common";
import { assistantHomarrProviderTokenHeader } from "@homarr/definitions";
import { showErrorNotification, showWarningNotification } from "@homarr/notifications";
import { closeSpotlight } from "@homarr/spotlight/store";
import { useI18n } from "@homarr/translation/client";
import { openMediaRequestSearch, openSpotlight, useRegisterSpotlightContextResults } from "@homarr/spotlight";
import type { WidgetComponentProps } from "@homarr/widgets";
import { createWorkshopClient } from "~/components/workshop/workshop-client";

import { AssistantContext, AssistantPreferencesContext, useAssistantPreferences } from "./assistant-context";
import type { AssistantContextValue } from "./assistant-context";
import type { AssistantWidgetRender } from "./assistant-launcher";
import { shouldAutomaticallyContinueAssistant } from "./assistant-auto-submit";
import { getAssistantProviderQuotaRefreshDelay, isAssistantProviderUnavailable } from "./assistant-provider-quota";
import {
  AssistantAutoApprovalContext,
  AssistantAutoApprovalProvider,
  useAssistantAutoApproval,
} from "./assistant-auto-approval";
import { getRunningAssistantPartType } from "./assistant-activity-state";
import { prepareAssistantRequestBody } from "./assistant-attachment-payload";
import { createAssistantBrowserToolExecutors } from "./assistant-browser-tool-executors";
import { AssistantAskUserTool, AssistantConfigureAppTool } from "./assistant-human-tools";
import { AssistantPanel } from "./assistant-panel";
import { getPendingAssistantAction } from "./assistant-pending-action";
import type { AssistantReasoningMode, AssistantRuntimeModelOption } from "./assistant-preferences";
import { resolveAssistantPreferenceModelId, resolveAssistantThreadPreferenceModelId } from "./assistant-preferences";
import { assistantAiSdkRuntimeOptions } from "./assistant-runtime-options";
import {
  AssistantComposerRuntimeProvider,
  AssistantComposerSurfaceBoundary,
  AssistantRunFocusPreserver,
  AssistantRuntimeProviderWithTools,
} from "./assistant-runtime-provider";
import { sendAssistantPrompt as sendPromptThroughRuntime } from "./assistant-send";
import { createAssistantPromptInteraction } from "./assistant-spotlight";
import { browserToolContracts } from "./assistant-tool-contracts";
import { AssistantConfigureBoardSettingsTool } from "./assistant-board-settings-tool";
import { AssistantConfigureWidgetTool } from "./assistant-widget-tool";
import type { AssistantUIMessage } from "./assistant-message-metadata";
import {
  getSuccessfulApprovedAssistantMutationIds,
  updateAssistantMutationRefreshState,
} from "./assistant-mutation-refresh";
import { initialAssistantNotificationState, updateAssistantNotificationState } from "./assistant-notifications";
import { AssistantViewRefreshProvider, useAssistantViewRefresh } from "./assistant-view-refresh";
import { AssistantBoardWidget } from "./assistant-widget";
import { AssistantHistoryContext, AssistantHistoryProvider, useAssistantHistoryState } from "./assistant-history-state";

const ignoreUnsupportedArchiveAction = () => Promise.resolve();
const assistantImageAttachmentTypes = ["image/gif", "image/jpeg", "image/png", "image/webp"];
const assistantDocumentAttachmentTypes = [
  "application/json",
  "text/csv",
  "text/html",
  "text/markdown",
  "text/plain",
  "text/xml",
];

const fileToDataUrlAsync = (file: File, signal?: AbortSignal) =>
  new Promise<string>((resolve, reject) => {
    signal?.throwIfAborted();
    const reader = new FileReader();
    const abort = () => {
      reader.abort();
      reject(signal?.reason as Error);
    };
    signal?.addEventListener("abort", abort, { once: true });
    reader.addEventListener("loadend", () => signal?.removeEventListener("abort", abort), { once: true });
    reader.addEventListener("load", () => resolve(String(reader.result)), { once: true });
    reader.addEventListener("error", () => reject(new Error("The attachment could not be read.")), { once: true });
    reader.readAsDataURL(file);
  });

const createAssistantAttachmentAdapter = (allowImages: boolean): AttachmentAdapter => {
  return {
    accept: [...(allowImages ? assistantImageAttachmentTypes : []), ...assistantDocumentAttachmentTypes].join(","),
    async add({ file }) {
      const isImage = file.type.startsWith("image/");
      if (isImage && !allowImages) {
        throw new Error("The selected model does not support image input.");
      }
      if (
        (!isImage && !assistantDocumentAttachmentTypes.includes(file.type)) ||
        (isImage && !assistantImageAttachmentTypes.includes(file.type))
      ) {
        throw new Error("This file type is not supported.");
      }
      const sizeLimit = isImage ? 1_000_000 : 350_000;
      if (file.size === 0) throw new Error("Empty files cannot be attached.");
      if (file.size > sizeLimit) {
        throw new Error(isImage ? "Images must be smaller than 1 MB." : "Documents must be smaller than 350 KB.");
      }
      const id = createId();
      return {
        id,
        type: isImage ? "image" : "document",
        name: file.name,
        file,
        contentType: file.type,
        status: { type: "requires-action", reason: "composer-send" },
      };
    },
    async send(attachment, options) {
      return {
        ...attachment,
        status: { type: "complete" },
        content: [
          {
            type: "file",
            filename: attachment.name,
            mimeType: attachment.contentType ?? "application/octet-stream",
            data: await fileToDataUrlAsync(attachment.file, options?.signal),
          },
        ],
      };
    },
    async remove() {},
  };
};

const AssistantPreferencesProvider = ({ children, active }: PropsWithChildren<{ active: boolean }>) => {
  const t = useI18n("assistant");
  const { data, isLoading } = clientApi.assistant.getRuntimeOptions.useQuery(undefined, {
    staleTime: 10 * 60_000,
    enabled: active,
  });
  const [modelId, setModelIdState] = useState<string | null>(null);
  const [reasoning, setReasoningState] = useState<AssistantReasoningMode>("auto");
  const workshopClient = useMemo(createWorkshopClient, []);
  const [providerUser, setProviderUser] = useState(workshopClient.currentUser);
  const [providerAuthReady, setProviderAuthReady] = useState(false);
  const [quota, setQuota] = useState<Awaited<ReturnType<typeof workshopClient.getAssistantUsage>> | null>(null);
  const [quotaLoading, setQuotaLoading] = useState(false);
  const [quotaError, setQuotaError] = useState<string | null>(null);
  const quotaRequestRef = useRef<AbortController | null>(null);
  const previousDefaultModelIdRef = useRef<string | null | undefined>(undefined);
  const preferencesRef = useRef<{ modelId: string | null; reasoning: AssistantReasoningMode }>({
    modelId: null,
    reasoning: "auto",
  });
  const models = useMemo<AssistantRuntimeModelOption[]>(
    () =>
      data?.models.map(({ id, name, description, contextLength, promptPrice, completionPrice, inputModalities }) => ({
        id,
        name,
        description,
        contextLength,
        promptPrice,
        completionPrice,
        inputModalities,
      })) ?? [],
    [data?.models],
  );

  const setModelId = useCallback((nextModelId: string) => {
    preferencesRef.current.modelId = nextModelId;
    setModelIdState(nextModelId);
  }, []);
  const setReasoning = useCallback((nextReasoning: AssistantReasoningMode) => {
    preferencesRef.current.reasoning = nextReasoning;
    setReasoningState(nextReasoning);
  }, []);
  const getRequestBody = useCallback(() => {
    const current = preferencesRef.current;
    return {
      ...(current.modelId ? { modelId: current.modelId } : {}),
      reasoning: current.reasoning,
      ...(typeof window === "undefined"
        ? {}
        : {
            clientContext: {
              pathname: window.location.pathname,
              timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
            },
          }),
    };
  }, []);
  const getRequestHeaders = useCallback((): Record<string, string> => {
    if (data?.provider !== "homarr") return {};
    const token = workshopClient.authToken;
    return token ? { [assistantHomarrProviderTokenHeader]: token } : {};
  }, [data?.provider, workshopClient]);
  const refreshQuota = useCallback(async () => {
    if (!active) return;
    quotaRequestRef.current?.abort();
    if (data?.provider !== "homarr" || !workshopClient.authToken) {
      quotaRequestRef.current = null;
      setQuota(null);
      setQuotaError(null);
      setQuotaLoading(false);
      return;
    }
    const controller = new AbortController();
    quotaRequestRef.current = controller;
    setQuotaLoading(true);
    setQuotaError(null);
    try {
      const nextQuota = await workshopClient.getAssistantUsage(controller.signal);
      if (quotaRequestRef.current === controller && !controller.signal.aborted) setQuota(nextQuota);
    } catch {
      if (quotaRequestRef.current === controller && !controller.signal.aborted) {
        setQuotaError(t("providerQuota.loadError"));
      }
    } finally {
      if (quotaRequestRef.current === controller) {
        quotaRequestRef.current = null;
        setQuotaLoading(false);
      }
    }
  }, [active, data?.provider, t, workshopClient]);
  const signInToProvider = useCallback(async () => {
    setQuotaLoading(true);
    setQuotaError(null);
    try {
      const user = await workshopClient.signInWithGitHub();
      setProviderUser(user);
      await refreshQuota();
    } catch (error) {
      setQuotaError(t("providerQuota.signInError"));
      throw error;
    } finally {
      setQuotaLoading(false);
    }
  }, [refreshQuota, t, workshopClient]);

  useEffect(() => {
    if (!data) return;
    const nextModelId = resolveAssistantPreferenceModelId({
      currentModelId: preferencesRef.current.modelId,
      previousDefaultModelId: previousDefaultModelIdRef.current,
      defaultModelId: data.defaultModelId,
      models: data.models,
    });
    previousDefaultModelIdRef.current = data.defaultModelId;
    if (nextModelId === preferencesRef.current.modelId) return;
    preferencesRef.current.modelId = nextModelId;
    setModelIdState(nextModelId);
  }, [data]);

  useEffect(() => {
    if (!active) {
      setProviderAuthReady(false);
      return;
    }
    if (data?.provider !== "homarr") {
      setProviderUser(null);
      setQuota(null);
      setQuotaError(null);
      return;
    }
    let disposed = false;
    const unsubscribe = workshopClient.subscribeToAuth(setProviderUser);
    void workshopClient
      .refreshAuth()
      .then((user) => {
        if (disposed) return;
        setProviderUser(user);
        setProviderAuthReady(true);
      })
      .catch(() => {
        if (!disposed) setProviderAuthReady(true);
      });
    return () => {
      disposed = true;
      unsubscribe();
    };
  }, [active, data?.provider, workshopClient]);

  useEffect(
    () => () => {
      quotaRequestRef.current?.abort();
    },
    [],
  );

  useEffect(() => {
    if (!active || !providerAuthReady || data?.provider !== "homarr" || !providerUser) return;
    void refreshQuota();
  }, [active, data?.provider, providerAuthReady, providerUser, refreshQuota]);

  useEffect(() => {
    if (!active || data?.provider !== "homarr" || !providerUser || !quota) return;
    const timeout = window.setTimeout(() => void refreshQuota(), getAssistantProviderQuotaRefreshDelay(quota.resetsAt));
    return () => window.clearTimeout(timeout);
  }, [active, data?.provider, providerUser, quota, refreshQuota]);

  useEffect(() => {
    if (active) return;
    quotaRequestRef.current?.abort();
    quotaRequestRef.current = null;
    setQuotaLoading(false);
  }, [active]);

  const value = useMemo(
    () => ({
      provider: data?.provider ?? null,
      defaultModelId: data?.defaultModelId ?? null,
      modelId,
      models,
      reasoning,
      isLoading: isLoading || (active && data?.provider === "homarr" && !providerAuthReady),
      providerUser,
      quota,
      quotaLoading,
      quotaError,
      setModelId,
      setReasoning,
      getRequestBody,
      getRequestHeaders,
      refreshQuota,
      signInToProvider,
    }),
    [
      data?.defaultModelId,
      data?.provider,
      getRequestBody,
      getRequestHeaders,
      isLoading,
      active,
      providerAuthReady,
      modelId,
      models,
      providerUser,
      quota,
      quotaError,
      quotaLoading,
      reasoning,
      refreshQuota,
      setModelId,
      setReasoning,
      signInToProvider,
    ],
  );

  return <AssistantPreferencesContext.Provider value={value}>{children}</AssistantPreferencesContext.Provider>;
};

const threadAdapter: RemoteThreadListAdapter = {
  async list() {
    const threads = await fetchApi.assistant.listThreads.query();
    return {
      threads: threads.map((thread) => ({
        remoteId: thread.id,
        status: "regular",
        title: thread.title ?? undefined,
        lastMessageAt: thread.updatedAt,
        custom: { modelId: thread.modelId },
      })),
    };
  },
  async initialize(localId) {
    const thread = await fetchApi.assistant.createThread.mutate({ localId });
    return { remoteId: thread.id, externalId: undefined };
  },
  async fetch(threadId) {
    const { thread } = await fetchApi.assistant.getThread.query({ threadId });
    return {
      remoteId: thread.id,
      status: "regular",
      title: thread.title ?? undefined,
      lastMessageAt: thread.updatedAt,
      custom: { modelId: thread.modelId },
    };
  },
  async rename(threadId, title) {
    await fetchApi.assistant.renameThread.mutate({ threadId, title });
  },
  async updateCustom(threadId, custom) {
    const modelId = custom?.modelId;
    if (typeof modelId !== "string") return;
    await fetchApi.assistant.updateThreadModel.mutate({ threadId, modelId });
  },
  // assistant-ui requires these callbacks even when the product does not expose archiving.
  archive: ignoreUnsupportedArchiveAction,
  unarchive: ignoreUnsupportedArchiveAction,
  async delete(threadId) {
    await fetchApi.assistant.deleteThread.mutate({ threadId });
  },
  async generateTitle(_threadId, messages) {
    const firstUserMessage = messages.find((message) => message.role === "user");
    const text =
      firstUserMessage?.content
        .filter((part) => part.type === "text")
        .map((part) => part.text)
        .join(" ")
        .trim()
        .slice(0, 72) || "New conversation";
    return createAssistantStream((controller) => controller.appendText(text));
  },
};

interface AssistantHistoryThread {
  getState: () => { remoteId: string | undefined };
  initialize: () => Promise<{ remoteId: string }>;
}

type AssistantHistoryFailure = "load" | "save";

const reportHistoryFailure = async <T,>(
  failure: AssistantHistoryFailure,
  onFailure: (failure: AssistantHistoryFailure) => void,
  operation: () => Promise<T>,
) => {
  try {
    return await operation();
  } catch (error) {
    onFailure(failure);
    throw error;
  }
};

const createHistoryAdapter = (
  getThread: () => AssistantHistoryThread,
  onFailure: (failure: AssistantHistoryFailure) => void,
  onLoaded: () => void,
): ThreadHistoryAdapter => ({
  async load() {
    return { messages: [] };
  },
  async append() {
    // AI SDK runtimes use the typed adapter returned by withFormat below.
  },
  withFormat<TMessage, TStorageFormat extends Record<string, unknown>>(
    formatAdapter: MessageFormatAdapter<TMessage, TStorageFormat>,
  ) {
    // assistant-ui's current AI SDK v7 runtime intentionally keeps this identifier for its
    // persisted UIMessage wire format. It is a storage codec name, not the installed SDK version.
    if (formatAdapter.format !== "ai-sdk/v6") {
      throw new Error(`Unsupported assistant message format: ${formatAdapter.format}`);
    }
    const storageFormat = formatAdapter.format;
    const erasedFormatAdapter = formatAdapter as MessageFormatAdapter<unknown, TStorageFormat>;
    const getExistingThreadId = () => getThread().getState().remoteId;
    return {
      async load() {
        const threadId = getExistingThreadId();
        if (!threadId) return { messages: [] };
        return reportHistoryFailure("load", onFailure, async () => {
          const { messages } = await fetchApi.assistant.getThread.query({ threadId });
          const decodedMessages = messages
            .filter((message) => message.format === formatAdapter.format)
            .map(
              (message) =>
                formatAdapter.decode({
                  id: message.id,
                  parent_id: message.parentId,
                  format: message.format,
                  content: message.content as TStorageFormat,
                }) as { parentId: string | null; message: TMessage },
            );
          onLoaded();
          return { messages: decodedMessages };
        });
      },
      async append(item) {
        await reportHistoryFailure("save", onFailure, async () => {
          // The first user message is persisted when its run starts, before the new thread has a
          // remote id. Initializing here creates the thread instead of dropping that message.
          const { remoteId: threadId } = await getThread().initialize();
          await fetchApi.assistant.appendMessage.mutate({
            threadId,
            id: erasedFormatAdapter.getId(item.message),
            parentId: item.parentId,
            format: storageFormat,
            content: erasedFormatAdapter.encode(item as { parentId: string | null; message: unknown }),
          });
        });
      },
      async update(item, localMessageId) {
        const threadId = getExistingThreadId();
        if (!threadId) return;
        const id = erasedFormatAdapter.getId(item.message);
        await reportHistoryFailure("save", onFailure, async () => {
          await fetchApi.assistant.appendMessage.mutate({
            threadId,
            id,
            parentId: item.parentId,
            format: storageFormat,
            content: erasedFormatAdapter.encode(item as { parentId: string | null; message: unknown }),
          });
          if (localMessageId !== id) {
            await fetchApi.assistant.deleteMessages.mutate({ threadId, ids: [localMessageId] });
          }
        });
      },
      async delete(items) {
        const threadId = getExistingThreadId();
        if (!threadId || items.length === 0) return;
        await reportHistoryFailure("save", onFailure, () =>
          fetchApi.assistant.deleteMessages.mutate({
            threadId,
            ids: items.map((item) => erasedFormatAdapter.getId(item.message)),
          }),
        );
      },
    };
  },
});

const AssistantThreadRuntime = () => {
  const t = useI18n("assistant");
  const aui = useAui();
  const localThreadId = useAuiState((state) => state.threadListItem.id);
  const threadId = useAuiState((state) => state.threadListItem.remoteId);
  const { failedThreadIds, setLoadFailed } = useAssistantHistoryState();
  const preferences = useAssistantPreferences();
  const { getRequestBody, getRequestHeaders, refreshQuota } = preferences;
  const transport = useMemo(
    () =>
      new AssistantChatTransport({
        api: "/api/assistant/chat",
        body: getRequestBody,
        fetch: (input, init) => {
          const headers = new Headers(init?.headers);
          for (const [name, value] of Object.entries(getRequestHeaders())) headers.set(name, value);
          return globalThis.fetch(input, { ...init, headers, body: prepareAssistantRequestBody(init?.body) });
        },
      }),
    [getRequestBody, getRequestHeaders],
  );
  const history = useMemo(
    () =>
      createHistoryAdapter(
        () => aui.threadListItem(),
        (failure) => {
          if (failure === "load") setLoadFailed(localThreadId, true);
          showErrorNotification({
            title: t("historyError.title"),
            message: t(`historyError.${failure}Messages`),
          });
        },
        () => setLoadFailed(localThreadId, false),
      ),
    [aui, localThreadId, setLoadFailed, t],
  );
  const selectedModel = preferences.models.find((model) => model.id === preferences.modelId);
  const attachments = useMemo(
    () =>
      createAssistantAttachmentAdapter(
        selectedModel === undefined ||
          selectedModel.inputModalities.length === 0 ||
          selectedModel.inputModalities.includes("image"),
      ),
    [selectedModel],
  );
  const [speech, setSpeech] = useState<SpeechSynthesisAdapter>();
  useEffect(() => {
    if ("speechSynthesis" in window && typeof SpeechSynthesisUtterance !== "undefined") {
      setSpeech(new WebSpeechSynthesisAdapter());
    }
  }, []);
  const feedback = useMemo(
    () => ({
      submit: ({ message, type }: { message: ThreadMessage; type: "positive" | "negative" }) => {
        if (!threadId) return;
        void fetchApi.assistant.submitFeedback
          .mutate({ threadId, messageId: message.id, type })
          .catch(() => showErrorNotification({ title: t("responseError.title"), message: t("feedbackError") }));
      },
    }),
    [t, threadId],
  );
  const onError = useCallback(() => {
    void refreshQuota();
    showErrorNotification({
      title: t("responseError.title"),
      message: t("responseError.description"),
      autoClose: 10_000,
    });
  }, [refreshQuota, t]);
  const onFinish = useCallback(() => void refreshQuota(), [refreshQuota]);

  const chat = useChat<AssistantUIMessage>({
    id: localThreadId,
    transport,
    onError,
    onFinish,
    sendAutomaticallyWhen: shouldAutomaticallyContinueAssistant,
  });

  // The lower-level AI SDK v7 runtime is intentional: Homarr supplies its own tRPC-backed
  // thread list and history adapters and shares this chat instance with its custom transport.
  const runtime = useAISDKRuntime(chat, {
    ...assistantAiSdkRuntimeOptions,
    isSendDisabled: failedThreadIds.has(localThreadId),
    adapters: {
      history,
      attachments,
      feedback,
      speech,
    },
  });
  transport.setRuntime(runtime);
  // oxlint-disable-next-line no-underscore-dangle -- assistant-ui requires this hook to resolve the persisted remote thread before sending.
  transport.__internal_setGetThreadListItem(() => (aui.threadListItem.source ? aui.threadListItem() : undefined));
  return runtime;
};

const AssistantRuntime = ({ children }: PropsWithChildren) => {
  const router = useRouter();
  const { refreshCurrentView } = useAssistantViewRefresh();
  const runtime = useRemoteThreadListRuntime({
    adapter: threadAdapter,
    runtimeHook: AssistantThreadRuntime,
  });
  const browserToolExecutors = useMemo(
    () =>
      createAssistantBrowserToolExecutors({
        getOrigin: () => window.location.origin,
        navigate: (path) => router.push(path),
        openCommandMenu: openSpotlight,
        openMediaRequestSearch,
        refreshCurrentView,
      }),
    [refreshCurrentView, router],
  );

  const toolkit = useMemo(
    () =>
      defineToolkit({
        ask_user: {
          type: "human",
          display: "standalone",
          ...browserToolContracts.ask_user,
          render: AssistantAskUserTool,
        },
        configure_app: {
          type: "human",
          display: "standalone",
          ...browserToolContracts.configure_app,
          render: AssistantConfigureAppTool,
        },
        configure_board_settings: {
          type: "human",
          display: "standalone",
          ...browserToolContracts.configure_board_settings,
          render: AssistantConfigureBoardSettingsTool,
        },
        configure_widget: {
          type: "human",
          display: "standalone",
          ...browserToolContracts.configure_widget,
          render: AssistantConfigureWidgetTool,
        },
        navigate_to_route: {
          type: "frontend",
          ...browserToolContracts.navigate_to_route,
          execute: browserToolExecutors.navigate_to_route,
          renderText: {
            running: ({ args }) => `Opening ${args.path}…`,
            complete: ({ args }) => `Opened ${args.path}`,
          },
        },
        open_command_menu: {
          type: "frontend",
          ...browserToolContracts.open_command_menu,
          execute: browserToolExecutors.open_command_menu,
          renderText: { running: "Opening command menu…", complete: "Command menu opened" },
        },
        open_media_request_search: {
          type: "frontend",
          ...browserToolContracts.open_media_request_search,
          execute: browserToolExecutors.open_media_request_search,
          renderText: { running: "Opening media search…", complete: "Media search opened" },
        },
        refresh_current_view: {
          type: "frontend",
          ...browserToolContracts.refresh_current_view,
          execute: browserToolExecutors.refresh_current_view,
          renderText: { running: "Refreshing current view…", complete: "Current view refreshed" },
        },
      }) as Toolkit,
    [browserToolExecutors],
  );
  return (
    <AssistantRuntimeProviderWithTools runtime={runtime} toolkit={toolkit}>
      <AssistantRuntimeEvents />
      <AssistantRunFocusPreserver />
      <AssistantPreferenceSync />
      {children}
    </AssistantRuntimeProviderWithTools>
  );
};

const AssistantPreferenceSync = () => {
  const preferences = useAssistantPreferences();
  // The local id distinguishes every thread immediately, including threads that do not have a remote id yet.
  const conversationId = useAuiState((state) => state.threadListItem.id);
  const remoteId = useAuiState((state) => state.threadListItem.remoteId);
  const threadCustom = useAuiState((state) => state.threadListItem.custom);
  const threadModelId = threadCustom?.modelId;
  const previousSyncKeyRef = useRef<string | null>(null);
  const modelCatalogKey = preferences.models.map((model) => model.id).join("\0");

  useEffect(() => {
    if (preferences.models.length === 0) return;
    const syncKey = `${conversationId}:${modelCatalogKey}`;
    if (previousSyncKeyRef.current === syncKey) return;
    const nextModelId = resolveAssistantThreadPreferenceModelId({
      isRemote: remoteId !== undefined,
      metadataLoaded: threadCustom !== undefined,
      threadModelId,
      defaultModelId: preferences.defaultModelId,
      models: preferences.models,
    });
    if (nextModelId === undefined) return;
    previousSyncKeyRef.current = syncKey;
    if (nextModelId) preferences.setModelId(nextModelId);
  }, [conversationId, modelCatalogKey, preferences, remoteId, threadCustom, threadModelId]);

  return null;
};

const AssistantRuntimeEvents = () => {
  const t = useI18n("assistant");
  useAuiEvent("composer.attachmentAddError", ({ message }) => {
    showErrorNotification({
      title: t("attachments.errorTitle"),
      message,
    });
  });
  return null;
};

const getMessageText = (message: ThreadMessage | undefined) =>
  message?.content
    .filter((part) => part.type === "text")
    .map((part) => part.text)
    .join("\n")
    .trim() ?? "";

const getLatestMessage = (messages: readonly ThreadMessage[], role: ThreadMessage["role"]) =>
  messages.findLast((message) => message.role === role);

const getNotificationKey = (message: ThreadMessage | undefined) => {
  if (message?.role !== "assistant") return null;
  if (message.status.type === "running") return null;
  return `${message.id}:${message.status.type}:${"reason" in message.status ? message.status.reason : ""}`;
};

interface EnabledAssistantRootProps {
  active: boolean;
  opened: boolean;
  onOpen: () => void;
  onClose: () => void;
  onStateChange: (value: AssistantContextValue) => void;
  onWidgetRender: (render: AssistantWidgetRender) => void;
  onRequestPrompt: (prompt: string) => boolean;
  pendingPrompt: string | null;
  onPromptHandled: () => void;
}

const EnabledAssistantProvider = ({
  active,
  opened,
  onOpen,
  onClose,
  onStateChange,
  onWidgetRender,
  onRequestPrompt,
  pendingPrompt,
  onPromptHandled,
}: EnabledAssistantRootProps) => {
  const t = useI18n("assistant");
  const [activityDismissed, setActivityDismissed] = useState(true);
  const [unreadCount, setUnreadCount] = useState(0);
  const [visibleWidgetIds, setVisibleWidgetIds] = useState<Set<string>>(() => new Set());
  const [queuedPrompt, setQueuedPrompt] = useState<{ conversationId: string; text: string } | null>(null);
  const aui = useAui();
  const preferences = useAssistantPreferences();
  const { isRefreshing, refreshCurrentView } = useAssistantViewRefresh();
  const messages = useAuiState((state) => state.thread.messages);
  // The local id is stable while a new thread is initialized and changes for every real thread switch.
  const conversationId = useAuiState((state) => state.threadListItem.id);
  const isRunning = useAuiState((state) => state.thread.isRunning);
  const isLoading = useAuiState((state) => state.thread.isLoading);
  const { failedThreadIds } = useAssistantHistoryState();
  const historyLoadFailed = failedThreadIds.has(conversationId);
  const latestAssistantMessage = getLatestMessage(messages, "assistant");
  const latestUserMessage = getLatestMessage(messages, "user");
  const latestAssistantText = getMessageText(latestAssistantMessage);
  const latestUserText = getMessageText(latestUserMessage);
  const latestStatus = latestAssistantMessage?.role === "assistant" ? latestAssistantMessage.status : undefined;
  const latestAssistantPart = latestAssistantMessage?.content.at(-1);
  const latestToolComplete = latestAssistantPart?.type === "tool-call" && latestAssistantPart.result !== undefined;
  const latestAssistantPartType = getRunningAssistantPartType(
    latestStatus?.type,
    latestAssistantPart?.type,
    latestToolComplete,
  );
  const pendingAction = getPendingAssistantAction(latestAssistantMessage);
  const assistantIsRunning = isRunning || queuedPrompt !== null;
  const providerUnavailable = isAssistantProviderUnavailable({
    provider: preferences.provider,
    signedIn: preferences.providerUser !== null,
    remaining: preferences.quota?.remaining,
  });
  const notificationKey = getNotificationKey(latestAssistantMessage);
  const notificationStateRef = useRef(initialAssistantNotificationState);
  const mutationRefreshStateRef = useRef<{ conversationId: string | null; toolCallIds: Set<string> }>({
    conversationId: null,
    toolCallIds: new Set(),
  });
  const successfulMutationIds = useMemo(() => getSuccessfulApprovedAssistantMutationIds(messages), [messages]);

  const markRead = useCallback(() => {
    notificationStateRef.current = {
      initialized: true,
      conversationId,
      notificationKey,
    };
    setUnreadCount(0);
  }, [conversationId, notificationKey]);
  const markReadRef = useRef(markRead);
  markReadRef.current = markRead;
  const open = useCallback(() => {
    markRead();
    setActivityDismissed(false);
    onOpen();
  }, [markRead, onOpen]);
  const close = onClose;
  const setWidgetVisible = useCallback((widgetId: string, visible: boolean) => {
    if (visible) markReadRef.current();
    setVisibleWidgetIds((current) => {
      if (current.has(widgetId) === visible) return current;
      const next = new Set(current);
      if (visible) next.add(widgetId);
      else next.delete(widgetId);
      return next;
    });
  }, []);
  const activateWidget = useCallback((widgetId: string) => {
    setVisibleWidgetIds((current) => {
      if (!current.has(widgetId) || current.values().next().value === widgetId) return current;
      return new Set([widgetId, ...Array.from(current).filter((id) => id !== widgetId)]);
    });
  }, []);
  const toggle = useCallback(() => {
    if (opened) close();
    else open();
  }, [close, open, opened]);
  const sendPrompt = useCallback(
    (prompt: string) => {
      const text = prompt.trim();
      if (text.length === 0) return false;
      if (historyLoadFailed) {
        showErrorNotification({ title: t("historyError.title"), message: t("historyError.loadMessages") });
        return false;
      }
      if (!active) return onRequestPrompt(text);
      if (providerUnavailable) {
        showWarningNotification({
          title: t("providerQuota.title"),
          message: preferences.providerUser
            ? t("providerQuota.exhaustedDescription")
            : t("providerQuota.signInDescription"),
        });
        return false;
      }
      if (assistantIsRunning || latestStatus?.type === "requires-action") {
        showWarningNotification({
          title: t("busy.title"),
          message: t("busy.description"),
        });
        return false;
      }
      setActivityDismissed(false);
      if (isLoading) {
        setQueuedPrompt({ conversationId, text });
        return true;
      }
      return sendPromptThroughRuntime(aui, text);
    },
    [
      active,
      onRequestPrompt,
      assistantIsRunning,
      aui,
      conversationId,
      historyLoadFailed,
      isLoading,
      latestStatus?.type,
      preferences.providerUser,
      providerUnavailable,
      t,
    ],
  );
  const selectModel = useCallback(
    (modelId: string) => {
      preferences.setModelId(modelId);
      const threadListItem = aui.threadListItem();
      if (!threadListItem.getState().remoteId) return;
      threadListItem.updateCustom({ ...threadListItem.getState().custom, modelId });
    },
    [aui, preferences],
  );

  useEffect(() => {
    if (pendingPrompt === null || isLoading || preferences.isLoading) return;
    if (!sendPrompt(pendingPrompt)) open();
    onPromptHandled();
  }, [isLoading, onPromptHandled, open, pendingPrompt, preferences.isLoading, sendPrompt]);

  useEffect(() => {
    if (assistantIsRunning) setActivityDismissed(false);
  }, [assistantIsRunning]);

  useEffect(() => {
    if (queuedPrompt === null) return;
    if (queuedPrompt.conversationId !== conversationId) {
      setQueuedPrompt(null);
      return;
    }
    if (isLoading || isRunning || historyLoadFailed) return;
    if (providerUnavailable) {
      setQueuedPrompt(null);
      return;
    }
    const prompt = queuedPrompt.text;
    setQueuedPrompt(null);
    sendPromptThroughRuntime(aui, prompt);
  }, [aui, conversationId, historyLoadFailed, isLoading, isRunning, providerUnavailable, queuedPrompt]);

  useEffect(() => {
    if (isLoading) return;
    const update = updateAssistantNotificationState(notificationStateRef.current, {
      conversationId,
      notificationKey,
    });
    notificationStateRef.current = update.state;
    if (!update.shouldNotify) return;
    setActivityDismissed(false);

    if (opened || visibleWidgetIds.size > 0) {
      setUnreadCount(0);
      return;
    }

    setUnreadCount((current) => current + 1);
  }, [conversationId, isLoading, notificationKey, opened, visibleWidgetIds.size]);

  useEffect(() => {
    if (isLoading) return;

    const update = updateAssistantMutationRefreshState(
      mutationRefreshStateRef.current,
      conversationId,
      successfulMutationIds,
    );
    mutationRefreshStateRef.current = update.state;
    if (!update.shouldRefresh) return;

    void refreshCurrentView().catch(() => {
      showWarningNotification({
        title: t("refresh.failedTitle"),
        message: t("refresh.failedDescription"),
      });
    });
  }, [conversationId, isLoading, refreshCurrentView, successfulMutationIds, t]);

  // Spotlight can retain its previous item while a registration transition is pending.
  const sendSpotlightPrompt = useCallbackRef(sendPrompt);
  const spotlightItem = useMemo(() => {
    const canSend =
      !historyLoadFailed && !providerUnavailable && !assistantIsRunning && latestStatus?.type !== "requires-action";
    return {
      id: "homarr-assistant",
      name: t("spotlight"),
      icon: "/logo/logo.png",
      description: providerUnavailable
        ? t("providerQuota.unavailableDescription")
        : canSend
          ? t("spotlightDescription")
          : t("busy.description"),
      unavailable: !canSend,
      alwaysVisible: true,
      placement: "fallback" as const,
      interaction: (query: string) =>
        createAssistantPromptInteraction({
          sendPrompt: sendSpotlightPrompt,
          onPromptAccepted: closeSpotlight,
          prompt: query,
          canSend,
        }),
    };
  }, [assistantIsRunning, historyLoadFailed, latestStatus?.type, providerUnavailable, sendSpotlightPrompt, t]);
  useRegisterSpotlightContextResults("homarr-assistant", [spotlightItem], [spotlightItem]);

  const activeWidgetId = visibleWidgetIds.values().next().value ?? null;
  const value = useMemo(
    () => ({
      enabled: true,
      unavailableDescription: null,
      opened,
      isRunning: assistantIsRunning,
      isRefreshing,
      unreadCount,
      hasVisibleWidget: visibleWidgetIds.size > 0,
      activeWidgetId,
      open,
      close,
      toggle,
      sendPrompt,
      refreshCurrentView,
      setWidgetVisible,
      activateWidget,
    }),
    [
      assistantIsRunning,
      activateWidget,
      activeWidgetId,
      close,
      isRefreshing,
      open,
      opened,
      refreshCurrentView,
      sendPrompt,
      setWidgetVisible,
      toggle,
      unreadCount,
      visibleWidgetIds.size,
    ],
  );

  useEffect(() => onStateChange(value), [onStateChange, value]);

  return (
    <AssistantAutoApprovalProvider conversationId={conversationId}>
      <AssistantContext.Provider value={value}>
        <AssistantComposerRuntimeProvider>
          <AssistantWidgetScope onRender={onWidgetRender} />
          <AssistantComposerSurfaceBoundary surfaceId="assistant-panel">
            <AssistantPanel
              opened={opened}
              onOpen={open}
              onClose={close}
              onDismissActivity={() => {
                markRead();
                setActivityDismissed(true);
              }}
              activityDismissed={activityDismissed}
              hasVisibleWidget={visibleWidgetIds.size > 0}
              isRunning={assistantIsRunning}
              unreadCount={unreadCount}
              latestAssistantText={latestAssistantText}
              latestAssistantPartType={latestAssistantPartType}
              latestUserText={queuedPrompt?.text ?? latestUserText}
              latestStatus={latestStatus}
              pendingAction={pendingAction}
              modelId={preferences.modelId}
              models={preferences.models}
              modelOptionsLoading={preferences.isLoading}
              reasoning={preferences.reasoning}
              isRefreshing={isRefreshing}
              onRefresh={refreshCurrentView}
              onModelChange={selectModel}
              onReasoningChange={preferences.setReasoning}
            />
          </AssistantComposerSurfaceBoundary>
        </AssistantComposerRuntimeProvider>
      </AssistantContext.Provider>
    </AssistantAutoApprovalProvider>
  );
};

/**
 * Keep the runtime separate from dashboard children so activating it does not
 * remount the board. Board widgets reuse its scopes inside their existing tree.
 */
export const EnabledAssistantRoot = (props: EnabledAssistantRootProps) => (
  <AssistantPreferencesProvider active={props.active}>
    <AssistantViewRefreshProvider>
      <AssistantHistoryProvider>
        <AssistantRuntime>
          <EnabledAssistantProvider {...props} />
        </AssistantRuntime>
      </AssistantHistoryProvider>
    </AssistantViewRefreshProvider>
  </AssistantPreferencesProvider>
);

interface AssistantWidgetScopeValue {
  aui: ReturnType<typeof useAui>;
  preferences: ReturnType<typeof useAssistantPreferences>;
  approval: ReturnType<typeof useAssistantAutoApproval>;
  history: ReturnType<typeof useAssistantHistoryState>;
}

const ScopedAssistantBoardWidget = ({
  scope,
  widget,
}: {
  scope: AssistantWidgetScopeValue;
  widget: WidgetComponentProps<"assistant">;
}) => {
  const config = useMemo(() => AuiConfig({}), []);
  return (
    <AuiProvider extends={scope.aui} config={config}>
      <AssistantPreferencesContext.Provider value={scope.preferences}>
        <AssistantAutoApprovalContext.Provider value={scope.approval}>
          <AssistantHistoryContext.Provider value={scope.history}>
            <AssistantBoardWidget {...widget} />
          </AssistantHistoryContext.Provider>
        </AssistantAutoApprovalContext.Provider>
      </AssistantPreferencesContext.Provider>
    </AuiProvider>
  );
};

const AssistantWidgetScope = ({ onRender }: { onRender: (render: AssistantWidgetRender) => void }) => {
  const aui = useAui();
  const preferences = useAssistantPreferences();
  const approval = useAssistantAutoApproval();
  const history = useAssistantHistoryState();
  const render = useCallback(
    (widget: WidgetComponentProps<"assistant">) => (
      <ScopedAssistantBoardWidget scope={{ aui, preferences, approval, history }} widget={widget} />
    ),
    [approval, aui, preferences, history],
  );
  useEffect(() => onRender(render), [onRender, render]);
  return null;
};