agent_session

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ChunkContent    = "CONTENT"
	ChunkToolCall   = "TOOL_CALL"
	ChunkToolResult = "TOOL_RESULT"
	ChunkThinking   = "THINKING"
	ChunkError      = "ERROR"
	ChunkDone       = "DONE"
)

Chunk types as the plugin proto names them (AgentMessageChunk_ChunkType.String()).

View Source
const MetadataToolCallID = "tool_call_id"

MetadataToolCallID is the metadata key the session adds to TOOL_CALL and TOOL_RESULT chunks so consumers can pair a result with its call.

Variables

This section is empty.

Functions

func NewSessionID

func NewSessionID() string

NewSessionID returns a fresh agent session id.

Types

type AgentMessageChunk

type AgentMessageChunk struct {
	Type     string                 `json:"type"` // CONTENT, TOOL_CALL, TOOL_RESULT, THINKING, ERROR, DONE
	Content  string                 `json:"content"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
	IsFinal  bool                   `json:"is_final"`
}

AgentMessageChunk represents a chunk of agent response

type AgentSession

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

AgentSession manages the runtime lifecycle of an agent plugin conversation

func NewAgentSession

func NewAgentSession(
	agentConfig *models.AgentConfig,
	pluginClient pb.PluginServiceClient,
	serviceBrokerID uint32,
	queue MessageQueue,
	db *gorm.DB,
) (*AgentSession, error)

NewAgentSession creates a new agent session

func NewAgentSessionWithID

func NewAgentSessionWithID(
	id string,
	agentConfig *models.AgentConfig,
	pluginClient pb.PluginServiceClient,
	serviceBrokerID uint32,
	queue MessageQueue,
	db *gorm.DB,
) (*AgentSession, error)

NewAgentSessionWithID is NewAgentSession with a caller-chosen id, so the queue and the session can share one identifier.

func (*AgentSession) AgentConfig

func (as *AgentSession) AgentConfig() *models.AgentConfig

AgentConfig returns the configuration the session was created for.

func (*AgentSession) CancelRun

func (as *AgentSession) CancelRun() bool

CancelRun aborts the plugin stream of the active turn, if any.

func (*AgentSession) Close

func (as *AgentSession) Close() error

Close closes the agent session and cleans up resources

func (*AgentSession) GetID

func (as *AgentSession) GetID() string

GetID returns the session ID

func (*AgentSession) GetQueue

func (as *AgentSession) GetQueue() MessageQueue

GetQueue returns the message queue for consuming responses

func (*AgentSession) ID

func (as *AgentSession) ID() string

ID returns the session id (HubSession).

func (*AgentSession) OwnerID

func (as *AgentSession) OwnerID() uint

OwnerID returns the user id the session was created for (0 if unset).

func (*AgentSession) SendMessage

func (as *AgentSession) SendMessage(userMessage string, history []map[string]interface{}) error

SendMessage sends a user message to the agent plugin and streams responses back

func (*AgentSession) SetOwnerID

func (as *AgentSession) SetOwnerID(id uint)

SetOwnerID records which user created the session; OwnerID reads it.

func (*AgentSession) StartRun

func (as *AgentSession) StartRun(userMessage string) (<-chan error, error)

StartRun sends one user message to the plugin and streams the reply into the queue. The returned channel receives the stream's terminal error (nil on a clean end) exactly once. The history handed to the plugin is the transcript before this message.

func (*AgentSession) Stop

func (as *AgentSession) Stop()

Stop closes the session (HubSession). Idempotent.

func (*AgentSession) Transcript

func (as *AgentSession) Transcript() []TranscriptMessage

Transcript returns a copy of the in-memory conversation.

func (*AgentSession) TryLockRun

func (as *AgentSession) TryLockRun() bool

TryLockRun claims the session for one turn; UnlockRun releases it.

func (*AgentSession) UnlockRun

func (as *AgentSession) UnlockRun()

UnlockRun releases the claim taken by TryLockRun.

type MessageQueue

type MessageQueue interface {
	PublishStream(ctx context.Context, data []byte) error
	PublishError(ctx context.Context, err error) error
	ConsumeStream(ctx context.Context) <-chan []byte
	ConsumeErrors(ctx context.Context) <-chan error
	Close() error
}

MessageQueue is a minimal interface for publishing agent responses This avoids importing chat_session to prevent circular dependencies

type TranscriptMessage

type TranscriptMessage struct {
	ID        string           `json:"id"`
	Role      string           `json:"role"` // user | assistant
	Parts     []TranscriptPart `json:"parts"`
	CreatedAt time.Time        `json:"created_at"`
}

TranscriptMessage is one turn of the session's in-memory transcript.

type TranscriptPart

type TranscriptPart struct {
	Type       string         `json:"type"` // text | reasoning | tool-call | data
	Text       string         `json:"text,omitempty"`
	ToolCallID string         `json:"toolCallId,omitempty"`
	ToolName   string         `json:"toolName,omitempty"`
	Args       map[string]any `json:"args,omitempty"`
	ArgsText   string         `json:"argsText,omitempty"`
	Result     any            `json:"result,omitempty"`
	HasResult  bool           `json:"-"`
	IsError    bool           `json:"isError,omitempty"`
	Name       string         `json:"name,omitempty"`
	Data       map[string]any `json:"data,omitempty"`
}

TranscriptPart is one part of an in-memory transcript message. It mirrors the parts the v2 chat history exposes so the API can convert it directly.

Jump to

Keyboard shortcuts

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