@lark-apaas/miaoda-cli 0.1.1-alpha.93b822c → 0.1.1-alpha.a1a5e34

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.
@@ -126,6 +126,12 @@ async function execSql(opts) {
126
126
  /**
127
127
  * 解析 InnerAdminExecuteSQL 错误哨兵(results 末尾那条 SqlType="ERROR" 项)的 data 字段。
128
128
  * 服务端约定:data 是 `{"code":"k_dl_xxx","message":"..."}` 形态的 JSON 字符串。
129
+ *
130
+ * 拿到 code+message 后复用 mapDataloomBizError 走与旧协议(ensureInnerSuccess)
131
+ * 完全一致的映射规则——k_dl_1300015 → RESULT_SET_TOO_LARGE、SQLSTATE 透传等
132
+ * hint / 语义 code 重写不能丢。否则新协议下 SERVER_ERROR_HINTS 会因为 code key
133
+ * 不匹配而 miss,CLI 用户看不到 LIMIT 引导这种关键 hint。
134
+ *
129
135
  * 解析失败则降级为 INTERNAL_DB_ERROR + 原始字符串,避免哨兵格式异常时整条调用炸掉。
130
136
  */
131
137
  function parseSqlErrorSentinel(payload) {
@@ -133,7 +139,7 @@ function parseSqlErrorSentinel(payload) {
133
139
  const parsed = JSON.parse(payload);
134
140
  const code = typeof parsed.code === "string" && parsed.code !== "" ? parsed.code : "INTERNAL_DB_ERROR";
135
141
  const message = typeof parsed.message === "string" ? parsed.message : payload;
136
- return new error_1.AppError(code, message);
142
+ return (0, client_1.mapDataloomBizError)(code, message);
137
143
  }
138
144
  catch {
139
145
  return new error_1.AppError("INTERNAL_DB_ERROR", payload);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SQLSTATE_MAP = void 0;
4
4
  exports.traceHttp = traceHttp;
5
5
  exports.ensureInnerSuccess = ensureInnerSuccess;
6
+ exports.mapDataloomBizError = mapDataloomBizError;
6
7
  exports.extractData = extractData;
7
8
  exports.buildInnerUrl = buildInnerUrl;
8
9
  const error_1 = require("../../utils/error");
@@ -54,40 +55,48 @@ function ensureInnerSuccess(body) {
54
55
  const code = body.status_code ?? body.ErrorCode ?? "0";
55
56
  if (code === "0" || code === "")
56
57
  return;
57
- const message = stripPgPrefix(body.error_msg ?? body.ErrorMessage ?? `dataloom API error [${code}]`);
58
+ const appErr = mapDataloomBizError(code, body.error_msg ?? body.ErrorMessage ?? `dataloom API error [${code}]`);
58
59
  // PRD 多语句失败:后端在 envelope 顶层透出 errorStatementIndex(从 0 起计),
59
- // 单语句 / 单元执行不会带这个字段。下面的 AppError 都把它带上,让最终
60
- // CLI JSON envelope 写到 error.statement_index。
61
- const stmtIdx = typeof body.errorStatementIndex === "number" ? body.errorStatementIndex : undefined;
62
- // k_dl_1300002 是 PG 执行透传错误;error_msg 里常带 SQLSTATE,优先按 SQLSTATE 映射
60
+ // 单语句 / 单元执行不会带这个字段。挂到 AppError 上让 emitError 落到错误信封
61
+ // statement_index 字段。
62
+ if (typeof body.errorStatementIndex === "number") {
63
+ appErr.statement_index = body.errorStatementIndex;
64
+ }
65
+ throw appErr;
66
+ }
67
+ /**
68
+ * 把 dataloom 业务错误码 + 原始 message 映射为语义化 AppError。
69
+ *
70
+ * 同时被两条错误路径共用:
71
+ * - ensureInnerSuccess(旧协议:BaseResp.status_code != "0")
72
+ * - api.ts 的 parseSqlErrorSentinel(新协议:success envelope + ERROR 哨兵)
73
+ *
74
+ * 优先级:
75
+ * 1. k_dl_1300002 + SQLSTATE 透传 → SQLSTATE_MAP
76
+ * 2. k_dl_1600000 + "Invalid DB Branch" → MULTI_ENV_NOT_INITIALIZED
77
+ * 3. BIZ_ERR_MAP 命中 → 语义 code(可能带 hint)
78
+ * 4. 兜底 DB_API_<code>
79
+ *
80
+ * stripPgPrefix 在这里做一次,调用方传 raw message 即可。
81
+ */
82
+ function mapDataloomBizError(code, rawMessage) {
83
+ const message = stripPgPrefix(rawMessage);
63
84
  if (code === "k_dl_1300002") {
64
85
  const sqlstate = extractSqlstate(message);
65
86
  if (sqlstate && exports.SQLSTATE_MAP[sqlstate]) {
66
- throw new error_1.AppError(exports.SQLSTATE_MAP[sqlstate], message, { statement_index: stmtIdx });
87
+ return new error_1.AppError(exports.SQLSTATE_MAP[sqlstate], message);
67
88
  }
68
89
  }
69
- // k_dl_1600000 是 dataloom 通用参数错误,不能整体映射;
70
- // message 以 "Invalid DB Branch" 开头的两种触发场景:
71
- // 1) 单环境应用使用 --env(多环境未初始化)
72
- // 2) 多环境应用传入了不存在的 env 名(如 --env staging)
73
- // 两者外部表象一致:dbBranch 在 db_branch 表里查不到,固定映射为 MULTI_ENV_NOT_INITIALIZED。
74
90
  if (code === "k_dl_1600000" && message.startsWith("Invalid DB Branch")) {
75
- throw new error_1.AppError("MULTI_ENV_NOT_INITIALIZED", "--env is not available (multi-env not initialized)", {
76
- next_actions: [
77
- "Verify the --env value matches an existing dbBranch.",
78
- ],
79
- });
91
+ return new error_1.AppError("MULTI_ENV_NOT_INITIALIZED", "--env is not available (multi-env not initialized)", { next_actions: ["Verify the --env value matches an existing dbBranch."] });
80
92
  }
81
- // 业务 code 优先映射到语义 CLI code;未知 code 透传为 DB_API_<code> 样式
82
93
  const mapped = BIZ_ERR_MAP.get(code);
83
94
  if (mapped) {
84
- throw new error_1.AppError(mapped.code, mapped.message ?? message, {
95
+ return new error_1.AppError(mapped.code, mapped.message ?? message, {
85
96
  next_actions: mapped.hint ? [mapped.hint] : undefined,
86
- statement_index: stmtIdx,
87
97
  });
88
98
  }
89
- // 兜底:dataloom 未映射的 code 原样透传
90
- throw new error_1.AppError(`DB_API_${code}`, message, { statement_index: stmtIdx });
99
+ return new error_1.AppError(`DB_API_${code}`, message);
91
100
  }
92
101
  /**
93
102
  * 剥掉 PG 透传错误的 "ERROR:" 前缀:CLI 输出本身就会前缀 "Error:",
@@ -491,9 +491,9 @@ async function loadTableMap(ctx) {
491
491
  }
492
492
  function enrichRelationNotExist(err, relation, tableMap) {
493
493
  const guess = (0, fuzzy_match_1.suggest)(relation, Object.keys(tableMap));
494
- if (guess) {
495
- err.next_actions.push(`Did you mean '${guess}'? Run \`miaoda db schema list\` to see all tables.`);
496
- }
494
+ err.next_actions.push(guess
495
+ ? `Did you mean '${guess}'? Run \`miaoda db schema list\` to see all tables.`
496
+ : "Run `miaoda db schema list` to see all tables.");
497
497
  }
498
498
  function enrichColumnNotExist(err, colMatch, tableMap) {
499
499
  const colName = colMatch[1];
@@ -506,18 +506,18 @@ function enrichColumnNotExist(err, colMatch, tableMap) {
506
506
  if (relation !== undefined && relationCols !== undefined) {
507
507
  // 指定了表 → 在该表的列里找
508
508
  const guess = (0, fuzzy_match_1.suggest)(colName, relationCols);
509
- if (guess) {
510
- err.next_actions.push(`Did you mean column '${guess}' in table '${relation}'?`);
511
- }
512
- }
513
- else {
514
- // 没指定表(CTE / 别名 / 子查询场景)→ 在所有表的列合集里找
515
- const allCols = Array.from(new Set(Object.values(tableMap).flat()));
516
- const guess = (0, fuzzy_match_1.suggest)(colName, allCols);
517
- if (guess) {
518
- err.next_actions.push(`Did you mean '${guess}'? Run \`miaoda db schema get <table>\` to see columns.`);
519
- }
509
+ err.next_actions.push(guess
510
+ ? `Did you mean column '${guess}' in table '${relation}'?`
511
+ : `Run \`miaoda db schema get ${relation}\` to see all columns.`);
512
+ return;
520
513
  }
514
+ // 没指定表(CTE / 别名 / 子查询 / 单表 SELECT 但 PG 报错没带 relation 等场景)
515
+ // → 跨表所有列合集 fuzzy;没命中也给一句通用 hint,让用户知道下一步去哪查。
516
+ const allCols = Array.from(new Set(Object.values(tableMap).flat()));
517
+ const guess = (0, fuzzy_match_1.suggest)(colName, allCols);
518
+ err.next_actions.push(guess
519
+ ? `Did you mean '${guess}'? Run \`miaoda db schema get <table>\` to see columns.`
520
+ : "Run `miaoda db schema get <table>` to see all columns.");
521
521
  }
522
522
  /**
523
523
  * 扫整段 SQL 找一个看似"拼错的关键字"token:纯字母 + 长度 ≥ 3 + 不是已知 keyword
@@ -48,13 +48,20 @@ const shared_1 = require("../../../cli/commands/shared");
48
48
  const colors_1 = require("../../../utils/colors");
49
49
  const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
50
50
  /**
51
- * 判断 src 是本地文件还是远程引用:
52
- * - src 本地 fs 可访问 → upload(dst 当 remote)
53
- * - 其他 → download(src 是 /path 或 file_name,由 resolveRemotePath 解析)
54
- *
55
- * `cp` 语义要求 src 必须存在;本地不存在就认为用户指向远程。不需要给 dst 猜方向。
51
+ * 判断 src 是本地路径还是远程引用——按 PRD `miaoda file cp` 规则的优先级:
52
+ * 1. `./` / `../` / `~/` 开头 + 裸文件名(不含 `/`)→ 本地路径(即使不存在)
53
+ * 给 handleUpload 把"不存在"准确抛成 FILE_SRC_NOT_FOUND,
54
+ * 避免回退到远程 download 输出 `FILE_NOT_FOUND` + "Run miaoda file ls"
55
+ * 这种与用户意图(上传)背离的引导。
56
+ * 2. `/` 开头:fs.existsSync 兜底——存在即本地(绝对路径上传),
57
+ * 不存在即远程 path(download,由 resolveRemotePath 处理)。
56
58
  */
57
59
  function isLocalSrc(src) {
60
+ if (src.startsWith("./") || src.startsWith("../") || src.startsWith("~/"))
61
+ return true;
62
+ if (!src.includes("/"))
63
+ return true;
64
+ // `/` 开头:可能是本地绝对路径,也可能是远程 path;交给 fs 探测。
58
65
  const expanded = expandHome(src);
59
66
  try {
60
67
  return node_fs_1.default.existsSync(expanded);
@@ -105,7 +112,10 @@ async function resolveRemotePath(appId, input) {
105
112
  async function handleUpload(appId, localRaw, remoteRaw, rename) {
106
113
  const localPath = expandHome(localRaw);
107
114
  if (!node_fs_1.default.existsSync(localPath) || !node_fs_1.default.statSync(localPath).isFile()) {
108
- throw new error_1.AppError("FILE_SRC_NOT_FOUND", `Local file '${localRaw}' does not exist`);
115
+ throw new error_1.AppError("FILE_SRC_NOT_FOUND", `Local file '${localRaw}' does not exist`, {
116
+ // 引导本地路径自检;远程下载请用 `/path` 形态,避免裸名/相对路径误入上传分支
117
+ next_actions: ["Check the local file path; use `/path` prefix for remote download."],
118
+ });
109
119
  }
110
120
  const stat = node_fs_1.default.statSync(localPath);
111
121
  if (stat.size > MAX_UPLOAD_BYTES) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/miaoda-cli",
3
- "version": "0.1.1-alpha.93b822c",
3
+ "version": "0.1.1-alpha.a1a5e34",
4
4
  "description": "Miaoda 平台命令行工具,面向 Agent 调用",
5
5
  "type": "commonjs",
6
6
  "bin": {