session

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LocalAgentsFileNames = []string{"AGENT.md", "AGENTS.md", "CLAUDE.md"}

LocalAgentsFileNames contains the filenames to check for local agent instructions.

Functions

This section is empty.

Types

type PendingSubAgentToolCall

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

PendingSubAgentToolCall represents a sub-agent tool call whose child session has been prepared and is awaiting execution by the caller. The caller runs the child session's completion loop and calls Complete() when finished. ConfirmTools uses the completed sub-agent's final message as the tool result.

func (*PendingSubAgentToolCall) Complete

func (p *PendingSubAgentToolCall) Complete()

Complete marks the sub-agent as completed. Must be called after the caller has finished running the child session's completion loop.

func (*PendingSubAgentToolCall) IsComplete

func (p *PendingSubAgentToolCall) IsComplete() bool

IsComplete reports whether Complete() has been called.

func (*PendingSubAgentToolCall) Session

func (p *PendingSubAgentToolCall) Session() *Session

Session returns the child session for this sub-agent. The caller runs the completion loop on this session.

func (*PendingSubAgentToolCall) ToolCallID

func (p *PendingSubAgentToolCall) ToolCallID() string

ToolCallID returns the ID of the SubAgent tool call this pending sub-agent is associated with.

type Session

type Session struct {
	// The application context provides access to the conversation repo,
	// agent resolution, system prompt, sandbox config, and model registry.
	Ctx *lmcli.Context

	// Configuration (set once, read by the loop)
	Agent       *api.Agent
	Model       *api.Model
	Effort      api.ReasoningEffort
	Temperature float32
	MaxTokens   int
	Permission  api.PermissionMode
	SandboxCfg  *sandbox.ToolSandboxConfig

	// Runtime state
	Conversation conversation.Conversation
	Messages     []conversation.Message

	// TemperatureOverride and MaxTokensOverride, when non-nil, keeps the user's
	// explicit choice across model switches instead of adopting the new model's
	// default.
	TemperatureOverride *float32
	MaxTokensOverride   *int

	// Persistent controls whether produced messages are persisted
	// to the repo by the caller after receiving events from the
	// completion loop.
	Persistent bool

	// SubAgentDescription is a short label for display when this session
	// is a sub-agent. Only meaningful when the session is a child of
	// another session.
	SubAgentDescription string
	// contains filtered or unexported fields
}

Session owns everything needed to orchestrate an agent loop: the model and agent configuration, permission mode, sandbox config, conversation messages, and optional persistence. It replaces the overlapping conversation.Thread and tui/app.Session types.

func LoadSession

func LoadSession(ctx *lmcli.Context, conv *conversation.Conversation) (*Session, error)

LoadSession loads an existing conversation's selected thread from the repo.

func NewSession

func NewSession(ctx *lmcli.Context) *Session

NewSession creates a new, empty session (for starting new conversations).

func (*Session) Append

func (s *Session) Append(msgs ...conversation.Message)

Append adds messages to the in-memory session without persisting.

func (*Session) ConfirmAndExecuteTools

func (s *Session) ConfirmAndExecuteTools(
	ctx context.Context,
	pendingSubAgents []PendingSubAgentToolCall,
	confirmations []api.ToolConfirmation,
) (allRejected bool, err error)

ConfirmAndExecuteTools validates that the last message is a ToolCall, executes the tool calls with the given confirmations using the session's configuration, and appends a ToolResult message to the session. For SubAgent tool calls, the result is taken from the corresponding PendingSubAgent's completed child session. PendingSubAgents must have Complete() called before passing them here (or the caller must have rejected the SubAgent tool call). The ctx parameter provides cancellation for tool execution. Returns allRejected=true if every tool call was rejected. Returns an error if the last message is not a ToolCall or if execution fails.

func (*Session) CurrentParts

func (s *Session) CurrentParts() []api.ContentPart

CurrentParts returns the streaming builder's current accumulated content parts. Returns nil when no streaming is in progress. The caller should treat the returned slice as ephemeral — it reflects whatever the builder has accumulated so far and may change with subsequent chunks.

The builder is written by the request goroutine under builderMu; the caller must hold builderMu to read safely.

func (*Session) IsDirty

func (s *Session) IsDirty(id uint) bool

IsDirty reports whether the message with the given database ID has been modified since it was last persisted.

func (*Session) MaxTokensOverridePtr

func (s *Session) MaxTokensOverridePtr() *int

MaxTokensOverridePtr returns the max-tokens override pointer, if any.

func (*Session) PendingSubAgentToolCalls

func (s *Session) PendingSubAgentToolCalls() []PendingSubAgentToolCall

PendingSubAgentToolCalls examines the last message for SubAgent tool calls and creates child sessions for each one. Returns nil when there are no pending sub-agents. The caller is responsible for running the completion loop on each returned PendingSubAgent's session, calling Complete() when finished, and passing them to ConfirmTools.

func (*Session) Persist

func (s *Session) Persist() error

Persist saves all pending changes to the repo:

  • If the conversation is new (ID==0), it creates the conversation and all messages atomically via StartConversation.
  • If the conversation exists, it updates the conversation metadata, updates any dirty (modified) messages, and appends new (unsaved) messages via Reply.

It also syncs the current Session.Agent to Conversation.Agent and, for code agents, ensures Conversation.Workspace is set.

func (*Session) Reload

func (s *Session) Reload() error

Reload re-fetches the conversation and its selected thread from the repo. Useful after operations like branching, cycling, or deleting that modify the tree structure externally.

func (*Session) RequestCompletion

func (s *Session) RequestCompletion(ctx context.Context) (<-chan api.Event, context.CancelFunc)

RequestCompletion starts a single completion step using the session's configuration. Returns an event channel for the caller to consume and a cancel function to stop the step. Cancelling ctx stops the step.

func (*Session) SetAgent

func (s *Session) SetAgent(agent *api.Agent)

SetAgent updates the agent in the session and reapplies the system prompt.

func (*Session) SetContent

func (s *Session) SetContent(i int, content string)

SetContent modifies a message's text content in-place and marks it dirty if it has already been saved. It updates the first text-type part, or appends one if none exists.

func (*Session) SetMessage

func (s *Session) SetMessage(i int, msg conversation.Message)

SetMessage replaces a message at the given index. If the message at that index was already saved, it is marked dirty.

func (*Session) SetModel

func (s *Session) SetModel(model *api.Model)

SetModel updates the model in the session.

func (*Session) SetParts

func (s *Session) SetParts(i int, parts []api.ContentPart)

SetParts replaces the content parts of the message at index i and marks it dirty if it has already been saved.

func (*Session) SetupSandbox

func (s *Session) SetupSandbox() error

SetupSandbox resolves the workspace from the conversation and configures the sandbox config on the session. Must be called before any completion request if sandboxing may be used.

func (*Session) TemperatureOverridePtr

func (s *Session) TemperatureOverridePtr() *float32

TemperatureOverridePtr returns the temperature override pointer, if any.

type TemplateData

type TemplateData struct {
	Date      string
	Workspace string
}

TemplateData holds variables available in system prompt templates.

Jump to

Keyboard shortcuts

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