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
- Variables
- func Due(t Task) bool
- func Once(ctx context.Context, in Inbox, r Runner, cfg Config) error
- func Parse(out string) (string, string)
- func Prompt(t Task, instruction string, max int) string
- func Run(ctx context.Context, in Inbox, r Runner, cfg Config) error
- type Card
- type Client
- func (c *Client) Call(ctx context.Context, tool string, args map[string]any, out any) error
- func (c *Client) Card(ctx context.Context) (Card, error)
- func (c *Client) Close() error
- func (c *Client) Inbox(ctx context.Context, since time.Time, wait time.Duration) ([]Task, error)
- func (c *Client) Reply(ctx context.Context, taskID, text, status string, needsOwner bool) error
- func (c *Client) SetCard(ctx context.Context, card Card) (Card, error)
- type Command
- type Config
- type Inbox
- type Message
- type Refused
- type Runner
- type Task
Constants ¶
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 ¶
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.
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 ¶
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 Parse ¶
Parse splits the runner's output into the reply and its status; without a status line the answer counts as completed.
func Prompt ¶
Prompt is the conversation for the runner, cut to max bytes by dropping the oldest messages after the first, which states the task.
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 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 ¶
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.
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.
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
}
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 Refused ¶
type Refused struct{ Actions []string }
Refused names the tools the CLI refused, as agy reports them.
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.