Sfoglia il codice sorgente

Merge pull request #232 from yeungalan/claude/sleepy-sagan-DrS8Z

Route AGI script console output through structured logger
Alan Yeung 2 settimane fa
parent
commit
4a72fb1d14
3 ha cambiato i file con 22 aggiunte e 0 eliminazioni
  1. 1 0
      src/agi.go
  2. 2 0
      src/mod/agi/agi.go
  3. 19 0
      src/mod/agi/agi.system.go

+ 1 - 0
src/agi.go

@@ -22,6 +22,7 @@ func AGIInit() {
 		ModuleRegisterParser: moduleHandler.RegisterModuleFromAGI,
 		ModuleListProvider:   moduleHandler.GetModuleListJSONForUser,
 		PackageManager:       packageManager,
+		Logger:               systemWideLogger,
 		UserHandler:          userHandler,
 		StartupRoot:          "./web",
 		ActivateScope:        []string{"./web", "./subservice"},

+ 2 - 0
src/mod/agi/agi.go

@@ -19,6 +19,7 @@ import (
 	apt "imuslab.com/arozos/mod/apt"
 	"imuslab.com/arozos/mod/filesystem"
 	"imuslab.com/arozos/mod/filesystem/arozfs"
+	"imuslab.com/arozos/mod/info/logger"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
 	"imuslab.com/arozos/mod/iot"
 	"imuslab.com/arozos/mod/share"
@@ -54,6 +55,7 @@ type AgiSysInfo struct {
 	LoadedModule    []string
 
 	//System Handlers
+	Logger               *logger.Logger
 	UserHandler          *user.UserHandler
 	ReservedTables       []string
 	PackageManager       *apt.AptPackageManager

+ 19 - 0
src/mod/agi/agi.system.go

@@ -38,6 +38,25 @@ func (g *Gateway) injectStandardLibs(vm *otto.Otto, scriptFile string, scriptSco
 	// Available in every AGI script — use it for log correlation, deduplication, etc.
 	vm.Set("EXECUTION_ID", execID)
 
+	// Override otto's built-in console.log (which maps to fmt.Println) so that
+	// script output goes through the structured logger and carries the execID.
+	// Use the system-wide logger (with file output) when available; fall back to
+	// agiLogger (stdout only) if the Gateway was created without one.
+	scriptLogger := agiLogger
+	if g.Option.Logger != nil {
+		scriptLogger = g.Option.Logger
+	}
+	vm.Set("_agi_console_log", func(call otto.FunctionCall) otto.Value {
+		parts := make([]string, 0, len(call.ArgumentList))
+		for _, arg := range call.ArgumentList {
+			str, _ := arg.ToString()
+			parts = append(parts, str)
+		}
+		scriptLogger.PrintAndLog("AGI", "["+execID+"] "+strings.Join(parts, " "), nil)
+		return otto.UndefinedValue()
+	})
+	vm.Run(`var console = { log: _agi_console_log, warn: _agi_console_log, error: _agi_console_log, info: _agi_console_log };`) //nolint:errcheck
+
 	//Response related
 	vm.Set("sendResp", func(call otto.FunctionCall) otto.Value {
 		argString, _ := call.Argument(0).ToString()