UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

194 lines (193 loc) • 10.4 kB
import { EVENT_MESSAGES } from "./catalog/events.js"; const AMBIENT = "core/event.ambient-context"; const LISTING_MORE_REMOVED = "core/event.listing.more-removed"; const LIMIT_AXES = ["core/event.limit-approach.axis-tokens", "core/event.limit-approach.axis-cost", "core/event.limit-approach.axis-turns", "core/event.limit-approach.axis-walltime"]; const ROWS = [ { kind: "todo_reminder", defaultPolicy: "off", dedupe: "repeat", messages: ["core/event.todo-reminder", "core/event.todo-reminder.list"] }, { kind: "task_reminder", defaultPolicy: "off", dedupe: "repeat", messages: ["core/event.task-reminder", "core/event.task-reminder.list"] }, { kind: "tool_search_usage_reminder", defaultPolicy: "off", dedupe: "repeat", messages: ["core/event.tool-search-usage-reminder", "core/event.tool-search-usage-reminder.no-direct-call", "core/event.tool-search-usage-reminder.more"] }, { kind: "changed_files", defaultPolicy: "off", dedupe: "repeat", messages: ["core/event.changed-files"] }, { kind: "output_style", defaultPolicy: "always", dedupe: "repeat", messages: ["core/event.output-style", "core/event.output-style.default-reminder"] }, { kind: "date_change", defaultPolicy: "always", dedupe: "replace-by-key", maxBytes: 160, messages: ["core/event.date-change"] }, { kind: "workflow_size_guideline_change", defaultPolicy: "always", dedupe: "replace-by-key", maxBytes: 256, messages: ["core/event.workflow-size-guideline-change", "core/event.workflow-size-guideline-change.unrestricted"] }, { kind: "budget_usd", defaultPolicy: "off", dedupe: "repeat", messages: ["core/event.budget-usd"] }, { kind: "total_tokens_reminder", defaultPolicy: "on", dedupe: "repeat", messages: ["core/event.total-tokens", "core/event.total-tokens.infinite"] }, { kind: "silent_turn_reminder", defaultPolicy: "on", dedupe: "repeat", messages: ["core/event.silent-turn-reminder"] }, { kind: "elapsed_time_reminder", defaultPolicy: "on", dedupe: "repeat", messages: ["core/event.elapsed-time"] }, { kind: "background_tasks", defaultPolicy: "on", dedupe: "repeat", messages: ["core/event.background-tasks"] }, { kind: "tools_delta", defaultPolicy: "off", dedupe: "replace-by-key", messages: [ "core/event.tools-delta.added", "core/event.tools-delta.added-grouped", "core/event.tools-delta.added-static", "core/event.tools-delta.added-static-grouped", "core/event.tools-delta.exception-grouped", "core/event.tools-delta.exception", "core/event.tools-delta.readded-one", "core/event.tools-delta.readded", "core/event.tools-delta.readded-static", "core/event.tools-delta.readded-loaded", "core/event.tools-delta.removed", "core/event.tools-delta.removed-grouped", AMBIENT, ], }, { kind: "mcp_unavailable", defaultPolicy: "on", dedupe: "replace-by-key", messages: [ "core/event.mcp-unavailable.for-now", "core/event.mcp-unavailable.for-this-session", "core/event.mcp-unavailable.retried", "core/event.mcp-unavailable.connection-failure", "core/event.mcp-unavailable.more", "core/event.mcp-unavailable.needs-authorization", "core/event.mcp-unavailable.still-connecting", "core/event.mcp-unavailable.still-connecting-wait", ], }, { kind: "agent_listing", defaultPolicy: "on", dedupe: "replace-by-key", messages: [ "core/event.agent-listing", "core/event.agent-listing.added", "core/event.agent-listing.removed", "core/event.agent-listing.concurrency", "core/event.agent-listing.models", "core/event.agent-listing.models-none", "core/event.agent-listing.capped", "core/event.agent-listing.more", "core/event.agent-listing.tools", "core/event.agent-listing.all-tools", LISTING_MORE_REMOVED, AMBIENT, ], }, { kind: "skills_listing", defaultPolicy: "on", dedupe: "replace-by-key", messages: ["core/event.skills-listing", "core/event.skills-listing.guide", "core/event.skills-listing.capped", "core/event.skills-listing.more", "core/event.skills-listing.files", "core/event.skills-listing.files-more", "core/event.skills-listing.files-count", "core/event.skills-listing.files-count-one", "core/event.skills-listing.added", "core/event.skills-listing.removed", LISTING_MORE_REMOVED, AMBIENT], }, { kind: "mcp_instructions", defaultPolicy: "off", dedupe: "replace-by-key", messages: [AMBIENT] }, { kind: "mcp_dropped_tools", defaultPolicy: "off", dedupe: "replace-by-key", maxBytes: 4096, messages: ["core/event.mcp-dropped-tools", "core/event.mcp-dropped-tools.entry", AMBIENT] }, { kind: "git_status", defaultPolicy: "always", messages: [ "core/event.git-status", "core/event.git-status.unavailable", "core/event.git-status.non-repo", "core/event.git-status.current-branch", "core/event.git-status.main-branch", "core/event.git-status.git-user", "core/event.git-status.status", "core/event.git-status.status-clean", "core/event.git-status.status-truncated", "core/event.git-status.recent-commits", "core/event.git-status.dirty", "core/event.git-status.clean", ], }, { kind: "permission_posture", defaultPolicy: "always", messages: [ "core/event.permission-posture.auto", "core/event.permission-posture.auto-exit", "core/event.permission-posture.bypass", "core/event.permission-posture.bypass-exit", "core/event.permission-posture.plan", "core/event.permission-posture.plan-sparse", "core/event.permission-posture.plan-no-exit", "core/event.permission-posture.plan-no-exit-sparse", "core/event.permission-posture.plan-child", "core/event.permission-posture.plan-file", "core/event.permission-posture.plan-file-sparse", "core/event.permission-posture.plan-exit", ], }, { kind: "limit_approach_converge", defaultPolicy: "always", messages: ["core/event.limit-approach.converge", ...LIMIT_AXES] }, { kind: "limit_approach_deliver", defaultPolicy: "always", messages: ["core/event.limit-approach.deliver", ...LIMIT_AXES] }, { kind: "final_verification", defaultPolicy: "always", messages: ["core/event.final-verification", "core/event.final-verification.reentry"] }, { kind: "advisor_tool", defaultPolicy: "always", messages: ["core/event.advisor-tool", "core/event.advisor-tool.abbreviated", "core/event.advisor-tool.removed"] }, { kind: "language", defaultPolicy: "always", messages: ["core/event.language", "core/event.language.cleared"] }, { kind: "skill_mention", defaultPolicy: "always", dedupe: "replace-by-key", maxBytes: 2048, messages: ["core/event.skill-mention"] }, { kind: "orphaned_background_tasks", defaultPolicy: "always", messages: ["core/event.orphaned-background-tasks", "core/event.orphaned-background-tasks.line", "core/event.orphaned-background-tasks.no-description"] }, { kind: "workflow_continuation", defaultPolicy: "always", dedupe: "replace-by-key", maxBytes: 8192, messages: [ "core/event.workflow-continuation.started", "core/event.workflow-continuation.started-denied", "core/event.workflow-continuation.ask", "core/event.workflow-continuation.model-continues", "core/event.workflow-continuation.settled", "core/event.workflow-continuation.not-applied", "core/event.workflow-continuation.more", ], }, ]; export function eventRowBand(row) { return row.defaultPolicy === "always" ? "event-mechanism" : "event-copy"; } export function eventRegistryProblems(rows, eventMessageIds) { const problems = []; const bandOfMessage = new Map(); for (const row of rows) { if (row.messages.length === 0) problems.push(`event registry: "${row.kind}" names no message`); const band = eventRowBand(row); for (const id of row.messages) { if (!eventMessageIds.has(id)) problems.push(`event registry: "${row.kind}" names "${id}", which is not an event message`); const first = bandOfMessage.get(id); if (first === undefined) bandOfMessage.set(id, { band, kind: row.kind }); else if (first.band !== band) problems.push(`event registry: "${id}" is placed by "${first.kind}" (${first.band}) and by "${row.kind}" (${band}) — one message lands in one band`); } } for (const id of eventMessageIds) if (!bandOfMessage.has(id)) problems.push(`event registry: the event message "${id}" words no kind's frame`); return problems; } const EVENT_MESSAGE_IDS = new Set(EVENT_MESSAGES.map((m) => m.id)); { const problems = eventRegistryProblems(ROWS, EVENT_MESSAGE_IDS); if (problems.length > 0) throw new Error(problems.join("; ")); } export const EVENT_PROMPT_REGISTRY = new Map(ROWS.map((e) => [e.kind, Object.freeze({ ...e, messages: Object.freeze([...e.messages]) })])); const KINDS_OF_MESSAGE = (() => { const m = new Map(); for (const row of EVENT_PROMPT_REGISTRY.values()) for (const id of row.messages) m.set(id, [...(m.get(id) ?? []), row.kind]); return m; })(); export function eventKindsOf(messageId) { return KINDS_OF_MESSAGE.get(messageId) ?? []; } export function eventBandOf(messageId) { const kind = KINDS_OF_MESSAGE.get(messageId)?.[0]; return kind === undefined ? undefined : eventRowBand(EVENT_PROMPT_REGISTRY.get(kind)); } export function eventDefaultOn(kind) { return EVENT_PROMPT_REGISTRY.get(kind)?.defaultPolicy === "on"; } export function eventMaxBytes(kind) { const n = EVENT_PROMPT_REGISTRY.get(kind)?.maxBytes; if (n === undefined) throw new Error(`event registry: "${kind}" declares no frame ceiling`); return n; }