Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([series-llm-override-resolution-helper]): extract resolveSeriesLlmOverride helper

[._.]/ Adam Eivyfcc37d556c35
diff --git a/server/lib/README.md b/server/lib/README.md
index a50373812c..c0a6cf2f06 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -49,6 +49,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `scenePrompt.js` | Scene-prompt composer + bible matchers (chars/places/objects in text). |
| `sceneExtractor.js` | Split prose or teleplay into scene list via LLM. |
| `seasonStructure.js` | Season/episode structure recommendation. |
+| `seriesLlmOverride.js` | Pure `resolveSeriesLlmOverride(series, { overrideProvider, overrideModel })` → `{ provider, model, providerMatchesSeries }` — shared fallback so Pipeline LLM actions honor the series' configured provider/model, only inheriting the series model when the effective provider still matches. |
| `bibleExtractor.js` | LLM bible-extraction stage + sanitization. |
| `catalogBulkParsers.js` | Dependency-free markdown/CSV/JSON parsers for `POST /api/catalog/bulk-import` and YAML/markdown serializers for `GET /api/catalog/export`. |
| `catalogChunking.js` | Pure lossless scrap-text chunker (`chunkRawText`, `CATALOG_CHUNK_MAX_CHARS`) — splits a long paste into ≤maxChars chunks on paragraph/newline/sentence/whitespace boundaries so the catalog extractor processes each child and unions results. |
diff --git a/server/lib/index.js b/server/lib/index.js
index 92bc7292c5..733ff5278a 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -44,6 +44,7 @@ export * from './creativeDirectorPrompts.js';
export * from './sceneExtractor.js';
export * from './scenePrompt.js';
export * from './seasonStructure.js';
+export * from './seriesLlmOverride.js';
export * from './storyArc.js';
export * from './storyBuilderIntegrity.js';
export * from './storyBuilderSteps.js';
diff --git a/server/lib/seriesLlmOverride.js b/server/lib/seriesLlmOverride.js
new file mode 100644
index 0000000000..6b57cbde88
--- /dev/null
+++ b/server/lib/seriesLlmOverride.js
@@ -0,0 +1,33 @@
+// Shared resolution for "which provider/model should a Pipeline LLM action
+// use?" — falls back to the series' configured LLM when the client doesn't
+// pass an explicit override, so every Pipeline action (extract-canon,
+// extract-scenes, season-episodes-generate) honors the provider/model picked
+// in the series header instead of the global active provider.
+//
+// A model id is provider-specific, so the series model is only inherited when
+// the EFFECTIVE provider is still the series provider. An override that
+// switches providers without naming a model leaves the model blank, so the new
+// provider's default resolves rather than forwarding a foreign model id that
+// would fail.
+//
+// Returns `''` (not `undefined`) for an unresolved provider/model so callers
+// can map to their own sentinel — most pass `provider || undefined` to the
+// extractor.
+
+/**
+ * Resolve the effective LLM provider/model for a Pipeline action against a series.
+ *
+ * @param {{ llm?: { provider?: string, model?: string } } | null | undefined} series
+ * @param {{ overrideProvider?: string, overrideModel?: string }} [overrides]
+ * @returns {{ provider: string, model: string, providerMatchesSeries: boolean }}
+ */
+export function resolveSeriesLlmOverride(series, { overrideProvider, overrideModel } = {}) {
+ const seriesProvider = series?.llm?.provider || '';
+ const provider = overrideProvider || seriesProvider || '';
+ // No override means we're using the series provider, so the series model is
+ // always safe to inherit; an override matches only when it names the same
+ // provider the series model belongs to.
+ const providerMatchesSeries = !overrideProvider || overrideProvider === seriesProvider;
+ const model = overrideModel || (providerMatchesSeries ? (series?.llm?.model || '') : '');
+ return { provider, model, providerMatchesSeries };
+}
diff --git a/server/lib/seriesLlmOverride.test.js b/server/lib/seriesLlmOverride.test.js
new file mode 100644
index 0000000000..41c0e006ab
--- /dev/null
+++ b/server/lib/seriesLlmOverride.test.js
@@ -0,0 +1,71 @@
+import { describe, it, expect } from 'vitest';
+import { resolveSeriesLlmOverride } from './seriesLlmOverride.js';
+
+describe('resolveSeriesLlmOverride', () => {
+ const series = { llm: { provider: 'anthropic', model: 'claude-x' } };
+
+ it('falls back to the series provider/model when no override is passed', () => {
+ expect(resolveSeriesLlmOverride(series, {})).toEqual({
+ provider: 'anthropic',
+ model: 'claude-x',
+ providerMatchesSeries: true,
+ });
+ });
+
+ it('inherits the series model when an override names the same provider', () => {
+ expect(resolveSeriesLlmOverride(series, { overrideProvider: 'anthropic' })).toEqual({
+ provider: 'anthropic',
+ model: 'claude-x',
+ providerMatchesSeries: true,
+ });
+ });
+
+ it('drops the series model when an override switches provider without a model', () => {
+ expect(resolveSeriesLlmOverride(series, { overrideProvider: 'openai' })).toEqual({
+ provider: 'openai',
+ model: '',
+ providerMatchesSeries: false,
+ });
+ });
+
+ it('honors an explicit override model even when switching providers', () => {
+ expect(
+ resolveSeriesLlmOverride(series, { overrideProvider: 'openai', overrideModel: 'gpt-x' }),
+ ).toEqual({ provider: 'openai', model: 'gpt-x', providerMatchesSeries: false });
+ });
+
+ it('honors an explicit override model when staying on the series provider', () => {
+ expect(
+ resolveSeriesLlmOverride(series, { overrideModel: 'claude-y' }),
+ ).toEqual({ provider: 'anthropic', model: 'claude-y', providerMatchesSeries: true });
+ });
+
+ it('returns empty strings when the series has no llm config and no override', () => {
+ expect(resolveSeriesLlmOverride({}, {})).toEqual({
+ provider: '',
+ model: '',
+ providerMatchesSeries: true,
+ });
+ });
+
+ it('tolerates a null/undefined series', () => {
+ expect(resolveSeriesLlmOverride(null, { overrideProvider: 'openai' })).toEqual({
+ provider: 'openai',
+ model: '',
+ providerMatchesSeries: false,
+ });
+ expect(resolveSeriesLlmOverride(undefined, {})).toEqual({
+ provider: '',
+ model: '',
+ providerMatchesSeries: true,
+ });
+ });
+
+ it('defaults overrides to an empty object', () => {
+ expect(resolveSeriesLlmOverride(series)).toEqual({
+ provider: 'anthropic',
+ model: 'claude-x',
+ providerMatchesSeries: true,
+ });
+ });
+});
diff --git a/server/routes/pipeline.js b/server/routes/pipeline.js
index 882409d3dc..223478ea02 100644
--- a/server/routes/pipeline.js
+++ b/server/routes/pipeline.js
@@ -68,6 +68,7 @@ import { COMIC_PAGE_VARIANTS, slotKeyForVariant } from '../services/pipeline/own
import { ASPECT_RATIOS, QUALITIES } from '../lib/creativeDirectorPresets.js';
import { IMAGE_GEN_MODE } from '../services/imageGen/modes.js';
import { extractScenes, SOURCE_KIND } from '../lib/sceneExtractor.js';
+import { resolveSeriesLlmOverride } from '../lib/seriesLlmOverride.js';
import { buildComicPdf, PAGE_SIZES, DEFAULT_PAGE_SIZE, ERR_NO_RENDERED_PAGES } from '../services/pipeline/comicPdf.js';
import {
buildVolumePdf,
@@ -1281,11 +1282,10 @@ router.post('/series/:id/seasons/:seasonId/episodes/generate', asyncHandler(asyn
// provider is still the series provider; an override that switches
// providers without naming a model leaves it blank so the new provider's
// default resolves.
- const provider = body.providerOverride || series.llm?.provider || '';
- const providerMatchesSeries = !body.providerOverride
- || body.providerOverride === (series.llm?.provider || '');
- const model = body.modelOverride
- || (providerMatchesSeries ? (series.llm?.model || '') : '');
+ const { provider, model } = resolveSeriesLlmOverride(series, {
+ overrideProvider: body.providerOverride,
+ overrideModel: body.modelOverride,
+ });
// Stamp new inserts as series-extracted (autoLock + sourceSeriesId) so
// continuity-derived canon survives later AI refines and stays
// attributable to this series. Matches the pre-B.4 series-side
@@ -1716,8 +1716,10 @@ router.post('/issues/:id/stages/storyboards/extract-scenes', asyncHandler(async
// provider would be paired with a foreign model id and fail (same guard as
// the extract-canon route). When the override switches providers without
// naming a model, leave it blank so the new provider's default resolves.
- const sceneProviderMatchesSeries = !body.providerOverride
- || body.providerOverride === (series.llm?.provider || '');
+ const sceneLlm = resolveSeriesLlmOverride(series, {
+ overrideProvider: body.providerOverride,
+ overrideModel: body.modelOverride,
+ });
const result = await extractScenes({
source,
sourceKind,
@@ -1727,10 +1729,8 @@ router.post('/issues/:id/stages/storyboards/extract-scenes', asyncHandler(async
work: { title: issue.title, kind: 'tv-episode' },
series: { name: series.name, styleNotes: series.styleNotes },
issue: { number: issue.number, title: issue.title },
- providerOverride: body.providerOverride || series.llm?.provider || undefined,
- modelOverride: body.modelOverride
- || (sceneProviderMatchesSeries ? series.llm?.model : undefined)
- || undefined,
+ providerOverride: sceneLlm.provider || undefined,
+ modelOverride: sceneLlm.model || undefined,
tag: `pipeline-storyboards-extract-${sourceKind}`,
});
@@ -1866,17 +1866,16 @@ router.post('/issues/:id/stages/:stageId/extract-canon', asyncHandler(async (req
// explicit override — matches every other Pipeline LLM action (e.g.
// storyboards/extract-scenes) so a manual extract honors the provider/model
// picked in the series header instead of the global default.
- const provider = body.providerOverride || series.llm?.provider || '';
// A model id is provider-specific. Only inherit the series model when the
// EFFECTIVE provider is still the series provider — otherwise the retry
// picker's whole point (switch provider, keep "Default model") would forward
// e.g. `providerOverride: anthropic` paired with a Codex/OpenAI model id and
// fail. When the user overrode to a different provider without naming a
// model, leave it blank so the extractor resolves that provider's default.
- const providerMatchesSeries = !body.providerOverride
- || body.providerOverride === (series.llm?.provider || '');
- const model = body.model
- || (providerMatchesSeries ? (series.llm?.model || '') : '');
+ const { provider, model } = resolveSeriesLlmOverride(series, {
+ overrideProvider: body.providerOverride,
+ overrideModel: body.model,
+ });
// Stamp the outcome on the stage so the Nouns UI can persist a
// failure/partial banner and the user can retry with a different