Skip to content

Commit d4eb538

Browse files
committed
refactor: Refactor message part types to use 'any' instead of 'interface{}'
- Updated message converter benchmark tests to replace map[string]interface{} with map[string]any for improved type safety. - Modified message converter unit tests to utilize map[string]any, enhancing consistency across tests. - Changed generated types to use 'any' for various fields, including error types and message parts, to streamline type handling. - Adjusted types in the main types.go file to replace map[string]interface{} with map[string]any in OptimizedMessagePart for better clarity and performance. Signed-off-by: Eden Reich <[email protected]>
1 parent 129a020 commit d4eb538

36 files changed

+645
-645
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (h *SimpleTaskHandler) HandleTask(ctx context.Context, task *types.Task, me
169169
userInput := ""
170170
if message != nil {
171171
for _, part := range message.Parts {
172-
if partMap, ok := part.(map[string]interface{}); ok {
172+
if partMap, ok := part.(map[string]any); ok {
173173
if text, ok := partMap["text"].(string); ok {
174174
userInput = text
175175
break
@@ -188,7 +188,7 @@ func (h *SimpleTaskHandler) HandleTask(ctx context.Context, task *types.Task, me
188188
MessageID: fmt.Sprintf("response-%s", task.ID),
189189
Role: "assistant",
190190
Parts: []types.Part{
191-
map[string]interface{}{
191+
map[string]any{
192192
"kind": "text",
193193
"text": responseText,
194194
},
@@ -380,17 +380,17 @@ func main() {
380380
weatherTool := server.NewBasicTool(
381381
"get_weather",
382382
"Get current weather information for a location",
383-
map[string]interface{}{
383+
map[string]any{
384384
"type": "object",
385-
"properties": map[string]interface{}{
386-
"location": map[string]interface{}{
385+
"properties": map[string]any{
386+
"location": map[string]any{
387387
"type": "string",
388388
"description": "The city name",
389389
},
390390
},
391391
"required": []string{"location"},
392392
},
393-
func(ctx context.Context, args map[string]interface{}) (string, error) {
393+
func(ctx context.Context, args map[string]any) (string, error) {
394394
location := args["location"].(string)
395395
return fmt.Sprintf(`{"location": "%s", "temperature": "22°C", "condition": "sunny", "humidity": "65%%"}`, location), nil
396396
},
@@ -401,11 +401,11 @@ func main() {
401401
timeTool := server.NewBasicTool(
402402
"get_current_time",
403403
"Get the current date and time",
404-
map[string]interface{}{
404+
map[string]any{
405405
"type": "object",
406-
"properties": map[string]interface{}{},
406+
"properties": map[string]any{},
407407
},
408-
func(ctx context.Context, args map[string]interface{}) (string, error) {
408+
func(ctx context.Context, args map[string]any) (string, error) {
409409
now := time.Now()
410410
return fmt.Sprintf(`{"current_time": "%s", "timezone": "%s"}`,
411411
now.Format("2006-01-02 15:04:05"), now.Location()), nil
@@ -1208,17 +1208,17 @@ toolBox := server.NewToolBox()
12081208
weatherTool := server.NewBasicTool(
12091209
"get_weather",
12101210
"Get current weather for a location",
1211-
map[string]interface{}{
1211+
map[string]any{
12121212
"type": "object",
1213-
"properties": map[string]interface{}{
1214-
"location": map[string]interface{}{
1213+
"properties": map[string]any{
1214+
"location": map[string]any{
12151215
"type": "string",
12161216
"description": "The city and state, e.g. San Francisco, CA",
12171217
},
12181218
},
12191219
"required": []string{"location"},
12201220
},
1221-
func(ctx context.Context, args map[string]interface{}) (string, error) {
1221+
func(ctx context.Context, args map[string]any) (string, error) {
12221222
location := args["location"].(string)
12231223

12241224
// Your weather API logic here
@@ -1410,13 +1410,13 @@ Leverage standard tool use patterns and Model Context Protocol (MCP) with task p
14101410
inputTool := server.NewBasicTool(
14111411
"request_user_input",
14121412
"Request additional input from the user",
1413-
map[string]interface{}{
1413+
map[string]any{
14141414
"type": "object",
1415-
"properties": map[string]interface{}{
1415+
"properties": map[string]any{
14161416
"prompt": {"type": "string", "description": "Question for the user"},
14171417
},
14181418
},
1419-
func(ctx context.Context, args map[string]interface{}) (string, error) {
1419+
func(ctx context.Context, args map[string]any) (string, error) {
14201420
prompt := args["prompt"].(string)
14211421

14221422
// Extract taskID from context (set by agent)
@@ -1440,14 +1440,14 @@ inputTool := server.NewBasicTool(
14401440
mcpConfirmTool := server.NewBasicTool(
14411441
"mcp_confirm_action",
14421442
"Request user confirmation via MCP protocol",
1443-
map[string]interface{}{
1443+
map[string]any{
14441444
"type": "object",
1445-
"properties": map[string]interface{}{
1445+
"properties": map[string]any{
14461446
"action": {"type": "string", "description": "Action requiring confirmation"},
14471447
"details": {"type": "object", "description": "Action details"},
14481448
},
14491449
},
1450-
func(ctx context.Context, args map[string]interface{}) (string, error) {
1450+
func(ctx context.Context, args map[string]any) (string, error) {
14511451
// Use MCP to present structured confirmation request
14521452
confirmationRequest := buildMCPConfirmation(args)
14531453

@@ -1505,7 +1505,7 @@ type CustomTaskProcessor struct{}
15051505

15061506
func (ctp *CustomTaskProcessor) ProcessToolResult(toolCallResult string) *adk.Message {
15071507
// Parse the tool result
1508-
var result map[string]interface{}
1508+
var result map[string]any
15091509
json.Unmarshal([]byte(toolCallResult), &result)
15101510

15111511
// Apply your business logic

client/client.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type A2AClient interface {
2323

2424
// Task operations
2525
SendTask(ctx context.Context, params types.MessageSendParams) (*types.JSONRPCSuccessResponse, error)
26-
SendTaskStreaming(ctx context.Context, params types.MessageSendParams, eventChan chan<- interface{}) error
26+
SendTaskStreaming(ctx context.Context, params types.MessageSendParams, eventChan chan<- any) error
2727
GetTask(ctx context.Context, params types.TaskQueryParams) (*types.JSONRPCSuccessResponse, error)
2828
ListTasks(ctx context.Context, params types.TaskListParams) (*types.JSONRPCSuccessResponse, error)
2929
CancelTask(ctx context.Context, params types.TaskIdParams) (*types.JSONRPCSuccessResponse, error)
@@ -128,7 +128,7 @@ func (c *Client) SendTask(ctx context.Context, params types.MessageSendParams) (
128128
req := types.JSONRPCRequest{
129129
JSONRPC: "2.0",
130130
Method: "message/send",
131-
Params: make(map[string]interface{}),
131+
Params: make(map[string]any),
132132
}
133133

134134
paramsBytes, err := json.Marshal(params)
@@ -137,7 +137,7 @@ func (c *Client) SendTask(ctx context.Context, params types.MessageSendParams) (
137137
return nil, fmt.Errorf("failed to marshal params: %w", err)
138138
}
139139

140-
var paramsMap map[string]interface{}
140+
var paramsMap map[string]any
141141
if err := json.Unmarshal(paramsBytes, &paramsMap); err != nil {
142142
c.logger.Error("failed to unmarshal params to map", zap.Error(err))
143143
return nil, fmt.Errorf("failed to unmarshal params to map: %w", err)
@@ -155,7 +155,7 @@ func (c *Client) SendTask(ctx context.Context, params types.MessageSendParams) (
155155
}
156156

157157
// SendTaskStreaming sends a task and streams the response (primary interface following official A2A pattern)
158-
func (c *Client) SendTaskStreaming(ctx context.Context, params types.MessageSendParams, eventChan chan<- interface{}) error {
158+
func (c *Client) SendTaskStreaming(ctx context.Context, params types.MessageSendParams, eventChan chan<- any) error {
159159
c.logger.Debug("starting task streaming",
160160
zap.String("method", "message/stream"),
161161
zap.String("message_id", params.Message.MessageID),
@@ -164,7 +164,7 @@ func (c *Client) SendTaskStreaming(ctx context.Context, params types.MessageSend
164164
req := types.JSONRPCRequest{
165165
JSONRPC: "2.0",
166166
Method: "message/stream",
167-
Params: make(map[string]interface{}),
167+
Params: make(map[string]any),
168168
}
169169

170170
paramsBytes, err := json.Marshal(params)
@@ -173,7 +173,7 @@ func (c *Client) SendTaskStreaming(ctx context.Context, params types.MessageSend
173173
return fmt.Errorf("failed to marshal params: %w", err)
174174
}
175175

176-
var paramsMap map[string]interface{}
176+
var paramsMap map[string]any
177177
if err := json.Unmarshal(paramsBytes, &paramsMap); err != nil {
178178
c.logger.Error("failed to unmarshal params to map", zap.Error(err))
179179
return fmt.Errorf("failed to unmarshal params to map: %w", err)
@@ -272,7 +272,7 @@ func (c *Client) GetTask(ctx context.Context, params types.TaskQueryParams) (*ty
272272
req := types.JSONRPCRequest{
273273
JSONRPC: "2.0",
274274
Method: "tasks/get",
275-
Params: make(map[string]interface{}),
275+
Params: make(map[string]any),
276276
}
277277

278278
paramsBytes, err := json.Marshal(params)
@@ -281,7 +281,7 @@ func (c *Client) GetTask(ctx context.Context, params types.TaskQueryParams) (*ty
281281
return nil, fmt.Errorf("failed to marshal params: %w", err)
282282
}
283283

284-
var paramsMap map[string]interface{}
284+
var paramsMap map[string]any
285285
if err := json.Unmarshal(paramsBytes, &paramsMap); err != nil {
286286
c.logger.Error("failed to unmarshal params to map", zap.Error(err))
287287
return nil, fmt.Errorf("failed to unmarshal params to map: %w", err)
@@ -305,7 +305,7 @@ func (c *Client) CancelTask(ctx context.Context, params types.TaskIdParams) (*ty
305305
req := types.JSONRPCRequest{
306306
JSONRPC: "2.0",
307307
Method: "tasks/cancel",
308-
Params: make(map[string]interface{}),
308+
Params: make(map[string]any),
309309
}
310310

311311
paramsBytes, err := json.Marshal(params)
@@ -314,7 +314,7 @@ func (c *Client) CancelTask(ctx context.Context, params types.TaskIdParams) (*ty
314314
return nil, fmt.Errorf("failed to marshal params: %w", err)
315315
}
316316

317-
var paramsMap map[string]interface{}
317+
var paramsMap map[string]any
318318
if err := json.Unmarshal(paramsBytes, &paramsMap); err != nil {
319319
c.logger.Error("failed to unmarshal params to map", zap.Error(err))
320320
return nil, fmt.Errorf("failed to unmarshal params to map: %w", err)
@@ -338,7 +338,7 @@ func (c *Client) ListTasks(ctx context.Context, params types.TaskListParams) (*t
338338
req := types.JSONRPCRequest{
339339
JSONRPC: "2.0",
340340
Method: "tasks/list",
341-
Params: make(map[string]interface{}),
341+
Params: make(map[string]any),
342342
}
343343

344344
paramsBytes, err := json.Marshal(params)
@@ -347,7 +347,7 @@ func (c *Client) ListTasks(ctx context.Context, params types.TaskListParams) (*t
347347
return nil, fmt.Errorf("failed to marshal params: %w", err)
348348
}
349349

350-
var paramsMap map[string]interface{}
350+
var paramsMap map[string]any
351351
if err := json.Unmarshal(paramsBytes, &paramsMap); err != nil {
352352
c.logger.Error("failed to unmarshal params to map", zap.Error(err))
353353
return nil, fmt.Errorf("failed to unmarshal params to map: %w", err)
@@ -553,7 +553,7 @@ func (c *Client) doRequestWithContext(ctx context.Context, req types.JSONRPCRequ
553553

554554
var rawResp struct {
555555
JSONRPC string `json:"jsonrpc"`
556-
ID interface{} `json:"id,omitempty"`
556+
ID any `json:"id,omitempty"`
557557
Result json.RawMessage `json:"result,omitempty"`
558558
Error *types.JSONRPCError `json:"error,omitempty"`
559559
}

0 commit comments

Comments
 (0)