110 lines
3.7 KiB
C
Raw Normal View History

2025-01-04 08:40:31 +01:00
// Written by retoor@molodetz.nl
2025-03-28 06:56:36 +01:00
// This code manages a collection of messages using JSON objects. It provides
// functions to retrieve all messages as a JSON array, add a new message with a
// specified role and content, and free the allocated resources.
2025-01-04 08:40:31 +01:00
2025-01-27 19:06:59 +01:00
// Uses the external library <json-c/json.h> for JSON manipulation
2025-01-04 08:40:31 +01:00
// MIT License
2025-03-28 06:56:36 +01:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
// THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2025-01-04 08:40:31 +01:00
2025-01-05 22:59:51 +01:00
#ifndef R_MESSAGES_H
#define R_MESSAGES_H
2025-01-27 19:06:59 +01:00
2025-01-04 06:00:03 +01:00
#include "json-c/json.h"
2025-03-28 06:56:36 +01:00
#include "tools.h"
#include <string.h>
2025-03-30 00:13:42 +01:00
#include "db_utils.h"
2025-01-27 19:06:59 +01:00
struct json_object *message_array = NULL;
2025-01-04 06:00:03 +01:00
2025-01-04 08:40:31 +01:00
struct json_object *message_list() {
2025-03-28 06:56:36 +01:00
if (!message_array) {
message_array = json_object_new_array();
}
return message_array;
2025-01-04 08:40:31 +01:00
}
2025-03-19 18:04:32 +01:00
bool messages_remove_last() {
2025-03-28 06:56:36 +01:00
struct json_object *messages = message_list();
int size = json_object_array_length(messages);
if (size) {
json_object_array_del_idx(messages, size - 1, 1);
return true;
}
return false;
2025-03-19 18:04:32 +01:00
}
2025-03-28 06:56:36 +01:00
void messages_remove() {
while (messages_remove_last())
continue;
2025-03-05 23:53:35 +01:00
}
2025-03-03 08:07:17 +01:00
struct json_object *message_add_tool_call(struct json_object *message) {
2025-03-28 06:56:36 +01:00
struct json_object *messages = message_list();
json_object_array_add(messages, message);
return message;
2025-03-03 08:07:17 +01:00
}
2025-03-03 17:06:05 +01:00
2025-03-28 06:56:36 +01:00
struct json_object *message_add_tool_result(const char *tool_call_id,
const char *tool_result) {
struct json_object *messages = message_list();
struct json_object *message = json_object_new_object();
2025-03-03 08:07:17 +01:00
2025-03-28 06:56:36 +01:00
json_object_object_add(message, "tool_call_id",
json_object_new_string(tool_call_id));
json_object_object_add(message, "tool_result",
json_object_new_string(tool_result));
2025-03-03 08:07:17 +01:00
2025-03-28 06:56:36 +01:00
json_object_array_add(messages, message);
return message;
2025-03-03 08:07:17 +01:00
}
2025-03-20 16:38:10 +01:00
void message_add_object(json_object *message) {
2025-03-28 06:56:36 +01:00
struct json_object *messages = message_list();
json_object_array_add(messages, message);
2025-03-20 16:38:10 +01:00
}
2025-03-30 00:13:42 +01:00
struct json_object *message_add(const char *role, const char *content);
2025-01-27 19:06:59 +01:00
struct json_object *message_add(const char *role, const char *content) {
2025-03-28 06:56:36 +01:00
struct json_object *messages = message_list();
struct json_object *message = json_object_new_object();
json_object_object_add(message, "role", json_object_new_string(role));
json_object_object_add(message, "content", json_object_new_string(content));
2025-03-03 08:07:17 +01:00
2025-03-28 06:56:36 +01:00
if (!strcmp(role, "user")) {
json_object_object_add(message, "tools", tools_descriptions());
}
2025-03-03 08:07:17 +01:00
2025-03-28 06:56:36 +01:00
json_object_array_add(messages, message);
return message;
2025-01-04 06:00:03 +01:00
}
2025-01-04 08:40:31 +01:00
char *message_json() {
2025-03-28 06:56:36 +01:00
return (char *)json_object_to_json_string_ext(message_list(),
JSON_C_TO_STRING_PRETTY);
2025-01-04 06:00:03 +01:00
}
2025-01-04 08:40:31 +01:00
void message_free() {
2025-03-28 06:56:36 +01:00
if (message_array) {
json_object_put(message_array);
message_array = NULL;
}
2025-01-04 06:00:03 +01:00
}
2025-01-27 19:06:59 +01:00
2025-03-19 18:04:32 +01:00
#endif