@blocklet/aigne-hub 0.2.18 → 0.2.20
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/lib/cjs/api/ai-kit.js +5 -1
- package/lib/cjs/api/call/v2.js +3 -1
- package/lib/cjs/api/error.js +32 -6
- package/lib/esm/api/ai-kit.js +5 -1
- package/lib/esm/api/call/v2.js +3 -1
- package/lib/esm/api/error.js +30 -5
- package/lib/types/api/error.d.ts +15 -4
- package/package.json +1 -1
package/lib/cjs/api/ai-kit.js
CHANGED
|
@@ -28,7 +28,11 @@ const createTextCompletionApi = ({ fetch, path, timeout, }) => async (options) =
|
|
|
28
28
|
}
|
|
29
29
|
return res.body;
|
|
30
30
|
})
|
|
31
|
-
: fetch(path,
|
|
31
|
+
: fetch(path, {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: { 'Content-Type': 'application/json' },
|
|
34
|
+
body: JSON.stringify(options),
|
|
35
|
+
})
|
|
32
36
|
.then((res) => res.json())
|
|
33
37
|
.then((data) => { var _a, _b, _c, _d, _e, _f, _g; return ({ text: (_d = (_a = data.text) !== null && _a !== void 0 ? _a : (_c = (_b = data.choices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text) !== null && _d !== void 0 ? _d : (_g = (_f = (_e = data.choices) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.message) === null || _g === void 0 ? void 0 : _g.content }); })
|
|
34
38
|
.catch(processResponseError);
|
package/lib/cjs/api/call/v2.js
CHANGED
|
@@ -11,6 +11,7 @@ exports.getUserCreditInfo = getUserCreditInfo;
|
|
|
11
11
|
const web_1 = require("stream/web");
|
|
12
12
|
const axios_1 = __importDefault(require("axios"));
|
|
13
13
|
const ufo_1 = require("ufo");
|
|
14
|
+
const error_1 = require("../error");
|
|
14
15
|
const types_1 = require("../types");
|
|
15
16
|
const event_stream_1 = require("../utils/event-stream");
|
|
16
17
|
const util_1 = require("../utils/util");
|
|
@@ -23,11 +24,12 @@ function isCacheExpired(cacheItem) {
|
|
|
23
24
|
return Date.now() - cacheItem.timestamp > CACHE_TTL;
|
|
24
25
|
}
|
|
25
26
|
function getConfig() {
|
|
27
|
+
var _a;
|
|
26
28
|
const baseUrl = process.env.BLOCKLET_AIGNE_API_URL;
|
|
27
29
|
const credentials = JSON.parse(process.env.BLOCKLET_AIGNE_API_CREDENTIAL || '{}');
|
|
28
30
|
const accessKey = credentials === null || credentials === void 0 ? void 0 : credentials.apiKey;
|
|
29
31
|
if (!baseUrl || !accessKey) {
|
|
30
|
-
throw new
|
|
32
|
+
throw new error_1.ConfigError(error_1.ConfigErrorType.MISSING_DASHBOARD_CONFIG, (0, ufo_1.joinURL)(new URL(((_a = process.env) === null || _a === void 0 ? void 0 : _a.BLOCKLET_APP_URL) || '').origin, '.well-known/service/admin/aigne'));
|
|
31
33
|
}
|
|
32
34
|
return { baseUrl, accessKey };
|
|
33
35
|
}
|
package/lib/cjs/api/error.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StatusCodeError = exports.CreditError = exports.
|
|
3
|
+
exports.StatusCodeError = exports.CreditError = exports.CreditErrorType = exports.ConfigError = exports.ConfigErrorType = exports.SubscriptionError = exports.SubscriptionErrorType = void 0;
|
|
4
|
+
// Subscription 为v1版本订阅错误, 新版本将废弃,保留是为了兼容
|
|
4
5
|
var SubscriptionErrorType;
|
|
5
6
|
(function (SubscriptionErrorType) {
|
|
6
7
|
SubscriptionErrorType["UNSUBSCRIBED"] = "UNSUBSCRIBED";
|
|
7
8
|
SubscriptionErrorType["UNKNOWN"] = "UNKNOWN";
|
|
8
9
|
})(SubscriptionErrorType || (exports.SubscriptionErrorType = SubscriptionErrorType = {}));
|
|
9
|
-
var CreditErrorType;
|
|
10
|
-
(function (CreditErrorType) {
|
|
11
|
-
CreditErrorType["NOT_ENOUGH"] = "NOT_ENOUGH";
|
|
12
|
-
CreditErrorType["UNKNOWN"] = "UNKNOWN";
|
|
13
|
-
})(CreditErrorType || (exports.CreditErrorType = CreditErrorType = {}));
|
|
14
10
|
const SubscriptionErrors = {
|
|
15
11
|
[SubscriptionErrorType.UNSUBSCRIBED]: 'Hello, in order to continue chatting, please first subscribe to AI-KIT service',
|
|
16
12
|
[SubscriptionErrorType.UNKNOWN]: 'An unknown error occurred',
|
|
@@ -24,6 +20,36 @@ class SubscriptionError extends Error {
|
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
22
|
exports.SubscriptionError = SubscriptionError;
|
|
23
|
+
// ConfigError 为v2版本配置错误, 用于v1版本订阅错误
|
|
24
|
+
var ConfigErrorType;
|
|
25
|
+
(function (ConfigErrorType) {
|
|
26
|
+
ConfigErrorType["UNKNOWN"] = "UNKNOWN";
|
|
27
|
+
ConfigErrorType["MISSING_API_KEY"] = "MISSING_API_KEY";
|
|
28
|
+
ConfigErrorType["MISSING_DASHBOARD_CONFIG"] = "MISSING_DASHBOARD_CONFIG";
|
|
29
|
+
})(ConfigErrorType || (exports.ConfigErrorType = ConfigErrorType = {}));
|
|
30
|
+
const ConfigErrors = {
|
|
31
|
+
[ConfigErrorType.UNKNOWN]: 'An unknown error occurred',
|
|
32
|
+
[ConfigErrorType.MISSING_API_KEY]: 'Hello, in order to continue chatting, please first configure the API key in the dashboard.',
|
|
33
|
+
[ConfigErrorType.MISSING_DASHBOARD_CONFIG]: 'Unable to connect to AIGNE Hub: missing baseUrl or accessKey.\n If you are an administrator, please configure them in the dashboard.\n If you are not an administrator, please contact your system admin for assistance.',
|
|
34
|
+
};
|
|
35
|
+
class ConfigError extends Error {
|
|
36
|
+
constructor(type, link) {
|
|
37
|
+
let message = ConfigErrors[type] || ConfigErrors[ConfigErrorType.UNKNOWN];
|
|
38
|
+
if (link) {
|
|
39
|
+
message += `\n${link}`;
|
|
40
|
+
}
|
|
41
|
+
super(message);
|
|
42
|
+
this.timestamp = new Date().toISOString();
|
|
43
|
+
this.type = type;
|
|
44
|
+
this.link = link;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ConfigError = ConfigError;
|
|
48
|
+
var CreditErrorType;
|
|
49
|
+
(function (CreditErrorType) {
|
|
50
|
+
CreditErrorType["NOT_ENOUGH"] = "NOT_ENOUGH";
|
|
51
|
+
CreditErrorType["UNKNOWN"] = "UNKNOWN";
|
|
52
|
+
})(CreditErrorType || (exports.CreditErrorType = CreditErrorType = {}));
|
|
27
53
|
const CreditErrors = {
|
|
28
54
|
[CreditErrorType.NOT_ENOUGH]: 'Hello, in order to continue chatting, please first buy some credits in the link below.',
|
|
29
55
|
[CreditErrorType.UNKNOWN]: 'An unknown error occurred',
|
package/lib/esm/api/ai-kit.js
CHANGED
|
@@ -24,7 +24,11 @@ export const createTextCompletionApi = ({ fetch, path, timeout, }) => async (opt
|
|
|
24
24
|
}
|
|
25
25
|
return res.body;
|
|
26
26
|
})
|
|
27
|
-
: fetch(path,
|
|
27
|
+
: fetch(path, {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
headers: { 'Content-Type': 'application/json' },
|
|
30
|
+
body: JSON.stringify(options),
|
|
31
|
+
})
|
|
28
32
|
.then((res) => res.json())
|
|
29
33
|
.then((data) => { var _a, _b, _c, _d, _e, _f, _g; return ({ text: (_d = (_a = data.text) !== null && _a !== void 0 ? _a : (_c = (_b = data.choices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.text) !== null && _d !== void 0 ? _d : (_g = (_f = (_e = data.choices) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.message) === null || _g === void 0 ? void 0 : _g.content }); })
|
|
30
34
|
.catch(processResponseError);
|
package/lib/esm/api/call/v2.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ReadableStream, TextDecoderStream } from 'stream/web';
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { joinURL } from 'ufo';
|
|
4
|
+
import { ConfigError, ConfigErrorType } from '../error';
|
|
4
5
|
import { isChatCompletionError, } from '../types';
|
|
5
6
|
import { EventSourceParserStream, readableToWeb } from '../utils/event-stream';
|
|
6
7
|
import { getRemoteBaseUrl } from '../utils/util';
|
|
@@ -13,11 +14,12 @@ function isCacheExpired(cacheItem) {
|
|
|
13
14
|
return Date.now() - cacheItem.timestamp > CACHE_TTL;
|
|
14
15
|
}
|
|
15
16
|
function getConfig() {
|
|
17
|
+
var _a;
|
|
16
18
|
const baseUrl = process.env.BLOCKLET_AIGNE_API_URL;
|
|
17
19
|
const credentials = JSON.parse(process.env.BLOCKLET_AIGNE_API_CREDENTIAL || '{}');
|
|
18
20
|
const accessKey = credentials === null || credentials === void 0 ? void 0 : credentials.apiKey;
|
|
19
21
|
if (!baseUrl || !accessKey) {
|
|
20
|
-
throw new
|
|
22
|
+
throw new ConfigError(ConfigErrorType.MISSING_DASHBOARD_CONFIG, joinURL(new URL(((_a = process.env) === null || _a === void 0 ? void 0 : _a.BLOCKLET_APP_URL) || '').origin, '.well-known/service/admin/aigne'));
|
|
21
23
|
}
|
|
22
24
|
return { baseUrl, accessKey };
|
|
23
25
|
}
|
package/lib/esm/api/error.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
+
// Subscription 为v1版本订阅错误, 新版本将废弃,保留是为了兼容
|
|
1
2
|
export var SubscriptionErrorType;
|
|
2
3
|
(function (SubscriptionErrorType) {
|
|
3
4
|
SubscriptionErrorType["UNSUBSCRIBED"] = "UNSUBSCRIBED";
|
|
4
5
|
SubscriptionErrorType["UNKNOWN"] = "UNKNOWN";
|
|
5
6
|
})(SubscriptionErrorType || (SubscriptionErrorType = {}));
|
|
6
|
-
export var CreditErrorType;
|
|
7
|
-
(function (CreditErrorType) {
|
|
8
|
-
CreditErrorType["NOT_ENOUGH"] = "NOT_ENOUGH";
|
|
9
|
-
CreditErrorType["UNKNOWN"] = "UNKNOWN";
|
|
10
|
-
})(CreditErrorType || (CreditErrorType = {}));
|
|
11
7
|
const SubscriptionErrors = {
|
|
12
8
|
[SubscriptionErrorType.UNSUBSCRIBED]: 'Hello, in order to continue chatting, please first subscribe to AI-KIT service',
|
|
13
9
|
[SubscriptionErrorType.UNKNOWN]: 'An unknown error occurred',
|
|
@@ -20,6 +16,35 @@ export class SubscriptionError extends Error {
|
|
|
20
16
|
this.type = type;
|
|
21
17
|
}
|
|
22
18
|
}
|
|
19
|
+
// ConfigError 为v2版本配置错误, 用于v1版本订阅错误
|
|
20
|
+
export var ConfigErrorType;
|
|
21
|
+
(function (ConfigErrorType) {
|
|
22
|
+
ConfigErrorType["UNKNOWN"] = "UNKNOWN";
|
|
23
|
+
ConfigErrorType["MISSING_API_KEY"] = "MISSING_API_KEY";
|
|
24
|
+
ConfigErrorType["MISSING_DASHBOARD_CONFIG"] = "MISSING_DASHBOARD_CONFIG";
|
|
25
|
+
})(ConfigErrorType || (ConfigErrorType = {}));
|
|
26
|
+
const ConfigErrors = {
|
|
27
|
+
[ConfigErrorType.UNKNOWN]: 'An unknown error occurred',
|
|
28
|
+
[ConfigErrorType.MISSING_API_KEY]: 'Hello, in order to continue chatting, please first configure the API key in the dashboard.',
|
|
29
|
+
[ConfigErrorType.MISSING_DASHBOARD_CONFIG]: 'Unable to connect to AIGNE Hub: missing baseUrl or accessKey.\n If you are an administrator, please configure them in the dashboard.\n If you are not an administrator, please contact your system admin for assistance.',
|
|
30
|
+
};
|
|
31
|
+
export class ConfigError extends Error {
|
|
32
|
+
constructor(type, link) {
|
|
33
|
+
let message = ConfigErrors[type] || ConfigErrors[ConfigErrorType.UNKNOWN];
|
|
34
|
+
if (link) {
|
|
35
|
+
message += `\n${link}`;
|
|
36
|
+
}
|
|
37
|
+
super(message);
|
|
38
|
+
this.timestamp = new Date().toISOString();
|
|
39
|
+
this.type = type;
|
|
40
|
+
this.link = link;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export var CreditErrorType;
|
|
44
|
+
(function (CreditErrorType) {
|
|
45
|
+
CreditErrorType["NOT_ENOUGH"] = "NOT_ENOUGH";
|
|
46
|
+
CreditErrorType["UNKNOWN"] = "UNKNOWN";
|
|
47
|
+
})(CreditErrorType || (CreditErrorType = {}));
|
|
23
48
|
const CreditErrors = {
|
|
24
49
|
[CreditErrorType.NOT_ENOUGH]: 'Hello, in order to continue chatting, please first buy some credits in the link below.',
|
|
25
50
|
[CreditErrorType.UNKNOWN]: 'An unknown error occurred',
|
package/lib/types/api/error.d.ts
CHANGED
|
@@ -2,15 +2,26 @@ export declare enum SubscriptionErrorType {
|
|
|
2
2
|
UNSUBSCRIBED = "UNSUBSCRIBED",
|
|
3
3
|
UNKNOWN = "UNKNOWN"
|
|
4
4
|
}
|
|
5
|
-
export declare enum CreditErrorType {
|
|
6
|
-
NOT_ENOUGH = "NOT_ENOUGH",
|
|
7
|
-
UNKNOWN = "UNKNOWN"
|
|
8
|
-
}
|
|
9
5
|
export declare class SubscriptionError extends Error {
|
|
10
6
|
timestamp: string;
|
|
11
7
|
type: SubscriptionErrorType;
|
|
12
8
|
constructor(type: SubscriptionErrorType);
|
|
13
9
|
}
|
|
10
|
+
export declare enum ConfigErrorType {
|
|
11
|
+
UNKNOWN = "UNKNOWN",
|
|
12
|
+
MISSING_API_KEY = "MISSING_API_KEY",
|
|
13
|
+
MISSING_DASHBOARD_CONFIG = "MISSING_DASHBOARD_CONFIG"
|
|
14
|
+
}
|
|
15
|
+
export declare class ConfigError extends Error {
|
|
16
|
+
timestamp: string;
|
|
17
|
+
type: ConfigErrorType;
|
|
18
|
+
link?: string;
|
|
19
|
+
constructor(type: ConfigErrorType, link?: string);
|
|
20
|
+
}
|
|
21
|
+
export declare enum CreditErrorType {
|
|
22
|
+
NOT_ENOUGH = "NOT_ENOUGH",
|
|
23
|
+
UNKNOWN = "UNKNOWN"
|
|
24
|
+
}
|
|
14
25
|
export declare class CreditError extends Error {
|
|
15
26
|
timestamp: string;
|
|
16
27
|
type: CreditErrorType;
|