-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadurl.js
More file actions
81 lines (69 loc) · 2.75 KB
/
Copy pathreadurl.js
File metadata and controls
81 lines (69 loc) · 2.75 KB
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
class EimiScriptReadUrl {
static commands = [{
name: 'tldr',
run: async function({args, line}) {
const match = line.match(this.summarizeRegex)
if (!match) throw Error('Usage: /tldr <url>')
const replacedLine = line.replace(this.summarizeRegex, this.prompt)
return {next: 'gen', replaceLine: replacedLine}
}
}]
constructor({eimiApi}) {
this.eimiApi = eimiApi;
this.requests = new Map();
this.summarizeRegex = /^\/tldr\s+(https?:\/\/[^\s]+)/;
this.prompt = `%%readurl($1)\n\n------\n\nWrite a summary.`
}
findOriginalMessage(requestMessage, allMessages) {
if (!requestMessage._id) return requestMessage;
const original = allMessages.find(m => m.id === requestMessage._id);
return original || requestMessage;
}
async _fetchUrlText(url, originalMessage) {
const key = `readurl-${url}`;
originalMessage.customData = originalMessage.customData || [];
const existing = originalMessage.customData.find(i => i.key === key);
if (existing) return existing.value;
const response = await fetch(`https://r.jina.ai/${encodeURIComponent(url)}`);
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
const text = await response.text();
const recheck = originalMessage.customData.find(i => i.key === key);
if (!recheck) originalMessage.customData.push({ key, value: text });
return text;
}
async fetchUrlText(url, originalMessage) {
if (this.requests.has(url)) return this.requests.get(url);
const promise = this._fetchUrlText(url, originalMessage);
this.requests.set(url, promise);
try {
return await promise;
} finally {
this.requests.delete(url);
}
}
async onRequest(request, newMessage, messages) {
request.messages = await Promise.all(request.messages.map(async (m) => {
const originalMessage = this.findOriginalMessage(m, messages);
if (!originalMessage) return m;
const newContent = await Promise.all(m.content.map(async (c) => {
if (c.type !== 'text') return c;
if (!c.text.includes('%%readurl(')) return c;
const regex = /%%readurl\((https?:\/\/[^)]+)\)/g;
let matches = [];
let match;
while ((match = regex.exec(c.text)) !== null) {
matches.push({ fullMatch: match[0], url: match[1] });
}
if (matches.length === 0) return c;
let newText = c.text;
for (const { fullMatch, url } of matches) {
const content = await this.fetchUrlText(url, originalMessage);
newText = newText.replace(fullMatch, `---\n${content}\n---`);
}
return { ...c, text: newText };
}));
return { ...m, content: newContent };
}));
}
}
return EimiScriptReadUrl;