@lark-apaas/miaoda-cli 0.1.2 → 0.1.3-alpha.40be425

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 (70) hide show
  1. package/README.md +8 -7
  2. package/dist/api/app/api.js +25 -0
  3. package/dist/api/app/index.js +15 -0
  4. package/dist/api/app/schemas.js +79 -0
  5. package/dist/api/app/types.js +58 -0
  6. package/dist/api/db/api.js +331 -24
  7. package/dist/api/db/client.js +36 -0
  8. package/dist/api/db/index.js +11 -1
  9. package/dist/api/deploy/api.js +60 -0
  10. package/dist/api/deploy/index.js +16 -0
  11. package/dist/api/deploy/schemas.js +105 -0
  12. package/dist/api/deploy/types.js +22 -0
  13. package/dist/api/file/api.js +25 -23
  14. package/dist/api/file/index.js +2 -1
  15. package/dist/api/file/parsers.js +14 -3
  16. package/dist/api/index.js +7 -1
  17. package/dist/api/observability/api.js +52 -0
  18. package/dist/api/observability/index.js +16 -0
  19. package/dist/api/observability/schemas.js +60 -0
  20. package/dist/api/observability/types.js +27 -0
  21. package/dist/cli/commands/app/index.js +62 -0
  22. package/dist/cli/commands/db/index.js +577 -5
  23. package/dist/cli/commands/deploy/index.js +155 -0
  24. package/dist/cli/commands/file/index.js +22 -7
  25. package/dist/cli/commands/index.js +10 -6
  26. package/dist/cli/commands/observability/index.js +240 -0
  27. package/dist/cli/commands/shared.js +83 -7
  28. package/dist/cli/handlers/app/get.js +47 -0
  29. package/dist/cli/handlers/app/index.js +7 -0
  30. package/dist/cli/handlers/app/update.js +59 -0
  31. package/dist/cli/handlers/db/audit.js +294 -0
  32. package/dist/cli/handlers/db/changelog.js +130 -0
  33. package/dist/cli/handlers/db/data.js +6 -6
  34. package/dist/cli/handlers/db/index.js +17 -1
  35. package/dist/cli/handlers/db/migration.js +235 -0
  36. package/dist/cli/handlers/db/quota.js +68 -0
  37. package/dist/cli/handlers/db/recovery.js +357 -0
  38. package/dist/cli/handlers/db/schema.js +2 -3
  39. package/dist/cli/handlers/db/sql.js +2 -3
  40. package/dist/cli/handlers/deploy/deploy.js +84 -0
  41. package/dist/cli/handlers/deploy/error-log.js +60 -0
  42. package/dist/cli/handlers/deploy/format.js +39 -0
  43. package/dist/cli/handlers/deploy/get.js +71 -0
  44. package/dist/cli/handlers/deploy/helpers.js +41 -0
  45. package/dist/cli/handlers/deploy/history.js +70 -0
  46. package/dist/cli/handlers/deploy/index.js +14 -0
  47. package/dist/cli/handlers/deploy/polling.js +162 -0
  48. package/dist/cli/handlers/file/cp.js +1 -2
  49. package/dist/cli/handlers/file/index.js +3 -1
  50. package/dist/cli/handlers/file/ls.js +1 -2
  51. package/dist/cli/handlers/file/quota.js +66 -0
  52. package/dist/cli/handlers/file/rm.js +1 -2
  53. package/dist/cli/handlers/file/sign.js +1 -2
  54. package/dist/cli/handlers/file/stat.js +3 -3
  55. package/dist/cli/handlers/observability/analytics.js +212 -0
  56. package/dist/cli/handlers/observability/helpers.js +66 -0
  57. package/dist/cli/handlers/observability/index.js +12 -0
  58. package/dist/cli/handlers/observability/log.js +94 -0
  59. package/dist/cli/handlers/observability/metric.js +208 -0
  60. package/dist/cli/handlers/observability/trace.js +102 -0
  61. package/dist/main.js +6 -2
  62. package/dist/utils/args.js +8 -0
  63. package/dist/utils/devops-error.js +28 -0
  64. package/dist/utils/git.js +29 -0
  65. package/dist/utils/http.js +118 -0
  66. package/dist/utils/index.js +15 -1
  67. package/dist/utils/output.js +360 -7
  68. package/dist/utils/poll.js +27 -0
  69. package/dist/utils/time.js +208 -0
  70. package/package.json +7 -5
package/README.md CHANGED
@@ -14,22 +14,22 @@ pnpm add -g @lark-apaas/miaoda-cli
14
14
  # 设置默认应用(需要应用上下文的命令可用 --app-id 覆盖)
15
15
  export MIAODA_APP_ID=app_demo_xxx
16
16
 
