-
Notifications
You must be signed in to change notification settings - Fork 0
/
memBootstrap.js
145 lines (118 loc) · 3.6 KB
/
memBootstrap.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require("dotenv").config();
(async () => {
const memory = require(process.env.MEMFILE),
{ getSummary, getCaption } = require("./llmutils"),
fs = require("fs"),
chrono = require("chrono-node");
function removeDates(input) {
const parsedDates = chrono.parse(input);
let lastEndIndex = 0;
let output = "";
for (const result of parsedDates) {
const startIndex = result.index;
const endIndex = startIndex + result.text.length;
// Add the text from the end of the last date to the start of this date
output += input.slice(lastEndIndex, startIndex);
lastEndIndex = endIndex;
}
// Add the remaining text after the last date
output += input.slice(lastEndIndex);
return output.trim();
}
/**
* @param {string} str To extract from
*/
function extractEmotes(str) {
var mutate = str;
const emoteRegex = /<(a?):(\w+):(\d+)>/gim,
matchArray = Array.from(str.matchAll(emoteRegex));
matchArray.forEach((m) => {
var emoteName = m[2];
if (emoteName.includes("_")) emoteName = emoteName.split("_").pop();
mutate = mutate.replace(m[0], `:${emoteName}:`);
});
return mutate;
}
console.log(memory["messages"].length);
console.log(
memory["messages"].filter(
(m) => !m["content"].includes("!ig") && !m["content"].includes("!hig")
).length
);
const filteredMessages = memory["messages"].filter(
(m) => !m["content"].includes("!ig") && !m["content"].includes("!hig")
);
function chunkArray(array, size) {
var chunks = [];
for (var i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
}
const chunkedConvos = chunkArray(filteredMessages, 10);
console.log(chunkedConvos.length);
const mappedConvos = chunkedConvos.map((c) =>
c.map((m, i) => {
let author;
if (m.author.id != "1044973907632996372")
if (m.author.nickname) author = m.author.nickname.replaceAll(" ", "_");
else author = m.author.name.replaceAll(" ", "_");
else author = "kekbot";
const result = `${author}: ${extractEmotes(m.content)}${
m.attachments.some((a) => a.fileName.split(".").pop().includes("gif"))
? " [gif]"
: ""
}${
m.attachments.some((a) =>
["png", "jpeg", "jpg"].includes(a.fileName.split(".").pop())
)
? ` [image]`
: ""
}`;
return result;
})
);
const summarizedConvos = await Promise.all(
mappedConvos.map(async (c) => {
let summ,
n = 1;
async function callSum() {
try {
summ = await getSummary(c.join("\n"));
} catch (e) {
if (n < 6) {
console.log(`Failed to get summary ${n} times, retrying...`);
await callSum();
n++;
} else
console.log(
`Failed to get summary of ${
c.join("\n").length > 256
? c.join("\n").slice(0, 256) + "..."
: c.join("\n")
}`
);
}
}
await callSum();
console.log(
`[${summ}]: ${
c.join("\n").length > 256
? c.join("\n").slice(0, 256) + "..."
: c.join("\n")
}`
);
try {
summ = removeDates(summ);
} catch (e) {
console.log(`Date removal for [${summ}] failed.`);
}
return summ;
})
);
fs.writeFileSync(
`./bootstrapmemory (${new Date().toISOString().replaceAll(":", "_")})`,
JSON.stringify(summarizedConvos.flat())
);
console.log("Bootstrap written");
})();