AsterOS 是 Aster 框架的统一运行时系统,提供多智能体协作的完整解决方案。它管理所有 Agents、Stars、Workflows,并自动生成 REST API 端点,支持多种 Interface 类型。
package main
import (
"context"
"log"
"github.com/astercloud/aster/pkg/asteros"
"github.com/astercloud/aster/pkg/cosmos"
)
func main() {
// 创建依赖
deps := createDependencies()
// 创建 Cosmos
cosmos := cosmos.New(&cosmos.Options{
Dependencies: deps,
MaxAgents: 10,
})
// 创建 AsterOS
os, err := asteros.New(&asteros.Options{
Name: "MyAsterOS",
Port: 8080,
Cosmos: cosmos,
})
if err != nil {
log.Fatal(err)
}
// 启动服务
if err := os.Serve(); err != nil {
log.Fatal(err)
}
}
// 创建 Agent
agentConfig := &types.AgentConfig{
AgentID: "chat-agent",
TemplateID: "chat-template",
ModelConfig: &types.ModelConfig{
Provider: "anthropic",
Model: "claude-sonnet-4-5",
APIKey: "your-api-key",
},
}
ag, err := agent.Create(ctx, agentConfig, deps)
if err != nil {
log.Fatal(err)
}
// 注册到 AsterOS
if err := os.RegisterAgent("chat-agent", ag); err != nil {
log.Fatal(err)
}
// 创建 Stars
starsInstance := stars.New(cosmos, "ChatTeam")
// 添加成员
if err := starsInstance.AddMember("leader", "agent-1", "leader"); err != nil {
log.Fatal(err)
}
if err := starsInstance.AddMember("worker", "agent-2", "worker"); err != nil {
log.Fatal(err)
}
// 注册到 AsterOS
if err := os.RegisterStars("chat-team", starsInstance); err != nil {
log.Fatal(err)
}
AsterOS 自动生成以下 REST API 端点:
GET /api/agents # 列出所有 Agent
POST /api/agents/{id}/run # 运行指定 Agent
GET /api/agents/{id}/status # 获取 Agent 状态
GET /api/stars # 列出所有 Stars
POST /api/stars/{id}/run # 运行 Stars 协作
POST /api/stars/{id}/join # 加入 Stars
POST /api/stars/{id}/leave # 离开 Stars
GET /api/stars/{id}/members # 获取成员列表
GET /api/workflows # 列出所有 Workflow
POST /api/workflows/{id}/execute # 执行 Workflow
GET /health # 健康检查
GET /metrics # Prometheus 指标
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ HTTP API │ │ A2A Interface │ │ AGUI Interface│
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
┌─────────────────────────────────────────────────────────────────┐
│ AsterOS Registry │
├─────────────────┬─────────────────┬─────────────────────────────┤
│ Agents │ Stars │ Workflows │
└─────────────────┴─────────────────┴─────────────────────────────┘
│ │ │
┌─────────────────────────────────────────────────────────────────┐
│ Cosmos │
│ (Agent Manager) │
└─────────────────────────────────────────────────────────────────┘
type Interface interface {
// 基本信息
Name() string
Type() InterfaceType
// 生命周期管理
Start(ctx context.Context, os *AsterOS) error
Stop(ctx context.Context) error
// 事件回调
OnAgentRegistered(agent *agent.Agent) error
OnStarsRegistered(stars *stars.Stars) error
OnWorkflowRegistered(wf workflow.Agent) error
}
type Options struct {
// 基本配置
Name string // AsterOS 名称
Port int // HTTP 端口 (默认: 8080)
Cosmos *cosmos.Cosmos // Cosmos 实例 (必需)
// API 配置
APIPrefix string // API 路径前缀 (默认: /api)
// 功能开关
AutoDiscover bool // 自动发现 (默认: true)
EnableCORS bool // CORS 支持 (默认: true)
EnableAuth bool // 认证授权 (默认: false)
EnableMetrics bool // 指标监控 (默认: true)
EnableHealth bool // 健康检查 (默认: true)
EnableLogging bool // 日志记录 (默认: true)
// 认证配置
APIKey string // API 密钥
// 日志配置
LogLevel string // 日志级别 (默认: info)
}
type MyInterface struct {
BaseInterface
// 自定义字段
}
func (i *MyInterface) Start(ctx context.Context, os *AsterOS) error {
// 自定义启动逻辑
return nil
}
func (i *MyInterface) OnAgentRegistered(agent *agent.Agent) error {
// 自定义 Agent 注册处理
return nil
}
// 添加 HTTP Interface (默认已包含)
httpIface := asteros.NewHTTPInterface()
os.AddInterface(httpIface)
// 添加 A2A Interface
a2aIface := asteros.NewA2AInterface()
os.AddInterface(a2aIface)
// 添加 AGUI Interface
aguiIface := asteros.NewAGUIInterface("/agui")
os.AddInterface(aguiIface)
curl http://localhost:8080/health
curl http://localhost:8080/metrics
AsterOS 使用结构化日志,支持不同级别:
🌟 AsterOS 'MyAsterOS' is running on http://localhost:8080
[Agent Create] Total tools loaded: 5
[Stars Join] Agent 'worker-1' joined stars 'chat-team'
// 启用认证
os, err := asteros.New(&asteros.Options{
EnableAuth: true,
APIKey: "your-secret-api-key",
})
// 启用 CORS (默认已启用)
os, err := asteros.New(&asteros.Options{
EnableCORS: true,
})
查看 examples/asteros/ 目录下的完整示例:
basic/: 基本 AsterOS 使用interfaces/: 多种 Interface 使用示例collaboration/: Stars 协作示例