@lcap/nasl 3.8.0-beta.8 → 3.8.2-alpha.1

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 (113) hide show
  1. package/.nyc_output/fe4ca6db-1eb6-4898-8aa5-ba8f8d4128f2.json +1 -0
  2. package/.nyc_output/processinfo/fe4ca6db-1eb6-4898-8aa5-ba8f8d4128f2.json +1 -0
  3. package/.nyc_output/processinfo/index.json +1 -1
  4. package/coverage/cobertura-coverage.xml +9325 -77393
  5. package/out/concepts/AuthLogicForCallInterface__.js +13 -2
  6. package/out/concepts/AuthLogicForCallInterface__.js.map +1 -1
  7. package/out/concepts/BackendVariable__.js +11 -1
  8. package/out/concepts/BackendVariable__.js.map +1 -1
  9. package/out/concepts/BindDirective__.js +1 -1
  10. package/out/concepts/BindDirective__.js.map +1 -1
  11. package/out/concepts/BindStyle__.js +1 -1
  12. package/out/concepts/BindStyle__.js.map +1 -1
  13. package/out/concepts/BusinessComponent__.js +14 -3
  14. package/out/concepts/BusinessComponent__.js.map +1 -1
  15. package/out/concepts/BusinessLogic__.js +13 -2
  16. package/out/concepts/BusinessLogic__.js.map +1 -1
  17. package/out/concepts/CallConnector__.js +2 -1
  18. package/out/concepts/CallConnector__.js.map +1 -1
  19. package/out/concepts/CallInterface__.js +12 -3
  20. package/out/concepts/CallInterface__.js.map +1 -1
  21. package/out/concepts/CallLogic__.js +24 -1
  22. package/out/concepts/CallLogic__.js.map +1 -1
  23. package/out/concepts/EntityProperty__.js +3 -2
  24. package/out/concepts/EntityProperty__.js.map +1 -1
  25. package/out/concepts/Entity__.js +1 -1
  26. package/out/concepts/Entity__.js.map +1 -1
  27. package/out/concepts/EnumItem__.js +2 -2
  28. package/out/concepts/EnumItem__.js.map +1 -1
  29. package/out/concepts/Interface__.js +1 -1
  30. package/out/concepts/Interface__.js.map +1 -1
  31. package/out/concepts/Logic__.js +13 -2
  32. package/out/concepts/Logic__.js.map +1 -1
  33. package/out/concepts/NewComposite__.d.ts +1 -0
  34. package/out/concepts/NewComposite__.js +23 -0
  35. package/out/concepts/NewComposite__.js.map +1 -1
  36. package/out/concepts/OverriddenLogic__.js +13 -2
  37. package/out/concepts/OverriddenLogic__.js.map +1 -1
  38. package/out/concepts/Param__.js +1 -1
  39. package/out/concepts/Param__.js.map +1 -1
  40. package/out/concepts/Structure__.js +1 -3
  41. package/out/concepts/Structure__.js.map +1 -1
  42. package/out/concepts/SubLogic__.js +13 -2
  43. package/out/concepts/SubLogic__.js.map +1 -1
  44. package/out/concepts/TypeAnnotation__.js +6 -3
  45. package/out/concepts/TypeAnnotation__.js.map +1 -1
  46. package/out/concepts/ViewComponentDeclaration__.js +3 -3
  47. package/out/concepts/ViewComponentDeclaration__.js.map +1 -1
  48. package/out/concepts/ViewElement__.d.ts +2 -0
  49. package/out/concepts/ViewElement__.js +60 -19
  50. package/out/concepts/ViewElement__.js.map +1 -1
  51. package/out/concepts/View__.js +24 -2
  52. package/out/concepts/View__.js.map +1 -1
  53. package/out/generator/genBundleFiles.js +1 -1
  54. package/out/generator/genBundleFiles.js.map +1 -1
  55. package/out/natural/genNaturalTS.d.ts +69 -16
  56. package/out/natural/genNaturalTS.js +198 -117
  57. package/out/natural/genNaturalTS.js.map +1 -1
  58. package/out/natural/getContext/index.d.ts +38 -26
  59. package/out/natural/getContext/index.js +318 -160
  60. package/out/natural/getContext/index.js.map +1 -1
  61. package/out/natural/index.d.ts +1 -0
  62. package/out/natural/index.js +1 -0
  63. package/out/natural/index.js.map +1 -1
  64. package/out/natural/tools.d.ts +11 -0
  65. package/out/natural/tools.js +162 -0
  66. package/out/natural/tools.js.map +1 -0
  67. package/out/natural/transformTS2UI.js +63 -26
  68. package/out/natural/transformTS2UI.js.map +1 -1
  69. package/out/natural/transformTSCode.js +26 -15
  70. package/out/natural/transformTSCode.js.map +1 -1
  71. package/out/server/naslServer.js +1 -0
  72. package/out/server/naslServer.js.map +1 -1
  73. package/out/translator/utils.js +1 -1
  74. package/out/translator/utils.js.map +1 -1
  75. package/package.json +1 -1
  76. package/sandbox/stdlib/nasl.util.ts +2 -2
  77. package/sandbox-natural/stdlib/nasl.core.ts +11 -0
  78. package/sandbox-natural/stdlib/nasl.ui.pre.d.ts +37 -37
  79. package/sandbox-natural/stdlib/nasl.util.ts +6 -6
  80. package/src/concepts/AuthLogicForCallInterface__.ts +13 -1
  81. package/src/concepts/BackendVariable__.ts +11 -1
  82. package/src/concepts/BindDirective__.ts +1 -1
  83. package/src/concepts/BindStyle__.ts +1 -1
  84. package/src/concepts/BusinessComponent__.ts +14 -2
  85. package/src/concepts/BusinessLogic__.ts +13 -1
  86. package/src/concepts/CallConnector__.ts +2 -1
  87. package/src/concepts/CallInterface__.ts +10 -3
  88. package/src/concepts/CallLogic__.ts +34 -14
  89. package/src/concepts/EntityProperty__.ts +5 -5
  90. package/src/concepts/Entity__.ts +1 -1
  91. package/src/concepts/EnumItem__.ts +2 -2
  92. package/src/concepts/Interface__.ts +1 -1
  93. package/src/concepts/Logic__.ts +13 -1
  94. package/src/concepts/NewComposite__.ts +24 -0
  95. package/src/concepts/OverriddenLogic__.ts +13 -1
  96. package/src/concepts/Param__.ts +1 -1
  97. package/src/concepts/Structure__.ts +1 -3
  98. package/src/concepts/SubLogic__.ts +13 -1
  99. package/src/concepts/TypeAnnotation__.ts +5 -3
  100. package/src/concepts/ViewComponentDeclaration__.ts +3 -3
  101. package/src/concepts/ViewElement__.ts +78 -19
  102. package/src/concepts/View__.ts +48 -24
  103. package/src/generator/genBundleFiles.ts +1 -1
  104. package/src/natural/genNaturalTS.ts +363 -147
  105. package/src/natural/getContext/index.ts +363 -175
  106. package/src/natural/index.ts +1 -0
  107. package/src/natural/tools.ts +138 -0
  108. package/src/natural/transformTS2UI.ts +60 -25
  109. package/src/natural/transformTSCode.ts +26 -16
  110. package/src/server/naslServer.ts +1 -0
  111. package/src/translator/utils.ts +1 -1
  112. package/.nyc_output/047fb941-6e69-4829-8b3c-14d37e06c63b.json +0 -1
  113. package/.nyc_output/processinfo/047fb941-6e69-4829-8b3c-14d37e06c63b.json +0 -1
