Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演
JavaScript ai-artai-videochrome-extensionclaude-codecodexhiggsfieldjimenglocal-firstmcpmidjourneymoodboardprompt-engineeringprompt-libraryprompt-managerreference-libraryseedancevisual-bookmarksweb-clipper
// Native Messaging permits 1 MiB from the host to Chrome. Chunk size is a
// transport boundary, not an asset/file limit; base64 and envelope fit below it.
import { operationBudget } from './resource-policy.js';
export const AGENT_PROTOCOL_VERSION = 1;
export const AGENT_CHUNK_BYTES = 192 * 1024;
// Chrome -> native host permits 64 MiB, unlike the 1 MiB reverse direction.
// Reserve the envelope and serialization copies, plus parallel tool working
// sets. This controls a response chunk, never the size of the original file.
export function agentDownloadChunkBytes(budget) {
return Math.max(1, Math.floor(Math.min(64 * 1024 * 1024 / 4, operationBudget(budget).workingBytes / 32)));
}
export const AGENT_HOST = "com.promptdirector.connector";
export const AGENT_SETTINGS_KEY = "agentConnection";
export const AGENT_JOB_PREFIX = "agentTask:";
export const AGENT_UPLOAD_PREFIX = "agentUpload:";
export const agentPosterAssetId = assetId => `${assetId}:poster`;
export function agentError(code, message, details) {
return Object.assign(new Error(message), { code, ...(details ? { details } : {}) });
}
export function requireAgentId(value) {
if (typeof value !== "string" || !/^[a-zA-Z0-9_-]{1,128}$/u.test(value)) {
throw agentError("invalid_input", "请求编号只允许字母、数字、下划线和连字符,长度为 1–128。");
}
return value;
}
export function requireWebUrl(value) {
let url;
try { url = new URL(value); } catch { throw agentError("invalid_url", "请提供完整 HTTP 或 HTTPS 网页地址。"); }
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password) {
throw agentError("invalid_url", "网页地址仅支持 HTTP/HTTPS,且不能包含账号密码。");
}
return url.href;
}
export function requireInteger(value, { min = 0, max = Number.MAX_SAFE_INTEGER } = {}) {
if (!Number.isSafeInteger(value) || value < min || value > max) throw agentError("invalid_input", "读取位置或长度无效。");
return value;
}
export function bytesToBase64(bytes) {
if (typeof bytes.toBase64 === 'function') return bytes.toBase64();
// Older supported Chrome versions lack toBase64. Encode aligned groups so
// fallback strings stay small even when the download response is large.
const groupBytes = AGENT_CHUNK_BYTES / 8; // 24 KiB, divisible by three.
const parts = [];
for (let offset = 0; offset < bytes.length; offset += groupBytes) {
let text = '';
for (const byte of bytes.subarray(offset, offset + groupBytes)) text += String.fromCharCode(byte);
parts.push(btoa(text));
}
return parts.join('');
}
export function base64ToBytes(value) {
if (typeof value !== "string" || !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u.test(value)) {
throw agentError("invalid_input", "文件分块编码无效。");
}
const bytes = Uint8Array.from(atob(value), c => c.charCodeAt(0));
if (bytes.length > AGENT_CHUNK_BYTES) throw agentError("chunk_too_large", "请按连接器声明的分块长度传输,文件本身不需要缩小。");
return bytes;
}