@lvx74/openrrouter-ai-agent 1.0.14 → 1.0.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvx74/openrrouter-ai-agent",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "A powerful AI agent toolkit compatible with @openai/agents for building conversational AI with tool calling support using OpenRouter",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -5,26 +5,34 @@ export async function callAI(prompt, temperature = 0.7, model = process.env.MODE
5
5
  apiKey: process.env.OPENROUTER_API_KEY,
6
6
  baseURL: 'https://openrouter.ai/api/v1',
7
7
  })
8
- try {
9
- const res = await openai.chat.completions.create({
10
- model,
11
- prompt: `${systemPrompt}\n\nUser: ${prompt}`,
12
- temperature: temperature
13
- })
14
- if(res.error) {
15
- throw new Error(`OpenAI API error: ${res.error.message}`);
8
+ let lastError;
9
+ for (let attempt = 1; attempt <= retry; attempt++) {
10
+ try {
11
+ const res = await openai.chat.completions.create({
12
+ model,
13
+ prompt: `${systemPrompt}\n\nUser: ${prompt}`,
14
+ temperature: temperature
15
+ })
16
+ if(res.error) {
17
+ throw new Error(`OpenAI API error: ${res.error.message}`);
18
+ }
19
+ const {usage} = res;
20
+ console.log('Usage:', usage);
21
+ const msg = res.choices[0].text.trim();
22
+ if (!msg) {
23
+ throw new Error('Risposta vuota dal modello');
24
+ }
25
+ return msg
26
+ } catch (error) {
27
+ lastError = error;
28
+ if (error.response && error.response.data) {
29
+ console.error('API error response:', error.response.data);
30
+ }
31
+ console.error(`Tentativo ${attempt} fallito:`, error.message);
32
+ if (attempt < retry) {
33
+ await new Promise(r => setTimeout(r, 500 * attempt));
34
+ }
16
35
  }
17
- //console.log('AI response:', res);
18
- const {usage} = res;
19
- console.log('Usage:', usage);
20
- const msg = res.choices[0].text.trim();
21
- return msg
22
- } catch (error) {
23
- // Mostra il body della risposta se disponibile (es. 400)
24
- if (error.response && error.response.data) {
25
- console.error('API error response:', error.response.data);
26
- }
27
- console.error(error);
28
- throw error;
29
36
  }
37
+ throw lastError;
30
38
  }