api

package
v0.0.0-...-c3c5a4c Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 58 Imported by: 0

README

node/internal/api

Agent Node 对本地 Client 暴露的 HTTP/SSE 端点(composition root)。

职责

文件 说明
server.go Server 结构、Option 和 NewServer 组合根
server_routes.go 统一挂载核心与领域路由
server_wiring.go Node 级依赖注入、执行事件审计、后台任务回灌
server_lifecycle.go HTTP 监听、Manage sidecar、优雅关闭
server_messages.go /v1/messages 入队与 /v1/streams SSE
server_agent_api.go health、Agent 信息、context、hydrate、skills 等基础接口
*_test.go HTTP 集成单测(agents、SSE、child agent、triggers)

边界:本包只做请求解析、JSON 映射、错误码;turn 执行与队列消费委托 session.Manager。

主要路由(摘要)

方法 路径 委托
CRUD /v1/agents Agent 主契约(ensure / hydrate / context / cancel / skills / child-agents / media / policy)
POST /v1/messages 入队 user / resume
GET /v1/streams SSE 订阅 stream.Hub
CRUD /v1/triggers triggers 调度与 fire

/v1/sessions* 路由已删除(404)。

完整契约见 docs/architecture/agent-node-api.md。

相关文档

Terminal WebSocket

GET /v1/agents/{agent_id}/terminals/ws opens one PTY per WebSocket connection. The first frame is open; later frames are input, resize, terminate, or close. JSON data fields use base64. resize is an internal UI/PTY operation, not an Agent tool. terminate sends Ctrl+C first, then closes the PTY if the process does not exit within the grace period.

Interactive terminal sessions are reaped after 10 minutes without input or PTY output. Reading an unchanged snapshot does not refresh this idle timer; Node restart always starts with an empty in-memory session registry.

Documentation

Overview

Package api 提供 Agent Node 对本地 Client 暴露的 HTTP/SSE 端点。

职责边界:路由解析、请求/响应 JSON、错误码映射;session 队列与 turn 执行委托 session.Manager。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DreamingScheduler

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

DreamingScheduler owns only the daily dreaming loop. Wake triggers and their interval do not participate in its due calculation.

func NewDreamingScheduler

func NewDreamingScheduler(a *autonomy.Store, agents *store.AgentStore, sessions *session.Manager, ensure func(context.Context, string) error) *DreamingScheduler

func (*DreamingScheduler) CurrentStatus

func (d *DreamingScheduler) CurrentStatus(agentID string, now time.Time) (result DreamingStatus)

func (*DreamingScheduler) Start

func (d *DreamingScheduler) Start(ctx context.Context)

func (*DreamingScheduler) Status

func (d *DreamingScheduler) Status(agentID string) DreamingStatus

func (*DreamingScheduler) Stop

func (d *DreamingScheduler) Stop()

func (*DreamingScheduler) Tick

func (d *DreamingScheduler) Tick(ctx context.Context) error

type DreamingStatus

type DreamingStatus struct {
	State       string    `json:"state"`
	NextAt      time.Time `json:"next_at,omitempty"`
	LastSuccess time.Time `json:"last_success,omitempty"`
	LastError   string    `json:"last_error,omitempty"`
}

func (DreamingStatus) MarshalJSON

func (s DreamingStatus) MarshalJSON() ([]byte, error)

type Option

type Option func(*serverOptions)

Option 为 NewServer 可选配置。

func WithConfigPath

func WithConfigPath(path string) Option

WithConfigPath 记录 Node 启动时加载的 config.yaml 路径(供 Web UI 保存设置)。

func WithDirectoryPicker

func WithDirectoryPicker(picker platform.DirectoryPicker) Option

WithDirectoryPicker injects the Node-native picker implementation (tests use this to avoid opening a real system dialog).

func WithLLM

func WithLLM(client llm.Client) Option

WithLLM 注入 LLM 客户端(单测/mock 用)。

func WithNodeSettings

func WithNodeSettings(ns *store.NodeSettingsStore) Option

WithNodeSettings 注入 Node 设置库(由 main BootstrapNodeSettings 打开)。

func WithPolicy

func WithPolicy(engine *policy.Engine) Option

WithPolicy 注入策略引擎(单测用)。

func WithSkipStore

func WithSkipStore() Option

WithSkipStore 禁用持久化(单测无需落盘时使用)。

func WithStore

func WithStore(st *store.SQLiteStore) Option

WithStore 注入 SQLite store(单测用);传 nil 且 WithSkipStore 时禁用持久化。

func WithTools

func WithTools(registry *tools.Registry) Option

WithTools 注入工具 registry(单测用)。

type Server

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

Server 承载 Agent Node HTTP 路由与运行时依赖。

func NewServer

func NewServer(cfg *config.Config, logger *slog.Logger, opts ...Option) *Server

NewServer 根据已校验配置构造 HTTP 处理器树。

逻辑: 1. 装配 LLM、tools、policy、SQLite(可被 Option 覆盖); 2. 创建 SSE Hub 与 session.Manager,挂载 turn/skills/compression 选项; 3. 可选初始化 triggers store/scheduler 与 bash 后台任务回灌; 4. 注册 /health、/v1/agents、/v1/messages、/v1/streams 等路由。

关键边界:子系统初始化失败时尽量降级(tools/policy/store/triggers 打日志后继续),避免整进程无法启动。

func (*Server) Close

func (s *Server) Close()

Close 释放 SQLite / 会话等资源。ListenAndServe 退出路径会调用;httptest 测试需显式 Cleanup。

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler 返回可用于 http.Server 的根 Handler(含 onboarding gate 与 access log)。

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context) error

ListenAndServe 在配置的 listen 地址启动 HTTP 服务;ctx 取消时触发优雅关闭。

生命周期分为两段: 1. 先启动 Manage sidecar 和 HTTP listener; 2. ctx 取消后停止后台运行时、关闭持久化资源,最后关闭 HTTP listener。

Jump to

Keyboard shortcuts

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