Documentation
¶
Overview ¶
Package agents lets the agents of different people talk through Keystone: an agent is a client key with a card, and a task is one request from one agent to another, delivered to the recipient's inbox (MCP server "agents"). The model follows A2A: tasks, context ids, messages and statuses.
Index ¶
- Constants
- Variables
- func AnsweredBy(t *proxydb.AgentTask) string
- func CheckRef(ref string) (string, error)
- func PinnedBy(t *proxydb.AgentTask) []string
- func Tools() []*mcp.Tool
- func ValidRecording(v string) bool
- type Card
- type Config
- type Event
- type Guard
- type Journal
- type Query
- type RedisJournal
- type Service
- func (s *Service) AsOwner(ctx context.Context, name, owner string) (*proxydb.Agent, error)
- func (s *Service) Call(ctx context.Context, id *apikeys.Identity, tool string, args json.RawMessage) (*mcp.CallToolResult, error)
- func (s *Service) Correct(ctx context.Context, id *apikeys.Identity, taskID, text string) (*proxydb.AgentTask, error)
- func (s *Service) Count(ctx context.Context, every time.Duration)
- func (s *Service) Get(ctx context.Context, id *apikeys.Identity, taskID string) (*proxydb.AgentTask, error)
- func (s *Service) Inbox(ctx context.Context, id *apikeys.Identity, q Query) ([]*proxydb.AgentTask, error)
- func (s *Service) List(ctx context.Context, id *apikeys.Identity, query string) (me *proxydb.Agent, out []*proxydb.Agent, err error)
- func (s *Service) Listen(ctx context.Context)
- func (s *Service) Me(ctx context.Context, id *apikeys.Identity) (*proxydb.Agent, error)
- func (s *Service) Outbox(ctx context.Context, id *apikeys.Identity, q Query) ([]*proxydb.AgentTask, error)
- func (s *Service) OwnerCorrect(ctx context.Context, me *proxydb.Agent, taskID, text string) (*proxydb.AgentTask, error)
- func (s *Service) OwnerReply(ctx context.Context, me *proxydb.Agent, taskID, text, status string) (*proxydb.AgentTask, error)
- func (s *Service) Pin(ctx context.Context, id *apikeys.Identity, taskID string, pinned bool) (*proxydb.AgentTask, error)
- func (s *Service) Register(ctx context.Context, id *apikeys.Identity, c Card) (*proxydb.Agent, error)
- func (s *Service) Reply(ctx context.Context, id *apikeys.Identity, taskID, text, status string, ...) (*proxydb.AgentTask, error)
- func (s *Service) Send(ctx context.Context, id *apikeys.Identity, to, text, contextID, ref string) (*proxydb.AgentTask, error)
- func (s *Service) Sweep(ctx context.Context, every time.Duration)
- func (s *Service) Tools() []*mcp.Tool
- type Store
Constants ¶
const ( RecordOff = "off" RecordMetadata = "metadata" RecordFull = "full" )
Recording levels of the journal.
const Server = registry.ReservedMCPServer
Server is the name of the built-in MCP server; allowed_mcp ["agents"] makes a key that may only talk to agents.
Variables ¶
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 ¶
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 ValidRecording ¶
ValidRecording reports whether v is a recording level; "" keeps the default.
Types ¶
type Card ¶
type Card struct {
Description string `json:"description,omitempty"`
Skills []string `json:"skills,omitempty"`
Repos []string `json:"repos,omitempty"`
AcceptFrom []string `json:"accept_from,omitempty"`
}
Card is what an agent says about itself.
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
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.
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 ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service holds the policy; guard and journal may be nil.
func (*Service) AsOwner ¶
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 ¶
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.
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, with their messages.
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 ¶
Listen wakes waiting inbox and outbox calls when tasks change, on this replica or another, until ctx ends.
func (*Service) Me ¶
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 writes 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. 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.
type Store ¶
type Store interface {
UpsertAgent(ctx context.Context, a *proxydb.Agent) 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) error
AgentInContext(ctx context.Context, keyID, contextID string) (bool, error)
CreateAgentTask(ctx context.Context, t *proxydb.AgentTask, text string, maxMessages int64) 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.