import type { BatchCounts, BatchJobRef, BatchJobState, BatchJobStatus, BatchProviderCallContext, BatchProviderCapabilities } from '../types/batch.js'; import type { EmbeddingInputType, EmbeddingModality, EmbeddingModelRegistryConfig, EmbeddingParts, EmbeddingProviderPart } from '../types/embeddings.js'; import type { OperationLifecycleLike } from '../types/lifecycle.js'; import type { DurableOperationHandle, OperationRunnerConfig } from '../types/operations.js'; import type { ResponseCost, TokenUsage } from '../types/response.js'; /** One input of an embedding batch, under the id its vector comes back by. */ export interface EmbeddingBatchItem { /** Echoed back with the vector. Required, and unique: a provider does not keep the order. */ customId: string; /** A text, or parts holding media for a model that takes it. */ input: string | EmbeddingParts; } /** A batch of inputs to embed through a provider's batch tier. */ export interface EmbeddingBatchSubmitRequest { /** The inputs. Every `customId` must be unique. */ items: readonly EmbeddingBatchItem[]; /** The concrete model. A batch tier takes no alias routing. */ model: string; /** The adapter to use. Defaults to the manager's default. */ provider?: string; /** What the vectors will be used for. */ inputType?: EmbeddingInputType; /** Vector size to return, on models that can shorten theirs. */ dimensions?: number; /** Scales every vector to unit length. */ normalize?: boolean; /** How long the provider may take, in its own vocabulary. Defaults to `24h`. */ completionWindow?: string; /** Replays the operation accepted under this key instead of submitting a second batch. */ idempotencyKey?: string; /** * The tenant it runs for: its credentials, regions, and budget apply, and its ref records it, so * every later call on the batch uses the tenant's key. */ tenantId?: string; /** Labels attached to the batch on the provider's side. */ metadata?: Record; /** Overrides the manager's poll interval. */ pollIntervalMs?: number; /** Gives up waiting after this long. The provider-side batch is left running. */ timeoutMs?: number; /** Stops waiting. The provider-side batch keeps running unless it is cancelled. */ signal?: AbortSignal; } /** One input as an adapter receives it: its text, and its parts when the batch holds media. */ export interface EmbeddingBatchProviderItem { /** The id its vector comes back by. */ customId: string; /** Its text: a string itself, or its text parts joined. */ input: string; /** Its parts, media resolved to base64 or a URL, when the batch holds media. */ contents?: EmbeddingProviderPart[]; } /** A batch as an adapter receives it: inputs checked and resolved. */ export interface EmbeddingBatchProviderRequest { /** The inputs. */ items: EmbeddingBatchProviderItem[]; /** The provider's model name. */ model: string; /** What the vectors will be used for. */ inputType?: EmbeddingInputType; /** Vector size to return. */ dimensions?: number; /** The completion window. */ completionWindow?: string; /** Labels for the provider's side. */ metadata?: Record; } /** One input's outcome: its vector, or why it failed. */ export interface EmbeddingBatchOutputItem { /** The id of the input this answers. */ customId: string; /** The vector, when the input succeeded. */ vector?: number[]; /** Why it failed, when it did. */ error?: { message: string; code?: string; status?: number; }; /** Input tokens, when the provider reports them. */ inputTokens?: number; } /** What an embedding batch tier supports: a batch tier's limits, and the media it takes. */ export interface EmbeddingBatchProviderCapabilities extends BatchProviderCapabilities { /** What its inputs can hold. Text only when absent. */ inputModalities?: readonly EmbeddingModality[]; } /** Identifies an embedding batch adapter and what it supports. */ export interface EmbeddingBatchProviderInfo { /** The adapter's name, which its refs carry. */ name: string; /** What it supports. */ capabilities: EmbeddingBatchProviderCapabilities; } /** * A provider's batch tier for embeddings. Everything after `submit` takes only a `BatchJobRef`, which * is JSON and survives a restart, so a worker can collect a batch it did not submit. */ export interface EmbeddingBatchProvider { /** What the adapter is and what it supports. */ readonly info: EmbeddingBatchProviderInfo; /** Submits the batch and returns a reference that survives a restart. */ submit(request: EmbeddingBatchProviderRequest, context: BatchProviderCallContext): Promise; /** Reads the batch's current status. */ poll(ref: BatchJobRef, context: BatchProviderCallContext): Promise; /** Every input's outcome, once the batch has finished. */ results(ref: BatchJobRef, context: BatchProviderCallContext): Promise; /** Cancels a running batch. */ cancel?(ref: BatchJobRef, context: BatchProviderCallContext): Promise; } /** A finished embedding batch: every input's outcome, its usage, and its cost at the batch price. */ export interface EmbeddingBatchResult { /** The batch. */ ref: BatchJobRef; /** Its final status. */ status: BatchJobStatus; /** Every input's outcome, matched by `customId`, never by position. */ items: EmbeddingBatchOutputItem[]; /** How many succeeded and failed. */ counts: BatchCounts; /** Input tokens: reported where the provider reports them, estimated where it does not. */ usage: TokenUsage; /** Priced from the embedding registry, with the provider's discount applied. */ cost: ResponseCost; /** ISO-8601 time it was submitted. */ createdAt?: string; /** ISO-8601 time it finished. */ completedAt?: string; /** The provider's final status payload. */ raw?: unknown; } /** Configuration for `EmbeddingBatchManager`. */ export interface EmbeddingBatchConfig { /** Adapters, by name. */ providers?: Record; /** The adapter used when a submission names none. */ defaultProvider?: string; /** How often to poll a running batch. Defaults to 30 seconds. */ pollIntervalMs?: number; /** The longest poll interval once backoff has grown it. Defaults to 5 minutes. */ maxPollIntervalMs?: number; /** How long a submission waits before giving up. Defaults to 26 hours. */ timeoutMs?: number; /** Embedding models and prices beyond the bundled ones, used to check inputs and price results. */ models?: EmbeddingModelRegistryConfig; } /** What the manager runs on. */ export interface EmbeddingBatchManagerRuntime { /** Persists each submission, so a submitted batch survives a restart. */ operations?: OperationRunnerConfig; /** * Runs every submission as one operation of a client's lifecycle, labelled `embedding`: pass * `ai.lifecycle`. It is authorized before the provider sees it, and its cost is reconciled when the * batch is collected. */ lifecycle?: OperationLifecycleLike; } /** * Embedding batch tiers behind one handle: submit, poll with backoff, collect, and price at the * batch rate. A submission is a durable operation that records its batch the moment it exists, so a * worker that dies while it waits is replaced by one that collects that batch, never a second. */ export declare class EmbeddingBatchManager { private readonly config; private readonly runtime; private readonly providers; /** Loaded on the first `submit()`: `start()` and `resume()` need neither the runner nor the lifecycle. */ private durable?; private readonly tokenizer; constructor(config?: EmbeddingBatchConfig, runtime?: EmbeddingBatchManagerRuntime); /** Registers an adapter under a name. Returns the manager, for chaining. */ registerEmbeddingBatchProvider(name: string, provider: EmbeddingBatchProvider): this; /** Whether an adapter is registered. */ hasEmbeddingBatchProvider(name: string): boolean; /** Every registered adapter's name. */ listEmbeddingBatchProviders(): string[]; /** * Submits a batch and returns a durable handle that settles when the provider finishes, which can * be hours away: persist `handle.id`, rather than block a request on `handle.result()`. */ submit(request: EmbeddingBatchSubmitRequest): Promise>; /** * Submits a batch and returns its ref at once, without waiting: for a caller that keeps the ref * itself, as the ingestion pipeline does, and collects it later with `resume()`. */ start(request: EmbeddingBatchSubmitRequest): Promise; /** Waits for a batch, submitted here or by another process, and collects it. */ resume(ref: BatchJobRef, options?: { signal?: AbortSignal; pollIntervalMs?: number; }): Promise; /** The batch's state now, without waiting. */ status(ref: BatchJobRef): Promise; /** Cancels a batch. Throws when the adapter cannot cancel. */ cancel(ref: BatchJobRef): Promise; /** A call on a batch outside its operation: with its tenant's key, inside its regions. */ private continuing; private prepare; private collect; private price; private resolveProvider; /** The adapter a ref belongs to: by its registered name, or the adapter's own name the ref carries. */ private providerFor; } /** What `batchEmbedder()` submits under. */ export interface BatchEmbedderOptions { /** The concrete model. */ model: string; /** The adapter. Defaults to the manager's default. */ provider?: string; /** What the vectors will be used for. Ingestion stores documents, so defaults to `document`. */ inputType?: EmbeddingInputType; /** Vector size to return. */ dimensions?: number; /** Scales every vector to unit length. */ normalize?: boolean; /** Texts per provider batch. Defaults to 10,000. */ size?: number; /** How often to poll. Defaults to the manager's interval. */ pollIntervalMs?: number; } /** What `createIngestionPipeline()` takes as `batchEmbed`: submit texts as one batch, collect it later. */ export interface BatchEmbedder { /** Texts per provider batch. */ readonly size: number; /** Submits texts as one batch, labelled with `key`, and returns its ref, which survives a restart. */ submit(texts: string[], key: string): Promise; /** Waits for a batch and returns its vectors, in the order its texts were submitted. */ collect(ref: BatchJobRef, signal?: AbortSignal): Promise; } /** * An embedding batch tier as the ingestion pipeline's `batchEmbed`: each window of chunks becomes one * provider batch at the batch price. A batch that does not complete, or any input that fails, fails * the collection, so a window is never written half embedded. */ export declare function batchEmbedder(manager: EmbeddingBatchManager, options: BatchEmbedderOptions): BatchEmbedder;