@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.
- package/.turbo/turbo-build.log +7 -7
- package/dist/index.cjs +139 -67
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +41 -5
- package/dist/index.mjs +139 -67
- package/dist/index.mjs.map +2 -2
- package/package.json +1 -1
- package/refresh-models.ts +0 -6
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
|
|
2
|
-
> @botpress/cognitive@0.1
|
|
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
|
|
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
|
|
14
|
-
DTS dist/index.d.ts
|
|
13
|
+
DTS ⚡️ Build success in 4315ms
|
|
14
|
+
DTS dist/index.d.ts 632.56 KB
|
|
15
15
|
|
|
16
|
-
> @botpress/cognitive@0.1
|
|
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
|
[1mdist/index.cjs[22m
|
|
22
22
|
Size limit: [32m[1m50 kB[22m[39m
|
|
23
|
-
Size: [32m[
|
|
23
|
+
Size: [32m[1m15.02 kB[22m[39m [90mbrotlied[39m
|
|
24
24
|
|
|
25
25
|
[1mdist/index.mjs[22m
|
|
26
26
|
Size limit: [32m[1m50 kB[22m[39m
|
|
27
|
-
Size: [32m[1m14.
|
|
27
|
+
Size: [32m[1m14.88 kB[22m[39m [90mbrotlied[39m
|
|
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 {
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
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
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
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
|
|
1832
|
-
|
|
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
|
|
1836
|
-
|
|
1837
|
-
|
|
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
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
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
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
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
|
|
2301
|
-
|
|
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();
|