All files / src/components/chat/ChatPanel WebSourcesNote.tsx

100% Statements 12/12
88.88% Branches 16/18
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                      14x         15x 15x 13x           4x                                             14x 7x   7x   3x           7x 7x                                                  
import { editRuleHref, ruleHref } from "@/apps/user/pages/rules/links";
import type { Rule } from "@/objects/Rule";
import type { WebSearchSummary } from "@/objects/WebSearch";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import {
  type NoticeLine,
  webSearchNotice,
} from "@/util/webSearchNotice";
import { useQuery } from "@tanstack/react-query";
import { Globe, RotateCcw } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
/**
 * The quiet line under an answer whose web searches something changed
 * (`util/webSearchNotice.ts`, from `meta.web_search`).
 *
 * A Tools rule that narrowed a search is named, with the sites it allowed
 * or ruled out and the ones it left out. Whoever may change the rule here
 * (`Rule.can_change`) gets **Edit rule**, opening the builder with the one
 * change that would let the agent use the site — which they review and
 * save; nothing is applied for them. Anyone else is told whose rule it is.
 * When no rule was involved — the engines found nothing, or could not
 * answer — the note says so, so a provider's failure is never read as a
 * rule, and offers to ask again.
 */
const WebSourcesNote: React.FC<{
  summary: WebSearchSummary;
  /** Sends the question this answer answered again; only on the latest. */
  onAskAgain?: () => void;
}> = ({ summary, onAskAgain }) => {
  const notice = webSearchNotice(summary);
  if (!notice) return null;
  return (
    <div className="mt-1.5 flex items-start gap-1.5 pl-0.5 text-xs text-gray-500 dark:text-gray-400">
      <Globe className="size-3 shrink-0 mt-0.5" aria-hidden="true" />
      <div className="space-y-0.5 min-w-0">
        {"lines" in notice ? (
          notice.lines.map((line, i) => (
            <RuleLine key={`${line.rule_id ?? i}-${i}`} line={line} />
          ))
        ) : (
          <p>
            {notice.text}
            {onAskAgain && (
              <button
                type="button"
                onClick={onAskAgain}
                className="ml-1.5 inline-flex items-center gap-1 font-medium text-gray-700 dark:text-gray-200 hover:underline"
              >
                <RotateCcw className="size-3" aria-hidden="true" />
                Ask again
              </button>
            )}
          </p>
        )}
      </div>
    </div>
  );
};
 
/** One rule's part, and what can be done about it from here. */
const RuleLine: React.FC<{ line: NoticeLine }> = ({ line }) => {
  const { getApiHandler } = useApiHandler();
  // The rule page's key: the note and the page read one cached rule.
  const { data } = useQuery<Rule | null>({
    queryKey: [`${QUERY_KEYS.GET.RULE_BY_ID}:${line.rule_id}`],
    queryFn: () => getApiHandler().rule_router.get(line.rule_id!),
    enabled: !!line.rule_id,
    staleTime: 60_000,
  });
  // Bound to an explicit type: the pinned TypeScript cannot infer
  // react-query v5's return type (docs/guides/frontend-components.md).
  const rule: Rule | null = data ?? null;
  return (
    <p>
      {line.text}
      {rule && rule.can_change && (
        <Link
          to={editRuleHref(rule.id, line.edit)}
          className="ml-1.5 font-medium text-gray-700 dark:text-gray-200 hover:underline"
        >
          Edit rule
        </Link>
      )}
      {rule && !rule.can_change && (
        <span>
          {" "}
          <Link to={ruleHref(rule.id)} className="hover:underline">
            Set by {rule.home_workspace_name ?? "another workspace"}
          </Link>{" "}
          — a governance editor there can change it.
        </span>
      )}
    </p>
  );
};
 
export default WebSourcesNote;