Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d935ad3

Browse files
RobertCraigieA4F54B
and
A4F54B
authoredSep 3, 2024··
fix(assistants): correctly accumulate tool calls when streaming (#1031)
* fix(accumulateDelta): AssistantStream accumulateDelta toolCall (#771) * minor style changes --------- Co-authored-by: A4F54B <runesformempool@outlook.com>
1 parent 22ebdc2 commit d935ad3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎src/lib/AssistantStream.ts

+24
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,30 @@ export class AssistantStream
684684
accValue.push(...deltaValue); // Use spread syntax for efficient addition
685685
continue;
686686
}
687+
688+
for (const deltaEntry of deltaValue) {
689+
if (!Core.isObj(deltaEntry)) {
690+
throw new Error(`Expected array delta entry to be an object but got: ${deltaEntry}`);
691+
}
692+
693+
const index = deltaEntry['index'];
694+
if (index == null) {
695+
console.error(deltaEntry);
696+
throw new Error('Expected array delta entry to have an `index` property');
697+
}
698+
699+
if (typeof index !== 'number') {
700+
throw new Error(`Expected array delta entry \`index\` property to be a number but got ${index}`);
701+
}
702+
703+
const accEntry = accValue[index];
704+
if (accEntry == null) {
705+
accValue.push(deltaEntry);
706+
} else {
707+
accValue[index] = this.accumulateDelta(accEntry, deltaEntry);
708+
}
709+
}
710+
continue;
687711
} else {
688712
throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`);
689713
}

0 commit comments

Comments
 (0)
Please sign in to comment.