UNPKG

histoire

Version:

A fast story book powered by Vite

112 lines (111 loc) • 5.82 kB
/** Most fields one form may show. */ const FIELD_LIMIT = 32; /** Most options one choice field may show. */ const OPTION_LIMIT = 100; /** String formats the form knows how to collect. */ const FORMATS = new Set(['email', 'uri', 'date', 'date-time']); /** Loopback hosts allowed over plain http (local OAuth callbacks). */ const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']); /** Optional bounded redacted text. */ function label(value, limit, sanitize) { return typeof value === 'string' && value.trim() ? sanitize(value).slice(0, limit) : undefined; } /** Optional finite number. */ function finite(value) { return typeof value === 'number' && Number.isFinite(value) ? value : undefined; } /** Copies only defined properties so projected cards stay compact. */ function defined(value) { return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined)); } /** * Titled (`oneOf` / `anyOf`) or plain (`enum`) options; undefined when invalid. * @param titled Titled enum options. * @param plain Untitled enum values. * @param sanitize Redacts known secrets. */ function options(titled, plain, sanitize) { const source = Array.isArray(titled) ? titled.map((item) => ({ value: item?.const, label: item?.title, description: item?.description })) : Array.isArray(plain) ? plain.map(item => ({ value: item, label: item, description: undefined })) : undefined; if (!source?.length || source.length > OPTION_LIMIT || source.some(item => typeof item.value !== 'string' || item.value.length > 1000)) return undefined; return source.map(item => defined({ value: item.value, label: label(item.label, 200, sanitize) ?? item.value, description: label(item.description, 1000, sanitize) })); } /** * Projects one ACP property schema; undefined for types the form cannot collect. * @param key Property name. * @param schema Agent property schema. * @param required Whether the schema requires it. * @param sanitize Redacts known secrets. */ function field(key, schema, required, sanitize) { const input = schema; const base = { key, required, title: label(input.title, 200, sanitize), description: label(input.description, 1000, sanitize) }; switch (input.type) { case 'string': { if (input.oneOf || input.enum) { const choices = options(input.oneOf, input.enum, sanitize); return choices && defined({ ...base, type: 'choice', options: choices, default: typeof input.default === 'string' ? input.default : undefined }); } return defined({ ...base, type: 'text', format: FORMATS.has(input.format) ? input.format : undefined, minLength: finite(input.minLength), maxLength: finite(input.maxLength), pattern: typeof input.pattern === 'string' && input.pattern.length <= 500 ? input.pattern : undefined, default: typeof input.default === 'string' ? input.default : undefined }); } case 'number': case 'integer': return defined({ ...base, type: 'number', integer: input.type === 'integer', minimum: finite(input.minimum), maximum: finite(input.maximum), default: finite(input.default) }); case 'boolean': return defined({ ...base, type: 'boolean', default: typeof input.default === 'boolean' ? input.default : undefined }); case 'array': { const choices = options(input.items?.anyOf, input.items?.enum, sanitize); const initial = Array.isArray(input.default) && input.default.every((item) => typeof item === 'string') ? input.default : undefined; return choices && defined({ ...base, type: 'multi-choice', options: choices, minItems: finite(input.minItems), maxItems: finite(input.maxItems), default: initial }); } default: return undefined; } } /** * Validated link: https anywhere, plain http only on loopback, never altered by redaction. * @param value Agent URL. * @param sanitize Redacts known secrets. */ function safeUrl(value, sanitize) { if (typeof value !== 'string' || value.length > 4096 || sanitize(value) !== value) return undefined; try { const url = new URL(value); return url.protocol === 'https:' || (url.protocol === 'http:' && LOOPBACK.has(url.hostname)) ? url : undefined; } catch { return undefined; } } /** * Projects an ACP elicitation into safe card content. * Returns undefined when the request cannot be shown faithfully, so the agent gets a decline. * @param request Agent elicitation request. * @param sanitize Redacts known secrets. */ export function projectElicitation(request, sanitize) { const message = label(request.message, 4000, sanitize) ?? 'The agent needs your input'; if (request.mode === 'url') { const input = request; const url = safeUrl(input.url, sanitize); return url && typeof input.elicitationId === 'string' ? { mode: 'url', message, url: url.href, host: url.host, elicitationId: input.elicitationId } : undefined; } if (request.mode !== 'form') return undefined; const schema = request.requestedSchema; const entries = Object.entries(schema?.properties ?? {}); if (!entries.length || entries.length > FIELD_LIMIT) return undefined; const required = new Set(Array.isArray(schema.required) ? schema.required : []); const fields = []; for (const [key, property] of entries) { const projected = key && key.length <= 64 ? field(key, property, required.has(key), sanitize) : undefined; if (!projected) return undefined; fields.push(projected); } return defined({ mode: 'form', message, title: label(schema.title, 200, sanitize), description: label(schema.description, 1000, sanitize), fields }); }