serving

package
v0.0.0-...-1c978d5 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

decorate.go:出站装饰与进度订阅的第三方扩展面 (设计见 docs/channel-card-design.md §2.4/§3)。

框架给事实(Outbound 语义字段、ProgressEvent 事件流),呈现策略 100% 归第三方:装饰器改文案、构造 Native 整卡、或置 Skip 否决发送; 进度订阅者拿原始事件自行控制节奏与形态。嵌入方直接塞函数值到 Binding;配置方经按名注册表(init 自注册、运行期只读、装配期查名 fail fast,与 model/source 同一惯例)。

deliver.go:交付物出站解析——"引用即附带"。终答里引用的 #dN 由框架 展开随行(大脑只行使策展权);always 语义不待引用恒随行。 设计:docs/deliverable-channel-plan.md §2.3。

lifecycle.go:一轮 IM 回复的生命周期驱动 (设计见 docs/channel-card-design.md §3.1/§4)。

生命周期全模式统一:processing 首发 → 过程更新 → answer/question/ error 收口,每一步构造语义 Outbound 送进装饰器,发不发由装饰器 Skip 决定;无装饰器时按 ReplyMode 内置默认策略(text 跳过 processing、card 占位+更新)。占位被 Skip/发送失败则后续自动 Send 而非 Update——机械处理,装饰器不用关心。

Package serving 是 Gateway:一个进程同时暴露三张脸——

  • 对人/前端:POST /agents/{name}/messages(JSON 或 SSE 流式);
  • 对其他 agent:A2A 协议(GET /a2a/agents、POST /a2a/agents/{name}/tasks), 与 provider/a2a 的消费端同一协议,agentkit 部署之间天然互通;
  • 对 IM:承载各 channel 的 webhook 路由。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterContextHook

func RegisterContextHook(h ContextHook)

RegisterContextHook 注册一个入口层 context 丰富器(init/装配期调用)。多个 钩子按注册顺序叠加;nil 忽略。注册后只读,契合"注册表装配期只读"约束。

func RegisterDecorator

func RegisterDecorator(name string, d Decorator)

RegisterDecorator 按名注册装饰器(init 期调用;重名 panic,装配期暴露)。

func RegisterProgressHandler

func RegisterProgressHandler(name string, h ProgressHandler)

RegisterProgressHandler 按名注册进度订阅者。

func ResolveDeliverables

func ResolveDeliverables(answer string, sink *runctx.DeliverableSink) []runctx.Deliverable

ResolveDeliverables 是裸跑宿主(CLI 等自己调 agent.Run 的场景)用的 出站解析入口,语义同 dispatcher/HTTP 内部路径。

Types

type Binding

type Binding struct {
	Channel channel.Channel
	Agent   Runnable
	// SessionMapping:chat(群共享会话)| chat_user(群内每人独立会话)。
	SessionMapping string
	// ReplyMode 是**无装饰器时的内置默认策略**:text(整段回复,跳过
	// processing)| card(占位卡+原地更新)| stream(占位+流式刷新)。
	// 装了 Decorator 后生命周期全模式统一驱动,发不发由装饰器 Skip 定。
	// card/stream 需通道支持 Update,不支持自动退化为整段。
	ReplyMode string
	// AskTimeout 是 ask_user / 审批等待用户回复的超时,默认 10 分钟。
	AskTimeout time.Duration
	// Placeholder 是 processing 占位文案的快捷覆盖,空 = 取 Texts.Placeholder
	// (再空则英文默认「⏳ Working…」)。
	Placeholder string
	// Texts 覆盖面向用户的文案(nil = 全英文默认);部分填充即可,空字段
	// 回落默认。IM 部署(如中文机器人)在此配中文。
	Texts *Texts
	// Decorator 装饰每条出站消息(nil = 不装饰,按 ReplyMode 默认策略)。
	Decorator Decorator
	// OnProgress 是第三方进度订阅(装了它,内置的卡片过程更新让位)。
	OnProgress ProgressHandler
}

Binding 把一个 channel.Channel 路由到一个 Agent。

type ContextHook

type ContextHook func(ctx context.Context, info InboundInfo) context.Context

ContextHook 在**每个入站轮次**执行 agent 前、于入口边界丰富其 context—— HTTP /messages、A2A、IM 三个入口统一经过。第三方注册它,把传输层 baggage (trace logid、租户、correlation id 等)提升/派生进 context,从而一路流到 agent、工具与 decorator。

调用时机:ctx 已含**存活的传输 baggage**(入口用 WithoutCancel 保值)与 runctx 用户身份;钩子在此之上叠加。返回丰富后的 ctx;不需要改动则原样返回。

type Decorator

type Decorator func(ctx context.Context, conv channel.ConvRef, out channel.Outbound) channel.Outbound

Decorator 应用于每条出站消息(占位/过程更新/终稿/问句/错误/通知), 在 channel.Send/Update 之前调用。读语义事实(Kind/Text/Progress/ Meta),三种产出:改写语义字段(适配器默认渲染)、构造 Native (适配器原样透传)、置 Skip(本步不发送)。

