feat(app): wire chat module + LocalFS storage + LLM registry into app.New

This commit is contained in:
2026-05-04 19:37:14 +08:00
parent 7aa8456b1b
commit 8effeee063
3 changed files with 133 additions and 0 deletions
+58
View File
@@ -30,12 +30,15 @@ import (
"github.com/google/uuid"
"github.com/yan1h/agent-coding-workflow/internal/audit"
"github.com/yan1h/agent-coding-workflow/internal/chat"
"github.com/yan1h/agent-coding-workflow/internal/config"
"github.com/yan1h/agent-coding-workflow/internal/infra/crypto"
"github.com/yan1h/agent-coding-workflow/internal/infra/db"
"github.com/yan1h/agent-coding-workflow/internal/infra/git"
"github.com/yan1h/agent-coding-workflow/internal/infra/llm"
"github.com/yan1h/agent-coding-workflow/internal/infra/logger"
"github.com/yan1h/agent-coding-workflow/internal/infra/notify"
"github.com/yan1h/agent-coding-workflow/internal/infra/storage"
"github.com/yan1h/agent-coding-workflow/internal/project"
httpx "github.com/yan1h/agent-coding-workflow/internal/transport/http"
"github.com/yan1h/agent-coding-workflow/internal/transport/http/middleware"
@@ -173,6 +176,61 @@ func New(ctx context.Context, cfg *config.Config, dist embed.FS) (*App, error) {
gopSvc := workspace.NewGitOpsService(wsRepo, auditRec, gitr, pa, credLoader)
workspace.NewHandler(wsSvc, wtSvc, gopSvc, userSvc, userAdminAdapter{svc: userSvc}).Mount(r)
// ===== chat 模块装配 =====
// 依赖图:
// pgx.Pool ─ chat.Repository ─┐
// ├─ EndpointLoader ─ llm.Registry ─┐
// crypto.Encryptor ───────────┘ │
// │
// storage.LocalFS ─ Storage ──┐ │
// ├─ MessageService ─ Handler ──────┤
// notify.Dispatcher ─ StreamerHub ─ ConversationService ─ Hub ───┘
//
// 启动期把卡死的 pending 消息复位为 error,避免行永远卡住。
chatRepo := chat.NewRepository(pool)
if err := chatRepo.MarkPendingAsErrorOnStartup(ctx); err != nil {
log.Warn("mark chat pending messages as error on startup", "err", err.Error())
}
endpointLoader := chat.NewEndpointLoader(chatRepo, enc)
llmRegistry := llm.NewRegistry(endpointLoader)
var attachStorage storage.Storage
switch cfg.Storage.Driver {
case "localfs", "":
s, err := storage.NewLocalFS(cfg.Storage.LocalFS.BasePath)
if err != nil {
return nil, fmt.Errorf("storage localfs: %w", err)
}
attachStorage = s
default:
return nil, fmt.Errorf("unsupported storage driver %q", cfg.Storage.Driver)
}
// safety_margin_pct 配置为百分比(5.0 表示 5%),构造器期望小数(0.05),故 /100。
safetyFraction := cfg.Chat.History.SafetyMarginPct / 100
retention := time.Duration(cfg.Chat.Stream.RingBufferRetentionSeconds) * time.Second
chatEpSvc := chat.NewEndpointService(chatRepo, enc, llmRegistry, auditRec, log)
chatTplSvc := chat.NewTemplateService(chatRepo, auditRec, log)
chatConvSvc := chat.NewConversationService(chatRepo, llmRegistry, auditRec, log)
chatHub := chat.NewStreamerHub(chatRepo, llmRegistry, notifyDispatcher, auditRec,
chatConvSvc, log, retention, safetyFraction)
chatMsgSvc := chat.NewMessageService(chatRepo, attachStorage, chatHub, auditRec, log,
chat.MessageServiceConfig{
MimeWhitelist: cfg.Chat.Attachment.MimeWhitelist,
MaxFileBytes: cfg.Chat.Attachment.MaxFileSizeMB << 20,
MaxMsgBytes: cfg.Chat.Attachment.MaxMessageSizeMB << 20,
SafetyPct: safetyFraction,
})
chatUsageSvc := chat.NewUsageService(chatRepo)
chatUploader := chat.NewUploader(chatRepo, attachStorage,
cfg.Chat.Attachment.MimeWhitelist,
cfg.Chat.Attachment.MaxFileSizeMB<<20,
)
chat.NewHandler(chatConvSvc, chatMsgSvc, chatTplSvc, chatEpSvc, chatUsageSvc,
chatUploader, userSvc, userAdminAdapter{svc: userSvc}).Mount(r)
if cfg.Bootstrap.AdminEmail != "" && cfg.Bootstrap.AdminPassword != "" {
if u, err := userSvc.Bootstrap(ctx, cfg.Bootstrap.AdminEmail, cfg.Bootstrap.AdminPassword); err != nil {
log.Warn("bootstrap admin failed", "err", err.Error())