@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.
@@ -1485,7 +1485,7 @@ var SEARCH_MAX_RESULTS = 5;
1485
1485
  async function searchWeb(env, input, fetchImpl = fetch) {
1486
1486
  const apiKey = env.TAVILY_API_KEY;
1487
1487
  if (!apiKey) {
1488
- throw new Error("Missing TAVILY_API_KEY configuration");
1488
+ throw new AiError("missing_key");
1489
1489
  }
1490
1490
  const maxResults = Math.min(
1491
1491
  Math.max(Math.trunc(input.maxResults ?? SEARCH_MAX_RESULTS), 1),
@@ -1495,34 +1495,52 @@ async function searchWeb(env, input, fetchImpl = fetch) {
1495
1495
  const baseUrl = (env.TAVILY_BASE_URL ?? "https://api.tavily.com").replace(/\/$/, "");
1496
1496
  const body = { query: input.query, max_results: maxResults, topic };
1497
1497
  if (input.includeAnswer) body.include_answer = true;
1498
- const res = await fetchImpl(`${baseUrl}/search`, {
1499
- method: "POST",
1500
- headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
1501
- body: JSON.stringify(body)
1502
- });
1498
+ let res;
1499
+ try {
1500
+ res = await fetchImpl(`${baseUrl}/search`, {
1501
+ method: "POST",
1502
+ headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
1503
+ body: JSON.stringify(body)
1504
+ });
1505
+ } catch (err) {
1506
+ throw errorFromUnknown(err);
1507
+ }
1503
1508
  if (!res.ok) {
1504
- let detail = `Tavily HTTP ${res.status}`;
1509
+ let detail;
1505
1510
  try {
1506
- const parsed2 = await res.json();
1507
- if (typeof parsed2?.detail === "string") detail = parsed2.detail;
1511
+ const parsed = await res.json();
1512
+ const detailField = parsed?.detail;
1513
+ if (Array.isArray(detailField)) {
1514
+ detail = "validation failed";
1515
+ } else if (detailField && typeof detailField === "object" && "error" in detailField) {
1516
+ const errorValue = detailField.error;
1517
+ if (typeof errorValue === "string") detail = errorValue;
1518
+ } else if (typeof detailField === "string") {
1519
+ detail = detailField;
1520
+ }
1508
1521
  } catch {
1522
+ detail = void 0;
1509
1523
  }
1510
- throw new Error(detail);
1524
+ throw errorFromStatus(res.status, detail);
1525
+ }
1526
+ try {
1527
+ const parsed = await res.json();
1528
+ const rawResults = Array.isArray(parsed.results) ? parsed.results : [];
1529
+ const results = rawResults.filter((row) => typeof row.url === "string" && row.url.length > 0).map((row) => ({
1530
+ title: typeof row.title === "string" ? row.title : "",
1531
+ url: row.url,
1532
+ content: typeof row.content === "string" ? row.content : "",
1533
+ score: typeof row.score === "number" ? row.score : null
1534
+ }));
1535
+ return {
1536
+ query: typeof parsed.query === "string" ? parsed.query : input.query,
1537
+ answer: typeof parsed.answer === "string" ? parsed.answer : null,
1538
+ results,
1539
+ responseTime: typeof parsed.response_time === "number" ? parsed.response_time : null
1540
+ };
1541
+ } catch (err) {
1542
+ throw errorFromUnknown(err);
1511
1543
  }
1512
- const parsed = await res.json();
1513
- const rawResults = Array.isArray(parsed.results) ? parsed.results : [];
1514
- const results = rawResults.filter((row) => typeof row.url === "string" && row.url.length > 0).map((row) => ({
1515
- title: typeof row.title === "string" ? row.title : "",
1516
- url: row.url,
1517
- content: typeof row.content === "string" ? row.content : "",
1518
- score: typeof row.score === "number" ? row.score : null
1519
- }));
1520
- return {
1521
- query: typeof parsed.query === "string" ? parsed.query : input.query,
1522
- answer: typeof parsed.answer === "string" ? parsed.answer : null,
1523
- results,
1524
- responseTime: typeof parsed.response_time === "number" ? parsed.response_time : null
1525
- };
1526
1544
  }
1527
1545
 
1528
1546
  // src/core/streaming/sse-response.ts