Files
DeepTutor/web/lib/cli-apps-api.ts
Bingxi Zhao (Frank) 44fa7a1552 release: v1.5.8
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
2026-08-02 13:23:45 +08:00

272 lines
8.5 KiB
TypeScript

import { apiFetch, apiUrl } from "@/lib/api";
/** Mirrors `deeptutor/api/routers/space_cli_apps.py`. */
export const CLI_APPS_BASE_PATH = "/api/v1/space/cli-apps";
/** Where the code comes from — the only honest input to "should I install this?". */
export type CliAppTrust = "first-party" | "third-party";
export type CliAppRuntime = "python" | "node" | "none";
/** One installed app, as the calling account sees it. */
export interface CliApp {
id: string;
display_name: string;
description: string;
category: string;
/** The name the chat agent calls it by, e.g. `cli_blender`. */
tool_name: string;
entry_point: string;
runtime: CliAppRuntime;
installed_at: string;
version: string;
/** Commit or ref this install is fixed to; `""` when it floats. */
pin: string;
trust: CliAppTrust;
/** Whether an administrator granted this account access to it. */
granted: boolean;
/** Whether it is switched on for this account. False whenever not granted. */
enabled: boolean;
/** False once the snapshot no longer lists it: removable, not offerable. */
in_catalog: boolean;
}
/** Why the list may be unusable even when it is not empty. */
export interface CliAppAccess {
/** True for an administrator: every installed app is available. */
unrestricted: boolean;
/** True when the account's grant denies code execution outright. */
exec_denied: boolean;
}
export interface CliAppState {
apps: CliApp[];
access: CliAppAccess;
/** The reviewed CLI-Anything commit first-party installs are pinned to. */
catalog_pin: string;
/** Install output, present only on the response to an install. */
log?: string;
}
export interface CliCatalogEntry {
id: string;
display_name: string;
description: string;
category: string;
origin: string;
trust: CliAppTrust;
requires: string;
homepage: string;
source_url: string;
entry_point: string;
runtime: CliAppRuntime;
install_kind: string;
/** The requirement or package that would be installed, including any pin. */
install_target: string;
installable: boolean;
/** Whether the install resolves to one fixed revision. */
pinned: boolean;
/** Present only when `installable` is false, and written for a person. */
unsupported_reason: string;
install_notes: string;
installed: boolean;
}
export interface CliCatalogPage {
entries: CliCatalogEntry[];
/** Empty once the last page has been served. */
next_cursor: string;
total: number;
categories: Record<string, number>;
catalog_pin: string;
}
/**
* A refused request, carrying the backend's reason and — for a failed install —
* its output. The log is the only actionable thing about a failed install, so it
* rides on the error rather than being left on the server.
*/
export class CliAppError extends Error {
readonly code: string;
readonly log: string;
constructor(message: string, code = "", log = "") {
super(message);
this.name = "CliAppError";
this.code = code;
this.log = log;
}
}
async function asJson(response: Response) {
if (!response.ok) {
let message = `${response.status} ${response.statusText}`;
let code = "";
let log = "";
try {
const body = await response.json();
const detail = (body as { detail?: unknown } | null)?.detail;
if (detail && typeof detail === "object") {
const shaped = detail as {
code?: unknown;
message?: unknown;
log?: unknown;
};
code = String(shaped.code ?? "");
log = String(shaped.log ?? "");
if (shaped.message) message = String(shaped.message);
} else if (detail) {
message = String(detail);
}
} catch {
/* keep the status line */
}
throw new CliAppError(message, code, log);
}
return response.json();
}
function str(value: unknown, fallback = ""): string {
return value === undefined || value === null ? fallback : String(value);
}
function normalizeApp(raw: unknown): CliApp {
const item = (raw ?? {}) as Record<string, unknown>;
return {
id: str(item.id),
display_name: str(item.display_name),
description: str(item.description),
category: str(item.category),
tool_name: str(item.tool_name),
entry_point: str(item.entry_point),
runtime: (str(item.runtime, "python") as CliAppRuntime) ?? "python",
installed_at: str(item.installed_at),
version: str(item.version),
pin: str(item.pin),
trust: (str(item.trust, "third-party") as CliAppTrust) ?? "third-party",
granted: Boolean(item.granted),
enabled: Boolean(item.enabled),
// Absent field reads as "still catalogued": treating it as withdrawn would
// hide a working app behind a warning.
in_catalog: item.in_catalog === undefined ? true : Boolean(item.in_catalog),
};
}
function normalizeState(raw: unknown): CliAppState {
const data = (raw ?? {}) as Record<string, unknown>;
const access = (data.access ?? {}) as Record<string, unknown>;
return {
apps: Array.isArray(data.apps) ? data.apps.map(normalizeApp) : [],
access: {
unrestricted: Boolean(access.unrestricted),
exec_denied: Boolean(access.exec_denied),
},
catalog_pin: str(data.catalog_pin),
...(data.log === undefined ? {} : { log: str(data.log) }),
};
}
function normalizeEntry(raw: unknown): CliCatalogEntry {
const item = (raw ?? {}) as Record<string, unknown>;
return {
id: str(item.id),
display_name: str(item.display_name),
description: str(item.description),
category: str(item.category),
origin: str(item.origin),
trust: (str(item.trust, "third-party") as CliAppTrust) ?? "third-party",
requires: str(item.requires),
homepage: str(item.homepage),
source_url: str(item.source_url),
entry_point: str(item.entry_point),
runtime: (str(item.runtime, "python") as CliAppRuntime) ?? "python",
install_kind: str(item.install_kind),
install_target: str(item.install_target),
installable: Boolean(item.installable),
pinned: Boolean(item.pinned),
unsupported_reason: str(item.unsupported_reason),
install_notes: str(item.install_notes),
installed: Boolean(item.installed),
};
}
export async function getCliApps(): Promise<CliAppState> {
const response = await apiFetch(apiUrl(`${CLI_APPS_BASE_PATH}/apps`), {
cache: "no-store",
});
return normalizeState(await asJson(response));
}
export async function setCliAppEnabled(
appId: string,
enabled: boolean,
): Promise<CliAppState> {
const response = await apiFetch(
apiUrl(`${CLI_APPS_BASE_PATH}/apps/${encodeURIComponent(appId)}/enabled`),
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ enabled }),
},
);
return normalizeState(await asJson(response));
}
export async function getCliCatalog(
query: {
q?: string;
category?: string;
installableOnly?: boolean;
cursor?: string;
limit?: number;
} = {},
): Promise<CliCatalogPage> {
const params = new URLSearchParams();
if (query.q) params.set("q", query.q);
if (query.category) params.set("category", query.category);
if (query.installableOnly === false) params.set("installable_only", "false");
if (query.cursor) params.set("cursor", query.cursor);
if (query.limit) params.set("limit", String(query.limit));
const search = params.toString();
const response = await apiFetch(
apiUrl(`${CLI_APPS_BASE_PATH}/catalog${search ? `?${search}` : ""}`),
{ cache: "no-store" },
);
const data = await asJson(response);
const counts = (data?.categories ?? {}) as Record<string, unknown>;
return {
entries: Array.isArray(data?.entries)
? data.entries.map(normalizeEntry)
: [],
next_cursor: str(data?.next_cursor),
total: typeof data?.total === "number" ? data.total : 0,
categories: Object.fromEntries(
Object.entries(counts).map(([category, count]) => [
category,
typeof count === "number" ? count : 0,
]),
),
catalog_pin: str(data?.catalog_pin),
};
}
/** Administrator only; a non-admin caller gets a 403 from the route itself. */
export async function installCliApp(appId: string): Promise<CliAppState> {
const response = await apiFetch(
apiUrl(
`${CLI_APPS_BASE_PATH}/catalog/${encodeURIComponent(appId)}/install`,
),
{ method: "POST" },
);
return normalizeState(await asJson(response));
}
/** Administrator only. */
export async function uninstallCliApp(appId: string): Promise<CliAppState> {
const response = await apiFetch(
apiUrl(`${CLI_APPS_BASE_PATH}/apps/${encodeURIComponent(appId)}`),
{ method: "DELETE" },
);
return normalizeState(await asJson(response));
}