Spaces:
Runtime error
Runtime error
Commit
·
02ea92c
1
Parent(s):
f915671
Update clewd.js
Browse files
clewd.js
CHANGED
|
@@ -35,7 +35,8 @@ const convertToType = value => {
|
|
| 35 |
const {countTokens} = require('@anthropic-ai/tokenizer');
|
| 36 |
const placeholder = Config.padtxt_placeholder || randomBytes(randomInt(5, 15)).toString('hex');
|
| 37 |
tokens = countTokens(content);
|
| 38 |
-
|
|
|
|
| 39 |
return content;
|
| 40 |
}, xmlPlot = (content, nonsys = false) => {
|
| 41 |
const card = content.includes('<card>');
|
|
@@ -117,11 +118,9 @@ const convertToType = value => {
|
|
| 117 |
.replace(/(?<=\n)\n(?=\n)/g, '');
|
| 118 |
//确保格式正确
|
| 119 |
if (apiKey) {
|
| 120 |
-
content = content.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
content.includes('<|reverseHA|>') && (content = content.replace(/\s*<\|reverseHA\|>\s*/g, '\n\n').replace(/\n\n(Assistant|Human):/g, function(match, p1) {return p1 === 'Human' ? '\n\nAssistant:' : '\n\nHuman:'}))
|
| 124 |
-
return content;
|
| 125 |
} else {
|
| 126 |
return content.trim().replace(/^Human:|\n\nAssistant:$/g, '');
|
| 127 |
}
|
|
@@ -225,14 +224,12 @@ const updateParams = res => {
|
|
| 225 |
method: 'DELETE'
|
| 226 |
});
|
| 227 |
updateParams(res);
|
| 228 |
-
} catch (err) { //
|
| 229 |
-
console.log(`[33mdeleteChat failed[0m`); //
|
| 230 |
-
} //
|
| 231 |
}, onListen = async () => {
|
| 232 |
/***************************** */
|
| 233 |
if (Firstlogin) {
|
| 234 |
Firstlogin = false;
|
| 235 |
-
console.log(`[2m${Main}[0m\n[33mhttp://${Config.Ip}:${Config.Port}/v1[0m\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings
|
| 236 |
Config.Settings.Superfetch && SuperfetchAvailable(true);
|
| 237 |
if (Config.localtunnel) {
|
| 238 |
const localtunnel = require('localtunnel');
|
|
@@ -258,8 +255,7 @@ const updateParams = res => {
|
|
| 258 |
if ('SET YOUR COOKIE HERE' === Config.Cookie || Config.Cookie?.length < 1) {
|
| 259 |
throw Error('Set your cookie inside config.js');
|
| 260 |
}
|
| 261 |
-
|
| 262 |
-
updateCookies(Config.Cookie);
|
| 263 |
//console.log(`[2m${Main}[0m\n[33mhttp://${Config.Ip}:${Config.Port}/v1[0m\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings.includes(setting) ? `??? [31m${setting}: ${Config.Settings[setting]}[0m` : `[1m${setting}:[0m ${ChangedSettings.includes(setting) ? '[33m' : '[36m'}${Config.Settings[setting]}[0m`)).sort().join('\n')}\n`);
|
| 264 |
//Config.Settings.Superfetch && SuperfetchAvailable(true);
|
| 265 |
const accRes = await fetch(Config.rProxy + '/api/organizations', {
|
|
@@ -387,7 +383,7 @@ const updateParams = res => {
|
|
| 387 |
}
|
| 388 |
/***************************** */
|
| 389 |
}, writeSettings = async (config, firstRun = false) => {
|
| 390 |
-
if (process.env.Cookie || process.env.CookieArray) return
|
| 391 |
write(ConfigPath, `/*\n* https://rentry.org/teralomaniac_clewd\n* https://github.com/teralomaniac/clewd\n*/\n\n// SET YOUR COOKIE BELOW\n\nmodule.exports = ${JSON.stringify(config, null, 4)}\n\n/*\n BufferSize\n * How many characters will be buffered before the AI types once\n * lower = less chance of \`PreventImperson\` working properly\n\n ---\n\n SystemInterval\n * How many messages until \`SystemExperiments alternates\`\n\n ---\n\n Other settings\n * https://gitgud.io/ahsk/clewd/#defaults\n * and\n * https://gitgud.io/ahsk/clewd/-/blob/master/CHANGELOG.md\n */`.trim().replace(/((?<!\r)\n|\r(?!\n))/g, '\r\n'));
|
| 392 |
if (firstRun) {
|
| 393 |
console.warn('[33mconfig file created!\nedit[0m [1mconfig.js[0m [33mto set your settings and restart the program[0m');
|
|
@@ -783,14 +779,14 @@ const updateParams = res => {
|
|
| 783 |
clewdStream.censored && console.warn('[33mlikely your account is hard-censored[0m');
|
| 784 |
prevImpersonated = clewdStream.impersonated;
|
| 785 |
setTitle('ok ' + bytesToSize(clewdStream.size));
|
| 786 |
-
429 == fetchAPI
|
| 787 |
clewdStream.empty();
|
| 788 |
}
|
| 789 |
if (!apiKey) { //if (prevImpersonated) {
|
| 790 |
await deleteChat(Conversation.uuid);
|
| 791 |
/******************************** */
|
| 792 |
changeflag += 1;
|
| 793 |
-
if (Config.CookieArray?.length > 0 && (429 == fetchAPI
|
| 794 |
changeflag = 0;
|
| 795 |
CookieChanger.emit('ChangeCookie');
|
| 796 |
}
|
|
@@ -864,7 +860,7 @@ const updateParams = res => {
|
|
| 864 |
Config.Settings[setting] = convertToType(process.env[setting]) ?? Config.Settings[setting];
|
| 865 |
}
|
| 866 |
} else {
|
| 867 |
-
Config[key] = key === 'CookieArray' ? (process.env[key]?.
|
| 868 |
}
|
| 869 |
}
|
| 870 |
Config.rProxy = Config.rProxy ? Config.rProxy.replace(/\/$/, '') : AI.end();
|
|
|
|
| 35 |
const {countTokens} = require('@anthropic-ai/tokenizer');
|
| 36 |
const placeholder = Config.padtxt_placeholder || randomBytes(randomInt(5, 15)).toString('hex');
|
| 37 |
tokens = countTokens(content);
|
| 38 |
+
const padding = placeholder.repeat(Math.floor(Math.max(1000, Config.Settings.padtxt - tokens) / countTokens(placeholder.trim())));
|
| 39 |
+
content = content.includes('<|padtxt|>') ? content.replace(/<\|padtxt\|>/, padding) : !apiKey ? padding + '\n\n\n' + content.trim() : content;
|
| 40 |
return content;
|
| 41 |
}, xmlPlot = (content, nonsys = false) => {
|
| 42 |
const card = content.includes('<card>');
|
|
|
|
| 118 |
.replace(/(?<=\n)\n(?=\n)/g, '');
|
| 119 |
//确保格式正确
|
| 120 |
if (apiKey) {
|
| 121 |
+
content = content.replace(/\n\n(Assistant|Human):(?!.*?\n\n(Assistant|Human):).*$/s, function(match, p1) {return p1 === 'Assistant' ? match : match + '\n\nAssistant: '}).replace(/\s*<\|noAssistant\|>\s*(.*?)(?:\n\nAssistant:\s*)?$/s, '\n\n$1');
|
| 122 |
+
content.includes('<|reverseHA|>') && (content = content.replace(/\s*<\|reverseHA\|>\s*/g, '\n\n').replace(/\n\n(Assistant|Human):/g, function(match, p1) {return p1 === 'Human' ? '\n\nAssistant:' : '\n\nHuman:'}));
|
| 123 |
+
return content.trim().replace(/^(Human|Assistant):/, '\n\n$&').replace(/\n\n(Human|Assistant):$/, '$& ');
|
|
|
|
|
|
|
| 124 |
} else {
|
| 125 |
return content.trim().replace(/^Human:|\n\nAssistant:$/g, '');
|
| 126 |
}
|
|
|
|
| 224 |
method: 'DELETE'
|
| 225 |
});
|
| 226 |
updateParams(res);
|
| 227 |
+
} catch (err) {console.log(`[33mdeleteChat failed[0m`)}; //
|
|
|
|
|
|
|
| 228 |
}, onListen = async () => {
|
| 229 |
/***************************** */
|
| 230 |
if (Firstlogin) {
|
| 231 |
Firstlogin = false;
|
| 232 |
+
console.log(`[2m${Main}[0m\n[33mhttp://${Config.Ip}:${Config.Port}/v1[0m\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings?.includes(setting) ? `??? [31m${setting}: ${Config.Settings[setting]}[0m` : `[1m${setting}:[0m ${ChangedSettings?.includes(setting) ? '[33m' : '[36m'}${Config.Settings[setting]}[0m`)).sort().join('\n')}\n`);
|
| 233 |
Config.Settings.Superfetch && SuperfetchAvailable(true);
|
| 234 |
if (Config.localtunnel) {
|
| 235 |
const localtunnel = require('localtunnel');
|
|
|
|
| 255 |
if ('SET YOUR COOKIE HERE' === Config.Cookie || Config.Cookie?.length < 1) {
|
| 256 |
throw Error('Set your cookie inside config.js');
|
| 257 |
}
|
| 258 |
+
updateCookies(Config.Cookie.replace(/^(sessionKey=)?/, 'sessionKey=')); //updateCookies(Config.Cookie);
|
|
|
|
| 259 |
//console.log(`[2m${Main}[0m\n[33mhttp://${Config.Ip}:${Config.Port}/v1[0m\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings.includes(setting) ? `??? [31m${setting}: ${Config.Settings[setting]}[0m` : `[1m${setting}:[0m ${ChangedSettings.includes(setting) ? '[33m' : '[36m'}${Config.Settings[setting]}[0m`)).sort().join('\n')}\n`);
|
| 260 |
//Config.Settings.Superfetch && SuperfetchAvailable(true);
|
| 261 |
const accRes = await fetch(Config.rProxy + '/api/organizations', {
|
|
|
|
| 383 |
}
|
| 384 |
/***************************** */
|
| 385 |
}, writeSettings = async (config, firstRun = false) => {
|
| 386 |
+
if (process.env.Cookie || process.env.CookieArray) return; //
|
| 387 |
write(ConfigPath, `/*\n* https://rentry.org/teralomaniac_clewd\n* https://github.com/teralomaniac/clewd\n*/\n\n// SET YOUR COOKIE BELOW\n\nmodule.exports = ${JSON.stringify(config, null, 4)}\n\n/*\n BufferSize\n * How many characters will be buffered before the AI types once\n * lower = less chance of \`PreventImperson\` working properly\n\n ---\n\n SystemInterval\n * How many messages until \`SystemExperiments alternates\`\n\n ---\n\n Other settings\n * https://gitgud.io/ahsk/clewd/#defaults\n * and\n * https://gitgud.io/ahsk/clewd/-/blob/master/CHANGELOG.md\n */`.trim().replace(/((?<!\r)\n|\r(?!\n))/g, '\r\n'));
|
| 388 |
if (firstRun) {
|
| 389 |
console.warn('[33mconfig file created!\nedit[0m [1mconfig.js[0m [33mto set your settings and restart the program[0m');
|
|
|
|
| 779 |
clewdStream.censored && console.warn('[33mlikely your account is hard-censored[0m');
|
| 780 |
prevImpersonated = clewdStream.impersonated;
|
| 781 |
setTitle('ok ' + bytesToSize(clewdStream.size));
|
| 782 |
+
429 == fetchAPI?.status ? console.log(`[35mExceeded limit![0m\n`) : console.log(`${200 == fetchAPI?.status ? '[32m' : '[33m'}${fetchAPI?.status}![0m\n`); //console.log(`${200 == fetchAPI.status ? '[32m' : '[33m'}${fetchAPI.status}![0m\n`);
|
| 783 |
clewdStream.empty();
|
| 784 |
}
|
| 785 |
if (!apiKey) { //if (prevImpersonated) {
|
| 786 |
await deleteChat(Conversation.uuid);
|
| 787 |
/******************************** */
|
| 788 |
changeflag += 1;
|
| 789 |
+
if (Config.CookieArray?.length > 0 && (429 == fetchAPI?.status || Config.Cookiecounter && changeflag >= Config.Cookiecounter)) {
|
| 790 |
changeflag = 0;
|
| 791 |
CookieChanger.emit('ChangeCookie');
|
| 792 |
}
|
|
|
|
| 860 |
Config.Settings[setting] = convertToType(process.env[setting]) ?? Config.Settings[setting];
|
| 861 |
}
|
| 862 |
} else {
|
| 863 |
+
Config[key] = key === 'CookieArray' ? (process.env[key]?.match(/(sessionKey=)?sk-ant-sid01-[\w-]{86}-[\w-]{6}AA/g) ?? Config[key]) : (convertToType(process.env[key]) ?? Config[key]);
|
| 864 |
}
|
| 865 |
}
|
| 866 |
Config.rProxy = Config.rProxy ? Config.rProxy.replace(/\/$/, '') : AI.end();
|