-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
190 lines (161 loc) · 8.32 KB
/
api.js
File metadata and controls
190 lines (161 loc) · 8.32 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// const API_ENDPOINT = 'https://text.pollinations.ai';
const API_ENDPOINT = 'http://localhost:16385';
function removeThink(text, playerIdx, playerName) {
// Check for content after last </think>
const thinkMatch = text.split('</think>');
if (thinkMatch.length > 1) {
// Get everything before the last </think> tag
const thinkContent = thinkMatch.slice(0, -1).join('</think>');
console.log('%cThink content:', 'color: blue', thinkContent);
// Update the thoughts display
const thoughtsDiv = document.querySelector('#player-thoughts .thoughts');
const playerThoughtId = `player-${playerIdx + 1}-thought`;
let playerThought = document.getElementById(playerThoughtId);
if (!playerThought) {
playerThought = document.createElement('div');
playerThought.id = playerThoughtId;
playerThought.className = `thought player-${playerIdx + 1}-thought`;
thoughtsDiv.appendChild(playerThought);
}
if (thinkContent) {
playerThought.innerHTML = `<strong>${playerName}:</strong> ${thinkContent}`;
}
// Return everything after the last </think> tag
return thinkMatch[thinkMatch.length - 1].trim();
}
return text;
}
async function fetchApiResponse(url, model, messages) {
const maxRetries = 3;
const initialDelay = 1000; // Initial delay of 1 second
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
// Calculate exponential backoff delay
const backoffDelay = initialDelay * Math.pow(2, attempt);
await delay(backoffDelay);
const response = await fetch(url+'/openai', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model,
messages,
temperature: 0.5,
seed: Math.floor(Math.random() * 1000000), // Generate a random seed for each request, between 0 and 999999Math.random()
referrer: "roblox"
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response;
} catch (error) {
if (attempt === maxRetries - 1) {
// If this was the last attempt, throw the error
throw new Error(`Failed after ${maxRetries} attempts: ${error.message}`);
}
console.log(`Attempt ${attempt + 1} failed, retrying... Error: ${error.message}`);
}
}
}
async function getConversationResponse(playerIdx, gameState, getPlayerDisplayName) {
const playerNames = gameState.players.map(getPlayerDisplayName);
try {
const [commonPrompt, conversationPrompt] = await Promise.all([
loadPrompt('common'),
loadPrompt('conversation')
]);
const fullTemplate = commonPrompt + '\n\n' + conversationPrompt;
const prompt = replaceTemplateVariables(fullTemplate, playerNames, playerIdx, gameState);
console.log('[API Request]', `Getting conversation response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, { model: gameState.players[playerIdx].model, prompt, seed: Math.random() });
try {
const response = await fetchApiResponse(API_ENDPOINT, gameState.players[playerIdx].model, [
{ role: 'user', content: prompt }
]);
if (!response.ok) {
const errorText = await response.text();
console.log('[API Error]', `Error getting conversation response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, errorText);
return '<stop>';
}
const result = await response.json();
console.log('[API Debug]', `Raw API response:`, result);
// Add null check for content
const content = result?.choices?.[0]?.message?.content;
if (!content) {
console.log('[API Error]', `No content in response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, result);
return '<stop>';
}
const trimmedContent = removeThink(content.trim(), playerIdx, getPlayerDisplayName(gameState.players[playerIdx]));
console.log('[API Response]', `Got conversation response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, {
originalContent: content,
trimmedContent
});
return trimmedContent;
} catch (error) {
console.log('[API Error]', `Exception getting conversation response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, error);
return '<stop>';
}
} catch (error) {
console.log('[API Error]', `Exception getting conversation response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, error);
return '<stop>';
}
}
async function getMoveResponse(playerIdx, gameState, getPlayerDisplayName) {
const playerNames = gameState.players.map(getPlayerDisplayName);
try {
const [commonPrompt, movePrompt] = await Promise.all([
loadPrompt('common'),
loadPrompt('move')
]);
const fullTemplate = commonPrompt + '\n\n' + movePrompt;
const prompt = replaceTemplateVariables(fullTemplate, playerNames, playerIdx, gameState);
console.log('[API Request]', `Getting move response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, { model: gameState.players[playerIdx].model, prompt, seed: Math.random() });
try {
const response = await fetchApiResponse(API_ENDPOINT, gameState.players[playerIdx].model, [
{ role: 'user', content: prompt }
]);
if (!response.ok) {
const errorText = await response.text();
console.log('[API Error]', `Error getting move response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, errorText);
return null;
}
const result = await response.json();
// Add null check for content
const content = result?.choices?.[0]?.message?.content;
if (!content) {
console.log('[API Error]', `No content in response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, result);
return null;
}
const moveText = removeThink(content.trim(), playerIdx, getPlayerDisplayName(gameState.players[playerIdx]));
console.log('[API Response]', `Got move response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, { rawResponse: moveText });
return moveText;
} catch (error) {
console.log('[API Error]', `Exception getting move response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, error);
return null;
}
} catch (error) {
console.log('[API Error]', `Exception getting move response for ${getPlayerDisplayName(gameState.players[playerIdx])}`, error);
return null;
}
}
async function fetchModels() {
return fetch(`${API_ENDPOINT}/models`)
.then(response => response.json());
}
function replaceTemplateVariables(template, playerNames, playerIdx, gameState) {
const characterType = gameState.players[playerIdx].characterType;
const result = template
.replace(/{{PLAYER_NAME}}/g, playerNames[playerIdx])
.replace(/{{PLAYERS_LIST}}/g, playerNames.join(', '))
.replace(/{{CHARACTER_TYPE}}/g, characterType)
.replace(/{{CHARACTER_DESCRIPTION}}/g, CHARACTER_TYPES[characterType])
.replace(/{{MAX_SUB_ROUND}}/g, MAX_SUB_ROUNDS)
.replace(/{{WIN_STEPS}}/g, FINISH_LINE)
.replace(/{{CONVERSATION_HISTORY}}/g, gameState.conversation.join('\n'))
.replace(/{{GAME_STATE}}/g, `Scores: ${gameState.scores.map((score, idx) =>
`${playerNames[idx]}: ${score}`).join(', ')}`);
// Add reasoning rules if enabled for this player
if (gameState.reasoningEnabled[playerIdx]) {
return result + '\n' + (promptCache.reasoning || '');
}
return result;
}