@sema-agent/core
Version:
Stateless, task-oriented AI agent core
109 lines (108 loc) • 4.86 kB
JavaScript
import { traitsHold } from "../../prompt-assembly/select.js";
import { ASK_USER_QUESTION_TOOL_NAME } from "../ask-question.js";
import { PRESENT_PLAN_TOOL_NAME } from "../present-plan-tool.js";
import { readBundledAttachments } from "../attachment-source.js";
import { legFirstInputOf, userRowSourceOf } from "../user-row-origin.js";
export const SILENT_TURN_REMINDER = Object.freeze({
TURNS: 5,
MAX_PER_STRETCH: 3,
SECONDS: 60,
});
const LANE_TRAITS = Object.freeze({
silentTurn: { allOf: ["silent_turn_reminder"] },
silentTurnTimed: { allOf: ["timed_silent_turn_reminder"] },
elapsedTime: { allOf: ["elapsed_time_reminder"] },
});
export function runTimeReminderLane(input) {
const { traits, childLeg, switches } = input;
const silentOn = switches.silentTurn && !childLeg && traitsHold(LANE_TRAITS.silentTurn, traits);
return Object.freeze({
silentTurn: !silentOn ? "off" : traitsHold(LANE_TRAITS.silentTurnTimed, traits) ? "timed" : "turns",
elapsedTime: switches.elapsedTime && traitsHold(LANE_TRAITS.elapsedTime, traits),
});
}
const SPEAKING_TOOLS = new Set([ASK_USER_QUESTION_TOOL_NAME, PRESENT_PLAN_TOOL_NAME]);
const EMPTY_TEXT_PLACEHOLDER = "(no content)";
const said = (v) => (typeof v === "string" ? v.trim() : "");
function speaks(row) {
return Array.isArray(row.content) && row.content.some((b) => (b?.type === "text" && said(b.text) !== "" && said(b.text) !== EMPTY_TEXT_PLACEHOLDER) ||
(b?.type === "thinking" && b.redacted !== true && said(b.thinking) !== "") ||
(b?.type === "toolCall" && SPEAKING_TOOLS.has(b.name)));
}
function personInput(row) {
const source = userRowSourceOf(row);
return source === undefined || source.origin === "human_input";
}
function bundledSources(row) {
const m = row;
if (m.attachments === undefined)
return undefined;
const text = typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((b) => (typeof b?.text === "string" ? b.text : "")).join("") : "";
return readBundledAttachments(m.attachments, text.length)?.map((a) => a.source);
}
export function readSilentStretch(rows, opening) {
let turns = 0;
let reminders = 0;
let since;
let earliest;
for (let i = rows.length - 1; i >= 0; i--) {
const row = rows[i];
const at = row.timestamp;
if (typeof at === "number" && Number.isFinite(at))
earliest = at;
const role = row.role;
if (role === "assistant") {
const assistant = row;
if (speaks(assistant)) {
since ??= assistant.timestamp;
return { turnsSinceLastReminder: turns, remindersInStretch: reminders, silentSince: since };
}
if (reminders === 0)
turns++;
continue;
}
if (role !== "user")
continue;
const bundled = bundledSources(row);
if (bundled?.includes("silent_turn_reminder") === true) {
reminders++;
since ??= row.timestamp;
continue;
}
if (bundled !== undefined)
continue;
const first = legFirstInputOf(row);
if (first === false || (first === true && opening?.byPerson === false && typeof at === "number" && at >= opening.at))
continue;
since ??= row.timestamp;
if (personInput(row))
break;
}
since ??= earliest;
return { turnsSinceLastReminder: turns, remindersInStretch: reminders, silentSince: since };
}
export function silentTurnReminderDue(stretch, arm, now) {
if (arm === "turns")
return stretch.remindersInStretch < SILENT_TURN_REMINDER.MAX_PER_STRETCH && stretch.turnsSinceLastReminder >= SILENT_TURN_REMINDER.TURNS;
if (stretch.silentSince === undefined || stretch.turnsSinceLastReminder < 1)
return false;
return Math.floor((now - stretch.silentSince) / 1000) >= SILENT_TURN_REMINDER.SECONDS;
}
export async function boundaryReminderInputs(input) {
const { lane, elapsedClock, opening, carried, rows } = input;
let silentTurn;
if (lane.silentTurn !== "off" && !carried("silent_turn_reminder")) {
try {
silentTurn = { stretch: readSilentStretch(await rows(), opening.current), now: Date.now() };
}
catch {
silentTurn = undefined;
}
}
const elapsedMs = lane.elapsedTime && !carried("elapsed_time_reminder") ? elapsedClock.elapsedMs() : undefined;
return { ...(silentTurn !== undefined ? { silentTurn } : {}), ...(elapsedMs !== undefined ? { elapsedMs } : {}) };
}
export function elapsedTimeSlots(ms) {
const total = Number.isFinite(ms) ? Math.max(0, Math.floor(ms / 1000)) : 0;
return { minutes: String(Math.floor(total / 60)), seconds: String(total % 60).padStart(2, "0") };
}