Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions apps/agentkit_server_app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/volcengine/veadk-go/apps/simple_app"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/cmd/launcher/web"
"google.golang.org/adk/cmd/launcher/web/api"
"google.golang.org/adk/cmd/launcher/web/webui"
"google.golang.org/adk/server/adkrest"
"google.golang.org/adk/session"
)

Expand Down Expand Up @@ -86,28 +86,28 @@ func (a *agentkitServerApp) SetupRouters(router *mux.Router, config *apps.RunCon
return fmt.Errorf("setup simple app routers failed: %w", err)
}

// setup web api routers
apiLauncher := api.NewLauncher()
_, err = apiLauncher.Parse([]string{
"--webui_address", fmt.Sprintf("localhost:%v", fmt.Sprint(a.Port)),
"--sse-write-timeout", "5m",
})

if err != nil {
return fmt.Errorf("apiLauncher parse parames failed: %w", err)
}

err = apiLauncher.SetupSubrouters(router, &launcher.Config{
launchConfig := &launcher.Config{
SessionService: config.SessionService,
ArtifactService: config.ArtifactService,
MemoryService: config.MemoryService,
AgentLoader: config.AgentLoader,
A2AOptions: config.A2AOptions,
})
if err != nil {
return fmt.Errorf("setup api routers failed: %w", err)
}

// setup web api routers
// Create the ADK REST API handler
apiHandler := adkrest.NewHandler(launchConfig, a.SEEWriteTimeout)

// Wrap it with CORS middleware
corsHandler := corsWithArgs(fmt.Sprintf("localhost:%d", a.Port))(apiHandler)

// Register it at the /api/ path
router.Methods("GET", "POST", "DELETE", "OPTIONS").PathPrefix("/api/").Handler(
http.StripPrefix("/api", corsHandler),
)
log.Printf(" api: you can access API using %s/api", a.GetWebUrl())
log.Printf(" api: for instance: %s/api/list-apps", a.GetWebUrl())

// setup webui routers
webuiLauncher := webui.NewLauncher()
_, err = webuiLauncher.Parse([]string{
Expand All @@ -129,8 +129,22 @@ func (a *agentkitServerApp) SetupRouters(router *mux.Router, config *apps.RunCon
return fmt.Errorf("setup webui routers failed: %w", err)
}

apiLauncher.UserMessage(a.GetWebUrl(), log.Println)
webuiLauncher.UserMessage(a.GetWebUrl(), log.Println)

return nil
}

func corsWithArgs(frontendAddress string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", frontendAddress)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
}
22 changes: 14 additions & 8 deletions apps/basic_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ type RunConfig struct {
}

type ApiConfig struct {
Port int
WriteTimeout time.Duration
ReadTimeout time.Duration
IdleTimeout time.Duration
Port int
WriteTimeout time.Duration
ReadTimeout time.Duration
IdleTimeout time.Duration
SEEWriteTimeout time.Duration
}

type BasicApp interface {
Expand All @@ -49,10 +50,11 @@ type BasicApp interface {

func DefaultApiConfig() ApiConfig {
return ApiConfig{
Port: 8000,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Port: 8000,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
SEEWriteTimeout: time.Second * 300,
}
}

Expand All @@ -72,6 +74,10 @@ func (a *ApiConfig) SetIdleTimeout(t int64) {
a.IdleTimeout = time.Second * time.Duration(t)
}

func (a *ApiConfig) SetSEEWriteTimeout(t int64) {
a.SEEWriteTimeout = time.Second * time.Duration(t)
}

func (a *ApiConfig) GetWebUrl() string {
return fmt.Sprintf("http://localhost:%d", a.Port)
}