|
| 1 | +import { defineCommand } from "bailian-cli-core"; |
| 2 | +import { |
| 3 | + APP_CONTROL_CREATE_API, |
| 4 | + APP_DATA_CREATE_DRAFT_API, |
| 5 | + APP_TYPE_AGENT, |
| 6 | + CONFIG_FLAGS, |
| 7 | + APP_TYPE_FLAG, |
| 8 | + consoleCall, |
| 9 | + consoleDryRunPreview, |
| 10 | + extractAppCode, |
| 11 | + readConfigJson, |
| 12 | + resolveAppType, |
| 13 | + validateConfigFlags, |
| 14 | +} from "./shared.ts"; |
| 15 | + |
| 16 | +/** Default agent config aligned with the console create modal (new-agent). */ |
| 17 | +function defaultAgentConfig(model?: string): Record<string, unknown> { |
| 18 | + return { |
| 19 | + model: model || "auto", |
| 20 | + model_config: { |
| 21 | + temperature: 0.7, |
| 22 | + enable_thinking: true, |
| 23 | + thinking_budget: 4000, |
| 24 | + }, |
| 25 | + dialog_round: 3, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +export default defineCommand({ |
| 30 | + description: { |
| 31 | + "en-US": "Create a Bailian application (agent or workflow)", |
| 32 | + "zh-CN": "创建百炼应用(智能体或工作流)", |
| 33 | + }, |
| 34 | + auth: "console", |
| 35 | + usageArgs: "--name <name> [flags]", |
| 36 | + flags: { |
| 37 | + name: { |
| 38 | + type: "string", |
| 39 | + valueHint: "<name>", |
| 40 | + description: { "en-US": "Application name (required)", "zh-CN": "应用名称(必填)" }, |
| 41 | + required: true, |
| 42 | + }, |
| 43 | + ...APP_TYPE_FLAG, |
| 44 | + description: { |
| 45 | + type: "string", |
| 46 | + valueHint: "<text>", |
| 47 | + description: { "en-US": "Application description", "zh-CN": "应用描述" }, |
| 48 | + }, |
| 49 | + icon: { |
| 50 | + type: "string", |
| 51 | + valueHint: "<key>", |
| 52 | + description: { "en-US": "Application icon key", "zh-CN": "应用图标 key" }, |
| 53 | + }, |
| 54 | + model: { |
| 55 | + type: "string", |
| 56 | + valueHint: "<model>", |
| 57 | + description: { |
| 58 | + "en-US": "Agent model ID (default: auto; agent only)", |
| 59 | + "zh-CN": "智能体模型 ID(默认:auto;仅 agent 有效)", |
| 60 | + }, |
| 61 | + }, |
| 62 | + ...CONFIG_FLAGS, |
| 63 | + }, |
| 64 | + notes: [ |
| 65 | + { |
| 66 | + "en-US": |
| 67 | + "Agent creation uses the app-control 2.0 API with a default config (model auto, dialog_round 3). --config-json/--config-file shallow-merges over the default config; --model overrides config.model.", |
| 68 | + "zh-CN": |
| 69 | + "智能体创建走 app-control 2.0 接口,携带默认配置(model auto、dialog_round 3)。--config-json/--config-file 浅合并覆盖默认配置;--model 覆盖 config.model。", |
| 70 | + }, |
| 71 | + { |
| 72 | + "en-US": |
| 73 | + "Workflow creation creates an empty draft canvas; edit it with `app workflow save` and publish with `app workflow publish`.", |
| 74 | + "zh-CN": |
| 75 | + "工作流创建为空草稿画布;用 `app workflow save` 编辑画布,用 `app workflow publish` 发布。", |
| 76 | + }, |
| 77 | + ], |
| 78 | + exampleArgs: [ |
| 79 | + { |
| 80 | + "en-US": '--name "Customer service"', |
| 81 | + "zh-CN": '--name "客户服务"', |
| 82 | + }, |
| 83 | + { |
| 84 | + "en-US": '--name "Customer service" --model qwen3-max --description "After-sales bot"', |
| 85 | + "zh-CN": '--name "客户服务" --model qwen3-max --description "售后机器人"', |
| 86 | + }, |
| 87 | + { |
| 88 | + "en-US": '--name "Order flow" --type workflow', |
| 89 | + "zh-CN": '--name "订单流程" --type workflow', |
| 90 | + }, |
| 91 | + { |
| 92 | + "en-US": '--name "My agent" --config-file ./agent-config.json', |
| 93 | + "zh-CN": '--name "我的智能体" --config-file ./agent-config.json', |
| 94 | + }, |
| 95 | + ], |
| 96 | + validate: (flags) => { |
| 97 | + const conflict = validateConfigFlags(flags); |
| 98 | + if (conflict) return conflict; |
| 99 | + if ( |
| 100 | + resolveAppType(flags) === "workflow" && |
| 101 | + (flags.model !== undefined || |
| 102 | + flags.configJson !== undefined || |
| 103 | + flags.configFile !== undefined) |
| 104 | + ) { |
| 105 | + return { |
| 106 | + "en-US": |
| 107 | + "--model/--config-json/--config-file only apply to agent creation; edit a workflow canvas with `app workflow save`", |
| 108 | + "zh-CN": |
| 109 | + "--model/--config-json/--config-file 仅适用于智能体创建;工作流画布请用 `app workflow save` 编辑", |
| 110 | + }; |
| 111 | + } |
| 112 | + return undefined; |
| 113 | + }, |
| 114 | + async run(ctx) { |
| 115 | + const { settings, flags } = ctx; |
| 116 | + const appType = resolveAppType(flags); |
| 117 | + const userConfig = await readConfigJson(flags); |
| 118 | + |
| 119 | + let api: string; |
| 120 | + let data: Record<string, unknown>; |
| 121 | + if (appType === "workflow") { |
| 122 | + api = APP_DATA_CREATE_DRAFT_API; |
| 123 | + data = { |
| 124 | + reqDTO: { |
| 125 | + name: flags.name, |
| 126 | + appDescription: flags.description, |
| 127 | + applicationConfigDTO: {}, |
| 128 | + createType: "Workflow", |
| 129 | + modelId: "", |
| 130 | + icon: flags.icon, |
| 131 | + subType: "chat", |
| 132 | + }, |
| 133 | + }; |
| 134 | + } else { |
| 135 | + const config = { ...defaultAgentConfig(flags.model), ...userConfig }; |
| 136 | + if (flags.model) config.model = flags.model; |
| 137 | + api = APP_CONTROL_CREATE_API; |
| 138 | + data = { |
| 139 | + reqDTO: { |
| 140 | + name: flags.name, |
| 141 | + description: flags.description, |
| 142 | + icon: flags.icon, |
| 143 | + source: "console", |
| 144 | + type: APP_TYPE_AGENT, |
| 145 | + status: 1, |
| 146 | + config, |
| 147 | + }, |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + if (settings.dryRun) { |
| 152 | + ctx.output.result(consoleDryRunPreview(settings, api, data)); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + const payload = await consoleCall(ctx.client, api, data); |
| 157 | + const appCode = extractAppCode(payload); |
| 158 | + ctx.output.result( |
| 159 | + { app_code: appCode ?? null, result: payload }, |
| 160 | + settings.output === "text" |
| 161 | + ? { |
| 162 | + text: ctx.localize({ |
| 163 | + "en-US": `Application created: ${appCode ?? "(see result)"}`, |
| 164 | + "zh-CN": `应用已创建:${appCode ?? "(见 result)"}`, |
| 165 | + }), |
| 166 | + } |
| 167 | + : undefined, |
| 168 | + ); |
| 169 | + }, |
| 170 | +}); |
0 commit comments