16a6c4b1aa
专用「审批」节点方案,全栈打通。 后端: - contract:TaskWaiting/TaskRejected 状态 + ApprovalSubject/ApprovalDecision。 - bus:PublishApproval + WaitApproval(订阅决定主题,带超时);消费者 AckWait 30s→15min(阻塞等人审期间消息未 ack,否则重投成重复任务)。 - dispatcher:ApprovalWaiter 接口 + approvalNode——执行到审批节点发 await 事件 + 置 waiting,阻塞等决定。批准→回 running 放行下游;拒绝/超时→errRejected 哨兵→ 剪下游→rejected,优雅收尾不计熔断。超时安全默认拒绝。 taskExecTimeout 3→10min(审批5 < 执行10 < AckWait15)。 - 网关:POST /tasks/:id/approve(仅 waiting 态受理,幂等)。 桌面端: - Studio 新增「人工审批」节点(nodeCatalog,可填标题/说明)。 - run.ts pendingApproval() 从 exec 流派生待审中断 + waiting 节点状态。 - BottomDrawer ApprovalBar:琥珀审批条(摘要 + 批准/拒绝 + 备注,调 api.approveTask)。 - ExecTrace waiting 状态灯。 验证:后端 curl 实测 waiting→批准→running→done;waiting→拒绝→rejected(下游未跑)。 前端 tsc + vitest 36 过(pendingApproval 4 例 + waiting 状态)。全模块 build+vet+test 全绿。 EINO_ADOPTION Phase D 标记 HITL 完成。 未覆盖:compose 路径(EINO_COMPOSE,默认关)的 approval 节点;桌面端审批条未在 GUI 实点。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
88 lines
3.2 KiB
Go
88 lines
3.2 KiB
Go
// Command dispatcher 启动 sundynix-dispatcher —— 第 4 层 AI Agent 调度集群。
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/sundynix/sundynix-dispatcher/internal/eino"
|
|
"github.com/sundynix/sundynix-dispatcher/internal/harness"
|
|
"github.com/sundynix/sundynix-dispatcher/internal/llm"
|
|
dnats "github.com/sundynix/sundynix-dispatcher/internal/nats"
|
|
"github.com/sundynix/sundynix-shared/otelx"
|
|
"github.com/sundynix/sundynix-shared/secrets"
|
|
)
|
|
|
|
func main() {
|
|
secrets.MustHaveKeyInProd() // 生产须配 SUNDYNIX_SECRET_KEY 以解密下发的 api_key 密文
|
|
otelx.SetupSlog("sundynix-dispatcher") // 结构化日志 + 链路感知(任务日志带 trace_id)
|
|
|
|
// 链路追踪:消费 span 续上 gateway 的 trace,再向下展开节点/工具/LLM span。
|
|
shutdownTrace, _ := otelx.Init(context.Background(), "sundynix-dispatcher")
|
|
defer func() { _ = shutdownTrace(context.Background()) }()
|
|
|
|
natsURL := envOr("NATS_URL", "nats://localhost:4222")
|
|
|
|
pool := llm.NewPool() // LLM Pool: vLLM / Ollama 集群
|
|
breaker := harness.NewCircuitBreaker() // Harness: 熔断降级中心
|
|
// Harness: LLM 自动化评测(规则 + LLM-as-judge,模型就绪时启用)。
|
|
eval := harness.NewEvaluator(pool.Ready, func(ctx context.Context, sys, user string) (string, error) {
|
|
return pool.Chat(ctx, []llm.ChatMessage{{Role: "system", Content: sys}, {Role: "user", Content: user}})
|
|
})
|
|
|
|
sub := dnats.MustConnect(natsURL)
|
|
defer sub.Close()
|
|
|
|
// 配置控制面:先订阅热更新,再后台重试拉初始配置(容忍 gateway 晚于本服务启动,
|
|
// 避免一次性请求扑空后只能干等热更新 → 降级桩跑全程)。
|
|
if _, err := sub.SubscribeModelConfigUpdated(pool.SetConfig); err != nil {
|
|
log.Printf("[dispatcher] subscribe model config: %v", err)
|
|
}
|
|
go sub.FetchModelConfigWithRetry(context.Background(), pool.SetConfig)
|
|
|
|
// sub 同时作为 Token 回流(TokenSink)、MCP 工具调用(ToolCaller)、执行事件(ExecSink)、
|
|
// 任务状态回写(StatusSink)与 HITL 审批等待(ApprovalWaiter)出口。
|
|
orch, err := eino.NewOrchestrator(pool, breaker, eval, sub, sub, sub, sub, sub)
|
|
if err != nil {
|
|
log.Fatalf("[dispatcher] build eino graph: %v", err)
|
|
}
|
|
|
|
// 健康心跳:dispatcher 无 HTTP/工具端点,挂一个 NATS 应答让管理端「服务状态」探到它在线。
|
|
startedAt := time.Now()
|
|
if unsub, herr := sub.ServeHealth(func() []byte {
|
|
data, _ := json.Marshal(map[string]any{
|
|
"ok": true,
|
|
"service": "dispatcher",
|
|
"model": pool.ModelName(),
|
|
"ready": pool.Ready(),
|
|
"uptime_s": int(time.Since(startedAt).Seconds()),
|
|
})
|
|
return data
|
|
}); herr != nil {
|
|
log.Printf("[dispatcher] serve health: %v", herr)
|
|
} else {
|
|
defer func() { _ = unsub() }()
|
|
}
|
|
|
|
// 监听退出信号,优雅停止消费。
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
log.Println("[dispatcher] consuming sundynix.tasks.* (Ctrl-C to quit)")
|
|
if err := sub.ConsumeTasks(ctx, orch.Handle); err != nil && err != context.Canceled {
|
|
log.Fatalf("[dispatcher] exit: %v", err)
|
|
}
|
|
}
|
|
|
|
func envOr(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|