All files / src/apps/user/pages/knowledge/recommendations recommendations.ts

100% Statements 24/24
75% Branches 21/28
100% Functions 5/5
100% Lines 24/24

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                                      8x                           8x 11x       8x   8x       15x 15x 24x 19x     15x 15x     8x 6x         8x 3x                     18x 18x   1x               7x       5x                   1x         1x           1x       2x      
import { encodeResourceId } from "@/components/detail/useResourceId";
import type { RecommendationAction } from "@/objects/Knowledge";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import type { LucideIcon } from "lucide-react";
import {
  BotOff,
  CalendarClock,
  Compass,
  Copy,
  Globe,
  Layers,
  Lightbulb,
  MessageCircleQuestion,
  Quote,
  RefreshCw,
  Unplug,
} from "lucide-react";
 
/**
 * Knowledge recommendations, as every page draws them: each kind's icon,
 * and the page each action opens.
 *
 * The kinds, their labels and what they say are svc-core's
 * (`services/knowledge/recommendations`); the keys here are pinned to them
 * by `recommendations.test.ts`. An icon names the kind; a recommendation is
 * a suggestion, not a status, so it carries no tone.
 */
export const RECOMMENDATION_ICONS: Record<string, LucideIcon> = {
  unanswered: MessageCircleQuestion,
  no_dated_knowledge: CalendarClock,
  no_method_knowledge: Compass,
  answered_from_the_web: Globe,
  retrieved_not_cited: Quote,
  granted_not_used: Unplug,
  reaches_no_agent: BotOff,
  refresh_for_its_kind: RefreshCw,
  same_documents: Copy,
  looks_dated: CalendarClock,
  mixed_kinds: Layers,
};
 
export const recommendationIcon = (kind: string): LucideIcon =>
  RECOMMENDATION_ICONS[kind] ?? Lightbulb;
 
/** The flows a page runs in place rather than opens: the generate dialog,
 *  and — on an agent's own page — its attach picker. */
export const IN_PLACE_FLOWS = new Set(["generate_knowledge"]);
 
const withParams = (
  path: string,
  params: Record<string, string | number | null | undefined>,
): string => {
  const query = new URLSearchParams();
  for (const [key, value] of Object.entries(params)) {
    if (value !== null && value !== undefined && value !== "") {
      query.set(key, String(value));
    }
  }
  const text = query.toString();
  return text ? `${path}?${text}` : path;
};
 
const sourcePage = (id: string) =>
  APP_ROUTES.USER.KNOWLEDGE.id.replace(
    ROUTE_PARAMS.KNOWLEDGE.id,
    encodeResourceId(id),
  );
 
const repositoryPage = (id: string) =>
  APP_ROUTES.USER.KNOWLEDGE.REPOSITORIES.id.replace(
    ROUTE_PARAMS.REPOSITORY.id,
    encodeResourceId(id),
  );
 
/**
 * The page an action opens, with what to fill in its address; `null` for a
 * flow the page runs in place, or one missing what it needs. Every page it
 * opens changes nothing until the person saves.
 */
export function actionHref(action: RecommendationAction): string | null {
  const p = action.prefill ?? {};
  switch (action.flow) {
    case "add_website":
      return withParams(APP_ROUTES.USER.KNOWLEDGE.new, {
        type: "web",
        url: p.url,
        name: p.name,
        refresh: p.refresh,
        max_age_days: p.max_age_days,
      });
    case "open_source":
      return p.knowledge_id
        ? withParams(sourcePage(p.knowledge_id), { tab: p.tab })
        : null;
    case "edit_source":
      return p.knowledge_id
        ? withParams(
            APP_ROUTES.USER.KNOWLEDGE.edit.replace(
              ROUTE_PARAMS.KNOWLEDGE.id,
              encodeResourceId(p.knowledge_id),
            ),
            { refresh: p.refresh, max_age_days: p.max_age_days },
          )
        : null;
    case "open_repository":
      return p.repository_id
        ? withParams(repositoryPage(p.repository_id), { tab: p.tab })
        : null;
    case "edit_repository":
      // The repository's page opens its details with the kind filled in.
      return p.repository_id
        ? withParams(repositoryPage(p.repository_id), { category: p.category })
        : null;
    case "attach_repository":
      // Off the agent's own page: the repository's Agents tab, where
      // "Use in your agents" is.
      return p.repository_id
        ? withParams(repositoryPage(p.repository_id), { tab: "agents" })
        : null;
    default:
      return null;
  }
}