|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/Protocol-Lattice/go-agent" |
| 13 | + "github.com/Protocol-Lattice/go-agent/src/adk" |
| 14 | + "github.com/Protocol-Lattice/go-agent/src/adk/modules" |
| 15 | + "github.com/Protocol-Lattice/go-agent/src/memory" |
| 16 | + "github.com/Protocol-Lattice/go-agent/src/memory/engine" |
| 17 | + "github.com/Protocol-Lattice/go-agent/src/models" |
| 18 | + "github.com/universal-tool-calling-protocol/go-utcp" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + ctx, cancel := context.WithCancel(context.Background()) |
| 23 | + defer cancel() |
| 24 | + |
| 25 | + fmt.Println("=== Autonomous Agent Cron Loop with Terminal User Input ===") |
| 26 | + fmt.Println("Demonstrates a continuously running agent with an interactive CLI.") |
| 27 | + fmt.Println() |
| 28 | + |
| 29 | + // 1. Create UTCP Client |
| 30 | + client, err := utcp.NewUTCPClient(ctx, &utcp.UtcpClientConfig{}, nil, nil) |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Failed to create UTCP client: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + // 2. Create and register a specialist tool agent |
| 36 | + monitorModel, err := models.NewGeminiLLM(ctx, "gemini-3-flash-preview", "You are SystemMonitor") |
| 37 | + if err != nil { |
| 38 | + log.Fatalf("Failed to create monitor model: %v", err) |
| 39 | + } |
| 40 | + analyst, err := agent.New(agent.Options{ |
| 41 | + Model: monitorModel, |
| 42 | + Memory: memory.NewSessionMemory(memory.NewMemoryBankWithStore(memory.NewInMemoryStore()), 8), |
| 43 | + SystemPrompt: "You monitor system health and perform background tasks.", |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("Failed to create tool agent: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + err = analyst.RegisterAsUTCPProvider(ctx, client, "local.monitor", "Monitors system resources") |
| 50 | + if err != nil { |
| 51 | + log.Fatalf("Failed to register tool: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + // 3. Create Orchestrator with CodeMode |
| 55 | + orchestratorModel, err := models.NewGeminiLLM(ctx, "gemini-3-flash-preview", "You are Orchestrator") |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("Failed to create orchestrator model: %v", err) |
| 58 | + } |
| 59 | + memOpts := engine.DefaultOptions() |
| 60 | + kit, err := adk.New(ctx, |
| 61 | + adk.WithDefaultSystemPrompt("You orchestrate workflow tools. You receive prompts from users and automated cron triggers."), |
| 62 | + adk.WithModules( |
| 63 | + modules.NewModelModule("model", func(_ context.Context) (models.Agent, error) { |
| 64 | + return orchestratorModel, nil |
| 65 | + }), |
| 66 | + modules.InMemoryMemoryModule(10000, memory.AutoEmbedder(), &memOpts), |
| 67 | + ), |
| 68 | + adk.WithCodeModeUtcp(client, orchestratorModel), |
| 69 | + ) |
| 70 | + if err != nil { |
| 71 | + log.Fatalf("Failed to initialize ADK: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + orchestrator, err := kit.BuildAgent(ctx) |
| 75 | + if err != nil { |
| 76 | + log.Fatalf("Failed to build orchestrator: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + // 4. Setup channels for the loop |
| 80 | + userInputCh := make(chan string) |
| 81 | + |
| 82 | + // 5. Start a goroutine to read from stdin |
| 83 | + go func() { |
| 84 | + fmt.Println("\nType your message below (type 'exit' to quit):") |
| 85 | + scanner := bufio.NewScanner(os.Stdin) |
| 86 | + for { |
| 87 | + fmt.Print("\n> ") |
| 88 | + if !scanner.Scan() { |
| 89 | + break |
| 90 | + } |
| 91 | + text := strings.TrimSpace(scanner.Text()) |
| 92 | + if text == "exit" || text == "quit" { |
| 93 | + cancel() |
| 94 | + return |
| 95 | + } |
| 96 | + if text != "" { |
| 97 | + userInputCh <- text |
| 98 | + } |
| 99 | + } |
| 100 | + }() |
| 101 | + |
| 102 | + // 6. Setup the cron ticker (e.g., tick every 10 seconds) |
| 103 | + ticker := time.NewTicker(10 * time.Second) |
| 104 | + defer ticker.Stop() |
| 105 | + |
| 106 | + fmt.Println("Agent is now running in an autonomous loop...") |
| 107 | + |
| 108 | + sessionID := "autonomous-session-1" |
| 109 | + |
| 110 | + // 7. Event Loop |
| 111 | + for { |
| 112 | + select { |
| 113 | + case <-ctx.Done(): |
| 114 | + fmt.Println("\nExiting autonomous loop...") |
| 115 | + return |
| 116 | + |
| 117 | + case input := <-userInputCh: |
| 118 | + // Ensure user prompt doesn't get messed up with background output |
| 119 | + fmt.Printf("\n[USER INPUT] Sending to orchestrator: %s\n", input) |
| 120 | + resp, generateErr := orchestrator.Generate(ctx, sessionID, input) |
| 121 | + if generateErr != nil { |
| 122 | + log.Printf("Orchestrator error on user input: %v", generateErr) |
| 123 | + } else { |
| 124 | + fmt.Printf("[AGENT] Response: %v\n> ", resp) |
| 125 | + } |
| 126 | + |
| 127 | + case t := <-ticker.C: |
| 128 | + // Autonomous background task execution |
| 129 | + fmt.Printf("\n\n--- [CRON EVENT TICK: %s] ---\n", t.Format("15:04:05")) |
| 130 | + backgroundPrompt := "[CRON] Perform background assessment and invoke local.monitor if necessary." |
| 131 | + fmt.Printf("Executing background prompt: %s\n", backgroundPrompt) |
| 132 | + |
| 133 | + resp, generateErr := orchestrator.Generate(ctx, sessionID, backgroundPrompt) |
| 134 | + if generateErr != nil { |
| 135 | + log.Printf("Orchestrator error on cron tick: %v", generateErr) |
| 136 | + } else { |
| 137 | + fmt.Printf("[AGENT BACKGROUND] Output: %v\n> ", resp) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments