toolcall

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package toolcall provides LLM tool call argument reassembly from streaming chunks. Handles incremental tool_calls[i].function.arguments fragments from OpenAI-compatible APIs (OpenAI, DeepSeek, GLM, Kimi, Qwen, Grok, Doubao, Gemini) and produces complete, validated ToolCall objects.

Designed for extraction to github.com/brightman-ai/kit/toolcall.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assembler

type Assembler struct {
	// contains filtered or unexported fields
}

Assembler accumulates incremental tool call deltas by index and produces complete ToolCall objects. It handles:

  • Split function.name across deltas (rare but spec-legal)
  • Incremental function.arguments concatenation (the common case)
  • Parallel tool calls with different indices
  • Out-of-order delta delivery (by index)

Usage:

a := toolcall.NewAssembler()
for chunk := range stream {
    for _, delta := range chunk.ToolCalls {
        a.Feed(delta)
    }
}
completed := a.Complete()

func NewAssembler

func NewAssembler() *Assembler

NewAssembler creates a new tool call assembler.

func (*Assembler) Complete

func (a *Assembler) Complete() []ToolCall

Complete returns all accumulated tool calls sorted by index. Call this after the stream ends (finish_reason = "tool_calls" or "stop"). Only entries with a non-empty function name are returned.

func (*Assembler) Feed

func (a *Assembler) Feed(d Delta)

Feed processes a single tool call delta. Call this for each delta in each streaming chunk's tool_calls array.

func (*Assembler) Pending

func (a *Assembler) Pending() int

Pending returns the number of in-progress tool call entries.

func (*Assembler) Reset

func (a *Assembler) Reset()

Reset clears all accumulated state. Use between LLM call rounds in a tool loop.

type Delta

type Delta struct {
	Index    int    `json:"index"`
	ID       string `json:"id,omitempty"`
	Type     string `json:"type,omitempty"`
	Function struct {
		Name      string `json:"name,omitempty"`
		Arguments string `json:"arguments,omitempty"` // incremental JSON fragment
	} `json:"function,omitempty"`
}

Delta represents an incremental tool call fragment from a streaming LLM response. In OpenAI-compatible APIs, tool_calls arrive as deltas:

  • First delta for an index: carries ID + function.name (+ possibly partial arguments)
  • Subsequent deltas: carry only function.arguments fragments to concatenate

type ToolCall

type ToolCall struct {
	Index    int              `json:"index"`
	ID       string           `json:"id"`
	Type     string           `json:"type"`
	Function ToolCallFunction `json:"function"`
}

ToolCall is a fully reassembled tool call with complete arguments.

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // complete JSON string
}

ToolCallFunction holds the function name and complete arguments JSON.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL