@vectorx/xhs-cloud-cli 1.0.0 → 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 (73) hide show
  1. package/lib/commands/agent/dev.js +40 -8
  2. package/lib/commands/agent/index.js +2 -0
  3. package/lib/commands/agent/info.js +107 -0
  4. package/lib/commands/agent/list.js +95 -0
  5. package/lib/commands/agent/new.js +32 -6
  6. package/lib/commands/auth/login.js +53 -38
  7. package/lib/commands/env/info.js +3 -1
  8. package/lib/commands/env/set.js +3 -1
  9. package/lib/commands/fun/deploy.js +3 -1
  10. package/lib/commands/fun/dev.js +9 -3
  11. package/lib/commands/fun/new.js +32 -6
  12. package/lib/core/base.js +9 -1
  13. package/lib/decorators/auth.js +2 -1
  14. package/lib/main.js +3 -1
  15. package/lib/utils/agent-check.js +158 -0
  16. package/lib/utils/templates.js +75 -0
  17. package/package.json +10 -8
  18. package/templates/cloud-agents/agent-base-template/project.config.json +6 -0
  19. package/templates/{chatbox-agent → cloud-agents/agent-base-template}/src/index.js +0 -1
  20. package/templates/cloud-agents/agent-doc-template/.env.template +1 -0
  21. package/templates/cloud-agents/agent-doc-template/agent-cloudbase-functions.json +11 -0
  22. package/templates/cloud-agents/agent-doc-template/package.json +11 -0
  23. package/templates/cloud-agents/agent-doc-template/project.config.json +6 -0
  24. package/templates/cloud-agents/agent-doc-template/src/index.js +68 -0
  25. package/templates/cloud-agents/agent-image-template/.env.template +1 -0
  26. package/templates/cloud-agents/agent-image-template/agent-cloudbase-functions.json +11 -0
  27. package/templates/cloud-agents/agent-image-template/package.json +11 -0
  28. package/templates/cloud-agents/agent-image-template/project.config.json +6 -0
  29. package/templates/cloud-agents/agent-image-template/src/index.js +56 -0
  30. package/templates/cloud-agents/agent-text-template/.env.template +1 -0
  31. package/templates/cloud-agents/agent-text-template/agent-cloudbase-functions.json +11 -0
  32. package/templates/cloud-agents/agent-text-template/package.json +11 -0
  33. package/templates/cloud-agents/agent-text-template/project.config.json +6 -0
  34. package/templates/cloud-agents/agent-text-template/src/index.js +38 -0
  35. package/templates/cloud-agents/agent-voice-template/.env.template +1 -0
  36. package/templates/cloud-agents/agent-voice-template/agent-cloudbase-functions.json +11 -0
  37. package/templates/cloud-agents/agent-voice-template/package.json +11 -0
  38. package/templates/cloud-agents/agent-voice-template/project.config.json +6 -0
  39. package/templates/cloud-agents/agent-voice-template/src/index.js +81 -0
  40. package/templates/cloud-agents/templates.json +9 -0
  41. package/templates/cloud-dev/templates.json +6 -0
  42. package/types/commands/agent/index.d.ts +2 -0
  43. package/types/commands/agent/info.d.ts +14 -0
  44. package/types/commands/agent/list.d.ts +14 -0
  45. package/types/commands/auth/login.d.ts +2 -0
  46. package/types/utils/agent-check.d.ts +1 -0
  47. package/types/utils/templates.d.ts +17 -0
  48. package/templates/chatbox-agent/project.config.json +0 -5
  49. package/templates/weather-agent/index.js +0 -92
  50. package/templates/weather-agent/project.config.json +0 -4
  51. /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/.env.template +0 -0
  52. /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/agent-cloudbase-functions.json +0 -0
  53. /package/templates/{chatbox-agent → cloud-agents/agent-base-template}/package.json +0 -0
  54. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/.env.template +0 -0
  55. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/README.md +0 -0
  56. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/agent-cloudbase-functions.json +0 -0
  57. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/package.json +0 -0
  58. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/binary/index.js +0 -0
  59. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/context/context-service.js +0 -0
  60. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/context/index.js +0 -0
  61. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/env/index.js +0 -0
  62. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/form/index.js +0 -0
  63. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/index.js +0 -0
  64. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/json/index.js +0 -0
  65. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/multipart/index.js +0 -0
  66. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/text/index.js +0 -0
  67. /package/templates/{ai-cloud-functions-example → cloud-dev/cloudfunction-route-template}/src/user/index.js +0 -0
  68. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/.env.template +0 -0
  69. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/agent-cloudbase-functions.json +0 -0
  70. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/package.json +0 -0
  71. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/project.config.json +0 -0
  72. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/src/echo.js +0 -0
  73. /package/templates/{cloudfunction-template → cloud-dev/cloudfunction-template}/src/index.js +0 -0
