mirror of
https://github.com/yexi-by/dsh-unrestricted.git
synced 2026-11-28 10:30:23 +08:00
feat: 支持自由编辑破限提示词与一键恢复默认(v0.2.17)
This commit is contained in:
1 parent
3d7177dcb9
commit
ccc61fac4c
17 files changed
+1122
-246
No files matched your search
@@ -3,10 +3,17 @@ import test from 'node:test'
|
||||
import { createUnrestrictedController } from '../src/client/controller.ts'
|
||||
|
||||
const settle = () => new Promise(setImmediate)
|
||||
const status = (version, enabled = true) => ({ enabled, version, modes: {} })
|
||||
const DEFAULT_PROMPT = '## Default execution contract'
|
||||
const status = (version, enabled = true, extra = {}) => ({
|
||||
enabled,
|
||||
defaultPrompt: DEFAULT_PROMPT,
|
||||
version,
|
||||
modes: {},
|
||||
...extra,
|
||||
})
|
||||
|
||||
/** 保留真实控制器,仅控制设置提交、配置事件和 RPC 的完成时序。 */
|
||||
async function fixture() {
|
||||
async function fixture({ supportsUnset = true } = {}) {
|
||||
let value = { enabled: true }
|
||||
const subscribers = new Set()
|
||||
const events = new Map()
|
||||
@@ -15,6 +22,7 @@ async function fixture() {
|
||||
recheck: [],
|
||||
preview: [],
|
||||
write: [],
|
||||
unset: [],
|
||||
}
|
||||
function defer(endpoint, payload) {
|
||||
return new Promise((resolve, reject) => pending[endpoint].push({ payload, resolve, reject }))
|
||||
@@ -26,6 +34,7 @@ async function fixture() {
|
||||
return () => subscribers.delete(listener)
|
||||
},
|
||||
set: (field, next) => defer('write', { field, next }),
|
||||
...(supportsUnset ? { unset: field => defer('unset', { field }) } : {}),
|
||||
}
|
||||
const ctx = {
|
||||
get: () => ({
|
||||
@@ -174,3 +183,58 @@ test('切换隐藏宿主品牌标识立即使旧预览失效并在提交后同
|
||||
assert.equal(f.view().busy, false)
|
||||
assert.equal(f.view().status.stripHostIdentity, true)
|
||||
})
|
||||
|
||||
test('保存与重置自定义提示词:规范换行、清理预览并在恢复默认时卸载字段', async () => {
|
||||
const f = await fixture()
|
||||
assert.equal(f.view().defaultPrompt, DEFAULT_PROMPT)
|
||||
assert.equal(f.view().customPrompt, '')
|
||||
assert.equal(f.view().isCustomPrompt, false)
|
||||
|
||||
const preview = f.face.loadPreview('standard')
|
||||
f.answer(f.take('preview'), { available: true, text: 'default preview' })
|
||||
await preview
|
||||
assert.equal(f.view().previews.standard.preview.text, 'default preview')
|
||||
|
||||
const saving = f.face.setCustomPrompt('## Custom\r\nLine 2')
|
||||
const writeRequest = f.take('write')
|
||||
assert.deepEqual(writeRequest.payload, { field: 'customPrompt', next: '## Custom\nLine 2' })
|
||||
f.commitSettings({ customPrompt: '## Custom\nLine 2' })
|
||||
assert.equal(f.view().customPrompt, '## Custom\nLine 2')
|
||||
assert.equal(f.view().isCustomPrompt, true)
|
||||
assert.equal(f.view().previews.standard, undefined)
|
||||
|
||||
f.answer(f.take('status'), status('0.2.17', true, { customPrompt: '## Custom\nLine 2', isCustomPrompt: true }))
|
||||
writeRequest.resolve(true)
|
||||
await settle()
|
||||
f.answer(f.take('status'), status('0.2.17', true, { customPrompt: '## Custom\nLine 2', isCustomPrompt: true }))
|
||||
assert.equal(await saving, true)
|
||||
assert.equal(f.view().busy, false)
|
||||
|
||||
// 保存与内置默认一致的内容时直接 unset,恢复默认状态。
|
||||
const matchingDefault = f.face.setCustomPrompt(`${DEFAULT_PROMPT}\r`.replace('\r', ''))
|
||||
const unsetRequest = f.take('unset')
|
||||
assert.deepEqual(unsetRequest.payload, { field: 'customPrompt' })
|
||||
f.commitSettings({ customPrompt: '' })
|
||||
assert.equal(f.view().isCustomPrompt, false)
|
||||
f.answer(f.take('status'), status('0.2.17', true, { customPrompt: '', isCustomPrompt: false }))
|
||||
unsetRequest.resolve(true)
|
||||
await settle()
|
||||
f.answer(f.take('status'), status('0.2.17', true, { customPrompt: '', isCustomPrompt: false }))
|
||||
assert.equal(await matchingDefault, true)
|
||||
})
|
||||
|
||||
test('缺少 settings.unset 时重置自定义提示词回退为空串,写入拒绝或异常返回 false', async () => {
|
||||
const f = await fixture({ supportsUnset: false })
|
||||
const resetting = f.face.resetCustomPrompt()
|
||||
const writeRequest = f.take('write')
|
||||
assert.deepEqual(writeRequest.payload, { field: 'customPrompt', next: '' })
|
||||
writeRequest.resolve(false)
|
||||
await settle()
|
||||
f.answer(f.take('status'), status('0.2.17', true))
|
||||
assert.equal(await resetting, false)
|
||||
|
||||
const failing = f.face.setCustomPrompt('## Fail case')
|
||||
f.take('write').reject(new Error('disk full'))
|
||||
assert.equal(await failing, false)
|
||||
assert.equal(f.view().message, 'disk full')
|
||||
})
|
||||
+30
-2
@@ -13,8 +13,9 @@ import assert from 'node:assert/strict'
|
||||
import {
|
||||
ANCHORS, BLOCK_SECTION, EXECUTION_MODE_BLOCK,
|
||||
HOST_IDENTITY_CONTEXTS, HOST_IDENTITY_SECTIONS, PRESETS,
|
||||
contractAdditions, fuseSections, fusedMinimalPrompt, renderPreview,
|
||||
sanitizeSectionBrandText, stripHostIdentityFromAssembly,
|
||||
contractAdditions, fuseSections, fusedMinimalPrompt, normalizePromptText,
|
||||
renderPreview, resolveExecutionModeBlock, sanitizeSectionBrandText,
|
||||
stripHostIdentityFromAssembly,
|
||||
} from '../src/rules.js'
|
||||
|
||||
const FIXTURES = fileURLToPath(new URL('./fixtures/', import.meta.url))
|
||||
@@ -455,3 +456,30 @@ test('fuseSections supports dynamic extra presets only when allowDynamicPreset i
|
||||
const ptcSection = allowed.sections.find(s => s.name === 'tools:ptc-only')
|
||||
assert.ok(ptcSection.text.endsWith(contractAdditions('ptc').ptcNote))
|
||||
})
|
||||
|
||||
test('custom execution prompt overrides default block safely, normalizes line endings, and disables interpolation', async () => {
|
||||
assert.equal(normalizePromptText('a\r\nb\rc'), 'a\nb\nc')
|
||||
assert.equal(resolveExecutionModeBlock(''), EXECUTION_MODE_BLOCK)
|
||||
assert.equal(resolveExecutionModeBlock(' \r\n\t '), EXECUTION_MODE_BLOCK)
|
||||
|
||||
const custom = '## Custom rules\r\nKeep {{char}} and {{user}} literal.'
|
||||
assert.equal(resolveExecutionModeBlock(custom), '## Custom rules\nKeep {{char}} and {{user}} literal.')
|
||||
|
||||
const sections = await sectionsOf('standard')
|
||||
const fused = fuseSections(sections, 'standard', { customPrompt: custom })
|
||||
assert.equal(fused.issues, undefined)
|
||||
const block = fused.sections.find(s => s.name === BLOCK_SECTION)
|
||||
assert.ok(block)
|
||||
assert.equal(block.text, '## Custom rules\nKeep {{char}} and {{user}} literal.')
|
||||
assert.equal(Number.isFinite(block.order), true)
|
||||
assert.equal(block.interpolate, false)
|
||||
|
||||
const minimalCustom = fusedMinimalPrompt('', custom)
|
||||
assert.ok(minimalCustom.startsWith(ANCHORS.personaMinimal))
|
||||
assert.ok(minimalCustom.includes('## Custom rules\nKeep {{char}} and {{user}} literal.'))
|
||||
assert.ok(!minimalCustom.includes(EXECUTION_MODE_BLOCK))
|
||||
|
||||
const minimalStructured = fusedMinimalPrompt(ANCHORS.structuredOutputFull, custom)
|
||||
assert.ok(minimalStructured.includes('## Custom rules\nKeep {{char}} and {{user}} literal.'))
|
||||
assert.ok(minimalStructured.includes(ANCHORS.structuredOutputFull))
|
||||
})
|
||||
Reference in new issue
Block a user