Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/agent/dsh-native-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
// serves the live origin at GET /bili/origin on the dsh webserver; this
// section polls that route while unresolved so the entry upgrades without a
// reload or app restart. Neither source known ⇒ degrade to a hint instead of
// a dead link.
// a dead link. The same panel is mounted at two slots (#2125): settings.section
// (always present — the launcher posture has no bundle page) and
// plugins.bundle.config (keyed by package name; the plugin detail page renders
// it only when this package is installed as a profile bundle and draws the
// title itself, so that registration drops the h3).

import { createElement, useEffect, useState } from "react";

type Dict = Record<string, string>;

type SlotOptions = { name: string; id: string; order: number; label: () => string; locale: string };
type SlotOptions =
| { name: string; id: string; order: number; label: () => string; locale: string }
| { name: string; key: string; locale: string };

type ClientContext = {
effect: (fn: () => void | (() => void), label?: string) => void;
Expand Down Expand Up @@ -110,7 +116,7 @@ export function apply(ctx: ClientContext): void {
"bili: dictionaries",
);
const t = ctx.locale.bind(NS);
const section = (): unknown => {
const panel = (titled: boolean): ((props: Record<string, unknown>) => unknown) => () => {
const [origin, setOrigin] = useState<string | undefined>(readOrigin());
useEffect(() => {
if (origin !== undefined) return;
Expand All @@ -119,7 +125,7 @@ export function apply(ctx: ClientContext): void {
return createElement(
"div",
{ style: { display: "flex", flexDirection: "column", gap: 12, padding: "20px 8px" } },
createElement("h3", { style: { margin: 0, fontSize: 16, fontWeight: 600 } }, t("title")),
titled ? createElement("h3", { style: { margin: 0, fontSize: 16, fontWeight: 600 } }, t("title")) : null,
origin === undefined
? createElement("p", { style: { margin: 0, opacity: 0.7, lineHeight: 1.6 } }, t("degraded"))
: createElement(
Expand Down Expand Up @@ -147,6 +153,10 @@ export function apply(ctx: ClientContext): void {
};
ctx.slots.inject(
"settings.section",
() => ctx.slots.register({ name: "settings.section", id: "bili", order: 100, label: () => t("nav"), locale: NS }, section),
() => ctx.slots.register({ name: "settings.section", id: "bili", order: 100, label: () => t("nav"), locale: NS }, panel(true)),
);
ctx.slots.inject(
"plugins.bundle.config",
() => ctx.slots.register({ name: "plugins.bundle.config", key: "billion-context", locale: NS }, panel(false)),
);
}
146 changes: 115 additions & 31 deletions tests/dsh-settings-ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,19 @@ function findButton(node: unknown): ElementNode | undefined {
return undefined;
}

test("#1590: client bundle registers the settings.section entry bili (wrapper id, require purity, both render branches)", async () => {
function findTag(node: unknown, type: string): ElementNode | undefined {
if (node !== null && typeof node === "object") {
const el = node as ElementNode;
if (el.type === type) return el;
for (const child of el.children ?? []) {
const hit = findTag(child, type);
if (hit !== undefined) return hit;
}
}
return undefined;
}

test("#1590/#2125: client bundle registers the settings.section entry bili AND the plugins.bundle.config panel (wrapper id, require purity, both render branches)", async () => {
const { build } = await import("esbuild");
// Reuse tsup.config.ts as the single source of truth for the wrapper
// banner/footer and externals so the test cannot drift from the shipped
Expand Down Expand Up @@ -313,13 +325,13 @@ test("#1590: client bundle registers the settings.section entry bili (wrapper id
assert.deepEqual(Array.from(face.inject), ["slots", "locale"]);
assert.equal(typeof face.apply, "function");

const runApply = (): { options: Record<string, unknown>; component: () => unknown; zh: Record<string, string>; en: Record<string, string> } => {
type Entry = { options: Record<string, unknown>; component: () => unknown };
const runApply = (): { slots: string[]; bySlot: Map<string, Entry>; zh: Record<string, string>; en: Record<string, string> } => {
const effects: Array<() => void> = [];
const dicts: Record<string, { zh: Record<string, string>; en: Record<string, string> }> = {};
let slot: string | undefined;
let provide: (() => void) | undefined;
let options: Record<string, unknown> | undefined;
let component: (() => unknown) | undefined;
const slots: string[] = [];
const bySlot = new Map<string, Entry>();
let current: string | undefined;
face.apply({
effect: (fn: () => void, _label?: string) => effects.push(fn),
locale: {
Expand All @@ -330,37 +342,46 @@ test("#1590: client bundle registers the settings.section entry bili (wrapper id
},
slots: {
inject: (name: string, p: () => void) => {
slot = name;
provide = p;
slots.push(name);
current = name;
p();
},
register: (opts: Record<string, unknown>, comp: () => unknown) => {
options = opts;
component = comp;
// Hook indices restart on every render (React matches hooks
// by position within a single render), so wrap the entry.
bySlot.set(current!, { options: opts, component: (): unknown => { hookIndex = 0; return comp(); } });
},
},
});
for (const eff of effects) eff();
assert.equal(slot, "settings.section");
provide!();
assert.ok(options !== undefined && component !== undefined && dicts["bili"] !== undefined);
// Hook indices restart on every render (React matches hooks by
// position within a single render), so wrap the entry point.
return { options: options!, component: (): unknown => { hookIndex = 0; return component!(); }, zh: dicts["bili"].zh, en: dicts["bili"].en };
assert.deepEqual(slots, ["settings.section", "plugins.bundle.config"]);
assert.equal(bySlot.size, 2);
assert.ok(dicts["bili"] !== undefined);
return { slots, bySlot, zh: dicts["bili"].zh, en: dicts["bili"].en };
};

sandbox.__BILI__ = { origin: "http://127.0.0.1:8787" };
const withOrigin = runApply();
assert.equal(withOrigin.options.name, "settings.section");
assert.equal(withOrigin.options.id, "bili");
assert.equal(withOrigin.options.order, 100);
assert.equal(withOrigin.options.locale, "bili");
assert.equal(typeof withOrigin.options.label, "function");
assert.equal((withOrigin.options.label as () => string)(), "bili设置");
const settings = withOrigin.bySlot.get("settings.section")!;
assert.equal(settings.options.name, "settings.section");
assert.equal(settings.options.id, "bili");
assert.equal(settings.options.order, 100);
assert.equal(settings.options.locale, "bili");
assert.equal(typeof settings.options.label, "function");
assert.equal((settings.options.label as () => string)(), "bili设置");
const bundle = withOrigin.bySlot.get("plugins.bundle.config")!;
assert.equal(bundle.options.name, "plugins.bundle.config");
assert.equal(bundle.options.key, "billion-context");
assert.equal(bundle.options.locale, "bili");
// Keyed slot: the host matches the key against its own package name — no
// list-slot fields may ride along.
assert.deepEqual(Object.keys(bundle.options).sort(), ["key", "locale", "name"]);
assert.deepEqual(Object.keys(withOrigin.zh).sort(), Object.keys(withOrigin.en).sort());
calls.length = 0;
resetHooks();
const tree = withOrigin.component() as ElementNode;
const tree = settings.component() as ElementNode;
assert.equal(tree.type, "div");
assert.notEqual(findTag(tree, "h3"), undefined, "the settings section keeps its own title");
const button = findButton(tree);
assert.ok(button !== undefined, "origin present renders the open button");
assert.equal(typeof button.props?.onClick, "function");
Expand All @@ -372,18 +393,41 @@ test("#1590: client bundle registers the settings.section entry bili (wrapper id
(button.props!.onClick as () => void)();
assert.deepEqual(opened, ["http://127.0.0.1:8787/__bili/"]);

// #2125: the bundle panel reuses the probe/button/hint but drops the title
// — the plugin detail page draws it.
calls.length = 0;
resetHooks();
const btree = bundle.component() as ElementNode;
assert.equal(btree.type, "div");
assert.equal(findTag(btree, "h3"), undefined, "the bundle panel does not repeat the page title");
const bButton = findButton(btree);
assert.ok(bButton !== undefined, "origin present renders the open button in the bundle panel");
assert.equal(typeof bButton.props?.onClick, "function");
const btexts: string[] = [];
collectText(btree, btexts);
assert.ok(btexts.some((x) => x.includes("http://127.0.0.1:8787")), `bundle button label carries the origin: ${JSON.stringify(btexts)}`);
opened.length = 0;
(bButton.props!.onClick as () => void)();
assert.deepEqual(opened, ["http://127.0.0.1:8787/__bili/"]);

delete sandbox.__BILI__;
const degraded = runApply();
calls.length = 0;
resetHooks();
const degTree = degraded.component() as ElementNode;
const degTree = degraded.bySlot.get("settings.section")!.component() as ElementNode;
assert.equal(findButton(degTree), undefined);
const degTexts: string[] = [];
collectText(degTree, degTexts);
assert.ok(degTexts.some((t) => t.includes("/acp")), `degraded hint points at /acp: ${JSON.stringify(degTexts)}`);
resetHooks();
const degBundleTree = degraded.bySlot.get("plugins.bundle.config")!.component() as ElementNode;
assert.equal(findButton(degBundleTree), undefined, "the bundle panel degrades like the settings section");
const degBundleTexts: string[] = [];
collectText(degBundleTree, degBundleTexts);
assert.ok(degBundleTexts.some((t) => t.includes("/acp")), "the bundle degraded hint points at /acp");
});

test("#1809: client polls /bili/origin while unresolved — upgrades on success, stays degraded and cancels when absent", async () => {
test("#1809/#2125: client polls /bili/origin while unresolved — upgrades on success, stays degraded and cancels when absent (both slots)", async () => {
const { build } = await import("esbuild");
const configs = ((await import("../tsup.config.ts")).default) as unknown as Array<{
entry: Record<string, string>;
Expand All @@ -409,7 +453,7 @@ test("#1809: client polls /bili/origin while unresolved — upgrades on success,
const code = result.outputFiles![0].text;

type Face = { inject: string[]; apply: (ctx: unknown) => void };
const mount = (extra: Record<string, unknown>): { component: () => unknown; resetHooks: () => void; runCleanups: () => void } => {
const mount = (extra: Record<string, unknown>, slotName = "settings.section"): { component: () => unknown; resetHooks: () => void; runCleanups: () => void } => {
const registrations: Array<{ id: string; factory: (require: (spec: string) => unknown) => unknown }> = [];
const sandbox: Record<string, unknown> = {};
sandbox.window = { __ModuleLoader__: { load: (reg: (typeof registrations)[number]) => registrations.push(reg) } };
Expand Down Expand Up @@ -444,7 +488,8 @@ test("#1809: client polls /bili/origin while unresolved — upgrades on success,
};
const face = registrations[0].factory(requireStub) as Face;
const dicts: Record<string, { zh: Record<string, string>; en: Record<string, string> }> = {};
let component: (() => unknown) | undefined;
const components = new Map<string, () => unknown>();
let current: string | undefined;
face.apply({
effect: (fn: () => void) => fn(),
locale: {
Expand All @@ -454,14 +499,17 @@ test("#1809: client polls /bili/origin while unresolved — upgrades on success,
bind: (ns: string) => (key: string) => dicts[ns]?.zh?.[key] ?? key,
},
slots: {
inject: (_name: string, p: () => void) => p(),
inject: (name: string, p: () => void) => {
current = name;
p();
},
register: (_opts: Record<string, unknown>, comp: () => unknown) => {
component = comp;
components.set(current!, comp);
},
},
});
assert.ok(component !== undefined);
const bound = component;
assert.ok(components.has(slotName), `slot ${slotName} must be registered`);
const bound = components.get(slotName)!;
return {
// Hook indices restart on every render (React semantics).
component: (): unknown => { hookIndex = 0; return bound(); },
Expand Down Expand Up @@ -522,4 +570,40 @@ test("#1809: client polls /bili/origin while unresolved — upgrades on success,
m.runCleanups();
assert.ok(cleared >= 1, "pending retry timers are cancelled on unmount");
}

{
// #2125: the bundle panel runs the same probe wiring through its own
// registration.
const opened: string[] = [];
const m = mount(
{
open: (url: string) => opened.push(url),
fetch: async () => ({ ok: true, json: async () => ({ origin: "http://127.0.0.1:9999" }) }),
setTimeout,
clearTimeout,
},
"plugins.bundle.config",
);
m.resetHooks();
const first = m.component() as ElementNode;
assert.equal(findButton(first), undefined);
await tick();
const second = m.component() as ElementNode;
const button = findButton(second);
assert.ok(button !== undefined, "the bundle panel upgrades on a resolved origin");
(button.props!.onClick as () => void)();
assert.deepEqual(opened, ["http://127.0.0.1:9999/__bili/"]);
m.runCleanups();
}
});

test("#2125: the manifest declares no dsh.client.inject (pre-0.2 dsh compat)", () => {
// dsh < 0.2.x cannot resolve "@deepseek-ai/dsh-client-ui-plugin-manager" and its
// client-module scanner stalls the whole web UI on an unknown inject target.
// dsh >= 0.2.0-rc.x ships the slot host inside dsh-web-app, so no injection is
// required — re-adding it would freeze older clients into an endless spinner.
const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")) as {
dsh?: { client?: { inject?: unknown } };
};
assert.equal(pkg.dsh?.client?.inject ?? null, null, "dsh.client.inject would break pre-0.2 dsh clients");
});
Loading