Open History to see the last commit
1import Anthropic_SDK from '@anthropic-ai/sdk'
2import { generateId as _generateId, getApiKeyFromEnv } from '@tanstack/ai-utils'
3import type { ClientOptions } from '@anthropic-ai/sdk'
4
5export interface AnthropicClientConfig extends ClientOptions {
6 apiKey: string
7}
8
9type AnyAnthropicMessagesCreate = (
10 params: never,
11 ...args: Array<never>
12) => unknown
13
14/**
15 * The minimal Anthropic client surface used by the text adapter.
16 *
17 * The callable is intentionally type-erased because alternative Anthropic
18 * clients can depend on a different 0.x release of the Anthropic SDK. Their
19 * request and response declarations may drift even when the runtime Messages
20 * protocol remains compatible.
21 */
22export interface AnthropicMessagesClient {
23 readonly beta: {
24 readonly messages: {
25 readonly create: AnyAnthropicMessagesCreate
26 }
27 }
28}
29
30/**
31 * Creates an Anthropic SDK client instance
32 */
33export function createAnthropicClient(
34 config: AnthropicClientConfig,
35): Anthropic_SDK {
36 return new Anthropic_SDK({
37 ...config,
38 apiKey: config.apiKey,
39 })
40}
41
42/**
43 * Gets Anthropic API key from environment variables
44 * @throws Error if ANTHROPIC_API_KEY is not found
45 */
46export function getAnthropicApiKeyFromEnv(): string {
47 return getApiKeyFromEnv('ANTHROPIC_API_KEY')
48}
49
50/**
51 * Generates a unique ID with a prefix
52 */
53export function generateId(prefix: string): string {
54 return _generateId(prefix)
55}