|
| 1 | +export * as WebSearchTavily from "./tavily.js" |
| 2 | + |
| 3 | +import { define } from "@opencode-ai/plugin/effect/plugin" |
| 4 | +import { Duration, Effect, Schema, Scope } from "effect" |
| 5 | +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http" |
| 6 | +import { App } from "../../app.js" |
| 7 | + |
| 8 | +export const endpoint = "https://api.tavily.com/search" |
| 9 | + |
| 10 | +const SearchRequest = Schema.Struct({ |
| 11 | + query: Schema.String, |
| 12 | + search_depth: Schema.Literal("basic"), |
| 13 | + chunks_per_source: Schema.Number, |
| 14 | + max_results: Schema.Number, |
| 15 | +}) |
| 16 | + |
| 17 | +const SearchResponse = Schema.Struct({ |
| 18 | + results: Schema.Array( |
| 19 | + Schema.Struct({ |
| 20 | + title: Schema.String, |
| 21 | + url: Schema.String, |
| 22 | + content: Schema.String, |
| 23 | + }), |
| 24 | + ), |
| 25 | +}) |
| 26 | + |
| 27 | +export const Plugin = define<HttpClient.HttpClient | Scope.Scope>({ |
| 28 | + id: "opencode.websearch.tavily", |
| 29 | + effect: Effect.fn("WebSearchTavily.Plugin")(function* (ctx) { |
| 30 | + const http = yield* HttpClient.HttpClient |
| 31 | + yield* ctx.integration.transform((draft) => { |
| 32 | + draft.update("tavily", (integration) => (integration.name = "Tavily")) |
| 33 | + draft.method.update({ |
| 34 | + integrationID: "tavily", |
| 35 | + method: { type: "key" }, |
| 36 | + }) |
| 37 | + draft.method.update({ |
| 38 | + integrationID: "tavily", |
| 39 | + method: { type: "env", names: ["TAVILY_API_KEY"] }, |
| 40 | + }) |
| 41 | + }) |
| 42 | + yield* ctx.websearch.transform((draft) => { |
| 43 | + draft.add({ |
| 44 | + id: "tavily", |
| 45 | + name: "Tavily", |
| 46 | + execute: (input) => |
| 47 | + Effect.gen(function* () { |
| 48 | + const connection = yield* ctx.integration.connection.active("tavily") |
| 49 | + const credential = connection ? yield* ctx.integration.connection.resolve(connection) : undefined |
| 50 | + const request = yield* HttpClientRequest.post(endpoint).pipe( |
| 51 | + HttpClientRequest.acceptJson, |
| 52 | + HttpClientRequest.setHeaders({ |
| 53 | + "User-Agent": App.useragent(ctx.app), |
| 54 | + "X-Client-Name": "opencode2", |
| 55 | + ...(credential?.type === "key" |
| 56 | + ? { Authorization: `Bearer ${credential.key}` } |
| 57 | + : { "X-Tavily-Access-Mode": "keyless" }), |
| 58 | + }), |
| 59 | + HttpClientRequest.schemaBodyJson(SearchRequest)({ |
| 60 | + query: input.query, |
| 61 | + search_depth: "basic", |
| 62 | + chunks_per_source: 3, |
| 63 | + max_results: 8, |
| 64 | + }), |
| 65 | + ) |
| 66 | + const response = yield* Effect.gen(function* () { |
| 67 | + const httpResponse = yield* HttpClient.filterStatusOk(http).execute(request) |
| 68 | + return yield* HttpClientResponse.schemaBodyJson(SearchResponse)(httpResponse) |
| 69 | + }).pipe( |
| 70 | + Effect.timeoutOrElse({ |
| 71 | + duration: Duration.seconds(25), |
| 72 | + orElse: () => Effect.fail(new Error("Tavily web search request timed out")), |
| 73 | + }), |
| 74 | + ) |
| 75 | + return response.results.map((item) => ({ |
| 76 | + url: item.url, |
| 77 | + title: item.title, |
| 78 | + ...(item.content ? { content: item.content } : {}), |
| 79 | + time: {}, |
| 80 | + })) |
| 81 | + }), |
| 82 | + }) |
| 83 | + }) |
| 84 | + }), |
| 85 | +}) |
0 commit comments