tool-call-parser.ts 802 B

1234567891011121314151617181920212223242526
  1. import type { ToolCallDelta } from "./types"
  2. import type { ToolCall } from "../llm-providers"
  3. export function accumulateToolCalls(deltas: ToolCallDelta[]): ToolCall[] {
  4. const groups = new Map<number, { id: string; name: string; argsChunks: string[] }>()
  5. for (const delta of deltas) {
  6. const group = groups.get(delta.index) || { id: "", name: "", argsChunks: [] }
  7. if (delta.id) group.id = delta.id
  8. if (delta.name) group.name = delta.name
  9. if (delta.arguments) group.argsChunks.push(delta.arguments)
  10. groups.set(delta.index, group)
  11. }
  12. return Array.from(groups.values()).map((g) => {
  13. const argsStr = g.argsChunks.join("")
  14. return {
  15. id: g.id,
  16. type: "function" as const,
  17. function: {
  18. name: g.name,
  19. arguments: argsStr,
  20. },
  21. }
  22. })
  23. }