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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | 7x 167x 167x 60x 44x 7x 13x 13x 13x 9x 4x 2x 7x 11x 11x 11x 10x 4x 13x 2x 1x 7x 19x 19x 19x 19x 19x 19x 19x 13x 19x 2x 1x 1x 1x 1x 19x 1x 1x 1x 1x 1x 1x 19x 1x 1x 1x 1x 1x 1x 1x 1x 19x 6x 13x 19x 19x 13x 13x 1x | import { Skeleton } from "@/components/detail";
import type {
KnowledgeCategoryKey,
KnowledgeRecommendation,
KnowledgeRecommendations,
RecommendationAction,
RecommendationPrefill,
} from "@/objects/Knowledge";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
import { parseServerDate } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { ArrowRight, Sparkles, Undo2 } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import GenerateKnowledgeModal from "../GenerateKnowledgeModal";
import { knowledgeCategory } from "../categories";
import { actionHref, recommendationIcon } from "./recommendations";
export interface RecommendationScope {
repositoryId?: string;
agentId?: string;
}
/** The recommendations for a page: everything, or what touches one
* repository or one agent. One query per scope, shared by a count and a
* list on the same page. */
export const useKnowledgeRecommendations = (
{ repositoryId, agentId }: RecommendationScope = {},
enabled = true,
) => {
const { getApiHandler } = useApiHandler();
return useQuery<KnowledgeRecommendations>({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_RECOMMENDATIONS, repositoryId ?? "", agentId ?? ""],
queryFn: () =>
getApiHandler().knowledge_router.recommendations({ repositoryId, agentId }),
enabled,
});
};
const keyOf = (item: KnowledgeRecommendation) => `${item.kind}:${item.subject.id}`;
const ActionButton: React.FC<{
action: RecommendationAction;
primary: boolean;
onRun: (action: RecommendationAction) => void;
runsInPlace: boolean;
}> = ({ action, primary, onRun, runsInPlace }) => {
const className = `inline-flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-xs font-medium transition-colors ${
primary
? "bg-gray-900 text-white hover:bg-gray-700 dark:bg-white dark:text-gray-900 dark:hover:bg-gray-200"
: "border border-gray-200 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
}`;
const href = runsInPlace ? null : actionHref(action);
if (href) {
return (
<Link to={href} className={className}>
{action.label}
<ArrowRight className="size-3" aria-hidden="true" />
</Link>
);
}
return (
<button type="button" onClick={() => onRun(action)} className={className}>
{action.flow === "generate_knowledge" && (
<Sparkles className="size-3" aria-hidden="true" />
)}
{action.label}
</button>
);
};
const RecommendationCard: React.FC<{
item: KnowledgeRecommendation;
canChange: boolean;
onRun: (item: KnowledgeRecommendation, action: RecommendationAction) => void;
inPlace: (action: RecommendationAction) => boolean;
onSetAside: (item: KnowledgeRecommendation) => void;
settingAside: boolean;
}> = ({ item, canChange, onRun, inPlace, onSetAside, settingAside }) => {
const Icon = recommendationIcon(item.kind);
const actions = canChange ? item.actions : item.actions.filter((a) => a.flow.startsWith("open_"));
return (
<li className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 px-4 py-3.5">
<div className="flex items-start gap-3">
<span className="shrink-0 rounded-lg bg-gray-100 dark:bg-gray-700 p-1.5 text-gray-600 dark:text-gray-300">
<Icon className="size-4" aria-hidden="true" />
</span>
<div className="min-w-0 flex-1 space-y-2">
<div>
<h3 className="text-sm font-semibold text-gray-900 dark:text-gray-100">
{item.title}
</h3>
<p className="mt-0.5 text-xs text-gray-600 dark:text-gray-400">{item.why}</p>
</div>
{item.figures.length > 0 && (
<dl className="flex flex-wrap gap-x-4 gap-y-1 text-xs">
{item.figures.map((f) => (
<div key={f.label} className="flex gap-1">
<dt className="text-gray-500 dark:text-gray-400">{f.label}</dt>
<dd className="font-semibold tabular-nums text-gray-900 dark:text-gray-100">
{f.value.toLocaleString()}
</dd>
</div>
))}
</dl>
)}
{item.examples.length > 0 && (
<div>
<div className="text-[11px] font-medium uppercase tracking-wide text-gray-400 dark:text-gray-500">
{item.subject.kind === "pair" ? "Held by both" : "Your questions"}
</div>
<ul className="mt-1 space-y-1">
{item.examples.map((e, n) => (
<li key={n} className="text-xs text-gray-700 dark:text-gray-300">
<span className="line-clamp-1">“{e.text}”</span>
{(e.detail || e.at) && (
<span className="block text-[11px] text-gray-400 dark:text-gray-500">
{e.detail}
{e.detail && e.at ? " · " : ""}
{e.at ? parseServerDate(e.at).toLocaleDateString() : ""}
</span>
)}
</li>
))}
</ul>
</div>
)}
<div className="flex flex-wrap items-center gap-2 pt-0.5">
{actions.map((action, n) => (
<ActionButton
key={`${action.flow}-${n}`}
action={action}
primary={n === 0}
runsInPlace={inPlace(action)}
onRun={(a) => onRun(item, a)}
/>
))}
{canChange && (
<button
type="button"
onClick={() => onSetAside(item)}
disabled={settingAside}
className="ml-auto text-xs text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 disabled:opacity-50"
>
Set aside
</button>
)}
</div>
</div>
</div>
</li>
);
};
export interface RecommendationListProps extends RecommendationScope {
/** Runs "attach a repository" in place: the agent's own page opens its
* attach picker. Elsewhere the repository's Agents tab is opened. */
onAttach?: (prefill: RecommendationPrefill) => void;
/** What to say with nothing to recommend; `null` draws nothing at all. */
empty?: React.ReactNode | null;
}
/**
* What to change about the workspace's knowledge, each with the evidence
* that says so and the step that would change it. An action opens an
* existing flow, filled in, which changes nothing until the person saves.
* A recommendation set aside stays aside, for everyone here, while its
* evidence reads the same (`fingerprint`).
*/
const RecommendationList: React.FC<RecommendationListProps> = ({
repositoryId,
agentId,
onAttach,
empty = "Nothing to recommend: the knowledge here is answering what it is asked.",
}) => {
const { getApiHandler } = useApiHandler();
const { data, isLoading } = useKnowledgeRecommendations({ repositoryId, agentId });
// Set aside here, by recommendation: its dismissal, so Undo can bring it
// back. The list is not refetched, so the row stays to offer that.
const [aside, setAside] = useState<Record<string, string>>({});
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [generating, setGenerating] = useState<{
category: KnowledgeCategoryKey;
fromTurns: string[];
questions: string[];
} | null>(null);
const inPlace = (action: RecommendationAction) =>
action.flow === "generate_knowledge" ||
(action.flow === "attach_repository" && !!onAttach);
const run = (item: KnowledgeRecommendation, action: RecommendationAction) => {
if (action.flow === "generate_knowledge") {
setGenerating({
category: knowledgeCategory(action.prefill.category).key,
fromTurns: action.prefill.from_turns ?? [],
questions: item.examples.map((e) => e.text),
});
} else Eif (action.flow === "attach_repository" && onAttach) {
onAttach(action.prefill);
}
};
const setItemAside = async (item: KnowledgeRecommendation) => {
setError(null);
setBusy(keyOf(item));
try {
const dismissed = await getApiHandler().knowledge_router.dismissRecommendation({
kind: item.kind,
subject: item.subject.id,
fingerprint: item.fingerprint,
});
setAside((prev) => ({ ...prev, [keyOf(item)]: dismissed.id }));
} catch (e) {
setError(errorDetail(e, "Could not set it aside. Try again."));
} finally {
setBusy(null);
}
};
const undo = async (item: KnowledgeRecommendation) => {
const id = aside[keyOf(item)];
Iif (!id) return;
setError(null);
setBusy(keyOf(item));
try {
await getApiHandler().knowledge_router.undoDismissal(id);
setAside(({ [keyOf(item)]: _, ...rest }) => rest);
} catch (e) {
setError(errorDetail(e, "Could not bring it back. Try again."));
} finally {
setBusy(null);
}
};
if (isLoading) {
return (
<div className="space-y-2" aria-label="Loading recommendations">
<Skeleton className="h-20 w-full rounded-xl" />
<Skeleton className="h-20 w-full rounded-xl" />
</div>
);
}
const items = data?.items ?? [];
const unavailable = Object.keys(data?.unavailable ?? {}).length;
Iif (items.length === 0 && empty === null && !unavailable) return null;
return (
<div className="space-y-2">
{items.length === 0 ? (
empty !== null && (
<p className="rounded-xl border border-dashed border-gray-200 dark:border-gray-700 px-4 py-3 text-xs text-gray-500 dark:text-gray-400">
{empty}
</p>
)
) : (
<ul className="space-y-2">
{items.map((item: KnowledgeRecommendation) =>
aside[keyOf(item)] ? (
<li
key={keyOf(item)}
className="flex items-center justify-between gap-3 rounded-xl border border-dashed border-gray-200 dark:border-gray-700 px-4 py-2.5 text-xs text-gray-500 dark:text-gray-400"
>
<span className="truncate">Set aside: {item.title}</span>
<button
type="button"
onClick={() => undo(item)}
disabled={busy === keyOf(item)}
className="inline-flex shrink-0 items-center gap-1 font-medium text-gray-700 dark:text-gray-300 hover:underline disabled:opacity-50"
>
<Undo2 className="size-3" aria-hidden="true" /> Undo
</button>
</li>
) : (
<RecommendationCard
key={keyOf(item)}
item={item}
canChange={!!data?.can_change}
onRun={run}
inPlace={inPlace}
onSetAside={setItemAside}
settingAside={busy === keyOf(item)}
/>
),
)}
</ul>
)}
{unavailable > 0 && (
<p className="text-[11px] text-gray-400 dark:text-gray-500">
{unavailable === 1 ? "One kind of recommendation" : `${unavailable} kinds of recommendation`}{" "}
could not be read just now, so this list may be missing some.
</p>
)}
{error && (
<p role="alert" className="text-xs text-red-600 dark:text-red-400">
{error}
</p>
)}
{generating && (
<GenerateKnowledgeModal
initialCategory={generating.category}
fromTurns={generating.fromTurns}
fromQuestions={generating.questions}
onClose={() => setGenerating(null)}
/>
)}
</div>
);
};
export default RecommendationList;
|