mirror of
https://github.com/yexi-by/dsh-unrestricted.git
synced 2026-11-28 02:30:22 +08:00
修复 Minimal 子代理与预览并发并发布 0.2.4
This commit is contained in:
1 parent
58adb081b9
commit
7beb2d2546
13 files changed
+375
-142
No files matched your search
+61
-63
@@ -30,6 +30,9 @@ window.__ModuleLoader__.load({
|
||||
busy: false
|
||||
};
|
||||
const listeners = /* @__PURE__ */ new Set();
|
||||
const previewRequests = /* @__PURE__ */ new Map();
|
||||
let statusGeneration = 0;
|
||||
let pendingOperations = 0;
|
||||
const view = {
|
||||
getSnapshot: () => snapshot,
|
||||
subscribe: (listener) => {
|
||||
@@ -39,89 +42,80 @@ window.__ModuleLoader__.load({
|
||||
};
|
||||
function publish() {
|
||||
const settingsSnapshot = settings.getSnapshot();
|
||||
const enabled = settingsSnapshot.value?.enabled === true;
|
||||
if (enabled !== snapshot.enabled) invalidatePreviews();
|
||||
snapshot = {
|
||||
enabled: settingsSnapshot.value?.enabled === true,
|
||||
enabled,
|
||||
writable: settingsSnapshot.writable,
|
||||
settingsStatus: settingsSnapshot.status,
|
||||
status: snapshot.status,
|
||||
previews: snapshot.previews,
|
||||
busy: snapshot.busy,
|
||||
busy: pendingOperations > 0,
|
||||
message: snapshot.message
|
||||
};
|
||||
for (const listener of listeners) listener();
|
||||
}
|
||||
function invalidatePreviews() {
|
||||
previewRequests.clear();
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: {}
|
||||
};
|
||||
}
|
||||
async function call(endpoint, payload) {
|
||||
const result = await connection.rpc.call(RPC_CHANNEL, endpoint, payload);
|
||||
if (!result.ok) throw new Error(result.error.message);
|
||||
return result.value;
|
||||
}
|
||||
async function refresh(invalidatePreviews = false) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: true
|
||||
};
|
||||
async function whileBusy(action) {
|
||||
pendingOperations++;
|
||||
publish();
|
||||
try {
|
||||
const status = await call("status", {});
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
status,
|
||||
previews: invalidatePreviews ? {} : snapshot.previews,
|
||||
busy: false,
|
||||
message: void 0
|
||||
};
|
||||
publish();
|
||||
} catch (error) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: false,
|
||||
message: messageOf(error)
|
||||
};
|
||||
await action();
|
||||
} finally {
|
||||
pendingOperations--;
|
||||
publish();
|
||||
}
|
||||
}
|
||||
async function setEnabled(enabled) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: true
|
||||
};
|
||||
publish();
|
||||
try {
|
||||
await settings.set("enabled", enabled);
|
||||
await refresh(true);
|
||||
} catch (error) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: false,
|
||||
message: messageOf(error)
|
||||
};
|
||||
publish();
|
||||
}
|
||||
function readStatus(endpoint, clearPreviews) {
|
||||
const generation = ++statusGeneration;
|
||||
return whileBusy(async () => {
|
||||
try {
|
||||
const status = await call(endpoint, {});
|
||||
if (generation !== statusGeneration) return;
|
||||
if (clearPreviews) invalidatePreviews();
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
status,
|
||||
message: void 0
|
||||
};
|
||||
} catch (error) {
|
||||
if (generation !== statusGeneration) return;
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
message: messageOf(error)
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
async function recheck() {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: true
|
||||
};
|
||||
publish();
|
||||
try {
|
||||
const status = await call("recheck", {});
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
status,
|
||||
previews: {},
|
||||
busy: false,
|
||||
message: void 0
|
||||
};
|
||||
publish();
|
||||
} catch (error) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
busy: false,
|
||||
message: messageOf(error)
|
||||
};
|
||||
publish();
|
||||
}
|
||||
function refresh(clearPreviews = false) {
|
||||
return readStatus("status", clearPreviews);
|
||||
}
|
||||
function setEnabled(enabled) {
|
||||
return whileBusy(async () => {
|
||||
try {
|
||||
await settings.set("enabled", enabled);
|
||||
await refresh(true);
|
||||
} catch (error) {
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
message: messageOf(error)
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
function recheck() {
|
||||
return readStatus("recheck", true);
|
||||
}
|
||||
/**
|
||||
* Fetch one preset's deploy preview. The card renders the raw fused prompt,
|
||||
@@ -129,6 +123,8 @@ window.__ModuleLoader__.load({
|
||||
* @param presetId - preset id from the card's row.
|
||||
*/
|
||||
async function loadPreview(presetId) {
|
||||
const request = Symbol(presetId);
|
||||
previewRequests.set(presetId, request);
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: {
|
||||
@@ -139,6 +135,7 @@ window.__ModuleLoader__.load({
|
||||
publish();
|
||||
try {
|
||||
const preview = await call("preview", { presetId });
|
||||
if (previewRequests.get(presetId) !== request) return;
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: {
|
||||
@@ -150,6 +147,7 @@ window.__ModuleLoader__.load({
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
if (previewRequests.get(presetId) !== request) return;
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: {
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"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.configForms 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 /** Whether a status refresh or recheck RPC is currently in flight. */\n busy: boolean\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 and invalidate cached previews. */\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 + configForms 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.configForms.get<UnrestrictedSettings>(SETTINGS_NS)\n\n let snapshot: UnrestrictedViewSnapshot = {\n enabled: false,\n writable: false,\n settingsStatus: 'loading',\n status: null,\n previews: {},\n busy: false,\n }\n const listeners = new Set<() => void>()\n\n const view: UnrestrictedViewSource = {\n getSnapshot: () => snapshot,\n subscribe: (listener) => {\n listeners.add(listener)\n Line 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.configForms 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 /** 是否仍有设置写入或状态检查尚未完成。 */\n busy: boolean\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 and invalidate cached previews. */\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 + configForms 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.configForms.get<UnrestrictedSettings>(SETTINGS_NS)\n\n let snapshot: UnrestrictedViewSnapshot = {\n enabled: false,\n writable: false,\n settingsStatus: 'loading',\n status: null,\n previews: {},\n busy: false,\n }\n const listeners = new Set<() => void>()\n // 每个预设只接收最近一次请求;清空标记同时废弃所有在途旧预览。\n const previewRequests = new Map<string, symbol>()\n let Line truncated
|
||||
Reference in new issue
Block a user