Skip to content

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.

new Message(data): Message;

Defined in: src/types/messages.ts:102

ParameterType
data{ role: Role; content: ContentBlock[]; trackingId?: string; metadata?: MessageMetadata; }
data.roleRole
data.contentContentBlock[]
data.trackingId?string
data.metadata?MessageMetadata

Message

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.


readonly content: ContentBlock[];

Defined in: src/types/messages.ts:85

Array of content blocks that make up this message.


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?: MessageMetadata;

Defined in: src/types/messages.ts:100

Optional metadata, not sent to model providers.

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.

ParameterTypeDescription
dataMessageDataMessageData to convert

Message

Message instance

Error if a content block type, at any depth, is unknown


toJSON(): MessageData;

Defined in: src/types/messages.ts:138

Serializes the Message to a JSON-compatible MessageData object. Called automatically by JSON.stringify().

MessageData

JSONSerializable.toJSON


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.

ParameterTypeDescription
dataMessageDataMessageData to deserialize

Message

Message instance


clone(): Message;

Defined in: src/types/messages.ts:162

Creates a deep copy of this Message (round-trips through serialization).

Message


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.

ParameterTypeDescription
overrides{ content?: ContentBlock[]; metadata?: MessageMetadata; }The content or metadata of the copy
overrides.content?ContentBlock[]-
overrides.metadata?MessageMetadata-

Message

The copy