Implements the foundational layer that all SynapBus features depend on: - internal/storage: SQLite connection manager (WAL mode, busy_timeout, foreign_keys) and embedded migration runner using modernc.org/sqlite - internal/messaging: MessagingService with send, read inbox, claim, mark done/failed, and FTS5 search. SQLite-backed MessageStore with conversation auto-creation and read/unread tracking via inbox_state. - internal/agents: AgentService with register, authenticate (bcrypt), update, deregister, discover by capability. HTTP auth middleware. - internal/mcp: MCP server using mark3labs/mcp-go with 9 registered tools (send_message, read_inbox, claim_messages, mark_done, search_messages, register_agent, discover_agents, update_agent, deregister_agent). SSE transport, health endpoint, connection manager. - internal/trace: Async trace recorder with buffered channel for recording agent actions to SQLite traces table. - cmd/synapbus: Updated main.go wiring storage, migrations, services, MCP server, chi router, and graceful shutdown. All code compiles with CGO_ENABLED=0. Full test suite passes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
177 lines
5.3 KiB
Go
177 lines
5.3 KiB
Go
package agents
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// AgentStore defines the storage interface for agent operations.
|
|
type AgentStore interface {
|
|
CreateAgent(ctx context.Context, agent *Agent) error
|
|
GetAgentByName(ctx context.Context, name string) (*Agent, error)
|
|
GetAgentByID(ctx context.Context, id int64) (*Agent, error)
|
|
UpdateAgent(ctx context.Context, agent *Agent) error
|
|
DeactivateAgent(ctx context.Context, name string) error
|
|
ListActiveAgents(ctx context.Context) ([]*Agent, error)
|
|
ListAgentsByOwner(ctx context.Context, ownerID int64) ([]*Agent, error)
|
|
SearchAgentsByCapability(ctx context.Context, query string) ([]*Agent, error)
|
|
}
|
|
|
|
// SQLiteAgentStore implements AgentStore using SQLite.
|
|
type SQLiteAgentStore struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
// NewSQLiteAgentStore creates a new SQLite-backed agent store.
|
|
func NewSQLiteAgentStore(db *sql.DB) *SQLiteAgentStore {
|
|
return &SQLiteAgentStore{db: db}
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) CreateAgent(ctx context.Context, agent *Agent) error {
|
|
caps := string(agent.Capabilities)
|
|
if caps == "" {
|
|
caps = "{}"
|
|
}
|
|
|
|
result, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
|
|
agent.Name, agent.DisplayName, agent.Type, caps, agent.OwnerID, agent.APIKeyHash, AgentStatusActive,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("insert agent: %w", err)
|
|
}
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return fmt.Errorf("get agent id: %w", err)
|
|
}
|
|
agent.ID = id
|
|
agent.Status = AgentStatusActive
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) GetAgentByName(ctx context.Context, name string) (*Agent, error) {
|
|
return s.scanAgent(s.db.QueryRowContext(ctx,
|
|
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
|
FROM agents WHERE name = ? AND status = 'active'`, name,
|
|
))
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) GetAgentByID(ctx context.Context, id int64) (*Agent, error) {
|
|
return s.scanAgent(s.db.QueryRowContext(ctx,
|
|
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
|
FROM agents WHERE id = ? AND status = 'active'`, id,
|
|
))
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) UpdateAgent(ctx context.Context, agent *Agent) error {
|
|
caps := string(agent.Capabilities)
|
|
if caps == "" {
|
|
caps = "{}"
|
|
}
|
|
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE agents SET display_name = ?, capabilities = ?, api_key_hash = ?, updated_at = CURRENT_TIMESTAMP
|
|
WHERE name = ? AND status = 'active'`,
|
|
agent.DisplayName, caps, agent.APIKeyHash, agent.Name,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) DeactivateAgent(ctx context.Context, name string) error {
|
|
result, err := s.db.ExecContext(ctx,
|
|
`UPDATE agents SET status = 'inactive', updated_at = CURRENT_TIMESTAMP
|
|
WHERE name = ? AND status = 'active'`,
|
|
name,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("deactivate agent: %w", err)
|
|
}
|
|
rowsAffected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rowsAffected == 0 {
|
|
return fmt.Errorf("agent not found: %s", name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) ListActiveAgents(ctx context.Context) ([]*Agent, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
|
FROM agents WHERE status = 'active' ORDER BY name`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
return s.scanAgents(rows)
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) ListAgentsByOwner(ctx context.Context, ownerID int64) ([]*Agent, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
|
FROM agents WHERE owner_id = ? AND status = 'active' ORDER BY name`,
|
|
ownerID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
return s.scanAgents(rows)
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) SearchAgentsByCapability(ctx context.Context, query string) ([]*Agent, error) {
|
|
// Simple LIKE search on the capabilities JSON field
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
|
FROM agents WHERE status = 'active' AND capabilities LIKE ? ORDER BY name`,
|
|
"%"+query+"%",
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
return s.scanAgents(rows)
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) scanAgent(row *sql.Row) (*Agent, error) {
|
|
var agent Agent
|
|
var caps string
|
|
err := row.Scan(
|
|
&agent.ID, &agent.Name, &agent.DisplayName, &agent.Type,
|
|
&caps, &agent.OwnerID, &agent.APIKeyHash, &agent.Status,
|
|
&agent.CreatedAt, &agent.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
agent.Capabilities = json.RawMessage(caps)
|
|
return &agent, nil
|
|
}
|
|
|
|
func (s *SQLiteAgentStore) scanAgents(rows *sql.Rows) ([]*Agent, error) {
|
|
var agents []*Agent
|
|
for rows.Next() {
|
|
var agent Agent
|
|
var caps string
|
|
err := rows.Scan(
|
|
&agent.ID, &agent.Name, &agent.DisplayName, &agent.Type,
|
|
&caps, &agent.OwnerID, &agent.APIKeyHash, &agent.Status,
|
|
&agent.CreatedAt, &agent.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
agent.Capabilities = json.RawMessage(caps)
|
|
agents = append(agents, &agent)
|
|
}
|
|
if agents == nil {
|
|
agents = []*Agent{}
|
|
}
|
|
return agents, rows.Err()
|
|
}
|