All files / src/components/chat/ExecutionPipeline KnowledgeLanes.tsx

93.02% Statements 40/43
75% Branches 51/68
92.3% Functions 12/13
94.73% Lines 36/38

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                                                16x 16x     5x       3x 3x 3x 3x 1x       3x 3x     3x         16x 3x 3x                       5x                             2x                         16x       4x               4x 4x       27x                                                         16x       2x 2x 2x 2x 2x 2x                           16x 3x                   4x                                                 16x     9x 9x 3x                             4x                            
import {
  PROFILE_LABELS,
  STAGE_STATE_STYLES,
  formatDuration,
  kindLabel,
  sysLbl,
} from "@/components/chat/ExecutionPipeline/nodeMeta";
import {
  KnowledgeMix,
  KnowledgePlanResult,
  LanePacking,
  LaneSearchResult,
} from "@/objects/Model";
import { PipelineAttempt } from "@/util/pipelineUtils";
import React from "react";
 
/**
 * How a turn searched its knowledge in lanes, as the retrieval stage shows
 * it: the mix (which kinds ran, in what role, and why; what was left out),
 * each lane's funnel from queries to what it packed, when it ran beside the
 * others, and how the join packed them. Counts and fixed reasons only:
 * nothing here quotes a passage.
 */
 
const noteCls = "text-[11px] text-gray-500 dark:text-gray-400 leading-relaxed";
const figureCls = "font-mono text-[10px] text-gray-500 dark:text-gray-400 tabular-nums";
 
function percent(share: number): string {
  return `${Math.round(share * 100)}%`;
}
 
function signalsLine(mix: KnowledgeMix): string {
  const parts: string[] = [];
  Eif (mix.signals.complexity) parts.push(mix.signals.complexity);
  if (mix.signals.live) parts.push("about what is current");
  if (mix.signals.window_days) {
    parts.push(
      `the past ${mix.signals.window_days} day${mix.signals.window_days === 1 ? "" : "s"}`,
    );
  }
  const named = mix.signals.named?.length ?? 0;
  Iif (named > 0) {
    parts.push(`names ${named} knowledge base${named === 1 ? "" : "s"}`);
  }
  return parts.join(" · ");
}
 
/** Which kinds of knowledge the turn searched, in what role and share, and
 *  what it left out — the turn's mix. */
export const MixNote: React.FC<{ mix: KnowledgeMix }> = ({ mix }) => {
  const signals = signalsLine(mix);
  return (
    <div className="space-y-1">
      <p className={sysLbl}>Knowledge mix</p>
      <p className={noteCls}>
        {PROFILE_LABELS[mix.profile] ?? mix.profile}
        {signals && (
          <span className="text-gray-400 dark:text-gray-500">{` — ${signals}`}</span>
        )}
      </p>
      {mix.lanes.length > 0 && (
        <ul className="space-y-0.5">
          {mix.lanes.map((lane) => (
            <li key={lane.category} className={noteCls}>
              <span className="font-semibold text-gray-600 dark:text-gray-300">
                {kindLabel(lane.category)}
              </span>
              {` — ${lane.role}, ${percent(lane.share)} of the budget`}
              {lane.reason && (
                <span className="block text-gray-400 dark:text-gray-500">
                  {lane.reason}
                </span>
              )}
            </li>
          ))}
        </ul>
      )}
      {mix.scoped_out.map((left) => (
        <p key={`${left.category}:${left.kb_ids.join(",")}`} className={noteCls}>
          {`${kindLabel(left.category)} left out`}
          {left.reason && (
            <span className="text-gray-400 dark:text-gray-500">{` — ${left.reason}`}</span>
          )}
        </p>
      ))}
    </div>
  );
};
 
/** One lane's funnel: queries, found, judged, ruled out, out of window,
 *  handed on, and what the join packed of it. */
export const LaneFunnel: React.FC<{
  lane: LaneSearchResult;
  packed?: LanePacking;
}> = ({ lane, packed }) => {
  const steps: [string, number][] = [
    [lane.queries.length === 1 ? "query" : "queries", lane.queries.length],
    ["found", lane.found],
    ["judged", lane.judged],
    ["ruled out", lane.ruled_out],
    ["out of window", lane.out_of_window],
    ["handed on", lane.handed_on],
  ];
  if (packed) steps.push(["packed", packed.packed]);
  return (
    <div className="space-y-0.5">
      <p className={figureCls}>
        {steps.map(([label, n], i) => (
          <span key={label}>
            {i > 0 && <span className="text-gray-300 dark:text-gray-600">{" → "}</span>}
            <span className="font-semibold text-gray-600 dark:text-gray-300">{n}</span>
            {` ${label}`}
          </span>
        ))}
      </p>
      {lane.queries.length > 0 && (
        <p className="text-[10px] text-gray-400 dark:text-gray-500 truncate" title={lane.queries.join(" | ")}>
          {lane.queries.join(" | ")}
        </p>
      )}
      {lane.judge_failure && (
        <p className="text-[10px] text-amber-700 dark:text-amber-300">
          {`Relevance judge failed (${lane.judge_failure}): its passages were kept unjudged.`}
        </p>
      )}
      {lane.degraded.map((line, i) => (
        <p key={i} className="text-[10px] text-amber-700 dark:text-amber-300">{line}</p>
      ))}
      {lane.error && (
        <p className="text-[10px] text-red-600 dark:text-red-400">{`Search failed: ${lane.error}`}</p>
      )}
    </div>
  );
};
 
