From 639448ca0908b1cb4120195047c2b7b68eba85a4 Mon Sep 17 00:00:00 2001 From: A4F54B Date: Wed, 22 May 2024 22:51:14 +0800 Subject: [PATCH 1/2] fix(accumulateDelta): AssistantStream accumulateDelta toolCall (#771) --- src/lib/AssistantStream.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/AssistantStream.ts b/src/lib/AssistantStream.ts index 32cde3e7a..3c94cc3de 100644 --- a/src/lib/AssistantStream.ts +++ b/src/lib/AssistantStream.ts @@ -683,6 +683,20 @@ export class AssistantStream if (accValue.every((x) => typeof x === 'string' || typeof x === 'number')) { accValue.push(...deltaValue); // Use spread syntax for efficient addition continue; + } else if (accValue.every((x) => Core.isObj(x))) { + deltaValue.forEach(item => { + if (item.hasOwnProperty('index')) { + const obj = accValue.find((x: any) => x['index'] === item['index']); + if (obj) { + accValue[accValue.indexOf(obj)] = this.accumulateDelta(obj, item); + } else { + accValue.push(item); + } + } else { + accValue.push(item); + } + }); + continue; } } else { throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`); From 4c52a8f1aaa35471a7f97ef74c97175a4a2909d8 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Tue, 3 Sep 2024 14:09:02 +0100 Subject: [PATCH 2/2] minor style changes --- src/lib/AssistantStream.ts | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/lib/AssistantStream.ts b/src/lib/AssistantStream.ts index 3c94cc3de..7c5ffb58e 100644 --- a/src/lib/AssistantStream.ts +++ b/src/lib/AssistantStream.ts @@ -683,21 +683,31 @@ export class AssistantStream if (accValue.every((x) => typeof x === 'string' || typeof x === 'number')) { accValue.push(...deltaValue); // Use spread syntax for efficient addition continue; - } else if (accValue.every((x) => Core.isObj(x))) { - deltaValue.forEach(item => { - if (item.hasOwnProperty('index')) { - const obj = accValue.find((x: any) => x['index'] === item['index']); - if (obj) { - accValue[accValue.indexOf(obj)] = this.accumulateDelta(obj, item); - } else { - accValue.push(item); - } - } else { - accValue.push(item); - } - }); - 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}`); }