pendra 0.17.0 → 0.17.2

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.mjs CHANGED
@@ -578,8 +578,14 @@ async function parseCompletion(response) {
578
578
  body
579
579
  });
580
580
  }
581
+ throwIfInBandError(parsed, "The server reported an error");
581
582
  return completionFrom(parsed, body, response.status);
582
583
  }
584
+ async function readJsonReply(response) {
585
+ const parsed = await response.json();
586
+ throwIfInBandError(parsed, "The server reported an error");
587
+ return parsed;
588
+ }
583
589
  function parseDecryptedCompletion(json, status) {
584
590
  let parsed;
585
591
  try {
@@ -1114,7 +1120,7 @@ var Transcriptions = class {
1114
1120
  const text = await response.text();
1115
1121
  return withPlacement({ text }, response.headers);
1116
1122
  }
1117
- const result = await response.json();
1123
+ const result = await readJsonReply(response);
1118
1124
  return withPlacement(result, response.headers, result.pendra);
1119
1125
  }
1120
1126
  /**
@@ -1515,22 +1521,10 @@ var Completions = class {
1515
1521
  "/api/v1/chat/completions",
1516
1522
  { method: "POST", body: topLevel, headers }
1517
1523
  );
1518
- const raw = await response.text();
1519
- let wrapper;
1520
- try {
1521
- wrapper = JSON.parse(raw);
1522
- } catch {
1523
- throw new APIResponseValidationError("Response was not valid JSON", {
1524
- status: response.status,
1525
- body: raw
1526
- });
1527
- }
1528
- if (!wrapper.pendra_encrypted) {
1529
- throw unsealedReplyError();
1530
- }
1531
- const plain = decryptor.openResult(wrapper.pendra_encrypted);
1524
+ const { inner, placement } = await readSealedReply(response);
1525
+ const plain = decryptor.openResult(inner);
1532
1526
  const completion = parseDecryptedCompletion(DECODER3.decode(plain), response.status);
1533
- return withPlacement(completion, response.headers, completion.pendra, wrapper.pendra);
1527
+ return withPlacement(completion, response.headers, completion.pendra, placement);
1534
1528
  }
1535
1529
  };
1536
1530
 
@@ -1599,7 +1593,7 @@ var Embeddings = class {
1599
1593
  "/api/v1/embeddings",
1600
1594
  { method: "POST", body, headers }
1601
1595
  );
1602
- const result = await response.json();
1596
+ const result = await readJsonReply(response);
1603
1597
  return withPlacement(result, response.headers, result.pendra);
1604
1598
  }
1605
1599
  async createEncrypted(model, body, headers) {
@@ -1614,13 +1608,10 @@ var Embeddings = class {
1614
1608
  "/api/v1/embeddings",
1615
1609
  { method: "POST", body: topLevel, headers }
1616
1610
  );
1617
- const wrapper = await response.json();
1618
- if (!wrapper.pendra_encrypted) {
1619
- throw unsealedReplyError();
1620
- }
1621
- const plain = decryptor.openResult(wrapper.pendra_encrypted);
1611
+ const { inner, placement } = await readSealedReply(response);
1612
+ const plain = decryptor.openResult(inner);
1622
1613
  const result = JSON.parse(DECODER4.decode(plain));
1623
- return withPlacement(result, response.headers, result.pendra, wrapper.pendra);
1614
+ return withPlacement(result, response.headers, result.pendra, placement);
1624
1615
  }
1625
1616
  };
1626
1617
 
@@ -1663,7 +1654,7 @@ var Generations = class {
1663
1654
  "/api/v1/images/generations",
1664
1655
  { method: "POST", body, headers }
1665
1656
  );
1666
- const result = await response.json();
1657
+ const result = await readJsonReply(response);
1667
1658
  return withPlacement(result, response.headers, result.pendra);
1668
1659
  }
1669
1660
  async createEncrypted(model, body, headers) {
@@ -1766,7 +1757,7 @@ var Rerank = class {
1766
1757
  "/api/v1/rerank",
1767
1758
  { method: "POST", body, headers }
1768
1759
  );
1769
- const result = await response.json();
1760
+ const result = await readJsonReply(response);
1770
1761
  return withPlacement(result, response.headers, result.pendra);
1771
1762
  }
1772
1763
  async createEncrypted(model, body, headers) {
@@ -1781,18 +1772,15 @@ var Rerank = class {
1781
1772
  "/api/v1/rerank",
1782
1773
  { method: "POST", body: topLevel, headers }
1783
1774
  );
1784
- const wrapper = await response.json();
1785
- if (!wrapper.pendra_encrypted) {
1786
- throw unsealedReplyError();
1787
- }
1788
- const plain = decryptor.openResult(wrapper.pendra_encrypted);
1775
+ const { inner, placement } = await readSealedReply(response);
1776
+ const plain = decryptor.openResult(inner);
1789
1777
  const result = JSON.parse(DECODER6.decode(plain));
1790
- return withPlacement(result, response.headers, result.pendra, wrapper.pendra);
1778
+ return withPlacement(result, response.headers, result.pendra, placement);
1791
1779
  }
1792
1780
  };
1793
1781
 
1794
1782
  // src/version.ts
1795
- var VERSION = "0.17.0";
1783
+ var VERSION = "0.17.2";
1796
1784
  var USER_AGENT = `pendra-node/${VERSION}`;
1797
1785
 
1798
1786
  // src/client.ts
@@ -1899,8 +1887,10 @@ var Pendra = class {
1899
1887
  clearTimeout(timeoutId);
1900
1888
  }
1901
1889
  if (response.status === 404) {
1890
+ const body = await readErrorBody(response);
1891
+ const detail = typeof body === "object" && body !== null ? body.detail : void 0;
1902
1892
  throw new PrivateInferenceError(
1903
- "No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
1893
+ messageFromDetail(detail) ?? "No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
1904
1894
  );
1905
1895
  }
1906
1896
  if (!response.ok) {