Team Ai
Apppublic

kernelimplode/clewd

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
clewd-utils.js117 linesDownload Raw Back to lib
1/*2* https://gitgud.io/ahsk/clewd3* https://github.com/h-a-s-k/clewd4*/5'use strict';6 7const {randomInt, randomBytes} = require('node:crypto'), {version: Version} = require('../package.json'), Encoder = (new TextDecoder, 8new TextEncoder), Main = 'clewd修改版 v' + Version + '(11) by tera', Replacements = {9    user: 'Human',10    assistant: 'Assistant',11    system: '',12    example_user: 'H',13    example_assistant: 'A'14}, DangerChars = [ ...new Set([ ...Object.values(Replacements).join(''), ...'\n', ...':', ...'\\n' ]) ].filter((char => ' ' !== char)).sort(), AI = {15    end: () => Buffer.from([ 104, 116, 116, 112, 115, 58, 47, 47, 99, 108, 97, 117, 100, 101, 46, 97, 105 ]).toString(),16    mdl: () => Buffer.from([ 99, 108, 97, 117, 100, 101, 45, 50, 46, 49 ]).toString(),17    zone: () => Buffer.from([ 65, 115, 105, 97, 47, 83, 104, 97, 110, 103, 104, 97, 105 ]).toString(), //Buffer.from([ 65, 109, 101, 114, 105, 99, 97, 47, 78, 101, 119, 95, 89, 111, 114, 107 ]).toString(),18    agent: () => Buffer.from([ 77, 111, 122, 105, 108, 108, 97, 47, 53, 46, 48, 32, 40, 77, 97, 99, 105, 110, 116, 111, 115, 104, 59, 32, 73, 110, 116, 101, 108, 32, 77, 97, 99, 32, 79, 83, 32, 88, 32, 49, 48, 95, 49, 53, 95, 55, 41, 32, 65, 112, 112, 108, 101, 87, 101, 98, 75, 105, 116, 47, 53, 51, 55, 46, 51, 54, 32, 40, 75, 72, 84, 77, 76, 44, 32, 108, 105, 107, 101, 32, 71, 101, 99, 107, 111, 41, 32, 67, 104, 114, 111, 109, 101, 47, 49, 49, 52, 46, 48, 46, 48, 46, 48, 32, 83, 97, 102, 97, 114, 105, 47, 53, 51, 55, 46, 51, 54, 32, 69, 100, 103, 47, 49, 49, 52, 46, 48, 46, 49, 56, 50, 51, 46, 55, 57 ]).toString(),19    cp: () => Buffer.from([ 55, 55, 49, 44, 52, 56, 54, 53, 45, 52, 56, 54, 54, 45, 52, 56, 54, 55, 45, 52, 57, 49, 57, 53, 45, 52, 57, 49, 57, 57, 45, 52, 57, 49, 57, 54, 45, 52, 57, 50, 48, 48, 45, 53, 50, 51, 57, 51, 45, 53, 50, 51, 57, 50, 45, 52, 57, 49, 55, 49, 45, 52, 57, 49, 55, 50, 45, 49, 53, 54, 45, 49, 53, 55, 45, 52, 55, 45, 53, 51, 44, 48, 45, 50, 51, 45, 54, 53, 50, 56, 49, 45, 49, 48, 45, 49, 49, 45, 51, 53, 45, 49, 54, 45, 53, 45, 49, 51, 45, 49, 56, 45, 53, 49, 45, 52, 53, 45, 52, 51, 45, 50, 55, 45, 49, 55, 53, 49, 51, 45, 50, 49, 44, 50, 57, 45, 50, 51, 45, 50, 52, 44, 48 ]).toString(),20    hdr: refPath => ({21        'Content-Type': 'application/json',22        Referer: `${AI.end()}/${refPath ? 'chat/' + refPath : ''}`,23        Origin: '' + AI.end()24    })25}, indexOfH = (text, last = false) => {26    let location = -1;27    const matchesH = text.match(/(?:(?:\\n)|\n){2}((?:Human|H): ?)/gm);28    matchesH?.length > 0 && (location = last ? text.lastIndexOf(matchesH[matchesH.length - 1]) : text.indexOf(matchesH[0]));29    return location;30}, indexOfA = (text, last = false) => {31    let location = -1;32    const matchesA = text.match(/(?:(?:\\n)|\n){2}((?:Assistant|A): ?)/gm);33    matchesA?.length > 0 && (location = last ? text.lastIndexOf(matchesA[matchesA.length - 1]) : text.indexOf(matchesA[0]));34    return location;35};36 37module.exports.encodeDataJSON = completion => Encoder.encode(`data: ${JSON.stringify(completion)}\n\n`);38 39module.exports.genericFixes = text => text.replace(/(\r\n|\r|\\n)/gm, '\n');40 41module.exports.Replacements = Replacements;42 43module.exports.DangerChars = DangerChars;44 45module.exports.checkResErr = async (res, CookieChanger, throwIt = true) => { //module.exports.checkResErr = async (res, throwIt = true) => {46    let err, json, errAPI;47    if ('string' == typeof res) {48        json = JSON.parse(res);49        errAPI = json.error;50        err = Error(errAPI.message);51    } else if (res.status < 200 || res.status >= 300) {52        err = Error('Unexpected response code: ' + (res.status || json.status));53        json = await res.json();54        errAPI = json.error;55    }56    if (errAPI) {57        err.status = res.status || json.status;58        err.planned = true;59        errAPI.message && (err.message = errAPI.message);60        errAPI.type && (err.type = errAPI.type);61        if ((429 === res.status || 429 === json.status) && errAPI.resets_at) {62            const hours = ((new Date(1e3 * errAPI.resets_at).getTime() - Date.now()) / 1e3 / 60 / 60).toFixed(1);63            err.message += `, expires in ${hours} hours`;64/************************** */65            if (CookieChanger) {66                console.log(`Exceeded limit!\n`);67                CookieChanger && CookieChanger.emit('ChangeCookie');68            }69/************************** */70        }71        if (throwIt) {72            throw err;73        }74    }75    return err;76};77 78module.exports.bytesToSize = (bytes = 0) => {79    const b = [ 'B', 'KB', 'MB', 'GB', 'TB' ];80    if (0 === bytes) {81        return '0 B';82    }83    const c = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 4);84    return 0 === c ? `${bytes} ${b[c]}` : `${(bytes / 1024 ** c).toFixed(1)} ${b[c]}`;85};86 87module.exports.indexOfAny = (text, last = false) => {88    let location = -1;89    const fakes = [ indexOfH(text, last), indexOfA(text, last) ].filter((idx => idx > -1)).sort();90    location = last ? fakes.reverse()[0] : fakes[0];91    return isNaN(location) ? -1 : location;92};93 94module.exports.cleanJSON = json => json.replace(/^data: {/gim, '{').replace(/\s+$/gim, '');95 96module.exports.fileName = () => {97    const len = randomInt(5, 15);98    let name = randomBytes(len).toString('hex');99    for (let i = 0; i < name.length; i++) {100        const char = name.charAt(i);101        isNaN(char) && randomInt(1, 5) % 2 == 0 && ' ' !== name.charAt(i - 1) && (name = name.slice(0, i) + ' ' + name.slice(i));102    }103    return name + '.txt';104};105 106module.exports.indexOfA = indexOfA;107 108module.exports.indexOfH = indexOfH;109 110module.exports.setTitle = title => {111    title = `${Main} - ${title}`;112    process.title !== title && (process.title = title);113};114 115module.exports.Main = Main;116 117module.exports.AI = AI;