yichong-canva2 2.0.0 → 2.2.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/README.md +124 -24
- package/dist/api/error.js +6 -0
- package/dist/api/predictions.js +25 -6
- package/dist/api/reporting.js +4 -2
- package/dist/api/request.js +1 -0
- package/dist/api/uploads.js +121 -11
- package/dist/api/usage.js +3 -4
- package/dist/login/account.js +69 -0
- package/dist/login/context.js +1 -0
- package/dist/login/login/apis/index.js +17 -13
- package/dist/login/login/apis/request.js +33 -4
- package/dist/login/login/login.js +111 -103
- package/dist/login/privider.js +191 -125
- package/dist/types/api/chat.d.ts +6 -1
- package/dist/types/api/content.d.ts +5 -1
- package/dist/types/api/error.d.ts +14 -2
- package/dist/types/api/index.d.ts +6 -1
- package/dist/types/api/moderation.d.ts +5 -1
- package/dist/types/api/predictions.d.ts +5 -1
- package/dist/types/api/reporting.d.ts +7 -0
- package/dist/types/api/reporting_payload.d.ts +6 -1
- package/dist/types/api/request.d.ts +40 -2
- package/dist/types/api/transport.d.ts +7 -1
- package/dist/types/api/types.d.ts +231 -22
- package/dist/types/api/uploads.d.ts +7 -0
- package/dist/types/api/usage.d.ts +7 -1
- package/dist/types/index.d.ts +5 -0
- package/dist/types/login/account.d.ts +36 -0
- package/dist/types/login/context.d.ts +36 -35
- package/dist/types/login/index.d.ts +1 -0
- package/dist/types/login/interface.d.ts +35 -2
- package/dist/types/login/login/apis/index.d.ts +35 -17
- package/dist/types/login/login/apis/request.d.ts +10 -6
- package/dist/types/login/login/interface.d.ts +10 -1
- package/dist/types/login/login/login.d.ts +43 -21
- package/dist/types/login/privider.d.ts +34 -21
- package/dist/types/login/use_canva.d.ts +5 -2
- package/dist/types/login/version.d.ts +6 -0
- package/package.json +1 -1
package/dist/login/privider.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useState, useRef, useEffect } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { getUserProfile, takeSubscribeBaseline, handleLogin } from "./login/login.js";
|
|
4
4
|
import { YiChongCanvaContext } from "./context.js";
|
|
5
5
|
import _ from "lodash";
|
|
6
6
|
import { getAppVersion } from "./version.js";
|
|
@@ -11,6 +11,9 @@ import { createChatApi } from "../api/chat.js";
|
|
|
11
11
|
import { createUsageApi } from "../api/usage.js";
|
|
12
12
|
import { createUploadApi } from "../api/uploads.js";
|
|
13
13
|
import { createErrorReportingApi } from "../api/reporting.js";
|
|
14
|
+
import { ApiError } from "../api/error.js";
|
|
15
|
+
import { withRequest, throwIfAborted } from "../api/transport.js";
|
|
16
|
+
import { createAccountReporter, AccountRequestError, retryableAccountError, accountDelay } from "./account.js";
|
|
14
17
|
import { language, addContextQuery } from "../api/request.js";
|
|
15
18
|
function reduceFreeTimes(baseUrl, passport, cnt, context) {
|
|
16
19
|
const header = {
|
|
@@ -76,6 +79,11 @@ const YiChongCanvaProvider = ({
|
|
|
76
79
|
const stopCurrentPolling = useRef(null);
|
|
77
80
|
const sessionStopRef = useRef(null);
|
|
78
81
|
const [loginStatus, setLoginStatus] = useState("idle");
|
|
82
|
+
const [accountError, setAccountError] = useState(null);
|
|
83
|
+
const accountRunRef = useRef(null);
|
|
84
|
+
const loginPendingRef = useRef(false);
|
|
85
|
+
const mountedRef = useRef(true);
|
|
86
|
+
const identityScopeRef = useRef(`${appId}|${productName}`);
|
|
79
87
|
const userStatusRef = useRef({});
|
|
80
88
|
const apiConfigRef = useRef({ apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale });
|
|
81
89
|
apiConfigRef.current = { apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale };
|
|
@@ -151,115 +159,146 @@ const YiChongCanvaProvider = ({
|
|
|
151
159
|
var _a;
|
|
152
160
|
return ((_a = userStatus.profile) == null ? void 0 : _a.usage_limits.free_limit) === -1;
|
|
153
161
|
};
|
|
154
|
-
const
|
|
162
|
+
const [reportAccountFailure] = useState(() => createAccountReporter(errorReportingApi.reportError));
|
|
163
|
+
const beginAccountRun = (operation) => {
|
|
164
|
+
var _a, _b;
|
|
165
|
+
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
166
|
+
(_b = accountRunRef.current) == null ? void 0 : _b.controller.abort();
|
|
167
|
+
const run = {
|
|
168
|
+
operation,
|
|
169
|
+
controller: new AbortController(),
|
|
170
|
+
startedAt: Date.now(),
|
|
171
|
+
attempts: 0,
|
|
172
|
+
stage: operation === "login" ? "canva_token" : "status",
|
|
173
|
+
appKey: `${appId}|${productName}`
|
|
174
|
+
};
|
|
175
|
+
accountRunRef.current = run;
|
|
176
|
+
setAccountError(null);
|
|
177
|
+
return run;
|
|
178
|
+
};
|
|
179
|
+
const isCurrentRun = (run) => mountedRef.current && accountRunRef.current === run && !run.controller.signal.aborted && run.appKey === `${apiConfigRef.current.appId}|${apiConfigRef.current.productName}`;
|
|
180
|
+
const requestOptions = (run) => ({
|
|
181
|
+
signal: run.controller.signal,
|
|
182
|
+
onStep: (stage, api) => {
|
|
183
|
+
run.stage = stage;
|
|
184
|
+
run.api = api;
|
|
185
|
+
run.attempts++;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
const failAccountRun = (run, error) => {
|
|
189
|
+
if (!isCurrentRun(run) || error instanceof ApiError && error.code === "aborted") return;
|
|
190
|
+
const failure = {
|
|
191
|
+
operation: run.operation,
|
|
192
|
+
stage: error instanceof AccountRequestError ? error.stage : run.stage,
|
|
193
|
+
code: error instanceof ApiError ? error.code : "account_error",
|
|
194
|
+
status: error instanceof ApiError ? error.status : void 0,
|
|
195
|
+
// 使用 SDK 生成的诊断文本;未知异常不直接发送任意异常对象/原始后端正文。
|
|
196
|
+
message: error instanceof ApiError ? error.message : "Account operation failed.",
|
|
197
|
+
attempts: run.attempts,
|
|
198
|
+
elapsedMs: Date.now() - run.startedAt,
|
|
199
|
+
api: error instanceof AccountRequestError ? error.api : run.api
|
|
200
|
+
};
|
|
201
|
+
setAccountError(failure);
|
|
202
|
+
reportAccountFailure(failure, run.appKey);
|
|
203
|
+
};
|
|
204
|
+
const login = (extraRetries = retry) => {
|
|
205
|
+
const run = beginAccountRun("login");
|
|
206
|
+
loginPendingRef.current = true;
|
|
155
207
|
setLoginStatus("loading");
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
setLoginStatus("success");
|
|
163
|
-
updateUserStatus(userStatus2);
|
|
164
|
-
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(userStatus2);
|
|
165
|
-
window.userId = userStatus2.userId || "";
|
|
166
|
-
window.pollUserSubscribe = pollUserStatus.current = (options) => {
|
|
167
|
-
var _a;
|
|
168
|
-
const {
|
|
169
|
-
fastIntervalMs = 5e3,
|
|
170
|
-
fastPhaseMs = 3e4,
|
|
171
|
-
slowIntervalMs = 15e3,
|
|
172
|
-
sessionTtlMs = 3e5
|
|
173
|
-
} = options || {};
|
|
174
|
-
setHasClickedUpgrade(true);
|
|
175
|
-
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
176
|
-
return new Promise((resolve) => {
|
|
177
|
-
const userId = (window == null ? void 0 : window.userId) || "";
|
|
178
|
-
const baseline = takeSubscribeBaseline(userStatusRef.current);
|
|
179
|
-
const sessionStart = Date.now();
|
|
180
|
-
console.log("checkout session start", baseline);
|
|
181
|
-
let stopInner = null;
|
|
182
|
-
let settled = false;
|
|
183
|
-
const finish = (u) => {
|
|
184
|
-
if (settled) return;
|
|
185
|
-
settled = true;
|
|
186
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
187
|
-
stopCurrentPolling.current = null;
|
|
188
|
-
}
|
|
189
|
-
if (sessionStopRef.current === myStop) {
|
|
190
|
-
sessionStopRef.current = null;
|
|
191
|
-
}
|
|
192
|
-
resolve(u);
|
|
193
|
-
};
|
|
194
|
-
const finalizeWithRefresh = () => {
|
|
195
|
-
if (settled) return;
|
|
196
|
-
Promise.race([
|
|
197
|
-
refreshUserStatus(),
|
|
198
|
-
new Promise(
|
|
199
|
-
(r) => setTimeout(() => r(userStatusRef.current), 1e4)
|
|
200
|
-
)
|
|
201
|
-
]).catch(() => userStatusRef.current).then((u) => finish(u || userStatusRef.current));
|
|
202
|
-
};
|
|
203
|
-
const myStop = (withRefresh) => {
|
|
204
|
-
stopInner == null ? void 0 : stopInner();
|
|
205
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
206
|
-
stopCurrentPolling.current = null;
|
|
207
|
-
}
|
|
208
|
-
if (sessionStopRef.current === myStop) {
|
|
209
|
-
sessionStopRef.current = null;
|
|
210
|
-
}
|
|
211
|
-
if (withRefresh) {
|
|
212
|
-
finalizeWithRefresh();
|
|
213
|
-
} else {
|
|
214
|
-
finish(userStatusRef.current);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
const exposedStop = () => myStop(true);
|
|
218
|
-
stopCurrentPolling.current = exposedStop;
|
|
219
|
-
sessionStopRef.current = myStop;
|
|
220
|
-
getUserProfile({
|
|
208
|
+
pollUserStatus.current = null;
|
|
209
|
+
void withRequest({ signal: run.controller.signal, timeoutMs: 3e4 }, async (signal) => {
|
|
210
|
+
for (let attempt = 0; ; attempt++) {
|
|
211
|
+
try {
|
|
212
|
+
return await handleLogin({
|
|
213
|
+
appId,
|
|
221
214
|
productName,
|
|
222
|
-
userId,
|
|
223
215
|
onPassport,
|
|
224
216
|
getRequestContext: getApiContext,
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// 会话时长由 sessionTtlMs 主导;次数上限只作双保险
|
|
228
|
-
maxCount: 1e4,
|
|
229
|
-
maxDurationMs: sessionTtlMs,
|
|
230
|
-
// 快阶段过后降频
|
|
231
|
-
interval: () => Date.now() - sessionStart < fastPhaseMs ? fastIntervalMs : slowIntervalMs,
|
|
232
|
-
onStop: (fn) => {
|
|
233
|
-
stopInner = fn;
|
|
234
|
-
},
|
|
235
|
-
successCallback(u) {
|
|
236
|
-
u.userId = userId;
|
|
237
|
-
updateUserStatus(u);
|
|
238
|
-
finish(u);
|
|
239
|
-
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(u);
|
|
240
|
-
}
|
|
241
|
-
}).catch(() => {
|
|
242
|
-
console.log("checkout session ttl reached");
|
|
243
|
-
finalizeWithRefresh();
|
|
244
|
-
}).finally(() => {
|
|
245
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
246
|
-
stopCurrentPolling.current = null;
|
|
247
|
-
}
|
|
248
|
-
if (sessionStopRef.current === myStop) {
|
|
249
|
-
sessionStopRef.current = null;
|
|
250
|
-
}
|
|
217
|
+
...requestOptions(run),
|
|
218
|
+
signal
|
|
251
219
|
});
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
setTimeout(() => {
|
|
258
|
-
login(tryCount + 1);
|
|
259
|
-
}, 3e3);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
throwIfAborted(signal);
|
|
222
|
+
if (attempt >= extraRetries || !retryableAccountError(error)) throw error;
|
|
223
|
+
await accountDelay(3e3, signal);
|
|
224
|
+
}
|
|
260
225
|
}
|
|
226
|
+
}).then((userStatus2) => {
|
|
227
|
+
if (!isCurrentRun(run)) return;
|
|
228
|
+
loginPendingRef.current = false;
|
|
229
|
+
setLoginStatus("success");
|
|
230
|
+
setAccountError(null);
|
|
231
|
+
updateUserStatus(userStatus2);
|
|
232
|
+
window.userId = userStatus2.userId || "";
|
|
233
|
+
window.pollUserSubscribe = pollUserStatus.current = startPolling;
|
|
234
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(userStatus2);
|
|
235
|
+
}).catch((error) => {
|
|
236
|
+
if (!isCurrentRun(run)) return;
|
|
237
|
+
loginPendingRef.current = false;
|
|
238
|
+
setLoginStatus("error");
|
|
239
|
+
failAccountRun(run, error);
|
|
261
240
|
});
|
|
262
241
|
};
|
|
242
|
+
function startPolling(options) {
|
|
243
|
+
const { fastIntervalMs = 5e3, fastPhaseMs = 3e4, slowIntervalMs = 15e3, sessionTtlMs = 3e5 } = options || {};
|
|
244
|
+
const run = beginAccountRun("poll");
|
|
245
|
+
setHasClickedUpgrade(true);
|
|
246
|
+
const userId = userStatusRef.current.userId || window.userId || "";
|
|
247
|
+
const baseline = takeSubscribeBaseline(userStatusRef.current);
|
|
248
|
+
return new Promise((resolve) => {
|
|
249
|
+
let settled = false;
|
|
250
|
+
let finishing = false;
|
|
251
|
+
const finish = (user) => {
|
|
252
|
+
if (settled) return;
|
|
253
|
+
settled = true;
|
|
254
|
+
if (stopCurrentPolling.current === exposedStop) stopCurrentPolling.current = null;
|
|
255
|
+
if (sessionStopRef.current === stop) sessionStopRef.current = null;
|
|
256
|
+
resolve(user);
|
|
257
|
+
};
|
|
258
|
+
const stop = (withRefresh) => {
|
|
259
|
+
if (settled || finishing) return;
|
|
260
|
+
run.controller.abort();
|
|
261
|
+
if (!withRefresh) {
|
|
262
|
+
finish(userStatusRef.current);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
finishing = true;
|
|
266
|
+
if (sessionStopRef.current === stop) sessionStopRef.current = null;
|
|
267
|
+
if (stopCurrentPolling.current === exposedStop) stopCurrentPolling.current = null;
|
|
268
|
+
void refreshUserStatus(1e4).then(finish);
|
|
269
|
+
};
|
|
270
|
+
const exposedStop = () => stop(true);
|
|
271
|
+
sessionStopRef.current = stop;
|
|
272
|
+
stopCurrentPolling.current = exposedStop;
|
|
273
|
+
void getUserProfile({
|
|
274
|
+
productName,
|
|
275
|
+
userId,
|
|
276
|
+
onPassport,
|
|
277
|
+
getRequestContext: getApiContext,
|
|
278
|
+
...requestOptions(run),
|
|
279
|
+
isSubscribe: true,
|
|
280
|
+
baseline,
|
|
281
|
+
maxCount: 1e4,
|
|
282
|
+
maxDurationMs: sessionTtlMs,
|
|
283
|
+
interval: () => Date.now() - run.startedAt < fastPhaseMs ? fastIntervalMs : slowIntervalMs
|
|
284
|
+
}).then((user) => {
|
|
285
|
+
if (!isCurrentRun(run) || settled || finishing) return;
|
|
286
|
+
if (!user) {
|
|
287
|
+
stop(true);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
user.userId = userId;
|
|
291
|
+
setAccountError(null);
|
|
292
|
+
updateUserStatus(user);
|
|
293
|
+
finish(user);
|
|
294
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
295
|
+
}).catch((error) => {
|
|
296
|
+
if (finishing) return;
|
|
297
|
+
failAccountRun(run, error);
|
|
298
|
+
finish(userStatusRef.current);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
}
|
|
263
302
|
useEffect(() => {
|
|
264
303
|
if (!version && !productName && !baseUrl && !appId) {
|
|
265
304
|
throw new Error("version, productName, baseUrl, appId is required");
|
|
@@ -336,10 +375,27 @@ const YiChongCanvaProvider = ({
|
|
|
336
375
|
updateUserStatus({});
|
|
337
376
|
}, [test]);
|
|
338
377
|
useEffect(() => {
|
|
339
|
-
|
|
340
|
-
|
|
378
|
+
mountedRef.current = true;
|
|
379
|
+
const scope = `${appId}|${productName}`;
|
|
380
|
+
if (identityScopeRef.current !== scope) {
|
|
381
|
+
identityScopeRef.current = scope;
|
|
382
|
+
window.userId = "";
|
|
383
|
+
passportRef.current = null;
|
|
384
|
+
userStatusRef.current = {};
|
|
385
|
+
setUserStatus({});
|
|
386
|
+
pollUserStatus.current = null;
|
|
341
387
|
}
|
|
342
|
-
|
|
388
|
+
if (autoLogin) login();
|
|
389
|
+
else setLoginStatus("idle");
|
|
390
|
+
return () => {
|
|
391
|
+
var _a, _b;
|
|
392
|
+
mountedRef.current = false;
|
|
393
|
+
loginPendingRef.current = false;
|
|
394
|
+
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
395
|
+
(_b = accountRunRef.current) == null ? void 0 : _b.controller.abort();
|
|
396
|
+
accountRunRef.current = null;
|
|
397
|
+
};
|
|
398
|
+
}, [autoLogin, appId, productName]);
|
|
343
399
|
const initCredits = () => {
|
|
344
400
|
var _a, _b;
|
|
345
401
|
if (!userStatus.subscription || !userStatus.profile) {
|
|
@@ -448,24 +504,34 @@ const YiChongCanvaProvider = ({
|
|
|
448
504
|
}
|
|
449
505
|
return "Premium";
|
|
450
506
|
};
|
|
451
|
-
function refreshUserStatus() {
|
|
452
|
-
return
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
507
|
+
function refreshUserStatus(timeoutMs = 3e4) {
|
|
508
|
+
if (loginPendingRef.current || !userStatusRef.current.userId || !userStatusRef.current.profile) return Promise.resolve(userStatusRef.current);
|
|
509
|
+
const existing = accountRunRef.current;
|
|
510
|
+
if ((existing == null ? void 0 : existing.operation) === "refresh" && existing.promise && isCurrentRun(existing)) return existing.promise;
|
|
511
|
+
const run = beginAccountRun("refresh");
|
|
512
|
+
const userId = userStatusRef.current.userId || window.userId || "";
|
|
513
|
+
run.promise = getUserProfile({
|
|
514
|
+
productName,
|
|
515
|
+
userId,
|
|
516
|
+
onPassport,
|
|
517
|
+
getRequestContext: getApiContext,
|
|
518
|
+
...requestOptions(run),
|
|
519
|
+
maxCount: 2,
|
|
520
|
+
maxDurationMs: timeoutMs
|
|
521
|
+
}).then((user) => {
|
|
522
|
+
if (!user || !isCurrentRun(run)) return userStatusRef.current;
|
|
523
|
+
user.userId = userId;
|
|
524
|
+
setAccountError(null);
|
|
525
|
+
updateUserStatus(user);
|
|
526
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
527
|
+
return user;
|
|
528
|
+
}).catch((error) => {
|
|
529
|
+
failAccountRun(run, error);
|
|
530
|
+
return userStatusRef.current;
|
|
531
|
+
}).finally(() => {
|
|
532
|
+
run.promise = void 0;
|
|
468
533
|
});
|
|
534
|
+
return run.promise;
|
|
469
535
|
}
|
|
470
536
|
useEffect(() => {
|
|
471
537
|
initCredits();
|
|
@@ -639,12 +705,13 @@ const YiChongCanvaProvider = ({
|
|
|
639
705
|
},
|
|
640
706
|
getPricingLink,
|
|
641
707
|
updateTestMode,
|
|
642
|
-
refreshUserStatus,
|
|
708
|
+
refreshUserStatus: () => refreshUserStatus(),
|
|
643
709
|
hasClickedUpgrade,
|
|
644
710
|
setHasClickedUpgrade: (v) => {
|
|
645
711
|
setHasClickedUpgrade(v);
|
|
646
712
|
},
|
|
647
713
|
loginStatus,
|
|
714
|
+
accountError,
|
|
648
715
|
updateCredits,
|
|
649
716
|
stopCurrentPolling: () => {
|
|
650
717
|
var _a;
|
|
@@ -655,8 +722,7 @@ const YiChongCanvaProvider = ({
|
|
|
655
722
|
return false;
|
|
656
723
|
},
|
|
657
724
|
manualLogin: (_retry = 1) => {
|
|
658
|
-
|
|
659
|
-
login(0);
|
|
725
|
+
login(_retry);
|
|
660
726
|
}
|
|
661
727
|
},
|
|
662
728
|
children
|
package/dist/types/api/chat.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { ApiContext } from './request';
|
|
2
2
|
import { ChatApi } from './types';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* 将同步大模型接口绑定到 Provider;应用通过 client.chat() 调用,不需另建 API 包装层。
|
|
5
|
+
* getContext 在每次调用时读取当前账号、语言和服务地址,避免工厂创建时锁住旧 JWT。
|
|
6
|
+
* 返回后端原始响应包,完整文本位于 data.result;这里只发起一次请求,不轮询、重试或扣费。
|
|
7
|
+
* 消息语义、模型选择与回退由后端处理;传入 model 并不代表后端一定使用该模型。
|
|
8
|
+
*/
|
|
4
9
|
export declare function createChatApi(getContext: () => ApiContext): ChatApi;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { ApiContext } from './request';
|
|
2
2
|
import { ContentExtractionApi } from './types';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* 绑定文件、视频和网页内容提取接口;接收可访问的 URL,不负责先上传本地文件。
|
|
5
|
+
* 每次调用从 Provider 获取当前身份;返回完整响应包,提取文本位于 data.content。
|
|
6
|
+
* 提取期间只等待一次同步请求,不轮询、不自动重试,也不自动调用 Chat 或消费接口。
|
|
7
|
+
*/
|
|
4
8
|
export declare function createContentExtractionApi(getContext: () => ApiContext): ContentExtractionApi;
|
|
@@ -1,12 +1,24 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* 具名后端 API 统一使用的异常,可用 instanceof ApiError 区分。
|
|
3
|
+
* 多数后端接口保留有效的 msg/message;上报 Worker 失败、本地校验、网络和取消使用统一文案。
|
|
4
|
+
* code 用于业务分支,status 仅表示可用的 HTTP 状态;不要从文案中解析错误类型。
|
|
5
|
+
* 例如 aborted 可按主动取消处理,account_not_ready 表示账号凭据尚未就绪。
|
|
6
|
+
*/
|
|
2
7
|
export declare class ApiError extends Error {
|
|
3
8
|
readonly code: string | number;
|
|
4
9
|
readonly status?: number | undefined;
|
|
10
|
+
/** 与 Error.message 相同,可用于界面展示;不保证所有本地错误文案已经本地化。 */
|
|
5
11
|
readonly userMessage: string;
|
|
6
|
-
/**
|
|
12
|
+
/** 经公共后端错误处理器读取的对象型 data,如 error_code/request_id;部分接口不返回此字段。 */
|
|
7
13
|
readonly data?: Record<string, unknown>;
|
|
8
14
|
/** Retry-After 响应头的秒数;工具包不会自动重试。 */
|
|
9
15
|
readonly retryAfter?: number;
|
|
16
|
+
/**
|
|
17
|
+
* @param message 可展示的错误说明。
|
|
18
|
+
* @param code 后端业务码或 SDK 标识,例如 invalid_request、timeout、network_error。
|
|
19
|
+
* @param status 已取得的 HTTP 状态;请求发出前的错误等情况通常没有此值。
|
|
20
|
+
* @param details 可选诊断数据和 Retry-After 秒数;仅提供信息,不触发重试。
|
|
21
|
+
*/
|
|
10
22
|
constructor(message: string, code: string | number, status?: number | undefined, details?: {
|
|
11
23
|
data?: Record<string, unknown>;
|
|
12
24
|
retryAfter?: number;
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 后端 API 的公共类型与异常出口。
|
|
3
|
+
* 实现工厂保持内部使用,业务通过 Provider 的 client 调用方法,共享登录身份和配置。
|
|
4
|
+
* 参数、返回值及方法的 JSDoc 定义在 types.ts,会随声明文件发布供编辑器提示。
|
|
5
|
+
*/
|
|
1
6
|
export { ApiError } from './error';
|
|
2
|
-
export type { ApiRequestOptions, ApiResponse, ChatApi, ChatParams, ChatPayload, ChatResult, CompleteUploadParams, CompleteUploadResponse, ContentExtractParams, ContentExtractResult, ContentExtractionApi, TextCheckParams, CreateSpecifyPredictionParams, ErrorReportingApi, ErrorReportSource, GetPredictionParams, GeneratePresignedUrlParams, GetUploadUrlParams, InitiateUploadParams, InitiateUploadResponse, JsonObject, JsonValue, Prediction, PredictionApi, ReportErrorParams, ReportErrorResult, PresignedUrlResponse, TextModerationApi, TextModerationCategory, TextModerationDecision, TextModerationResult, UploadApi, UploadFileParams, UploadFileResult, UploadedPart, UploadPartUrlResponse, UsageApi, UsageParams, UsageResponse, } from './types';
|
|
7
|
+
export type { ApiRequestOptions, ApiResponse, ChatApi, ChatParams, ChatPayload, ChatResult, CompleteUploadParams, CompleteUploadResponse, ContentExtractParams, ContentExtractResult, ContentExtractionApi, TextCheckParams, CreateSpecifyPredictionParams, ErrorReportingApi, ErrorReportSource, GetPredictionParams, GeneratePresignedUrlParams, GetUploadUrlParams, InitiateUploadParams, InitiateUploadResponse, JsonObject, JsonValue, Prediction, PredictionApi, ReportErrorParams, ReportErrorResult, PresignedUrlResponse, TextModerationApi, TextModerationCategory, TextModerationDecision, TextModerationResult, UploadApi, UploadFileParams, UploadFileResult, UploadedPart, UploadPartUrlResponse, UploadProgress, UsageApi, UsageParams, UsageResponse, } from './types';
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { ApiContext } from './request';
|
|
2
2
|
import { TextModerationApi } from './types';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* 绑定文本审核能力,对外名为 client.textCheck(),后端仍使用 public 审核路由。
|
|
5
|
+
* 返回含 allowed / decision / categories 的原始响应;应用应根据结果控制后续业务。
|
|
6
|
+
* 本方法不会自动发起生成或扣费,审核请求本身也不会被 SDK 自动重试。
|
|
7
|
+
*/
|
|
4
8
|
export declare function createTextModerationApi(getContext: () => ApiContext): TextModerationApi;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { ApiContext } from './request';
|
|
2
2
|
import { PredictionApi } from './types';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* 将后端同名的任务创建/查询接口绑定到 Provider,保留参数名与原始响应结构。
|
|
5
|
+
* 创建仅返回任务 ID,查询仅取得一次状态;轮询间隔、取消业务与结果展示由应用编排。
|
|
6
|
+
* 两种调用都只发送一次请求,不自动重试、审核输入或记录消费,避免重复创建/扣费。
|
|
7
|
+
*/
|
|
4
8
|
export declare function createPredictionApi(getContext: () => ApiContext): PredictionApi;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { ApiContext } from './request';
|
|
2
2
|
import { ErrorReportingApi } from './types';
|
|
3
|
+
/** 上报的应用/账号归属由 Provider 注入;passport 只参与脱敏,不会作为上报认证票据。 */
|
|
3
4
|
export interface ReportingContext extends Pick<ApiContext, "userStatus" | "productName"> {
|
|
4
5
|
appId: string;
|
|
5
6
|
version: number;
|
|
6
7
|
/** 只用于从诊断内容中移除当前账户凭据。 */
|
|
7
8
|
passport?: string;
|
|
8
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* 绑定显式错误上报能力:整理诊断信息,交由现有 Worker 转发到指定飞书机器人。
|
|
12
|
+
* 返回 Worker 的确认结果,不安装全局异常监听,也不在业务 API 失败时自动上报或重试;账户模块的最终失败由 Provider 调用。
|
|
13
|
+
* 登录失败本身也需要诊断,因此允许账号尚未就绪,用 unknown 标记缺失身份。
|
|
14
|
+
* 上报失败仍会抛出 ApiError,应用应单独捕获,避免它覆盖原来的业务异常。
|
|
15
|
+
*/
|
|
9
16
|
export declare function createErrorReportingApi(getContext: () => ReportingContext): ErrorReportingApi;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import { JsonObject, ReportErrorParams } from './types';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* 将 Error、字符串或其他异常值转换为体积受限的 JSON 诊断信息,供 reporting.ts 发送。
|
|
4
|
+
* 不读取 window/localStorage、不发送请求、不修改原错误;已知凭据由调用层显式传入。
|
|
5
|
+
* 按字段名、常见凭据文本和已知值做尽力脱敏,并限制深度、项数及文本长度。
|
|
6
|
+
* 任意业务文本仍可能包含个人信息,调用方应主动挑选 context,不能把此处当作全面匿名化。
|
|
7
|
+
*/
|
|
3
8
|
export declare function reportingPayload(params: ReportErrorParams, secrets?: readonly string[]): JsonObject;
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { UserStatus } from '../login/interface';
|
|
2
2
|
import { ApiError } from './error';
|
|
3
3
|
import { ApiRequestOptions, ApiResponse, JsonObject } from './types';
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* 各服务共用的调用上下文,由 Provider 在调用时提供当前快照。
|
|
6
|
+
* 身份来自已完成的登录流程,不从 window / localStorage 搜索或猜测 API 凭据。
|
|
7
|
+
* query 中的归属信息不是认证本身;各服务仍须使用其对应的认证协议。
|
|
8
|
+
*/
|
|
5
9
|
export interface CommonRequestContext {
|
|
6
10
|
userStatus: UserStatus;
|
|
7
11
|
productName: string;
|
|
8
12
|
getLocale?: () => string;
|
|
9
13
|
}
|
|
14
|
+
/** AI 网关上下文;具体能力可由 Provider 指向独立服务地址,但共用这里的请求协议。 */
|
|
10
15
|
export interface ApiContext extends CommonRequestContext {
|
|
11
16
|
apiBaseUrl: string;
|
|
12
17
|
}
|
|
@@ -17,17 +22,50 @@ declare const apiPaths: {
|
|
|
17
22
|
readonly contentExtract: "/api/content-extract";
|
|
18
23
|
readonly chat: "/api/chat";
|
|
19
24
|
};
|
|
25
|
+
/** 粗筛非空、非数组对象;是否属于普通 JSON 对象由 validateJson 进一步判断。 */
|
|
20
26
|
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
27
|
+
/** 仅判断是否含有效字符,不修改返回给后端的原始字符串及其首尾空白。 */
|
|
21
28
|
export declare function isNonemptyString(value: unknown): value is string;
|
|
29
|
+
/** 统一标记本地参数错误,调用方可通过 code 区分它与网络或后端业务错误。 */
|
|
22
30
|
export declare function invalid(message: string): never;
|
|
23
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* 递归验证枚举到的参数值,防止 stringify 悄悄丢掉 undefined / 函数,或将非有限数改成 null。
|
|
33
|
+
* 只接受普通对象/数组等 JSON 值,拒绝 Date、类实例及循环引用,不执行自定义序列化逻辑。
|
|
34
|
+
* ancestors 只跟踪当前递归路径:同一对象被多个字段引用可以通过,真正的祖先回环才拒绝。
|
|
35
|
+
*/
|
|
24
36
|
export declare function validateJson(value: unknown, ancestors?: Set<object>): void;
|
|
37
|
+
/**
|
|
38
|
+
* 语言优先取 Provider 的实时回调,其次文档、浏览器,最后使用 en。
|
|
39
|
+
* 将区域标记规范化为 Accept-Language 可用值;若收到语言列表,仅使用第一项。
|
|
40
|
+
* 这里只生成请求头,浏览器请求不额外添加 accept_language 查询参数。
|
|
41
|
+
*/
|
|
25
42
|
export declare function language(getLocale?: () => string): string;
|
|
26
43
|
/** 每次请求读取当前运行环境的 IANA 时区,环境不支持时使用 UTC。 */
|
|
27
44
|
export declare function timezone(): string;
|
|
45
|
+
/**
|
|
46
|
+
* 在服务基地址的已有路径后追加固定接口路径,兼容反向代理前缀。
|
|
47
|
+
* 拒绝凭据、查询串和片段,避免混入历史身份参数或让同一个配置产生不明确的请求目标。
|
|
48
|
+
* 允许 HTTP 用于本地服务;带身份的正式部署应使用可信任的 HTTPS 基地址。
|
|
49
|
+
*/
|
|
28
50
|
export declare function endpoint(baseUrl: string, path: string): URL;
|
|
51
|
+
/**
|
|
52
|
+
* 写入后端通用参数:系统 uid、Canva platform_uid、平台、产品渠道及运行环境时区。
|
|
53
|
+
* uid 对应 profile.id,platform_uid 对应 Canva userId;缺失身份不伪造为空字符串。
|
|
54
|
+
* 本函数会修改传入 URL;语言单独放在请求头,是否必须已登录由各服务调用处判断。
|
|
55
|
+
*/
|
|
29
56
|
export declare function addContextQuery(url: URL, context: CommonRequestContext): void;
|
|
57
|
+
/** 读取完整 JSON 正文;HTML 错误页或损坏 JSON 只产生统一错误,不把原始正文混进异常。 */
|
|
30
58
|
export declare function readJson(response: Response): Promise<unknown>;
|
|
59
|
+
/**
|
|
60
|
+
* 尽量保留后端 msg / code 以及对象类型的诊断 data,供应用展示或识别业务失败。
|
|
61
|
+
* Retry-After 仅接受整数秒并作为提示返回;此处不会安排重试,也不解析 HTTP 日期格式。
|
|
62
|
+
*/
|
|
31
63
|
export declare function backendError(response: Response, payload: unknown, fallbackCode: string | number): ApiError;
|
|
64
|
+
/**
|
|
65
|
+
* AI 接口的公共 POST:注入 Bearer JWT、Channel、语言与通用 query,并校验响应包。
|
|
66
|
+
* 返回后端的 { code, msg, data },不提取/重命名 data;具体 API 再验证自己的业务数据。
|
|
67
|
+
* 只执行一次请求,超时/取消涵盖读取响应正文;credentials: omit 不依赖浏览器 Cookie。
|
|
68
|
+
* 禁止重定向,避免认证头被带往配置以外的跳转目标。
|
|
69
|
+
*/
|
|
32
70
|
export declare function postApi<T>(context: ApiContext, method: keyof typeof apiPaths, params: JsonObject, options?: ApiRequestOptions): Promise<ApiResponse<T>>;
|
|
33
71
|
export {};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { ApiRequestOptions } from './types';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* 为一次完整操作提供统一的超时与取消边界,默认等待 60 秒;具体 API 可覆盖默认值。
|
|
4
|
+
* work 必须将接收到的 signal 传给内部请求,并在此回调内完成所需响应正文的读取。
|
|
5
|
+
* 多步上传也可共用同一个预算,超时不会因为开始下一个步骤而重置。
|
|
6
|
+
* 只执行一次 work,不做重试;取消客户端等待无法保证撤销后端已经接受的操作。
|
|
7
|
+
*/
|
|
3
8
|
export declare function withRequest<T>(options: ApiRequestOptions, work: (signal: AbortSignal) => Promise<T>): Promise<T>;
|
|
9
|
+
/** 多步操作在阶段交界处主动检查,避免取消后继续发起下一次签名、上传或合并请求。 */
|
|
4
10
|
export declare function throwIfAborted(signal: AbortSignal): void;
|