also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,171 @@ | ||
| 1 | import type { AIProvider, ChatOptions, ChatResponse, Message, StreamChunk } from '../types' | |
| 2 | ||
| 3 | interface AnthropicConfig { | |
| 4 | apiKey: string | |
| 5 | model?: string | |
| 6 | } | |
| 7 | ||
| 8 | export function anthropicProvider(config: AnthropicConfig): AIProvider { | |
| 9 | const defaultModel = config.model ?? 'claude-sonnet-4-20250514' | |
| 10 | ||
| 11 | return { | |
| 12 | name: 'anthropic', | |
| 13 | ||
| 14 | async chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse> { | |
| 15 | if (!config.apiKey) { | |
| 16 | throw new Error('Anthropic API key is required.') | |
| 17 | } | |
| 18 | ||
| 19 | const model = options?.model ?? defaultModel | |
| 20 | const systemMessages = messages.filter(m => m.role === 'system') | |
| 21 | const nonSystemMessages = messages.filter(m => m.role !== 'system') | |
| 22 | ||
| 23 | const systemText = options?.system | |
| 24 | ?? (systemMessages.length > 0 | |
| 25 | ? systemMessages.map(m => m.content).join('\n') | |
| 26 | : undefined) | |
| 27 | ||
| 28 | const body: Record<string, unknown> = { | |
| 29 | model, | |
| 30 | messages: nonSystemMessages.map(m => ({ role: m.role, content: m.content })), | |
| 31 | max_tokens: options?.maxTokens ?? 1024, | |
| 32 | } | |
| 33 | ||
| 34 | if (systemText) { | |
| 35 | body.system = systemText | |
| 36 | } | |
| 37 | if (options?.temperature !== undefined) { | |
| 38 | body.temperature = options.temperature | |
| 39 | } | |
| 40 | if (options?.topP !== undefined) { | |
| 41 | body.top_p = options.topP | |
| 42 | } | |
| 43 | if (options?.stop) { | |
| 44 | body.stop_sequences = options.stop | |
| 45 | } | |
| 46 | ||
| 47 | const response = await fetch('https://api.anthropic.com/v1/messages', { | |
| 48 | method: 'POST', | |
| 49 | headers: { | |
| 50 | 'Content-Type': 'application/json', | |
| 51 | 'x-api-key': config.apiKey, | |
| 52 | 'anthropic-version': '2023-06-01', | |
| 53 | }, | |
| 54 | body: JSON.stringify(body), | |
| 55 | }) | |
| 56 | ||
| 57 | if (!response.ok) { | |
| 58 | throw new Error(`Anthropic API error: ${response.status} ${response.statusText}`) | |
| 59 | } | |
| 60 | ||
| 61 | const data = await response.json() as { | |
| 62 | content: Array<{ type: string, text: string }> | |
| 63 | model: string | |
| 64 | usage: { input_tokens: number, output_tokens: number } | |
| 65 | stop_reason: string | |
| 66 | } | |
| 67 | ||
| 68 | return { | |
| 69 | content: data.content.map(c => c.text).join(''), | |
| 70 | model: data.model, | |
| 71 | usage: { | |
| 72 | inputTokens: data.usage.input_tokens, | |
| 73 | outputTokens: data.usage.output_tokens, | |
| 74 | totalTokens: data.usage.input_tokens + data.usage.output_tokens, | |
| 75 | }, | |
| 76 | finishReason: data.stop_reason, | |
| 77 | } | |
| 78 | }, | |
| 79 | ||
| 80 | async *stream(messages: Message[], options?: ChatOptions): AsyncIterable<StreamChunk> { | |
| 81 | if (!config.apiKey) { | |
| 82 | throw new Error('Anthropic API key is required.') | |
| 83 | } | |
| 84 | ||
| 85 | const model = options?.model ?? defaultModel | |
| 86 | const systemMessages = messages.filter(m => m.role === 'system') | |
| 87 | const nonSystemMessages = messages.filter(m => m.role !== 'system') | |
| 88 | ||
| 89 | const systemText = options?.system | |
| 90 | ?? (systemMessages.length > 0 | |
| 91 | ? systemMessages.map(m => m.content).join('\n') | |
| 92 | : undefined) | |
| 93 | ||
| 94 | const body: Record<string, unknown> = { | |
| 95 | model, | |
| 96 | messages: nonSystemMessages.map(m => ({ role: m.role, content: m.content })), | |
| 97 | max_tokens: options?.maxTokens ?? 1024, | |
| 98 | stream: true, | |
| 99 | } | |
| 100 | ||
| 101 | if (systemText) { | |
| 102 | body.system = systemText | |
| 103 | } | |
| 104 | if (options?.temperature !== undefined) { | |
| 105 | body.temperature = options.temperature | |
| 106 | } | |
| 107 | if (options?.topP !== undefined) { | |
| 108 | body.top_p = options.topP | |
| 109 | } | |
| 110 | if (options?.stop) { | |
| 111 | body.stop_sequences = options.stop | |
| 112 | } | |
| 113 | ||
| 114 | const response = await fetch('https://api.anthropic.com/v1/messages', { | |
| 115 | method: 'POST', | |
| 116 | headers: { | |
| 117 | 'Content-Type': 'application/json', | |
| 118 | 'x-api-key': config.apiKey, | |
| 119 | 'anthropic-version': '2023-06-01', | |
| 120 | }, | |
| 121 | body: JSON.stringify(body), | |
| 122 | }) | |
| 123 | ||
| 124 | if (!response.ok) { | |
| 125 | throw new Error(`Anthropic API error: ${response.status} ${response.statusText}`) | |
| 126 | } | |
| 127 | ||
| 128 | const reader = response.body?.getReader() | |
| 129 | if (!reader) { | |
| 130 | throw new Error('No response body') | |
| 131 | } | |
| 132 | ||
| 133 | const decoder = new TextDecoder() | |
| 134 | let buffer = '' | |
| 135 | ||
| 136 | while (true) { | |
| 137 | const { done, value } = await reader.read() | |
| 138 | if (done) | |
| 139 | break | |
| 140 | ||
| 141 | buffer += decoder.decode(value, { stream: true }) | |
| 142 | const lines = buffer.split('\n') | |
| 143 | buffer = lines.pop() ?? '' | |
| 144 | ||
| 145 | for (const line of lines) { | |
| 146 | if (!line.startsWith('data: ')) | |
| 147 | continue | |
| 148 | const data = line.slice(6) | |
| 149 | if (data === '[DONE]') { | |
| 150 | yield { content: '', done: true, finishReason: 'stop' } | |
| 151 | return | |
| 152 | } | |
| 153 | ||
| 154 | try { | |
| 155 | const event = JSON.parse(data) as { type: string, delta?: { type: string, text: string }, stop_reason?: string } | |
| 156 | if (event.type === 'content_block_delta' && event.delta?.text) { | |
| 157 | yield { content: event.delta.text, done: false } | |
| 158 | } | |
| 159 | else if (event.type === 'message_stop') { | |
| 160 | yield { content: '', done: true, finishReason: 'stop' } | |
| 161 | return | |
| 162 | } | |
| 163 | } | |
| 164 | catch { | |
| 165 | // skip malformed JSON | |
| 166 | } | |
| 167 | } | |
| 168 | } | |
| 169 | }, | |
| 170 | } | |
| 171 | } | |