@sema-agent/core
Version:
Stateless, task-oriented AI agent core
117 lines (116 loc) • 4.95 kB
JavaScript
import { textContentHashOf } from "../core/canonical-json.js";
import { canonical } from "./epoch.js";
export const PREFIX_INPUT_CLASSIFICATION = {
provider: "cache-identity",
modelId: "cache-identity",
artifactDigest: "cache-identity",
layoutGroups: "cache-identity",
stableSystemText: "cache-identity",
toolWire: "cache-identity",
thinkingLevel: "cache-identity",
maxTokens: "cache-identity",
hasOutputSchema: "cache-identity",
cacheRetention: "cache-identity",
sessionId: "proven-irrelevant",
taskId: "proven-irrelevant",
turn: "volatile-no-reuse",
cacheRead: "volatile-no-reuse",
};
export const LOWERING_VERSION = "sema-lowering@1";
export function loweringRecordFor(api) {
if (api.startsWith("anthropic")) {
return {
version: LOWERING_VERSION,
form: "anthropic-blocks",
intentionalDivergences: [
"system rides top-level `system` blocks (cache_control on the layout boundary), not a system message",
"reasoning replays via provider-native thinking blocks",
],
};
}
if (api.startsWith("openai")) {
return {
version: LOWERING_VERSION,
form: "openai-merged",
intentionalDivergences: [
"multi-block system layouts merge into ONE system message (block boundaries erased — no per-block cache_control on this wire)",
"reasoning-content replay is provider-dialect dependent (loss: some dialects drop replayed reasoning)",
],
};
}
return { version: LOWERING_VERSION, form: "other", intentionalDivergences: ["unknown wire family — lowering fidelity unverified"] };
}
function identityOf(e) {
return textContentHashOf(canonical(e));
}
export function buildTurnPromptSnapshot(raw, api, promptHasher) {
const promptSha = promptHasher ?? textContentHashOf;
const lowering = loweringRecordFor(api);
let requestPolicy = {
thinkingLevel: raw.thinkingLevel,
maxTokens: raw.maxTokens ?? null,
hasOutputSchema: raw.hasOutputSchema,
...(raw.cacheRetention !== undefined ? { cacheRetention: raw.cacheRetention } : {}),
};
const toolFace = (t) => ({ name: t.name, description: t.description, parameters: t.parameters });
const elements = {
provider: raw.provider,
modelId: raw.modelId,
artifactDigest: raw.artifactDigest,
layoutDigest: textContentHashOf(canonical(raw.layoutGroups)),
stableSystemDigest: promptSha(raw.stableSystemText),
toolWireDigest: textContentHashOf(canonical(raw.toolWire.map(toolFace))),
requestPolicyDigest: textContentHashOf(canonical(requestPolicy)),
loweringVersion: `${lowering.version}/${lowering.form}/${api}`,
};
const bump = (cause) => {
const next = identityOf(elements);
if (next === snapshot.cacheIdentity)
return;
snapshot.cacheIdentity = next;
snapshot.onDeclared?.({ cause });
};
let mounted = elements.toolWireDigest;
const snapshot = {
elements,
cacheIdentity: "",
lowering,
get mountedToolWireDigest() {
return mounted;
},
refreshTools(toolWire, source) {
elements.toolWireDigest = textContentHashOf(canonical(toolWire.map(toolFace)));
if (source !== "view")
mounted = elements.toolWireDigest;
bump("tool_table");
},
refreshStableSystem(stableSystemText, artifactDigest) {
elements.stableSystemDigest = promptSha(stableSystemText);
if (artifactDigest !== undefined)
elements.artifactDigest = artifactDigest;
bump("compaction");
},
refreshRequestPolicy(p) {
const retention = p.cacheRetention === undefined ? requestPolicy.cacheRetention : (p.cacheRetention ?? undefined);
requestPolicy = {
thinkingLevel: p.thinkingLevel !== undefined ? p.thinkingLevel : requestPolicy.thinkingLevel,
maxTokens: p.maxTokens !== undefined ? p.maxTokens : requestPolicy.maxTokens,
hasOutputSchema: requestPolicy.hasOutputSchema,
...(retention !== undefined ? { cacheRetention: retention } : {}),
...((p.fastSpeed !== undefined ? p.fastSpeed : requestPolicy.fastSpeed === true) ? { fastSpeed: true } : {}),
};
elements.requestPolicyDigest = textContentHashOf(canonical(requestPolicy));
bump("request_policy");
},
refreshModel(m) {
elements.provider = m.provider;
elements.modelId = m.modelId;
bump("model_namespace");
},
declare(declaration) {
snapshot.onDeclared?.(declaration);
},
};
snapshot.cacheIdentity = identityOf(elements);
return snapshot;
}