Skip to content

Commit 3d3bbd4

Browse files
authored
feat(core): add tavily web search (#42591)
1 parent 51f0b49 commit 3d3bbd4

6 files changed

Lines changed: 165 additions & 2 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { WebSearchExa } from "./exa.js"
22
import { WebSearchFirecrawl } from "./firecrawl.js"
33
import { WebSearchParallel } from "./parallel.js"
4+
import { WebSearchTavily } from "./tavily.js"
45

5-
export const WebSearchPlugins = [WebSearchExa.Plugin, WebSearchFirecrawl.Plugin, WebSearchParallel.Plugin] as const
6+
export const WebSearchPlugins = [
7+
WebSearchExa.Plugin,
8+
WebSearchFirecrawl.Plugin,
9+
WebSearchParallel.Plugin,
10+
WebSearchTavily.Plugin,
11+
] as const
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
})

‎packages/core/test/plugin/websearch.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { WebSearch } from "@opencode-ai/core/websearch"
55
import { WebSearchExa } from "@opencode-ai/core/plugin/websearch/exa"
66
import { WebSearchFirecrawl } from "@opencode-ai/core/plugin/websearch/firecrawl"
77
import { WebSearchParallel } from "@opencode-ai/core/plugin/websearch/parallel"
8+
import { WebSearchTavily } from "@opencode-ai/core/plugin/websearch/tavily"
89
import { host, integrationHost, webSearchHost } from "./host"
910
import { requests, resetWebSearchFixture, webSearchIntegrationTest } from "./websearch-fixture"
1011

@@ -190,4 +191,71 @@ describe("built-in web search providers", () => {
190191
expect(JSON.stringify(output)).not.toContain("parallel-secret")
191192
}),
192193
)
194+
195+
it.effect("registers Tavily with keyless and keyed Search API access", () =>
196+
Effect.gen(function* () {
197+
resetWebSearchFixture(
198+
JSON.stringify({
199+
query: "effect typescript",
200+
results: [
201+
{
202+
url: "https://effect.website",
203+
title: "Effect",
204+
content: "Effect documentation",
205+
score: 0.99,
206+
},
207+
],
208+
}),
209+
)
210+
const integrations = yield* Integration.Service
211+
const websearch = yield* WebSearch.Service
212+
yield* WebSearchTavily.Plugin.effect(
213+
host({ integration: integrationHost(integrations), websearch: webSearchHost(websearch) }),
214+
)
215+
216+
expect(yield* integrations.get(Integration.ID.make("tavily"))).toMatchObject({
217+
id: "tavily",
218+
name: "Tavily",
219+
methods: [{ type: "key" }, { type: "env", names: ["TAVILY_API_KEY"] }],
220+
})
221+
const query = {
222+
query: "effect typescript",
223+
providerID: WebSearch.ID.make("tavily"),
224+
}
225+
expect(yield* websearch.query(query)).toEqual(
226+
new WebSearch.Response({
227+
providerID: WebSearch.ID.make("tavily"),
228+
results: [
229+
{
230+
url: "https://effect.website",
231+
title: "Effect",
232+
content: "Effect documentation",
233+
time: {},
234+
},
235+
],
236+
}),
237+
)
238+
expect(requests[0]).toMatchObject({
239+
url: WebSearchTavily.endpoint,
240+
headers: { "x-client-name": "opencode2", "x-tavily-access-mode": "keyless" },
241+
body: {
242+
query: "effect typescript",
243+
search_depth: "basic",
244+
chunks_per_source: 3,
245+
max_results: 8,
246+
},
247+
})
248+
expect(requests[0]?.headers.authorization).toBeUndefined()
249+
250+
yield* integrations.connection.key({
251+
integrationID: Integration.ID.make("tavily"),
252+
key: "tavily-secret",
253+
})
254+
yield* websearch.query(query)
255+
expect(requests[1]).toMatchObject({
256+
headers: { authorization: "Bearer tavily-secret", "x-client-name": "opencode2" },
257+
})
258+
expect(requests[1]?.headers["x-tavily-access-mode"]).toBeUndefined()
259+
}),
260+
)
193261
})

‎packages/session-ui/src/components/message-part.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ function webSearchProviderLabel(provider: unknown, i18n: ReturnType<typeof useI1
467467
? "Exa"
468468
: provider === "firecrawl"
469469
? "Firecrawl"
470-
: undefined
470+
: provider === "tavily"
471+
? "Tavily"
472+
: undefined
471473
if (name) return i18n.t("ui.tool.websearch.provider", { provider: name })
472474
return i18n.t("ui.tool.websearch")
473475
}

‎packages/tui/src/util/tool-display.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function webSearchProviderLabel(provider: unknown) {
2323
if (provider === "parallel") return "Parallel Web Search"
2424
if (provider === "exa") return "Exa Web Search"
2525
if (provider === "firecrawl") return "Firecrawl Web Search"
26+
if (provider === "tavily") return "Tavily Web Search"
2627
return "Web Search"
2728
}
2829

‎packages/tui/test/util/tool-display.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe("webSearchProviderLabel", () => {
2323
test("labels known providers", () => {
2424
expect(webSearchProviderLabel("parallel")).toBe("Parallel Web Search")
2525
expect(webSearchProviderLabel("exa")).toBe("Exa Web Search")
26+
expect(webSearchProviderLabel("tavily")).toBe("Tavily Web Search")
2627
})
2728

2829
for (const [name, provider] of [

0 commit comments

Comments
 (0)