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
+54
-40
@@ -65,7 +65,7 @@ export interface UnrestrictedViewSnapshot {
|
||||
status: UnrestrictedStatus | null
|
||||
/** presetId -> fetched deploy preview. */
|
||||
previews: Record<string, UnrestrictedPreviewEntry>
|
||||
/** Whether a status refresh or recheck RPC is currently in flight. */
|
||||
/** 是否仍有设置写入或状态检查尚未完成。 */
|
||||
busy: boolean
|
||||
/** Last transport/business failure, if any. */
|
||||
message?: string
|
||||
@@ -122,6 +122,10 @@ export function createUnrestrictedController(ctx: Context) {
|
||||
busy: false,
|
||||
}
|
||||
const listeners = new Set<() => void>()
|
||||
// 每个预设只接收最近一次请求;清空标记同时废弃所有在途旧预览。
|
||||
const previewRequests = new Map<string, symbol>()
|
||||
let statusGeneration = 0
|
||||
let pendingOperations = 0
|
||||
|
||||
const view: UnrestrictedViewSource = {
|
||||
getSnapshot: () => snapshot,
|
||||
@@ -133,68 +137,74 @@ export function createUnrestrictedController(ctx: Context) {
|
||||
|
||||
function publish(): void {
|
||||
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(): void {
|
||||
previewRequests.clear()
|
||||
snapshot = { ...snapshot, previews: {} }
|
||||
}
|
||||
|
||||
async function call<T>(endpoint: string, payload: unknown): Promise<T> {
|
||||
const result = await connection.rpc.call(RPC_CHANNEL, endpoint, payload) as RpcResult<T>
|
||||
if (!result.ok) throw new Error(result.error.message)
|
||||
return result.value
|
||||
}
|
||||
|
||||
async function refresh(invalidatePreviews = false): Promise<void> {
|
||||
snapshot = { ...snapshot, busy: true }
|
||||
async function whileBusy(action: () => Promise<void>): Promise<void> {
|
||||
pendingOperations++
|
||||
publish()
|
||||
try {
|
||||
const status = await call<UnrestrictedStatus>('status', {})
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
status,
|
||||
previews: invalidatePreviews ? {} : snapshot.previews,
|
||||
busy: false,
|
||||
message: undefined,
|
||||
await action()
|
||||
} finally {
|
||||
pendingOperations--
|
||||
publish()
|
||||
}
|
||||
}
|
||||
|
||||
function readStatus(endpoint: 'status' | 'recheck', clearPreviews: boolean): Promise<void> {
|
||||
const generation = ++statusGeneration
|
||||
return whileBusy(async () => {
|
||||
try {
|
||||
const status = await call<UnrestrictedStatus>(endpoint, {})
|
||||
if (generation !== statusGeneration) return
|
||||
if (clearPreviews) invalidatePreviews()
|
||||
snapshot = { ...snapshot, status, message: undefined }
|
||||
} catch (error) {
|
||||
if (generation !== statusGeneration) return
|
||||
snapshot = { ...snapshot, message: messageOf(error) }
|
||||
}
|
||||
publish()
|
||||
} catch (error) {
|
||||
snapshot = { ...snapshot, busy: false, message: messageOf(error) }
|
||||
publish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function setEnabled(enabled: boolean): Promise<void> {
|
||||
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 refresh(clearPreviews = false): Promise<void> {
|
||||
return readStatus('status', clearPreviews)
|
||||
}
|
||||
|
||||
async function recheck(): Promise<void> {
|
||||
snapshot = { ...snapshot, busy: true }
|
||||
publish()
|
||||
try {
|
||||
const status = await call<UnrestrictedStatus>('recheck', {})
|
||||
// The recheck invalidates every cached preview: the deployed bytes may
|
||||
// have changed under a new master.
|
||||
snapshot = { ...snapshot, status, previews: {}, busy: false, message: undefined }
|
||||
publish()
|
||||
} catch (error) {
|
||||
snapshot = { ...snapshot, busy: false, message: messageOf(error) }
|
||||
publish()
|
||||
}
|
||||
function setEnabled(enabled: boolean): Promise<void> {
|
||||
return whileBusy(async () => {
|
||||
try {
|
||||
await settings.set('enabled', enabled)
|
||||
await refresh(true)
|
||||
} catch (error) {
|
||||
snapshot = { ...snapshot, message: messageOf(error) }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function recheck(): Promise<void> {
|
||||
return readStatus('recheck', true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,6 +213,8 @@ export function createUnrestrictedController(ctx: Context) {
|
||||
* @param presetId - preset id from the card's row.
|
||||
*/
|
||||
async function loadPreview(presetId: string): Promise<void> {
|
||||
const request = Symbol(presetId)
|
||||
previewRequests.set(presetId, request)
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: { ...snapshot.previews, [presetId]: { status: 'loading' } },
|
||||
@@ -210,11 +222,13 @@ export function createUnrestrictedController(ctx: Context) {
|
||||
publish()
|
||||
try {
|
||||
const preview = await call<UnrestrictedPreview>('preview', { presetId })
|
||||
if (previewRequests.get(presetId) !== request) return
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: { ...snapshot.previews, [presetId]: { status: 'ready', preview } },
|
||||
}
|
||||
} catch (error) {
|
||||
if (previewRequests.get(presetId) !== request) return
|
||||
snapshot = {
|
||||
...snapshot,
|
||||
previews: { ...snapshot.previews, [presetId]: { status: 'failed', message: messageOf(error) } },
|
||||
|
||||
+35
-16
@@ -76,6 +76,8 @@ export function apply(ctx, config) {
|
||||
runtime: new Map(),
|
||||
/** Agent -> section disposer for minimal persona shadows. */
|
||||
shadows: new Map(),
|
||||
/** 已由调用方在 agent scope 注册 persona 的 Minimal agent。 */
|
||||
customMinimal: new WeakSet(),
|
||||
/** Fused minimal prompt, computed only after the minimal anchors verify. */
|
||||
fusedMinimal: null,
|
||||
/** presetId -> last successfully fused top-level live assembly (the deploy preview). */
|
||||
@@ -212,22 +214,29 @@ export function apply(ctx, config) {
|
||||
unshadowMinimal(agent)
|
||||
return
|
||||
}
|
||||
if (state.shadows.has(agent)) return
|
||||
const dispose = agent.ctx.systemPrompt.section({
|
||||
name: 'deployment:persona-prefix',
|
||||
order: 0,
|
||||
complete: true,
|
||||
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())
|
||||
},
|
||||
})
|
||||
if (state.shadows.has(agent) || state.customMinimal.has(agent)) return
|
||||
let dispose
|
||||
try {
|
||||
dispose = agent.ctx.systemPrompt.section({
|
||||
name: 'deployment:persona-prefix',
|
||||
order: 0,
|
||||
complete: true,
|
||||
text: (context) => {
|
||||
// 结构化工具注册在子代理 scope,不能只查询全局工具目录。
|
||||
const hasStructured = context?.agent?.ctx?.tools
|
||||
?.schemas?.(context.agent)
|
||||
?.some(tool => tool.name === 'structured_output') === true
|
||||
return hasStructured
|
||||
? fusedMinimalPrompt(ANCHORS.structuredOutputFull)
|
||||
: (state.fusedMinimal ?? fusedMinimalPrompt())
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
// 原生委派角色已占用同一名称;保留调用方贡献,在组装时融合。
|
||||
if (messageOf(error) !== 'prompt section "deployment:persona-prefix" is already registered in this scope') throw error
|
||||
state.customMinimal.add(agent)
|
||||
return
|
||||
}
|
||||
state.shadows.set(agent, dispose)
|
||||
state.fusedText.set('minimal', state.fusedMinimal)
|
||||
}
|
||||
@@ -394,6 +403,14 @@ export function apply(ctx, config) {
|
||||
// the fused minimal section for this assembly when possible.
|
||||
await ensureCheck('minimal')
|
||||
syncMinimalShadow(agent)
|
||||
if (state.customMinimal.has(agent) && usable('minimal')) {
|
||||
const fused = fuseSections(original.sections, 'minimal', { isSubagent: true })
|
||||
if (fused.issues !== undefined) {
|
||||
state.runtime.set('minimal', fused.issues)
|
||||
return original
|
||||
}
|
||||
return { ...original, sections: fused.sections }
|
||||
}
|
||||
return original
|
||||
}
|
||||
// If this blank session was previously `minimal` and switched to another
|
||||
@@ -431,6 +448,7 @@ export function apply(ctx, config) {
|
||||
ctx.on('agent-preset/selected', (sessionId) => {
|
||||
const agent = ctx.get('agents')?.get?.(sessionId)
|
||||
if (agent === undefined) return
|
||||
state.customMinimal.delete(agent)
|
||||
if (!state.enabled) {
|
||||
unshadowMinimal(agent)
|
||||
return
|
||||
@@ -439,6 +457,7 @@ export function apply(ctx, config) {
|
||||
})
|
||||
|
||||
ctx.on('agent/disposed', ({ agent }) => {
|
||||
state.customMinimal.delete(agent)
|
||||
unshadowMinimal(agent)
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user