mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:09:24 +08:00
Memory. A v1.5.7 source deployment grew past 14 GB before V8 aborted; the
causes were independent, so this cut fixes each one and bounds what was
previously unbounded.
- The dev server was never actually capped: `next dev` renders in a worker it
spawns, and V8 flags in argv are not inherited, so
`node --max-old-space-size=4096 .../next dev` never reached the process
holding the memory. Next then substituted 50% of total RAM — a 12 GB heap on
a 24 GB machine. web/scripts/dev.mjs routes the ceiling through NODE_OPTIONS
(which the worker inherits and Next honors), clamped to 4 GB and sized off
the cgroup limit in a container.
- Source installs serve a production build: `deeptutor start` builds once into
.next-deeptutor and reuses it until a fingerprint over the web/ tree, the
version file, or the build-time public settings changes. `--dev` is the
opt-in for HMR and keeps using .next, so neither mode invalidates the other.
- LLM clients and providers come from a bounded event-loop-local LRU pool of 2
instead of one per turn, closed on shutdown and retired on a settings
reload; only the selected backend's SDK is imported.
- Caches gained ceilings: knowledge-task log streams cap events and bytes per
task with a 1h TTL and a completion tombstone, the completed-task table
keeps 256, the LlamaIndex index cache drops 8 -> 2 with a 10-minute idle
evict, and a book runtime is released when its queue drains.
- After a turn, a finished book job, or a completed KB task, cycles are
collected and glibc is asked to return free pages via malloc_trim; the
container and launcher set MALLOC_ARENA_MAX=2 and a lower trim threshold,
both operator-overridable.
- Every uvicorn launch sets --timeout-keep-alive 300. web/proxy.ts forwards
over Node's http.globalAgent, whose 5s idle reaper matched uvicorn's default
exactly; both ends raced to close the same socket and the loser's request
died with ECONNRESET ("Failed to load sessions").
- deeptutor.agents, deeptutor.core.agentic, provider_core, the document
extractor's parser libraries and the LLM error-mapping rules resolve on
first use. `python -m deeptutor.api.run_server` no longer reloads by
default (DEEPTUTOR_DEV_RELOAD=1 restores it).
README prose and all ten translated READMEs now describe the production-build
default instead of dev mode. Release notes: assets/releases/ver1-5-8.md
222 lines
7.4 KiB
TypeScript
222 lines
7.4 KiB
TypeScript
import {
|
|
McpApiError,
|
|
type McpCatalogEntry,
|
|
type McpCatalogField,
|
|
type McpCatalogPage,
|
|
} from "@/lib/mcp-api";
|
|
|
|
/**
|
|
* Pure logic behind the per-user MCP store (`/space/mcp`).
|
|
*
|
|
* Everything here is deliberately free of React and of `fetch` so the rules that
|
|
* decide what the page shows — which refusal message, which category chips,
|
|
* whether Install is allowed, what "Load more" accumulates — are unit-testable
|
|
* without a DOM.
|
|
*/
|
|
|
|
/**
|
|
* Category order for the store's filter row.
|
|
*
|
|
* Mirrors the closed `CatalogCategory` enum in
|
|
* `deeptutor/services/mcp/catalog/models.py`. It is only an ordering: the counts
|
|
* decide which chips exist, so a category added on the backend still appears
|
|
* (at the end) rather than vanishing from the filter.
|
|
*/
|
|
export const MCP_CATALOG_CATEGORIES: readonly string[] = [
|
|
"search",
|
|
"docs",
|
|
"code",
|
|
"data",
|
|
"browser",
|
|
"science",
|
|
"maps",
|
|
"business",
|
|
"ai",
|
|
"utility",
|
|
];
|
|
|
|
/** The tiers a store visitor can filter by, in menu order. */
|
|
export const MCP_CATALOG_TIERS: readonly string[] = ["curated", "registry"];
|
|
|
|
export interface McpCategoryChip {
|
|
category: string;
|
|
count: number;
|
|
}
|
|
|
|
/**
|
|
* The category chips to render, in canonical order.
|
|
*
|
|
* Zero-count categories are dropped: the backend counts only the entries this
|
|
* surface can install, so a chip with no matches would open onto an empty grid.
|
|
*/
|
|
export function catalogCategoryChips(
|
|
counts: Record<string, number>,
|
|
): McpCategoryChip[] {
|
|
const known = MCP_CATALOG_CATEGORIES.filter(
|
|
(category) => counts[category] > 0,
|
|
).map((category) => ({ category, count: counts[category] }));
|
|
const extra = Object.keys(counts)
|
|
.filter(
|
|
(category) =>
|
|
counts[category] > 0 && !MCP_CATALOG_CATEGORIES.includes(category),
|
|
)
|
|
.sort()
|
|
.map((category) => ({ category, count: counts[category] }));
|
|
return [...known, ...extra];
|
|
}
|
|
|
|
// ── refusals ─────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* i18n key for a backend refusal code, or `null` when only the backend's own
|
|
* message can explain the failure.
|
|
*
|
|
* The codes come from `deeptutor/api/routers/space_mcp.py`; their messages are
|
|
* English strings assembled server-side, and several of them need an actionable
|
|
* next step rather than a translation of the cause.
|
|
*/
|
|
export function mcpRefusalKey(code: string): string | null {
|
|
return REFUSAL_KEYS[code] ?? null;
|
|
}
|
|
|
|
const REFUSAL_KEYS: Record<string, string> = {
|
|
"mcp.stdio_not_allowed": "mcp.refusal.stdioNotAllowed",
|
|
"mcp.blocked_url": "mcp.refusal.blockedUrl",
|
|
"mcp.name_reserved": "mcp.refusal.nameReserved",
|
|
"mcp.too_many_servers": "mcp.refusal.tooManyServers",
|
|
"mcp.missing_credential": "mcp.refusal.missingCredential",
|
|
"mcp.entry_admin_only": "mcp.refusal.entryAdminOnly",
|
|
};
|
|
|
|
/**
|
|
* Codes whose backend message names the actual cause, so the translated
|
|
* sentence is a preamble rather than a replacement. A blocked URL is the
|
|
* clearest case: "unresolvable host", "only http/https", and "private address"
|
|
* all arrive under one code and need different fixes from the reader.
|
|
*/
|
|
const REFUSALS_KEEPING_DETAIL = new Set(["mcp.blocked_url"]);
|
|
|
|
/**
|
|
* Human-readable copy for a failed MCP request.
|
|
*
|
|
* A mapped refusal renders as the app's own sentence; anything else falls back
|
|
* to the message the backend sent, which for a blocked URL or a transport error
|
|
* carries the only detail worth reading.
|
|
*/
|
|
export function describeMcpError(
|
|
error: unknown,
|
|
t: (key: string) => string,
|
|
): string {
|
|
if (error instanceof McpApiError && error.code) {
|
|
const key = mcpRefusalKey(error.code);
|
|
if (key) {
|
|
const detail = REFUSALS_KEEPING_DETAIL.has(error.code)
|
|
? error.message.trim()
|
|
: "";
|
|
return detail ? `${t(key)} ${detail}` : t(key);
|
|
}
|
|
}
|
|
return error instanceof Error ? error.message : String(error);
|
|
}
|
|
|
|
// ── credential form ──────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Which required credentials are still blank.
|
|
*
|
|
* Whitespace counts as blank, matching the backend's `build_server_config`,
|
|
* which strips before deciding a required field is missing — a form that
|
|
* accepted " " would send a request the backend refuses.
|
|
*/
|
|
export function missingRequiredFields(
|
|
fields: readonly McpCatalogField[],
|
|
values: Record<string, string>,
|
|
): string[] {
|
|
return fields
|
|
.filter((field) => field.required && !(values[field.key] ?? "").trim())
|
|
.map((field) => field.key);
|
|
}
|
|
|
|
/** Whether Install may be pressed for *fields* filled with *values*. */
|
|
export function canInstallEntry(
|
|
fields: readonly McpCatalogField[],
|
|
values: Record<string, string>,
|
|
): boolean {
|
|
return missingRequiredFields(fields, values).length === 0;
|
|
}
|
|
|
|
/** Drop blank values so an optional field is left unset rather than cleared. */
|
|
export function filledCredentials(
|
|
values: Record<string, string>,
|
|
): Record<string, string> {
|
|
return Object.fromEntries(
|
|
Object.entries(values).filter(([, value]) => value.trim() !== ""),
|
|
);
|
|
}
|
|
|
|
// ── pagination ───────────────────────────────────────────────────────────
|
|
|
|
export interface McpCatalogList {
|
|
entries: McpCatalogEntry[];
|
|
/** Cursor for the next page; `""` once the store is exhausted. */
|
|
cursor: string;
|
|
total: number;
|
|
categories: Record<string, number>;
|
|
}
|
|
|
|
/**
|
|
* Fold one page into the list already on screen.
|
|
*
|
|
* Pass `null` for the first page of a fresh query. Entries are de-duplicated by
|
|
* id so a re-fetched cursor (a double click on "Load more") cannot double a row,
|
|
* and `total`/`categories` always come from the newest page — the backend
|
|
* recomputes them per query.
|
|
*/
|
|
export function appendCatalogPage(
|
|
previous: McpCatalogList | null,
|
|
page: McpCatalogPage,
|
|
): McpCatalogList {
|
|
const entries = previous ? [...previous.entries] : [];
|
|
const seen = new Set(entries.map((entry) => entry.id));
|
|
for (const entry of page.entries) {
|
|
if (seen.has(entry.id)) continue;
|
|
seen.add(entry.id);
|
|
entries.push(entry);
|
|
}
|
|
return {
|
|
entries,
|
|
cursor: page.next_cursor,
|
|
total: page.total,
|
|
categories: page.categories,
|
|
};
|
|
}
|
|
|
|
// ── presentation helpers ─────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Pick *lang* out of an i18n map, degrading to the base language then English.
|
|
* Mirrors `localized_text` in the catalog models.
|
|
*/
|
|
export function localizedCatalogText(
|
|
texts: Record<string, string>,
|
|
lang: string,
|
|
): string {
|
|
for (const key of [lang, lang.split("-")[0], "en"]) {
|
|
const text = texts[key];
|
|
if (text) return text;
|
|
}
|
|
return Object.values(texts).find((text) => text) ?? "";
|
|
}
|
|
|
|
/**
|
|
* Initials for an entry's avatar. The catalog ships no logos on purpose — a
|
|
* remote logo per installed service would leak the viewer's app list to each
|
|
* vendor — so the store draws its own mark from the name.
|
|
*/
|
|
export function catalogInitials(displayName: string): string {
|
|
const words = displayName.split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
if (words.length === 0) return "?";
|
|
if (words.length === 1) return words[0].slice(0, 2).toUpperCase();
|
|
return (words[0][0] + words[1][0]).toUpperCase();
|
|
}
|