yichong-canva2 2.5.1 → 2.7.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 +107 -0
- package/dist/api/error.js +28 -2
- package/dist/api/predictions.js +7 -2
- package/dist/api/reporting.js +8 -4
- package/dist/api/request.js +11 -5
- package/dist/api/telemetry/client.js +281 -0
- package/dist/api/telemetry/map_legacy.js +68 -0
- package/dist/api/telemetry/sanitize.js +108 -0
- package/dist/api/telemetry/stage.js +74 -0
- package/dist/api/usage_reporting.js +9 -5
- package/dist/index.js +13 -1
- package/dist/login/context.js +3 -0
- package/dist/login/pricing_unit.js +48 -0
- package/dist/login/privider.js +82 -4
- package/dist/types/api/error.d.ts +25 -1
- package/dist/types/api/index.d.ts +3 -2
- package/dist/types/api/reporting.d.ts +3 -1
- package/dist/types/api/request.d.ts +6 -2
- package/dist/types/api/telemetry/client.d.ts +34 -0
- package/dist/types/api/telemetry/index.d.ts +2 -0
- package/dist/types/api/telemetry/map_legacy.d.ts +14 -0
- package/dist/types/api/telemetry/sanitize.d.ts +11 -0
- package/dist/types/api/telemetry/stage.d.ts +34 -0
- package/dist/types/api/telemetry/types.d.ts +86 -0
- package/dist/types/api/types.d.ts +21 -6
- package/dist/types/api/usage_reporting.d.ts +3 -1
- package/dist/types/login/context.d.ts +12 -3
- package/dist/types/login/index.d.ts +2 -0
- package/dist/types/login/pricing_unit.d.ts +15 -0
- package/dist/types/login/privider.d.ts +8 -1
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ReportStage } from './types';
|
|
2
|
+
/** stage 词表,与 01-事件格式.md 第 4.2 节一致。 */
|
|
3
|
+
export declare const REPORT_STAGES: readonly ReportStage[];
|
|
4
|
+
export declare function isReportStage(value: unknown): value is ReportStage;
|
|
5
|
+
/**
|
|
6
|
+
* 携带失败步骤和错误码的错误,供生成流程抛给 app.tsx 记录 generate_fail。
|
|
7
|
+
* 只是数据载体,不上报、不重试;cause 保留原始错误,isAbortError(cause) 仍可识别取消。
|
|
8
|
+
*/
|
|
9
|
+
export declare class StageError extends Error {
|
|
10
|
+
readonly stage: ReportStage;
|
|
11
|
+
readonly code: string;
|
|
12
|
+
readonly expected?: boolean;
|
|
13
|
+
readonly cause: unknown;
|
|
14
|
+
constructor(cause: unknown, options: {
|
|
15
|
+
stage: ReportStage;
|
|
16
|
+
code?: string;
|
|
17
|
+
expected?: boolean;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 按 01 第 5 节的顺序推断错误码:StageError → 后端 error_code → SDK / Canva 的字符串错误码转大写
|
|
22
|
+
* → <STAGE>_FAILED → UNKNOWN。数字业务码不当作错误码(放 backend_code),避免后端自由文本进入分组键。
|
|
23
|
+
* 后端的 content_policy_violation 记为 POLICY_BLOCKED(generate_blocked,不告警),应用不用再匹配 E005。
|
|
24
|
+
*/
|
|
25
|
+
export declare function toErrorCode(error: unknown, stage?: ReportStage): string;
|
|
26
|
+
/**
|
|
27
|
+
* 记录每一步耗时:timer.run("upload", () => client.uploadFile(...))。
|
|
28
|
+
* 失败的步骤同样记录耗时;同一步骤多次执行时累加。只计时,不改变返回值或异常。
|
|
29
|
+
*/
|
|
30
|
+
export declare function createStageTimer(now?: () => number): {
|
|
31
|
+
timings: Partial<Record<ReportStage, number>>;
|
|
32
|
+
run<T>(stage: ReportStage, task: () => Promise<T> | T): Promise<T>;
|
|
33
|
+
add: (stage: ReportStage, ms: number) => void;
|
|
34
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 新通道(schema canva.v1)的公开类型。格式定义见 new_canva/docs/canva-telemetry/01-事件格式.md。
|
|
3
|
+
* 新通道与旧 GET 统计、飞书错误上报并行,默认关闭;这里的类型不改变旧接口的任何行为。
|
|
4
|
+
*/
|
|
5
|
+
/** 失败、耗时所属的步骤;应用不另起名字,子流程用 flow 区分。 */
|
|
6
|
+
export type ReportStage = "input_read" | "selection_read" | "upload" | "text_check" | "content_extract" | "chat" | "create_prediction" | "wait_prediction" | "cancel_prediction" | "tts" | "vtt" | "direct_api" | "parse_result" | "download_result" | "local_render" | "insert" | "consume" | "render" | "account" | "unknown";
|
|
7
|
+
/** 扁平属性:只允许字符串、数字、布尔和 null。不放用户输入、提示词、URL、文件名或凭据。 */
|
|
8
|
+
export type TelemetryProps = Record<string, string | number | boolean | null>;
|
|
9
|
+
/** 每一步的耗时(毫秒),键只能取 ReportStage。 */
|
|
10
|
+
export type TelemetryTimings = Partial<Record<ReportStage, number>>;
|
|
11
|
+
/** 应用传入的错误描述;SDK 从 cause 提取 name / message / stack 并脱敏。 */
|
|
12
|
+
export interface TelemetryErrorInput {
|
|
13
|
+
/** 大写蛇形错误码;省略时按 toErrorCode(cause, stage) 推断。 */
|
|
14
|
+
code?: string;
|
|
15
|
+
/** 失败所在步骤。 */
|
|
16
|
+
stage: ReportStage;
|
|
17
|
+
/** 是否在预料之中;省略时 SDK 自动判断(取消、审核拦截、Canva not_found 等为 true,其余为 false)。 */
|
|
18
|
+
expected?: boolean;
|
|
19
|
+
/** 原始错误对象,可以是 Error、ApiError、StageError 或字符串。 */
|
|
20
|
+
cause?: unknown;
|
|
21
|
+
}
|
|
22
|
+
/** client.track 的参数:只进新通道。 */
|
|
23
|
+
export interface TrackParams {
|
|
24
|
+
/** 小写蛇形,"对象_动作",例如 quota_blocked、example_click。 */
|
|
25
|
+
name: string;
|
|
26
|
+
/** 同一次生成共用的操作 ID。 */
|
|
27
|
+
operation_id?: string;
|
|
28
|
+
/** 子流程名,默认 main。 */
|
|
29
|
+
flow?: string;
|
|
30
|
+
/** 当前页面:index / progress / result 或应用自定义页面名。 */
|
|
31
|
+
page?: string;
|
|
32
|
+
props?: TelemetryProps;
|
|
33
|
+
timings?: TelemetryTimings;
|
|
34
|
+
duration_ms?: number;
|
|
35
|
+
error?: TelemetryErrorInput;
|
|
36
|
+
}
|
|
37
|
+
/** Provider 的 telemetry 属性。 */
|
|
38
|
+
export interface TelemetryConfig {
|
|
39
|
+
/** 默认 false:不发送任何新通道请求,旧通道不受影响。 */
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
/** 默认 https://telemetry.imgkits.com/v1/events;只接受 https,本地调试允许 http://localhost。 */
|
|
42
|
+
endpoint?: string;
|
|
43
|
+
}
|
|
44
|
+
/** 发送到采集 Worker 的单条事件(信封 + 事件 + 错误)。 */
|
|
45
|
+
export interface TelemetryEvent {
|
|
46
|
+
schema: "canva.v1";
|
|
47
|
+
event_id: string;
|
|
48
|
+
occurred_at: string;
|
|
49
|
+
seq: number;
|
|
50
|
+
app: {
|
|
51
|
+
id: string;
|
|
52
|
+
channel: string;
|
|
53
|
+
version: string;
|
|
54
|
+
sdk: string;
|
|
55
|
+
};
|
|
56
|
+
user: {
|
|
57
|
+
id: string | null;
|
|
58
|
+
plan: "free" | "subscriber" | "unlimited" | "unknown";
|
|
59
|
+
};
|
|
60
|
+
session: {
|
|
61
|
+
id: string;
|
|
62
|
+
locale: string;
|
|
63
|
+
};
|
|
64
|
+
env: "production" | "review" | "dev";
|
|
65
|
+
name: string;
|
|
66
|
+
operation_id?: string;
|
|
67
|
+
flow: string;
|
|
68
|
+
page?: string;
|
|
69
|
+
props: TelemetryProps;
|
|
70
|
+
timings?: TelemetryTimings;
|
|
71
|
+
duration_ms?: number;
|
|
72
|
+
error?: TelemetryErrorPayload;
|
|
73
|
+
repeat_count: number;
|
|
74
|
+
dropped_props: number;
|
|
75
|
+
}
|
|
76
|
+
export interface TelemetryErrorPayload {
|
|
77
|
+
code: string;
|
|
78
|
+
stage: ReportStage;
|
|
79
|
+
expected: boolean;
|
|
80
|
+
name?: string;
|
|
81
|
+
message?: string;
|
|
82
|
+
stack?: string;
|
|
83
|
+
http_status?: number;
|
|
84
|
+
backend_code?: string;
|
|
85
|
+
request_id?: string;
|
|
86
|
+
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* 后端 API 的公开参数、响应与方法签名。
|
|
3
|
-
* 在 YiChongCanvaProvider 子组件中用 useYiChongCanva() 获取 client,直接调用同名方法。
|
|
4
|
-
* 身份、渠道、语言等公共信息由 Provider 提供,不需要业务额外封装一层转发 API。
|
|
5
|
-
* 各服务保留自己的响应结构:AI 用 code,账户消费用 ok,上传和上报另有对应类型。
|
|
6
|
-
*/
|
|
1
|
+
import { TelemetryErrorInput, TelemetryTimings, TrackParams } from './telemetry/types';
|
|
7
2
|
/** 可完整序列化的 JSON 值;运行时还会拒绝 NaN、Infinity、循环引用及类实例。 */
|
|
8
3
|
export type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
|
|
9
4
|
/** 模型参数等开放对象;可选字段应省略,不要在对象内部放 undefined、File 或函数。 */
|
|
@@ -138,6 +133,12 @@ export interface Prediction {
|
|
|
138
133
|
error?: string | null;
|
|
139
134
|
/** 部分后端返回的补充错误文案。 */
|
|
140
135
|
error_msg?: string | null;
|
|
136
|
+
/** 失败任务的统一错误码(msg 分级打开后才有);有它时 error_msg / error 已是用户文案。2.7.0 起。 */
|
|
137
|
+
error_code?: string;
|
|
138
|
+
/** 改写前的 error 原文,只用于排查,不要展示。2.7.0 起。 */
|
|
139
|
+
raw_error?: string | null;
|
|
140
|
+
/** 改写前的 error_msg 原文,只用于排查,不要展示。2.7.0 起。 */
|
|
141
|
+
raw_error_msg?: string | null;
|
|
141
142
|
/** 后端附加的平台标识。 */
|
|
142
143
|
$platform?: string;
|
|
143
144
|
/** 后端附加的模型标识。 */
|
|
@@ -611,6 +612,20 @@ export interface ReportUsageParams {
|
|
|
611
612
|
* 对象最多 3 层、30 个字段,数组最多 10 项,字符串最多 256 字符;最终 URL 最多 8192 字符。
|
|
612
613
|
*/
|
|
613
614
|
data?: Record<string, unknown> | null;
|
|
615
|
+
/** 2.6.0 起,只进新通道(旧 GET 统计忽略):子流程名,默认 main。 */
|
|
616
|
+
flow?: string;
|
|
617
|
+
/** 2.6.0 起,只进新通道:每一步耗时(毫秒),键取 ReportStage。 */
|
|
618
|
+
timings?: TelemetryTimings;
|
|
619
|
+
/** 2.6.0 起,只进新通道:失败事件的错误描述。code 为 POLICY_BLOCKED 的 generate_fail 在新通道记为 generate_blocked。 */
|
|
620
|
+
error?: TelemetryErrorInput;
|
|
621
|
+
}
|
|
622
|
+
/** 新通道(canva.v1)专用的上报方法;默认关闭,见 Provider 的 telemetry 属性。 */
|
|
623
|
+
export interface TelemetryApi {
|
|
624
|
+
/**
|
|
625
|
+
* 只发到新通道:标准新增事件(如 quota_blocked)和应用自定义事件。
|
|
626
|
+
* 同步返回、永不抛错,不影响旧统计和飞书错误上报;新通道关闭时直接忽略。
|
|
627
|
+
*/
|
|
628
|
+
track(params: TrackParams): void;
|
|
614
629
|
}
|
|
615
630
|
/** 行为统计,不扣费、不刷新账户、不弹错误,也不会把自身失败转发到飞书。 */
|
|
616
631
|
export interface UsageReportingApi {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CommonRequestContext } from './request';
|
|
2
|
-
import { UsageReportingApi, UsageReportingConfig } from './types';
|
|
2
|
+
import { ReportUsageParams, UsageReportingApi, UsageReportingConfig } from './types';
|
|
3
3
|
/** Provider 当前快照;账户尚未就绪也可调用,不通过登录校验或账户消费 API。 */
|
|
4
4
|
export interface UsageReportingContext extends CommonRequestContext {
|
|
5
5
|
usageReporting?: UsageReportingConfig;
|
|
@@ -7,6 +7,8 @@ export interface UsageReportingContext extends CommonRequestContext {
|
|
|
7
7
|
metadata?: Record<string, unknown>;
|
|
8
8
|
/** 已知凭据只参与脱敏,绝不发送至统计接口。 */
|
|
9
9
|
secrets?: readonly string[];
|
|
10
|
+
/** 2.6.0:把同一次调用登记到新通道。同步、不抛错,不影响下面旧 GET 的任何行为。 */
|
|
11
|
+
capture?: (params: ReportUsageParams) => void;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
* 兼容已有图片 GET 统计接口,改用 fetch 读取 HTTP 确认以便做有限重试。
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { AccountError } from './account';
|
|
3
3
|
import { UseResult } from './consumption';
|
|
4
|
-
import {
|
|
4
|
+
import { PricingUnit } from './pricing_unit';
|
|
5
|
+
import { ChatApi, ContentExtractionApi, ErrorReportingApi, PredictionApi, TelemetryApi, TextModerationApi, TextToSpeechApi, UploadApi, UsageApi, UsageReportingApi, VttApi } from '../api/types';
|
|
5
6
|
import { canUseError, CheckoutSessionOptions, TestMode, UserStatus } from './interface';
|
|
6
7
|
/**
|
|
7
8
|
* `useYiChongCanva()` 返回的客户端:账户状态、历史额度方法和后端同名 API 共用一个 Provider。
|
|
@@ -9,7 +10,7 @@ import { canUseError, CheckoutSessionOptions, TestMode, UserStatus } from './int
|
|
|
9
10
|
* `userStatus` 等字段随 React 渲染更新;后端 API 则在每次调用时读取 Provider 的最新身份引用。
|
|
10
11
|
* use() 返回不会 reject 的消费结果;同名消费 API 仍保留原始响应并以 ApiError 表达失败。
|
|
11
12
|
*/
|
|
12
|
-
export interface YiChongCanvaContextType extends PredictionApi, TextModerationApi, ContentExtractionApi, ChatApi, VttApi, TextToSpeechApi, UsageApi, UploadApi, ErrorReportingApi, UsageReportingApi {
|
|
13
|
+
export interface YiChongCanvaContextType extends PredictionApi, TextModerationApi, ContentExtractionApi, ChatApi, VttApi, TextToSpeechApi, UsageApi, UploadApi, ErrorReportingApi, UsageReportingApi, TelemetryApi {
|
|
13
14
|
/** 当前账户快照;登录前为 `{}`。Canva 用户 ID 是 userId,系统用户 ID 是 profile.id。 */
|
|
14
15
|
userStatus: UserStatus;
|
|
15
16
|
/** 深度合并本地账户快照,并同步 API 使用的引用;不请求服务器,也不触发 onUpdateUserStatus。 */
|
|
@@ -30,8 +31,11 @@ export interface YiChongCanvaContextType extends PredictionApi, TextModerationAp
|
|
|
30
31
|
* 同一账户的连续调用串行执行、使用最新余额;切换账户后旧队列不继续发新请求。
|
|
31
32
|
* notUpload 仅阻止积分扣费请求,订阅月用量仍会发送。结果明确区分全成功、跳过、失败、部分成功和未知。
|
|
32
33
|
* Promise 始终 resolve;旧应用可继续忽略返回值。partial 的 charged=true 仅表示部分已确认。
|
|
34
|
+
* 2.6.0:options.operationId 把结果关联到本次生成,新通道开启时自动发送 consume_result;不改变扣费行为。
|
|
33
35
|
*/
|
|
34
|
-
use: (cnt?: number, force?: boolean, notUpload?: boolean
|
|
36
|
+
use: (cnt?: number, force?: boolean, notUpload?: boolean, options?: {
|
|
37
|
+
operationId?: string;
|
|
38
|
+
}) => Promise<UseResult>;
|
|
35
39
|
/** 旧的纯本地判断:订阅用户在 force=false 时直接通过,不考虑月限额;force=true 只检查积分。 */
|
|
36
40
|
canUse: (cnt?: number, force?: boolean) => boolean;
|
|
37
41
|
/** 纯本地额度判断,默认检查 1 次;考虑月额度和积分。force=true 只查积分,免费无限仍直接通过。 */
|
|
@@ -70,6 +74,11 @@ export interface YiChongCanvaContextType extends PredictionApi, TextModerationAp
|
|
|
70
74
|
loginStatus: "success" | "error" | "loading" | "idle";
|
|
71
75
|
/** 最近一次账户登录/刷新/支付检查失败;开始重试或成功后清除。自动上报不依赖 Footer 挂载。 */
|
|
72
76
|
accountError: AccountError | null;
|
|
77
|
+
/**
|
|
78
|
+
* 2.7.0:定价语言包里的计量单位,按 productName 和界面语言(主语言,没有退 en)读取。
|
|
79
|
+
* 读取中或拿不到时为 null,应用退回 config.ts 的 unitConfig;改定价单位不需要前端发版。
|
|
80
|
+
*/
|
|
81
|
+
pricingUnit: PricingUnit | null;
|
|
73
82
|
/** 手动启动登录,参数是失败后的重试次数(默认 1,另有首次请求);通过状态而非 Promise 获取结果。 */
|
|
74
83
|
manualLogin: (_retry?: number) => void;
|
|
75
84
|
}
|
|
@@ -5,3 +5,5 @@ export * from './context';
|
|
|
5
5
|
export * from './use_canva';
|
|
6
6
|
export type { AccountError, AccountStage, AccountOperation } from './account';
|
|
7
7
|
export type { UseResult, UseChannel, UseError, UseSkipReason, ConfirmedUsage } from './consumption';
|
|
8
|
+
export { loadPricingUnit, PRICING_LANGUAGE_BASE_URL } from './pricing_unit';
|
|
9
|
+
export type { PricingUnit } from './pricing_unit';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 定价语言包里的计量单位(2.7.0 起)。Footer / QuotaAlert 优先用它,拿不到时用应用 config.ts 的 unitConfig。
|
|
3
|
+
* 来源与定价页同一份文件,改了定价单位不用前端发版;locale 是实际取到的语言包(例如 zh、en)。
|
|
4
|
+
*/
|
|
5
|
+
export interface PricingUnit {
|
|
6
|
+
unitNoun: string;
|
|
7
|
+
unitNounPlural: string;
|
|
8
|
+
locale: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const PRICING_LANGUAGE_BASE_URL = "https://project.deletetweets.ai/yichong_pricing_language";
|
|
11
|
+
/**
|
|
12
|
+
* 先取界面语言的主语言(zh-TW → zh、pt-BR → pt),没有再退到 en;都拿不到返回 null。
|
|
13
|
+
* 404、断网、超时、格式不对都只返回 null,不抛错、不上报:部分渠道本来就没有定价语言包。
|
|
14
|
+
*/
|
|
15
|
+
export declare function loadPricingUnit(channel: string, locale: string, signal?: AbortSignal): Promise<PricingUnit | null>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { Mode, TestMode, UserStatus } from './interface';
|
|
3
3
|
import { UsageReportingConfig } from '../api/types';
|
|
4
|
+
import { TelemetryConfig } from '../api/telemetry/types';
|
|
4
5
|
/**
|
|
5
6
|
* 应用入口的账户与 API 配置。不同服务有独立根地址,修改其中一个不会自动改写其他服务地址。
|
|
6
7
|
* productName 应使用后端登记的应用渠道名称,而不是界面中可随意修改的标题。
|
|
@@ -34,6 +35,12 @@ export interface YiChongCanvaProviderProps {
|
|
|
34
35
|
* 自动补充当前身份、会话及应用发布号,登录前可发送;不读取或上报业务输入、结果正文。
|
|
35
36
|
*/
|
|
36
37
|
usageReporting?: UsageReportingConfig;
|
|
38
|
+
/**
|
|
39
|
+
* 2.6.0:新通道(canva.v1),默认关闭。开启后 reportUsage / reportError / track / use 的结果
|
|
40
|
+
* 额外发一份到采集 Worker;旧 GET 统计和飞书错误上报的行为完全不变。
|
|
41
|
+
* usageReporting.enabled=false 时新通道同样关闭。
|
|
42
|
+
*/
|
|
43
|
+
telemetry?: TelemetryConfig;
|
|
37
44
|
/** 默认 true,挂载后自动登录;false 时由 client.handleLogin/manualLogin 显式启动。 */
|
|
38
45
|
autoLogin?: boolean;
|
|
39
46
|
/** 初始模拟模式,默认 noTest;挂载 effect 会将开发环境设为 credits、其他环境设为 noTest。 */
|
|
@@ -67,4 +74,4 @@ export interface YiChongCanvaProviderProps {
|
|
|
67
74
|
* // App 或其子组件内:const client = useYiChongCanva();
|
|
68
75
|
* // 账号就绪后,直接调用 client.createSpecifyPrediction({ owner, name, input })。
|
|
69
76
|
*/
|
|
70
|
-
export declare const YiChongCanvaProvider: ({ children, appId, productName, onUpdateUserStatus, baseUrl, apiBaseUrl, contentBaseUrl, chatBaseUrl, ttsBaseUrl, uploadBaseUrl, getLocale, usageReporting, autoLogin, testMode: _testMode, version, mode, retry, maxAvailablePoints, }: YiChongCanvaProviderProps) => React.JSX.Element;
|
|
77
|
+
export declare const YiChongCanvaProvider: ({ children, appId, productName, onUpdateUserStatus, baseUrl, apiBaseUrl, contentBaseUrl, chatBaseUrl, ttsBaseUrl, uploadBaseUrl, getLocale, usageReporting, telemetry: telemetryConfig, autoLogin, testMode: _testMode, version, mode, retry, maxAvailablePoints, }: YiChongCanvaProviderProps) => React.JSX.Element;
|