histoire
Version:
A fast story book powered by Vite
150 lines (149 loc) • 8.6 kB
JavaScript
import { Buffer } from 'node:buffer';
import { COMMENT_ATTACHMENT_LIMIT } from '@histoire/shared';
import { ATTACHMENT_BYTES, attachmentName } from './attachments.js';
import { COMMENT_DECISION_LIMIT, COMMENT_DECISION_PLAN_CHARS, COMMENT_PLAN_LIMIT, COMMENT_STEP_LIMIT } from './limits.js';
/** User fields stay bounded before file IO or agent prompt construction. */
function text(value, maximum, label) {
if (typeof value !== 'string' || !value.trim() || value.length > maximum || value.includes('\0'))
throw new Error(`Invalid comment ${label}`);
return value;
}
/** UUIDs are opaque labels and never participate in filesystem paths. */
export function commentId(value) {
if (typeof value !== 'string' || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value))
throw new Error('Invalid comment ID');
return value;
}
/** Stored attachment references keep generated paths and bounded display metadata only. */
export function validateCommentAttachments(value) {
if (value === undefined)
return undefined;
if (!Array.isArray(value) || value.length > COMMENT_ATTACHMENT_LIMIT)
throw new Error('Invalid comment attachments');
return value.map((item) => {
if (!item || typeof item.path !== 'string' || !attachmentName(item.path) || typeof item.mimeType !== 'string' || !/^[\w.+-]{1,60}\/[\w.+-]{1,60}$/.test(item.mimeType) || !Number.isSafeInteger(item.size) || item.size < 0 || item.size > ATTACHMENT_BYTES)
throw new Error('Invalid comment attachment');
return { path: item.path, name: text(item.name, 200, 'attachment'), mimeType: item.mimeType, size: item.size };
});
}
/** Session choices are opaque agent value IDs. */
export function validateCommentOptions(value) {
if (value === undefined)
return undefined;
if (!value || typeof value !== 'object' || Array.isArray(value))
throw new Error('Invalid comment options');
const input = value;
const result = {};
for (const field of ['model', 'effort', 'mode']) {
if (input[field] !== undefined)
result[field] = text(input[field], 200, 'option');
}
return result;
}
/**
* Canvas point of a pinned thread, in bounded logical frame coordinates.
* @param value Untrusted anchor.
*/
export function validateCommentAnchor(value) {
const anchor = value;
if (!anchor || typeof anchor !== 'object' || !Number.isFinite(anchor.x) || !Number.isFinite(anchor.y) || anchor.x < 0 || anchor.y < 0 || anchor.x > 1e6 || anchor.y > 1e6)
throw new Error('Invalid comment anchor');
return { x: anchor.x, y: anchor.y, ...(anchor.selector === undefined ? {} : { selector: text(anchor.selector, 4096, 'selector') }) };
}
/**
* JSON-safe prop snapshot, at most 16 KB once serialized.
* @param value Untrusted props.
*/
export function validateCommentProps(value) {
if (value === undefined)
return undefined;
if (!value || typeof value !== 'object' || Array.isArray(value))
throw new Error('Invalid comment props');
const json = JSON.stringify(value);
if (Buffer.byteLength(json) > 16 * 1024)
throw new Error('Comment props exceed 16 KB');
// Round-trip drops functions, undefined values and prototypes.
return JSON.parse(json);
}
/**
* Generated screenshot path; existence is checked against the screenshot inventory.
* @param value Untrusted project-relative path.
*/
export function validateCommentScreenshot(value) {
if (value === undefined)
return undefined;
if (typeof value !== 'string' || !/^\.histoire\/screenshots\/[\w-]+\.(?:png|webp)$/i.test(value))
throw new Error('Invalid comment screenshot');
return value;
}
/** Persisted agent step kinds and statuses mirror ACP tool calls. */
const STEP_KINDS = ['read', 'edit', 'delete', 'move', 'search', 'execute', 'think', 'fetch', 'other'];
const STEP_STATUSES = ['pending', 'in_progress', 'completed', 'failed'];
/** Optional non-negative line count. */
function lineCount(value) {
if (value === undefined)
return undefined;
if (!Number.isSafeInteger(value) || value < 0)
throw new Error('Invalid comment step');
return value;
}
/** Agent steps keep only display fields; arguments and outputs are never persisted. */
function validateCommentSteps(value) {
if (value === undefined)
return undefined;
if (!Array.isArray(value) || value.length > COMMENT_STEP_LIMIT)
throw new Error('Invalid comment steps');
return value.map((step) => {
if (!step || !STEP_KINDS.includes(step.kind) || !STEP_STATUSES.includes(step.status))
throw new Error('Invalid comment step');
const target = step.target === undefined ? undefined : text(step.target, 512, 'step target');
if (target && (target.startsWith('/') || target.split('/').includes('..')))
throw new Error('Invalid comment step');
const added = lineCount(step.added);
const removed = lineCount(step.removed);
return { id: text(step.id, 200, 'step'), kind: step.kind, title: text(step.title, 300, 'step title'), status: step.status, ...(target ? { target } : {}), ...(added === undefined ? {} : { added }), ...(removed === undefined ? {} : { removed }) };
});
}
/** Persisted plan entry statuses and priorities mirror ACP plans. */
const PLAN_STATUSES = ['pending', 'in_progress', 'completed'];
const PLAN_PRIORITIES = ['high', 'medium', 'low'];
/** Agent plans keep only bounded display entries. */
function validateCommentPlan(value) {
if (!Array.isArray(value) || value.length > COMMENT_PLAN_LIMIT)
throw new Error('Invalid comment plan');
return value.map((entry) => {
if (!entry || !PLAN_STATUSES.includes(entry.status) || (entry.priority !== undefined && !PLAN_PRIORITIES.includes(entry.priority)))
throw new Error('Invalid comment plan');
return { content: text(entry.content, 300, 'plan entry'), status: entry.status, ...(entry.priority ? { priority: entry.priority } : {}) };
});
}
/** Plan approval outcomes. */
const DECISION_OUTCOMES = ['approved', 'kept-planning'];
/** Plan decisions keep bounded markdown, possibly empty, and a known outcome. */
function validateCommentDecisions(value) {
if (!Array.isArray(value) || value.length > COMMENT_DECISION_LIMIT)
throw new Error('Invalid comment decisions');
return value.map((item) => {
if (!item || !DECISION_OUTCOMES.includes(item.outcome) || typeof item.plan !== 'string' || item.plan.length > COMMENT_DECISION_PLAN_CHARS || item.plan.includes('\0'))
throw new Error('Invalid comment decision');
return { plan: item.plan, outcome: item.outcome };
});
}
/** Preserve only project-relative agent-reported edits with finite line counts. */
export function validateCommentMessage(value) {
if (!value || typeof value !== 'object')
throw new Error('Invalid comment reply');
const input = value;
if (!['user', 'agent'].includes(input.author) || !Number.isFinite(Date.parse(input.at)))
throw new Error('Invalid comment reply');
const changes = input.changes === undefined
? undefined
: Array.isArray(input.changes)
? input.changes.slice(0, 32).map((change) => {
if (!change || typeof change.file !== 'string' || change.file.length > 512 || change.file.startsWith('/') || change.file.includes('\\') || change.file.split('/').includes('..') || !Number.isSafeInteger(change.added) || !Number.isSafeInteger(change.removed) || change.added < 0 || change.removed < 0)
throw new Error('Invalid comment change');
return { file: change.file, added: change.added, removed: change.removed };
})
: (() => { throw new Error('Invalid comment changes'); })();
return { author: input.author, body: text(input.body, 8192, 'reply'), at: input.at, agentId: input.agentId === undefined ? undefined : text(input.agentId, 128, 'agent'), changes, ...(input.failed === true && input.author === 'agent' ? { failed: true } : {}), ...(input.author === 'agent' && input.steps !== undefined ? { steps: validateCommentSteps(input.steps) } : {}), ...(input.author === 'agent' && input.plan !== undefined ? { plan: validateCommentPlan(input.plan) } : {}), ...(input.author === 'agent' && input.decisions !== undefined ? { decisions: validateCommentDecisions(input.decisions) } : {}), ...(input.author === 'user' && input.attachments !== undefined ? { attachments: validateCommentAttachments(input.attachments) } : {}) };
}