diff --git a/server/restapi/services/agentloader.go b/agent/loader.go similarity index 63% rename from server/restapi/services/agentloader.go rename to agent/loader.go index db306137..96cc5462 100644 --- a/server/restapi/services/agentloader.go +++ b/agent/loader.go @@ -12,47 +12,45 @@ // See the License for the specific language governing permissions and // limitations under the License. -package services +package agent import ( "fmt" - - "google.golang.org/adk/agent" ) -// AgentLoader allows to load a particular agent by name and get the root agent -type AgentLoader interface { +// Loader allows to load a particular agent by name and get the root agent +type Loader interface { // ListAgents returns a list of names of all agents ListAgents() []string // LoadAgent returns an agent by its name. Returns error if there is no agent with such a name. - LoadAgent(name string) (agent.Agent, error) + LoadAgent(name string) (Agent, error) // RootAgent returns the root agent - RootAgent() agent.Agent + RootAgent() Agent } -// multiAgentLoader should be used when you have multiple agents -type multiAgentLoader struct { - agentMap map[string]agent.Agent - root agent.Agent +// multiLoader should be used when you have multiple agents +type multiLoader struct { + agentMap map[string]Agent + root Agent } -// singleAgentLoader should be used when you have only one agent -type singleAgentLoader struct { - root agent.Agent +// singleLoader should be used when you have only one agent +type singleLoader struct { + root Agent } -// NewSingleAgentLoader returns a loader with only one agent, which becomes the root agent -func NewSingleAgentLoader(a agent.Agent) AgentLoader { - return &singleAgentLoader{root: a} +// NewSingleLoader returns a loader with only one agent, which becomes the root agent +func NewSingleLoader(a Agent) Loader { + return &singleLoader{root: a} } // singleAgentLoader implements AgentLoader. Returns root agent's name -func (s *singleAgentLoader) ListAgents() []string { +func (s *singleLoader) ListAgents() []string { return []string{s.root.Name()} } // singleAgentLoader implements AgentLoader. Returns root for empty name and for root.Name(), error otherwise. -func (s *singleAgentLoader) LoadAgent(name string) (agent.Agent, error) { +func (s *singleLoader) LoadAgent(name string) (Agent, error) { if name == "" { return s.root, nil } @@ -63,14 +61,14 @@ func (s *singleAgentLoader) LoadAgent(name string) (agent.Agent, error) { } // singleAgentLoader implements AgentLoader. Returns the root agent. -func (s *singleAgentLoader) RootAgent() agent.Agent { +func (s *singleLoader) RootAgent() Agent { return s.root } -// NewMultiAgentLoader returns a new AgentLoader with the given root Agent and other agents. +// NewMultiLoader returns a new AgentLoader with the given root Agent and other agents. // Returns an error if more than one agent (including root) shares the same name -func NewMultiAgentLoader(root agent.Agent, agents ...agent.Agent) (AgentLoader, error) { - m := make(map[string]agent.Agent) +func NewMultiLoader(root Agent, agents ...Agent) (Loader, error) { + m := make(map[string]Agent) m[root.Name()] = root for _, a := range agents { if _, ok := m[a.Name()]; ok { @@ -79,14 +77,14 @@ func NewMultiAgentLoader(root agent.Agent, agents ...agent.Agent) (AgentLoader, } m[a.Name()] = a } - return &multiAgentLoader{ + return &multiLoader{ agentMap: m, root: root, }, nil } // multiAgentLoader implements AgentLoader. Returns the list of all agents' names (including root agent) -func (m *multiAgentLoader) ListAgents() []string { +func (m *multiLoader) ListAgents() []string { agents := make([]string, 0, len(m.agentMap)) for name := range m.agentMap { agents = append(agents, name) @@ -95,7 +93,7 @@ func (m *multiAgentLoader) ListAgents() []string { } // multiAgentLoader implements LoadAgent. Returns an agent with given name or error if no such an agent is found -func (m *multiAgentLoader) LoadAgent(name string) (agent.Agent, error) { +func (m *multiLoader) LoadAgent(name string) (Agent, error) { agent, ok := m.agentMap[name] if !ok { return nil, fmt.Errorf("agent %s not found. Please specify one of those: %v", name, m.ListAgents()) @@ -104,6 +102,6 @@ func (m *multiAgentLoader) LoadAgent(name string) (agent.Agent, error) { } // multiAgentLoader implements LoadAgent. -func (m *multiAgentLoader) RootAgent() agent.Agent { +func (m *multiLoader) RootAgent() Agent { return m.root } diff --git a/server/restapi/services/agentloader_test.go b/agent/loader_test.go similarity index 56% rename from server/restapi/services/agentloader_test.go rename to agent/loader_test.go index d3ff4570..b068193e 100644 --- a/server/restapi/services/agentloader_test.go +++ b/agent/loader_test.go @@ -12,83 +12,93 @@ // See the License for the specific language governing permissions and // limitations under the License. -package services +package agent import ( + "iter" "testing" - "google.golang.org/adk/agent" - "google.golang.org/adk/agent/llmagent" + "google.golang.org/adk/session" ) +var _ Agent = (*testAgent)(nil) + +type testAgent struct { + name string +} + +func (a *testAgent) Name() string { + return a.name +} + +func (a *testAgent) Description() string { + panic("not implemented") +} + +func (a *testAgent) Run(InvocationContext) iter.Seq2[*session.Event, error] { + panic("not implemented") +} +func (a *testAgent) SubAgents() []Agent { + panic("not implemented") +} + +func (a *testAgent) internal() *agent { + panic("not implemented") +} + func TestDuplicateName(t *testing.T) { - agent1, err := llmagent.New(llmagent.Config{ - Name: "weather_time_agent", - }) - if err != nil { - t.Fatalf("failed to create agent: %v", err) - } + agent1 := &testAgent{name: "weather_time_agent"} // duplicate name - agent2, err := llmagent.New(llmagent.Config{ - Name: "weather_time_agent", - }) - if err != nil { - t.Fatalf("failed to create agent: %v", err) - } - agent3, err := llmagent.New(llmagent.Config{ - Name: "unique", - }) - if err != nil { - t.Fatalf("failed to create agent: %v", err) - } + agent2 := &testAgent{name: "weather_time_agent"} + agent3 := &testAgent{name: "unique"} tests := []struct { name string - root agent.Agent - agents []agent.Agent + root Agent + agents []Agent wantErr bool }{ { name: "root only", root: agent1, - agents: []agent.Agent{}, + agents: []Agent{}, wantErr: false, }, { name: "root duplicate object", root: agent1, - agents: []agent.Agent{agent1}, + agents: []Agent{agent1}, wantErr: true, }, { name: "root duplicate name", root: agent1, - agents: []agent.Agent{agent2}, + agents: []Agent{agent2}, wantErr: true, }, { name: "non-root duplicate name", root: agent3, - agents: []agent.Agent{agent1, agent2}, + agents: []Agent{agent1, agent2}, wantErr: true, }, { name: "non-root duplicate object", root: agent3, - agents: []agent.Agent{agent1, agent1}, + agents: []Agent{agent1, agent1}, wantErr: true, }, { name: "no duplicates", root: agent1, - agents: []agent.Agent{agent3}, + agents: []Agent{agent3}, wantErr: false, }, } for _, tt := range tests { - _, err := NewMultiAgentLoader(tt.root, tt.agents...) + _, err := NewMultiLoader(tt.root, tt.agents...) if (err != nil) != tt.wantErr { - t.Errorf("NewMultiAgentLoader() name=%v, error = %v, wantErr %v", tt.name, err, tt.wantErr) + t.Errorf("NewMultiLoader() name=%v, error = %v, wantErr %v", tt.name, err, tt.wantErr) } } diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index ac9f467e..77861f17 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -19,9 +19,9 @@ import ( "context" "github.com/a2aproject/a2a-go/a2asrv" + "google.golang.org/adk/agent" "google.golang.org/adk/artifact" "google.golang.org/adk/memory" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" ) @@ -56,6 +56,6 @@ type Config struct { SessionService session.Service ArtifactService artifact.Service MemoryService memory.Service - AgentLoader services.AgentLoader + AgentLoader agent.Loader A2AOptions []a2asrv.RequestHandlerOption } diff --git a/cmd/launcher/web/a2a/a2a_test.go b/cmd/launcher/web/a2a/a2a_test.go index ccae77ea..7d82ab84 100644 --- a/cmd/launcher/web/a2a/a2a_test.go +++ b/cmd/launcher/web/a2a/a2a_test.go @@ -27,7 +27,6 @@ import ( "google.golang.org/adk/agent" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/web" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/genai" ) @@ -82,7 +81,7 @@ func TestWebLauncher_ServesA2A(t *testing.T) { t.Fatalf("agent.New() error = %v", err) } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(agnt), + AgentLoader: agent.NewSingleLoader(agnt), SessionService: session.InMemoryService(), } diff --git a/cmd/launcher/web/api/api.go b/cmd/launcher/web/api/api.go index 58ec6227..007b8328 100644 --- a/cmd/launcher/web/api/api.go +++ b/cmd/launcher/web/api/api.go @@ -24,7 +24,7 @@ import ( "google.golang.org/adk/cmd/launcher" weblauncher "google.golang.org/adk/cmd/launcher/web" "google.golang.org/adk/internal/cli/util" - restapiweb "google.golang.org/adk/server/restapi/web" + "google.golang.org/adk/server/adkrest" ) // apiConfig contains parametres for lauching ADK REST API @@ -68,7 +68,7 @@ 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 handler - apiHandler := restapiweb.NewHandler(config) + apiHandler := adkrest.NewHandler(config) // Wrap it with CORS middleware corsHandler := corsWithArgs(a.config.frontendAddress)(apiHandler) diff --git a/cmd/launcher/web/webui/webui.go b/cmd/launcher/web/webui/webui.go index 218306b7..a85f1d33 100644 --- a/cmd/launcher/web/webui/webui.go +++ b/cmd/launcher/web/webui/webui.go @@ -27,7 +27,7 @@ import ( "google.golang.org/adk/cmd/launcher" weblauncher "google.golang.org/adk/cmd/launcher/web" "google.golang.org/adk/internal/cli/util" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // webUIConfig contains parametres for lauching ADK Web UI @@ -96,7 +96,7 @@ func (w *webUILauncher) AddSubrouter(router *mux.Router, pathPrefix string, back BackendUrl string `json:"backendUrl"` }{BackendUrl: backendAddress} rUI.Methods("GET").Path("/assets/config/runtime-config.json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - handlers.EncodeJSONResponse(runtimeConfigResponse, http.StatusOK, w) + controllers.EncodeJSONResponse(runtimeConfigResponse, http.StatusOK, w) }) // redirect the user from / to pathPrefix (/ui/) diff --git a/examples/a2a/main.go b/examples/a2a/main.go index 99e016a4..242d7e2c 100644 --- a/examples/a2a/main.go +++ b/examples/a2a/main.go @@ -32,7 +32,6 @@ import ( "google.golang.org/adk/model/gemini" "google.golang.org/adk/runner" "google.golang.org/adk/server/adka2a" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/adk/tool" "google.golang.org/adk/tool/geminitool" @@ -120,7 +119,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(remoteAgent), + AgentLoader: agent.NewSingleLoader(remoteAgent), } l := full.NewLauncher() diff --git a/examples/mcp/main.go b/examples/mcp/main.go index af47a803..1929970f 100644 --- a/examples/mcp/main.go +++ b/examples/mcp/main.go @@ -24,11 +24,11 @@ import ( "github.com/modelcontextprotocol/go-sdk/mcp" "golang.org/x/oauth2" + "google.golang.org/adk/agent" "google.golang.org/adk/agent/llmagent" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/tool" "google.golang.org/adk/tool/mcptoolset" "google.golang.org/genai" @@ -110,7 +110,7 @@ func main() { } // Create LLMAgent with MCP tool set - agent, err := llmagent.New(llmagent.Config{ + a, err := llmagent.New(llmagent.Config{ Name: "helper_agent", Model: model, Description: "Helper agent.", @@ -124,7 +124,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(agent), + AgentLoader: agent.NewSingleLoader(a), } l := full.NewLauncher() if err = l.Execute(ctx, config, os.Args[1:]); err != nil { diff --git a/examples/quickstart/main.go b/examples/quickstart/main.go index bc26aced..f386490d 100644 --- a/examples/quickstart/main.go +++ b/examples/quickstart/main.go @@ -19,11 +19,11 @@ import ( "log" "os" + "google.golang.org/adk/agent" "google.golang.org/adk/agent/llmagent" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/tool" "google.golang.org/adk/tool/geminitool" "google.golang.org/genai" @@ -39,7 +39,7 @@ func main() { log.Fatalf("Failed to create model: %v", err) } - agent, err := llmagent.New(llmagent.Config{ + a, err := llmagent.New(llmagent.Config{ Name: "weather_time_agent", Model: model, Description: "Agent to answer questions about the time and weather in a city.", @@ -53,7 +53,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(agent), + AgentLoader: agent.NewSingleLoader(a), } l := full.NewLauncher() diff --git a/examples/restapi/main.go b/examples/rest/main.go similarity index 91% rename from examples/restapi/main.go rename to examples/rest/main.go index 5e65e189..c64a436a 100644 --- a/examples/restapi/main.go +++ b/examples/rest/main.go @@ -22,11 +22,11 @@ import ( "net/http" "os" + "google.golang.org/adk/agent" "google.golang.org/adk/agent/llmagent" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" - "google.golang.org/adk/server/restapi/web" + "google.golang.org/adk/server/adkrest" "google.golang.org/adk/session" "google.golang.org/adk/tool" "google.golang.org/adk/tool/geminitool" @@ -45,7 +45,7 @@ func main() { } // Create an agent - agent, err := llmagent.New(llmagent.Config{ + a, err := llmagent.New(llmagent.Config{ Name: "weather_time_agent", Model: model, Description: "Agent to answer questions about the time and weather in a city.", @@ -60,12 +60,12 @@ func main() { // Configure the ADK REST API config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(agent), + AgentLoader: agent.NewSingleLoader(a), SessionService: session.InMemoryService(), } // Create the REST API handler - this returns a standard http.Handler - apiHandler := web.NewHandler(config) + apiHandler := adkrest.NewHandler(config) // Create a standard net/http ServeMux mux := http.NewServeMux() diff --git a/examples/tools/multipletools/main.go b/examples/tools/multipletools/main.go index ef34e712..a14f0a9c 100644 --- a/examples/tools/multipletools/main.go +++ b/examples/tools/multipletools/main.go @@ -20,11 +20,11 @@ import ( "os" "strings" + "google.golang.org/adk/agent" "google.golang.org/adk/agent/llmagent" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/tool" "google.golang.org/adk/tool/agenttool" "google.golang.org/adk/tool/functiontool" @@ -90,7 +90,7 @@ func main() { log.Fatalf("Failed to create agent: %v", err) } - agent, err := llmagent.New(llmagent.Config{ + a, err := llmagent.New(llmagent.Config{ Name: "root_agent", Model: model, Description: "You can do a google search and generate poems.", @@ -105,7 +105,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(agent), + AgentLoader: agent.NewSingleLoader(a), } l := full.NewLauncher() diff --git a/examples/vertexai/imagegenerator/main.go b/examples/vertexai/imagegenerator/main.go index f0fb0a13..ca86831b 100644 --- a/examples/vertexai/imagegenerator/main.go +++ b/examples/vertexai/imagegenerator/main.go @@ -23,12 +23,12 @@ import ( "os" "path/filepath" + "google.golang.org/adk/agent" "google.golang.org/adk/agent/llmagent" "google.golang.org/adk/artifact" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/tool" "google.golang.org/adk/tool/functiontool" "google.golang.org/adk/tool/loadartifactstool" @@ -63,7 +63,7 @@ func main() { log.Fatalf("Failed to create generate image tool: %v", err) } - agent, err := llmagent.New(llmagent.Config{ + a, err := llmagent.New(llmagent.Config{ Name: "image_generator", Model: model, Description: "Agent to generate pictures, answers questions about it and saves it locally if asked.", @@ -80,7 +80,7 @@ func main() { config := &launcher.Config{ ArtifactService: artifact.InMemoryService(), - AgentLoader: services.NewSingleAgentLoader(agent), + AgentLoader: agent.NewSingleLoader(a), } l := full.NewLauncher() diff --git a/examples/web/main.go b/examples/web/main.go index 58058075..2fff8d48 100644 --- a/examples/web/main.go +++ b/examples/web/main.go @@ -28,7 +28,6 @@ import ( "google.golang.org/adk/examples/web/agents" "google.golang.org/adk/model" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/adk/tool" "google.golang.org/adk/tool/geminitool" @@ -75,7 +74,7 @@ func main() { llmAuditor := agents.GetLLmAuditorAgent(ctx, model) imageGeneratorAgent := agents.GetImageGeneratorAgent(ctx, model) - agentLoader, err := services.NewMultiAgentLoader( + agentLoader, err := agent.NewMultiLoader( rootAgent, llmAuditor, imageGeneratorAgent, diff --git a/examples/workflowagents/loop/main.go b/examples/workflowagents/loop/main.go index 20df4ed6..ef44fc84 100644 --- a/examples/workflowagents/loop/main.go +++ b/examples/workflowagents/loop/main.go @@ -25,7 +25,6 @@ import ( "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/genai" ) @@ -71,7 +70,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(loopAgent), + AgentLoader: agent.NewSingleLoader(loopAgent), } l := full.NewLauncher() diff --git a/examples/workflowagents/parallel/main.go b/examples/workflowagents/parallel/main.go index 8dd5bda9..6f5b7bf4 100644 --- a/examples/workflowagents/parallel/main.go +++ b/examples/workflowagents/parallel/main.go @@ -28,7 +28,6 @@ import ( "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/genai" ) @@ -66,7 +65,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(parallelAgent), + AgentLoader: agent.NewSingleLoader(parallelAgent), } l := full.NewLauncher() diff --git a/examples/workflowagents/sequential/main.go b/examples/workflowagents/sequential/main.go index c4a2e4ce..250668da 100644 --- a/examples/workflowagents/sequential/main.go +++ b/examples/workflowagents/sequential/main.go @@ -26,7 +26,6 @@ import ( "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model" - "google.golang.org/adk/server/restapi/services" "google.golang.org/adk/session" "google.golang.org/genai" ) @@ -84,7 +83,7 @@ func main() { } config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(sequentialAgent), + AgentLoader: agent.NewSingleLoader(sequentialAgent), } l := full.NewLauncher() diff --git a/examples/workflowagents/sequentialCode/main.go b/examples/workflowagents/sequentialCode/main.go index f0d6ad66..34edd5f8 100644 --- a/examples/workflowagents/sequentialCode/main.go +++ b/examples/workflowagents/sequentialCode/main.go @@ -25,7 +25,6 @@ import ( "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" - "google.golang.org/adk/server/restapi/services" "google.golang.org/genai" ) @@ -140,7 +139,7 @@ Do not add any other text before or after the code block.`, log.Printf("Successfully created root agent: %s", rootAgent.Name()) config := &launcher.Config{ - AgentLoader: services.NewSingleAgentLoader(rootAgent), + AgentLoader: agent.NewSingleLoader(rootAgent), } l := full.NewLauncher() if err = l.Execute(ctx, config, os.Args[1:]); err != nil { diff --git a/server/restapi/handlers/apps.go b/server/adkrest/controllers/apps.go similarity index 73% rename from server/restapi/handlers/apps.go rename to server/adkrest/controllers/apps.go index a8a231a7..4da9de90 100644 --- a/server/restapi/handlers/apps.go +++ b/server/adkrest/controllers/apps.go @@ -12,25 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers +package controllers import ( "net/http" - "google.golang.org/adk/server/restapi/services" + "google.golang.org/adk/agent" ) // AppsAPIController is the controller for the Apps API. type AppsAPIController struct { - agentLoader services.AgentLoader + agentLoader agent.Loader } -func NewAppsAPIController(agentLoader services.AgentLoader) *AppsAPIController { +func NewAppsAPIController(agentLoader agent.Loader) *AppsAPIController { return &AppsAPIController{agentLoader: agentLoader} } -// ListApps handles listing all loaded agents. -func (c *AppsAPIController) ListApps(rw http.ResponseWriter, req *http.Request) { +// ListAppsHandler handles listing all loaded agents. +func (c *AppsAPIController) ListAppsHandler(rw http.ResponseWriter, req *http.Request) { apps := c.agentLoader.ListAgents() EncodeJSONResponse(apps, http.StatusOK, rw) } diff --git a/server/restapi/handlers/artifacts.go b/server/adkrest/controllers/artifacts.go similarity index 86% rename from server/restapi/handlers/artifacts.go rename to server/adkrest/controllers/artifacts.go index d35dbc9c..a2180e22 100644 --- a/server/restapi/handlers/artifacts.go +++ b/server/adkrest/controllers/artifacts.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers +package controllers import ( "net/http" @@ -20,7 +20,7 @@ import ( "github.com/gorilla/mux" "google.golang.org/adk/artifact" - "google.golang.org/adk/server/restapi/models" + "google.golang.org/adk/server/adkrest/internal/models" ) // ArtifactsAPIController is the controller for the Artifacts API. @@ -32,8 +32,8 @@ func NewArtifactsAPIController(artifactService artifact.Service) *ArtifactsAPICo return &ArtifactsAPIController{artifactService: artifactService} } -// ListArtifacts lists all the artifact filenames within a session. -func (c *ArtifactsAPIController) ListArtifacts(rw http.ResponseWriter, req *http.Request) { +// ListArtifactsHandler lists all the artifact filenames within a session. +func (c *ArtifactsAPIController) ListArtifactsHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(vars) if err != nil { @@ -60,8 +60,8 @@ func (c *ArtifactsAPIController) ListArtifacts(rw http.ResponseWriter, req *http EncodeJSONResponse(files, http.StatusOK, rw) } -// LoadArtifact gets an artifact from the artifact service storage. -func (c *ArtifactsAPIController) LoadArtifact(rw http.ResponseWriter, req *http.Request) { +// LoadArtifactHandler gets an artifact from the artifact service storage. +func (c *ArtifactsAPIController) LoadArtifactHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(vars) if err != nil { @@ -103,8 +103,8 @@ func (c *ArtifactsAPIController) LoadArtifact(rw http.ResponseWriter, req *http. EncodeJSONResponse(resp.Part, http.StatusOK, rw) } -// LoadArtifactVersion gets an artifact from the artifact service storage with specified version. -func (c *ArtifactsAPIController) LoadArtifactVersion(rw http.ResponseWriter, req *http.Request) { +// LoadArtifactVersionHandler gets an artifact from the artifact service storage with specified version. +func (c *ArtifactsAPIController) LoadArtifactVersionHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(vars) if err != nil { @@ -149,8 +149,8 @@ func (c *ArtifactsAPIController) LoadArtifactVersion(rw http.ResponseWriter, req EncodeJSONResponse(resp.Part, http.StatusOK, rw) } -// DeleteArtifact handles deleting an artifact. -func (c *ArtifactsAPIController) DeleteArtifact(rw http.ResponseWriter, req *http.Request) { +// DeleteArtifactHandler handles deleting an artifact. +func (c *ArtifactsAPIController) DeleteArtifactHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(vars) if err != nil { diff --git a/server/restapi/handlers/debug.go b/server/adkrest/controllers/debug.go similarity index 85% rename from server/restapi/handlers/debug.go rename to server/adkrest/controllers/debug.go index cb591ac5..aa58d6d1 100644 --- a/server/restapi/handlers/debug.go +++ b/server/adkrest/controllers/debug.go @@ -12,15 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers +package controllers import ( "fmt" "net/http" "github.com/gorilla/mux" - "google.golang.org/adk/server/restapi/models" - "google.golang.org/adk/server/restapi/services" + "google.golang.org/adk/agent" + "google.golang.org/adk/server/adkrest/internal/models" + "google.golang.org/adk/server/adkrest/internal/services" "google.golang.org/adk/session" "google.golang.org/genai" ) @@ -28,11 +29,11 @@ import ( // DebugAPIController is the controller for the Debug API. type DebugAPIController struct { sessionService session.Service - agentloader services.AgentLoader + agentloader agent.Loader spansExporter *services.APIServerSpanExporter } -func NewDebugAPIController(sessionService session.Service, agentLoader services.AgentLoader, spansExporter *services.APIServerSpanExporter) *DebugAPIController { +func NewDebugAPIController(sessionService session.Service, agentLoader agent.Loader, spansExporter *services.APIServerSpanExporter) *DebugAPIController { return &DebugAPIController{ sessionService: sessionService, agentloader: agentLoader, @@ -40,8 +41,8 @@ func NewDebugAPIController(sessionService session.Service, agentLoader services. } } -// TraceDict returns the debug information for the session in form of dictionary. -func (c *DebugAPIController) TraceDict(rw http.ResponseWriter, req *http.Request) { +// TraceDictHandler returns the debug information for the session in form of dictionary. +func (c *DebugAPIController) TraceDictHandler(rw http.ResponseWriter, req *http.Request) { params := mux.Vars(req) eventID := params["event_id"] if eventID == "" { @@ -57,8 +58,8 @@ func (c *DebugAPIController) TraceDict(rw http.ResponseWriter, req *http.Request EncodeJSONResponse(eventDict, http.StatusOK, rw) } -// EventGraph returns the debug information for the session and session events in form of graph. -func (c *DebugAPIController) EventGraph(rw http.ResponseWriter, req *http.Request) { +// EventGraphHandler returns the debug information for the session and session events in form of graph. +func (c *DebugAPIController) EventGraphHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(vars) if err != nil { diff --git a/server/restapi/errors/errors.go b/server/adkrest/controllers/errors.go similarity index 77% rename from server/restapi/errors/errors.go rename to server/adkrest/controllers/errors.go index 0d76ca7e..6e4994ef 100644 --- a/server/restapi/errors/errors.go +++ b/server/adkrest/controllers/errors.go @@ -12,23 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -package errors +package controllers -type StatusError struct { +type statusError struct { Err error Code int } -func NewStatusError(err error, code int) StatusError { - return StatusError{Err: err, Code: code} +func newStatusError(err error, code int) statusError { + return statusError{Err: err, Code: code} } // Error returns an associated error -func (se StatusError) Error() string { +func (se statusError) Error() string { return se.Err.Error() } // Status returns an associated status code -func (se StatusError) Status() int { +func (se statusError) Status() int { return se.Code } diff --git a/server/restapi/handlers/handlers.go b/server/adkrest/controllers/handlers.go similarity index 81% rename from server/restapi/handlers/handlers.go rename to server/adkrest/controllers/handlers.go index 554a3f43..f472ec53 100644 --- a/server/restapi/handlers/handlers.go +++ b/server/adkrest/controllers/handlers.go @@ -12,16 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package handlers contains the controllers for the ADK-Web REST API. -package handlers +// Package controllers contains the controllers for the ADK-Web REST API. +package controllers import ( "encoding/json" "net/http" - - "google.golang.org/adk/server/restapi/errors" ) +// TODO: Move to an internal package, controllers doesn't have to be public API. + // EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code func EncodeJSONResponse(i any, status int, w http.ResponseWriter) { wHeader := w.Header() @@ -39,12 +39,12 @@ func EncodeJSONResponse(i any, status int, w http.ResponseWriter) { type errorHandler func(http.ResponseWriter, *http.Request) error -// FromErrorHandler writes the error code returned from the http handler. -func FromErrorHandler(fn errorHandler) http.HandlerFunc { +// NewErrorHandler writes the error code returned from the http handler. +func NewErrorHandler(fn errorHandler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := fn(w, r) if err != nil { - if statusErr, ok := err.(errors.StatusError); ok { + if statusErr, ok := err.(statusError); ok { http.Error(w, statusErr.Error(), statusErr.Status()) } else { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/server/restapi/handlers/runtime.go b/server/adkrest/controllers/runtime.go similarity index 75% rename from server/restapi/handlers/runtime.go rename to server/adkrest/controllers/runtime.go index 105b132d..cfc3ca0b 100644 --- a/server/restapi/handlers/runtime.go +++ b/server/adkrest/controllers/runtime.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers +package controllers import ( "context" @@ -23,9 +23,7 @@ import ( "google.golang.org/adk/agent" "google.golang.org/adk/artifact" "google.golang.org/adk/runner" - "google.golang.org/adk/server/restapi/errors" - "google.golang.org/adk/server/restapi/models" - "google.golang.org/adk/server/restapi/services" + "google.golang.org/adk/server/adkrest/internal/models" "google.golang.org/adk/session" ) @@ -33,15 +31,15 @@ import ( type RuntimeAPIController struct { sessionService session.Service artifactService artifact.Service - agentLoader services.AgentLoader + agentLoader agent.Loader } -func NewRuntimeAPIRouter(sessionService session.Service, agentLoader services.AgentLoader, artifactService artifact.Service) *RuntimeAPIController { +func NewRuntimeAPIRouter(sessionService session.Service, agentLoader agent.Loader, artifactService artifact.Service) *RuntimeAPIController { return &RuntimeAPIController{sessionService: sessionService, agentLoader: agentLoader, artifactService: artifactService} } // RunAgent executes a non-streaming agent run for a given session and message. -func (c *RuntimeAPIController) RunAgentHTTP(rw http.ResponseWriter, req *http.Request) error { +func (c *RuntimeAPIController) RunHandler(rw http.ResponseWriter, req *http.Request) error { runAgentRequest, err := decodeRequestBody(req) if err != nil { return err @@ -75,18 +73,18 @@ func (c *RuntimeAPIController) runAgent(ctx context.Context, runAgentRequest mod var events []*session.Event for event, err := range resp { if err != nil { - return nil, errors.NewStatusError(fmt.Errorf("run agent: %w", err), http.StatusInternalServerError) + return nil, newStatusError(fmt.Errorf("run agent: %w", err), http.StatusInternalServerError) } events = append(events, event) } return events, nil } -// RunAgentSSE executes an agent run and streams the resulting events using Server-Sent Events (SSE). -func (c *RuntimeAPIController) RunAgentSSE(rw http.ResponseWriter, req *http.Request) error { +// RunSSEHandler executes an agent run and streams the resulting events using Server-Sent Events (SSE). +func (c *RuntimeAPIController) RunSSEHandler(rw http.ResponseWriter, req *http.Request) error { flusher, ok := rw.(http.Flusher) if !ok { - return errors.NewStatusError(fmt.Errorf("streaming not supported"), http.StatusInternalServerError) + return newStatusError(fmt.Errorf("streaming not supported"), http.StatusInternalServerError) } rw.Header().Set("Content-Type", "text/event-stream") @@ -115,7 +113,7 @@ func (c *RuntimeAPIController) RunAgentSSE(rw http.ResponseWriter, req *http.Req if err != nil { _, err := fmt.Fprintf(rw, "Error while running agent: %v\n", err) if err != nil { - return errors.NewStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) + return newStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) } flusher.Flush() continue @@ -131,15 +129,15 @@ func (c *RuntimeAPIController) RunAgentSSE(rw http.ResponseWriter, req *http.Req func flashEvent(flusher http.Flusher, rw http.ResponseWriter, event session.Event) error { _, err := fmt.Fprintf(rw, "data: ") if err != nil { - return errors.NewStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) + return newStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) } err = json.NewEncoder(rw).Encode(models.FromSessionEvent(event)) if err != nil { - return errors.NewStatusError(fmt.Errorf("encode response: %w", err), http.StatusInternalServerError) + return newStatusError(fmt.Errorf("encode response: %w", err), http.StatusInternalServerError) } _, err = fmt.Fprintf(rw, "\n") if err != nil { - return errors.NewStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) + return newStatusError(fmt.Errorf("write response: %w", err), http.StatusInternalServerError) } flusher.Flush() return nil @@ -152,7 +150,7 @@ func (c *RuntimeAPIController) validateSessionExists(ctx context.Context, appNam SessionID: sessionID, }) if err != nil { - return errors.NewStatusError(fmt.Errorf("get session: %w", err), http.StatusNotFound) + return newStatusError(fmt.Errorf("get session: %w", err), http.StatusNotFound) } return nil } @@ -160,7 +158,7 @@ func (c *RuntimeAPIController) validateSessionExists(ctx context.Context, appNam func (c *RuntimeAPIController) getRunner(req models.RunAgentRequest) (*runner.Runner, *agent.RunConfig, error) { curAgent, err := c.agentLoader.LoadAgent(req.AppName) if err != nil { - return nil, nil, errors.NewStatusError(fmt.Errorf("load agent: %w", err), http.StatusInternalServerError) + return nil, nil, newStatusError(fmt.Errorf("load agent: %w", err), http.StatusInternalServerError) } r, err := runner.New(runner.Config{ @@ -171,7 +169,7 @@ func (c *RuntimeAPIController) getRunner(req models.RunAgentRequest) (*runner.Ru }, ) if err != nil { - return nil, nil, errors.NewStatusError(fmt.Errorf("create runner: %w", err), http.StatusInternalServerError) + return nil, nil, newStatusError(fmt.Errorf("create runner: %w", err), http.StatusInternalServerError) } streamingMode := agent.StreamingModeNone @@ -191,7 +189,7 @@ func decodeRequestBody(req *http.Request) (decodedReq models.RunAgentRequest, er d := json.NewDecoder(req.Body) d.DisallowUnknownFields() if err := d.Decode(&runAgentRequest); err != nil { - return runAgentRequest, errors.NewStatusError(fmt.Errorf("decode request: %w", err), http.StatusBadRequest) + return runAgentRequest, newStatusError(fmt.Errorf("decode request: %w", err), http.StatusBadRequest) } return runAgentRequest, nil } diff --git a/server/restapi/handlers/sessions.go b/server/adkrest/controllers/sessions.go similarity index 91% rename from server/restapi/handlers/sessions.go rename to server/adkrest/controllers/sessions.go index f9f1383b..15792e0e 100644 --- a/server/restapi/handlers/sessions.go +++ b/server/adkrest/controllers/sessions.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers +package controllers import ( "context" @@ -20,7 +20,7 @@ import ( "net/http" "github.com/gorilla/mux" - "google.golang.org/adk/server/restapi/models" + "google.golang.org/adk/server/adkrest/internal/models" "google.golang.org/adk/session" ) @@ -37,7 +37,7 @@ func NewSessionsAPIController(service session.Service) *SessionsAPIController { } // CreateSesssionHTTP is a HTTP handler for the create session API. -func (c *SessionsAPIController) CreateSessionHTTP(rw http.ResponseWriter, req *http.Request) { +func (c *SessionsAPIController) CreateSessionHandler(rw http.ResponseWriter, req *http.Request) { params := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(params) if err != nil { @@ -81,7 +81,7 @@ func (c *SessionsAPIController) createSession(ctx context.Context, sessionID mod } // DeleteSession handles deleting a specific session. -func (c *SessionsAPIController) DeleteSessionHTTP(rw http.ResponseWriter, req *http.Request) { +func (c *SessionsAPIController) DeleteSessionHandler(rw http.ResponseWriter, req *http.Request) { params := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(params) if err != nil { @@ -106,7 +106,7 @@ func (c *SessionsAPIController) DeleteSessionHTTP(rw http.ResponseWriter, req *h } // GetSession retrieves a specific session by its ID. -func (c *SessionsAPIController) GetSessionHTTP(rw http.ResponseWriter, req *http.Request) { +func (c *SessionsAPIController) GetSessionHandler(rw http.ResponseWriter, req *http.Request) { params := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(params) if err != nil { @@ -135,7 +135,7 @@ func (c *SessionsAPIController) GetSessionHTTP(rw http.ResponseWriter, req *http } // ListSessions handles listing all sessions for a given app and user. -func (c *SessionsAPIController) ListSessionsHTTP(rw http.ResponseWriter, req *http.Request) { +func (c *SessionsAPIController) ListSessionsHandler(rw http.ResponseWriter, req *http.Request) { params := mux.Vars(req) sessionID, err := models.SessionIDFromHTTPParameters(params) if err != nil { diff --git a/server/restapi/handlers/sessions_test.go b/server/adkrest/controllers/sessions_test.go similarity index 94% rename from server/restapi/handlers/sessions_test.go rename to server/adkrest/controllers/sessions_test.go index 66738f61..49ed1492 100644 --- a/server/restapi/handlers/sessions_test.go +++ b/server/adkrest/controllers/sessions_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package handlers_test +package controllers_test import ( "bytes" @@ -27,9 +27,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/gorilla/mux" - "google.golang.org/adk/server/restapi/fakes" - "google.golang.org/adk/server/restapi/handlers" - "google.golang.org/adk/server/restapi/models" + "google.golang.org/adk/server/adkrest/controllers" + "google.golang.org/adk/server/adkrest/internal/fakes" + "google.golang.org/adk/server/adkrest/internal/models" ) func TestGetSession(t *testing.T) { @@ -116,7 +116,7 @@ func TestGetSession(t *testing.T) { for _, tt := range tc { t.Run(tt.name, func(t *testing.T) { sessionService := fakes.FakeSessionService{Sessions: tt.storedSessions} - apiController := handlers.NewSessionsAPIController(&sessionService) + apiController := controllers.NewSessionsAPIController(&sessionService) req, err := http.NewRequest(http.MethodGet, "/apps/testApp/users/testUser/sessions/testSession", nil) if err != nil { t.Fatalf("new request: %v", err) @@ -125,7 +125,7 @@ func TestGetSession(t *testing.T) { req = mux.SetURLVars(req, sessionVars(tt.sessionID)) rr := httptest.NewRecorder() - apiController.GetSessionHTTP(rr, req) + apiController.GetSessionHandler(rr, req) if status := rr.Code; status != tt.wantStatus { t.Fatalf("handler returned wrong status code: got %v want %v", status, tt.wantStatus) @@ -229,7 +229,7 @@ func TestCreateSession(t *testing.T) { for _, tt := range tc { t.Run(tt.name, func(t *testing.T) { sessionService := fakes.FakeSessionService{Sessions: tt.storedSessions} - apiController := handlers.NewSessionsAPIController(&sessionService) + apiController := controllers.NewSessionsAPIController(&sessionService) reqBytes, err := json.Marshal(tt.createRequestObj) if err != nil { t.Fatalf("marshal request: %v", err) @@ -242,7 +242,7 @@ func TestCreateSession(t *testing.T) { req = mux.SetURLVars(req, sessionVars(tt.sessionID)) rr := httptest.NewRecorder() - apiController.CreateSessionHTTP(rr, req) + apiController.CreateSessionHandler(rr, req) if status := rr.Code; status != tt.wantStatus { t.Errorf("handler returned wrong status code: got %v want %v", status, tt.wantStatus) @@ -303,7 +303,7 @@ func TestDeleteSession(t *testing.T) { for _, tt := range tc { t.Run(tt.name, func(t *testing.T) { sessionService := fakes.FakeSessionService{Sessions: tt.storedSessions} - apiController := handlers.NewSessionsAPIController(&sessionService) + apiController := controllers.NewSessionsAPIController(&sessionService) req, err := http.NewRequest(http.MethodDelete, "/apps/testApp/users/testUser/sessions/testSession", nil) if err != nil { t.Fatalf("new request: %v", err) @@ -312,7 +312,7 @@ func TestDeleteSession(t *testing.T) { req = mux.SetURLVars(req, sessionVars(tt.sessionID)) rr := httptest.NewRecorder() - apiController.DeleteSessionHTTP(rr, req) + apiController.DeleteSessionHandler(rr, req) if status := rr.Code; status != tt.wantStatus { t.Fatalf("handler returned wrong status code: got %v want %v", status, tt.wantStatus) } @@ -405,7 +405,7 @@ func TestListSessions(t *testing.T) { for _, tt := range tc { t.Run(tt.name, func(t *testing.T) { sessionService := fakes.FakeSessionService{Sessions: tt.storedSessions} - apiController := handlers.NewSessionsAPIController(&sessionService) + apiController := controllers.NewSessionsAPIController(&sessionService) req, err := http.NewRequest(http.MethodDelete, "/apps/testApp/users/testUser/sessions/testSession", nil) if err != nil { t.Fatalf("new request: %v", err) @@ -417,7 +417,7 @@ func TestListSessions(t *testing.T) { }) rr := httptest.NewRecorder() - apiController.ListSessionsHTTP(rr, req) + apiController.ListSessionsHandler(rr, req) if status := rr.Code; status != tt.wantStatus { t.Fatalf("handler returned wrong status code: got %v want %v", status, tt.wantStatus) } diff --git a/server/restapi/web/server.go b/server/adkrest/handler.go similarity index 61% rename from server/restapi/web/server.go rename to server/adkrest/handler.go index 8cf4377f..7a6f7917 100644 --- a/server/restapi/web/server.go +++ b/server/adkrest/handler.go @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package web prepares router dedicated to ADK REST API for http web server -package web +package adkrest import ( "net/http" @@ -21,9 +20,9 @@ import ( "github.com/gorilla/mux" "google.golang.org/adk/cmd/launcher" "google.golang.org/adk/internal/telemetry" - "google.golang.org/adk/server/restapi/handlers" - "google.golang.org/adk/server/restapi/routers" - "google.golang.org/adk/server/restapi/services" + "google.golang.org/adk/server/adkrest/controllers" + "google.golang.org/adk/server/adkrest/internal/routers" + "google.golang.org/adk/server/adkrest/internal/services" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -34,12 +33,14 @@ func NewHandler(config *launcher.Config) http.Handler { telemetry.AddSpanProcessor(sdktrace.NewSimpleSpanProcessor(adkExporter)) router := mux.NewRouter().StrictSlash(true) + // TODO: Allow taking a prefix to allow customizing the path + // where the ADK REST API will be served. setupRouter(router, - routers.NewSessionsAPIRouter(handlers.NewSessionsAPIController(config.SessionService)), - routers.NewRuntimeAPIRouter(handlers.NewRuntimeAPIRouter(config.SessionService, config.AgentLoader, config.ArtifactService)), - routers.NewAppsAPIRouter(handlers.NewAppsAPIController(config.AgentLoader)), - routers.NewDebugAPIRouter(handlers.NewDebugAPIController(config.SessionService, config.AgentLoader, adkExporter)), - routers.NewArtifactsAPIRouter(handlers.NewArtifactsAPIController(config.ArtifactService)), + routers.NewSessionsAPIRouter(controllers.NewSessionsAPIController(config.SessionService)), + routers.NewRuntimeAPIRouter(controllers.NewRuntimeAPIRouter(config.SessionService, config.AgentLoader, config.ArtifactService)), + 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 diff --git a/server/restapi/fakes/testsessionservice.go b/server/adkrest/internal/fakes/testsessionservice.go similarity index 100% rename from server/restapi/fakes/testsessionservice.go rename to server/adkrest/internal/fakes/testsessionservice.go diff --git a/server/restapi/models/event.go b/server/adkrest/internal/models/event.go similarity index 100% rename from server/restapi/models/event.go rename to server/adkrest/internal/models/event.go diff --git a/server/restapi/models/models.go b/server/adkrest/internal/models/models.go similarity index 100% rename from server/restapi/models/models.go rename to server/adkrest/internal/models/models.go diff --git a/server/restapi/models/runtime.go b/server/adkrest/internal/models/runtime.go similarity index 100% rename from server/restapi/models/runtime.go rename to server/adkrest/internal/models/runtime.go diff --git a/server/restapi/models/session.go b/server/adkrest/internal/models/session.go similarity index 100% rename from server/restapi/models/session.go rename to server/adkrest/internal/models/session.go diff --git a/server/restapi/routers/apps.go b/server/adkrest/internal/routers/apps.go similarity index 81% rename from server/restapi/routers/apps.go rename to server/adkrest/internal/routers/apps.go index acfe9dd0..279aee67 100644 --- a/server/restapi/routers/apps.go +++ b/server/adkrest/internal/routers/apps.go @@ -17,16 +17,16 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // AppsAPIRouter defines the routes for the Apps API. type AppsAPIRouter struct { - appsController *handlers.AppsAPIController + appsController *controllers.AppsAPIController } // NewAppsAPIRouter creates a new AppsAPIRouter. -func NewAppsAPIRouter(controller *handlers.AppsAPIController) *AppsAPIRouter { +func NewAppsAPIRouter(controller *controllers.AppsAPIController) *AppsAPIRouter { return &AppsAPIRouter{appsController: controller} } @@ -38,7 +38,7 @@ func (r *AppsAPIRouter) Routes() Routes { Name: "ListApps", Methods: []string{http.MethodGet}, Pattern: "/list-apps", - HandlerFunc: r.appsController.ListApps, + HandlerFunc: r.appsController.ListAppsHandler, }, } } diff --git a/server/restapi/routers/artifacts.go b/server/adkrest/internal/routers/artifacts.go similarity index 79% rename from server/restapi/routers/artifacts.go rename to server/adkrest/internal/routers/artifacts.go index 4207c7e7..69cead72 100644 --- a/server/restapi/routers/artifacts.go +++ b/server/adkrest/internal/routers/artifacts.go @@ -17,16 +17,16 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // ArtifactsAPIRouter defines the routes for the Artifacts API. type ArtifactsAPIRouter struct { - artifactsController *handlers.ArtifactsAPIController + artifactsController *controllers.ArtifactsAPIController } // NewArtifactsAPIRouter creates a new ArtifactsAPIRouter. -func NewArtifactsAPIRouter(controller *handlers.ArtifactsAPIController) *ArtifactsAPIRouter { +func NewArtifactsAPIRouter(controller *controllers.ArtifactsAPIController) *ArtifactsAPIRouter { return &ArtifactsAPIRouter{artifactsController: controller} } @@ -37,25 +37,25 @@ func (r *ArtifactsAPIRouter) Routes() Routes { Name: "ListArtifacts", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts", - HandlerFunc: r.artifactsController.ListArtifacts, + HandlerFunc: r.artifactsController.ListArtifactsHandler, }, Route{ Name: "LoadArtifact", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{artifact_name}", - HandlerFunc: r.artifactsController.LoadArtifact, + HandlerFunc: r.artifactsController.LoadArtifactHandler, }, Route{ Name: "LoadArtifact", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{artifact_name}/versions/{version}", - HandlerFunc: r.artifactsController.LoadArtifactVersion, + HandlerFunc: r.artifactsController.LoadArtifactVersionHandler, }, Route{ Name: "DeleteArtifact", Methods: []string{http.MethodDelete, http.MethodOptions}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{artifact_name}", - HandlerFunc: r.artifactsController.DeleteArtifact, + HandlerFunc: r.artifactsController.DeleteArtifactHandler, }, } } diff --git a/server/restapi/routers/debug.go b/server/adkrest/internal/routers/debug.go similarity index 80% rename from server/restapi/routers/debug.go rename to server/adkrest/internal/routers/debug.go index 945c91cd..29827f4e 100644 --- a/server/restapi/routers/debug.go +++ b/server/adkrest/internal/routers/debug.go @@ -17,16 +17,16 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // DebugAPIRouter defines the routes for the Debug API. type DebugAPIRouter struct { - runtimeController *handlers.DebugAPIController + runtimeController *controllers.DebugAPIController } // NewDebugAPIRouter creates a new DebugAPIRouter. -func NewDebugAPIRouter(controller *handlers.DebugAPIController) *DebugAPIRouter { +func NewDebugAPIRouter(controller *controllers.DebugAPIController) *DebugAPIRouter { return &DebugAPIRouter{runtimeController: controller} } @@ -38,19 +38,19 @@ func (r *DebugAPIRouter) Routes() Routes { Name: "GetTraceDict", Methods: []string{http.MethodGet}, Pattern: "/debug/trace/{event_id}", - HandlerFunc: r.runtimeController.TraceDict, + HandlerFunc: r.runtimeController.TraceDictHandler, }, Route{ Name: "GetEventGraph", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}/events/{event_id}/graph", - HandlerFunc: r.runtimeController.EventGraph, + HandlerFunc: r.runtimeController.EventGraphHandler, }, Route{ Name: "GetSessionTrace", Methods: []string{http.MethodGet}, Pattern: "/debug/trace/session/{session_id}", - HandlerFunc: handlers.Unimplemented, + HandlerFunc: controllers.Unimplemented, }, } } diff --git a/server/restapi/routers/eval.go b/server/adkrest/internal/routers/eval.go similarity index 87% rename from server/restapi/routers/eval.go rename to server/adkrest/internal/routers/eval.go index 3fbe95f8..2c6c2dfc 100644 --- a/server/restapi/routers/eval.go +++ b/server/adkrest/internal/routers/eval.go @@ -17,7 +17,7 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // EvalAPIRouter defines the routes for the Eval API. @@ -30,19 +30,19 @@ func (r *EvalAPIRouter) Routes() Routes { Name: "ListEvalSets", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/eval_sets", - HandlerFunc: handlers.Unimplemented, + HandlerFunc: controllers.Unimplemented, }, Route{ Name: "ListEvalSets", Methods: []string{http.MethodPost, http.MethodOptions}, Pattern: "/apps/{app_name}/eval_sets/{eval_set_name}", - HandlerFunc: handlers.Unimplemented, + HandlerFunc: controllers.Unimplemented, }, Route{ Name: "ListEvalResults", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/eval_results", - HandlerFunc: handlers.Unimplemented, + HandlerFunc: controllers.Unimplemented, }, } } diff --git a/server/restapi/routers/routers.go b/server/adkrest/internal/routers/routers.go similarity index 100% rename from server/restapi/routers/routers.go rename to server/adkrest/internal/routers/routers.go diff --git a/server/restapi/routers/runtime.go b/server/adkrest/internal/routers/runtime.go similarity index 77% rename from server/restapi/routers/runtime.go rename to server/adkrest/internal/routers/runtime.go index 1a344ee6..2c4c67ff 100644 --- a/server/restapi/routers/runtime.go +++ b/server/adkrest/internal/routers/runtime.go @@ -17,16 +17,16 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // RuntimeAPIRouter defines the routes for the Runtime API. type RuntimeAPIRouter struct { - runtimeController *handlers.RuntimeAPIController + runtimeController *controllers.RuntimeAPIController } // NewRuntimeAPIRouter creates a new RuntimeAPIRouter. -func NewRuntimeAPIRouter(controller *handlers.RuntimeAPIController) *RuntimeAPIRouter { +func NewRuntimeAPIRouter(controller *controllers.RuntimeAPIController) *RuntimeAPIRouter { return &RuntimeAPIRouter{runtimeController: controller} } @@ -38,13 +38,13 @@ func (r *RuntimeAPIRouter) Routes() Routes { Name: "RunAgent", Methods: []string{http.MethodPost, http.MethodOptions}, Pattern: "/run", - HandlerFunc: handlers.FromErrorHandler(r.runtimeController.RunAgentHTTP), + HandlerFunc: controllers.NewErrorHandler(r.runtimeController.RunHandler), }, Route{ Name: "RunAgentSse", Methods: []string{http.MethodPost, http.MethodOptions}, Pattern: "/run_sse", - HandlerFunc: handlers.FromErrorHandler(r.runtimeController.RunAgentSSE), + HandlerFunc: controllers.NewErrorHandler(r.runtimeController.RunSSEHandler), }, } } diff --git a/server/restapi/routers/sessions.go b/server/adkrest/internal/routers/sessions.go similarity index 78% rename from server/restapi/routers/sessions.go rename to server/adkrest/internal/routers/sessions.go index 6b8cf342..7d38b101 100644 --- a/server/restapi/routers/sessions.go +++ b/server/adkrest/internal/routers/sessions.go @@ -17,16 +17,16 @@ package routers import ( "net/http" - "google.golang.org/adk/server/restapi/handlers" + "google.golang.org/adk/server/adkrest/controllers" ) // SessionsAPIRouter defines the routes for the Sessions API. type SessionsAPIRouter struct { - sessionController *handlers.SessionsAPIController + sessionController *controllers.SessionsAPIController } // NewSessionsAPIRouter creates a new SessionsAPIRouter. -func NewSessionsAPIRouter(controller *handlers.SessionsAPIController) *SessionsAPIRouter { +func NewSessionsAPIRouter(controller *controllers.SessionsAPIController) *SessionsAPIRouter { return &SessionsAPIRouter{sessionController: controller} } @@ -37,31 +37,31 @@ func (r *SessionsAPIRouter) Routes() Routes { Name: "GetSession", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}", - HandlerFunc: r.sessionController.GetSessionHTTP, + HandlerFunc: r.sessionController.GetSessionHandler, }, Route{ Name: "CreateSession", Methods: []string{http.MethodPost}, Pattern: "/apps/{app_name}/users/{user_id}/sessions", - HandlerFunc: r.sessionController.CreateSessionHTTP, + HandlerFunc: r.sessionController.CreateSessionHandler, }, Route{ Name: "CreateSessionWithId", Methods: []string{http.MethodPost}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}", - HandlerFunc: r.sessionController.CreateSessionHTTP, + HandlerFunc: r.sessionController.CreateSessionHandler, }, Route{ Name: "DeleteSession", Methods: []string{http.MethodDelete, http.MethodOptions}, Pattern: "/apps/{app_name}/users/{user_id}/sessions/{session_id}", - HandlerFunc: r.sessionController.DeleteSessionHTTP, + HandlerFunc: r.sessionController.DeleteSessionHandler, }, Route{ Name: "ListSessions", Methods: []string{http.MethodGet}, Pattern: "/apps/{app_name}/users/{user_id}/sessions", - HandlerFunc: r.sessionController.ListSessionsHTTP, + HandlerFunc: r.sessionController.ListSessionsHandler, }, } } diff --git a/server/restapi/services/agentgraphgenerator.go b/server/adkrest/internal/services/agentgraphgenerator.go similarity index 100% rename from server/restapi/services/agentgraphgenerator.go rename to server/adkrest/internal/services/agentgraphgenerator.go diff --git a/server/restapi/services/agentgraphgenerator_test.go b/server/adkrest/internal/services/agentgraphgenerator_test.go similarity index 100% rename from server/restapi/services/agentgraphgenerator_test.go rename to server/adkrest/internal/services/agentgraphgenerator_test.go diff --git a/server/restapi/services/apiserverspanexporter.go b/server/adkrest/internal/services/apiserverspanexporter.go similarity index 100% rename from server/restapi/services/apiserverspanexporter.go rename to server/adkrest/internal/services/apiserverspanexporter.go diff --git a/server/restapi/services/apiserverspanexporter_test.go b/server/adkrest/internal/services/apiserverspanexporter_test.go similarity index 100% rename from server/restapi/services/apiserverspanexporter_test.go rename to server/adkrest/internal/services/apiserverspanexporter_test.go diff --git a/server/restapi/services/doc.go b/server/adkrest/internal/services/doc.go similarity index 100% rename from server/restapi/services/doc.go rename to server/adkrest/internal/services/doc.go diff --git a/server/restapi/handlers/eval.go b/server/restapi/handlers/eval.go deleted file mode 100644 index 044311d8..00000000 --- a/server/restapi/handlers/eval.go +++ /dev/null @@ -1,15 +0,0 @@ -// 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 handlers