package toolhost import ( "context" "fmt" "time" "laodingbot/internal/config" "laodingbot/internal/logger" "laodingbot/internal/tools" "laodingbot/tools/filedoc" "laodingbot/tools/fileoperation" "laodingbot/tools/git" "laodingbot/tools/giteaticket" "laodingbot/tools/piplan" "laodingbot/tools/shell" "laodingbot/tools/websearch" ) func RunChild(ctx context.Context, cfg config.Config, log *logger.Logger) error { var registryLog *logger.Logger var fileLog *logger.Logger var gitLog *logger.Logger var shellLog *logger.Logger var searchLog *logger.Logger var fileDocLog *logger.Logger var piPlanLog *logger.Logger var giteaTicketLog *logger.Logger var serverLog *logger.Logger if log != nil { log.Infof("toolhost child starting") registryLog = log.WithComponent("toolhost.registry") fileLog = log.WithComponent("toolhost.file") gitLog = log.WithComponent("toolhost.git") shellLog = log.WithComponent("toolhost.shell") searchLog = log.WithComponent("toolhost.websearch") fileDocLog = log.WithComponent("toolhost.filedoc") piPlanLog = log.WithComponent("toolhost.piplan") giteaTicketLog = log.WithComponent("toolhost.giteaticket") serverLog = log.WithComponent("toolhost.server") } registry := tools.NewRegistry(registryLog) registry.Register(fileoperation.New(cfg.Security.AllowedDirs, cfg.ToolOutputMaxChars, fileLog)) registry.Register(git.New( cfg.Security.WorkDir, time.Duration(cfg.ToolCallTimeoutSec)*time.Second, cfg.ToolOutputMaxChars, gitLog, )) registry.Register(shell.New( cfg.Security.AllowedCommands, cfg.Security.WorkDir, time.Duration(cfg.ToolCallTimeoutSec)*time.Second, cfg.ToolOutputMaxChars, shellLog, )) registry.Register(websearch.New( websearch.Config{ Engine: cfg.WebSearch.Engine, APIKey: cfg.WebSearch.APIKey, }, cfg.ToolOutputMaxChars, searchLog, )) registry.Register(filedoc.New( filedoc.Config{ APIKey: cfg.LLM.APIKey, BaseURL: cfg.LLM.BaseURL, Model: cfg.LLM.FileModel, Timeout: time.Duration(cfg.ToolCallTimeoutSec) * time.Second, }, cfg.ToolOutputMaxChars, fileDocLog, )) registry.Register(piplan.New(cfg.PIPlanMaxChars, piPlanLog)) registry.Register(giteaticket.New( giteaticket.Config{ BaseURL: cfg.Gitea.BaseURL, Token: cfg.Gitea.Token, Owner: cfg.Gitea.Owner, Repo: cfg.Gitea.Repo, Timeout: time.Duration(cfg.ToolCallTimeoutSec) * time.Second, }, giteaTicketLog, )) server := NewServer(registry, serverLog) if err := server.Serve(ctx, stdin(), stdout()); err != nil && ctx.Err() == nil { return fmt.Errorf("toolhost serve failed: %w", err) } if log != nil { log.Infof("toolhost child stopped") } return nil }