UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

51 lines (50 loc) • 1.63 kB
export const ATTACHMENT_SOURCES = Object.freeze([ "todo_reminder", "task_reminder", "tool_search_usage_reminder", "changed_files", "output_style", "date_change", "workflow_size_guideline_change", "budget_usd", "total_tokens_reminder", "background_tasks", "tools_delta", "mcp_unavailable", "agent_listing", "skills_listing", "mcp_instructions", "mcp_dropped_tools", "workflow_continuation", "skill_mention", "silent_turn_reminder", "elapsed_time_reminder", ]); const ATTACHMENT_SOURCE_SET = new Set(ATTACHMENT_SOURCES); export function isAttachmentSource(v) { return ATTACHMENT_SOURCE_SET.has(v); } export const BUNDLE_BLOCK_SEPARATOR = "\n"; export function composeBundle(blocks) { return { text: blocks.map((b) => b.block).join(BUNDLE_BLOCK_SEPARATOR), attachments: blocks.map((b) => ({ source: b.source, chars: b.block.length })), }; } export function readBundledAttachments(raw, textLength) { if (!Array.isArray(raw) || raw.length === 0) return undefined; const list = raw; const out = []; let spanned = (list.length - 1) * BUNDLE_BLOCK_SEPARATOR.length; for (const item of list) { if (item === null || typeof item !== "object") return undefined; const e = item; if (!isAttachmentSource(e.source) || typeof e.chars !== "number" || !Number.isSafeInteger(e.chars) || e.chars < 0) return undefined; out.push({ source: e.source, chars: e.chars }); spanned += e.chars; } return spanned <= textLength ? out : undefined; }