跳转到内容

通过 SSE 暴露 HTTP

当你需要通过 HTTP + Server-Sent Events 对外提供 agent 服务,并挂上多种协议适配器(AI SDK、AG-UI、A2A、MCP)时,使用本页。

  • awaken 启用了 server feature
  • tokio 启用了 rt-multi-threadsignal
  • 已构建好一个 AgentRuntime
  1. 添加依赖:
[dependencies]
awaken = { git = "https://github.com/AwakenWorks/awaken", features = ["server"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "signal"] }
  1. 构建 runtime:
use std::sync::Arc;
use awaken::engine::GenaiExecutor;
use awaken::registry_spec::ModelSpec;
use awaken::{AgentRuntimeBuilder, AgentSpec};
use awaken::contract::commit_coordinator::CommitCoordinator;
use awaken::stores::{InMemoryStore, MemoryCommitCoordinator};
let store = Arc::new(InMemoryStore::new());
let coordinator = MemoryCommitCoordinator::wrap(store.clone()) as Arc<dyn CommitCoordinator>;
let runtime = AgentRuntimeBuilder::new()
.with_agent_spec(
AgentSpec::new("assistant")
.with_model_id("claude-sonnet")
.with_system_prompt("You are a helpful assistant."),
)
.with_tool("search", Arc::new(SearchTool))
.with_provider("anthropic", Arc::new(GenaiExecutor::new()))
.with_model(ModelSpec::new("claude-sonnet", "anthropic", "claude-sonnet-4-20250514"))
.with_commit_coordinator(coordinator)
.build()?;
let runtime = Arc::new(runtime);
  1. 创建应用状态:
use awaken::server::app::{ServerState, ServerConfig};
use awaken::server::mailbox::{Mailbox, MailboxConfig};
use awaken::stores::InMemoryMailboxStore;
let mailbox_store = Arc::new(InMemoryMailboxStore::new());
let mailbox = Arc::new(Mailbox::new(
runtime.clone(),
mailbox_store,
store.clone(),
"default-consumer".to_string(),
MailboxConfig::default(),
));
let state = ServerState::new(
runtime.clone(),
mailbox,
store,
runtime.resolver_arc(),
ServerConfig::default(),
);
  1. 构建 router:
use awaken::server::routes::build_router;
let app = build_router().with_state(state);

build_router() 会注册:

  • /health
  • /v1/threads
  • /v1/runs
  • /v1/config/*/v1/capabilities
  • AI SDK v6、AG-UI、A2A、MCP 路由
  1. 启动服务:
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
axum::serve(listener, app).await?;
  1. 配置 SSE buffer:
let config = ServerConfig {
address: "0.0.0.0:8080".into(),
sse_buffer_size: 128,
..ServerConfig::default()
};
Terminal window
curl http://localhost:3000/health

应返回 200 OK。然后可以创建 thread 并启动 run。

错误原因修复
端口已占用3000 已被其他进程使用ServerConfig.addressTcpListener::bind
SSE 立即断开客户端不支持 text/event-streamcurl --no-buffer 或标准 SSE 客户端
路由缺失没有启用 server feature确保 awaken 开启 features = ["server"]

crates/awaken-server/tests/run_api.rs

  • crates/awaken-server/src/app.rs
  • crates/awaken-server/src/routes.rs
  • crates/awaken-server/src/http_sse.rs
  • crates/awaken-server/src/mailbox.rs