UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

50 lines (49 loc) • 3.26 kB
import { MAILBOX_TOMBSTONED_RECIPIENT_CODE } from "../core/mailbox-store.js"; import { peerSessionBoxHandle } from "./peer-directory.js"; import { isPeerReleaseCause, peerIdleNoticeLabel, renderCrossSessionDeliveryNotice, renderCrossSessionDroppedNotice, renderCrossSessionIdleNotice } from "./peer-notices.js"; function noticeMeta(route, to, kind, notice) { const modeClass = route.self.modeClass?.(); return { fromSession: route.self.sessionId, ...(route.self.occupant !== undefined ? { fromOccupant: route.self.occupant } : {}), ...(modeClass === "bypass" || modeClass === "prompting" ? { fromMode: modeClass } : {}), kind, notice, ...(to.occupant !== undefined ? { occupant: to.occupant } : {}), }; } async function appendNotice(route, to, content, peerMeta, classification) { try { await route.mailbox.append(route.scope, peerSessionBoxHandle(to.sessionId), { from: route.self.name ?? peerSessionBoxHandle(route.self.sessionId), content, sentAt: (route.now ?? Date.now)(), hopChain: [], peerMeta, }); return true; } catch (e) { if (e?.code !== MAILBOX_TOMBSTONED_RECIPIENT_CODE) { route.onError?.(e, { phase: "degraded", sessionId: route.self.sessionId, classification }); } return false; } } export function routePeerDeliveryReceipt(route, to, state, refSeq, detail) { const recipient = route.self.name ?? peerSessionBoxHandle(route.self.sessionId); const said = state === "refused" && detail !== undefined ? peerIdleNoticeLabel(detail) : state === "delivered" && isPeerReleaseCause(detail) ? detail : undefined; const notice = { state, refSeq, recipient, ...(said !== undefined ? { detail: said } : {}) }; return appendNotice(route, to, renderCrossSessionDeliveryNotice(state, 1, [recipient], said), noticeMeta(route, to, "delivery_notice", notice), "peer-receipt-route"); } export function routePeerDroppedReceipt(route, to, refSeq, dropReason) { const recipient = route.self.name ?? peerSessionBoxHandle(route.self.sessionId); const notice = { state: "dropped", refSeq, recipient, ...(dropReason !== undefined ? { dropReason } : {}) }; return appendNotice(route, to, renderCrossSessionDroppedNotice(1, [recipient], [dropReason]), noticeMeta(route, to, "delivery_notice", notice), "peer-receipt-route"); } export function routePeerIdleNotice(route, to, notice) { const detail = notice.detail !== undefined ? peerIdleNoticeLabel(notice.detail) : undefined; const label = notice.label ?? peerIdleNoticeLabel(route.self.name ?? peerSessionBoxHandle(route.self.sessionId)) ?? peerSessionBoxHandle(route.self.sessionId); const meta = { state: notice.kind, ...(notice.finishedAt !== undefined ? { finishedAt: notice.finishedAt } : {}), ...(detail !== undefined ? { detail } : {}), ...(notice.kind === "expired" ? { target: label } : {}) }; return appendNotice(route, to, renderCrossSessionIdleNotice({ kind: notice.kind, label, ...(notice.finishedAt !== undefined ? { finishedAt: notice.finishedAt } : {}), ...(detail !== undefined ? { detail } : {}) }), noticeMeta(route, to, "idle_notice", meta), "peer-idle-route"); }