80 lines
4.0 KiB
JavaScript
80 lines
4.0 KiB
JavaScript
const PRUNABLE_IMAGE_TOOLS = new Set([
|
|
'read_output_screen', 'read_slot_images', 'canvas_template_image', 'canvas_candidate_image',
|
|
'video_inspect',
|
|
]);
|
|
|
|
export function pruneHistoricalToolImages(messages, { keepLatest = 1 } = {}) {
|
|
let remaining = keepLatest;
|
|
let pruned;
|
|
for (let index = messages.length - 1; index >= 0; index--) {
|
|
const message = messages[index];
|
|
if (message?.role !== 'toolResult' || !PRUNABLE_IMAGE_TOOLS.has(message.toolName) ||
|
|
!Array.isArray(message.content) || !message.content.some(block => block?.type === 'image')) continue;
|
|
if (remaining > 0) {
|
|
remaining--;
|
|
continue;
|
|
}
|
|
const content = message.content.filter(block => block?.type !== 'image');
|
|
const released = message.content.length - content.length;
|
|
content.push({type: 'text', text: `已释放 ${released} 张历史查看图片。如需复看实际像素,请重新调用 ${message.toolName} 读取。`});
|
|
pruned ??= messages.slice();
|
|
pruned[index] = {...message, content};
|
|
}
|
|
return pruned ?? messages;
|
|
}
|
|
|
|
export function createImageContextPruningExtension(options) {
|
|
return pi => {
|
|
const replacements = new Map();
|
|
pi.on('context', event => {
|
|
const production=typeof options?.production==='function' ? options.production(event.messages) : options?.production;
|
|
return {messages: production ? slimProductionContext(event.messages, replacements) :
|
|
pruneHistoricalToolImages(event.messages, options)};
|
|
});
|
|
};
|
|
}
|
|
|
|
export function slimProductionContext(messages, replacements = new Map()) {
|
|
let newestRead = true;
|
|
let recentScripts = 3;
|
|
let assistantTurns = 0;
|
|
const seen = new Set();
|
|
const generic = pruneHistoricalToolImages(messages);
|
|
return messages.slice().reverse().map((message, offset) => {
|
|
if (message.role === 'assistant') assistantTurns++;
|
|
if (message.role !== 'toolResult') return message;
|
|
const {toolName, toolCallId, details, timestamp} = message;
|
|
if (toolName === 'read_output_screen' && message.content.some(block => block.type === 'image')) {
|
|
const screens = (details?.screens ?? [details]).filter(screen => screen?.screenFingerprint && !screen.error);
|
|
if (!screens.length) return generic[messages.length - 1 - offset];
|
|
let index = 0;
|
|
const content = message.content.map((block, position) => {
|
|
if (block.type !== 'image') return block;
|
|
const screen = screens[index++];
|
|
if (!screen) return block;
|
|
const key = JSON.stringify([details.outputId, details.variantId, details.documentKey, screen.page, screen.screenFingerprint]);
|
|
const id = toolCallId + ':' + position;
|
|
const reason = seen.has(key) ? '已被同屏同指纹的更新读取取代' :
|
|
newestRead ? '已退出近期图片窗口,原图可重新读取' : '已有更新的整屏读取,原图可重新读取';
|
|
if ((!newestRead || assistantTurns >= 2 || seen.has(key)) && !replacements.has(id)) {
|
|
replacements.set(id, {type:'text',text:`第 ${screen.page + 1} 屏,指纹 ${screen.screenFingerprint},读取于 ${new Date(timestamp).toISOString()}。${reason}。`});
|
|
}
|
|
seen.add(key);
|
|
return replacements.get(id) ?? block;
|
|
});
|
|
newestRead = false;
|
|
return {...message, content};
|
|
}
|
|
if (toolName === 'codemode' && message.content.every(block => block.type === 'text') &&
|
|
message.content.reduce((length, block) => length + block.text.length, 0) > 2000) {
|
|
if (recentScripts-- <= 0 && !replacements.has(toolCallId)) {
|
|
const calls = message.nestedCalls?.calls ?? details?.calls ?? [];
|
|
const names = [...new Set(calls.map(call => call.name))].filter(Boolean).join('、');
|
|
replacements.set(toolCallId, [{type:'text',text:`历史 codemode 回执(${message.isError ? '失败' : '已返回'}${names ? ',调用:' + names : ''}),编号 ${toolCallId},可用 memory_search 查看全文。`}]);
|
|
}
|
|
if (replacements.has(toolCallId)) return {...message, content:replacements.get(toolCallId)};
|
|
}
|
|
return generic[messages.length - 1 - offset];
|
|
}).reverse();
|
|
}
|