chat

package
v1.3.39 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const ToolBodyMaxLines = 10

ToolBodyMaxLines is the maximum number of body lines shown before truncation.

Variables

This section is empty.

Functions

func FormatBody

func FormatBody(content string, width int, maxLines int) (string, bool)

FormatBody renders tool body content with optional truncation. For long output, shows the last maxLines lines (users care about the end). Returns the formatted body and whether it was truncated.

func FormatJSONResult

func FormatJSONResult(raw string) string

FormatJSONResult parses a JSON string and renders it as human-readable key-value pairs.

func PrettifyToolName

func PrettifyToolName(name string) string

PrettifyToolName converts internal tool names to display names. e.g. "run_command" → "Bash", "read_file" → "Read", "search_files" → "Grep"

Types

type AgentToolItem

type AgentToolItem struct {
	CachedItem
	// contains filtered or unexported fields
}

AgentToolItem renders a subagent with nested tool calls. AgentToolItem displays a sub-agent's lifecycle in the main chat. The label field holds the sub-agent's name (from description param).

func NewAgentToolItem

func NewAgentToolItem(id, label string, status ToolStatus, styles Styles) *AgentToolItem

NewAgentToolItem creates a new agent tool item. The label parameter is the sub-agent name shown in the header.

func (*AgentToolItem) AppendNested

func (a *AgentToolItem) AppendNested(item Item)

AppendNested adds a nested tool item.

func (*AgentToolItem) Height

func (a *AgentToolItem) Height(width int) int

func (*AgentToolItem) ID

func (a *AgentToolItem) ID() string

func (*AgentToolItem) Label added in v1.3.22

func (a *AgentToolItem) Label() string

func (*AgentToolItem) Render

func (a *AgentToolItem) Render(width int) string

func (*AgentToolItem) Result added in v1.3.22

func (a *AgentToolItem) Result() string

func (*AgentToolItem) SetResult

func (a *AgentToolItem) SetResult(result string)

SetResult updates the agent result.

func (*AgentToolItem) SetStatus

func (a *AgentToolItem) SetStatus(s ToolStatus)

SetStatus updates the agent status.

func (*AgentToolItem) Status added in v1.3.22

func (a *AgentToolItem) Status() ToolStatus

func (*AgentToolItem) UpdateNested

func (a *AgentToolItem) UpdateNested(id string, item Item)

UpdateNested updates a nested item by ID.

type AssistantItem

type AssistantItem struct {
	CachedItem
	// contains filtered or unexported fields
}

AssistantItem renders an assistant message (supports streaming).

func NewAssistantItem

func NewAssistantItem(id string, styles Styles) *AssistantItem

NewAssistantItem creates a new assistant message item.

func (*AssistantItem) Height

func (a *AssistantItem) Height(width int) int

func (*AssistantItem) ID

func (a *AssistantItem) ID() string

func (*AssistantItem) Reasoning added in v1.3.7

func (a *AssistantItem) Reasoning() string

Reasoning returns the current reasoning content.

func (*AssistantItem) Render

func (a *AssistantItem) Render(width int) string

func (*AssistantItem) SetFinished

func (a *AssistantItem) SetFinished()

SetFinished marks the assistant as done streaming.

func (*AssistantItem) SetReasoning added in v1.3.7

func (a *AssistantItem) SetReasoning(text string)

SetReasoning updates the reasoning/thinking content.

func (*AssistantItem) SetReasoningFinished added in v1.3.7

func (a *AssistantItem) SetReasoningFinished()

SetReasoningFinished collapses reasoning into a one-line summary.

func (*AssistantItem) SetText

func (a *AssistantItem) SetText(text string)

SetText updates the assistant content (for streaming).

func (*AssistantItem) Text added in v1.3.22

func (a *AssistantItem) Text() string

type BaseToolItem

type BaseToolItem struct {
	CachedItem
	// contains filtered or unexported fields
}

BaseToolItem provides shared rendering logic for all tool items. Concrete tool types embed this and override RenderBody/RenderParams.

func NewBaseToolItem

func NewBaseToolItem(id, toolName string, status ToolStatus, input string, styles Styles) *BaseToolItem

NewBaseToolItem creates a base tool item.

func (*BaseToolItem) Height

func (t *BaseToolItem) Height(width int) int

func (*BaseToolItem) ID

func (t *BaseToolItem) ID() string

func (*BaseToolItem) Input