@@ -9,235 +9,359 @@ import {
9
9
  getStructures,
10
10
  getLogics,
11
11
  getModuleInterfaces,
12
+ getConnections,
12
13
  getConnectors,
13
14
  getDependencies,
14
15
  getFrontendVariables,
16
+ getServerVariables,
15
17
  getFrontendViews,
16
18
  getCurrentNodeContext,
17
19
  getCurrentNodeContextForUI,
18
20
  getNaslUIOld,
21
+ getNaslUICode,
22
+ getEnumsCode,
23
+ getEntitiesCode,
24
+ getStructuresCode,
25
+ getLogicsCode,
26
+ getModuleInterfacesCode,
27
+ getConnectorsCode,
28
+ getDependenciesCode,
29
+ getFrontendViewsCode,
30
+ wrapTSBlock,
19
31
  } from './getContext';
20
32
  import { createCompilerState } from '../translator';
33
+ import { fr } from 'date-fns/locale';
34
+
35
+ export * as getNaturalContext from './getContext';
21
36
 
22
37
  export function genNaturalTS(app: App, currentNode?: BaseNode, focusedNodePath?: string) {
23
38
  if (currentNode?.concept === 'Logic') {
24
39
  const logic = currentNode as Logic;
25
- const code = logic?.toNaturalTS(createCompilerState(undefined, {
26
- focusedNodePath,
27
- needNamespace: true,
28
- autoPrefixName: true,
29
- }));
40
+ const code = logic?.toNaturalTS(
41
+ createCompilerState(undefined, {
42
+ focusedNodePath,
43
+ needNamespace: true,
44
+ autoPrefixName: true,
45
+ })
46
+ );
30
47
  return code;
31
48
  }
32
49
  return getCurrentNodeContextForUI(currentNode).code;
33
50
  }
