This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (76 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// AIクラスの定義
export default class AI {
static availableModels = [
'gpt-4o',
'claude-sonnet-3.5',
'gemini-pro',
'blackboxai'
];
constructor(model = 'blackboxai') {
if (!AI.availableModels.includes(model)) {
throw new Error(`Invalid model specified. Available models are: ${AI.availableModels.join(', ')}`);
}
this.apiUrl = 'https://www.blackbox.ai/api/chat';
this.headers = {
'Content-Type': 'application/json',
};
this.conversationHistory = {};
this.model = model; // モデルを設定
}
async sendMessage(conversationId, content) {
// 会話履歴が存在しない場合は初期化
if (!this.conversationHistory[conversationId]) {
this.conversationHistory[conversationId] = [];
}
// ユーザーからのメッセージを追加
const message = { id: conversationId, content, role: 'user' };
this.conversationHistory[conversationId].push(message);
// ペイロードを作成
const payload = this.createPayload(conversationId);
try {
const response = await fetch(this.apiUrl, {
body: JSON.stringify(payload),
headers: this.headers,
method: 'POST'
}).then(res => res.text())
const cleanedResponse = this.cleanResponse(response);
const assistantMessage = { id: `response-${Date.now()}`, content: cleanedResponse, role: 'assistant' };
this.conversationHistory[conversationId].push(assistantMessage);
return assistantMessage.content;
} catch (error) {
this.handleError(error);
}
}
createPayload(conversationId) {
return {
messages: this.conversationHistory[conversationId],
id: conversationId,
previewToken: null,
userId: null,
codeModelMode: true,
agentMode: {},
trendingAgentMode: {},
isMicMode: false,
userSystemPrompt: null,
maxTokens: 1024,
playgroundTopP: 0.9,
playgroundTemperature: 0.5,
isChromeExt: false,
githubToken: null,
clickedAnswer2: false,
clickedAnswer3: false,
clickedForceWebSearch: false,
visitFromDelta: false,
mobileClient: false,
userSelectedModel: this.model // 動的モデル選択
};
}
cleanResponse(data) {
// 不要なテキストを削除
return data.replace(/Generated by BLACKBOX\.AI, try unlimited chat https:\/\/www\.blackbox\.ai\n\n/g, '');
}
handleError(error) {
console.error('Error communicating with Blackbox.ai:', error);
throw new Error('An error occurred while sending the message.');
}
}