func (t *BaseToolItem) Input() string

Input returns the raw input JSON when available, otherwise the display detail.

func (*BaseToolItem) IsError added in v1.3.22

func (t *BaseToolItem) IsError() bool

IsError reports whether the current result is an error.

func (*BaseToolItem) Render

func (t *BaseToolItem) Render(width int) string

Render produces the full tool output: header + optional body. This is the base implementation. Concrete types should call renderCore with their own params/body overrides.

func (*BaseToolItem) RenderBody

func (t *BaseToolItem) RenderBody(width int) string

RenderBody renders the tool output body. Override in concrete types for specialized body rendering.

func (*BaseToolItem) RenderParams

func (t *BaseToolItem) RenderParams() string

RenderParams returns the display parameters for the tool header.

func (*BaseToolItem) Result added in v1.3.22

func (t *BaseToolItem) Result() string

Result returns the current rendered tool result body.

func (*BaseToolItem) SetResult

func (t *BaseToolItem) SetResult(result string, isError bool)

SetResult updates the tool result and invalidates cache.

func (*BaseToolItem) SetStatus

func (t *BaseToolItem) SetStatus(s ToolStatus)

SetStatus updates the tool status and invalidates cache.

func (*BaseToolItem) Status

func (t *BaseToolItem) Status() ToolStatus

Status returns the current tool status.

func (*BaseToolItem) ToolName

func (t *BaseToolItem) ToolName() string

ToolName returns the original tool name when available.

type BashToolItem

type BashToolItem struct {
	BaseToolItem
}

BashToolItem renders bash command execution. BashToolItem renders bash command execution.

func NewBashToolItem

func NewBashToolItem(id, displayName, command string, status ToolStatus, styles Styles) *BashToolItem

type CachedItem

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

CachedItem provides common caching for items whose rendering is expensive. Embed in concrete item types and call GetCached/SetCached/Invalidate.

func (*CachedItem) GetCached

func (c *CachedItem) GetCached(width int) (string, int, bool)

GetCached returns the cached render and height if the width matches. Returns ("", 0, false) on cache miss.

func (*CachedItem) Invalidate

func (c *CachedItem) Invalidate()

Invalidate clears the cache, forcing re-render on next access.

func (*CachedItem) SetCached

func (c *CachedItem) SetCached(rendered string, width, height int)

SetCached stores the rendered output and its height.

type CmdToolItem

type CmdToolItem struct {
	BaseToolItem
}

CmdToolItem renders background command lifecycle operations. The detail string comes pre-formatted from describeTool, e.g.:

  • start_command: "go build ./..."
  • write_command_input: "[abc12345] → y\n" (input text, most important)
  • read_command_output: "abc12345"
  • wait_command: "abc12345 (30s)"
  • stop_command: "abc12345"
  • list_commands: "" (no params)

type FileToolItem

type FileToolItem struct {
	BaseToolItem
}

FileToolItem renders file read/write/edit operations.

func NewFileToolItem

func NewFileToolItem(id, displayName, filePath string, status ToolStatus, styles Styles, lang string, rawArgs string, toolName string) *FileToolItem

NewFileToolItem creates a new file operation tool item.

type GenericToolItem

type GenericToolItem struct {
	BaseToolItem
}

GenericToolItem is a fallback for unrecognized tools.

func NewGenericToolItem

func NewGenericToolItem(id, displayName string, status ToolStatus, detail string, styles Styles) *GenericToolItem

NewGenericToolItem creates a generic tool item.

func NewMarkdownToolItem

func NewMarkdownToolItem(id, displayName string, status ToolStatus, detail string, styles Styles) *GenericToolItem

NewMarkdownToolItem creates a tool item that renders its result as markdown.

type GitToolItem

type GitToolItem struct {
	BaseToolItem
}

GitToolItem renders git operations with command-output body.

type Item

type Item interface {
	// Render produces the ANSI-styled string for this item at the given width.
	Render(width int) string

	// ID returns a unique identifier for deduplication and scroll targeting.
	ID() string

	// Height returns the number of visual lines this item occupies at the given width.
	// Used by the virtual list to compute scroll positions without full rendering.
	Height(width int) int
}

Item is the core interface for any renderable element in the conversation.

func NewToolItem

func NewToolItem(id string, ctx ToolContext, status ToolStatus, styles Styles) Item

