From 5228ac36e7d490ab3eb49f170d8fcfb250a43845 Mon Sep 17 00:00:00 2001 From: GramingFoxTeam Date: Fri, 14 Jul 2023 19:59:03 +0300 Subject: [PATCH] feat(server): regex support for server-side --- apps/server/src/PROTOCOL.md | 2 +- apps/server/src/config.example.json | 9 +++++++++ apps/server/src/config.json | 9 +++++++++ apps/server/src/events/runAI.js | 23 +++++++++++++++++++++-- apps/server/src/index.js | 3 ++- 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 apps/server/src/config.example.json create mode 100644 apps/server/src/config.json diff --git a/apps/server/src/PROTOCOL.md b/apps/server/src/PROTOCOL.md index ce39387..4cfbdd4 100644 --- a/apps/server/src/PROTOCOL.md +++ b/apps/server/src/PROTOCOL.md @@ -23,10 +23,10 @@ And the server would return something like this: "response": [ { "confidence": 0.99, - "id": "String", "name": "revanced_download" } ] +} ``` ### Training the AI diff --git a/apps/server/src/config.example.json b/apps/server/src/config.example.json new file mode 100644 index 0000000..cdd14d0 --- /dev/null +++ b/apps/server/src/config.example.json @@ -0,0 +1,9 @@ +{ + "regexSupport": false, + "regexResponses": [ + { + "label": "videobuffer", + "regex": "insert regex here" + } + ] +} \ No newline at end of file diff --git a/apps/server/src/config.json b/apps/server/src/config.json new file mode 100644 index 0000000..cdd14d0 --- /dev/null +++ b/apps/server/src/config.json @@ -0,0 +1,9 @@ +{ + "regexSupport": false, + "regexResponses": [ + { + "label": "videobuffer", + "regex": "insert regex here" + } + ] +} \ No newline at end of file diff --git a/apps/server/src/events/runAI.js b/apps/server/src/events/runAI.js index c22a0b5..85b3772 100644 --- a/apps/server/src/events/runAI.js +++ b/apps/server/src/events/runAI.js @@ -1,7 +1,26 @@ import { serialize } from 'bson'; -export default async function runAI(client, data) { - const witAIReq = await fetch( +export default async function runAI(client, data, config) { + if (config.regexSupport) { + for (const reply of config.regexResponses) { + if (new RegExp(reply.regex).test(data.text)) { + client.write( + serialize({ + op: 2, + id: data.id, + response: [ + { + confidence: 1, + name: reply.label + } + ] + }) + ); + return; + } + } + } + const witAIReq = await fetch( `https://api.wit.ai/message?v=20230215&q=${encodeURI(data.text)}`, { headers: { diff --git a/apps/server/src/index.js b/apps/server/src/index.js index 1a4ec11..1e40d87 100644 --- a/apps/server/src/index.js +++ b/apps/server/src/index.js @@ -1,6 +1,7 @@ import { createServer } from 'node:net'; import { deserialize } from 'bson'; import { runAI, runOCR, trainAI } from './events/index.js'; +import Config from './config.json' assert { type: 'json' }; const server = createServer(async (client) => { client.on('data', async (data) => { @@ -10,7 +11,7 @@ const server = createServer(async (client) => { switch (eventData.op) { case 1: { - runAI(client, eventData); + runAI(client, eventData, Config); break; }