84 lines
4.4 KiB
JavaScript
84 lines
4.4 KiB
JavaScript
import {openXiaoyiSkills} from '../src/xiaoyi-release.mjs';
|
|
import {xiaoyiFixture} from './xiaoyi-fixture.mjs';
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs/promises';
|
|
import {tmpdir} from 'node:os';
|
|
import path from 'node:path';
|
|
import {slimProductionContext} from '../src/image-context-pruning.mjs';
|
|
|
|
test('同屏同指纹的旧图替换后,增加读取也保留原摘要', () => {
|
|
const screen = (toolCallId, screenFingerprint) => ({
|
|
role:'toolResult', toolName:'read_output_screen', toolCallId, timestamp:0,
|
|
details:{page:1,screenFingerprint},content:[{type:'image',data:'test'}],
|
|
});
|
|
const results = new Map();
|
|
const messages = [screen('a','fingerprint'),screen('b','fingerprint')];
|
|
const [first, newest] = slimProductionContext(messages, results);
|
|
assert.match(first.content[0].text, /第 1 屏.*fingerprint.*1970-01-01.*同屏同指纹/);
|
|
assert.equal(newest.content[0].type,'image');
|
|
assert.equal(messages[0].content[0].type,'image');
|
|
assert.equal(slimProductionContext([...messages,screen('c','new')],results)[0].content[0],first.content[0]);
|
|
});
|
|
|
|
test('保留最近三个超长脚本回执,历史摘要稳定且指向原回执', () => {
|
|
const script = toolCallId => ({
|
|
role:'toolResult',toolName:'codemode',toolCallId,content:[{type:'text',text:'a'.repeat(2100)}],
|
|
});
|
|
const results = new Map();
|
|
const messages = ['a','b','c','d'].map(script);
|
|
const [old,...recent] = slimProductionContext(messages,results);
|
|
assert.match(old.content[0].text,/编号 a.*memory_search/);
|
|
assert.deepEqual(recent,messages.slice(1));
|
|
assert.equal(slimProductionContext([...messages,script('e')],results)[0].content,old.content);
|
|
});
|
|
|
|
test('long production turns trim old receipts and retain user canvas changes and corrections',()=>{
|
|
const change={type:'text',text:JSON.stringify({userCanvasChanges:{slotId:'manual',text:'人工改图'}})};
|
|
const steer={role:'user',content:[{type:'text',text:'第二屏保留用户选图'}]};
|
|
const messages=[steer];
|
|
for(let index=0;index<30;index++){
|
|
const details={generation:[{slotId:'hero',candidateId:'candidate-'+index,requestId:'paid-'+index,phase:'unknown',
|
|
extra:'x'.repeat(10_000)}]};
|
|
messages.push({role:'toolResult',toolName:'await_image_slots',toolCallId:'wait-'+index,details,
|
|
content:[{type:'text',text:JSON.stringify(details)},change]},
|
|
{role:'toolResult',toolName:'codemode',toolCallId:'script-'+index,content:[{type:'text',text:'x'.repeat(10_000)},change]});
|
|
}
|
|
const projected=slimProductionContext(messages);
|
|
const size=rows=>Buffer.byteLength(JSON.stringify(rows.map(({content})=>content)));
|
|
assert.ok(size(messages)>500_000);
|
|
assert.ok(size(projected)<65_000);
|
|
assert.equal(projected[0],steer);
|
|
for(const row of projected.slice(1))assert.deepEqual(row.content.at(-1),change);
|
|
assert.match(projected[1].content[0].text,/paid-0.*wait-0.*memory_search/);
|
|
assert.match(projected[2].content[0].text,/script-0.*memory_search/);
|
|
assert.equal(projected.at(-1),messages.at(-1));
|
|
});
|
|
|
|
test('离线重开保留相同的锁定入口与所有可按需读取的正文', async t => {
|
|
const dir = await fs.mkdtemp(path.join(tmpdir(),'xiaoyi-guide-'));
|
|
t.after(() => fs.rm(dir,{recursive:true,force:true}));
|
|
const entry = '规则/SKILL.md';
|
|
const supplement = '规则/模板补充.md';
|
|
const supplementPath = '共用/小壹补充.md';
|
|
const sharedPath = '共用/规则.md';
|
|
const guides = {
|
|
[entry]:'---\nname: guide\ndescription: Demo guide.\n---\n制作指引正文',
|
|
[supplement]:'模板补充首段\n模板补充末段',
|
|
[supplementPath]:'小壹补充首段\n小壹补充末段',
|
|
[sharedPath]:'共用规则首段\n共用规则末段',
|
|
'共用/SKILL.md':'共用入口首段\n共用入口末段',
|
|
};
|
|
const fetch = xiaoyiFixture('demo','1',entry,{...guides,'附录.md':'按需读取的附录'},
|
|
{supplement,overrides:[{sharedPath,supplementPath}]});
|
|
const first = await openXiaoyiSkills({dir},'demo',undefined,undefined,fetch);
|
|
const second = await openXiaoyiSkills({dir},'demo',undefined,undefined,
|
|
()=>assert.fail('locked prefixes must not use network'));
|
|
assert.deepEqual(first.execution,second.execution);
|
|
for (const [file,body] of Object.entries(guides)) {
|
|
assert.equal(second.memoryFiles.get(file).toString('utf8'),body);
|
|
await assert.rejects(fs.access(path.join(second.filesRoot,file)),{code:'ENOENT'});
|
|
}
|
|
assert.ok(await first.resolveFile(path.join(first.filesRoot,'附录.md')));
|
|
});
|