@wkgeo/cli 0.0.0-stage → 1.1.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +223 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1042 -3
  4. package/SKILL.md +927 -0
  5. package/lib/ai/client.mjs +161 -0
  6. package/lib/ai/config.mjs +221 -0
  7. package/lib/ai/context.mjs +468 -0
  8. package/lib/ai/log.mjs +53 -0
  9. package/lib/api.mjs +339 -0
  10. package/lib/args.mjs +235 -0
  11. package/lib/cli.mjs +168 -0
  12. package/lib/cms.mjs +330 -0
  13. package/lib/commands/ai.mjs +392 -0
  14. package/lib/commands/apps.mjs +116 -0
  15. package/lib/commands/auth.mjs +1288 -0
  16. package/lib/commands/billing.mjs +666 -0
  17. package/lib/commands/cms.mjs +2533 -0
  18. package/lib/commands/config-auth.mjs +621 -0
  19. package/lib/commands/config.mjs +434 -0
  20. package/lib/commands/dnscred.mjs +428 -0
  21. package/lib/commands/domainops.mjs +546 -0
  22. package/lib/commands/domainorder.mjs +439 -0
  23. package/lib/commands/import.mjs +164 -0
  24. package/lib/commands/index.mjs +541 -0
  25. package/lib/commands/logs.mjs +141 -0
  26. package/lib/commands/orders.mjs +444 -0
  27. package/lib/commands/overview.mjs +394 -0
  28. package/lib/commands/plugins.mjs +678 -0
  29. package/lib/commands/site.mjs +435 -0
  30. package/lib/commands/siteops.mjs +1012 -0
  31. package/lib/commands/sites.mjs +905 -0
  32. package/lib/commands/skill.mjs +119 -0
  33. package/lib/commands/template.mjs +258 -0
  34. package/lib/config.mjs +351 -0
  35. package/lib/credentials.mjs +160 -0
  36. package/lib/device-store.mjs +132 -0
  37. package/lib/device.mjs +464 -0
  38. package/lib/errors.mjs +299 -0
  39. package/lib/format.mjs +339 -0
  40. package/lib/http.mjs +71 -0
  41. package/lib/index.mjs +44 -0
  42. package/lib/local-import.mjs +185 -0
  43. package/lib/login.mjs +1638 -0
  44. package/lib/oauth-urls.mjs +163 -0
  45. package/lib/page.mjs +393 -0
  46. package/lib/pkce.mjs +48 -0
  47. package/lib/proxy.mjs +454 -0
  48. package/lib/qr.mjs +31 -0
  49. package/lib/runtime.mjs +650 -0
  50. package/lib/site-state.mjs +137 -0
  51. package/lib/tar.mjs +207 -0
  52. package/lib/template-package.mjs +522 -0
  53. package/package.json +53 -4
  54. package/wkgeo.mjs +17 -0
