Message
Defined in: src/types/messages.ts:71
A message in a conversation between user and assistant. Each message has a role (user or assistant) and an array of content blocks.
Implements
Section titled “Implements”JSONSerializable<MessageData>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Message(data): Message;Defined in: src/types/messages.ts:102
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
data | { role: Role; content: ContentBlock[]; trackingId?: string; metadata?: MessageMetadata; } |
data.role | Role |
data.content | ContentBlock[] |
data.trackingId? | string |
data.metadata? | MessageMetadata |
Returns
Section titled “Returns”Message
Properties
Section titled “Properties”readonly type: "message";Defined in: src/types/messages.ts:75
Discriminator for message type.
readonly role: Role;Defined in: src/types/messages.ts:80
The role of the message sender.
content
Section titled “content”readonly content: ContentBlock[];Defined in: src/types/messages.ts:85
Array of content blocks that make up this message.
trackingId
Section titled “trackingId”readonly trackingId: string;Defined in: src/types/messages.ts:95
Durable, stable UUID for the message, assigned at construction. Every Message has one — a
caller-supplied id is preserved, otherwise a fresh UUID is minted (so callers do not normally
set it; a caller supplying its own should use a UUID v4, e.g. crypto.randomUUID()). Survives
session save/restore, and is stripped before model calls. Preserved when a message is copied or
restored, so ids are unique within a conversation, but the same message carries the same id
across sessions (copying another agent’s messages does not re-key them).
metadata?
Section titled “metadata?”metadata?: MessageMetadata;Defined in: src/types/messages.ts:100
Optional metadata, not sent to model providers.
Methods
Section titled “Methods”fromMessageData()
Section titled “fromMessageData()”static fromMessageData(data): Message;Defined in: src/types/messages.ts:120
Creates a Message instance from MessageData. Use Message.fromJSON for data Message.toJSON produced.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | MessageData | MessageData to convert |
Returns
Section titled “Returns”Message
Message instance
Throws
Section titled “Throws”Error if a content block type, at any depth, is unknown
toJSON()
Section titled “toJSON()”toJSON(): MessageData;Defined in: src/types/messages.ts:138
Serializes the Message to a JSON-compatible MessageData object. Called automatically by JSON.stringify().
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”JSONSerializable.toJSON
fromJSON()
Section titled “fromJSON()”static fromJSON(data): Message;Defined in: src/types/messages.ts:155
Creates a Message from data Message.toJSON produced, such as a saved session. Unlike Message.fromMessageData, it keeps a content block of a type this version does not recognize, also inside a tool result, as an UnknownBlock, so data a newer SDK version saved still loads.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | MessageData | MessageData to deserialize |
Returns
Section titled “Returns”Message
Message instance
clone()
Section titled “clone()”clone(): Message;Defined in: src/types/messages.ts:162
Creates a deep copy of this Message (round-trips through serialization).
Returns
Section titled “Returns”Message
with()
Section titled “with()”with(overrides): Message;Defined in: src/types/messages.ts:178
Experimental
A shallow copy of this message with the given content or metadata in place of its own. The copy keeps this
message’s role and tracking id, and shares its blocks. A field left out, or set to undefined, keeps this
message’s value.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
overrides | { content?: ContentBlock[]; metadata?: MessageMetadata; } | The content or metadata of the copy |
overrides.content? | ContentBlock[] | - |
overrides.metadata? | MessageMetadata | - |
Returns
Section titled “Returns”Message
The copy