@lvx74/openrrouter-ai-agent 1.0.10 โ 1.0.11
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/lib/utils.js +13 -8
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.11",
|
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/lib/utils.js
CHANGED
@@ -18,8 +18,8 @@ export async function parseJSON(content, tryAgain = true, logger = console) {
|
|
18
18
|
content = content.slice(0, -1);
|
19
19
|
}
|
20
20
|
|
21
|
-
const start = content.indexOf('{');
|
22
|
-
const end = content.lastIndexOf('}');
|
21
|
+
const start = Math.min(content.indexOf('{'), content.indexOf('['));
|
22
|
+
const end = Math.max(content.lastIndexOf('}'), content.lastIndexOf(']'));
|
23
23
|
|
24
24
|
logger.log('๐ parseJSON - Posizioni JSON:', { start, end, contentLength: content.length });
|
25
25
|
|
@@ -71,14 +71,19 @@ export async function checkAndCompressHistory(history) {
|
|
71
71
|
if (history.length > (process.env.MAX_HISTORY_LENGTH || 40)) {
|
72
72
|
console.log(`๐ Cronologia troppo lunga (${history.length} messaggi), compressione in corso...`);
|
73
73
|
const latest = history.slice(-4);
|
74
|
-
const data = history.slice(0, -4).filter(m=>m.role !== 'system').slice(-(process.env.MAX_HISTORY_LENGTH || 40));
|
74
|
+
const data = history.slice(0, -4).filter(m => m.role !== 'system').slice(-(process.env.MAX_HISTORY_LENGTH || 40));
|
75
75
|
const prompt = fs.readFileSync(join(__dirname, '../prompts/summarize_conversation_prompt.txt'), 'utf-8') + '\n' + JSON.stringify(data);
|
76
|
-
const compressed = await callAI(prompt, 0.2, process.env.COMPRESS_MODEL || 'deepseek/deepseek-chat-v3-0324:free'
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
const compressed = await callAI(prompt, 0.2, process.env.COMPRESS_MODEL || 'deepseek/deepseek-chat-v3-0324:free');
|
77
|
+
try {
|
78
|
+
const parsed = await parseJSON(compressed, true);
|
79
|
+
if (!parsed || typeof parsed !== 'object') {
|
80
|
+
throw new Error('La risposta compressa non รจ un oggetto JSON valido');
|
81
|
+
}
|
82
|
+
return [...parsed, ...latest];
|
83
|
+
} catch (error) {
|
84
|
+
console.error('โ Errore nel parsing della risposta compressa:', error.message);
|
85
|
+
return history.slice(0, -4).filter(m => m.role !== 'system').slice(-(process.env.MAX_HISTORY_LENGTH || 40))
|
80
86
|
}
|
81
|
-
return [...parsed, ...latest];
|
82
87
|
}
|
83
88
|
return history;
|
84
89
|
}
|