@botpress/cognitive 0.1.50 → 0.2.1

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.
@@ -1,28 +1,28 @@
1
1
 
2
- > @botpress/cognitive@0.1.50 build /home/runner/work/botpress/botpress/packages/cognitive
2
+ > @botpress/cognitive@0.2.1 build /home/runner/work/botpress/botpress/packages/cognitive
3
3
  > pnpm build:type && pnpm build:neutral && size-limit
4
4
 
5
5
 
6
- > @botpress/cognitive@0.1.50 build:type /home/runner/work/botpress/botpress/packages/cognitive
6
+ > @botpress/cognitive@0.2.1 build:type /home/runner/work/botpress/botpress/packages/cognitive
7
7
  > tsup --tsconfig tsconfig.build.json ./src/index.ts --dts-resolve --dts-only --clean
8
8
 
9
9
  CLI Building entry: ./src/index.ts
10
10
  CLI Using tsconfig: tsconfig.build.json
11
11
  CLI tsup v8.0.2
12
12
  DTS Build start
13
- DTS ⚡️ Build success in 9361ms
14
- DTS dist/index.d.ts 631.36 KB
13
+ DTS ⚡️ Build success in 4315ms
14
+ DTS dist/index.d.ts 632.56 KB
15
15
 
16
- > @botpress/cognitive@0.1.50 build:neutral /home/runner/work/botpress/botpress/packages/cognitive
16
+ > @botpress/cognitive@0.2.1 build:neutral /home/runner/work/botpress/botpress/packages/cognitive
17
17
  > ts-node -T ./build.ts --neutral
18
18
 
19
19
  Done
20
20
 
21
21
  dist/index.cjs
22
22
  Size limit: 50 kB
23
- Size: 14.74 kB brotlied
23
+ Size: 15.02 kB brotlied
24
24
 
25
25
  dist/index.mjs
26
26
  Size limit: 50 kB
27
- Size: 14.58 kB brotlied
27
+ Size: 14.88 kB brotlied
28
28
 
package/dist/index.cjs CHANGED
@@ -1763,6 +1763,7 @@ var CognitiveBeta = class _CognitiveBeta {
1763
1763
  _withCredentials;
1764
1764
  _headers;
1765
1765
  _debug = false;
1766
+ _events = createNanoEvents();
1766
1767
  constructor(props) {
1767
1768
  this._apiUrl = props.apiUrl || "https://api.botpress.cloud";
1768
1769
  this._timeout = props.timeout || 60001;
@@ -1793,15 +1794,28 @@ var CognitiveBeta = class _CognitiveBeta {
1793
1794
  debug: this._debug
1794
1795
  });
1795
1796
  }
1797
+ on(event, cb) {
1798
+ return this._events.on(event, cb);
1799
+ }
1796
1800
  async generateText(input, options = {}) {
1797
1801
  const signal = options.signal ?? AbortSignal.timeout(this._timeout);
1798
- const { data } = await this._withServerRetry(
1799
- () => this._axiosClient.post("/v2/cognitive/generate-text", input, {
1800
- signal,
1801
- timeout: options.timeout ?? this._timeout
1802
- })
1803
- );
1804
- return data;
1802
+ const req = { input };
1803
+ this._events.emit("request", req);
1804
+ try {
1805
+ const { data } = await this._withServerRetry(
1806
+ () => this._axiosClient.post("/v2/cognitive/generate-text", input, {
1807
+ signal,
1808
+ timeout: options.timeout ?? this._timeout
1809
+ }),
1810
+ options,
1811
+ req
1812
+ );
1813
+ this._events.emit("response", req, data);
1814
+ return data;
1815
+ } catch (error) {
1816
+ this._events.emit("error", req, error);
1817
+ throw error;
1818
+ }
1805
1819
  }
