UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

119 lines (118 loc) • 8.91 kB
/** * The selector: the one reading that picks, for a catalog message, the variant a run receives — and decides * whether an overlay's `when` applies to this run (every source class alike). Five dimensions, each with exactly one * writer, so no value ever needs a precedence order: * * - `traits` — the model's declared traits (`Model.promptTraits`) with the task's one switch on `lean_prompt` * (`TaskSpec.promptLean`), bundles expanded: {@link effectiveTraitsOf}. The engine reads no model id to choose text. * - `host` — `role` (which role base the run is assembled for; `TaskSpec.promptRole`, absent ⇒ `general`) and * `form` (`TaskSpec.promptForm`, which every engine-built child receives through the parent spec seat; absent ⇒ * `standard`). * - `facts` — the runtime facts prepare produces (one derived: `executionEnvironmentDeclared`). * - `binding` — the deployment's scenario name (`TaskSpec.promptBinding`, an open vocabulary; absent ⇒ none). * - `locale` — which rendering of the chosen variant the run reads (`TaskSpec.promptLocale`; absent ⇒ `canonical`, the * catalog's own text). It picks no variant: the variant is chosen first, its rendering after ({@link messageText}). * * A `when` is a conjunction: every listed trait present, every `noneOf` trait absent, every host key equal, every * fact equal, the binding inside its `anyOf` (or outside its `noneOf`). No disjunction across dimensions. */ import type { PromptManifestSelectors } from "../core/trace.js"; import { type CatalogMessage, type FactConjunction, type HostForm, type HostRole, type PromptCatalog, type PromptFactName, type PromptLocale, type PromptTraitName, type RenderedLocale, type TraitCondition, type Variant, type VariantWhen } from "./catalog/schema.js"; import type { PromptRuntimeFacts } from "./types.js"; /** The run's selector values, resolved once per assembly. */ export interface PromptSelectors { readonly traits: ReadonlySet<PromptTraitName>; readonly host: { readonly role: HostRole; readonly form: HostForm; }; readonly facts: Readonly<Record<PromptFactName, boolean>>; readonly binding?: string; readonly locale: PromptLocale; } /** * The run's effective traits — the one place a trait set is formed: the model's declared set (exact: nothing is added * to what it lists; absent ⇒ `["lean_prompt"]`), then the task's one switch (`promptLean`: `true` adds `lean_prompt`, * `false` removes it, absent leaves the model's answer), then each declared bundle's members. The task can move * nothing else: a model-family trait stays the model's. */ export declare function effectiveTraitsOf(declared: readonly PromptTraitName[] | undefined, lean: boolean | undefined): ReadonlySet<PromptTraitName>; /** * The model door's reading of `Model.promptTraits`: absent, or a list of trait names ({@link isPromptTraitName}) holding * at most one name of each {@link PROMPT_TRAIT_EXCLUSIVE_GROUPS} group (bundle members counted, named in declaration * order). Anything else is refused by name (`config.prompt_trait_unknown`), never read as the default — the model would * silently receive another prompt shape, or one of two texts by order. */ export declare function readModelPromptTraits(model: { readonly id?: unknown; readonly promptTraits?: unknown; }): readonly PromptTraitName[] | undefined; /** The predicate facts a condition may test; optional runtime facts read as false when absent. */ export declare function catalogFactsOf(facts: PromptRuntimeFacts): Readonly<Record<PromptFactName, boolean>>; /** * The task seat's two host values as its door judges them (`TaskSpec.promptRole` / `TaskSpec.promptForm`; an * engine-built child's spec carries its parent's form through the parent spec seat). The form is the run's; the role * word is the task's statement of its role base, the first source of the role-base decision (`roleBaseOf`, the * request-body sugar), which gives the run's host role. A value outside the closed set is refused by name * (`config.prompt_selector_invalid`), never read as the default. */ export declare function resolveHost(task: { readonly promptRole?: unknown; readonly promptForm?: unknown; }): { readonly role?: HostRole; readonly form?: HostForm; }; /** The task's switch on the lean shape as its one writer gave it (`TaskSpec.promptLean`): a boolean or absent; any other * value is refused by name (`config.prompt_selector_invalid`), never read as absent. */ export declare function resolvePromptLean(task: { readonly promptLean?: unknown; }): boolean | undefined; /** The rendering of the catalog as its one writer gave it (`TaskSpec.promptLocale`, `SideQuerySpec.promptLocale` — * `seat` names which in the refusal): absent (the catalog's own text), or a rendered locale — today `"zh-Hans"`. * `"canonical"` is the absence's name, not a value a caller writes, so it is refused like any other word * (`config.prompt_selector_invalid`), never read as absent. */ export declare function resolvePromptLocale(task: { readonly promptLocale?: unknown; }, seat?: string): RenderedLocale | undefined; /** A role-base seat's role word as its writer gave it — the one door every seat that may name a role base passes * (`TaskSpec.promptRole`, a role preset's `promptRole`, the delegation tool's `promptRole`; `seat` names where it was * written): absent, or a word of {@link HOST_ROLES}; anything else is refused by name (`config.prompt_selector_invalid`). */ export declare function readPromptRole(seat: string, value: unknown): HostRole | undefined; /** The run's selectors — their one mint: the facts from the runtime facts, the host from its two writers' values, the * binding as the task's door validated it, the effective traits ({@link effectiveTraitsOf}; absent ⇒ those of a * model that declares none, with no task switch) and the locale as its door gave it (absent ⇒ `canonical`). Prepare * mints once and every reader of the run — the manifest, the identity, the `prompt.compose` event and the composer — * reads that one object. */ export declare function selectorsOf(facts: PromptRuntimeFacts, host?: { readonly role?: HostRole; readonly form?: HostForm; }, binding?: string, traits?: ReadonlySet<PromptTraitName>, locale?: RenderedLocale): PromptSelectors; /** The selectors' plain form (traits sorted): what the manifest shows, the identity's `binding` element digests and the * `prompt.compose` event carries — one record. */ export declare function selectorRecordOf(s: PromptSelectors): PromptManifestSelectors; export declare function factsHold(conj: FactConjunction | undefined, facts: Readonly<Record<PromptFactName, boolean>>): boolean; /** Whether a trait condition holds over a trait set — every `allOf` present, no `noneOf` present. The one trait reading: * a catalog variant's, an overlay's and a tool description variant's condition all go through it. */ export declare function traitsHold(cond: TraitCondition | undefined, traits: ReadonlySet<PromptTraitName>): boolean; /** Whether a `when` holds under the run's selectors. */ export declare function whenHolds(when: VariantWhen | undefined, s: PromptSelectors): boolean; /** * A `when` in its normal form — the one spelling of what it asks: every list sorted and without repeats, a conjunct that * asks nothing (an empty `traits`, `host` or `facts`, an empty list) left out, and a condition left with no conjunct * (`{}`) is no condition at all (`undefined`). Two conditions that hold in the same runs by construction are equal as * values here, so the overlay door (`validateOverlay`) hands every reader — the artifact digest, the publish-side * conflict check — the same condition. Idempotent; it never changes which runs a condition holds for. */ export declare function canonicalWhen(when: VariantWhen | undefined): VariantWhen | undefined; /** The variant this run receives, or undefined when no variant's `when` holds (the message is absent). */ export declare function selectVariant(message: CatalogMessage, s: PromptSelectors): Variant | undefined; /** A message's text this run: the selector chooses the variant, the run's locale its rendering ({@link renderingOf}). * `undefined` ⇒ no variant's `when` holds, the message is absent. */ export declare function messageText(catalog: PromptCatalog, message: CatalogMessage, s: PromptSelectors): { readonly variant: string; readonly text: string; } | undefined; /** A chosen variant's text in a locale — as written (`canonical`) or that locale's rendering — read by {@link messageText} * and by the engine text reading. A missing rendering is a defect the catalog door refuses, so it throws, never falls back. */ export declare function renderingOf(catalog: PromptCatalog, messageId: string, variant: Variant, locale: PromptLocale): string;