This commit is contained in:
retoor 2026-01-28 19:47:14 +01:00
parent 3f8e1ec53a
commit 5ee81eb58e

View File

@ -44,7 +44,8 @@ static void handle_sigint(int sig);
static char *get_env_string(void) { static char *get_env_string(void) {
FILE *fp = popen("env", "r"); FILE *fp = popen("env", "r");
if (!fp) return NULL; if (!fp)
return NULL;
size_t buffer_size = 1024; size_t buffer_size = 1024;
size_t total_size = 0; size_t total_size = 0;
@ -55,7 +56,8 @@ static char *get_env_string(void) {
} }
size_t bytes_read; size_t bytes_read;
while ((bytes_read = fread(output + total_size, 1, buffer_size - total_size, fp)) > 0) { while ((bytes_read = fread(output + total_size, 1, buffer_size - total_size,
fp)) > 0) {
total_size += bytes_read; total_size += bytes_read;
if (total_size >= buffer_size) { if (total_size >= buffer_size) {
buffer_size *= 2; buffer_size *= 2;
@ -120,7 +122,8 @@ static char *get_prompt_from_args(int argc, char **argv) {
fprintf(stderr, "Syntax highlighting disabled.\n"); fprintf(stderr, "Syntax highlighting disabled.\n");
} else if (strncmp(argv[i], "--session=", 10) == 0) { } else if (strncmp(argv[i], "--session=", 10) == 0) {
continue; continue;
} else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--session") == 0) { } else if (strcmp(argv[i], "-s") == 0 ||
strcmp(argv[i], "--session") == 0) {
i++; i++;
continue; continue;
} else { } else {
@ -166,7 +169,8 @@ static bool try_prompt(int argc, char *argv[]) {
static bool include_file(const char *path) { static bool include_file(const char *path) {
char *file_content = read_file(path); char *file_content = read_file(path);
if (!file_content) return false; if (!file_content)
return false;
if (global_messages) { if (global_messages) {
messages_add(global_messages, "system", file_content); messages_add(global_messages, "system", file_content);
@ -192,7 +196,8 @@ static void repl(void) {
while (true) { while (true) {
line = line_read("> "); line = line_read("> ");
if (!line || !*line) continue; if (!line || !*line)
continue;
if (!strncmp(line, "!dump", 5)) { if (!strncmp(line, "!dump", 5)) {
char *json = messages_to_string(global_messages); char *json = messages_to_string(global_messages);
@ -215,7 +220,8 @@ static void repl(void) {
if (!strncmp(line, "!verbose", 8)) { if (!strncmp(line, "!verbose", 8)) {
bool verbose = !r_config_is_verbose(cfg); bool verbose = !r_config_is_verbose(cfg);
r_config_set_verbose(cfg, verbose); r_config_set_verbose(cfg, verbose);
fprintf(stderr, "%s\n", verbose ? "Verbose mode enabled" : "Verbose mode disabled"); fprintf(stderr, "%s\n",
verbose ? "Verbose mode enabled" : "Verbose mode disabled");
continue; continue;
} }
if (line && *line != '\n') { if (line && *line != '\n') {
@ -231,7 +237,9 @@ static void repl(void) {
if (http) { if (http) {
http_client_set_show_spinner(http, false); http_client_set_show_spinner(http, false);
char *response = NULL; char *response = NULL;
if (http_get(http, r_config_get_models_url(cfg), &response) == R_SUCCESS && response) { if (http_get(http, r_config_get_models_url(cfg), &response) ==
R_SUCCESS &&
response) {
printf("Models: %s\n", response); printf("Models: %s\n", response);
free(response); free(response);
} }
@ -290,42 +298,63 @@ static void init(void) {
if (!getcwd(cwd, sizeof(cwd))) { if (!getcwd(cwd, sizeof(cwd))) {
strcpy(cwd, "unknown"); strcpy(cwd, "unknown");
} }
snprintf( snprintf(
payload, sizeof(payload), payload, sizeof(payload),
"# AUTONOMOUS AGENT INSTRUCTIONS\n" "# AUTONOMOUS AGENT INSTRUCTIONS\n"
"Current date/time: %s\n" "Current date/time: %s\n"
"Working directory: %s\n\n" "Working directory: %s\n\n"
"You are an autonomous AI agent. You operate in a loop: reason about the task, " "You are an autonomous AI agent. You operate in a loop: reason about the "
"select and execute tools when needed, observe results, and continue until the goal is achieved.\n\n" "task, "
"select and execute tools when needed, observe results, and continue "
"until the goal is achieved.\n\n"
"## Reasoning Pattern (ReAct)\n" "## Reasoning Pattern (ReAct)\n"
"For complex tasks, think step-by-step:\n" "For complex tasks, think step-by-step:\n"
"1. Thought: What do I need to accomplish? What information do I have?\n" "1. Thought: What do I need to accomplish? What information do I have?\n"
"2. Action: Which tool should I use? With what parameters?\n" "2. Action: Which tool should I use? With what parameters?\n"
"3. Observation: What did the tool return? What does this tell me?\n" "3. Observation: What did the tool return? What does this tell me?\n"
"4. Repeat until the goal is complete.\n\n" "4. Repeat until the goal is complete.\n\n"
"5. Do not ask questions, do assumptions autonomously."
"## Tool Usage\n" "## Tool Usage\n"
"- Use tools proactively to gather information and take actions\n" "- Use tools proactively to gather information and take actions\n"
"- If a tool fails, analyze the error and try a different approach\n" "- If a tool fails, analyze the error and try a different approach\n"
"- You can call multiple tools in sequence to accomplish complex tasks\n\n" "- You can call multiple tools in sequence to accomplish complex "
"tasks\n\n"
"- Take decissions based on tool output yourself autonomously."
"## CRITICAL OUTPUT RULES\n" "## CRITICAL OUTPUT RULES\n"
"- You MUST include the actual content/data from tool results in your response\n" "- You MUST include the actual content/data from tool results in your "
"response\n"
"- When you search the web, QUOTE the relevant information found\n" "- When you search the web, QUOTE the relevant information found\n"
"- When you run a command, SHOW the output\n" "- When you run a command, SHOW the output\n"
"- NEVER say 'I found information' without showing what you found\n" "- NEVER say 'I found information' without showing what you found\n"
"- NEVER say 'task complete' or 'report provided' - SHOW THE ACTUAL DATA\n" "- NEVER say 'task complete' or 'report provided' - SHOW THE ACTUAL "
"- The user cannot see tool results - only YOUR response. Include everything relevant.\n\n" "DATA\n"
"- The user cannot see tool results - only YOUR response. Include "
"everything relevant.\n\n"
"## Local Database\n" "## Local Database\n"
"You have a local SQLite database accessible via db_query, db_get, and db_set tools.\n" "You have a local SQLite database accessible via db_query, db_get, and "
"db_set tools.\n"
"Use stemmed, lowercase keys to prevent duplicates.\n" "Use stemmed, lowercase keys to prevent duplicates.\n"
"Schema: %s\n\n" "Schema: %s\n\n"
"## Response Format\n" "## Response Format\n"
"Your response IS the only thing the user sees. Tool outputs are hidden from them.\n" "Your response IS the only thing the user sees. Tool outputs are hidden "
"You MUST copy/paste relevant data from tool results into your response.\n" "from them.\n"
"You MUST copy/paste relevant data from tool results into your "
"response.\n"
"Bad: 'I searched and found information about X.'\n" "Bad: 'I searched and found information about X.'\n"
"Good: 'Here is what I found: [actual content from search results]'\n", "Good: 'Here is what I found: [actual content from search results]'\n"
"## Reasoning\n"
"Let the user know what you thougt process / reasoning is and why you "
"choose to do things the way you do."
"## Backup\n"
"You have the power to delete and modify files whatever your want to but "
"always make a .bak file with backup before editing unless you've "
"created the file yourself."
"## Terminal execution\n"
"You are in a bash environment, so can also execute bash commands.\n "
"Everything is allowed.\n"
"Be very creative with your bash tool use, use it exessively.\n"
"Prefer commands that do not require root access.\n",
datetime, cwd, schema ? schema : "{}"); datetime, cwd, schema ? schema : "{}");
free(schema); free(schema);
fprintf(stderr, "Loading..."); fprintf(stderr, "Loading...");
@ -388,7 +417,8 @@ static void parse_session_arg(int argc, char *argv[]) {
} }
return; return;
} }
if ((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--session") == 0) && i + 1 < argc) { if ((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--session") == 0) &&
i + 1 < argc) {
const char *name = argv[++i]; const char *name = argv[++i];
if (!r_config_set_session_id(cfg, name)) { if (!r_config_set_session_id(cfg, name)) {
fprintf(stderr, "Error: Invalid session name '%s'\n", name); fprintf(stderr, "Error: Invalid session name '%s'\n", name);