This commit is contained in:
retoor 2026-01-28 20:06:18 +01:00
parent 4f15e7873b
commit 0d91c38149
2 changed files with 48 additions and 44 deletions

View File

@ -32,6 +32,7 @@ static const char *incomplete_phrases[] = {
"I need to", "I should", "I can ", "Going to ",
"Will now", "Proceeding", "Starting to", "About to ",
"First, I", "Then I", "After that", "Following that",
"Now let me", "Let's check", "Let me check",
NULL
};
@ -274,9 +275,10 @@ char *agent_run(agent_handle agent, const char *user_message) {
return NULL;
}
char *final_response = NULL;
char *accumulated_response = NULL;
size_t accumulated_len = 0;
while (agent->state == AGENT_STATE_RUNNING) {
while (agent->state == AGENT_STATE_RUNNING || agent->state == AGENT_STATE_EXECUTING_TOOLS) {
agent->iteration_count++;
if (agent->iteration_count > agent->max_iterations) {
@ -310,6 +312,7 @@ char *agent_run(agent_handle agent, const char *user_message) {
agent->tool_retry_count, agent->max_tool_retries);
}
json_data = agent_build_request(agent, NULL, NULL);
agent->state = AGENT_STATE_RUNNING;
continue;
}
@ -320,6 +323,26 @@ char *agent_run(agent_handle agent, const char *user_message) {
messages_add_object(agent->messages, json_object_get(message_obj));
}
char *content = agent_get_content(choice);
if (content && *content) {
// Print content immediately to the user
extern void parse_markdown_to_ansi(const char *content);
parse_markdown_to_ansi(content);
printf("\n");
size_t content_len = strlen(content);
char *new_acc = realloc(accumulated_response, accumulated_len + content_len + 2);
if (new_acc) {
accumulated_response = new_acc;
if (accumulated_len > 0) {
strcat(accumulated_response, "\n");
accumulated_len += 1;
}
strcpy(accumulated_response + accumulated_len, content);
accumulated_len += content_len;
}
}
bool has_tools = agent_has_tool_calls(choice);
if (agent->verbose) {
@ -349,40 +372,36 @@ char *agent_run(agent_handle agent, const char *user_message) {
if (!json_data) {
agent->state = AGENT_STATE_ERROR;
agent_set_error(agent, "Failed to create follow-up JSON");
free(content);
break;
}
} else {
char *content = agent_get_content(choice);
if (content && agent_response_indicates_incomplete(content)) {
} else if (content && agent_response_indicates_incomplete(content)) {
if (agent->verbose) {
fprintf(stderr, "[Agent] Response indicates incomplete work, auto-continuing\n");
}
free(content);
json_data = agent_build_request(agent, "user",
"Continue. Execute the necessary actions to complete the task.");
agent->state = AGENT_STATE_RUNNING;
if (!json_data) {
agent->state = AGENT_STATE_ERROR;
agent_set_error(agent, "Failed to create continue JSON");
free(content);
break;
}
} else {
final_response = content;
agent->state = AGENT_STATE_COMPLETED;
if (agent->verbose) {
fprintf(stderr, "[Agent] Completed in %d iteration(s)\n",
agent->iteration_count);
}
}
}
free(content);
}
free(json_data);
return final_response;
return accumulated_response;
}
char *agent_chat(const char *user_message, messages_handle messages) {

View File

@ -32,7 +32,6 @@ static messages_handle global_messages = NULL;
extern tool_registry_t *tools_get_registry(void);
extern void tools_registry_shutdown(void);
static void render(const char *content);
static bool include_file(const char *path);
static char *get_prompt_from_stdin(char *prompt);
static char *get_prompt_from_args(int argc, char **argv);
@ -159,7 +158,7 @@ static bool try_prompt(int argc, char *argv[]) {
free(prompt);
return false;
}
render(response);
// response is already printed inside agent_run
free(response);
free(prompt);
return true;
@ -179,14 +178,6 @@ static bool include_file(const char *path) {
return true;
}
static void render(const char *content) {
if (syntax_highlight_enabled) {
parse_markdown_to_ansi(content);
} else {
printf("%s", content);
}
}
static void repl(void) {
r_config_handle cfg = r_config_get_instance();
tool_registry_t *tools = tools_get_registry();
@ -261,18 +252,12 @@ static void repl(void) {
while (line && *line != '\n') {
char *response = agent_chat(line, global_messages);
if (response) {
render(response);
printf("\n");
if (strstr(response, "_STEP_")) {
line = "continue";
} else {
line = NULL;
}
// response is already printed inside agent_run via parse_markdown_to_ansi
free(response);
} else {
fprintf(stderr, "Agent returned no response\n");
line = NULL;
}
line = NULL;
}
}
}