Skip to content

Commit

Permalink
fix(claude): fix claude web api request failure
Browse files Browse the repository at this point in the history
  • Loading branch information
UNICKCHENG committed Aug 19, 2023
1 parent 2173b48 commit 3347723
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 1,902 deletions.
63 changes: 0 additions & 63 deletions app/api/claude/append_message/route.ts

This file was deleted.

25 changes: 0 additions & 25 deletions app/api/claude/retry_message/route.ts

This file was deleted.

18 changes: 0 additions & 18 deletions libs/claude/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ interface Conversation {
updated_at: string,
}

/**
* Get a conversation with the default name,
* or create one if it doesn't exist.
*/
export const autoGetConversationId = cache(async (org_id: string, req_url: string) => {
const sessionKey: string = headers().get('Authorization')?.split(' ')[1]!;
const conversations: Conversation[] = await getConversations(org_id, sessionKey);
if (0 < conversations.length) {
for (const conversation of conversations) {
if (process.env.CLAUDE_DEFAULT_CONVERSATION_NAME == conversation.name) {
return conversation.uuid;
}
}
}
const conversation: Conversation = await createConversation(org_id, sessionKey);
return conversation.uuid;
})

export async function getConversations(org_id: string, sessionKey: string): Promise<Conversation[]> {
const base_url: string = `${process.env.CLAUDE_BASE}/organizations/${org_id}/chat_conversations`;
const data = await fetch(base_url, {
Expand Down
6 changes: 6 additions & 0 deletions libs/cycletls/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import initCycleTLS, { type CycleTLSRequestOptions } from 'cycletls'
export * from './message'
export default initCycleTLS
export {
type CycleTLSRequestOptions,
}
14 changes: 14 additions & 0 deletions libs/cycletls/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 将 claude web api 返回格式转为正常文本内容
*/
export function formatConversion(result: string) {
let content: string = '';
const lines = result.split("\n\n");
lines.map((line) => line.replace(/^data: /, "").trim())
.filter((line) => line !== "")
.forEach((line) => {
const te = JSON.parse(line);
content += te.completion ? te.completion : '';
});
return content;
}
Loading

0 comments on commit 3347723

Please sign in to comment.