import { ArcjetMetadata } from "../../metadata.js"; import "../../types.js"; import { TanStackAiAgentContext } from "../v0/context.js"; /** * Structural source `tanstackAiContext` can read. * * Correlation is a **caller-owned** id from helper options, * `chat({ context })`, or `chat({ threadId })`. This helper never mints * a new id. It never reads `traceId`, `requestId`, `streamId`, or * `runId`. * * In TanStack AI 1.x `chat({ threadId })` is required and * `ChatMiddlewareContext.threadId` is the caller's value, so it is a * caller-owned id. It is the last fallback: a field on * `chat({ context })` or a helper option wins over it. * * Accepts: * - `chat({ context })` / the user object on `ChatMiddlewareContext.context` * - a `ChatMiddlewareContext`-shaped envelope (only `context` and * `threadId` are read) * - the app context object itself * - `{ threadId, context }`, the values an app passes to `chat()` * - helper `init.sessionId` / `init.correlationId` */ export interface TanStackAiContextSource { context?: unknown; correlationId?: unknown; sessionId?: unknown; conversationId?: unknown; /** The caller's `chat({ threadId })`. The last correlation fallback. */ threadId?: 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; } /** * Derive correlation and metadata from a TanStack AI `chat()` run: the * `chat({ context })` object, the `{ threadId, context }` an app passes * to `chat()`, or a `ChatMiddlewareContext`. Never mints a new id. * Never calls `createAgentContext`. 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.correlationId` / `init.sessionId` (a caller-owned fallback) * 4. `threadId` — the caller's `chat({ threadId })` * * Pass the same `{ threadId, context }` to an inbound `guard()` before * `chat()` that `chat()` gets, and both calls share one id. * * 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/v1"; * * const appContext = { sessionId: conversationId }; * export function beforeChat() { * return tanstackAiContext({ threadId, context: appContext }); * } * ``` */ export declare function tanstackAiContext(source?: TanStackAiContextSource, init?: { sessionId?: string; correlationId?: string; metadata?: ArcjetMetadata; }): TanStackAiAgentContext; export type { TanStackAiAgentContext };