teralomaniac commited on
Commit
02ea92c
1 Parent(s): f915671

Update clewd.js

Browse files
Files changed (1) hide show
  1. clewd.js +12 -16
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
- !apiKey && (content = placeholder.repeat(Math.floor(Math.max(1000, Config.Settings.padtxt - tokens) / countTokens(placeholder.trim()))) + '\n\n\n' + content.trim());
 
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.trim().replace(/^Human:/, '\n\nHuman:')
121
- .replace(/(\n\nAssistant|\n\nHuman):(?!.*?\n\n(Assistant|Human):).*$/s, function(match, p1) {return p1 === '\n\nAssistant' ? match : match + '\n\nAssistant: '})
122
- .replace(/\s*<\|noAssistant\|>\s*(.*?)(?:\n\nAssistant:)?$/s, '\n\n$1');
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(`deleteChat failed`); //
230
- } //
231
  }, onListen = async () => {
232
  /***************************** */
233
  if (Firstlogin) {
234
  Firstlogin = false;
235
- console.log(`${Main}\nhttp://${Config.Ip}:${Config.Port}/v1\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings.includes(setting) ? `??? ${setting}: ${Config.Settings[setting]}` : `${setting}: ${ChangedSettings.includes(setting) ? '' : ''}${Config.Settings[setting]}`)).sort().join('\n')}\n`);
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
- !/^sessionKey=/.test(Config.Cookie) && (Config.Cookie += 'sessionKey='); //
262
- updateCookies(Config.Cookie);
263
  //console.log(`${Main}\nhttp://${Config.Ip}:${Config.Port}/v1\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings.includes(setting) ? `??? ${setting}: ${Config.Settings[setting]}` : `${setting}: ${ChangedSettings.includes(setting) ? '' : ''}${Config.Settings[setting]}`)).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 ChangedSettings = '', UnknownSettings = '';
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('config file created!\nedit config.js to set your settings and restart the program');
@@ -783,14 +779,14 @@ const updateParams = res => {
783
  clewdStream.censored && console.warn('likely your account is hard-censored');
784
  prevImpersonated = clewdStream.impersonated;
785
  setTitle('ok ' + bytesToSize(clewdStream.size));
786
- 429 == fetchAPI.status ? console.log(`Exceeded limit!\n`) : console.log(`${200 == fetchAPI.status ? '' : ''}${fetchAPI.status}!\n`); //console.log(`${200 == fetchAPI.status ? '' : ''}${fetchAPI.status}!\n`);
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.status || Config.Cookiecounter && changeflag >= Config.Cookiecounter)) {
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]?.split(',')?.map(x => x.replace(/[\[\]"\s]/g, '')) ?? Config[key]) : (convertToType(process.env[key]) ?? Config[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(`deleteChat failed`)}; //
 
 
228
  }, onListen = async () => {
229
  /***************************** */
230
  if (Firstlogin) {
231
  Firstlogin = false;
232
+ console.log(`${Main}\nhttp://${Config.Ip}:${Config.Port}/v1\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings?.includes(setting) ? `??? ${setting}: ${Config.Settings[setting]}` : `${setting}: ${ChangedSettings?.includes(setting) ? '' : ''}${Config.Settings[setting]}`)).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(`${Main}\nhttp://${Config.Ip}:${Config.Port}/v1\n\n${Object.keys(Config.Settings).map((setting => UnknownSettings.includes(setting) ? `??? ${setting}: ${Config.Settings[setting]}` : `${setting}: ${ChangedSettings.includes(setting) ? '' : ''}${Config.Settings[setting]}`)).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('config file created!\nedit config.js to set your settings and restart the program');
 
779
  clewdStream.censored && console.warn('likely your account is hard-censored');
780
  prevImpersonated = clewdStream.impersonated;
781
  setTitle('ok ' + bytesToSize(clewdStream.size));
782
+ 429 == fetchAPI?.status ? console.log(`Exceeded limit!\n`) : console.log(`${200 == fetchAPI?.status ? '' : ''}${fetchAPI?.status}!\n`); //console.log(`${200 == fetchAPI.status ? '' : ''}${fetchAPI.status}!\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();