- 四组并行完成:main/wire*.go、wire_capacity|channels|runtime、channels|persona_*|emotion|observability、 voice|plugins|stream|skills|vision|subagent_tool|extractor|mcp|sessionread_tool|terminal_tool。 - 机械校验(git diff -U0 去 // 行与空行)仅剩 wire_frontend.go:84 一处行尾注释:代码前缀 「\ta.gw = gw 」经 cat -A 与 HEAD 逐字节比对一致(allow 清单已声明)。 - 验收:隔离 worktree(HEAD + 本批 patch)go build/vet 绿、全树 58 包 ok、 linux/amd64 + darwin/arm64 交叉编译绿;gofmt -l 干净。 - 备注:整树两次红均为负载敏感 flake(backthink / orchestration 的 timeout 类用例单独 -count=20 全过)。
121 lines
4.0 KiB
Go
121 lines
4.0 KiB
Go
// cyrene 命令是 Cyrene v2 的唯一二进制入口:它把全部子系统(配置、日志、数据库、
|
||
// bus、人格、模型适配器、工具分发器、记忆栈、agent 循环、编排、执行、IoT、渠道
|
||
// 会话与适配器)组装进同一个进程,在单端口(:8080)上同时提供网关与内嵌前端,
|
||
// 并在收到终止信号时优雅退出。
|
||
//
|
||
// 构建:`go build -ldflags="-s -w" -trimpath -o cyrene ./cmd/cyrene/`。
|
||
// 前端必须先构建,SPA 才能被内嵌:`cd frontend/web && npm run build`。
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"io/fs"
|
||
"log/slog"
|
||
"os"
|
||
"path/filepath"
|
||
"time"
|
||
// 内嵌 IANA 时区数据库,让配置的 TZ(config.TZ,默认 "Asia/Shanghai")
|
||
// 在任何平台都能解析:Windows 不自带系统 tzdata,Go 也刻意不捆绑,缺了它
|
||
// 循环的时间注入在 Windows 上会静默失效(internal/runtime/loop/time.go 会记
|
||
// "timezone load failed")。代价约 450 KB,换来二进制自包含。
|
||
_ "time/tzdata"
|
||
|
||
capreg "git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/platform"
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/webui"
|
||
)
|
||
|
||
// envPath 是启动时合并的 .env 文件(仅开发用;生产走 systemd/env_file)。
|
||
// .env 不存在不算错误。
|
||
const envPath = ".env"
|
||
|
||
// shutdownTimeout 是优雅退场的时长上界。
|
||
const shutdownTimeout = 15 * time.Second
|
||
|
||
// externalDistCandidates 是前端覆盖目录的候选清单:按序检查,都不存在才回落到
|
||
// 内嵌 dist;命中的第一个生效。路径相对进程工作目录。
|
||
func externalDistCandidates() []string {
|
||
return []string{
|
||
filepath.Join("frontend", "web", "dist"),
|
||
filepath.Join("frontend", "dist"),
|
||
}
|
||
}
|
||
|
||
// resolveFrontendFS 返回用于伺服 SPA 的文件系统:存在外部 dist 目录时用它
|
||
// (部署期热覆盖),否则用内嵌 dist。
|
||
func resolveFrontendFS(log *slog.Logger) fs.FS {
|
||
for _, dir := range externalDistCandidates() {
|
||
if info, err := os.Stat(dir); err == nil && info.IsDir() {
|
||
log.Info("serving external frontend dist", "dir", dir)
|
||
return os.DirFS(dir)
|
||
}
|
||
}
|
||
log.Info("serving embedded frontend dist")
|
||
return webui.Assets()
|
||
}
|
||
|
||
func main() {
|
||
if err := run(); err != nil {
|
||
fmt.Fprintln(os.Stderr, "cyrene:", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
// run 启动应用、拉起全部服务与网关,然后阻塞等待终止信号或网关失败。
|
||
func run() error {
|
||
cfg := config.MustLoad(envPath)
|
||
format := logger.FormatText
|
||
if cfg.Env == "production" {
|
||
format = logger.FormatJSON
|
||
}
|
||
logRing := logger.NewRingBuffer(logger.DefaultRingCapacity)
|
||
log := logger.New(logger.Options{Level: cfg.LogLevel, Format: string(format), Ring: logRing})
|
||
log = logger.WithSource(log, "main")
|
||
|
||
ctx, stop := platform.SignalContext()
|
||
defer stop()
|
||
|
||
a, err := newApp(ctx, cfg, log)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 可观测性日志追尾:logger 的环形缓冲在上面(网关之前)就建好了,
|
||
// 现在网关已存在,把它交给网关。
|
||
wireGatewayLogBuffer(a, logRing)
|
||
|
||
if err := a.automation.Start(ctx); err != nil {
|
||
return fmt.Errorf("start automations: %w", err)
|
||
}
|
||
a.backthink.Start(ctx)
|
||
a.log.Info("cyrene subsystems started",
|
||
"personas", a.personas.CountPersonas(),
|
||
"tools", len(a.caps.List(capreg.KindTool)),
|
||
"channels", len(a.caps.List(capreg.KindChannel)))
|
||
a.log.Info("cyrene ready", "data_dir", resolveDataDir(cfg))
|
||
|
||
errCh, err := a.gw.Start(ctx)
|
||
if err != nil {
|
||
return fmt.Errorf("start gateway: %w", err)
|
||
}
|
||
|
||
select {
|
||
case <-ctx.Done():
|
||
a.log.Info("cyrene signal received, shutting down")
|
||
case gwErr := <-errCh:
|
||
if gwErr != nil {
|
||
shutCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||
defer cancel()
|
||
a.close(shutCtx)
|
||
return fmt.Errorf("gateway failed: %w", gwErr)
|
||
}
|
||
}
|
||
|
||
shutCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||
defer cancel()
|
||
a.close(shutCtx)
|
||
return nil
|
||
}
|