@codebam/cf-workers-telegram-bot 12.6.16 → 12.6.18

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 CHANGED
@@ -79,7 +79,7 @@ input, config) {
79
79
  return `- Name: ${t.name}\n Description: ${t.description}\n Parameters: ${JSON.stringify(t.parameters)}`;
80
80
  })
81
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.`;
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": {"query": "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
83
  systemMsg.content = systemMsg.content + promptInstruction;
84
84
  }
85
85
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/dist/utils.d.ts CHANGED
@@ -45,7 +45,8 @@ export declare const searchTool: {
45
45
  };
46
46
  required: string[];
47
47
  };
48
- function: ({ query }: {
49
- query: string;
48
+ function: (args: {
49
+ query?: string;
50
+ q?: string;
50
51
  }) => Promise<string>;
51
52
  };
package/dist/utils.js CHANGED
@@ -132,7 +132,8 @@ export const searchTool = {
132
132
  },
133
133
  required: ['query'],
134
134
  },
135
- function: async ({ query }) => {
135
+ function: async (args) => {
136
+ const query = args.query || args.q || '';
136
137
  const instances = [
137
138
  'https://searxng.site/',
138
139
  'https://priv.au/',
@@ -164,7 +165,50 @@ export const searchTool = {
164
165
  // Continue to next fallback
165
166
  }
166
167
  }
167
- // Fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
168
+ // Fallback to DuckDuckGo Lite Search
169
+ try {
170
+ const ddgUrl = 'https://lite.duckduckgo.com/lite/';
171
+ const ddgRes = await fetch(ddgUrl, {
172
+ method: 'POST',
173
+ headers: {
174
+ 'User-Agent': userAgent,
175
+ 'Content-Type': 'application/x-www-form-urlencoded',
176
+ Accept: 'text/html',
177
+ },
178
+ body: `q=${encodeURIComponent(query)}`,
179
+ });
180
+ if (ddgRes.status === 200 || ddgRes.status === 202) {
181
+ const html = await ddgRes.text();
182
+ const cleanHtml = (str) => str
183
+ .replace(/<[^>]*>/g, '')
184
+ .replace(/&nbsp;/g, ' ')
185
+ .replace(/\s+/g, ' ')
186
+ .trim();
187
+ const links = [];
188
+ const linkRegex = /<a[^>]*class='result-link'[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/g;
189
+ let match;
190
+ while ((match = linkRegex.exec(html)) !== null) {
191
+ links.push({ url: match[1], title: cleanHtml(match[2]) });
192
+ }
193
+ const snippets = [];
194
+ const snippetRegex = /<td[^>]*class='result-snippet'[^>]*>([\s\S]*?)<\/td>/g;
195
+ while ((match = snippetRegex.exec(html)) !== null) {
196
+ snippets.push(cleanHtml(match[1]));
197
+ }
198
+ if (links.length > 0) {
199
+ const results = links.map((link, i) => ({
200
+ title: link.title,
201
+ url: link.url,
202
+ snippet: snippets[i] || '',
203
+ }));
204
+ return JSON.stringify({ results });
205
+ }
206
+ }
207
+ }
208
+ catch {
209
+ // Continue to next fallback
210
+ }
211
+ // Fallback to Wikipedia Search if DuckDuckGo Lite is blocked/rate-limited
168
212
  try {
169
213
  const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
170
214
  const res = await fetch(wikiUrl, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.16",
3
+ "version": "12.6.18",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",