UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

51 lines (50 loc) • 1.92 kB
import { eventMaxBytes } from "../../prompt-assembly/event-registry.js"; import { SKILL_TOOL_NAME } from "./synthetic-tools.js"; const MENTION_TOKEN = /\/[a-zA-Z][a-zA-Z0-9.:_-]*/g; const MENTION_EDGE = /^[\s\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u3000-\u303f\uff01-\uff0f\uff1a-\uff20\uff3b-\uff40\uff5b-\uff65\u2014\u2026\u2018\u2019\u201c\u201d]$/u; function isEdge(cp) { return cp === undefined || MENTION_EDGE.test(cp); } function codePointBefore(text, at) { if (at <= 0) return undefined; return Array.from(text.slice(Math.max(0, at - 2), at)).pop(); } function codePointAt(text, at) { const cp = text.codePointAt(at); return cp === undefined ? undefined : String.fromCodePoint(cp); } export function skillMentionsIn(text, listed) { if (listed.size === 0 || !text.includes("/")) return []; const named = []; const seen = new Set(); for (const m of text.matchAll(MENTION_TOKEN)) { const start = m.index; let end = start + m[0].length; while (text[end - 1] === ".") end--; if (!isEdge(codePointBefore(text, start)) || !isEdge(codePointAt(text, end))) continue; const name = text.slice(start + 1, end); if (!listed.has(name) || seen.has(name)) continue; seen.add(name); named.push(name); } return named; } export function renderSkillMentionFrame(names, say) { const ceiling = eventMaxBytes("skill_mention"); const lines = []; let bytes = 0; for (const name of names) { const line = say("core/event.skill-mention", { name, tool: SKILL_TOOL_NAME }); const cost = Buffer.byteLength(line, "utf8") + (lines.length > 0 ? 1 : 0); if (bytes + cost > ceiling) continue; lines.push(line); bytes += cost; } return lines.length > 0 ? lines.join("\n") : undefined; }