import { Anthropic } from "@anthropic-ai/sdk"; import { BaseLLM, ChatMessage, ChatMessageRole, ChatParams, ChatResult, ClassifyParams, ClassifyResult, SummarizeParams, SummarizeResult, FileCapabilities } from "@memberjunction/ai"; /** * Sentinel a prompt can embed to tell the Anthropic adapter WHERE the stable, cacheable prefix ends * and the volatile (per-turn) content begins. The adapter places an Anthropic `cache_control` * breakpoint at each marker (caching everything before it) and removes the marker from the text the * model sees. Without a marker the whole block is cached as before. * * Why it's needed: Anthropic only caches up to an explicit breakpoint and read-hits require the new * request to match a cached prefix AT a breakpoint. If volatile content (date/scratchpad/payload) * sits at the end of the system prompt and the only breakpoint is at the very end, the cached * segment includes the volatile bytes, so every turn misses and rewrites. Putting the marker between * the stable instructions and the volatile tail makes the stable prefix a reusable cache segment. * * Providers that cache the longest common prefix automatically (OpenAI, Gemini) don't need this; * they should strip the marker from outgoing content. */ export declare const ANTHROPIC_CACHE_BREAKPOINT = "<<>>"; export declare class AnthropicLLM extends BaseLLM { private _anthropic; private _streamingState; constructor(apiKey: string); /** * Read only getter method to get the Anthropic client instance */ get AnthropicClient(): Anthropic; /** * Anthropic supports streaming */ get SupportsStreaming(): boolean; /** * Anthropic natively supports assistant prefill */ get SupportsPrefill(): boolean; /** * Anthropic supports PDF and image file inputs natively. */ GetFileCapabilities(): FileCapabilities | null; /** * Format message content for Anthropic API with optional caching. * Supports both text and image content blocks. * @param content The message content (string or content blocks) * @param enableCaching Whether to enable caching * @returns Array of formatted content blocks for Anthropic API */ /** * Turn a text string into one or more Anthropic text blocks, honoring any * {@link ANTHROPIC_CACHE_BREAKPOINT} markers it contains. * * - No marker: a single text block, with `cache_control` iff `cacheLastSegment` is true * (preserves the historical "cache the whole block" behavior). * - With marker(s): split on the marker into segments. Every segment BEFORE a marker gets a * `cache_control` breakpoint (so the stable prefix is cached); the final segment is left * uncached unless `cacheLastSegment` is true. The marker text itself is removed. Breakpoints * are capped at {@link MAX_CACHE_BREAKPOINTS}. */ private pushTextBlocks; private formatContentWithCaching; /** * Format an image content block for Anthropic's API. * Anthropic expects images in the format: * { type: "image", source: { type: "base64", media_type: "image/jpeg", data: "..." } } * @param block The image content block * @returns Formatted image block for Anthropic, or null if invalid */ private formatImageBlock; /** * Format a file content block as an Anthropic document block. * Anthropic supports documents in the format: * { type: "document", source: { type: "base64", media_type: "application/pdf", data: "..." } } * * Supported MIME types: application/pdf, text/plain, text/csv, text/html, * application/vnd.openxmlformats-officedocument.wordprocessingml.document (docx), * application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx) */ private formatDocumentBlock; /** Infer MIME type from file extension when mimeType is not provided */ private inferDocumentMimeType; /** * Format messages for Anthropic API with caching support. * Handles both text and multi-modal content (images). * @param messages Messages to format * @param enableCaching Whether to enable caching * @returns Formatted messages */ protected formatMessagesWithCaching(messages: ChatMessage[], enableCaching?: boolean): any[]; /** * Format system messages for Anthropic API with caching support. * System messages support multi-modal content (images). * @param messages Messages to format * @param enableCaching Whether to enable caching * @returns Flattened array of formatted content blocks */ protected formatSystemMessagesWithCaching(messages: ChatMessage[], enableCaching?: boolean): any[]; /** * Appends an assistant prefill message to the messages array if prefill text is provided. * This causes the model to continue generating from where the prefill ends. * @param messages The original messages array * @param prefill The prefill text, or undefined to skip * @returns A new messages array with the prefill appended, or the original if no prefill */ private appendPrefillMessage; /** * Utility method to map a MemberJunction role to OpenAI role * - user maps to user * - assistant maps to assistant * - anything else maps to user * While the above is a direct 1:1 mapping, it is possible that OpenAI may have more roles in the future and this method will need to be updated for flexibility * @param role * @returns */ ConvertMJToAnthropicRole(role: ChatMessageRole): 'assistant' | 'user'; /** * Was this error produced by the caller aborting the request? * * The Anthropic SDK raises {@link APIUserAbortError} when the `signal` we hand it fires (and it * does NOT retry an aborted request — `makeRequest` throws the abort before any retry decision). * We also treat "the token is aborted" as cancellation, which covers the raw `Stream` path where * the SDK swallows the abort instead of surfacing an error. */ private isCancellation; /** * Build the failed ChatResult returned when a request is cancelled (caller abort or the * timeout composed into `cancellationToken` by AIPromptRunner). Shaped like every other * failure this driver reports, so callers keep using `success === false` + `errorMessage`. */ private buildCancelledResult; /** * Non-streaming implementation for Anthropic */ protected nonStreamingChatCompletion(params: ChatParams): Promise; /** * Reset streaming state for a new request. Overrides the base-class hook so * `BaseLLM.handleStreamingChatCompletion` calls this both at the start of a * request AND in its `finally` block — the latter releases accumulated * thinking buffers (which can grow to 100k+ chars on extended-thinking * outputs) and prevents state from a prior request bleeding into the next. * See audit R2-C5. */ protected resetStreamingState(): void; /** * Create a streaming request for Anthropic */ protected createStreamingRequest(params: ChatParams): Promise; /** * Process a streaming chunk from Anthropic */ protected processStreamingChunk(chunk: any): { content: string; finishReason?: string; usage?: any; }; /** * Process pending content to extract thinking blocks * Returns content that should be emitted to the user */ private processThinkingInStreamingContent; /** * Create the final response from streaming results for Anthropic */ protected finalizeStreamingResponse(accumulatedContent: string | null | undefined, lastChunk: any | null | undefined, usage: any | null | undefined): ChatResult; SummarizeText(params: SummarizeParams): Promise; ClassifyText(params: ClassifyParams): Promise; } //# sourceMappingURL=anthropic.d.ts.map