agents

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: 22 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 agents lets people's agents talk through Keystone, as A2A does: an agent is a client key with a card, a task one request from one agent to another, delivered to the recipient's inbox (the MCP server "agents").

Index

Constants

View Source
const (
	RecordOff      = "off"
	RecordMetadata = "metadata"
	RecordFull     = "full"
)

Recording levels of the journal.

View Source
const MaxMessageBytesLimit = 1 << 20

MaxMessageBytesLimit caps AGENTS_MAX_MESSAGE_BYTES, so one message always fits a tool result and the admin GetAgentTask window.

Server is the name of the built-in MCP server; allowed_mcp ["agents"] makes a key that may only talk to agents.

Variables

View Source
var (
	// ErrInvalid is a request the caller can fix; its text says how.
	ErrInvalid = errors.New("invalid")
	// ErrDenied is a request the policy refuses.
	ErrDenied = errors.New("permission_denied")
)

Functions

func AnsweredBy

func AnsweredBy(t *proxydb.AgentTask) string

AnsweredBy is who wrote the latest answer of an ended task, a correction included: agent or owner; "" while it is open or has no answer.

func CheckRef

func CheckRef(ref string) (string, error)

CheckRef validates an external_ref: printable, up to maxRef bytes.

func PinnedBy

func PinnedBy(t *proxydb.AgentTask) []string

PinnedBy names the agents that pinned the task.

func Tools

func Tools() []*mcp.Tool

Tools are the tools of the agents server, named without the server prefix.

func ValidRecording

func ValidRecording(v string) bool

ValidRecording reports whether v is a recording level; "" keeps the default.

Types

type Card

type Card struct {
	Description *string   `json:"description"`
	Skills      *[]string `json:"skills"`
	Repos       *[]string `json:"repos"`
	AcceptFrom  *[]string `json:"accept_from"`
}

Card is what an agent says about itself; a field left out keeps its value, an empty one clears it.

type Config

type Config struct {
	MaxDepth        int32 // tasks one task may cause in a chain
	MaxMessages     int64 // messages in one conversation (context id)
	MaxMessageBytes int   // one message
	MaxTasksPerPair int64 // tasks one agent sends another an hour; 0 = unlimited, not defaulted
	Recording       string
	Retention       time.Duration
	MaxWait         time.Duration // longest wait of inbox and outbox; keep it under the HTTP timeout
	// How long after its last change an ended task takes a correction.
	CorrectionWindow time.Duration
	MaxPinned        int64 // tasks one agent may pin
}

Config bounds conversations; zero values take the defaults.

type Event

type Event struct {
	Type      string    `json:"type"` // task.created, task.<status>, task.corrected
	At        time.Time `json:"at"`
	TaskID    string    `json:"task_id"`
	ContextID string    `json:"context_id"`
	From      string    `json:"from"`
	FromOwner string    `json:"from_owner,omitempty"`
	To        string    `json:"to"`
	ToOwner   string    `json:"to_owner,omitempty"`
	Status    string    `json:"status"`
	Reason    string    `json:"reason,omitempty"`
	Depth     int32     `json:"depth"`
	Bytes     int       `json:"bytes"`
	// owner when the agent's person wrote the message, through the console.
	Author string `json:"author,omitempty"`
	// Only at recording level full; cut to the event limit, with the hash of the whole.
	Text      string `json:"text,omitempty"`
	Truncated bool   `json:"truncated,omitempty"`
	SHA256    string `json:"sha256,omitempty"`
}

Event is one journal entry of a conversation.

type Guard

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

Guard runs the model guardrails on messages between agents. PII is blocked or masked as GUARDRAILS_PII_ACTION says; a suspected injection is held for a person (input-required) instead of blocked, unless its action is log.

func NewGuard

func NewGuard(g *guardrails.Guardrails) *Guard

NewGuard is nil when guardrails are off, so messages pass unchecked.

func (*Guard) Check

