Packages

An Elixir framework for building AI agents from OTP primitives. Weaves conversations, tool calls, and reasoning into coherent agents using GenServer, Task.Supervisor, and ETS.

Current section

Files

Jump to
loom_ex lib loom_ex llm sse_parser.ex
Raw

lib/loom_ex/llm/sse_parser.ex

defmodule LoomEx.LLM.SSEParser do
@spec new() :: binary()
def new, do: ""
@spec parse(binary(), binary()) :: {binary(), [map()]}
def parse(buffer, chunk) do
data = buffer <> chunk
parts = String.split(data, "\n\n")
{remaining, complete} = List.pop_at(parts, -1)
events =
complete
|> Enum.flat_map(fn segment ->
segment
|> String.split("\n")
|> Enum.flat_map(fn line ->
case String.trim_leading(line) do
"data: [DONE]" ->
[]
"data: " <> json ->
case Jason.decode(json) do
{:ok, decoded} -> [decoded]
_ -> []
end
_ ->
[]
end
end)
end)
{remaining || "", events}
end
end