import type { Action, ProviderResult } from "../types/components.js"; import type { AgentContext, ContextDefinition } from "../types/contexts.js"; import type { Memory } from "../types/memory.js"; import type { PromptSegment, ToolDefinition } from "../types/model.js"; import type { Content, JsonValue } from "../types/primitives.js"; /** * Neutral in-memory representation of a prompt-assembly context: messages, tools, * provider segments, and metadata collected before rendering into a model call. * Event-typed and role-tagged so producers (providers, actions) and the renderer * agree on a single intermediate shape independent of any model's wire format. */ type ContextObjectEventType = "message" | "memory" | "provider" | "tool" | "instruction" | "segment" | "metadata" | (string & {}); type ContextObjectRole = "system" | "user" | "assistant" | "tool" | (string & {}); export interface ContextObjectMessage { id?: string; role: ContextObjectRole; content: string | Content | JsonValue; name?: string; metadata?: Record; } export interface ContextObjectTool { id?: string; name: string; description?: string; parameters?: unknown; action?: Action; metadata?: Record; } export interface ContextObjectPromptSegment extends PromptSegment { /** Recorded source-message instant; presentation only, never effect authority. */ createdAt?: number; id?: string; label?: string; tokenCount?: number; metadata?: Record; } export interface ContextEventBase { id: string; type: ContextObjectEventType; createdAt?: number; source?: string; metadata?: Record; } export interface ContextMessageEvent extends ContextEventBase { type: "message"; message: ContextObjectMessage; } export interface ContextMemoryEvent extends ContextEventBase { type: "memory"; memory: Memory; } export interface ContextProviderEvent extends ContextEventBase { /** Runtime-only dependency scope: other operations must restore this source first. */ [OWNED_CONTEXT_SOURCE_SCOPE]?: { readonly actionNames: readonly string[]; /** Checks current runtime provenance against this exact canonical turn. */ canDefer(context: ContextObject, source: ContextProviderEvent): boolean; }; /** Serialized originals require fresh runtime provenance before deferral. */ discoveryRequiresRuntimeBinding?: true; reviewableSources?: ProviderResult["reviewableSources"]; type: "provider"; name: string; text?: string; /** Provider-owned index; the complete text stays in the original context. */ discoveryText?: string; /** Complete body requires fresh composition; never a fully loaded source. */ referenceBodyDeferred?: true; values?: Record; data?: Record; /** * Mirrors the originating `Provider.cacheStable`. Read by * `context-renderer.ts`'s `renderEvent` to mark the rendered prompt segment * `stable`, so `buildStageChatMessages` (planner/evaluator stages) routes a * genuinely-stable provider's content into the cached system message rather * than the uncached user message. */ cacheStable?: boolean; } export declare const OWNED_CONTEXT_SOURCE_SCOPE: unique symbol; export interface ContextToolEvent extends ContextEventBase { type: "tool"; tool: ContextObjectTool; } export interface ContextInstructionEvent extends ContextEventBase { type: "instruction"; content: string; role?: ContextObjectRole; stable?: boolean; } export interface ContextSegmentEvent extends ContextEventBase { type: "segment"; segment: ContextObjectPromptSegment; } export interface ContextMetadataEvent extends ContextEventBase { type: "metadata"; key: string; value: JsonValue; } export type ContextEvent = ContextMessageEvent | ContextMemoryEvent | ContextProviderEvent | ContextToolEvent | ContextInstructionEvent | ContextSegmentEvent | ContextMetadataEvent | (ContextEventBase & Record); export interface ContextObject { id: string; version?: "v5" | (string & {}); createdAt?: number; metadata?: Record; staticPrefix?: { systemPrompt?: ContextObjectPromptSegment; characterPrompt?: ContextObjectPromptSegment; staticProviders?: ContextObjectPromptSegment[]; alwaysTools?: ToolDefinition[]; contextRegistryDigest?: string; }; trajectoryPrefix?: { messageHandlerThought?: string; selectedContexts?: AgentContext[]; contextDefinitions?: ContextDefinition[]; contextProviders?: ContextObjectPromptSegment[]; expandedTools?: ToolDefinition[]; createdAtStageId?: string; }; plannedQueue?: Array<{ id?: string; name: string; args?: JsonValue; status: "queued" | "running" | "completed" | "skipped" | "failed"; sourceStageId?: string; contextScope?: AgentContext; parentToolCallId?: string; }>; metrics?: Record; limits?: Record; /** * Append-only construction log. Consumers should render by walking this array * in order; updates are represented as new events rather than mutations. */ events: readonly ContextEvent[]; } interface CreateContextObjectOptions { id: string; createdAt?: number; metadata?: ContextObject["metadata"]; staticPrefix?: ContextObject["staticPrefix"]; trajectoryPrefix?: ContextObject["trajectoryPrefix"]; plannedQueue?: ContextObject["plannedQueue"]; metrics?: ContextObject["metrics"]; limits?: ContextObject["limits"]; events?: readonly ContextEvent[]; } /** Constructs a per-turn context object with static prefixes and an append-only event log. */ export declare function createContextObject({ id, createdAt, metadata, staticPrefix, trajectoryPrefix, plannedQueue, metrics, limits, events, }: CreateContextObjectOptions): ContextObject; export declare function appendContextEvent(context: ContextObject, event: ContextEvent): ContextObject; export {};