2026-02-28 17:48:33 +08:00
|
|
|
package toolhost
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"laodingbot/internal/config"
|
|
|
|
|
"laodingbot/internal/logger"
|
|
|
|
|
"laodingbot/internal/tools"
|
2026-03-05 17:44:19 +08:00
|
|
|
"laodingbot/tools/fileoperation"
|
|
|
|
|
"laodingbot/tools/shell"
|
|
|
|
|
"laodingbot/tools/websearch"
|
2026-02-28 17:48:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func RunChild(ctx context.Context, cfg config.Config, log *logger.Logger) error {
|
|
|
|
|
var registryLog *logger.Logger
|
|
|
|
|
var fileLog *logger.Logger
|
|
|
|
|
var shellLog *logger.Logger
|
2026-03-05 17:44:19 +08:00
|
|
|
var searchLog *logger.Logger
|
2026-02-28 17:48:33 +08:00
|
|
|
var serverLog *logger.Logger
|
|
|
|
|
if log != nil {
|
|
|
|
|
log.Infof("toolhost child starting")
|
|
|
|
|
registryLog = log.WithComponent("toolhost.registry")
|
|
|
|
|
fileLog = log.WithComponent("toolhost.file")
|
|
|
|
|
shellLog = log.WithComponent("toolhost.shell")
|
2026-03-05 17:44:19 +08:00
|
|
|
searchLog = log.WithComponent("toolhost.websearch")
|
2026-02-28 17:48:33 +08:00
|
|
|
serverLog = log.WithComponent("toolhost.server")
|
|
|
|
|
}
|
|
|
|
|
registry := tools.NewRegistry(registryLog)
|
2026-03-05 17:44:19 +08:00
|
|
|
registry.Register(fileoperation.New(cfg.Security.AllowedDirs, cfg.ToolOutputMaxChars, fileLog))
|
|
|
|
|
registry.Register(shell.New(
|
2026-02-28 17:48:33 +08:00
|
|
|
cfg.Security.AllowedCommands,
|
|
|
|
|
cfg.Security.WorkDir,
|
|
|
|
|
time.Duration(cfg.ToolCallTimeoutSec)*time.Second,
|
|
|
|
|
cfg.ToolOutputMaxChars,
|
|
|
|
|
shellLog,
|
|
|
|
|
))
|
2026-03-05 17:44:19 +08:00
|
|
|
registry.Register(websearch.New(
|
|
|
|
|
websearch.Config{
|
|
|
|
|
Engine: cfg.WebSearch.Engine,
|
|
|
|
|
APIKey: cfg.WebSearch.APIKey,
|
|
|
|
|
},
|
|
|
|
|
cfg.ToolOutputMaxChars,
|
|
|
|
|
searchLog,
|
|
|
|
|
))
|
2026-02-28 17:48:33 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|