7814
Ta
tiny-c-agent / files
tiny-c-agent / src / message.h
2.0 KB
1// tiny messages: the provider-neutral transcript format. 2// 3// Provider adapters translate these to and from each wire protocol. The agent 4// loop and the session store share this shape. 5 6#ifndef TINY_MESSAGE_H 7#define TINY_MESSAGE_H 8 9#include <stdbool.h> 10#include <stddef.h> 11 12#include <yyjson.h> 13 14typedef enum { 15 TINY_ROLE_USER, 16 TINY_ROLE_ASSISTANT, 17 TINY_ROLE_TOOL, // a tool result 18} tiny_role; 19 20typedef enum { 21 TINY_BLOCK_TEXT, 22 TINY_BLOCK_THINKING, 23 TINY_BLOCK_TOOL_CALL, 24 TINY_BLOCK_IMAGE, 25} tiny_block_type; 26 27typedef struct { 28 tiny_block_type type; 29 char *text; // TEXT, THINKING 30 char *tool_call_id; // TOOL_CALL 31 char *tool_name; // TOOL_CALL 32 char *arguments; // TOOL_CALL, a JSON object string 33 char *media_type; // IMAGE, e.g. "image/png" 34 char *data; // IMAGE, base64 35} tiny_block; 36 37typedef struct { 38 tiny_role role; 39 tiny_block *blocks; 40 size_t blocks_len; 41 char *tool_call_id; // TOOL: the call this result answers 42 bool is_error; // TOOL 43 char *stop_reason; // ASSISTANT: stop, length, toolUse, error, aborted 44 char *error_message; // ASSISTANT, when stop_reason is error 45 long tokens_in; // ASSISTANT usage 46 long tokens_out; 47 long tokens_total; 48} tiny_message; 49 50tiny_message tiny_message_user(const char *text); 51tiny_message tiny_message_tool(const char *tool_call_id, const char *text, bool is_error); 52 53// Appends a zeroed block and returns it for the caller to fill. 54tiny_block *tiny_message_add_block(tiny_message *m, tiny_block_type type); 55 56// True when the message carries at least one image block. 57bool tiny_message_has_image(const tiny_message *m); 58 59// Concatenates the message's text blocks (TEXT only) into one malloc'd string. 60char *tiny_message_text(const tiny_message *m); 61 62void tiny_message_free(tiny_message *m); 63 64// Serializes the message to its provider-neutral JSON object. 65yyjson_mut_val *tiny_message_to_val(yyjson_mut_doc *doc, const tiny_message *m); 66char *tiny_message_to_json(const tiny_message *m); 67 68#endif