yichong-canva2 2.4.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -1
- package/dist/api/error.js +5 -1
- package/dist/api/predictions.js +106 -9
- package/dist/api/reporting.js +1 -1
- package/dist/api/request.js +4 -0
- package/dist/api/uploads.js +4 -4
- package/dist/index.js +4 -1
- package/dist/login/cache.js +38 -0
- package/dist/login/consumption.js +94 -0
- package/dist/login/context.js +13 -2
- package/dist/login/login/apis/index.js +3 -2
- package/dist/login/login/apis/request.js +1 -13
- package/dist/login/login/login.js +2 -1
- package/dist/login/privider.js +167 -197
- package/dist/login/version.js +22 -17
- package/dist/types/api/error.d.ts +2 -0
- package/dist/types/api/index.d.ts +3 -2
- package/dist/types/api/predictions.d.ts +8 -3
- package/dist/types/api/request.d.ts +4 -2
- package/dist/types/api/types.d.ts +42 -4
- package/dist/types/login/cache.d.ts +10 -0
- package/dist/types/login/consumption.d.ts +68 -0
- package/dist/types/login/context.d.ts +7 -4
- package/dist/types/login/index.d.ts +1 -0
- package/dist/types/login/interface.d.ts +12 -0
- package/dist/types/login/login/apis/index.d.ts +1 -1
- package/dist/types/login/version.d.ts +4 -3
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UserStatus } from '../login/interface';
|
|
2
2
|
import { ApiError } from './error';
|
|
3
|
-
import { ApiRequestOptions, ApiResponse, JsonObject } from './types';
|
|
3
|
+
import { ApiRequestOptions, ApiResponse, CancelPredictionResponse, JsonObject } from './types';
|
|
4
4
|
/**
|
|
5
5
|
* 各服务共用的调用上下文,由 Provider 在调用时提供当前快照。
|
|
6
6
|
* 身份来自已完成的登录流程,不从 window / localStorage 搜索或猜测 API 凭据。
|
|
@@ -18,6 +18,7 @@ export interface ApiContext extends CommonRequestContext {
|
|
|
18
18
|
declare const apiPaths: {
|
|
19
19
|
readonly createSpecifyPrediction: "/replicate/createSpecifyPrediction";
|
|
20
20
|
readonly getPrediction: "/replicate/getPrediction";
|
|
21
|
+
readonly cancelPrediction: "/replicate/cancelPrediction";
|
|
21
22
|
readonly textCheck: "/api/public/text-moderation/check";
|
|
22
23
|
readonly contentExtract: "/api/content-extract";
|
|
23
24
|
readonly chat: "/api/chat";
|
|
@@ -72,5 +73,6 @@ export declare function backendError(response: Response, payload: unknown, fallb
|
|
|
72
73
|
* 只执行一次请求,超时/取消涵盖读取响应正文;credentials: omit 不依赖浏览器 Cookie。
|
|
73
74
|
* 禁止重定向,避免认证头被带往配置以外的跳转目标。
|
|
74
75
|
*/
|
|
75
|
-
export declare function postApi
|
|
76
|
+
export declare function postApi(context: ApiContext, method: "cancelPrediction", params: JsonObject, options?: ApiRequestOptions): Promise<CancelPredictionResponse>;
|
|
77
|
+
export declare function postApi<T>(context: ApiContext, method: Exclude<keyof typeof apiPaths, "cancelPrediction">, params: JsonObject, options?: ApiRequestOptions): Promise<ApiResponse<T>>;
|
|
76
78
|
export {};
|
|
@@ -126,11 +126,11 @@ export interface ApiResponse<T> {
|
|
|
126
126
|
/** 对应接口的数据,不会被 SDK 解包或重命名。 */
|
|
127
127
|
data: T;
|
|
128
128
|
}
|
|
129
|
-
/**
|
|
129
|
+
/** 一次查询取得的任务快照;可单次查询,也可交给 waitForPrediction 等待完成。 */
|
|
130
130
|
export interface Prediction {
|
|
131
131
|
/** 任务 ID。 */
|
|
132
132
|
id: string;
|
|
133
|
-
/** 常见值:starting / processing / succeeded / failed / canceled。 */
|
|
133
|
+
/** 常见值:starting / processing / succeeded / failed / canceled / aborted。 */
|
|
134
134
|
status: string;
|
|
135
135
|
/** 模型输出;可能是 URL、数组或对象,需按所选模型处理,不能一律当成图片 URL。 */
|
|
136
136
|
output?: JsonValue;
|
|
@@ -155,6 +155,22 @@ export interface ApiRequestOptions {
|
|
|
155
155
|
*/
|
|
156
156
|
timeoutMs?: number;
|
|
157
157
|
}
|
|
158
|
+
/** 等待已创建的任务完成;不创建任务、不消费、不隐式调用后端取消。 */
|
|
159
|
+
export interface WaitForPredictionOptions extends ApiRequestOptions {
|
|
160
|
+
/** 两次查询之间的间隔,默认 3000 毫秒;允许 0,不在最后一次查询后继续等待。 */
|
|
161
|
+
intervalMs?: number;
|
|
162
|
+
/** 查询次数上限,默认 60;失败的查询也计入次数,必须为正整数。 */
|
|
163
|
+
maxPolls?: number;
|
|
164
|
+
/** 所有查询及间隔共用的总等待预算,默认 180000 毫秒;单次查询仍最多等待 60 秒。 */
|
|
165
|
+
timeoutMs?: number;
|
|
166
|
+
/**
|
|
167
|
+
* 默认 false,仅 succeeded 才成功。显式 true 时允许未结束任务已有 HTTP(S) 输出就返回,
|
|
168
|
+
* 供已确认支持提前输出的旧模型使用;failed/canceled/aborted 始终优先作为失败处理。
|
|
169
|
+
*/
|
|
170
|
+
allowEarlyOutput?: boolean;
|
|
171
|
+
}
|
|
172
|
+
/** 后端取消接口存在两种原始响应;与 yichong-api 保持相同合同,不人为包装成 ok 布尔值。 */
|
|
173
|
+
export type CancelPredictionResponse = ApiResponse<Prediction> | Prediction;
|
|
158
174
|
/** 指定模型的异步任务 API,业务流程可以用 restorePhoto 等业务函数组合这些原始方法。 */
|
|
159
175
|
export interface PredictionApi {
|
|
160
176
|
/**
|
|
@@ -174,6 +190,22 @@ export interface PredictionApi {
|
|
|
174
190
|
* 应用自行安排下次查询并处理 succeeded / failed / canceled 等终态。
|
|
175
191
|
*/
|
|
176
192
|
getPrediction(params: GetPredictionParams, options?: ApiRequestOptions): Promise<ApiResponse<Prediction>>;
|
|
193
|
+
/**
|
|
194
|
+
* 按 intervalMs 查询并返回成功的任务快照。默认只在 succeeded 时返回,输出由业务另行提取。
|
|
195
|
+
* failed/canceled/aborted 抛 prediction_failed,保留 error_msg 或 error;请求取消抛 aborted。
|
|
196
|
+
* 查询次数或总预算用完抛 prediction_timeout;临时网络/单次超时可继续,连续 3 次失败后保留原错误。
|
|
197
|
+
* HTTP、鉴权、业务错误直接抛出;不会重复创建、扣费或自动取消后端任务。
|
|
198
|
+
* @example
|
|
199
|
+
* const task = await client.waitForPrediction({ id }, { signal: controller.signal });
|
|
200
|
+
* const url = getPredictionOutputUrl(task.output);
|
|
201
|
+
*/
|
|
202
|
+
waitForPrediction(params: GetPredictionParams, options?: WaitForPredictionOptions): Promise<Prediction>;
|
|
203
|
+
/**
|
|
204
|
+
* POST /replicate/cancelPrediction,只发送一次,默认超时 60 秒,保留后端原始响应。
|
|
205
|
+
* 失败抛 ApiError,由业务决定是否展示;各供应商的取消能力不同,不保证上游任务或费用立即停止。
|
|
206
|
+
* 取消前端等待与取消后端任务相互独立,waitForPrediction 不会自动调用本方法。
|
|
207
|
+
*/
|
|
208
|
+
cancelPrediction(params: GetPredictionParams, options?: ApiRequestOptions): Promise<CancelPredictionResponse>;
|
|
177
209
|
}
|
|
178
210
|
/** 文本审核能力,与生成接口分开调用。 */
|
|
179
211
|
export interface TextModerationApi {
|
|
@@ -324,11 +356,17 @@ export interface UploadFileParams {
|
|
|
324
356
|
/** 目标桶,省略时使用 video;不会从文件 MIME 类型推断桶名。 */
|
|
325
357
|
bucketName?: string;
|
|
326
358
|
/**
|
|
327
|
-
* 省略时生成
|
|
359
|
+
* 省略时生成 {retention}_{app_channel}_{YYYY-MM-DD}_{32位随机}.{后缀},默认前缀 temp_。
|
|
328
360
|
* 渠道取 Provider.productName,日期取本地日期;随机部分沿用旧应用 randomString(32) 的固定大小写字母/数字字符集。
|
|
329
|
-
* 显式传入时使用该对象 key
|
|
361
|
+
* 显式传入时使用该对象 key,不自动添加前缀或改名;不能与 retention 同时传入。
|
|
330
362
|
*/
|
|
331
363
|
objectName?: string;
|
|
364
|
+
/**
|
|
365
|
+
* 自动命名的前缀,默认 temp;temp_ 对应现有 1 天规则,day30_ 对应 30 天规则。
|
|
366
|
+
* 只选择对象前缀,实际删除取决于目标桶的 R2 生命周期规则;普通/分片上传遵循同一规则。
|
|
367
|
+
* 与自定义 objectName 二选一,不能同时指定。
|
|
368
|
+
*/
|
|
369
|
+
retention?: "temp" | "day30";
|
|
332
370
|
/** true 时小文件也使用分片;省略或 false 时按文件大小选择。 */
|
|
333
371
|
multipart?: boolean;
|
|
334
372
|
/**
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 版本是 Canva 应用属性;不同应用共用 localhost 时也不能共享审核版本。 */
|
|
2
|
+
export declare const versionCacheKey: (appId: string) => string;
|
|
3
|
+
/** 测试额度同时隔离应用、产品和两套账户 ID;JSON 编码避免下划线拼接产生键冲突。 */
|
|
4
|
+
export declare function testCacheKey(kind: "test_info" | "test_free_cnt", appId: string, productName: string, userId?: string, uid?: string): string;
|
|
5
|
+
/** passport 沿用历史键名(只含 Canva userId),已缓存的票据升级后仍可复用。 */
|
|
6
|
+
export declare const passportCacheKey: (userId: string) => string;
|
|
7
|
+
/** 隐私模式/禁用存储不能阻断账户和消费流程。旧的全局 version/test_info/test_free_cnt 不再读取。 */
|
|
8
|
+
export declare function readCache(key: string): string | null;
|
|
9
|
+
export declare function writeCache(key: string, value: string): void;
|
|
10
|
+
export declare function removeCache(key: string): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ReportErrorParams, UsageApi } from '../api/types';
|
|
2
|
+
/** use() 的三个真实消费渠道;组合渠道会按月额度、积分的顺序各提交一次。 */
|
|
3
|
+
export type UseChannel = "free_cnt" | "monthly" | "monthly_and_free_cnt";
|
|
4
|
+
/** 已获得后端成功确认的数量;不包含本地乐观扣减,也不把超时推测成失败。 */
|
|
5
|
+
export interface ConfirmedUsage {
|
|
6
|
+
monthly: number;
|
|
7
|
+
freeCnt: number;
|
|
8
|
+
}
|
|
9
|
+
/** 可供应用诊断的消费错误,不包含 passport、JWT 或原始请求/响应正文。 */
|
|
10
|
+
export interface UseError {
|
|
11
|
+
code: string | number;
|
|
12
|
+
message: string;
|
|
13
|
+
status?: number;
|
|
14
|
+
}
|
|
15
|
+
export type UseSkipReason = "test_mode" | "in_review_test" | "free_unlimited" | "not_upload" | "account_changed";
|
|
16
|
+
type UseResultBase = {
|
|
17
|
+
confirmed: ConfirmedUsage;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* use() 始终 resolve。charged=true 表示至少一项已确认扣费;全部成功还须判断 status。
|
|
21
|
+
* unknown 的 charged=null 表示可能已记账,禁止据此自动重试。
|
|
22
|
+
* partial 通过 confirmed 和 remaining 区分已成功部分与失败/未知/主动跳过的剩余部分。
|
|
23
|
+
*/
|
|
24
|
+
export type UseResult = UseResultBase & ({
|
|
25
|
+
status: "charged";
|
|
26
|
+
charged: true;
|
|
27
|
+
channel: UseChannel;
|
|
28
|
+
} | {
|
|
29
|
+
status: "skipped";
|
|
30
|
+
charged: false;
|
|
31
|
+
reason: UseSkipReason;
|
|
32
|
+
} | {
|
|
33
|
+
status: "failed";
|
|
34
|
+
charged: false;
|
|
35
|
+
reason: "invalid_count" | "account_not_ready" | "request_failed";
|
|
36
|
+
error: UseError;
|
|
37
|
+
} | {
|
|
38
|
+
status: "unknown";
|
|
39
|
+
charged: null;
|
|
40
|
+
channel: UseChannel;
|
|
41
|
+
error: UseError;
|
|
42
|
+
} | {
|
|
43
|
+
status: "partial";
|
|
44
|
+
charged: true;
|
|
45
|
+
channel: "monthly_and_free_cnt";
|
|
46
|
+
remaining: "failed" | "unknown" | "skipped";
|
|
47
|
+
reason?: UseSkipReason;
|
|
48
|
+
error?: UseError;
|
|
49
|
+
});
|
|
50
|
+
export declare const skippedUsage: (reason: UseSkipReason) => UseResult;
|
|
51
|
+
/** 不信任任意异常文本;上报与公开结果均使用固定诊断,业务码/HTTP 状态单独保留。 */
|
|
52
|
+
export declare function usageFailure(error: unknown): UseError;
|
|
53
|
+
export declare function invalidUsageResult(): UseResult;
|
|
54
|
+
export declare function isValidUsageCount(cnt: number): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* 这里只组织已计划的两种消费,不负责套餐判断和 React 状态。
|
|
57
|
+
* 每一步只发一次请求;月额度失败即停止,不继续扣积分。两次请求不是后端事务。
|
|
58
|
+
* report 是辅助通道,单次编排只上报一次,既不 await 也不允许拒绝泄漏到业务。
|
|
59
|
+
*/
|
|
60
|
+
export declare function recordUsage(options: {
|
|
61
|
+
monthly: number;
|
|
62
|
+
freeCnt: number;
|
|
63
|
+
notUpload: boolean;
|
|
64
|
+
api: Pick<UsageApi, "reduceFreeCnt" | "addMonthlyUsage">;
|
|
65
|
+
isCurrent: () => boolean;
|
|
66
|
+
report: (params: ReportErrorParams) => unknown;
|
|
67
|
+
}): Promise<UseResult>;
|
|
68
|
+
export {};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { AccountError } from './account';
|
|
3
|
+
import { UseResult } from './consumption';
|
|
3
4
|
import { ChatApi, ContentExtractionApi, ErrorReportingApi, PredictionApi, TextModerationApi, TextToSpeechApi, UploadApi, UsageApi, UsageReportingApi, VttApi } from '../api/types';
|
|
4
5
|
import { canUseError, CheckoutSessionOptions, TestMode, UserStatus } from './interface';
|
|
5
6
|
/**
|
|
6
7
|
* `useYiChongCanva()` 返回的客户端:账户状态、历史额度方法和后端同名 API 共用一个 Provider。
|
|
7
8
|
*
|
|
8
9
|
* `userStatus` 等字段随 React 渲染更新;后端 API 则在每次调用时读取 Provider 的最新身份引用。
|
|
9
|
-
*
|
|
10
|
+
* use() 返回不会 reject 的消费结果;同名消费 API 仍保留原始响应并以 ApiError 表达失败。
|
|
10
11
|
*/
|
|
11
12
|
export interface YiChongCanvaContextType extends PredictionApi, TextModerationApi, ContentExtractionApi, ChatApi, VttApi, TextToSpeechApi, UsageApi, UploadApi, ErrorReportingApi, UsageReportingApi {
|
|
12
13
|
/** 当前账户快照;登录前为 `{}`。Canva 用户 ID 是 userId,系统用户 ID 是 profile.id。 */
|
|
@@ -25,10 +26,12 @@ export interface YiChongCanvaContextType extends PredictionApi, TextModerationAp
|
|
|
25
26
|
isSubscribed: boolean;
|
|
26
27
|
/**
|
|
27
28
|
* 历史消费入口:默认消耗 1;订阅用户先用月额度再用积分,force=true 直接走积分分支。
|
|
28
|
-
*
|
|
29
|
-
*
|
|
29
|
+
* 本地余额乐观更新,不会先执行 canUseLimit 或自动回滚;消费请求无幂等保障,因此不自动重试。
|
|
30
|
+
* 同一账户的连续调用串行执行、使用最新余额;切换账户后旧队列不继续发新请求。
|
|
31
|
+
* notUpload 仅阻止积分扣费请求,订阅月用量仍会发送。结果明确区分全成功、跳过、失败、部分成功和未知。
|
|
32
|
+
* Promise 始终 resolve;旧应用可继续忽略返回值。partial 的 charged=true 仅表示部分已确认。
|
|
30
33
|
*/
|
|
31
|
-
use: (cnt?: number, force?: boolean, notUpload?: boolean) =>
|
|
34
|
+
use: (cnt?: number, force?: boolean, notUpload?: boolean) => Promise<UseResult>;
|
|
32
35
|
/** 旧的纯本地判断:订阅用户在 force=false 时直接通过,不考虑月限额;force=true 只检查积分。 */
|
|
33
36
|
canUse: (cnt?: number, force?: boolean) => boolean;
|
|
34
37
|
/** 纯本地额度判断,默认检查 1 次;考虑月额度和积分。force=true 只查积分,免费无限仍直接通过。 */
|
|
@@ -28,6 +28,18 @@ export interface UserStatus {
|
|
|
28
28
|
/** 月额度上限;值为 -1 时对外额度 getter 用 null 表示无限。 */
|
|
29
29
|
monthly_usage_limit: number;
|
|
30
30
|
};
|
|
31
|
+
/** 账户绑定的 Canva 用户 ID;后端旧账户可能缺失。 */
|
|
32
|
+
canva_id?: string;
|
|
33
|
+
/** 账户创建时间(秒)。 */
|
|
34
|
+
created_at?: number;
|
|
35
|
+
/** 后端按 timezone 计算的下次月用量重置时间(`Y-m-d H:i:s`);无订阅或未知时可能为 null。 */
|
|
36
|
+
nextUsageResetTime?: string | null;
|
|
37
|
+
/**
|
|
38
|
+
* 后端按产品渠道下发的审核开关(usa `params.apps.<渠道>.review`,未配置为 false)。
|
|
39
|
+
* true:应用处于 Canva 审核期;false:正常运行;缺失:旧后端或旧账户。
|
|
40
|
+
* 支付链接按 `review === false || canAcceptPayments` 显示,缺失不能当作 false。SDK 不据此改变任何行为。
|
|
41
|
+
*/
|
|
42
|
+
review?: boolean;
|
|
31
43
|
};
|
|
32
44
|
/** 个人订阅和额度计数;字段保留后端命名,避免把积分余额和月用量混为一谈。 */
|
|
33
45
|
subscription?: {
|
|
@@ -23,7 +23,7 @@ declare function getUserIds(token: string, appId: string, context: CommonRequest
|
|
|
23
23
|
declare function handleUserLoginByCanvaId(userId: string, productName: string, context: CommonRequestContext, options?: AccountRequestOptions): Promise<Record<string, unknown>>;
|
|
24
24
|
/**
|
|
25
25
|
* 获取账户服务 passport,供 status 和消费请求的 X-Access-Ticket 使用。
|
|
26
|
-
*
|
|
26
|
+
* 优先读取本地缓存(存储被禁用时退回本页面内存);缓存键历史上只包含 Canva userId,不包含 productName。
|
|
27
27
|
* 此处不主动检查过期;后续 status 缺少 profile 时,getUserProfile 会删除缓存再申请。
|
|
28
28
|
*
|
|
29
29
|
* imgkits 使用不带产品前缀的历史路径,其余产品使用产品前缀。
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { CommonRequestContext } from '../api/request';
|
|
2
|
+
import { ApiRequestOptions } from '../api/types';
|
|
2
3
|
/** 远程配置中的应用数字版本,用于 Provider 判断审核/测试阶段,不是 npm 包的语义版本。 */
|
|
3
4
|
export interface AppVersion {
|
|
4
|
-
version
|
|
5
|
+
version?: number;
|
|
5
6
|
}
|
|
6
7
|
/**
|
|
7
8
|
* 从账户服务站点配置读取版本;Provider 使用 canva_<appId> 作为 key。
|
|
8
9
|
* success=false 时运行时返回空对象,Provider 会对缺少数字 version 的情况按非测试处理。
|
|
9
|
-
*
|
|
10
|
+
* 最长等待 10 秒,包含读取正文;支持 Provider 切换应用时取消。失败不重试、不缓存。
|
|
10
11
|
*/
|
|
11
|
-
export declare function getAppVersion(key: string, context?: CommonRequestContext): Promise<AppVersion>;
|
|
12
|
+
export declare function getAppVersion(key: string, context?: CommonRequestContext, options?: ApiRequestOptions): Promise<AppVersion>;
|