Files
memoryweave/go/internal/api/routes/agent.go
T
xiaowei 1b0e1a549c fix: 蒸馏引擎 + 命名空间隔离 + PassiveValidator + ephemeral清理
- 蒸馏引擎: LLM调用需LLM_API_KEY(不再复用API_KEY), 新增flush日志
- namespace三层: shared/hermes-main/openclaw-main 隔离, Rust IPC client-side过滤
- PassiveValidator: 蒸馏完成回调中加载已有记忆进行P1/P2/P3匹配
- ephemeral清理: currentUnixMS修复, AgentInfo.LastSeen, 定时扫描
- Go代理: goproxy.cn配置
- 图谱: 孤儿边自动补节点, namespace兼容, 多跳导航前缀修复
- 召回管道: shared namespace联合搜索, recall_count跟踪
2026-05-29 08:04:57 +08:00

112 lines
2.5 KiB
Go

// 织忆 MemoryWeave — Agent 注册 API
package routes
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/xiaoxue/memoryweave/internal/distributed"
)
type AgentRegistry struct {
agents map[string]AgentInfo
limiter *distributed.RateLimiter
}
type AgentInfo struct {
AgentID string `json:"agent_id"`
APIKey string `json:"api_key,omitempty"`
Quota Quota `json:"quota"`
Registered string `json:"registered"`
LastSeen int64 `json:"last_seen"` // Unix millis
}
type Quota struct {
RecallQPS int `json:"recall_qps"`
CommitQPS int `json:"commit_qps"`
Burst int `json:"burst"`
}
func NewAgentRegistry(limiter *distributed.RateLimiter) *AgentRegistry {
return &AgentRegistry{
agents: make(map[string]AgentInfo),
limiter: limiter,
}
}
// POST /api/v1/agents/register
func (ar *AgentRegistry) Register(w http.ResponseWriter, r *http.Request) {
var req struct {
AgentID string `json:"agent_id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
respondError(w, 400, "invalid body")
return
}
if req.AgentID == "" {
respondError(w, 400, "agent_id required")
return
}
if _, exists := ar.agents[req.AgentID]; exists {
respondError(w, 409, "agent already registered")
return
}
apiKey := generateKey(32)
info := AgentInfo{
AgentID: req.AgentID,
APIKey: apiKey,
Quota: Quota{RecallQPS: 10, CommitQPS: 2, Burst: 20},
Registered: "now",
}
ar.agents[req.AgentID] = info
respond(w, 201, info)
}
// GET /api/v1/agents — 已注册 Agent 列表
func (ar *AgentRegistry) List(w http.ResponseWriter, r *http.Request) {
var list []AgentInfo
for _, info := range ar.agents {
info.APIKey = "" // 不泄漏 key
list = append(list, info)
}
respond(w, 200, map[string]interface{}{"agents": list, "count": len(list)})
}
// ListAgents 返回所有已注册 agent(用于 ephemeral 清理)
func (ar *AgentRegistry) ListAgents() []AgentInfo {
var list []AgentInfo
for _, info := range ar.agents {
list = append(list, info)
}
return list
}
// TouchAgent 更新 agent 最后活跃时间
func (ar *AgentRegistry) TouchAgent(agentID string) {
if info, ok := ar.agents[agentID]; ok {
info.LastSeen = time.Now().UnixMilli()
ar.agents[agentID] = info
}
}
// Auth 验证 API Key
func (ar *AgentRegistry) Auth(apiKey string) (*AgentInfo, bool) {
for _, info := range ar.agents {
if info.APIKey == apiKey {
return &info, true
}
}
return nil, false
}
func generateKey(length int) string {
b := make([]byte, length)
rand.Read(b)
return fmt.Sprintf("mw-%x", b)
}