Files
memoryweave/go/internal/api/routes/agent.go
T
xiaowei 2a350cf781 fix(distill): max_tokens 800→1200 — Agnes推理token预算不足
Agnes 2.0-flash是推理模型,推理token占大量预算。
800仍不够→JSON截断(parse error: unexpected end of JSON input)。
1200确保推理+正文都够用。
2026-08-20 01:13:06 +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)
}