Message represents a single role's contribution to a generation. Each message can contain multiple parts (for example text and an image), and each generation can contain multiple messages.

Type Parameters

  • T = unknown

Implements

Constructors

  • Type Parameters

    • T = unknown

    Parameters

    • message: {
          content: (
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text: string;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media: { contentType?: string; url: string };
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest: { input?: unknown; name: string; ref?: string };
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse: { name: string; output?: unknown; ref?: string };
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom: Record<string, any>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
          )[];
          metadata?: Record<string, unknown>;
          role: "user" | "model" | "tool" | "system";
      }
    • Optionaloptions: { parser?: MessageParser<T> }

    Returns Message<T>

Properties

content: (
    | {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text: string;
        toolRequest?: undefined;
        toolResponse?: undefined;
    }
    | {
        custom?: Record<string, unknown>;
        data?: unknown;
        media: { contentType?: string; url: string };
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest?: undefined;
        toolResponse?: undefined;
    }
    | {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }
    | {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest?: undefined;
        toolResponse: { name: string; output?: unknown; ref?: string };
    }
    | {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest?: undefined;
        toolResponse?: undefined;
    }
    | {
        custom: Record<string, any>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest?: undefined;
        toolResponse?: undefined;
    }
)[]
metadata?: Record<string, any>
parser?: MessageParser<T>
role: "user" | "model" | "tool" | "system"

Accessors

  • get data(): null | T
  • Returns the first detected data part of a message.

    Returns null | T

    The first data part detected in the message (if any).

  • get interrupts(): {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]
  • Returns all tool requests annotated with interrupt metadata.

    Returns {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]

    Array of all interrupt tool requests.

  • get media(): null | { contentType?: string; url: string }
  • Returns the first media part detected in the message. Useful for extracting (for example) an image from a generation expected to create one.

    Returns null | { contentType?: string; url: string }

    The first detected media part in the message.

  • get output(): T
  • Attempts to parse the content of the message according to the supplied output parser. Without a parser, returns data contained in the message or tries to parse JSON from the text of the message.

    Returns T

    The structured output contained in the message.

  • get text(): string
  • Concatenates all text parts present in the message with no delimiter.

    Returns string

    A string of all concatenated text parts.

  • get toolRequests(): {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]
  • Returns all tool request found in this message.

    Returns {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]

    Array of all tool request found in this message.

Methods

  • Converts the Message to a plain JS object.

    Returns {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }

    Plain JS object representing the data contained in the message.

  • Returns {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest?: undefined;
        toolResponse: { name: string; output?: unknown; ref?: string };
    }[]

  • Parameters

    • lenientMessage:
          | string
          | {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          }
          | {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          } & { content: string }

    Returns Message<unknown>

  • Parameters

    • lenientPart:
          | string
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text: string;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media: { contentType?: string; url: string };
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest: { input?: unknown; name: string; ref?: string };
              toolResponse?: undefined;
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse: { name: string; output?: unknown; ref?: string };
          }
          | {
              custom?: Record<string, unknown>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | {
              custom: Record<string, any>;
              data?: unknown;
              media?: undefined;
              metadata?: Record<string, unknown>;
              text?: undefined;
              toolRequest?: undefined;
              toolResponse?: undefined;
          }
          | (
              | string
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text: string;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media: { contentType?: string; url: string };
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest: { input?: unknown; name: string; ref?: string };
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse: { name: string; output?: unknown; ref?: string };
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom: Record<string, any>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
          )[]

    Returns (
        | {
            custom?: Record<string, unknown>;
            data?: unknown;
            media?: undefined;
            metadata?: Record<string, unknown>;
            text: string;
            toolRequest?: undefined;
            toolResponse?: undefined;
        }
        | {
            custom?: Record<string, unknown>;
            data?: unknown;
            media: { contentType?: string; url: string };
            metadata?: Record<string, unknown>;
            text?: undefined;
            toolRequest?: undefined;
            toolResponse?: undefined;
        }
        | {
            custom?: Record<string, unknown>;
            data?: unknown;
            media?: undefined;
            metadata?: Record<string, unknown>;
            text?: undefined;
            toolRequest: { input?: unknown; name: string; ref?: string };
            toolResponse?: undefined;
        }
        | {
            custom?: Record<string, unknown>;
            data?: unknown;
            media?: undefined;
            metadata?: Record<string, unknown>;
            text?: undefined;
            toolRequest?: undefined;
            toolResponse: { name: string; output?: unknown; ref?: string };
        }
        | {
            custom?: Record<string, unknown>;
            data?: unknown;
            media?: undefined;
            metadata?: Record<string, unknown>;
            text?: undefined;
            toolRequest?: undefined;
            toolResponse?: undefined;
        }
        | {
            custom: Record<string, any>;
            data?: unknown;
            media?: undefined;
            metadata?: Record<string, unknown>;
            text?: undefined;
            toolRequest?: undefined;
            toolResponse?: undefined;
        }
    )[]

  • Parameters

    • lenientMessage:
          | string
          | {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          }
          | {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          } & {
              content: | string
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text: string;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media: { contentType?: string; url: string };
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest: { input?: unknown; name: string; ref?: string };
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse: { name: string; output?: unknown; ref?: string };
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom: Record<string, any>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              role: string;
          }
    • OptionaldefaultRole: "user" | "model" | "tool" | "system"

    Returns {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }