@codebam/cf-workers-telegram-bot 12.6.15 → 12.6.17

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,7 @@ export const searchTool = {
164
165
  // Continue to next fallback
165
166
  }
166
167
  }
167
- // Final fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
168
+ // Fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
168
169
  try {
169
170
  const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
170
171
  const res = await fetch(wikiUrl, {
@@ -173,18 +174,51 @@ export const searchTool = {
173
174
  if (res.status === 200) {
174
175
  const data = (await res.json());
175
176
  if (data && data.query && Array.isArray(data.query.search)) {
176
- const results = data.query.search.map((item) => ({
177
+ const wikiResults = data.query.search.map((item) => ({
177
178
  title: item.title,
178
179
  snippet: item.snippet.replace(/<\/?[^>]+(>|$)/g, ''), // strip HTML tags
179
180
  url: `https://en.wikipedia.org/wiki/${encodeURIComponent(item.title)}`,
180
181
  }));
181
- return JSON.stringify({ results });
182
+ if (wikiResults.length > 0) {
183
+ return JSON.stringify({ results: wikiResults });
184
+ }
182
185
  }
183
186
  }
184
187
  }
185
- catch (e) {
186
- return `Error executing search: All public search instances and Wikipedia fallback failed. Error: ${String(e)}`;
188
+ catch {
189
+ // Continue to next fallback
190
+ }
191
+ // Final fallback to Google News RSS search for recent general web/news results
192
+ try {
193
+ const googleNewsUrl = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`;
194
+ const res = await fetch(googleNewsUrl, {
195
+ headers: { 'User-Agent': userAgent },
196
+ });
197
+ if (res.status === 200) {
198
+ const xml = await res.text();
199
+ const items = [];
200
+ const itemRegex = /<item>([\s\S]*?)<\/item>/g;
201
+ let match;
202
+ while ((match = itemRegex.exec(xml)) !== null && items.length < 5) {
203
+ const content = match[1];
204
+ const titleMatch = /<title>([\s\S]*?)<\/title>/.exec(content);
205
+ const linkMatch = /<link>([\s\S]*?)<\/link>/.exec(content);
206
+ const descMatch = /<description>([\s\S]*?)<\/description>/.exec(content);
207
+ const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
208
+ const link = linkMatch ? linkMatch[1] : '';
209
+ const desc = descMatch ? descMatch[1].replace(/<[^>]*>/g, '').replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
210
+ if (title && link) {
211
+ items.push({ title, url: link, snippet: desc });
212
+ }
213
+ }
214
+ if (items.length > 0) {
215
+ return JSON.stringify({ results: items });
216
+ }
217
+ }
218
+ }
219
+ catch {
220
+ // Continue
187
221
  }
188
- return 'Error executing search: All public search instances and Wikipedia fallback returned no results.';
222
+ return 'Error executing search: All public search instances, Wikipedia fallback, and Google News fallback returned no results.';
189
223
  },
190
224
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.15",
3
+ "version": "12.6.17",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",