34
51
 
35
- export function genNaturalTSContextForLogic(
52
+ // =========================== logic ===========================
53
+ export function genNaturalTSContextJSONForLogic(
36
54
  app: App,
37
55
  currentNode?: BaseNode,
38
56
  focusedNodePath?: string,
39
- requiredIndexes?: string[],
40
- experimental?: boolean
57
+ experimental?: boolean,
58
+ material?: {
59
+ moduleInterfaces: any;
60
+ connectors: any;
61
+ dependencies: any;
62
+ basicMaterials: 'pc' | 'h5' | { json: any; tsDeclaration: string; };
63
+ extensionMaterials: {
64
+ name: string;
65
+ title: string;
66
+ description: string;
67
+ tsDescription: string;
68
+ }[];
69
+ }
41
70
  ) {
42
71
  const frontend = currentNode?.getAncestor('Frontend') as Frontend;
43
72
  const logicType = currentNode?.parentNode?.concept === 'App' ? 'global_logic' : 'view_logic';
44
73
  const state = createCompilerState('', { descriptionComment: true });
45
74
 
46
- let code = '';
47
- code += '### [1] 标准库\n';
48
- code += getNaslCore(logicType).code;
49
-
50
- code += '\n### [2] nasl.util\n';
51
- code += getNaslUtil().code;
52
-
53
- if (frontend) {
54
- code += '\n### [3] nasl.ui\n';
55
- code += getNaslUIOld().code;
56
- }
57
-
58
- if (logicType === 'global_logic') {
59
- code += '\n### [4] nasl.oql\n';
60
- code += getNaslOQL().code;
75
+ const { naslCore } = getNaslCore(logicType);
76
+ const { naslUtil } = getNaslUtil();
77
+ const { naslUI } = getNaslUIOld();
78
+ let components = [];
79
+ if (logicType === 'view_logic' && material?.basicMaterials && material?.extensionMaterials) {
80
+ components = getNaslUI(app, frontend.type, {
81
+ basicMaterials: material?.basicMaterials,
82
+ extensionMaterials: material?.extensionMaterials,
83
+ }).naslUI;
61
84
  }
85
+ const { naslOql } = getNaslOQL();
86
+ const { enums } = getEnums(app, state);
87
+ const { entities } = getEntities(app, state);
88
+ const { structures } = getStructures(app, state);
89
+ const { appLogics } = getLogics(app, currentNode, state);
90
+ const { interfaceDependencies } = getModuleInterfaces(
91
+ material?.moduleInterfaces || app?.interfaceDependencies,
92
+ state
93
+ );
62
94
 
63
- code += '\n### [5] 全局上下文\n';
64
- code += '\n#### [5-0] 枚举\n';
65
- code += getEnums(app, state, requiredIndexes).code;
66
-
67
- code += '\n#### [5-1] 实体\n';
68
- code += getEntities(app, state, requiredIndexes).code;
69
-
70
- code += '\n#### [5-2] 可用的数据结构\n';
71
- code += getStructures(app, state, requiredIndexes).code;
72
-
73
- code += '\n### [6] 服务端逻辑\n';
74
- code += getLogics(app, currentNode, requiredIndexes).code;
75
-
76
- code += '\n### [7] 可调用的接口列表\n';
77
- code += getModuleInterfaces(app, state, requiredIndexes).code;
78
-
79
- if (app?.connections?.length > 0 && logicType === 'global_logic') {
80
- code += '\n### [8] 连接器逻辑列表\n';
81
- code += getConnectors(app, state, requiredIndexes).code;
95
+ let connections = [];
96
+ if (material?.connectors?.length) {
97
+ connections = getConnectors(material.connectors, state).connectors;
98
+ } else {
99
+ connections = getConnections(material?.connectors || app.connections, state).connections;
82
100
  }
83
101
 
84
- if (app?.dependencies?.length > 0) {
85
- code += '\n### [9] 依赖库逻辑列表\n';
86
- code += getDependencies(app, state, requiredIndexes).code;
87
- }
102
+ const { dependencies } = getDependencies(
103
+ material?.dependencies || app?.dependencies,
104
+ state,
105
+ logicType,
106
+ frontend?.type
107
+ );
108
+ const { frontendVariables } = getFrontendVariables(
109
+ currentNode?.getAncestor('Frontend') as Frontend,
110
+ state
111
+ );
88
112
 
89
- if (frontend) {
90
- code += '\n### [10] 页面上下文\n';
91
- if (frontend?.variables?.length > 0) {
92
- code += '\n#### [10-0] 前端全局变量\n';
93
- code += getFrontendVariables(frontend, state).code;
94
- }
113
+ const { backendVariables } = getServerVariables(app, state);
95
114
 
96
- code += '\n#### [10-1] 前端页面列表\n';
97
- code += getFrontendViews(frontend, state, currentNode, true).code;
98
- }
115
+ const { views } = getFrontendViews(frontend, state, currentNode, false);
116
+ const { currentLogic } = getCurrentNodeContext(currentNode, focusedNodePath, experimental);
99
117
 
100
- if (focusedNodePath) {
101
- code += '\n### 当前逻辑上下文\n';
102
- code += getCurrentNodeContext(currentNode, focusedNodePath, experimental).code;
103
- }
104
- return code;
118
+ return {
119
+ naslCore,
120
+ naslUtil,
121
+ naslUI,
122
+ components,
123
+ naslOql,
124
+ enums,
125
+ entities,
126
+ structures,
127
+ appLogics,
128
+ interfaceDependencies,
129
+ connections,
130
+ dependencies,
131
+ frontendVariables,
132
+ backendVariables,
133
+ views,
134
+ currentLogic,
135
+ logicType,
136
+ };
105
137
  }
106
138
 
107
- export const genNaturalTSContext = genNaturalTSContextForLogic;
108
-
109
- export function genNaturalTSContextForUI(
110
- app: App,
111
- currentNode: BaseNode,
112
- focusedNodePath?: string,
113
- requiredIndexes?: string[],
114
- material?: { basicMaterials: { json: any; tsDeclaration: string; }, extensionMaterials: { name: string; title: string; description: string; tsDescription: string; }[]; },
115
- ) {
116
- const frontend = currentNode.getAncestor('Frontend') as Frontend;
117
- const state = createCompilerState('', { descriptionComment: true });
139
+ /**
140
+ * 生成 Logic 的 TypeScript 上下文
141
+ * @param tsContextJSON: 通过 genNaturalTSContextForLogic 获得到 tsContextJSON
142
+ * @param codeType: 'detail' | 'short'。detail 为详细代码,short 为简短代码
143
+ * 'detail' 将所有的code都返回(拼接成详细的code)。RAG召回,分析和执行阶段使用
144
+ * 'short' 只返回简短的code。模型召回,对应的分析阶段使用
145
+ * @param requiredIndexes?: 通过索引获取对应的 code。可挑选需要的 code
146
+ **/
147
+ export const genNaturalTSContextFromJSONForLogic = (
148
+ tsContextJSON: any,
149
+ codeType: 'detail' | 'short',
150
+ requiredIndexes?: string[]
151
+ ) => {
152
+ const {
153
+ naslCore,
154
+ naslUtil,
155
+ naslUI,
156
+ naslOql,
157
+ enums,
158
+ entities,
159
+ structures,
160
+ appLogics,
161
+ interfaceDependencies,
162
+ connections,
163
+ dependencies,
164
+ frontendVariables,
165
+ backendVariables,
166
+ views,
167
+ currentLogic,
168
+ logicType,
169
+ } = tsContextJSON;
118
170
 
119
171
  let code = '';
120
172
  code += '### [1] 标准库\n';
121
- code += getNaslCore('view_logic').code;
173
+ code += wrapTSBlock(naslCore);
122
174
 
123
175
  code += '\n### [2] nasl.util\n';
124
- code += getNaslUtil().code;
176
+ code += wrapTSBlock(naslUtil);
125
177
 
126
- if (frontend) {
178
+ if (naslUI && logicType === 'view_logic') {
127
179
  code += '\n### [3] nasl.ui\n';
128
- code += getNaslUI(app, frontend.type, material || frontend.type, requiredIndexes).code;
129
- code += '\n';
180
+ if (typeof naslUI === 'string') {
181
+ code += wrapTSBlock(naslUI);
182
+ } else {
183
+ code += getNaslUICode(naslUI, codeType, requiredIndexes);
184
+ }
185
+ }
186
+
187
+ if (naslOql && logicType === 'global_logic') {
188
+ code += '\n### [4] nasl.oql\n';
189
+ code += wrapTSBlock(naslOql);
130
190
  }
131
191
 
132
192
  code += '\n### [5] 全局上下文\n';
133
- code += '\n#### [5-0] 枚举\n';
134
- code += getEnums(app, state, requiredIndexes).code;
193
+ if (enums.data?.length) {
194
+ code += '\n#### [5-0] 枚举\n';
195
+ code += getEnumsCode(enums);
196
+ }
135
197
 
136
- code += '\n#### [5-1] 实体\n';
137
- code += getEntities(app, state, requiredIndexes).code;
198
+ if (entities?.length) {
199
+ code += '\n#### [5-1] 实体\n';
200
+ code += getEntitiesCode(entities);
201
+ }
138
202
 
139
- code += '\n#### [5-2] 可用的数据结构\n';
140
- code += getStructures(app, state, requiredIndexes).code;
203
+ if (structures.data?.length) {
204
+ code += '\n#### [5-2] 可用的数据结构\n';
205
+ code += getStructuresCode(structures);
206
+ }
141
207
 
142
- code += '\n### [6] 服务端逻辑\n';
143
- code += getLogics(app, currentNode, requiredIndexes).code;
208
+ if (appLogics.data?.length) {
209
+ code += '\n### [6] 服务端逻辑\n';
210
+ code += getLogicsCode(appLogics, codeType, requiredIndexes);
211
+ }
144
212
 
145
- code += '\n### [7] 可调用的接口列表\n';
146
- code += getModuleInterfaces(app, state, requiredIndexes).code;
213
+ if (interfaceDependencies) {
214
+ code += '\n### [7] 可调用的接口列表\n';
215
+ code += getModuleInterfacesCode(interfaceDependencies, codeType, requiredIndexes);
216
+ }
147
217
 
148
- if (app?.dependencies?.length > 0) {
218
+ if (logicType === 'global_logic' && connections) {
219
+ code += '\n### [8] 连接器逻辑列表\n';
220
+ code += getConnectorsCode(connections, codeType, requiredIndexes);
221
+ }
222
+ if (dependencies) {
149
223
  code += '\n### [9] 依赖库逻辑列表\n';
150
- code += getDependencies(app, state, requiredIndexes).code;
224
+ code += getDependenciesCode(dependencies, codeType, requiredIndexes);
151
225
  }
152
226
 
153
- if (frontend) {
227
+ if (logicType === 'view_logic') {
154
228
  code += '\n### [10] 页面上下文\n';
155
- if (frontend?.variables?.length > 0) {
229
+ if (frontendVariables) {
156
230
  code += '\n#### [10-0] 前端全局变量\n';
157
- code += getFrontendVariables(frontend, state).code;
231
+ code += frontendVariables;
158
232
  }
159
233
 
160
- code += '\n#### [10-1] 前端页面列表\n';
161
- code += getFrontendViews(frontend, state, currentNode, true).code;
234
+ if (views?.data?.length) {
235
+ code += '\n#### [10-1] 前端页面列表\n';
236
+ code += getFrontendViewsCode(views);
237
+ }
238
+ } else {
239
+ code += '\n### [10] 全局变量\n';
240
+ code += backendVariables;
162
241
  }
163
242
 
164
- code += '\n### 当前页面上下文\n';
165
- code += getCurrentNodeContextForUI(currentNode).code;
243
+ code += '\n### 当前逻辑上下文\n';
244
+ code += wrapTSBlock(currentLogic);
245
+
166
246
  return code;
167
- }
247
+ };
168
248
 
169
- export function genNaturalTSContextJSONForLogic(
249
+ export function genNaturalTSContextForLogic(
170
250
  app: App,
171
251
  currentNode?: BaseNode,
172
252
  focusedNodePath?: string,
173
- experimental?: boolean
253
+ requiredIndexes?: string[],
254
+ experimental?: boolean,
255
+ material?: {
256
+ moduleInterfaces: any;
257
+ connectors: any;
258
+ dependencies: any;
259
+ basicMaterials: 'pc' | 'h5' | { json: any; tsDeclaration: string; };
260
+ extensionMaterials: {
261
+ name: string;
262
+ title: string;
263
+ description: string;
264
+ tsDescription: string;
265
+ }[];
266
+ },
267
+ restParams?: {
268
+ codeType?: 'detail' | 'short';
269
+ }
174
270
  ) {
175
- const frontend = currentNode?.getAncestor('Frontend') as Frontend;
176
- const logicType = currentNode?.parentNode?.concept === 'App' ? 'global_logic' : 'view_logic';
177
- const state = createCompilerState('', { descriptionComment: true });
271
+ const tsContextJSON = genNaturalTSContextJSONForLogic(
272
+ app,
273
+ currentNode,
274
+ focusedNodePath,
275
+ experimental,
276
+ material
277
+ );
278
+ const code = genNaturalTSContextFromJSONForLogic(
279
+ tsContextJSON,
280
+ restParams.codeType,
281
+ requiredIndexes
282
+ );
283
+ return code;
284
+ }
178
285
 
179
- const { naslCore } = getNaslCore(logicType);
286
+ export const genNaturalTSContext = genNaturalTSContextForLogic;
287
+ export const genNaturalTsContextJson = genNaturalTSContextJSONForLogic;
288
+
289
+ // =========================== view ===========================
290
+
291
+ // 生成 View 的 tsJson
292
+ export function genNaturalTSContextJSONForUI(
293
+ app: App,
294
+ currentNode: BaseNode,
295
+ material?: {
296
+ basicMaterials: 'pc' | 'h5' | { json: any; tsDeclaration: string; };
297
+ extensionMaterials: {
298
+ name: string;
299
+ title: string;
300
+ description: string;
301
+ tsDescription: string;
302
+ }[];
303
+ moduleInterfaces: any;
304
+ connectors: any;
305
+ dependencies: any;
306
+ }
307
+ ) {
308
+ const frontend = currentNode.getAncestor('Frontend') as Frontend;
309
+ const state = createCompilerState('', { descriptionComment: true, needNamespace: true });
310
+
311
+ const { naslCore } = getNaslCore('view_logic');
180
312
  const { naslUtil } = getNaslUtil();
181
- const { naslUI } = getNaslUIOld();
182
- const { naslOql } = getNaslOQL();
183
- const { enums } = getEnums(app, state, []);
184
- const { entities } = getEntities(app, state, []);
185
- const { structures } = getStructures(app, state, []);
186
- const { appLogics } = getLogics(app, currentNode, []);
187
- const { interfaceDependencies } = getModuleInterfaces(app, state, []);
188
- const { connections } = getConnectors(app, state, []);
189
- const { dependencies } = getDependencies(app, state, []);
190
- const { frontendVar, frontendVariables } = getFrontendVariables(
191
- currentNode?.getAncestor('Frontend') as Frontend,
313
+ const { naslUI } = getNaslUI(app, frontend.type, {
314
+ basicMaterials: material?.basicMaterials,
315
+ extensionMaterials: material?.extensionMaterials,
316
+ });
317
+ const { enums } = getEnums(app, state);
318
+ const { entities } = getEntities(app, state);
319
+ const { structures } = getStructures(app, state);
320
+ const { appLogics } = getLogics(app, currentNode, state);
321
+ const { interfaceDependencies } = getModuleInterfaces(
322
+ material?.moduleInterfaces || app?.interfaceDependencies,
192
323
  state
193
324
  );
325
+ const { dependencies } = getDependencies(
326
+ material?.dependencies || app?.dependencies,
327
+ state,
328
+ 'view_logic',
329
+ frontend?.type
330
+ );
331
+ const { frontendVariables } = getFrontendVariables(frontend, state);
194
332
  const { views } = getFrontendViews(frontend, state, currentNode, false);
195
- const { currentLogic } = getCurrentNodeContext(currentNode, focusedNodePath, experimental);
333
+ const { currentLogic } = getCurrentNodeContextForUI(currentNode);
196
334
 
197
335
  return {
198
336
  naslCore,
199
337
  naslUtil,
200
338
  naslUI,
201
- naslOql,
202
339
  enums,
203
340
  entities,
204
341
  structures,
205
342
  appLogics,
206
343
  interfaceDependencies,
207
- connections,
208
344
  dependencies,
209
- frontendVar, // @deprecated
210
345
  frontendVariables,
211
346
  views,
212
- currentLogic,
347
+ currentPage: currentLogic,
213
348
  };
214
349
  }
215
350
 
216
- export const genNaturalTsContextJson = genNaturalTSContextJSONForLogic;
217
-
218
- export function genNaturalTSContextJSONForUI(
219
- app: App,
220
- currentNode: BaseNode,
221
- material?: { basicMaterials: { json: any; tsDeclaration: string; }, extensionMaterials: { name: string; title: string; description: string; tsDescription: string; }[]; },
222
- requiredIndexes?: string[],
223
- ) {
224
- const frontend = currentNode.getAncestor('Frontend') as Frontend;
225
- const state = createCompilerState();
226
-
227
- const { naslCore } = getNaslCore('view_logic');
228
- const { naslUtil } = getNaslUtil();
229
- const { naslUI } = getNaslUI(app, frontend.type, material || frontend.type, requiredIndexes);
230
- const { enums } = getEnums(app, state, requiredIndexes);
231
- const { entities } = getEntities(app, state, requiredIndexes);
232
- const { structures } = getStructures(app, state, requiredIndexes);
233
- const { appLogics } = getLogics(app, currentNode, requiredIndexes);
234
- const { interfaceDependencies } = getModuleInterfaces(app, state, requiredIndexes);
235
- const { dependencies } = getDependencies(app, state, requiredIndexes);
236
- const { frontendVariables } = getFrontendVariables(frontend, state);
237
- const { views } = getFrontendViews(frontend, state, currentNode, true);
238
- const { currentLogic } = getCurrentNodeContextForUI(currentNode);
239
-
240
- return {
351
+ /**
352
+ * 根据 tsContextJSON 生成 View 的 TypeScript 上下文
353
+ * @param tsContextJSON: 通过 genNaturalTSContextJSONForUI 获得到 tsContextJSON
354
+ * @param codeType: 'detail' | 'short'。detail 为详细代码,short 为简短代码
355
+ * 'detail' 将所有的code都返回(拼接成详细的code)。RAG召回,分析和执行阶段使用
356
+ * 'short' 只返回简短的code。模型召回,对应的分析阶段使用
357
+ * @param requiredIndexes?: 通过索引获取对应的 code。可挑选需要的 code
358
+ **/
359
+ export const genNaturalTSContextFromJSONForUI = (
360
+ tsContextJSON: any,
361
+ codeType: 'detail' | 'short',
362
+ requiredIndexes?: string[]
363
+ ) => {
364
+ const {
241
365
  naslCore,
242
366
  naslUtil,
243
367
  naslUI,
@@ -249,8 +373,100 @@ export function genNaturalTSContextJSONForUI(
249
373
  dependencies,
250
374
  frontendVariables,
251
375
  views,
252
- currentPage: currentLogic,
253
- };
254
- }
376
+ currentPage,
377
+ } = tsContextJSON;
255
378
 
256
- export * as getNaturalContext from './getContext';
379
+ let code = '';
380
+ code += '### [1] 标准库\n';
381
+ code += wrapTSBlock(naslCore);
382
+
383
+ code += '\n### [2] nasl.util\n';
384
+ code += wrapTSBlock(naslUtil);
385
+
386
+ if (naslUI) {
387
+ code += '\n### [3] nasl.ui\n';
388
+ if (typeof naslUI === 'string') {
389
+ code += wrapTSBlock(naslUI);
390
+ } else {
391
+ code += getNaslUICode(naslUI, codeType, requiredIndexes);
392
+ }
393
+ }
394
+
395
+ code += '\n### [5] 全局上下文\n';
396
+ if (enums.data?.length) {
397
+ code += '\n#### [5-0] 枚举\n';
398
+ code += getEnumsCode(enums);
399
+ }
400
+
401
+ if (entities?.length) {
402
+ code += '\n#### [5-1] 实体\n';
403
+ code += getEntitiesCode(entities);
404
+ }
405
+
406
+ if (structures.data?.length) {
407
+ code += '\n#### [5-2] 可用的数据结构\n';
408
+ code += getStructuresCode(structures);
409
+ }
410
+
411
+ if (appLogics.data?.length) {
412
+ code += '\n### [6] 服务端逻辑\n';
413
+ code += getLogicsCode(appLogics, codeType, requiredIndexes);
414
+ }
415
+
416
+ if (interfaceDependencies) {
417
+ code += '\n### [7] 可调用的接口列表\n';
418
+ code += getModuleInterfacesCode(interfaceDependencies, codeType, requiredIndexes);
419
+ }
420
+
421
+ if (dependencies) {
422
+ code += '\n### [9] 依赖库逻辑列表\n';
423
+ code += getDependenciesCode(dependencies, codeType, requiredIndexes);
424
+ }
425
+
426
+ code += '\n### [10] 页面上下文\n';
427
+ if (frontendVariables) {
428
+ code += '\n#### [10-0] 前端全局变量\n';
429
+ code += frontendVariables;
430
+ }
431
+
432
+ if (views?.data?.length) {
433
+ code += '\n#### [10-1] 前端页面列表\n';
434
+ code += getFrontendViewsCode(views);
435
+ }
436
+
437
+ code += '\n### 当前页面上下文\n';
438
+ code += wrapTSBlock(currentPage);
439
+
440
+ return code;
441
+ };
442
+
443
+ // 兼容各版本,统一调用 genNaturalTSContextForUI 方法
444
+ export function genNaturalTSContextForUI(
445
+ app: App,
446
+ currentNode: BaseNode,
447
+ focusedNodePath?: string,
448
+ requiredIndexes?: string[],
449
+ material?: {
450
+ basicMaterials: 'pc' | 'h5' | { json: any; tsDeclaration: string; };
451
+ extensionMaterials: {
452
+ name: string;
453
+ title: string;
454
+ description: string;
455
+ tsDescription: string;
456
+ }[];
457
+ moduleInterfaces: any;
458
+ connectors: any;
459
+ dependencies: any;
460
+ },
461
+ restParams?: {
462
+ codeType?: 'detail' | 'short';
463
+ }
464
+ ) {
465
+ const tsContextJSON = genNaturalTSContextJSONForUI(app, currentNode, material);
466
+ const code = genNaturalTSContextFromJSONForUI(
467
+ tsContextJSON,
468
+ restParams.codeType,
469
+ requiredIndexes
470
+ );
471
+ return code;
472
+ }