@umituz/web-ai 1.0.10 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1487,7 +1487,7 @@ var SEARCH_MAX_RESULTS = 5;
1487
1487
  async function searchWeb(env, input, fetchImpl = fetch) {
1488
1488
  const apiKey = env.TAVILY_API_KEY;
1489
1489
  if (!apiKey) {
1490
- throw new Error("Missing TAVILY_API_KEY configuration");
1490
+ throw new AiError("missing_key");
1491
1491
  }
1492
1492
  const maxResults = Math.min(
1493
1493
  Math.max(Math.trunc(input.maxResults ?? SEARCH_MAX_RESULTS), 1),
@@ -1497,34 +1497,52 @@ async function searchWeb(env, input, fetchImpl = fetch) {
1497
1497
  const baseUrl = (env.TAVILY_BASE_URL ?? "https://api.tavily.com").replace(/\/$/, "");
1498
1498
  const body = { query: input.query, max_results: maxResults, topic };
1499
1499
  if (input.includeAnswer) body.include_answer = true;
1500
- const res = await fetchImpl(`${baseUrl}/search`, {
1501
- method: "POST",
1502
- headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
1503
- body: JSON.stringify(body)
1504
- });
1500
+ let res;
1501
+ try {
1502
+ res = await fetchImpl(`${baseUrl}/search`, {
1503
+ method: "POST",
1504
+ headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
1505
+ body: JSON.stringify(body)
1506
+ });
1507
+ } catch (err) {
1508
+ throw errorFromUnknown(err);
1509
+ }
1505
1510
  if (!res.ok) {
1506
- let detail = `Tavily HTTP ${res.status}`;
1511
+ let detail;
1507
1512
  try {
1508
- const parsed2 = await res.json();
1509
- if (typeof parsed2?.detail === "string") detail = parsed2.detail;
1513
+ const parsed = await res.json();
1514
+ const detailField = parsed?.detail;
1515
+ if (Array.isArray(detailField)) {
1516
+ detail = "validation failed";
1517
+ } else if (detailField && typeof detailField === "object" && "error" in detailField) {
1518
+ const errorValue = detailField.error;
1519
+ if (typeof errorValue === "string") detail = errorValue;
1520
+ } else if (typeof detailField === "string") {
1521
+ detail = detailField;
1522
+ }
1510
1523
  } catch {
1524
+ detail = void 0;
1511
1525
  }
1512
- throw new Error(detail);
1526
+ throw errorFromStatus(res.status, detail);
1527
+ }
1528
+ try {
1529
+ const parsed = await res.json();
1530
+ const rawResults = Array.isArray(parsed.results) ? parsed.results : [];
1531
+ const results = rawResults.filter((row) => typeof row.url === "string" && row.url.length > 0).map((row) => ({
1532
+ title: typeof row.title === "string" ? row.title : "",
1533
+ url: row.url,
1534
+ content: typeof row.content === "string" ? row.content : "",
1535
+ score: typeof row.score === "number" ? row.score : null
1536
+ }));
1537
+ return {
1538
+ query: typeof parsed.query === "string" ? parsed.query : input.query,
1539
+ answer: typeof parsed.answer === "string" ? parsed.answer : null,
1540
+ results,
1541
+ responseTime: typeof parsed.response_time === "number" ? parsed.response_time : null
1542
+ };
1543
+ } catch (err) {
1544
+ throw errorFromUnknown(err);
1513
1545
  }
1514
- const parsed = await res.json();
1515
- const rawResults = Array.isArray(parsed.results) ? parsed.results : [];
1516
- const results = rawResults.filter((row) => typeof row.url === "string" && row.url.length > 0).map((row) => ({
1517
- title: typeof row.title === "string" ? row.title : "",
1518
- url: row.url,
1519
- content: typeof row.content === "string" ? row.content : "",
1520
- score: typeof row.score === "number" ? row.score : null
1521
- }));
1522
- return {
1523
- query: typeof parsed.query === "string" ? parsed.query : input.query,
1524
- answer: typeof parsed.answer === "string" ? parsed.answer : null,
1525
- results,
1526
- responseTime: typeof parsed.response_time === "number" ? parsed.response_time : null
1527
- };
1528
1546
  }
1529
1547
 
1530
1548
  // src/core/streaming/sse-response.ts