diff --git a/apps/agentkit_server_app/app.go b/apps/agentkit_server_app/app.go index 274b751..5715ddb 100644 --- a/apps/agentkit_server_app/app.go +++ b/apps/agentkit_server_app/app.go @@ -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" ) @@ -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{ @@ -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) + }) + } +} diff --git a/apps/basic_app.go b/apps/basic_app.go index 3faf9e1..9cd92e1 100644 --- a/apps/basic_app.go +++ b/apps/basic_app.go @@ -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 { @@ -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, } } @@ -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) }