UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

112 lines (111 loc) • 7.35 kB
/** * Adaptive `max_tokens` recovery for the ONE provider 400 that is self-healing (CC parity `Fjp` / * `Ijp`, ruled 2026-08-05). * * The failure: the input fits the model's context window, but `input + max_tokens` does not. Nothing * downstream rescues it — the compaction layer's own detectors match "prompt is too long", a * DIFFERENT message meaning the input ALONE overflows, so this 400 is classified `invalid_request`, * is not retried, and terminates the task even though lowering this one request's output cap would * have served it. The provider states every number needed to compute the fix in the error text. * * The recovery is deliberately NARROW: only a 400 whose body carries this exact sentence can trigger * it, and anything the parser cannot read as three integers leaves the original failure untouched. * Two fail-loud guards then bound it — a floor below which lowering the cap stops being useful, and * a no-progress guard so a provider that keeps reporting the same numbers cannot loop the engine. */ import { type OutputCapReport } from "../core/terminal-cause.js"; /** CC parity (`Ijp`): the smallest output cap worth retrying with. Below this the answer room is so * small that a re-send is not a recovery, so the original 400 is surfaced instead. */ export declare const FLOOR_OUTPUT_TOKENS = 3000; /** The three numbers the provider reported. */ export interface ContextOverflowReport { /** Tokens the provider measured in the request input. */ inputTokens: number; /** The output cap this request carried. */ maxTokens: number; /** The model's total context limit, as the provider counts it. */ contextLimit: number; } /** * Read the three numbers out of a provider 400 body, or `undefined` for every other failure — * including a 400 of any other shape, a body whose provenance is unestablished, a truncated body, and * numbers too large to be exact integers. A caller that gets `undefined` must surface the original * failure unchanged. */ export declare function parseContextOverflow(status: number, body: string): ContextOverflowReport | undefined; /** * The three numbers of a 400 refusing a request whose prompt plus output cap exceed the context window, stated in OpenAI's * wording ({@link REQUESTED_TOKENS_OVERFLOW_RE}) — under the provenance rule of {@link parseContextOverflow} (an ended 400 body, * `error.message` of its JSON envelope, the sentence first and stated once) — or `undefined`. The numbers must agree with each * other: exact integers, the total the sum of its two parts, and the total over the window; anything else is not this refusal. * Nothing in the brain re-sends on it: the prompt-too-long recovery chain reads it at its end (`AssistantMessage.refusedContextOverflow`). */ export declare function parseRequestedTokensOverflow(status: number, body: string): ContextOverflowReport | undefined; /** `v` as a {@link ContextOverflowReport}, copied, when its numbers describe an overflow as a provider can state one — exact * non-negative integers whose input does not fit beside the cap (and, given a stated `total`, whose two parts sum to it) — each * member read once; else `undefined`. */ export declare function contextOverflowReportOf(v: unknown, total?: number): ContextOverflowReport | undefined; /** The status sentence of a re-send at a lowered output cap — one sentence wherever the engine lowers. */ export declare function outputCapLoweredDetail(from: number, to: number): string; /** * What to do about a parsed overflow. `adjust` is the only outcome that re-sends; the other two are * refusals that the caller must report ALONGSIDE the original provider failure, so an operator sees * that a recovery was attempted and why it was declined rather than a bare 400. */ export type OutputCapAdjustment = /** `current` is the cap this adjustment lowers FROM — the smallest one known to be in effect, which * is what a disclosure must name (the provider's echoed number may not be what was sent). */ { outcome: "adjust"; maxOutputTokens: number; current: number; } /** The room left under the context limit is below {@link FLOOR_OUTPUT_TOKENS} — the input itself is * the problem, and no output cap makes this request servable. */ | { outcome: "below_floor"; available: number; } /** The recomputed cap is not STRICTLY below the cap already in effect — the previous adjustment * did not take effect, or the provider re-reported the same numbers, or the numbers would RAISE * the cap. Re-sending cannot help, and in the raising case would be actively worse. */ | { outcome: "no_progress"; available: number; current: number; }; /** * Compute the lowered output cap for a re-send, or the reason not to. * * `otherCapsInEffect` are the caps the CALLER knows about beyond the one the provider echoed — * typically the cap this engine already imposed on an earlier attempt, and the cap the request * actually carried on the wire. The SMALLEST of all of them bounds the adjustment, and a new cap * must be strictly below it. That single rule does three jobs: * - it terminates the loop (every accepted adjustment lowers the cap, and the floor bounds it); * - it makes an adapter that ignored an imposed cap fail loud after one round instead of looping; * - it keeps the recovery from ever RAISING a cap on the strength of a number the provider * reported but the request never carried. * * `report.maxTokens` is always one of the caps considered — the provider says the failing request * carried it, and a value it did not carry can only be smaller in the direction that matters. */ export declare function planOutputCapAdjustment(report: ContextOverflowReport, otherCapsInEffect?: readonly (number | undefined)[]): OutputCapAdjustment; /** * Whether a 400 body states that the request's output cap is above what the model allows (`undefined` = it does not), and — * when the sentence's two numbers are such a pair as `outputCapReportOf` admits — those numbers (`report`: the cap the request * carried as the provider echoes it, the most the model allows). The one reading the failed terminal names the part by * (`output_cap_too_large`); nothing re-sends on it. */ /** The provider's sentence for a request refused over its media blocks — "too many or oversized images" — matched as a whole * anywhere in the diagnostic (the reference reads it so: a containment test on the error message). */ export declare const MEDIA_ITEMS_REFUSED_SENTENCE = "Request rejected as unprocessable by the model (too many or oversized images, or unsupported parameters)"; /** * Whether a failed HTTP response states that the request was refused over its media blocks: status 400 and the diagnostic * of its JSON envelope (the provenance rule of {@link parseContextOverflow}) contains {@link MEDIA_ITEMS_REFUSED_SENTENCE}. * The one reading the failed turn is typed `media_items_refused` by; an in-band frame stating the same words is not read * here, so its classification and its fallback walk stay the failure table's. */ export declare function mediaItemsRefusalOf(status: number, body: string): boolean; export declare function outputCapRefusalOf(status: number, body: string): { readonly report?: OutputCapReport; } | undefined;