xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / main.c

4.4 KB
1// tiny-c-agent - config-first agent for the terminal.
2
3#include <limits.h>
4#include <signal.h>
5#include <stdbool.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include <curl/curl.h>
12
13#include "agent.h"
14#include "config.h"
15#include "headless.h"
16#include "resolve.h"
17#include "resources.h"
18#include "tool.h"
19#include "util.h"
20#include "tui/tui.h"
21
22static volatile bool g_cancel;
23
24static void on_signal(int sig) {
25    (void)sig;
26    g_cancel = true;
27}
28
29int main(int argc, char **argv) {
30    const char *config_path = NULL;
31    const char *prompt = NULL;
32    bool print_mode = false;
33
34    for (int i = 1; i < argc; i++) {
35        const char *arg = argv[i];
36        if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) {
37            if (++i >= argc) {
38                fprintf(stderr, "%s requires a path\n", arg);
39                return 2;
40            }
41            config_path = argv[i];
42        } else if (strncmp(arg, "--config=", 9) == 0) {
43            config_path = arg + 9;
44        } else if (strcmp(arg, "-p") == 0 || strcmp(arg, "--print") == 0) {
45            print_mode = true;
46            if (i + 1 < argc) prompt = argv[++i];
47        } else if (strncmp(arg, "--print=", 8) == 0) {
48            print_mode = true;
49            prompt = arg + 8;
50        } else {
51            fprintf(stderr, "unknown argument: %s\n", arg);
52            return 2;
53        }
54    }
55
56    char *stdin_prompt = NULL;
57    if (print_mode && !prompt) {
58        if (isatty(fileno(stdin))) {
59            fprintf(stderr, "-p requires a prompt\n");
60            return 2;
61        }
62        stdin_prompt = tiny_read_stream(stdin);
63        prompt = stdin_prompt;
64    }
65
66    curl_global_init(CURL_GLOBAL_DEFAULT);
67
68    char *err = NULL;
69    tiny_config *cfg = tiny_config_load(config_path, &err);
70    if (!cfg) {
71        fprintf(stderr, "%s\n", err ? err : "config error");
72        free(err);
73        free(stdin_prompt);
74        curl_global_cleanup();
75        return 1;
76    }
77
78    tiny_model model;
79    memset(&model, 0, sizeof model);
80    if (tiny_resolve_model(cfg, &model, &err) != 0) {
81        if (print_mode) {
82            fprintf(stderr, "%s\n", err ? err : "model error");
83            free(err);
84            tiny_config_free(cfg);
85            free(stdin_prompt);
86            curl_global_cleanup();
87            return 1;
88        }
89        // Keep the interactive TUI usable: the user can pick a provider with
90        // /provider. The turn reports NO_MODEL until one is configured.
91        free(err);
92        err = NULL;
93    }
94
95    char cwd[PATH_MAX];
96    if (!getcwd(cwd, sizeof cwd)) strcpy(cwd, ".");
97
98    tiny_resources *resources = tiny_resources_build(cfg, cwd, &err);
99    if (!resources) {
100        fprintf(stderr, "%s\n", err ? err : "prompt error");
101        free(err);
102        tiny_config_free(cfg);
103        free(stdin_prompt);
104        curl_global_cleanup();
105        return 1;
106    }
107
108    size_t tools_len;
109    const tiny_tool *tools = tiny_tools(model.supports_images, &tools_len);
110
111    if (!print_mode) {
112        // The TUI owns the terminal and its own signal handlers.
113        tiny_tui_config tc = {
114            .cfg = cfg,
115            .model = model,
116            .resources = resources,
117            .cwd = cwd,
118        };
119        int code = tiny_tui_run(&tc);
120        tiny_resources_free(resources);
121        tiny_config_free(cfg);
122        free(err);
123        free(stdin_prompt);
124        curl_global_cleanup();
125        return code;
126    }
127
128    tiny_session *session = tiny_session_open(cfg->sessions_dir, cwd, prompt, &err);
129    if (!session) {
130        fprintf(stderr, "%s\n", err ? err : "session error");
131        free(err);
132        tiny_resources_free(resources);
133        tiny_config_free(cfg);
134        free(stdin_prompt);
135        curl_global_cleanup();
136        return 1;
137    }
138
139    signal(SIGINT, on_signal);
140    signal(SIGTERM, on_signal);
141
142    tiny_headless head;
143    tiny_headless_init(&head, stdout, stderr);
144
145    tiny_agent a = {0};
146    a.model = model;
147    a.system_prompt = resources->system_prompt;
148    a.tools = tools;
149    a.tools_len = tools_len;
150
151    a.session = session;
152    a.on_event = tiny_headless_event;
153    a.user = &head;
154    a.cancel = &g_cancel;
155
156    tiny_agent_turn(&a, prompt);
157
158    int code = tiny_headless_finish(&head);
159
160    tiny_agent_free(&a);
161    tiny_headless_free(&head);
162    tiny_session_close(session);
163    tiny_resources_free(resources);
164    tiny_config_free(cfg);
165    free(err);
166    free(stdin_prompt);
167    curl_global_cleanup();
168    return code;
169}
170