diff --git a/README.md b/README.md index 6e455af..d89eabe 100644 --- a/README.md +++ b/README.md @@ -585,10 +585,10 @@ old one stays on disk, and the system prompt is put back. `/model` lists the models the config names (the core endpoint, every delegate with a model, every exit) as buttons and points the running transport at the -one you press. **The choice lasts for this run and is not saved**: the next -start uses the config's `default`, or `-m` when given. One conversation can sit -on a different model without touching what every later start begins with. -`/model ` keeps the endpoint and takes another model from it. +one you press. The choice is written to the session's sidecar +(`.model`) and comes back when that session is opened again, so one +conversation can sit on a different model without changing what a new one +starts on. `/model ` keeps the endpoint and takes another model from it. `/model probe` asks the configured endpoints `/v1/models` and lists what they really serve. @@ -723,8 +723,7 @@ conversation and puts it on screen. It continues that session id rather than opening a fresh one beside it - the first version did the latter, which looked like a resume and was none. The agent profile comes back too: the engine keeps the session's profile in `.agent`, so a session left in plan resumes -in plan. The model is not part of the session: `/model` did not save it, so a -resume starts on the config default (or `-m`). +in plan; the model it was left on is in `.model` and comes back too. ## MCP diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 7ff0211..a2ee3d1 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -512,10 +512,11 @@ and stand for `http://127.0.0.1:11434/v1` and `https://opencode.ai/zen/go/v1` (the latter with `OPENCODE_GO_API_KEY`). -`/model` offers the models the config names and switches the transport for -**this run only**; nothing is written to the config or to the session, so the -next start is the config's `default` (or `-m`). The provider block supplies -kind, endpoint and key variable. `/model probe` asks each endpoint +`/model` offers the models the config names and switches the transport; the +choice is written to the session's sidecar (`.model`) as a selector, +so reopening that session brings the model back without changing what a new +session starts on. The provider block supplies kind, endpoint and key variable; +the sidecar only names the choice. `/model probe` asks each endpoint `/v1/models` and lists what it really serves; the picker in the curses chat also asks the endpoint the session is on and adds those models to its list. `/model ` takes another model from the current endpoint. With a plugin diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 424e0ce..c508ba6 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -83,8 +83,7 @@ Stream. (`test_gateway.sh`, `test_ui.sh`); `/connect` auch im TUI - **Modelle wechseln und Provider anlegen**: `/model` liest die konfigurierten Modelle (Haupt-Endpoint, Delegates, Exits), haengt den Transport um, merkt - die Wahl nur im laufenden Prozess (nichts wird gespeichert) und fragt per - `/model probe` die Endpoints nach + die Wahl in `.model` und fragt per `/model probe` die Endpoints nach `/v1/models`; das TUI zeigt im Picker auch die Modelle des Endpoints, auf dem die Sitzung gerade laeuft. `/connect` schreibt eine validierte `delegate`-Zeile in den Drop-in neben der benutzten Konfiguration, prueft sie diff --git a/src/backend/engine.c b/src/backend/engine.c index 6a0dfbf..033e14f 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -401,18 +401,93 @@ profile_save(struct sp_engine *e) (void)chmod(path, 0600); } -/* - * The transcript note after a successful /model. The choice lives only in - * this engine: nothing is written to the config and nothing to the session, - * so the next start is the config (or -m), not the last thing someone tried. - */ +/* /.model: the model /model chose in this session */ +static void +session_model_path(struct sp_engine *e, char *buf, size_t cap) +{ + snprintf(buf, cap, "%s/%s.model", e->sess.dir, e->sess.id); +} + +static int +session_model_save(struct sp_engine *e, const char *sel, char *err, + size_t errcap) +{ + char path[800], tmp[820]; + FILE *f; + + if (e == NULL || sel == NULL || *sel == '\0') + return -1; + session_model_path(e, path, sizeof(path)); + snprintf(tmp, sizeof(tmp), "%s.tmp", path); + f = fopen(tmp, "w"); + if (f == NULL) { + if (err != NULL && errcap > 0) + snprintf(err, errcap, "%s", strerror(errno)); + return -1; + } + (void)fchmod(fileno(f), 0600); + fprintf(f, "%s\n", sel); + if (fclose(f) != 0 || rename(tmp, path) != 0) { + if (err != NULL && errcap > 0) + snprintf(err, errcap, "%s", strerror(errno)); + (void)unlink(tmp); + return -1; + } + return 0; +} + +static int +session_model_read(struct sp_engine *e, char *buf, size_t cap) +{ + char path[800], line[320]; + FILE *f; + size_t n; + + if (e == NULL || e->sess.dir[0] == '\0' || buf == NULL || cap == 0) + return -1; + session_model_path(e, path, sizeof(path)); + f = fopen(path, "r"); + if (f == NULL) + return -1; + if (fgets(line, sizeof(line), f) == NULL) { + fclose(f); + return -1; + } + fclose(f); + line[strcspn(line, "\r\n")] = '\0'; + n = strlen(line); + if (n == 0 || n >= cap) + return -1; + memcpy(buf, line, n + 1); + return 0; +} + +/* the selector that puts this engine back on the model it is on now */ +static void +current_model_selector(struct sp_engine *e, char *buf, size_t cap) +{ + const char *act = sp_usage_active(); + + if (act != NULL && *act != '\0') + snprintf(buf, cap, "%s/%s", sp_provider_label(act), + e->cur_model); + else + snprintf(buf, cap, "%s", e->cur_model); +} + +/* the transcript note after a successful /model: saved in this session */ static void session_model_note(struct sp_engine *e, const char *sel, struct sp_buf *out, const char *model, const char *provider) { - (void)e; - (void)sel; - sp_buf_printf(out, sp_t("model.switched"), model, provider); + char err[128]; + + err[0] = '\0'; + if (session_model_save(e, sel, err, sizeof(err)) != 0) + sp_buf_printf(out, sp_t("model.save_failed"), model, provider, + err[0] != '\0' ? err : "?"); + else + sp_buf_printf(out, sp_t("model.switched"), model, provider); } static int @@ -1965,6 +2040,13 @@ cmd_new(struct sp_engine *e, struct sp_buf *out) (void)sp_log_open(e->sess.dir, e->sess.id); sp_logf("session", "started: %s", e->sess.id); profile_save(e); + { + char sel[320]; + + current_model_selector(e, sel, sizeof(sel)); + if (sel[0] != '\0') + (void)session_model_save(e, sel, NULL, 0); + } ev_push(e, SP_EV_CLEAR, 0, NULL, NULL); sp_buf_printf(out, sp_t("session.new"), e->sess.id); } @@ -2731,7 +2813,7 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o, { struct sp_engine_opts local; struct sp_engine *e; - char cwd[700]; + char cwd[700], saved[320], saved_model[320]; int start = -1, code = -1; if (out == NULL || o == NULL || o->conf == NULL || o->base == NULL) @@ -2791,6 +2873,35 @@ sp_engine_open(struct sp_engine **out, const struct sp_engine_opts *o, } if (open_session(e, o, err, &code) != 0) goto fail; + if (session_model_read(e, saved, sizeof(saved)) == 0) { + struct sp_choice ch; + + memset(&ch, 0, sizeof(ch)); + if (sp_provider_choose(e->conf, saved, &ch) == 0 && + ch.d != NULL) { + local.endpoint = ch.d->endpoint; + snprintf(saved_model, sizeof(saved_model), "%s", + ch.model[0] != '\0' ? ch.model : ch.d->model); + local.model = saved_model; + local.kind = ch.d->kind; + local.key_env = ch.d->key_env; + sp_usage_set_active(ch.d->id); + } else if (strchr(saved, '/') == NULL) { + /* a model of the endpoint the run was configured with */ + snprintf(saved_model, sizeof(saved_model), "%s", saved); + local.model = saved_model; + } else { + sp_logf("session", "saved model '%s' is gone; " + "using the default", saved); + saved[0] = '\0'; + } + if (saved[0] != '\0') { + snprintf(e->cur_endpoint, sizeof(e->cur_endpoint), "%s", + local.endpoint != NULL ? local.endpoint : ""); + snprintf(e->cur_model, sizeof(e->cur_model), "%.255s", + local.model != NULL ? local.model : ""); + } + } e->ag.sess = &e->sess; sp_logf("session", "started: %s", e->sess.id); if (load_plugins(e, o, err) != 0) diff --git a/tests/test_engine.c b/tests/test_engine.c index 0726904..1057c07 100644 --- a/tests/test_engine.c +++ b/tests/test_engine.c @@ -286,19 +286,25 @@ main(void) } /* - * /model changes this run only. Nothing is written to the config or to - * a session sidecar, so a restart is the config (or -m), not the last - * model someone tried. + * /model belongs to the session: the selector is written to the + * session's sidecar and comes back when the session is reopened. */ kind = sp_engine_input(eng, "/model test-model-2", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "test-model-2")); { - char mp[800]; + char mp[800], line[320]; + FILE *mf; snprintf(mp, sizeof(mp), "%s/%s.model", path, sp_engine_session_id(eng)); - CHECK(access(mp, F_OK) != 0); + mf = fopen(mp, "r"); + CHECK(mf != NULL); + if (mf != NULL) { + CHECK(fgets(line, sizeof(line), mf) != NULL); + CHECK(contains(line, "test-model-2")); + fclose(mf); + } } sp_buf_clear(&out); @@ -354,8 +360,7 @@ main(void) struct sp_engine_state st; CHECK(sp_engine_state(eng2, &st) == 0); - /* the config wins: /model was not saved */ - CHECK(strcmp(st.model, "test-model") == 0); + CHECK(strcmp(st.model, "test-model-2") == 0); } n2 = sp_engine_agents(eng2); for (i = 0; i < n2; i++)