Skip to content

Commit f5d265a

Browse files
authored
Restructure the REST API packages (google#254)
1 parent 437ef38 commit f5d265a

File tree

44 files changed

+215
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+215
-229
lines changed

‎server/restapi/services/agentloader.go‎ renamed to ‎agent/loader.go‎

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,45 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package services
15+
package agent
1616

1717
import (
1818
"fmt"
19-
20-
"google.golang.org/adk/agent"
2119
)
2220

23-
// AgentLoader allows to load a particular agent by name and get the root agent
24-
type AgentLoader interface {
21+
// Loader allows to load a particular agent by name and get the root agent
22+
type Loader interface {
2523
// ListAgents returns a list of names of all agents
2624
ListAgents() []string
2725
// LoadAgent returns an agent by its name. Returns error if there is no agent with such a name.
28-
LoadAgent(name string) (agent.Agent, error)
26+
LoadAgent(name string) (Agent, error)
2927
// RootAgent returns the root agent
30-
RootAgent() agent.Agent
28+
RootAgent() Agent
3129
}
3230

33-
// multiAgentLoader should be used when you have multiple agents
34-
type multiAgentLoader struct {
35-
agentMap map[string]agent.Agent
36-
root agent.Agent
31+
// multiLoader should be used when you have multiple agents
32+
type multiLoader struct {
33+
agentMap map[string]Agent
34+
root Agent
3735
}
3836

39-
// singleAgentLoader should be used when you have only one agent
40-
type singleAgentLoader struct {
41-
root agent.Agent
37+
// singleLoader should be used when you have only one agent
38+
type singleLoader struct {
39+
root Agent
4240
}
4341

44-
// NewSingleAgentLoader returns a loader with only one agent, which becomes the root agent
45-
func NewSingleAgentLoader(a agent.Agent) AgentLoader {
46-
return &singleAgentLoader{root: a}
42+
// NewSingleLoader returns a loader with only one agent, which becomes the root agent
43+
func NewSingleLoader(a Agent) Loader {
44+
return &singleLoader{root: a}
4745
}
4846

4947
// singleAgentLoader implements AgentLoader. Returns root agent's name
50-
func (s *singleAgentLoader) ListAgents() []string {
48+
func (s *singleLoader) ListAgents() []string {
5149
return []string{s.root.Name()}
5250
}
5351

5452
// singleAgentLoader implements AgentLoader. Returns root for empty name and for root.Name(), error otherwise.
55-
func (s *singleAgentLoader) LoadAgent(name string) (agent.Agent, error) {
53+
func (s *singleLoader) LoadAgent(name string) (Agent, error) {
5654
if name == "" {
5755
return s.root, nil
5856
}
@@ -63,14 +61,14 @@ func (s *singleAgentLoader) LoadAgent(name string) (agent.Agent, error) {
6361
}
6462

6563
// singleAgentLoader implements AgentLoader. Returns the root agent.
66-
func (s *singleAgentLoader) RootAgent() agent.Agent {
64+
func (s *singleLoader) RootAgent() Agent {
6765
return s.root
6866
}
6967

70-
// NewMultiAgentLoader returns a new AgentLoader with the given root Agent and other agents.
68+
// NewMultiLoader returns a new AgentLoader with the given root Agent and other agents.
7169
// Returns an error if more than one agent (including root) shares the same name
72-
func NewMultiAgentLoader(root agent.Agent, agents ...agent.Agent) (AgentLoader, error) {
73-
m := make(map[string]agent.Agent)
70+
func NewMultiLoader(root Agent, agents ...Agent) (Loader, error) {
71+
m := make(map[string]Agent)
7472
m[root.Name()] = root
7573
for _, a := range agents {
7674
if _, ok := m[a.Name()]; ok {
@@ -79,14 +77,14 @@ func NewMultiAgentLoader(root agent.Agent, agents ...agent.Agent) (AgentLoader,
7977
}
8078
m[a.Name()] = a
8179
}
82-
return &multiAgentLoader{
80+
return &multiLoader{
8381
agentMap: m,
8482
root: root,
8583
}, nil
8684
}
8785

8886
// multiAgentLoader implements AgentLoader. Returns the list of all agents' names (including root agent)
89-
func (m *multiAgentLoader) ListAgents() []string {
87+
func (m *multiLoader) ListAgents() []string {
9088
agents := make([]string, 0, len(m.agentMap))
9189
for name := range m.agentMap {
9290
agents = append(agents, name)
@@ -95,7 +93,7 @@ func (m *multiAgentLoader) ListAgents() []string {
9593
}
9694

9795
// multiAgentLoader implements LoadAgent. Returns an agent with given name or error if no such an agent is found
98-
func (m *multiAgentLoader) LoadAgent(name string) (agent.Agent, error) {
96+
func (m *multiLoader) LoadAgent(name string) (Agent, error) {
9997
agent, ok := m.agentMap[name]
10098
if !ok {
10199
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) {
104102
}
105103

106104
// multiAgentLoader implements LoadAgent.
107-
func (m *multiAgentLoader) RootAgent() agent.Agent {
105+
func (m *multiLoader) RootAgent() Agent {
108106
return m.root
109107
}

‎server/restapi/services/agentloader_test.go‎ renamed to ‎agent/loader_test.go‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,93 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package services
15+
package agent
1616

1717
import (
18+
"iter"
1819
"testing"
1920

20-
"google.golang.org/adk/agent"
21-
"google.golang.org/adk/agent/llmagent"
21+
"google.golang.org/adk/session"
2222
)
2323

24+
var _ Agent = (*testAgent)(nil)
25+
26+
type testAgent struct {
27+
name string
28+
}
29+
30+
func (a *testAgent) Name() string {
31+
return a.name
32+
}
33+
34+
func (a *testAgent) Description() string {
35+
panic("not implemented")
36+
}
37+
38+
func (a *testAgent) Run(InvocationContext) iter.Seq2[*session.Event, error] {
39+
panic("not implemented")
40+
}
41+
func (a *testAgent) SubAgents() []Agent {
42+
panic("not implemented")
43+
}
44+
45+
func (a *testAgent) internal() *agent {
46+
panic("not implemented")
47+
}
48+
2449
func TestDuplicateName(t *testing.T) {
25-
agent1, err := llmagent.New(llmagent.Config{
26-
Name: "weather_time_agent",
27-
})
28-
if err != nil {
29-
t.Fatalf("failed to create agent: %v", err)
30-
}
50+
agent1 := &testAgent{name: "weather_time_agent"}
3151
// duplicate name
32-
agent2, err := llmagent.New(llmagent.Config{
33-
Name: "weather_time_agent",
34-
})
35-
if err != nil {
36-
t.Fatalf("failed to create agent: %v", err)
37-
}
38-
agent3, err := llmagent.New(llmagent.Config{
39-
Name: "unique",
40-
})
41-
if err != nil {
42-
t.Fatalf("failed to create agent: %v", err)
43-
}
52+
agent2 := &testAgent{name: "weather_time_agent"}
53+
agent3 := &testAgent{name: "unique"}
4454

4555
tests := []struct {
4656
name string
47-
root agent.Agent
48-
agents []agent.Agent
57+
root Agent
58+
agents []Agent
4959
wantErr bool
5060
}{
5161
{
5262
name: "root only",
5363
root: agent1,
54-
agents: []agent.Agent{},
64+
agents: []Agent{},
5565
wantErr: false,
5666
},
5767
{
5868
name: "root duplicate object",
5969
root: agent1,
60-
agents: []agent.Agent{agent1},
70+
agents: []Agent{agent1},
6171
wantErr: true,
6272
},
6373
{
6474
name: "root duplicate name",
6575
root: agent1,
66-
agents: []agent.Agent{agent2},
76+
agents: []Agent{agent2},
6777
wantErr: true,
6878
},
6979
{
7080
name: "non-root duplicate name",
7181
root: agent3,
72-
agents: []agent.Agent{agent1, agent2},
82+
agents: []Agent{agent1, agent2},
7383
wantErr: true,
7484
},
7585
{
7686
name: "non-root duplicate object",
7787
root: agent3,
78-
agents: []agent.Agent{agent1, agent1},
88+
agents: []Agent{agent1, agent1},
7989
wantErr: true,
8090
},
8191
{
8292
name: "no duplicates",
8393
root: agent1,
84-
agents: []agent.Agent{agent3},
94+
agents: []Agent{agent3},
8595
wantErr: false,
8696
},
8797
}
8898
for _, tt := range tests {
89-
_, err := NewMultiAgentLoader(tt.root, tt.agents...)
99+
_, err := NewMultiLoader(tt.root, tt.agents...)
90100
if (err != nil) != tt.wantErr {
91-
t.Errorf("NewMultiAgentLoader() name=%v, error = %v, wantErr %v", tt.name, err, tt.wantErr)
101+
t.Errorf("NewMultiLoader() name=%v, error = %v, wantErr %v", tt.name, err, tt.wantErr)
92102
}
93103
}
94104

‎cmd/launcher/launcher.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"context"
2020

2121
"github.com/a2aproject/a2a-go/a2asrv"
22+
"google.golang.org/adk/agent"
2223
"google.golang.org/adk/artifact"
2324
"google.golang.org/adk/memory"
24-
"google.golang.org/adk/server/restapi/services"
2525
"google.golang.org/adk/session"
2626
)
2727

@@ -56,6 +56,6 @@ type Config struct {
5656
SessionService session.Service
5757
ArtifactService artifact.Service
5858
MemoryService memory.Service
59-
AgentLoader services.AgentLoader
59+
AgentLoader agent.Loader
6060
A2AOptions []a2asrv.RequestHandlerOption
6161
}

‎cmd/launcher/web/a2a/a2a_test.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"google.golang.org/adk/agent"
2828
"google.golang.org/adk/cmd/launcher"
2929
"google.golang.org/adk/cmd/launcher/web"
30-
"google.golang.org/adk/server/restapi/services"
3130
"google.golang.org/adk/session"
3231
"google.golang.org/genai"
3332
)
@@ -82,7 +81,7 @@ func TestWebLauncher_ServesA2A(t *testing.T) {
8281
t.Fatalf("agent.New() error = %v", err)
8382
}
8483
config := &launcher.Config{
85-
AgentLoader: services.NewSingleAgentLoader(agnt),
84+
AgentLoader: agent.NewSingleLoader(agnt),
8685
SessionService: session.InMemoryService(),
8786
}
8887

‎cmd/launcher/web/api/api.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"google.golang.org/adk/cmd/launcher"
2525
weblauncher "google.golang.org/adk/cmd/launcher/web"
2626
"google.golang.org/adk/internal/cli/util"
27-
restapiweb "google.golang.org/adk/server/restapi/web"
27+
"google.golang.org/adk/server/adkrest"
2828
)
2929

3030
// apiConfig contains parametres for lauching ADK REST API
@@ -68,7 +68,7 @@ func (a *apiLauncher) UserMessage(webURL string, printer func(v ...any)) {
6868
// SetupSubrouters adds the API router to the parent router.
6969
func (a *apiLauncher) SetupSubrouters(router *mux.Router, config *launcher.Config) error {
7070
// Create the ADK REST API handler
71-
apiHandler := restapiweb.NewHandler(config)
71+
apiHandler := adkrest.NewHandler(config)
7272

7373
// Wrap it with CORS middleware
7474
corsHandler := corsWithArgs(a.config.frontendAddress)(apiHandler)

‎cmd/launcher/web/webui/webui.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"google.golang.org/adk/cmd/launcher"
2828
weblauncher "google.golang.org/adk/cmd/launcher/web"
2929
"google.golang.org/adk/internal/cli/util"
30-
"google.golang.org/adk/server/restapi/handlers"
30+
"google.golang.org/adk/server/adkrest/controllers"
3131
)
3232

3333
// webUIConfig contains parametres for lauching ADK Web UI
@@ -96,7 +96,7 @@ func (w *webUILauncher) AddSubrouter(router *mux.Router, pathPrefix string, back
9696
BackendUrl string `json:"backendUrl"`
9797
}{BackendUrl: backendAddress}
9898
rUI.Methods("GET").Path("/assets/config/runtime-config.json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
99-
handlers.EncodeJSONResponse(runtimeConfigResponse, http.StatusOK, w)
99+
controllers.EncodeJSONResponse(runtimeConfigResponse, http.StatusOK, w)
100100
})
101101

102102
// redirect the user from / to pathPrefix (/ui/)

‎examples/a2a/main.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"google.golang.org/adk/model/gemini"
3333
"google.golang.org/adk/runner"
3434
"google.golang.org/adk/server/adka2a"
35-
"google.golang.org/adk/server/restapi/services"
3635
"google.golang.org/adk/session"
3736
"google.golang.org/adk/tool"
3837
"google.golang.org/adk/tool/geminitool"
@@ -120,7 +119,7 @@ func main() {
120119
}
121120

122121
config := &launcher.Config{
123-
AgentLoader: services.NewSingleAgentLoader(remoteAgent),
122+
AgentLoader: agent.NewSingleLoader(remoteAgent),
124123
}
125124

126125
l := full.NewLauncher()

‎examples/mcp/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424

2525
"github.com/modelcontextprotocol/go-sdk/mcp"
2626
"golang.org/x/oauth2"
27+
"google.golang.org/adk/agent"
2728
"google.golang.org/adk/agent/llmagent"
2829
"google.golang.org/adk/cmd/launcher"
2930
"google.golang.org/adk/cmd/launcher/full"
3031
"google.golang.org/adk/model/gemini"
31-
"google.golang.org/adk/server/restapi/services"
3232
"google.golang.org/adk/tool"
3333
"google.golang.org/adk/tool/mcptoolset"
3434
"google.golang.org/genai"
@@ -110,7 +110,7 @@ func main() {
110110
}
111111

112112
// Create LLMAgent with MCP tool set
113-
agent, err := llmagent.New(llmagent.Config{
113+
a, err := llmagent.New(llmagent.Config{
114114
Name: "helper_agent",
115115
Model: model,
116116
Description: "Helper agent.",
@@ -124,7 +124,7 @@ func main() {
124124
}
125125

126126
config := &launcher.Config{
127-
AgentLoader: services.NewSingleAgentLoader(agent),
127+
AgentLoader: agent.NewSingleLoader(a),
128128
}
129129
l := full.NewLauncher()
130130
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {

‎examples/quickstart/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
"log"
2020
"os"
2121

22+
"google.golang.org/adk/agent"
2223
"google.golang.org/adk/agent/llmagent"
2324
"google.golang.org/adk/cmd/launcher"
2425
"google.golang.org/adk/cmd/launcher/full"
2526
"google.golang.org/adk/model/gemini"
26-
"google.golang.org/adk/server/restapi/services"
2727
"google.golang.org/adk/tool"
2828
"google.golang.org/adk/tool/geminitool"
2929
"google.golang.org/genai"
@@ -39,7 +39,7 @@ func main() {
3939
log.Fatalf("Failed to create model: %v", err)
4040
}
4141

42-
agent, err := llmagent.New(llmagent.Config{
42+
a, err := llmagent.New(llmagent.Config{
4343
Name: "weather_time_agent",
4444
Model: model,
4545
Description: "Agent to answer questions about the time and weather in a city.",
@@ -53,7 +53,7 @@ func main() {
5353
}
5454

5555
config := &launcher.Config{
56-
AgentLoader: services.NewSingleAgentLoader(agent),
56+
AgentLoader: agent.NewSingleLoader(a),
5757
}
5858

5959
l := full.NewLauncher()

0 commit comments

Comments
 (0)