chat

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package chat implements the conversational AI chat box, including tool definitions that the model can invoke to interact with the repository.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatConfirmationPrompt

func FormatConfirmationPrompt(pc *PendingConfirmation) string

FormatConfirmationPrompt returns a user-facing prompt string for the pending confirmation, suitable for display in a terminal.

Types

type ConfirmationManager

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

ConfirmationManager handles the approval workflow for destructive tool calls. Only one confirmation may be pending at a time.

func NewConfirmationManager

func NewConfirmationManager(registry *ToolRegistry) *ConfirmationManager

NewConfirmationManager creates a manager that uses the given registry to classify tool calls as safe or destructive.

func (*ConfirmationManager) Accept

func (m *ConfirmationManager) Accept() *ai.ToolCall

Accept approves the pending confirmation and returns the tool call. Clears the pending state. Returns nil if nothing is pending.

func (*ConfirmationManager) Check

Check examines a tool call and returns:

  • (nil, true) if the tool is safe and can execute immediately
  • (pending, false) if the tool requires confirmation
  • (nil, false) if the tool is unknown

func (*ConfirmationManager) Clear

func (m *ConfirmationManager) Clear()

Clear removes any pending confirmation without accepting or rejecting.

func (*ConfirmationManager) HasPending

func (m *ConfirmationManager) HasPending() bool

HasPending reports whether there is a confirmation waiting.

func (*ConfirmationManager) Pending

Pending returns the current pending confirmation, or nil.

func (*ConfirmationManager) Reject

func (m *ConfirmationManager) Reject() string

Reject denies the pending confirmation and clears the pending state. Returns a description of what was rejected, or an empty string if nothing was pending.

type Deps

type Deps struct {
	Registry   *ai.Registry
	Executor   *ToolExecutor
	Confirming *ConfirmationManager
	SysPrompt  *SystemPromptBuilder
	Redactor   *ai.Redactor
	Audit      *ai.AuditLogger
	Theme      *theme.Theme
	ChatCfg    config.ChatConfig
}

Deps bundles the required dependencies for creating a chat Model, keeping the constructor signature short and readable.

type InputHistory

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

InputHistory provides terminal-style input history with Up/Down navigation.

func (*InputHistory) Down

func (h *InputHistory) Down() (string, bool)

Down moves to a newer history entry. If we move past the newest entry, it resets the index and returns the saved draft. Returns ("", false) when not currently browsing history.

func (*InputHistory) Push

func (h *InputHistory) Push(entry string)

Push adds a sent message to history. It trims whitespace, skips empty strings, and suppresses consecutive duplicates.

func (*InputHistory) Reset

func (h *InputHistory) Reset()

Reset clears the browsing state, returning to "not browsing" mode.

func (*InputHistory) Up

func (h *InputHistory) Up(currentInput string) (string, bool)

Up moves to an older history entry. On the first call (index == -1) it saves currentInput as the draft and jumps to the newest entry. Subsequent calls decrement the index (minimum 0). Returns the entry and true, or ("", false) when history is empty.

type Model

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

--------------------------------------------------------------------------- Model --------------------------------------------------------------------------- Model is the Bubble Tea model for the chat footer. It manages user input, streams AI responses, orchestrates multi-turn tool calling, and handles destructive-operation confirmations.

func New

func New(d Deps) Model

New creates a new chat Model with all required dependencies.

func (*Model) Blur

func (m *Model) Blur()

Blur removes keyboard focus from the chat input and disables overlay mode.

func (*Model) ClearHistory

func (m *Model) ClearHistory()

ClearHistory removes all conversation history and resets the display.

func (*Model) Close

func (m *Model) Close()

Close cancels any in-flight operations and releases resources.

func (*Model) Focus

func (m *Model) Focus()

Focus gives keyboard focus to the chat input and enables overlay mode.

func (Model) Focused

func (m Model) Focused() bool

--------------------------------------------------------------------------- Focus management --------------------------------------------------------------------------- Focused reports whether the chat has keyboard focus.

func (Model) Height

func (m Model) Height() int

Height returns the current height of the chat model in terminal rows. In overlay mode the modal is rendered separately by the parent (app.go), so the chat footer stays at its collapsed height.

func (Model) Init

func (m Model) Init() tea.Cmd

--------------------------------------------------------------------------- tea.Model interface --------------------------------------------------------------------------- Init implements tea.Model. No initial command is needed.

func (Model) RenderModalContent