NewToolItem creates the appropriate tool item type based on the ToolContext. The caller (chatStartTool) is responsible for filling Detail from describeTool; NewToolItem uses it directly instead of re-parsing RawArgs.

type List

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

List is a virtual-scrolling list of Items. Only items visible in the current viewport are rendered.

func NewList

func NewList(width, height int) *List

NewList creates a new virtual list with the given dimensions.

func (*List) Append

func (l *List) Append(items ...Item)

Append adds items to the end of the list.

func (*List) AtBottom

func (l *List) AtBottom() bool

AtBottom returns whether the viewport is at the bottom.

func (*List) FindByID

func (l *List) FindByID(id string) Item

FindByID returns the item with the given ID, or nil.

func (*List) Follow

func (l *List) Follow() bool

Follow returns whether auto-scroll is active.

func (*List) Height

func (l *List) Height() int

Height returns the viewport height.

func (*List) ItemAt

func (l *List) ItemAt(idx int) Item

ItemAt returns the item at the given index, or nil.

func (*List) Len

func (l *List) Len() int

Len returns the number of items.

func (*List) RemoveByID added in v1.1.90

func (l *List) RemoveByID(id string)

RemoveByID removes an item by ID. No-op if not found.

func (*List) Render

func (l *List) Render() string

Render produces the visible portion of the list as a single string. Only items within the viewport are rendered. When follow is active, scroll position is re-synced automatically.

func (*List) ScrollDown

func (l *List) ScrollDown(n int)

ScrollDown scrolls the viewport by n lines.

func (*List) ScrollToEnd

func (l *List) ScrollToEnd()

ScrollToEnd scrolls to the very bottom and re-enables follow.

func (*List) ScrollUp

func (l *List) ScrollUp(n int)

ScrollUp scrolls the viewport up by n lines.

func (*List) SetFollow

func (l *List) SetFollow(f bool)

SetFollow enables or disables auto-scroll to bottom.

func (*List) SetItems

func (l *List) SetItems(items []Item)

SetItems replaces all items.

func (*List) SetSize

func (l *List) SetSize(width, height int)

SetSize updates the viewport dimensions.

func (*List) UpdateItem

func (l *List) UpdateItem(id string, item Item)

UpdateItem replaces the item with the given ID.

func (*List) YOffset

func (l *List) YOffset() int

YOffset returns the current scroll position as a line offset from the top.

type ListToolItem

type ListToolItem struct {
	BaseToolItem
}

ListToolItem renders directory listing operations.

type LspToolItem

type LspToolItem struct {
	BaseToolItem
}

LspToolItem renders LSP operations (hover, definition, references, etc.)

type SearchToolItem

type SearchToolItem struct {
	BaseToolItem
}

SearchToolItem renders grep/glob/ls operations.

func NewSearchToolItem

func NewSearchToolItem(id, displayName, pattern string, status ToolStatus, styles Styles) *SearchToolItem

NewSearchToolItem creates a new search tool item.

type SpacerItem

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

SpacerItem adds vertical space between message groups.

func NewSpacerItem

func NewSpacerItem(height int) *SpacerItem

NewSpacerItem creates a spacer with the given height in lines.

func (*SpacerItem) Height

func (s *SpacerItem) Height(width int) int

func (*SpacerItem) ID

func (s *SpacerItem) ID() string

func (*SpacerItem) Render

func (s *SpacerItem) Render(width int) string

type Styles

type Styles struct {
	// User message
	UserPrefix string
	UserIcon   string
	UserStyle  lipgloss.Style

	// Assistant message
	AssistantPrefix string
	AssistantIcon   string
	AssistantStyle  lipgloss.Style

	// Tool name rendering
	ToolName lipgloss.Style

	// Tool body
	ToolBody lipgloss.Style
	BashBody lipgloss.Style // command output with subtle background

	// System message
	SystemPrefix string
	SystemStyle  lipgloss.Style

	// Reasoning / thinking
	ReasoningPrefix string
	ReasoningStyle  lipgloss.Style

	// Error
	ErrorStyle lipgloss.Style

	// Muted
	MutedStyle lipgloss.Style

	// Spacing
	ItemGap int // lines between items
}

Styles holds all rendering styles for the chat package.

func DefaultStyles

func DefaultStyles() Styles

DefaultStyles returns the default style set.

func (Styles) ToolHeader

func (s Styles) ToolHeader(status ToolStatus, name string, width int, params ...string) string

func (Styles) ToolIcon

