Files
openchamber/packages/web/server/lib/small-model/config-injection.js
T
Serhii Dziupin 8a85073261 fix(server): forward Small Model override to managed OpenCode config
OpenChamber's Settings → Chat → Small Model override only fed OpenChamber's
own /api/small-model/generate utility service; it never reached the managed
OpenCode server, whose internal title/summary generation reads small_model
from its config. With the override injected into OPENCODE_CONFIG_CONTENT at
managed-process launch, session title generation uses the user's explicit
model instead of falling back (or failing to resolve) — fixing sessions that
stayed untitled even with a Small Model configured.

Only an explicit override (smallModelUseDefault === false with a non-empty
smallModelOverride) is injected; "use default" leaves the config untouched
so OpenCode's own resolution chain stays authoritative. Malformed user config
is left unmodified. External OpenCode servers are unaffected (they are not
launched with this env).

Fixes #2497
2026-08-05 13:46:30 +03:00

52 lines
1.9 KiB
JavaScript

/**
* Applies the user's explicit Small Model override (Settings → Chat → Small
* Model) to the configuration injected into the managed OpenCode process.
*
* OpenCode's own session-title and summary generation reads `small_model`
* from its config layers. Previously the OpenChamber settings override only
* fed OpenChamber's own `/api/small-model/generate` utility service, so a
* configured Small Model never reached OpenCode's title generation and
* sessions kept their fallback/untitled state. Injecting the override as
* `small_model` in the managed `OPENCODE_CONFIG_CONTENT` closes that gap for
* the managed server.
*
* Only an explicit override applies (`smallModelUseDefault === false` with a
* non-empty `smallModelOverride`). "Use default" leaves the config untouched,
* so OpenCode's own resolution chain (config `small_model`, then its family
* scan) stays authoritative — this mirrors the precedence documented in
* `packages/web/server/lib/small-model/DOCUMENTATION.md`.
*
* Malformed user config is left untouched rather than rewritten: OpenCode's
* own loader is the right place to surface it, and silently rewriting it
* would hide the error.
*/
export const applySmallModelOverrideToOpenCodeConfig = ({
configContent,
smallModelUseDefault,
smallModelOverride,
}) => {
if (smallModelUseDefault !== false) {
return configContent;
}
const override = typeof smallModelOverride === 'string' ? smallModelOverride.trim() : '';
if (!override) {
return configContent;
}
const current = (() => {
if (typeof configContent !== 'string' || configContent.trim().length === 0) {
return {};
}
try {
return JSON.parse(configContent);
} catch {
return null;
}
})();
if (current === null || typeof current !== 'object' || Array.isArray(current)) {
return configContent;
}
return JSON.stringify({ ...current, small_model: override });
};