export type ClaudeChatProxyDto = {messages: Array<{role: 'user' | 'assistant', content: string} | {[key: string]: unknown}>, model: string, max_tokens?: number | undefined} | {[key: string]: unknown}; export type CreateApiApiAiV1MessagesArgs = {body: ClaudeChatProxyDto}; export type CreateApiApiAiV1MessagesOptions = {baseUrl?: string | undefined, apikeyHeaderXApiKey?: string | undefined, fetch?: typeof fetch | undefined}; /** * Anthropic 对话 * * POST /api/ai/v1/messages */ export const createApiApiAiV1Messages = async (args: CreateApiApiAiV1MessagesArgs, options?: CreateApiApiAiV1MessagesOptions) => { const {baseUrl = "https://aitoearn.cn", fetch: fetchFn = fetch, apikeyHeaderXApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/event-stream") if (apikeyHeaderXApiKey !== undefined) headers.set("X-Api-Key", apikeyHeaderXApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/ai/v1/messages${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 res.text() };