relay

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TestDispatcherFactory func(app.AgentApplication) state.Dispatcher

TestDispatcherFactory 是 relay 包内 _test.go 文件构造测试 Handler 用的钩子。 由 package relay_test 的 init() 注入(典型实现:backend.NewDispatcher)。

生产代码不读它(生产 server.go 显式传 backend.NewDispatcher 给 NewHandler), 它存在的唯一目的是让 package relay 内的内部测试在不反向 import backend 子包的前提下也能拿到默认 dispatcher 实现,从而避免 relay → backend → relay 的 循环依赖。命名前缀 Test 也是为了让用户清楚这是 test-only 钩子。

Functions

func CloseContext added in v0.0.13

func CloseContext(rctx *state.RelayContext)

CloseContext releases request readers and captured replay resources. Both operations are idempotent, so callers may safely defer it on every path.

func ListModels

func ListModels(store *cache.Store) gin.HandlerFunc

ListModels returns the available models in OpenAI-compatible format.

业务规则在 modelview 包;handler 只负责取 UserInfo、调 modelview、套 OpenAI 响应壳。

func NewContext added in v0.0.13

func NewContext(c *gin.Context, agent app.AgentApplication) *state.RelayContext

NewContext constructs the request-scoped relay state shared by the ordinary relay pipeline and the bound-attempt proxy pipeline.

Types

type Handler

type Handler struct {
	Agent app.AgentApplication
	// contains filtered or unexported fields
}

Handler is the relay handler that routes requests to upstream AI providers through either the native codec path or the legacy new-api adaptor path.

Handler 只持有 App / Agent / 4 个 stage 实例, 主流程 Relay() 走 ctxBuilder → Solver → executor → publisher 4 阶段。 其它依赖(Store / Bus / Logger / RetryMax / Timeout / ...) 全部下沉到 backend / stage 内部从 rctx.Agent 间接取;EventBus 由 NewHandler 调用方(server.go)显式从 app.Application 取出后传入,再交给 publish.Publisher 持有。

func NewHandler

func NewHandler(
	bus app.EventBus,
	agentApp app.AgentApplication,
	dispatcher state.Dispatcher,
	registry *inflight.Registry,
	permit limiter.PermitStore,
	registries *resilience.RuntimeRegistries,
	options ...HandlerOption,
) *Handler

NewHandler 是规范构造器: 入参 bus / agentApp / dispatcher 三个最小依赖一次性装配 stage 实例 (ctxBuilder/planner/executor/publisher)。

任意入参为 nil 时构造本身不 panic,但调用 Relay() 时具体哪一阶段触发依赖可能 nil deref。 生产链路由 server.go 显式装配两侧;测试链路可注入 stub。

dispatcher 由调用方显式注入(生产为 backend.NewDispatcher(agentApp))。 这一外置是为了避免 relay 包反向 import backend 子包导致的循环依赖。

bus 入参收窄自 app.Application(17 方法)→ app.EventBus(1 方法), 因为 NewHandler 只需要 EventBus 一项;让测试 stub 不必满足 Application 全集。

func (*Handler) ProviderAttemptExecutor added in v0.0.13

func (h *Handler) ProviderAttemptExecutor() attemptexec.ProviderAttemptExecutor

func (*Handler) Relay

func (h *Handler) Relay(c *gin.Context)

Relay is the generic handler for all AI API endpoints. It's a 4-stage pipeline orchestrator: ctxBuilder -> planner -> executor -> publisher,配合一个 writeResponse 兜底。

错误流:任一 stage 失败立刻 set FailPhase + Err 并跳到 publisher;publisher 决定 填多少 UsageLogEntry 字段,writeResponse 决定 HTTP 状态码 + 响应体。

type HandlerOption added in v0.0.13

type HandlerOption func(*handlerOptions)

func WithAttemptRouting added in v0.0.13

func WithAttemptRouting(
	sourceAgentID string,
	routes exec.AttemptRouteBuilder,
	local exec.LocalAttemptExecutor,
	remote exec.RemoteAttemptExecutor,
) HandlerOption

Directories

Path Synopsis
Package affinity 实现 agent 本地的渠道缓存粘性:把 (用户, API token, 真实模型) 上次发生 缓存活动的上游 channel 记下来,供 plan 阶段优先选用。
Package affinity 实现 agent 本地的渠道缓存粘性:把 (用户, API token, 真实模型) 上次发生 缓存活动的上游 channel 记下来,供 plan 阶段优先选用。
Package backend defines the Backend contract and the Dispatcher that routes each Attempt to one of native / passthrough / legacy implementations based on Attempt.Mode.
Package backend defines the Backend contract and the Dispatcher that routes each Attempt to one of native / passthrough / legacy implementations based on Attempt.Mode.
common
Package common 提供 backend(native / passthrough)共用的工具。
Package common 提供 backend(native / passthrough)共用的工具。
Package dataflow 把 channel 内部、llmkit 编码前的公共 IR 请求处理 表达成一串独立的 Step,由 ChannelDataFlow 统一运行与描述。
Package dataflow 把 channel 内部、llmkit 编码前的公共 IR 请求处理 表达成一串独立的 Step,由 ChannelDataFlow 统一运行与描述。
Package inflight 跟踪 agent 当前在途的 relay 请求,供看门狗告警与 master 远程诊断。
Package inflight 跟踪 agent 当前在途的 relay 请求,供看门狗告警与 master 远程诊断。
Package modelview 计算"当前 caller 在 /v1/models 中应见的模型列表"。
Package modelview 计算"当前 caller 在 /v1/models 中应见的模型列表"。
pipeline
ctxbuild
Package ctxbuild 是 relay pipeline 的 Phase 1:把 *gin.Context 装配成 state.RelayContext.Input 的请求级 immutable 输入。
Package ctxbuild 是 relay pipeline 的 Phase 1:把 *gin.Context 装配成 state.RelayContext.Input 的请求级 immutable 输入。
publish
Package publish 是 relay pipeline 的第 4 阶段(最末端):把累加好的 RelayContext 状态 一次性收成 protocol.UsageLogEntry 并经 EventBus 发布 usage.completed。
Package publish 是 relay pipeline 的第 4 阶段(最末端):把累加好的 RelayContext 状态 一次性收成 protocol.UsageLogEntry 并经 EventBus 发布 usage.completed。
Package resilience 给单次 channel dispatch 套重试/熔断/超时。
Package resilience 给单次 channel dispatch 套重试/熔断/超时。
Package script 实现管理员动态 goja 脚本的编译与执行。
Package script 实现管理员动态 goja 脚本的编译与执行。
Package state 是 relay 子包之间共享的叶子包,承载跨包共享的数据结构、 pipeline phase 枚举、relay mode 枚举、Dispatcher 接口以及哨兵 error。
Package state 是 relay 子包之间共享的叶子包,承载跨包共享的数据结构、 pipeline phase 枚举、relay mode 枚举、Dispatcher 接口以及哨兵 error。
Package transform provides IR-level request transformers (system prompt injection, role mapping, thinking pass-through/strip) that the relay pipeline applies between inbound decode and outbound encode.
Package transform provides IR-level request transformers (system prompt injection, role mapping, thinking pass-through/strip) that the relay pipeline applies between inbound decode and outbound encode.

Jump to

Keyboard shortcuts

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