Message

Represents a single message in a chat interaction.

Supports text, base64-encoded images (multimodal), and tool call results. Backward compatible: Message("user", "hello") still compiles.

Members

Functions

toJson
JSONValue toJson()

Converts the message to a JSON object for the Ollama API.

Variables

content
string content;

Text content of the message.

images
string[] images;

Optional base64-encoded images for multimodal input.

role
string role;

Sender role: "user", "assistant", or "system".

tool_calls
ToolCall[] tool_calls;

Optional tool calls made by the assistant.

Examples

// Basic message — no optional fields
auto m = Message("user", "Hello, world!");
auto j = m.toJson();
assert(j["role"].str == "user");
assert(j["content"].str == "Hello, world!");
assert("images"     !in j);
assert("tool_calls" !in j);

// Message with images
auto m2 = Message("user", "What is in this image?", ["aGVsbG8="]);
auto j2 = m2.toJson();
assert(j2["images"].arrayNoRef.length == 1);
assert(j2["images"][0].str == "aGVsbG8=");

// Message with tool_calls
auto tc = ToolCall("id-1", "search", parseJSON(`{"query":"D language"}`));
auto m3 = Message("assistant", "", null, [tc]);
auto j3 = m3.toJson();
assert(j3["tool_calls"].arrayNoRef.length == 1);
assert(j3["tool_calls"][0]["function"]["name"].str == "search");

// Backward compatibility: two-field initialization still compiles
Message m4;
m4.role    = "system";
m4.content = "You are a helpful assistant.";
auto j4 = m4.toJson();
assert(j4["role"].str == "system");

Meta