|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package agentengine provides a sublauncher that provides web interface as required by Agent Engine |
| 16 | +package agentengine |
| 17 | + |
| 18 | +import ( |
| 19 | + "flag" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/gorilla/mux" |
| 26 | + |
| 27 | + "google.golang.org/adk/cmd/launcher" |
| 28 | + weblauncher "google.golang.org/adk/cmd/launcher/web" |
| 29 | + "google.golang.org/adk/internal/cli/util" |
| 30 | + "google.golang.org/adk/server/agentengine" |
| 31 | +) |
| 32 | + |
| 33 | +// agentEngineConfig contains parameters for launching ADK Agent Engine server |
| 34 | +type agentEngineConfig struct { |
| 35 | + pathPrefix string |
| 36 | + agentEngineID string |
| 37 | + maxPayloadSize int64 |
| 38 | + sseWriteTimeout time.Duration |
| 39 | +} |
| 40 | + |
| 41 | +type agentEngineLauncher struct { |
| 42 | + flags *flag.FlagSet // flags are used to parse command-line arguments |
| 43 | + config *agentEngineConfig |
| 44 | +} |
| 45 | + |
| 46 | +// NewLauncher creates new api launcher. It extends Web launcher |
| 47 | +func NewLauncher(agentEngineId string) weblauncher.Sublauncher { |
| 48 | + config := &agentEngineConfig{} |
| 49 | + |
| 50 | + fs := flag.NewFlagSet("web", flag.ContinueOnError) |
| 51 | + fs.StringVar(&config.pathPrefix, "path_prefix", "/api", "ADK Agent Engine API path prefix. Default is '/api'.") |
| 52 | + fs.Int64Var(&config.maxPayloadSize, "max_payload_size", 10*1024*1024, "The payload will be truncated after this amount of bytes") |
| 53 | + fs.DurationVar(&config.sseWriteTimeout, "sse-write-timeout", 120*time.Second, "SSE server write timeout (i.e. '10s', '2m' - see time.ParseDuration for details) - for writing the SSE response after reading the headers & body") |
| 54 | + |
| 55 | + config.agentEngineID = agentEngineId |
| 56 | + |
| 57 | + return &agentEngineLauncher{ |
| 58 | + config: config, |
| 59 | + flags: fs, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// CommandLineSyntax implements web.Sublauncher. Returns the command-line syntax for the agentEngine launcher. |
| 64 | +func (a *agentEngineLauncher) CommandLineSyntax() string { |
| 65 | + return util.FormatFlagUsage(a.flags) |
| 66 | +} |
| 67 | + |
| 68 | +// SimpleDescription implements web.Sublauncher |
| 69 | +func (a *agentEngineLauncher) SimpleDescription() string { |
| 70 | + return "starts AgentEngine server which serves reasoning engine API while deployed to Agent Engine" |
| 71 | +} |
| 72 | + |
| 73 | +// UserMessage implements web.Sublauncher. |
| 74 | +func (a *agentEngineLauncher) UserMessage(webUrl string, printer func(v ...any)) { |
| 75 | + // TODO(kdroste) description |
| 76 | + printer(fmt.Sprintf(" agentEngine: you can access this server locally by %s%s", webUrl, "/api/reasoning_engine")) |
| 77 | + printer(fmt.Sprintf(" %s%s", webUrl, "/api/stream_reasoning_engine")) |
| 78 | + printer(" to access it while deployed to Agent Engine, you should use") |
| 79 | + printer(" https://${LOCATION_ID}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/reasoningEngines/${RESOURCE_ID}:query") |
| 80 | + printer(" or https://${LOCATION_ID}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/reasoningEngines/${RESOURCE_ID}:streamQuery") |
| 81 | +} |
| 82 | + |
| 83 | +// SetupSubrouters adds the API router to the parent router. |
| 84 | +func (a *agentEngineLauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error { |
| 85 | + // Create the ADK AgentEngine API handler |
| 86 | + apiHandler, err := agentengine.NewHandler(config, a.config.sseWriteTimeout, a.config.maxPayloadSize, a.config.agentEngineID) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("agentengine.NewHandler failed: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + router.Methods("POST"). |
| 92 | + PathPrefix(a.config.pathPrefix). |
| 93 | + Handler(http.StripPrefix(a.config.pathPrefix, apiHandler)) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// Keyword implements web.Sublauncher. Returns the command-line keyword for A2A launcher. |
| 99 | +func (a *agentEngineLauncher) Keyword() string { |
| 100 | + return "agentengine" |
| 101 | +} |
| 102 | + |
| 103 | +var _ weblauncher.Sublauncher = &agentEngineLauncher{} |
| 104 | + |
| 105 | +// Parse parses the command-line arguments for the API launcher. |
| 106 | +func (a *agentEngineLauncher) Parse(args []string) ([]string, error) { |
| 107 | + err := a.flags.Parse(args) |
| 108 | + if err != nil || !a.flags.Parsed() { |
| 109 | + return nil, fmt.Errorf("failed to parse agent engine flags: %v", err) |
| 110 | + } |
| 111 | + p := a.config.pathPrefix |
| 112 | + if !strings.HasPrefix(p, "/") { |
| 113 | + p = "/" + p |
| 114 | + } |
| 115 | + a.config.pathPrefix = strings.TrimSuffix(p, "/") |
| 116 | + |
| 117 | + restArgs := a.flags.Args() |
| 118 | + return restArgs, nil |
| 119 | +} |
0 commit comments