@@ -28,12 +28,25 @@ exports.FunNewCommand = void 0;
28
28
  const fs_1 = __importDefault(require("fs"));
29
29
  const path_1 = __importDefault(require("path"));
30
30
  const chalk_1 = __importDefault(require("chalk"));
31
+ const inquirer_1 = __importDefault(require("inquirer"));
31
32
  const base_1 = require("../../core/base");
32
33
  const decorators_1 = require("../../decorators");
34
+ const templates_1 = require("../../utils/templates");
33
35
  let FunNewCommand = class FunNewCommand extends base_1.Command {
34
36
  execute(options, log) {
35
37
  return __awaiter(this, void 0, void 0, function* () {
36
- const { projectName, dir } = options;
38
+ let { projectName, dir, template } = options;
39
+ if (!projectName) {
40
+ const { projectName: inputName } = yield inquirer_1.default.prompt([
41
+ {
42
+ type: "input",
43
+ name: "projectName",
44
+ message: "请输入项目名称:",
45
+ validate: (v) => ((v || "").trim() ? true : "项目名称不能为空"),
46
+ },
47
+ ]);
48
+ projectName = (inputName || "").trim();
49
+ }
37
50
  if (!projectName) {
38
51
  log.error("项目名称不能为空");
39
52
  process.exit(1);
@@ -44,18 +57,20 @@ let FunNewCommand = class FunNewCommand extends base_1.Command {
44
57
  log.error(`项目目录已存在: ${projectDir}`);
45
58
  process.exit(1);
46
59
  }
60
+ const templateId = yield (0, templates_1.selectTemplate)("cloud-dev", { template });
61
+ const templateDir = (0, templates_1.resolveTemplatePath)("cloud-dev", templateId);
62
+ if (!fs_1.default.existsSync(templateDir)) {
63
+ log.error(`模板目录不存在: ${templateDir}`);
64
+ throw new Error(`模板目录不存在: ${templateDir}`);
65
+ }
47
66
  log.breakLine();
48
67
  console.log(chalk_1.default.bold.blue("📋 云函数项目创建信息"));
49
68
  console.log(chalk_1.default.gray("─".repeat(40)));
50
69
  console.log(chalk_1.default.bold("创建目录: ") + chalk_1.default.green(projectDir));
51
70
  console.log(chalk_1.default.bold("目录名称: ") + chalk_1.default.cyan(projectName));
71
+ console.log(chalk_1.default.bold("使用模板: ") + chalk_1.default.cyan(templateId));
52
72
  console.log(chalk_1.default.gray("─".repeat(40)));
53
73
  log.breakLine();
54
- const templateDir = path_1.default.join(__dirname, "../../../", "templates", "ai-cloud-functions-example");
55
- if (!fs_1.default.existsSync(templateDir)) {
56
- log.error(`模板目录不存在: ${templateDir}`);
57
- throw new Error(`模板目录不存在: ${templateDir}`);
58
- }
59
74
  const spinnerCopy = require("ora")("复制模板文件...").start();
60
75
  try {
61
76
  yield this.copyTemplate(templateDir, projectDir);
@@ -71,6 +86,13 @@ let FunNewCommand = class FunNewCommand extends base_1.Command {
71
86
  log.info(` - cd ${projectDir}`);
72
87
  log.info(" - npm install");
73
88
  log.info(" - npm run dev");
89
+ log.breakLine();
90
+ console.log(chalk_1.default.bgYellow.black(" ⚠️ 重要:配置云环境 "));
91
+ console.log(chalk_1.default.yellow("云函数发布前需指定目标云环境:"));
92
+ console.log(chalk_1.default.gray(" • 前往开放平台创建云环境(若尚未创建)"));
93
+ console.log(chalk_1.default.cyan(" • rcb env list — 查看当前账号下的云环境列表"));
94
+ console.log(chalk_1.default.cyan(" • rcb env set -e <envId> — 指定当前云函数要发布的环境"));
95
+ log.breakLine();
74
96
  });
75
97
  }
76
98
  copyTemplate(sourceDir, targetDir) {
@@ -107,6 +129,10 @@ let FunNewCommand = class FunNewCommand extends base_1.Command {
107
129
  flags: "-p, --projectName <projectName>",
108
130
  desc: "指定项目名称,默认为当前目录名称",
109
131
  },
132
+ {
133
+ flags: "-t, --template <template>",
134
+ desc: "指定模板 ID,不传则交互式选择",
135
+ },
110
136
  ],
111
137
  };
112
138
  }
package/lib/core/base.js CHANGED
@@ -8,11 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.Command = exports.registrableCommands = void 0;
13
16
  exports.ICommand = ICommand;
14
17
  const cloud_toolkit_1 = require("@vectorx/cloud-toolkit");
15
18
  const commander_1 = require("commander");
19
+ const ora_1 = __importDefault(require("ora"));
16
20
  exports.registrableCommands = [];
17
21
  function ICommand() {
18
22
  return (target) => {
@@ -94,6 +98,7 @@ class Command {
94
98
  });
95
99
  instance.description(desc);
96
100
  instance.action((...args) => __awaiter(this, void 0, void 0, function* () {
101
+ const loading = (0, ora_1.default)({ text: "执行中...", color: "cyan" }).start();
97
102
  const params = args.slice(0, -1);
98
103
  const commandObj = args[args.length - 1];
99
104
  const cmdOptions = instance.opts();
@@ -133,7 +138,9 @@ class Command {
133
138
  let cmdPath = rawCmdPath ? normalizeCmdPath(rawCmdPath) : buildFullCmdPath();
134
139
  const expectedParts = [cmd];
135
140
  if (this.options.childCmd) {
136
- expectedParts.push(typeof this.options.childCmd === "string" ? this.options.childCmd : this.options.childCmd.cmd);
141
+ expectedParts.push(typeof this.options.childCmd === "string"
142
+ ? this.options.childCmd
143
+ : this.options.childCmd.cmd);
137
144
  }
138
145
  if (this.options.childSubCmd) {
139
146
  expectedParts.push(this.options.childSubCmd);
@@ -154,6 +161,7 @@ class Command {
154
161
  });
155
162
  const startTime = Date.now();
156
163
  const ctx = { cmd, params, options: cmdOptions };
164
+ loading.stop();
157
165
  try {
158
166
  const res = yield this.execute(ctx);
159
167
  tracker.reportSuccess(Date.now() - startTime);
@@ -35,7 +35,8 @@ function AuthGuard() {
35
35
  cloud_toolkit_1.logger.error(`Warning: 执行当前命令需要先登录~`);
36
36
  cloud_toolkit_1.logger.breakLine();
37
37
  console.log(chalk_1.default.yellow("💡提示:请先执行登录命令"));
38
- console.log(chalk_1.default.gray(" 使用命令: ") + chalk_1.default.cyan("rcb login --secretId <secretId> --secretKey <secretKey>"));
38
+ console.log(chalk_1.default.gray(" 使用命令: ") +
39
+ chalk_1.default.cyan("rcb login --secretId <secretId> --secretKey <secretKey>"));
39
40
  cloud_toolkit_1.logger.breakLine();
40
41
  console.log(chalk_1.default.gray("🔗 获取登录秘钥:https://miniapp.xiaohongshu.com/console-panel"));
41
42
  cloud_toolkit_1.logger.breakLine();
package/lib/main.js CHANGED
@@ -17,7 +17,9 @@ require("./commands");
17
17
  function registerCommands() {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
19
  try {
20
- (0, cloud_toolkit_1.initCliApm)({ cliVersion: (process === null || process === void 0 ? void 0 : process.CLI_VERSION) || "unknown" });
20
+ (0, cloud_toolkit_1.initCliApm)({
21
+ cliVersion: (process === null || process === void 0 ? void 0 : process.CLI_VERSION) || "unknown",
22
+ });
21
23
  }
22
24
  catch (e) {
23
25
  }
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.checkAndGuideAgentProject = checkAndGuideAgentProject;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const cloud_toolkit_1 = require("@vectorx/cloud-toolkit");
19
+ const chalk_1 = __importDefault(require("chalk"));
20
+ const inquirer_1 = __importDefault(require("inquirer"));
21
+ const open_1 = __importDefault(require("open"));
22
+ const OPEN_PLATFORM_URL = "https://miniapp.xiaohongshu.com/app-choose";
23
+ function isPlaceholder(value) {
24
+ if (!value || !value.trim())
25
+ return true;
26
+ const v = value.trim();
27
+ return v.startsWith("[") || v.includes("从开放平台获取");
28
+ }
29
+ function readProjectConfig(workDir) {
30
+ const configPath = path_1.default.join(workDir, "project.config.json");
31
+ if (!fs_1.default.existsSync(configPath))
32
+ return null;
33
+ try {
34
+ return JSON.parse(fs_1.default.readFileSync(configPath, "utf-8"));
35
+ }
36
+ catch (_a) {
37
+ return null;
38
+ }
39
+ }
40
+ function writeProjectConfig(workDir, updates) {
41
+ var _a;
42
+ const configPath = path_1.default.join(workDir, "project.config.json");
43
+ const existing = (_a = readProjectConfig(workDir)) !== null && _a !== void 0 ? _a : {};
44
+ const next = Object.assign(Object.assign(Object.assign(Object.assign({}, existing), { agentId: updates.agentId }), (updates.name !== undefined ? { name: updates.name } : {})), (updates.desc !== undefined ? { desc: updates.desc } : {}));
45
+ fs_1.default.writeFileSync(configPath, JSON.stringify(next, null, 2), "utf-8");
46
+ }
47
+ function openUrl(url) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ try {
50
+ yield (0, open_1.default)(url);
51
+ }
52
+ catch (_a) {
53
+ console.log(chalk_1.default.cyan(`请手动访问:${url}`));
54
+ }
55
+ });
56
+ }
57
+ function checkAndGuideAgentProject(workDir) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ var _a, _b, _c, _d, _e, _f;
60
+ const config = readProjectConfig(workDir);
61
+ if (!config || !("agentId" in config)) {
62
+ return undefined;
63
+ }
64
+ const rawAgentId = config.agentId;
65
+ if (isPlaceholder(rawAgentId)) {
66
+ console.log(chalk_1.default.yellow("\n⚠️ 检测到 project.config.json 中的 agentId 尚未配置,正在引导您关联智能体..."));
67
+ return yield selectAndBindAgent(workDir);
68
+ }
69
+ try {
70
+ const detail = yield (0, cloud_toolkit_1.getAgentInfoById)(rawAgentId);
71
+ if (!detail) {
72
+ console.log(chalk_1.default.yellow(`\n⚠️ project.config.json 中的 agentId「${rawAgentId}」在当前账号下未找到,正在引导您重新关联...`));
73
+ return yield selectAndBindAgent(workDir);
74
+ }
75
+ const remoteName = (_a = detail.agentName) !== null && _a !== void 0 ? _a : "";
76
+ const remoteDesc = (_b = detail.description) !== null && _b !== void 0 ? _b : "";
77
+ const localName = (_c = config.name) !== null && _c !== void 0 ? _c : "";
78
+ const localDesc = (_d = config.desc) !== null && _d !== void 0 ? _d : "";
79
+ const localAgentId = (_e = config.agentId) !== null && _e !== void 0 ? _e : "";
80
+ const needsUpdate = localAgentId !== detail.agentId || localName !== remoteName || localDesc !== remoteDesc;
81
+ if (needsUpdate) {
82
+ writeProjectConfig(workDir, {
83
+ agentId: detail.agentId,
84
+ name: remoteName,
85
+ desc: remoteDesc,
86
+ });
87
+ console.log(chalk_1.default.green(`✅ 已同步 project.config.json(agentId: ${detail.agentId},name: ${remoteName || "(空)"},desc: ${remoteDesc || "(空)"})`));
88
+ }
89
+ else {
90
+ console.log(chalk_1.default.gray(`ℹ️ project.config.json 与开放平台一致,无需更新(agentId: ${detail.agentId})`));
91
+ }
92
+ return detail.agentId;
93
+ }
94
+ catch (err) {
95
+ console.warn(chalk_1.default.yellow(`\n⚠️ 校验 agentId 时出现错误(${(_f = err === null || err === void 0 ? void 0 : err.message) !== null && _f !== void 0 ? _f : String(err)}),将跳过校验直接启动。`));
96
+ return rawAgentId;
97
+ }
98
+ });
99
+ }
100
+ function selectAndBindAgent(workDir) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ var _a, _b, _c, _d;
103
+ let agentList = [];
104
+ try {
105
+ agentList = yield (0, cloud_toolkit_1.getAgentList)({ pageNo: 1, pageSize: 50 });
106
+ }
107
+ catch (err) {
108
+ console.warn(chalk_1.default.yellow(`\n⚠️ 获取智能体列表失败(${(_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : String(err)}),将跳过关联直接启动。`));
109
+ return undefined;
110
+ }
111
+ if (!agentList || agentList.length === 0) {
112
+ console.log("");
113
+ console.log(chalk_1.default.bold.yellow("你好,您当前的账号下没有智能体,请先去开放平台创建智能体:"));
114
+ console.log(chalk_1.default.cyan(OPEN_PLATFORM_URL));
115
+ console.log("");
116
+ yield openUrl(OPEN_PLATFORM_URL);
117
+ process.exit(0);
118
+ }
119
+ let selectedId;
120
+ if (agentList.length === 1) {
121
+ const only = agentList[0];
122
+ selectedId = only.agentId;
123
+ console.log(chalk_1.default.green(`\n✅ 检测到您账号下只有一个智能体,已自动关联:${only.agentName}(${only.agentId})`));
124
+ }
125
+ else {
126
+ console.log("");
127
+ const { chosenId } = yield inquirer_1.default.prompt([
128
+ {
129
+ type: "list",
130
+ name: "chosenId",
131
+ message: "检测到您账号下有多个智能体,请选择要开发的智能体:",
132
+ choices: agentList.map((a) => ({
133
+ name: `${a.agentName} ${chalk_1.default.gray(`(${a.agentId})`)}`,
134
+ value: a.agentId,
135
+ })),
136
+ },
137
+ ]);
138
+ selectedId = chosenId;
139
+ }
140
+ try {
141
+ const detail = yield (0, cloud_toolkit_1.getAgentInfoById)(selectedId);
142
+ if (detail) {
143
+ writeProjectConfig(workDir, {
144
+ agentId: detail.agentId,
145
+ name: (_b = detail.agentName) !== null && _b !== void 0 ? _b : "",
146
+ desc: (_c = detail.description) !== null && _c !== void 0 ? _c : "",
147
+ });
148
+ console.log(chalk_1.default.green(`✅ project.config.json 已更新(agentId: ${detail.agentId},name: ${detail.agentName || "(空)"},desc: ${detail.description || "(空)"})`));
149
+ return detail.agentId;
150
+ }
151
+ }
152
+ catch (err) {
153
+ console.warn(chalk_1.default.yellow(`\n⚠️ 获取智能体详情失败(${(_d = err === null || err === void 0 ? void 0 : err.message) !== null && _d !== void 0 ? _d : String(err)}),将直接写入 agentId。`));
154
+ }
155
+ writeProjectConfig(workDir, { agentId: selectedId });
156
+ return selectedId;
157
+ });
158
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.TEMPLATE_CATEGORIES = void 0;
16
+ exports.loadTemplatesConfig = loadTemplatesConfig;
17
+ exports.resolveTemplatePath = resolveTemplatePath;
18
+ exports.selectTemplate = selectTemplate;
19
+ const fs_1 = __importDefault(require("fs"));
20
+ const path_1 = __importDefault(require("path"));
21
+ const inquirer_1 = __importDefault(require("inquirer"));
22
+ const TEMPLATES_ROOT = path_1.default.join(__dirname, "../../templates");
23
+ exports.TEMPLATE_CATEGORIES = {
24
+ "cloud-agents": path_1.default.join(TEMPLATES_ROOT, "cloud-agents"),
25
+ "cloud-dev": path_1.default.join(TEMPLATES_ROOT, "cloud-dev"),
26
+ };
27
+ function loadTemplatesConfig(category) {
28
+ const categoryDir = exports.TEMPLATE_CATEGORIES[category];
29
+ const configPath = path_1.default.join(categoryDir, "templates.json");
30
+ if (!fs_1.default.existsSync(configPath)) {
31
+ return [];
32
+ }
33
+ try {
34
+ const content = fs_1.default.readFileSync(configPath, "utf-8");
35
+ const config = JSON.parse(content);
36
+ if (!Array.isArray(config.templates))
37
+ return [];
38
+ return config.templates.filter((t) => {
39
+ const templateDir = path_1.default.join(categoryDir, t.id);
40
+ return fs_1.default.existsSync(templateDir) && fs_1.default.statSync(templateDir).isDirectory();
41
+ });
42
+ }
43
+ catch (_a) {
44
+ return [];
45
+ }
46
+ }
47
+ function resolveTemplatePath(category, templateId) {
48
+ return path_1.default.join(exports.TEMPLATE_CATEGORIES[category], templateId);
49
+ }
50
+ function selectTemplate(category, options) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const templates = loadTemplatesConfig(category);
53
+ if (templates.length === 0) {
54
+ throw new Error(`未找到可用模板,请检查 ${exports.TEMPLATE_CATEGORIES[category]}/templates.json`);
55
+ }
56
+ if (options.template) {
57
+ const found = templates.find((t) => t.id === options.template);
58
+ if (found)
59
+ return found.id;
60
+ throw new Error(`模板 "${options.template}" 不存在。可用: ${templates.map((t) => t.id).join(", ")}`);
61
+ }
62
+ const { templateId } = yield inquirer_1.default.prompt([
63
+ {
64
+ type: "list",
65
+ name: "templateId",
66
+ message: "请选择模板:",
67
+ choices: templates.map((t) => ({
68
+ name: `${t.name} - ${t.desc}`,
69
+ value: t.id,
70
+ })),
71
+ },
72
+ ]);
73
+ return templateId;
74
+ });
75
+ }
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@vectorx/xhs-cloud-cli",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "xhs-cloud-cli 主要用于如下场景: - agent 调试、发布 - cloud 云函数的调试、部署",
5
+ "engines": {
6
+ "node": ">=20.0.0"
7
+ },
5
8
  "main": "lib/index.js",
