@xdxer/dingtalk-agent 0.1.4-beta.10 → 0.1.4-beta.12

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.
@@ -0,0 +1,202 @@
1
+ // Managed Agent Platform 注册表与工作区归属解析。
2
+ //
3
+ // 一个 Agent 工作区归属于至多一个托管平台;归属解析顺序固定:
4
+ // DTA_AGENT_PLATFORM 环境变量 > dingtalk-agent.json#agentPlatform
5
+ // > .dingtalk-agent/agent-platform.json > 按 Provider 声明推断。
6
+ // 平台相关的方法学不写进 CLI——它们随对应平台的 Skill 包分发,只在用户选择该平台时安装。
7
+ // 平台命令(如 deploy/promote/observe)必须先通过归属门禁;coming-soon 平台一律 fail-closed。
8
+ import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
9
+ import { homedir } from 'node:os';
10
+ import { dirname, join, parse, resolve } from 'node:path';
11
+ import { findAgentProjectRoot } from './development-workspace.js';
12
+ import { findExecutable } from './doctor.js';
13
+ import { installGlobalSkill } from './skill-manager.js';
14
+ export const MANAGED_AGENT_PLATFORMS = [
15
+ {
16
+ name: 'multica-dingtalk',
17
+ label: 'Multica (DingTalk)',
18
+ status: 'supported',
19
+ description: '钉钉 Multica 托管平台:供给、部署、钉钉机器人绑定、观测与调度。',
20
+ skills: ['dingtalk-agent-deploy', 'dingtalk-agent-boot-multica', 'multica-fde-external'],
21
+ commands: ['deploy', 'promote', 'observe'],
22
+ },
23
+ {
24
+ name: 'deap',
25
+ label: 'DEAP',
26
+ status: 'coming-soon',
27
+ description: '敬请期待。',
28
+ skills: [],
29
+ commands: [],
30
+ },
31
+ ];
32
+ export const AGENT_PLATFORM_FILE = '.dingtalk-agent/agent-platform.json';
33
+ const PROJECT_FILE = 'dingtalk-agent.json';
34
+ export function agentPlatformDefinition(name) {
35
+ return MANAGED_AGENT_PLATFORMS.find((item) => item.name === name) || null;
36
+ }
37
+ export function resolveAgentPlatform(start = process.cwd(), env = process.env) {
38
+ const fromEnv = String(env.DTA_AGENT_PLATFORM || '').trim();
39
+ if (fromEnv)
40
+ return resolution(fromEnv, 'env', '');
41
+ const projectRoot = findAgentProjectRoot(start);
42
+ if (projectRoot) {
43
+ const manifestPath = join(projectRoot, PROJECT_FILE);
44
+ if (existsSync(manifestPath) && !lstatSync(manifestPath).isSymbolicLink()) {
45
+ const manifest = readJsonOrNull(manifestPath);
46
+ const declared = typeof manifest?.agentPlatform === 'string' ? manifest.agentPlatform.trim() : '';
47
+ if (declared)
48
+ return resolution(declared, 'project-manifest', manifestPath);
49
+ }
50
+ }
51
+ const platformPath = findUp(start, AGENT_PLATFORM_FILE);
52
+ if (platformPath) {
53
+ const stored = readJsonOrNull(platformPath);
54
+ const declared = typeof stored?.agentPlatform === 'string' ? stored.agentPlatform.trim() : '';
55
+ if (declared)
56
+ return resolution(declared, 'platform-file', platformPath);
57
+ }
58
+ if (projectRoot) {
59
+ const manifest = readJsonOrNull(join(projectRoot, PROJECT_FILE));
60
+ const providers = Object.values(manifest?.workspaces || {})
61
+ .map((workspace) => workspace?.provider?.kind);
62
+ if (providers.includes('multica')) {
63
+ return resolution('multica-dingtalk', 'inferred', join(projectRoot, PROJECT_FILE));
64
+ }
65
+ }
66
+ return resolution(null, 'none', '');
67
+ }
68
+ export function useAgentPlatform(start, name, options) {
69
+ const definition = agentPlatformDefinition(name);
70
+ if (!definition) {
71
+ throw new Error(`未知 managed agent platform: ${name}\n` +
72
+ `可用: ${MANAGED_AGENT_PLATFORMS.map((item) => `${item.name}(${statusLabel(item.status)})`).join('、')}`);
73
+ }
74
+ if (definition.status !== 'supported') {
75
+ throw new Error(`${definition.label} 敬请期待——当前版本尚未开放该平台。`);
76
+ }
77
+ const projectRoot = findAgentProjectRoot(start);
78
+ let written;
79
+ if (projectRoot && existsSync(join(projectRoot, PROJECT_FILE))) {
80
+ const manifestPath = join(projectRoot, PROJECT_FILE);
81
+ if (lstatSync(manifestPath).isSymbolicLink()) {
82
+ throw new Error(`${PROJECT_FILE} 不能是符号链接`);
83
+ }
84
+ const manifest = readJsonOrNull(manifestPath);
85
+ if (!manifest || typeof manifest !== 'object') {
86
+ throw new Error(`${PROJECT_FILE} 不是合法 JSON,无法写入 agentPlatform 归属`);
87
+ }
88
+ manifest.agentPlatform = definition.name;
89
+ writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n');
90
+ written = manifestPath;
91
+ }
92
+ else {
93
+ const target = join(resolve(start), AGENT_PLATFORM_FILE);
94
+ mkdirSync(dirname(target), { recursive: true });
95
+ writeFileSync(target, JSON.stringify({ $schema: 'dingtalk-agent/agent-platform@1', agentPlatform: definition.name }, null, 2) + '\n');
96
+ written = target;
97
+ }
98
+ const skillInstalls = [];
99
+ if (options.installSkills !== false) {
100
+ for (const skill of definition.skills) {
101
+ skillInstalls.push(installGlobalSkill(options.packageRoot, {
102
+ name: skill, home: options.home, env: options.env,
103
+ }));
104
+ }
105
+ }
106
+ return { ...resolveAgentPlatform(start, options.env || process.env), written, skillInstalls };
107
+ }
108
+ /** deploy/promote/observe 等平台命令的归属门禁:未归属、未知或 coming-soon 一律 fail-closed。 */
109
+ export function requireSupportedAgentPlatform(command, start = process.cwd(), env = process.env) {
110
+ const resolved = resolveAgentPlatform(start, env);
111
+ if (!resolved.platform) {
112
+ throw new Error(`当前工作区未声明 managed agent platform,无法执行 \`${command}\`。\n` +
113
+ '先运行 `dta agent-platform use multica-dingtalk`,或设置 DTA_AGENT_PLATFORM 环境变量。');
114
+ }
115
+ if (!resolved.definition) {
116
+ throw new Error(`工作区声明了未知 platform: ${resolved.platform}(来源 ${resolved.source})。\n` +
117
+ `可用: ${MANAGED_AGENT_PLATFORMS.map((item) => item.name).join('、')}`);
118
+ }
119
+ if (resolved.definition.status !== 'supported') {
120
+ throw new Error(`${resolved.definition.label} 敬请期待——\`${command}\` 尚未在该平台开放。`);
121
+ }
122
+ if (!resolved.definition.commands.includes(command)) {
123
+ throw new Error(`${resolved.definition.label} 不承接命令 \`${command}\`。`);
124
+ }
125
+ return resolved;
126
+ }
127
+ export function statusLabel(status) {
128
+ return status === 'supported' ? '已支持' : '敬请期待';
129
+ }
130
+ /**
131
+ * 平台工具链就绪检查(只读,不阻断):选择/查看平台归属时提示用户缺什么、怎么补。
132
+ * 强制门禁仍由 requireSupportedAgentPlatform 与各平台命令自身承担。
133
+ */
134
+ export function agentPlatformReadiness(name, env = process.env, home = homedir()) {
135
+ if (name !== 'multica-dingtalk')
136
+ return [];
137
+ const checks = [];
138
+ const cli = findExecutable('multica', env);
139
+ checks.push(cli
140
+ ? { id: 'multica-cli', ok: true, summary: `multica CLI 已安装(${cli})` }
141
+ : {
142
+ id: 'multica-cli', ok: false, summary: '未找到 multica CLI',
143
+ hint: '安装:curl -fsSL https://raw.githubusercontent.com/multica-ai/multica/main/scripts/install.sh | bash',
144
+ });
145
+ checks.push(hasMulticaToken(home)
146
+ ? { id: 'multica-login', ok: true, summary: 'Multica 已登录(本地存在 token)' }
147
+ : {
148
+ id: 'multica-login', ok: false, summary: 'Multica 尚未登录',
149
+ hint: '登录:multica login --server-url <endpoint>(预发环境为 https://pre-fde-workbench.dingtalk.com)',
150
+ });
151
+ if (['HTTPS_PROXY', 'https_proxy', 'ALL_PROXY', 'all_proxy'].some((key) => env[key])) {
152
+ checks.push({
153
+ id: 'proxy-env', ok: false, summary: '检测到代理环境变量,可能阻断 Multica 直连',
154
+ hint: '连接失败时用 env -u HTTPS_PROXY -u https_proxy -u ALL_PROXY -u all_proxy 运行 multica 与平台脚本',
155
+ });
156
+ }
157
+ return checks;
158
+ }
159
+ function hasMulticaToken(home) {
160
+ const candidates = [join(home, '.multica', 'config.json')];
161
+ try {
162
+ for (const entry of readdirSync(join(home, '.multica', 'profiles'))) {
163
+ candidates.push(join(home, '.multica', 'profiles', entry, 'config.json'));
164
+ }
165
+ }
166
+ catch { /* profiles 目录不存在 */ }
167
+ return candidates.some((path) => {
168
+ const config = readJsonOrNull(path);
169
+ return typeof config?.token === 'string' && config.token.trim().length > 0;
170
+ });
171
+ }
172
+ function resolution(name, source, file) {
173
+ return {
174
+ $schema: 'dingtalk-agent/agent-platform-resolution@1',
175
+ platform: name,
176
+ source,
177
+ file,
178
+ definition: name ? agentPlatformDefinition(name) : null,
179
+ platforms: MANAGED_AGENT_PLATFORMS,
180
+ };
181
+ }
182
+ function readJsonOrNull(path) {
183
+ try {
184
+ return JSON.parse(readFileSync(path, 'utf8'));
185
+ }
186
+ catch {
187
+ return null;
188
+ }
189
+ }
190
+ function findUp(start, relativePath) {
191
+ let dir = resolve(start);
192
+ while (true) {
193
+ const candidate = join(dir, relativePath);
194
+ if (existsSync(candidate) && !lstatSync(candidate).isSymbolicLink())
195
+ return candidate;
196
+ const parent = dirname(dir);
197
+ if (parent === dir || dir === parse(dir).root)
198
+ return null;
199
+ dir = parent;
200
+ }
201
+ }
202
+ //# sourceMappingURL=agent-platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-platform.js","sourceRoot":"","sources":["../../src/agent-platform.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,EAAE;AACF,oCAAoC;AACpC,gEAAgE;AAChE,6DAA6D;AAC7D,oDAAoD;AACpD,wEAAwE;AAExE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACpG,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,kBAAkB,EAA0B,MAAM,oBAAoB,CAAA;AAa/E,MAAM,CAAC,MAAM,uBAAuB,GAA8B;IAChE;QACE,IAAI,EAAE,kBAAkB;QACxB,KAAK,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AC,CAAA;QAC3B,IAAI,MAAM,KAAK,GAAG,IAAI,GAAG,KAAK,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI;YAAE,OAAO,IAAI,CAAA;QAC1D,GAAG,GAAG,MAAM,CAAA;IACd,CAAC;AACH,CAAC","sourcesContent":["// Managed Agent Platform 注册表与工作区归属解析。\n//\n// 一个 Agent 工作区归属于至多一个托管平台;归属解析顺序固定:\n// DTA_AGENT_PLATFORM 环境变量 > dingtalk-agent.json#agentPlatform\n// > .dingtalk-agent/agent-platform.json > 按 Provider 声明推断。\n// 平台相关的方法学不写进 CLI——它们随对应平台的 Skill 包分发,只在用户选择该平台时安装。\n// 平台命令(如 deploy/promote/observe)必须先通过归属门禁;coming-soon 平台一律 fail-closed。\n\nimport { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { dirname, join, parse, resolve } from 'node:path'\n\nimport { findAgentProjectRoot } from './development-workspace.js'\nimport { findExecutable } from './doctor.js'\nimport { installGlobalSkill, type GlobalSkillStatus } from './skill-manager.js'\n\nexport type AgentPlatformStatus = 'supported' | 'coming-soon'\n\nexport interface AgentPlatformDefinition {\n name: string\n label: string\n status: AgentPlatformStatus\n description: string\n skills: string[]\n commands: string[]\n}\n\nexport const MANAGED_AGENT_PLATFORMS: AgentPlatformDefinition[] = [\n {\n name: 'multica-dingtalk',\n label: 'Multica (DingTalk)',\n status: 'supported',\n description: '钉钉 Multica 托管平台:供给、部署、钉钉机器人绑定、观测与调度。',\n skills: ['dingtalk-agent-deploy', 'dingtalk-agent-boot-multica', 'multica-fde-external'],\n commands: ['deploy', 'promote', 'observe'],\n },\n {\n name: 'deap',\n label: 'DEAP',\n status: 'coming-soon',\n description: '敬请期待。',\n skills: [],\n commands: [],\n },\n]\n\nexport const AGENT_PLATFORM_FILE = '.dingtalk-agent/agent-platform.json'\nconst PROJECT_FILE = 'dingtalk-agent.json'\n\nexport type AgentPlatformSource = 'env' | 'project-manifest' | 'platform-file' | 'inferred' | 'none'\n\nexport interface AgentPlatformResolution {\n $schema: 'dingtalk-agent/agent-platform-resolution@1'\n platform: string | null\n source: AgentPlatformSource\n file: string\n definition: AgentPlatformDefinition | null\n platforms: AgentPlatformDefinition[]\n}\n\nexport interface AgentPlatformUseResult extends AgentPlatformResolution {\n written: string\n skillInstalls: GlobalSkillStatus[]\n}\n\nexport function agentPlatformDefinition(name: string): AgentPlatformDefinition | null {\n return MANAGED_AGENT_PLATFORMS.find((item) => item.name === name) || null\n}\n\nexport function resolveAgentPlatform(\n start = process.cwd(), env: NodeJS.ProcessEnv = process.env,\n): AgentPlatformResolution {\n const fromEnv = String(env.DTA_AGENT_PLATFORM || '').trim()\n if (fromEnv) return resolution(fromEnv, 'env', '')\n\n const projectRoot = findAgentProjectRoot(start)\n if (projectRoot) {\n const manifestPath = join(projectRoot, PROJECT_FILE)\n if (existsSync(manifestPath) && !lstatSync(manifestPath).isSymbolicLink()) {\n const manifest = readJsonOrNull(manifestPath)\n const declared = typeof manifest?.agentPlatform === 'string' ? manifest.agentPlatform.trim() : ''\n if (declared) return resolution(declared, 'project-manifest', manifestPath)\n }\n }\n\n const platformPath = findUp(start, AGENT_PLATFORM_FILE)\n if (platformPath) {\n const stored = readJsonOrNull(platformPath)\n const declared = typeof stored?.agentPlatform === 'string' ? stored.agentPlatform.trim() : ''\n if (declared) return resolution(declared, 'platform-file', platformPath)\n }\n\n if (projectRoot) {\n const manifest = readJsonOrNull(join(projectRoot, PROJECT_FILE))\n const providers = Object.values(manifest?.workspaces || {})\n .map((workspace: any) => workspace?.provider?.kind)\n if (providers.includes('multica')) {\n return resolution('multica-dingtalk', 'inferred', join(projectRoot, PROJECT_FILE))\n }\n }\n\n return resolution(null, 'none', '')\n}\n\nexport interface AgentPlatformUseOptions {\n packageRoot: string\n home?: string\n env?: NodeJS.ProcessEnv\n installSkills?: boolean\n}\n\nexport function useAgentPlatform(\n start: string, name: string, options: AgentPlatformUseOptions,\n): AgentPlatformUseResult {\n const definition = agentPlatformDefinition(name)\n if (!definition) {\n throw new Error(\n `未知 managed agent platform: ${name}\\n` +\n `可用: ${MANAGED_AGENT_PLATFORMS.map((item) => `${item.name}(${statusLabel(item.status)})`).join('、')}`,\n )\n }\n if (definition.status !== 'supported') {\n throw new Error(`${definition.label} 敬请期待——当前版本尚未开放该平台。`)\n }\n\n const projectRoot = findAgentProjectRoot(start)\n let written: string\n if (projectRoot && existsSync(join(projectRoot, PROJECT_FILE))) {\n const manifestPath = join(projectRoot, PROJECT_FILE)\n if (lstatSync(manifestPath).isSymbolicLink()) {\n throw new Error(`${PROJECT_FILE} 不能是符号链接`)\n }\n const manifest = readJsonOrNull(manifestPath)\n if (!manifest || typeof manifest !== 'object') {\n throw new Error(`${PROJECT_FILE} 不是合法 JSON,无法写入 agentPlatform 归属`)\n }\n manifest.agentPlatform = definition.name\n writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\\n')\n written = manifestPath\n } else {\n const target = join(resolve(start), AGENT_PLATFORM_FILE)\n mkdirSync(dirname(target), { recursive: true })\n writeFileSync(target, JSON.stringify(\n { $schema: 'dingtalk-agent/agent-platform@1', agentPlatform: definition.name }, null, 2,\n ) + '\\n')\n written = target\n }\n\n const skillInstalls: GlobalSkillStatus[] = []\n if (options.installSkills !== false) {\n for (const skill of definition.skills) {\n skillInstalls.push(installGlobalSkill(options.packageRoot, {\n name: skill, home: options.home, env: options.env,\n }))\n }\n }\n\n return { ...resolveAgentPlatform(start, options.env || process.env), written, skillInstalls }\n}\n\n/** deploy/promote/observe 等平台命令的归属门禁:未归属、未知或 coming-soon 一律 fail-closed。 */\nexport function requireSupportedAgentPlatform(\n command: string, start = process.cwd(), env: NodeJS.ProcessEnv = process.env,\n): AgentPlatformResolution {\n const resolved = resolveAgentPlatform(start, env)\n if (!resolved.platform) {\n throw new Error(\n `当前工作区未声明 managed agent platform,无法执行 \\`${command}\\`。\\n` +\n '先运行 `dta agent-platform use multica-dingtalk`,或设置 DTA_AGENT_PLATFORM 环境变量。',\n )\n }\n if (!resolved.definition) {\n throw new Error(\n `工作区声明了未知 platform: ${resolved.platform}(来源 ${resolved.source})。\\n` +\n `可用: ${MANAGED_AGENT_PLATFORMS.map((item) => item.name).join('、')}`,\n )\n }\n if (resolved.definition.status !== 'supported') {\n throw new Error(`${resolved.definition.label} 敬请期待——\\`${command}\\` 尚未在该平台开放。`)\n }\n if (!resolved.definition.commands.includes(command)) {\n throw new Error(`${resolved.definition.label} 不承接命令 \\`${command}\\`。`)\n }\n return resolved\n}\n\nexport function statusLabel(status: AgentPlatformStatus): string {\n return status === 'supported' ? '已支持' : '敬请期待'\n}\n\nexport interface AgentPlatformReadinessCheck {\n id: string\n ok: boolean\n summary: string\n hint?: string\n}\n\n/**\n * 平台工具链就绪检查(只读,不阻断):选择/查看平台归属时提示用户缺什么、怎么补。\n * 强制门禁仍由 requireSupportedAgentPlatform 与各平台命令自身承担。\n */\nexport function agentPlatformReadiness(\n name: string, env: NodeJS.ProcessEnv = process.env, home = homedir(),\n): AgentPlatformReadinessCheck[] {\n if (name !== 'multica-dingtalk') return []\n const checks: AgentPlatformReadinessCheck[] = []\n const cli = findExecutable('multica', env)\n checks.push(cli\n ? { id: 'multica-cli', ok: true, summary: `multica CLI 已安装(${cli})` }\n : {\n id: 'multica-cli', ok: false, summary: '未找到 multica CLI',\n hint: '安装:curl -fsSL https://raw.githubusercontent.com/multica-ai/multica/main/scripts/install.sh | bash',\n })\n checks.push(hasMulticaToken(home)\n ? { id: 'multica-login', ok: true, summary: 'Multica 已登录(本地存在 token)' }\n : {\n id: 'multica-login', ok: false, summary: 'Multica 尚未登录',\n hint: '登录:multica login --server-url <endpoint>(预发环境为 https://pre-fde-workbench.dingtalk.com)',\n })\n if (['HTTPS_PROXY', 'https_proxy', 'ALL_PROXY', 'all_proxy'].some((key) => env[key])) {\n checks.push({\n id: 'proxy-env', ok: false, summary: '检测到代理环境变量,可能阻断 Multica 直连',\n hint: '连接失败时用 env -u HTTPS_PROXY -u https_proxy -u ALL_PROXY -u all_proxy 运行 multica 与平台脚本',\n })\n }\n return checks\n}\n\nfunction hasMulticaToken(home: string): boolean {\n const candidates = [join(home, '.multica', 'config.json')]\n try {\n for (const entry of readdirSync(join(home, '.multica', 'profiles'))) {\n candidates.push(join(home, '.multica', 'profiles', entry, 'config.json'))\n }\n } catch { /* profiles 目录不存在 */ }\n return candidates.some((path) => {\n const config = readJsonOrNull(path)\n return typeof config?.token === 'string' && config.token.trim().length > 0\n })\n}\n\nfunction resolution(\n name: string | null, source: AgentPlatformSource, file: string,\n): AgentPlatformResolution {\n return {\n $schema: 'dingtalk-agent/agent-platform-resolution@1',\n platform: name,\n source,\n file,\n definition: name ? agentPlatformDefinition(name) : null,\n platforms: MANAGED_AGENT_PLATFORMS,\n }\n}\n\nfunction readJsonOrNull(path: string): any {\n try { return JSON.parse(readFileSync(path, 'utf8')) } catch { return null }\n}\n\nfunction findUp(start: string, relativePath: string): string | null {\n let dir = resolve(start)\n while (true) {\n const candidate = join(dir, relativePath)\n if (existsSync(candidate) && !lstatSync(candidate).isSymbolicLink()) return candidate\n const parent = dirname(dir)\n if (parent === dir || dir === parse(dir).root) return null\n dir = parent\n }\n}\n"]}
@@ -10,6 +10,7 @@ export const CURRENT_WORKSPACE_FILE = '.dingtalk-agent/current-workspace';
10
10
  export const WORKSPACE_STATE_ROOT = '.dingtalk-agent/state/workspaces';
