From 171417cda676ed13475b502a40611a8b626e2042 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 31 Oct 2026 16:07:43 +0800 Subject: [PATCH 1/3] feat(web): prompt-pack picker lists the full resolved pack inventory (#5319) GET /__bili/packs returns every pack the resolver can see (project > user > shipped > builtin, first-wins per name) with provenance; the settings panel's promptPack dropdown now renders those names instead of hardcoded default+lean, with a note line for the shipped chat pack. Unchanged behavior until loadConfig fills the list (falls back to the old two options on fetch failure). listResolvedPacks() shares the exact chain builder with per-request resolution. Co-authored-by: qwen3.8-27b --- src/compress-settings.ts | 36 ++++++++---- src/server/admin.ts | 12 +++- src/web/client.ts | 11 +++- src/web/i18n.ts | 2 + tests/web-pack-picker.test.ts | 107 ++++++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 tests/web-pack-picker.test.ts diff --git a/src/compress-settings.ts b/src/compress-settings.ts index ac344f4c76..95d6659ccb 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -1,4 +1,4 @@ -import { DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, defaultPrompts, resolvePrompts, createPackResolver, builtinSource, createDirPackSource, isValidPackName, type AbsorbConfig, type Config, type CcrConfig, type PackSurface, type Prompts } from "acp-kernel"; +import { DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, defaultPrompts, resolvePrompts, createPackResolver, builtinSource, createDirPackSource, isValidPackName, type AbsorbConfig, type Config, type CcrConfig, type Pack, type PackSurface, type Prompts } from "acp-kernel"; import * as path from "node:path"; import { findRoute, type CompressSettings, type NamedProviderRecipe, type ProviderRoutes } from "./config.js"; import { configDir } from "./paths.js"; @@ -167,6 +167,21 @@ interface SurfaceResolution { packVersion?: string; } +function packResolver( + dirs?: { projectDir?: string; userDirs?: readonly string[] }, +): ReturnType { + // #5319: host-shipped packs sit between the user dirs and the kernel + // builtin registry — same-name user/project files still win, and the + // kernel registry itself stays untouched (Kernel Boundary: adding a pack + // must not require a kernel/** change). + return createPackResolver([ + createDirPackSource("project", dirs?.projectDir ?? path.join(process.cwd(), ".billion-context", "packs")), + ...(dirs?.userDirs ?? [path.join(configDir(), "packs")]).map((dir) => createDirPackSource("user", dir)), + shippedPacksSource, + builtinSource, + ]); +} + /** Resolve the pack surface for one request: `promptPack` names a pack in the * host's resolver chain [project `./.billion-context/packs` > user * `/packs` > shipped host-bundled packs (`chat`, #5319) > kernel @@ -180,16 +195,7 @@ export function resolveCompressSurfaceDetailed( ): SurfaceResolution { const name = s.promptPack; if (typeof name !== "string" || name === "default" || !isValidPackName(name)) return { surface: {}, packName: "default" }; - // #5319: host-shipped packs sit between the user dirs and the kernel - // builtin registry — same-name user/project files still win, and the - // kernel registry itself stays untouched (Kernel Boundary: adding a pack - // must not require a kernel/** change). - const resolver = createPackResolver([ - createDirPackSource("project", dirs?.projectDir ?? path.join(process.cwd(), ".billion-context", "packs")), - ...(dirs?.userDirs ?? [path.join(configDir(), "packs")]).map((dir) => createDirPackSource("user", dir)), - shippedPacksSource, - builtinSource, - ]); + const resolver = packResolver(dirs); const pack = resolver.resolve(name); if (!pack) { if (!warnedUnknownPack.has(name)) { @@ -201,6 +207,14 @@ export function resolveCompressSurfaceDetailed( return { surface: pack.surface, packName: name, packVersion: pack.version }; } +/** Every pack resolvable by name right now (first-wins per name) — the panel + * picker's inventory (#5319 web half). Same chain as per-request resolution. */ +export function listResolvedPacks( + dirs?: { projectDir?: string; userDirs?: readonly string[] }, +): Pack[] { + return packResolver(dirs).listPacks(); +} + export function resolveCompressSurface( s: CompressSettings, dirs?: { projectDir?: string; userDirs?: readonly string[] }, diff --git a/src/server/admin.ts b/src/server/admin.ts index f0d7c352a8..93c84e8b7c 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -6,7 +6,7 @@ import { APIG_RESIGN_SCHEME, KNOWN_SIGNATURE_SCHEMES, readPendingRefusals, unres import { handleAcpCache, readKeySwitchStats, readModelSwitchStats, readProjSwitchStats, readPromptSwitchStats } from "../cache-ledger.js"; import type { ProxyOptions } from "../config.js"; import { effectivePromptCacheRouting, loadNamedProviders, loadOptions, loadRoutes, resolveResignSettings } from "../config.js"; -import { applyCompressSettings, resolveCompressSurfaceDetailed } from "../compress-settings.js"; +import { applyCompressSettings, listResolvedPacks, resolveCompressSurfaceDetailed } from "../compress-settings.js"; import { clearConflictEvents, summarizeConflicts } from "../conflict-watch.js"; import { cannotResolveTarget, getAdvisoryState } from "../advisory.js"; import { detectCostAdvisories } from "../plugin-advisory.js"; @@ -335,6 +335,16 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv // #4899: pricing editor data — resolved per-model prices (user overrides > // models.dev snapshot) for the panel's billing settings card. Read-only. if (req.method === "GET" && req.url === "/__bili/pricing") return handlePricingGet(res); + // #5319 (web half): resolved prompt-pack inventory for the panel picker — + // the project > user > shipped > builtin chain, first-wins per name. + // Read-only; every /__bili/ URL already passed the loopback/origin gates. + if (req.method === "GET" && req.url === "/__bili/packs") { + const packs = listResolvedPacks() + .map((p) => ({ name: p.name, version: p.version ?? null, description: p.description ?? null, source: p.source })); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ packs })); + return; + } // #3719: per-release notes for the panel's changelog section — same // read-only admin gating as every other /__bili/ GET above (the gates run // before dispatch). Always answers 200; offline degrades to empty+error. diff --git a/src/web/client.ts b/src/web/client.ts index a3feaca1e2..751433ee5a 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -172,6 +172,9 @@ export const WEB_CLIENT = `(function () { if (m && m[1] !== undefined) API_ROOT = m[1] + "__bili"; } } catch (e) {} + // #5319 (web half): resolved pack names from GET /__bili/packs — filled by + // loadConfig(); empty until then and the picker falls back to default+lean. + let packNames = []; function t(key, vars) { let text = MESSAGES[locale][key]; if (text === undefined) text = MESSAGES["zh-CN"][key]; @@ -3620,6 +3623,11 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + async function loadConfig() { try { const cfg = await json(API_ROOT + "/config"); + // #5319 (web half): refresh the resolved pack inventory so the picker + // shows everything on disk (shipped chat pack, user/project files). + json(API_ROOT + "/packs").then((p) => { + if (p && Array.isArray(p.packs)) packNames = p.packs.map((x) => String(x.name)); + }).catch(() => {}); const cfgPathEl = $("cfg-path"); if (cfgPathEl) cfgPathEl.textContent = cfg.path || t("common.empty"); const fileBtn = $("copy-cfg-file"); @@ -3826,7 +3834,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + abRow.inp.checked = ab ? ab.enabled === true : false; const pv = (cp && typeof cp.promptPack === "string") ? cp.promptPack : "default"; while (packSel.options.length > 0) packSel.removeChild(packSel.lastChild); - ["default", "lean"].forEach((name) => { + (packNames.length ? packNames : ["default", "lean"]).forEach((name) => { const o = document.createElement("option"); o.value = name; o.textContent = name; @@ -3985,6 +3993,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + function updatePackNote() { if (packSel.value === "lean") packNote.textContent = t("cfg.q_pack_lean_desc"); else if (packSel.value === "default") packNote.textContent = t("cfg.q_pack_default_desc"); + else if (packSel.value === "chat") packNote.textContent = t("cfg.q_pack_chat_desc"); else packNote.textContent = t("cfg.q_pack_custom"); } box.appendChild(packNote); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 49c3cc65ac..0bd81ba370 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -583,6 +583,7 @@ export const MESSAGES: Record> = { "cfg.q_parse_err": "高级 JSON 暂时无法解析——快速配置控件已暂停,请先修正下方 JSON", "cfg.q_pack_default_desc": "default(标准):完整提示词面——详细工具描述+全量压缩规则,引导最充分。实测(qwen 分词)每次请求约 2,900+ tokens", "cfg.q_pack_lean_desc": "lean(精简):省 token 面——工具描述压成一行、规则精简,压缩引导更简练。实测(qwen 分词)每次请求约 1,400 tokens,比 default 省约一半", + "cfg.q_pack_chat_desc": "chat(陪伴):为长期陪伴 / 多会话记忆调优的压缩规则——摘要优先保留人、关系、进行中的线与承诺,而不是代码细节;与 default 同机制,仅规则文案不同", "cfg.q_pack_custom": "自定义 pack——从 ~/.config/billion-context/packs/(或项目 .billion-context/packs/)按名读取", "cfg.q_pck": "prompt_cache_key 自注入(promptCache.routing)", "cfg.q_pck_disabled_desc": "关闭(当前默认):bili 不自行写 prompt_cache_key,客户端自带的值仍原样转发。直连官方 API 的用户保持此项即可。", @@ -1251,6 +1252,7 @@ export const MESSAGES: Record> = { "cfg.q_parse_err": "The advanced JSON cannot be parsed right now — quick controls are paused until it is valid again", "cfg.q_pack_default_desc": "default (full surface): detailed tool descriptions plus complete compression rules — richest guidance. Measured ≈2,900+ tokens per request (qwen tokenizer)", "cfg.q_pack_lean_desc": "lean: one-line tool descriptions, condensed rules — terser guidance. Measured ≈1,400 tokens per request (qwen tokenizer), roughly half of default", + "cfg.q_pack_chat_desc": "chat: companion-oriented rules — summaries keep people, relationships, open threads and promises over code detail; same machinery as default, different copy", "cfg.q_pack_custom": "custom pack — loaded by name from ~/.config/billion-context/packs/ (or project .billion-context/packs/)", "cfg.q_pck": "prompt_cache_key self-stamping (promptCache.routing)", "cfg.q_pck_disabled_desc": "Off (current default): bili never writes its own prompt_cache_key; client-supplied values still forward verbatim. Direct official-API users stay here.", diff --git a/tests/web-pack-picker.test.ts b/tests/web-pack-picker.test.ts new file mode 100644 index 0000000000..931ac7a34f --- /dev/null +++ b/tests/web-pack-picker.test.ts @@ -0,0 +1,107 @@ +// #5319 (web half): GET /__bili/packs returns the resolved pack inventory +// (project > user > shipped > builtin, first-wins per name) that the settings +// panel's promptPack picker renders. The loopback + trusted-origin gates are +// covered by the existing admin gate tests; here we assert inventory shape, +// provenance labels, and user-dir discovery over a real server on port 0. +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdirSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.js"; +import type { ProxyOptions } from "../src/config.js"; +import { SessionStore, _setStoreForTest } from "../src/persist.js"; +import { _setForTest as setRegistryForTest } from "../src/registry.js"; +import { rmrf } from "./tmp-rm.js"; + +async function waitReady(port: number): Promise { + for (let i = 0; i < 200; i++) { + try { + if ((await fetch(`http://127.0.0.1:${port}/__bili/health`, { signal: AbortSignal.timeout(500) })).status === 200) return; + } catch {} + await new Promise((r) => setTimeout(r, 50)); + } + throw new Error(`proxy on port ${port} did not become ready`); +} + +function get(port: number, reqPath: string): Promise<{ status: number; body: string }> { + const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); + const req = http.request({ host: "127.0.0.1", port, path: reqPath }, (res) => { + let body = ""; + res.on("data", (c: Buffer) => { body += c.toString("utf8"); }); + res.on("end", () => resolve({ status: res.statusCode ?? 0, body })); + }); + req.once("error", reject); + req.end(); + return promise; +} + +test("#5319 web half: GET /__bili/packs lists resolved packs with provenance", async (t) => { + const root = path.join(tmpdir(), `bili-pack-picker-${process.pid}-${Date.now()}`); + const cfgHome = path.join(root, "xdg-config"); + const packDir = path.join(cfgHome, "billion-context", "packs"); + mkdirSync(packDir, { recursive: true }); + // A user-dir file pack, visible to the resolver via XDG_CONFIG_HOME/billion-context/packs. + writeFileSync(path.join(packDir, "zz-test.json"), JSON.stringify({ promptSections: { philosophy: "zz philosophy" } }), "utf8"); + const biliConfig = path.join(root, "billion-context.json"); + writeFileSync(biliConfig, "{}\n", "utf8"); + const prevFile = process.env.BILI_CONFIG_FILE; + const prevXdg = process.env.XDG_CONFIG_HOME; + process.env.BILI_CONFIG_FILE = biliConfig; + process.env.XDG_CONFIG_HOME = cfgHome; + t.after(() => { + if (prevFile === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevFile; + if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = prevXdg; + rmrf(root); + }); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + try { + await waitReady(port); + const r = await get(port, "/__bili/packs"); + assert.equal(r.status, 200); + const data = JSON.parse(r.body) as { packs?: Array<{ name: string; version: string | null; description: string | null; source: string }> }; + assert.ok(Array.isArray(data.packs) && data.packs.length > 0, "pack list present"); + const byName = new Map(data.packs.map((p) => [p.name, p])); + assert.equal(byName.get("default")?.source, "builtin:default"); + assert.equal(byName.get("lean")?.source, "builtin:lean"); + assert.equal(byName.get("chat")?.source, "shipped:chat", "#5320 shipped builtin listed"); + const zz = byName.get("zz-test"); + assert.ok(zz, "user-dir file pack listed"); + assert.ok(zz.source.startsWith("file:"), `user pack source is a file origin, got ${zz.source}`); + } finally { + proxy.close(); + } +}); From 164b10ebd450aacfe7f908f1400d180a6861bd50 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 31 Oct 2026 16:24:01 +0800 Subject: [PATCH 2/3] feat(web): manage user-dir prompt packs from the panel (#5328) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add New/Edit/Delete for custom compress prompt packs under `~/.config/billion-context/packs/` so users never need a terminal or a file path: - GET/PUT/DELETE /__bili/packs/user/ (api.ts) — same loopback + trusted-origin gates as every other /__bili/ route; names shaped by the route regex AND re-validated with the kernel's isValidPackName; payloads parsed + sanitized exactly like dir-pack loading (bad JSON, non-object top level, or surfaces without recognized fields are refused before touching disk); writes go through the existing atomic temp-file+rename pattern. - GET /__bili/packs gains userDir so the panel knows which listed entries it may edit (server authority on the directory). - Panel picker row: name field + New/Edit/Delete + inline JSON editor; Edit/Delete only act on user-dir packs — builtins/shipped/project packs get override guidance instead; saving auto-selects the pack. - i18n zh-CN/en; CONFIGURATION(.zh-CN).md note under promptPack. - tests/web-user-packs.test.ts: create/read/delete round-trip, unknown keys preserved for host adapters, refused payloads leave no file, dotted/traversal names rejected. Branch note: stacked directly on 171417cd (#5325, web half of #5319); #5325 merges first or together, both orderings stay conflict-free. --- CONFIGURATION.md | 2 +- CONFIGURATION.zh-CN.md | 2 +- src/server/admin.ts | 16 +++- src/web/api.ts | 77 +++++++++++++++++- src/web/client.ts | 153 +++++++++++++++++++++++++++++++++-- src/web/i18n.ts | 30 +++++++ src/web/index.ts | 3 + tests/web-user-packs.test.ts | 143 ++++++++++++++++++++++++++++++++ 8 files changed, 413 insertions(+), 13 deletions(-) create mode 100644 tests/web-user-packs.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 32cdd464f9..19381c7726 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -1492,7 +1492,7 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **Type:** `string` (pack name, e.g. `"lean"`) - **Default:** `default` *(unset is equivalent — identity surface, kernel defaults everywhere)* - **Status:** ACTIVE -- **Description:** Select a named prompt pack — a curated surface preset covering tool descriptions, compress system-prompt sections, and nudge sections — resolved from the host's pack chain: **project** `./.billion-context/packs/.json` → **user** `/packs/.json` → **shipped** (`chat`) → **builtin** (`default`, `lean`). Built-in `lean` swaps the four ACP tool descriptions for one-liners (no snippet/guideline chrome) while keeping the compression rules default. Built-in `chat` (#5319) rewrites only the compress philosophy and how-to-compress prose for long-term companionship / multi-session memory use; every other surface piece stays at the host defaults, and a same-name user or project file overrides it. Unknown names fall back to the identity surface with a one-time warning. Same three-level merge as the other fields; pack-surface sections (tool/section overrides) apply directly, without the `acknowledgePromptsRisk` gate — that gate governs only inline `compress.prompts` rule-text overrides. Note a pack's `prompts` block is ignored by this proxy: rule-text overrides are possible only via inline `compress.prompts`. Requires `acp-kernel` >= 0.0.66. +- **Description:** Select a named prompt pack — a curated surface preset covering tool descriptions, compress system-prompt sections, and nudge sections — resolved from the host's pack chain: **project** `./.billion-context/packs/.json` → **user** `/packs/.json` → **shipped** (`chat`) → **builtin** (`default`, `lean`). Built-in `lean` swaps the four ACP tool descriptions for one-liners (no snippet/guideline chrome) while keeping the compression rules default. Built-in `chat` (#5319) rewrites only the compress philosophy and how-to-compress prose for long-term companionship / multi-session memory use; every other surface piece stays at the host defaults, and a same-name user or project file overrides it. Unknown names fall back to the identity surface with a one-time warning. Same three-level merge as the other fields; pack-surface sections (tool/section overrides) apply directly, without the `acknowledgePromptsRisk` gate — that gate governs only inline `compress.prompts` rule-text overrides. Note a pack's `prompts` block is ignored by this proxy: rule-text overrides are possible only via inline `compress.prompts`. Requires `acp-kernel` >= 0.0.66. The settings panel can manage the user-dir files directly — New/Edit/Delete for packs under `/packs/` (the picker knows each entry's provenance; builtins and shipped packs stay read-only there, override them with a same-named user pack instead). #### `reconcile` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 3dff1ef69d..63bc7ca6b2 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -1483,7 +1483,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 - **类型:** `string`(包名,如 `"lean"`) - **默认值:** `default`(未设置等同——恒等表面,全部使用内核默认值) - **状态:** ACTIVE -- **说明:** 选择一个具名 prompt pack —— 一套策划好的表面预设,覆盖工具描述、压缩系统提示词段落、nudge 段落 —— 从宿主侧的包解析链解析:**项目** `./.billion-context/packs/.json` → **用户** `/packs/.json` → **随包附带**(`chat`)→ **内核内置**(`default`、`lean`)。内置 `lean` 把四个 ACP 工具描述换成单行版(无 snippet/guideline 包装),压缩规则保持默认。内置 `chat`(#5319)只替换压缩哲学段与 how-to-compress 规则段,面向长期陪伴 / 多会话记忆场景;其余表面全部保持宿主默认,同名的用户/项目文件会覆盖它。未知包名回退到恒等表面并记录一次警告。与其他字段一样三级级联合并;包的表面覆盖(工具/段落)直接生效,不经 `acknowledgePromptsRisk` 门控——该门控只管内联 `compress.prompts` 的规则文本覆盖。注意:包文件里的 `prompts` 块会被本代理忽略,规则文本只能经内联 `compress.prompts` 设置。需要 `acp-kernel` >= 0.0.66。 +- **说明:** 选择一个具名 prompt pack —— 一套策划好的表面预设,覆盖工具描述、压缩系统提示词段落、nudge 段落 —— 从宿主侧的包解析链解析:**项目** `./.billion-context/packs/.json` → **用户** `/packs/.json` → **随包附带**(`chat`)→ **内核内置**(`default`、`lean`)。内置 `lean` 把四个 ACP 工具描述换成单行版(无 snippet/guideline 包装),压缩规则保持默认。内置 `chat`(#5319)只替换压缩哲学段与 how-to-compress 规则段,面向长期陪伴 / 多会话记忆场景;其余表面全部保持宿主默认,同名的用户/项目文件会覆盖它。未知包名回退到恒等表面并记录一次警告。与其他字段一样三级级联合并;包的表面覆盖(工具/段落)直接生效,不经 `acknowledgePromptsRisk` 门控——该门控只管内联 `compress.prompts` 的规则文本覆盖。注意:包文件里的 `prompts` 块会被本代理忽略,规则文本只能经内联 `compress.prompts` 设置。需要 `acp-kernel` >= 0.0.66。设置面板可以直接管理用户目录里的包文件——对 `/packs/` 下的包 新建/编辑/删除(选择器知道每个条目的来源;内置与 shipped 包在那里只读,要用同名用户包覆盖)。 #### `reconcile` diff --git a/src/server/admin.ts b/src/server/admin.ts index 93c84e8b7c..840718062a 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -15,7 +15,7 @@ import { log as loggerLog, getLogPath } from "../logger.js"; import { getBlindTunnelStats } from "../mitm.js"; import { handlePluginAttribution, handlePluginCompact, handlePluginFork, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginSessionName, handlePluginSnapshot, handlePluginStatus, handlePluginTool } from "../plugin.js"; import { parseAgentProviderReport, recordAgentProviders, agentProviderRecipes, sanitizeAgentProviderField } from "../agent-providers.js"; -import { defaultLogFile } from "../paths.js"; +import { configDir, defaultLogFile } from "../paths.js"; import { getUpdateVisibility, updateBannerPayload } from "../update-notes.js"; import { installVersionAction, triggerManualCheck, updateActionsAvailable, updateStateSnapshot } from "./update-actions.js"; import { getUnrecognizedPathStats } from "./observability.js"; @@ -32,7 +32,7 @@ import { formatUpstreamError, getUpstreamConnectionStatus, proxyDispatcher, reco import { detectStaleInstall, getLastInstall } from "../update.js"; import { getPlannedUpgrade, getUpdatePolicyState } from "../update-policy.js"; import { PACKAGE_NAME, VERSION, BUILD_COMMIT } from "../version.js"; -import { buildBlockContent, buildMessageContent, buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, buildSessionTree, handleConfigGet, handleConfigPut, handlePricingGet, hiddenEmptyCount, renderUI, type BlockContentView } from "../web/index.js"; +import { buildBlockContent, buildMessageContent, buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, buildSessionTree, handleConfigGet, handleConfigPut, handlePricingGet, handleUserPackDelete, handleUserPackGet, handleUserPackPut, hiddenEmptyCount, renderUI, type BlockContentView } from "../web/index.js"; import { handleChangelogGet } from "../web/changelog.js"; import { kickBlogRefresh } from "../web/blog-refresh.js"; import { handleBlogAssetGet, handleBlogGet } from "../web/blogfeed.js"; @@ -341,10 +341,20 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv if (req.method === "GET" && req.url === "/__bili/packs") { const packs = listResolvedPacks() .map((p) => ({ name: p.name, version: p.version ?? null, description: p.description ?? null, source: p.source })); + // #5328: the panel needs the user-pack dir to decide which listed + // entries it may edit/delete; the server is the authority on it. res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ packs })); + res.end(JSON.stringify({ packs, userDir: path.join(configDir(), "packs") })); return; } + // #5328: user-dir pack management — GET/PUT/DELETE /packs/ + // .json. The route regex shapes the name to the pack-name charset + // ([A-Za-z0-9][A-Za-z0-9._-]*) before dispatch; the handlers re-validate + // with isValidPackName. Everything else under /__bili/ is untouched. + const mUserPack = /^\/__bili\/packs\/user\/([A-Za-z0-9][A-Za-z0-9._-]*)$/.exec(req.url ?? ""); + if (mUserPack !== null && req.method === "GET") return handleUserPackGet(res, mUserPack[1]); + if (mUserPack !== null && req.method === "PUT") return handleUserPackPut(req, res, mUserPack[1]); + if (mUserPack !== null && req.method === "DELETE") return handleUserPackDelete(res, mUserPack[1]); // #3719: per-release notes for the panel's changelog section — same // read-only admin gating as every other /__bili/ GET above (the gates run // before dispatch). Always answers 200; offline degrades to empty+error. diff --git a/src/web/api.ts b/src/web/api.ts index ea8242b1e9..d42ff1a4c4 100644 --- a/src/web/api.ts +++ b/src/web/api.ts @@ -1,8 +1,9 @@ import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs"; -import { dirname } from "node:path"; +import { dirname, join } from "node:path"; import { randomUUID } from "node:crypto"; import type { IncomingMessage, ServerResponse } from "node:http"; -import { configFile } from "../paths.js"; +import { configDir, configFile } from "../paths.js"; +import { isValidPackName, sanitizePackSurface, type PromptPackFile } from "acp-kernel"; import { collectNamedProviders, loadNamedProviders, @@ -99,6 +100,78 @@ function atomicWriteConfig(config: ConfigShape): void { } } +// --------------------------------------------------------------------------- +// #5328: user-dir prompt-pack management. These are the ONLY routes that +// write under /packs/ — the web editor's 新建/编辑/删除 buttons hit +// them so users never have to touch a terminal or file path. Builtin and +// shipped packs are read-only by construction (they live in code, not files); +// the client shows "override with a same-named file" guidance for them. +// Names arrive pre-shaped by the /__bili/ route regex; re-validate here so +// the handlers stay safe for any future caller. + +function userPackFile(name: string): string { + return join(configDir(), "packs", `${name}.json`); +} + +function packSurfaceHasContent(surface: ReturnType): boolean { + return Object.entries(surface).some(([, value]) => + value !== undefined && + (typeof value !== "object" || value === null ? true : Object.keys(value).length > 0)); +} + +export async function handleUserPackGet(res: ServerResponse, name: string): Promise { + if (!isValidPackName(name)) return sendError(res, 400, `invalid pack name: ${name}`); + const file = userPackFile(name); + if (!existsSync(file)) return sendError(res, 404, `no user pack named "${name}"`); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ name, path: file, json: readFileSync(file, "utf8") })); +} + +export async function handleUserPackPut(req: IncomingMessage, res: ServerResponse, name: string): Promise { + if (!isValidPackName(name)) return sendError(res, 400, `invalid pack name: ${name}`); + const raw = await readJsonBody(req); + if (!raw || typeof raw !== "object" || Array.isArray(raw) || typeof (raw as Record).json !== "string") + return sendError(res, 400, 'expected { "json": "" }'); + let parsed: unknown; + try { parsed = JSON.parse((raw as Record).json as string); } + catch (error) { return sendError(res, 400, `pack json is not valid JSON: ${String(error)}`); } + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) + return sendError(res, 400, "pack top level must be a JSON object"); + // Sanitize exactly like dir-pack loading (createDirPackSource): nonsense + // values are dropped at save time instead of biting at resolution time, + // and a pack that resolves to an empty surface is refused rather than + // silently written. + if (!packSurfaceHasContent(sanitizePackSurface(parsed as PromptPackFile))) + return sendError(res, 400, "no recognized pack fields (promptSections / prompts / nudgeSections / toolPrompts / adapters)"); + const file = userPackFile(name); + mkdirSync(dirname(file), { recursive: true }); + const tempPath = `${file}.${process.pid}.${randomUUID()}.tmp`; + let descriptor: number | undefined; + try { + descriptor = openSync(tempPath, "wx", 0o644); + writeFileSync(descriptor, JSON.stringify(parsed, null, 2) + "\n", "utf8"); + fsyncSync(descriptor); + closeSync(descriptor); + descriptor = undefined; + renameSync(tempPath, file); + } catch (error) { + if (descriptor !== undefined) closeSync(descriptor); + try { unlinkSync(tempPath); } catch { } + return sendError(res, 500, `failed to write user pack: ${String(error)}`); + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, path: file })); +} + +export async function handleUserPackDelete(res: ServerResponse, name: string): Promise { + if (!isValidPackName(name)) return sendError(res, 400, `invalid pack name: ${name}`); + const file = userPackFile(name); + if (!existsSync(file)) return sendError(res, 404, `no user pack named "${name}"`); + unlinkSync(file); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true })); +} + export async function handleConfigGet(res: ServerResponse): Promise { const upstream = readUpstreamSettings(); const config = readConfig(); diff --git a/src/web/client.ts b/src/web/client.ts index 751433ee5a..3f5ec421ae 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -174,7 +174,31 @@ export const WEB_CLIENT = `(function () { } catch (e) {} // #5319 (web half): resolved pack names from GET /__bili/packs — filled by // loadConfig(); empty until then and the picker falls back to default+lean. + // #5328: per-name provenance + the user-pack dir (server authority) so the + // panel knows which entries 新建/编辑/删除 may touch. let packNames = []; + let packSources = {}; + let packUserDir = ""; + // #5328: indirections so the inventory refresher lives at panel scope + // (loadConfig can call it before/after the quick-config block is built) + // while DOM and selection handling stay where the elements exist. + let packSelEl = null; + let rerenderQuick = null; + async function refreshPackInventory() { + try { + const p = await json(API_ROOT + "/packs"); + if (!p || !Array.isArray(p.packs)) return; + packNames = p.packs.map((x) => String(x.name)); + packSources = {}; + for (const x of p.packs) packSources[String(x.name)] = String(x.source || ""); + if (p.userDir) packUserDir = String(p.userDir) + "/"; + } catch (e) { } + const prev = packSelEl ? packSelEl.value : ""; + // Option reconstruction never fires the change listener — restore a + // still-present custom selection manually. + if (packSelEl && prev && packNames.includes(prev)) packSelEl.value = prev; + if (rerenderQuick) rerenderQuick(); + } function t(key, vars) { let text = MESSAGES[locale][key]; if (text === undefined) text = MESSAGES["zh-CN"][key]; @@ -3622,12 +3646,11 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + async function loadConfig() { try { - const cfg = await json(API_ROOT + "/config"); - // #5319 (web half): refresh the resolved pack inventory so the picker - // shows everything on disk (shipped chat pack, user/project files). - json(API_ROOT + "/packs").then((p) => { - if (p && Array.isArray(p.packs)) packNames = p.packs.map((x) => String(x.name)); - }).catch(() => {}); + const cfg = await json(API_ROOT + "/config"); + // #5319/#5328: refresh the resolved pack inventory (+ per-name + // source + user-pack dir) so the picker and the editor stay in + // sync with what is actually on disk. + refreshPackInventory(); const cfgPathEl = $("cfg-path"); if (cfgPathEl) cfgPathEl.textContent = cfg.path || t("common.empty"); const fileBtn = $("copy-cfg-file"); @@ -3987,6 +4010,8 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + plab.textContent = t("cfg.q_pack"); pw.appendChild(plab); pw.appendChild(packSel); + packSelEl = packSel; + rerenderQuick = () => syncAll(); box.appendChild(pw); const packNote = document.createElement("div"); packNote.style.cssText = "margin:-6px 0 4px;font-size:12px;color:#57606a"; @@ -3997,6 +4022,122 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + else packNote.textContent = t("cfg.q_pack_custom"); } box.appendChild(packNote); + // #5328: user-dir pack management — 新建/编辑/删除 under the dir the + // server reported via GET /__bili/packs (userDir). Builtin/shipped + // packs are read-only here; guidance says "override with a same-named + // file" instead of pretending they can be edited in place. + const pmRow = document.createElement("div"); + pmRow.style.cssText = "margin:4px 0 2px;display:flex;gap:8px;align-items:center;flex-wrap:wrap"; + const pmName = document.createElement("input"); + pmName.type = "text"; + pmName.className = "field-input mono"; + pmName.placeholder = t("cfg.q_pack_name_ph"); + pmName.style.cssText = "width:200px"; + const pmNew = document.createElement("button"); + pmNew.className = "btn sm"; + pmNew.textContent = t("cfg.q_pack_new"); + const pmEdit = document.createElement("button"); + pmEdit.className = "btn sm"; + pmEdit.textContent = t("cfg.q_pack_edit"); + const pmDel = document.createElement("button"); + pmDel.className = "btn sm"; + pmDel.textContent = t("cfg.q_pack_del"); + const pmStatus = document.createElement("span"); + pmStatus.style.cssText = "flex-basis:100%;font-size:12px;color:#57606a"; + pmRow.append(pmName, pmNew, pmEdit, pmDel, pmStatus); + const peBox = document.createElement("div"); + peBox.hidden = true; + peBox.style.cssText = "margin:4px 0"; + const peTa = document.createElement("textarea"); + peTa.className = "editor mono codebox"; + peTa.spellcheck = false; + peTa.rows = 10; + peTa.placeholder = '{\\n "promptSections": {\\n "philosophy": "…"\\n }\\n}'; + const peBtns = document.createElement("div"); + peBtns.style.cssText = "margin-top:6px;display:flex;gap:8px"; + const peSave = document.createElement("button"); + peSave.className = "btn sm"; + peSave.textContent = t("cfg.q_pack_save"); + const peCancel = document.createElement("button"); + peCancel.className = "btn sm"; + peCancel.textContent = t("cfg.q_pack_cancel"); + peBtns.append(peSave, peCancel); + peBox.append(peTa, peBtns); + box.append(pmRow, peBox); + let editingPack = ""; // "" = closed; otherwise the name being edited + let editingNew = false; + function pmSet(text, isError) { + pmStatus.textContent = text || ""; + pmStatus.style.color = isError ? "#cf222e" : "#57606a"; + } + function selectedIsUserPack(name) { + return packUserDir !== "" && String(packSources[name] || "").startsWith(packUserDir); + } + pmNew.addEventListener("click", () => { + editingPack = ""; + editingNew = true; + pmName.value = ""; + peTa.value = '{\\n "promptSections": {\\n "philosophy": ""\\n }\\n}\\n'; + peBox.hidden = false; + pmSet(""); + }); + pmEdit.addEventListener("click", async () => { + const name = packSel.value; + if (!name || name === "default") { pmSet(t("cfg.q_pack_ro_default"), true); return; } + if (!selectedIsUserPack(name)) { + const src = String(packSources[name] || ""); + pmSet(src.startsWith("file:") ? t("cfg.q_pack_ro_project") : t("cfg.q_pack_ro_builtin"), true); + return; + } + try { + const r = await json(API_ROOT + "/packs/user/" + encodeURIComponent(name)); + peTa.value = r && typeof r.json === "string" && r.json ? r.json : "{}\\n"; + editingPack = name; + editingNew = false; + pmName.value = name; + peBox.hidden = false; + pmSet(""); + } catch (e) { pmSet(String(e), true); } + }); + pmDel.addEventListener("click", async () => { + const name = packSel.value; + if (!selectedIsUserPack(name)) { pmSet(t("cfg.q_pack_ro_del"), true); return; } + if (typeof confirm === "function" && !confirm(t("cfg.q_pack_confirm_del", { name }))) return; + try { + await json(API_ROOT + "/packs/user/" + encodeURIComponent(name), { method: "DELETE" }); + pmSet(t("cfg.q_pack_deleted")); + refreshPackInventory(); + } catch (e) { pmSet(String(e), true); } + }); + peCancel.addEventListener("click", () => { + peBox.hidden = true; + editingPack = ""; + editingNew = false; + pmSet(""); + }); + peSave.addEventListener("click", async () => { + // No regex literals with backslashes here (WEB_CLIENT template): + // strip one leading path separator via charCode instead. + const rawName = (editingNew ? pmName.value.trim() : editingPack); + const first = rawName.charCodeAt(0); + const name = (first === 47 || first === 92 ? rawName.slice(1) : rawName).trim(); + if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name) || name.includes("..")) { pmSet(t("cfg.q_pack_bad_name"), true); return; } + let parsed; + try { parsed = JSON.parse(peTa.value); } catch (e) { pmSet(t("cfg.q_pack_bad_json", { err: String(e) }), true); return; } + try { + await json(API_ROOT + "/packs/user/" + encodeURIComponent(name), { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ json: JSON.stringify(parsed, null, 2) }), + }); + pmSet(t("cfg.q_pack_saved", { name })); + peBox.hidden = true; + editingPack = ""; + editingNew = false; + refreshPackInventory(); + commit((d) => { if (!compressOf(d)) d.compress = {}; d.compress.promptPack = name; }); + } catch (e) { pmSet(String(e), true); } + }); packSel.addEventListener("change", () => commit((d) => { if (!compressOf(d)) d.compress = {}; if (packSel.value === "default") delete d.compress.promptPack; else d.compress.promptPack = packSel.value; diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 0bd81ba370..264014a285 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -585,6 +585,21 @@ export const MESSAGES: Record> = { "cfg.q_pack_lean_desc": "lean(精简):省 token 面——工具描述压成一行、规则精简,压缩引导更简练。实测(qwen 分词)每次请求约 1,400 tokens,比 default 省约一半", "cfg.q_pack_chat_desc": "chat(陪伴):为长期陪伴 / 多会话记忆调优的压缩规则——摘要优先保留人、关系、进行中的线与承诺,而不是代码细节;与 default 同机制,仅规则文案不同", "cfg.q_pack_custom": "自定义 pack——从 ~/.config/billion-context/packs/(或项目 .billion-context/packs/)按名读取", + "cfg.q_pack_name_ph": "包名(新建时填写)", + "cfg.q_pack_new": "新建", + "cfg.q_pack_edit": "编辑当前所选", + "cfg.q_pack_del": "删除当前所选", + "cfg.q_pack_save": "保存", + "cfg.q_pack_cancel": "取消", + "cfg.q_pack_saved": "已保存 {name}.json 到用户目录,并已切换为它", + "cfg.q_pack_deleted": "已删除(若列表里还有同名条目,说明有更高优先级目录的同名文件在起作用)", + "cfg.q_pack_bad_name": "包名只能含字母/数字/._- 且不能以符号开头", + "cfg.q_pack_bad_json": "JSON 不合法:{err}", + "cfg.q_pack_ro_default": "default 是内置行为,没有文件可编辑", + "cfg.q_pack_ro_project": "项目目录的 pack 请手动编辑 .billion-context/packs/ 下的文件", + "cfg.q_pack_ro_builtin": "内置/随包发布(shipped)的包不能在此编辑——在 ~/.config/billion-context/packs/ 新建一个同名包即可整体覆盖它", + "cfg.q_pack_ro_del": "只有用户目录(~/.config/billion-context/packs/)里的包可以删除;内置/shipped/项目包请用同名用户包覆盖或直接删文件", + "cfg.q_pack_confirm_del": "删除用户目录里的 pack:{name}?(配置文件本身不受影响)", "cfg.q_pck": "prompt_cache_key 自注入(promptCache.routing)", "cfg.q_pck_disabled_desc": "关闭(当前默认):bili 不自行写 prompt_cache_key,客户端自带的值仍原样转发。直连官方 API 的用户保持此项即可。", "cfg.q_pck_auto_desc": "旧版默认行为:插件车道请求一律加盖会话 id,Responses 线仅对 api.openai.com 注入;上游拒绝时自动 A/B 探测并记忆停用。中转站/号池场景推荐此项。", @@ -1254,6 +1269,21 @@ export const MESSAGES: Record> = { "cfg.q_pack_lean_desc": "lean: one-line tool descriptions, condensed rules — terser guidance. Measured ≈1,400 tokens per request (qwen tokenizer), roughly half of default", "cfg.q_pack_chat_desc": "chat: companion-oriented rules — summaries keep people, relationships, open threads and promises over code detail; same machinery as default, different copy", "cfg.q_pack_custom": "custom pack — loaded by name from ~/.config/billion-context/packs/ (or project .billion-context/packs/)", + "cfg.q_pack_name_ph": "pack name (for new packs)", + "cfg.q_pack_new": "New", + "cfg.q_pack_edit": "Edit selected", + "cfg.q_pack_del": "Delete selected", + "cfg.q_pack_save": "Save", + "cfg.q_pack_cancel": "Cancel", + "cfg.q_pack_saved": "saved {name}.json to your user dir and switched to it", + "cfg.q_pack_deleted": "deleted (if a same-named entry remains, a higher-precedence dir's file still wins)", + "cfg.q_pack_bad_name": "pack name may only contain letters/digits/._- and must not start with one", + "cfg.q_pack_bad_json": "invalid JSON: {err}", + "cfg.q_pack_ro_default": "default is built-in behaviour — there is no file to edit", + "cfg.q_pack_ro_project": "project-dir packs are edited by hand under .billion-context/packs/", + "cfg.q_pack_ro_builtin": "built-in / shipped packs can't be edited here — create a same-named pack in ~/.config/billion-context/packs/ to override it", + "cfg.q_pack_ro_del": "only user-dir packs (~/.config/billion-context/packs/) can be deleted; override builtins with a same-named user pack instead", + "cfg.q_pack_confirm_del": "delete the user-dir pack: {name}? (your config file is untouched)", "cfg.q_pck": "prompt_cache_key self-stamping (promptCache.routing)", "cfg.q_pck_disabled_desc": "Off (current default): bili never writes its own prompt_cache_key; client-supplied values still forward verbatim. Direct official-API users stay here.", "cfg.q_pck_auto_desc": "Pre-#5079 behavior: plugin-lane requests are stamped with the session id, the Responses wire injects only for api.openai.com; rejecting upstreams are auto-detected via A/B probe and remembered. Recommended for relay/account-pool setups.", diff --git a/src/web/index.ts b/src/web/index.ts index 845ca89a7e..c1890b3bf7 100644 --- a/src/web/index.ts +++ b/src/web/index.ts @@ -6,6 +6,9 @@ export { handleConfigPut, handlePricingGet, handleSummaryCredentialPut, + handleUserPackDelete, + handleUserPackGet, + handleUserPackPut, readProviders, readUpstreamSettings, } from "./api.js"; diff --git a/tests/web-user-packs.test.ts b/tests/web-user-packs.test.ts new file mode 100644 index 0000000000..8569fb5b13 --- /dev/null +++ b/tests/web-user-packs.test.ts @@ -0,0 +1,143 @@ +// #5328: user-dir prompt-pack management over the web API — +// GET/PUT/DELETE /__bili/packs/user/ maps 1:1 onto files under +// /billion-context/packs/. Covers create/read/delete round- +// trips, rejection of invalid names and empty or non-pack payloads, and that +// /__bili/packs reports the user dir so the panel knows what it may edit. +// Loopback/origin gating is covered by the existing admin gate tests. +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { existsSync, mkdirSync, readFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.js"; +import type { ProxyOptions } from "../src/config.js"; +import { SessionStore, _setStoreForTest } from "../src/persist.js"; +import { _setForTest as setRegistryForTest } from "../src/registry.js"; +import { rmrf } from "./tmp-rm.js"; + +async function waitReady(port: number): Promise { + for (let i = 0; i < 200; i++) { + try { + if ((await fetch(`http://127.0.0.1:${port}/__bili/health`, { signal: AbortSignal.timeout(500) })).status === 200) return; + } catch {} + await new Promise((r) => setTimeout(r, 50)); + } + throw new Error(`proxy on port ${port} did not become ready`); +} + +function request(method: string, port: number, reqPath: string, body?: string): Promise<{ status: number; body: string }> { + const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); + const req = http.request({ host: "127.0.0.1", port, path: reqPath, method }, (res) => { + let data = ""; + res.on("data", (c: Buffer) => { data += c.toString("utf8"); }); + res.on("end", () => resolve({ status: res.statusCode ?? 0, body: data })); + }); + req.once("error", reject); + if (body !== undefined) req.write(body); + req.end(); + return promise; +} + +const packJson = (obj: unknown): string => JSON.stringify({ json: JSON.stringify(obj, null, 2) }); + +test("#5328: user-dir pack management endpoints", async (t) => { + const root = path.join(tmpdir(), `bili-user-packs-${process.pid}-${Date.now()}`); + const cfgHome = path.join(root, "xdg-config"); + const packDir = path.join(cfgHome, "billion-context", "packs"); + mkdirSync(packDir, { recursive: true }); + const biliConfig = path.join(root, "billion-context.json"); + await import("node:fs").then((fs) => fs.writeFileSync(biliConfig, "{}\n", "utf8")); + const prevFile = process.env.BILI_CONFIG_FILE; + const prevXdg = process.env.XDG_CONFIG_HOME; + process.env.BILI_CONFIG_FILE = biliConfig; + process.env.XDG_CONFIG_HOME = cfgHome; + t.after(() => { + if (prevFile === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevFile; + if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = prevXdg; + rmrf(root); + }); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + try { + await waitReady(port); + // /__bili/packs reports the user dir the panel may manage. + const inv = JSON.parse((await request("GET", port, "/__bili/packs")).body) as { userDir?: string }; + assert.equal(inv.userDir, packDir, "userDir matches XDG config packs dir"); + + // Create. + const payload = { promptSections: { philosophy: "user philosophy" }, customExtra: "kept" }; + const put = await request("PUT", port, "/__bili/packs/user/my-pack", packJson(payload)); + assert.equal(put.status, 200, `create ok (${put.body})`); + const file = path.join(packDir, "my-pack.json"); + assert.ok(existsSync(file), "file written to user packs dir"); + const onDisk = JSON.parse(readFileSync(file, "utf8")) as Record; + assert.equal(onDisk.customExtra, "kept", "unknown top-level keys are preserved for host adapters"); + const inv2 = JSON.parse((await request("GET", port, "/__bili/packs")).body) as { packs: Array<{ name: string; source: string }> }; + const listed = inv2.packs.find((p) => p.name === "my-pack"); + assert.ok(listed && listed.source.startsWith("file:"), "created pack appears in inventory with file: provenance"); + + // Read back the raw text. + const got = JSON.parse((await request("GET", port, "/__bili/packs/user/my-pack")).body) as { name: string; json: string }; + assert.equal(got.name, "my-pack"); + assert.equal(JSON.parse(got.json).promptSections.philosophy, "user philosophy"); + + // Invalid payloads are refused before touching disk. + const badJson = await request("PUT", port, "/__bili/packs/user/bad-json", JSON.stringify({ json: "{nope" })); + assert.equal(badJson.status, 400, `invalid JSON refused (${badJson.body})`); + const emptyPack = await request("PUT", port, "/__bili/packs/user/empty-pack", packJson({})); + assert.equal(emptyPack.status, 400, `pack without recognized fields refused (${emptyPack.body})`); + assert.ok(!existsSync(path.join(packDir, "empty-pack.json")), "refused pack leaves no file"); + const foreignKeys = await request("PUT", port, "/__bili/packs/user/foreign", packJson({ foo: 1 })); + assert.equal(foreignKeys.status, 400, `unrecognized-only surface refused (${foreignKeys.body})`); + + // Names: dotted interior rejected by the pack-name convention even + // though the route charset would allow it; traversal never 2xx. + const dotted = await request("PUT", port, "/__bili/packs/user/a..b", packJson(payload)); + assert.equal(dotted.status, 400, `name containing .. refused (${dotted.body})`); + assert.ok(!existsSync(path.join(packDir, "a..b.json"))); + const traversal = await request("PUT", port, "/__bili/packs/user/" + encodeURIComponent("../evil"), packJson(payload)); + assert.notEqual(traversal.status, 200, "traversal-shaped name must not succeed"); + assert.ok(!existsSync(path.join(cfgHome, "evil.json")), "nothing written outside packs dir"); + + // Delete. + const del = await request("DELETE", port, "/__bili/packs/user/my-pack"); + assert.equal(del.status, 200, `delete ok (${del.body})`); + assert.ok(!existsSync(file), "file removed"); + assert.equal((await request("DELETE", port, "/__bili/packs/user/my-pack")).status, 404, "second delete is 404"); + assert.equal((await request("GET", port, "/__bili/packs/user/my-pack")).status, 404, "get of deleted is 404"); + } finally { + proxy.close(); + } +}); From b9218e857f41116fbb2e85c2bac344231098b2ee Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 31 Oct 2026 17:30:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat(cli):=20bili=20pack=20install|remove|l?= =?UTF-8?q?ist=20=E2=80=94=20npm-distributable=20prompt=20packs=20(#5340)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Final piece of the pack workstream (#5319 item ④): a documented standard pack format plus a first-class way to install other people's packs. Convention: an npm package is installable as a prompt pack when its root contains bili-pack.json (the same object shape as any `~/.config/billion-context/packs/.json`). `bili pack install ` delegates to the local `npm` in a throwaway sandbox and reads that one file out — registry auth, version ranges, scoped names, git URLs and .tgz paths all behave like a normal npm install; no tar parsing, no new dependency. - src/pack-install.ts: installPackFromNpm/removePackFile/packDirFor. Kernel-shaped validation end to end (isValidPackName, sanitizePackSurface + non-empty surface check shared with the web handler via the now-exported packSurfaceHasContent), atomic temp-file+rename writes, default destination /packs, --project targets ./.billion-context/packs/, existing files are never overwritten without --force. - cli.ts: `bili pack install [--project] [--force]` / `remove ` / `list` (also accepts plural `packs`); list prints every resolvable pack with version and provenance. - docs: CONFIGURATION.md + zh-CN gain the full pack file format section (annotated example, tri-state null semantics, sanitize behavior) and publish/install instructions. - tests/pack-install.test.ts: local-directory npm specs keep the suite offline — install→resolvable round-trip, force semantics, missing entry file, invalid names, empty surfaces, remove errors, dir targets. Verified live against dist: list shows builtin/shipped packs, install from a real package dir lands in the XDG packs dir, reinstall refuses without --force, remove cleans up. Branch note: stacked on #5331 (web half) and its base #5325 — the code reuses their exported helpers; merge bottom-up. --- CONFIGURATION.md | 18 +++++ CONFIGURATION.zh-CN.md | 18 +++++ src/cli.ts | 64 ++++++++++++++- src/pack-install.ts | 161 +++++++++++++++++++++++++++++++++++++ src/web/api.ts | 2 +- tests/pack-install.test.ts | 74 +++++++++++++++++ 6 files changed, 333 insertions(+), 4 deletions(-) create mode 100644 src/pack-install.ts create mode 100644 tests/pack-install.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 19381c7726..1d7d021264 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -1494,6 +1494,24 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **Status:** ACTIVE - **Description:** Select a named prompt pack — a curated surface preset covering tool descriptions, compress system-prompt sections, and nudge sections — resolved from the host's pack chain: **project** `./.billion-context/packs/.json` → **user** `/packs/.json` → **shipped** (`chat`) → **builtin** (`default`, `lean`). Built-in `lean` swaps the four ACP tool descriptions for one-liners (no snippet/guideline chrome) while keeping the compression rules default. Built-in `chat` (#5319) rewrites only the compress philosophy and how-to-compress prose for long-term companionship / multi-session memory use; every other surface piece stays at the host defaults, and a same-name user or project file overrides it. Unknown names fall back to the identity surface with a one-time warning. Same three-level merge as the other fields; pack-surface sections (tool/section overrides) apply directly, without the `acknowledgePromptsRisk` gate — that gate governs only inline `compress.prompts` rule-text overrides. Note a pack's `prompts` block is ignored by this proxy: rule-text overrides are possible only via inline `compress.prompts`. Requires `acp-kernel` >= 0.0.66. The settings panel can manage the user-dir files directly — New/Edit/Delete for packs under `/packs/` (the picker knows each entry's provenance; builtins and shipped packs stay read-only there, override them with a same-named user pack instead). +**Pack file format.** Each `/.json` holds one pack object. Unknown fields are dropped by the same sanitizer used on every load path (`sanitizePackSurface`), so extra metadata is harmless: + +```json +{ + "name": "my-pack", // optional — defaults to the file name without .json + "version": "1.0.0", // optional — shown by `bili pack list` and the panel + "description": "…", // optional + "promptSections": { + "philosophy": "…", // replace a whole system-prompt section; JSON null keeps the default + "howToCompress": "…" // start from the tuned texts in src/chat-pack.ts + } +} +``` + +Optional surface blocks: `prompts` (inline rule-text overrides), `nudgeSections`, `toolPrompts`, and `adapters` (host-specific extras, e.g. `"pi": { "promptSections": { … } }`). At least one recognized field must survive sanitizing or the file is refused when written. + +**Installing other people's packs.** Any npm package whose root contains a `bili-pack.json` (the pack object above) is installable: `bili pack install ` — also accepts `name@version`, scoped names, git URLs and `.tgz` paths (it delegates to your local `npm`, so registry auth behaves normally). Files land in `/packs/` by default (`--project` targets `./.billion-context/packs/`; `--force` replaces an existing pack instead of refusing). Afterwards manage them from the settings panel, or via `bili pack remove ` / `bili pack list`. + #### `reconcile` - **Type:** `string` — `"off"` | `"warn"` | `"repair"` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 63bc7ca6b2..50a1435f14 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -1485,6 +1485,24 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 - **状态:** ACTIVE - **说明:** 选择一个具名 prompt pack —— 一套策划好的表面预设,覆盖工具描述、压缩系统提示词段落、nudge 段落 —— 从宿主侧的包解析链解析:**项目** `./.billion-context/packs/.json` → **用户** `/packs/.json` → **随包附带**(`chat`)→ **内核内置**(`default`、`lean`)。内置 `lean` 把四个 ACP 工具描述换成单行版(无 snippet/guideline 包装),压缩规则保持默认。内置 `chat`(#5319)只替换压缩哲学段与 how-to-compress 规则段,面向长期陪伴 / 多会话记忆场景;其余表面全部保持宿主默认,同名的用户/项目文件会覆盖它。未知包名回退到恒等表面并记录一次警告。与其他字段一样三级级联合并;包的表面覆盖(工具/段落)直接生效,不经 `acknowledgePromptsRisk` 门控——该门控只管内联 `compress.prompts` 的规则文本覆盖。注意:包文件里的 `prompts` 块会被本代理忽略,规则文本只能经内联 `compress.prompts` 设置。需要 `acp-kernel` >= 0.0.66。设置面板可以直接管理用户目录里的包文件——对 `/packs/` 下的包 新建/编辑/删除(选择器知道每个条目的来源;内置与 shipped 包在那里只读,要用同名用户包覆盖)。 +**包文件格式。** 每个 `/.json` 是一个 pack 对象。未识别的字段会被与所有加载路径相同的 sanitize 器丢弃(`sanitizePackSurface`),所以多写元数据无害: + +```json +{ + "name": "my-pack", // 可选 —— 缺省用文件名(去 .json) + "version": "1.0.0", // 可选 —— `bili pack list` 与面板会显示 + "description": "……", // 可选 + "promptSections": { + "philosophy": "……", // 替换整段系统提示词;JSON null = 保留默认文本 + "howToCompress": "……" // 可直接从 src/chat-pack.ts 里调好的全文起步 + } +} +``` + +可选的表面块:`prompts`(内联规则文本覆盖)、`nudgeSections`、`toolPrompts`、`adapters`(宿主专属附加,如 `"pi": { "promptSections": { … } }`)。至少一个字段在 sanitize 后存活,否则写入时会被拒绝。 + +**安装别人的包。** 任何根目录带 `bili-pack.json`(即上面的 pack 对象)的 npm 包都可安装:`bili pack install `——也接受 `name@version`、带 scope 名、git URL、`.tgz` 路径(底层委托本机 `npm`,所以 registry 鉴权行为与普通 npm 一致)。默认落到 `/packs/`(`--project` 指向 `./.billion-context/packs/`;已存在同名包时默认拒绝,`--force` 才替换)。之后可用设置面板管理,或 `bili pack remove ` / `bili pack list`。 + #### `reconcile` - **类型:** `string` — `"off"` | `"warn"` | `"repair"` diff --git a/src/cli.ts b/src/cli.ts index 9b76bbfb1d..8387e476e6 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -16,6 +16,7 @@ * bili acp-cache diff attribute cache breaks from ACP_DUMP_BODY dumps * bili sessions ls|rm|archive|restore [id] manage persisted sessions * bili storage gc [--days N] run the storage sweep now (--dry-run plans only) + * bili pack install install a custom compress prompt pack from npm (#5319) * bili test pi non-polluting pi smoke test * bili --version * bili --help @@ -42,6 +43,8 @@ import { PLUGIN_AGENTS, isPluginAgent, pluginInstall, pluginRemove, pluginStatus import { runLaunch, runTestPi, isLaunchClient, type ClientName } from "./launcher.js"; import { exportSession } from "./export.js"; import { runSessionsCommand, runStorageGcCommand } from "./storage-maintenance.js"; +import { installPackFromNpm, removePackFile } from "./pack-install.js"; +import { listResolvedPacks } from "./compress-settings.js"; import { renderJson, renderText, runDiff } from "./acp-cache-diff.js"; import { fixDshLedgers, renderDoctorReport, renderLedgerFixReport, runDoctor } from "./doctor.js"; import { VERSION, PACKAGE_NAME, BUILD_COMMIT } from "./version.js"; @@ -94,6 +97,10 @@ Usage: release unreferenced content, shrink index.db (--days N overrides the age; --vacuum full|incremental; prints the sweep plan first, --dry-run stops there) + bili pack install npm-install a custom compress prompt pack + bili pack list / remove list resolvable packs / delete a local pack file + (--project targets ./.billion-context/packs/ instead; + install also accepts --force to replace an existing pack) bili acp-cache diff offline prefix-diff attribution over ACP_DUMP_BODY dumps: pairs adjacent requests per session and classifies each (pure-append / mid-stream-rewrite / prefix-stable-miss); @@ -183,7 +190,7 @@ Docs: https://github.com/ranxianglei/billion-context `; type Parsed = { - command: "start" | "update" | "doctor" | "help" | "version" | "launch" | "test" | "export" | "plugin-register" | "mcp" | "plugin" | "acp-cache" | "sessions" | "storage"; + command: "start" | "update" | "doctor" | "help" | "version" | "launch" | "test" | "export" | "plugin-register" | "mcp" | "plugin" | "acp-cache" | "sessions" | "storage" | "pack"; client?: ClientName; clientArgs: string[]; mitmDomains: string[]; @@ -196,6 +203,10 @@ type Parsed = { pluginAction?: "install" | "remove" | "update" | "list"; pluginAgent?: PluginAgent; pluginWithMcp?: boolean; + packAction?: "install" | "remove" | "list"; + packSpec?: string; + packProject?: boolean; + packForce?: boolean; acpCacheDir?: string; acpCacheLog?: string; acpCacheNoLog?: boolean; @@ -226,6 +237,10 @@ export function parseArgs(argv: string[]): Parsed { let pluginAction: Parsed["pluginAction"]; let pluginAgent: Parsed["pluginAgent"]; let pluginWithMcp = false; + let packAction: Parsed["packAction"]; + let packSpec: string | undefined; + let packProject = false; + let packForce = false; let acpCacheDir: string | undefined; let acpCacheLog: string | undefined; let acpCacheNoLog = false; @@ -302,6 +317,12 @@ export function parseArgs(argv: string[]): Parsed { case "--with-mcp": pluginWithMcp = true; break; + case "--project": + packProject = true; + break; + case "--force": + packForce = true; + break; case "--json": jsonOutput = true; doctorJson = true; @@ -476,6 +497,20 @@ export function parseArgs(argv: string[]): Parsed { console.error(`bili sessions: unknown action "${sub ?? ""}" (try "bili sessions ls|rm|archive|restore")`); process.exit(2); } + } else if (cmd === "pack" || cmd === "packs") { + command = "pack"; + const action = positional[1]; + if (action !== "install" && action !== "remove" && action !== "list") { + console.error(`bili pack: unknown action "${action ?? ""}" (try "bili pack install | remove | list")`); + process.exit(2); + } + packAction = action; + const arg = positional[2]; + if (packAction !== "list" && arg === undefined) { + console.error(`bili pack ${packAction}: <${packAction === "install" ? "npm-spec" : "name"}> is required`); + process.exit(2); + } + packSpec = arg; } else if (cmd === "storage") { command = "storage"; if (positional[1] !== "gc") { @@ -488,11 +523,11 @@ export function parseArgs(argv: string[]): Parsed { } } - return { command, client, clientArgs, mitmDomains, overrides, exportSelector, exportOutput, exportFull, exportJsonl, registerConversationId, pluginAction, pluginAgent, pluginWithMcp, acpCacheDir, acpCacheLog, acpCacheNoLog, acpCacheSession, sessionsSub, sessionsTarget, sessionsNoVacuum, storageDays, storageVacuum, storageDryRun, jsonOutput, doctorJson, doctorFix }; + return { command, client, clientArgs, mitmDomains, overrides, exportSelector, exportOutput, exportFull, exportJsonl, registerConversationId, pluginAction, pluginAgent, pluginWithMcp, acpCacheDir, acpCacheLog, acpCacheNoLog, acpCacheSession, sessionsSub, sessionsTarget, sessionsNoVacuum, storageDays, storageVacuum, storageDryRun, jsonOutput, doctorJson, doctorFix, packAction, packSpec, packProject, packForce }; } export async function main(): Promise { - const { command, client, clientArgs, mitmDomains, overrides, exportSelector, exportOutput, exportFull, exportJsonl, registerConversationId, pluginAction, pluginAgent, pluginWithMcp, acpCacheDir, acpCacheLog, acpCacheNoLog, acpCacheSession, sessionsSub, sessionsTarget, sessionsNoVacuum, storageDays, storageVacuum, storageDryRun, jsonOutput, doctorJson, doctorFix } = parseArgs(process.argv.slice(2)); + const { command, client, clientArgs, mitmDomains, overrides, exportSelector, exportOutput, exportFull, exportJsonl, registerConversationId, pluginAction, pluginAgent, pluginWithMcp, acpCacheDir, acpCacheLog, acpCacheNoLog, acpCacheSession, sessionsSub, sessionsTarget, sessionsNoVacuum, storageDays, storageVacuum, storageDryRun, jsonOutput, doctorJson, doctorFix, packAction, packSpec, packProject, packForce } = parseArgs(process.argv.slice(2)); if (command === "help") { process.stdout.write(HELP); return; @@ -605,6 +640,29 @@ export async function main(): Promise { return; } } + if (command === "pack") { + try { + if (packAction === "list") { + for (const p of listResolvedPacks()) { + console.log(`${p.name}\t${p.version ?? "-"}\t${p.source}${p.description ? ` ${p.description}` : ""}`); + } + return; + } + if (packAction === "install") { + const r = installPackFromNpm(packSpec!, { project: packProject, force: packForce }); + console.log(`installed pack "${r.name}"${r.version ? ` v${r.version}` : ""} -> ${r.file}`); + return; + } + if (packAction === "remove") { + console.log(`removed ${removePackFile(packSpec!, packProject)}`); + return; + } + } catch (error) { + console.error(`bili pack: ${error instanceof Error ? error.message : String(error)}`); + process.exit(1); + } + return; + } if (command === "export") { try { const text = await exportSession(exportSelector, { output: exportOutput, full: exportFull, jsonl: exportJsonl }); diff --git a/src/pack-install.ts b/src/pack-install.ts new file mode 100644 index 0000000000..a70844c1a8 --- /dev/null +++ b/src/pack-install.ts @@ -0,0 +1,161 @@ +// `bili pack install|remove <…> | list`: manage compress prompt packs on +// disk without opening an editor (#5319 item ④ — standard pack format + +// npm-distributable packs). +// +// Convention for npm packages: one file `bili-pack.json` in the package +// root, holding the SAME object shape as a hand-written +// `/packs/.json`. Installation shells out to the LOCAL +// `npm` (`npm install --prefix `), so registry auth, version +// ranges, scoped names, git URLs and `.tgz` paths all behave exactly like +// a normal npm install; we only read one file out of the result. No tar +// parsing, no new dependency. +// +// Writes follow the repo rules: names re-validated with the kernel's +// isValidPackName, surfaces sanitized exactly like dir-pack loading +// (sanitizePackSurface), an empty-after-sanitize pack is refused, and an +// existing target file is never overwritten without --force (no silent +// overwrite of user config). Writes go through the temp-file+rename +// pattern shared with every other writer here. + +import { execFileSync } from "node:child_process"; +import { closeSync, existsSync, fsyncSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { randomUUID } from "node:crypto"; +import { isValidPackName, sanitizePackSurface, type PromptPackFile } from "acp-kernel"; +import { configDir } from "./paths.js"; +import { packSurfaceHasContent } from "./web/api.js"; + +/** The file an npm package must ship to be installable as a pack. */ +export const PACK_ENTRY_FILE = "bili-pack.json"; + +const NPM_TIMEOUT_MS = 120_000; + +export interface PackInstallOptions { + /** Target `./.billion-context/packs/` instead of the user-dir default. */ + project?: boolean; + /** Replace an existing pack file (refused otherwise). */ + force?: boolean; +} + +/** Directory a pack lands in, by target. */ +export function packDirFor(project: boolean): string { + return project ? path.join(process.cwd(), ".billion-context", "packs") : path.join(configDir(), "packs"); +} + +interface InstalledPack { + /** Resolved pack name (entry's `name`, falling back to the npm package name). */ + name: string; + version?: string; + file: string; +} + +function atomicWriteFile(file: string, content: string): void { + mkdirSync(path.dirname(file), { recursive: true }); + const tempPath = `${file}.${process.pid}.${randomUUID()}.tmp`; + let descriptor: number | undefined; + try { + descriptor = openSync(tempPath, "wx", 0o644); + writeFileSync(descriptor, content, "utf8"); + fsyncSync(descriptor); + closeSync(descriptor); + descriptor = undefined; + renameSync(tempPath, file); + } catch (error) { + if (descriptor !== undefined) closeSync(descriptor); + try { unlinkSync(tempPath); } catch { } + throw error; + } +} + +/** Find the package directory npm just installed under prefix/node_modules. */ +function findInstalledPkg(prefix: string): { dir: string; manifest: Record } | undefined { + const nmRoot = path.join(prefix, "node_modules"); + if (!existsSync(nmRoot)) return undefined; + const candidates: string[] = []; + const push = (dir: string) => candidates.push(dir); + for (const entry of readdirSync(nmRoot)) { + if (entry.startsWith(".")) continue; + const full = path.join(nmRoot, entry); + if (!statSync(full).isDirectory()) continue; + if (entry.startsWith("@")) { + for (const sub of readdirSync(full)) { + push(path.join(full, sub)); + } + } else { + push(full); + } + } + let best: { dir: string; mtimeMs: number; manifest: Record } | undefined; + for (const dir of candidates) { + const manifestPath = path.join(dir, "package.json"); + if (!existsSync(manifestPath)) continue; + let manifest: Record; + try { manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as Record; } catch { continue; } + const mtimeMs = statSync(dir).mtimeMs; + if (!best || mtimeMs > best.mtimeMs) best = { dir, mtimeMs, manifest }; + } + return best ? { dir: best.dir, manifest: best.manifest } : undefined; +} + +export function installPackFromNpm(spec: string, options: PackInstallOptions = {}): InstalledPack { + const trimmed = spec.trim(); + if (!trimmed) throw new Error("spec is required (an npm package name, name@version, git URL, or .tgz path)"); + const destDir = packDirFor(options.project ?? false); + + const tmp = mkdtempSync(path.join(os.tmpdir(), "bili-pack-")); + try { + let stderr = ""; + try { + execFileSync("npm", [ + "install", "--prefix", tmp, "--no-save", "--no-audit", "--no-fund", trimmed, + ], { timeout: NPM_TIMEOUT_MS, stdio: ["ignore", "pipe", "pipe"] }); + } catch (error) { + const err = error as { stderr?: unknown; message?: string }; + stderr = typeof err.stderr === "string" ? err.stderr : ""; + if (/ENOENT/.test(String(err.message))) throw new Error("npm was not found on PATH"); + const tail = stderr.trim().split("\n").slice(-3).join(" ").slice(0, 300); + throw new Error(`npm install ${trimmed} failed${tail ? `: ${tail}` : ""}`); + } + + const installed = findInstalledPkg(tmp); + if (!installed) throw new Error(`npm reported success but no package appeared in the sandbox — cannot locate its ${PACK_ENTRY_FILE}`); + const pkgName = typeof installed.manifest.name === "string" ? installed.manifest.name : path.basename(installed.dir); + + const entryPath = path.join(installed.dir, PACK_ENTRY_FILE); + if (!existsSync(entryPath)) { + throw new Error(`${trimmed} is not a billion-context pack: it has no \`${PACK_ENTRY_FILE}\` in the package root (that file holds the pack JSON — see CONFIGURATION.md “Pack file format”)`); + } + let parsed: unknown; + try { parsed = JSON.parse(readFileSync(entryPath, "utf8")); } + catch (error) { throw new Error(`${PACK_ENTRY_FILE} in ${trimmed} is not valid JSON: ${String(error)}`); } + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + throw new Error(`${PACK_ENTRY_FILE} top level must be a JSON object`); + } + const packJson = parsed as PromptPackFile & Record; + if (!packSurfaceHasContent(sanitizePackSurface(packJson))) { + throw new Error(`${PACK_ENTRY_FILE} has no recognized pack fields (promptSections / prompts / nudgeSections / toolPrompts / adapters)`); + } + const name = typeof packJson.name === "string" && packJson.name ? packJson.name : pkgName; + if (!isValidPackName(name)) { + throw new Error(`pack name "${name}" is invalid — letters/digits/._- only, must not start with one, and no ".." (set "name" in ${PACK_ENTRY_FILE})`); + } + const file = path.join(destDir, `${name}.json`); + if (!options.force && existsSync(file)) { + throw new Error(`a pack named "${name}" already exists at ${file} — pass --force to replace it`); + } + atomicWriteFile(file, JSON.stringify(packJson, null, 2) + "\n"); + return { name, version: typeof packJson.version === "string" ? packJson.version : undefined, file }; + } finally { + rmSync(tmp, { recursive: true, force: true }); + } +} + +/** Delete a pack file by name (user dir by default, project dir with `project`). */ +export function removePackFile(name: string, project: boolean = false): string { + if (!isValidPackName(name)) throw new Error(`invalid pack name: ${name}`); + const file = path.join(packDirFor(project), `${name}.json`); + if (!existsSync(file)) throw new Error(`no pack named "${name}" in ${path.dirname(file)} (check the other location too)`); + unlinkSync(file); + return file; +} diff --git a/src/web/api.ts b/src/web/api.ts index d42ff1a4c4..c68888968c 100644 --- a/src/web/api.ts +++ b/src/web/api.ts @@ -113,7 +113,7 @@ function userPackFile(name: string): string { return join(configDir(), "packs", `${name}.json`); } -function packSurfaceHasContent(surface: ReturnType): boolean { +export function packSurfaceHasContent(surface: ReturnType): boolean { return Object.entries(surface).some(([, value]) => value !== undefined && (typeof value !== "object" || value === null ? true : Object.keys(value).length > 0)); diff --git a/tests/pack-install.test.ts b/tests/pack-install.test.ts new file mode 100644 index 0000000000..2e1296fc50 --- /dev/null +++ b/tests/pack-install.test.ts @@ -0,0 +1,74 @@ +// #5340: `bili pack install|remove|list` — installs a pack from an npm +// package (convention: bili-pack.json in the package root). Specs here are +// LOCAL DIRECTORIES so the suite stays deterministic and offline: `npm +// install ` exercises the same code path as a registry spec. + +import test from "node:test"; +import assert from "node:assert/strict"; +import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { configDir } from "../src/paths.js"; +import { installPackFromNpm, removePackFile, packDirFor, PACK_ENTRY_FILE } from "../src/pack-install.js"; +import { listResolvedPacks } from "../src/compress-settings.js"; + +function fixturePkg(withEntry: boolean, entry?: unknown): string { + const dir = mkdtempSync(path.join(os.tmpdir(), "bili-pack-fixture-")); + writeFileSync(path.join(dir, "package.json"), JSON.stringify({ name: "zz-fixture-pack", version: "1.0.0" }, null, 2)); + if (withEntry) writeFileSync(path.join(dir, PACK_ENTRY_FILE), typeof entry === "string" ? entry : JSON.stringify(entry ?? { promptSections: { philosophy: "fixture" } })); + return dir; +} + +const userPacksDir = path.join(configDir(), "packs"); + +test("install from a local package dir lands a resolvable user pack", () => { + const fixture = fixturePkg(true, { name: "zz-fixture-pack", version: "1.0.0", promptSections: { philosophy: "fixture" } }); + const r = installPackFromNpm(fixture); + assert.equal(r.name, "zz-fixture-pack"); + assert.equal(r.version, "1.0.0"); + assert.ok(existsSync(r.file)); + assert.equal(r.file, path.join(userPacksDir, "zz-fixture-pack.json")); + const listed = listResolvedPacks(); + const found = listed.find((p) => p.name === "zz-fixture-pack"); + assert.ok(found, "installed pack must appear in the resolved list"); + assert.equal(found?.source.startsWith("file:"), true); + removePackFile("zz-fixture-pack"); +}); + +test("reinstall refuses without --force and replaces with --force", () => { + const f1 = fixturePkg(true, { name: "zz-force", promptSections: { philosophy: "first" } }); + installPackFromNpm(f1); + const file = path.join(userPacksDir, "zz-force.json"); + assert.throws(() => installPackFromNpm(fixturePkg(true, { name: "zz-force", promptSections: { philosophy: "second" } })), /--force/); + // no silent overwrite without force + assert.match(readFileSync(file, "utf8"), /first/); + const r = installPackFromNpm(fixturePkg(true, { name: "zz-force", promptSections: { philosophy: "second" } }), { force: true }); + assert.equal(r.file, file); + assert.match(readFileSync(file, "utf8"), /second/); + removePackFile("zz-force"); +}); + +test("a package without bili-pack.json is refused with a clear message", () => { + const fixture = fixturePkg(false); + assert.throws(() => installPackFromNpm(fixture), new RegExp(PACK_ENTRY_FILE)); +}); + +test("invalid pack names and empty surfaces are refused, nothing written", () => { + const badName = fixturePkg(true, { name: "../evil", promptSections: { philosophy: "x" } }); + assert.throws(() => installPackFromNpm(badName), /pack name .* is invalid/); + const empty = fixturePkg(true, { description: "only unknown fields" }); + assert.throws(() => installPackFromNpm(empty), /no recognized pack fields/); +}); + +test("remove deletes the file and errors on the second call", () => { + installPackFromNpm(fixturePkg(true, { name: "zz-remove", promptSections: { philosophy: "x" } })); + const file = removePackFile("zz-remove"); + assert.ok(!existsSync(file)); + assert.throws(() => removePackFile("zz-remove"), /no pack named/); + assert.throws(() => removePackFile("../evil"), /invalid pack name/i); +}); + +test("packDirFor distinguishes project vs user targets", () => { + assert.equal(packDirFor(false), userPacksDir); + assert.equal(packDirFor(true), path.join(process.cwd(), ".billion-context", "packs")); +});