Files
app/tauri/src/runtime/artifacts/artifact-content-cache.ts
T

28 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Host 所有的易失 Artifact 内容缓存。
*
* 它只保存已经通过 ArtifactState 接纳的宿主字节;内容不写入 Conversation Store
* Agent 只能引用不透明 local_ref,不能直接填充或读取缓存。
*
* Host-owned volatile bytes for Artifact metadata already accepted by
* ArtifactState. This cache has no network, persistence, DOM or protocol
* dependency: an Agent can name an opaque ref but cannot populate or read it.
*/
export class ArtifactContentCache {
private readonly blobs = new Map<string, Blob>();
public put(localRef: string, content: Blob): boolean {
if (!opaqueRef(localRef) || content.size > 512 * 1024 * 1024) return false;
this.blobs.set(localRef, content);
return true;
}
public get(localRef: string): Blob | undefined { return this.blobs.get(localRef); }
public has(localRef: string | undefined): boolean { return Boolean(localRef && this.blobs.has(localRef)); }
public clear(): void { this.blobs.clear(); }
}
function opaqueRef(value: string): boolean {
return /^[A-Za-z0-9_.:-]{1,128}$/.test(value) && !/[\\/]/.test(value);
}