summaryrefslogtreecommitdiff
path: root/src/domain/message.c
blob: b1db3e1ab7f697c8e9cec2a0eec5ca78aa69a54f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "domain/message.h"

#include <stdlib.h>
#include <string.h>

#include "base/allocator.h"

static char *
duplicate_string(const char *value)
{
  const size_t length = strlen(value);
  char *copy = malloc(length + 1);
  if (copy != NULL)
    {
      memcpy(copy, value, length + 1);
    }
  return copy;
}

static int
copy_failed(tiny_message_t *message)
{
  tiny_message_free(message);
  return -1;
}

void
tiny_message_init(tiny_message_t *message)
{
  if (message == NULL)
    {
      return;
    }
  memset(message, 0, sizeof(*message));
  message->metadata.memory_tier = TINY_TIER_SHORT;
}

void
tiny_message_free(tiny_message_t *message)
{
  size_t index;
  if (message == NULL)
    {
      return;
    }
  free(message->session_id);
  free(message->content);
  free(message->metadata.tool_call_id);
  for (index = 0; index < message->metadata.tag_count; ++index)
    {
      free(message->metadata.tags[index]);
    }
  free(message->metadata.tags);
  tiny_message_init(message);
}

int
tiny_message_copy(tiny_message_t *destination, const tiny_message_t *source)
{
  size_t index;
  if (destination == NULL || source == NULL || destination == source || source->session_id == NULL
      || source->content == NULL
      || (source->metadata.tag_count != 0 && source->metadata.tags == NULL))
    {
      return -1;
    }
  tiny_message_init(destination);
  memcpy(destination->message_id, source->message_id, sizeof(destination->message_id));
  destination->timestamp_ms = source->timestamp_ms;
  destination->role = source->role;
  destination->metadata.memory_tier = source->metadata.memory_tier;
  destination->session_id = duplicate_string(source->session_id);
  destination->content = duplicate_string(source->content);
  if (destination->session_id == NULL || destination->content == NULL)
    {
      return copy_failed(destination);
    }
  if (source->metadata.tool_call_id != NULL)
    {
      destination->metadata.tool_call_id = duplicate_string(source->metadata.tool_call_id);
      if (destination->metadata.tool_call_id == NULL)
        {
          return copy_failed(destination);
        }
    }
  if (source->metadata.tag_count > 0)
    {
      destination->metadata.tags = calloc(source->metadata.tag_count, sizeof(char *));
      if (destination->metadata.tags == NULL)
        {
          return copy_failed(destination);
        }
      for (index = 0; index < source->metadata.tag_count; ++index)
        {
          destination->metadata.tags[index] = duplicate_string(source->metadata.tags[index]);
          if (destination->metadata.tags[index] == NULL)
            {
              return copy_failed(destination);
            }
          destination->metadata.tag_count++;
        }
    }
  return 0;
}

const char *
tiny_role_name(tiny_role_t role)
{
  switch (role)
    {
    case TINY_ROLE_USER:
      return "user";
    case TINY_ROLE_ASSISTANT:
      return "assistant";
    case TINY_ROLE_SYSTEM:
      return "system";
    default:
      return "unknown";
    }
}

bool
tiny_role_parse(const char *value, tiny_role_t *role)
{
  if (value == NULL || role == NULL)
    {
      return false;
    }
  if (strcmp(value, "user") == 0)
    {
      *role = TINY_ROLE_USER;
    }
  else if (strcmp(value, "assistant") == 0)
    {
      *role = TINY_ROLE_ASSISTANT;
    }
  else if (strcmp(value, "system") == 0)
    {
      *role = TINY_ROLE_SYSTEM;
    }
  else
    {
      return false;
    }
  return true;
}

const char *
tiny_tier_name(tiny_memory_tier_t tier)
{
  switch (tier)
    {
    case TINY_TIER_SHORT:
      return "short";
    case TINY_TIER_MID:
      return "mid";
    case TINY_TIER_LONG:
      return "long";
    case TINY_TIER_DISCARD:
      return "discard";
    default:
      return "unknown";
    }
}

bool
tiny_tier_parse(const char *value, tiny_memory_tier_t *tier)
{
  if (value == NULL || tier == NULL)
    {
      return false;
    }
  if (strcmp(value, "short") == 0)
    {
      *tier = TINY_TIER_SHORT;
    }
  else if (strcmp(value, "mid") == 0)
    {
      *tier = TINY_TIER_MID;
    }
  else if (strcmp(value, "long") == 0)
    {
      *tier = TINY_TIER_LONG;
    }
  else if (strcmp(value, "discard") == 0)
    {
      *tier = TINY_TIER_DISCARD;
    }
  else
    {
      return false;
    }
  return true;
}

bool
tiny_utf8_valid(const char *value, size_t length)
{
  size_t index = 0;
  if (value == NULL)
    {
      return length == 0;
    }
  while (index < length)
    {
      const unsigned char first = (unsigned char)value[index++];
      unsigned needed;
      uint32_t codepoint;
      if (first < 0x80)
        {
          if (first == 0)
            {
              return false;
            }
          continue;
        }
      if ((first & 0xe0) == 0xc0)
        {
          needed = 1;
          codepoint = first & 0x1f;
        }
      else if ((first & 0xf0) == 0xe0)
        {
          needed = 2;
          codepoint = first & 0x0f;
        }
      else if ((first & 0xf8) == 0xf0)
        {
          needed = 3;
          codepoint = first & 0x07;
        }
      else
        {
          return false;
        }
      if (index + needed > length)
        {
          return false;
        }
      while (needed-- > 0)
        {
          const unsigned char next = (unsigned char)value[index++];
          if ((next & 0xc0) != 0x80)
            {
              return false;
            }
          codepoint = (codepoint << 6) | (next & 0x3f);
        }
      if (codepoint > 0x10ffff || (codepoint >= 0xd800 && codepoint <= 0xdfff))
        {
          return false;
        }
      if ((first & 0xe0) == 0xc0 && codepoint < 0x80)
        {
          return false;
        }
      if ((first & 0xf0) == 0xe0 && codepoint < 0x800)
        {
          return false;
        }
      if ((first & 0xf8) == 0xf0 && codepoint < 0x10000)
        {
          return false;
        }
    }
  return true;
}

bool
tiny_message_id_valid(const char *value)
{
  static const char alphabet[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
  size_t index;
  if (value == NULL || strlen(value) != TINY_MESSAGE_ID_LENGTH || value[0] > '7')
    {
      return false;
    }
  for (index = 0; index < TINY_MESSAGE_ID_LENGTH; ++index)
    {
      if (strchr(alphabet, value[index]) == NULL)
        {
          return false;
        }
    }
  return true;
}