ratalai commited on
Commit
3c988d7
·
1 Parent(s): fde3bc0

Fix CORS issue: use official Anthropic API instead of proxy

Browse files
Files changed (1) hide show
  1. src/features/chat/anthropicChat.ts +7 -12
src/features/chat/anthropicChat.ts CHANGED
@@ -1,21 +1,14 @@
1
  import { Message } from "../messages/messages";
2
 
3
- const WORKER_URL = 'https://anthropic-api-proxy.ratadune.workers.dev/';
4
- //console.log("WORKER_URL=", WORKER_URL);
 
5
 
6
  export async function getAnthropicChatResponseStream(
7
  messages: Message[],
8
  apiKey: string,
9
  model: string
10
  ) {
11
- /*
12
- console.log("Function called with:", {
13
- messagesCount: messages.length,
14
- apiKeyLength: apiKey.length,
15
- model: model
16
- });
17
- */
18
-
19
  // 提取系統消息
20
  const systemMessage = messages.find(msg => msg.role === "system");
21
  let userMessages = messages.filter(msg => msg.role !== "system");
@@ -30,12 +23,14 @@ export async function getAnthropicChatResponseStream(
30
  system: systemMessage ? systemMessage.content : undefined,
31
  };
32
  console.log("Request body:", JSON.stringify(requestBody, null, 2));
33
- //console.log("apiK:", apiKey);
34
  try {
35
- const response = await fetch(`${WORKER_URL}?api_key=${apiKey}`, {
36
  method: 'POST',
37
  headers: {
38
  'Content-Type': 'application/json',
 
 
39
  },
40
  body: JSON.stringify(requestBody),
41
  });
 
1
  import { Message } from "../messages/messages";
2
 
3
+ // 直接使用 Anthropic 官方 API(修正 CORS 問題)
4
+ const ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages';
5
+ const ANTHROPIC_API_VERSION = '2023-06-01';
6
 
7
  export async function getAnthropicChatResponseStream(
8
  messages: Message[],
9
  apiKey: string,
10
  model: string
11
  ) {
 
 
 
 
 
 
 
 
12
  // 提取系統消息
13
  const systemMessage = messages.find(msg => msg.role === "system");
14
  let userMessages = messages.filter(msg => msg.role !== "system");
 
23
  system: systemMessage ? systemMessage.content : undefined,
24
  };
25
  console.log("Request body:", JSON.stringify(requestBody, null, 2));
26
+
27
  try {
28
+ const response = await fetch(ANTHROPIC_API_URL, {
29
  method: 'POST',
30
  headers: {
31
  'Content-Type': 'application/json',
32
+ 'x-api-key': apiKey,
33
+ 'anthropic-version': ANTHROPIC_API_VERSION,
34
  },
35
  body: JSON.stringify(requestBody),
36
  });