/** * Finish-reason constants and helpers. * * LLM providers emit different keys (`finish_reason`, `stop_reason`, * `stopReason`, `finishReason`) and different values for the same concept. * This module is the single source of truth for detecting *truncation* — * i.e., the model hit its output budget and the response is incomplete. * * Used by: * - `Graph.ts` — sticky `lastFinishReason` across inner subgraph invokes, * so host's continuation retry can detect truncation that happens * inside a scoped-subgraph child node. * - Any future continuation / auto-retry logic added to agents. * * Kept as a small utils module (instead of inline in Graph.ts) so that * additional callers — e.g., structured output recovery, sub-agent * orchestrators — can reuse the exact same detection logic without drift. */ /** * Canonical set of finish-reason strings that mean "output was truncated". * * Covers: * - `max_tokens` — Anthropic direct API, Bedrock * - `length` — OpenAI/Azure * - `MAX_TOKENS` — VertexAI/Google (uppercased enum) */ export const TRUNCATION_FINISH_REASONS: ReadonlySet = new Set([ 'max_tokens', 'length', 'MAX_TOKENS', ]); /** * @returns true when the given finish/stop reason indicates the response * was cut short by the output token budget. */ export function isTruncationReason(reason: string | undefined | null): boolean { return reason != null && TRUNCATION_FINISH_REASONS.has(reason); }