|
| 1 | +package com.android.everytalk.data.network |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import com.android.everytalk.data.DataClass.WebSearchResult |
| 5 | +import io.ktor.client.HttpClient |
| 6 | +import io.ktor.client.engine.okhttp.OkHttp |
| 7 | +import io.ktor.client.plugins.HttpRedirect |
| 8 | +import io.ktor.client.plugins.HttpTimeout |
| 9 | +import io.ktor.client.request.post |
| 10 | +import io.ktor.client.request.header |
| 11 | +import io.ktor.client.request.setBody |
| 12 | +import io.ktor.client.statement.bodyAsText |
| 13 | +import io.ktor.http.ContentType |
| 14 | +import io.ktor.http.HttpHeaders |
| 15 | +import io.ktor.http.isSuccess |
| 16 | +import io.ktor.http.contentType |
| 17 | +import kotlinx.coroutines.Dispatchers |
| 18 | +import kotlinx.coroutines.withContext |
| 19 | +import kotlinx.serialization.json.Json |
| 20 | +import kotlinx.serialization.json.JsonPrimitive |
| 21 | +import kotlinx.serialization.json.JsonElement |
| 22 | +import kotlinx.serialization.json.buildJsonObject |
| 23 | +import kotlinx.serialization.json.contentOrNull |
| 24 | +import kotlinx.serialization.json.jsonArray |
| 25 | +import kotlinx.serialization.json.jsonObject |
| 26 | +import kotlinx.serialization.json.jsonPrimitive |
| 27 | + |
| 28 | +data class ExternalWebSearchResponse( |
| 29 | + val provider: ExternalWebSearchProvider, |
| 30 | + val results: List<WebSearchResult>, |
| 31 | +) |
| 32 | + |
| 33 | +object ExternalWebSearchService { |
| 34 | + private const val TAG = "ExternalWebSearchService" |
| 35 | + private const val DEFAULT_TIMEOUT_MS = 30_000L |
| 36 | + private const val DEFAULT_RESULT_LIMIT = 5 |
| 37 | + |
| 38 | + private val json = Json { ignoreUnknownKeys = true } |
| 39 | + |
| 40 | + private val client by lazy { |
| 41 | + HttpClient(OkHttp) { |
| 42 | + install(HttpTimeout) { |
| 43 | + requestTimeoutMillis = DEFAULT_TIMEOUT_MS |
| 44 | + connectTimeoutMillis = DEFAULT_TIMEOUT_MS |
| 45 | + socketTimeoutMillis = DEFAULT_TIMEOUT_MS |
| 46 | + } |
| 47 | + install(HttpRedirect) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + suspend fun search( |
| 52 | + provider: ExternalWebSearchProvider, |
| 53 | + apiKey: String, |
| 54 | + query: String, |
| 55 | + ): Result<ExternalWebSearchResponse> = withContext(Dispatchers.IO) { |
| 56 | + runCatching { |
| 57 | + val trimmedQuery = query.trim() |
| 58 | + require(trimmedQuery.isNotBlank()) { "搜索内容不能为空" } |
| 59 | + require(apiKey.isNotBlank()) { "${provider.displayName} API Key 未配置" } |
| 60 | + |
| 61 | + val response = client.post(provider.baseUrl) { |
| 62 | + contentType(ContentType.Application.Json) |
| 63 | + when (provider) { |
| 64 | + ExternalWebSearchProvider.TAVILY -> { |
| 65 | + header(HttpHeaders.Authorization, "Bearer $apiKey") |
| 66 | + setBody( |
| 67 | + buildJsonObject { |
| 68 | + put("query", JsonPrimitive(trimmedQuery)) |
| 69 | + put("search_depth", JsonPrimitive("basic")) |
| 70 | + put("max_results", JsonPrimitive(DEFAULT_RESULT_LIMIT)) |
| 71 | + put("include_answer", JsonPrimitive(false)) |
| 72 | + put("include_images", JsonPrimitive(false)) |
| 73 | + }.toString() |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + ExternalWebSearchProvider.EXA -> { |
| 78 | + header("x-api-key", apiKey) |
| 79 | + setBody( |
| 80 | + buildJsonObject { |
| 81 | + put("query", JsonPrimitive(trimmedQuery)) |
| 82 | + put("type", JsonPrimitive("auto")) |
| 83 | + put("numResults", JsonPrimitive(DEFAULT_RESULT_LIMIT)) |
| 84 | + put( |
| 85 | + "contents", |
| 86 | + buildJsonObject { |
| 87 | + put( |
| 88 | + "highlights", |
| 89 | + buildJsonObject { |
| 90 | + put("maxCharacters", JsonPrimitive(1200)) |
| 91 | + } |
| 92 | + ) |
| 93 | + } |
| 94 | + ) |
| 95 | + }.toString() |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + ExternalWebSearchProvider.BOCHA -> { |
| 100 | + header(HttpHeaders.Authorization, "Bearer $apiKey") |
| 101 | + setBody( |
| 102 | + buildJsonObject { |
| 103 | + put("query", JsonPrimitive(trimmedQuery)) |
| 104 | + put("freshness", JsonPrimitive("oneYear")) |
| 105 | + put("summary", JsonPrimitive(true)) |
| 106 | + put("count", JsonPrimitive(DEFAULT_RESULT_LIMIT)) |
| 107 | + }.toString() |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + ExternalWebSearchProvider.SERPAPI -> { |
| 112 | + setBody( |
| 113 | + buildJsonObject { |
| 114 | + put("q", JsonPrimitive(trimmedQuery)) |
| 115 | + put("api_key", JsonPrimitive(apiKey)) |
| 116 | + put("engine", JsonPrimitive("google")) |
| 117 | + put("num", JsonPrimitive(DEFAULT_RESULT_LIMIT)) |
| 118 | + put("hl", JsonPrimitive("zh-cn")) |
| 119 | + put("google_domain", JsonPrimitive("google.com")) |
| 120 | + }.toString() |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (!response.status.isSuccess()) { |
| 127 | + throw IllegalStateException("${provider.displayName} 搜索失败: HTTP ${response.status.value}") |
| 128 | + } |
| 129 | + |
| 130 | + val body = response.bodyAsText() |
| 131 | + val results = parseResults(provider, body) |
| 132 | + if (results.isEmpty()) { |
| 133 | + Log.w(TAG, "provider=${provider.providerId} empty results body=${body.take(1000)}") |
| 134 | + throw IllegalStateException("${provider.displayName} 未返回可用搜索结果") |
| 135 | + } |
| 136 | + ExternalWebSearchResponse(provider = provider, results = results) |
| 137 | + }.onFailure { |
| 138 | + Log.e(TAG, "搜索失败 provider=${provider.providerId}", it) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private fun parseResults( |
| 143 | + provider: ExternalWebSearchProvider, |
| 144 | + body: String, |
| 145 | + ): List<WebSearchResult> { |
| 146 | + val root = json.parseToJsonElement(body).jsonObject |
| 147 | + return when (provider) { |
| 148 | + ExternalWebSearchProvider.TAVILY -> root["results"]?.jsonArray.orEmpty().mapIndexedNotNull { index, element -> |
| 149 | + val item = element.jsonObject |
| 150 | + val href = item["url"]?.jsonPrimitive?.contentOrNull.orEmpty() |
| 151 | + if (href.isBlank()) return@mapIndexedNotNull null |
| 152 | + WebSearchResult( |
| 153 | + index = index, |
| 154 | + title = item["title"]?.jsonPrimitive?.contentOrNull.orEmpty().ifBlank { href }, |
| 155 | + href = href, |
| 156 | + snippet = item["content"]?.jsonPrimitive?.contentOrNull.orEmpty(), |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + ExternalWebSearchProvider.EXA -> root["results"]?.jsonArray.orEmpty().mapIndexedNotNull { index, element -> |
| 161 | + val item = element.asJsonObjectOrNull() ?: return@mapIndexedNotNull null |
| 162 | + val href = item["url"]?.jsonPrimitive?.contentOrNull.orEmpty() |
| 163 | + if (href.isBlank()) return@mapIndexedNotNull null |
| 164 | + WebSearchResult( |
| 165 | + index = index, |
| 166 | + title = item["title"]?.jsonPrimitive?.contentOrNull.orEmpty().ifBlank { href }, |
| 167 | + href = href, |
| 168 | + snippet = item["text"]?.jsonPrimitive?.contentOrNull |
| 169 | + ?: extractExaHighlightText(item["highlights"]) |
| 170 | + ?: item["summary"]?.jsonPrimitive?.contentOrNull |
| 171 | + ?: "", |
| 172 | + ) |
| 173 | + } |
| 174 | + |
| 175 | + ExternalWebSearchProvider.BOCHA -> root["data"]?.jsonObject?.get("webPages")?.jsonObject |
| 176 | + ?.get("value")?.jsonArray.orEmpty().mapIndexedNotNull { index, element -> |
| 177 | + val item = element.jsonObject |
| 178 | + val href = item["url"]?.jsonPrimitive?.contentOrNull.orEmpty() |
| 179 | + if (href.isBlank()) return@mapIndexedNotNull null |
| 180 | + WebSearchResult( |
| 181 | + index = index, |
| 182 | + title = item["name"]?.jsonPrimitive?.contentOrNull.orEmpty().ifBlank { href }, |
| 183 | + href = href, |
| 184 | + snippet = item["snippet"]?.jsonPrimitive?.contentOrNull.orEmpty(), |
| 185 | + ) |
| 186 | + } |
| 187 | + |
| 188 | + ExternalWebSearchProvider.SERPAPI -> root["organic_results"]?.jsonArray.orEmpty().mapIndexedNotNull { index, element -> |
| 189 | + val item = element.jsonObject |
| 190 | + val href = item["link"]?.jsonPrimitive?.contentOrNull.orEmpty() |
| 191 | + if (href.isBlank()) return@mapIndexedNotNull null |
| 192 | + WebSearchResult( |
| 193 | + index = index, |
| 194 | + title = item["title"]?.jsonPrimitive?.contentOrNull.orEmpty().ifBlank { href }, |
| 195 | + href = href, |
| 196 | + snippet = item["snippet"]?.jsonPrimitive?.contentOrNull.orEmpty(), |
| 197 | + ) |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private fun JsonElement.asJsonObjectOrNull() = runCatching { jsonObject }.getOrNull() |
| 203 | + |
| 204 | + private fun extractExaHighlightText(highlightsElement: JsonElement?): String? { |
| 205 | + val highlights = runCatching { highlightsElement?.jsonArray }.getOrNull().orEmpty() |
| 206 | + if (highlights.isEmpty()) return null |
| 207 | + |
| 208 | + return highlights.firstNotNullOfOrNull { highlight -> |
| 209 | + highlight.jsonPrimitive.contentOrNull |
| 210 | + ?: highlight.asJsonObjectOrNull() |
| 211 | + ?.get("text")?.jsonPrimitive?.contentOrNull |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments