File size: 1,942 Bytes
450060f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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,
  nbTotalPanels = 8,
  existingPanels = [],
}: {
  preset: Preset;
  stylePrompt?: string;
  userStoryPrompt?: string;
  nbPanelsToGenerate?: number;
  nbTotalPanels?: number;
  existingPanels?: GeneratedPanel[];
}): Promise<GeneratedPanel[]> => {

  let panels: GeneratedPanel[] = []
  const startAt: number = (existingPanels.length + 1) || 0
  const endAt: number = startAt + nbPanelsToGenerate

  try {

    const prompt = joinWords([ userStoryPrompt ])

    const panelCandidates: GeneratedPanel[] = await predictNextPanels({
      preset,
      prompt,
      nbPanelsToGenerate,
      nbTotalPanels,
      existingPanels,
    })

    // console.log("LLM responded with panelCandidates:", 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 i = 0; i < nbPanelsToGenerate; i++) {
      panels.push({
        panel: startAt + i,
        instructions: `${panelCandidates[i]?.instructions || ""}`,
        caption: `${panelCandidates[i]?.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; 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
  }
}