kiosapi 0.1.23 → 0.1.25
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/agent/run.js +42 -6
- package/package.json +1 -1
package/dist/agent/run.js
CHANGED
|
@@ -56,9 +56,32 @@ function trimContext(messages) {
|
|
|
56
56
|
const dropped = clean.length - tail.length;
|
|
57
57
|
if (dropped <= 0)
|
|
58
58
|
return [system, ...clean];
|
|
59
|
+
// Collect baca_file paths from the dropped messages so the model knows which files it
|
|
60
|
+
// already has context for — prevents it from re-orienting by re-reading config/schema files.
|
|
61
|
+
const droppedHead = clean.slice(0, clean.length - tail.length + skip);
|
|
62
|
+
const droppedPaths = [];
|
|
63
|
+
for (const msg of droppedHead) {
|
|
64
|
+
if (msg.role === 'assistant' && msg.tool_calls) {
|
|
65
|
+
for (const tc of msg.tool_calls) {
|
|
66
|
+
if (tc.function?.name === 'baca_file') {
|
|
67
|
+
try {
|
|
68
|
+
const a = JSON.parse(tc.function.arguments ?? '{}');
|
|
69
|
+
if (a.path)
|
|
70
|
+
droppedPaths.push(a.path);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
/* ignore */
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const filesNote = droppedPaths.length > 0
|
|
80
|
+
? ` File sudah dibaca (tak lagi di window): ${[...new Set(droppedPaths)].join(', ')}. JANGAN baca ulang — gunakan cari untuk mencari teks spesifik.`
|
|
81
|
+
: '';
|
|
59
82
|
const note = {
|
|
60
83
|
role: 'system',
|
|
61
|
-
content: `[Kiosapi: ${dropped} pesan awal dihapus dari konteks
|
|
84
|
+
content: `[Kiosapi: ${dropped} pesan awal dihapus dari konteks.${filesNote}]`,
|
|
62
85
|
};
|
|
63
86
|
return [system, note, ...tail];
|
|
64
87
|
}
|
|
@@ -452,7 +475,7 @@ export async function runTurn(s, userText) {
|
|
|
452
475
|
// other calls (daftar src, baca ...) appear between the repeating call.
|
|
453
476
|
const callCounts = new Map();
|
|
454
477
|
const lastSigs = []; // last 3 sigs for consecutive detection
|
|
455
|
-
const COUNT_LIMIT =
|
|
478
|
+
const COUNT_LIMIT = 6; // same tool+args called 6× total → loop (higher because cache now returns content)
|
|
456
479
|
const CONSEC_LIMIT = 3; // same sig 3× in a row → loop
|
|
457
480
|
// Read-only tool cache: on 2nd+ identical call, return the previous result with a warning
|
|
458
481
|
// instead of re-running. This gives the model early feedback so it can self-correct before
|
|
@@ -550,10 +573,23 @@ export async function runTurn(s, userText) {
|
|
|
550
573
|
if (count > 1 && READ_ONLY_TOOLS.has(call.function.name) && toolCache.has(sig)) {
|
|
551
574
|
const toolName = call.function.name;
|
|
552
575
|
const cachedOut = toolCache.get(sig) ?? '';
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
576
|
+
// Always include the cached content so the model has the data it needs even after
|
|
577
|
+
// trimContext drops old messages from its window. The escalating warning text
|
|
578
|
+
// discourages repetition; withholding the content at count=3+ only causes the model
|
|
579
|
+
// to keep retrying — counter-productive to stopping the loop.
|
|
580
|
+
const stopNote = count >= COUNT_LIMIT - 1
|
|
581
|
+
? `\n\n⚠ STOP (ke-${count}): JANGAN panggil "${toolName}" lagi dengan argumen yang sama. Gunakan tool "selesai" dan jelaskan kendalanya, atau gunakan cari/baca_file dengan path/range BERBEDA.`
|
|
582
|
+
: `\n\n⚠ Cache ke-${count}: "${toolName}" sudah dipanggil ${count}× dengan argumen sama. Gunakan cari atau baca_file(path, mulai=N) untuk bagian berbeda.`;
|
|
583
|
+
const warn = `[Cache ke-${count}]\n${cachedOut}${stopNote}`;
|
|
584
|
+
const cachePathHint = (() => {
|
|
585
|
+
try {
|
|
586
|
+
return ` ${JSON.parse(call.function.arguments).path ?? ''}`;
|
|
587
|
+
}
|
|
588
|
+
catch {
|
|
589
|
+
return '';
|
|
590
|
+
}
|
|
591
|
+
})();
|
|
592
|
+
console.log(dim(` ↩ ${toolName}${cachePathHint} (cache ke-${count})`));
|
|
557
593
|
s.messages.push({ role: 'tool', content: warn, tool_call_id: call.id });
|
|
558
594
|
continue;
|
|
559
595
|
}
|