@blocklet/aigne-hub 0.2.17 → 0.2.19
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/call/v2.js +11 -14
- package/lib/cjs/api/error.js +32 -6
- package/lib/esm/api/call/v2.js +11 -14
- 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/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
|
}
|
|
@@ -62,6 +64,7 @@ async function callRemoteApi(input, config, options = {}) {
|
|
|
62
64
|
url: (0, ufo_1.joinURL)(url, config.endpoint),
|
|
63
65
|
headers,
|
|
64
66
|
timeout: options === null || options === void 0 ? void 0 : options.timeout,
|
|
67
|
+
responseType: options === null || options === void 0 ? void 0 : options.responseType,
|
|
65
68
|
};
|
|
66
69
|
if (method === 'GET') {
|
|
67
70
|
requestConfig.params = input;
|
|
@@ -72,16 +75,17 @@ async function callRemoteApi(input, config, options = {}) {
|
|
|
72
75
|
return (0, api_1.catchAndRethrowUpstreamError)(axios_1.default.request(requestConfig));
|
|
73
76
|
}
|
|
74
77
|
async function chatCompletionsV2(input, options = {}) {
|
|
75
|
-
const
|
|
78
|
+
const params = {
|
|
76
79
|
endpoint: 'api/v2/chat/completions',
|
|
77
80
|
isStreamEndpoint: true,
|
|
78
|
-
}
|
|
81
|
+
};
|
|
82
|
+
const response = await callRemoteApi(input, params, { ...options, responseType: 'stream' });
|
|
79
83
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream')
|
|
80
84
|
return response;
|
|
81
85
|
return new web_1.ReadableStream({
|
|
82
86
|
async start(controller) {
|
|
83
87
|
try {
|
|
84
|
-
const stream = (0, event_stream_1.readableToWeb)(response.data)
|
|
88
|
+
const stream = (0, event_stream_1.readableToWeb)((await response).data)
|
|
85
89
|
.pipeThrough(new web_1.TextDecoderStream())
|
|
86
90
|
.pipeThrough(new event_stream_1.EventSourceParserStream());
|
|
87
91
|
for await (const chunk of stream) {
|
|
@@ -110,26 +114,19 @@ async function chatCompletionsV2(input, options = {}) {
|
|
|
110
114
|
});
|
|
111
115
|
}
|
|
112
116
|
async function imageGenerationsV2(input, options = {}) {
|
|
113
|
-
const response = await callRemoteApi(input, {
|
|
114
|
-
endpoint: 'api/v2/image/generations',
|
|
115
|
-
}, options);
|
|
117
|
+
const response = await callRemoteApi(input, { endpoint: 'api/v2/image/generations' }, options);
|
|
116
118
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream')
|
|
117
119
|
return response;
|
|
118
120
|
return response.data;
|
|
119
121
|
}
|
|
120
122
|
async function embeddingsV2(input, options = {}) {
|
|
121
|
-
const response = await callRemoteApi(input, {
|
|
122
|
-
endpoint: 'api/v2/embeddings',
|
|
123
|
-
}, options);
|
|
123
|
+
const response = await callRemoteApi(input, { endpoint: 'api/v2/embeddings' }, options);
|
|
124
124
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream') {
|
|
125
125
|
return response;
|
|
126
126
|
}
|
|
127
127
|
return response.data;
|
|
128
128
|
}
|
|
129
129
|
async function getUserCreditInfo() {
|
|
130
|
-
const response = await callRemoteApi({}, {
|
|
131
|
-
endpoint: 'api/user/info',
|
|
132
|
-
method: 'GET',
|
|
133
|
-
});
|
|
130
|
+
const response = await callRemoteApi({}, { endpoint: 'api/user/info', method: 'GET' });
|
|
134
131
|
return response.data;
|
|
135
132
|
}
|
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/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
|
}
|
|
@@ -52,6 +54,7 @@ export async function callRemoteApi(input, config, options = {}) {
|
|
|
52
54
|
url: joinURL(url, config.endpoint),
|
|
53
55
|
headers,
|
|
54
56
|
timeout: options === null || options === void 0 ? void 0 : options.timeout,
|
|
57
|
+
responseType: options === null || options === void 0 ? void 0 : options.responseType,
|
|
55
58
|
};
|
|
56
59
|
if (method === 'GET') {
|
|
57
60
|
requestConfig.params = input;
|
|
@@ -62,16 +65,17 @@ export async function callRemoteApi(input, config, options = {}) {
|
|
|
62
65
|
return catchAndRethrowUpstreamError(axios.request(requestConfig));
|
|
63
66
|
}
|
|
64
67
|
export async function chatCompletionsV2(input, options = {}) {
|
|
65
|
-
const
|
|
68
|
+
const params = {
|
|
66
69
|
endpoint: 'api/v2/chat/completions',
|
|
67
70
|
isStreamEndpoint: true,
|
|
68
|
-
}
|
|
71
|
+
};
|
|
72
|
+
const response = await callRemoteApi(input, params, { ...options, responseType: 'stream' });
|
|
69
73
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream')
|
|
70
74
|
return response;
|
|
71
75
|
return new ReadableStream({
|
|
72
76
|
async start(controller) {
|
|
73
77
|
try {
|
|
74
|
-
const stream = readableToWeb(response.data)
|
|
78
|
+
const stream = readableToWeb((await response).data)
|
|
75
79
|
.pipeThrough(new TextDecoderStream())
|
|
76
80
|
.pipeThrough(new EventSourceParserStream());
|
|
77
81
|
for await (const chunk of stream) {
|
|
@@ -100,26 +104,19 @@ export async function chatCompletionsV2(input, options = {}) {
|
|
|
100
104
|
});
|
|
101
105
|
}
|
|
102
106
|
export async function imageGenerationsV2(input, options = {}) {
|
|
103
|
-
const response = await callRemoteApi(input, {
|
|
104
|
-
endpoint: 'api/v2/image/generations',
|
|
105
|
-
}, options);
|
|
107
|
+
const response = await callRemoteApi(input, { endpoint: 'api/v2/image/generations' }, options);
|
|
106
108
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream')
|
|
107
109
|
return response;
|
|
108
110
|
return response.data;
|
|
109
111
|
}
|
|
110
112
|
export async function embeddingsV2(input, options = {}) {
|
|
111
|
-
const response = await callRemoteApi(input, {
|
|
112
|
-
endpoint: 'api/v2/embeddings',
|
|
113
|
-
}, options);
|
|
113
|
+
const response = await callRemoteApi(input, { endpoint: 'api/v2/embeddings' }, options);
|
|
114
114
|
if ((options === null || options === void 0 ? void 0 : options.responseType) === 'stream') {
|
|
115
115
|
return response;
|
|
116
116
|
}
|
|
117
117
|
return response.data;
|
|
118
118
|
}
|
|
119
119
|
export async function getUserCreditInfo() {
|
|
120
|
-
const response = await callRemoteApi({}, {
|
|
121
|
-
endpoint: 'api/user/info',
|
|
122
|
-
method: 'GET',
|
|
123
|
-
});
|
|
120
|
+
const response = await callRemoteApi({}, { endpoint: 'api/user/info', method: 'GET' });
|
|
124
121
|
return response.data;
|
|
125
122
|
}
|
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;
|