UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

77 lines (76 loc) • 3.39 kB
import { createHash } from "node:crypto"; import { fitsProviderEchoCharset, isProviderEchoShape } from "./provider-echo-shape.js"; export const TOOL_CALL_ID_FOLD_SEPARATOR = "~"; const FOLD_BASE_CHARS = 32; const FOLD_DIGEST_HEX = 32; export function mintFallbackToolCallId(prefix) { return `${prefix}${Math.random().toString(36).slice(2)}`; } export const TOOL_CALL_ID_FOLD_REASONS = Object.freeze(["charset", "length", "duplicate"]); export function toolCallIdFoldReason(raw) { const separated = raw.includes(TOOL_CALL_ID_FOLD_SEPARATOR); if (!separated && isProviderEchoShape(raw)) return undefined; return !separated && fitsProviderEchoCharset(raw) ? "length" : "charset"; } export function toolCallFoldReason(call) { const wireId = providerToolCallId(call); if (wireId === call.id) return undefined; return toolCallIdFoldReason(wireId) ?? (RENAMED_ENGINE_ID.test(call.id) ? "duplicate" : undefined); } const RENAMED_ENGINE_ID = new RegExp(`^[\\x21-\\x7D]{1,${FOLD_BASE_CHARS}}${TOOL_CALL_ID_FOLD_SEPARATOR}[1-9][0-9]*${TOOL_CALL_ID_FOLD_SEPARATOR}[0-9a-f]{${FOLD_DIGEST_HEX}}$`); export function responseToolCallIds() { return { handedOut: new Set(), arrivals: new Map() }; } export function admitToolCallId(raw, prefix, response) { if (typeof raw !== "string" || raw === "") { let id = mintFallbackToolCallId(prefix); while (response?.handedOut.has(id) === true) id = mintFallbackToolCallId(prefix); response?.handedOut.add(id); return { engineId: id, wireId: id }; } if (response === undefined) return { engineId: engineIdOf(raw), wireId: raw }; const arrival = (response.arrivals.get(raw) ?? 0) + 1; response.arrivals.set(raw, arrival); if (!response.handedOut.has(raw)) { response.handedOut.add(raw); return { engineId: engineIdOf(raw), wireId: raw }; } let wireId = `${prefix}${digestOf(`${arrival}\u0000${raw}`)}`; for (let attempt = 1; response.handedOut.has(wireId); attempt++) wireId = `${prefix}${digestOf(`${arrival}\u0000${raw}\u0000${attempt}`)}`; response.handedOut.add(wireId); return { engineId: `${foldBase(raw)}${TOOL_CALL_ID_FOLD_SEPARATOR}${arrival}${TOOL_CALL_ID_FOLD_SEPARATOR}${digestOf(wireId)}`, wireId }; } function engineIdOf(raw) { if (toolCallIdFoldReason(raw) === undefined) return raw; return `${foldBase(raw)}${TOOL_CALL_ID_FOLD_SEPARATOR}${digestOf(raw)}`; } function foldBase(raw) { return raw.slice(0, FOLD_BASE_CHARS).replace(/[^\x21-\x7D]/g, "-"); } function digestOf(value) { return createHash("sha256").update(Buffer.from(value, "utf16le")).digest("hex").slice(0, FOLD_DIGEST_HEX); } export function providerToolCallId(call) { return typeof call.wireId === "string" && call.wireId !== "" ? call.wireId : call.id; } export function providerResultIds(messages) { const byEngineId = new Map(); for (const m of messages) { if (m.role !== "assistant" || !Array.isArray(m.content)) continue; for (const c of m.content) { if (c?.type !== "toolCall" || typeof c.id !== "string") continue; const wire = providerToolCallId(c); if (wire !== c.id) byEngineId.set(c.id, wire); } } return (engineId) => byEngineId.get(engineId) ?? engineId; }