重构破限交付契约并加入部署预览

This commit is contained in:
yexi committed 2026-09-11 21:19:04 +08:00
1 parent e98a656937
commit 5c6492db71
14 files changed
+936 -112

No files matched your search

+220 -6
View File
@@ -4,6 +4,7 @@ window.__ModuleLoader__.load({
var module = { exports: {} };
var exports = module.exports;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");
//#region src/client/controller.ts
const RPC_CHANNEL = "/dsh-unrestricted";
@@ -24,7 +25,8 @@ window.__ModuleLoader__.load({
enabled: false,
writable: false,
settingsStatus: "loading",
status: null
status: null,
previews: {}
};
const listeners = /* @__PURE__ */ new Set();
const view = {
@@ -41,6 +43,7 @@ window.__ModuleLoader__.load({
writable: settingsSnapshot.writable,
settingsStatus: settingsSnapshot.status,
status: snapshot.status,
previews: snapshot.previews,
message: snapshot.message
};
for (const listener of listeners) listener();
@@ -78,6 +81,7 @@ window.__ModuleLoader__.load({
snapshot = {
...snapshot,
status,
previews: {},
message: void 0
};
publish();
@@ -89,6 +93,46 @@ window.__ModuleLoader__.load({
publish();
}
}
/**
* Fetch one preset's deploy preview. The card renders the raw fused prompt,
* so the user reads the exact bytes before switching the mode on.
* @param presetId - preset id from the card's row.
*/
async function loadPreview(presetId) {
snapshot = {
...snapshot,
previews: {
...snapshot.previews,
[presetId]: { status: "loading" }
}
};
publish();
try {
const preview = await call("preview", { presetId });
snapshot = {
...snapshot,
previews: {
...snapshot.previews,
[presetId]: {
status: "ready",
preview
}
}
};
} catch (error) {
snapshot = {
...snapshot,
previews: {
...snapshot.previews,
[presetId]: {
status: "failed",
message: messageOf(error)
}
}
};
}
publish();
}
ctx.effect(() => settings.subscribe(publish), "dsh-unrestricted: settings mirror");
ctx.effect(() => ctx.remote.$on("settings/document-updated", (namespace) => {
if (namespace === void 0 || namespace === SETTINGS_NS) refresh();
@@ -99,7 +143,8 @@ window.__ModuleLoader__.load({
hooks: { view },
setEnabled,
recheck,
refresh
refresh,
loadPreview
}) };
}
//#endregion
@@ -128,6 +173,19 @@ window.__ModuleLoader__.load({
stateValidationFailed: "校验未通过,已保持原版",
recheck: "重新校验",
refresh: "刷新",
preflightTitle: "本次即将写入的内容",
fingerprint: "契约指纹",
fingerprintNote: "与当前提示词一一对应。各模式实际部署的就是下面这段文本。",
preset: "预设",
presetStandard: "standard",
presetPtc: "ptc",
presetCordis: "cordis",
presetMinimal: "minimal",
showPreview: "查看预览",
hidePreview: "收起预览",
previewLoading: "正在组装当前提示词…",
previewUnavailable: "暂不可预览:",
previewSize: "行",
settingsUnavailable: "当前连接不可写设置(远程浏览器只读)。",
settingsLoading: "正在读取设置…",
operationFailed: "操作失败:"
@@ -155,6 +213,19 @@ window.__ModuleLoader__.load({
stateValidationFailed: "Check failed — stock prompt kept",
recheck: "Re-check",
refresh: "Refresh",
preflightTitle: "What this deployment would write",
fingerprint: "Contract fingerprint",
fingerprintNote: "One-to-one with the current prompt: the text below is exactly what each mode deploys.",
preset: "Preset",
presetStandard: "standard",
presetPtc: "ptc",
presetCordis: "cordis",
presetMinimal: "minimal",
showPreview: "Show preview",
hidePreview: "Hide preview",
previewLoading: "Assembling the current prompt…",
previewUnavailable: "No preview available: ",
previewSize: "lines",
settingsUnavailable: "This connection cannot write settings (remote browser is read-only).",
settingsLoading: "Reading settings…",
operationFailed: "Operation failed: "
@@ -359,6 +430,69 @@ window.__ModuleLoader__.load({
color: var(--dsw-alias-label-secondary);
}
.dsh-unrestricted-presetId,
.dsh-unrestricted-fingerprint {
border: 1px solid var(--dsw-alias-border-l2);
border-radius: 4px;
padding: 1px 5px;
background: var(--dsw-alias-bg-layer-2);
color: var(--dsw-alias-label-secondary);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 16px;
}
.dsh-unrestricted-fingerprint {
color: var(--dsw-alias-label-primary);
letter-spacing: 0.4px;
}
.dsh-unrestricted-inlineAction {
border: 1px solid var(--dsw-alias-border-l2);
border-radius: 6px;
padding: 1px 8px;
background: transparent;
color: var(--dsw-alias-label-secondary);
font: inherit;
font-size: 12px;
cursor: pointer;
}
.dsh-unrestricted-inlineAction:hover {
background: var(--dsw-alias-interactive-bg-hover);
color: var(--dsw-alias-label-primary);
}
.dsh-unrestricted-inlineAction:focus-visible {
outline: 2px solid var(--dsw-alias-state-business-primary);
outline-offset: 2px;
}
.dsh-unrestricted-preview {
display: flex;
flex-basis: 100%;
flex-direction: column;
gap: 6px;
box-sizing: border-box;
padding-left: 15px;
}
.dsh-unrestricted-previewText {
max-height: 320px;
margin: 0;
border: 1px solid var(--dsw-alias-border-l2);
border-radius: 8px;
padding: 10px 12px;
background: var(--dsw-alias-bg-layer-2);
color: var(--dsw-alias-label-secondary);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 17px;
overflow: auto;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.dsh-unrestricted-issues {
flex-basis: 100%;
padding-left: 15px;
@@ -409,23 +543,33 @@ window.__ModuleLoader__.load({
}
//#endregion
//#region src/client/UnrestrictedCard.tsx
/**
* The unrestricted settings card: one persisted toggle, the per-preset fusion
* state reported by the host half, and the deploy preview for the preset the
* user is inspecting. All data arrives through the props shares; the only
* component-local state is which preview is open.
*/
/** Mode rows in display order, with their locale label keys. */
const MODE_ROWS = [
{
id: "standard",
labelKey: "modeStandard"
labelKey: "modeStandard",
presetKey: "presetStandard"
},
{
id: "ptc",
labelKey: "modePtc"
labelKey: "modePtc",
presetKey: "presetPtc"
},
{
id: "cordis",
labelKey: "modeCordis"
labelKey: "modeCordis",
presetKey: "presetCordis"
},
{
id: "minimal",
labelKey: "modeMinimal"
labelKey: "modeMinimal",
presetKey: "presetMinimal"
}
];
/** Locale key for one mode state. */
@@ -446,9 +590,20 @@ window.__ModuleLoader__.load({
const { t } = props;
const view = props.useView((snapshot) => snapshot);
const status = view.status;
const [openPreview, setOpenPreview] = (0, react.useState)(null);
const hasFailedMode = status !== null && Object.values(status.modes).some((mode) => mode.state === "failed");
const summaryKey = view.settingsStatus === "loading" ? "summaryLoading" : view.settingsStatus === "unavailable" ? "summaryUnavailable" : hasFailedMode ? "summaryValidationFailed" : view.enabled ? "summaryEnabled" : "summaryDisabled";
const summaryState = view.settingsStatus === "ready" ? hasFailedMode ? "error" : view.enabled ? "active" : "off" : "pending";
/** Toggle one preset's preview, fetching the current bytes on first open. */
function togglePreview(presetId) {
if (openPreview === presetId) {
setOpenPreview(null);
return;
}
setOpenPreview(presetId);
const entry = view.previews[presetId];
if (entry === void 0 || entry.status === "failed") props.loadPreview(presetId);
}
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("details", {
className: "dsh-unrestricted-card",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("summary", {
@@ -500,6 +655,8 @@ window.__ModuleLoader__.load({
className: "dsh-unrestricted-modes",
children: MODE_ROWS.map((row) => {
const mode = status?.modes[row.id];
const entry = view.previews[row.id];
const isOpen = openPreview === row.id;
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
className: "dsh-unrestricted-mode",
children: [
@@ -515,14 +672,71 @@ window.__ModuleLoader__.load({
className: "dsh-unrestricted-modeState",
children: t(stateKey(mode))
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("code", {
className: "dsh-unrestricted-presetId",
children: t(row.presetKey)
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
type: "button",
className: "dsh-unrestricted-inlineAction",
onClick: () => {
togglePreview(row.id);
},
children: t(isOpen ? "hidePreview" : "showPreview")
}),
mode !== void 0 && mode.issues.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: "dsh-unrestricted-issues",
children: mode.issues.join("; ")
}),
isOpen && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "dsh-unrestricted-preview",
children: entry === void 0 || entry.status === "loading" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
className: "dsh-unrestricted-note",
children: t("previewLoading")
}) : entry.status === "failed" ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
className: "dsh-unrestricted-error",
children: [t("previewUnavailable"), entry.message]
}) : entry.preview?.available === false ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
className: "dsh-unrestricted-error",
children: [t("previewUnavailable"), entry.preview.reason]
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
className: "dsh-unrestricted-note",
children: [
t("previewSize"),
" ",
entry.preview?.lines,
" · ",
entry.preview?.bytes,
" B"
]
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("pre", {
className: "dsh-unrestricted-previewText",
children: entry.preview?.text
})] })
})
]
}, row.id);
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h4", {
className: "dsh-unrestricted-statusTitle",
children: t("preflightTitle")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
className: "dsh-unrestricted-note",
children: [
t("fingerprint"),
" ",
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("code", {
className: "dsh-unrestricted-fingerprint",
children: status?.contract ?? "—"
})
]
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
className: "dsh-unrestricted-note",
children: t("fingerprintNote")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
className: "dsh-unrestricted-note",
children: t("subagentNote")
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"client.js","names":[],"sources":["../src/client/controller.ts","../src/client/locales.ts","../src/client/styles.ts","../src/client/UnrestrictedCard.tsx","../src/client/index.tsx"],"sourcesContent":["/**\n * Browser-half controller for the unrestricted card: binds the `unrestricted`\n * settings namespace (the toggle) and polls the host half's private RPC for\n * per-preset fusion state. Mirrors the card-controller pattern: the apply\n * closure owns the controller, the slot inject face hands hooks and callbacks\n * to the component.\n */\nimport type { Context } from '@deepseek-ai/cordis'\nimport type { ConnectionHandle } from '@deepseek-ai/dsh-client-connection/client'\n// Type-only: the ctx.remote Context merge and the forwarded-event key face.\nimport type {} from '@deepseek-ai/dsh-api-remotes/client'\n// Type-only: the ctx.settingsScope Context merge.\nimport type {} from '@deepseek-ai/dsh-client-ui-settings/client'\n\n/** The toggle section stored in the `unrestricted` settings namespace. */\nexport interface UnrestrictedSettings {\n enabled?: boolean\n}\n\n/** Per-preset fusion state reported by the host half. */\nexport interface UnrestrictedModeState {\n state: 'off' | 'checking' | 'active' | 'failed'\n issues: string[]\n}\n\n/** Host status endpoint payload. */\nexport interface UnrestrictedStatus {\n enabled: boolean\n modes: Record<string, UnrestrictedModeState>\n}\n\n/** Full view snapshot the card subscribes to. */\nexport interface UnrestrictedViewSnapshot {\n /** Persisted toggle; undefined until the settings scope first syncs. */\n enabled: boolean\n /** Whether the Host document accepts writes. */\n writable: boolean\n /** Settings scope sync state. */\n settingsStatus: 'loading' | 'ready' | 'unavailable'\n /** Live host status; null until the first RPC answer. */\n status: UnrestrictedStatus | null\n /** Last transport/business failure, if any. */\n message?: string\n}\n\n/** Minimal RPC wire shape (structural copy of the host RpcResult). */\ntype RpcResult<T> = { ok: true; value: T } | { ok: false; error: { code: string; message: string } }\n\n/** Minimal observable contract consumed by the slot renderer's hook binding. */\nexport interface UnrestrictedViewSource {\n getSnapshot(): UnrestrictedViewSnapshot\n subscribe(listener: () => void): () => void\n}\n\n/** Inject face handed to the card at registration. */\nexport interface UnrestrictedCardInjected {\n hooks: {\n /** Reactive card view. */\n view: UnrestrictedViewSource\n }\n /** Persist the toggle. */\n setEnabled: (enabled: boolean) => Promise<void>\n /** Re-run the host-side anchor checks and refresh. */\n recheck: () => Promise<void>\n /** Re-read host status. */\n refresh: () => Promise<void>\n}\n\nconst RPC_CHANNEL = '/dsh-unrestricted'\nconst SETTINGS_NS = 'unrestricted'\n\n/** Human-readable error text for the banner. */\nfunction messageOf(error: unknown): string {\n return error instanceof Error ? error.message : String(error)\n}\n\n/**\n * Create the card controller bound to the calling plugin's context.\n * @param ctx - the browser plugin context (connection + remote + settingsScope injected).\n * @returns the controller whose face() feeds the slot inject share.\n */\nexport function createUnrestrictedController(ctx: Context) {\n const connection = ctx.get('connection') as ConnectionHandle\n const settings = ctx.settingsScope.bind<UnrestrictedSettings>({ namespace: SETTINGS_NS })\n\n let snapshot: UnrestrictedViewSnapshot = {\n enabled: false,\n writable: false,\n settingsStatus: 'loading',\n status: null,\n }\n const listeners = new Set<() => void>()\n\n const view: UnrestrictedViewSource = {\n getSnapshot: () => snapshot,\n subscribe: (listener) => {\n listeners.add(listener)\n return () => listeners.delete(listener)\n },\n }\n\n function publish(): void {\n const settingsSnapshot = settings.getSnapshot()\n snapshot = {\n enabled: settingsSnapshot.value?.enabled === true,\n writable: settingsSnapshot.writable,\n settingsStatus: settingsSnapshot.status,\n status: snapshot.status,\n message: snapshot.message,\n }\n for (const listener of listeners) listener()\n }\n\n async function call<T>(endpoint: string, payload: unknown): Promise<T> {\n const result = await connection.rpc.call(RPC_CHANNEL, endpoint, payload) as RpcResult<T>\n if (!result.ok) throw new Error(result.error.message)\n return result.value\n }\n\n async function refresh(): Promise<void> {\n try {\n const status = await call<UnrestrictedStatus>('status', {})\n snapshot = { ...snapshot, status, message: undefined }\n publish()\n } catch (error) {\n snapshot = { ...snapshot, message: messageOf(error) }\n publish()\n }\n }\n\n async function setEnabled(enabled: boolean): Promise<void> {\n await settings.set('enabled', enabled)\n publish()\n // Give the host settings watcher onLine truncated
{"version":3,"file":"client.js","names":["useState"],"sources":["../src/client/controller.ts","../src/client/locales.ts","../src/client/styles.ts","../src/client/UnrestrictedCard.tsx","../src/client/index.tsx"],"sourcesContent":["/**\n * Browser-half controller for the unrestricted card: binds the `unrestricted`\n * settings namespace (the toggle) and polls the host half's private RPC for\n * per-preset fusion state. Mirrors the card-controller pattern: the apply\n * closure owns the controller, the slot inject face hands hooks and callbacks\n * to the component.\n */\nimport type { Context } from '@deepseek-ai/cordis'\nimport type { ConnectionHandle } from '@deepseek-ai/dsh-client-connection/client'\n// Type-only: the ctx.remote Context merge and the forwarded-event key face.\nimport type {} from '@deepseek-ai/dsh-api-remotes/client'\n// Type-only: the ctx.settingsScope Context merge.\nimport type {} from '@deepseek-ai/dsh-client-ui-settings/client'\n\n/** The toggle section stored in the `unrestricted` settings namespace. */\nexport interface UnrestrictedSettings {\n enabled?: boolean\n}\n\n/** Per-preset fusion state reported by the host half. */\nexport interface UnrestrictedModeState {\n state: 'off' | 'checking' | 'active' | 'failed'\n issues: string[]\n}\n\n/** Host status endpoint payload. */\nexport interface UnrestrictedStatus {\n enabled: boolean\n /** Fingerprint of the contract bytes the host half deploys. */\n contract: string\n modes: Record<string, UnrestrictedModeState>\n}\n\n/** Deploy preview for one preset: what switching on would actually assemble. */\nexport interface UnrestrictedPreview {\n presetId: string\n contract: string\n available: boolean\n /** `live` = bytes a live agent received; `standing` = preset scope, no agent bound. */\n source?: 'live' | 'standing'\n /** Fused prompt text; present when available. */\n text?: string\n bytes?: number\n lines?: number\n /** Why no preview could be built; present when unavailable. */\n reason?: string\n}\n\n/** One fetched preview, keyed by preset, plus its in-flight/error state. */\nexport interface UnrestrictedPreviewEntry {\n status: 'loading' | 'ready' | 'failed'\n preview?: UnrestrictedPreview\n message?: string\n}\n\n/** Full view snapshot the card subscribes to. */\nexport interface UnrestrictedViewSnapshot {\n /** Persisted toggle; undefined until the settings scope first syncs. */\n enabled: boolean\n /** Whether the Host document accepts writes. */\n writable: boolean\n /** Settings scope sync state. */\n settingsStatus: 'loading' | 'ready' | 'unavailable'\n /** Live host status; null until the first RPC answer. */\n status: UnrestrictedStatus | null\n /** presetId -> fetched deploy preview. */\n previews: Record<string, UnrestrictedPreviewEntry>\n /** Last transport/business failure, if any. */\n message?: string\n}\n\n/** Minimal RPC wire shape (structural copy of the host RpcResult). */\ntype RpcResult<T> = { ok: true; value: T } | { ok: false; error: { code: string; message: string } }\n\n/** Minimal observable contract consumed by the slot renderer's hook binding. */\nexport interface UnrestrictedViewSource {\n getSnapshot(): UnrestrictedViewSnapshot\n subscribe(listener: () => void): () => void\n}\n\n/** Inject face handed to the card at registration. */\nexport interface UnrestrictedCardInjected {\n hooks: {\n /** Reactive card view. */\n view: UnrestrictedViewSource\n }\n /** Persist the toggle. */\n setEnabled: (enabled: boolean) => Promise<void>\n /** Re-run the host-side anchor checks and refresh. */\n recheck: () => Promise<void>\n /** Re-read host status. */\n refresh: () => Promise<void>\n /** Fetch the exact fused prompt this preset would deploy. */\n loadPreview: (presetId: string) => Promise<void>\n}\n\nconst RPC_CHANNEL = '/dsh-unrestricted'\nconst SETTINGS_NS = 'unrestricted'\n\n/** Human-readable error text for the banner. */\nfunction messageOf(error: unknown): string {\n return error instanceof Error ? error.message : String(error)\n}\n\n/**\n * Create the card controller bound to the calling plugin's context.\n * @param ctx - the browser plugin context (connection + remote + settingsScope injected).\n * @returns the controller whose face() feeds the slot inject share.\n */\nexport function createUnrestrictedController(ctx: Context) {\n const connection = ctx.get('connection') as ConnectionHandle\n const settings = ctx.settingsScope.bind<UnrestrictedSettings>({ namespace: SETTINGS_NS })\n\n let snapshot: UnrestrictedViewSnapshot = {\n enabled: false,\n writable: false,\n settingsStatus: 'loading',\n status: null,\n previews: {},\n }\n const listeners = new Set<() => void>()\n\n const view: UnrestrictedViewSource = {\n getSnapshot: () => snapshot,\n subscribe: (listener) => {\n listeners.add(listener)\n return () => listeners.delete(listener)\n },\n }\n\n function publish(): void {\n const settingsSnapshot = setLine truncated