File size: 1,283 Bytes
2f7798c
c32ec0d
2f7798c
c74ff6a
2531bbf
 
 
 
 
 
2f7798c
 
c32ec0d
 
2f7798c
c74ff6a
 
 
 
 
5dd2af5
 
 
 
c985fd8
 
 
 
 
5dd2af5
 
 
 
2f7798c
c32ec0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f7798c
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
export function dirtyLLMResponseCleaner(input: string) {
  let str = (
    `${input || ""}`
    // a summary of all the weird hallucinations I saw it make..
    .replaceAll(`"]`, `"}]`)
    .replaceAll(`" ]`, `"}]`)
    .replaceAll(`"  ]`, `"}]`)
    .replaceAll(`"\n]`, `"}]`)
    .replaceAll(`"\n ]`, `"}]`)
    .replaceAll(`"\n  ]`, `"}]`)
    .replaceAll("}}", "}")
    .replaceAll("]]", "]")
    .replaceAll("[[", "[")
    .replaceAll("{{", "{")
    .replaceAll(",,", ",")
    .replaceAll("[0]", "")
    .replaceAll("[1]", "")
    .replaceAll("[2]", "")
    .replaceAll("[3]", "")
    .replaceAll("[4]", "")
    .replaceAll("[5]", "")
    .replaceAll("[6]", "")
    .replaceAll("[7]", "")
    .replaceAll("[8]", "")
    .replaceAll("[panel 0]", "")
    .replaceAll("[panel 1]", "")
    .replaceAll("[panel 2]", "")
    .replaceAll("[panel 3]", "")
    .replaceAll("[panel 4]", "")
    .replaceAll("[panel 5]", "")
    .replaceAll("[panel 6]", "")
    .replaceAll("[panel 7]", "")
    .replaceAll("[panel 8]", "")
  )

  // repair missing end of JSON array
  if (str.at(-1) === '}') {
    str = str + "]"
  }

  if (str.at(-1) === '"') {
    str = str + "}]"
  }

  if (str[0] === '{') {
    str = "[" + str
  }
  
  if (str[0] === '"') {
    str = "[{" + str
  }

  return str
}