ideacode 1.1.6 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +59 -25
- package/dist/repl.js +1 -0
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -9,6 +9,11 @@ export async function fetchModels(apiKey) {
|
|
|
9
9
|
const json = (await res.json());
|
|
10
10
|
return json.data ?? [];
|
|
11
11
|
}
|
|
12
|
+
const MAX_RETRIES = 3;
|
|
13
|
+
const INITIAL_BACKOFF_MS = 1000;
|
|
14
|
+
function sleep(ms) {
|
|
15
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
16
|
+
}
|
|
12
17
|
export async function callApi(apiKey, messages, systemPrompt, model) {
|
|
13
18
|
const body = {
|
|
14
19
|
model,
|
|
@@ -17,17 +22,31 @@ export async function callApi(apiKey, messages, systemPrompt, model) {
|
|
|
17
22
|
messages,
|
|
18
23
|
tools: makeSchema(),
|
|
19
24
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
let lastError = null;
|
|
26
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
27
|
+
const res = await fetch(config.apiUrl, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: {
|
|
30
|
+
"Content-Type": "application/json",
|
|
31
|
+
Authorization: `Bearer ${apiKey}`,
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(body),
|
|
34
|
+
});
|
|
35
|
+
if (res.ok)
|
|
36
|
+
return res.json();
|
|
37
|
+
const text = await res.text();
|
|
38
|
+
lastError = new Error(`API ${res.status}: ${text}`);
|
|
39
|
+
if ((res.status === 429 || res.status === 503) && attempt < MAX_RETRIES) {
|
|
40
|
+
const retryAfter = res.headers.get("retry-after");
|
|
41
|
+
const waitMs = retryAfter
|
|
42
|
+
? Math.max(1000, parseInt(retryAfter, 10) * 1000)
|
|
43
|
+
: INITIAL_BACKOFF_MS * Math.pow(2, attempt);
|
|
44
|
+
await sleep(waitMs);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
throw lastError;
|
|
48
|
+
}
|
|
49
|
+
throw lastError;
|
|
31
50
|
}
|
|
32
51
|
function parseStreamChunk(line) {
|
|
33
52
|
if (line.startsWith("data: ")) {
|
|
@@ -176,18 +195,33 @@ export async function callSummarize(apiKey, messages, model) {
|
|
|
176
195
|
system: SUMMARIZE_SYSTEM,
|
|
177
196
|
messages,
|
|
178
197
|
};
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
198
|
+
let lastError = null;
|
|
199
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
200
|
+
const res = await fetch(config.apiUrl, {
|
|
201
|
+
method: "POST",
|
|
202
|
+
headers: {
|
|
203
|
+
"Content-Type": "application/json",
|
|
204
|
+
Authorization: `Bearer ${apiKey}`,
|
|
205
|
+
},
|
|
206
|
+
body: JSON.stringify(body),
|
|
207
|
+
});
|
|
208
|
+
if (res.ok) {
|
|
209
|
+
const data = (await res.json());
|
|
210
|
+
const blocks = data.content ?? [];
|
|
211
|
+
const textBlock = blocks.find((b) => b.type === "text" && b.text);
|
|
212
|
+
return (textBlock?.text ?? "").trim();
|
|
213
|
+
}
|
|
214
|
+
const text = await res.text();
|
|
215
|
+
lastError = new Error(`Summarize API ${res.status}: ${text}`);
|
|
216
|
+
if ((res.status === 429 || res.status === 503) && attempt < MAX_RETRIES) {
|
|
217
|
+
const retryAfter = res.headers.get("retry-after");
|
|
218
|
+
const waitMs = retryAfter
|
|
219
|
+
? Math.max(1000, parseInt(retryAfter, 10) * 1000)
|
|
220
|
+
: INITIAL_BACKOFF_MS * Math.pow(2, attempt);
|
|
221
|
+
await sleep(waitMs);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
throw lastError;
|
|
225
|
+
}
|
|
226
|
+
throw lastError;
|
|
193
227
|
}
|
package/dist/repl.js
CHANGED