import { ArcjetMetadata } from "../../metadata.js"; import "../../types.js"; /** * Structural source `tanstackAiContext` can read. * * Correlation is a **caller-owned** id from helper options or * `chat({ context })`. This helper never mints a new id. It never * reads TanStack's auto-generated `threadId` (or the deprecated * `conversationId` alias on `ChatMiddlewareContext`). It never reads * `traceId`, `requestId`, `streamId`, or `runId`. * * Accepts: * - `chat({ context })` / the user object on `ChatMiddlewareContext.context` * - a `ChatMiddlewareContext`-shaped envelope (only `context` is read) * - the app context object itself * - helper `init.sessionId` / `init.correlationId` */ export interface TanStackAiContextSource { context?: unknown; correlationId?: unknown; sessionId?: unknown; conversationId?: unknown; /** Present on TanStack's middleware envelope. Never used for correlation. */ requestId?: unknown; /** Present on TanStack's middleware envelope. Never used for correlation. */ streamId?: unknown; } /** * Context derived from a TanStack AI `chat()` run. `correlationId` is * omitted when nothing valid was present — this helper never mints one. */ export interface TanStackAiAgentContext { correlationId?: string; metadata?: ArcjetMetadata; } /** * The source as an object, or `undefined`. * * @internal Shared with `tanstack-ai/v1`; not exported from `@arcjet/guard/tanstack-ai/v0`. */ export declare function asContextSource(source: unknown): TanStackAiContextSource | undefined; /** * TanStack's `ChatMiddlewareContext` always carries `requestId` and * `streamId` (both 0.8.0 and 0.69.x). Those fields are SDK-minted, so * an envelope that looks like one must not be mined for correlation — * including the 0.69 `threadId` / deprecated `conversationId` alias. * * @internal Shared with `tanstack-ai/v1`; not exported from `@arcjet/guard/tanstack-ai/v0`. */ export declare function isMiddlewareEnvelope(source: TanStackAiContextSource): boolean; /** * The integrator-owned app object. On a `chat({ context })` / * `ChatMiddlewareContext` envelope that is `source.context`. On a bare * app object it is the source itself. * * @internal Shared with `tanstack-ai/v1`; not exported from `@arcjet/guard/tanstack-ai/v0`. */ export declare function readAppContext(source: TanStackAiContextSource | undefined): TanStackAiContextSource | undefined; /** * The first candidate that is a valid correlation id, and the last * rejected one for the warning. * * @internal Shared with `tanstack-ai/v1`; not exported from `@arcjet/guard/tanstack-ai/v0`. */ export declare function firstValidId(candidates: ReadonlyArray<{ value: unknown; label: string; }>): { id: string | undefined; rejected: string | undefined; }; /** * The first value that is a non-empty, valid correlation-id string. * * @internal Shared with `tanstack-ai/v1`; not exported from `@arcjet/guard/tanstack-ai/v0`. */ export declare function validMetadataString(values: unknown[]): string | undefined; /** * Derive correlation and metadata from a TanStack AI `chat({ context })` * object or a `ChatMiddlewareContext`. Never mints a new id. Never * calls `createAgentContext`. Never reads `ctx.threadId` (TanStack * auto-generates it). Never reads `traceId` / `requestId` / `streamId` * / `runId`. * * Preference order for `correlationId`: * 1. Fields the integrator put on `chat({ context })`: * `correlationId`, then `sessionId`, then `conversationId` * 2. Documented copies on a bare app object (not a middleware envelope) * 3. `init.sessionId` / `init.correlationId` (a caller-owned fallback) * * Prefer `tanstackAiContext({ context: appContext })`. A bare object * that also has string `requestId` and `streamId` is treated as a * `ChatMiddlewareContext` envelope, so a top-level `sessionId` on * that object is ignored. * * An invalid candidate is skipped (and warned when `ARCJET_LOG_LEVEL` * asks for warnings). If nothing valid remains, `correlationId` is * omitted so the decision is uncorrelated rather than joined to a * generated id nobody has. * * @example * ```ts * import { tanstackAiContext } from "@arcjet/guard/tanstack-ai/v0"; * * const appContext = { sessionId: conversationId }; * export function beforeChat() { * return tanstackAiContext({ context: appContext }); * } * ``` */ export declare function tanstackAiContext(source?: TanStackAiContextSource, init?: { sessionId?: string; correlationId?: string; metadata?: ArcjetMetadata; }): TanStackAiAgentContext;