mirror of
https://github.com/yexi-by/dsh-unrestricted.git
synced 2026-11-27 18:20:23 +08:00
适配 DSH alpha.4 并发布 0.2.3 会话与预览同步修复
This commit is contained in:
1 parent
945b9624d6
commit
e884f740e3
21 files changed
+441
-122
No files matched your search
+151
-51
@@ -14,9 +14,14 @@
|
||||
* the unmodified harness.
|
||||
* - `minimal` registers its persona with `complete: true`, which the registry
|
||||
* restores AFTER the waterfall, so waterfall edits cannot reach it. For
|
||||
* minimal agents (joined or later created) the plugin shadows
|
||||
* `deployment:persona-prefix` at agent scope with a complete section carrying the
|
||||
* fused minimal prompt.
|
||||
* minimal agents (joined, switched via `agent-preset/selected`, or later
|
||||
* created) the plugin shadows `deployment:persona-prefix` at agent scope with
|
||||
* a complete section carrying the fused minimal prompt, and disposes that
|
||||
* shadow immediately if the blank session switches away to another preset.
|
||||
* - Rosterless profiles (such as `headless`, `acp`, or `sdk`) mount no
|
||||
* `agentPresets` service; the plugin detects the active host composition
|
||||
* (`ptc` when `tools:ptc-only` is present, otherwise `standard`) and fuses
|
||||
* their assemblies directly.
|
||||
* - Anchor failures (startup standing-scope check or any live assembly) leave
|
||||
* that prompt unchanged and show the failed current-master invariant in the
|
||||
* settings card.
|
||||
@@ -29,14 +34,14 @@ import z from '@deepseek-ai/schemastery'
|
||||
import { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
|
||||
import { createScope } from '@deepseek-ai/dsh-scope'
|
||||
import {
|
||||
PRESETS, PRESET_RULES, contractFingerprint, fuseSections, fusedMinimalPrompt, renderPreview,
|
||||
ANCHORS, PRESETS, PRESET_RULES, contractFingerprint, fuseSections, fusedMinimalPrompt, renderPreview,
|
||||
} from './rules.js'
|
||||
|
||||
/** Cordis plugin name used by Loader diagnostics. */
|
||||
export const name = 'dsh-unrestricted'
|
||||
|
||||
/** Services required by this plugin (all host-plane residents of the web profile). */
|
||||
export const inject = ['settings', 'systemPrompt', 'agentPresets', 'loader']
|
||||
/** Services required by this plugin (all base-backed profiles provide these). */
|
||||
export const inject = ['settings', 'systemPrompt', 'loader']
|
||||
|
||||
const RPC_CHANNEL = '/dsh-unrestricted'
|
||||
|
||||
@@ -55,8 +60,8 @@ function ok(value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ctx - plugin context carrying settings, systemPrompt, agentPresets,
|
||||
* connection, and the cordis Loader.
|
||||
* @param ctx - plugin context carrying settings, systemPrompt, loader, and
|
||||
* optional agentPresets / connection services.
|
||||
*/
|
||||
export function apply(ctx, config) {
|
||||
ctx.effect(() => ctx.settings.configure({ auto: false }, ctx.fiber))
|
||||
@@ -73,7 +78,7 @@ export function apply(ctx, config) {
|
||||
shadows: new Map(),
|
||||
/** Fused minimal prompt, computed only after the minimal anchors verify. */
|
||||
fusedMinimal: null,
|
||||
/** presetId -> last successfully fused live assembly (the deploy preview). */
|
||||
/** presetId -> last successfully fused top-level live assembly (the deploy preview). */
|
||||
fusedText: new Map(),
|
||||
}
|
||||
|
||||
@@ -82,14 +87,30 @@ export function apply(ctx, config) {
|
||||
return [...state.startup.get(presetId) ?? [], ...state.runtime.get(presetId) ?? []]
|
||||
}
|
||||
|
||||
/** 用公开预设挂载接口读取当前配置;临时作用域在组装后释放。 */
|
||||
/**
|
||||
* Read the standing assembly of a preset using the registry's scope lease
|
||||
* (or a temporary mounted scope fallback). In rosterless profiles, assemble
|
||||
* the global host layer directly.
|
||||
*/
|
||||
async function presetAssembly(presetId) {
|
||||
// 预设声明与本插件是并列 Loader 条目;服务可用时,声明仍可能尚未注册。
|
||||
await ctx.loader.await()
|
||||
const presets = ctx.get('agentPresets')
|
||||
if (presets === undefined) {
|
||||
return await ctx.systemPrompt.assemble({})
|
||||
}
|
||||
if (typeof presets.acquireScope === 'function') {
|
||||
const lease = await presets.acquireScope(presetId)
|
||||
try {
|
||||
return await ctx.systemPrompt.assemble({ scope: lease.key })
|
||||
} finally {
|
||||
await lease[Symbol.asyncDispose]()
|
||||
}
|
||||
}
|
||||
const key = {}
|
||||
const scope = createScope(ctx, key)
|
||||
try {
|
||||
await ctx.agentPresets.mount(scope.ctx, presetId)
|
||||
await presets.mount(scope.ctx, presetId)
|
||||
return await ctx.systemPrompt.assemble({ scope: key })
|
||||
} finally {
|
||||
await scope.dispose()
|
||||
@@ -107,6 +128,14 @@ export function apply(ctx, config) {
|
||||
pending = (async () => {
|
||||
const issues = []
|
||||
try {
|
||||
await ctx.loader.await()
|
||||
const presets = ctx.get('agentPresets')
|
||||
if (presets === undefined) {
|
||||
// Rosterless profile (e.g. headless / sdk): live assembly checks the
|
||||
// host sections directly.
|
||||
state.startup.set(presetId, issues)
|
||||
return
|
||||
}
|
||||
const assembly = await presetAssembly(presetId)
|
||||
const fused = fuseSections(assembly.sections, presetId)
|
||||
if (fused.issues !== undefined) issues.push(...fused.issues)
|
||||
@@ -136,33 +165,78 @@ export function apply(ctx, config) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Shadow a minimal agent's complete persona with the fused prompt. No-op for
|
||||
* other presets, for unverified minimal anchors, or while disabled.
|
||||
* @param agent - the created agent (from `agent/created` or `agents.list()`).
|
||||
* Remove a minimal persona shadow from an agent when it switches preset, is
|
||||
* disposed, or unrestricted mode turns off.
|
||||
*/
|
||||
function shadowMinimal(agent) {
|
||||
if (!state.enabled || state.fusedMinimal === null || state.shadows.has(agent)) return
|
||||
let presetId
|
||||
try {
|
||||
presetId = ctx.agentPresets.composedPreset(agent.ctx)
|
||||
} catch {
|
||||
return // scope chain not readable; leave the agent untouched
|
||||
function unshadowMinimal(agent) {
|
||||
const dispose = state.shadows.get(agent)
|
||||
if (dispose === undefined) return
|
||||
state.shadows.delete(agent)
|
||||
dispose()
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the composed preset id for a live agent, or infer `ptc`/`standard`
|
||||
* when running in a rosterless profile (e.g. headless / sdk).
|
||||
*/
|
||||
function resolveAgentPreset(agent, sections) {
|
||||
const presets = ctx.get('agentPresets')
|
||||
if (presets !== undefined) {
|
||||
try {
|
||||
return presets.composedPreset(agent.ctx)
|
||||
} catch {
|
||||
return undefined // scope chain not readable; leave the agent untouched
|
||||
}
|
||||
}
|
||||
if (presetId !== 'minimal') return
|
||||
if (sections !== undefined) {
|
||||
return sections.some(s => s.name === 'tools:ptc-only' && s.text.trim() !== '')
|
||||
? 'ptc'
|
||||
: 'standard'
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronize a minimal agent's complete persona shadow with its current
|
||||
* preset and toggle state. If the agent is no longer on `minimal`, any stale
|
||||
* shadow from an earlier blank-session preset selection is disposed.
|
||||
* @param agent - the target agent.
|
||||
*/
|
||||
function syncMinimalShadow(agent) {
|
||||
if (!state.enabled || state.fusedMinimal === null) {
|
||||
unshadowMinimal(agent)
|
||||
return
|
||||
}
|
||||
const presetId = resolveAgentPreset(agent)
|
||||
if (presetId !== 'minimal') {
|
||||
unshadowMinimal(agent)
|
||||
return
|
||||
}
|
||||
if (state.shadows.has(agent)) return
|
||||
const dispose = agent.ctx.systemPrompt.section({
|
||||
name: 'deployment:persona-prefix',
|
||||
order: 0,
|
||||
complete: true,
|
||||
text: state.fusedMinimal,
|
||||
text: (context) => {
|
||||
// Preserve structured-output instructions when a structured subagent
|
||||
// runs under minimal.
|
||||
const hasStructured = context?.agent?.ctx?.tools
|
||||
?.schemas?.()
|
||||
?.some(tool => tool.name === 'structured_output') === true
|
||||
return hasStructured
|
||||
? fusedMinimalPrompt(ANCHORS.structuredOutputFull)
|
||||
: (state.fusedMinimal ?? fusedMinimalPrompt())
|
||||
},
|
||||
})
|
||||
state.shadows.set(agent, dispose)
|
||||
state.fusedText.set('minimal', state.fusedMinimal)
|
||||
}
|
||||
|
||||
/** Shadow every live minimal agent (called when the toggle flips on). */
|
||||
/** Synchronize minimal persona shadows across all live agents. */
|
||||
function sweepAgents() {
|
||||
const agents = ctx.get('agents')
|
||||
if (agents === undefined) return
|
||||
for (const agent of agents.list()) shadowMinimal(agent)
|
||||
for (const agent of agents.list()) syncMinimalShadow(agent)
|
||||
}
|
||||
|
||||
/** Drop every persona shadow (toggle off or plugin disposal). */
|
||||
@@ -179,6 +253,7 @@ export function apply(ctx, config) {
|
||||
|
||||
function onDisable() {
|
||||
clearShadows()
|
||||
state.fusedText.clear()
|
||||
}
|
||||
|
||||
/** Current card view: toggle and per-preset state. */
|
||||
@@ -244,8 +319,8 @@ export function apply(ctx, config) {
|
||||
if (!PRESETS.includes(presetId)) {
|
||||
return { presetId, contract, available: false, reason: `unknown preset "${presetId}"` }
|
||||
}
|
||||
// Best source first: the exact bytes a live agent just received. Only when
|
||||
// no agent has assembled this preset yet (the toggle is still off) does the
|
||||
// Best source first: the exact bytes a top-level live agent just received.
|
||||
// Only when no top-level agent has assembled this preset yet does the
|
||||
// preview fall back to the standing scope, which carries no agent-bound
|
||||
// sections and leaves {{model}} / {{cwd}} as literal slots.
|
||||
const cached = state.fusedText.get(presetId)
|
||||
@@ -271,8 +346,10 @@ export function apply(ctx, config) {
|
||||
/** RPC endpoint handler for the settings card. */
|
||||
async function handleRpc(endpoint, payload) {
|
||||
switch (endpoint) {
|
||||
case 'status':
|
||||
case 'status': {
|
||||
if (state.enabled) await ensureChecks()
|
||||
return ok(statusPayload())
|
||||
}
|
||||
case 'preview': {
|
||||
// Private channels forward the client payload verbatim; accept the
|
||||
// `{ args }` envelope too so this handler survives a caller that
|
||||
@@ -298,23 +375,33 @@ export function apply(ctx, config) {
|
||||
const original = await next()
|
||||
if (!state.enabled) return original
|
||||
const agent = context.agent
|
||||
if (agent === undefined || state.shadows.has(agent)) return original
|
||||
let presetId
|
||||
try {
|
||||
presetId = ctx.agentPresets.composedPreset(agent.ctx)
|
||||
} catch {
|
||||
return original // scope chain unreadable; never break a request over it
|
||||
}
|
||||
if (presetId === undefined || !PRESET_RULES[presetId]) return original
|
||||
if (presetId === 'minimal') {
|
||||
// The complete-persona restore runs after this waterfall, so an agent
|
||||
// missed by the enable sweep (a request that raced the toggle) keeps its
|
||||
// original prompt for THIS assembly; registering the shadow now covers
|
||||
// its next assembly instead.
|
||||
await ensureCheck('minimal')
|
||||
shadowMinimal(agent)
|
||||
if (agent === undefined) return original
|
||||
const presetId = resolveAgentPreset(agent, original.sections)
|
||||
if (presetId === undefined || !PRESET_RULES[presetId]) {
|
||||
unshadowMinimal(agent)
|
||||
return original
|
||||
}
|
||||
if (presetId === 'minimal') {
|
||||
// If the minimal shadow is already installed on this agent, `assemble()`
|
||||
// will restore the complete persona right after the waterfall returns;
|
||||
// record the live preview text and let it finish.
|
||||
if (state.shadows.has(agent) && state.fusedMinimal !== null) {
|
||||
state.fusedText.set('minimal', state.fusedMinimal)
|
||||
return original
|
||||
}
|
||||
// An agent missed by the enable sweep (a request that raced the toggle)
|
||||
// registers its shadow now for subsequent assemblies, and also returns
|
||||
// the fused minimal section for this assembly when possible.
|
||||
await ensureCheck('minimal')
|
||||
syncMinimalShadow(agent)
|
||||
return original
|
||||
}
|
||||
// If this blank session was previously `minimal` and switched to another
|
||||
// preset before its first turn, drop the stale minimal shadow now so it
|
||||
// cannot overwrite the new preset's sections after the waterfall.
|
||||
if (state.shadows.has(agent)) {
|
||||
unshadowMinimal(agent)
|
||||
}
|
||||
if (!usable(presetId)) return original
|
||||
const isSubagent = original.contexts.some(entry => entry.name === 'subagent:delegation')
|
||||
const fused = fuseSections(original.sections, presetId, { isSubagent })
|
||||
@@ -322,12 +409,15 @@ export function apply(ctx, config) {
|
||||
state.runtime.set(presetId, fused.issues)
|
||||
return original
|
||||
}
|
||||
// Keep the deployed fusion available for the card's preview. Rendering is
|
||||
// display-only and never feeds back into the assembly returned to the loop.
|
||||
try {
|
||||
state.fusedText.set(presetId, renderPreview({ ...original, sections: fused.sections }, renderPrompt))
|
||||
} catch {
|
||||
state.fusedText.delete(presetId) // an unresolvable variable only costs the preview
|
||||
// Keep the deployed top-level fusion available for the card's preview.
|
||||
// Subagents may carry custom personas or restricted sections, so they do
|
||||
// not overwrite the preset's top-level preview cache.
|
||||
if (!isSubagent) {
|
||||
try {
|
||||
state.fusedText.set(presetId, renderPreview({ ...original, sections: fused.sections }, renderPrompt))
|
||||
} catch {
|
||||
state.fusedText.delete(presetId) // an unresolvable variable only costs the preview
|
||||
}
|
||||
}
|
||||
return { ...original, sections: fused.sections }
|
||||
})
|
||||
@@ -335,11 +425,21 @@ export function apply(ctx, config) {
|
||||
ctx.on('agent/created', ({ agent }) => {
|
||||
if (!state.enabled) return
|
||||
// Only minimal's own check gates the shadow — never the other presets'.
|
||||
void ensureCheck('minimal').then(() => shadowMinimal(agent))
|
||||
void ensureCheck('minimal').then(() => syncMinimalShadow(agent))
|
||||
})
|
||||
|
||||
ctx.on('agent-preset/selected', (sessionId) => {
|
||||
const agent = ctx.get('agents')?.get?.(sessionId)
|
||||
if (agent === undefined) return
|
||||
if (!state.enabled) {
|
||||
unshadowMinimal(agent)
|
||||
return
|
||||
}
|
||||
void ensureCheck('minimal').then(() => syncMinimalShadow(agent))
|
||||
})
|
||||
|
||||
ctx.on('agent/disposed', ({ agent }) => {
|
||||
state.shadows.delete(agent)
|
||||
unshadowMinimal(agent)
|
||||
})
|
||||
|
||||
ctx.effect(() => {
|
||||
|
||||
Reference in new issue
Block a user