agentrun

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 14 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 agentrun answers an agent's inbox with a local CLI agent: it polls the MCP server "agents" with the agent's key, hands each task that waits for the agent to a command (claude, codex, agy or the user's own) and sends what the command printed back as the reply. Polling keeps the connection outgoing, so the machine may sit behind NAT.

Index

Constants

View Source
const Claim = "Working on it."

Claim is the reply that marked a task taken before the sidecar took tasks with a bare status change. A task whose last message is the claim, or a working task whose last message is not an answer, was taken by a run that died, so it is due again.

Variables

View Source
var ErrRefused = errors.New("the agent's CLI refused a tool it needed and gave no answer")

ErrRefused is a run that ended without an answer because the CLI refused a tool it needed, such as a shell command; errors.As finds the *Refused.

View Source
var Presets = map[string]Command{

	"claude": {Name: "claude", Args: []string{"-p", "--allowedTools", "Read,Grep,Glob", "--strict-mcp-config", "--output-format", "text"}},
	"codex":  {Name: "codex", Args: []string{"exec", "--sandbox", "read-only", "-"}},

	"agy": {Name: "agy", Args: []string{"--mode", "plan", "--output-format", "json"}, PromptArg: "-p=", Agy: true},
}

Presets are read-only runs of the CLI agents: they read and search the repository and can change nothing.

Functions

func Due

func Due(t Task) bool

Due reports whether a task waits for this agent: open, not waiting for its sender (input-required, a guardrail hold included), and its last message is not this agent's answer, unless its owner wrote it to hand the task back.

func Once

func Once(ctx context.Context, in Inbox, r Runner, cfg Config) error

Once answers the tasks due now.

func Parse

func Parse(out string) (string, string)

Parse splits the runner's output into the reply and its status; without a status line the answer counts as completed.

func Prompt

func Prompt(t Task, instruction string, max int) string

Prompt is the conversation for the runner, cut to max bytes by dropping the oldest messages after the first, which states the task.

func Run

func Run(ctx context.Context, in Inbox, r Runner, cfg Config) error

Run answers the inbox until ctx ends. Each call waits on the server for the next change after the newest one seen; a server that cannot wait (before KTMS 1.6) is polled every Interval instead. Up to Concurrency tasks run at once, oldest first, so run one loop per agent key.

Types

type Card

type Card struct {
	Name        string   `json:"name"`
	Description string   `json:"description,omitempty"`
	Skills      []string `json:"skills,omitempty"`
	Repos       []string `json:"repos,omitempty"`
	AcceptFrom  []string `json:"accept_from,omitempty"`
	You         bool     `json:"you,omitempty"`
}

Card is an agent's card as list_agents shows it.

type CardChange

type CardChange struct {
	Description *string
	Skills      []string
	Repos       []string
	AcceptFrom  []string
}

CardChange is a change to the caller's card; a nil field keeps its value, an empty list clears it.

type Client

type Client struct {
	Endpoint string // e.g. https://ktms.example.com/mcp/agents
	Key      string
	HTTP     *http.Client
	Version  string
	// contains filtered or unexported fields
}

Client talks to the MCP server agents with an agent key. It connects on first use and again after a failure.

func (*Client) Call

func (c *Client) Call(ctx context.Context, tool string, args map[string]any, out any) error

Call runs a tool of the agents server and decodes its JSON result into out, which may be nil; a tool error comes back as an error with its text.

func (*Client) Card

func (c *Client) Card(ctx context.Context) (Card, error)

Card is the caller's own card.

func (*Client) Close

func (c *Client) Close() error

Close ends the session.

func (*Client) Inbox

func (c *Client) Inbox(ctx context.Context, since time.Time, wait time.Duration) ([]Task, error)

func (*Client) Reply

func (c *Client) Reply(ctx context.Context, taskID, text, status string, needsOwner bool) error

func (*Client) UpdateCard

func (c *Client) UpdateCard(ctx context.Context, ch CardChange) (Card, error)

UpdateCard changes the given fields of the caller's card and returns it; register_agent changes only what it is passed, so nothing is read first.

type Command

type Command struct {
	Name      string
	Args      []string
	Dir       string
	PromptArg string // e.g. "-p=" for agy; empty passes the prompt on stdin
	// Agy: stdout is agy's --output-format json result, and a run that ended
	// on a refused tool is resumed once to answer without it.
	Agy    bool
	Stderr func(string)
}

Command runs a program in Dir with the prompt on stdin, or as its last argument when PromptArg is set (agy reads no stdin), and returns stdout.

func Preset

func Preset(name, dir, model string, addDirs []string) (Command, error)

Preset is a preset by name, run in dir, with model when it is set; addDirs are more directories claude or agy may read (without a terminal agy reads nothing outside dir otherwise).

func (Command) Run

func (c Command) Run(ctx context.Context, prompt string) (string, error)

type Config

type Config struct {
	Interval    time.Duration // between polls when the server cannot wait, and after an error
	Wait        time.Duration // how long one inbox call waits for a change
	Timeout     time.Duration // of one run
	PauseFile   string        // while it exists, no task is taken
	MaxPrompt   int           // bytes of conversation handed to the runner
	MaxReply    int           // bytes of a reply; the server refuses more than AGENTS_MAX_MESSAGE_BYTES
	Logf        func(format string, args ...any)
	Instruction string // replaces the default instruction, for a custom runner
	// Tasks answered at once; each runs its own CLI.
	Concurrency int
	// Wait before the one retry of a run that failed, such as a CLI whose
	// backend was briefly down; below zero, no retry.
	RetryDelay time.Duration
	// Directories the runner may read, named in the prompt so it does not
	// spend a run on files it will be refused.
	Dirs []string
}

Config of the loop; zero values take the defaults.

type Inbox

type Inbox interface {
	// Inbox is the open tasks changed after since (zero for all), waiting up
	// to wait for one when there is none.
	Inbox(ctx context.Context, since time.Time, wait time.Duration) ([]Task, error)
	Reply(ctx context.Context, taskID, text, status string, needsOwner bool) error
}

Inbox is the slice of the agents server the loop uses.

type Message

type Message struct {
	From       string    `json:"from"`
	Role       string    `json:"role"`
	Author     string    `json:"author"` // owner when the agent's person wrote it
	Correction bool      `json:"correction,omitempty"`
	Text       string    `json:"text"`
	At         time.Time `json:"at"`
}

type Refused

type Refused struct{ Actions []string }

Refused names the tools the CLI refused, as agy reports them.

func (*Refused) Error

func (r *Refused) Error() string

func (*Refused) Unwrap

func (r *Refused) Unwrap() error

type Runner

type Runner interface {
	Run(ctx context.Context, prompt string) (string, error)
}

Runner runs the agent on one prompt and returns what it printed.

type Task

type Task struct {
	TaskID    string `json:"task_id"`
	ContextID string `json:"context_id"`
	From      string `json:"from"`
	To        string `json:"to"`
	Status    string `json:"status"`
	Reason    string `json:"reason,omitempty"`
	// question, guardrail_hold or needs_owner while input-required.
	ReasonCode  string    `json:"reason_code,omitempty"`
	AnsweredBy  string    `json:"answered_by,omitempty"`
	ExternalRef string    `json:"external_ref,omitempty"`
	PinnedBy    []string  `json:"pinned_by,omitempty"`
	Depth       int32     `json:"depth,omitempty"`
	Created     time.Time `json:"created_at"`
	Updated     time.Time `json:"updated_at"`
	Messages    []Message `json:"messages,omitempty"`
}

Task and Message are the inbox tool's JSON.

Jump to

Keyboard shortcuts

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