通过 SSE 暴露 HTTP
当你需要通过 HTTP + Server-Sent Events 对外提供 agent 服务,并挂上多种协议适配器(AI SDK、AG-UI、A2A、MCP)时,使用本页。
awaken启用了serverfeaturetokio启用了rt-multi-thread和signal- 已构建好一个
AgentRuntime
- 添加依赖:
[dependencies]awaken = { git = "https://github.com/AwakenWorks/awaken", features = ["server"] }tokio = { version = "1", features = ["rt-multi-thread", "macros", "signal"] }- 构建 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);- 创建应用状态:
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(),);- 构建 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 路由
- 启动服务:
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;axum::serve(listener, app).await?;- 配置 SSE buffer:
let config = ServerConfig { address: "0.0.0.0:8080".into(), sse_buffer_size: 128, ..ServerConfig::default()};curl http://localhost:3000/health应返回 200 OK。然后可以创建 thread 并启动 run。
| 错误 | 原因 | 修复 |
|---|---|---|
| 端口已占用 | 3000 已被其他进程使用 | 改 ServerConfig.address 或 TcpListener::bind |
| SSE 立即断开 | 客户端不支持 text/event-stream | 用 curl --no-buffer 或标准 SSE 客户端 |
| 路由缺失 | 没有启用 server feature | 确保 awaken 开启 features = ["server"] |
crates/awaken-server/tests/run_api.rs
crates/awaken-server/src/app.rscrates/awaken-server/src/routes.rscrates/awaken-server/src/http_sse.rscrates/awaken-server/src/mailbox.rs