SkillKit.Agent.StreamAccumulator (SkillKit v0.4.0)

Copy Markdown View Source

Shared helpers for consuming an LLM event stream into an %AssistantMessage{}.

Used by SkillKit.Agent.Server for the main agent loop and by SkillKit.Agent.SkillActivation for the in-process skill sub-loop.

The accumulator is a plain map holding partial text, the running list of tool calls, and accumulated usage. new/0 builds an empty one and finalize/1 turns it into an %AssistantMessage{}.

Event-specific updates (text concatenation, tool-call collection, usage merging) are applied by the caller inside its own Enum.reduce/3, so each consumer can emit its own side effects (event notification, telemetry, agent tagging) without coupling to this module.

Summary

Functions

An all-zero usage map.

Finalizes the accumulator into an %AssistantMessage{}.

Sums a %Usage{} event into a running usage map.

Types

t()

@type t() :: %{
  text: String.t(),
  tool_calls: [SkillKit.Types.ToolCall.t()],
  usage: usage()
}

usage()

@type usage() :: %{
  input_tokens: non_neg_integer(),
  output_tokens: non_neg_integer(),
  cache_creation_input_tokens: non_neg_integer(),
  cache_read_input_tokens: non_neg_integer()
}

Functions

empty_usage()

@spec empty_usage() :: usage()

An all-zero usage map.

finalize(acc)

@spec finalize(t()) :: SkillKit.Types.AssistantMessage.t()

Finalizes the accumulator into an %AssistantMessage{}.

merge_usage(acc_usage, usage)

@spec merge_usage(usage(), SkillKit.Event.Usage.t()) :: usage()

Sums a %Usage{} event into a running usage map.

new()

@spec new() :: t()