2026-09-15 07:54:50 +03:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
2026-09-23 12:50:59 +03:00
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2026-09-15 07:54:50 +03:00
|
|
|
|
import { BookOpenIcon } from "lucide-react";
|
|
|
|
|
|
import {
|
|
|
|
|
|
GRAPH_CONTEXT_TOKENS_DEFAULT,
|
2026-09-15 08:09:56 +03:00
|
|
|
|
GRAPH_CONTEXT_TOKENS_STOPS,
|
|
|
|
|
|
type ChatSettings,
|
2026-09-15 07:54:50 +03:00
|
|
|
|
} from "@/src/core";
|
2026-09-23 12:50:59 +03:00
|
|
|
|
import { useSettingsSaved } from "@/components/settings-signal";
|
|
|
|
|
|
import { useSettingsDialog } from "./settings/settings-dialog.tsx";
|
2026-09-15 07:54:50 +03:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The knowledge-context budget control (vision-context-injection.md) — the
|
|
|
|
|
|
* "session data" section's one input, shown where sessions START: the home
|
2026-09-15 08:00:04 +03:00
|
|
|
|
* (graph) view's new-session dock and the /chat/new draft. It renders in
|
|
|
|
|
|
* the StatsBar's LEFT slot — the very place session data (context/cost)
|
|
|
|
|
|
* lives once a session exists, which pre-chat is free real estate (just
|
|
|
|
|
|
* "—"). Input before output: the panel that will later show what the
|
|
|
|
|
|
* session contributed to the graph asks first what the graph will
|
|
|
|
|
|
* contribute to the session.
|
2026-09-15 07:54:50 +03:00
|
|
|
|
*
|
2026-09-15 08:09:56 +03:00
|
|
|
|
* A DISCRETE 6-position slider (owner, 2026-09-15): off, then powers of four
|
|
|
|
|
|
* from 64 — a continuous 0–4096 dial was precision theater (a 128-token
|
|
|
|
|
|
* budget is ~2 propositions). The native range input walks positions 0–5
|
|
|
|
|
|
* and maps through GRAPH_CONTEXT_TOKENS_STOPS; every stop is a real
|
|
|
|
|
|
* decision. Non-stop values from a stale client snap to the nearest stop
|
|
|
|
|
|
* for display. The readout shows the FULL number (no k-rounding). The
|
|
|
|
|
|
* value rides ChatSettings (the same lifted settings the model picker
|
|
|
|
|
|
* uses), so it persists pre-chat via the same deferred PUT. Sessions
|
|
|
|
|
|
* never show this control — the injection is one-shot at session start.
|
2026-09-15 07:54:50 +03:00
|
|
|
|
*/
|
|
|
|
|
|
export function GraphContextControl({
|
|
|
|
|
|
value,
|
|
|
|
|
|
onChange,
|
|
|
|
|
|
testId = "graph-context-control",
|
|
|
|
|
|
}: {
|
|
|
|
|
|
value: number;
|
|
|
|
|
|
onChange: (tokens: number) => void;
|
|
|
|
|
|
testId?: string;
|
|
|
|
|
|
}) {
|
2026-09-23 12:50:59 +03:00
|
|
|
|
// Wave 6: context injection rides the embedding model — with none
|
|
|
|
|
|
// configured (graph.embeddingModel null) the control is inert, so it
|
|
|
|
|
|
// grays out and its hint deep-links to the Graph section's picker.
|
|
|
|
|
|
// Loading renders the normal control (the searchStatus precedent:
|
|
|
|
|
|
// never gray on null/unknown). Every settings save re-reads.
|
|
|
|
|
|
const { open: openSettingsSection } = useSettingsDialog();
|
|
|
|
|
|
const [modelUnset, setModelUnset] = useState(false);
|
|
|
|
|
|
const [known, setKnown] = useState(false);
|
|
|
|
|
|
const readModel = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const resp = await fetch("/api/settings");
|
|
|
|
|
|
if (!resp.ok) return;
|
|
|
|
|
|
const body = (await resp.json()) as { settings?: { graph?: { embeddingModel?: string | null } } };
|
|
|
|
|
|
setModelUnset(!body.settings?.graph?.embeddingModel);
|
|
|
|
|
|
setKnown(true);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// leave the last known state
|
|
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
void readModel();
|
|
|
|
|
|
}, [readModel]);
|
|
|
|
|
|
useSettingsSaved(() => {
|
|
|
|
|
|
void readModel();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (known && modelUnset) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div
|
|
|
|
|
|
data-testid={`${testId}-needs-model`}
|
|
|
|
|
|
className="flex items-center gap-2 text-xs text-muted-foreground opacity-40"
|
|
|
|
|
|
>
|
|
|
|
|
|
<BookOpenIcon className="size-3 shrink-0" />
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => openSettingsSection("graph")}
|
|
|
|
|
|
className="underline underline-offset-2 transition-colors hover:text-foreground"
|
|
|
|
|
|
>
|
|
|
|
|
|
Graph context needs an embedding model
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-15 08:09:56 +03:00
|
|
|
|
// Snap for display only: a legacy non-stop value shows its nearest stop
|
|
|
|
|
|
// and the next drag lands on a real one.
|
|
|
|
|
|
const stop = nearestStop(value);
|
|
|
|
|
|
// indexOf on a readonly tuple rejects a plain number; the snap above
|
|
|
|
|
|
// always yields a member, so the cast is honest.
|
|
|
|
|
|
const index = (GRAPH_CONTEXT_TOKENS_STOPS as readonly number[]).indexOf(stop);
|
2026-09-15 07:54:50 +03:00
|
|
|
|
return (
|
2026-09-15 08:00:04 +03:00
|
|
|
|
<div data-testid={testId} className="flex items-center gap-2 text-xs text-muted-foreground">
|
2026-09-15 07:54:50 +03:00
|
|
|
|
<BookOpenIcon className="size-3 shrink-0" />
|
2026-09-18 18:03:25 +03:00
|
|
|
|
<span className="shrink-0">Graph context</span>
|
2026-09-15 07:54:50 +03:00
|
|
|
|
<input
|
|
|
|
|
|
type="range"
|
|
|
|
|
|
min={0}
|
2026-09-15 08:09:56 +03:00
|
|
|
|
max={GRAPH_CONTEXT_TOKENS_STOPS.length - 1}
|
|
|
|
|
|
step={1}
|
|
|
|
|
|
value={index}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
const next = (GRAPH_CONTEXT_TOKENS_STOPS as readonly number[])[Number(e.target.value)];
|
|
|
|
|
|
if (next !== undefined) onChange(next);
|
|
|
|
|
|
}}
|
2026-09-15 07:54:50 +03:00
|
|
|
|
aria-label="Prior-knowledge token budget"
|
2026-09-15 08:00:04 +03:00
|
|
|
|
className="h-1 w-24 shrink-0 accent-primary"
|
2026-09-15 07:54:50 +03:00
|
|
|
|
data-testid="graph-context-slider"
|
|
|
|
|
|
/>
|
2026-09-23 00:57:28 +03:00
|
|
|
|
{/* Content-sized (owner, fix wave): the control hugs its value —
|
|
|
|
|
|
no fixed-width readout means no dead gap when "off", and the
|
|
|
|
|
|
dictation strip in the stats bar's middle always fills whatever
|
|
|
|
|
|
space the slider leaves. tabular-nums keeps the digits steady. */}
|
2026-09-15 07:54:50 +03:00
|
|
|
|
<span
|
2026-09-23 00:57:28 +03:00
|
|
|
|
className="shrink-0 tabular-nums"
|
2026-09-15 07:54:50 +03:00
|
|
|
|
data-testid="graph-context-value"
|
|
|
|
|
|
>
|
2026-09-15 08:02:07 +03:00
|
|
|
|
{value === 0 ? "off" : `${value} tok`}
|
2026-09-15 07:54:50 +03:00
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-15 08:09:56 +03:00
|
|
|
|
/** The nearest stop to any budget value (display snapping). */
|
|
|
|
|
|
export function nearestStop(value: number): number {
|
|
|
|
|
|
let best: number = GRAPH_CONTEXT_TOKENS_STOPS[0] ?? 0;
|
|
|
|
|
|
for (const s of GRAPH_CONTEXT_TOKENS_STOPS) {
|
|
|
|
|
|
if (Math.abs(s - value) < Math.abs(best - value)) best = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
return best;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-15 07:54:50 +03:00
|
|
|
|
/** The control's effective value for a chat's settings — absent = the
|
|
|
|
|
|
* default (on), never 0: the decided default is a modest slice of prior
|
|
|
|
|
|
* knowledge for every fresh session. */
|
2026-09-15 08:09:56 +03:00
|
|
|
|
export function effectiveContextTokens(
|
|
|
|
|
|
settings: Pick<ChatSettings, "graphContextTokens"> | undefined,
|
|
|
|
|
|
): number {
|
2026-09-15 07:54:50 +03:00
|
|
|
|
return settings?.graphContextTokens ?? GRAPH_CONTEXT_TOKENS_DEFAULT;
|
|
|
|
|
|
}
|