func (s Styles) ToolIcon(status ToolStatus) string

ToolIcon returns the icon for a given tool status.

func (Styles) ToolIconStyle

func (s Styles) ToolIconStyle(status ToolStatus) string

ToolIconStyle returns a styled icon for the given tool status.

type SystemItem

type SystemItem struct {
	CachedItem
	// contains filtered or unexported fields
}

SystemItem renders a system/status/info message.

func NewSystemItem

func NewSystemItem(id, text string, styles Styles) *SystemItem

NewSystemItem creates a new system message item.

func (*SystemItem) AppendText added in v1.2.7

func (s *SystemItem) AppendText(text string)

func (*SystemItem) Height

func (s *SystemItem) Height(width int) int

func (*SystemItem) ID

func (s *SystemItem) ID() string

func (*SystemItem) Render

func (s *SystemItem) Render(width int) string

func (*SystemItem) SetText added in v1.2.7

func (s *SystemItem) SetText(text string)

func (*SystemItem) Text added in v1.3.22

func (s *SystemItem) Text() string

type TodoTask

type TodoTask struct {
	ID      string
	Content string
	Status  string // "done", "in_progress", "pending"
}

TodoTask represents a single todo/task item.

type TodoToolItem

type TodoToolItem struct {
	CachedItem
	// contains filtered or unexported fields
}

TodoToolItem renders a todo/task list.

func NewTodoToolItem

func NewTodoToolItem(id string, tasks []TodoTask, styles Styles, lang string) *TodoToolItem

NewTodoToolItem creates a new todo list tool item.

func (*TodoToolItem) Height

func (t *TodoToolItem) Height(width int) int

func (*TodoToolItem) ID

func (t *TodoToolItem) ID() string

func (*TodoToolItem) Render

func (t *TodoToolItem) Render(width int) string

func (*TodoToolItem) SetTasks

func (t *TodoToolItem) SetTasks(tasks []TodoTask)

SetTasks updates the task list.

type ToolBodyBehavior

type ToolBodyBehavior int

NewBashToolItem creates a new bash tool item. ToolBodyBehavior describes how a tool's result body should be rendered.

const (
	BodyDefault    ToolBodyBehavior = iota // show result as-is (truncated)
	BodySuppress                           // hide body entirely
	BodyFormatJSON                         // parse JSON and render as key-value pairs
	BodyMarkdown                           // render result as markdown
)

func GetToolBodyBehavior

func GetToolBodyBehavior(toolName string) ToolBodyBehavior

GetToolBodyBehavior returns the body rendering behavior for a given tool name.

type ToolContext

type ToolContext struct {
	ToolName    string // original tool name, e.g. "run_command"
	DisplayName string // prettified name, e.g. "Bash"
	Detail      string // extracted detail, e.g. "go build ./..."
	RawArgs     string // raw JSON input (for body rendering / fallback)
	Lang        string // language code, e.g. "zh-CN", "en"
}

ToolContext carries pre-resolved display information from the caller (typically describeTool in the TUI layer). It is the primary data source for tool rendering — the caller is responsible for extracting the right detail from rawArgs, so NewToolItem no longer needs to do JSON parsing.

type ToolStatus

type ToolStatus int

ToolStatus represents the current state of a tool call.

const (
	StatusPending ToolStatus = iota
	StatusRunning
	StatusSuccess
	StatusError
	StatusCanceled
)

func (ToolStatus) String

func (s ToolStatus) String() string

String returns a human-readable status name.

type UserItem

type UserItem struct {
	CachedItem
	// contains filtered or unexported fields
}

UserItem renders a user message with a prefix icon.

func NewUserItem

func NewUserItem(id, text string, styles Styles) *UserItem

NewUserItem creates a new user message item.

func (*UserItem) Height

func (u *UserItem) Height(width int) int

func (*UserItem) ID

func (u *UserItem) ID() string

func (*UserItem) Prefix added in v1.3.23

func (u *UserItem) Prefix() string

func (*UserItem) Render

func (u *UserItem) Render(width int) string

func (*UserItem) SetPrefix added in v1.2.7

func (u *UserItem) SetPrefix(prefix string)

func (*UserItem) Text added in v1.3.22

func (u *UserItem) Text() string

type WebToolItem

type WebToolItem struct {
	BaseToolItem
}

WebToolItem renders web fetch/search operations.

Jump to

Keyboard shortcuts

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