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 | 4x 12x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 2x 4x 4x 3x 3x 1x 3x 3x 2x 4x 2x 2x 2x | import { KpiStrip, RankedList, type RankedRow } from "@/components/charts";
import TimeSeriesChart from "@/components/charts/TimeSeriesChart";
import { SegmentedFilter } from "@/components/collection";
import ResourceCostCard from "@/components/cost/ResourceCostCard";
import { SectionCard, SkeletonLines } from "@/components/detail";
import type { CostSource } from "@/objects/Cost";
import type { KnowledgeUsage, UsageRanked } from "@/objects/Knowledge";
import { chatMessageHref, resourceHref } from "@/routes/resourceHref";
import { useQuery } from "@tanstack/react-query";
import { BarChart3, Bot, FileText, Layers, MessageSquare } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import { formatAgo } from "../figures";
/**
* What a source or repository is used for: the answers in this workspace
* citing it over a window (svc-core `services/knowledge/usage.py`) — how
* many, per day, by which agents, from which documents, and the viewer's own
* newest ones (a conversation is its person's, so only theirs are listed).
* Every count is of distinct answers.
*/
export const USAGE_WINDOWS = ["30", "90", "365"] as const;
type UsageWindow = (typeof USAGE_WINDOWS)[number];
const WINDOW_OPTIONS = USAGE_WINDOWS.map((days) => ({
value: days,
label: days === "365" ? "Year" : `${days} days`,
}));
/** "25 Oct", from a day the API sends as `YYYY-MM-DD` (UTC). */
const dayLabel = (day: string) => {
const [year, month, date] = day.split("-").map(Number);
return new Date(Date.UTC(year, month - 1, date)).toLocaleDateString(undefined, {
day: "numeric",
month: "short",
timeZone: "UTC",
});
};
/** The window's answers against the same length of time before it. */
const trend = (usage: KnowledgeUsage) => {
const change = usage.answers - usage.previous_answers;
const before = `the ${usage.days} days before`;
Iif (change === 0) return `Same as ${before}`;
return `${change > 0 ? "+" : "−"}${Math.abs(change).toLocaleString()} on ${before}`;
};
const agentRows = (agents: UsageRanked[]): RankedRow[] =>
agents.map((a, i) => ({
key: a.id ?? `deleted-${i}`,
label: a.name,
value: a.answers,
href: a.id ? resourceHref("agent", a.id) : null,
note: a.is_system ? "built in" : a.id ? undefined : "deleted since",
}));
const documentRows = (documents: UsageRanked[]): RankedRow[] =>
documents.map((d, i) => ({
key: d.id ?? `whole-${d.knowledge_id ?? i}`,
label: d.url ? (
<a href={d.url} target="_blank" rel="noopener noreferrer" className="hover:underline">
{d.name}
</a>
) : (
d.name
),
value: d.answers,
}));
const sourceRows = (sources: UsageRanked[]): RankedRow[] =>
sources.map((s, i) => ({
key: s.id ?? `source-${i}`,
label: s.name,
value: s.answers,
href: s.id ? resourceHref("knowledge", s.id) : null,
}));
export interface UsagePanelProps {
/** The query key prefix this resource's usage is cached under. */
queryKey: unknown[];
fetch: (days: number) => Promise<KnowledgeUsage | null>;
costSource: CostSource;
sourceId: string;
/** "source", "repository": the cost card's noun. */
noun: string;
}
const UsagePanel: React.FC<UsagePanelProps> = ({
queryKey,
fetch,
costSource,
sourceId,
noun,
}) => {
const [days, setDays] = useState<UsageWindow>("90");
const { data, isLoading } = useQuery<KnowledgeUsage | null>({
queryKey: [...queryKey, Number(days)],
queryFn: () => fetch(Number(days)),
staleTime: 60_000,
});
const usage: KnowledgeUsage | null | undefined = data;
return (
<div className="space-y-5">
<div className="flex flex-wrap items-center justify-between gap-3">
<p className="text-xs text-gray-500 dark:text-gray-400">
Answers given in this workspace that cited it.
</p>
<SegmentedFilter value={days} onChange={setDays} options={WINDOW_OPTIONS} />
</div>
<KpiStrip
tiles={[
{
label: "Answers citing it",
value: (usage?.answers ?? 0).toLocaleString(),
hint: usage ? trend(usage) : undefined,
loading: isLoading,
},
{
label: "Agents",
value: (usage?.agents.length ?? 0).toLocaleString(),
hint: "That cited it",
loading: isLoading,
},
{
label: "Documents cited",
value: (usage?.documents.filter((d) => d.id).length ?? 0).toLocaleString(),
hint: "Of its most cited",
loading: isLoading,
},
]}
/>
<SectionCard icon={BarChart3} title="Answers per day">
{isLoading ? (
<SkeletonLines count={4} />
) : !usage || usage.answers === 0 ? (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
No answer cited it in this window.
</p>
) : (
<TimeSeriesChart
points={usage.daily.map((d) => ({ label: dayLabel(d.day), value: d.answers }))}
unit="Answers"
/>
)}
</SectionCard>
<div className="grid gap-5 lg:grid-cols-2">
<SectionCard icon={Bot} title="Agents citing it">
{isLoading ? (
<SkeletonLines count={3} />
) : (
<RankedList
rows={agentRows(usage?.agents ?? [])}
unit="answers"
empty="No agent cited it in this window."
/>
)}
</SectionCard>
<SectionCard icon={FileText} title="Most cited">
{isLoading ? (
<SkeletonLines count={3} />
) : (
<RankedList
rows={documentRows(usage?.documents ?? [])}
unit="answers"
empty="Nothing of it was cited in this window."
/>
)}
</SectionCard>
</div>
{usage && usage.sources.length > 0 && (
<SectionCard
icon={Layers}
title="Its sources"
description="A source in two repositories counts in both"
>
<RankedList rows={sourceRows(usage.sources)} unit="answers" />
</SectionCard>
)}
<SectionCard
icon={MessageSquare}
title="Your answers that cited it"
description="Only your own: a conversation is its person's"
>
{isLoading ? (
<SkeletonLines count={3} />
) : !usage?.recent.length ? (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
None of your answers cited it in this window.
</p>
) : (
<ul className="divide-y divide-gray-100 dark:divide-gray-700">
{usage.recent.map((answer) => {
const href = chatMessageHref({
agentId: answer.agent_id,
conversationId: answer.conversation_id,
messageId: answer.message_id,
});
const what = answer.agent_name ?? "A deleted agent";
return (
<li
key={answer.message_id}
className="flex items-center justify-between gap-3 py-2 text-sm"
>
{href ? (
<Link to={href} className="min-w-0 truncate text-gray-700 dark:text-gray-200 hover:underline">
{what}
</Link>
) : (
<span className="min-w-0 truncate text-gray-500 dark:text-gray-400">{what}</span>
)}
<span className="shrink-0 text-xs text-gray-400 dark:text-gray-500">
{formatAgo(answer.cited_at)}
</span>
</li>
);
})}
</ul>
)}
</SectionCard>
<ResourceCostCard source={costSource} sourceId={sourceId} noun={noun} />
</div>
);
};
export default UsagePanel;
|