17
- # 插件管理
18
- miaoda plugin list-packages
19
- miaoda plugin install @demo/example-plugin
17
+ # 文件操作
18
+ miaoda file ls
19
+ miaoda file upload ./local/path
20
20
  ```
21
21
 
22
22
  JSON 结构化输出(Agent 推荐):
23
23
 
24
24
  ```bash
25
25
  # 输出全部字段
26
- miaoda plugin list --json
26
+ miaoda file ls --json
27
27
 
28
28
  # 可选字段级选择
29
- miaoda plugin list --json id,name
29
+ miaoda file ls --json id,name
30
30
 
31
31
  # 或通过 --output 指定格式
32
- miaoda plugin list --output json
32
+ miaoda file ls --output json
33
33
  ```
34
34
 
35
35
  ## 命令树
@@ -38,7 +38,8 @@ miaoda plugin list --output json
38
38
 
39
39
  | 域 | 用途 |
40
40
  |---|---|
41
- | `miaoda plugin ...` | 插件管理:安装、更新、移除、插件实例查询 |
41
+ | `miaoda file ...` | 文件操作:上传、下载、元数据、签名下载、批量删除 |
42
+ | `miaoda db ...` | 数据操作:SQL 执行、表结构查询、数据导入导出 |
42
43
 
43
44
  完整命令通过 `miaoda --help` 或 `miaoda <domain> --help` 查看。
44
45
 
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAppInfo = getAppInfo;
4
+ exports.updateAppMeta = updateAppMeta;
5
+ const http_1 = require("../../utils/http");
6
+ const devops_error_1 = require("../../utils/devops-error");
7
+ const DEFAULT_ERR_CODE = "INTERNAL_DEVOPS_ERROR";
8
+ function envelopeOpts(errPrefix) {
9
+ return {
10
+ errPrefix,
11
+ defaultErrCode: DEFAULT_ERR_CODE,
12
+ mapErr: devops_error_1.mapDevopsError,
13
+ };
14
+ }
15
+ /** GET /v1/devops/app/:appID — 获取应用详情 */
16
+ async function getAppInfo(appID) {
17
+ const url = `/v1/devops/app/${encodeURIComponent(appID)}`;
18
+ return (0, http_1.getInnerApi)(url, envelopeOpts("Failed to get app info"));
19
+ }
20
+ /** POST /v1/devops/app/:appID/meta — 更新应用元数据 */
21
+ async function updateAppMeta(req) {
22
+ const url = `/v1/devops/app/${encodeURIComponent(req.appID)}/meta`;
23
+ // 路径已带 appID,body 里也保留以匹配 BAM IDL 的 sensitive:"no" 透传约定
24
+ return (0, http_1.postInnerApi)(url, req, envelopeOpts("Failed to update app"));
25
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArchType = exports.Source = exports.BizType = exports.AppType = exports.AppMode = exports.AppStatus = exports.appMetaSchema = exports.updateAppMeta = exports.getAppInfo = void 0;
4
+ var api_1 = require("./api");
5
+ Object.defineProperty(exports, "getAppInfo", { enumerable: true, get: function () { return api_1.getAppInfo; } });
6
+ Object.defineProperty(exports, "updateAppMeta", { enumerable: true, get: function () { return api_1.updateAppMeta; } });
7
+ var schemas_1 = require("./schemas");
8
+ Object.defineProperty(exports, "appMetaSchema", { enumerable: true, get: function () { return schemas_1.appMetaSchema; } });
9
+ var types_1 = require("./types");
10
+ Object.defineProperty(exports, "AppStatus", { enumerable: true, get: function () { return types_1.AppStatus; } });
11
+ Object.defineProperty(exports, "AppMode", { enumerable: true, get: function () { return types_1.AppMode; } });
12
+ Object.defineProperty(exports, "AppType", { enumerable: true, get: function () { return types_1.AppType; } });
13
+ Object.defineProperty(exports, "BizType", { enumerable: true, get: function () { return types_1.BizType; } });
14
+ Object.defineProperty(exports, "Source", { enumerable: true, get: function () { return types_1.Source; } });
15
+ Object.defineProperty(exports, "ArchType", { enumerable: true, get: function () { return types_1.ArchType; } });
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appMetaSchema = void 0;
4
+ const output_1 = require("../../utils/output");
5
+ const types_1 = require("./types");
6
+ // Partial<Record<...>>:未来 BAM 加新枚举值时这里没声明也不报错,运行期 lookup 自然得 undefined
7
+ const STATUS_TEXT = {
8
+ [types_1.AppStatus.UNSPECIFIED]: "unknown",
9
+ [types_1.AppStatus.PREPARING]: "preparing",
10
+ [types_1.AppStatus.UNRELEASED]: "unreleased",
11
+ [types_1.AppStatus.RELEASED]: "released",
12
+ [types_1.AppStatus.DELETED]: "deleted",
13
+ [types_1.AppStatus.UPGRADING]: "upgrading",
14
+ };
15
+ const MODE_TEXT = {
16
+ [types_1.AppMode.UNSPECIFIED]: "unknown",
17
+ [types_1.AppMode.STANDARD]: "standard",
18
+ [types_1.AppMode.EXPERT]: "expert",
19
+ };
20
+ const TYPE_TEXT = {
21
+ [types_1.AppType.UNSPECIFIED]: "unknown",
22
+ [types_1.AppType.STANDARD]: "standard",
23
+ [types_1.AppType.PROTOTYPE]: "prototype",
24
+ [types_1.AppType.APPLICATION]: "application",
25
+ [types_1.AppType.DESIGN]: "design",
26
+ [types_1.AppType.OPENCLAW]: "openclaw",
27
+ [types_1.AppType.CLAW_SUB]: "claw_sub",
28
+ };
29
+ const BIZ_TYPE_TEXT = {
30
+ [types_1.BizType.UNSPECIFIED]: "unknown",
31
+ [types_1.BizType.MIAODA]: "miaoda",
32
+ [types_1.BizType.FORCE]: "force",
33
+ };
34
+ const ARCH_TYPE_TEXT = {
35
+ [types_1.ArchType.UNSPECIFIED]: "unknown",
36
+ [types_1.ArchType.FULL_STACK]: "full_stack",
37
+ [types_1.ArchType.NOT_FULL_STACK]: "not_full_stack",
38
+ };
39
+ const SOURCE_TEXT = {
40
+ [types_1.Source.UNSPECIFIED]: "unknown",
41
+ [types_1.Source.CHAT]: "chat",
42
+ [types_1.Source.TEMPLATE]: "template",
43
+ [types_1.Source.IMPORT_ZIP]: "import_zip",
44
+ [types_1.Source.IMPORT_BASE]: "import_base",
45
+ };
46
+ function lookup(table) {
47
+ return (v) => {
48
+ if (typeof v !== "number")
49
+ return undefined;
50
+ return table[v];
51
+ };
52
+ }
53
+ /**
54
+ * AppMeta 的 pretty 渲染契约。
55
+ *
56
+ * 注:BAM 没给 createdAt / updatedAt 的单位注解;先按 ms(fmt.ms)渲染,
57
+ * 待 e2e 真值确认后调整。enum 字段通过 derive 翻译成可读字符串,JSON 模式
58
+ * 仍是 BAM 原值(数字枚举),与 BAM 接口契约保持一致。
59
+ */
60
+ exports.appMetaSchema = {
61
+ columns: [
62
+ { key: "appID", label: "app-id" },
63
+ { key: "name" },
64
+ { key: "description" },
65
+ { key: "status_text", label: "status", derive: (row) => lookup(STATUS_TEXT)(row.status) },
66
+ { key: "appType_text", label: "type", derive: (row) => lookup(TYPE_TEXT)(row.appType) },
67
+ { key: "appMode_text", label: "mode", derive: (row) => lookup(MODE_TEXT)(row.appMode) },
68
+ { key: "bizType_text", label: "biz-type", derive: (row) => lookup(BIZ_TYPE_TEXT)(row.bizType) },
69
+ { key: "archType_text", label: "arch", derive: (row) => lookup(ARCH_TYPE_TEXT)(row.archType) },
70
+ { key: "source_text", label: "source", derive: (row) => lookup(SOURCE_TEXT)(row.source) },
71
+ { key: "ownedBy", label: "owner" },
72
+ { key: "createdBy", label: "creator" },
73
+ { key: "dataBranchID", label: "branch" },
74
+ { key: "parentAppID", label: "parent-app" },
75
+ { key: "createdAt", label: "created-at", format: output_1.fmt.ms() },
76
+ { key: "updatedAt", label: "updated-at", format: output_1.fmt.ms() },
77
+ ],
78
+ strict: true,
79
+ };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ // 与 BAM lark.apaas.devops v1.0.326 对齐:
3
+ // - CLIGetAppInfo (4070322) → AppInfo / AppMeta
4
+ // - CLIUpdateAppMeta (4070323) → 入参 name? / description?
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ArchType = exports.Source = exports.BizType = exports.AppType = exports.AppMode = exports.AppStatus = void 0;
7
+ /** 应用状态 */
8
+ var AppStatus;
9
+ (function (AppStatus) {
10
+ AppStatus[AppStatus["UNSPECIFIED"] = 0] = "UNSPECIFIED";
11
+ AppStatus[AppStatus["PREPARING"] = 1] = "PREPARING";
12
+ AppStatus[AppStatus["UNRELEASED"] = 2] = "UNRELEASED";
13
+ AppStatus[AppStatus["RELEASED"] = 3] = "RELEASED";
14
+ /** BAM 注释为 Deprecated;保留枚举以兼容历史响应数据 */
15
+ AppStatus[AppStatus["DELETED"] = 4] = "DELETED";
16
+ AppStatus[AppStatus["UPGRADING"] = 5] = "UPGRADING";
17
+ })(AppStatus || (exports.AppStatus = AppStatus = {}));
18
+ /** 应用模式 */
19
+ var AppMode;
20
+ (function (AppMode) {
21
+ AppMode[AppMode["UNSPECIFIED"] = 0] = "UNSPECIFIED";
22
+ AppMode[AppMode["STANDARD"] = 1] = "STANDARD";
23
+ AppMode[AppMode["EXPERT"] = 2] = "EXPERT";
24
+ })(AppMode || (exports.AppMode = AppMode = {}));
25
+ /** 应用类型 */
26
+ var AppType;
27
+ (function (AppType) {
28
+ AppType[AppType["UNSPECIFIED"] = 0] = "UNSPECIFIED";
29
+ AppType[AppType["STANDARD"] = 1] = "STANDARD";
30
+ AppType[AppType["PROTOTYPE"] = 2] = "PROTOTYPE";
31
+ AppType[AppType["APPLICATION"] = 3] = "APPLICATION";
32
+ AppType[AppType["DESIGN"] = 4] = "DESIGN";
33
+ AppType[AppType["OPENCLAW"] = 5] = "OPENCLAW";
34
+ AppType[AppType["CLAW_SUB"] = 6] = "CLAW_SUB";
35
+ })(AppType || (exports.AppType = AppType = {}));
36
+ /** 业务线 */
37
+ var BizType;
38
+ (function (BizType) {
39
+ BizType[BizType["UNSPECIFIED"] = 0] = "UNSPECIFIED";
40
+ BizType[BizType["MIAODA"] = 1] = "MIAODA";
41
+ BizType[BizType["FORCE"] = 2] = "FORCE";
42
+ })(BizType || (exports.BizType = BizType = {}));
43
+ /** 应用来源 */
44
+ var Source;
45
+ (function (Source) {
46
+ Source[Source["UNSPECIFIED"] = 0] = "UNSPECIFIED";
47
+ Source[Source["CHAT"] = 1] = "CHAT";
48
+ Source[Source["TEMPLATE"] = 2] = "TEMPLATE";
49
+ Source[Source["IMPORT_ZIP"] = 3] = "IMPORT_ZIP";
50
+ Source[Source["IMPORT_BASE"] = 4] = "IMPORT_BASE";
51
+ })(Source || (exports.Source = Source = {}));
52
+ /** 架构类型 */
53
+ var ArchType;
54
+ (function (ArchType) {
55
+ ArchType[ArchType["UNSPECIFIED"] = 0] = "UNSPECIFIED";
56
+ ArchType[ArchType["FULL_STACK"] = 1] = "FULL_STACK";
57
+ ArchType[ArchType["NOT_FULL_STACK"] = 2] = "NOT_FULL_STACK";
58
+ })(ArchType || (exports.ArchType = ArchType = {}));
@@ -4,27 +4,39 @@ exports.execSql = execSql;
4
4
  exports.getSchema = getSchema;
5
5
  exports.importData = importData;
6
6
  exports.exportData = exportData;
7
+ exports.listDDLChangelog = listDDLChangelog;
8
+ exports.getAuditStatus = getAuditStatus;
9
+ exports.setAuditConfig = setAuditConfig;
10
+ exports.listAuditLog = listAuditLog;
11
+ exports.migrationInit = migrationInit;
12
+ exports.migrate = migrate;
13
+ exports.getMigrationStatus = getMigrationStatus;
14
+ exports.recover = recover;
15
+ exports.getRecoveryPreview = getRecoveryPreview;
16
+ exports.getDbQuota = getDbQuota;
7
17
  const http_1 = require("../../utils/http");
8
18
  const error_1 = require("../../utils/error");
9
19
  const http_client_1 = require("@lark-apaas/http-client");
10
20
  const client_1 = require("./client");
11
- /**
12
- * SDK 抛出的 HttpError 统一映射成 CLI 层错误:
13
- * 1. 先尝试从 response body 解 envelope,命中 dataloom 业务 code → AppError
14
- * 2. 兜底返 HttpError,保留真实 status 码与上下文
15
- *
16
- * 配合调用点的 try/catch + traceHttp,让 --verbose 在错误路径上也能拿到
17
- * x-tt-logid 与 status,方便定位线上问题。
18
- */
19
- async function mapDbHttpError(err, url, ctx,
20
- /**
21
- * 可选 hook:解析到响应 body 后,如果 envelope 命中业务错误并抛出 AppError,
22
- * 调用方可以借此把 body 里的额外字段(如 multi-statement 的 partial results)
23
- * 挂到 AppError 上。
24
- */
25
- onErrorBody) {
21
+ const DEFAULT_TIMEOUT_HINT = "The server may still be processing your request. Retry the command if needed.";
22
+ async function mapDbHttpError(err, url, ctx, opts) {
26
23
  if (err instanceof error_1.AppError)
27
24
  throw err;
25
+ // 客户端超时 / abort:SdkHttpError 时 response 通常缺失(status=0),落到 throw 时
26
+ // message 形如 'Failed to recover: 0' 让用户看不懂。识别 abort / timeout 关键字后
27
+ // 单独抛专用 AppError(带领域可定制的 message + hint),不再包成通用 "Failed to X"。
28
+ if (err instanceof Error) {
29
+ const lower = err.message.toLowerCase();
30
+ if (lower.includes("aborted") ||
31
+ lower.includes("timeout") ||
32
+ err.name === "AbortError" ||
33
+ err.name === "TimeoutError") {
34
+ const code = opts?.timeout?.code ?? "REQUEST_TIMEOUT";
35
+ const message = opts?.timeout?.message ?? "Request timed out after 30s";
36
+ const hint = opts?.timeout?.hint ?? DEFAULT_TIMEOUT_HINT;
37
+ throw new error_1.AppError(code, message, { next_actions: [hint] });
38
+ }
39
+ }
28
40
  if (err instanceof http_client_1.HttpError) {
29
41
  const status = err.response?.status ?? 0;
30
42
  const statusText = err.response?.statusText ?? "";
@@ -35,8 +47,8 @@ onErrorBody) {
35
47
  (0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
36
48
  }
37
49
  catch (appErr) {
38
- if (appErr instanceof error_1.AppError && onErrorBody) {
39
- onErrorBody(body, appErr);
50
+ if (appErr instanceof error_1.AppError && opts?.onErrorBody) {
51
+ opts.onErrorBody(body, appErr);
40
52
  }
41
53
  throw appErr;
42
54
  }
@@ -90,7 +102,17 @@ async function execSql(opts) {
90
102
  }
91
103
  catch (err) {
92
104
  (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
93
- await mapDbHttpError(err, url, "Failed to execute SQL", attachSqlPartialResults);
105
+ await mapDbHttpError(err, url, "Failed to execute SQL", {
106
+ onErrorBody: attachSqlPartialResults,
107
+ // SQL 路径单独的超时文案:PG 的 statement_timeout 会回滚整条事务,所以这里
108
+ // 明示「事务已回滚、没有改动落地」,并把 hint 引导到「简化 SQL / 加 LIMIT / 拆条」。
109
+ timeout: {
110
+ code: "SQL_EXECUTION_TIMEOUT",
111
+ message: "SQL execution timed out after 30s",
112
+ hint: "The transaction was rolled back; no changes were applied. " +
113
+ "Simplify the SQL, add filters or LIMIT for queries, or split it into smaller statements.",
114
+ },
115
+ });
94
116
  throw err; // 不可达
95
117
  }
96
118
  const body = (await response.json());
@@ -247,10 +269,14 @@ async function exportData(opts) {
247
269
  await mapDbHttpError(err, url, "Failed to export data");
248
270
  throw err; // 不可达
249
271
  }
250
- // 成功路径:响应 body 通常是原始 CSV/JSON 字节,但部分错误场景下网关会返
272
+ // 成功路径:响应 body 通常是原始 CSV/SQL/JSON 字节,但部分错误场景下网关会返
251
273
  // HTTP 200 + JSON envelope(status_code != "0"),需要在这里嗅探兜底。
252
- const contentType = response.headers.get("Content-Type") ??
253
- (opts.format === "csv" ? "text/csv" : "application/json");
274
+ const defaultContentType = {
275
+ csv: "text/csv",
276
+ sql: "text/plain",
277
+ json: "application/json",
278
+ };
279
+ const contentType = response.headers.get("Content-Type") ?? defaultContentType[opts.format];
254
280
  const ab = await response.arrayBuffer();
255
281
  const buf = Buffer.from(new Uint8Array(ab));
256
282
  if (buf.length === 0) {
@@ -258,8 +284,9 @@ async function exportData(opts) {
258
284
  }
259
285
  // Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
260
286
  // InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
261
- // CSV 格式做 sniff —— JSON 格式正常成功响应也是 application/json,会误判。
262
- if (opts.format === "csv" && /application\/json/i.test(contentType)) {
287
+ // CSV / SQL 都是非 JSON 输出,application/json 响应必是错误信封;JSON 格式
288
+ // 成功响应自身就是 application/json,跳过 sniff 避免误判。
289
+ if (opts.format !== "json" && /application\/json/i.test(contentType)) {
263
290
  try {
264
291
  const parsed = JSON.parse(buf.toString("utf8"));
265
292
  if (parsed.status_code != null && parsed.status_code !== "0") {
@@ -269,7 +296,7 @@ async function exportData(opts) {
269
296
  }
270
297
  catch (err) {
271
298
  // 已经被 extractData 抛成 AppError → 透传;否则 JSON.parse 失败说明 body
272
- // 真的是 CSV 文本,继续按成功流程走
299
+ // 真的是 CSV / SQL 文本,继续按成功流程走
273
300
  if (err instanceof error_1.AppError)
274
301
  throw err;
275
302
  }
@@ -286,3 +313,283 @@ async function exportData(opts) {
286
313
  recordCount,
287
314
  };
288
315
  }
316
+ // ── db changelog → InnerAdminListDDLChangelog ──
317
+ /**
318
+ * 后端:GET /v1/dataloom/app/{appId}/db/changelog?table=&since=&until=&limit=&cursor=&dbBranch=
319
+ *
320
+ * 时间字段 since/until 由 CLI 端归一化为 ISO 8601 UTC 后透传;后端按 created_at 比较。
321
+ */
322
+ async function listDDLChangelog(opts) {
323
+ const client = (0, http_1.getHttpClient)();
324
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/changelog", {
325
+ table: opts.table,
326
+ since: opts.since,
327
+ until: opts.until,
328
+ limit: opts.limit !== undefined ? String(opts.limit) : undefined,
329
+ cursor: opts.cursor,
330
+ dbBranch: opts.dbBranch,
331
+ });
332
+ const start = Date.now();
333
+ let response;
334
+ try {
335
+ response = await client.get(url);
336
+ (0, client_1.traceHttp)("GET", url, start, response);
337
+ }
338
+ catch (err) {
339
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
340
+ await mapDbHttpError(err, url, "Failed to list DDL changelog");
341
+ throw err; // 不可达
342
+ }
343
+ const body = (await response.json());
344
+ const data = (0, client_1.extractData)(body);
345
+ return {
346
+ items: data.items ?? [],
347
+ nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
348
+ hasMore: Boolean(data.hasMore),
349
+ };
350
+ }
351
+ // ── db audit → InnerAdminGetAuditStatus / InnerAdminSetAuditConfig ──
352
+ /**
353
+ * 后端:GET /v1/dataloom/app/{appId}/db/audit/status?table=&dbBranch=
354
+ * 查表审计开关状态。table 非空 → 单表过滤;空 → 返当前 workspace 全部已配置表。
355
+ */
356
+ async function getAuditStatus(opts) {
357
+ const client = (0, http_1.getHttpClient)();
358
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/status", {
359
+ table: opts.table,
360
+ dbBranch: opts.dbBranch,
361
+ });
362
+ const start = Date.now();
363
+ let response;
364
+ try {
365
+ response = await client.get(url);
366
+ (0, client_1.traceHttp)("GET", url, start, response);
367
+ }
368
+ catch (err) {
369
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
370
+ await mapDbHttpError(err, url, "Failed to get audit status");
371
+ throw err; // 不可达
372
+ }
373
+ const respBody = (await response.json());
374
+ const data = (0, client_1.extractData)(respBody);
375
+ return data.items ?? [];
376
+ }
377
+ /**
378
+ * 后端:POST /v1/dataloom/app/{appId}/db/audit/config
379
+ * 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
380
+ */
381
+ async function setAuditConfig(opts) {
382
+ const client = (0, http_1.getHttpClient)();
383
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/config");
384
+ const body = {
385
+ table: opts.table,
386
+ enabled: opts.enabled,
387
+ };
388
+ if (opts.retention !== undefined && opts.retention !== "")
389
+ body.retention = opts.retention;
390
+ if (opts.dbBranch !== undefined && opts.dbBranch !== "")
391
+ body.dbBranch = opts.dbBranch;
392
+ const start = Date.now();
393
+ let response;
394
+ try {
395
+ response = await client.post(url, body);
396
+ (0, client_1.traceHttp)("POST", url, start, response);
397
+ }
398
+ catch (err) {
399
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
400
+ await mapDbHttpError(err, url, "Failed to set audit config");
401
+ throw err; // 不可达
402
+ }
403
+ const respBody = (await response.json());
404
+ const data = (0, client_1.extractData)(respBody);
405
+ if (!data.status) {
406
+ throw new error_1.AppError("INTERNAL_DB_ERROR", "audit config response missing status field");
407
+ }
408
+ return data.status;
409
+ }
410
+ // ── db audit log → InnerAdminListAuditLog ──
411
+ /**
412
+ * 后端:GET /v1/dataloom/app/{appId}/db/audit/log?tables=&since=&until=&limit=&cursor=&dbBranch=
413
+ *
414
+ * 走 admin-inner 接口而不是 InnerAdminExecuteSQL 直接 SELECT pg_audit:
415
+ * - operator 在 details JSONB 内是 user_id,服务端解析成 username
416
+ * - summary 后端按 type + before/after diff 合成(pg_audit 表无此列)
417
+ * - before/after JSONB 后端 JSON.stringify 后透传字符串,CLI 按需 parse
418
+ *
419
+ * 多表用逗号拼接走 query;后端按 target_table IN (...) 一次查。skipped 字段返
420
+ * 多表中无记录的表名,便于 CLI 展示 hint。
421
+ */
422
+ async function listAuditLog(opts) {
423
+ if (opts.tables.length === 0) {
424
+ throw new error_1.AppError("ARGS_INVALID", "at least one table is required");
425
+ }
426
+ const client = (0, http_1.getHttpClient)();
427
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/log", {
428
+ tables: opts.tables.join(","),
429
+ since: opts.since,
430
+ until: opts.until,
431
+ limit: opts.limit !== undefined ? String(opts.limit) : undefined,
432
+ cursor: opts.cursor,
433
+ dbBranch: opts.dbBranch,
434
+ });
435
+ const start = Date.now();
436
+ let response;
437
+ try {
438
+ response = await client.get(url);
439
+ (0, client_1.traceHttp)("GET", url, start, response);
440
+ }
441
+ catch (err) {
442
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
443
+ await mapDbHttpError(err, url, "Failed to list audit log");
444
+ throw err; // 不可达
445
+ }
446
+ const body = (await response.json());
447
+ const data = (0, client_1.extractData)(body);
448
+ return {
449
+ items: data.items ?? [],
450
+ nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
451
+ hasMore: Boolean(data.hasMore),
452
+ skipped: data.skipped ?? [],
453
+ };
454
+ }
455
+ // ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
456
+ /**
457
+ * 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
458
+ * 单库 → dev/online 双库初始化,不可逆。对应公开 API EnableMultiEnvDB 的 admin-inner 通道。
459
+ */
460
+ async function migrationInit(opts) {
461
+ const client = (0, http_1.getHttpClient)();
462
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/enableMultiEnv");
463
+ const body = {};
464
+ if (opts.syncData !== undefined)
465
+ body.syncData = opts.syncData;
466
+ const start = Date.now();
467
+ let response;
468
+ try {
469
+ response = await client.post(url, body);
470
+ (0, client_1.traceHttp)("POST", url, start, response);
471
+ }
472
+ catch (err) {
473
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
474
+ await mapDbHttpError(err, url, "Failed to init migration");
475
+ throw err; // 不可达
476
+ }
477
+ const respBody = (await response.json());
478
+ return (0, client_1.extractData)(respBody);
479
+ }
480
+ /**
481
+ * 后端:POST /v1/dataloom/app/{appId}/db/migration
482
+ * 合并 diff + apply:dryRun=true 只返 changes 不下发;dryRun=false 才执行。
483
+ */
484
+ async function migrate(opts) {
485
+ const client = (0, http_1.getHttpClient)();
486
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/migration");
487
+ const start = Date.now();
488
+ let response;
489
+ try {
490
+ response = await client.post(url, { dryRun: opts.dryRun });
491
+ (0, client_1.traceHttp)("POST", url, start, response);
492
+ }
493
+ catch (err) {
494
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
495
+ await mapDbHttpError(err, url, "Failed to migrate");
496
+ throw err; // 不可达
497
+ }
498
+ const respBody = (await response.json());
499
+ return (0, client_1.extractData)(respBody);
500
+ }
501
+ /**
502
+ * 后端:GET /v1/dataloom/app/{appId}/db/migration/status?taskId=...
503
+ * CLI 拿到 migration apply 的 taskId 后定时调本接口,直到 status=success/failed。
504
+ * 网络层超时仍走 mapDbHttpError → 单次 30s;轮询节奏由 CLI handler 自行控制。
505
+ */
506
+ async function getMigrationStatus(opts) {
507
+ const client = (0, http_1.getHttpClient)();
508
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/migration/status", {
509
+ taskId: opts.taskId,
510
+ dbBranch: opts.dbBranch,
511
+ });
512
+ const start = Date.now();
513
+ let response;
514
+ try {
515
+ response = await client.get(url);
516
+ (0, client_1.traceHttp)("GET", url, start, response);
517
+ }
518
+ catch (err) {
519
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
520
+ await mapDbHttpError(err, url, "Failed to get migration status");
521
+ throw err; // 不可达
522
+ }
523
+ const body = (await response.json());
524
+ return (0, client_1.extractData)(body);
525
+ }
526
+ // ── db recovery → InnerAdminRecover ──
527
+ /**
528
+ * 后端:POST /v1/dataloom/app/{appId}/db/recovery
529
+ * 合并 PITR diff + apply:dryRun=true 预览影响;dryRun=false 触发恢复。
530
+ */
531
+ async function recover(opts) {
532
+ const client = (0, http_1.getHttpClient)();
533
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/recovery");
534
+ const start = Date.now();
535
+ let response;
536
+ try {
537
+ response = await client.post(url, {
538
+ target: opts.target,
539
+ dryRun: opts.dryRun,
540
+ });
541
+ (0, client_1.traceHttp)("POST", url, start, response);
542
+ }
543
+ catch (err) {
544
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
545
+ await mapDbHttpError(err, url, "Failed to recover");
546
+ throw err; // 不可达
547
+ }
548
+ const respBody = (await response.json());
549
+ return (0, client_1.extractData)(respBody);
550
+ }
551
+ /**
552
+ * 后端:GET /v1/dataloom/app/{appId}/db/recovery/preview?previewRequestId=...
553
+ * CLI 拿到 recovery diff 的 previewRequestId 后定时调本接口直到 previewStatus=success/failed。
554
+ */
555
+ async function getRecoveryPreview(opts) {
556
+ const client = (0, http_1.getHttpClient)();
557
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/recovery/preview", {
558
+ previewRequestId: opts.previewRequestId,
559
+ dbBranch: opts.dbBranch,
560
+ });
561
+ const start = Date.now();
562
+ let response;
563
+ try {
564
+ response = await client.get(url);
565
+ (0, client_1.traceHttp)("GET", url, start, response);
566
+ }
567
+ catch (err) {
568
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
569
+ await mapDbHttpError(err, url, "Failed to get recovery preview");
570
+ throw err; // 不可达
571
+ }
572
+ const body = (await response.json());
573
+ return (0, client_1.extractData)(body);
574
+ }
575
+ // ── db quota → InnerAdminGetDbQuota ──
576
+ /**
577
+ * 后端:GET /v1/dataloom/app/{appId}/db/quota?dbBranch=
578
+ */
579
+ async function getDbQuota(opts) {
580
+ const client = (0, http_1.getHttpClient)();
581
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/quota", { dbBranch: opts.dbBranch });
582
+ const start = Date.now();
583
+ let response;
584
+ try {
585
+ response = await client.get(url);
586
+ (0, client_1.traceHttp)("GET", url, start, response);
587
+ }
588
+ catch (err) {
589
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
590
+ await mapDbHttpError(err, url, "Failed to get db quota");
591
+ throw err; // 不可达
592
+ }
593
+ const respBody = (await response.json());
594
+ return (0, client_1.extractData)(respBody);
595
+ }
@@ -129,6 +129,42 @@ const BIZ_ERR_MAP = new Map(Object.entries({
129
129
  // k_dl_1300015:SELECT 结果超过 1000 行硬拦;多行 hint 由 output.ts 的
130
130
  // SERVER_ERROR_HINTS 按语义 code 兜底,这里只做 code 改名
131
131
  k_dl_1300015: { code: "RESULT_SET_TOO_LARGE" },
132
+ // audit
133
+ k_dl_1310001: {
134
+ code: "AUDIT_ALREADY_ENABLED",
135
+ hint: "Use `miaoda db audit status <table>` to confirm current state.",
136
+ },
137
+ k_dl_1310002: {
138
+ code: "AUDIT_NOT_ENABLED",
139
+ hint: "Run `miaoda db audit enable <table>` first.",
140
+ },
141
+ k_dl_1310003: {
142
+ code: "INVALID_RETENTION",
143
+ hint: "Allowed values: 7d, 30d, 180d, 360d, forever.",
144
+ },
145
+ // migration
146
+ k_dl_1320001: {
147
+ code: "MIGRATION_NOT_AVAILABLE",
148
+ hint: "Migration commands require an expert-mode application.",
149
+ },
150
+ k_dl_1320002: { code: "MULTI_ENV_ALREADY_INITIALIZED" },
151
+ k_dl_1320003: {
152
+ code: "NO_PENDING_CHANGES",
153
+ hint: "dev and online schemas are already in sync.",
154
+ },
155
+ // recovery
156
+ k_dl_1330001: {
157
+ code: "RECOVERY_WINDOW_EXCEEDED",
158
+ hint: "Pick a timestamp inside the supported recovery window.",
159
+ },
160
+ k_dl_1330002: {
161
+ code: "RECOVERY_IN_PROGRESS",
162
+ hint: "Wait for the running recovery to finish, or check its status.",
163
+ },
164
+ k_dl_1330003: {
165
+ code: "INVALID_TIMESTAMP",
166
+ hint: "Use ISO 8601 / yyyy-mm-dd / yyyy-mm-dd HH:MM:SS.",
167
+ },
132
168
  }));
133
169
  /** PG SQLSTATE → CLI code(当前 dataloom 不一定透传,预留未来使用) */
134
170
  exports.SQLSTATE_MAP = {