Vendored
+45
-26
@@ -396,6 +396,7 @@ async function complete(opts, onEvent) {
|
||||
|
||||
let text = '';
|
||||
let thinking = '';
|
||||
let thinkState = { inThink: false, carry: '' };
|
||||
const toolCalls = [];
|
||||
const abortRun = () => {
|
||||
try {
|
||||
@@ -420,39 +421,57 @@ async function complete(opts, onEvent) {
|
||||
const n = events.normalizeCompletionEvent(ev);
|
||||
if (!n) continue;
|
||||
watch.bump();
|
||||
if (n.type === 'contentDelta') {
|
||||
text += n.delta;
|
||||
if (onEvent) onEvent(n);
|
||||
if (repeatLoop.isRepeating(text)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
const split = events.expandThinkEvents(n, thinkState);
|
||||
thinkState = split.state;
|
||||
for (const p of split.events) {
|
||||
if (p.type === 'contentDelta') {
|
||||
text += p.delta;
|
||||
if (onEvent) onEvent(p);
|
||||
if (repeatLoop.isRepeating(text)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
}
|
||||
} else if (p.type === 'thinkingDelta') {
|
||||
thinking += p.delta;
|
||||
if (onEvent) onEvent(p);
|
||||
if (repeatLoop.isRepeating(thinking)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
}
|
||||
} else if (p.type === 'toolCall') {
|
||||
toolCalls.push(p.call);
|
||||
if (onEvent) onEvent(p);
|
||||
} else if (onEvent) {
|
||||
onEvent(p);
|
||||
}
|
||||
} else if (n.type === 'thinkingDelta') {
|
||||
thinking += n.delta;
|
||||
if (onEvent) onEvent(n);
|
||||
if (repeatLoop.isRepeating(thinking)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
}
|
||||
} else if (n.type === 'toolCall') {
|
||||
toolCalls.push(n.call);
|
||||
if (onEvent) onEvent(n);
|
||||
} else if (onEvent) {
|
||||
onEvent(n);
|
||||
}
|
||||
}
|
||||
} else if (run.tokenStream) {
|
||||
for await (const token of run.tokenStream) {
|
||||
if (watch.timedOut()) return;
|
||||
watch.bump();
|
||||
text += token;
|
||||
if (onEvent) onEvent({ type: 'contentDelta', delta: token });
|
||||
if (repeatLoop.isRepeating(text)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
const split = events.expandThinkEvents({ type: 'contentDelta', delta: token }, thinkState);
|
||||
thinkState = split.state;
|
||||
for (const p of split.events) {
|
||||
if (p.type === 'thinkingDelta') {
|
||||
thinking += p.delta;
|
||||
if (onEvent) onEvent(p);
|
||||
if (repeatLoop.isRepeating(thinking)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
text += p.delta;
|
||||
if (onEvent) onEvent({ type: 'contentDelta', delta: p.delta });
|
||||
if (repeatLoop.isRepeating(text)) {
|
||||
abortRun();
|
||||
settleTimeout();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (run.toolCallStream) {
|
||||
|
||||
Reference in New Issue
Block a user