@cnbcool/cnb-api-generate 1.2.8 → 2.0.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 (35) hide show
  1. package/built/codegen/printer/printer-skills-client-core.js +10 -0
  2. package/built/utils/clean-array-desc.js +9 -0
  3. package/built/utils/collect-used-keys.js +16 -0
  4. package/built/utils/flat-option.js +2 -0
  5. package/built/utils/flatten-array-object-options.js +35 -0
  6. package/built/utils/flatten-nested-object-options.js +40 -0
  7. package/built/utils/flatten-tool-options.js +108 -0
  8. package/built/utils/generate-flat-options.js +50 -0
  9. package/built/utils/is-array-of-objects.js +10 -0
  10. package/built/utils/is-nested-object.js +9 -0
  11. package/built/utils/option-value-flag.js +14 -0
  12. package/built/utils/to-display-key.js +10 -0
  13. package/built/utils/trim-summary.js +12 -0
  14. package/client/index.ts +29 -504
  15. package/client/lib/execute-action.ts +126 -0
  16. package/client/lib/extra-help.ts +15 -0
  17. package/client/lib/flat-options-data.ts +13 -0
  18. package/client/lib/format-output.ts +78 -0
  19. package/client/lib/format-params.ts +220 -0
  20. package/client/lib/help-data.ts +13 -0
  21. package/client/lib/key-mapping-data.ts +13 -0
  22. package/client/lib/parsers.ts +130 -0
  23. package/client/lib/register-fallback.ts +14 -0
  24. package/client/lib/register-modules.ts +121 -0
  25. package/client/lib/summary-extractors.ts +189 -0
  26. package/client/lib/trim-summary.ts +11 -0
  27. package/client/shortcuts.ts +10 -198
  28. package/client/utils/build-nested-field-map.ts +38 -0
  29. package/client/utils/flat-key-from-segments.ts +8 -0
  30. package/client/utils/match-array-object-field.ts +19 -0
  31. package/client/utils/restore-original-keys.ts +23 -0
  32. package/package.json +3 -2
  33. package/client/modules.help.ts +0 -49
  34. package/client/schemaToJson.ts +0 -26
  35. package/client/tools.help.ts +0 -124
@@ -1,124 +0,0 @@
1
- import { trimSummary } from './modules.help';
2
-
3
- /**
4
- * 显示工具帮助文档
5
- * - path 和 query 参数混合展示为统一的 --key value 格式(LLM 不需要区分来源)
6
- * - body 参数展示顶层字段列表,从 schema.required 数组读取必填标记
7
- * - 权限直接读 help.json 中预提取的 permission 字段
8
- * @param helpData 帮助数据
9
- * @param moduleName 模块名称
10
- * @param tool 工具名称
11
- */
12
- export function showToolHelp(
13
- helpData: any,
14
- moduleName: string,
15
- tool: string,
16
- ): void {
17
- const toolHelpData = helpData.modulesHelp[moduleName][tool];
18
- if (!toolHelpData) {
19
- console.error(`工具 ${tool} 不存在`);
20
- process.exit(1);
21
- }
22
-
23
- const cliCmd = process.env.CNB_CLI_CMD || 'cnb';
24
- const { summary, permission, help } = toolHelpData;
25
- const { parameter } = help;
26
- const { path, query, body } = parameter;
27
-
28
- // 收集所有扁平参数(path + query 混合展示)
29
- const paramLines: string[] = [];
30
- const exampleParts: string[] = [];
31
-
32
- // path 参数
33
- if (path) {
34
- for (const [key, param] of Object.entries(path) as any) {
35
- const req = param.required ? ',必填' : '';
36
- paramLines.push(` --${key.padEnd(14)} (${param.type}${req}) ${param.description || ''}`);
37
- if (param.required) {
38
- exampleParts.push(`--${key} <${param.type}>`);
39
- }
40
- }
41
- }
42
-
43
- // query 参数
44
- if (query) {
45
- for (const [key, param] of Object.entries(query) as any) {
46
- const req = param.required ? ',必填' : '';
47
- const enumStr = param.enum ? ` [${param.enum.join(',')}]` : '';
48
- paramLines.push(` --${key.padEnd(14)} (${param.type}${req}) ${param.description || ''}${enumStr}`);
49
- if (param.required) {
50
- exampleParts.push(`--${key} <${param.type}>`);
51
- }
52
- }
53
- }
54
-
55
- // body 参数
56
- let bodyMsg = '';
57
- let bodyExample = '';
58
- if (body) {
59
- const { schema } = body;
60
- if (schema) {
61
- const bodyLines: string[] = [];
62
- const bodyExampleObj: Record<string, string> = {};
63
- const props = schema.type === 'array'
64
- ? schema.items?.properties
65
- : schema.properties;
66
-
67
- // Swagger 的 required 是父级数组,不在每个 property 上
68
- const requiredFields = new Set<string>(
69
- schema.required || schema.items?.required || []
70
- );
71
-
72
- if (props) {
73
- for (const [key, prop] of Object.entries(props) as any) {
74
- const type = prop.type || 'object';
75
- const isRequired = requiredFields.has(key);
76
- const req = isRequired ? ',必填' : '';
77
- // body 属性的 description 在生成时未 trim,运行时 trim
78
- const desc = trimSummary(prop.description || '');
79
- bodyLines.push(` ${key.padEnd(16)} (${type}${req}) ${desc}`);
80
- if (isRequired) {
81
- bodyExampleObj[key] = `<${type}>`;
82
- }
83
- }
84
- }
85
-
86
- if (bodyLines.length > 0) {
87
- bodyMsg = `\nBody (--data JSON):\n${bodyLines.join('\n')}`;
88
- }
89
- if (Object.keys(bodyExampleObj).length > 0) {
90
- bodyExample = ` --data '${JSON.stringify(bodyExampleObj)}'`;
91
- } else if (bodyLines.length > 0) {
92
- // 没有 required 标记时,取前两个字段作为示例
93
- const sampleKeys = Object.keys(props).slice(0, 2);
94
- const sampleObj: Record<string, string> = {};
95
- for (const k of sampleKeys) {
96
- sampleObj[k] = `<${props[k].type || 'string'}>`;
97
- }
98
- if (sampleKeys.length > 0) {
99
- bodyExample = ` --data '${JSON.stringify(sampleObj)}'`;
100
- }
101
- }
102
- }
103
- }
104
-
105
- // 构建输出
106
- let output = `${moduleName} ${tool} - ${summary}`;
107
- if (permission) {
108
- output += `\n权限: ${permission}`;
109
- }
110
-
111
- if (paramLines.length > 0) {
112
- output += `\n\n参数:\n${paramLines.join('\n')}`;
113
- }
114
-
115
- if (bodyMsg) {
116
- output += bodyMsg;
117
- }
118
-
119
- // 示例
120
- const exampleCmd = `${cliCmd} ${moduleName} ${tool}${exampleParts.length > 0 ? ' ' + exampleParts.join(' ') : ''}${bodyExample}`;
121
- output += `\n\n示例: ${exampleCmd}`;
122
-
123
- console.log(output);
124
- }