|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/cloudwego/eino-ext/a2a/models" |
| 8 | + "github.com/cloudwego/eino-ext/a2a/transport" |
| 9 | +) |
| 10 | + |
| 11 | +// A2AClient represents an A2A protocol client |
| 12 | +type A2AClient struct { |
| 13 | + cli transport.ClientTransport |
| 14 | +} |
| 15 | + |
| 16 | +type Config struct { |
| 17 | + Transport transport.ClientTransport |
| 18 | +} |
| 19 | + |
| 20 | +// NewA2AClient creates a new A2A client |
| 21 | +func NewA2AClient(ctx context.Context, config *Config) (*A2AClient, error) { |
| 22 | + if config == nil { |
| 23 | + return nil, errors.New("config is required") |
| 24 | + } |
| 25 | + |
| 26 | + return &A2AClient{cli: config.Transport}, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (c *A2AClient) AgentCard(ctx context.Context) (*models.AgentCard, error) { |
| 30 | + return c.cli.AgentCard(ctx) |
| 31 | +} |
| 32 | + |
| 33 | +func (c *A2AClient) SendMessage(ctx context.Context, params *models.MessageSendParams) (*models.SendMessageResponseUnion, error) { |
| 34 | + if params != nil { |
| 35 | + (¶ms.Message).EnsureRequiredFields() |
| 36 | + } |
| 37 | + return c.cli.SendMessage(ctx, params) |
| 38 | +} |
| 39 | + |
| 40 | +func (c *A2AClient) SendMessageStreaming(ctx context.Context, params *models.MessageSendParams) (*ServerStreamingWrapper, error) { |
| 41 | + if params != nil { |
| 42 | + (¶ms.Message).EnsureRequiredFields() |
| 43 | + } |
| 44 | + stream, err := c.cli.SendMessageStreaming(ctx, params) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + return &ServerStreamingWrapper{stream}, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *A2AClient) GetTask(ctx context.Context, params *models.TaskQueryParams) (*models.Task, error) { |
| 52 | + return c.cli.GetTask(ctx, params) |
| 53 | +} |
| 54 | + |
| 55 | +func (c *A2AClient) CancelTask(ctx context.Context, params *models.TaskIDParams) (*models.Task, error) { |
| 56 | + return c.cli.CancelTask(ctx, params) |
| 57 | +} |
| 58 | + |
| 59 | +func (c *A2AClient) ResubscribeTask(ctx context.Context, params *models.TaskIDParams) (*ServerStreamingWrapper, error) { |
| 60 | + stream, err := c.cli.ResubscribeTask(ctx, params) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + return &ServerStreamingWrapper{stream}, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *A2AClient) SetPushNotificationConfig(ctx context.Context, params *models.TaskPushNotificationConfig) (*models.TaskPushNotificationConfig, error) { |
| 68 | + return c.cli.SetPushNotificationConfig(ctx, params) |
| 69 | +} |
| 70 | + |
| 71 | +func (c *A2AClient) GetPushNotificationConfig(ctx context.Context, params *models.GetTaskPushNotificationConfigParams) (*models.TaskPushNotificationConfig, error) { |
| 72 | + return c.cli.GetPushNotificationConfig(ctx, params) |
| 73 | +} |
| 74 | + |
| 75 | +// todo: list/delete notification config & agent/authenticatedExtendedCard |
| 76 | + |
| 77 | +type ServerStreamingWrapper struct { |
| 78 | + s models.ResponseReader |
| 79 | +} |
| 80 | + |
| 81 | +func (s *ServerStreamingWrapper) Recv() (resp *models.SendMessageStreamingResponseUnion, err error) { |
| 82 | + defer func() { |
| 83 | + if err != nil { |
| 84 | + s.s.Close() |
| 85 | + } |
| 86 | + }() |
| 87 | + return s.s.Read() |
| 88 | +} |
| 89 | + |
| 90 | +func (s *ServerStreamingWrapper) Close() error { |
| 91 | + return s.s.Close() |
| 92 | +} |
0 commit comments