mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:39:47 +08:00
After each reply the chat composer shows a predicted next line as its placeholder, and Tab accepts it. When the assistant has just asked a question, such as a quiz in chat, the likeliest next line is the answer, so the hint gives it away. There was no way to turn it off. Add an `ask_hint_enabled` switch to the per-user starter settings (Settings -> Conversation -> Composer), on by default. When it is off, `get_ask_hint` returns an empty hint before reading the session or the cache, so no LLM call is made and no line cached while it was on leaks through. A PUT that omits the field keeps the stored value, so an older client or a settings draft saved before this change cannot turn the hint back on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
221 lines
7.3 KiB
TypeScript
221 lines
7.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
import Link from "next/link";
|
|
import { ArrowUpRight, Loader2 } from "lucide-react";
|
|
import ChatResponseTimeoutSection from "@/components/settings/ChatResponseTimeoutSection";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
SettingRow,
|
|
SettingSection,
|
|
SettingsPageHeader,
|
|
inputClass,
|
|
} from "@/components/settings/shared";
|
|
import { Toggle } from "@/components/settings/Toggle";
|
|
import { useSettings } from "@/features/settings/store/SettingsStore";
|
|
import { apiFetch, apiUrl } from "@/lib/api";
|
|
import { EXTENSION_ENDPOINTS } from "@/lib/settings-extensions";
|
|
|
|
type StarterSettings = { trace_count: number; ask_hint_enabled: boolean };
|
|
|
|
type StarterSettingsPayload = {
|
|
settings: StarterSettings;
|
|
bounds: { trace_count: [number, number] };
|
|
};
|
|
|
|
export default function StarterSettingsPage() {
|
|
const { t } = useTranslation();
|
|
const { registerExtension, pendingExtensionPayload, draftRevision } =
|
|
useSettings();
|
|
const [payload, setPayload] = useState<StarterSettingsPayload | null>(null);
|
|
const [draft, setDraft] = useState<StarterSettings | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
async function load() {
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
const response = await apiFetch(
|
|
apiUrl(EXTENSION_ENDPOINTS["chat-starters"]),
|
|
);
|
|
const data = (await response.json().catch(() => ({}))) as
|
|
StarterSettingsPayload | { detail?: string };
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
"detail" in data && data.detail
|
|
? String(data.detail)
|
|
: t("Failed to load starter settings."),
|
|
);
|
|
}
|
|
if (cancelled) return;
|
|
const next = data as StarterSettingsPayload;
|
|
setPayload(next);
|
|
// An edit left behind earlier in this session (or parked in a saved
|
|
// draft) wins over the server value — leaving the page is not a way
|
|
// to discard changes. Spread over the server value because a draft
|
|
// parked before a field existed does not carry it.
|
|
const pending = pendingExtensionPayload("chat-starters") as
|
|
Partial<StarterSettings> | undefined;
|
|
setDraft({ ...next.settings, ...pending });
|
|
} catch (err) {
|
|
if (!cancelled)
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
} finally {
|
|
if (!cancelled) setLoading(false);
|
|
}
|
|
}
|
|
load();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [draftRevision, pendingExtensionPayload, t]);
|
|
|
|
const dirty = useMemo(
|
|
() =>
|
|
Boolean(
|
|
payload &&
|
|
draft &&
|
|
(payload.settings.trace_count !== draft.trace_count ||
|
|
payload.settings.ask_hint_enabled !== draft.ask_hint_enabled),
|
|
),
|
|
[draft, payload],
|
|
);
|
|
|
|
// Flush through the global Apply (top toolbar) instead of a local button.
|
|
const draftRef = useRef(draft);
|
|
draftRef.current = draft;
|
|
const save = useCallback(async () => {
|
|
const current = draftRef.current;
|
|
if (!current) return;
|
|
setError(null);
|
|
try {
|
|
const response = await apiFetch(
|
|
apiUrl(EXTENSION_ENDPOINTS["chat-starters"]),
|
|
{
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(current),
|
|
},
|
|
);
|
|
const data = (await response.json().catch(() => ({}))) as
|
|
StarterSettingsPayload | { detail?: string };
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
"detail" in data && data.detail
|
|
? String(data.detail)
|
|
: t("Failed to save starter settings."),
|
|
);
|
|
}
|
|
const next = data as StarterSettingsPayload;
|
|
setPayload(next);
|
|
setDraft({ ...next.settings });
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
throw err;
|
|
}
|
|
}, [t]);
|
|
|
|
useEffect(() => {
|
|
registerExtension("chat-starters", { dirty, save, payload: draft });
|
|
return () => registerExtension("chat-starters", null);
|
|
}, [dirty, draft, save, registerExtension]);
|
|
|
|
const bounds = payload?.bounds.trace_count;
|
|
|
|
return (
|
|
<div>
|
|
<SettingsPageHeader
|
|
title={t("Conversation")}
|
|
description={t(
|
|
"Manage home suggestions and how long DeepTutor waits for replies.",
|
|
)}
|
|
/>
|
|
|
|
<ChatResponseTimeoutSection />
|
|
|
|
{loading && (
|
|
<div className="flex items-center gap-2 text-[13px] text-[var(--muted-foreground)]">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
{t("Loading starter settings...")}
|
|
</div>
|
|
)}
|
|
|
|
{!loading && error && (
|
|
<div className="mb-5 rounded-xl border border-red-500/30 bg-red-500/10 px-4 py-3 text-[13px] text-red-600 dark:text-red-300">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
{!loading && payload && draft && (
|
|
<SettingSection
|
|
title={t("Starting points")}
|
|
description={t(
|
|
"Your consolidated memory is always included. This controls how much raw recent activity goes with it — conversations, practice questions, searches, documents, in one list ordered by time.",
|
|
)}
|
|
>
|
|
<SettingRow
|
|
title={t("Recent activities")}
|
|
description={t(
|
|
"More gives the model a longer view of what you have touched; fewer keeps it focused on this week. Takes effect the next time the lines are regenerated.",
|
|
)}
|
|
control={
|
|
<input
|
|
className={`${inputClass} w-28`}
|
|
type="number"
|
|
min={bounds?.[0] ?? 3}
|
|
max={bounds?.[1] ?? 100}
|
|
value={draft.trace_count}
|
|
onChange={(event) =>
|
|
setDraft((current) =>
|
|
current
|
|
? { ...current, trace_count: Number(event.target.value) }
|
|
: current,
|
|
)
|
|
}
|
|
/>
|
|
}
|
|
/>
|
|
</SettingSection>
|
|
)}
|
|
|
|
{!loading && payload && draft && (
|
|
<SettingSection title={t("Composer")}>
|
|
<SettingRow
|
|
title={t("Suggest my next message")}
|
|
description={t(
|
|
"After each reply, the input box shows a guess at what you might type next; Tab accepts it. Turn this off when practicing in chat — the likeliest next message to a question is its answer.",
|
|
)}
|
|
control={
|
|
<Toggle
|
|
checked={draft.ask_hint_enabled}
|
|
onChange={(next) =>
|
|
setDraft((current) =>
|
|
current ? { ...current, ask_hint_enabled: next } : current,
|
|
)
|
|
}
|
|
/>
|
|
}
|
|
/>
|
|
</SettingSection>
|
|
)}
|
|
|
|
{!loading && payload && (
|
|
<p className="mt-5 text-[11.5px] leading-relaxed text-[var(--muted-foreground)]">
|
|
{t("These lines are written by the active model unless you pin one.")}{" "}
|
|
<Link
|
|
href="/settings/task-models"
|
|
className="inline-flex items-center gap-1 text-[var(--foreground)] underline-offset-2 hover:underline"
|
|
>
|
|
{t("Task models")}
|
|
<ArrowUpRight className="h-3 w-3" />
|
|
</Link>
|
|
</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|