@sema-agent/core
Version:
Stateless, task-oriented AI agent core
190 lines (189 loc) • 10.8 kB
JavaScript
import { eventDefaultOn } from "../../prompt-assembly/event-registry.js";
import { stripGitStatusUnits } from "./git-status-frame.js";
import { SKILLS_LISTING_PROBE_HEADER } from "./synthetic-tools.js";
import { AGENT_LISTING_REMOVED_HEADER, SKILLS_LISTING_DELTA_HEADER, SKILLS_LISTING_REMOVED_HEADER, agentListingDeltaHeader, agentListingInitialHeader, createAttachmentState, replayAnnouncedListing, replayAnnouncedModels } from "./turn-attachments.js";
import { wireOf } from "../tool-name-readings.js";
import { rosterToolName } from "../tool-roster.js";
export async function runAttachmentSeats(input) {
const { spec, prepared, resume, rs, runner, next } = input;
rs.counters.wroteThisRun = false;
rs.counters.finalVerifyInjections = 0;
rs.counters.groundingSignalPreR9 = false;
rs.counters.groundingSignalPostR9 = false;
rs.attach.attachmentsCfg = spec.attachments;
rs.attach.agentListingOn = rs.attach.attachmentsCfg?.agentListing !== false && eventDefaultOn("agent_listing");
rs.attach.skillsListingOn = rs.attach.attachmentsCfg?.skillsListing !== false && eventDefaultOn("skills_listing");
const listingsLive = (rs.attach.agentListingOn && prepared.agentListing !== undefined) || (rs.attach.skillsListingOn && prepared.skillsListing !== undefined);
const backgroundTasksLive = (rs.attach.attachmentsCfg?.backgroundTasks ?? eventDefaultOn("background_tasks")) === true;
rs.attach.attachState = rs.attach.attachmentsCfg !== undefined || listingsLive || backgroundTasksLive || prepared.outputStyleFrame !== undefined ? createAttachmentState() : undefined;
rs.attach.dateState = prepared.dateChange !== undefined ? { announcedDate: prepared.dateChange.anchor?.current?.announcedDate ?? prepared.dateChange.legDate } : undefined;
rs.attach.instrProbe = runner.deps.probeInstructionSources;
rs.attach.instrState =
rs.attach.instrProbe !== undefined && prepared.instructionSources !== undefined && prepared.instructionSources.length > 0
? { lastAnnouncedHash: new Map(prepared.instructionSources.map((s) => [s.path, s.contentHash])) }
: undefined;
rs.attach.sizeGuidelineState =
prepared.workflowSizeGuideline !== undefined
? { announcedGuideline: prepared.workflowSizeGuideline.legGuideline, current: prepared.workflowSizeGuideline.current }
: undefined;
rs.counters.cadenceTurns = 0;
rs.turn.lastTurnHadToolCalls = false;
if (rs.attach.attachState !== undefined && (rs.attach.attachmentsCfg?.backgroundTasks ?? eventDefaultOn("background_tasks")) === true) {
try {
const branch = await prepared.session.getBranch();
for (let i = branch.length - 1; i >= 0; i--) {
const e = branch[i];
if (e.type === "compaction") {
rs.attach.attachState.postCompactPending = true;
break;
}
if (e.type === "message" && e.message.role === "assistant")
break;
}
}
catch {
}
}
if (rs.attach.attachState !== undefined &&
((rs.attach.agentListingOn && prepared.agentListing?.seedAnnounced === true) ||
(rs.attach.skillsListingOn && prepared.skillsListing?.seedAnnounced === true))) {
const cpListings = resume?.cp.state.announcedListings;
let entryListings;
try {
entryListings = await prepared.session.getAnnouncedListing();
}
catch {
entryListings = undefined;
}
const textOf = (m) => {
const c = m.content;
if (typeof c === "string")
return c;
if (Array.isArray(c)) {
return c
.map((b) => (b !== null && typeof b === "object" && b.type === "text" ? String(b.text ?? "") : ""))
.join("\n");
}
return "";
};
let branchTexts = null;
const branchLoad = async () => {
if (branchTexts === null) {
const branch = await prepared.session.getBranch();
branchTexts = branch.flatMap((e) => {
if (e.type !== "message")
return [];
const m = e.message;
if (m.role !== "user")
return [];
const full = textOf(e.message);
if (m.engineMinted === true)
return [stripGitStatusUnits(full, prepared.reminderMark)];
if (Array.isArray(m.engineSegments) && m.engineSegments.length > 0) {
return m.engineSegments.map((s) => stripGitStatusUnits(full.slice(Math.max(0, s.start), Math.max(0, s.end)), prepared.reminderMark));
}
if (typeof m.enginePrefixChars === "number" && m.enginePrefixChars > 0)
return [stripGitStatusUnits(full.slice(0, m.enginePrefixChars), prepared.reminderMark)];
return [];
});
}
return branchTexts;
};
const branchReplay = async (headers) => {
try {
return replayAnnouncedListing(await branchLoad(), headers);
}
catch {
return undefined;
}
};
if (rs.attach.agentListingOn && prepared.agentListing?.seedAnnounced === true) {
const listing = prepared.agentListing;
if (cpListings?.agents === undefined && entryListings?.agents !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedAgentTypes = new Map(entryListings.agents.map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.agents = [...entryListings.agents];
if (entryListings.models !== undefined) {
rs.attach.attachState.announcedModels = [...entryListings.models];
prepared.announcedListingsRef.models = [...entryListings.models];
}
}
else if (cpListings?.agents !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedAgentTypes = new Map(cpListings.agents.map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.agents = [...cpListings.agents];
if (cpListings.models !== undefined) {
rs.attach.attachState.announcedModels = [...cpListings.models];
prepared.announcedListingsRef.models = [...cpListings.models];
}
}
else {
const replayed = await branchReplay({
initial: agentListingInitialHeader(listing.toolName),
delta: agentListingDeltaHeader(listing.toolName),
removed: AGENT_LISTING_REMOVED_HEADER,
});
if (replayed !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedAgentTypes = new Map([...replayed].map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.agents = [...replayed];
const replayedModels = replayAnnouncedModels(await branchLoad());
if (replayedModels !== undefined) {
rs.attach.attachState.announcedModels = [...replayedModels];
prepared.announcedListingsRef.models = [...replayedModels];
}
}
}
}
if (rs.attach.skillsListingOn && prepared.skillsListing?.seedAnnounced === true) {
const listing = prepared.skillsListing;
if (cpListings?.skills === undefined && entryListings?.skills !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedSkills = new Map(entryListings.skills.map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.skills = [...entryListings.skills];
}
else if (cpListings?.skills !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedSkills = new Map(cpListings.skills.map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.skills = [...cpListings.skills];
}
else {
const replayed = await branchReplay({
initial: SKILLS_LISTING_PROBE_HEADER,
delta: SKILLS_LISTING_DELTA_HEADER,
removed: SKILLS_LISTING_REMOVED_HEADER,
});
if (replayed !== undefined) {
const descOf = new Map(listing.entries.map((e) => [e.name, e.description]));
rs.attach.attachState.announcedSkills = new Map([...replayed].map((n) => [n, descOf.get(n) ?? ""]));
prepared.announcedListingsRef.skills = [...replayed];
}
}
}
}
if (spec.finalVerification === true && resume !== undefined) {
try {
const branch = await prepared.session.getBranch();
for (const e of branch) {
if (e.type !== "message")
continue;
const m = e.message;
if (m.role === "user") {
const text = typeof m.content === "string"
? m.content
: m.content.map((b) => (b !== null && typeof b === "object" && b.type === "text" ? String(b.text ?? "") : "")).join("\n");
const fvMarked = m.engineMinted === true && text.includes(`<system-reminder mark="${prepared.reminderMark}">[final verification]`);
if ((fvMarked || text.includes("<system-reminder>[final verification]")) && rs.counters.finalVerifyInjections < 2)
rs.counters.finalVerifyInjections += 1;
}
else if (m.role === "toolResult" && !rs.counters.wroteThisRun && (prepared.toolEffects.get(wireOf("verify.wroteThisRunSeed", { toolName: rosterToolName(prepared.toolRosterDeltas.current, m.toolName) })) ?? "write") !== "read") {
rs.counters.wroteThisRun = true;
}
}
}
catch {
}
}
rs.attach.attachmentsInjected = 0;
return await next({});
}