@lvx74/openrrouter-ai-agent 1.0.13 ā 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 +1 -1
- package/src/Agent.js +4 -2
- package/src/cli.js +18 -11
- package/src/lib/ai-client.js +3 -0
- package/src/lib/utils.js +12 -6
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lvx74/openrrouter-ai-agent",
|
3
|
-
"version": "1.0.
|
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
|
-
|
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
|
-
|
113
|
-
|
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
|
-
|
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
|
-
|
133
|
+
|
127
134
|
})
|
128
135
|
|
129
136
|
rl.on('close', () => {
|
package/src/lib/ai-client.js
CHANGED
@@ -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
|
@@ -20,9 +20,15 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
|
24
|
-
const
|
25
|
-
|
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 });
|
26
32
|
|
27
33
|
if (start === -1 || end === -1) {
|
28
34
|
console.error('ā Nessun blocco JSON trovato nel contenuto');
|
@@ -31,7 +37,7 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
|
|
31
37
|
}
|
32
38
|
|
33
39
|
let jsonString = content.substring(start, end + 1);
|
34
|
-
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));
|
35
41
|
|
36
42
|
// Escape virgolette interne per evitare crash
|
37
43
|
jsonString = jsonString.replace(/:\s*"([^"]*?)"(?=\s*,|\s*})/g, (match, group) => {
|
@@ -41,7 +47,7 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
|
|
41
47
|
|
42
48
|
try {
|
43
49
|
const parsed = JSON.parse(jsonString);
|
44
|
-
logger.log('ā
parseJSON - JSON parsato con successo');
|
50
|
+
//logger.log('ā
parseJSON - JSON parsato con successo');
|
45
51
|
return parsed;
|
46
52
|
} catch (error) {
|
47
53
|
if (!tryAgain) {
|