feat(runtime): default to passthrough and persist execution permissions
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { PNG } from 'pngjs';
|
||||
import jpeg from 'jpeg-js';
|
||||
@@ -78,23 +79,23 @@ function sentImage(decoded, orientation, maximum) {
|
||||
}
|
||||
return { bytes:PNG.sync.write(output),width,height };
|
||||
}
|
||||
export function validateImage(input) {
|
||||
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(IMAGE_LIMITS.bytes / 3) * 4) throw reject('单张图片最多2MB,请先裁剪或压缩。',413);
|
||||
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 > IMAGE_LIMITS.bytes) throw reject('单张图片最多2MB,请先裁剪或压缩。',413);
|
||||
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)>IMAGE_LIMITS.dimension || size.width*size.height>IMAGE_LIMITS.pixels) throw reject('图片尺寸过大,请裁剪后再添加。',413);
|
||||
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:16.8,maxMemoryUsageInMB:160}); }
|
||||
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 = IMAGE_LIMITS.sentDimension, sent = sentImage(decoded,size.orientation,maximum);
|
||||
while(sent.bytes.length>IMAGE_LIMITS.sentBytes && maximum>64) { maximum=Math.floor(maximum*0.75);sent=sentImage(decoded,size.orientation,maximum); }
|
||||
if(sent.bytes.length>IMAGE_LIMITS.sentBytes)throw reject('图片发送副本超过大小限制,请先裁剪。',413);
|
||||
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 };
|
||||
}
|
||||
|
||||
@@ -103,8 +104,8 @@ export class InputAttachments {
|
||||
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>=IMAGE_LIMITS.drafts)throw reject('待发送图片已满,请先移除不用的图片。',409);
|
||||
const value=validateImage(input),identity={id:session.id,projectId:session.projectId,taskId:null,runId:null};
|
||||
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);
|
||||
@@ -120,7 +121,7 @@ export class InputAttachments {
|
||||
row.status='withdrawn';this.store.save(session,{type:'input_attachment.withdrawn'});return this.list(session);
|
||||
}
|
||||
validateSelection(session,ids) {
|
||||
if(!Array.isArray(ids)||ids.length>IMAGE_LIMITS.perTask||new Set(ids).size!==ids.length)throw reject('每个任务当前支持一张图片。');
|
||||
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}={}) {
|
||||
|
||||
Reference in New Issue
Block a user