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