chore: remove entire agent, auth, browse, chat, db_utils, and http_curl source trees
This commit deletes the complete implementation of the agent loop, authentication module, web browsing utilities, chat prompt builder, database helper, and HTTP client. All corresponding header files and test harnesses are also removed. The diff shows the deletion of 6 source files and 6 header files totaling over 1000 lines of code, including the agent state machine, API key resolution logic, curl-based HTTP functions, SQLite database initialization and CRUD operations, and the JSON chat prompt construction.
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
#include "config.h"
|
||||
#include "util/path.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct config_t {
|
||||
char *api_url;
|
||||
char *models_url;
|
||||
char *model;
|
||||
char *api_key;
|
||||
char *db_path;
|
||||
char *session_id;
|
||||
char *system_message;
|
||||
double temperature;
|
||||
bool use_tools;
|
||||
bool use_strict;
|
||||
bool verbose;
|
||||
};
|
||||
|
||||
static struct config_t *instance = NULL;
|
||||
|
||||
static char *strdup_safe(const char *s) {
|
||||
return s ? strdup(s) : NULL;
|
||||
}
|
||||
|
||||
static bool resolve_env_bool(const char *env_name, bool default_val) {
|
||||
const char *val = getenv(env_name);
|
||||
if (!val) return default_val;
|
||||
if (!strcmp(val, "true") || !strcmp(val, "1")) return true;
|
||||
if (!strcmp(val, "false") || !strcmp(val, "0")) return false;
|
||||
return default_val;
|
||||
}
|
||||
|
||||
static const char *resolve_api_key(void) {
|
||||
const char *key = getenv("OPENROUTER_API_KEY");
|
||||
if (key && *key) return key;
|
||||
|
||||
key = getenv("R_KEY");
|
||||
if (key && *key) return key;
|
||||
|
||||
key = getenv("OPENAI_API_KEY");
|
||||
if (key && *key) return key;
|
||||
|
||||
return "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";
|
||||
}
|
||||
|
||||
static bool is_valid_session_id(const char *session_id) {
|
||||
if (!session_id || !*session_id) return false;
|
||||
if (strlen(session_id) > 255) return false;
|
||||
|
||||
for (const char *p = session_id; *p; p++) {
|
||||
if (!isalnum((unsigned char)*p) && *p != '-' && *p != '_' && *p != '.') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
config_handle config_create(void) {
|
||||
if (instance) return instance;
|
||||
|
||||
struct config_t *cfg = calloc(1, sizeof(struct config_t));
|
||||
if (!cfg) return NULL;
|
||||
|
||||
const char *base_url = getenv("R_BASE_URL");
|
||||
if (base_url && *base_url) {
|
||||
size_t len = strlen(base_url);
|
||||
cfg->api_url = malloc(len + 32);
|
||||
cfg->models_url = malloc(len + 32);
|
||||
if (cfg->api_url && cfg->models_url) {
|
||||
snprintf(cfg->api_url, len + 32, "%s/v1/chat/completions", base_url);
|
||||
snprintf(cfg->models_url, len + 32, "%s/v1/models", base_url);
|
||||
}
|
||||
} else {
|
||||
cfg->api_url = strdup("https://api.openai.com/v1/chat/completions");
|
||||
cfg->models_url = strdup("https://api.openai.com/v1/models");
|
||||
}
|
||||
|
||||
const char *model = getenv("R_MODEL");
|
||||
cfg->model = strdup(model && *model ? model : "gpt-4o-mini");
|
||||
|
||||
cfg->api_key = strdup(resolve_api_key());
|
||||
cfg->db_path = strdup("~/.r.db");
|
||||
cfg->temperature = 0.1;
|
||||
cfg->use_tools = resolve_env_bool("R_USE_TOOLS", true);
|
||||
cfg->use_strict = resolve_env_bool("R_USE_STRICT", true);
|
||||
cfg->verbose = false;
|
||||
|
||||
const char *session = getenv("R_SESSION");
|
||||
if (session && is_valid_session_id(session)) {
|
||||
cfg->session_id = strdup(session);
|
||||
} else {
|
||||
cfg->session_id = strdup("default");
|
||||
}
|
||||
|
||||
const char *system_msg = getenv("R_SYSTEM_MESSAGE");
|
||||
cfg->system_message = strdup_safe(system_msg);
|
||||
|
||||
instance = cfg;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
void config_destroy(config_handle cfg) {
|
||||
if (!cfg) return;
|
||||
|
||||
if (cfg != instance) {
|
||||
if (cfg->api_url) free(cfg->api_url);
|
||||
if (cfg->models_url) free(cfg->models_url);
|
||||
if (cfg->model) free(cfg->model);
|
||||
if (cfg->api_key) free(cfg->api_key);
|
||||
if (cfg->db_path) free(cfg->db_path);
|
||||
if (cfg->session_id) free(cfg->session_id);
|
||||
if (cfg->system_message) free(cfg->system_message);
|
||||
free(cfg);
|
||||
}
|
||||
}
|
||||
|
||||
const char *config_get_api_url(config_handle cfg) {
|
||||
return cfg ? cfg->api_url : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_models_url(config_handle cfg) {
|
||||
return cfg ? cfg->models_url : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_api_key(config_handle cfg) {
|
||||
return cfg ? cfg->api_key : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_model(config_handle cfg) {
|
||||
return cfg ? cfg->model : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_db_path(config_handle cfg) {
|
||||
return cfg ? cfg->db_path : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_session_id(config_handle cfg) {
|
||||
return cfg ? cfg->session_id : NULL;
|
||||
}
|
||||
|
||||
const char *config_get_system_message(config_handle cfg) {
|
||||
return cfg ? cfg->system_message : NULL;
|
||||
}
|
||||
|
||||
double config_get_temperature(config_handle cfg) {
|
||||
return cfg ? cfg->temperature : 0.0;
|
||||
}
|
||||
|
||||
bool config_use_tools(config_handle cfg) {
|
||||
return cfg ? cfg->use_tools : true;
|
||||
}
|
||||
|
||||
bool config_use_strict(config_handle cfg) {
|
||||
return cfg ? cfg->use_strict : true;
|
||||
}
|
||||
|
||||
bool config_is_verbose(config_handle cfg) {
|
||||
return cfg ? cfg->verbose : false;
|
||||
}
|
||||
|
||||
r_status_t config_set_model(config_handle cfg, const char *model) {
|
||||
if (!cfg || !model) return R_ERROR_INVALID_ARG;
|
||||
|
||||
if (cfg->model) free(cfg->model);
|
||||
cfg->model = strdup(model);
|
||||
|
||||
return R_SUCCESS;
|
||||
}
|
||||
|
||||
r_status_t config_set_session_id(config_handle cfg, const char *session_id) {
|
||||
if (!cfg || !session_id) return R_ERROR_INVALID_ARG;
|
||||
|
||||
if (!is_valid_session_id(session_id)) {
|
||||
return R_ERROR_SESSION_INVALID;
|
||||
}
|
||||
|
||||
if (cfg->session_id) free(cfg->session_id);
|
||||
cfg->session_id = strdup(session_id);
|
||||
|
||||
return R_SUCCESS;
|
||||
}
|
||||
|
||||
void config_set_verbose(config_handle cfg, bool verbose) {
|
||||
if (cfg) cfg->verbose = verbose;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
#ifndef R_INTERFACES_CONFIG_H
|
||||
#define R_INTERFACES_CONFIG_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct config_t *config_handle;
|
||||
|
||||
config_handle config_create(void);
|
||||
void config_destroy(config_handle cfg);
|
||||
|
||||
const char *config_get_api_url(config_handle cfg);
|
||||
const char *config_get_models_url(config_handle cfg);
|
||||
const char *config_get_api_key(config_handle cfg);
|
||||
const char *config_get_model(config_handle cfg);
|
||||
const char *config_get_db_path(config_handle cfg);
|
||||
const char *config_get_session_id(config_handle cfg);
|
||||
const char *config_get_system_message(config_handle cfg);
|
||||
|
||||
double config_get_temperature(config_handle cfg);
|
||||
bool config_use_tools(config_handle cfg);
|
||||
bool config_use_strict(config_handle cfg);
|
||||
bool config_is_verbose(config_handle cfg);
|
||||
|
||||
r_status_t config_set_model(config_handle cfg, const char *model);
|
||||
r_status_t config_set_session_id(config_handle cfg, const char *session_id);
|
||||
void config_set_verbose(config_handle cfg, bool verbose);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
#ifndef R_INTERFACES_DATABASE_H
|
||||
#define R_INTERFACES_DATABASE_H
|
||||
|
||||
#include "r_error.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct database_t *database_handle;
|
||||
|
||||
database_handle database_open(const char *path);
|
||||
void database_close(database_handle db);
|
||||
|
||||
r_status_t database_init(database_handle db);
|
||||
r_status_t database_execute(database_handle db, const char *sql, char **error);
|
||||
|
||||
typedef void (*database_callback_t)(void *ctx, int col_count, char **values, char **names);
|
||||
|
||||
r_status_t database_query(database_handle db, const char *sql,
|
||||
database_callback_t callback, void *ctx);
|
||||
|
||||
r_status_t database_set(database_handle db, const char *key, const char *value);
|
||||
r_status_t database_get(database_handle db, const char *key, char **value);
|
||||
r_status_t database_delete(database_handle db, const char *key);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
#ifndef R_INTERFACES_HTTP_H
|
||||
#define R_INTERFACES_HTTP_H
|
||||
|
||||
#include "r_error.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct http_client_t *http_client_handle;
|
||||
|
||||
typedef enum {
|
||||
HTTP_METHOD_GET,
|
||||
HTTP_METHOD_POST,
|
||||
HTTP_METHOD_PUT,
|
||||
HTTP_METHOD_DELETE
|
||||
} http_method_t;
|
||||
|
||||
http_client_handle http_create(const char *base_url);
|
||||
void http_destroy(http_client_handle client);
|
||||
|
||||
void http_set_bearer_token(http_client_handle client, const char *token);
|
||||
void http_set_timeout(http_client_handle client, long timeout_seconds);
|
||||
void http_set_connect_timeout(http_client_handle client, long timeout_seconds);
|
||||
|
||||
r_status_t http_request(
|
||||
http_client_handle client,
|
||||
http_method_t method,
|
||||
const char *path,
|
||||
const void *body,
|
||||
size_t body_size,
|
||||
char **response,
|
||||
int *response_code
|
||||
);
|
||||
|
||||
r_status_t http_get(http_client_handle client, const char *path, char **response);
|
||||
r_status_t http_post(http_client_handle client, const char *path, const char *body, char **response);
|
||||
r_status_t http_post_json(http_client_handle client, const char *path, const char *json, char **response);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
#include "logger.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
struct logger_t {
|
||||
log_level_t level;
|
||||
};
|
||||
|
||||
static const char *level_strings[] = {
|
||||
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
|
||||
};
|
||||
static const char *level_colors[] = {
|
||||
"\033[90m", "\033[36m", "\033[32m", "\033[33m", "\033[31m", "\033[35m"
|
||||
};
|
||||
static const char *color_reset = "\033[0m";
|
||||
|
||||
logger_handle logger_create(log_level_t level) {
|
||||
struct logger_t *logger = malloc(sizeof(struct logger_t));
|
||||
if (!logger) return NULL;
|
||||
|
||||
logger->level = level;
|
||||
return logger;
|
||||
}
|
||||
|
||||
void logger_destroy(logger_handle logger) {
|
||||
if (logger) free(logger);
|
||||
}
|
||||
|
||||
void logger_set_level(logger_handle logger, log_level_t level) {
|
||||
if (logger) logger->level = level;
|
||||
}
|
||||
|
||||
log_level_t logger_get_level(logger_handle logger) {
|
||||
return logger ? logger->level : LOG_LEVEL_INFO;
|
||||
}
|
||||
|
||||
void logger_log(logger_handle logger, log_level_t level,
|
||||
const char *file, int line, const char *fmt, ...) {
|
||||
if (!logger || !fmt) return;
|
||||
|
||||
if (level < logger->level) return;
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm *tm_info = localtime(&now);
|
||||
char time_buf[32];
|
||||
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info);
|
||||
|
||||
const char *basename = strrchr(file, '/');
|
||||
basename = basename ? basename + 1 : file;
|
||||
|
||||
fprintf(stderr, "%s[%s%s%s %s:%d] ",
|
||||
level_colors[level], level_strings[level], color_reset,
|
||||
basename, line);
|
||||
vfprintf(stderr, fmt, args);
|
||||
fprintf(stderr, "%s\n", color_reset);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
#ifndef R_INTERFACES_LOGGER_H
|
||||
#define R_INTERFACES_LOGGER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_TRACE,
|
||||
LOG_LEVEL_DEBUG,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_WARN,
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_FATAL
|
||||
} log_level_t;
|
||||
|
||||
typedef struct logger_t *logger_handle;
|
||||
|
||||
logger_handle logger_create(log_level_t level);
|
||||
void logger_destroy(logger_handle logger);
|
||||
|
||||
void logger_set_level(logger_handle logger, log_level_t level);
|
||||
log_level_t logger_get_level(logger_handle logger);
|
||||
|
||||
void logger_log(logger_handle logger, log_level_t level,
|
||||
const char *file, int line, const char *fmt, ...);
|
||||
|
||||
#define LOG_TRACE(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_TRACE, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define LOG_DEBUG(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define LOG_INFO(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define LOG_WARN(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define LOG_ERROR(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define LOG_FATAL(logger, fmt, ...) \
|
||||
logger_log(logger, LOG_LEVEL_FATAL, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user