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