UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

85 lines (84 loc) • 5.47 kB
import { createHash } from "node:crypto"; import { Type } from "typebox"; import { SKILL_CONTENT_MAX_CHARS } from "../core/runner/synthetic-tools.js"; export const SKILL_PROPOSAL_TOOL_NAME = "ProposeSkill"; const NAME_MAX = 64; const DESCRIPTION_MAX = 1024; const LIST_MAX = 64; const ENTRY_MAX = 512; const PROPOSAL_ID_CHARS = 32; const NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; const ruleList = (what) => Type.Optional(Type.Array(Type.String({ minLength: 1, maxLength: ENTRY_MAX }), { maxItems: LIST_MAX, description: `${what}, each spelled as a permission rule (\`Read\`, \`Bash(git:*)\`).` })); const PARAMETERS = Type.Object({ name: Type.String({ minLength: 1, maxLength: NAME_MAX, pattern: NAME_PATTERN.source, description: "The Skill's name: letters, digits, `.`, `_`, `-`." }), description: Type.String({ minLength: 1, maxLength: DESCRIPTION_MAX, description: "One line saying when the Skill applies." }), content: Type.String({ minLength: 1, maxLength: SKILL_CONTENT_MAX_CHARS, description: "The Skill's instructions, as they would be loaded." }), allowed_tools: ruleList("Tools the Skill would let run without asking once activated"), disallowed_tools: ruleList("Tools the Skill would refuse once activated"), }); async function exposureOf(ctx) { try { return (await ctx.contentExposure?.()) ?? "unknown"; } catch { return "unknown"; } } function proposalIdOf(sessionId, skill) { const canonical = JSON.stringify([sessionId, skill.name, skill.description, skill.content, skill.allowedTools ?? null, skill.disallowedTools ?? null]); return createHash("sha256").update(canonical).digest("hex").slice(0, PROPOSAL_ID_CHARS); } function readVerdict(raw) { if (raw === null || typeof raw !== "object") return undefined; const v = raw; if (typeof v.accepted !== "boolean" || typeof v.reason !== "string") return undefined; if (v.ref !== undefined && typeof v.ref !== "string") return undefined; return { accepted: v.accepted, reason: v.reason, ...(typeof v.ref === "string" ? { ref: v.ref } : {}) }; } const listOf = (list) => (list !== undefined && list.length > 0 ? [...list] : undefined); export function createSkillProposalTool(opts) { if (typeof opts?.propose !== "function") throw new TypeError("createSkillProposalTool: propose must be a function"); if (opts.allowToolGrants !== undefined && typeof opts.allowToolGrants !== "boolean") throw new TypeError("createSkillProposalTool: allowToolGrants must be a boolean when given"); const { propose } = opts; const grantsAllowed = opts.allowToolGrants === true; return { name: SKILL_PROPOSAL_TOOL_NAME, description: "Propose a new Skill for review: a reusable set of instructions for a kind of task you expect to meet again. " + "The proposal goes to the deployment's review; it is not active in this run and cannot be invoked here. " + (grantsAllowed ? "`allowed_tools` lists tools the Skill would let run without asking; each entry is shown to the reviewer." : "This deployment does not accept `allowed_tools` on a proposal; leave it out."), parameters: PARAMETERS, contentOrigin: "local", execute: async (rawArgs, ctx) => { const a = rawArgs; const allowedTools = listOf(a.allowed_tools); if (allowedTools !== undefined && !grantsAllowed) { return { content: "This deployment does not accept `allowed_tools` on a Skill proposal. Propose it again without `allowed_tools`; nothing was submitted.", details: { accepted: false }, isError: true }; } const disallowedTools = listOf(a.disallowed_tools); const skill = { name: a.name, description: a.description, content: a.content, ...(allowedTools !== undefined ? { allowedTools } : {}), ...(disallowedTools !== undefined ? { disallowedTools } : {}) }; const sessionId = ctx.sessionId ?? ""; const exposure = await exposureOf(ctx); const proposalId = proposalIdOf(sessionId, skill); const source = { sessionId, ...(ctx.taskId !== undefined ? { taskId: ctx.taskId } : {}), ...(ctx.principal !== undefined ? { principal: ctx.principal } : {}), exposure }; let verdict; try { verdict = readVerdict(await propose({ proposalId, skill, source }, { signal: ctx.signal ?? new AbortController().signal })); } catch { verdict = undefined; } if (verdict === undefined) { return { content: `The deployment's review seat gave no verdict on this Skill proposal (id ${proposalId.slice(0, PROPOSAL_ID_CHARS)}); whether it was stored is unknown.`, details: { proposalId, accepted: false, exposure, seatFault: true }, isError: true }; } const details = { proposalId, accepted: verdict.accepted, exposure }; if (!verdict.accepted) return { content: `The deployment's review did not accept this Skill proposal (id ${proposalId.slice(0, PROPOSAL_ID_CHARS)}). Its reason goes to the people running this deployment, not to this conversation.`, details, isError: true }; return { content: `This Skill proposal was received for review (id ${proposalId}). It is not active in this run; once approved it can be loaded in a later run.`, details }; }, }; }