@@ -0,0 +1,394 @@
1
+ /**
2
+ * 站点总览 / 账号流量 / 控制台首页快照(B7 / task-92)。
3
+ *
4
+ * ## 这一层是什么
5
+ * task-92(2026-10-29)把 CMS `actionTable` 里剩下的 8 条"控制台类"动作搬进 CLI 自己的
6
+ * 云函数 `wkgeo-cli-fc`(`src/console-core.js`,逐字复制 CMS handler + 本地归属闸)。
7
+ * 本文件覆盖其中 3 条只读动作:
8
+ * * `overview summary` → `overview`(栏目 / 内容 / 草稿 / 素材数 + 存储用量)
9
+ * * `overview traffic` → `traffic`(**账号级**整月账单:ESA / OSS 流量与费用 + 账户余额)
10
+ * * `overview dashboard` → `getSiteDashboardSnapshot`(控制台首页那张卡:容量 / 流量 / 费用快照)
11
+ *
12
+ * ## 三条口径(与 B1 内容面 / B3 发布面 / B4 站点面 / B5 插件面完全一致)
13
+ * 1. **站点必须先定**:`--site` > `WKONGAI_SITE` > `wkgeo site use` 记住的那个;
14
+ * 一个都没有就当场报用法错误(`consoleCall` 统一挡,不多跑一次往返)。
15
+ * 2. **全部只读**:这里没有任何写动作,也不写审计。
16
+ * 3. **默认人类可读,`--json` 给原始数据**(信封由 `runtime.done()` 统一包)。
17
+ *
18
+ * ## 权限面(这两条与其余 6 条不同,务必看清)
19
+ * * `overview summary` ⇒ `site:read` + 站点归属(不是自己名下站 403 `not_site_owner`)。
20
+ * * `overview traffic` ⇒ 除 `site:read` 外,服务端还要求**平台管理员**:
21
+ * 账号级整月账单(按产品拆的用量/金额 + **账户余额**)属于平台成本数据,
22
+ * 普通用户一律 403「仅平台管理员可查看账号级流量与费用」——**在查缓存/调账单接口之前就拦**。
23
+ * * `overview dashboard` ⇒ 站点归属校验;其中 `traffic` / `cost` 两块**只对平台管理员**下发,
24
+ * 非管理员恒为 `null` 且 `account_restricted=true`(那不是"还没采到",是"不可见")。
25
+ *
26
+ * ## 别在这里做的事
27
+ * * ❌ 不走 `/cms` 转发面(B7 起这 3 条都改走 `/cli` 自持通道)
28
+ * * ❌ 不在 CLI 侧重算流量/费用口径:`usedScope:'day'` 表示"已用"只是**最近一条日快照那天**的
29
+ * 单日用量,不是整周期累计 —— 文案照服务端给的 `note` / `attributionNote` 转述,不自造
30
+ * * ❌ 不把 `null` 当 0:账号级数据受限时服务端给 `null`,CLI 也显示「不可见」而不是「0 GB」
31
+ */
32
+
33
+ /* ------------------------------------------------------------------ 公共小工具 */
34
+
35
+ /** 所有控制台类命令共用的 `--site`(默认取 `wkgeo site use` 选中的站点) */
36
+ const SITE_FLAG = {
37
+ site: {
38
+ type: 'string',
39
+ desc: '站点 ID(uuid;默认用 `wkgeo site use` 选中的站点,可用 WKONGAI_SITE 覆盖)',
40
+ },
41
+ };
42
+
43
+ function siteIdOf(ctx) {
44
+ const site = ctx.cmsSite();
45
+ return (site && site.id) || '';
46
+ }
47
+
48
+ function siteText(ctx) {
49
+ const site = ctx.cmsSite();
50
+ if (!site || !site.id) return '(未选站)';
51
+ return site.label ? `${site.label}(${site.id})` : String(site.id);
52
+ }
53
+
54
+ /** 服务端动作名(同时用于回执) */
55
+ const ACTION = {
56
+ overview: 'overview',
57
+ traffic: 'traffic',
58
+ getSiteDashboardSnapshot: 'getSiteDashboardSnapshot',
59
+ };
60
+
61
+ /** 字节 → 人类可读(1024 进位;非数字回 `—`,**不拿 0 冒充**) */
62
+ function fmtBytes(bytes) {
63
+ const n = Number(bytes);
64
+ if (!Number.isFinite(n) || n < 0) return '—';
65
+ if (n < 1024) return `${n} B`;
66
+ const units = ['KB', 'MB', 'GB', 'TB'];
67
+ let v = n / 1024;
68
+ let i = 0;
69
+ while (v >= 1024 && i < units.length - 1) {
70
+ v /= 1024;
71
+ i += 1;
72
+ }
73
+ return `${v.toFixed(v >= 100 ? 0 : v >= 10 ? 1 : 2)} ${units[i]}`;
74
+ }
75
+
76
+ /** GB 数(服务端已经算好,可能为 null ⇒ 显示「—」而不是 0) */
77
+ function fmtGb(gb) {
78
+ const n = Number(gb);
79
+ if (!Number.isFinite(n)) return '—';
80
+ return `${n.toFixed(n >= 100 ? 0 : n >= 10 ? 1 : 2)} GB`;
81
+ }
82
+
83
+ /** 金额:服务端是**厘**(`amount` 是元,`totalCents` 是分)—— 这里只格式元 */
84
+ function fmtMoney(yuan, currency = 'CNY') {
85
+ const n = Number(yuan);
86
+ if (!Number.isFinite(n)) return '—';
87
+ return `${currency === 'CNY' ? '¥' : `${currency} `}${n.toFixed(2)}`;
88
+ }
89
+
90
+ /* ══════════════════════════════ 站点概览(overview) ══════════════════════════════ */
91
+
92
+ export const overviewSummaryCommand = {
93
+ name: 'overview summary',
94
+ summary: '看本站总量(栏目 / 内容 / 已发布 / 草稿 / 素材 + 存储用量;只读)',
95
+ usage: 'wkgeo overview summary [--lang <语言>] [--site <站点ID>] [--json]',
96
+ args: [],
97
+ flags: {
98
+ 'lang': { type: 'string', desc: '按哪个语言出数(不写 = 站点默认语言;如 zh-CN / en)' },
99
+ ...SITE_FLAG,
100
+ },
101
+ examples: [
102
+ 'wkgeo overview summary',
103
+ 'wkgeo overview summary --lang en',
104
+ 'wkgeo overview summary --json',
105
+ ],
106
+ notes: [
107
+ '需要 `site:read`(只读)。',
108
+ '栏目数按该语言统计;`category_languages` 给出"每个栏目有哪些语言版本"(用来发现漏翻的栏目)。',
109
+ '`storage.month_upload_count` 是**本月**新增素材的条数与字节数(服务端按上传时间过滤)。',
110
+ '这是"当前站点数据",不含账号级流量/费用 —— 那些只给平台管理员(见 `overview traffic` / `overview dashboard`)。',
111
+ ],
112
+ async run(ctx) {
113
+ const { out } = ctx;
114
+ const body = {};
115
+ const lang = String(ctx.flags.lang || '').trim();
116
+ if (lang) body._lang = lang;
117
+
118
+ const res = (await ctx.consoleCall(ACTION.overview, body)) || {};
119
+ const storage = (res && res.storage) || {};
120
+ const month = (storage && storage.monthUploadCount) || {};
121
+
122
+ const data = {
123
+ site_id: siteIdOf(ctx),
124
+ site_name: String(res.siteName || ''),
125
+ language: String(res.language || ''),
126
+ categories: Number(res.categories || 0),
127
+ category_languages: res.categoryLanguages || null,
128
+ contents: Number(res.contents || 0),
129
+ published: Number(res.published || 0),
130
+ drafts: Number(res.drafts || 0),
131
+ assets: Number(res.assets || 0),
132
+ storage: {
133
+ count: Number(storage.count || 0),
134
+ bytes: storage.bytes === undefined ? null : Number(storage.bytes || 0),
135
+ by_kind: storage.byKind || null,
136
+ month_upload_count: {
137
+ count: Number(month.count || 0),
138
+ bytes: month.bytes === undefined ? null : Number(month.bytes || 0),
139
+ },
140
+ },
141
+ };
142
+
143
+ if (!out.json) {
144
+ out.head(`站点概览(${data.site_name || siteText(ctx)} · ${data.language || '默认语言'})`);
145
+ out.table(
146
+ [
147
+ { title: '项目', key: 'label', width: 14 },
148
+ { title: '数量', key: 'value', width: 12 },
149
+ { title: '说明', key: 'note', width: 34 },
150
+ ],
151
+ [
152
+ { label: '栏目', value: String(data.categories), note: '该语言下的栏目数' },
153
+ { label: '内容', value: String(data.contents), note: `其中已发布 ${data.published} / 草稿 ${data.drafts}` },
154
+ { label: '素材', value: String(data.assets), note: '素材库总条数' },
155
+ { label: '存储', value: fmtBytes(data.storage.bytes), note: `共 ${data.storage.count} 个文件` },
156
+ {
157
+ label: '本月上传',
158
+ value: String(data.storage.month_upload_count.count),
159
+ note: fmtBytes(data.storage.month_upload_count.bytes),
160
+ },
161
+ ],
162
+ );
163
+ if (data.contents && data.drafts) {
164
+ out.info(`草稿 ${data.drafts} 篇还没发布:\`wkgeo contents list --status 草稿\``);
165
+ }
166
+ out.info('账号级流量 / 费用只给平台管理员:`wkgeo overview traffic` / `wkgeo overview dashboard`');
167
+ }
168
+ ctx.done(data);
169
+ return data;
170
+ },
171
+ };
172
+
173
+ /* ══════════════════════════════ 账号级账单(traffic) ══════════════════════════════ */
174
+
175
+ /** 账单分项的中文名(服务端 `billingBreakdown[key].label` 是权威,这里只兜底) */
176
+ const BILL_LABEL = {
177
+ esaTraffic: 'ESA 分发流量',
178
+ ossOriginTraffic: 'OSS 回源流量',
179
+ ossInternetOut: 'OSS 外网流出',
180
+ storageCapacity: '存储容量',
181
+ other: '其它',
182
+ };
183
+
184
+ export const overviewTrafficCommand = {
185
+ name: 'overview traffic',
186
+ summary: '看账号级本月流量与费用(ESA / OSS 分项 + 账户余额;**仅平台管理员**)',
187
+ usage: 'wkgeo overview traffic [--refresh] [--site <站点ID>] [--json]',
188
+ args: [],
189
+ flags: {
190
+ refresh: { type: 'boolean', desc: '绕过 1 小时缓存,重新拉一次账单(服务端清缓存后再查)' },
191
+ ...SITE_FLAG,
192
+ },
193
+ examples: [
194
+ 'wkgeo overview traffic',
195
+ 'wkgeo overview traffic --refresh',
196
+ 'wkgeo overview traffic --json',
197
+ ],
198
+ notes: [
199
+ '需要 `site:read`,且**服务端要求平台管理员**:非管理员一律 403「仅平台管理员可查看账号级流量与费用」。',
200
+ '这是**整个账号**的整月账单(`Granularity=MONTHLY`),拆不到单个站点 —— 归因口径以 `attributionNote` 为准。',
201
+ '服务端缓存 1 小时(`from_cache=true` 就是缓存命中);`--refresh` 会让服务端先清缓存。',
202
+ '`balance` 是阿里云账户可用余额(元);取不到是 `null`,**不是 0**。',
203
+ ],
204
+ async run(ctx) {
205
+ const { out } = ctx;
206
+ const body = {};
207
+ if (ctx.flags.refresh) body.refresh = true;
208
+
209
+ const res = (await ctx.consoleCall(ACTION.traffic, body)) || {};
210
+ const breakdown = (res && res.billingBreakdown) || {};
211
+ const rows = Object.keys(breakdown).map((key) => {
212
+ const one = breakdown[key] || {};
213
+ return {
214
+ key,
215
+ label: String(one.label || BILL_LABEL[key] || key),
216
+ usage: Number(one.usage || 0),
217
+ unit: String(one.unit || ''),
218
+ amount: Number(one.amount || 0),
219
+ };
220
+ });
221
+
222
+ const data = {
223
+ site_id: siteIdOf(ctx),
224
+ billing_period: String(res.billingPeriod || ''),
225
+ sum: Number(res.sum || 0),
226
+ balance: res.balance === null || res.balance === undefined ? null : Number(res.balance),
227
+ raw_count: Number(res.rawCount || 0),
228
+ from_cache: res.fromCache === true,
229
+ breakdown: rows,
230
+ };
231
+
232
+ if (!out.json) {
233
+ out.head(`账号级本月流量与费用(账单月 ${data.billing_period || '—'}${data.from_cache ? ' · 缓存' : ''})`);
234
+ out.table(
235
+ [
236
+ { title: '项目', key: 'label', width: 18 },
237
+ { title: '用量', key: 'usage', width: 12 },
238
+ { title: '金额', key: 'amount', width: 12 },
239
+ ],
240
+ rows.map((one) => ({
241
+ label: one.label,
242
+ usage: one.unit ? `${one.usage} ${one.unit}` : '—',
243
+ amount: fmtMoney(one.amount),
244
+ })),
245
+ );
246
+ out.line(` 合计:${fmtMoney(data.sum)} 余额:${data.balance === null ? '—(取不到)' : fmtMoney(data.balance)}`);
247
+ out.line(` 账单明细 ${data.raw_count} 条(整月合计,不是单日)`);
248
+ out.info('拆不到单个站点:额度与用量都是账号级的(见服务端 attributionNote)');
249
+ }
250
+ ctx.done(data);
251
+ return data;
252
+ },
253
+ };
254
+
255
+ /* ══════════════════════════════ 控制台首页快照(dashboard) ══════════════════════════════ */
256
+
257
+ export const overviewDashboardCommand = {
258
+ name: 'overview dashboard',
259
+ summary: '看控制台首页那张卡(容量 / 流量 / 费用快照 + 待统计项;账号级两块只给平台管理员)',
260
+ usage: 'wkgeo overview dashboard [--site <站点ID>] [--json]',
261
+ args: [],
262
+ flags: { ...SITE_FLAG },
263
+ examples: ['wkgeo overview dashboard', 'wkgeo overview dashboard --json'],
264
+ notes: [
265
+ '需要 `site:read` + 站点归属;`req.body.siteId` 与令牌判定的站点不一致时服务端 403「无权查看其它站点」。',
266
+ '快照是**前一天 24:00** 的日快照(每天 01:00 采集、06:00 幂等补跑);`next_stat_date` 是下一次能看到的日期。',
267
+ '`traffic` / `cost` 两块**只对平台管理员**下发:非管理员是 `null` + `account_restricted=true`(不可见,不是没采到)。',
268
+ '`traffic.used_scope=day` ⇒ "已用"只是**那天**的单日用量,不是整周期累计;额度来自 ESA 套餐(实时、30 分钟缓存)。',
269
+ '流量额度 `traffic_quota.ok=false` 只表示 ESA 取不到(凭据 / 权限 / 超时),**不影响**概览其余部分。',
270
+ ],
271
+ async run(ctx) {
272
+ const { out } = ctx;
273
+ const res = (await ctx.consoleCall(ACTION.getSiteDashboardSnapshot)) || {};
274
+ const site = (res && res.site) || {};
275
+ const snap = (res && res.snapshot) || null;
276
+ const storage = (snap && snap.storage) || null;
277
+ const traffic = (snap && snap.traffic) || null;
278
+ const cost = (snap && snap.cost) || null;
279
+ const quota = (res && res.trafficQuota) || null;
280
+ const plan = (res && res.plan) || null;
281
+
282
+ const data = {
283
+ site_id: siteIdOf(ctx),
284
+ site: {
285
+ id: String(site.id || ''),
286
+ name: String(site.name || ''),
287
+ asset_storage_mode: String(site.assetStorageMode || ''),
288
+ },
289
+ plan: plan || null,
290
+ pending: res.pending === true,
291
+ pending_parts: Array.isArray(res.pendingParts) ? res.pendingParts : [],
292
+ account_restricted: res.accountRestricted === true,
293
+ account_restricted_note: String(res.accountRestrictedNote || ''),
294
+ next_stat_date: String(res.nextStatDate || ''),
295
+ schedule_note: String(res.scheduleNote || ''),
296
+ traffic_attribution_note: String(res.trafficAttributionNote || ''),
297
+ snapshot: snap
298
+ ? {
299
+ stat_date: String(snap.statDate || ''),
300
+ cutoff_label: String(snap.cutoffLabel || ''),
301
+ status: String(snap.status || ''),
302
+ error: snap.error || null,
303
+ updated_at: snap.updatedAt || null,
304
+ storage: storage
305
+ ? {
306
+ bytes: storage.bytes === null || storage.bytes === undefined ? null : Number(storage.bytes),
307
+ scope: String(storage.scope || ''),
308
+ source: String(storage.source || ''),
309
+ mode: String(storage.mode || ''),
310
+ status: String(storage.status || ''),
311
+ note: String(storage.note || ''),
312
+ }
313
+ : null,
314
+ traffic: traffic
315
+ ? {
316
+ used_gb: traffic.usedGb === null || traffic.usedGb === undefined ? null : Number(traffic.usedGb),
317
+ plan_traffic_gb:
318
+ traffic.planTrafficGb === null || traffic.planTrafficGb === undefined
319
+ ? null
320
+ : Number(traffic.planTrafficGb),
321
+ remaining_gb:
322
+ traffic.remainingGb === null || traffic.remainingGb === undefined
323
+ ? null
324
+ : Number(traffic.remainingGb),
325
+ used_scope: String(traffic.usedScope || ''),
326
+ shared: traffic.shared === true,
327
+ note: String(traffic.note || ''),
328
+ attribution_note: String(traffic.attributionNote || ''),
329
+ }
330
+ : null,
331
+ cost: cost
332
+ ? {
333
+ total_cents:
334
+ cost.totalCents === null || cost.totalCents === undefined ? null : Number(cost.totalCents),
335
+ currency: String(cost.currency || 'CNY'),
336
+ by_product: Array.isArray(cost.byProduct) ? cost.byProduct : [],
337
+ }
338
+ : null,
339
+ }
340
+ : null,
341
+ traffic_quota: quota
342
+ ? {
343
+ ok: quota.ok === true,
344
+ plan_traffic_gb:
345
+ quota.planTrafficGb === null || quota.planTrafficGb === undefined ? null : Number(quota.planTrafficGb),
346
+ cycle_start: quota.cycleStart || null,
347
+ cycle_end: quota.cycleEnd || null,
348
+ error: quota.error || null,
349
+ }
350
+ : null,
351
+ };
352
+
353
+ if (!out.json) {
354
+ out.head(`控制台首页快照(${data.site.name || siteText(ctx)})`);
355
+ if (data.pending || !data.snapshot) {
356
+ out.warn(
357
+ `还没有快照(待统计:${data.pending_parts.length ? data.pending_parts.join(' / ') : '未说明'}),` +
358
+ `下一次能看到的日期:${data.next_stat_date || '—'}`,
359
+ );
360
+ } else {
361
+ out.line(` 数据截止:${data.snapshot.cutoff_label || data.snapshot.stat_date}`);
362
+ out.line(
363
+ ` 站点容量:${data.snapshot.storage ? fmtBytes(data.snapshot.storage.bytes) : '—'}` +
364
+ `${data.snapshot.storage && data.snapshot.storage.mode ? `(口径 ${data.snapshot.storage.mode})` : ''}`,
365
+ );
366
+ }
367
+ if (data.traffic_quota && data.traffic_quota.ok) {
368
+ out.line(
369
+ ` 流量额度:${fmtGb(data.traffic_quota.plan_traffic_gb)}` +
370
+ `${data.traffic_quota.cycle_start ? `(周期 ${data.traffic_quota.cycle_start} → ${data.traffic_quota.cycle_end})` : ''}`,
371
+ );
372
+ } else {
373
+ out.line(' 流量额度:暂不可用(ESA 取不到:凭据 / 权限 / 超时)');
374
+ }
375
+ if (data.account_restricted) {
376
+ out.info(` 账号级流量 / 费用:${data.account_restricted_note || '仅平台管理员可见'}`);
377
+ } else if (data.snapshot && data.snapshot.traffic) {
378
+ out.line(
379
+ ` 账号级流量:已用 ${fmtGb(data.snapshot.traffic.used_gb)}` +
380
+ `(单日口径 ${data.snapshot.traffic.used_scope || 'day'})· 剩余 ${fmtGb(data.snapshot.traffic.remaining_gb)}`,
381
+ );
382
+ if (data.snapshot.cost) {
383
+ out.line(` 账号级费用:${fmtMoney((data.snapshot.cost.total_cents || 0) / 100, data.snapshot.cost.currency)}`);
384
+ }
385
+ }
386
+ out.line(` 快照计划:${data.schedule_note}`);
387
+ }
388
+ ctx.done(data);
389
+ return data;
390
+ },
391
+ };
392
+
393
+ /** 注册表(顺序 = 帮助里的顺序) */
394
+ export const OVERVIEW_COMMANDS = [overviewSummaryCommand, overviewTrafficCommand, overviewDashboardCommand];