c7a02c3905
5 层 + 1 条 NATS 零拷贝消息总线的 monorepo(Monolith First → Microservices Morph B)。 纵向主干(任务流 + Token 流回流)已真实跑通,横向各层能力为带注释的桩。 已贯通(real code): - sundynix-shared: 共享契约 + JetStream/core NATS 真实收发(bus) + 内嵌 NATS(devnats) + e2e 测试 - sundynix-gateway: Gin 接入 + DSL 解析组装 + NATS Publish + SSE 流式输出 - sundynix-dispatcher: NATS 消费 + Eino Orchestrator 流式回流 + 熔断器 + LLM Pool 占位流式 - 链路: HTTP POST → DSL → sundynix.tasks.* → Dispatcher → Token 经 sundynix.streams.<id> 回流 → SSE - 基础设施: docker-compose(nats/postgres/redis/neo4j/milvus) + Makefile(make demo/e2e) 待填(桩): - Eino 图编排 compose.NewGraph、LLM Pool 接 vLLM/Ollama - Gateway store 换真实 pgx/redis - sundynix-mcp-go: Bleve+Milvus+Neo4j 混合检索 / UniOffice / 外部 API - sundynix-mcp-py: gVisor 沙箱 / MinerU(PaddleOCR) / Docker 解释器 - sundynix-desktop: React Flow 画布 → DSL 导出 → SSE 展示
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
// Package nats 是网关对共享 bus 的薄封装(发布任务 / 订阅 Token 回流)。
|
|
package nats
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
sharedbus "github.com/sundynix/sundynix-shared/bus"
|
|
"github.com/sundynix/sundynix-shared/contract"
|
|
)
|
|
|
|
// Bus 包装共享 bus,向网关其余代码暴露发布能力。
|
|
type Bus struct {
|
|
inner *sharedbus.Bus
|
|
}
|
|
|
|
// MustConnect 接入 NATS 并确保任务流存在。
|
|
func MustConnect(url string) *Bus {
|
|
inner, err := sharedbus.Connect(url)
|
|
if err != nil {
|
|
log.Fatalf("[nats] connect: %v", err)
|
|
}
|
|
if err := inner.EnsureTaskStream(context.Background()); err != nil {
|
|
log.Fatalf("[nats] ensure stream: %v", err)
|
|
}
|
|
log.Printf("[nats] connected %s, task stream ready", url)
|
|
return &Bus{inner: inner}
|
|
}
|
|
|
|
// PublishTask 把组装后的 Task 发布到 sundynix.tasks.<id>。
|
|
func (b *Bus) PublishTask(ctx context.Context, t *contract.Task) error {
|
|
seq, err := b.inner.PublishTask(ctx, t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Printf("[nats] published task %s (seq=%d)", t.ID, seq)
|
|
return nil
|
|
}
|
|
|
|
// SubscribeTokens 订阅 sundynix.streams.<taskID> 的 Token 回流,
|
|
// 每个 Token 触发 onToken,流结束触发 onDone,返回 unsub。
|
|
func (b *Bus) SubscribeTokens(taskID string, onToken func([]byte), onDone func()) (func() error, error) {
|
|
return b.inner.SubscribeTokens(taskID, onToken, onDone)
|
|
}
|
|
|
|
func (b *Bus) Close() { b.inner.Close() }
|