@lcap/nasl
Version:
NetEase Application Specific Language
336 lines • 16.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genNaturalTSContextForUI = exports.genNaturalTSContextFromJSONForUI = exports.genNaturalTSContextJSONForUI = exports.genNaturalTsContextJson = exports.genNaturalTSContext = exports.genNaturalTSContextForLogic = exports.genNaturalTSContextFromJSONForLogic = exports.genNaturalTSContextJSONForLogic = exports.genNaturalTSCurrentContext = exports.genNaturalTS = exports.getNaturalContext = void 0;
const getContext_1 = require("./getContext");
const translator_1 = require("../translator");
const tools_1 = require("./tools");
exports.getNaturalContext = __importStar(require("./getContext"));
function genNaturalTS(app, currentNode, focusedNodePath) {
if (currentNode?.concept === 'Logic') {
const logic = currentNode;
const code = logic?.toNaturalTS((0, translator_1.createCompilerState)(undefined, {
focusedNodePath,
needNamespace: true,
autoPrefixName: true,
}));
return code;
}
return (0, getContext_1.getCurrentNodeContextForUI)(currentNode).code;
}
exports.genNaturalTS = genNaturalTS;
// 只生成当前节点相关的上下文
function genNaturalTSCurrentContext(currentNode, focusedNodePath, experimental, restParams) {
let extraParams = {};
const { currentNodeContext, currentView } = (0, getContext_1.getCurrentNodeContext)(currentNode, focusedNodePath, experimental, restParams);
const frontend = currentNode?.getAncestor('Frontend');
if (frontend) {
const view = currentNode.getAncestor('View');
if (view) {
extraParams.currentViewTitle = view?.title || view?.name || '';
const viewElement = currentNode.getAncestor('ViewElement');
if (viewElement) {
extraParams.viewElementText =
viewElement?.bindAttrs.find((attr) => attr?.name === 'text' && attr?.type === 'string')?.value ?? '';
}
}
}
if (currentNode.parentNode?.concept === 'View') {
extraParams.currentNodeName = `viewLogic_${currentNode.name}`;
}
else if (currentNode.parentNode?.concept === 'BindEvent') {
extraParams.currentNodeName = `on_${currentNode.parentNode?.parentNode?.name}_${currentNode.name}`;
}
return {
currentNodeContext,
currentView,
...extraParams,
};
}
exports.genNaturalTSCurrentContext = genNaturalTSCurrentContext;
// =========================== logic ===========================
function genNaturalTSContextJSONForLogic(app, currentNode, focusedNodePath, experimental, material, restParams) {
let extraParams = {};
const frontend = currentNode?.getAncestor('Frontend');
const logicType = (0, tools_1.getLogicType)(currentNode);
const state = (0, translator_1.createCompilerState)('', { descriptionComment: true, typeMap: restParams?.typeMap });
const { naslCore } = (0, getContext_1.getNaslCore)(logicType);
const { naslUtil } = (0, getContext_1.getNaslUtil)();
const { naslUI } = (0, getContext_1.getNaslUIOld)();
let components = [];
let allNodesAPI = {};
if (logicType !== 'global_logic' &&
!restParams?.noComponents &&
material?.basicMaterials &&
material?.extensionMaterials) {
const { naslUI: componentsTemp, allNodesAPI: allNodeAPITemp } = (0, getContext_1.getNaslUI)(app, frontend.type, {
basicMaterials: material?.basicMaterials,
extensionMaterials: material?.extensionMaterials,
});
components = componentsTemp;
allNodesAPI = allNodeAPITemp;
}
const { enums } = (0, getContext_1.getEnums)(app, state, restParams);
const { entities } = (0, getContext_1.getEntities)(app, state);
const { structures } = (0, getContext_1.getStructures)(app, state);
const { appLogics } = (0, getContext_1.getLogics)(app, currentNode, state, restParams);
const { interfaceDependencies } = (0, getContext_1.getModuleInterfaces)(material?.moduleInterfaces || app?.interfaceDependencies, state, restParams);
let connections = [];
if (restParams?.sourceType !== 'completion') {
connections = (0, getContext_1.getConnectors)(app, material?.connectors || app.connectorDependencies, state, restParams).connectors;
}
const { dependencies } = (0, getContext_1.getDependencies)(material?.dependencies || app?.dependencies, state, logicType, frontend?.type, restParams);
const { frontendVariables } = (0, getContext_1.getFrontendVariables)(currentNode?.getAncestor('Frontend'), state);
const { backendVariables } = (0, getContext_1.getServerVariables)(app, state);
const { views } = (0, getContext_1.getFrontendViews)(frontend, state, currentNode, false);
const { componentDeclaration } = (0, getContext_1.getCurrentComponentDeclaration)(currentNode, allNodesAPI);
const curRelatedContext = genNaturalTSCurrentContext(currentNode, focusedNodePath, experimental, restParams);
return {
naslCore,
naslUtil,
naslUI,
components,
enums,
entities,
structures,
appLogics,
interfaceDependencies,
connections,
dependencies,
frontendVariables,
backendVariables,
views,
logicType,
componentDeclaration,
...extraParams,
...curRelatedContext,
};
}
exports.genNaturalTSContextJSONForLogic = genNaturalTSContextJSONForLogic;
/**
* 生成 Logic 的 TypeScript 上下文
* @param tsContextJSON: 通过 genNaturalTSContextForLogic 获得到 tsContextJSON
* @param codeType: 'detail' | 'short'。detail 为详细代码,short 为简短代码
* 'detail' 将所有的code都返回(拼接成详细的code)。RAG召回,分析和执行阶段使用
* 'short' 只返回简短的code。模型召回,对应的分析阶段使用
* @param requiredIndexes?: 通过索引获取对应的 code。可挑选需要的 code
**/
const genNaturalTSContextFromJSONForLogic = (tsContextJSON, codeType, requiredIndexes) => {
const { naslCore, naslUtil, naslUI, enums, entities, structures, appLogics, interfaceDependencies, connections, dependencies, frontendVariables, backendVariables, views, currentNodeContext, logicType, componentDeclaration, } = tsContextJSON;
let code = '';
code += '### [1]标准库与类型定义\n';
code += (0, getContext_1.wrapTSBlock)(naslCore);
code += '\n### [2]平台内置函数库\n';
code += (0, getContext_1.wrapTSBlock)(naslUtil);
if (naslUI && logicType !== 'global_logic') {
code += '\n### [3] nasl.ui\n';
if (typeof naslUI === 'string') {
code += (0, getContext_1.wrapTSBlock)(naslUI);
}
else {
code += (0, getContext_1.getNaslUICode)(naslUI, codeType, requiredIndexes);
}
if (componentDeclaration) {
code += '\n#### [3-1] 组件声明\n';
code += (0, getContext_1.wrapTSBlock)(`declare namespace nasl.ui {\n${componentDeclaration}}`);
}
}
code += '\n### [5] 全局上下文\n';
if (enums.data?.length) {
code += '\n#### [5-0] 枚举\n';
code += (0, getContext_1.getEnumsCode)(enums);
}
if (entities?.length) {
code += '\n#### [5-1] 实体\n';
code += (0, getContext_1.getEntitiesCode)(entities);
}
if (structures.data?.length) {
code += '\n#### [5-2] 可用的数据结构\n';
code += (0, getContext_1.getStructuresCode)(structures);
}
if (appLogics.data?.length) {
code += '\n### [6] 服务端逻辑\n';
code += (0, getContext_1.getLogicsCode)(appLogics, codeType, requiredIndexes);
}
if (interfaceDependencies) {
code += '\n### [7] 可调用的接口列表\n';
code += (0, getContext_1.getModuleInterfacesCode)(interfaceDependencies, codeType, requiredIndexes);
}
if (logicType === 'global_logic' && connections) {
code += '\n### [8] 连接器逻辑列表\n';
code += (0, getContext_1.getConnectorsCode)(connections, codeType, requiredIndexes);
}
if (dependencies) {
code += '\n### [9] 依赖库逻辑列表\n';
code += (0, getContext_1.getDependenciesCode)(dependencies, codeType, requiredIndexes);
}
if (logicType !== 'global_logic') {
code += '\n### [10] 页面上下文\n';
if (frontendVariables?.data?.length) {
code += '\n#### [10-0] 前端全局变量\n';
code += (0, getContext_1.getVariablesCode)(frontendVariables);
}
if (views?.data?.length) {
code += '\n#### [10-1] 前端页面列表\n';
code += (0, getContext_1.getFrontendViewsCode)(views);
}
}
else {
code += '\n### [10] 全局变量\n';
if (backendVariables?.data?.length) {
code += (0, getContext_1.getVariablesCode)(backendVariables);
}
}
code += '\n### 当前逻辑上下文\n';
code += (0, getContext_1.wrapTSBlock)(currentNodeContext);
return code;
};
exports.genNaturalTSContextFromJSONForLogic = genNaturalTSContextFromJSONForLogic;
function genNaturalTSContextForLogic(app, currentNode, focusedNodePath, requiredIndexes, experimental, material, restParams) {
const tsContextJSON = genNaturalTSContextJSONForLogic(app, currentNode, focusedNodePath, experimental, material, restParams);
const code = (0, exports.genNaturalTSContextFromJSONForLogic)(tsContextJSON, restParams?.codeType || 'short', requiredIndexes);
return code;
}
exports.genNaturalTSContextForLogic = genNaturalTSContextForLogic;
exports.genNaturalTSContext = genNaturalTSContextForLogic;
exports.genNaturalTsContextJson = genNaturalTSContextJSONForLogic;
// =========================== view ===========================
// 生成 View 的 tsJson
function genNaturalTSContextJSONForUI(app, currentNode, material, restParams) {
const frontend = currentNode.getAncestor('Frontend');
const state = (0, translator_1.createCompilerState)('', {
descriptionComment: true,
needNamespace: true,
typeMap: restParams?.typeMap,
});
const { naslCore } = (0, getContext_1.getNaslCore)('view_logic');
const { naslUtil } = (0, getContext_1.getNaslUtil)();
const { naslUI } = (0, getContext_1.getNaslUIOld)();
const { naslUI: components, allNodesAPI } = (0, getContext_1.getNaslUI)(app, frontend.type, {
basicMaterials: material?.basicMaterials,
extensionMaterials: material?.extensionMaterials,
}, restParams?.notUseBasicMaterial);
const { enums } = (0, getContext_1.getEnums)(app, state);
const { entities } = (0, getContext_1.getEntities)(app, state);
const { structures } = (0, getContext_1.getStructures)(app, state);
const { appLogics } = (0, getContext_1.getLogics)(app, currentNode, state);
const { interfaceDependencies } = (0, getContext_1.getModuleInterfaces)(material?.moduleInterfaces || app?.interfaceDependencies, state);
const { dependencies } = (0, getContext_1.getDependencies)(material?.dependencies || app?.dependencies, state, 'view_logic', frontend?.type);
const { frontendVariables } = (0, getContext_1.getFrontendVariables)(frontend, state);
const { views } = (0, getContext_1.getFrontendViews)(frontend, state, currentNode, false);
const { currentNodeContext } = (0, getContext_1.getCurrentNodeContextForUI)(currentNode, restParams?.typeMap);
const { componentDeclaration } = (0, getContext_1.getCurrentComponentDeclaration)(currentNode, allNodesAPI);
return {
naslCore,
naslUtil,
naslUI,
components,
componentDeclaration,
enums,
entities,
structures,
appLogics,
interfaceDependencies,
dependencies,
frontendVariables,
views,
currentPage: currentNodeContext,
};
}
exports.genNaturalTSContextJSONForUI = genNaturalTSContextJSONForUI;
/**
* 根据 tsContextJSON 生成 View 的 TypeScript 上下文
* @param tsContextJSON: 通过 genNaturalTSContextJSONForUI 获得到 tsContextJSON
* @param codeType: 'detail' | 'short'。detail 为详细代码,short 为简短代码
* 'detail' 将所有的code都返回(拼接成详细的code)。RAG召回,分析和执行阶段使用
* 'short' 只返回简短的code。模型召回,对应的分析阶段使用
* @param requiredIndexes?: 通过索引获取对应的 code。可挑选需要的 code
**/
const genNaturalTSContextFromJSONForUI = (tsContextJSON, codeType, requiredIndexes) => {
const { naslCore, naslUtil, components, componentDeclaration, enums, entities, structures, appLogics, interfaceDependencies, dependencies, frontendVariables, views, currentPage, } = tsContextJSON;
let code = '';
code += '### [1] 标准库与类型定义\n';
code += (0, getContext_1.wrapTSBlock)(naslCore);
code += '\n### [2] 平台内置函数库\n';
code += (0, getContext_1.wrapTSBlock)(naslUtil);
let requiredIndexesName = [];
if (components) {
code += '\n### [3] nasl.ui\n';
const { code: componentsCode, requiredIndexesName: requiredIndexesNameTemp } = (0, getContext_1.getNaslUICode)(components, codeType, requiredIndexes);
requiredIndexesName = requiredIndexesNameTemp;
code += componentsCode;
const newComponentDeclaration = (0, getContext_1.removeUnnecessary)(componentDeclaration, requiredIndexesName, codeType);
if (newComponentDeclaration) {
code += '\n#### [3-1] 组件声明\n';
code += (0, getContext_1.wrapTSBlock)(`declare namespace nasl.ui {\n${newComponentDeclaration}\n}`);
}
}
code += '\n### [5] 全局上下文\n';
if (enums.data?.length) {
code += '\n#### [5-0] 枚举\n';
code += (0, getContext_1.getEnumsCode)(enums);
}
if (entities?.length) {
code += '\n#### [5-1] 实体\n';
code += (0, getContext_1.getEntitiesCode)(entities);
}
if (structures.data?.length) {
code += '\n#### [5-2] 可用的数据结构\n';
code += (0, getContext_1.getStructuresCode)(structures);
}
if (appLogics.data?.length) {
code += '\n### [6] 服务端逻辑\n';
code += (0, getContext_1.getLogicsCode)(appLogics, codeType, requiredIndexes);
}
if (interfaceDependencies) {
code += '\n### [7] 可调用的接口列表\n';
code += (0, getContext_1.getModuleInterfacesCode)(interfaceDependencies, codeType, requiredIndexes);
}
if (dependencies) {
code += '\n### [9] 依赖库逻辑列表\n';
code += (0, getContext_1.getDependenciesCode)(dependencies, codeType, requiredIndexes);
}
code += '\n### [10] 页面上下文\n';
if (frontendVariables?.data?.length) {
code += '\n#### [10-0] 前端全局变量\n';
code += (0, getContext_1.getVariablesCode)(frontendVariables);
}
if (views?.data?.length) {
code += '\n#### [10-1] 前端页面列表\n';
code += (0, getContext_1.getFrontendViewsCode)(views);
}
code += '\n### 当前页面上下文\n';
code += (0, getContext_1.wrapTSBlock)(currentPage);
return code;
};
exports.genNaturalTSContextFromJSONForUI = genNaturalTSContextFromJSONForUI;
// 兼容各版本,统一调用 genNaturalTSContextForUI 方法
function genNaturalTSContextForUI(app, currentNode, focusedNodePath, requiredIndexes, material, restParams) {
const tsContextJSON = genNaturalTSContextJSONForUI(app, currentNode, material, restParams);
const code = (0, exports.genNaturalTSContextFromJSONForUI)(tsContextJSON, restParams?.codeType || 'short', requiredIndexes);
return code;
}
exports.genNaturalTSContextForUI = genNaturalTSContextForUI;
//# sourceMappingURL=genNaturalTS.js.map