func (m Model) RenderModalContent(width, height int) string

RenderModalContent renders the inner content of the floating chat modal dialog. The caller (app.go) applies the border, title, and positioning. width and height are the inner dimensions available after the border is subtracted. This method uses a value receiver so local width/input adjustments do not persist.

func (*Model) SetSize

func (m *Model) SetSize(width, height int)

SetSize updates the available dimensions for the chat model.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update handles messages and returns the updated model and any commands. This model is designed to be embedded within the app model — it does not implement tea.Model directly. The parent model calls Update and View.

func (Model) View

func (m Model) View() string

View implements tea.Model. Returns the chat UI as a string. Rendering is delegated to renderView() in view.go.

type PendingConfirmation

type PendingConfirmation struct {
	Call        ai.ToolCall
	Description string // Human-readable description of what will happen
}

PendingConfirmation holds a tool call that requires user approval before execution.

type SendMessageCmd

type SendMessageCmd struct{ Content string }

SendMessageCmd is an external command to programmatically send a chat message, as if the user typed it and pressed Enter.

type StreamChunkMsg

type StreamChunkMsg struct{ Chunk ai.StreamChunk }

--------------------------------------------------------------------------- Custom message types --------------------------------------------------------------------------- StreamChunkMsg delivers an incremental piece of a streaming AI response.

type SystemPromptBuilder

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

SystemPromptBuilder constructs context-aware system prompts for the chat AI. The prompt is rebuilt on each message to reflect current repository state.

func NewSystemPromptBuilder

func NewSystemPromptBuilder(client git.GitClient, override string) *SystemPromptBuilder

NewSystemPromptBuilder creates a builder. If override is non-empty, it replaces the default prompt entirely.

func (*SystemPromptBuilder) Build

func (b *SystemPromptBuilder) Build(ctx context.Context) string

Build generates the current system prompt with live repo context. If git operations fail, the prompt is still returned with placeholder values so the AI remains functional.

type ToolCallMsg

type ToolCallMsg struct{ Calls []ai.ToolCall }

ToolCallMsg signals that the AI response contains tool invocations.

type ToolExecutor

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

ToolExecutor maps AI tool calls to actual git and file operations.

func NewToolExecutor

func NewToolExecutor(client git.GitClient, jail *mcp.PathJail, limiter *mcp.RateLimiter, registry *ToolRegistry) *ToolExecutor

NewToolExecutor creates a ToolExecutor wired to the given git client, path jail, rate limiter, and tool registry.

func (*ToolExecutor) Execute

func (e *ToolExecutor) Execute(ctx context.Context, call ai.ToolCall) ToolResult

Execute runs a tool call and returns the result. It does NOT check safety classification — caller must handle confirmation first.

type ToolInfo

type ToolInfo struct {
	Definition ai.ToolDefinition
	Safety     ToolSafety
}

ToolInfo combines a tool definition with its safety classification.

type ToolRegistry

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

ToolRegistry holds all available chat tools with their definitions and safety classifications.

func NewToolRegistry

func NewToolRegistry() *ToolRegistry

NewToolRegistry creates a registry with all built-in tools.

func (*ToolRegistry) Definitions

func (r *ToolRegistry) Definitions() []ai.ToolDefinition

Definitions returns all tool definitions for sending to the AI provider.

func (*ToolRegistry) Get

func (r *ToolRegistry) Get(name string) (ToolInfo, bool)

Get returns the tool info for a given name, or false if not found.

func (*ToolRegistry) IsSafe

func (r *ToolRegistry) IsSafe(name string) bool

IsSafe reports whether the named tool can be executed without confirmation. Returns false for unknown tools.

type ToolResult

type ToolResult struct {
	ToolID  string // Matches the ToolCall.ID
	Content string // Result content for the AI to consume
	Error   string // Error message if execution failed
}

ToolResult holds the outcome of executing a tool call.

type ToolResultMsg

type ToolResultMsg struct{ Results []ToolResult }

ToolResultMsg delivers results from executed tool calls back to the model.

type ToolSafety

type ToolSafety int

ToolSafety classifies a tool's risk level. Safe tools can execute immediately; Destructive tools require explicit user confirmation before execution.

const (
	// Safe tools are read-only or low-risk and execute immediately.
	Safe ToolSafety = iota

	// Destructive tools modify or delete data and require user confirmation.
	Destructive
)

Jump to

Keyboard shortcuts

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