/** When one branch ran within its pass: a bar from its start to its end,
 *  so branches that ran side by side read as such. Nothing without times. */
export const BranchTimeline: React.FC<{
  attempt: PipelineAttempt;
  span: { start: number; length: number } | null;
}> = ({ attempt, span }) => {
  const { started_ms, ended_ms, duration_ms } = attempt.execution;
  Iif (!span || span.length <= 0 || typeof started_ms !== "number") return null;
  const end = ended_ms ?? started_ms + (duration_ms ?? 0);
  const left = ((started_ms - span.start) / span.length) * 100;
  const width = Math.max(((end - started_ms) / span.length) * 100, 1);
  return (
    <div
      className="relative h-1 rounded bg-gray-100 dark:bg-gray-700/60"
      aria-label={`Ran from ${formatDuration(started_ms - span.start) || "0ms"} for ${formatDuration(end - started_ms) || "0ms"}`}
    >
      <span
        className={`absolute top-0 h-1 rounded ${STAGE_STATE_STYLES[attempt.state].connector}`}
        style={{ left: `${Math.max(0, Math.min(left, 99))}%`, width: `${Math.min(width, 100)}%` }}
      />
    </div>
  );
};
 
/** The lanes a plan started, each with its role and own queries. */
export const PlanLanes: React.FC<{ plan: KnowledgePlanResult }> = ({ plan }) => (
  <div className="space-y-1">
    {plan.mode === "gap" && plan.gaps.length > 0 && (
      <p className={noteCls}>{`Corrective pass, for: ${plan.gaps.join("; ")}`}</p>
    )}
    {plan.mode === "agentic" && (
      <p className={noteCls}>
        No lanes: the knowledge is searched with its tools, in the join.
      </p>
    )}
    {plan.lanes.map((lane) => (
      <div key={lane.category} className="space-y-0.5">
        <p className={noteCls}>
          <span className="font-semibold text-gray-600 dark:text-gray-300">
            {kindLabel(lane.category)}
          </span>
          {lane.role && ` — ${lane.role}`}
          {` · ${lane.kb_ids.length} knowledge base${lane.kb_ids.length === 1 ? "" : "s"}`}
        </p>
        {lane.queries.length > 0 && (
          <p className="text-[10px] font-mono text-gray-400 dark:text-gray-500 break-words">
            {lane.queries.join(" | ")}
          </p>
        )}
      </div>
    ))}
    {plan.planner_failure && (
      <p className="text-[10px] text-amber-700 dark:text-amber-300">
        {`Query planner failed (${plan.planner_failure}): each lane searched the request as written.`}
      </p>
    )}
  </div>
);
 
/** How the join packed each lane: its share of the budget, what it used,
 *  the passages it packed, and the budget it took over from the others. */
export const PackingTable: React.FC<{ lanes: Record<string, LanePacking> }> = ({
  lanes,
}) => {
  const rows = Object.entries(lanes);
  if (rows.length === 0) return null;
  return (
    <div>
      <p className={sysLbl}>Packed by lane</p>
      <table className="w-full text-[10px] tabular-nums text-gray-500 dark:text-gray-400">
        <thead>
          <tr className="text-left text-gray-400 dark:text-gray-500">
            <th className="font-medium">Lane</th>
            <th className="font-medium text-right">Budget</th>
            <th className="font-medium text-right">Used</th>
            <th className="font-medium text-right">Passages</th>
            <th className="font-medium text-right">Taken over</th>
          </tr>
        </thead>
        <tbody>
          {rows.map(([kind, p]) => (
            <tr key={kind}>
              <td>{`${kindLabel(kind)}${p.role ? ` · ${p.role}` : ""}`}</td>
              <td className="text-right">{p.budget}</td>
              <td className="text-right">{p.used}</td>
              <td className="text-right">{p.packed}</td>
              <td className="text-right">{p.spilled_in || "—"}</td>
            </tr>
          ))}
        </tbody>
      </table>
      <p className="text-[10px] text-gray-400 dark:text-gray-500">Budget and use in tokens.</p>
    </div>
  );
};