|
1 | 1 | import type { OrchestrationV2TurnItem, OrchestrationV2TurnItemStatus } from "@t3tools/contracts"; |
| 2 | +import * as Option from "effect/Option"; |
2 | 3 | import * as Schema from "effect/Schema"; |
3 | 4 |
|
4 | 5 | import type { MuseItem } from "./protocol.ts"; |
5 | 6 |
|
6 | 7 | const decodeToolArgs = Schema.decodeUnknownSync( |
7 | 8 | Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)), |
8 | 9 | ); |
| 10 | +/** Muse's `web_search` tool output: the query and its hits, as JSON. */ |
| 11 | +const decodeWebSearchOutput = Schema.decodeUnknownOption( |
| 12 | + Schema.fromJsonString( |
| 13 | + Schema.Struct({ |
| 14 | + results: Schema.Array( |
| 15 | + Schema.Struct({ |
| 16 | + url: Schema.optional(Schema.String), |
| 17 | + title: Schema.optional(Schema.String), |
| 18 | + snippet: Schema.optional(Schema.String), |
| 19 | + }), |
| 20 | + ), |
| 21 | + }), |
| 22 | + ), |
| 23 | +); |
9 | 24 |
|
10 | 25 | type Fields< |
11 | 26 | T extends OrchestrationV2TurnItem["type"], |
@@ -68,6 +83,7 @@ export function museToolPresentation( |
68 | 83 | item.result?.summary ?? |
69 | 84 | item.text ?? |
70 | 85 | item.summary?.join("\n") ?? |
| 86 | + item.finalSummary ?? |
71 | 87 | item.fallbackText ?? |
72 | 88 | item.objective ?? |
73 | 89 | ""; |
@@ -100,11 +116,25 @@ export function museToolPresentation( |
100 | 116 | } |
101 | 117 | if (item.kind === "toolCall" && /web|browse|fetch/i.test(toolName)) { |
102 | 118 | const query = stringArg("query", "url", "pattern")?.trim(); |
| 119 | + const pageUrl = stringArg("url")?.trim(); |
| 120 | + // A search lists its hits; a page fetch (or a failure) shows its text under the page's link. |
| 121 | + const hits = |
| 122 | + item.tool === "web_search" && !detail |
| 123 | + ? Option.getOrUndefined(decodeWebSearchOutput(text))?.results.map( |
| 124 | + ({ url, title, snippet }) => ({ |
| 125 | + ...(url?.trim() ? { url: url.trim() } : {}), |
| 126 | + ...(title?.trim() ? { title: title.trim() } : {}), |
| 127 | + ...(snippet ? { snippet } : {}), |
| 128 | + }), |
| 129 | + ) |
| 130 | + : undefined; |
| 131 | + const results = |
| 132 | + hits ?? (output ? [{ ...(pageUrl ? { url: pageUrl } : {}), snippet: output }] : []); |
103 | 133 | return { |
104 | 134 | ...shared, |
105 | 135 | type: "web_search", |
106 | 136 | ...(query ? { patterns: [query] } : {}), |
107 | | - ...(output ? { results: [{ snippet: output }] } : {}), |
| 137 | + ...(results.length ? { results } : {}), |
108 | 138 | }; |
109 | 139 | } |
110 | 140 | return { |
|
0 commit comments