Class: Message
Defined in: src/lib/classes/message.ts:157
An immutable, validated conversation message from a human participant or the model.
Remarks
Covers only user and assistant roles — system instructions, developer directives, and tool results are not represented here. Constructed from a RawMessage via rawMessageSchema. Temporal fields are normalised to Luxon DateTime instances at construction time. Both content and identity.representation are @nhtio/adk!Tokenizable so token cost can be estimated inline.
A message may carry content (text), attachments (media), or both. The cross-field rule on rawMessageSchema enforces that at least one is present. Downstream code that reaches for message.content must handle the attachments-only case where content is undefined.
Constructors
Constructor
new Message(raw: RawMessage): Message;Defined in: src/lib/classes/message.ts:226
Parameters
| Parameter | Type | Description |
|---|---|---|
raw | RawMessage | The raw message input validated against rawMessageSchema. |
Returns
Message
Throws
@nhtio/adk!E_INVALID_INITIAL_MESSAGE_VALUE when raw does not satisfy the schema — including the cross-field rule that at least one of content or attachments must be present and non-empty.
Properties
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
attachments | readonly | readonly Media[] | undefined | Media attachments carried by this message. Remarks Always defined as a frozen array — empty when the message has no attachments. Both user and assistant messages may carry attachments. Each entry carries its own trustTier and modalityHazard; the renderer wraps each in its own trust envelope independent of the message envelope. | src/lib/classes/message.ts:204 |
content | readonly | Tokenizable | undefined | undefined | The message content as a @nhtio/adk!Tokenizable for inline token estimation, or undefined for attachments-only messages. Remarks undefined when the message was constructed with only attachments. Render code that needs the text portion must guard for the missing case rather than blindly calling message.content.toString(). | src/lib/classes/message.ts:194 |
createdAt | readonly | DateTime | undefined | When this message was created. | src/lib/classes/message.ts:208 |
id | readonly | string | undefined | Stable unique identifier for this message. | src/lib/classes/message.ts:182 |
identity | readonly | Identity | undefined | The identity of the participant who authored this message. | src/lib/classes/message.ts:206 |
role | readonly | MessageRole | undefined | Whether this message is from the human participant or the model. | src/lib/classes/message.ts:184 |
updatedAt | readonly | DateTime | undefined | When this message was last modified. | src/lib/classes/message.ts:210 |
schema | static | ObjectSchema<RawMessage> | rawMessageSchema | Validator schema that accepts a RawMessage object. Remarks Reusable fragment for any schema that needs to validate or nest a message entry — for example, a collection schema that holds an array of messages. | src/lib/classes/message.ts:165 |
Methods
[ENCODE_METHOD]()
ENCODE_METHOD: unknown;Defined in: src/lib/classes/message.ts:304
Serialise this Message into an @nhtio/encoder snapshot.
Returns
unknown
A RawMessage-shaped snapshot.
Remarks
Emits a RawMessage-shaped object holding the live nested primitives — content (@nhtio/adk!Tokenizable), attachments (@nhtio/adk!Media[]), identity (@nhtio/adk!Identity), and Luxon temporal fields — which the encoder recurses into. A text-only message round-trips trivially; a message carrying @nhtio/adk!Media round-trips only if each attachment's reader is describable (a fromWebFile-backed attachment throws @nhtio/adk!E_READER_NOT_DESCRIBABLE at encode). The frozen attachments array is copied to a plain array for the snapshot. Round-trips via Message.[DECODE_METHOD].
[DECODE_METHOD]()
static DECODE_METHOD: Message;Defined in: src/lib/classes/message.ts:324
Reconstruct a Message from a Message.[ENCODE_METHOD] snapshot.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | unknown | The snapshot produced by Message.[ENCODE_METHOD]. |
Returns
Message
A fully-validated Message.
isMessage()
static isMessage(value: unknown): value is Message;Defined in: src/lib/classes/message.ts:177
Returns true if value is a Message instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to test. |
Returns
value is Message
true when value is a Message instance.
Remarks
Uses @nhtio/adk!isInstanceOf for cross-realm safety — instanceof would fail for instances created in a different module copy or VM context.