Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.8 KB
Raw Blame
// The proactive bot's one suggestion to add a helper-model key (server/host/helper-suggestion.ts), in the person's own
// language. A suggestion, not an approval: Add key opens Settings on this device, Not now is kept and ends it. Labelled as
// the app's own tip, with what the key and Jev are, so it never reads as an unattributed message from a stranger.
import { useStoreDispatch } from "@/state/store-select";
import { useState } from "react";
import { Lightbulb, Loader2 } from "lucide-react";

import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { api, type Message } from "@/state/store";
import { ASK_PRIMARY_BUTTON, ASK_QUIET_BUTTON } from "./AskCard";

export function HelperKeySuggestion({ threadId, message }: { threadId?: string; message: Message }) {
  const dispatch = useStoreDispatch();
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const declined = message.setupSuggestion?.answer === "notNow";

  const notNow = async () => {
    if (saving || !threadId) return;
    setSaving(true);
    setError(null);
    try {
      await api("/api/helpers/suggestion/not-now", { method: "POST", body: JSON.stringify({ threadId, messageId: message.id }) });
    } catch (cause) {
      setError(cause instanceof Error ? cause.message : String(cause));
    } finally {
      setSaving(false);
    }
  };

  return (
    <div data-setup-suggestion="helperKey" className="max-w-[600px] text-[15px] leading-relaxed text-ink">
      <p data-testid="helper-suggestion-from" dir="auto" className="mb-1 flex items-center gap-1 text-[12px] font-medium text-ink-secondary">
        <Lightbulb size={13} aria-hidden="true" />
        {t("chat.helperSuggestion.from")}
      </p>
      <p dir="auto">{t("chat.helperSuggestion.text")}</p>
      <p dir="auto" className="mt-1 text-[13px] text-ink-secondary">{t("chat.helperSuggestion.about")}</p>
      {declined ? (
        <p dir="auto" className="mt-1 text-[13px] text-ink-secondary">{t("chat.helperSuggestion.declined")}</p>
      ) : (
        <div className="mt-2 flex flex-wrap items-center gap-2">
          <button
            type="button"
            data-testid="helper-suggestion-add"
            onClick={() => dispatch({ type: "toggleAppSettings", open: true, section: "decisionModel" })}
            className={ASK_PRIMARY_BUTTON}
          >
            {t("chat.helperSuggestion.addKey")}
          </button>
          <button type="button" data-testid="helper-suggestion-not-now" onClick={() => void notNow()} disabled={saving} className={cn(ASK_QUIET_BUTTON, "my-0")}>
            {saving ? <Loader2 size={14} className="animate-spin" /> : t("chat.helperSuggestion.notNow")}
          </button>
        </div>
      )}
      {error && <p role="alert" className="mt-1 text-[12px] text-danger">{error}</p>}
    </div>
  );
}