@hamp10/agentforge 0.2.2 → 0.2.4
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/OllamaAgent.js +53 -0
package/package.json
CHANGED
package/src/OllamaAgent.js
CHANGED
|
@@ -203,6 +203,7 @@ export class OllamaAgent extends EventEmitter {
|
|
|
203
203
|
messages.push(userMessage);
|
|
204
204
|
|
|
205
205
|
let finalContent = '';
|
|
206
|
+
let allOutput = ''; // accumulate everything streamed across all turns
|
|
206
207
|
const MAX_TURNS = 25;
|
|
207
208
|
|
|
208
209
|
for (let turn = 0; turn < MAX_TURNS; turn++) {
|
|
@@ -316,6 +317,7 @@ export class OllamaAgent extends EventEmitter {
|
|
|
316
317
|
thinkBuffer = inThinkBlock ? thinkBuffer.slice(thinkBuffer.lastIndexOf('<think>')) : '';
|
|
317
318
|
|
|
318
319
|
streamContent += out;
|
|
320
|
+
allOutput += out;
|
|
319
321
|
if (out) {
|
|
320
322
|
this.emit('agent_output', { agentId, output: out });
|
|
321
323
|
}
|
|
@@ -397,6 +399,57 @@ export class OllamaAgent extends EventEmitter {
|
|
|
397
399
|
finalContent = streamContent;
|
|
398
400
|
}
|
|
399
401
|
break;
|
|
402
|
+
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Use all accumulated output if final turn had no content (agent ended after tool calls)
|
|
406
|
+
if (!finalContent && allOutput) {
|
|
407
|
+
finalContent = allOutput;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// If still no output (model did only tool calls, never wrote text), ask for a summary
|
|
411
|
+
if (!finalContent && !controller.signal.aborted) {
|
|
412
|
+
this.emit('agent_output', { agentId, output: '\n' });
|
|
413
|
+
messages.push({ role: 'user', content: 'Summarize what you just did and show me the result.' });
|
|
414
|
+
|
|
415
|
+
try {
|
|
416
|
+
const summaryRes = await fetch(`${this.baseUrl}/v1/chat/completions`, {
|
|
417
|
+
method: 'POST',
|
|
418
|
+
headers: { 'Content-Type': 'application/json' },
|
|
419
|
+
signal: controller.signal,
|
|
420
|
+
body: JSON.stringify({
|
|
421
|
+
model: this.model,
|
|
422
|
+
messages,
|
|
423
|
+
stream: true,
|
|
424
|
+
...(isQwen3 ? { options: { think: false } } : {})
|
|
425
|
+
})
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
if (summaryRes.ok) {
|
|
429
|
+
const reader = summaryRes.body.getReader();
|
|
430
|
+
const decoder = new TextDecoder();
|
|
431
|
+
let buf = '';
|
|
432
|
+
while (true) {
|
|
433
|
+
const { done, value } = await reader.read();
|
|
434
|
+
if (done) break;
|
|
435
|
+
buf += decoder.decode(value, { stream: true });
|
|
436
|
+
const lines = buf.split('\n');
|
|
437
|
+
buf = lines.pop();
|
|
438
|
+
for (const line of lines) {
|
|
439
|
+
if (!line.startsWith('data: ')) continue;
|
|
440
|
+
const payload = line.slice(6).trim();
|
|
441
|
+
if (payload === '[DONE]') continue;
|
|
442
|
+
let evt;
|
|
443
|
+
try { evt = JSON.parse(payload); } catch { continue; }
|
|
444
|
+
const text = evt.choices?.[0]?.delta?.content;
|
|
445
|
+
if (text) {
|
|
446
|
+
finalContent += text;
|
|
447
|
+
this.emit('agent_output', { agentId, output: text });
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
} catch {}
|
|
400
453
|
}
|
|
401
454
|
|
|
402
455
|
// Persist history for next task
|