@open-dy/interest-card-cli 0.1.0-beta.29 → 0.1.0-beta.30

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.
@@ -78,6 +78,164 @@ export function telemetryLoginMethod(parsed) {
78
78
  }
79
79
  return parsed.options.qr ? 'qr' : 'link';
80
80
  }
81
+ const ERROR_MESSAGE_MAX_CHARS = 300;
82
+ const ERROR_DETAIL_MAX_CHARS = 200;
83
+ const ERROR_TOTAL_MAX_CHARS = 1000;
84
+ const LOGID_PATTERN = /^[A-Za-z0-9]{8,64}$/;
85
+ /** Applied in order. What is left is for finding the cause, not for reading someone's machine. */
86
+ const ERROR_MESSAGE_REDACTIONS = [
87
+ // Credentials: CLI tokens, bearer headers, cookie pairs, JSON fields, then any other long opaque string.
88
+ [/\bict\.[\w.-]+/g, 'ict.<redacted>'],
89
+ [/\bBearer\s+\S+/gi, 'Bearer <redacted>'],
90
+ [/\b([\w-]*(?:session|sid|token|passport|secret|cookie)[\w-]*)=[^;\s&"']+/gi, '$1=<redacted>'],
91
+ [
92
+ /(["']?[\w-]*(?:session|sid|token|passport|secret|cookie|password)[\w-]*["']?\s*:\s*)(?:"[^"]*"|'[^']*'|[^\s,}]+)/gi,
93
+ '$1<redacted>',
94
+ ],
95
+ [/[A-Za-z0-9+/_-]{40,}={0,2}/g, '<redacted>'],
96
+ [/[\w.+-]+@[\w-]+(?:\.[\w-]+)+/g, '<email>'],
97
+ // A query string can carry anything; the path is enough to tell which request failed.
98
+ [/(https?:\/\/[^\s?#"']+)[?#][^\s"']*/g, '$1'],
99
+ ];
100
+ /**
101
+ * `CliError.details` fields worth reporting, in the order they are written. Only these are read:
102
+ * `hint` is fixed text, `projectId` has its own param, and `data` / `response` / `stdout` can hold
103
+ * whatever the server or a child process answered.
104
+ */
105
+ const ERROR_DETAIL_KEYS = [
106
+ 'status',
107
+ 'code',
108
+ 'error',
109
+ 'errorCode',
110
+ 'rpcCode',
111
+ 'causeCode',
112
+ 'exitCode',
113
+ 'exit',
114
+ 'method',
115
+ 'route',
116
+ 'path',
117
+ 'url',
118
+ 'op',
119
+ 'command',
120
+ 'registry',
121
+ 'contentType',
122
+ 'redirected',
123
+ 'timeoutSeconds',
124
+ 'timeoutMs',
125
+ 'retryAfterSeconds',
126
+ 'reason',
127
+ 'reasons',
128
+ 'message',
129
+ 'msg',
130
+ 'cause',
131
+ 'stderr',
132
+ ];
133
+ /** A failed response's JSON body: only what says why it failed. */
134
+ const ERROR_BODY_KEYS = ['code', 'error', 'error_code', 'err_no', 'status_code', 'message', 'msg', 'err_msg'];
135
+ function cleanErrorText(text, home, maxChars, keepEnd = false) {
136
+ let cleaned = text.replace(/\s+/g, ' ').trim();
137
+ if (home && home !== path.sep) {
138
+ cleaned = cleaned.split(home).join('~');
139
+ }
140
+ cleaned = cleaned.replace(/([A-Za-z]:\\Users\\|\/Users\/|\/home\/)[^\s\\/"']+/g, '$1<user>');
141
+ for (const [pattern, replacement] of ERROR_MESSAGE_REDACTIONS) {
142
+ cleaned = cleaned.replace(pattern, replacement);
143
+ }
144
+ if (cleaned.length <= maxChars) {
145
+ return cleaned;
146
+ }
147
+ // A child's stderr says what went wrong at the end, a message at the start.
148
+ return keepEnd ? `…${cleaned.slice(-maxChars)}` : `${cleaned.slice(0, maxChars)}…`;
149
+ }
150
+ function detailText(value) {
151
+ if (typeof value === 'string') {
152
+ return value;
153
+ }
154
+ if (typeof value === 'number' || typeof value === 'boolean') {
155
+ return String(value);
156
+ }
157
+ if (value instanceof Error) {
158
+ return value.message;
159
+ }
160
+ if (Array.isArray(value)) {
161
+ return value.map(detailText).filter(Boolean).join('; ');
162
+ }
163
+ return '';
164
+ }
165
+ function bodyDetails(body) {
166
+ if (typeof body !== 'string' || !body.trim().startsWith('{')) {
167
+ return [];
168
+ }
169
+ try {
170
+ const parsed = JSON.parse(body);
171
+ return ERROR_BODY_KEYS.map(key => [`body.${key}`, detailText(parsed[key])]);
172
+ }
173
+ catch {
174
+ // Cut or not JSON: nothing in it can be told apart from the rest.
175
+ return [];
176
+ }
177
+ }
178
+ /** Node's own failures: `ENOENT`, `EACCES`, and what `fetch failed` hides in `cause`. */
179
+ function nativeErrorDetails(error) {
180
+ const pairs = [];
181
+ if (!(error instanceof Error)) {
182
+ return pairs;
183
+ }
184
+ if (!(error instanceof CliError)) {
185
+ if (error.name && error.name !== 'Error') {
186
+ pairs.push(['name', error.name]);
187
+ }
188
+ pairs.push(['code', detailText(error.code)]);
189
+ pairs.push(['syscall', detailText(error.syscall)]);
190
+ }
191
+ const { cause } = error;
192
+ if (cause instanceof Error) {
193
+ pairs.push(['cause', cause.message]);
194
+ pairs.push(['causeCode', detailText(cause.code)]);
195
+ }
196
+ else {
197
+ pairs.push(['cause', detailText(cause)]);
198
+ }
199
+ return pairs;
200
+ }
201
+ /**
202
+ * The failed command's message for `err_msg`, with what the error knows about why: HTTP status,
203
+ * server / RPC / exit codes, the request path, the underlying cause, the end of a child's stderr,
204
+ * and the server's log id. One line, without the home directory, credentials or emails, every
205
+ * part cut to a fixed length. Messages are written by the CLI or answered by the server; the
206
+ * creator's prompt is never part of one.
207
+ */
208
+ export function telemetryErrorMessage(error, home = os.homedir()) {
209
+ const message = cleanErrorText(error instanceof Error ? error.message : typeof error === 'string' ? error : '', home, ERROR_MESSAGE_MAX_CHARS);
210
+ const details = error instanceof CliError && typeof error.details === 'object' && error.details !== null
211
+ ? error.details
212
+ : {};
213
+ const pairs = [
214
+ ...ERROR_DETAIL_KEYS.map(key => [key, detailText(details[key])]),
215
+ ...bodyDetails(details.body),
216
+ ...nativeErrorDetails(error),
217
+ ];
218
+ const seen = new Set();
219
+ const parts = [];
220
+ for (const [key, raw] of pairs) {
221
+ const value = cleanErrorText(raw, home, ERROR_DETAIL_MAX_CHARS, key === 'stderr');
222
+ // The message often repeats a detail (the server's `msg`, an exit code): say it once.
223
+ if (!value || seen.has(key) || message.includes(value) && value.length > 3) {
224
+ continue;
225
+ }
226
+ seen.add(key);
227
+ parts.push(`${key}=${value}`);
228
+ }
229
+ const logid = typeof details.xTtLogid === 'string' && LOGID_PATTERN.test(details.xTtLogid)
230
+ ? details.xTtLogid
231
+ : '';
232
+ const tail = logid ? ` [logid=${logid}]` : '';
233
+ let text = [message, parts.join(' ')].filter(Boolean).join(' | ');
234
+ if (text.length > ERROR_TOTAL_MAX_CHARS - tail.length) {
235
+ text = `${text.slice(0, ERROR_TOTAL_MAX_CHARS - tail.length - 1)}…`;
236
+ }
237
+ return `${text}${tail}`.trim();
238
+ }
81
239
  export function telemetryErrorCode(error) {
82
240
  if (error instanceof CliError && ERROR_CODE_PATTERN.test(error.code)) {
83
241
  return error.code;
@@ -119,8 +277,7 @@ export function buildTelemetryJob(telemetry, identity, options = {}) {
119
277
  result: telemetry.result,
120
278
  duration_ms: Math.max(0, Math.round(telemetry.durationMs)),
121
279
  error_code: telemetry.errorCode || '',
122
- // Raw error messages can hold paths and prompts, so err_msg stays empty.
123
- err_msg: '',
280
+ err_msg: telemetry.errorMessage || '',
124
281
  website: WEBSITE_BY_PLATFORM[identity.platform || 'creator'],
125
282
  app_mode: INTEREST_CARD_APP_MODE,
126
283
  product_type: INTEREST_CARD_PRODUCT_TYPE,
@@ -173,6 +330,7 @@ export async function reportCommand(parsed, outcome) {
173
330
  command: telemetryCommandName(parsed),
174
331
  result: outcome.canceled ? 'canceled' : failed ? 'fail' : 'success',
175
332
  errorCode: failed ? telemetryErrorCode(outcome.error) : undefined,
333
+ errorMessage: failed ? telemetryErrorMessage(outcome.error) : undefined,
176
334
  durationMs: Date.now() - outcome.startedAt,
177
335
  json: Boolean(parsed.options.json),
178
336
  conversationId: project.conversationId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-dy/interest-card-cli",
3
- "version": "0.1.0-beta.29",
3
+ "version": "0.1.0-beta.30",
4
4
  "description": "Pull, edit and sync Douyin interest card projects locally with any AI agent",
5
5
  "private": false,
6
6
  "type": "module",