import { Preset } from "../engine/presets" import { GeneratedPanel } from "@/types" import { predictNextPanels } from "./predictNextPanels" import { joinWords } from "@/lib/joinWords" export const getStoryContinuation = async ({ preset, stylePrompt = "", userStoryPrompt = "", nbPanelsToGenerate = 2, existingPanels = [], }: { preset: Preset; stylePrompt: string; userStoryPrompt: string; nbPanelsToGenerate: number; existingPanels: GeneratedPanel[]; }): Promise => { let panels: GeneratedPanel[] = [] const startAt: number = existingPanels.at(-1)?.panel || existingPanels.length || 0 const endAt: number = startAt + nbPanelsToGenerate try { const prompt = joinWords([ userStoryPrompt ]) const panelCandidates: GeneratedPanel[] = await predictNextPanels({ preset, prompt, nbPanelsToGenerate, existingPanels, }) console.log("LLM responded with panels:", panelCandidates) // we clean the output from the LLM // most importantly, we need to adjust the panel index, // to start from where we last finished for (let p = startAt; p < endAt; p++) { panels.push({ panel: p, instructions: `${panelCandidates[p]?.instructions || ""}`, caption: `${panelCandidates[p]?.caption || ""}`, }) } } catch (err) { console.log("LLM step failed due to:", err) console.log("we are now switching to a degraded mode, using 4 similar panels") panels = [] for (let p = startAt; p < endAt; p++) { panels.push({ panel: p, instructions: joinWords([ stylePrompt, userStoryPrompt, `${".".repeat(p)}`, ]), caption: "(Sorry, LLM generation failed: using degraded mode)" }) } console.error(err) } finally { return panels } }