xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / tui / commands.c

10.8 KB
1#include "commands.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include "util.h"
8#include "catalog.h"
9#include "complete.h"
10#include "resolve.h"
11#include "theme.h"
12
13// The keybindings the TUI accepts, in the order /help prints them.
14static const char *const KEYBINDINGS[][2] = {
15    {"Enter", "submit"},
16    {"Shift+Enter", "newline (Ctrl+J also works)"},
17    {"Esc", "pause the turn at the next step boundary"},
18    {"Ctrl+C", "cancel the turn"},
19    {"Ctrl+D", "exit on an empty draft"},
20    {"Tab", "complete command or path"},
21};
22
23// The built-in providers, in the order /provider lists them.
24static const struct {
25    const char *id;
26    const char *name;
27} BUILTINS[] = {
28    {"opencode-zen", "OpenCode Zen"},
29    {"opencode-go", "OpenCode Go"},
30};
31
32// The effort values the config accepts, in ascending order.
33static const char *const EFFORTS[] = {
34    "off", "minimal", "low", "medium", "high", "xhigh", "max",
35};
36
37static void run_help(tiny_command_ctx *ctx, const char *args);
38static void run_new(tiny_command_ctx *ctx, const char *args);
39static void run_provider(tiny_command_ctx *ctx, const char *args);
40static void run_model(tiny_command_ctx *ctx, const char *args);
41static void run_thinking(tiny_command_ctx *ctx, const char *args);
42
43static const tiny_command COMMANDS[] = {
44    {"help", "list commands and keybindings", run_help},
45    {"new", "start a new session", run_new},
46    {"provider", "choose the provider and model", run_provider},
47    {"model", "choose a model for the current provider", run_model},
48    {"thinking", "set the effort level", run_thinking},
49};
50
51static void emit_str(tiny_command_ctx *ctx, const char *s) {
52    ctx->write(ctx->user, strdup(s));
53}
54
55// One aligned `key  description` block; the key column is dimmed.
56static char *keyed(const char *key, const char *description, size_t width) {
57    tui_buf b;
58    buf_init(&b);
59    buf_adds(&b, "  ");
60    tui_buf pad;
61    buf_init(&pad);
62    buf_adds(&pad, key);
63    while (pad.len < width) buf_addc(&pad, ' ');
64    fg_dim(&b, pad.data ? pad.data : "");
65    buf_free(&pad);
66    buf_adds(&b, "  ");
67    buf_adds(&b, description);
68    return buf_steal(&b);
69}
70
71static void run_help(tiny_command_ctx *ctx, const char *args) {
72    (void)args;
73    size_t width = 0;
74    for (size_t i = 0; i < sizeof COMMANDS / sizeof COMMANDS[0]; i++) {
75        char name[64];
76        snprintf(name, sizeof name, "/%s", COMMANDS[i].name);
77        if (strlen(name) > width) width = strlen(name);
78    }
79    for (size_t i = 0; i < sizeof KEYBINDINGS / sizeof KEYBINDINGS[0]; i++)
80        if (strlen(KEYBINDINGS[i][0]) > width) width = strlen(KEYBINDINGS[i][0]);
81
82    emit_str(ctx, "commands");
83    for (size_t i = 0; i < sizeof COMMANDS / sizeof COMMANDS[0]; i++) {
84        char name[64];
85        snprintf(name, sizeof name, "/%s", COMMANDS[i].name);
86        ctx->write(ctx->user, keyed(name, COMMANDS[i].description, width));
87    }
88    emit_str(ctx, "");
89    emit_str(ctx, "keybindings");
90    for (size_t i = 0; i < sizeof KEYBINDINGS / sizeof KEYBINDINGS[0]; i++)
91        ctx->write(ctx->user, keyed(KEYBINDINGS[i][0], KEYBINDINGS[i][1], width));
92}
93
94static void run_new(tiny_command_ctx *ctx, const char *args) {
95    (void)args;
96    ctx->new_session(ctx->user);
97    emit_str(ctx, "new session");
98}
99
100static const tiny_provider *find_custom(const tiny_config *cfg, const char *id) {
101    for (size_t i = 0; i < cfg->custom_providers_len; i++)
102        if (strcmp(cfg->custom_providers[i].id, id) == 0) return &cfg->custom_providers[i];
103    return NULL;
104}
105
106static bool is_builtin(const char *id) {
107    for (size_t i = 0; i < sizeof BUILTINS / sizeof BUILTINS[0]; i++)
108        if (strcmp(BUILTINS[i].id, id) == 0) return true;
109    return false;
110}
111
112// The first model advertised for a provider, or NULL when it has none.
113static const char *first_model(const tiny_config *cfg, const char *provider) {
114    const tiny_provider *custom = find_custom(cfg, provider);
115    if (custom) return custom->models_len > 0 ? custom->models[0] : NULL;
116    const char *canonical = tiny_provider_canonical(provider);
117    for (size_t i = 0; i < tiny_catalog_count(); i++) {
118        const tiny_model_info *e = tiny_catalog_get(i);
119        if (strcmp(e->provider, canonical) == 0) return e->id;
120    }
121    return NULL;
122}
123
124void tiny_choice_free(tiny_choice *c) {
125    strv_free(&c->ids);
126    strv_free(&c->labels);
127}
128
129static void push_option(tiny_choice *c, const char *id, const char *label) {
130    strv_push(&c->ids, strdup(id));
131    strv_push(&c->labels, strdup(label && label[0] ? label : id));
132}
133
134tiny_choice tiny_choose_list(const tiny_config *cfg, tiny_choose what,
135                             const char *provider) {
136    tiny_choice c = {0};
137
138    if (what == TINY_CHOOSE_PROVIDER) {
139        for (size_t i = 0; i < sizeof BUILTINS / sizeof BUILTINS[0]; i++)
140            push_option(&c, BUILTINS[i].id, BUILTINS[i].name);
141        for (size_t i = 0; i < cfg->custom_providers_len; i++) {
142            const tiny_provider *p = &cfg->custom_providers[i];
143            push_option(&c, p->id, p->name);
144        }
145    } else if (what == TINY_CHOOSE_MODEL) {
146        const char *prov = provider ? provider : cfg->provider;
147        if (prov) {
148            const tiny_provider *custom = find_custom(cfg, prov);
149            if (custom) {
150                for (size_t i = 0; i < custom->models_len; i++)
151                    push_option(&c, custom->models[i], custom->models[i]);
152            } else {
153                const char *canonical = tiny_provider_canonical(prov);
154                for (size_t i = 0; i < tiny_catalog_count(); i++) {
155                    const tiny_model_info *e = tiny_catalog_get(i);
156                    if (strcmp(e->provider, canonical) == 0)
157                        push_option(&c, e->id, e->name);
158                }
159            }
160        }
161    } else {
162        const tiny_model_info *info = cfg->provider && cfg->model
163            ? tiny_catalog_lookup(tiny_provider_canonical(cfg->provider), cfg->model)
164            : NULL;
165        if (info) {
166            for (size_t i = 0; i < info->effort_len; i++) {
167                const char *v = strcmp(info->effort[i], "none") == 0 ? "off" : info->effort[i];
168                push_option(&c, v, v);
169            }
170        }
171    }
172
173    return c;
174}
175
176static const char *provider_name(const tiny_config *cfg, const char *id) {
177    for (size_t i = 0; i < sizeof BUILTINS / sizeof BUILTINS[0]; i++)
178        if (strcmp(BUILTINS[i].id, id) == 0) return BUILTINS[i].name;
179    const tiny_provider *p = find_custom(cfg, id);
180    if (p) return p->name ? p->name : p->id;
181    return id;
182}
183
184char *tiny_choose_title(const tiny_config *cfg, tiny_choose what,
185                        const char *provider) {
186    if (what == TINY_CHOOSE_PROVIDER) return strdup("Select a provider");
187    if (what == TINY_CHOOSE_EFFORT) return strdup("Select a thinking level");
188    const char *prov = provider ? provider : cfg->provider;
189    if (!prov) return strdup("Select a model");
190    const char *name = provider_name(cfg, prov);
191    size_t n = strlen(name) + 32;
192    char *s = xmalloc(n);
193    snprintf(s, n, "Select a model for %s", name);
194    return s;
195}
196
197static void run_provider(tiny_command_ctx *ctx, const char *args) {
198    char *id = tiny_trim(args);
199    if (id[0] == '\0') {
200        free(id);
201        ctx->choose(ctx->user, TINY_CHOOSE_PROVIDER);
202        return;
203    }
204    if (!is_builtin(id) && !find_custom(ctx->cfg, id)) {
205        char msg[256];
206        snprintf(msg, sizeof msg, "! unknown provider: %s", id);
207        emit_str(ctx, msg);
208        free(id);
209        return;
210    }
211    const char *model = first_model(ctx->cfg, id);
212    if (!model) {
213        char msg[256];
214        snprintf(msg, sizeof msg, "! no models for provider: %s", id);
215        emit_str(ctx, msg);
216        free(id);
217        return;
218    }
219    char *err = NULL;
220    if (ctx->apply(ctx->user, id, model, NULL, &err) != 0) {
221        char msg[512];
222        snprintf(msg, sizeof msg, "! %s", err ? err : "cannot switch provider");
223        emit_str(ctx, msg);
224        free(err);
225    }
226    free(id);
227}
228
229static void run_model(tiny_command_ctx *ctx, const char *args) {
230    char *id = tiny_trim(args);
231    if (id[0] == '\0') {
232        free(id);
233        ctx->choose(ctx->user, TINY_CHOOSE_MODEL);
234        return;
235    }
236    char *err = NULL;
237    if (ctx->apply(ctx->user, NULL, id, NULL, &err) != 0) {
238        char msg[512];
239        snprintf(msg, sizeof msg, "! %s", err ? err : "cannot switch model");
240        emit_str(ctx, msg);
241        free(err);
242    }
243    free(id);
244}
245
246static void run_thinking(tiny_command_ctx *ctx, const char *args) {
247    char *level = tiny_trim(args);
248    if (level[0] == '\0') {
249        free(level);
250        ctx->choose(ctx->user, TINY_CHOOSE_EFFORT);
251        return;
252    }
253    bool valid = false;
254    for (size_t i = 0; i < sizeof EFFORTS / sizeof EFFORTS[0]; i++)
255        if (strcmp(EFFORTS[i], level) == 0) valid = true;
256    if (!valid) {
257        char msg[256];
258        snprintf(msg, sizeof msg, "! unknown effort: %s", level);
259        emit_str(ctx, msg);
260        free(level);
261        return;
262    }
263    char *err = NULL;
264    if (ctx->apply(ctx->user, NULL, NULL, level, &err) != 0) {
265        char msg[512];
266        snprintf(msg, sizeof msg, "! %s", err ? err : "cannot set effort");
267        emit_str(ctx, msg);
268        free(err);
269    }
270    free(level);
271}
272
273const tiny_command *tiny_find_command(const char *text, const char **args) {
274    if (text[0] != '/') return NULL;
275    const char *p = text + 1;
276    size_t n = 0;
277    while (p[n] && p[n] != ' ' && p[n] != '\t' && p[n] != '\n') n++;
278    for (size_t i = 0; i < sizeof COMMANDS / sizeof COMMANDS[0]; i++) {
279        if (strlen(COMMANDS[i].name) == n && strncmp(COMMANDS[i].name, p, n) == 0) {
280            const char *rest = p + n;
281            while (*rest == ' ' || *rest == '\t') rest++;
282            *args = rest;
283            return &COMMANDS[i];
284        }
285    }
286    return NULL;
287}
288
289char *tiny_complete_command(const char *draft) {
290    if (draft[0] != '/') return NULL;
291    for (const char *p = draft; *p; p++)
292        if (*p == ' ' || *p == '\t' || *p == '\n') return NULL;
293    const char *typed = draft + 1;
294    size_t tn = strlen(typed);
295    const tiny_command *match = NULL;
296    size_t nmatch = 0;
297    char *shared = NULL;
298    for (size_t i = 0; i < sizeof COMMANDS / sizeof COMMANDS[0]; i++) {
299        if (strncmp(COMMANDS[i].name, typed, tn) != 0) continue;
300        nmatch++;
301        if (!match) match = &COMMANDS[i];
302        if (!shared) shared = strdup(COMMANDS[i].name);
303        else {
304            char *next = tiny_common_prefix(shared, COMMANDS[i].name);
305            free(shared);
306            shared = next;
307        }
308    }
309    char *result = NULL;
310    if (nmatch == 1) {
311        tui_buf b;
312        buf_init(&b);
313        buf_addc(&b, '/');
314        buf_adds(&b, match->name);
315        result = b.data ? buf_steal(&b) : NULL;
316    } else if (nmatch > 1 && strlen(shared) > tn) {
317        tui_buf b;
318        buf_init(&b);
319        buf_addc(&b, '/');
320        buf_adds(&b, shared);
321        result = b.data ? buf_steal(&b) : NULL;
322    }
323    free(shared);
324    return result;
325}