func LookupDecorator

func LookupDecorator(name string) (Decorator, error)

LookupDecorator 按名解析装饰器(装配层用,查无报错 fail fast)。

type Dispatcher

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

Dispatcher 承接所有 Binding 的消息分发:

  • 同会话串行、跨会话并发(每会话一个 worker 队列);
  • event_id 幂等去重(平台会重试投递);
  • 挂起中的 ask_user / 审批问题优先截获该会话的下一条消息。

func NewDispatcher

func NewDispatcher(logger *slog.Logger) *Dispatcher

NewDispatcher 创建分发器,并确保进度事件发射切面已挂载(幂等)。

func (*Dispatcher) EnableSuspend

func (d *Dispatcher) EnableSuspend(kv store.KV)

EnableSuspend 启用挂起模式:交互等待持久化到注入的 KV 后端,跨进程 重启可恢复。挂起模式与流式回复不兼容,启用后流式绑定退化为整段回复。

func (*Dispatcher) Handler

func (d *Dispatcher) Handler(b Binding) channel.InboundHandler

Handler 返回绑定到 b 的 channel.InboundHandler,交给 channel.Channel.Start。

type InboundInfo

type InboundInfo struct {
	Channel string // "http" | "a2a" | 通道名(如 feishu)
	User    string // 终端用户身份(可空)
	Session string // 会话标识
}

InboundInfo 是交给 ContextHook 的传输中立请求身份(HTTP/A2A/IM 统一口径)。

type ProgressHandler

type ProgressHandler func(ctx context.Context, conv channel.ConvRef, ev runctx.ProgressEvent)

ProgressHandler 是绑定级进度订阅者:装了它,内置的卡片过程更新让位, IM 呈现完全由第三方接管。投递经异步有界队列,慢/挂不影响执行主流程。

func LookupProgressHandler

func LookupProgressHandler(name string) (ProgressHandler, error)

LookupProgressHandler 按名解析进度订阅者。

type Runnable

type Runnable interface {
	Name() string
	Description() string
	Run(ctx context.Context, sessionID, input string) (string, error)
	Stream(ctx context.Context, sessionID, input string) (*schema.StreamReader[*schema.Message], error)
	// Interrupt 叫停会话当前运行;Steer 向运行中注入插话。均幂等,无运行时为空操作。
	Interrupt(sessionID string)
	Steer(sessionID, msg string)
}

Runnable 是分发层/服务层对 agent 的最小契约(消费方定义): 运行一轮、流式一轮、身份描述、运行控制。*agent.Agent 天然实现; 服务层只依赖此接口,测试与装饰(代理/限流/审计包装)无需真 Agent。

type Server

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

Server 是 Gateway 实例。

func New

func New(addr string, agents []Runnable, logger *slog.Logger) *Server

New 创建 Gateway 并注册 agent 路由。

func (*Server) AttachChannel

func (s *Server) AttachChannel(ctx context.Context, ch channel.Channel, d *Dispatcher, b Binding) error

AttachChannel 把一个 channel 绑定到 agent 并挂载其 webhook。

func (*Server) EnableSuspend

func (s *Server) EnableSuspend(kv store.KV)

EnableSuspend 启用 /messages 的持久化挂起,后端与 IM 通道共用。

func (*Server) Mux

func (s *Server) Mux() *http.ServeMux

Mux 暴露路由器,channel 的 webhook 注册在此。

func (*Server) Run

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

Run 启动服务,阻塞直到 ctx 取消。

type Texts

type Texts struct {
	Placeholder string // processing placeholder (no verbs)
	StepRunning string // in-progress step line: %s = step name
	StepDone    string // finished step line: %s name, %.1f seconds
	StepFailed  string // failed step line: %s name, %.1f seconds
	Summary     string // completion meta: %.1f seconds, %d tool calls
	Stopped     string // ack for a stop command (no verbs)
	Steered     string // ack for a steer command (no verbs)
	Overloaded  string // session queue full (no verbs)
	Thinking    string // stream placeholder (no verbs)
	Suspended   string // waiting-for-reply close line (no verbs)
	Failure     string // error close line: %s = error text
	Approval    string // approval question: %s description, %s arguments
	AskTimeout  string // ask/approval reply timeout (no verbs)
	Deliverable string // deliverable follow-up header: %s id, %s title; empty format = content only
}

Texts holds every end-user-facing string a channel emits, so a deployment can translate or reword them per binding without forking the framework. Defaults are English (see defaultTexts); a Chinese-facing IM bot overrides the fields it cares about. Fields whose comment shows % verbs are fmt templates — preserve the verbs and their order when overriding.

func NewTexts

func NewTexts(overrides map[string]string) (*Texts, error)

NewTexts builds an override Texts from a map of snake_case field names (placeholder, step_running, step_done, step_failed, summary, stopped, steered, overloaded, thinking, suspended, failure, approval, ask_timeout). Unknown keys fail fast so a typo in YAML is caught at assembly. An empty or nil map returns nil (use the English default).

Jump to

Keyboard shortcuts

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