agenttest

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 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

Overview

Package agenttest provides a scriptable agent.Agent for tests of the run manager and the HTTP API. A Script plays the CLI: it reads client inputs and emits events.

Index

Constants

View Source
const (
	InputMessage   = "message"
	InputInterrupt = "interrupt"
	InputAnswer    = "answer"
	InputRespond   = "respond"
	InputClose     = "close"
)

Input kinds delivered to a Script.

Variables

This section is empty.

Functions

func Echo

func Echo(ctx context.Context, s *Session) error

Echo answers every message with "echo: <text>" after one assistant event.

func Hook

func Hook(ctx context.Context, s *Session) error

Hook sends a hook_callback control request per message and reports the client's answer.

func Permission

func Permission(ctx context.Context, s *Session) error

Permission asks for "Write" once per message and reports the decision as the result.

func Streaming

func Streaming(ctx context.Context, s *Session) error

Streaming emits three partial events before each complete message.

func WriteExecutable added in v0.2.1

func WriteExecutable(t testing.TB, path, content string)

WriteExecutable writes a script that a test then runs as a fake CLI.

It holds syscall.ForkLock while the file is open for writing. Without it, a parallel test can fork in that moment; the child keeps the write descriptor until it calls exec, and running the script fails on Linux with "text file busy" (ETXTBSY).

Types

type Agent

type Agent struct {
	Script  Script
	Started chan agent.Request
	// StartErr makes Start fail.
	StartErr error
	// contains filtered or unexported fields
}

Agent is a fake agent. Started receives every request when non-nil.

func (*Agent) AuthStatus

func (*Agent) AuthStatus(context.Context) (agent.AuthStatus, error)

AuthStatus implements agent.Agent.

func (*Agent) Info

func (*Agent) Info() agent.Info

Info implements agent.Agent.

func (*Agent) Interactive

Interactive implements agent.Agent.

func (*Agent) LastRequest

func (a *Agent) LastRequest() agent.Request

LastRequest returns the most recent request passed to Start.

func (*Agent) Name

func (*Agent) Name() string

Name implements agent.Agent.

func (*Agent) ProviderSections

func (*Agent) ProviderSections() map[agent.Section]reflect.Type

ProviderSections implements agent.SectionDescriber.

func (*Agent) Start

func (a *Agent) Start(ctx context.Context, req agent.Request, onEvent agent.EventFunc) (agent.Conversation, error)

Start implements agent.Agent.

func (*Agent) Validate

func (*Agent) Validate(req *agent.Request) error

Validate implements agent.Agent.

func (*Agent) Version

func (*Agent) Version(context.Context) (string, error)

Version implements agent.Agent.

type ControlFields

type ControlFields struct {
	Subtype string `json:"subtype"`
}

ControlFields are the fake agent's fields of a control request's provider section.

type Input

type Input struct {
	Kind   string
	Text   string
	Answer agent.PermissionAnswer
	// Respond fields.
	ID       string
	Response json.RawMessage
	Error    string
}

Input is one client action. The initial prompt arrives as the first InputMessage.

type ResultFields

type ResultFields struct {
	Reason string `json:"reason"`
}

ResultFields are the fake agent's fields of a turn result's provider section.

type Script

type Script func(ctx context.Context, s *Session) error

Script plays the CLI until it returns; the returned error is what Wait reports.

func Blocking

func Blocking(release <-chan struct{}) Script

Blocking starts a turn and then waits: an interrupt ends the turn with an error result, the context ends the process. Release (when non-nil) finishes the turn successfully.

type Session

type Session struct {
	Request agent.Request
	// contains filtered or unexported fields
}

Session is the fake conversation; Scripts use Next and the emit helpers.

func (*Session) AgentTurn

func (s *Session) AgentTurn(text string) error

AgentTurn emits the terminal event of a turn the agent started on its own.

func (*Session) Answer

func (s *Session) Answer(answer agent.PermissionAnswer) error

Answer implements agent.Conversation.

func (*Session) Ask

func (s *Session) Ask(id, kind string) error

Ask emits a permission request of a kind (agent.Permission*).

func (*Session) AskControl

func (s *Session) AskControl(id, subtype string) error

AskControl emits a generic control request.

func (*Session) CloseInput

func (s *Session) CloseInput() error

CloseInput implements agent.Conversation.

func (*Session) Control

func (s *Session) Control(_ context.Context, request json.RawMessage) (json.RawMessage, error)

Control implements agent.Conversation: it answers {"echo": request}; subtype "fail" is rejected.

func (*Session) Emit

func (s *Session) Emit(eventType, raw string) error

Emit sends a native event.

func (*Session) Finish

func (s *Session) Finish(text string, isError bool) error

Finish emits the terminal event of a turn.

func (*Session) FinishWithUsage

func (s *Session) FinishWithUsage(text string, isError bool, tokens *agent.Tokens) error

FinishWithUsage emits a terminal event with explicit token counts (nil: unreported).

func (*Session) Interrupt

func (s *Session) Interrupt() error

Interrupt implements agent.Conversation.

func (*Session) NativeSessionID

func (s *Session) NativeSessionID() string

NativeSessionID implements agent.NativeSession.

func (*Session) Next

func (s *Session) Next(ctx context.Context) (Input, bool)

Next returns the next client input, or false when ctx ends.

func (*Session) Observe

func (s *Session) Observe(raw string, observation agent.Observation) error

Observe emits a native event carrying optional structured lifecycle data.

func (*Session) Partial

func (s *Session) Partial(text string) error

Partial emits an incremental output event.

func (*Session) Respond

func (s *Session) Respond(id string, response json.RawMessage, errMessage string) error

Respond implements agent.Conversation.

func (*Session) Send

func (s *Session) Send(text string) error

Send implements agent.Conversation.

func (*Session) StartTurn

func (s *Session) StartTurn() error

StartTurn emits the first event of a turn.

func (*Session) Wait

func (s *Session) Wait() error

Wait implements agent.Conversation.

Jump to

Keyboard shortcuts

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