()
| 213 | } |
| 214 | |
| 215 | func (t *Toolset) createHandler() tools.ToolHandler { |
| 216 | return func(ctx context.Context, toolCall tools.ToolCall) (*tools.ToolCallResult, error) { |
| 217 | var args struct { |
| 218 | Message string `json:"message"` |
| 219 | } |
| 220 | if err := json.Unmarshal([]byte(toolCall.Function.Arguments), &args); err != nil { |
| 221 | return nil, fmt.Errorf("failed to parse arguments: %w", err) |
| 222 | } |
| 223 | |
| 224 | t.mu.RLock() |
| 225 | client := t.client |
| 226 | t.mu.RUnlock() |
| 227 | |
| 228 | if client == nil { |
| 229 | return nil, errors.New("A2A client not initialized") |
| 230 | } |
| 231 | |
| 232 | params := &a2a.MessageSendParams{ |
| 233 | Message: a2a.NewMessage(a2a.MessageRoleUser, &a2a.TextPart{Text: args.Message}), |
| 234 | } |
| 235 | |
| 236 | var response strings.Builder |
| 237 | for event, err := range client.SendStreamingMessage(ctx, params) { |
| 238 | if err != nil { |
| 239 | return nil, fmt.Errorf("A2A call failed: %w", err) |
| 240 | } |
| 241 | response.WriteString(extractText(event)) |
| 242 | } |
| 243 | |
| 244 | result := cmp.Or(response.String(), "No response from agent") |
| 245 | |
| 246 | // TODO(dga): could this be a tool call error? |
| 247 | return tools.ResultSuccess(result), nil |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func extractText(event a2a.Event) string { |
| 252 | var parts a2a.ContentParts |
no test coverage detected