Browse Source

feat: ncloud/func with vm

未来全栈 3 months ago
parent
commit
e1bfe969c6

+ 21 - 0
rag-server/api/ncloud/README.md

@@ -0,0 +1,21 @@
+# JS沙盒
+- isolated-vm
+    - https://www.npmjs.com/package/isolated-vm
+        - vm => vm2 => isolated-vm
+            - 官方API:https://nodejs.org/api/vm.html#vm_vm_executing_javascript
+            - vm2放弃维护 => isolated-vm
+
+# 环境允许模块
+- module 云函数可写成module包形式,便于编写、加载和测试
+- fetch 前后端同构网络请求库
+
+# /func 函数执行接口
+
+``` bash
+curl -X POST http://127.0.0.1:1337/api/ncloud/func
+```
+
+# 云函数编写注意事项
+- 关于打印调试
+    - vm环境中console.log不可见
+    - 需要return或者throw在调用外部打印具体结果

+ 54 - 0
rag-server/api/ncloud/func/routes.js

@@ -0,0 +1,54 @@
+// routes.js
+const express = require('express');
+const router = express.Router();
+
+// 虚拟沙盒
+const vm = require('node:vm');
+global.Parse = {
+    title:"Parse"
+}
+const envContext = {
+    Parse:global.Parse,
+    module:module, // 允许代码段编写模块
+}
+
+
+// POST 路由处理
+/**
+ * @param code 代码
+ */
+router.post('/func', async (req, res) => {
+    let { code,func,params } = req.body;
+
+    // 加载模块
+    console.log(req.body)
+    let cloudModule = loadModule(code);
+
+    // 调用 exampleFunc 方法
+    let result
+    
+    try {
+        console.log(cloudModule,result)
+        result = await cloudModule[func](params); // 输出: Hello from exampleFunc!
+        res.json({
+            code:200,
+            data:result
+        });
+    } catch (error) {
+        res.status(500).json({ 
+            code:500,
+            error:error
+        });
+    }
+});
+
+
+
+function loadModule(code) {
+    // 使用 vm.runInNewContext 执行代码
+    vm.runInNewContext(code, envContext);
+    // 返回上下文中的模块对象
+    return envContext.module.exports;
+}
+
+module.exports = router;

+ 26 - 0
rag-server/api/ncloud/test/test-func.js

@@ -0,0 +1,26 @@
+
+
+testFunc()
+async function testFunc(){
+
+    let response = await fetch("http://127.0.0.1:1337/api/ncloud/func",{
+        method:"POST",
+        headers: {
+            "Content-Type": "application/json",
+        },
+        body:JSON.stringify({
+            code:`
+            module.exports.exampleFunc = function(params) {
+                console.log('Hello from '+params?.title+'!');
+                return "666!"+params?.title
+            };
+            `,
+            func:"exampleFunc",
+            params:{
+                title:"NovaCloud Function Call"
+            }
+        })
+    })
+    let json = await response.json()
+    console.log(json)
+}

+ 2 - 0
rag-server/dev-server.js

@@ -36,6 +36,8 @@ async function main(){
   app.use('/api/agent', pdfRouter); // 使用路由
   const retriveRouter = require('./api/agent/retrive/routes'); // 根据你的文件结构调整路径
   app.use('/api/agent', retriveRouter); // 使用路由
+  const ncloudFunc = require('./api/ncloud/func/routes'); 
+  app.use('/api/ncloud', ncloudFunc); // 使用路由
 
   const port = 1337;
   app.listen(port, function() {

+ 0 - 0
rag-server/logs/parse-server.err.2024-12-24


+ 20 - 0
rag-server/logs/parse-server.info.2024-12-24

@@ -0,0 +1,20 @@
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:53:42.715Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:54:59.907Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:55:12.831Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:56:15.999Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:57:47.401Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:58:19.197Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:59:03.957Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T06:59:12.844Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:07:14.359Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:08:22.085Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:09:08.056Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:09:26.715Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:09:37.540Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:10:09.610Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:10:38.731Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:10:56.271Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:11:52.245Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:12:40.611Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:13:35.759Z"}
+{"level":"warn","message":"DeprecationWarning: The Parse Server option 'encodeParseObjectInCloudFunction' default will change to 'true' in a future version.","timestamp":"2024-12-24T07:14:09.798Z"}

File diff suppressed because it is too large
+ 290 - 765
rag-server/package-lock.json


+ 1 - 0
rag-server/package.json

@@ -13,6 +13,7 @@
     "gpt-tokenizer": "^2.8.1",
     "langchain": "^0.3.7",
     "mammoth": "^1.8.0",
+    "parse-server": "^7.4.0",
     "pdf-parse": "^1.1.1",
     "pg-promise": "^11.10.2",
     "shelljs": "^0.8.5"

+ 11 - 0
vm-server/package.json

@@ -0,0 +1,11 @@
+{
+  "name": "vm-server",
+  "version": "1.0.0",
+  "description": "- isolated-vm\r     - https://www.npmjs.com/package/isolated-vm\r         - vm => vm2 => isolated-vm",
+  "main": "server.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC"
+}

+ 0 - 0
vm-server/server.js


Some files were not shown because too many files changed in this diff