Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

Small improvements in the exec loop. (#128)

Junjie Wanga5c1aff3a297
diff --git a/cmd/ax/exec.go b/cmd/ax/exec.go
index d9ce288..4c13c59 100644
--- a/cmd/ax/exec.go
+++ b/cmd/ax/exec.go
@@ -20,6 +20,7 @@ import (
"io"
"os"
"os/signal"
+ "strings"
"syscall"
"github.com/google/ax/agent"
@@ -92,15 +93,13 @@ func execLoop(ctx context.Context, id string, agentID string, input string) erro
d := internal.NewDisplay(id)
d.DisplayHeader()
- if input == "" {
- var err error
- input, err = d.PromptForInput()
- if err != nil {
- return err
- }
+ input, quit, err := promptUser(d, input)
+ if err != nil {
+ return err
+ }
+ if quit {
+ return nil
}
-
- d.DisplayInput(input)
history := []*proto.Content{
{
Role: "user",
@@ -177,11 +176,13 @@ func execLoop(ctx context.Context, id string, agentID string, input string) erro
// for the upcoming executions.
history = resetHistory(history)
- input, err = d.PromptForInput()
+ input, quit, err := promptUser(d, "")
if err != nil {
return err
}
- d.DisplayInput(input)
+ if quit {
+ return nil
+ }
history = append(history, &proto.Content{
Role: "user",
Content: &proto.Content_Text{
@@ -331,3 +332,25 @@ func resetHistory(history []*proto.Content) []*proto.Content {
}
return out
}
+
+// promptUser loops until the user provides a non-empty input string.
+// It returns:
+// - string: the valid user input
+// - bool: true if the user entered a quit command
+// - error: any error that occurred during prompting
+func promptUser(d *internal.Display, input string) (string, bool, error) {
+ for strings.TrimSpace(input) == "" {
+ var err error
+ input, err = d.PromptForInput()
+ if err != nil {
+ return "", false, err
+ }
+ }
+
+ d.DisplayInput(input)
+ if strings.ToLower(strings.TrimSpace(input)) == "q" {
+ fmt.Println("Goodbye!")
+ return "", true, nil
+ }
+ return input, false, nil
+}
diff --git a/cmd/ax/internal/display.go b/cmd/ax/internal/display.go
index d5c19f3..ec3d1df 100644
--- a/cmd/ax/internal/display.go
+++ b/cmd/ax/internal/display.go
@@ -112,7 +112,7 @@ func (d *Display) PromptForInput() (string, error) {
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
- Placeholder("Enter prompt...").
+ Placeholder("Enter prompt... (type `q` to quit)").
Value(&userInput),
),
).WithWidth(boxWidth)
diff --git a/internal/controller/task/sqlite.go b/internal/controller/task/sqlite.go
index 8aa3008..254c698 100644
--- a/internal/controller/task/sqlite.go
+++ b/internal/controller/task/sqlite.go
@@ -18,6 +18,8 @@ import (
"context"
"database/sql"
"fmt"
+ "os"
+ "path/filepath"
"time"
"github.com/google/ax/proto"
@@ -34,6 +36,11 @@ const sqliteBusyTimeout = 10 * time.Second
// OpenSQLiteEventLog opens (or creates) a SQLite database at path and initializes the event log schema.
func OpenSQLiteEventLog(path string) (*SQLiteEventLog, error) {
+ dir := filepath.Dir(path)
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ return nil, fmt.Errorf("sqlite_eventlog: mkdir %s: %w", dir, err)
+ }
+
dsn := fmt.Sprintf("%s?_pragma=busy_timeout(%d)", path, sqliteBusyTimeout.Milliseconds())
db, err := sql.Open("sqlite", dsn)
if err != nil {
diff --git a/internal/controller/task/sqlite_test.go b/internal/controller/task/sqlite_test.go
index 034b41e..8c19c5c 100644
--- a/internal/controller/task/sqlite_test.go
+++ b/internal/controller/task/sqlite_test.go
@@ -16,6 +16,7 @@ package task
import (
"context"
+ "os"
"path/filepath"
"sync"
"testing"
@@ -138,3 +139,24 @@ func TestSQLiteEventLog_Empty(t *testing.T) {
t.Fatalf("expected 0 events, got %d", len(events))
}
}
+
+func TestSQLiteEventLog_CreatesParentDirectory(t *testing.T) {
+ // Create a path with a non-existent parent directory
+ dbPath := filepath.Join(t.TempDir(), "newdir", "test.db")
+
+ log, err := OpenSQLiteEventLog(dbPath)
+ if err != nil {
+ t.Fatalf("failed to open sqlite event log and create directory: %v", err)
+ }
+ defer log.Close()
+
+ // Verify that the parent directory actually exists
+ if _, err := os.Stat(filepath.Dir(dbPath)); os.IsNotExist(err) {
+ t.Fatalf("expected parent directory to be created, but it does not exist")
+ }
+
+ // Verify that the database file was created
+ if _, err := os.Stat(dbPath); os.IsNotExist(err) {
+ t.Fatalf("expected database file to be created, but it does not exist")
+ }
+}