(args: Record<string, unknown>, context: ToolContext)
| 11 | } |
| 12 | |
| 13 | export async function webSearch(args: Record<string, unknown>, context: ToolContext): Promise<ToolResult> { |
| 14 | const query = String(args.query ?? "") |
| 15 | if (!query) return { text: "FAILED: query is empty", isError: true } |
| 16 | |
| 17 | try { |
| 18 | const url = getUrlFromToken("https://api.matterai.so/axoncode/websearch", context.token) |
| 19 | const response = await fetch(url, { |
| 20 | method: "POST", |
| 21 | headers: { |
| 22 | "Content-Type": "application/json", |
| 23 | Authorization: `Bearer ${context.token}`, |
| 24 | }, |
| 25 | body: JSON.stringify({ query }), |
| 26 | signal: AbortSignal.timeout(30_000), |
| 27 | }) |
| 28 | if (!response.ok) { |
| 29 | return { text: `Web search failed (${response.status})`, isError: true } |
| 30 | } |
| 31 | const data = (await response.json()) as { results?: WebSearchResult[] } |
| 32 | const results = data.results |
| 33 | if (!results || results.length === 0) { |
| 34 | return { text: `No results found for query: "${query}"` } |
| 35 | } |
| 36 | const formatted = results |
| 37 | .map((result, index) => { |
| 38 | let entry = `[${index + 1}] ${result.title}\nURL: ${result.url}` |
| 39 | if (result.publish_date) entry += `\nPublished: ${result.publish_date}` |
| 40 | if (result.excerpts && result.excerpts.length > 0) entry += `\n\n${result.excerpts.join("\n\n")}` |
| 41 | return entry |
| 42 | }) |
| 43 | .join("\n\n---\n\n") |
| 44 | return { text: `Web search results for "${query}":\n\n${formatted}` } |
| 45 | } catch (error) { |
| 46 | return { text: `Web search failed: ${(error as Error).message}`, isError: true } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | export async function webFetch(args: Record<string, unknown>, context: ToolContext): Promise<ToolResult> { |
| 51 | const targetUrl = String(args.url ?? "") |
nothing calls this directly
no test coverage detected