|
|
@@ -1,15 +1,9 @@
|
|
|
import z from "zod"
|
|
|
+import { Effect } from "effect"
|
|
|
+import { HttpClient } from "effect/unstable/http"
|
|
|
import { Tool } from "./tool"
|
|
|
+import * as McpExa from "./mcp-exa"
|
|
|
import DESCRIPTION from "./websearch.txt"
|
|
|
-import { abortAfterAny } from "../util/abort"
|
|
|
-
|
|
|
-const API_CONFIG = {
|
|
|
- BASE_URL: "https://mcp.exa.ai",
|
|
|
- ENDPOINTS: {
|
|
|
- SEARCH: "/mcp",
|
|
|
- },
|
|
|
- DEFAULT_NUM_RESULTS: 8,
|
|
|
-} as const
|
|
|
|
|
|
const Parameters = z.object({
|
|
|
query: z.string().describe("Websearch query"),
|
|
|
@@ -30,121 +24,53 @@ const Parameters = z.object({
|
|
|
.describe("Maximum characters for context string optimized for LLMs (default: 10000)"),
|
|
|
})
|
|
|
|
|
|
-interface McpSearchRequest {
|
|
|
- jsonrpc: string
|
|
|
- id: number
|
|
|
- method: string
|
|
|
- params: {
|
|
|
- name: string
|
|
|
- arguments: {
|
|
|
- query: string
|
|
|
- numResults?: number
|
|
|
- livecrawl?: "fallback" | "preferred"
|
|
|
- type?: "auto" | "fast" | "deep"
|
|
|
- contextMaxCharacters?: number
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-interface McpSearchResponse {
|
|
|
- jsonrpc: string
|
|
|
- result: {
|
|
|
- content: Array<{
|
|
|
- type: string
|
|
|
- text: string
|
|
|
- }>
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export const WebSearchTool = Tool.define("websearch", async () => {
|
|
|
- return {
|
|
|
- get description() {
|
|
|
- return DESCRIPTION.replace("{{year}}", new Date().getFullYear().toString())
|
|
|
- },
|
|
|
- parameters: Parameters,
|
|
|
- async execute(params, ctx) {
|
|
|
- await ctx.ask({
|
|
|
- permission: "websearch",
|
|
|
- patterns: [params.query],
|
|
|
- always: ["*"],
|
|
|
- metadata: {
|
|
|
- query: params.query,
|
|
|
- numResults: params.numResults,
|
|
|
- livecrawl: params.livecrawl,
|
|
|
- type: params.type,
|
|
|
- contextMaxCharacters: params.contextMaxCharacters,
|
|
|
- },
|
|
|
- })
|
|
|
-
|
|
|
- const searchRequest: McpSearchRequest = {
|
|
|
- jsonrpc: "2.0",
|
|
|
- id: 1,
|
|
|
- method: "tools/call",
|
|
|
- params: {
|
|
|
- name: "web_search_exa",
|
|
|
- arguments: {
|
|
|
- query: params.query,
|
|
|
- type: params.type || "auto",
|
|
|
- numResults: params.numResults || API_CONFIG.DEFAULT_NUM_RESULTS,
|
|
|
- livecrawl: params.livecrawl || "fallback",
|
|
|
- contextMaxCharacters: params.contextMaxCharacters,
|
|
|
- },
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- const { signal, clearTimeout } = abortAfterAny(25000, ctx.abort)
|
|
|
-
|
|
|
- try {
|
|
|
- const headers: Record<string, string> = {
|
|
|
- accept: "application/json, text/event-stream",
|
|
|
- "content-type": "application/json",
|
|
|
- }
|
|
|
-
|
|
|
- const response = await fetch(`${API_CONFIG.BASE_URL}${API_CONFIG.ENDPOINTS.SEARCH}`, {
|
|
|
- method: "POST",
|
|
|
- headers,
|
|
|
- body: JSON.stringify(searchRequest),
|
|
|
- signal,
|
|
|
- })
|
|
|
-
|
|
|
- clearTimeout()
|
|
|
-
|
|
|
- if (!response.ok) {
|
|
|
- const errorText = await response.text()
|
|
|
- throw new Error(`Search error (${response.status}): ${errorText}`)
|
|
|
- }
|
|
|
-
|
|
|
- const responseText = await response.text()
|
|
|
-
|
|
|
- // Parse SSE response
|
|
|
- const lines = responseText.split("\n")
|
|
|
- for (const line of lines) {
|
|
|
- if (line.startsWith("data: ")) {
|
|
|
- const data: McpSearchResponse = JSON.parse(line.substring(6))
|
|
|
- if (data.result && data.result.content && data.result.content.length > 0) {
|
|
|
- return {
|
|
|
- output: data.result.content[0].text,
|
|
|
- title: `Web search: ${params.query}`,
|
|
|
- metadata: {},
|
|
|
- }
|
|
|
- }
|
|
|
+export const WebSearchTool = Tool.defineEffect(
|
|
|
+ "websearch",
|
|
|
+ Effect.gen(function* () {
|
|
|
+ const http = yield* HttpClient.HttpClient
|
|
|
+
|
|
|
+ return {
|
|
|
+ get description() {
|
|
|
+ return DESCRIPTION.replace("{{year}}", new Date().getFullYear().toString())
|
|
|
+ },
|
|
|
+ parameters: Parameters,
|
|
|
+ execute: (params: z.infer<typeof Parameters>, ctx: Tool.Context) =>
|
|
|
+ Effect.gen(function* () {
|
|
|
+ yield* Effect.promise(() =>
|
|
|
+ ctx.ask({
|
|
|
+ permission: "websearch",
|
|
|
+ patterns: [params.query],
|
|
|
+ always: ["*"],
|
|
|
+ metadata: {
|
|
|
+ query: params.query,
|
|
|
+ numResults: params.numResults,
|
|
|
+ livecrawl: params.livecrawl,
|
|
|
+ type: params.type,
|
|
|
+ contextMaxCharacters: params.contextMaxCharacters,
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ )
|
|
|
+
|
|
|
+ const result = yield* McpExa.call(
|
|
|
+ http,
|
|
|
+ "web_search_exa",
|
|
|
+ McpExa.SearchArgs,
|
|
|
+ {
|
|
|
+ query: params.query,
|
|
|
+ type: params.type || "auto",
|
|
|
+ numResults: params.numResults || 8,
|
|
|
+ livecrawl: params.livecrawl || "fallback",
|
|
|
+ contextMaxCharacters: params.contextMaxCharacters,
|
|
|
+ },
|
|
|
+ "25 seconds",
|
|
|
+ )
|
|
|
+
|
|
|
+ return {
|
|
|
+ output: result ?? "No search results found. Please try a different query.",
|
|
|
+ title: `Web search: ${params.query}`,
|
|
|
+ metadata: {},
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- output: "No search results found. Please try a different query.",
|
|
|
- title: `Web search: ${params.query}`,
|
|
|
- metadata: {},
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- clearTimeout()
|
|
|
-
|
|
|
- if (error instanceof Error && error.name === "AbortError") {
|
|
|
- throw new Error("Search request timed out")
|
|
|
- }
|
|
|
-
|
|
|
- throw error
|
|
|
- }
|
|
|
- },
|
|
|
- }
|
|
|
-})
|
|
|
+ }).pipe(Effect.runPromise),
|
|
|
+ }
|
|
|
+ }),
|
|
|
+)
|