File size: 2,155 Bytes
9bcdb59
b022cb9
faf4ba4
 
2f9a87c
9bcdb59
 
 
faf4ba4
 
2f9a87c
 
faf4ba4
b022cb9
9bcdb59
 
d303a22
 
2f9a87c
 
d303a22
b022cb9
faf4ba4
9bcdb59
faf4ba4
3220193
faf4ba4
9bcdb59
faf4ba4
9bcdb59
faf4ba4
9bcdb59
faf4ba4
 
 
 
1c1e6e9
faf4ba4
b022cb9
faf4ba4
9bcdb59
9bfb451
9bcdb59
faf4ba4
 
 
9bfb451
faf4ba4
9bfb451
 
95a4e14
9bfb451
faf4ba4
 
 
9bcdb59
9bfb451
 
faf4ba4
d303a22
faf4ba4
 
 
 
 
 
 
95a4e14
faf4ba4
 
 
2f9a87c
9bfb451
faf4ba4
 
9bcdb59
faf4ba4
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
72
73
74
75
76
77
78
import { Preset } from "../engine/presets"
import { GeneratedPanel, LLMVendorConfig } from "@/types"
import { predictNextPanels } from "./predictNextPanels"
import { joinWords } from "@/lib/joinWords"
import { sleep } from "@/lib/sleep"

export const getStoryContinuation = async ({
  preset,
  stylePrompt = "",
  userStoryPrompt = "",
  nbPanelsToGenerate,
  maxNbPanels,
  existingPanels = [],
  llmVendorConfig
}: {
  preset: Preset;
  stylePrompt?: string;
  userStoryPrompt?: string;
  nbPanelsToGenerate: number;
  maxNbPanels: number;
  existingPanels?: GeneratedPanel[];
  llmVendorConfig: LLMVendorConfig
}): 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,
      maxNbPanels,
      existingPanels,
      llmVendorConfig,
    })

    // 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 || ""}`,
        speech: `${panelCandidates[i]?.speech || ""}`,
        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)}`,
        ]),
        speech: "...",
        caption: "(Sorry, LLM generation failed: using degraded mode)"
      })
    }
    await sleep(2000)
    // console.error(err)
  } finally {
    return panels
  }
}