@codebam/cf-workers-telegram-bot 12.6.14 → 12.6.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai.js +17 -1
- package/dist/utils.js +39 -6
- package/package.json +1 -1
package/dist/ai.js
CHANGED
|
@@ -66,6 +66,22 @@ input, config) {
|
|
|
66
66
|
parameters: t.parameters,
|
|
67
67
|
},
|
|
68
68
|
}));
|
|
69
|
+
// Inject instructions if tools are provided
|
|
70
|
+
if (tools.length > 0) {
|
|
71
|
+
let systemMsg = messages.find((m) => m.role === 'system');
|
|
72
|
+
if (!systemMsg) {
|
|
73
|
+
systemMsg = { role: 'system', content: '' };
|
|
74
|
+
messages.unshift(systemMsg);
|
|
75
|
+
}
|
|
76
|
+
const toolInstructions = tools
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
+
.map((t) => {
|
|
79
|
+
return `- Name: ${t.name}\n Description: ${t.description}\n Parameters: ${JSON.stringify(t.parameters)}`;
|
|
80
|
+
})
|
|
81
|
+
.join('\n');
|
|
82
|
+
const promptInstruction = `\n\n[SYSTEM INSTRUCTION] You have access to the following tools:\n${toolInstructions}\n\nTo use a tool, you MUST output a tool call wrapped in XML format, like so:\n<tool_call>{"name": "search", "arguments": {"q": "query" }}</tool_call>\nor\n<tool_call>{"name": "fetch", "arguments": {"url": "https://example.com" }}</tool_call>\n\nMake sure the tool call is outputted EXACTLY as shown. The system will intercept this call, execute the tool, and return the output to you. Do not write code or direct the user to run code; call the tools yourself.`;
|
|
83
|
+
systemMsg.content = systemMsg.content + promptInstruction;
|
|
84
|
+
}
|
|
69
85
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
86
|
const runModel = async (msgs, stream) => {
|
|
71
87
|
if (isGemini) {
|
|
@@ -87,7 +103,7 @@ input, config) {
|
|
|
87
103
|
}
|
|
88
104
|
return await ai.run(model, { messages: msgs, tools: cfTools.length > 0 ? cfTools : undefined, stream });
|
|
89
105
|
};
|
|
90
|
-
if (cfTools.length === 0
|
|
106
|
+
if (cfTools.length === 0) {
|
|
91
107
|
return await runModel(messages, config.streamFinalResponse);
|
|
92
108
|
}
|
|
93
109
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/dist/utils.js
CHANGED
|
@@ -164,7 +164,7 @@ export const searchTool = {
|
|
|
164
164
|
// Continue to next fallback
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
-
//
|
|
167
|
+
// Fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
|
|
168
168
|
try {
|
|
169
169
|
const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
|
|
170
170
|
const res = await fetch(wikiUrl, {
|
|
@@ -173,18 +173,51 @@ export const searchTool = {
|
|
|
173
173
|
if (res.status === 200) {
|
|
174
174
|
const data = (await res.json());
|
|
175
175
|
if (data && data.query && Array.isArray(data.query.search)) {
|
|
176
|
-
const
|
|
176
|
+
const wikiResults = data.query.search.map((item) => ({
|
|
177
177
|
title: item.title,
|
|
178
178
|
snippet: item.snippet.replace(/<\/?[^>]+(>|$)/g, ''), // strip HTML tags
|
|
179
179
|
url: `https://en.wikipedia.org/wiki/${encodeURIComponent(item.title)}`,
|
|
180
180
|
}));
|
|
181
|
-
|
|
181
|
+
if (wikiResults.length > 0) {
|
|
182
|
+
return JSON.stringify({ results: wikiResults });
|
|
183
|
+
}
|
|
182
184
|
}
|
|
183
185
|
}
|
|
184
186
|
}
|
|
185
|
-
catch
|
|
186
|
-
|
|
187
|
+
catch {
|
|
188
|
+
// Continue to next fallback
|
|
189
|
+
}
|
|
190
|
+
// Final fallback to Google News RSS search for recent general web/news results
|
|
191
|
+
try {
|
|
192
|
+
const googleNewsUrl = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`;
|
|
193
|
+
const res = await fetch(googleNewsUrl, {
|
|
194
|
+
headers: { 'User-Agent': userAgent },
|
|
195
|
+
});
|
|
196
|
+
if (res.status === 200) {
|
|
197
|
+
const xml = await res.text();
|
|
198
|
+
const items = [];
|
|
199
|
+
const itemRegex = /<item>([\s\S]*?)<\/item>/g;
|
|
200
|
+
let match;
|
|
201
|
+
while ((match = itemRegex.exec(xml)) !== null && items.length < 5) {
|
|
202
|
+
const content = match[1];
|
|
203
|
+
const titleMatch = /<title>([\s\S]*?)<\/title>/.exec(content);
|
|
204
|
+
const linkMatch = /<link>([\s\S]*?)<\/link>/.exec(content);
|
|
205
|
+
const descMatch = /<description>([\s\S]*?)<\/description>/.exec(content);
|
|
206
|
+
const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
|
|
207
|
+
const link = linkMatch ? linkMatch[1] : '';
|
|
208
|
+
const desc = descMatch ? descMatch[1].replace(/<[^>]*>/g, '').replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
|
|
209
|
+
if (title && link) {
|
|
210
|
+
items.push({ title, url: link, snippet: desc });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (items.length > 0) {
|
|
214
|
+
return JSON.stringify({ results: items });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
// Continue
|
|
187
220
|
}
|
|
188
|
-
return 'Error executing search: All public search instances and
|
|
221
|
+
return 'Error executing search: All public search instances, Wikipedia fallback, and Google News fallback returned no results.';
|
|
189
222
|
},
|
|
190
223
|
};
|