6
9
  "types": "types/index.d.ts",
7
10
  "bin": {
@@ -16,10 +19,6 @@
16
19
  ],
17
20
  "dependencies": {
18
21
  "@koa/router": "^12.0.1",
19
- "@vectorx/agent-simulator": "1.0.0",
20
- "@vectorx/cloud-toolkit": "1.0.0",
21
- "@vectorx/function-debugger": "1.0.0",
22
- "@vectorx/functions-framework": "1.0.0",
23
22
  "address": "^1.1.2",
24
23
  "archiver": "^6.0.1",
25
24
  "camelcase-keys": "^7.0.2",
@@ -66,7 +65,11 @@
66
65
  "ws": "^8.18.2",
67
66
  "xdg-basedir": "^4.0.0",
68
67
  "yargs": "^16.2.0",
69
- "yargs-parser": "^21.0.1"
68
+ "yargs-parser": "^21.0.1",
69
+ "@vectorx/agent-simulator": "1.1.0",
70
+ "@vectorx/cloud-toolkit": "2.0.0",
71
+ "@vectorx/function-debugger": "1.1.0",
72
+ "@vectorx/functions-framework": "1.1.0"
70
73
  },
71
74
  "devDependencies": {
72
75
  "@types/archiver": "^5.3.2",
@@ -100,6 +103,5 @@
100
103
  "test": "echo \"Error: no test specified\" && exit 1",
101
104
  "dev": "rimraf lib types && tsc -w",
102
105
  "gen:command": "ts-node scripts/generate-command.ts"
103
- },
104
- "readme": "# xhs cloud 命令行工具\nxhs-cloud-cli 主要用于如下场景:\n - agent 调试、发布\n - cloud 云函数的调试、部署\n\n使用方式:\n - xhs-cloud-cli agent dev \n\n - xhs-cloud-cli cloud dev\n\n## 安装 CloudBase CLI\n\nsrc/\n├── commands/\n│ └── agent/\n│ └── dev.ts # agent dev 命令实现\n├── core/\n│ └── server/\n│ ├── server.node.ts # Koa 服务器实现\n│ └── function-loader.ts # 函数加载器实现\n\n### npm\n\n```shell\nnpm install -g @cloudbase/cli\n```\n\n## 命令\n> xhs-cloud-cli\n\n## 命令\n- deploy\n- dev\n\n"
106
+ }
105
107
  }
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "[从开放平台获取]",
3
+ "agentId": "[从开放平台获取]",
4
+ "desc": "基础版本智能体模板",
5
+ "version": "0.0.1"
6
+ }
@@ -10,7 +10,6 @@ let test_answer = "你好,我是开放平台智能体,我可以为你做很
10
10
  */