11
11
  export const LEGACY_WORKSPACE_NAME = 'legacy-prepared';
12
12
  const PROJECT_KEYS = new Set(['$schema', 'name', 'dtaVersion', 'agent', 'workspaces']);
13
+ const PROJECT_OPTIONAL_KEYS = new Set([...PROJECT_KEYS, 'agentPlatform']);
13
14
  const AGENT_KEYS = new Set(['definition', 'skills']);
14
15
  const WORKSPACE_KEYS = new Set(['stage', 'provider', 'storage', 'authority']);
15
16
  const OPENCODE_KEYS = new Set(['kind', 'model']);
@@ -187,12 +188,17 @@ function loadAgentProject(start) {
187
188
  }
188
189
  function validateProjectManifest(value, root) {
189
190
  const input = objectValue(value, 'Project manifest');
190
- assertExactKeys(input, PROJECT_KEYS, 'Project manifest');
191
+ assertAllowedKeys(input, PROJECT_OPTIONAL_KEYS, 'Project manifest');
192
+ const missing = [...PROJECT_KEYS].filter((key) => !(key in input));
193
+ if (missing.length)
194
+ throw new Error(`Project manifest 缺少字段: ${missing.join(', ')}`);
191
195
  if (input.$schema !== 'dingtalk-agent/project@1') {
192
196
  throw new Error('Project manifest 缺少 dingtalk-agent/project@1 schema');
193
197
  }
194
198
  const name = stableName(input.name, 'Project name');
195
199
  const dtaVersion = requiredString(input.dtaVersion, 'dtaVersion');
200
+ const agentPlatform = input.agentPlatform === undefined
201
+ ? undefined : requiredString(input.agentPlatform, 'agentPlatform');
196
202
  const agentInput = objectValue(input.agent, 'Project agent');
197
203
  assertExactKeys(agentInput, AGENT_KEYS, 'Project agent');
198
204
  const definition = safeRelativePath(root, requiredString(agentInput.definition, 'agent.definition'), 'agent.definition', false);
@@ -216,6 +222,7 @@ function validateProjectManifest(value, root) {
216
222
  }
217
223
  return {
218
224
  $schema: 'dingtalk-agent/project@1', name, dtaVersion,
225
+ ...(agentPlatform === undefined ? {} : { agentPlatform }),
219
226
  agent: { definition, skills }, workspaces,
220
227
  };
221
228
  }