func (gd *Guard) Check(ctx context.Context, text string) (string, string, error)

Check returns the text to deliver and, when a person should see it first, why; an error refuses the message.

type Journal

type Journal interface {
	Record(ctx context.Context, ev Event) error
}

Journal keeps conversation events for audit and for exporters.

type Query

type Query struct {
	All         bool // inbox: ended tasks too
	Limit       int
	Since       time.Time
	Wait        time.Duration
	ExternalRef string
	Pinned      bool // only tasks the caller pinned
}

Query narrows inbox and outbox: tasks changed after Since, and, with Wait, a wait of up to Wait for the first such task when there is none yet.

type RedisJournal

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

RedisJournal is the best-effort journal: a capped Redis stream. The oldest events go first when it is full, and the write never blocks a conversation for longer than the client's timeout.

func NewRedisJournal

func NewRedisJournal(rdb redis.Cmdable, key string, maxLen int64, maxEventBytes int) *RedisJournal

func (*RedisJournal) Record

func (j *RedisJournal) Record(ctx context.Context, ev Event) error

type Service

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

Service holds the policy; guard and journal may be nil.

func New

func New(store Store, cfg Config, guard *Guard, journal Journal) *Service

func (*Service) AsOwner

func (s *Service) AsOwner(ctx context.Context, name, owner string) (*proxydb.Agent, error)

AsOwner is the agent named name, for its owner to act as: owner must be the owner of its key. Anyone else's agent is not found, as a missing one.

func (*Service) Call

func (s *Service) Call(ctx context.Context, id *apikeys.Identity, tool string, args json.RawMessage) (*mcp.CallToolResult, error)

Call runs one tool for the caller. Policy refusals and mistakes come back as tool errors the model can act on, as MCP wants.

func (*Service) Correct

func (s *Service) Correct(ctx context.Context, id *apikeys.Identity, taskID, text string) (*proxydb.AgentTask, error)

Correct amends the answer of an ended task the caller received, within CorrectionWindow of its last change. The task keeps its status; its change wakes the sender's waiting outbox.

func (*Service) Count

func (s *Service) Count(ctx context.Context, every time.Duration)

Count sets ktms_agent_tasks every interval until ctx ends. Every status is set, zero included, so a status that empties does not keep its last value.

func (*Service) Get

func (s *Service) Get(ctx context.Context, id *apikeys.Identity, taskID string) (*proxydb.AgentTask, error)

Get is a task the caller sent or received, with its first message and as many of its newest as resultBytes holds; one message of the largest size always fits.

func (*Service) Inbox

func (s *Service) Inbox(ctx context.Context, id *apikeys.Identity, q Query) ([]*proxydb.AgentTask, error)

Inbox is the open tasks sent to the caller, each with its first message and as many of its newest as its share of resultBytes holds.

func (*Service) List

func (s *Service) List(ctx context.Context, id *apikeys.Identity, query string) (me *proxydb.Agent, out []*proxydb.Agent, err error)

List is the agents the caller can see, itself included; with query, those whose card holds every word of it.

func (*Service) Listen

func (s *Service) Listen(ctx context.Context)

Listen wakes waiting inbox and outbox calls when tasks change, on this replica or another, until ctx ends.

func (*Service) Me

func (s *Service) Me(ctx context.Context, id *apikeys.Identity) (*proxydb.Agent, error)

Me is the caller's agent, registered on first use. Its name is the key's and cannot change: whoever mints keys names agents, so no agent can take another's name to pass an accept_from list.

func (*Service) Outbox

func (s *Service) Outbox(ctx context.Context, id *apikeys.Identity, q Query) ([]*proxydb.AgentTask, error)

Outbox is the tasks the caller sent, without their messages.

func (*Service) OwnerCorrect

func (s *Service) OwnerCorrect(ctx context.Context, me *proxydb.Agent, taskID, text string) (*proxydb.AgentTask, error)

