mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 10:50:34 +08:00
Save Draft and Apply were the same write. Both persisted to the live files
and both invalidated the runtime caches, so nothing but their labels told
them apart — and Save Draft only ever wrote the model catalog, while half
the settings pages (memory, starting points, network, attachments,
document parsing) keep their state outside it.
The result was silent data loss, reproducible in four steps: open Starting
points, change 20 to 44, press Save Draft, read "Draft saved" — the server
still had 20, and navigating away lost the edit with no warning. The
button was enabled, because `hasUnsavedChanges` counted extension state
that `saveCatalog` never touched.
So a draft is now a place rather than a label. One envelope holding the
catalog and a payload per page, stored beside the live settings and read
by nothing else:
Save draft → envelope written, runtime untouched
Apply → live files written, envelope cleared
Discard → envelope dropped, back to what is live
Two other losses closed along the way. Unsaved page state now lives on the
provider instead of the page, so moving around inside Settings keeps it —
a settings page unmounts on every navigation, which is why the edit
vanished. And an edit that exists nowhere else warns before the tab is
closed or reloaded.
The toolbar had no way to say "there is a draft waiting", so a draft saved
yesterday would have sat there unexplained; it now reports which of the
three states you are in. The storage path moved into that line's tooltip —
self-hosted users occasionally want to know which file backs a page, but
it is a developer fact and was on screen for everyone.
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { apiFetch, apiUrl } from "@/lib/api";
|
|
|
|
/**
|
|
* Settings pages that keep state outside the model catalog.
|
|
*
|
|
* Each one registers with `registerExtension(key, …)` under the key below and
|
|
* saves by PUT-ing its own payload to the matching endpoint. The table exists
|
|
* so a draft can be applied for a page that is not on screen: the user edits
|
|
* Starting points, navigates to LLM, and presses Apply — nothing is mounted to
|
|
* run that page's own save closure any more, but its drafted payload is still
|
|
* pending and has to land somewhere.
|
|
*
|
|
* The pages import their URL from here rather than writing it twice, so the
|
|
* two paths cannot drift apart.
|
|
*/
|
|
export const EXTENSION_ENDPOINTS = {
|
|
"chat-starters": "/api/v1/settings/chat-starters",
|
|
"chat-attachments": "/api/v1/settings/chat-attachments",
|
|
"chat-timeout": "/api/v1/settings/chat-response-timeout",
|
|
capabilities: "/api/v1/capabilities/settings",
|
|
memory: "/api/v1/memory/settings",
|
|
network: "/api/v1/settings/network",
|
|
} as const;
|
|
|
|
export type ExtensionKey = keyof typeof EXTENSION_ENDPOINTS;
|
|
|
|
export function isExtensionKey(value: string): value is ExtensionKey {
|
|
return value in EXTENSION_ENDPOINTS;
|
|
}
|
|
|
|
/** Write one drafted payload to the endpoint that owns it. */
|
|
export async function applyExtensionPayload(
|
|
key: string,
|
|
payload: unknown,
|
|
): Promise<void> {
|
|
if (!isExtensionKey(key) || payload == null) return;
|
|
const response = await apiFetch(apiUrl(EXTENSION_ENDPOINTS[key]), {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`${key}: HTTP ${response.status}`);
|
|
}
|
|
}
|