7814
Ta
tiny-c-agent / files
tiny-c-agent / src / message.c
4.6 KB
1#include "message.h" 2 3#include <stdlib.h> 4#include <string.h> 5 6#include "util.h" 7 8tiny_message tiny_message_user(const char *text) { 9 tiny_message m = {0}; 10 m.role = TINY_ROLE_USER; 11 tiny_block *b = tiny_message_add_block(&m, TINY_BLOCK_TEXT); 12 b->text = xstrdup(text ? text : ""); 13 return m; 14} 15 16tiny_message tiny_message_tool(const char *tool_call_id, const char *text, bool is_error) { 17 tiny_message m = {0}; 18 m.role = TINY_ROLE_TOOL; 19 m.is_error = is_error; 20 m.tool_call_id = xstrdup(tool_call_id ? tool_call_id : ""); 21 tiny_block *b = tiny_message_add_block(&m, TINY_BLOCK_TEXT); 22 b->text = xstrdup(text ? text : ""); 23 return m; 24} 25 26tiny_block *tiny_message_add_block(tiny_message *m, tiny_block_type type) { 27 m->blocks = realloc(m->blocks, (m->blocks_len + 1) * sizeof *m->blocks); 28 if (!m->blocks) abort(); 29 tiny_block *b = &m->blocks[m->blocks_len++]; 30 memset(b, 0, sizeof *b); 31 b->type = type; 32 return b; 33} 34 35bool tiny_message_has_image(const tiny_message *m) { 36 for (size_t i = 0; i < m->blocks_len; i++) 37 if (m->blocks[i].type == TINY_BLOCK_IMAGE) return true; 38 return false; 39} 40 41char *tiny_message_text(const tiny_message *m) { 42 tiny_buf b; 43 buf_init(&b); 44 for (size_t i = 0; i < m->blocks_len; i++) 45 if (m->blocks[i].type == TINY_BLOCK_TEXT && m->blocks[i].text) 46 buf_adds(&b, m->blocks[i].text); 47 return buf_steal(&b); 48} 49 50void tiny_message_free(tiny_message *m) { 51 if (!m) return; 52 for (size_t i = 0; i < m->blocks_len; i++) { 53 free(m->blocks[i].text); 54 free(m->blocks[i].tool_call_id); 55 free(m->blocks[i].tool_name); 56 free(m->blocks[i].arguments); 57 free(m->blocks[i].media_type); 58 free(m->blocks[i].data); 59 } 60 free(m->blocks); 61 free(m->tool_call_id); 62 free(m->stop_reason); 63 free(m->error_message); 64 memset(m, 0, sizeof *m); 65} 66 67yyjson_mut_val *tiny_message_to_val(yyjson_mut_doc *doc, const tiny_message *m) { 68 yyjson_mut_val *o = yyjson_mut_obj(doc); 69 const char *role = m->role == TINY_ROLE_USER ? "user" 70 : m->role == TINY_ROLE_ASSISTANT ? "assistant" 71 : "tool"; 72 yyjson_mut_obj_add_strcpy(doc, o, "role", role); 73 74 yyjson_mut_val *content = yyjson_mut_arr(doc); 75 yyjson_mut_obj_add_val(doc, o, "content", content); 76 for (size_t i = 0; i < m->blocks_len; i++) { 77 const tiny_block *b = &m->blocks[i]; 78 yyjson_mut_val *bo = yyjson_mut_obj(doc); 79 yyjson_mut_arr_add_val(content, bo); 80 switch (b->type) { 81 case TINY_BLOCK_TEXT: 82 yyjson_mut_obj_add_strcpy(doc, bo, "type", "text"); 83 yyjson_mut_obj_add_strcpy(doc, bo, "text", b->text ? b->text : ""); 84 break; 85 case TINY_BLOCK_THINKING: 86 yyjson_mut_obj_add_strcpy(doc, bo, "type", "thinking"); 87 yyjson_mut_obj_add_strcpy(doc, bo, "thinking", b->text ? b->text : ""); 88 break; 89 case TINY_BLOCK_TOOL_CALL: 90 yyjson_mut_obj_add_strcpy(doc, bo, "type", "toolCall"); 91 yyjson_mut_obj_add_strcpy(doc, bo, "id", b->tool_call_id ? b->tool_call_id : ""); 92 yyjson_mut_obj_add_strcpy(doc, bo, "name", b->tool_name ? b->tool_name : ""); 93 yyjson_mut_obj_add_strcpy(doc, bo, "arguments", b->arguments ? b->arguments : "{}"); 94 break; 95 case TINY_BLOCK_IMAGE: 96 yyjson_mut_obj_add_strcpy(doc, bo, "type", "image"); 97 yyjson_mut_obj_add_strcpy(doc, bo, "data", b->data ? b->data : ""); 98 yyjson_mut_obj_add_strcpy(doc, bo, "mimeType", b->media_type ? b->media_type : ""); 99 break; 100 } 101 } 102 103 if (m->role == TINY_ROLE_TOOL) { 104 yyjson_mut_obj_add_strcpy(doc, o, "toolCallId", m->tool_call_id ? m->tool_call_id : ""); 105 yyjson_mut_obj_add_bool(doc, o, "isError", m->is_error); 106 } else if (m->role == TINY_ROLE_ASSISTANT) { 107 if (m->stop_reason) yyjson_mut_obj_add_strcpy(doc, o, "stopReason", m->stop_reason); 108 if (m->error_message) yyjson_mut_obj_add_strcpy(doc, o, "errorMessage", m->error_message); 109 yyjson_mut_val *usage = yyjson_mut_obj(doc); 110 yyjson_mut_obj_add_val(doc, o, "usage", usage); 111 yyjson_mut_obj_add_int(doc, usage, "input", m->tokens_in); 112 yyjson_mut_obj_add_int(doc, usage, "output", m->tokens_out); 113 yyjson_mut_obj_add_int(doc, usage, "total", m->tokens_total); 114 } 115 return o; 116} 117 118char *tiny_message_to_json(const tiny_message *m) { 119 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); 120 yyjson_mut_doc_set_root(doc, tiny_message_to_val(doc, m)); 121 size_t len = 0; 122 char *s = yyjson_mut_write(doc, 0, &len); 123 yyjson_mut_doc_free(doc); 124 return s; 125}