Implement json.Unmarshaler to CreateMessageResult (#191)
When calling `ServerSession.CreateMessage`, will get an json unmarshal error because `CreateMessageResult.Content` is an interface, and it doesn't implement `json.Unmarshaller`. Implement the `json.Unmarshaller` to `CreateMessageResult`, let it able to unmarshal the client response without error
This commit is contained in:
parent
982a0bceca
commit
bacca7a1d2
2 changed files with 18 additions and 1 deletions
|
|
@ -125,7 +125,7 @@ func TestEndToEnd(t *testing.T) {
|
|||
loggingMessages := make(chan *LoggingMessageParams, 100) // big enough for all logging
|
||||
opts := &ClientOptions{
|
||||
CreateMessageHandler: func(context.Context, *ClientSession, *CreateMessageParams) (*CreateMessageResult, error) {
|
||||
return &CreateMessageResult{Model: "aModel"}, nil
|
||||
return &CreateMessageResult{Model: "aModel", Content: &TextContent{}}, nil
|
||||
},
|
||||
ToolListChangedHandler: func(context.Context, *ClientSession, *ToolListChangedParams) { notificationChans["tools"] <- 0 },
|
||||
PromptListChangedHandler: func(context.Context, *ClientSession, *PromptListChangedParams) { notificationChans["prompts"] <- 0 },
|
||||
|
|
|
|||
|
|
@ -264,6 +264,23 @@ type CreateMessageResult struct {
|
|||
StopReason string `json:"stopReason,omitempty"`
|
||||
}
|
||||
|
||||
func (r *CreateMessageResult) UnmarshalJSON(data []byte) error {
|
||||
type result CreateMessageResult // avoid recursion
|
||||
var wire struct {
|
||||
result
|
||||
Content *wireContent `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &wire); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
if wire.result.Content, err = contentFromWire(wire.Content, map[string]bool{"text": true, "image": true, "audio": true}); err != nil {
|
||||
return err
|
||||
}
|
||||
*r = CreateMessageResult(wire.result)
|
||||
return nil
|
||||
}
|
||||
|
||||
type GetPromptParams struct {
|
||||
// This property is reserved by the protocol to allow clients and servers to
|
||||
// attach additional metadata to their responses.
|
||||
|
|
|
|||
Loading…
Reference in a new issue