Skip to content

Commit

Permalink
fix(assistants): correctly accumulate tool calls when streaming (#1031)
Browse files Browse the repository at this point in the history
* fix(accumulateDelta): AssistantStream accumulateDelta toolCall (#771)

* minor style changes

---------

Co-authored-by: A4F54B <runesformempool@outlook.com>
  • Loading branch information
RobertCraigie and A4F54B authored Sep 3, 2024
1 parent 22ebdc2 commit d935ad3
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib/AssistantStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,30 @@ export class AssistantStream
accValue.push(...deltaValue); // Use spread syntax for efficient addition
continue;
}

for (const deltaEntry of deltaValue) {
if (!Core.isObj(deltaEntry)) {
throw new Error(`Expected array delta entry to be an object but got: ${deltaEntry}`);
}

const index = deltaEntry['index'];
if (index == null) {
console.error(deltaEntry);
throw new Error('Expected array delta entry to have an `index` property');
}

if (typeof index !== 'number') {
throw new Error(`Expected array delta entry \`index\` property to be a number but got ${index}`);
}

const accEntry = accValue[index];
if (accEntry == null) {
accValue.push(deltaEntry);
} else {
accValue[index] = this.accumulateDelta(accEntry, deltaEntry);
}
}
continue;
} else {
throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`);
}
Expand Down

0 comments on commit d935ad3

Please sign in to comment.