chatui

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package chatui contains the reusable terminal chat controls used by suga and other clients. It deliberately depends only on public packages so hosts do not need access to suga's internal application layers.

Index

Constants

View Source
const (
	StreamTextDelta   = "text_delta"
	StreamMessageStop = "message_stop"
	StreamRetry       = "retry"
)

Variables

This section is empty.

Functions

func ApprovalPrompt

func ApprovalPrompt(tool, scopeDescription string, allowAlways bool) string

ApprovalPrompt builds the compact answer line. allowAlways is false for an unsafe shell command: a deny scope is still useful, but an allow scope must never bypass the command safety matcher. Without a translator it falls back to the Chinese texts.

func ApprovalPromptWithTranslator

func ApprovalPromptWithTranslator(t Translator, tool, scopeDescription string, allowAlways bool) string

ApprovalPromptWithTranslator builds the compact answer line with the injected host UI translator; the compatibility wrapper above keeps older callers in zh. It accepts the host's minimal translation contract (Translator).

func PersistPrompt

func PersistPrompt(action string) string

PersistPrompt is explicit about the safe default: an empty answer keeps the rule in the current session, while only u/p request a file writeback.

func PersistPromptWithTranslator

func PersistPromptWithTranslator(t Translator, action string) string

PersistPromptWithTranslator builds the rule writeback question using the injected translator while retaining the safe session-only default.

Types

type ApprovalReply

type ApprovalReply uint8

ApprovalReply is the decision made from the one-line answer to an approval question. The wiring layer turns the two persistent replies into session rules.

const (
	ReplyDeny ApprovalReply = iota
	ReplyAllow
	ReplyAllowAlways
	ReplyDenyAlways
)

func ParseApprovalReply

func ParseApprovalReply(answer string) ApprovalReply

ParseApprovalReply accepts the stable answer aliases used by chat. Empty and unknown answers stay deny, preserving the old fail-closed behavior.

type CodeBlock

type CodeBlock struct {
	Lang   string
	Lines  []string
	Folded int
}

CodeBlock is one fenced code block of a rendered message: the language of its fence, every line as the model wrote it and how many lines the fold limit hid (chat.code_max_lines). chat's :code command shows one of them again in full.

type LiveText

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

LiveText owns one model message's streamed text, line buffering and optional markdown rendering. It has no dependency on suga's engine or application layers.

func NewLiveText

func NewLiveText(w io.Writer) *LiveText

func NewPrefixedLiveText

func NewPrefixedLiveText(w io.Writer, prefix string) *LiveText

func (*LiveText) Answer

func (l *LiveText) Answer(text string)

func (*LiveText) CodeBlocks

func (l *LiveText) CodeBlocks() []CodeBlock

func (*LiveText) Finish

func (l *LiveText) Finish() bool

func (*LiveText) Handle

func (l *LiveText) Handle(event StreamEvent) bool

Handle writes deltas and closes a model message. It returns true for stream events.

func (*LiveText) SetCode

func (l *LiveText) SetCode(highlight bool, maxLines, width int)

func (*LiveText) SetMarkdown

func (l *LiveText) SetMarkdown(color bool)

func (*LiveText) SetMarkdownTheme

func (l *LiveText) SetMarkdownTheme(theme string)

func (*LiveText) SetTranslator

func (l *LiveText) SetTranslator(tr Translator)

func (*LiveText) SetWholeLines

func (l *LiveText) SetWholeLines(on bool)

type MDTerm

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

MDTerm renders markdown text line by line to a terminal writer. Zero value is not usable; call NewMDTerm.

func NewMDTerm

func NewMDTerm(w io.Writer, color bool) *MDTerm

NewMDTerm returns an MDTerm writing rendered lines to w; color styles them, with the dark theme's palette (SetTheme switches it).

func (*MDTerm) CodeBlocks

func (t *MDTerm) CodeBlocks() []CodeBlock

CodeBlocks returns the code blocks of the most recent message that had one, in the order they appeared (:code).

func (*MDTerm) Flush

func (t *MDTerm) Flush()

Flush renders the buffered, still incomplete line and ends the message: a code block left open does not carry into the next message.

func (*MDTerm) Print

func (t *MDTerm) Print(text string)

Print renders text that arrives as one piece (a final answer that was not streamed) and ends it on a line of its own. Empty text only ends the line its prefix was written on.

func (*MDTerm) SetCode

func (t *MDTerm) SetCode(highlight bool, maxLines int)

SetCode configures fenced code blocks: highlight colors them by their language (chat.code_highlight; it needs color) and maxLines folds a block after that many lines (chat.code_max_lines, 0: no fold).

func (*MDTerm) SetLinePrefix

func (t *MDTerm) SetLinePrefix(prefix string)

SetLinePrefix sets the text printed at the start of every message's first line ("" removes it).

func (*MDTerm) SetTheme

func (t *MDTerm) SetTheme(name string)

SetTheme switches the palette the elements are painted with (chat.theme, "dark" or "light"); an unknown name keeps the previous one.

func (*MDTerm) SetTranslator

func (t *MDTerm) SetTranslator(tr Translator)

SetTranslator installs the host UI catalog of the lines the renderer writes itself (the folded-code notice); nil keeps the historical Chinese text.

func (*MDTerm) SetWidth

func (t *MDTerm) SetWidth(width int)

SetWidth sets the terminal width used to leave a too wide table as it came (0: any width is accepted).

func (*MDTerm) Write

func (t *MDTerm) Write(text string)

Write takes the next chunk of markdown text and prints every line it completes; the rest waits in the buffer for its newline.

type PersistReply

type PersistReply uint8

PersistReply identifies the requested persistence destination. The wiring layer performs the writeback after the session rule has been installed in memory.

const (
	PersistSession PersistReply = iota
	PersistUser
	PersistProject
)

func ParsePersistReply

func ParsePersistReply(answer string) PersistReply

ParsePersistReply keeps Enter as the safe default and recognizes only the explicit destinations shown in the follow-up question.

type StreamEvent

type StreamEvent struct {
	Kind string
	Text string
}

StreamEvent is the smallest event contract needed by the streaming text area. Hosts adapt their engine-specific event type at the application boundary.

type Translator

type Translator interface {
	T(key string, args ...map[string]any) string
}

Translator is the minimal host translation contract used by chat rendering.

Directories

Path Synopsis
Package lineedit is a copy of the VT100 line editor (Terminal) from golang.org/x/term v0.29.0 (terminal.go), made display-width aware.
Package lineedit is a copy of the VT100 line editor (Terminal) from golang.org/x/term v0.29.0 (terminal.go), made display-width aware.
Package termtext removes terminal control sequences from text that came from a model, a tool or another external source before chat writes it to a terminal.
Package termtext removes terminal control sequences from text that came from a model, a tool or another external source before chat writes it to a terminal.

Jump to

Keyboard shortcuts

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