UNPKG

openai-swarmjs

Version:

Agentic framework inspired from OpenAI's swarm framework for TS, JS

32 lines (31 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeFields = mergeFields; exports.mergeChunk = mergeChunk; function mergeFields(target, source) { for (const [key, value] of Object.entries(source)) { if (typeof value === 'string') { target[key] = (target[key] || '') + value; } else if (value !== null && typeof value === 'object') { target[key] = target[key] || {}; mergeFields(target[key], value); } } } function mergeChunk(finalResponse, delta) { delete delta.role; mergeFields(finalResponse, delta); const toolCalls = delta.toolCalls; if (toolCalls?.[0]) { const index = toolCalls[0].index; delete toolCalls[0].index; finalResponse.toolCalls = finalResponse.toolCalls || {}; finalResponse.toolCalls[index] = finalResponse.toolCalls[index] || { function: { arguments: '', name: '' }, id: '', type: '', }; mergeFields(finalResponse.toolCalls[index], toolCalls[0]); } }