Files
Agent-Workbench/src/core/context/input-attachments.mjs
T

143 lines
11 KiB
JavaScript

import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import { randomUUID } from 'node:crypto';
import { PNG } from 'pngjs';
import jpeg from 'jpeg-js';
export const IMAGE_LIMITS = { bytes: 2 * 1024 * 1024, pixels: 16 * 1024 * 1024, dimension: 8192, sentDimension: 1024, sentBytes: 768 * 1024, perTask: 1, drafts: 8 };
const reject = (message, status = 400) => Object.assign(new Error(message), { status });
function dimensions(bytes, type) {
if (type === 'image/png' && bytes.length >= 33 && bytes.subarray(0, 8).equals(Buffer.from([137,80,78,71,13,10,26,10]))
&& bytes.readUInt32BE(8) === 13 && bytes.toString('ascii', 12, 16) === 'IHDR') {
let ended = false;
for (let offset = 8; offset < bytes.length;) {
if (offset + 12 > bytes.length) throw reject('PNG区块不完整。');
const length = bytes.readUInt32BE(offset), name = bytes.toString('ascii', offset + 4, offset + 8);
if (offset + length + 12 > bytes.length || ['acTL','fcTL','fdAT'].includes(name)) throw reject('只支持完整的静态PNG图片。');
offset += length + 12;
if (name === 'IEND') { ended = length === 0 && offset === bytes.length; break; }
}
if (!ended) throw reject('PNG结束区块或尾部内容无效。');
return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20), orientation: 1 };
}
if (type === 'image/jpeg' && bytes[0] === 255 && bytes[1] === 216 && bytes.at(-2) === 255 && bytes.at(-1) === 217) {
let size, orientation = 1;
for (let p = 2; p < bytes.length;) {
if (bytes[p++] !== 255) break;
while (bytes[p] === 255) p++;
const marker = bytes[p++];
if (marker === 217 && p === bytes.length && size) return { ...size, orientation };
if (p + 2 > bytes.length || marker === 216 || marker === 217) break;
const length = bytes.readUInt16BE(p);
if (length < 2 || p + length > bytes.length) break;
if ([192,193,194].includes(marker) && length >= 8) size = { width: bytes.readUInt16BE(p + 5), height: bytes.readUInt16BE(p + 3) };
if (marker === 225 && bytes.toString('ascii',p+2,p+8) === 'Exif\0\0') orientation = exifOrientation(bytes.subarray(p+8,p+length));
p += length;
if (marker === 218) {
// Scan data may contain escaped FF bytes and restart markers; the next
// real marker must still be parsed, including progressive scan headers.
while (p < bytes.length) {
if (bytes[p++] !== 255) continue;
const start = p - 1;
while (bytes[p] === 255) p++;
if (bytes[p] === 0 || bytes[p] >= 208 && bytes[p] <= 215) { p++; continue; }
p = start; break;
}
}
}
}
throw reject('图片内容与PNG/JPEG类型不符,或图片已损坏。');
}
function exifOrientation(bytes) {
try {
const little = bytes.toString('ascii',0,2) === 'II';
if (!little && bytes.toString('ascii',0,2) !== 'MM') throw new Error();
const u16 = offset => little ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset);
const u32 = offset => little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
if (u16(2) !== 42) throw new Error();
const offset = u32(4), count = u16(offset);
if (offset < 8 || offset + 2 + count * 12 + 4 > bytes.length) throw new Error();
for (let i=0;i<count;i++) {
const entry=offset+2+i*12;
if (u16(entry)!==274) continue;
if(u16(entry+2)!==3 || u32(entry+4)!==1) throw new Error();
const value=u16(entry+8); if(value<1 || value>8) throw new Error(); return value;
}
return 1;
} catch { throw reject('JPEG方向元数据已损坏,请重新导出图片。'); }
}
function sentImage(decoded, orientation, maximum) {
const rotated = orientation >= 5;
const w = rotated ? decoded.height : decoded.width, h = rotated ? decoded.width : decoded.height;
const ratio = Math.min(1,maximum/Math.max(w,h)), width=Math.max(1,Math.round(w*ratio)), height=Math.max(1,Math.round(h*ratio));
const output = new PNG({width,height});
for(let y=0;y<height;y++) for(let x=0;x<width;x++) {
const u=Math.min(w-1,Math.floor(x*w/width)),v=Math.min(h-1,Math.floor(y*h/height));
const [sx,sy] = orientation===2 ? [decoded.width-1-u,v] : orientation===3 ? [decoded.width-1-u,decoded.height-1-v]
: orientation===4 ? [u,decoded.height-1-v] : orientation===5 ? [v,u] : orientation===6 ? [v,decoded.height-1-u]
: orientation===7 ? [decoded.width-1-v,decoded.height-1-u] : orientation===8 ? [decoded.width-1-v,u] : [u,v];
output.data.set(decoded.data.subarray((sy*decoded.width+sx)*4,(sy*decoded.width+sx)*4+4),(y*width+x)*4);
}
return { bytes:PNG.sync.write(output),width,height };
}
export function validateImage(input, config = {}) {
if (!input || Object.keys(input).some(key => !['name','mediaType','base64'].includes(key)) || typeof input.name !== 'string' || !input.name.trim() || input.name.length > 160
|| /[\\/\x00-\x1f]/.test(input.name) || !['image/png','image/jpeg'].includes(input.mediaType) || typeof input.base64 !== 'string') throw reject('请选择PNG或JPEG图片,文件名不能包含路径。');
if (input.base64.length > Math.ceil(ceiling(config, IMAGE_LIMITS.bytes) / 3) * 4) throw reject('单张图片最多2MB,请先裁剪或压缩。',413);
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(input.base64)) throw reject('图片编码无效。');
const bytes = Buffer.from(input.base64,'base64');
if (bytes.length > ceiling(config, IMAGE_LIMITS.bytes)) throw reject('单张图片最多2MB,请先裁剪或压缩。',413);
if (!bytes.length || bytes.toString('base64') !== input.base64) throw reject('图片编码或大小无效。');
const size = dimensions(bytes,input.mediaType);
if (!size.width || !size.height || Math.max(size.width,size.height)>ceiling(config, IMAGE_LIMITS.dimension) || size.width*size.height>ceiling(config, IMAGE_LIMITS.pixels)) throw reject('图片尺寸过大,请裁剪后再添加。',413);
let decoded;
try { decoded = input.mediaType==='image/png' ? PNG.sync.read(bytes,{checkCRC:true}) : jpeg.decode(bytes,{useTArray:true,tolerantDecoding:false,maxResolutionInMP:ceiling(config,16.8),maxMemoryUsageInMB:ceiling(config,160)}); }
catch { throw reject('图片无法完整解码,请重新导出PNG/JPEG。'); }
if (decoded.width!==size.width || decoded.height!==size.height) throw reject('图片实际尺寸不一致。');
let maximum = unrestricted(config) ? Math.max(decoded.width, decoded.height) : IMAGE_LIMITS.sentDimension, sent = sentImage(decoded,size.orientation,maximum);
while(sent.bytes.length>ceiling(config, IMAGE_LIMITS.sentBytes) && maximum>64) { maximum=Math.floor(maximum*0.75);sent=sentImage(decoded,size.orientation,maximum); }
if(sent.bytes.length>ceiling(config, IMAGE_LIMITS.sentBytes))throw reject('图片发送副本超过大小限制,请先裁剪。',413);
return { bytes, sent:sent.bytes, width:size.width,height:size.height,orientation:size.orientation,sentWidth:sent.width,sentHeight:sent.height };
}
export class InputAttachments {
constructor(store, { busy=()=>false }={}) { this.store=store;this.busy=busy; }
list(session) { return [...this.store.core.input_attachment.values()].filter(row=>row.sessionId===session.id && ['draft','attached'].includes(row.status)).map(row=>structuredClone(row)); }
upload(session,input) {
if(this.busy(session.projectId))throw reject('请等待当前项目执行结束后再添加附件。',409);
if(this.list(session).filter(row=>row.status==='draft').length>=ceiling(this.store, IMAGE_LIMITS.drafts))throw reject('待发送图片已满,请先移除不用的图片。',409);
const value=validateImage(input, this.store.config),identity={id:session.id,projectId:session.projectId,taskId:null,runId:null};
const original=this.store.artifactStore.write(identity,null,value.bytes,{kind:'input-image-original',mediaType:input.mediaType});
const sent=this.store.artifactStore.write(identity,null,value.sent,{kind:'input-image-sent',mediaType:'image/png'});
this.store.core.artifact.set(original.id,original);this.store.core.artifact.set(sent.id,sent);
const record={id:randomUUID(),sessionId:session.id,projectId:session.projectId,taskId:null,runId:null,status:'draft',name:input.name.trim(),mediaType:input.mediaType,
originalArtifactId:original.id,sentArtifactId:sent.id,sha256:original.sha256,bytes:original.bytes,sentSha256:sent.sha256,sentBytes:sent.bytes,
width:value.width,height:value.height,orientation:value.orientation,sentWidth:value.sentWidth,sentHeight:value.sentHeight,createdAt:new Date().toISOString()};
this.store.putRecord('input_attachment',session,record,'input_attachment.uploaded');return structuredClone(record);
}
get(session,id) { return this.store.owned('input_attachment',id,session.id); }
remove(session,id) {
const row=this.get(session,id);
if(row.status!=='draft'||this.busy(session.projectId))throw reject('已发送附件保留在原任务中;只能移除尚未发送的图片。',409);
row.status='withdrawn';this.store.save(session,{type:'input_attachment.withdrawn'});return this.list(session);
}
validateSelection(session,ids) {
if(!Array.isArray(ids)||ids.length>ceiling(this.store, IMAGE_LIMITS.perTask)||new Set(ids).size!==ids.length)throw reject('每个任务当前支持一张图片。');
return ids.map(id=>{const row=this.get(session,id);if(row.status!=='draft')throw reject('图片已发送或移除,请重新选择。',409);this.read(session,id);this.read(session,id,{original:true});return row;});
}
read(session,id,{original=false}={}) {
const row=this.get(session,id);if(row.status==='withdrawn')throw reject('这张待发送图片已移除。',410);
const artifact=this.store.owned('artifact',original?row.originalArtifactId:row.sentArtifactId,session.id);
if(row.projectId!==session.projectId || artifact.taskId!==row.taskId||artifact.runId!==row.runId||artifact.sha256!==(original?row.sha256:row.sentSha256)
||artifact.kind!==(original?'input-image-original':'input-image-sent') || artifact.mediaType!==(original?row.mediaType:'image/png') || artifact.bytes!==(original?row.bytes:row.sentBytes))throw reject('附件身份已变化,请核对原记录。',409);
if(row.status==='attached'){
const task=this.store.owned('task',row.taskId,session.id),run=this.store.owned('run',row.runId,session.id);
if(run.taskId!==task.id||!task.attachmentIds?.includes(row.id))throw reject('附件不属于原Task/Run。',409);
}
return {row,artifact,bytes:this.store.artifactStore.readBytes(artifact)};
}
images(session,task) {
return (task.attachmentIds??[]).map(id=>{const {row,bytes}=this.read(session,id);this.read(session,id,{original:true});if(row.taskId!==task.id||row.status!=='attached')throw reject('附件不属于当前任务。',409);
return {id:row.id,sha256:row.sentSha256,width:row.sentWidth,height:row.sentHeight,dataUrl:'data:image/png;base64,'+bytes.toString('base64')};});
}
}