@lvx74/openrrouter-ai-agent 1.0.11 β†’ 1.0.14

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.11",
3
+ "version": "1.0.14",
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",
package/src/Agent.js CHANGED
@@ -165,7 +165,8 @@ export class Agent extends EventEmitter {
165
165
  const { name, arguments: argsStr } = toolCall.function
166
166
 
167
167
  try {
168
- const args = JSON.parse(argsStr)
168
+ console.log(`πŸ”§ Esecuzione tool: ${name} con args:`, argsStr)
169
+ const args = argsStr.trim() ? JSON.parse(argsStr.trim()) : {}
169
170
  const tool = this.toolMap.get(name)
170
171
 
171
172
  if (!tool) {
@@ -187,7 +188,7 @@ export class Agent extends EventEmitter {
187
188
  continue
188
189
  }
189
190
 
190
- console.log(`⚑ Eseguendo tool: ${name}...`)
191
+ console.log(`⚑ Eseguendo tool: ${name}(${JSON.stringify(args)})`)
191
192
  const startTime = Date.now()
192
193
  const result = await tool.execute(args, this.session)
193
194
 
@@ -217,6 +218,7 @@ export class Agent extends EventEmitter {
217
218
  } catch (error) {
218
219
  const errorMsg = `Errore nell'esecuzione di ${name}: ${error.message}`
219
220
  console.log(`❌ Tool Error: ${errorMsg}`)
221
+ console.log(error)
220
222
 
221
223
  const toolMessage = {
222
224
  role: 'tool',
package/src/cli.js CHANGED
@@ -97,33 +97,40 @@ export function createChatInterface(agent, options = {}) {
97
97
  console.log(' β€’ /help - Mostra questo aiuto')
98
98
  }
99
99
 
100
+ let isRunning = false;
101
+
100
102
  // Main input handler
101
103
  async function handleInput(input) {
102
-
104
+ if (isRunning) {
105
+ console.log('\n⏳ Attendi che la risposta precedente sia completata...');
106
+ return;
107
+ }
103
108
  if (handleSpecialCommands(input)) {
104
- return
109
+ return;
105
110
  }
106
-
107
111
  if (input.trim() === '') {
108
- return
112
+ return;
109
113
  }
110
-
111
114
  try {
112
- const result = await agent.run(input)
113
- console.log(`\nπŸ€– ${assistantName}: ${result.content}`)
115
+ isRunning = true;
116
+ const result = await agent.run(input);
117
+ console.log(`\nπŸ€– ${assistantName}: ${result.content}`);
118
+ rl.prompt()
114
119
  if (historyFile) {
115
- saveHistory(agent, historyFile)
116
- console.log(`\nπŸ’Ύ Cronologia salvata in ${historyFile}`)
120
+ saveHistory(agent, historyFile);
121
+ // console.log(`\nπŸ’Ύ Cronologia salvata in ${historyFile}`);
117
122
  }
118
123
  } catch (error) {
119
- console.error('\n❌ Errore:', error.message)
124
+ console.error('\n❌ Errore:', error.message);
125
+ } finally {
126
+ isRunning = false;
120
127
  }
121
128
  }
122
129
 
123
130
  // Event listeners
124
131
  rl.on('line', async (input) => {
125
132
  await handleInput(input)
126
- rl.prompt()
133
+
127
134
  })
128
135
 
129
136
  rl.on('close', () => {
@@ -14,6 +14,9 @@ export async function callAI(prompt, temperature = 0.7, model = process.env.MODE
14
14
  if(res.error) {
15
15
  throw new Error(`OpenAI API error: ${res.error.message}`);
16
16
  }
17
+ //console.log('AI response:', res);
18
+ const {usage} = res;
19
+ console.log('Usage:', usage);
17
20
  const msg = res.choices[0].text.trim();
18
21
  return msg
19
22
  } catch (error) {
package/src/lib/utils.js CHANGED
@@ -8,7 +8,7 @@ const __dirname = dirname(__filename);
8
8
 
9
9
  export async function parseJSON(content, tryAgain = true, logger = console) {
10
10
 
11
- logger.log('πŸ” parseJSON - Contenuto ricevuto (primi 200 caratteri):', content.substring(0, 200));
11
+ //logger.log('πŸ” parseJSON - Contenuto ricevuto (primi 200 caratteri):', content.substring(0, 200));
12
12
 
13
13
  try {
14
14
  // Tenta di trovare il primo blocco JSON valido
@@ -18,10 +18,17 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
18
18
  content = content.slice(0, -1);
19
19
  }
20
20
 
21
- const start = Math.min(content.indexOf('{'), content.indexOf('['));
22
- const end = Math.max(content.lastIndexOf('}'), content.lastIndexOf(']'));
23
21
 
24
- logger.log('πŸ” parseJSON - Posizioni JSON:', { start, end, contentLength: content.length });
22
+
23
+ // Trova la posizione del primo '{' o '['
24
+ const startMatch = content.match(/[\{\[]/);
25
+ const start = startMatch ? startMatch.index : -1;
26
+
27
+ // Trova la posizione dell'ultimo '}' o ']'
28
+ const endMatch = [...content.matchAll(/[\}\]]/g)].pop();
29
+ const end = endMatch ? endMatch.index : -1;
30
+
31
+ //logger.log('πŸ” parseJSON - Posizioni JSON:', { start, end, contentLength: content.length });
25
32
 
26
33
  if (start === -1 || end === -1) {
27
34
  console.error('❌ Nessun blocco JSON trovato nel contenuto');
@@ -30,7 +37,7 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
30
37
  }
31
38
 
32
39
  let jsonString = content.substring(start, end + 1);
33
- logger.log('πŸ” parseJSON - JSON estratto (primi 200 caratteri):', jsonString.substring(0, 200));
40
+ //logger.log('πŸ” parseJSON - JSON estratto (primi 200 caratteri):', jsonString.substring(0, 200));
34
41
 
35
42
  // Escape virgolette interne per evitare crash
36
43
  jsonString = jsonString.replace(/:\s*"([^"]*?)"(?=\s*,|\s*})/g, (match, group) => {
@@ -40,7 +47,7 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
40
47
 
41
48
  try {
42
49
  const parsed = JSON.parse(jsonString);
43
- logger.log('βœ… parseJSON - JSON parsato con successo');
50
+ //logger.log('βœ… parseJSON - JSON parsato con successo');
44
51
  return parsed;
45
52
  } catch (error) {
46
53
  if (!tryAgain) {
@@ -1,6 +1,7 @@
1
1
  Sei un assistente AI incaricato di riassumere la conversazione tra utente e assistente.
2
2
  Riceverai una lista di messaggi in formato JSON (con campi role e content).
3
3
  Il tuo compito Γ¨ comprimere la history mantenendo solo le informazioni essenziali, eliminando ridondanze e dettagli superflui, ma preservando il senso e il contesto della conversazione.
4
+ Cerca di comprimere il JSON risultante in circa 20 elementi (messaggi), facendo il merge dei messaggi dove possibile per ridurre la lunghezza mantenendo il senso e il contesto.
4
5
  Restituisci sempre e solo un JSON valido, con la stessa struttura (array di messaggi, ciascuno con role e content), pronto per essere usato come nuova history.
5
6
  Non aggiungere testo fuori dal JSON, nΓ© spiegazioni.
6
7
  Se necessario, accorpa piΓΉ messaggi simili in uno solo, mantenendo la coerenza.