@sema-agent/core
Version:
Stateless, task-oriented AI agent core
60 lines (59 loc) • 3.7 kB
JavaScript
import { formatLocalDate } from "../../prompts/default.js";
import { valveLeaves } from "../../agents/tool-filter.js";
import { SKILL_TOOL_NAME } from "./synthetic-tools.js";
import { renderAgentListingDelta } from "./turn-attachments.js";
export function prepareListings(input) {
const { spec, onceLedger, sessionId, toolFaceSnapshot, activeTools, deferred, resume, skillsListing, listingBudget, listingRideRef, renderWithDate, envFacts, tzValid, userTz, dateAnchor } = input;
const rosterBearingSpec = (spec.tools ?? []).find((t) => t.agentListing !== undefined && t.agentListing.length > 0);
const leaves = valveLeaves(spec, toolFaceSnapshot.selector);
const rosterFaceDeferred = rosterBearingSpec !== undefined &&
leaves(rosterBearingSpec) &&
(toolFaceSnapshot.defer?.includes(rosterBearingSpec.name) ?? false) &&
deferred.has(rosterBearingSpec.name) &&
!activeTools.has(rosterBearingSpec.name);
const agentListingSpec = rosterBearingSpec !== undefined &&
!rosterFaceDeferred &&
leaves(rosterBearingSpec)
? rosterBearingSpec
: undefined;
const agentListing = agentListingSpec
? {
entries: agentListingSpec.agentListing,
toolName: agentListingSpec.name,
budget: listingBudget,
seedAnnounced: resume !== undefined || spec.sessionId !== undefined,
...(agentListingSpec.agentModels !== undefined ? { models: agentListingSpec.agentModels } : {}),
}
:
!rosterFaceDeferred && ((resume?.seed.announcedListings?.agents?.length ?? 0) > 0 || spec.sessionId !== undefined)
?
{
entries: [],
toolName: (spec.tools ?? []).find((t) => t.agentListing !== undefined)?.name ?? "Agent",
budget: listingBudget,
seedAnnounced: true,
}
: undefined;
if (spec.attachments?.agentListing === false && agentListing !== undefined && agentListing.entries.length > 0) {
onceLedger.onError(new Error(`attachments.agentListing is explicitly false but the "${agentListing.toolName}" delegation tool mounts a ${agentListing.entries.length}-type roster — the model will never see the agent-type listing its tool description points to.`), { phase: "config", sessionId });
}
if (spec.attachments?.skillsListing === false && skillsListing !== undefined && skillsListing.entries.length > 0) {
onceLedger.onError(new Error(`attachments.skillsListing is explicitly false but ${skillsListing.entries.length} skill(s) are mounted — the model will never see the skills listing the ${SKILL_TOOL_NAME} tool description points to.`), { phase: "config", sessionId });
}
const announcedListingsRef = onceLedger.arms();
if (rosterFaceDeferred && rosterBearingSpec !== undefined) {
const rideEntries = rosterBearingSpec.agentListing;
const rideModels = rosterBearingSpec.agentModels;
listingRideRef.current = (newly) => {
if (!newly.includes(rosterBearingSpec.name))
return undefined;
if ((announcedListingsRef.agents?.length ?? 0) > 0)
return undefined;
return renderAgentListingDelta({ announcedAgentTypes: undefined }, rideEntries, rosterBearingSpec.name, rideModels, listingBudget);
};
}
const dateChange = renderWithDate
? { legDate: envFacts.date, today: () => formatLocalDate(new Date(), tzValid ? userTz : undefined), anchor: dateAnchor }
: undefined;
return { agentListing, announcedListingsRef, dateChange };
}