OwnerCorrect is Correct by the owner of agent me (from AsOwner).

func (*Service) OwnerReply

func (s *Service) OwnerReply(ctx context.Context, me *proxydb.Agent, taskID, text, status string) (*proxydb.AgentTask, error)

OwnerReply answers a task as the owner of agent me (from AsOwner), on whichever side of it the agent is; the message is marked as the owner's.

func (*Service) Pin

func (s *Service) Pin(ctx context.Context, id *apikeys.Identity, taskID string, pinned bool) (*proxydb.AgentTask, error)

Pin keeps a task the caller sent or received past the retention sweep, or with pinned false lets it go; each side pins for itself.

func (*Service) Register

func (s *Service) Register(ctx context.Context, id *apikeys.Identity, c Card) (*proxydb.Agent, error)

Register changes the caller's card. Its name and recording level are not the agent's to choose.

func (*Service) Reply

func (s *Service) Reply(ctx context.Context, id *apikeys.Identity, taskID, text, status string, needsOwner bool) (*proxydb.AgentTask, error)

Reply answers a task and returns it without its messages. Its recipient moves it to working, input-required, completed or failed; its sender answers a question (back to working) or cancels it.

func (*Service) Send

func (s *Service) Send(ctx context.Context, id *apikeys.Identity, to, text, contextID, ref string) (*proxydb.AgentTask, error)

Send opens a task for agent to. With contextID it continues a conversation the caller takes part in, one level deeper. ref ties it to an issue elsewhere; a continuation without one takes the conversation's.

func (*Service) Sweep

func (s *Service) Sweep(ctx context.Context, every time.Duration)

Sweep deletes tasks unchanged for longer than the retention, every interval, until ctx ends. No retention keeps them forever.

func (*Service) Tools

func (s *Service) Tools() []*mcp.Tool

Tools are the service's tools, for the MCP gateway.

type Store

type Store interface {
	InsertAgent(ctx context.Context, keyID, name string) error
	UpdateAgentCard(ctx context.Context, keyID string, c proxydb.AgentCard) error
	GetAgentByKey(ctx context.Context, keyID string) (*proxydb.Agent, error)
	GetAgentByName(ctx context.Context, name string) (*proxydb.Agent, error)
	GetAPIKeyByLowerName(ctx context.Context, name string) (*proxydb.APIKey, error)
	ListAgents(ctx context.Context, f proxydb.AgentFilter) ([]*proxydb.Agent, error)
	ContextDepth(ctx context.Context, contextID, toAgent string) (int32, error)
	WithAgentMessages(ctx context.Context, tasks []*proxydb.AgentTask, window int) error
	AgentInContext(ctx context.Context, keyID, contextID string) (bool, error)
	CreateAgentTask(ctx context.Context, t *proxydb.AgentTask, text string, lim proxydb.TaskLimits) error
	AddAgentMessage(ctx context.Context, taskID, fromAgent, role, author, text, status, reason, reasonCode string, maxMessages int64) (*proxydb.AgentTask, error)
	AddAgentCorrection(ctx context.Context, taskID, fromAgent, author, text string, window time.Duration, maxMessages int64) (*proxydb.AgentTask, error)
	GetAgentTask(ctx context.Context, id string) (*proxydb.AgentTask, error)
	ListAgentTasks(ctx context.Context, f proxydb.TaskFilter) ([]*proxydb.AgentTask, error)
	DeleteAgentTasksBefore(ctx context.Context, cutoff time.Time) (int64, error)
	CountAgentTasks(ctx context.Context) (map[string]int64, error)
	ListenAgents(ctx context.Context, wake func(keyID string)) error
	SetAgentTaskPinned(ctx context.Context, taskID, byAgent string, pinned bool, maxPinned int64) (*proxydb.AgentTask, error)
	ContextRef(ctx context.Context, contextID string) (string, error)
}

Store is the slice of proxydb the service needs.

Jump to

Keyboard shortcuts

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