import {z} from 'zod' export type AnthropicSystemContent = {type: 'text', text: string}; export type AnthropicTool = {name: string, description?: (string | null) | undefined, input_schema: {[key: string]: unknown}}; export type AnthropicThinkingConfig = {type?: ('enabled' | 'disabled' | 'adaptive' | null) | undefined, budget_tokens?: (number | null) | undefined, enabled?: (boolean | null) | undefined}; export type AnthropicTokenCountRequest = {model: string, messages: Array<{[key: string]: unknown}>, system?: (string | Array | null) | undefined, tools?: (Array | null) | undefined, thinking?: (AnthropicThinkingConfig | null) | undefined, tool_choice?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiAnthropicV1MessagesCountTokensArgs = {'x-deepinfra-service-tier'?: (string | null) | undefined, 'xi-api-key'?: (string | null) | undefined, 'x-api-key'?: (string | null) | undefined, body: AnthropicTokenCountRequest}; export type CreateApiAnthropicV1MessagesCountTokensOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAnthropicV1MessagesCountTokensResponse = z.unknown(); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Anthropic Messages Count Tokens * * POST /anthropic/v1/messages/count_tokens */ export const createApiAnthropicV1MessagesCountTokens = async (args: CreateApiAnthropicV1MessagesCountTokensArgs, options?: CreateApiAnthropicV1MessagesCountTokensOptions) => { const {baseUrl = "https://api.deepinfra.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-deepinfra-service-tier'] != null) headers.set("x-deepinfra-service-tier", String(args['x-deepinfra-service-tier'])) if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/anthropic/v1/messages/count_tokens${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAnthropicV1MessagesCountTokensResponse, await res.json()) };