aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / setup_wizard.c
| 1 | /* setup_wizard.c -- Allen's first-run setup; see setup_wizard.h. */ |
| 2 | #include "setup_wizard.h" |
| 3 | |
| 4 | #define HELLO_MS 6000u /* hello stays about as long as it takes to read */ |
| 5 | #define BACK_MS 4000u /* "welcome back" after a restart */ |
| 6 | #define SCAN_LINE_MS 700u /* the window eases one found line in every this often */ |
| 7 | #define SCAN_REST_MS 2500u |
| 8 | #define AUTO_PICK_MS 10000u /* a single model is used if nothing is chosen */ |
| 9 | #define TEST_MS 30000u |
| 10 | #define READY_MS 15000u |
| 11 | |
| 12 | static const char *const step_names[SW_STEPS] = { |
| 13 | "hello", "intent", "other", "other-check", "scan", "consent", "each", "games", "ai", |
| 14 | "llm", "service", "url", "key", "testing", "test-failed", "ready", "done", |
| 15 | }; |
| 16 | |
| 17 | const char *sw_step_name(int step) { return step >= 0 && step < SW_STEPS ? step_names[step] : "?"; } |
| 18 | |
| 19 | static void zero(void *p, unsigned n) |
| 20 | { |
| 21 | volatile uint8_t *b = p; /* volatile: the key is wiped even when nothing reads it after */ |
| 22 | while (n--) |
| 23 | *b++ = 0; |
| 24 | } |
| 25 | |
| 26 | static int same(const char *a, const char *b) |
| 27 | { |
| 28 | while (*a && *a == *b) |
| 29 | a++, b++; |
| 30 | return *a == *b; |
| 31 | } |
| 32 | |
| 33 | static int starts(const char *s, const char *w) |
| 34 | { |
| 35 | while (*w) |
| 36 | if (*s++ != *w++) |
| 37 | return 0; |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /* ---- the log ---- */ |
| 42 | |
| 43 | static void event(sw_wizard *w, const char *a, const char *b) |
| 44 | { |
| 45 | char *d = w->ev[(w->ev_head + w->ev_n) % SW_EVENTS]; |
| 46 | unsigned at = si_copy(d, a, SW_LINE); |
| 47 | if (b) |
| 48 | si_cat(d, at, b, SW_LINE); |
| 49 | if (w->ev_n < SW_EVENTS) |
| 50 | w->ev_n++; |
| 51 | else |
| 52 | w->ev_head = (w->ev_head + 1) % SW_EVENTS; |
| 53 | } |
| 54 | |
| 55 | int sw_event(sw_wizard *w, char *out, unsigned cap) |
| 56 | { |
| 57 | if (!w->ev_n) |
| 58 | return 0; |
| 59 | si_copy(out, w->ev[w->ev_head], cap); |
| 60 | w->ev_head = (w->ev_head + 1) % SW_EVENTS; |
| 61 | w->ev_n--; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* ---- the view ---- */ |
| 66 | |
| 67 | static void say(sw_wizard *w, const char *s) |
| 68 | { |
| 69 | if (!same(w->v.say, s)) { |
| 70 | si_copy(w->v.say, s, SW_SAY); |
| 71 | w->v.said_gen++; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static char *line(sw_wizard *w) |
| 76 | { |
| 77 | if (w->v.nline >= SW_LINES) |
| 78 | return 0; |
| 79 | char *d = w->v.line[w->v.nline++]; |
| 80 | d[0] = 0; |
| 81 | return d; |
| 82 | } |
| 83 | |
| 84 | static void add_line(sw_wizard *w, const char *s) |
| 85 | { |
| 86 | char *d = line(w); |
| 87 | if (d) |
| 88 | si_copy(d, s, SW_LINE); |
| 89 | } |
| 90 | |
| 91 | static void choice(sw_wizard *w, const char *s) |
| 92 | { |
| 93 | if (w->v.nchoice < SW_CHOICES) |
| 94 | si_copy(w->v.choice[w->v.nchoice++], s, SW_CHOICE); |
| 95 | } |
| 96 | |
| 97 | static unsigned gb(char *d, unsigned at, uint32_t mib, unsigned cap) |
| 98 | { |
| 99 | at = si_num(d, at, (mib + 512) / 1024, cap); |
| 100 | return si_cat(d, at, " GB", cap); |
| 101 | } |
| 102 | |
| 103 | /* ---- what Allen would set up ---- */ |
| 104 | |
| 105 | static const si_display *main_screen(const sw_wizard *w) { return w->inv->ndisplay ? &w->inv->display[0] : 0; } |
| 106 | |
| 107 | static int screen_can_go_faster(const sw_wizard *w) |
| 108 | { |
| 109 | const si_display *s = main_screen(w); |
| 110 | return s && s->hz && s->max_hz > s->hz; |
| 111 | } |
| 112 | |
| 113 | static unsigned item_text(const sw_wizard *w, unsigned it, char *d, unsigned cap) |
| 114 | { |
| 115 | unsigned at = 0; |
| 116 | int g = si_best_gpu(w->inv); |
| 117 | switch (it) { |
| 118 | case SW_I_DRIVERS: |
| 119 | return si_copy(d, "Use my own drivers for the parts I can drive", cap); |
| 120 | case SW_I_SCREEN: |
| 121 | at = si_copy(d, "Set your screen to ", cap); |
| 122 | at = si_num(d, at, main_screen(w)->max_hz, cap); |
| 123 | return si_cat(d, at, " times a second", cap); |
| 124 | case SW_I_CORES: |
| 125 | return si_copy(d, "Keep the fast processor cores free for games", cap); |
| 126 | case SW_I_AI_GPU: |
| 127 | if (g < 0) |
| 128 | return si_copy(d, "Run the AI on the processor", cap); |
| 129 | at = si_copy(d, "Run the AI on your ", cap); |
| 130 | return si_cat(d, at, w->inv->gpu[g].name, cap); |
| 131 | case SW_I_BUILD: |
| 132 | return si_copy(d, "Build drivers for any part I can't drive yet", cap); |
| 133 | } |
| 134 | d[0] = 0; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static const char *const item_id[SW_I_COUNT] = {"drivers-own", "screen-refresh", "cpu-games", "ai-gpu", |
| 139 | "drivers-build"}; |
| 140 | |
| 141 | static void plan_items(sw_wizard *w) |
| 142 | { |
| 143 | w->nitem = 0; |
| 144 | w->item[w->nitem++] = SW_I_DRIVERS; |
| 145 | if ((w->want & SW_WANT_GAMES) && screen_can_go_faster(w)) |
| 146 | w->item[w->nitem++] = SW_I_SCREEN; |
| 147 | if (w->want & SW_WANT_GAMES) |
| 148 | w->item[w->nitem++] = SW_I_CORES; |
| 149 | if (w->want & SW_WANT_AI) |
| 150 | w->item[w->nitem++] = SW_I_AI_GPU; |
| 151 | w->item[w->nitem++] = SW_I_BUILD; |
| 152 | } |
| 153 | |
| 154 | /* ---- the AI models found ---- */ |
| 155 | |
| 156 | static int is_27b(const char *n) { return starts(n, "qwen3.8-27b"); } |
| 157 | |
| 158 | static void cand_from_model(sw_cand *c, const si_model *m) |
| 159 | { |
| 160 | zero(c, sizeof *c); |
| 161 | c->kind = SW_LLM_LOCAL; |
| 162 | c->src = m->src; |
| 163 | si_copy(c->name, m->name, sizeof c->name); |
| 164 | if (m->running) |
| 165 | si_copy(c->url, m->where, sizeof c->url); |
| 166 | if (m->src == SI_SRC_PAIR) { |
| 167 | si_copy(c->label, "Allen's own model pair", sizeof c->label); |
| 168 | si_copy(c->hint, "27B + 30B, working together, on this computer", sizeof c->hint); |
| 169 | return; |
| 170 | } |
| 171 | si_copy(c->label, is_27b(m->name) ? "The 27B model" : m->name, sizeof c->label); |
| 172 | si_copy(c->hint, |
| 173 | m->running ? "Already running on this computer" |
| 174 | : m->src == SI_SRC_OLLAMA ? "Installed with Ollama on this computer" |
| 175 | : m->src == SI_SRC_LMSTUDIO ? "Installed with LM Studio on this computer" |
| 176 | : "A model file on this computer", |
| 177 | sizeof c->hint); |
| 178 | } |
| 179 | |
| 180 | /* On this computer first (the pair, then what is running, then what is |
| 181 | * installed), then other AienOS computers nearby. */ |
| 182 | static void find_models(sw_wizard *w) |
| 183 | { |
| 184 | const si_inv *v = w->inv; |
| 185 | w->ncand = 0; |
| 186 | for (int pass = 0; pass < 3; pass++) |
| 187 | for (unsigned i = 0; i < v->nmodel && w->ncand < SW_CANDS; i++) { |
| 188 | const si_model *m = &v->model[i]; |
| 189 | int p = m->src == SI_SRC_PAIR ? 0 : m->running ? 1 : 2; |
| 190 | if (p == pass) |
| 191 | cand_from_model(&w->cand[w->ncand++], m); |
| 192 | } |
| 193 | for (unsigned i = 0; i < v->npeer && w->ncand < SW_CANDS; i++) { |
| 194 | const si_peer *q = &v->peer[i]; |
| 195 | if (q->kind != SI_PEER_AIENOS || !q->model[0] || !q->url[0]) |
| 196 | continue; |
| 197 | sw_cand *c = &w->cand[w->ncand++]; |
| 198 | zero(c, sizeof *c); |
| 199 | c->kind = SW_LLM_PEER; |
| 200 | c->src = SI_SRC_PEER; |
| 201 | si_copy(c->name, q->model, sizeof c->name); |
| 202 | si_copy(c->url, q->url, sizeof c->url); |
| 203 | si_copy(c->label, is_27b(q->model) ? "The 27B model" : q->model, sizeof c->label); |
| 204 | unsigned at = si_copy(c->hint, "On ", sizeof c->hint); |
| 205 | at = si_cat(c->hint, at, q->name[0] ? q->name : "another AienOS computer", sizeof c->hint); |
| 206 | si_cat(c->hint, at, ", nearby", sizeof c->hint); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* the memory a model can use: the best graphics card's, else main memory */ |
| 211 | static uint32_t ai_mib(const sw_wizard *w) |
| 212 | { |
| 213 | int g = si_best_gpu(w->inv); |
| 214 | return g >= 0 ? w->inv->gpu[g].vram_mib : 0; |
| 215 | } |
| 216 | |
| 217 | static int fetch_big(const sw_wizard *w) |
| 218 | { |
| 219 | uint32_t m = ai_mib(w); |
| 220 | return m >= SW_MIB_30B || (!m && w->inv->ram_mib >= SW_MIB_30B * 2); |
| 221 | } |
| 222 | |
| 223 | /* ---- steps ---- */ |
| 224 | |
| 225 | static void render(sw_wizard *w); |
| 226 | |
| 227 | static void go(sw_wizard *w, int step, uint32_t now) |
| 228 | { |
| 229 | if (step == SW_CONSENT) |
| 230 | plan_items(w); |
| 231 | w->step = step; |
| 232 | w->step_t = now; |
| 233 | w->v.lit = 0; |
| 234 | w->listing = step == SW_LLM ? w->listing : 0; |
| 235 | event(w, "step ", sw_step_name(step)); |
| 236 | render(w); |
| 237 | } |
| 238 | |
| 239 | /* after the consent: the paths for what they want, then the AI, then ready */ |
| 240 | static void next_path(sw_wizard *w, int from, uint32_t now) |
| 241 | { |
| 242 | if (from < SW_GAMES && (w->want & SW_WANT_GAMES)) |
| 243 | go(w, SW_GAMES, now); |
| 244 | else if (from < SW_AI && (w->want & SW_WANT_AI)) |
| 245 | go(w, SW_AI, now); |
| 246 | else |
| 247 | go(w, SW_LLM, now); |
| 248 | } |
| 249 | |
| 250 | static void games_lines(sw_wizard *w) |
| 251 | { |
| 252 | const si_display *s = main_screen(w); |
| 253 | char *d = line(w); |
| 254 | unsigned at; |
| 255 | if (s && s->hz && s->max_hz > s->hz) { |
| 256 | at = si_copy(d, "Screen: ", SW_LINE); |
| 257 | at = si_num(d, at, s->hz, SW_LINE); |
| 258 | at = si_cat(d, at, " times a second now, it can do ", SW_LINE); |
| 259 | at = si_num(d, at, s->max_hz, SW_LINE); |
| 260 | si_cat(d, at, ". My graphics driver will set it.", SW_LINE); |
| 261 | } else if (s && s->hz) { |
| 262 | at = si_copy(d, "Screen: ", SW_LINE); |
| 263 | at = si_num(d, at, s->hz, SW_LINE); |
| 264 | si_cat(d, at, " times a second, the most it can do.", SW_LINE); |
| 265 | } else if (s) { |
| 266 | at = si_copy(d, "Screen: ", SW_LINE); |
| 267 | at = si_num(d, at, s->w, SW_LINE); |
| 268 | at = si_cat(d, at, " by ", SW_LINE); |
| 269 | at = si_num(d, at, s->h, SW_LINE); |
| 270 | si_cat(d, at, "; how often it refreshes is not known yet.", SW_LINE); |
| 271 | } else |
| 272 | si_copy(d, "Screen: I can't read your screen's modes yet.", SW_LINE); |
| 273 | int g = si_best_gpu(w->inv); |
| 274 | d = line(w); |
| 275 | if (g < 0) |
| 276 | si_copy(d, "Graphics: none I can use for games here.", SW_LINE); |
| 277 | else { |
| 278 | at = si_copy(d, "Graphics: ", SW_LINE); |
| 279 | at = si_cat(d, at, w->inv->gpu[g].name, SW_LINE); |
| 280 | if (w->inv->gpu[g].driver[0]) { |
| 281 | at = si_cat(d, at, ", driven by ", SW_LINE); |
| 282 | si_cat(d, at, w->inv->gpu[g].driver, SW_LINE); |
| 283 | } else |
| 284 | si_cat(d, at, ", no driver for it yet.", SW_LINE); |
| 285 | } |
| 286 | d = line(w); |
| 287 | const si_cpu *c = &w->inv->cpu; |
| 288 | if (c->fast && c->efficient) { |
| 289 | at = si_copy(d, "Processor: the ", SW_LINE); |
| 290 | at = si_num(d, at, c->fast, SW_LINE); |
| 291 | at = si_cat(d, at, " fast cores stay free for the game, the rest on the ", SW_LINE); |
| 292 | at = si_num(d, at, c->efficient, SW_LINE); |
| 293 | si_cat(d, at, " efficient ones.", SW_LINE); |
| 294 | } else if (c->fast) { |
| 295 | at = si_copy(d, "Processor: I'll keep its ", SW_LINE); |
| 296 | at = si_num(d, at, c->fast, SW_LINE); |
| 297 | si_cat(d, at, " cores free for the game.", SW_LINE); |
| 298 | } else |
| 299 | si_copy(d, "Processor: I'll keep it free for the game.", SW_LINE); |
| 300 | add_line(w, "Frames a second: I'll measure them the first time a game runs. Nothing to do now."); |
| 301 | } |
| 302 | |
| 303 | static void fit_line(sw_wizard *w, const char *what, uint32_t mib, uint32_t mem, int shared) |
| 304 | { |
| 305 | char *d = line(w); |
| 306 | if (!d) |
| 307 | return; |
| 308 | unsigned at = si_copy(d, what, SW_LINE); |
| 309 | at = si_cat(d, at, " needs ", SW_LINE); |
| 310 | at = gb(d, at, mib, SW_LINE); |
| 311 | at = si_cat(d, at, " for its weights: ", SW_LINE); |
| 312 | uint32_t ram = w->inv->ram_mib; |
| 313 | if (mem && mib <= mem) |
| 314 | si_cat(d, at, shared || !mem ? "it fits." : "it fits on the graphics card.", SW_LINE); |
| 315 | else if (mem && !shared && mib <= mem + ram) |
| 316 | si_cat(d, at, "it fits if I share main memory, slower.", SW_LINE); |
| 317 | else if (!mem && ram && mib <= ram) |
| 318 | si_cat(d, at, "it fits in main memory, on the processor.", SW_LINE); |
| 319 | else |
| 320 | si_cat(d, at, "too big for this computer.", SW_LINE); |
| 321 | } |
| 322 | |
| 323 | static void ai_lines(sw_wizard *w) |
| 324 | { |
| 325 | int g = si_best_gpu(w->inv); |
| 326 | uint32_t mem = ai_mib(w); |
| 327 | int shared = g >= 0 && w->inv->gpu[g].kind == SI_GPU_UNIFIED; |
| 328 | char *d = line(w); |
| 329 | unsigned at; |
| 330 | if (g < 0 || !mem) { |
| 331 | at = si_copy(d, g < 0 ? "No graphics card I can use for AI here" : "Your graphics memory is not known yet", |
| 332 | SW_LINE); |
| 333 | si_cat(d, at, "; the processor will run it.", SW_LINE); |
| 334 | } else { |
| 335 | at = si_copy(d, w->inv->gpu[g].name, SW_LINE); |
| 336 | at = si_cat(d, at, shared ? " shares " : " has ", SW_LINE); |
| 337 | at = gb(d, at, mem, SW_LINE); |
| 338 | si_cat(d, at, shared ? " of memory with the processor." : " of its own memory.", SW_LINE); |
| 339 | } |
| 340 | fit_line(w, "The 27B model", SW_MIB_27B, mem, shared); |
| 341 | fit_line(w, "The 30B model", SW_MIB_30B, mem, shared); |
| 342 | fit_line(w, "A small 4B model", SW_MIB_4B, mem, shared); |
| 343 | add_line(w, "The working memory on top of the weights is not measured yet."); |
| 344 | } |
| 345 | |
| 346 | static void llm_view(sw_wizard *w) |
| 347 | { |
| 348 | char s[SW_SAY]; |
| 349 | unsigned at = si_copy(s, (w->want & SW_WANT_AI) ? "" : "I build drivers with an AI. ", SW_SAY); |
| 350 | int connect_ok = w->courier || w->listing != 2; /* offered; without a wire, picking it says why and it goes */ |
| 351 | if (w->ncand == 1 && !w->listing) { |
| 352 | const sw_cand *c = &w->cand[0]; |
| 353 | at = si_cat(s, at, "I found ", SW_SAY); |
| 354 | at = si_cat(s, at, c->label, SW_SAY); |
| 355 | at = si_cat(s, at, c->kind == SW_LLM_PEER ? " nearby. I'll use it." : " on this computer. I'll use it.", SW_SAY); |
| 356 | say(w, s); |
| 357 | add_line(w, c->hint); |
| 358 | choice(w, "Use it"); |
| 359 | choice(w, "Choose another"); |
| 360 | si_copy(w->v.aside, "I'll use it in 10 seconds unless you choose", sizeof w->v.aside); |
| 361 | si_copy(w->v.hint, w->v.lit == 0 ? c->hint : "See everything I found, or connect your own", SW_LINE); |
| 362 | return; |
| 363 | } |
| 364 | if (w->ncand) { |
| 365 | if (w->ncand == 1) |
| 366 | at = si_cat(s, at, "Here's what I found.", SW_SAY); |
| 367 | else { |
| 368 | at = si_cat(s, at, "I found ", SW_SAY); |
| 369 | at = si_num(s, at, w->ncand, SW_SAY); |
| 370 | at = si_cat(s, at, " AI models. The first one fits best.", SW_SAY); |
| 371 | } |
| 372 | if (w->listing == 2) |
| 373 | at = si_cat(s, at, " I can't keep a key safely here yet. Set it up on the host instead.", SW_SAY); |
| 374 | say(w, s); |
| 375 | unsigned shown = w->ncand < SW_CHOICES - 1 ? w->ncand : SW_CHOICES - 1; |
| 376 | for (unsigned i = 0; i < shown; i++) |
| 377 | choice(w, w->cand[i].label); |
| 378 | if (connect_ok) |
| 379 | choice(w, "Connect my own"); |
| 380 | if ((unsigned)w->v.lit < shown) |
| 381 | si_copy(w->v.hint, w->cand[w->v.lit].hint, SW_LINE); |
| 382 | else |
| 383 | si_copy(w->v.hint, "Claude, ChatGPT, or another service", SW_LINE); |
| 384 | } else { |
| 385 | at = si_cat(s, at, "I didn't find an AI model on this computer or nearby.", SW_SAY); |
| 386 | if (w->listing == 2) |
| 387 | at = si_cat(s, at, " I can't keep a key safely here yet. Set it up on the host instead.", SW_SAY); |
| 388 | say(w, s); |
| 389 | choice(w, "Get one for me"); |
| 390 | if (connect_ok) |
| 391 | choice(w, "Connect my own"); |
| 392 | if (w->v.lit == 0) |
| 393 | si_copy(w->v.hint, fetch_big(w) ? "The 30B model, a 29 GB download" : "The small 4B model, an 8 GB download", |
| 394 | SW_LINE); |
| 395 | else |
| 396 | si_copy(w->v.hint, "Claude, ChatGPT, or another service", SW_LINE); |
| 397 | } |
| 398 | si_copy(w->v.aside, "Not now: Escape", sizeof w->v.aside); |
| 399 | } |
| 400 | |
| 401 | static const char *service_name(int s) { return s == SW_LLM_CLAUDE ? "Claude" : s == SW_LLM_OPENAI ? "ChatGPT" : "that service"; } |
| 402 | |
| 403 | static void ready_lines(sw_wizard *w) |
| 404 | { |
| 405 | char *d; |
| 406 | unsigned at; |
| 407 | if ((w->consent & 1u << SW_I_SCREEN) && main_screen(w)) { |
| 408 | d = line(w); |
| 409 | at = si_copy(d, "Games will use your screen at ", SW_LINE); |
| 410 | at = si_num(d, at, main_screen(w)->max_hz, SW_LINE); |
| 411 | si_cat(d, at, " times a second once my graphics driver is in.", SW_LINE); |
| 412 | } |
| 413 | d = line(w); |
| 414 | switch (w->llm_kind) { |
| 415 | case SW_LLM_LOCAL: |
| 416 | case SW_LLM_PEER: |
| 417 | at = si_copy(d, "Your AI is ", SW_LINE); |
| 418 | at = si_cat(d, at, w->llm_name, SW_LINE); |
| 419 | si_cat(d, at, w->llm_kind == SW_LLM_PEER ? ", on a computer nearby." : ", on this computer.", SW_LINE); |
| 420 | break; |
| 421 | case SW_LLM_CLAUDE: |
| 422 | case SW_LLM_OPENAI: |
| 423 | case SW_LLM_OTHER: |
| 424 | at = si_copy(d, "Your AI is ", SW_LINE); |
| 425 | at = si_cat(d, at, w->llm_kind == SW_LLM_OTHER ? "your own service" : service_name(w->llm_kind), SW_LINE); |
| 426 | si_cat(d, at, ", through your key. It works.", SW_LINE); |
| 427 | break; |
| 428 | case SW_LLM_FETCH: |
| 429 | at = si_copy(d, "I'll get ", SW_LINE); |
| 430 | at = si_cat(d, at, w->llm_name, SW_LINE); |
| 431 | si_cat(d, at, " when this computer can download it.", SW_LINE); |
| 432 | break; |
| 433 | default: |
| 434 | si_copy(d, "No AI yet. Tell me when you'd like one.", SW_LINE); |
| 435 | } |
| 436 | if (w->consent & 1u << SW_I_BUILD) |
| 437 | add_line(w, "Next I'll check every part and build drivers for the ones I can't drive yet."); |
| 438 | else if (w->consent_asked < 0 || !w->consent) |
| 439 | add_line(w, "Nothing was changed. Say set up again any time."); |
| 440 | add_line(w, "Ask me anything, or tell me what you'd like to do."); |
| 441 | } |
| 442 | |
| 443 | static void render(sw_wizard *w) |
| 444 | { |
| 445 | sw_view *v = &w->v; |
| 446 | char buf[SW_SAY]; |
| 447 | unsigned at; |
| 448 | v->nline = 0; |
| 449 | v->nchoice = 0; |
| 450 | v->hint[0] = v->aside[0] = v->prompt[0] = 0; |
| 451 | v->ntyped = 0; |
| 452 | v->typed[0] = 0; |
| 453 | switch (w->step) { |
| 454 | case SW_HELLO: |
| 455 | say(w, w->resumed ? "Welcome back. Let's carry on where we stopped." |
| 456 | : "Hello. I'm Allen. I'm going to look after this computer with you."); |
| 457 | break; |
| 458 | case SW_INTENT: |
| 459 | say(w, "What would you like this computer to do best?"); |
| 460 | choice(w, "Play games smoothly"); |
| 461 | choice(w, "Run an AI model here"); |
| 462 | choice(w, "Something else"); |
| 463 | si_copy(v->hint, |
| 464 | v->lit == 0 ? "Faster frames, a steady picture, the most from your screen and graphics." |
| 465 | : v->lit == 1 ? "Use the graphics and memory in this computer to run an AI that stays on it." |
| 466 | : "Tell me in your own words.", |
| 467 | SW_LINE); |
| 468 | si_copy(v->aside, "Not now: Escape", sizeof v->aside); |
| 469 | break; |
| 470 | case SW_OTHER: |
| 471 | say(w, "Tell me what you'd like to do."); |
| 472 | si_copy(v->prompt, "In your own words", sizeof v->prompt); |
| 473 | v->ntyped = si_copy(v->typed, w->text, SW_TEXT); |
| 474 | break; |
| 475 | case SW_OTHER_CHECK: |
| 476 | if ((w->want & (SW_WANT_GAMES | SW_WANT_AI)) == (SW_WANT_GAMES | SW_WANT_AI)) |
| 477 | say(w, "You want smoother games and an AI here. I'll set up both."); |
| 478 | else if (w->want & SW_WANT_GAMES) |
| 479 | say(w, "You want smoother games. I'll focus on your screen, graphics and processor."); |
| 480 | else if (w->want & SW_WANT_AI) |
| 481 | say(w, "You want to run an AI here. I'll set up the graphics and memory for it."); |
| 482 | else { |
| 483 | at = si_copy(buf, "You'd like this: ", SW_SAY); |
| 484 | char wish[72]; |
| 485 | si_copy(wish, w->wish, sizeof wish); |
| 486 | at = si_cat(buf, at, wish, SW_SAY); |
| 487 | si_cat(buf, at, ". I'll keep everything else simple.", SW_SAY); |
| 488 | say(w, buf); |
| 489 | } |
| 490 | choice(w, "That's right"); |
| 491 | choice(w, "Not quite"); |
| 492 | break; |
| 493 | case SW_SCAN: { |
| 494 | say(w, (w->want & ~SW_WANT_ASKED) || w->resumed ? "Getting to know this computer." |
| 495 | : "That's fine. I'll set up the basics first. Getting to know " |
| 496 | "this computer."); |
| 497 | char found[SW_LINES][96]; |
| 498 | unsigned n = si_summary(w->inv, found, SW_LINES); |
| 499 | for (unsigned i = 0; i < n; i++) |
| 500 | add_line(w, found[i]); |
| 501 | if (n <= 1 && !w->inv->ndisplay && !w->inv->cpu.fast) |
| 502 | add_line(w, "I couldn't find much yet. I'll keep looking while you use it."); |
| 503 | break; |
| 504 | } |
| 505 | case SW_CONSENT: |
| 506 | say(w, "Here's what I'd like to set up:"); |
| 507 | for (unsigned i = 0; i < w->nitem; i++) { |
| 508 | char *d = line(w); |
| 509 | if (d) |
| 510 | item_text(w, w->item[i], d, SW_LINE); |
| 511 | } |
| 512 | add_line(w, "Nothing on your other drives is changed. You can undo any of this."); |
| 513 | choice(w, "Set it up"); |
| 514 | choice(w, "Show me each one"); |
| 515 | choice(w, "Not now"); |
| 516 | break; |
| 517 | case SW_EACH: { |
| 518 | say(w, "Shall I do this?"); |
| 519 | char *d = line(w); |
| 520 | item_text(w, w->item[w->each], d, SW_LINE); |
| 521 | choice(w, "Yes"); |
| 522 | choice(w, "Skip"); |
| 523 | si_copy(v->aside, "Skip the rest: Escape", sizeof v->aside); |
| 524 | break; |
| 525 | } |
| 526 | case SW_GAMES: |
| 527 | say(w, "Making games smoother."); |
| 528 | games_lines(w); |
| 529 | choice(w, "Carry on"); |
| 530 | break; |
| 531 | case SW_AI: |
| 532 | say(w, "Finding the best way to run an AI here."); |
| 533 | ai_lines(w); |
| 534 | choice(w, "Carry on"); |
| 535 | break; |
| 536 | case SW_LLM: |
| 537 | llm_view(w); |
| 538 | break; |
| 539 | case SW_SERVICE: |
| 540 | say(w, "Which service?"); |
| 541 | choice(w, "Claude"); |
| 542 | choice(w, "ChatGPT"); |
| 543 | choice(w, "Another service"); |
| 544 | si_copy(v->aside, "Back: Escape", sizeof v->aside); |
| 545 | break; |
| 546 | case SW_URL: |
| 547 | say(w, "What's your service's web address? It starts with https:// and its settings page shows it."); |
| 548 | si_copy(v->prompt, "Its web address", sizeof v->prompt); |
| 549 | v->ntyped = si_copy(v->typed, w->text, SW_TEXT); |
| 550 | if (w->why[0]) |
| 551 | add_line(w, w->why); |
| 552 | break; |
| 553 | case SW_KEY: |
| 554 | at = si_copy(buf, "Type your key for ", SW_SAY); |
| 555 | at = si_cat(buf, at, service_name(w->service), SW_SAY); |
| 556 | at = si_cat(buf, at, w->service == SW_LLM_OTHER ? ", or press Enter if it needs none." : ".", SW_SAY); |
| 557 | si_cat(buf, at, " I test it once and never show it again; it is not written to this disk.", SW_SAY); |
| 558 | say(w, buf); |
| 559 | at = si_copy(v->prompt, "Key for ", sizeof v->prompt); |
| 560 | si_cat(v->prompt, at, service_name(w->service), sizeof v->prompt); |
| 561 | for (unsigned i = 0; i < w->nkey && i + 1 < SW_TEXT; i++) |
| 562 | v->typed[i] = '*'; |
| 563 | v->ntyped = w->nkey < SW_TEXT - 1 ? w->nkey : SW_TEXT - 1; |
| 564 | v->typed[v->ntyped] = 0; |
| 565 | if (w->why[0]) |
| 566 | add_line(w, w->why); |
| 567 | break; |
| 568 | case SW_TESTING: |
| 569 | say(w, "Testing it with one short request."); |
| 570 | si_copy(v->aside, "Stop: Escape", sizeof v->aside); |
| 571 | break; |
| 572 | case SW_TEST_FAILED: |
| 573 | at = si_copy(buf, "That didn't work. ", SW_SAY); |
| 574 | if (w->why[0]) { |
| 575 | at = si_cat(buf, at, "The service said: ", SW_SAY); |
| 576 | at = si_cat(buf, at, w->why, SW_SAY); |
| 577 | at = si_cat(buf, at, ". ", SW_SAY); |
| 578 | } |
| 579 | si_cat(buf, at, "Try again?", SW_SAY); |
| 580 | say(w, buf); |
| 581 | choice(w, "Try again"); |
| 582 | choice(w, "Choose another"); |
| 583 | break; |
| 584 | case SW_READY: |
| 585 | say(w, "Allen is ready."); |
| 586 | ready_lines(w); |
| 587 | choice(w, "Start"); |
| 588 | break; |
| 589 | default: |
| 590 | say(w, ""); |
| 591 | break; |
| 592 | } |
| 593 | if (v->nchoice && (unsigned)v->lit >= v->nchoice) |
| 594 | v->lit = 0; |
| 595 | v->gen++; |
| 596 | } |
| 597 | |
| 598 | /* ---- picking ---- */ |
| 599 | |
| 600 | static void classify(sw_wizard *w) |
| 601 | { |
| 602 | char low[SW_TEXT + 2]; |
| 603 | unsigned n = 0; |
| 604 | low[n++] = ' '; |
| 605 | for (unsigned i = 0; w->wish[i] && n < SW_TEXT; i++) { |
| 606 | char c = w->wish[i]; |
| 607 | low[n++] = c >= 'A' && c <= 'Z' ? (char)(c - 'A' + 'a') : c >= 'a' && c <= 'z' ? c : c >= '0' && c <= '9' ? c : ' '; |
| 608 | } |
| 609 | low[n++] = ' '; |
| 610 | low[n] = 0; |
| 611 | static const char *const games[] = {"game", "gaming", "fps", "frame", "play", "steam", "esport", 0}; |
| 612 | static const char *const ai[] = {" ai ", "model", "llm", " chat", "gpt", "claude", "assistant", "agent", 0}; |
| 613 | unsigned want = 0; |
| 614 | for (unsigned i = 0; i < n; i++) { |
| 615 | for (unsigned k = 0; games[k]; k++) |
| 616 | if (starts(low + i, games[k])) |
| 617 | want |= SW_WANT_GAMES; |
| 618 | for (unsigned k = 0; ai[k]; k++) |
| 619 | if (starts(low + i, ai[k])) |
| 620 | want |= SW_WANT_AI; |
| 621 | } |
| 622 | w->want = SW_WANT_ASKED | (want ? want : SW_WANT_OTHER); |
| 623 | } |
| 624 | |
| 625 | static void intent_event(sw_wizard *w) |
| 626 | { |
| 627 | unsigned x = w->want; |
| 628 | event(w, "intent ", |
| 629 | (x & SW_WANT_GAMES) && (x & SW_WANT_AI) ? "games+ai" |
| 630 | : x & SW_WANT_GAMES ? "games" |
| 631 | : x & SW_WANT_AI ? "ai" |
| 632 | : x & SW_WANT_OTHER ? "other" |
| 633 | : "not now"); |
| 634 | } |
| 635 | |
| 636 | static void pick_llm(sw_wizard *w, unsigned kind, const char *name, const char *url, uint32_t now) |
| 637 | { |
| 638 | w->llm_kind = (uint8_t)kind; |
| 639 | si_copy(w->llm_name, name, sizeof w->llm_name); |
| 640 | si_copy(w->llm_url, url, sizeof w->llm_url); |
| 641 | event(w, |
| 642 | kind == SW_LLM_LOCAL ? "llm picked local " |
| 643 | : kind == SW_LLM_PEER ? "llm picked peer " |
| 644 | : kind == SW_LLM_FETCH ? "llm picked fetch " |
| 645 | : "llm picked service ", |
| 646 | name); |
| 647 | go(w, SW_READY, now); |
| 648 | } |
| 649 | |
| 650 | static void connect_own(sw_wizard *w, uint32_t now) |
| 651 | { |
| 652 | if (!w->courier) { /* nowhere safe for a key: say so, offer the rest */ |
| 653 | w->listing = 2; |
| 654 | event(w, "connect refused: no wire to keep a key", 0); |
| 655 | render(w); |
| 656 | return; |
| 657 | } |
| 658 | go(w, SW_SERVICE, now); |
| 659 | } |
| 660 | |
| 661 | static void choose(sw_wizard *w, unsigned i, uint32_t now) |
| 662 | { |
| 663 | switch (w->step) { |
| 664 | case SW_INTENT: |
| 665 | if (i == 2) { |
| 666 | w->ntext = 0; |
| 667 | w->text[0] = 0; |
| 668 | go(w, SW_OTHER, now); |
| 669 | return; |
| 670 | } |
| 671 | w->want = SW_WANT_ASKED | (i == 0 ? SW_WANT_GAMES : SW_WANT_AI); |
| 672 | intent_event(w); |
| 673 | go(w, SW_SCAN, now); |
| 674 | return; |
| 675 | case SW_OTHER_CHECK: |
| 676 | if (i == 0) { |
| 677 | intent_event(w); |
| 678 | go(w, SW_SCAN, now); |
| 679 | } else { |
| 680 | w->ntext = 0; |
| 681 | w->text[0] = 0; |
| 682 | go(w, SW_OTHER, now); |
| 683 | } |
| 684 | return; |
| 685 | case SW_CONSENT: |
| 686 | if (i == 0) { |
| 687 | for (unsigned k = 0; k < w->nitem; k++) |
| 688 | w->consent |= 1u << w->item[k]; |
| 689 | w->consent_asked = 1; |
| 690 | event(w, "consent all", 0); |
| 691 | next_path(w, SW_CONSENT, now); |
| 692 | } else if (i == 1) { |
| 693 | w->each = 0; |
| 694 | event(w, "consent each", 0); |
| 695 | go(w, SW_EACH, now); |
| 696 | } else { |
| 697 | w->consent_asked = -1; |
| 698 | event(w, "consent not now", 0); |
| 699 | next_path(w, SW_CONSENT, now); |
| 700 | } |
| 701 | return; |
| 702 | case SW_EACH: { |
| 703 | unsigned it = w->item[w->each]; |
| 704 | if (i == 0) |
| 705 | w->consent |= 1u << it; |
| 706 | event(w, i == 0 ? "consent yes " : "consent skip ", item_id[it]); |
| 707 | if (++w->each >= w->nitem) { |
| 708 | w->consent_asked = 1; |
| 709 | next_path(w, SW_CONSENT, now); |
| 710 | } else { |
| 711 | w->v.lit = 0; |
| 712 | render(w); |
| 713 | } |
| 714 | return; |
| 715 | } |
| 716 | case SW_GAMES: |
| 717 | case SW_AI: |
| 718 | next_path(w, w->step, now); |
| 719 | return; |
| 720 | case SW_LLM: |
| 721 | if (w->ncand == 1 && !w->listing) { |
| 722 | if (i == 0) |
| 723 | pick_llm(w, w->cand[0].kind, w->cand[0].name, w->cand[0].url, now); |
| 724 | else { |
| 725 | w->listing = 1; |
| 726 | w->v.lit = 0; |
| 727 | render(w); |
| 728 | } |
| 729 | return; |
| 730 | } |
| 731 | if (w->ncand) { |
| 732 | unsigned shown = w->ncand < SW_CHOICES - 1 ? w->ncand : SW_CHOICES - 1; |
| 733 | if (i < shown) |
| 734 | pick_llm(w, w->cand[i].kind, w->cand[i].name, w->cand[i].url, now); |
| 735 | else |
| 736 | connect_own(w, now); |
| 737 | return; |
| 738 | } |
| 739 | if (i == 0) |
| 740 | pick_llm(w, SW_LLM_FETCH, fetch_big(w) ? "the 30B model" : "the small 4B model", "", now); |
| 741 | else |
| 742 | connect_own(w, now); |
| 743 | return; |
| 744 | case SW_SERVICE: |
| 745 | w->service = i == 0 ? SW_LLM_CLAUDE : i == 1 ? SW_LLM_OPENAI : SW_LLM_OTHER; |
| 746 | w->why[0] = 0; |
| 747 | w->ntext = 0; |
| 748 | w->text[0] = 0; |
| 749 | sw_forget_key(w); |
| 750 | if (w->service == SW_LLM_OTHER) |
| 751 | go(w, SW_URL, now); |
| 752 | else { |
| 753 | si_copy(w->llm_url, w->service == SW_LLM_CLAUDE ? "https://api.anthropic.com" : "https://api.openai.com", |
| 754 | sizeof w->llm_url); |
| 755 | go(w, SW_KEY, now); |
| 756 | } |
| 757 | return; |
| 758 | case SW_TEST_FAILED: |
| 759 | w->why[0] = 0; |
| 760 | if (i == 0) |
| 761 | go(w, SW_KEY, now); |
| 762 | else { |
| 763 | w->listing = 1; |
| 764 | go(w, SW_LLM, now); |
| 765 | } |
| 766 | return; |
| 767 | case SW_READY: |
| 768 | event(w, "done", 0); |
| 769 | go(w, SW_DONE, now); |
| 770 | return; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /* Escape: "Not now" on a question, "Back" on the steps inside one */ |
| 775 | static void not_now(sw_wizard *w, uint32_t now) |
| 776 | { |
| 777 | switch (w->step) { |
| 778 | case SW_HELLO: |
| 779 | case SW_SCAN: |
| 780 | go(w, w->step == SW_HELLO ? SW_INTENT : SW_CONSENT, now); |
| 781 | return; |
| 782 | case SW_INTENT: |
| 783 | w->want = SW_WANT_ASKED; |
| 784 | intent_event(w); |
| 785 | go(w, SW_SCAN, now); |
| 786 | return; |
| 787 | case SW_OTHER: |
| 788 | case SW_OTHER_CHECK: |
| 789 | go(w, SW_INTENT, now); |
| 790 | return; |
| 791 | case SW_CONSENT: |
| 792 | choose(w, 2, now); |
| 793 | return; |
| 794 | case SW_EACH: |
| 795 | while (w->each < w->nitem) |
| 796 | event(w, "consent skip ", item_id[w->item[w->each++]]); |
| 797 | w->consent_asked = 1; |
| 798 | next_path(w, SW_CONSENT, now); |
| 799 | return; |
| 800 | case SW_GAMES: |
| 801 | case SW_AI: |
| 802 | next_path(w, w->step, now); |
| 803 | return; |
| 804 | case SW_LLM: |
| 805 | w->llm_kind = SW_LLM_NONE; |
| 806 | event(w, "llm skipped", 0); |
| 807 | go(w, SW_READY, now); |
| 808 | return; |
| 809 | case SW_SERVICE: |
| 810 | go(w, SW_LLM, now); |
| 811 | return; |
| 812 | case SW_URL: |
| 813 | case SW_KEY: |
| 814 | case SW_TESTING: |
| 815 | case SW_TEST_FAILED: |
| 816 | sw_forget_key(w); |
| 817 | w->test_out = 0; |
| 818 | w->why[0] = 0; |
| 819 | go(w, SW_SERVICE, now); |
| 820 | return; |
| 821 | case SW_READY: |
| 822 | choose(w, 0, now); |
| 823 | return; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static int typing(int step) { return step == SW_OTHER || step == SW_URL || step == SW_KEY; } |
| 828 | |
| 829 | static void submit(sw_wizard *w, uint32_t now) |
| 830 | { |
| 831 | switch (w->step) { |
| 832 | case SW_OTHER: |
| 833 | if (!w->ntext) |
| 834 | return; |
| 835 | si_copy(w->wish, w->text, sizeof w->wish); |
| 836 | classify(w); |
| 837 | go(w, SW_OTHER_CHECK, now); |
| 838 | return; |
| 839 | case SW_URL: |
| 840 | if (!starts(w->text, "http://") && !starts(w->text, "https://")) { |
| 841 | si_copy(w->why, "That doesn't look like a web address. It starts with http:// or https://.", SW_LINE); |
| 842 | render(w); |
| 843 | return; |
| 844 | } |
| 845 | si_copy(w->llm_url, w->text, sizeof w->llm_url); |
| 846 | w->why[0] = 0; |
| 847 | go(w, SW_KEY, now); |
| 848 | return; |
| 849 | case SW_KEY: |
| 850 | if (!w->nkey && w->service != SW_LLM_OTHER) { |
| 851 | si_copy(w->why, "I need the key to connect. Type it, or press Escape to go back.", SW_LINE); |
| 852 | render(w); |
| 853 | return; |
| 854 | } |
| 855 | w->why[0] = 0; |
| 856 | w->test_out = 1; |
| 857 | event(w, "key test wanted ", w->service == SW_LLM_CLAUDE ? "claude" : w->service == SW_LLM_OPENAI ? "openai" : "other"); |
| 858 | go(w, SW_TESTING, now); |
| 859 | return; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | int sw_key(sw_wizard *w, int key, char c, uint32_t now) |
| 864 | { |
| 865 | unsigned gen = w->v.gen; |
| 866 | if (w->step == SW_DONE) |
| 867 | return 0; |
| 868 | if (typing(w->step)) { |
| 869 | char *buf = w->step == SW_KEY ? w->key : w->text; |
| 870 | unsigned *n = w->step == SW_KEY ? &w->nkey : &w->ntext; |
| 871 | if (key == SW_K_CHAR && c >= ' ' && c < 127 && *n + 1 < SW_TEXT) { |
| 872 | buf[(*n)++] = c; |
| 873 | buf[*n] = 0; |
| 874 | render(w); |
| 875 | } else if (key == SW_K_BACK && *n) { |
| 876 | buf[--(*n)] = 0; |
| 877 | render(w); |
| 878 | } else if (key == SW_K_ENTER) |
| 879 | submit(w, now); |
| 880 | else if (key == SW_K_ESC) |
| 881 | not_now(w, now); |
| 882 | return w->v.gen != gen; |
| 883 | } |
| 884 | unsigned nc = w->v.nchoice; |
| 885 | switch (key) { |
| 886 | case SW_K_LEFT: |
| 887 | case SW_K_UP: |
| 888 | if (nc) { |
| 889 | w->v.lit = (w->v.lit + (int)nc - 1) % (int)nc; |
| 890 | render(w); |
| 891 | } |
| 892 | break; |
| 893 | case SW_K_RIGHT: |
| 894 | case SW_K_DOWN: |
| 895 | case SW_K_TAB: |
| 896 | if (nc) { |
| 897 | w->v.lit = (w->v.lit + 1) % (int)nc; |
| 898 | render(w); |
| 899 | } |
| 900 | break; |
| 901 | case SW_K_ENTER: |
| 902 | if (nc) |
| 903 | choose(w, (unsigned)w->v.lit, now); |
| 904 | else if (w->step == SW_HELLO || w->step == SW_SCAN) |
| 905 | not_now(w, now); /* Enter moves on from a screen that moves on by itself */ |
| 906 | break; |
| 907 | case SW_K_ESC: |
| 908 | not_now(w, now); |
| 909 | break; |
| 910 | case SW_K_CHAR: |
| 911 | if (c >= '1' && c <= '9' && (unsigned)(c - '1') < nc) /* a choice by its number */ |
| 912 | choose(w, (unsigned)(c - '1'), now); |
| 913 | else if (w->step == SW_INTENT && c > ' ' && c < 127) { /* typing on the question: their own words */ |
| 914 | w->text[0] = c; |
| 915 | w->text[1] = 0; |
| 916 | w->ntext = 1; |
| 917 | go(w, SW_OTHER, now); |
| 918 | render(w); |
| 919 | } |
| 920 | break; |
| 921 | } |
| 922 | return w->v.gen != gen; |
| 923 | } |
| 924 | |
| 925 | int sw_tick(sw_wizard *w, uint32_t now) |
| 926 | { |
| 927 | unsigned gen = w->v.gen; |
| 928 | uint32_t in = now - w->step_t; |
| 929 | switch (w->step) { |
| 930 | case SW_HELLO: |
| 931 | if (in >= (w->resumed ? BACK_MS : HELLO_MS)) { |
| 932 | if (w->resumed) { |
| 933 | int to = w->resumed; |
| 934 | w->resumed = 0; |
| 935 | go(w, to, now); |
| 936 | } else |
| 937 | go(w, SW_INTENT, now); |
| 938 | } |
| 939 | break; |
| 940 | case SW_SCAN: |
| 941 | if (in >= w->v.nline * SCAN_LINE_MS + SCAN_REST_MS) { |
| 942 | go(w, SW_CONSENT, now); |
| 943 | } |
| 944 | break; |
| 945 | case SW_LLM: |
| 946 | if (w->ncand == 1 && !w->listing && in >= AUTO_PICK_MS) { |
| 947 | event(w, "llm auto-picked after 10 s", 0); |
| 948 | pick_llm(w, w->cand[0].kind, w->cand[0].name, w->cand[0].url, now); |
| 949 | } |
| 950 | break; |
| 951 | case SW_TESTING: |
| 952 | if (w->test_out == 2 && now - w->test_t >= TEST_MS) |
| 953 | sw_test_done(w, 0, "no answer came back", now); |
| 954 | break; |
| 955 | case SW_READY: |
| 956 | if (in >= READY_MS) |
| 957 | choose(w, 0, now); |
| 958 | break; |
| 959 | } |
| 960 | return w->v.gen != gen; |
| 961 | } |
| 962 | |
| 963 | int sw_test_wanted(sw_wizard *w, const char **kind, const char **base_url, const char **model, const char **key) |
| 964 | { |
| 965 | if (w->test_out != 1 || w->step != SW_TESTING) |
| 966 | return 0; |
| 967 | *kind = w->service == SW_LLM_CLAUDE ? "anthropic" : "openai"; |
| 968 | *base_url = w->llm_url; |
| 969 | *model = "auto"; |
| 970 | *key = w->key; |
| 971 | w->test_out = 2; |
| 972 | w->test_t = w->step_t; |
| 973 | return 1; |
| 974 | } |
| 975 | |
| 976 | void sw_test_done(sw_wizard *w, int ok, const char *why, uint32_t now) |
| 977 | { |
| 978 | if (w->step != SW_TESTING || w->test_out != 2) |
| 979 | return; |
| 980 | w->test_out = 0; |
| 981 | sw_forget_key(w); |
| 982 | if (ok) { |
| 983 | event(w, "key test ok", 0); |
| 984 | pick_llm(w, (unsigned)w->service, |
| 985 | w->service == SW_LLM_CLAUDE ? "Claude" : w->service == SW_LLM_OPENAI ? "ChatGPT" : "your own service", |
| 986 | w->llm_url, now); |
| 987 | return; |
| 988 | } |
| 989 | event(w, "key test failed", 0); |
| 990 | si_copy(w->why, why ? why : "", SW_LINE); |
| 991 | go(w, SW_TEST_FAILED, now); |
| 992 | } |
| 993 | |
| 994 | void sw_forget_key(sw_wizard *w) |
| 995 | { |
| 996 | zero(w->key, sizeof w->key); |
| 997 | w->nkey = 0; |
| 998 | } |
| 999 | |
| 1000 | const sw_view *sw_view_of(const sw_wizard *w) { return &w->v; } |
| 1001 | int sw_step_of(const sw_wizard *w) { return w->step; } |
| 1002 | |
| 1003 | /* ---- records ---- */ |
| 1004 | |
| 1005 | static unsigned kv_num(char *d, unsigned at, const char *k, uint32_t v, unsigned cap) |
| 1006 | { |
| 1007 | at = si_cat(d, at, k, cap); |
| 1008 | at = si_cat(d, at, " ", cap); |
| 1009 | at = si_num(d, at, v, cap); |
| 1010 | return si_cat(d, at, "\n", cap); |
| 1011 | } |
| 1012 | |
| 1013 | static unsigned kv_str(char *d, unsigned at, const char *k, const char *v, unsigned cap) |
| 1014 | { |
| 1015 | at = si_cat(d, at, k, cap); |
| 1016 | at = si_cat(d, at, " ", cap); |
| 1017 | at = si_cat(d, at, v[0] ? v : "-", cap); |
| 1018 | return si_cat(d, at, "\n", cap); |
| 1019 | } |
| 1020 | |
| 1021 | /* fits: the whole record, or 0 */ |
| 1022 | static unsigned whole(char *out, unsigned at, unsigned cap) |
| 1023 | { |
| 1024 | if (at + 1 >= cap) { |
| 1025 | out[0] = 0; |
| 1026 | return 0; |
| 1027 | } |
| 1028 | return at; |
| 1029 | } |
| 1030 | |
| 1031 | unsigned sw_save(const sw_wizard *w, char *out, unsigned cap) |
| 1032 | { |
| 1033 | if (!cap) |
| 1034 | return 0; |
| 1035 | unsigned at = si_copy(out, "aienos-setup v1\n", cap); |
| 1036 | at = kv_num(out, at, "step", (uint32_t)w->step, cap); |
| 1037 | at = kv_num(out, at, "want", w->want, cap); |
| 1038 | at = kv_num(out, at, "consent", w->consent, cap); |
| 1039 | at = kv_num(out, at, "asked", (uint32_t)(w->consent_asked + 1), cap); |
| 1040 | at = kv_num(out, at, "llm", w->llm_kind, cap); |
| 1041 | at = kv_str(out, at, "name", w->llm_name, cap); |
| 1042 | at = kv_str(out, at, "url", w->llm_url, cap); |
| 1043 | return whole(out, at, cap); |
| 1044 | } |
| 1045 | |
| 1046 | unsigned sw_llm_conf(const sw_wizard *w, char *out, unsigned cap) |
| 1047 | { |
| 1048 | if (!cap) |
| 1049 | return 0; |
| 1050 | out[0] = 0; |
| 1051 | unsigned at = 0; |
| 1052 | switch (w->llm_kind) { |
| 1053 | case SW_LLM_LOCAL: |
| 1054 | case SW_LLM_PEER: |
| 1055 | if (!w->llm_url[0]) |
| 1056 | return 0; /* a file nothing serves yet: nothing to call */ |
| 1057 | at = kv_str(out, at, "kind", "openai", cap); |
| 1058 | at = kv_str(out, at, "base_url", w->llm_url, cap); |
| 1059 | at = kv_str(out, at, "model", w->llm_name, cap); |
| 1060 | at = kv_str(out, at, "key_env", "none", cap); |
| 1061 | break; |
| 1062 | case SW_LLM_CLAUDE: |
| 1063 | at = kv_str(out, at, "kind", "anthropic", cap); |
| 1064 | at = kv_str(out, at, "base_url", w->llm_url, cap); |
| 1065 | at = kv_str(out, at, "model", "auto", cap); |
| 1066 | at = kv_str(out, at, "key_env", "ANTHROPIC_API_KEY", cap); |
| 1067 | break; |
| 1068 | case SW_LLM_OPENAI: |
| 1069 | case SW_LLM_OTHER: |
| 1070 | at = kv_str(out, at, "kind", "openai", cap); |
| 1071 | at = kv_str(out, at, "base_url", w->llm_url, cap); |
| 1072 | at = kv_str(out, at, "model", "auto", cap); |
| 1073 | at = kv_str(out, at, "key_env", w->llm_kind == SW_LLM_OPENAI ? "OPENAI_API_KEY" : "AIENOS_LLM_KEY", cap); |
| 1074 | break; |
| 1075 | default: |
| 1076 | return 0; |
| 1077 | } |
| 1078 | return whole(out, at, cap); |
| 1079 | } |
| 1080 | |
| 1081 | unsigned sw_consent(const sw_wizard *w, char *out, unsigned cap) |
| 1082 | { |
| 1083 | if (!cap) |
| 1084 | return 0; |
| 1085 | unsigned at = si_copy(out, "aienos-consent v1\n", cap); |
| 1086 | for (unsigned i = 0; i < w->nitem; i++) { |
| 1087 | at = si_cat(out, at, item_id[w->item[i]], cap); |
| 1088 | at = si_cat(out, at, w->consent & 1u << w->item[i] ? " yes\n" : " no\n", cap); |
| 1089 | } |
| 1090 | return whole(out, at, cap); |
| 1091 | } |
| 1092 | |
| 1093 | /* the value after "key " on its own line of a record, copied to v (cap bytes); 0 if absent */ |
| 1094 | static int field(const char *s, unsigned n, const char *key, char *v, unsigned cap) |
| 1095 | { |
| 1096 | unsigned kl = si_len(key); |
| 1097 | for (unsigned i = 0; i < n;) { |
| 1098 | unsigned e = i; |
| 1099 | while (e < n && s[e] != '\n') |
| 1100 | e++; |
| 1101 | if (e - i > kl && s[i + kl] == ' ') { |
| 1102 | unsigned k = 0; |
| 1103 | while (k < kl && s[i + k] == key[k]) |
| 1104 | k++; |
| 1105 | if (k == kl) { |
| 1106 | unsigned m = 0; |
| 1107 | for (unsigned j = i + kl + 1; j < e && m + 1 < cap; j++) |
| 1108 | v[m++] = s[j]; |
| 1109 | v[m] = 0; |
| 1110 | if (m == 1 && v[0] == '-') |
| 1111 | v[0] = 0; |
| 1112 | return 1; |
| 1113 | } |
| 1114 | } |
| 1115 | i = e + 1; |
| 1116 | } |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int field_num(const char *s, unsigned n, const char *key, uint32_t *out) |
| 1121 | { |
| 1122 | char v[12]; |
| 1123 | if (!field(s, n, key, v, sizeof v) || !v[0]) |
| 1124 | return 0; |
| 1125 | uint32_t x = 0; |
| 1126 | for (unsigned i = 0; v[i]; i++) { |
| 1127 | if (v[i] < '0' || v[i] > '9' || x > 100000000u) |
| 1128 | return 0; |
| 1129 | x = x * 10 + (uint32_t)(v[i] - '0'); |
| 1130 | } |
| 1131 | *out = x; |
| 1132 | return 1; |
| 1133 | } |
| 1134 | |
| 1135 | static int saved_ok(const char *s, unsigned n) { return s && n >= 16 && starts(s, "aienos-setup v1\n"); } |
| 1136 | |
| 1137 | int sw_saved_done(const char *saved, unsigned n) |
| 1138 | { |
| 1139 | uint32_t step; |
| 1140 | return saved_ok(saved, n) && field_num(saved, n, "step", &step) && step == SW_DONE; |
| 1141 | } |
| 1142 | |
| 1143 | /* where a restart carries on: a step inside a question goes back to the question */ |
| 1144 | static int resume_at(uint32_t step) |
| 1145 | { |
| 1146 | switch (step) { |
| 1147 | case SW_OTHER: |
| 1148 | case SW_OTHER_CHECK: |
| 1149 | return SW_INTENT; |
| 1150 | case SW_EACH: |
| 1151 | return SW_CONSENT; |
| 1152 | case SW_SERVICE: |
| 1153 | case SW_URL: |
| 1154 | case SW_KEY: |
| 1155 | case SW_TESTING: |
| 1156 | case SW_TEST_FAILED: |
| 1157 | return SW_LLM; |
| 1158 | default: |
| 1159 | return step < SW_STEPS ? (int)step : SW_HELLO; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | void sw_start(sw_wizard *w, const si_inv *inv, int courier, const char *saved, unsigned n, uint32_t now) |
| 1164 | { |
| 1165 | zero(w, sizeof *w); |
| 1166 | w->inv = inv; |
| 1167 | w->courier = courier; |
| 1168 | w->step_t = now; |
| 1169 | find_models(w); |
| 1170 | uint32_t step = 0, want = 0, consent = 0, asked = 1, kind = 0; |
| 1171 | if (saved_ok(saved, n) && field_num(saved, n, "step", &step) && step > SW_HELLO && step < SW_DONE) { |
| 1172 | field_num(saved, n, "want", &want); |
| 1173 | field_num(saved, n, "consent", &consent); |
| 1174 | field_num(saved, n, "asked", &asked); |
| 1175 | field_num(saved, n, "llm", &kind); |
| 1176 | w->want = want & 15u; |
| 1177 | w->consent = consent & ((1u << SW_I_COUNT) - 1); |
| 1178 | w->consent_asked = asked <= 2 ? (int)asked - 1 : 0; |
| 1179 | if (kind <= SW_LLM_FETCH) |
| 1180 | w->llm_kind = (uint8_t)kind; |
| 1181 | field(saved, n, "name", w->llm_name, sizeof w->llm_name); |
| 1182 | field(saved, n, "url", w->llm_url, sizeof w->llm_url); |
| 1183 | plan_items(w); |
| 1184 | w->resumed = resume_at(step); |
| 1185 | event(w, "resumed at ", sw_step_name(w->resumed)); |
| 1186 | } |
| 1187 | w->step = SW_HELLO; |
| 1188 | event(w, "step ", "hello"); |
| 1189 | render(w); |
| 1190 | } |
| 1191 | |
| 1192 | unsigned sw_record(const sw_wizard *w, char *out, unsigned cap) |
| 1193 | { |
| 1194 | unsigned n = sw_save(w, out, cap); |
| 1195 | unsigned m = n ? sw_llm_conf(w, out + n, cap - n) : 0; /* 0 too: no AI picked yet */ |
| 1196 | unsigned c = n ? sw_consent(w, out + n + m, cap - n - m) : 0; |
| 1197 | if (!n || !c) { |
| 1198 | if (cap) |
| 1199 | out[0] = 0; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | return n + m + c; |
| 1203 | } |