message

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package message defines the Message type and the three roles a turn can occupy in a jungi conversation.

There are three distinct audiences for messages:

  • RoleUser: text entered by the human at the keyboard
  • RoleModel: text returned by the provider API
  • RoleHarness: status and informational text injected by the application itself (e.g. "session started", compaction notices)

The API only understands user and assistant roles, so RoleHarness messages must be stripped before transmission; see FilterForAPI.

Index

Constants

View Source
const (
	RoleUser    = iota // text submitted by the human
	RoleModel          // text returned by the provider API
	RoleHarness        // application-generated status text, never sent to the API
)

Role constants for Message. Using iota keeps the values compact and avoids magic integers scattered across the codebase.

Variables

This section is empty.

Functions

func AppendAssistant added in v0.2.0

AppendAssistant wraps an assistant message from a chat completion response (including any tool_calls it carries) into a ChatMessages value, for appending to the running conversation before the tool results answering those calls are sent back. Resending the assistant's own tool_calls is required — OpenRouter's chat-completions API rejects a "tool" role message that does not follow an assistant message carrying the matching tool_call_id.

func OpenRouterText added in v0.2.0

func OpenRouterText(result *components.ChatResult) string

OpenRouterText extracts the assistant's reply text from the first choice of a chat completion result. Non-text content items (e.g. images) are skipped; jungi only ever sends and expects text.

func ToMessages added in v0.2.0

func ToMessages(systemPrompt string, msgs []Message) []components.ChatMessages

ToMessages translates the internal message slice into the OpenRouter SDK's chat-completions ChatMessages format.

Unlike the Anthropic and OpenAI Responses APIs, OpenRouter's chat-completions shape carries the system prompt as a leading message in the same slice rather than a dedicated request field, so systemPrompt is prepended here as a system message when non-empty. Messages with RoleModel map to the "assistant" role; everything else (in practice, RoleUser) maps to "user". RoleHarness messages must be filtered out before calling this function via FilterForAPI.

func ToolResults added in v0.2.0

func ToolResults(results []ToolResult) []components.ChatMessages

ToolResults adapts the provider-neutral ToolResult slice into OpenRouter "tool" role chat messages, keyed by ToolCallID to match the tool_calls the prior assistant turn emitted.

Types

type Message

type Message struct {
	Role      int
	Content   string
	Hidden    bool
	PreStyled bool
	CreatedAt time.Time
}

Message is a single turn in the conversation, attributed to one of the three roles above. Hidden messages are sent to the API but not displayed in the TUI history viewport.

PreStyled, when true, signals that Content already contains ANSI escape sequences and should not be passed through glamour markdown rendering. It is used for harness messages that embed pre-rendered output (e.g. edit_file diffs).

func FilterForAPI

func FilterForAPI(msgs []Message) []Message

FilterForAPI returns a copy of msgs containing only user and model turns.

The provider API rejects any role other than "user" and "assistant". Harness messages exist solely for in-terminal display and must be removed before the slice is handed to the client layer.

type ToolResult

type ToolResult struct {
	ToolUseID string
	Content   string
	IsError   bool
}

ToolResult represents a single tool execution result to be sent back to the model. ToolUseID must match the ID of the ToolUseRequest it answers.

type ToolUseRequest

type ToolUseRequest struct {
	ID       string
	Name     string
	Command  string          // shell: the shell command
	Restart  bool            // shell: restart flag
	Reason   string          // unsafe_shell: reason for running outside the sandbox
	Timeout  int             // shell: per-invocation timeout override in seconds (0 = use default)
	RawInput json.RawMessage // full input JSON for tool-specific parsing
}

ToolUseRequest represents a single tool invocation requested by the model.

func ToolUses added in v0.2.0

func ToolUses(result *components.ChatResult) []ToolUseRequest

ToolUses parses the tool_calls from the first choice of a chat completion result into ToolUseRequest values the caller can act on.

ID is set to the tool call's ID, which is what ToolResults matches against via ToolCallID when building the next request's tool response messages.

Source Files

  • adapter.go
  • message.go
  • toolcall.go

Jump to

Keyboard shortcuts

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