mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
refactor(core): simplify websearch tool results (#64471)
This commit is contained in:
committed by
GitHub
Unverified
parent
30654dae6c
commit
8a30e585a6
@@ -25,10 +25,14 @@ export const Input = Schema.Struct({
|
||||
query: Schema.String.annotate({ description: "Websearch query" }),
|
||||
})
|
||||
|
||||
const Output = Schema.Struct({
|
||||
provider: WebSearch.ID,
|
||||
results: Schema.Array(WebSearch.Result),
|
||||
})
|
||||
const Output = Schema.Array(
|
||||
Schema.Struct({
|
||||
url: WebSearch.Result.fields.url,
|
||||
title: WebSearch.Result.fields.title,
|
||||
content: WebSearch.Result.fields.content,
|
||||
published: WebSearch.Result.fields.time.fields.published,
|
||||
}),
|
||||
)
|
||||
export const Plugin = {
|
||||
id: "opencode.tool.websearch",
|
||||
effect: Effect.fn("WebSearchTool.Plugin")(function* (ctx: Context) {
|
||||
@@ -144,12 +148,14 @@ export const Plugin = {
|
||||
)
|
||||
}),
|
||||
)
|
||||
const output = {
|
||||
provider: result.providerID,
|
||||
results: result.results,
|
||||
}
|
||||
const content = output.results.length
|
||||
? output.results
|
||||
const output = result.results.map((hit) => ({
|
||||
url: hit.url,
|
||||
...(hit.title === undefined ? {} : { title: hit.title }),
|
||||
...(hit.content === undefined ? {} : { content: hit.content }),
|
||||
...(hit.time.published === undefined ? {} : { published: hit.time.published }),
|
||||
}))
|
||||
const content = result.results.length
|
||||
? result.results
|
||||
.map((result) => {
|
||||
const title = result.title ?? result.url
|
||||
const published = result.time.published
|
||||
@@ -159,7 +165,7 @@ export const Plugin = {
|
||||
})
|
||||
.join("\n\n")
|
||||
: NO_RESULTS
|
||||
return { output, content, metadata: { provider: output.provider } }
|
||||
return { output, content, metadata: { provider: result.providerID } }
|
||||
}).pipe(
|
||||
Effect.mapError((error) => {
|
||||
const fallback = `Unable to search the web for ${input.query}`
|
||||
|
||||
@@ -126,6 +126,7 @@ describe("WebSearchTool registration", () => {
|
||||
}),
|
||||
).toMatchObject({
|
||||
status: "completed",
|
||||
output: [{ url: "https://example.com", title: "Search results", content: "search results" }],
|
||||
content: [{ type: "text", text: "## [Search results](https://example.com)\n\nsearch results" }],
|
||||
})
|
||||
expect(fixture.assertions).toMatchObject([
|
||||
@@ -148,7 +149,7 @@ describe("WebSearchTool registration", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("keeps normalized results in structured output", () =>
|
||||
it.effect("returns flat results and keeps the provider in metadata", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* setup
|
||||
yield* fixture.websearch.select(WebSearch.ID.make("parallel"))
|
||||
@@ -170,17 +171,14 @@ describe("WebSearchTool registration", () => {
|
||||
}),
|
||||
).toEqual({
|
||||
status: "completed",
|
||||
output: {
|
||||
provider: "parallel",
|
||||
results: [
|
||||
{
|
||||
url: "https://effect.website",
|
||||
title: "Effect",
|
||||
content: "parallel results",
|
||||
time: { published: Date.parse("2026-07-25T00:00:00.000Z") },
|
||||
},
|
||||
],
|
||||
},
|
||||
output: [
|
||||
{
|
||||
url: "https://effect.website",
|
||||
title: "Effect",
|
||||
content: "parallel results",
|
||||
published: Date.parse("2026-07-25T00:00:00.000Z"),
|
||||
},
|
||||
],
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
@@ -207,6 +205,7 @@ describe("WebSearchTool registration", () => {
|
||||
}),
|
||||
).toMatchObject({
|
||||
status: "completed",
|
||||
output: [],
|
||||
content: [{ type: "text", text: WebSearchTool.NO_RESULTS }],
|
||||
})
|
||||
}),
|
||||
@@ -419,7 +418,7 @@ describe("WebSearchTool registration", () => {
|
||||
const replacement = WebSearch.ID.make(first === "exa" ? "parallel" : "exa")
|
||||
expect(progress).toEqual([{ provider: first }, { provider: replacement }])
|
||||
expect(result).toMatchObject({
|
||||
output: { provider: replacement, results: fixture.results },
|
||||
output: [{ url: "https://example.com", title: "Search results", content: "search results" }],
|
||||
metadata: { provider: replacement },
|
||||
})
|
||||
expect(fixture.formRequests).toEqual([])
|
||||
|
||||
Reference in New Issue
Block a user