@volcengine/ark-runtime 1.0.0
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/LICENSE.txt +202 -0
- package/README.md +104 -0
- package/dist/cjs/index.js +1717 -0
- package/dist/esm/client.d.ts +97 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/config.d.ts +46 -0
- package/dist/esm/config.d.ts.map +1 -0
- package/dist/esm/encryption/encrypt-chat.d.ts +24 -0
- package/dist/esm/encryption/encrypt-chat.d.ts.map +1 -0
- package/dist/esm/encryption/index.d.ts +4 -0
- package/dist/esm/encryption/index.d.ts.map +1 -0
- package/dist/esm/encryption/key-agreement.d.ts +73 -0
- package/dist/esm/encryption/key-agreement.d.ts.map +1 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.mjs +1476 -0
- package/dist/esm/rslib-runtime.mjs +37 -0
- package/dist/esm/types/bot.d.ts +109 -0
- package/dist/esm/types/bot.d.ts.map +1 -0
- package/dist/esm/types/chat-completion.d.ts +167 -0
- package/dist/esm/types/chat-completion.d.ts.map +1 -0
- package/dist/esm/types/common.d.ts +29 -0
- package/dist/esm/types/common.d.ts.map +1 -0
- package/dist/esm/types/content-generation.d.ts +118 -0
- package/dist/esm/types/content-generation.d.ts.map +1 -0
- package/dist/esm/types/context.d.ts +49 -0
- package/dist/esm/types/context.d.ts.map +1 -0
- package/dist/esm/types/embeddings.d.ts +44 -0
- package/dist/esm/types/embeddings.d.ts.map +1 -0
- package/dist/esm/types/error.d.ts +45 -0
- package/dist/esm/types/error.d.ts.map +1 -0
- package/dist/esm/types/file.d.ts +66 -0
- package/dist/esm/types/file.d.ts.map +1 -0
- package/dist/esm/types/http-request-error.d.ts +13 -0
- package/dist/esm/types/http-request-error.d.ts.map +1 -0
- package/dist/esm/types/images.d.ts +78 -0
- package/dist/esm/types/images.d.ts.map +1 -0
- package/dist/esm/types/index.d.ts +13 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/dist/esm/types/multimodal-embedding.d.ts +56 -0
- package/dist/esm/types/multimodal-embedding.d.ts.map +1 -0
- package/dist/esm/types/responses/enums.d.ts +38 -0
- package/dist/esm/types/responses/enums.d.ts.map +1 -0
- package/dist/esm/types/responses/helpers.d.ts +22 -0
- package/dist/esm/types/responses/helpers.d.ts.map +1 -0
- package/dist/esm/types/responses/index.d.ts +4 -0
- package/dist/esm/types/responses/index.d.ts.map +1 -0
- package/dist/esm/types/responses/types.d.ts +906 -0
- package/dist/esm/types/responses/types.d.ts.map +1 -0
- package/dist/esm/types/tokenization.d.ts +22 -0
- package/dist/esm/types/tokenization.d.ts.map +1 -0
- package/dist/esm/utils/breaker-provider.d.ts +9 -0
- package/dist/esm/utils/breaker-provider.d.ts.map +1 -0
- package/dist/esm/utils/breaker.d.ts +28 -0
- package/dist/esm/utils/breaker.d.ts.map +1 -0
- package/dist/esm/utils/normalize.d.ts +51 -0
- package/dist/esm/utils/normalize.d.ts.map +1 -0
- package/dist/esm/utils/request-builder.d.ts +15 -0
- package/dist/esm/utils/request-builder.d.ts.map +1 -0
- package/dist/esm/utils/request-id.d.ts +5 -0
- package/dist/esm/utils/request-id.d.ts.map +1 -0
- package/dist/esm/utils/retry.d.ts +11 -0
- package/dist/esm/utils/retry.d.ts.map +1 -0
- package/dist/esm/utils/sse-decoder.d.ts +23 -0
- package/dist/esm/utils/sse-decoder.d.ts.map +1 -0
- package/dist/esm/utils/stream-reader.d.ts +67 -0
- package/dist/esm/utils/stream-reader.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/example/README.md +118 -0
- package/example/batch-chat.ts +64 -0
- package/example/bot-chat.ts +66 -0
- package/example/chat-completion-function-call.ts +141 -0
- package/example/chat-completion-reasoning.ts +64 -0
- package/example/chat-completion-vision.ts +70 -0
- package/example/chat-completion.ts +62 -0
- package/example/content-generation.ts +70 -0
- package/example/context.ts +69 -0
- package/example/embeddings.ts +31 -0
- package/example/file-upload.ts +53 -0
- package/example/images.ts +74 -0
- package/example/list-input-items.ts +34 -0
- package/example/multimodal-embeddings.ts +36 -0
- package/example/responses/basic.ts +75 -0
- package/example/responses/doubao-app.ts +53 -0
- package/example/responses/mcp.ts +66 -0
- package/example/responses/streaming.ts +45 -0
- package/example/responses/video.ts +74 -0
- package/example/responses/web-search.ts +52 -0
- package/example/structured-outputs.ts +71 -0
- package/example/tokenization.ts +30 -0
- package/package.json +47 -0
- package/src/client.ts +1199 -0
- package/src/config.ts +68 -0
- package/src/encryption/encrypt-chat.ts +146 -0
- package/src/encryption/index.ts +21 -0
- package/src/encryption/key-agreement.ts +270 -0
- package/src/index.ts +10 -0
- package/src/types/ark.d.ts +9 -0
- package/src/types/bot.ts +127 -0
- package/src/types/chat-completion.ts +228 -0
- package/src/types/common.ts +37 -0
- package/src/types/content-generation.ts +135 -0
- package/src/types/context.ts +59 -0
- package/src/types/embeddings.ts +74 -0
- package/src/types/error.ts +93 -0
- package/src/types/file.ts +76 -0
- package/src/types/http-request-error.ts +34 -0
- package/src/types/images.ts +102 -0
- package/src/types/index.ts +12 -0
- package/src/types/multimodal-embedding.ts +67 -0
- package/src/types/responses/enums.ts +163 -0
- package/src/types/responses/helpers.ts +67 -0
- package/src/types/responses/index.ts +3 -0
- package/src/types/responses/types.ts +1335 -0
- package/src/types/tokenization.ts +24 -0
- package/src/utils/breaker-provider.ts +17 -0
- package/src/utils/breaker.ts +56 -0
- package/src/utils/normalize.ts +154 -0
- package/src/utils/request-builder.ts +51 -0
- package/src/utils/request-id.ts +17 -0
- package/src/utils/retry.ts +76 -0
- package/src/utils/sse-decoder.ts +140 -0
- package/src/utils/stream-reader.ts +270 -0
|
@@ -0,0 +1,1717 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
14
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: definition[key]
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
})();
|
|
20
|
+
(()=>{
|
|
21
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
22
|
+
})();
|
|
23
|
+
(()=>{
|
|
24
|
+
__webpack_require__.r = (exports1)=>{
|
|
25
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
26
|
+
value: 'Module'
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
})();
|
|
33
|
+
var __webpack_exports__ = {};
|
|
34
|
+
__webpack_require__.r(__webpack_exports__);
|
|
35
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
+
E2eeClient: ()=>E2eeClient,
|
|
37
|
+
EventStreamDecoder: ()=>EventStreamDecoder,
|
|
38
|
+
ErrBodyWithoutProjectName: ()=>ErrBodyWithoutProjectName,
|
|
39
|
+
Breaker: ()=>Breaker,
|
|
40
|
+
ErrBodyWithoutEndpoint: ()=>ErrBodyWithoutEndpoint,
|
|
41
|
+
ChatMessageRoleTool: ()=>"tool",
|
|
42
|
+
ErrorRetryMaxDelayMs: ()=>8000,
|
|
43
|
+
ObjectTypeFile: ()=>"file",
|
|
44
|
+
StatusFailed: ()=>"failed",
|
|
45
|
+
genRequestId: ()=>genRequestId,
|
|
46
|
+
ErrAKSKNotSupported: ()=>ErrAKSKNotSupported,
|
|
47
|
+
getCertInfo: ()=>getCertInfo,
|
|
48
|
+
ErrBodyWithoutBot: ()=>ErrBodyWithoutBot,
|
|
49
|
+
ErrorRetryBaseDelayMs: ()=>500,
|
|
50
|
+
ImageGenerationStreamEventPartialSucceeded: ()=>ImageGenerationStreamEventPartialSucceeded,
|
|
51
|
+
deepCopyRequest: ()=>deepCopyRequest,
|
|
52
|
+
encryptChatRequest: ()=>encryptChatRequest,
|
|
53
|
+
aesGcmEncrypt: ()=>aesGcmEncrypt,
|
|
54
|
+
ErrTooManyEmptyStreamMessages: ()=>ErrTooManyEmptyStreamMessages,
|
|
55
|
+
checkIsModeAICC: ()=>checkIsModeAICC,
|
|
56
|
+
retry: ()=>retry,
|
|
57
|
+
ArkRequestError: ()=>ArkRequestError,
|
|
58
|
+
ArkRuntimeClient: ()=>ArkRuntimeClient,
|
|
59
|
+
DefaultStsTimeout: ()=>604800,
|
|
60
|
+
FileStatusActive: ()=>"active",
|
|
61
|
+
RetryAfterHeader: ()=>RetryAfterHeader,
|
|
62
|
+
decryptChatResponse: ()=>decryptChatResponse,
|
|
63
|
+
ToolChoiceAuto: ()=>"auto",
|
|
64
|
+
ModelBreakerProvider: ()=>ModelBreakerProvider,
|
|
65
|
+
ErrChatCompletionStreamNotSupported: ()=>ErrChatCompletionStreamNotSupported,
|
|
66
|
+
FileStatusFailed: ()=>"failed",
|
|
67
|
+
ArkAPIError: ()=>ArkAPIError,
|
|
68
|
+
StatusQueued: ()=>"queued",
|
|
69
|
+
base64ToEmbeddingResponse: ()=>base64ToEmbeddingResponse,
|
|
70
|
+
responses: ()=>responses_namespaceObject,
|
|
71
|
+
DefaultMandatoryRefreshTimeout: ()=>600,
|
|
72
|
+
ChatMessageRoleAssistant: ()=>ChatMessageRoleAssistant,
|
|
73
|
+
GenerateImagesResponseFormatBase64: ()=>GenerateImagesResponseFormatBase64,
|
|
74
|
+
ChatCompletionStreamReader: ()=>ChatCompletionStreamReader,
|
|
75
|
+
aesGcmEncryptBase64: ()=>aesGcmEncryptBase64,
|
|
76
|
+
ClientSessionTokenHeader: ()=>ClientSessionTokenHeader,
|
|
77
|
+
BotChatCompletionStreamReader: ()=>BotChatCompletionStreamReader,
|
|
78
|
+
ObjectTypeList: ()=>"list",
|
|
79
|
+
FileStatusProcessing: ()=>FileStatusProcessing,
|
|
80
|
+
ImageGenerationStreamEventCompleted: ()=>ImageGenerationStreamEventCompleted,
|
|
81
|
+
OrderDesc: ()=>"desc",
|
|
82
|
+
ToolChoiceRequired: ()=>ToolChoiceRequired,
|
|
83
|
+
ImageGenerationStreamReader: ()=>ImageGenerationStreamReader,
|
|
84
|
+
ChatMessageRoleSystem: ()=>"system",
|
|
85
|
+
ClientRequestHeader: ()=>ClientRequestHeader,
|
|
86
|
+
KeyAgreementClient: ()=>KeyAgreementClient,
|
|
87
|
+
GenerateImagesSizeAdaptive: ()=>GenerateImagesSizeAdaptive,
|
|
88
|
+
ImageGenerationStreamEventPartialFailed: ()=>ImageGenerationStreamEventPartialFailed,
|
|
89
|
+
PurposeUserData: ()=>PurposeUserData,
|
|
90
|
+
aesGcmDecrypt: ()=>aesGcmDecrypt,
|
|
91
|
+
decodeBase64Embedding: ()=>decodeBase64Embedding,
|
|
92
|
+
saveToLocalCertificate: ()=>saveToLocalCertificate,
|
|
93
|
+
DefaultAdvisoryRefreshTimeout: ()=>1800,
|
|
94
|
+
ClientEncryptInfoHeader: ()=>ClientEncryptInfoHeader,
|
|
95
|
+
StatusRunning: ()=>StatusRunning,
|
|
96
|
+
decryptChatStreamResponse: ()=>decryptChatStreamResponse,
|
|
97
|
+
ToolChoiceNone: ()=>"none",
|
|
98
|
+
ErrContentFieldsMisused: ()=>ErrContentFieldsMisused,
|
|
99
|
+
ClientIsEncryptedHeader: ()=>ClientIsEncryptedHeader,
|
|
100
|
+
loadLocalCertificate: ()=>loadLocalCertificate,
|
|
101
|
+
OrderAsc: ()=>"asc",
|
|
102
|
+
GenerateImagesResponseFormatURL: ()=>"url",
|
|
103
|
+
ChatMessageRoleUser: ()=>"user",
|
|
104
|
+
ResponsesStreamReader: ()=>ResponsesStreamReader,
|
|
105
|
+
StatusSucceeded: ()=>StatusSucceeded,
|
|
106
|
+
aesGcmDecryptBase64: ()=>aesGcmDecryptBase64,
|
|
107
|
+
MaxRetryDelay: ()=>8,
|
|
108
|
+
StatusCancelled: ()=>StatusCancelled,
|
|
109
|
+
InitialRetryDelay: ()=>0.5
|
|
110
|
+
});
|
|
111
|
+
var responses_namespaceObject = {};
|
|
112
|
+
__webpack_require__.r(responses_namespaceObject);
|
|
113
|
+
__webpack_require__.d(responses_namespaceObject, {
|
|
114
|
+
ContentItemType_input_audio: ()=>ContentItemType_input_audio,
|
|
115
|
+
ContentItemType_input_file: ()=>ContentItemType_input_file,
|
|
116
|
+
ContentItemType_input_image: ()=>ContentItemType_input_image,
|
|
117
|
+
ContentItemType_input_text: ()=>ContentItemType_input_text,
|
|
118
|
+
ContentItemType_input_video: ()=>ContentItemType_input_video,
|
|
119
|
+
ContentItemType_output_text: ()=>ContentItemType_output_text,
|
|
120
|
+
ContentItemType_summary_text: ()=>ContentItemType_summary_text,
|
|
121
|
+
MessageRole_assistant: ()=>MessageRole_assistant,
|
|
122
|
+
MessageRole_developer: ()=>MessageRole_developer,
|
|
123
|
+
MessageRole_system: ()=>"system",
|
|
124
|
+
MessageRole_user: ()=>"user",
|
|
125
|
+
getEventType: ()=>getEventType,
|
|
126
|
+
isDelta: ()=>isDelta,
|
|
127
|
+
isDeltaDone: ()=>isDeltaDone,
|
|
128
|
+
isResponseDone: ()=>isResponseDone
|
|
129
|
+
});
|
|
130
|
+
const external_axios_namespaceObject = require("axios");
|
|
131
|
+
var external_axios_default = /*#__PURE__*/ __webpack_require__.n(external_axios_namespaceObject);
|
|
132
|
+
const external_fs_namespaceObject = require("fs");
|
|
133
|
+
const external_https_namespaceObject = require("https");
|
|
134
|
+
const external_crypto_namespaceObject = require("crypto");
|
|
135
|
+
function genRequestId() {
|
|
136
|
+
const now = new Date();
|
|
137
|
+
const ts = now.getFullYear().toString() + String(now.getMonth() + 1).padStart(2, "0") + String(now.getDate()).padStart(2, "0") + String(now.getHours()).padStart(2, "0") + String(now.getMinutes()).padStart(2, "0") + String(now.getSeconds()).padStart(2, "0");
|
|
138
|
+
const hex = (0, external_crypto_namespaceObject.randomBytes)(10).toString("hex");
|
|
139
|
+
return ts + hex;
|
|
140
|
+
}
|
|
141
|
+
function calculateRetryDelay(attemptNumber, minRetryDelay, maxRetryDelay) {
|
|
142
|
+
const base = Math.min(minRetryDelay * Math.pow(2, attemptNumber - 1), maxRetryDelay);
|
|
143
|
+
return Math.floor(Math.min(maxRetryDelay, base + Math.random() * base));
|
|
144
|
+
}
|
|
145
|
+
async function retry(policy, fn, isRetryable, signal) {
|
|
146
|
+
let lastError;
|
|
147
|
+
for(let attempt = 0; attempt <= policy.maxAttempts; attempt++)try {
|
|
148
|
+
return await fn();
|
|
149
|
+
} catch (err) {
|
|
150
|
+
lastError = err;
|
|
151
|
+
if (!isRetryable(err)) throw err;
|
|
152
|
+
if (attempt === policy.maxAttempts) break;
|
|
153
|
+
const delayMs = calculateRetryDelay(attempt + 1, policy.initialBackoffMs, policy.maxBackoffMs);
|
|
154
|
+
await sleep(delayMs, signal);
|
|
155
|
+
}
|
|
156
|
+
throw lastError;
|
|
157
|
+
}
|
|
158
|
+
function sleep(ms, signal) {
|
|
159
|
+
return new Promise((resolve, reject)=>{
|
|
160
|
+
if (signal?.aborted) return void reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
161
|
+
const timer = setTimeout(resolve, ms);
|
|
162
|
+
signal?.addEventListener("abort", ()=>{
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
165
|
+
}, {
|
|
166
|
+
once: true
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function _define_property(obj, key, value) {
|
|
171
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
172
|
+
value: value,
|
|
173
|
+
enumerable: true,
|
|
174
|
+
configurable: true,
|
|
175
|
+
writable: true
|
|
176
|
+
});
|
|
177
|
+
else obj[key] = value;
|
|
178
|
+
return obj;
|
|
179
|
+
}
|
|
180
|
+
class Breaker {
|
|
181
|
+
allow(waitIndex) {
|
|
182
|
+
const elapsed = (Date.now() - this.allowTime) / 1000;
|
|
183
|
+
if (elapsed <= 0) return false;
|
|
184
|
+
if (elapsed > 10) return true;
|
|
185
|
+
return waitIndex < Math.pow(2, elapsed);
|
|
186
|
+
}
|
|
187
|
+
getAllowedDelayMs() {
|
|
188
|
+
const delay = this.allowTime - Date.now();
|
|
189
|
+
return delay < 1000 ? 1000 : delay;
|
|
190
|
+
}
|
|
191
|
+
reset(durationMs) {
|
|
192
|
+
this.allowTime = Date.now() + durationMs;
|
|
193
|
+
}
|
|
194
|
+
async wait() {
|
|
195
|
+
const id = String(this.nextIndex++);
|
|
196
|
+
const idx = this.waiters.size;
|
|
197
|
+
this.waiters.set(id, idx);
|
|
198
|
+
try {
|
|
199
|
+
while(!this.allow(idx))await new Promise((r)=>setTimeout(r, this.getAllowedDelayMs()));
|
|
200
|
+
} finally{
|
|
201
|
+
this.waiters.delete(id);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
constructor(){
|
|
205
|
+
_define_property(this, "allowTime", void 0);
|
|
206
|
+
_define_property(this, "waiters", new Map());
|
|
207
|
+
_define_property(this, "nextIndex", 0);
|
|
208
|
+
this.allowTime = Date.now();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function breaker_provider_define_property(obj, key, value) {
|
|
212
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
213
|
+
value: value,
|
|
214
|
+
enumerable: true,
|
|
215
|
+
configurable: true,
|
|
216
|
+
writable: true
|
|
217
|
+
});
|
|
218
|
+
else obj[key] = value;
|
|
219
|
+
return obj;
|
|
220
|
+
}
|
|
221
|
+
class ModelBreakerProvider {
|
|
222
|
+
getOrCreate(model) {
|
|
223
|
+
let breaker = this.breakers.get(model);
|
|
224
|
+
if (!breaker) {
|
|
225
|
+
breaker = new Breaker();
|
|
226
|
+
this.breakers.set(model, breaker);
|
|
227
|
+
}
|
|
228
|
+
return breaker;
|
|
229
|
+
}
|
|
230
|
+
constructor(){
|
|
231
|
+
breaker_provider_define_property(this, "breakers", new Map());
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function sse_decoder_define_property(obj, key, value) {
|
|
235
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
236
|
+
value: value,
|
|
237
|
+
enumerable: true,
|
|
238
|
+
configurable: true,
|
|
239
|
+
writable: true
|
|
240
|
+
});
|
|
241
|
+
else obj[key] = value;
|
|
242
|
+
return obj;
|
|
243
|
+
}
|
|
244
|
+
var _computedKey;
|
|
245
|
+
_computedKey = Symbol.asyncIterator;
|
|
246
|
+
let _computedKey1 = _computedKey;
|
|
247
|
+
class EventStreamDecoder {
|
|
248
|
+
async *[_computedKey1]() {
|
|
249
|
+
const lines = this.readLines();
|
|
250
|
+
let event = "";
|
|
251
|
+
let dataLines = [];
|
|
252
|
+
for await (const line of lines){
|
|
253
|
+
if ("" === line) {
|
|
254
|
+
if (dataLines.length > 0) yield {
|
|
255
|
+
event,
|
|
256
|
+
data: dataLines.join("\n")
|
|
257
|
+
};
|
|
258
|
+
event = "";
|
|
259
|
+
dataLines = [];
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (line.startsWith(":")) continue;
|
|
263
|
+
const colonIdx = line.indexOf(":");
|
|
264
|
+
let field;
|
|
265
|
+
let value;
|
|
266
|
+
if (-1 === colonIdx) {
|
|
267
|
+
field = line;
|
|
268
|
+
value = "";
|
|
269
|
+
} else {
|
|
270
|
+
field = line.slice(0, colonIdx);
|
|
271
|
+
value = line.slice(colonIdx + 1);
|
|
272
|
+
if (value.startsWith(" ")) value = value.slice(1);
|
|
273
|
+
}
|
|
274
|
+
switch(field){
|
|
275
|
+
case "event":
|
|
276
|
+
event = value;
|
|
277
|
+
break;
|
|
278
|
+
case "data":
|
|
279
|
+
dataLines.push(value);
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (dataLines.length > 0) yield {
|
|
284
|
+
event,
|
|
285
|
+
data: dataLines.join("\n")
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
async *readLines() {
|
|
289
|
+
const chunks = this.iterateStream();
|
|
290
|
+
for await (const chunk of chunks){
|
|
291
|
+
this.buffer += "string" == typeof chunk ? chunk : this.decoder.decode(chunk, {
|
|
292
|
+
stream: true
|
|
293
|
+
});
|
|
294
|
+
const lines = this.buffer.split("\n");
|
|
295
|
+
this.buffer = lines.pop() ?? "";
|
|
296
|
+
for (const line of lines)yield line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
297
|
+
}
|
|
298
|
+
if (this.buffer.length > 0) {
|
|
299
|
+
yield this.buffer;
|
|
300
|
+
this.buffer = "";
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async *iterateStream() {
|
|
304
|
+
const stream = this.stream;
|
|
305
|
+
if ("function" == typeof stream.getReader) {
|
|
306
|
+
this.reader = stream.getReader();
|
|
307
|
+
try {
|
|
308
|
+
while(true){
|
|
309
|
+
const { done, value } = await this.reader.read();
|
|
310
|
+
if (done) break;
|
|
311
|
+
if (value) yield value;
|
|
312
|
+
}
|
|
313
|
+
} finally{
|
|
314
|
+
this.reader.releaseLock();
|
|
315
|
+
this.reader = null;
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (Symbol.asyncIterator in stream) {
|
|
320
|
+
for await (const chunk of stream)yield chunk;
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
throw new Error("Unsupported stream type");
|
|
324
|
+
}
|
|
325
|
+
cancel() {
|
|
326
|
+
this.reader?.cancel();
|
|
327
|
+
}
|
|
328
|
+
constructor(stream){
|
|
329
|
+
sse_decoder_define_property(this, "stream", void 0);
|
|
330
|
+
sse_decoder_define_property(this, "reader", void 0);
|
|
331
|
+
sse_decoder_define_property(this, "decoder", void 0);
|
|
332
|
+
sse_decoder_define_property(this, "buffer", void 0);
|
|
333
|
+
this.stream = stream;
|
|
334
|
+
this.reader = null;
|
|
335
|
+
this.decoder = new TextDecoder();
|
|
336
|
+
this.buffer = "";
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function http_request_error_define_property(obj, key, value) {
|
|
340
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
341
|
+
value: value,
|
|
342
|
+
enumerable: true,
|
|
343
|
+
configurable: true,
|
|
344
|
+
writable: true
|
|
345
|
+
});
|
|
346
|
+
else obj[key] = value;
|
|
347
|
+
return obj;
|
|
348
|
+
}
|
|
349
|
+
class HttpRequestError extends Error {
|
|
350
|
+
constructor(name = "Exception", message, status, data, originalError){
|
|
351
|
+
super(message), http_request_error_define_property(this, "status", void 0), http_request_error_define_property(this, "data", void 0), http_request_error_define_property(this, "originalError", void 0), http_request_error_define_property(this, "name", void 0);
|
|
352
|
+
this.name = name;
|
|
353
|
+
this.status = status;
|
|
354
|
+
this.data = data;
|
|
355
|
+
this.originalError = originalError;
|
|
356
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, HttpRequestError);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function error_define_property(obj, key, value) {
|
|
360
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
361
|
+
value: value,
|
|
362
|
+
enumerable: true,
|
|
363
|
+
configurable: true,
|
|
364
|
+
writable: true
|
|
365
|
+
});
|
|
366
|
+
else obj[key] = value;
|
|
367
|
+
return obj;
|
|
368
|
+
}
|
|
369
|
+
class ArkAPIError extends HttpRequestError {
|
|
370
|
+
get httpStatusCode() {
|
|
371
|
+
return this.status ?? 0;
|
|
372
|
+
}
|
|
373
|
+
toString() {
|
|
374
|
+
return `Error code: ${this.httpStatusCode} - ${JSON.stringify({
|
|
375
|
+
code: this.code,
|
|
376
|
+
message: this.message,
|
|
377
|
+
param: this.param,
|
|
378
|
+
type: this.type,
|
|
379
|
+
request_id: this.requestId
|
|
380
|
+
})}`;
|
|
381
|
+
}
|
|
382
|
+
constructor(opts){
|
|
383
|
+
super("ApiException", opts.message, opts.httpStatusCode, void 0, void 0), error_define_property(this, "code", void 0), error_define_property(this, "param", void 0), error_define_property(this, "type", void 0), error_define_property(this, "requestId", void 0);
|
|
384
|
+
this.name = "ArkAPIError";
|
|
385
|
+
this.code = opts.code;
|
|
386
|
+
this.param = opts.param;
|
|
387
|
+
this.type = opts.type;
|
|
388
|
+
this.requestId = opts.requestId;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
class ArkRequestError extends HttpRequestError {
|
|
392
|
+
get httpStatusCode() {
|
|
393
|
+
return this.status ?? 0;
|
|
394
|
+
}
|
|
395
|
+
toString() {
|
|
396
|
+
return `RequestError code: ${this.httpStatusCode}, err: ${this.originalError?.message ?? this.message}, request_id: ${this.requestId}`;
|
|
397
|
+
}
|
|
398
|
+
constructor(opts){
|
|
399
|
+
super("NetworkError", opts.message, opts.httpStatusCode, void 0, opts.cause), error_define_property(this, "requestId", void 0);
|
|
400
|
+
this.name = "ArkRequestError";
|
|
401
|
+
this.requestId = opts.requestId;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const ErrTooManyEmptyStreamMessages = new Error("stream has sent too many empty messages");
|
|
405
|
+
const ErrChatCompletionStreamNotSupported = new Error("streaming is not supported with this method, please use CreateChatCompletionStream");
|
|
406
|
+
const ErrContentFieldsMisused = new Error("can't use both Content and MultiContent properties simultaneously");
|
|
407
|
+
const ErrBodyWithoutEndpoint = new Error("can't fetch endpoint sts token without endpoint");
|
|
408
|
+
const ErrBodyWithoutBot = new Error("can't fetch bot sts token without bot id");
|
|
409
|
+
const ErrAKSKNotSupported = new Error("ak&sk authentication is currently not supported for this method, please use api key instead");
|
|
410
|
+
const ErrBodyWithoutProjectName = new Error("project name is required for preset endpoint");
|
|
411
|
+
const external_path_namespaceObject = require("path");
|
|
412
|
+
const external_os_namespaceObject = require("os");
|
|
413
|
+
function key_agreement_define_property(obj, key, value) {
|
|
414
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
415
|
+
value: value,
|
|
416
|
+
enumerable: true,
|
|
417
|
+
configurable: true,
|
|
418
|
+
writable: true
|
|
419
|
+
});
|
|
420
|
+
else obj[key] = value;
|
|
421
|
+
return obj;
|
|
422
|
+
}
|
|
423
|
+
const AES_KEY_SIZE = 32;
|
|
424
|
+
const AES_NONCE_SIZE = 12;
|
|
425
|
+
const CIPHER_VERSION_AICC_V01 = "AICCv01";
|
|
426
|
+
class KeyAgreementClient {
|
|
427
|
+
generateECIESKeyPair() {
|
|
428
|
+
const ecdh = external_crypto_namespaceObject.createECDH("prime256v1");
|
|
429
|
+
ecdh.generateKeys();
|
|
430
|
+
const serverPubKeyDer = this.serverPublicKey.export({
|
|
431
|
+
type: "spki",
|
|
432
|
+
format: "der"
|
|
433
|
+
});
|
|
434
|
+
const serverPubKey = extractRawPublicKeyFromSPKI(serverPubKeyDer);
|
|
435
|
+
const sharedSecret = ecdh.computeSecret(serverPubKey);
|
|
436
|
+
const keyMaterial = hkdfExpand(sharedSecret, AES_KEY_SIZE + AES_NONCE_SIZE);
|
|
437
|
+
const sessionToken = ecdh.getPublicKey("base64");
|
|
438
|
+
return [
|
|
439
|
+
keyMaterial,
|
|
440
|
+
sessionToken
|
|
441
|
+
];
|
|
442
|
+
}
|
|
443
|
+
constructor(pemCert){
|
|
444
|
+
key_agreement_define_property(this, "serverPublicKey", void 0);
|
|
445
|
+
const x509 = new external_crypto_namespaceObject.X509Certificate(pemCert);
|
|
446
|
+
this.serverPublicKey = x509.publicKey;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function extractRawPublicKeyFromSPKI(spkiDer) {
|
|
450
|
+
const uncompressedKeyLen = 65;
|
|
451
|
+
if (spkiDer.length >= uncompressedKeyLen) return spkiDer.subarray(spkiDer.length - uncompressedKeyLen);
|
|
452
|
+
return spkiDer;
|
|
453
|
+
}
|
|
454
|
+
function hkdfExpand(ikm, length) {
|
|
455
|
+
const derivedKey = external_crypto_namespaceObject.hkdfSync("sha256", ikm, Buffer.alloc(0), Buffer.alloc(0), length);
|
|
456
|
+
return Buffer.from(derivedKey);
|
|
457
|
+
}
|
|
458
|
+
function aesGcmEncrypt(key, nonce, plaintext) {
|
|
459
|
+
const cipher = external_crypto_namespaceObject.createCipheriv("aes-256-gcm", key, nonce);
|
|
460
|
+
const encrypted = Buffer.concat([
|
|
461
|
+
cipher.update(plaintext),
|
|
462
|
+
cipher.final()
|
|
463
|
+
]);
|
|
464
|
+
const tag = cipher.getAuthTag();
|
|
465
|
+
return Buffer.concat([
|
|
466
|
+
encrypted,
|
|
467
|
+
tag
|
|
468
|
+
]);
|
|
469
|
+
}
|
|
470
|
+
function aesGcmDecrypt(key, nonce, ciphertext) {
|
|
471
|
+
const tagLength = 16;
|
|
472
|
+
const encrypted = ciphertext.subarray(0, ciphertext.length - tagLength);
|
|
473
|
+
const tag = ciphertext.subarray(ciphertext.length - tagLength);
|
|
474
|
+
const decipher = external_crypto_namespaceObject.createDecipheriv("aes-256-gcm", key, nonce);
|
|
475
|
+
decipher.setAuthTag(tag);
|
|
476
|
+
return Buffer.concat([
|
|
477
|
+
decipher.update(encrypted),
|
|
478
|
+
decipher.final()
|
|
479
|
+
]);
|
|
480
|
+
}
|
|
481
|
+
function aesGcmEncryptBase64(key, nonce, plaintext) {
|
|
482
|
+
const encrypted = aesGcmEncrypt(key, nonce, Buffer.from(plaintext, "utf-8"));
|
|
483
|
+
return encrypted.toString("base64");
|
|
484
|
+
}
|
|
485
|
+
function aesGcmDecryptBase64(key, nonce, ciphertext) {
|
|
486
|
+
const cipherBuf = Buffer.from(ciphertext, "base64");
|
|
487
|
+
const decrypted = aesGcmDecrypt(key, nonce, cipherBuf);
|
|
488
|
+
return decrypted.toString("utf-8");
|
|
489
|
+
}
|
|
490
|
+
function getCertInfo(certPem) {
|
|
491
|
+
try {
|
|
492
|
+
const x509 = new external_crypto_namespaceObject.X509Certificate(certPem);
|
|
493
|
+
const altNames = x509.subjectAltName ?? "";
|
|
494
|
+
const dnsNames = [];
|
|
495
|
+
for (const part of altNames.split(",")){
|
|
496
|
+
const trimmed = part.trim();
|
|
497
|
+
if (trimmed.startsWith("DNS:")) dnsNames.push(trimmed.substring(4));
|
|
498
|
+
}
|
|
499
|
+
const expireTime = Math.floor(new Date(x509.validTo).getTime() / 1000);
|
|
500
|
+
if (dnsNames.length > 1 && dnsNames[0].startsWith("ring.") && dnsNames[1].startsWith("key.")) return {
|
|
501
|
+
ringId: dnsNames[0].substring(5),
|
|
502
|
+
keyId: dnsNames[1].substring(4),
|
|
503
|
+
expireTime
|
|
504
|
+
};
|
|
505
|
+
return {
|
|
506
|
+
ringId: "",
|
|
507
|
+
keyId: "",
|
|
508
|
+
expireTime
|
|
509
|
+
};
|
|
510
|
+
} catch {
|
|
511
|
+
return {
|
|
512
|
+
ringId: "",
|
|
513
|
+
keyId: "",
|
|
514
|
+
expireTime: 0
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
function checkIsModeAICC() {
|
|
519
|
+
return "AICC" === process.env.VOLC_ARK_ENCRYPTION;
|
|
520
|
+
}
|
|
521
|
+
function loadLocalCertificate(model) {
|
|
522
|
+
if (!model || /[/\\:..]/.test(model)) return null;
|
|
523
|
+
const dir = external_path_namespaceObject.join(external_os_namespaceObject.homedir(), ".ark", "certificates");
|
|
524
|
+
const certPath = external_path_namespaceObject.join(dir, `${model}.pem`);
|
|
525
|
+
try {
|
|
526
|
+
const stat = external_fs_namespaceObject.statSync(certPath);
|
|
527
|
+
const age = Date.now() - stat.mtimeMs;
|
|
528
|
+
if (age > 1209600000) {
|
|
529
|
+
external_fs_namespaceObject.unlinkSync(certPath);
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
const certPem = external_fs_namespaceObject.readFileSync(certPath, "utf-8");
|
|
533
|
+
const { ringId, keyId } = getCertInfo(certPem);
|
|
534
|
+
const aiccEnabled = checkIsModeAICC();
|
|
535
|
+
if (("" === ringId || "" === keyId) && !aiccEnabled) return certPem;
|
|
536
|
+
if ("" !== ringId && "" !== keyId && aiccEnabled) return certPem;
|
|
537
|
+
external_fs_namespaceObject.unlinkSync(certPath);
|
|
538
|
+
return null;
|
|
539
|
+
} catch {
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function saveToLocalCertificate(model, certPem) {
|
|
544
|
+
if (!model || /[/\\:..]/.test(model)) return;
|
|
545
|
+
const dir = external_path_namespaceObject.join(external_os_namespaceObject.homedir(), ".ark", "certificates");
|
|
546
|
+
external_fs_namespaceObject.mkdirSync(dir, {
|
|
547
|
+
recursive: true
|
|
548
|
+
});
|
|
549
|
+
const certPath = external_path_namespaceObject.join(dir, `${model}.pem`);
|
|
550
|
+
external_fs_namespaceObject.writeFileSync(certPath, certPem, "utf-8");
|
|
551
|
+
}
|
|
552
|
+
class E2eeClient {
|
|
553
|
+
generateECIESKeyPair() {
|
|
554
|
+
return this.cipher.generateECIESKeyPair();
|
|
555
|
+
}
|
|
556
|
+
getEncryptInfo() {
|
|
557
|
+
return JSON.stringify(this.info);
|
|
558
|
+
}
|
|
559
|
+
getExpireTime() {
|
|
560
|
+
return this.info.expire_time;
|
|
561
|
+
}
|
|
562
|
+
constructor(certificate){
|
|
563
|
+
key_agreement_define_property(this, "certificate", void 0);
|
|
564
|
+
key_agreement_define_property(this, "cipher", void 0);
|
|
565
|
+
key_agreement_define_property(this, "info", void 0);
|
|
566
|
+
key_agreement_define_property(this, "isAICC", void 0);
|
|
567
|
+
this.certificate = certificate;
|
|
568
|
+
this.cipher = new KeyAgreementClient(certificate);
|
|
569
|
+
const { ringId, keyId, expireTime } = getCertInfo(certificate);
|
|
570
|
+
this.info = {
|
|
571
|
+
expire_time: expireTime
|
|
572
|
+
};
|
|
573
|
+
this.isAICC = false;
|
|
574
|
+
if (ringId && keyId) {
|
|
575
|
+
this.info.version = CIPHER_VERSION_AICC_V01;
|
|
576
|
+
this.info.ring_id = ringId;
|
|
577
|
+
this.info.key_id = keyId;
|
|
578
|
+
this.isAICC = true;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const encrypt_chat_AES_KEY_SIZE = 32;
|
|
583
|
+
const encrypt_chat_AES_NONCE_SIZE = 12;
|
|
584
|
+
function encryptURL(urlString, fn) {
|
|
585
|
+
try {
|
|
586
|
+
const parsed = new URL(urlString);
|
|
587
|
+
if ([
|
|
588
|
+
"https:",
|
|
589
|
+
"http:",
|
|
590
|
+
"file:",
|
|
591
|
+
"ftp:"
|
|
592
|
+
].includes(parsed.protocol)) return urlString;
|
|
593
|
+
} catch {}
|
|
594
|
+
if (urlString.startsWith("data:")) return fn(urlString);
|
|
595
|
+
return urlString;
|
|
596
|
+
}
|
|
597
|
+
function processMessageContent(content, fn) {
|
|
598
|
+
if (null == content) return content;
|
|
599
|
+
if ("string" == typeof content) return fn(content);
|
|
600
|
+
for (const part of content){
|
|
601
|
+
if ("text" === part.type && part.text) part.text = fn(part.text);
|
|
602
|
+
if ("image_url" === part.type && part.image_url?.url) part.image_url.url = encryptURL(part.image_url.url, fn);
|
|
603
|
+
}
|
|
604
|
+
return content;
|
|
605
|
+
}
|
|
606
|
+
function deepCopyRequest(request) {
|
|
607
|
+
return JSON.parse(JSON.stringify(request));
|
|
608
|
+
}
|
|
609
|
+
function encryptChatRequest(keyNonce, request) {
|
|
610
|
+
if (keyNonce.length !== encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE) throw new Error(`keyNonce must be ${encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE} bytes, got ${keyNonce.length}`);
|
|
611
|
+
const key = keyNonce.subarray(0, encrypt_chat_AES_KEY_SIZE);
|
|
612
|
+
const nonce = keyNonce.subarray(encrypt_chat_AES_KEY_SIZE);
|
|
613
|
+
const fn = (text)=>aesGcmEncryptBase64(key, nonce, text);
|
|
614
|
+
for (const msg of request.messages ?? [])if (null != msg.content) msg.content = processMessageContent(msg.content, fn);
|
|
615
|
+
}
|
|
616
|
+
function decryptChatResponse(keyNonce, response) {
|
|
617
|
+
if (keyNonce.length !== encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE) throw new Error(`keyNonce must be ${encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE} bytes, got ${keyNonce.length}`);
|
|
618
|
+
const key = keyNonce.subarray(0, encrypt_chat_AES_KEY_SIZE);
|
|
619
|
+
const nonce = keyNonce.subarray(encrypt_chat_AES_KEY_SIZE);
|
|
620
|
+
const fn = (text)=>aesGcmDecryptBase64(key, nonce, text);
|
|
621
|
+
for (const choice of response.choices ?? [])if ("content_filter" !== choice.finish_reason) {
|
|
622
|
+
if (choice.message?.content != null) choice.message.content = processMessageContent(choice.message.content, fn);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
function decryptChatStreamResponse(keyNonce, response) {
|
|
626
|
+
if (keyNonce.length !== encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE) throw new Error(`keyNonce must be ${encrypt_chat_AES_KEY_SIZE + encrypt_chat_AES_NONCE_SIZE} bytes, got ${keyNonce.length}`);
|
|
627
|
+
const key = keyNonce.subarray(0, encrypt_chat_AES_KEY_SIZE);
|
|
628
|
+
const nonce = keyNonce.subarray(encrypt_chat_AES_KEY_SIZE);
|
|
629
|
+
const fn = (text)=>aesGcmDecryptBase64(key, nonce, text);
|
|
630
|
+
for (const choice of response.choices ?? [])if ("content_filter" !== choice.finish_reason) {
|
|
631
|
+
if (choice.delta?.content && "string" == typeof choice.delta.content) choice.delta.content = fn(choice.delta.content);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function normalizeChatCompletionResponse(resp) {
|
|
635
|
+
if (resp.choices) for (const choice of resp.choices)normalizeChatCompletionChoice(choice);
|
|
636
|
+
return resp;
|
|
637
|
+
}
|
|
638
|
+
function normalizeChatCompletionChoice(choice) {
|
|
639
|
+
if (choice.message && !("name" in choice.message)) choice.message.name = null;
|
|
640
|
+
if ("logprobs" in choice && (null === choice.logprobs || void 0 === choice.logprobs)) delete choice.logprobs;
|
|
641
|
+
if (!("finish_reason" in choice)) choice.finish_reason = null;
|
|
642
|
+
}
|
|
643
|
+
function normalizeChatCompletionStreamChunk(chunk) {
|
|
644
|
+
if (chunk.choices) for (const choice of chunk.choices)normalizeStreamChoice(choice);
|
|
645
|
+
if ("usage" in chunk && (null === chunk.usage || void 0 === chunk.usage)) delete chunk.usage;
|
|
646
|
+
return chunk;
|
|
647
|
+
}
|
|
648
|
+
function normalizeStreamChoice(choice) {
|
|
649
|
+
if (!("finish_reason" in choice)) choice.finish_reason = null;
|
|
650
|
+
if ("logprobs" in choice && (null === choice.logprobs || void 0 === choice.logprobs)) delete choice.logprobs;
|
|
651
|
+
if (choice.delta) {
|
|
652
|
+
if ("content" in choice.delta && "" === choice.delta.content) delete choice.delta.content;
|
|
653
|
+
if ("role" in choice.delta && "" === choice.delta.role) delete choice.delta.role;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function normalizeEmbeddingResponse(resp) {
|
|
657
|
+
if (resp.usage) normalizeUsage(resp.usage);
|
|
658
|
+
return resp;
|
|
659
|
+
}
|
|
660
|
+
function normalizeUsage(usage) {
|
|
661
|
+
if (void 0 === usage.completion_tokens || null === usage.completion_tokens) usage.completion_tokens = 0;
|
|
662
|
+
if (!usage.prompt_tokens_details) usage.prompt_tokens_details = {
|
|
663
|
+
cached_tokens: 0
|
|
664
|
+
};
|
|
665
|
+
if (!usage.completion_tokens_details) usage.completion_tokens_details = {
|
|
666
|
+
reasoning_tokens: 0
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function stream_reader_define_property(obj, key, value) {
|
|
670
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
671
|
+
value: value,
|
|
672
|
+
enumerable: true,
|
|
673
|
+
configurable: true,
|
|
674
|
+
writable: true
|
|
675
|
+
});
|
|
676
|
+
else obj[key] = value;
|
|
677
|
+
return obj;
|
|
678
|
+
}
|
|
679
|
+
var stream_reader_computedKey, stream_reader_computedKey1, _computedKey2, _computedKey3;
|
|
680
|
+
const ERROR_PREFIX = '{"error":';
|
|
681
|
+
class BaseStreamReader {
|
|
682
|
+
close() {
|
|
683
|
+
this.isFinished = true;
|
|
684
|
+
this.cleanup?.();
|
|
685
|
+
this.cleanup = void 0;
|
|
686
|
+
}
|
|
687
|
+
constructor(stream, emptyMessagesLimit, headers, cleanup){
|
|
688
|
+
stream_reader_define_property(this, "stream", void 0);
|
|
689
|
+
stream_reader_define_property(this, "emptyMessagesLimit", void 0);
|
|
690
|
+
stream_reader_define_property(this, "headers", void 0);
|
|
691
|
+
stream_reader_define_property(this, "isFinished", void 0);
|
|
692
|
+
stream_reader_define_property(this, "emptyMessagesCount", void 0);
|
|
693
|
+
stream_reader_define_property(this, "cleanup", void 0);
|
|
694
|
+
this.stream = stream;
|
|
695
|
+
this.emptyMessagesLimit = emptyMessagesLimit;
|
|
696
|
+
this.headers = headers;
|
|
697
|
+
this.isFinished = false;
|
|
698
|
+
this.emptyMessagesCount = 0;
|
|
699
|
+
this.cleanup = cleanup;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
stream_reader_computedKey = Symbol.asyncIterator;
|
|
703
|
+
let _computedKey4 = stream_reader_computedKey;
|
|
704
|
+
class DataLineStreamReader extends BaseStreamReader {
|
|
705
|
+
async *[_computedKey4]() {
|
|
706
|
+
const chunks = this.iterateChunks();
|
|
707
|
+
let errBuffer = "";
|
|
708
|
+
let hasError = false;
|
|
709
|
+
for await (const rawLine of this.readLines(chunks)){
|
|
710
|
+
if (this.isFinished) return;
|
|
711
|
+
const trimmed = rawLine.trim();
|
|
712
|
+
if ("" === trimmed) continue;
|
|
713
|
+
let dataContent = null;
|
|
714
|
+
if (trimmed.startsWith("data:")) dataContent = trimmed.slice(5).trim();
|
|
715
|
+
if (dataContent && dataContent.startsWith(ERROR_PREFIX)) {
|
|
716
|
+
hasError = true;
|
|
717
|
+
errBuffer += dataContent;
|
|
718
|
+
continue;
|
|
719
|
+
}
|
|
720
|
+
if (hasError) {
|
|
721
|
+
errBuffer += trimmed.startsWith("data:") ? trimmed.slice(5).trim() : trimmed;
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
if (null === dataContent) {
|
|
725
|
+
this.emptyMessagesCount++;
|
|
726
|
+
if (this.emptyMessagesCount > this.emptyMessagesLimit) throw ErrTooManyEmptyStreamMessages;
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
if ("[DONE]" === dataContent) return void this.close();
|
|
730
|
+
const parsed = JSON.parse(dataContent);
|
|
731
|
+
this.emptyMessagesCount = 0;
|
|
732
|
+
yield parsed;
|
|
733
|
+
}
|
|
734
|
+
if (hasError && errBuffer) {
|
|
735
|
+
const errResp = JSON.parse(errBuffer);
|
|
736
|
+
if (errResp.error) throw new ArkAPIError({
|
|
737
|
+
message: errResp.error.message,
|
|
738
|
+
code: errResp.error.code,
|
|
739
|
+
param: errResp.error.param,
|
|
740
|
+
type: errResp.error.type,
|
|
741
|
+
httpStatusCode: 0,
|
|
742
|
+
requestId: errResp.error.request_id ?? this.headers?.["x-client-request-id"] ?? ""
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async *readLines(chunks) {
|
|
747
|
+
for await (const chunk of chunks){
|
|
748
|
+
this.buffer += chunk;
|
|
749
|
+
const lines = this.buffer.split("\n");
|
|
750
|
+
this.buffer = lines.pop() ?? "";
|
|
751
|
+
for (const line of lines)yield line;
|
|
752
|
+
}
|
|
753
|
+
if (this.buffer) {
|
|
754
|
+
yield this.buffer;
|
|
755
|
+
this.buffer = "";
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
async *iterateChunks() {
|
|
759
|
+
const stream = this.stream;
|
|
760
|
+
if ("function" == typeof stream.getReader) {
|
|
761
|
+
const reader = stream.getReader();
|
|
762
|
+
try {
|
|
763
|
+
while(true){
|
|
764
|
+
const { done, value } = await reader.read();
|
|
765
|
+
if (done) break;
|
|
766
|
+
yield "string" == typeof value ? value : this.decoder.decode(value, {
|
|
767
|
+
stream: true
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
} finally{
|
|
771
|
+
reader.releaseLock();
|
|
772
|
+
}
|
|
773
|
+
} else if (Symbol.asyncIterator in stream) for await (const chunk of stream)yield "string" == typeof chunk ? chunk : this.decoder.decode(chunk, {
|
|
774
|
+
stream: true
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
constructor(...args){
|
|
778
|
+
super(...args), stream_reader_define_property(this, "decoder", new TextDecoder()), stream_reader_define_property(this, "buffer", "");
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
stream_reader_computedKey1 = Symbol.asyncIterator;
|
|
782
|
+
let _computedKey5 = stream_reader_computedKey1;
|
|
783
|
+
class ChatCompletionStreamReader extends DataLineStreamReader {
|
|
784
|
+
async *[_computedKey5]() {
|
|
785
|
+
for await (const chunk of this.iterateBase()){
|
|
786
|
+
if (this.keyNonce.length > 0) decryptChatStreamResponse(this.keyNonce, chunk);
|
|
787
|
+
yield normalizeChatCompletionStreamChunk(chunk);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
async *iterateBase() {
|
|
791
|
+
yield* super[Symbol.asyncIterator]();
|
|
792
|
+
}
|
|
793
|
+
constructor(stream, emptyMessagesLimit, headers, keyNonce, cleanup){
|
|
794
|
+
super(stream, emptyMessagesLimit, headers, cleanup), stream_reader_define_property(this, "keyNonce", void 0);
|
|
795
|
+
this.keyNonce = keyNonce ?? Buffer.alloc(0);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
_computedKey2 = Symbol.asyncIterator;
|
|
799
|
+
class BotChatCompletionStreamReader extends DataLineStreamReader {
|
|
800
|
+
async *[_computedKey2]() {
|
|
801
|
+
for await (const chunk of this.iterateBase())yield normalizeChatCompletionStreamChunk(chunk);
|
|
802
|
+
}
|
|
803
|
+
async *iterateBase() {
|
|
804
|
+
yield* super[Symbol.asyncIterator]();
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
class ImageGenerationStreamReader extends DataLineStreamReader {
|
|
808
|
+
}
|
|
809
|
+
_computedKey3 = Symbol.asyncIterator;
|
|
810
|
+
let _computedKey6 = _computedKey3;
|
|
811
|
+
class ResponsesStreamReader extends BaseStreamReader {
|
|
812
|
+
async *[_computedKey6]() {
|
|
813
|
+
for await (const sseEvent of this.decoder){
|
|
814
|
+
if (this.isFinished) return;
|
|
815
|
+
const data = sseEvent.data.trim();
|
|
816
|
+
if (data.startsWith(ERROR_PREFIX)) {
|
|
817
|
+
const errResp = JSON.parse(data);
|
|
818
|
+
if (errResp.error) throw new ArkAPIError({
|
|
819
|
+
message: errResp.error.message,
|
|
820
|
+
code: errResp.error.code,
|
|
821
|
+
param: errResp.error.param,
|
|
822
|
+
type: errResp.error.type,
|
|
823
|
+
httpStatusCode: 0,
|
|
824
|
+
requestId: errResp.error.request_id ?? this.headers?.["x-client-request-id"] ?? ""
|
|
825
|
+
});
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
if (data.startsWith("[DONE]")) return void this.close();
|
|
829
|
+
const parsed = JSON.parse(data);
|
|
830
|
+
yield parsed;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
close() {
|
|
834
|
+
super.close();
|
|
835
|
+
this.decoder.cancel();
|
|
836
|
+
}
|
|
837
|
+
constructor(stream, emptyMessagesLimit, headers, cleanup){
|
|
838
|
+
super(stream, emptyMessagesLimit, headers, cleanup), stream_reader_define_property(this, "decoder", void 0);
|
|
839
|
+
this.decoder = new EventStreamDecoder(stream);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const defaultBaseURL = "https://ark.cn-beijing.volces.com/api/v3";
|
|
843
|
+
const defaultRegion = "cn-beijing";
|
|
844
|
+
const defaultEmptyMessagesLimit = 300;
|
|
845
|
+
const defaultRetryTimes = 2;
|
|
846
|
+
const defaultTimeoutMs = 600000;
|
|
847
|
+
const defaultBatchMaxParallel = 3000;
|
|
848
|
+
const resourceTypeEndpoint = "endpoint";
|
|
849
|
+
const resourceTypePresetEndpoint = "presetendpoint";
|
|
850
|
+
function resolveConfig(config) {
|
|
851
|
+
let baseURL = config.baseURL ?? process.env.ARK_BASE_URL ?? defaultBaseURL;
|
|
852
|
+
if (baseURL.endsWith("/")) baseURL = baseURL.slice(0, -1);
|
|
853
|
+
return {
|
|
854
|
+
apiKey: config.apiKey ?? process.env.ARK_API_KEY ?? "",
|
|
855
|
+
ak: config.ak ?? "",
|
|
856
|
+
sk: config.sk ?? "",
|
|
857
|
+
region: config.region ?? defaultRegion,
|
|
858
|
+
baseURL,
|
|
859
|
+
httpClient: config.httpClient,
|
|
860
|
+
emptyMessagesLimit: config.emptyMessagesLimit ?? defaultEmptyMessagesLimit,
|
|
861
|
+
retryTimes: config.retryTimes ?? defaultRetryTimes,
|
|
862
|
+
timeout: config.timeout ?? defaultTimeoutMs,
|
|
863
|
+
batchMaxParallel: config.batchMaxParallel ?? defaultBatchMaxParallel
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
const ClientRequestHeader = "X-Client-Request-Id";
|
|
867
|
+
const RetryAfterHeader = "Retry-After";
|
|
868
|
+
const ClientSessionTokenHeader = "X-Session-Token";
|
|
869
|
+
const ClientEncryptInfoHeader = "X-Encrypt-Info";
|
|
870
|
+
const ClientIsEncryptedHeader = "x-is-encrypted";
|
|
871
|
+
const PurposeUserData = "user_data";
|
|
872
|
+
const FileStatusProcessing = "processing";
|
|
873
|
+
function client_define_property(obj, key, value) {
|
|
874
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
875
|
+
value: value,
|
|
876
|
+
enumerable: true,
|
|
877
|
+
configurable: true,
|
|
878
|
+
writable: true
|
|
879
|
+
});
|
|
880
|
+
else obj[key] = value;
|
|
881
|
+
return obj;
|
|
882
|
+
}
|
|
883
|
+
const chatCompletionsSuffix = "/chat/completions";
|
|
884
|
+
const botChatCompletionsSuffix = "/bots/chat/completions";
|
|
885
|
+
const embeddingsSuffix = "/embeddings";
|
|
886
|
+
const multimodalEmbeddingsSuffix = "/embeddings/multimodal";
|
|
887
|
+
const generateImagesPath = "/images/generations";
|
|
888
|
+
const tokenizationSuffix = "/tokenization";
|
|
889
|
+
const contextCreateSuffix = "/context/create";
|
|
890
|
+
const contextChatSuffix = "/context/chat/completions";
|
|
891
|
+
const contentGenerationTaskPath = "/contents/generations/tasks";
|
|
892
|
+
const filePrefix = "/files";
|
|
893
|
+
const batchChatCompletionsSuffix = "/batch/chat/completions";
|
|
894
|
+
const batchEmbeddingsSuffix = "/batch/embeddings";
|
|
895
|
+
const batchMultiModalEmbeddingsSuffix = "/batch/embeddings/multimodal";
|
|
896
|
+
const e2eGetCertificatePath = "/e2e/get/certificate";
|
|
897
|
+
const FILE_SCHEME = "file";
|
|
898
|
+
const POLL_INTERVAL_MS = 3000;
|
|
899
|
+
const MAX_WAIT_TIME_MS = 600000;
|
|
900
|
+
class ArkRuntimeClient {
|
|
901
|
+
static withApiKey(apiKey, config = {}) {
|
|
902
|
+
return new ArkRuntimeClient({
|
|
903
|
+
...config,
|
|
904
|
+
apiKey
|
|
905
|
+
});
|
|
906
|
+
}
|
|
907
|
+
static withAkSk(ak, sk, config = {}) {
|
|
908
|
+
return new ArkRuntimeClient({
|
|
909
|
+
...config,
|
|
910
|
+
ak,
|
|
911
|
+
sk
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
fullURL(suffix) {
|
|
915
|
+
return `${this.config.baseURL}${suffix}`;
|
|
916
|
+
}
|
|
917
|
+
isAPIKeyAuth() {
|
|
918
|
+
return this.config.apiKey.length > 0;
|
|
919
|
+
}
|
|
920
|
+
getResourceTypeById(resourceId) {
|
|
921
|
+
if (resourceId.startsWith("ep-m-")) return resourceTypePresetEndpoint;
|
|
922
|
+
if (resourceId.startsWith("ep-")) return resourceTypeEndpoint;
|
|
923
|
+
if (resourceId.startsWith("bot-")) return "bot";
|
|
924
|
+
return resourceTypePresetEndpoint;
|
|
925
|
+
}
|
|
926
|
+
stsKey(resourceType, resourceId) {
|
|
927
|
+
return `${resourceType}#${resourceId}`;
|
|
928
|
+
}
|
|
929
|
+
needRefresh(resourceType, resourceId, delta) {
|
|
930
|
+
const info = this.resourceStsTokens.get(this.stsKey(resourceType, resourceId));
|
|
931
|
+
if (!info) return true;
|
|
932
|
+
return info.expiredTime - Math.floor(Date.now() / 1000) < delta;
|
|
933
|
+
}
|
|
934
|
+
async refreshToken(resourceType, resourceId, projectName) {
|
|
935
|
+
if (!this.needRefresh(resourceType, resourceId, this.advisoryRefreshTimeout)) return;
|
|
936
|
+
if (this.refreshPromise) {
|
|
937
|
+
await this.refreshPromise;
|
|
938
|
+
if (!this.needRefresh(resourceType, resourceId, this.advisoryRefreshTimeout)) return;
|
|
939
|
+
}
|
|
940
|
+
this.refreshPromise = this.doRefresh(resourceType, resourceId, projectName);
|
|
941
|
+
try {
|
|
942
|
+
await this.refreshPromise;
|
|
943
|
+
} finally{
|
|
944
|
+
this.refreshPromise = null;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
async doRefresh(resourceType, resourceId, projectName) {
|
|
948
|
+
try {
|
|
949
|
+
const { ARKClient } = await import("@volcengine/ark");
|
|
950
|
+
const { GetApiKeyCommand } = await import("@volcengine/ark");
|
|
951
|
+
const arkClient = new ARKClient({
|
|
952
|
+
accessKeyId: this.config.ak,
|
|
953
|
+
secretAccessKey: this.config.sk,
|
|
954
|
+
region: this.config.region
|
|
955
|
+
});
|
|
956
|
+
const input = {
|
|
957
|
+
DurationSeconds: 604800,
|
|
958
|
+
ResourceIds: [
|
|
959
|
+
resourceId
|
|
960
|
+
],
|
|
961
|
+
ResourceType: resourceType,
|
|
962
|
+
...projectName ? {
|
|
963
|
+
ProjectName: projectName
|
|
964
|
+
} : {}
|
|
965
|
+
};
|
|
966
|
+
const resp = await arkClient.send(new GetApiKeyCommand(input));
|
|
967
|
+
const result = resp.Result;
|
|
968
|
+
if (result?.ApiKey && result?.ExpiredTime) this.resourceStsTokens.set(this.stsKey(resourceType, resourceId), {
|
|
969
|
+
token: result.ApiKey,
|
|
970
|
+
expiredTime: result.ExpiredTime
|
|
971
|
+
});
|
|
972
|
+
} catch (err) {
|
|
973
|
+
const isMandatory = this.needRefresh(resourceType, resourceId, this.mandatoryRefreshTimeout);
|
|
974
|
+
if (isMandatory) throw err;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
async getEndpointStsToken(endpointId) {
|
|
978
|
+
return this.getResourceStsToken(resourceTypeEndpoint, endpointId);
|
|
979
|
+
}
|
|
980
|
+
async getResourceStsToken(resourceType, resourceId, projectName) {
|
|
981
|
+
await this.refreshToken(resourceType, resourceId, projectName ?? "");
|
|
982
|
+
const info = this.resourceStsTokens.get(this.stsKey(resourceType, resourceId));
|
|
983
|
+
return info?.token ?? "";
|
|
984
|
+
}
|
|
985
|
+
async getAuthHeader(resourceType, resourceId, projectName) {
|
|
986
|
+
if (this.isAPIKeyAuth()) return {
|
|
987
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
988
|
+
};
|
|
989
|
+
resourceType = this.getResourceTypeById(resourceId);
|
|
990
|
+
await this.refreshToken(resourceType, resourceId, projectName ?? "");
|
|
991
|
+
const info = this.resourceStsTokens.get(this.stsKey(resourceType, resourceId));
|
|
992
|
+
return {
|
|
993
|
+
Authorization: `Bearer ${info?.token ?? ""}`
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
retryPolicy() {
|
|
997
|
+
return {
|
|
998
|
+
maxAttempts: this.config.retryTimes,
|
|
999
|
+
initialBackoffMs: 500,
|
|
1000
|
+
maxBackoffMs: 8000
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
isRetryable(err) {
|
|
1004
|
+
if (err instanceof ArkAPIError) return err.httpStatusCode >= 500 || 429 === err.httpStatusCode;
|
|
1005
|
+
if (err instanceof ArkRequestError) return err.httpStatusCode >= 500;
|
|
1006
|
+
return false;
|
|
1007
|
+
}
|
|
1008
|
+
async executeRequest(method, url, resourceType, resourceId, opts) {
|
|
1009
|
+
const authHeader = await this.getAuthHeader(resourceType, resourceId, opts?.projectName);
|
|
1010
|
+
const requestId = genRequestId();
|
|
1011
|
+
const headers = {
|
|
1012
|
+
[ClientRequestHeader]: requestId,
|
|
1013
|
+
"Content-Type": "application/json",
|
|
1014
|
+
Accept: opts?.stream ? "text/event-stream" : "application/json",
|
|
1015
|
+
...authHeader,
|
|
1016
|
+
...opts?.customHeaders
|
|
1017
|
+
};
|
|
1018
|
+
if (opts?.stream) {
|
|
1019
|
+
headers["Cache-Control"] = "no-cache";
|
|
1020
|
+
headers["Connection"] = "keep-alive";
|
|
1021
|
+
}
|
|
1022
|
+
if (opts?.projectName) headers["X-Project-Name"] = opts.projectName;
|
|
1023
|
+
const axiosConfig = {
|
|
1024
|
+
method: method,
|
|
1025
|
+
url,
|
|
1026
|
+
headers,
|
|
1027
|
+
data: opts?.body,
|
|
1028
|
+
params: opts?.query,
|
|
1029
|
+
signal: opts?.signal,
|
|
1030
|
+
responseType: opts?.stream ? "stream" : "json",
|
|
1031
|
+
validateStatus: ()=>true
|
|
1032
|
+
};
|
|
1033
|
+
const resp = await this.httpClient.request(axiosConfig);
|
|
1034
|
+
if (resp.status < 200 || resp.status >= 400) this.handleErrorResponse(resp, requestId);
|
|
1035
|
+
return resp;
|
|
1036
|
+
}
|
|
1037
|
+
handleErrorResponse(resp, requestId) {
|
|
1038
|
+
let errData;
|
|
1039
|
+
try {
|
|
1040
|
+
errData = "string" == typeof resp.data ? JSON.parse(resp.data) : resp.data;
|
|
1041
|
+
} catch {
|
|
1042
|
+
throw new ArkRequestError({
|
|
1043
|
+
httpStatusCode: resp.status,
|
|
1044
|
+
message: `Request failed with status ${resp.status}`,
|
|
1045
|
+
requestId
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
if (errData?.error) throw new ArkAPIError({
|
|
1049
|
+
message: errData.error.message ?? "Unknown error",
|
|
1050
|
+
code: errData.error.code,
|
|
1051
|
+
param: errData.error.param,
|
|
1052
|
+
type: errData.error.type ?? "unknown",
|
|
1053
|
+
httpStatusCode: resp.status,
|
|
1054
|
+
requestId: errData.error.request_id ?? resp.headers?.[ClientRequestHeader.toLowerCase()] ?? requestId
|
|
1055
|
+
});
|
|
1056
|
+
throw new ArkRequestError({
|
|
1057
|
+
httpStatusCode: resp.status,
|
|
1058
|
+
message: `Request failed with status ${resp.status}`,
|
|
1059
|
+
requestId
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
async getE2eeClient(resourceId, authorization) {
|
|
1063
|
+
const cached = this.e2eeManager.get(resourceId);
|
|
1064
|
+
if (cached && cached.isAICC === checkIsModeAICC()) return cached;
|
|
1065
|
+
const existing = this.e2eeFlightMap.get(resourceId);
|
|
1066
|
+
if (existing) return existing;
|
|
1067
|
+
const promise = this.doLoadE2eeClient(resourceId, authorization);
|
|
1068
|
+
this.e2eeFlightMap.set(resourceId, promise);
|
|
1069
|
+
try {
|
|
1070
|
+
const client = await promise;
|
|
1071
|
+
return client;
|
|
1072
|
+
} finally{
|
|
1073
|
+
this.e2eeFlightMap.delete(resourceId);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
async doLoadE2eeClient(resourceId, authorization) {
|
|
1077
|
+
const cached = this.e2eeManager.get(resourceId);
|
|
1078
|
+
if (cached && cached.isAICC === checkIsModeAICC()) return cached;
|
|
1079
|
+
let certPem = loadLocalCertificate(resourceId);
|
|
1080
|
+
if (!certPem) {
|
|
1081
|
+
certPem = await this.loadServerCertificate(resourceId, authorization);
|
|
1082
|
+
saveToLocalCertificate(resourceId, certPem);
|
|
1083
|
+
}
|
|
1084
|
+
const client = new E2eeClient(certPem);
|
|
1085
|
+
this.e2eeManager.set(resourceId, client);
|
|
1086
|
+
return client;
|
|
1087
|
+
}
|
|
1088
|
+
async loadServerCertificate(resourceId, authorization) {
|
|
1089
|
+
const url = this.fullURL(e2eGetCertificatePath);
|
|
1090
|
+
const body = {
|
|
1091
|
+
model: resourceId
|
|
1092
|
+
};
|
|
1093
|
+
if (checkIsModeAICC()) body.type = "AICCv0.1";
|
|
1094
|
+
const resp = await this.httpClient.request({
|
|
1095
|
+
method: "POST",
|
|
1096
|
+
url,
|
|
1097
|
+
headers: {
|
|
1098
|
+
"Content-Type": "application/json",
|
|
1099
|
+
Accept: "application/json",
|
|
1100
|
+
Authorization: authorization,
|
|
1101
|
+
[ClientSessionTokenHeader]: e2eGetCertificatePath
|
|
1102
|
+
},
|
|
1103
|
+
data: body,
|
|
1104
|
+
validateStatus: ()=>true
|
|
1105
|
+
});
|
|
1106
|
+
if (resp.status < 200 || resp.status >= 400) throw new Error(`getting Certificate failed: HTTP ${resp.status}`);
|
|
1107
|
+
const cr = resp.data;
|
|
1108
|
+
if (cr.error && Object.keys(cr.error).length > 0) throw new Error(`getting Certificate failed: ${JSON.stringify(cr.error)}`);
|
|
1109
|
+
return cr.Certificate;
|
|
1110
|
+
}
|
|
1111
|
+
async encryptRequestBody(resourceId, requestId, authorization, request) {
|
|
1112
|
+
const e2eeClient = await this.getE2eeClient(resourceId, authorization);
|
|
1113
|
+
const [keyNonce, sessionToken] = e2eeClient.generateECIESKeyPair();
|
|
1114
|
+
const extraHeaders = {
|
|
1115
|
+
[ClientSessionTokenHeader]: sessionToken
|
|
1116
|
+
};
|
|
1117
|
+
if (checkIsModeAICC()) extraHeaders[ClientEncryptInfoHeader] = e2eeClient.getEncryptInfo();
|
|
1118
|
+
this.keyNonceMap.set(requestId, keyNonce);
|
|
1119
|
+
const requestCopy = deepCopyRequest(request);
|
|
1120
|
+
encryptChatRequest(keyNonce, requestCopy);
|
|
1121
|
+
return {
|
|
1122
|
+
body: requestCopy,
|
|
1123
|
+
extraHeaders
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
async preprocessResponseInput(input) {
|
|
1127
|
+
if (!input || "string" == typeof input || !Array.isArray(input)) return;
|
|
1128
|
+
await this.preprocessResponseMultiModal(input);
|
|
1129
|
+
}
|
|
1130
|
+
async preprocessResponseMultiModal(inputItems) {
|
|
1131
|
+
const tasks = [];
|
|
1132
|
+
for (const item of inputItems){
|
|
1133
|
+
if (!item || "message" !== item.type && !("role" in item && "content" in item)) continue;
|
|
1134
|
+
const inputMessage = item;
|
|
1135
|
+
if (inputMessage.content && Array.isArray(inputMessage.content)) for (const contentItem of inputMessage.content){
|
|
1136
|
+
const fileUrl = this.getMultiModalFileUrl(contentItem);
|
|
1137
|
+
if (fileUrl) tasks.push(this.preprocessResponseFile(contentItem, fileUrl));
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
if (tasks.length > 0) await Promise.all(tasks);
|
|
1141
|
+
}
|
|
1142
|
+
getMultiModalFileUrl(contentItem) {
|
|
1143
|
+
if ("input_video" === contentItem.type) {
|
|
1144
|
+
const video = contentItem;
|
|
1145
|
+
if (video.video_url) return video.video_url;
|
|
1146
|
+
} else if ("input_image" === contentItem.type) {
|
|
1147
|
+
const image = contentItem;
|
|
1148
|
+
if (image.image_url) return image.image_url;
|
|
1149
|
+
} else if ("input_file" === contentItem.type) {
|
|
1150
|
+
const file = contentItem;
|
|
1151
|
+
if (file.file_url) return file.file_url;
|
|
1152
|
+
}
|
|
1153
|
+
return null;
|
|
1154
|
+
}
|
|
1155
|
+
parseFileUrl(rawUrl) {
|
|
1156
|
+
try {
|
|
1157
|
+
const parsed = new URL(rawUrl);
|
|
1158
|
+
if (parsed.protocol !== `${FILE_SCHEME}:`) return null;
|
|
1159
|
+
return parsed.pathname;
|
|
1160
|
+
} catch {
|
|
1161
|
+
return null;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
async preprocessResponseFile(contentItem, rawUrl) {
|
|
1165
|
+
const localPath = this.parseFileUrl(rawUrl);
|
|
1166
|
+
if (!localPath) return;
|
|
1167
|
+
const fileStream = external_fs_namespaceObject.createReadStream(localPath);
|
|
1168
|
+
try {
|
|
1169
|
+
let fileMeta = await this.uploadFile({
|
|
1170
|
+
file: fileStream,
|
|
1171
|
+
purpose: PurposeUserData
|
|
1172
|
+
});
|
|
1173
|
+
const deadline = Date.now() + MAX_WAIT_TIME_MS;
|
|
1174
|
+
while(fileMeta.status === FileStatusProcessing && Date.now() < deadline){
|
|
1175
|
+
await new Promise((resolve)=>setTimeout(resolve, POLL_INTERVAL_MS));
|
|
1176
|
+
fileMeta = await this.retrieveFile(fileMeta.id);
|
|
1177
|
+
}
|
|
1178
|
+
if ("input_video" === contentItem.type) {
|
|
1179
|
+
const video = contentItem;
|
|
1180
|
+
video.video_url = "";
|
|
1181
|
+
video.file_id = fileMeta.id;
|
|
1182
|
+
} else if ("input_image" === contentItem.type) {
|
|
1183
|
+
const image = contentItem;
|
|
1184
|
+
delete image.image_url;
|
|
1185
|
+
image.file_id = fileMeta.id;
|
|
1186
|
+
} else if ("input_file" === contentItem.type) {
|
|
1187
|
+
const file = contentItem;
|
|
1188
|
+
delete file.file_url;
|
|
1189
|
+
file.file_id = fileMeta.id;
|
|
1190
|
+
}
|
|
1191
|
+
} finally{
|
|
1192
|
+
fileStream.destroy();
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
async doRequest(method, url, resourceType, resourceId, opts) {
|
|
1196
|
+
return retry(this.retryPolicy(), async ()=>{
|
|
1197
|
+
const resp = await this.executeRequest(method, url, resourceType, resourceId, opts);
|
|
1198
|
+
return resp.data;
|
|
1199
|
+
}, (err)=>this.isRetryable(err), opts?.signal);
|
|
1200
|
+
}
|
|
1201
|
+
async doStreamRequest(method, url, resourceType, resourceId, opts) {
|
|
1202
|
+
const resp = await retry(this.retryPolicy(), async ()=>this.executeRequest(method, url, resourceType, resourceId, {
|
|
1203
|
+
...opts,
|
|
1204
|
+
stream: true
|
|
1205
|
+
}), (err)=>this.isRetryable(err), opts?.signal);
|
|
1206
|
+
const headers = {};
|
|
1207
|
+
if (resp.headers) {
|
|
1208
|
+
for (const [k, v] of Object.entries(resp.headers))if ("string" == typeof v) headers[k] = v;
|
|
1209
|
+
}
|
|
1210
|
+
return {
|
|
1211
|
+
stream: resp.data,
|
|
1212
|
+
headers
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
async createChatCompletion(request, opts) {
|
|
1216
|
+
if (request.stream) throw ErrChatCompletionStreamNotSupported;
|
|
1217
|
+
const resp = await this.doRequest("POST", this.fullURL(chatCompletionsSuffix), resourceTypeEndpoint, request.model, {
|
|
1218
|
+
...opts,
|
|
1219
|
+
body: request
|
|
1220
|
+
});
|
|
1221
|
+
return normalizeChatCompletionResponse(resp);
|
|
1222
|
+
}
|
|
1223
|
+
async createChatCompletionStream(request, opts) {
|
|
1224
|
+
let body = {
|
|
1225
|
+
...request,
|
|
1226
|
+
stream: true
|
|
1227
|
+
};
|
|
1228
|
+
let extraHeaders;
|
|
1229
|
+
const isEncrypted = opts?.customHeaders?.[ClientIsEncryptedHeader] === "true";
|
|
1230
|
+
const requestId = genRequestId();
|
|
1231
|
+
if (isEncrypted) {
|
|
1232
|
+
const authHeader = await this.getAuthHeader(resourceTypeEndpoint, request.model, opts?.projectName);
|
|
1233
|
+
const result = await this.encryptRequestBody(request.model, requestId, authHeader.Authorization, body);
|
|
1234
|
+
body = result.body;
|
|
1235
|
+
extraHeaders = result.extraHeaders;
|
|
1236
|
+
}
|
|
1237
|
+
const mergedOpts = {
|
|
1238
|
+
...opts,
|
|
1239
|
+
body,
|
|
1240
|
+
customHeaders: {
|
|
1241
|
+
...opts?.customHeaders,
|
|
1242
|
+
...extraHeaders
|
|
1243
|
+
}
|
|
1244
|
+
};
|
|
1245
|
+
const { stream, headers } = await this.doStreamRequest("POST", this.fullURL(chatCompletionsSuffix), resourceTypeEndpoint, request.model, mergedOpts);
|
|
1246
|
+
const keyNonce = this.keyNonceMap.get(requestId);
|
|
1247
|
+
const cleanup = keyNonce ? ()=>{
|
|
1248
|
+
this.keyNonceMap.delete(requestId);
|
|
1249
|
+
} : void 0;
|
|
1250
|
+
return new ChatCompletionStreamReader(stream, this.config.emptyMessagesLimit, headers, keyNonce, cleanup);
|
|
1251
|
+
}
|
|
1252
|
+
async createBotChatCompletion(request, opts) {
|
|
1253
|
+
if (request.stream) throw ErrChatCompletionStreamNotSupported;
|
|
1254
|
+
const model = request.bot_id || request.model;
|
|
1255
|
+
const resp = await this.doRequest("POST", this.fullURL(botChatCompletionsSuffix), "bot", model, {
|
|
1256
|
+
...opts,
|
|
1257
|
+
body: {
|
|
1258
|
+
...request,
|
|
1259
|
+
model
|
|
1260
|
+
}
|
|
1261
|
+
});
|
|
1262
|
+
return normalizeChatCompletionResponse(resp);
|
|
1263
|
+
}
|
|
1264
|
+
async createBotChatCompletionStream(request, opts) {
|
|
1265
|
+
const model = request.bot_id || request.model;
|
|
1266
|
+
const body = {
|
|
1267
|
+
...request,
|
|
1268
|
+
model,
|
|
1269
|
+
stream: true
|
|
1270
|
+
};
|
|
1271
|
+
const { stream, headers } = await this.doStreamRequest("POST", this.fullURL(botChatCompletionsSuffix), "bot", model, {
|
|
1272
|
+
...opts,
|
|
1273
|
+
body
|
|
1274
|
+
});
|
|
1275
|
+
return new BotChatCompletionStreamReader(stream, this.config.emptyMessagesLimit, headers);
|
|
1276
|
+
}
|
|
1277
|
+
async createEmbeddings(request, opts) {
|
|
1278
|
+
const resp = await this.doRequest("POST", this.fullURL(embeddingsSuffix), resourceTypeEndpoint, request.model, {
|
|
1279
|
+
...opts,
|
|
1280
|
+
body: request
|
|
1281
|
+
});
|
|
1282
|
+
return normalizeEmbeddingResponse(resp);
|
|
1283
|
+
}
|
|
1284
|
+
async createMultiModalEmbeddings(request, opts) {
|
|
1285
|
+
return this.doRequest("POST", this.fullURL(multimodalEmbeddingsSuffix), resourceTypeEndpoint, request.model, {
|
|
1286
|
+
...opts,
|
|
1287
|
+
body: request
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
async generateImages(request, opts) {
|
|
1291
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1292
|
+
return this.doRequest("POST", this.fullURL(generateImagesPath), resourceTypeEndpoint, request.model, {
|
|
1293
|
+
...opts,
|
|
1294
|
+
body: request
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
async generateImagesStream(request, opts) {
|
|
1298
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1299
|
+
const body = {
|
|
1300
|
+
...request,
|
|
1301
|
+
stream: true
|
|
1302
|
+
};
|
|
1303
|
+
const { stream, headers } = await this.doStreamRequest("POST", this.fullURL(generateImagesPath), resourceTypeEndpoint, request.model, {
|
|
1304
|
+
...opts,
|
|
1305
|
+
body
|
|
1306
|
+
});
|
|
1307
|
+
return new ImageGenerationStreamReader(stream, this.config.emptyMessagesLimit, headers);
|
|
1308
|
+
}
|
|
1309
|
+
async createTokenization(request, opts) {
|
|
1310
|
+
return this.doRequest("POST", this.fullURL(tokenizationSuffix), resourceTypeEndpoint, request.model, {
|
|
1311
|
+
...opts,
|
|
1312
|
+
body: request
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
async createContext(request, opts) {
|
|
1316
|
+
return this.doRequest("POST", this.fullURL(contextCreateSuffix), resourceTypeEndpoint, request.model, {
|
|
1317
|
+
...opts,
|
|
1318
|
+
body: request
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
async createContextChatCompletion(request, opts) {
|
|
1322
|
+
if (request.stream) throw ErrChatCompletionStreamNotSupported;
|
|
1323
|
+
const resp = await this.doRequest("POST", this.fullURL(contextChatSuffix), resourceTypeEndpoint, request.model, {
|
|
1324
|
+
...opts,
|
|
1325
|
+
body: request
|
|
1326
|
+
});
|
|
1327
|
+
return normalizeChatCompletionResponse(resp);
|
|
1328
|
+
}
|
|
1329
|
+
async createContextChatCompletionStream(request, opts) {
|
|
1330
|
+
const body = {
|
|
1331
|
+
...request,
|
|
1332
|
+
stream: true
|
|
1333
|
+
};
|
|
1334
|
+
const { stream, headers } = await this.doStreamRequest("POST", this.fullURL(contextChatSuffix), resourceTypeEndpoint, request.model, {
|
|
1335
|
+
...opts,
|
|
1336
|
+
body
|
|
1337
|
+
});
|
|
1338
|
+
return new ChatCompletionStreamReader(stream, this.config.emptyMessagesLimit, headers);
|
|
1339
|
+
}
|
|
1340
|
+
async createContentGenerationTask(request, opts) {
|
|
1341
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1342
|
+
return this.doRequest("POST", this.fullURL(contentGenerationTaskPath), resourceTypeEndpoint, request.model, {
|
|
1343
|
+
...opts,
|
|
1344
|
+
body: request
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
async getContentGenerationTask(taskId, opts) {
|
|
1348
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1349
|
+
return this.doRequest("GET", `${this.fullURL(contentGenerationTaskPath)}/${taskId}`, resourceTypeEndpoint, "", opts);
|
|
1350
|
+
}
|
|
1351
|
+
async deleteContentGenerationTask(taskId, opts) {
|
|
1352
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1353
|
+
await this.doRequest("DELETE", `${this.fullURL(contentGenerationTaskPath)}/${taskId}`, resourceTypeEndpoint, "", opts);
|
|
1354
|
+
}
|
|
1355
|
+
async listContentGenerationTasks(request, opts) {
|
|
1356
|
+
if (!this.isAPIKeyAuth()) throw ErrAKSKNotSupported;
|
|
1357
|
+
const query = {};
|
|
1358
|
+
if (null != request.page_num) query.page_num = String(request.page_num);
|
|
1359
|
+
if (null != request.page_size) query.page_size = String(request.page_size);
|
|
1360
|
+
if (request.filter) {
|
|
1361
|
+
if (request.filter.status) query["filter.status"] = request.filter.status;
|
|
1362
|
+
if (request.filter.model) query["filter.model"] = request.filter.model;
|
|
1363
|
+
if (request.filter.service_tier) query["filter.service_tier"] = request.filter.service_tier;
|
|
1364
|
+
if (request.filter.task_ids?.length) query["filter.task_ids"] = request.filter.task_ids;
|
|
1365
|
+
}
|
|
1366
|
+
return this.doRequest("GET", this.fullURL(contentGenerationTaskPath), resourceTypeEndpoint, "", {
|
|
1367
|
+
...opts,
|
|
1368
|
+
query: query
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
async createResponses(body, opts) {
|
|
1372
|
+
await this.preprocessResponseInput(body.input);
|
|
1373
|
+
return this.doRequest("POST", this.fullURL("/responses"), resourceTypeEndpoint, body.model, {
|
|
1374
|
+
...opts,
|
|
1375
|
+
body
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
async createResponsesStream(body, opts) {
|
|
1379
|
+
await this.preprocessResponseInput(body.input);
|
|
1380
|
+
const streamBody = {
|
|
1381
|
+
...body,
|
|
1382
|
+
stream: true
|
|
1383
|
+
};
|
|
1384
|
+
const { stream, headers } = await this.doStreamRequest("POST", this.fullURL("/responses"), resourceTypeEndpoint, body.model, {
|
|
1385
|
+
...opts,
|
|
1386
|
+
body: streamBody
|
|
1387
|
+
});
|
|
1388
|
+
return new ResponsesStreamReader(stream, this.config.emptyMessagesLimit, headers);
|
|
1389
|
+
}
|
|
1390
|
+
async getResponses(responseId, query, opts) {
|
|
1391
|
+
if (!responseId) throw new Error("missing required response_id parameter");
|
|
1392
|
+
return this.doRequest("GET", this.fullURL(`/responses/${responseId}`), "", "", {
|
|
1393
|
+
...opts,
|
|
1394
|
+
query: query
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
async deleteResponse(responseId, opts) {
|
|
1398
|
+
if (!responseId) throw new Error("missing required response_id parameter");
|
|
1399
|
+
await this.doRequest("DELETE", this.fullURL(`/responses/${responseId}`), "", "", opts);
|
|
1400
|
+
}
|
|
1401
|
+
async listResponseInputItems(responseId, query, opts) {
|
|
1402
|
+
if (!responseId) throw new Error("missing required response_id parameter");
|
|
1403
|
+
return this.doRequest("GET", this.fullURL(`/responses/${responseId}/input_items`), "", "", {
|
|
1404
|
+
...opts,
|
|
1405
|
+
query: query
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
async uploadFile(request, opts) {
|
|
1409
|
+
const formData = new FormData();
|
|
1410
|
+
formData.append("file", request.file);
|
|
1411
|
+
formData.append("purpose", request.purpose);
|
|
1412
|
+
if (request.preprocess_configs) formData.append("preprocess_configs", JSON.stringify(request.preprocess_configs));
|
|
1413
|
+
if (null != request.expire_at) formData.append("expire_at", String(request.expire_at));
|
|
1414
|
+
const authHeader = await this.getAuthHeader("", "");
|
|
1415
|
+
const requestId = genRequestId();
|
|
1416
|
+
const resp = await this.httpClient.request({
|
|
1417
|
+
method: "POST",
|
|
1418
|
+
url: this.fullURL(filePrefix),
|
|
1419
|
+
headers: {
|
|
1420
|
+
[ClientRequestHeader]: requestId,
|
|
1421
|
+
...authHeader,
|
|
1422
|
+
...opts?.customHeaders
|
|
1423
|
+
},
|
|
1424
|
+
data: formData,
|
|
1425
|
+
signal: opts?.signal,
|
|
1426
|
+
validateStatus: ()=>true
|
|
1427
|
+
});
|
|
1428
|
+
if (resp.status < 200 || resp.status >= 400) this.handleErrorResponse(resp, requestId);
|
|
1429
|
+
return resp.data;
|
|
1430
|
+
}
|
|
1431
|
+
async retrieveFile(fileId, opts) {
|
|
1432
|
+
if (!fileId) throw new Error("missing required file_id parameter");
|
|
1433
|
+
return this.doRequest("GET", this.fullURL(`${filePrefix}/${fileId}`), "", "", opts);
|
|
1434
|
+
}
|
|
1435
|
+
async listFiles(request, opts) {
|
|
1436
|
+
const query = {};
|
|
1437
|
+
if (request?.purpose) query.purpose = request.purpose;
|
|
1438
|
+
if (request?.after) query.after = request.after;
|
|
1439
|
+
if (request?.limit != null) query.limit = String(request.limit);
|
|
1440
|
+
if (request?.order) query.order = request.order;
|
|
1441
|
+
return this.doRequest("GET", this.fullURL(filePrefix), "", "", {
|
|
1442
|
+
...opts,
|
|
1443
|
+
query
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
async deleteFile(fileId, opts) {
|
|
1447
|
+
if (!fileId) throw new Error("missing required file_id parameter");
|
|
1448
|
+
return this.doRequest("DELETE", this.fullURL(`${filePrefix}/${fileId}`), "", "", opts);
|
|
1449
|
+
}
|
|
1450
|
+
async createBatchChatCompletion(request, opts) {
|
|
1451
|
+
if (request.stream) throw ErrChatCompletionStreamNotSupported;
|
|
1452
|
+
const breaker = this.modelBreakerProvider.getOrCreate(request.model);
|
|
1453
|
+
await breaker.wait();
|
|
1454
|
+
const resp = await this.doRequest("POST", this.fullURL(batchChatCompletionsSuffix), resourceTypeEndpoint, request.model, {
|
|
1455
|
+
...opts,
|
|
1456
|
+
body: request
|
|
1457
|
+
});
|
|
1458
|
+
return normalizeChatCompletionResponse(resp);
|
|
1459
|
+
}
|
|
1460
|
+
async createBatchEmbeddings(request, opts) {
|
|
1461
|
+
const breaker = this.modelBreakerProvider.getOrCreate(request.model);
|
|
1462
|
+
await breaker.wait();
|
|
1463
|
+
const resp = await this.doRequest("POST", this.fullURL(batchEmbeddingsSuffix), resourceTypeEndpoint, request.model, {
|
|
1464
|
+
...opts,
|
|
1465
|
+
body: request
|
|
1466
|
+
});
|
|
1467
|
+
return normalizeEmbeddingResponse(resp);
|
|
1468
|
+
}
|
|
1469
|
+
async createBatchMultiModalEmbeddings(request, opts) {
|
|
1470
|
+
const breaker = this.modelBreakerProvider.getOrCreate(request.model);
|
|
1471
|
+
await breaker.wait();
|
|
1472
|
+
return this.doRequest("POST", this.fullURL(batchMultiModalEmbeddingsSuffix), resourceTypeEndpoint, request.model, {
|
|
1473
|
+
...opts,
|
|
1474
|
+
body: request
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1477
|
+
constructor(config){
|
|
1478
|
+
client_define_property(this, "config", void 0);
|
|
1479
|
+
client_define_property(this, "httpClient", void 0);
|
|
1480
|
+
client_define_property(this, "batchHttpClient", void 0);
|
|
1481
|
+
client_define_property(this, "resourceStsTokens", new Map());
|
|
1482
|
+
client_define_property(this, "refreshPromise", null);
|
|
1483
|
+
client_define_property(this, "advisoryRefreshTimeout", 1800);
|
|
1484
|
+
client_define_property(this, "mandatoryRefreshTimeout", 600);
|
|
1485
|
+
client_define_property(this, "modelBreakerProvider", new ModelBreakerProvider());
|
|
1486
|
+
client_define_property(this, "e2eeManager", new Map());
|
|
1487
|
+
client_define_property(this, "keyNonceMap", new Map());
|
|
1488
|
+
client_define_property(this, "e2eeFlightMap", new Map());
|
|
1489
|
+
this.config = resolveConfig(config);
|
|
1490
|
+
this.httpClient = this.config.httpClient ?? external_axios_default().create({
|
|
1491
|
+
timeout: this.config.timeout,
|
|
1492
|
+
httpsAgent: new external_https_namespaceObject.Agent({
|
|
1493
|
+
keepAlive: true
|
|
1494
|
+
}),
|
|
1495
|
+
paramsSerializer: (params)=>{
|
|
1496
|
+
const parts = [];
|
|
1497
|
+
for (const [key, value] of Object.entries(params))if (Array.isArray(value)) for (const v of value)parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
|
|
1498
|
+
else if (null != value) parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
1499
|
+
return parts.join("&");
|
|
1500
|
+
}
|
|
1501
|
+
});
|
|
1502
|
+
this.batchHttpClient = external_axios_default().create({
|
|
1503
|
+
timeout: this.config.timeout,
|
|
1504
|
+
httpsAgent: new external_https_namespaceObject.Agent({
|
|
1505
|
+
keepAlive: true
|
|
1506
|
+
})
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
const ChatMessageRoleAssistant = "assistant";
|
|
1511
|
+
const ToolChoiceRequired = "required";
|
|
1512
|
+
function decodeBase64Embedding(b64) {
|
|
1513
|
+
const buf = Buffer.from(b64, "base64");
|
|
1514
|
+
const floats = [];
|
|
1515
|
+
for(let i = 0; i < buf.length; i += 4)floats.push(buf.readFloatLE(i));
|
|
1516
|
+
return floats;
|
|
1517
|
+
}
|
|
1518
|
+
function base64ToEmbeddingResponse(resp) {
|
|
1519
|
+
return {
|
|
1520
|
+
id: "",
|
|
1521
|
+
created: 0,
|
|
1522
|
+
object: resp.object,
|
|
1523
|
+
model: resp.model,
|
|
1524
|
+
usage: resp.usage,
|
|
1525
|
+
data: resp.data.map((item)=>({
|
|
1526
|
+
object: item.object,
|
|
1527
|
+
embedding: decodeBase64Embedding(item.embedding),
|
|
1528
|
+
index: item.index
|
|
1529
|
+
}))
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
const GenerateImagesResponseFormatBase64 = "b64_json";
|
|
1533
|
+
const GenerateImagesSizeAdaptive = "adaptive";
|
|
1534
|
+
const ImageGenerationStreamEventPartialSucceeded = "image_generation.partial_succeeded";
|
|
1535
|
+
const ImageGenerationStreamEventPartialFailed = "image_generation.partial_failed";
|
|
1536
|
+
const ImageGenerationStreamEventCompleted = "image_generation.completed";
|
|
1537
|
+
const StatusSucceeded = "succeeded";
|
|
1538
|
+
const StatusCancelled = "cancelled";
|
|
1539
|
+
const StatusRunning = "running";
|
|
1540
|
+
const MessageRole_developer = "developer";
|
|
1541
|
+
const MessageRole_assistant = "assistant";
|
|
1542
|
+
const ContentItemType_input_text = "input_text";
|
|
1543
|
+
const ContentItemType_input_image = "input_image";
|
|
1544
|
+
const ContentItemType_input_video = "input_video";
|
|
1545
|
+
const ContentItemType_input_audio = "input_audio";
|
|
1546
|
+
const ContentItemType_input_file = "input_file";
|
|
1547
|
+
const ContentItemType_output_text = "output_text";
|
|
1548
|
+
const ContentItemType_summary_text = "summary_text";
|
|
1549
|
+
function getEventType(event) {
|
|
1550
|
+
if (!event) return "";
|
|
1551
|
+
return event.type ?? "";
|
|
1552
|
+
}
|
|
1553
|
+
function isDelta(event) {
|
|
1554
|
+
const t = event.type;
|
|
1555
|
+
return "response.output_text.delta" === t || "response.reasoning_summary_text.delta" === t || "response.function_call_arguments.delta" === t || "response.transcription_text.delta" === t || "response.output_text.annotation.added" === t || "response.web_search_call.in_progress" === t || "response.web_search_call.searching" === t || "response.web_search_call.completed" === t || "response.image_process_call.in_progress" === t || "response.image_process_call.completed" === t || "response.mcp_list_tools.completed" === t || "response.mcp_call.in_progress" === t || "response.mcp_call.arguments_delta" === t || "response.mcp_call.arguments_done" === t || "response.mcp_call.completed" === t || "response.mcp_call.failed" === t || "response.knowledge_search_call.in_progress" === t || "response.knowledge_search_call.searching" === t || "response.knowledge_search_call.completed" === t || "response.knowledge_search_call.failed" === t;
|
|
1556
|
+
}
|
|
1557
|
+
function isDeltaDone(event) {
|
|
1558
|
+
const t = event.type;
|
|
1559
|
+
return "response.output_text.done" === t || "response.reasoning_summary_text.done" === t || "response.function_call_arguments.done" === t || "response.transcription_text.done" === t;
|
|
1560
|
+
}
|
|
1561
|
+
function isResponseDone(event) {
|
|
1562
|
+
const t = event.type;
|
|
1563
|
+
return "response.completed" === t || "response.failed" === t || "response.incomplete" === t;
|
|
1564
|
+
}
|
|
1565
|
+
exports.ArkAPIError = __webpack_exports__.ArkAPIError;
|
|
1566
|
+
exports.ArkRequestError = __webpack_exports__.ArkRequestError;
|
|
1567
|
+
exports.ArkRuntimeClient = __webpack_exports__.ArkRuntimeClient;
|
|
1568
|
+
exports.BotChatCompletionStreamReader = __webpack_exports__.BotChatCompletionStreamReader;
|
|
1569
|
+
exports.Breaker = __webpack_exports__.Breaker;
|
|
1570
|
+
exports.ChatCompletionStreamReader = __webpack_exports__.ChatCompletionStreamReader;
|
|
1571
|
+
exports.ChatMessageRoleAssistant = __webpack_exports__.ChatMessageRoleAssistant;
|
|
1572
|
+
exports.ChatMessageRoleSystem = __webpack_exports__.ChatMessageRoleSystem;
|
|
1573
|
+
exports.ChatMessageRoleTool = __webpack_exports__.ChatMessageRoleTool;
|
|
1574
|
+
exports.ChatMessageRoleUser = __webpack_exports__.ChatMessageRoleUser;
|
|
1575
|
+
exports.ClientEncryptInfoHeader = __webpack_exports__.ClientEncryptInfoHeader;
|
|
1576
|
+
exports.ClientIsEncryptedHeader = __webpack_exports__.ClientIsEncryptedHeader;
|
|
1577
|
+
exports.ClientRequestHeader = __webpack_exports__.ClientRequestHeader;
|
|
1578
|
+
exports.ClientSessionTokenHeader = __webpack_exports__.ClientSessionTokenHeader;
|
|
1579
|
+
exports.DefaultAdvisoryRefreshTimeout = __webpack_exports__.DefaultAdvisoryRefreshTimeout;
|
|
1580
|
+
exports.DefaultMandatoryRefreshTimeout = __webpack_exports__.DefaultMandatoryRefreshTimeout;
|
|
1581
|
+
exports.DefaultStsTimeout = __webpack_exports__.DefaultStsTimeout;
|
|
1582
|
+
exports.E2eeClient = __webpack_exports__.E2eeClient;
|
|
1583
|
+
exports.ErrAKSKNotSupported = __webpack_exports__.ErrAKSKNotSupported;
|
|
1584
|
+
exports.ErrBodyWithoutBot = __webpack_exports__.ErrBodyWithoutBot;
|
|
1585
|
+
exports.ErrBodyWithoutEndpoint = __webpack_exports__.ErrBodyWithoutEndpoint;
|
|
1586
|
+
exports.ErrBodyWithoutProjectName = __webpack_exports__.ErrBodyWithoutProjectName;
|
|
1587
|
+
exports.ErrChatCompletionStreamNotSupported = __webpack_exports__.ErrChatCompletionStreamNotSupported;
|
|
1588
|
+
exports.ErrContentFieldsMisused = __webpack_exports__.ErrContentFieldsMisused;
|
|
1589
|
+
exports.ErrTooManyEmptyStreamMessages = __webpack_exports__.ErrTooManyEmptyStreamMessages;
|
|
1590
|
+
exports.ErrorRetryBaseDelayMs = __webpack_exports__.ErrorRetryBaseDelayMs;
|
|
1591
|
+
exports.ErrorRetryMaxDelayMs = __webpack_exports__.ErrorRetryMaxDelayMs;
|
|
1592
|
+
exports.EventStreamDecoder = __webpack_exports__.EventStreamDecoder;
|
|
1593
|
+
exports.FileStatusActive = __webpack_exports__.FileStatusActive;
|
|
1594
|
+
exports.FileStatusFailed = __webpack_exports__.FileStatusFailed;
|
|
1595
|
+
exports.FileStatusProcessing = __webpack_exports__.FileStatusProcessing;
|
|
1596
|
+
exports.GenerateImagesResponseFormatBase64 = __webpack_exports__.GenerateImagesResponseFormatBase64;
|
|
1597
|
+
exports.GenerateImagesResponseFormatURL = __webpack_exports__.GenerateImagesResponseFormatURL;
|
|
1598
|
+
exports.GenerateImagesSizeAdaptive = __webpack_exports__.GenerateImagesSizeAdaptive;
|
|
1599
|
+
exports.ImageGenerationStreamEventCompleted = __webpack_exports__.ImageGenerationStreamEventCompleted;
|
|
1600
|
+
exports.ImageGenerationStreamEventPartialFailed = __webpack_exports__.ImageGenerationStreamEventPartialFailed;
|
|
1601
|
+
exports.ImageGenerationStreamEventPartialSucceeded = __webpack_exports__.ImageGenerationStreamEventPartialSucceeded;
|
|
1602
|
+
exports.ImageGenerationStreamReader = __webpack_exports__.ImageGenerationStreamReader;
|
|
1603
|
+
exports.InitialRetryDelay = __webpack_exports__.InitialRetryDelay;
|
|
1604
|
+
exports.KeyAgreementClient = __webpack_exports__.KeyAgreementClient;
|
|
1605
|
+
exports.MaxRetryDelay = __webpack_exports__.MaxRetryDelay;
|
|
1606
|
+
exports.ModelBreakerProvider = __webpack_exports__.ModelBreakerProvider;
|
|
1607
|
+
exports.ObjectTypeFile = __webpack_exports__.ObjectTypeFile;
|
|
1608
|
+
exports.ObjectTypeList = __webpack_exports__.ObjectTypeList;
|
|
1609
|
+
exports.OrderAsc = __webpack_exports__.OrderAsc;
|
|
1610
|
+
exports.OrderDesc = __webpack_exports__.OrderDesc;
|
|
1611
|
+
exports.PurposeUserData = __webpack_exports__.PurposeUserData;
|
|
1612
|
+
exports.ResponsesStreamReader = __webpack_exports__.ResponsesStreamReader;
|
|
1613
|
+
exports.RetryAfterHeader = __webpack_exports__.RetryAfterHeader;
|
|
1614
|
+
exports.StatusCancelled = __webpack_exports__.StatusCancelled;
|
|
1615
|
+
exports.StatusFailed = __webpack_exports__.StatusFailed;
|
|
1616
|
+
exports.StatusQueued = __webpack_exports__.StatusQueued;
|
|
1617
|
+
exports.StatusRunning = __webpack_exports__.StatusRunning;
|
|
1618
|
+
exports.StatusSucceeded = __webpack_exports__.StatusSucceeded;
|
|
1619
|
+
exports.ToolChoiceAuto = __webpack_exports__.ToolChoiceAuto;
|
|
1620
|
+
exports.ToolChoiceNone = __webpack_exports__.ToolChoiceNone;
|
|
1621
|
+
exports.ToolChoiceRequired = __webpack_exports__.ToolChoiceRequired;
|
|
1622
|
+
exports.aesGcmDecrypt = __webpack_exports__.aesGcmDecrypt;
|
|
1623
|
+
exports.aesGcmDecryptBase64 = __webpack_exports__.aesGcmDecryptBase64;
|
|
1624
|
+
exports.aesGcmEncrypt = __webpack_exports__.aesGcmEncrypt;
|
|
1625
|
+
exports.aesGcmEncryptBase64 = __webpack_exports__.aesGcmEncryptBase64;
|
|
1626
|
+
exports.base64ToEmbeddingResponse = __webpack_exports__.base64ToEmbeddingResponse;
|
|
1627
|
+
exports.checkIsModeAICC = __webpack_exports__.checkIsModeAICC;
|
|
1628
|
+
exports.decodeBase64Embedding = __webpack_exports__.decodeBase64Embedding;
|
|
1629
|
+
exports.decryptChatResponse = __webpack_exports__.decryptChatResponse;
|
|
1630
|
+
exports.decryptChatStreamResponse = __webpack_exports__.decryptChatStreamResponse;
|
|
1631
|
+
exports.deepCopyRequest = __webpack_exports__.deepCopyRequest;
|
|
1632
|
+
exports.encryptChatRequest = __webpack_exports__.encryptChatRequest;
|
|
1633
|
+
exports.genRequestId = __webpack_exports__.genRequestId;
|
|
1634
|
+
exports.getCertInfo = __webpack_exports__.getCertInfo;
|
|
1635
|
+
exports.loadLocalCertificate = __webpack_exports__.loadLocalCertificate;
|
|
1636
|
+
exports.responses = __webpack_exports__.responses;
|
|
1637
|
+
exports.retry = __webpack_exports__.retry;
|
|
1638
|
+
exports.saveToLocalCertificate = __webpack_exports__.saveToLocalCertificate;
|
|
1639
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
1640
|
+
"ArkAPIError",
|
|
1641
|
+
"ArkRequestError",
|
|
1642
|
+
"ArkRuntimeClient",
|
|
1643
|
+
"BotChatCompletionStreamReader",
|
|
1644
|
+
"Breaker",
|
|
1645
|
+
"ChatCompletionStreamReader",
|
|
1646
|
+
"ChatMessageRoleAssistant",
|
|
1647
|
+
"ChatMessageRoleSystem",
|
|
1648
|
+
"ChatMessageRoleTool",
|
|
1649
|
+
"ChatMessageRoleUser",
|
|
1650
|
+
"ClientEncryptInfoHeader",
|
|
1651
|
+
"ClientIsEncryptedHeader",
|
|
1652
|
+
"ClientRequestHeader",
|
|
1653
|
+
"ClientSessionTokenHeader",
|
|
1654
|
+
"DefaultAdvisoryRefreshTimeout",
|
|
1655
|
+
"DefaultMandatoryRefreshTimeout",
|
|
1656
|
+
"DefaultStsTimeout",
|
|
1657
|
+
"E2eeClient",
|
|
1658
|
+
"ErrAKSKNotSupported",
|
|
1659
|
+
"ErrBodyWithoutBot",
|
|
1660
|
+
"ErrBodyWithoutEndpoint",
|
|
1661
|
+
"ErrBodyWithoutProjectName",
|
|
1662
|
+
"ErrChatCompletionStreamNotSupported",
|
|
1663
|
+
"ErrContentFieldsMisused",
|
|
1664
|
+
"ErrTooManyEmptyStreamMessages",
|
|
1665
|
+
"ErrorRetryBaseDelayMs",
|
|
1666
|
+
"ErrorRetryMaxDelayMs",
|
|
1667
|
+
"EventStreamDecoder",
|
|
1668
|
+
"FileStatusActive",
|
|
1669
|
+
"FileStatusFailed",
|
|
1670
|
+
"FileStatusProcessing",
|
|
1671
|
+
"GenerateImagesResponseFormatBase64",
|
|
1672
|
+
"GenerateImagesResponseFormatURL",
|
|
1673
|
+
"GenerateImagesSizeAdaptive",
|
|
1674
|
+
"ImageGenerationStreamEventCompleted",
|
|
1675
|
+
"ImageGenerationStreamEventPartialFailed",
|
|
1676
|
+
"ImageGenerationStreamEventPartialSucceeded",
|
|
1677
|
+
"ImageGenerationStreamReader",
|
|
1678
|
+
"InitialRetryDelay",
|
|
1679
|
+
"KeyAgreementClient",
|
|
1680
|
+
"MaxRetryDelay",
|
|
1681
|
+
"ModelBreakerProvider",
|
|
1682
|
+
"ObjectTypeFile",
|
|
1683
|
+
"ObjectTypeList",
|
|
1684
|
+
"OrderAsc",
|
|
1685
|
+
"OrderDesc",
|
|
1686
|
+
"PurposeUserData",
|
|
1687
|
+
"ResponsesStreamReader",
|
|
1688
|
+
"RetryAfterHeader",
|
|
1689
|
+
"StatusCancelled",
|
|
1690
|
+
"StatusFailed",
|
|
1691
|
+
"StatusQueued",
|
|
1692
|
+
"StatusRunning",
|
|
1693
|
+
"StatusSucceeded",
|
|
1694
|
+
"ToolChoiceAuto",
|
|
1695
|
+
"ToolChoiceNone",
|
|
1696
|
+
"ToolChoiceRequired",
|
|
1697
|
+
"aesGcmDecrypt",
|
|
1698
|
+
"aesGcmDecryptBase64",
|
|
1699
|
+
"aesGcmEncrypt",
|
|
1700
|
+
"aesGcmEncryptBase64",
|
|
1701
|
+
"base64ToEmbeddingResponse",
|
|
1702
|
+
"checkIsModeAICC",
|
|
1703
|
+
"decodeBase64Embedding",
|
|
1704
|
+
"decryptChatResponse",
|
|
1705
|
+
"decryptChatStreamResponse",
|
|
1706
|
+
"deepCopyRequest",
|
|
1707
|
+
"encryptChatRequest",
|
|
1708
|
+
"genRequestId",
|
|
1709
|
+
"getCertInfo",
|
|
1710
|
+
"loadLocalCertificate",
|
|
1711
|
+
"responses",
|
|
1712
|
+
"retry",
|
|
1713
|
+
"saveToLocalCertificate"
|
|
1714
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
1715
|
+
Object.defineProperty(exports, '__esModule', {
|
|
1716
|
+
value: true
|
|
1717
|
+
});
|