Agent-Mode mit Aufgabenteilung und automatischer Recherche
This commit is contained in:
+55
-4
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -15,9 +16,12 @@ import (
|
||||
"github.com/local/glpi-neural-brain/internal/engine"
|
||||
"github.com/local/glpi-neural-brain/internal/graph"
|
||||
"github.com/local/glpi-neural-brain/internal/ingest"
|
||||
"github.com/local/glpi-neural-brain/internal/sourceagent"
|
||||
webui "github.com/local/glpi-neural-brain/internal/web"
|
||||
)
|
||||
|
||||
const buildVersion = "source-agent-integrated-v1"
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
@@ -25,23 +29,70 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})))
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
if cfg.Mode == "agent" {
|
||||
runAgent(ctx, cfg)
|
||||
return
|
||||
}
|
||||
runBrain(ctx, cancel, cfg)
|
||||
}
|
||||
|
||||
func runAgent(ctx context.Context, cfg config.Config) {
|
||||
runner, err := sourceagent.NewRunner(sourceagent.RunnerConfig{
|
||||
BrainURL: cfg.AgentBrainURL, AgentID: cfg.AgentID, Token: cfg.AgentToken, DataDir: cfg.DataDir,
|
||||
ConfigFile: cfg.AgentConfigFile, ConfigRefresh: cfg.AgentConfigRefresh, HTTPTimeout: cfg.AgentHTTPTimeout,
|
||||
Concurrency: cfg.AgentConcurrency, BatchSize: cfg.AgentBatchSize, AllowPrivate: cfg.AgentAllowPrivate, Version: buildVersion,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("source agent initialization failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer runner.Close()
|
||||
runner.Start(ctx)
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /api/status", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(runner.Status())
|
||||
})
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
|
||||
srv := &http.Server{Addr: cfg.ListenAddr, Handler: mux, ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 30 * time.Second}
|
||||
go func() {
|
||||
slog.Info("neural brain source-agent listening", "addr", cfg.ListenAddr, "agent_id", cfg.AgentID, "brain_url", cfg.AgentBrainURL)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("agent status server failed", "error", err)
|
||||
}
|
||||
}()
|
||||
<-ctx.Done()
|
||||
shutdown, c := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer c()
|
||||
_ = srv.Shutdown(shutdown)
|
||||
}
|
||||
|
||||
func runBrain(ctx context.Context, cancel context.CancelFunc, cfg config.Config) {
|
||||
g, err := graph.Open(cfg.DataDir)
|
||||
if err != nil {
|
||||
slog.Error("graph open failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
broker := activity.New(120)
|
||||
sourceStore, err := sourceagent.OpenStore(cfg.DataDir)
|
||||
if err != nil {
|
||||
slog.Error("source agent store open failed", "error", err)
|
||||
_ = g.Close()
|
||||
os.Exit(1)
|
||||
}
|
||||
defer sourceStore.Close()
|
||||
eng := engine.New(cfg, g, broker)
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
eng.SetSourceInbox(sourceStore)
|
||||
eng.Start(ctx)
|
||||
watcher := ingest.NewAgentWatcher(cfg.AgentRunsFiles, g, broker)
|
||||
watcher.Start(ctx)
|
||||
ui := &webui.Server{Engine: eng, Graph: g, Broker: broker, APIKey: cfg.APIKey}
|
||||
ui := &webui.Server{Engine: eng, Graph: g, Broker: broker, APIKey: cfg.APIKey, SourceAgents: sourceStore}
|
||||
srv := &http.Server{Addr: cfg.ListenAddr, Handler: ui.Handler(), ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 10 * time.Minute, IdleTimeout: 90 * time.Second}
|
||||
go func() {
|
||||
storage := g.StorageStatus()
|
||||
slog.Info("neural brain listening", "addr", cfg.ListenAddr, "knowledge_dirs", cfg.KnowledgeDirs, "staging_dirs", cfg.StagingDirs, "graph_backend", storage.Backend, "graph_db", storage.Path)
|
||||
slog.Info("neural brain listening", "mode", "brain", "addr", cfg.ListenAddr, "knowledge_dirs", cfg.KnowledgeDirs, "staging_dirs", cfg.StagingDirs, "graph_backend", storage.Backend, "graph_db", storage.Path)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("server failed", "error", err)
|
||||
cancel()
|
||||
|
||||
Reference in New Issue
Block a user