Skip to content
Open
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
13 changes: 12 additions & 1 deletion cmd/launcher/web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ func (a *apiLauncher) UserMessage(webURL string, printer func(v ...any)) {

// SetupSubrouters adds the API router to the parent router.
func (a *apiLauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error {
// Create the ADK REST API config
adkrestConfig := &adkrest.Config{
AgentLoader: config.AgentLoader,
SessionService: config.SessionService,
ArtifactService: config.ArtifactService,
MemoryService: config.MemoryService,
SSEWriteTimeout: a.config.sseWriteTimeout,
}
// Create the ADK REST API handler
apiHandler := adkrest.NewHandler(config, a.config.sseWriteTimeout)
apiHandler, err := adkrest.NewHandler(adkrestConfig)
if err != nil {
return fmt.Errorf("failed to create ADK REST API handler: %v", err)
}

// Wrap it with CORS middleware
corsHandler := corsWithArgs(a.config.frontendAddress)(apiHandler)
Expand Down
17 changes: 12 additions & 5 deletions examples/rest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

"google.golang.org/adk/agent"
"google.golang.org/adk/agent/llmagent"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/artifact"
"google.golang.org/adk/memory"
"google.golang.org/adk/model/gemini"
"google.golang.org/adk/server/adkrest"
"google.golang.org/adk/session"
Expand Down Expand Up @@ -60,13 +61,19 @@ func main() {
}

// Configure the ADK REST API
config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(a),
SessionService: session.InMemoryService(),
config := &adkrest.Config{
SessionService: session.InMemoryService(),
ArtifactService: artifact.InMemoryService(),
AgentLoader: agent.NewSingleLoader(a),
MemoryService: memory.InMemoryService(),
SSEWriteTimeout: 120 * time.Second,
}

// Create the REST API handler - this returns a standard http.Handler
apiHandler := adkrest.NewHandler(config, 120*time.Second)
apiHandler, err := adkrest.NewHandler(config)
if err != nil {
log.Fatalf("Failed to create ADK REST API handler: %v", err)
}

// Create a standard net/http ServeMux
mux := http.NewServeMux()
Expand Down
53 changes: 53 additions & 0 deletions server/adkrest/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adkrest

import (
"errors"
"time"

"google.golang.org/adk/agent"
"google.golang.org/adk/artifact"
"google.golang.org/adk/memory"
"google.golang.org/adk/runner"
"google.golang.org/adk/session"
)

// Config defines the services and loaders required by the adkrest package.
type Config struct {
SessionService session.Service
ArtifactService artifact.Service
AgentLoader agent.Loader
MemoryService memory.Service
SSEWriteTimeout time.Duration
PluginConfig runner.PluginConfig
}

// validate validates the config
func (c *Config) validate() error {
if c.SessionService == nil {
return errors.New("session service is required")
}
if c.ArtifactService == nil {
return errors.New("artifact service is required")
}
if c.AgentLoader == nil {
return errors.New("agent loader is required")
}
if c.MemoryService == nil {
return errors.New("memory service is required")
}
return nil
}
12 changes: 8 additions & 4 deletions server/adkrest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package adkrest

import (
"net/http"
"time"

"github.com/gorilla/mux"
sdktrace "go.opentelemetry.io/otel/sdk/trace"

"google.golang.org/adk/internal/telemetry"
"google.golang.org/adk/cmd/launcher"
"google.golang.org/adk/server/adkrest/controllers"
"google.golang.org/adk/server/adkrest/internal/routers"
Expand All @@ -29,7 +29,11 @@ import (
)

// NewHandler creates and returns an http.Handler for the ADK REST API.
func NewHandler(config *launcher.Config, sseWriteTimeout time.Duration) http.Handler {
func NewHandler(config *Config) (http.Handler, error) {
if err := config.validate(); err != nil {
return nil, err
}

adkExporter := services.NewAPIServerSpanExporter()
processor := sdktrace.NewSimpleSpanProcessor(adkExporter)
config.TelemetryOptions = append(config.TelemetryOptions, telemetry.WithSpanProcessors(processor))
Expand All @@ -39,13 +43,13 @@ func NewHandler(config *launcher.Config, sseWriteTimeout time.Duration) http.Han
// where the ADK REST API will be served.
setupRouter(router,
routers.NewSessionsAPIRouter(controllers.NewSessionsAPIController(config.SessionService)),
routers.NewRuntimeAPIRouter(controllers.NewRuntimeAPIController(config.SessionService, config.MemoryService, config.AgentLoader, config.ArtifactService, sseWriteTimeout, config.PluginConfig)),
routers.NewRuntimeAPIRouter(controllers.NewRuntimeAPIController(config.SessionService, config.MemoryService, config.AgentLoader, config.ArtifactService, config.SSEWriteTimeout, config.PluginConfig)),
routers.NewAppsAPIRouter(controllers.NewAppsAPIController(config.AgentLoader)),
routers.NewDebugAPIRouter(controllers.NewDebugAPIController(config.SessionService, config.AgentLoader, adkExporter)),
routers.NewArtifactsAPIRouter(controllers.NewArtifactsAPIController(config.ArtifactService)),
&routers.EvalAPIRouter{},
)
return router
return router, nil
}

func setupRouter(router *mux.Router, subrouters ...routers.Router) *mux.Router {
Expand Down