@@ -10,58 +10,67 @@ import (
1010 "fmt"
1111)
1212
13- // Content is the wire format for content.
14- // It represents the protocol types TextContent, ImageContent, AudioContent
15- // and EmbeddedResource.
16- // Use [NewTextContent], [NewImageContent], [NewAudioContent] or [NewResourceContent]
17- // to create one.
13+ // A ContentBlock is one of a TextContent, ImageContent, AudioContent
14+ // ResourceLink, or EmbeddedResource.
15+ // Use [NewTextContent], [NewImageContent], [NewAudioContent], [NewResourceLink]
16+ // or [NewResourceContents] to create one.
1817//
19- // The Type field must be one of "text", "image", "audio" or "resource". The
20- // constructors above populate this field appropriately.
21- // Although at most one of Text, Data, and Resource should be non-zero, consumers of Content
22- // use the Type field to determine which value to use; values in the other fields are ignored.
23- type Content struct {
24- Type string `json:"type"`
25- Text string `json:"text,omitempty"`
26- MIMEType string `json:"mimeType,omitempty"`
27- Data []byte `json:"data,omitempty"`
28- Resource * ResourceContents `json:"resource,omitempty"`
29- Annotations * Annotations `json:"annotations,omitempty"`
18+ // The Type field must be one of "text", "image", "audio", "resource_link" or "resource".
19+ // The constructors above populate this field appropriately.
20+ // Although at most one of Text, Data, ResourceLink and Resource should be non-zero,
21+ // consumers of ContentBlock use the Type field to determine which value to use;
22+ // values in the other fields are ignored.
23+ // TODO(jba,rfindley): rethink this type. Each kind (text, image, etc.) should have its own
24+ // meta and annotations, otherwise they're duplicated for Resource and ResourceContents.
25+ type ContentBlock struct {
26+ Meta map [string ]any `json:"_meta,omitempty"`
27+ Type string `json:"type"`
28+ Text string `json:"text,omitempty"`
29+ MIMEType string `json:"mimeType,omitempty"`
30+ Data []byte `json:"data,omitempty"`
31+ ResourceLink * Resource `json:"resource_link,omitempty"`
32+ Resource * ResourceContents `json:"resource,omitempty"`
33+ Annotations * Annotations `json:"annotations,omitempty"`
3034}
3135
32- func (c * Content ) UnmarshalJSON (data []byte ) error {
33- type wireContent Content // for naive unmarshaling
36+ func (c * ContentBlock ) UnmarshalJSON (data []byte ) error {
37+ type wireContent ContentBlock // for naive unmarshaling
3438 var c2 wireContent
3539 if err := json .Unmarshal (data , & c2 ); err != nil {
3640 return err
3741 }
3842 switch c2 .Type {
39- case "text" , "image" , "audio" , "resource" :
43+ case "text" , "image" , "audio" , "resource" , "resource_link" :
4044 default :
4145 return fmt .Errorf ("unrecognized content type %s" , c .Type )
4246 }
43- * c = Content (c2 )
47+ * c = ContentBlock (c2 )
4448 return nil
4549}
4650
47- // NewTextContent creates a [Content ] with text.
48- func NewTextContent (text string ) * Content {
49- return & Content {Type : "text" , Text : text }
51+ // NewTextContent creates a [ContentBlock ] with text.
52+ func NewTextContent (text string ) * ContentBlock {
53+ return & ContentBlock {Type : "text" , Text : text }
5054}
5155
52- // NewImageContent creates a [Content ] with image data.
53- func NewImageContent (data []byte , mimeType string ) * Content {
54- return & Content {Type : "image" , Data : data , MIMEType : mimeType }
56+ // NewImageContent creates a [ContentBlock ] with image data.
57+ func NewImageContent (data []byte , mimeType string ) * ContentBlock {
58+ return & ContentBlock {Type : "image" , Data : data , MIMEType : mimeType }
5559}
5660
57- // NewAudioContent creates a [Content ] with audio data.
58- func NewAudioContent (data []byte , mimeType string ) * Content {
59- return & Content {Type : "audio" , Data : data , MIMEType : mimeType }
61+ // NewAudioContent creates a [ContentBlock ] with audio data.
62+ func NewAudioContent (data []byte , mimeType string ) * ContentBlock {
63+ return & ContentBlock {Type : "audio" , Data : data , MIMEType : mimeType }
6064}
6165
62- // NewResourceContent creates a [Content] with an embedded resource.
63- func NewResourceContent (resource * ResourceContents ) * Content {
64- return & Content {Type : "resource" , Resource : resource }
66+ // NewResourceLink creates a [ContentBlock] with a [Resource].
67+ func NewResourceLink (r * Resource ) * ContentBlock {
68+ return & ContentBlock {Type : "resource_link" , ResourceLink : r }
69+ }
70+
71+ // NewResourceContents creates a [ContentBlock] with an embedded resource (a [ResourceContents]).
72+ func NewResourceContents (rc * ResourceContents ) * ContentBlock {
73+ return & ContentBlock {Type : "resource" , Resource : rc }
6574}
6675
6776// ResourceContents represents the union of the spec's {Text,Blob}ResourceContents types.
@@ -71,10 +80,11 @@ func NewResourceContent(resource *ResourceContents) *Content {
7180// A ResourceContents is either a TextResourceContents or a BlobResourceContents.
7281// Use [NewTextResourceContents] or [NextBlobResourceContents] to create one.
7382type ResourceContents struct {
74- URI string `json:"uri"` // resource location; must not be empty
75- MIMEType string `json:"mimeType,omitempty"`
76- Text string `json:"text"`
77- Blob []byte `json:"blob,omitempty"` // if nil, then text; else blob
83+ Meta map [string ]any `json:"_meta,omitempty"`
84+ URI string `json:"uri"` // resource location; must not be empty
85+ MIMEType string `json:"mimeType,omitempty"`
86+ Text string `json:"text"`
87+ Blob []byte `json:"blob,omitempty"` // if nil, then text; else blob
7888}
7989
8090func (r ResourceContents ) MarshalJSON () ([]byte , error ) {
0 commit comments