MingaEditor.Agent.BufferSync (Minga v0.1.0)

Copy Markdown View Source

Syncs agent conversation messages into a *Agent* Buffer.

Converts the session's message list into markdown text and writes it into the buffer. The buffer provides a vim-navigable view of the conversation, with tree-sitter markdown highlighting.

Summary

Types

Line type for buffer-line-to-message mapping.

Functions

Builds a per-buffer-line index mapping buffer lines to message indices and types.

Returns the buffer line number where the given message index starts.

Starts the *Agent* buffer.

Syncs messages into the agent buffer as markdown text.

Types

line_type()

@type line_type() :: :text | :code | :tool | :thinking | :usage | :system | :empty

Line type for buffer-line-to-message mapping.

Functions

line_message_index(messages)

@spec line_message_index([term()]) :: [{non_neg_integer(), line_type()}]

Builds a per-buffer-line index mapping buffer lines to message indices and types.

Returns a list where each element corresponds to a buffer line and contains {message_index, line_type}. The list is indexed by buffer line number.

Prefer reading the cached index from UIState.cached_line_index when available (populated by sync/3). This function is the fallback for when the cache is empty (e.g., before the first sync).

message_start_line(messages, msg_idx)

@spec message_start_line([term()], non_neg_integer()) :: non_neg_integer() | nil

Returns the buffer line number where the given message index starts.

Returns nil if the message index is not found in the current layout.

start_buffer()

@spec start_buffer() :: pid() | nil

Starts the *Agent* buffer.

Returns the buffer pid. The buffer is nofile (no save), read-only (no user edits), unlisted (hidden from buffer picker), and persistent (survives buffer kill).

sync(pid, messages, opts \\ [])

@spec sync(pid(), [term()], keyword()) :: [{non_neg_integer(), line_type()}]

Syncs messages into the agent buffer as markdown text.

Each message is rendered as a markdown section with a header. The buffer cursor is moved to the end (auto-scroll).

Returns the line-to-message index computed from the synced content, so the caller can cache it in state for later lookups without recomputing.