1806
1820
  async listModels() {
1807
1821
  const { data } = await this._withServerRetry(
@@ -1811,61 +1825,88 @@ var CognitiveBeta = class _CognitiveBeta {
1811
1825
  }
1812
1826
  async *generateTextStream(request, options = {}) {
1813
1827
  const signal = options.signal ?? AbortSignal.timeout(this._timeout);
1814
- if (isBrowser()) {
1815
- const res2 = await fetch(`${this._apiUrl}/v2/cognitive/generate-text-stream`, {
1816
- method: "POST",
1817
- headers: {
1818
- ...this._headers,
1819
- "Content-Type": "application/json"
1820
- },
1821
- credentials: this._withCredentials ? "include" : "omit",
1822
- body: JSON.stringify({ ...request, stream: true }),
1823
- signal
1824
- });
1825
- if (!res2.ok) {
1826
- const text = await res2.text().catch(() => "");
1827
- const err = new Error(`HTTP ${res2.status}: ${text || res2.statusText}`);
1828
- err.response = { status: res2.status, data: text };
1829
- throw err;
1828
+ const req = { input: request };
1829
+ const chunks = [];
1830
+ let lastChunk;
1831
+ this._events.emit("request", req);
1832
+ try {
1833
+ if (isBrowser()) {
1834
+ const res2 = await fetch(`${this._apiUrl}/v2/cognitive/generate-text-stream`, {
1835
+ method: "POST",
1836
+ headers: {
1837
+ ...this._headers,
1838
+ "Content-Type": "application/json"
1839
+ },
1840
+ credentials: this._withCredentials ? "include" : "omit",
1841
+ body: JSON.stringify({ ...request, stream: true }),
1842
+ signal
1843
+ });
1844
+ if (!res2.ok) {
1845
+ const text = await res2.text().catch(() => "");
1846
+ const err = new Error(`HTTP ${res2.status}: ${text || res2.statusText}`);
1847
+ err.response = { status: res2.status, data: text };
1848
+ throw err;
1849
+ }
1850
+ const body = res2.body;
1851
+ if (!body) {
1852
+ throw new Error("No response body received for streaming request");
1853
+ }
1854
+ const reader = body.getReader();
1855
+ const iterable = (async function* () {
1856
+ for (; ; ) {
1857
+ const { value, done } = await reader.read();
1858
+ if (done) {
1859
+ break;
1860
+ }
1861
+ if (value) {
1862
+ yield value;
1863
+ }
1864
+ }
1865
+ })();
1866
+ for await (const obj of this._ndjson(iterable)) {
1867
+ chunks.push(obj);
1868
+ lastChunk = obj;
1869
+ yield obj;
1870
+ }
1871
+ if (lastChunk?.metadata) {
1872
+ this._events.emit("response", req, {
1873
+ output: chunks.map((c) => c.output || "").join(""),
1874
+ metadata: lastChunk.metadata
1875
+ });
1876
+ }
1877
+ return;
1830
1878
  }
1831
- const body = res2.body;
1832
- if (!body) {
1879
+ const res = await this._withServerRetry(
1880
+ () => this._axiosClient.post(
1881
+ "/v2/cognitive/generate-text-stream",
1882
+ { ...request, stream: true },
1883
+ {
1884
+ responseType: "stream",
1885
+ signal,
1886
+ timeout: options.timeout ?? this._timeout
1887
+ }
1888
+ ),
1889
+ options,
1890
+ req
1891
+ );
1892
+ const nodeStream = res.data;
1893
+ if (!nodeStream) {
1833
1894
  throw new Error("No response body received for streaming request");
1834
1895
  }
1835
- const reader = body.getReader();
1836
- const iterable = (async function* () {
1837
- for (; ; ) {
1838
- const { value, done } = await reader.read();
1839
- if (done) {
1840
- break;
1841
- }
1842
- if (value) {
1843
- yield value;
1844
- }
1845
- }
1846
- })();
1847
- for await (const obj of this._ndjson(iterable)) {
1896
+ for await (const obj of this._ndjson(nodeStream)) {
1897
+ chunks.push(obj);
1898
+ lastChunk = obj;
1848
1899
  yield obj;
1849
1900
  }
1850
- return;
1851
- }
1852
- const res = await this._withServerRetry(
1853
- () => this._axiosClient.post(
1854
- "/v2/cognitive/generate-text-stream",
1855
- { ...request, stream: true },
1856
- {
1857
- responseType: "stream",
1858
- signal,
1859
- timeout: options.timeout ?? this._timeout
1860
- }
1861
- )
1862
- );
1863
- const nodeStream = res.data;
1864
- if (!nodeStream) {
1865
- throw new Error("No response body received for streaming request");
1866
- }
1867
- for await (const obj of this._ndjson(nodeStream)) {
1868
- yield obj;
1901
+ if (lastChunk?.metadata) {
1902
+ this._events.emit("response", req, {
1903
+ output: chunks.map((c) => c.output || "").join(""),
1904
+ metadata: lastChunk.metadata
1905
+ });
1906
+ }
1907
+ } catch (error) {
1908
+ this._events.emit("error", req, error);
1909
+ throw error;
1869
1910
  }
1870
1911
  }
1871
1912
  async *_ndjson(stream) {
@@ -1907,14 +1948,30 @@ var CognitiveBeta = class _CognitiveBeta {
1907
1948
  }
1908
1949
  return false;
1909
1950
  }
1910
- async _withServerRetry(fn) {
1911
- return (0, import_exponential_backoff.backOff)(fn, {
1912
- numOfAttempts: 3,
1913
- startingDelay: 300,
1914
- timeMultiple: 2,
1915
- jitter: "full",
1916
- retry: (e) => this._isRetryableServerError(e)
1917
- });
1951
+ async _withServerRetry(fn, options = {}, req) {
1952
+ let attemptCount = 0;
1953
+ return (0, import_exponential_backoff.backOff)(
1954
+ async () => {
1955
+ try {
1956
+ const result = await fn();
1957
+ attemptCount = 0;
1958
+ return result;
1959
+ } catch (error) {
1960
+ if (attemptCount > 0 && req) {
1961
+ this._events.emit("retry", req, error);
1962
+ }
1963
+ attemptCount++;
1964
+ throw error;
1965
+ }
1966
+ },
1967
+ {
1968
+ numOfAttempts: 3,
1969
+ startingDelay: 300,
1970
+ timeMultiple: 2,
1971
+ jitter: "full",
1972
+ retry: (e) => !options.signal?.aborted && this._isRetryableServerError(e)
1973
+ }
1974
+ );
1918
1975
  }
1919
1976
  };
1920
1977
  var getCognitiveV2Model = (model) => {
@@ -2297,8 +2354,21 @@ var Cognitive = class _Cognitive {
2297
2354
  delete input.systemPrompt;
2298
2355
  }
2299
2356
  const betaClient = new CognitiveBeta(this._client.config);
2300
- const response = await betaClient.generateText(input);
2301
- return {
2357
+ const props = { input };
2358
+ betaClient.on("request", () => {
2359
+ this._events.emit("request", props);
2360
+ });
2361
+ betaClient.on("error", (_req, error) => {
2362
+ this._events.emit("error", props, error);
2363
+ });
2364
+ betaClient.on("retry", (_req, error) => {
2365
+ this._events.emit("retry", props, error);
2366
+ });
2367
+ const response = await betaClient.generateText(input, {
2368
+ signal: input.signal,
2369
+ timeout: this._timeoutMs
2370
+ });
2371
+ const result = {
2302
2372
  output: {
2303
2373
  id: "beta-output",
2304
2374
  provider: response.metadata.provider,
@@ -2336,6 +2406,8 @@ var Cognitive = class _Cognitive {
2336
2406
  }
2337
2407
  }
2338
2408
  };
2409
+ this._events.emit("response", props, result);
2410
+ return result;
2339
2411
  }
2340
2412
  async _generateContent(input) {
2341
2413
  const start = Date.now();