UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

56 lines (55 loc) • 4.43 kB
import { Type } from "typebox"; import { callerToolNameProblem } from "../core/caller-tool-name.js"; export const MEMORY_WRITE_TOOL_NAME = "memory_write"; const DESCRIPTION = "Save one fact to persistent memory as a new entry. Each entry holds one fact: a short kebab-case `name` (it is also the entry's file name), a one-line `description` used to judge whether the entry is relevant when memory is looked up later, a `type` (`user` — who the user is; `feedback` — guidance the user gave on how to work, with the why; `project` — ongoing work or constraints not derivable from the code; `reference` — pointers to external resources), and the fact itself as `body` (for feedback/project, follow it with **Why:** and **How to apply:** lines).\n\n" + "This tool only creates: a name that already exists is refused, and nothing is overwritten or removed. Before saving, check that no existing entry already covers the fact. The entry is checked when it is written (secrets, oversized text and framing markup are refused, and nothing is written); it joins long-term memory when this session's memory is harvested at the end of the task, which judges it again."; const PARAMETERS = Type.Object({ name: Type.String({ description: "The entry's short kebab-case name — also its file name: letters, digits, '.', '_' and '-' only, not starting with '.'." }), description: Type.String({ description: "One line: what the entry is about, used to judge relevance when memory is looked up later." }), type: Type.String({ description: "user | feedback | project | reference" }), body: Type.String({ description: "The fact (markdown)." }), evidence: Type.Optional(Type.Unknown({ description: "Optional. One line of plain text: what the fact rests on (e.g. what the user said, or where it was read)." })), confidence: Type.Optional(Type.Unknown({ description: "Optional. How sure you are of the fact: low | medium | high." })), lastConfirmedAt: Type.Optional(Type.Unknown({ description: "Optional. The date the fact was last confirmed to hold, as YYYY-MM-DD (an ISO time may follow: YYYY-MM-DDThh:mm:ssZ)." })), }, { additionalProperties: false }); const NO_ENGINE_MEMORY = "this leg has no engine memory to write into"; function answer(toolName, entryName, outcome) { if (outcome.kind === "created") return `Saved memory entry "${entryName.slice(0, 80)}". It is judged again and committed to long-term memory when this session's memory is harvested at the end of the task.`; const why = outcome.kind === "unavailable" ? `${outcome.reason.slice(0, 500)}. Nothing was written.` : outcome.reason.slice(0, 2000); return { content: `${toolName.slice(0, 64)} refused: ${why.slice(0, 2600)}`, isError: true, details: { outcome: outcome.kind, ...(outcome.kind === "refused" ? { code: outcome.code.slice(0, 40) } : {}) } }; } export function createMemoryWriteTool(opts = {}) { if (opts === null || typeof opts !== "object") throw new TypeError("createMemoryWriteTool: options must be an object when given"); if (opts.name !== undefined) { const problem = typeof opts.name === "string" ? callerToolNameProblem(opts.name) : "it is not a string"; if (problem !== undefined) throw new TypeError(`createMemoryWriteTool: name: ${problem.slice(0, 400)}`); } const toolName = opts.name ?? MEMORY_WRITE_TOOL_NAME; return { name: toolName, description: DESCRIPTION, parameters: PARAMETERS, contentOrigin: "local", execute: async (rawArgs, ctx) => { const a = rawArgs; const stated = {}; for (const k of ["evidence", "confidence", "lastConfirmedAt"]) if (a[k] !== undefined) stated[k] = a[k]; const port = ctx?.memoryEntryCreate; if (port === undefined) return answer(toolName, String(a.name), { kind: "unavailable", reason: NO_ENGINE_MEMORY }); let outcome; try { outcome = await port({ name: a.name, description: a.description, type: a.type, body: a.body, ...stated }); } catch { outcome = { kind: "refused", code: "write_failed", reason: "the memory write port failed; whether the entry was written is unknown." }; } return answer(toolName, String(a.name), outcome); }, }; }