11
11
  class MyAgent extends AgentRuntime {
12
12
  async sendMessage(message) {
13
- console.log("==== message ====", JSON.stringify(message, null, 2));
14
13
  return new Promise((res) => {
15
14
  const charArr = test_answer.split("")
16
15
 
@@ -0,0 +1 @@
1
+ # 环境变量模板,复制为 .env 后填写
@@ -0,0 +1,11 @@
1
+ {
2
+ "functionsRoot": "./src",
3
+ "functions": [
4
+ {
5
+ "triggerPath": "/",
6
+ "name": "main",
7
+ "directory": "./",
8
+ "source": "index.js"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "description": "文档理解智能体模板(最简版)",
4
+ "scripts": {
5
+ "dev": "rcb-ff"
6
+ },
7
+ "dependencies": {
8
+ "@vectorx/agent-runtime": "latest",
9
+ "@vectorx/functions-framework": "latest"
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "[从开放平台获取]",
3
+ "agentId": "[从开放平台获取]",
4
+ "desc": "文档理解智能体(最简版)",
5
+ "version": "0.0.1"
6
+ }
@@ -0,0 +1,68 @@
1
+ const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
2
+
3
+ class DocAgent extends AgentRuntime {
4
+ async sendMessage(message) {
5
+ try {
6
+ const userContent = [];
7
+
8
+ // 1. 添加文档 URL(标准上传的文件)
9
+ if (Array.isArray(message?.files) && message.files.length > 0) {
10
+ message.files.forEach((file) => {
11
+ if ((file.fileSource === "standard" || !file.fileSource) && file.url) {
12
+ userContent.push({
13
+ type: "doc_url",
14
+ doc_url: [file.url],
15
+ file_parsing_strategy: "auto",
16
+ });
17
+ }
18
+ });
19
+ }
20
+
21
+ // 2. 添加用户文本提示
22
+ const userPrompt = message?.msg || "请分析这个文档";
23
+ if (userPrompt.trim()) {
24
+ userContent.push({ type: "text", text: userPrompt.trim() });
25
+ }
26
+
27
+ if (userContent.length === 0) {
28
+ this.sseSender.send({
29
+ data: {
30
+ choices: [{
31
+ message: { role: "assistant", type: "error", content: "请上传文档或输入分析提示" },
32
+ finish_reason: "error",
33
+ }],
34
+ },
35
+ });
36
+ this.sseSender.end();
37
+ return;
38
+ }
39
+
40
+ const model = this.createModel("qwen-doc-turbo");
41
+ const modelResponse = await model.streamText({
42
+ messages: [{ role: "user", content: userContent }],
43
+ parameters: { temperature: 0.3, max_tokens: 2000 },
44
+ enable_thinking: true,
45
+ });
46
+
47
+ for await (const chunk of modelResponse) {
48
+ this.sseSender.send({ data: chunk });
49
+ }
50
+ } catch (error) {
51
+ console.error("DocAgent error:", error);
52
+ this.sseSender.send({
53
+ data: {
54
+ choices: [{
55
+ message: { role: "assistant", type: "error", content: "文档分析出错:" + (error?.message || "Unknown") },
56
+ finish_reason: "error",
57
+ }],
58
+ },
59
+ });
60
+ } finally {
61
+ this.sseSender.end();
62
+ }
63
+ }
64
+ }
65
+
66
+ exports.main = (event, context) => {
67
+ return AgentDriver.run(event, context, new DocAgent(context));
68
+ };
@@ -0,0 +1 @@
1
+ # 环境变量模板,复制为 .env 后填写
@@ -0,0 +1,11 @@
1
+ {
2
+ "functionsRoot": "./src",
3
+ "functions": [
4
+ {
5
+ "triggerPath": "/",
6
+ "name": "main",
7
+ "directory": "./",
8
+ "source": "index.js"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "description": "文生图智能体模板(最简版)",
4
+ "scripts": {
5
+ "dev": "rcb-ff"
6
+ },
7
+ "dependencies": {
8
+ "@vectorx/agent-runtime": "latest",
9
+ "@vectorx/functions-framework": "latest"
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "[从开放平台获取]",
3
+ "agentId": "[从开放平台获取]",
4
+ "desc": "文生图智能体(最简版)",
5
+ "version": "0.0.1"
6
+ }
@@ -0,0 +1,56 @@
1
+ const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
2
+
3
+ class ImageAgent extends AgentRuntime {
4
+ async sendMessage(message) {
5
+ try {
6
+ if (!message?.msg) {
7
+ this.sseSender.send({
8
+ data: {
9
+ choices: [{
10
+ message: { role: "assistant", type: "error", content: "请输入图片描述" },
11
+ finish_reason: "error",
12
+ }],
13
+ },
14
+ });
15
+ this.sseSender.end();
16
+ return;
17
+ }
18
+
19
+ const model = this.createModel("qwen-image");
20
+ const modelResponse = await model.streamText({
21
+ messages: [
22
+ {
23
+ role: "user",
24
+ content: [{ type: "text", text: message.msg }],
25
+ },
26
+ ],
27
+ parameters: {
28
+ negative_prompt: "",
29
+ prompt_extend: true,
30
+ watermark: true,
31
+ size: "1328*1328",
32
+ },
33
+ });
34
+
35
+ for await (const chunk of modelResponse) {
36
+ this.sseSender.send({ data: chunk });
37
+ }
38
+ } catch (error) {
39
+ console.error("ImageAgent error:", error);
40
+ this.sseSender.send({
41
+ data: {
42
+ choices: [{
43
+ message: { role: "assistant", type: "error", content: "图片生成出错:" + (error?.message || "Unknown") },
44
+ finish_reason: "error",
45
+ }],
46
+ },
47
+ });
48
+ } finally {
49
+ this.sseSender.end();
50
+ }
51
+ }
52
+ }
53
+
54
+ exports.main = (event, context) => {
55
+ return AgentDriver.run(event, context, new ImageAgent(context));
56
+ };
@@ -0,0 +1 @@
1
+ # 环境变量模板,复制为 .env 后填写
@@ -0,0 +1,11 @@
1
+ {
2
+ "functionsRoot": "./src",
3
+ "functions": [
4
+ {
5
+ "triggerPath": "/",
6
+ "name": "main",
7
+ "directory": "./",
8
+ "source": "index.js"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "description": "文本模型智能体模板(最简版)",
4
+ "scripts": {
5
+ "dev": "rcb-ff"
6
+ },
7
+ "dependencies": {
8
+ "@vectorx/agent-runtime": "latest",
9
+ "@vectorx/functions-framework": "latest"
10
+ }
11
+ }