Skip to content

Commit d37f123

Browse files
committed
test
1 parent b415335 commit d37f123

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

clewd.js

+47-43
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@
77
const {createServer: Server, IncomingMessage, ServerResponse} = require('node:http'), {createHash: Hash, randomUUID, randomInt, randomBytes} = require('node:crypto'), {TransformStream, ReadableStream} = require('node:stream/web'), {Readable, Writable} = require('node:stream'), {Blob} = require('node:buffer'), {existsSync: exists, writeFileSync: write, createWriteStream} = require('node:fs'), {join: joinP} = require('node:path'), {ClewdSuperfetch: Superfetch, SuperfetchAvailable} = require('./lib/clewd-superfetch'), {AI, fileName, genericFixes, bytesToSize, setTitle, checkResErr, Replacements, Main} = require('./lib/clewd-utils'), ClewdStream = require('./lib/clewd-stream');
88

99
/******************************************************* */
10-
let currentIndex, Firstlogin = true, changeflag = 0, changetime = 0, totaltime, uuidOrgArray = [], model, tokens, apiKey;
10+
let currentIndex, Firstlogin = true, changeflag = 0, changetime = 0, totaltime, invalidtime = 0, uuidOrgArray = [], model, tokens, apiKey, timestamp = Date.now();
1111

1212
const events = require('events'), CookieChanger = new events.EventEmitter();
1313
require('events').EventEmitter.defaultMaxListeners = 0;
1414

1515
CookieChanger.on('ChangeCookie', () => {
16-
changeflag = 0;
17-
Proxy && Proxy.close();
18-
console.log(`Changing Cookie...\n`);
19-
Proxy.listen(Config.Port, Config.Ip, onListen);
20-
Proxy.on('error', (err => {
21-
console.error('Proxy error\n%o', err);
22-
}));
16+
setTimeout(() => {
17+
changeflag = 0;
18+
Proxy && Proxy.close();
19+
console.log(`Changing Cookie...\n`);
20+
Proxy.listen(Config.Port, Config.Ip, onListen);
21+
Proxy.on('error', (err => {
22+
console.error('Proxy error\n%o', err);
23+
}));
24+
timestamp = Date.now();
25+
invalidtime += 1;
26+
}, !Config.rProxy || Config.rProxy === AI.end() ? 15000 + timestamp - Date.now() : 0);
2327
});
2428

2529
const convertToType = value => {
@@ -37,10 +41,9 @@ const convertToType = value => {
3741
const placeholder = Config.padtxt_placeholder || randomBytes(randomInt(5, 15)).toString('hex');
3842
tokens = countTokens(content);
3943
const padding = placeholder.repeat(Math.floor(Math.max(1000, Config.Settings.padtxt - tokens) / countTokens(placeholder.trim())));
40-
content = content.includes('<|padtxt|>') ? content.replace(/<\|padtxt\|>/, padding) : !apiKey ? padding + '\n\n\n' + content.trim() : content;
44+
content = content.includes('<|padtxt|>') ? content.replace(/<\|padtxt\|>/gm, padding) : !apiKey ? padding + '\n\n\n' + content.trim() : content;
4145
return content;
4246
}, xmlPlot = (content, nonsys = false) => {
43-
const card = content.includes('<card>') || content.includes('<|card|>');
4447
//role合并
4548
const MergeDisable = content.includes('<|Merge Disable|>');
4649
const MergeHumanDisable = content.includes('<|Merge Human Disable|>');
@@ -94,13 +97,7 @@ const convertToType = value => {
9497
if (!apiKey && segcontentlastIndex >= 2 && segcontentHuman[segcontentlastIndex].includes('<|Plain Prompt Enable|>') && !content.includes('\n\nPlainPrompt:')) {
9598
content = segcontentHuman.slice(0, segcontentlastIndex).join('\n\nHuman:') + '\n\nPlainPrompt:' + segcontentHuman.slice(segcontentlastIndex).join('\n\nHuman:').replace(/\n\nHuman: *PlainPrompt:/, '\n\nPlainPrompt:');
9699
}
97-
//<card>群组
98-
if (!card) {
99-
return content.replace(/(<reply>\n|\n<\/reply>)/g, '').replace(/<customname>(.*?)<\/customname>/gm, '$1');
100-
} else {
101-
content = content.replace(/(<reply>\n|\n<\/reply>)\1*/g, '$1').replace(/<customname>(.*?)<\/customname>:/gm, '$1:\n');
102-
}
103-
//<card>消除空XML tags、两端空白符和多余的\n
100+
//消除空XML tags、两端空白符和多余的\n
104101
content = content.replace(/\s*<\|curtail\|>\s*/g, '\n')
105102
.replace(/\n<\/(card|hidden|META)>\s+?<\1>\n/g, '\n')
106103
.replace(/\n<(\/?card|example|hidden|plot|META)>\s+?<\1>/g, '\n<$1>')
@@ -138,7 +135,7 @@ let uuidOrg, curPrompt = {}, prevPrompt = {}, prevMessages = [], prevImpersonate
138135
localtunnel: false,
139136
BufferSize: 1,
140137
SystemInterval: 3,
141-
rProxy: AI.end(),
138+
rProxy: '',
142139
api_rProxy: '',
143140
padtxt_placeholder: '',
144141
PromptExperimentFirst: '',
@@ -208,7 +205,7 @@ const updateParams = res => {
208205
return;
209206
}
210207
try { //
211-
const res = await fetch(`${Config.rProxy}/api/organizations/${uuidOrg}/chat_conversations/${uuid}`, {
208+
const res = await fetch(`${Config.rProxy || AI.end()}/api/organizations/${uuidOrg}/chat_conversations/${uuid}`, {
212209
headers: {
213210
...AI.hdr(),
214211
Cookie: getCookies()
@@ -244,13 +241,13 @@ const updateParams = res => {
244241
}
245242
try {
246243
/***************************** */
247-
if ('SET YOUR COOKIE HERE' === Config.Cookie || Config.Cookie?.length < 1) {
244+
if ('SET YOUR COOKIE HERE' === Config.Cookie || Config.Cookie?.length < 1 || (Config.CookieArray?.length > 0 && invalidtime >= totaltime)) { //if ('SET YOUR COOKIE HERE' === Config.Cookie || Config.Cookie?.length < 1) {
248245
return console.log(`No cookie available, apiKey-Only mode enabled.\n`); //throw Error('Set your cookie inside config.js');
249246
}
250247
updateCookies(Config.Cookie.replace(/^(sessionKey=)?/, 'sessionKey=')); //updateCookies(Config.Cookie);
251248
//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`);
252249
//Config.Settings.Superfetch && SuperfetchAvailable(true);
253-
const accRes = await fetch(Config.rProxy + '/api/organizations', {
250+
const accRes = await fetch((Config.rProxy || AI.end()) + '/api/organizations', {
254251
method: 'GET',
255252
headers: {
256253
...AI.hdr(),
@@ -276,7 +273,7 @@ const updateParams = res => {
276273
setTitle('ok');
277274
updateParams(accRes);
278275
/**************************** */
279-
const accountRes = await fetch(Config.rProxy + '/api/auth/current_account', {
276+
const accountRes = await fetch((Config.rProxy || AI.end()) + '/api/auth/current_account', {
280277
method: 'GET',
281278
headers: {
282279
...AI.hdr(),
@@ -335,7 +332,7 @@ const updateParams = res => {
335332
if ('consumer_restricted_mode' === type || 'consumer_banned' === type) { //if ('consumer_restricted_mode' === type) {
336333
return;
337334
}
338-
const req = await (Config.Settings.Superfetch ? Superfetch : fetch)(`${Config.rProxy}/api/organizations/${uuidOrg}/flags/${type}/dismiss`, {
335+
const req = await (Config.Settings.Superfetch ? Superfetch : fetch)(`${Config.rProxy || AI.end()}/api/organizations/${uuidOrg}/flags/${type}/dismiss`, {
339336
headers: {
340337
...AI.hdr(),
341338
Cookie: getCookies()
@@ -359,7 +356,7 @@ const updateParams = res => {
359356
return CookieChanger.emit('ChangeCookie');
360357
/***************************** */
361358
}
362-
const convRes = await fetch(`${Config.rProxy}/api/organizations/${uuidOrg}/chat_conversations`, {
359+
const convRes = await fetch(`${Config.rProxy || AI.end()}/api/organizations/${uuidOrg}/chat_conversations`, {
363360
method: 'GET',
364361
headers: {
365362
...AI.hdr(),
@@ -369,6 +366,7 @@ const updateParams = res => {
369366
updateParams(convRes);
370367
conversations.length > 0 && await Promise.all(conversations.map((conv => deleteChat(conv.uuid))));
371368
/***************************** */
369+
invalidtime = 0;
372370
} catch (err) {
373371
console.error('Clewd:\n%o', err);
374372
Config.CookieArray?.length > 0 && CookieChanger.emit('ChangeCookie');
@@ -429,15 +427,20 @@ const updateParams = res => {
429427
const body = JSON.parse(Buffer.concat(buffer).toString()), temperature = Math.max(.1, Math.min(1, body.temperature));
430428
let {messages} = body;
431429
/************************* */
432-
apiKey = /(?<=^Bearer \s*)sk-ant-api[\w-]*(?=\s*)$/.exec(req.headers.authorization);
433-
let api_max_tokens, api_model;
430+
apiKey = req.headers.authorization?.match(/sk-ant-api\d\d-[\w-]{86}-[\w-]{6}AA/g);
431+
let max_tokens_to_sample, stop_sequences;
434432
if (apiKey) {
435-
apiKey = apiKey[0];
436-
api_max_tokens = body.max_tokens;
437-
api_model = body.model;
438-
if (!api_model.includes('claude')) throw Error('Please change to claude model in "External"');
433+
stop_sequences = body.stop;
434+
max_tokens_to_sample = body.max_tokens;
435+
model = body.model;
436+
if (!model.includes('claude')) throw Error('Please change to claude model in "External"');
437+
} else if (req.headers.authorization.includes('sk-ant-api')) {
438+
throw Error('apiKey Wrong');
439439
} else if (Config.ProxyPassword != '' && req.headers.authorization != 'Bearer ' + Config.ProxyPassword) {
440440
throw Error('ProxyPassword Wrong');
441+
} else if (Config.CookieArray?.length > 0 && invalidtime >= Config.CookieArray?.length) {
442+
invalidtime = 0;
443+
return CookieChanger.emit('ChangeCookie');
441444
}
442445
/************************* */
443446
if (messages?.length < 1) {
@@ -518,7 +521,7 @@ const updateParams = res => {
518521
const names = Object.keys(headers), values = Object.values(headers);
519522
headers = names.map(((header, idx) => `${header}: ${values[idx]}`));
520523
}
521-
res = await (Config.Settings.Superfetch ? Superfetch : fetch)(Config.rProxy + '/api/retry_message', {
524+
res = await (Config.Settings.Superfetch ? Superfetch : fetch)((Config.rProxy || AI.end()) + '/api/retry_message', {
522525
stream: true,
523526
signal,
524527
method: 'POST',
@@ -534,7 +537,7 @@ const updateParams = res => {
534537
fetchAPI = await (async signal => {
535538
Conversation.uuid = randomUUID().toString();
536539
Conversation.depth = 0;
537-
const res = await (Config.Settings.Superfetch ? Superfetch : fetch)(`${Config.rProxy}/api/organizations/${uuidOrg}/chat_conversations`, {
540+
const res = await (Config.Settings.Superfetch ? Superfetch : fetch)(`${Config.rProxy || AI.end()}/api/organizations/${uuidOrg}/chat_conversations`, {
538541
signal,
539542
headers: {
540543
...AI.hdr(),
@@ -643,8 +646,8 @@ const updateParams = res => {
643646
/******************************** */
644647
if (Config.Settings.xmlPlot) {
645648
idx > 0 && (spacing = '\n\n');
646-
const prefix = message.customname ? message.role + ': <customname>' + message.name + '</customname>: ' : 'system' !== message.role || message.name ? Replacements[message.name || message.role] + ': ' : 'xmlPlot: ' + Replacements[message.role];
647-
return `${spacing}${prefix}${message.customname ? '<reply>\n' + message.content.trim() + '\n</reply>' : message.content}`;
649+
const prefix = message.customname ? message.role + ': ' + message.name + ': ' : 'system' !== message.role || message.name ? Replacements[message.name || message.role] + ': ' : 'xmlPlot: ' + Replacements[message.role];
650+
return `${spacing}${message.strip ? '' : prefix}${message.content}`;
648651
} else {
649652
/******************************** */
650653
idx > 0 && (spacing = systemMessages.includes(message) ? '\n' : '\n\n');
@@ -657,16 +660,16 @@ const updateParams = res => {
657660
systems
658661
};
659662
})(messages, type);
660-
console.log(`${apiKey ? api_model : model} [[2m${type}[0m]${!retryRegen && systems.length > 0 ? ' ' + systems.join(' [33m/[0m ') : ''}`); //console.log(`${model} [[2m${type}[0m]${!retryRegen && systems.length > 0 ? ' ' + systems.join(' [33m/[0m ') : ''}`);
663+
console.log(`${model} [[2m${type}[0m]${!retryRegen && systems.length > 0 ? ' ' + systems.join(' [33m/[0m ') : ''}`); //console.log(`${model} [[2m${type}[0m]${!retryRegen && systems.length > 0 ? ' ' + systems.join(' [33m/[0m ') : ''}`);
661664
'R' !== type || prompt || (prompt = '...regen...');
662665
/******************************** */
663-
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt, api_model && api_model != 'claude-2.1') : apiKey ? `\n\nHuman: ${genericFixes(prompt)}\n\nAssistant: ` : genericFixes(prompt);
666+
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt, model != AI.mdl()) : apiKey ? `\n\nHuman: ${genericFixes(prompt)}\n\nAssistant: ` : genericFixes(prompt);
664667
Config.Settings.FullColon && (prompt = apiKey
665668
? prompt.replace(/(\n\nAssistant|\n\nHuman):/, function(match, p1) {return p1 === '\n\nHuman' ? match : p1 + ':'}).replace(/(\n\nAssistant|\n\nHuman):(?!.*?\n\n(Assistant|Human):)/s, function(match, p1) {return p1 === '\n\nAssistant' ? match : p1 + ':'})
666669
: prompt.replace(/(?<=\n\n(H(?:uman)?|A(?:ssistant)?)):[ ]?/g, ': '));
667670
Config.Settings.padtxt && (prompt = padtxt(prompt));
668671
/******************************** */
669-
Logger?.write(`\n\n-------\n[${(new Date).toLocaleString()}]\n####### ${apiKey ? api_model : model} PROMPT (${type}):\n${prompt}\n--\n####### [Tokens: ${tokens}] REPLY:\n`); //Logger?.write(`\n\n-------\n[${(new Date).toLocaleString()}]\n####### PROMPT (${type}):\n${prompt}\n--\n####### REPLY:\n`);
672+
Logger?.write(`\n\n-------\n[${(new Date).toLocaleString()}]\n####### ${model} (${type}) ${tokens}t PROMPT:\n${prompt}\n--\n####### REPLY:\n`); //Logger?.write(`\n\n-------\n[${(new Date).toLocaleString()}]\n####### PROMPT (${type}):\n${prompt}\n--\n####### REPLY:\n`);
670673
retryRegen || (fetchAPI = await (async (signal, model, prompt, temperature, type) => {
671674
/******************************** */
672675
if (apiKey) {
@@ -675,12 +678,13 @@ const updateParams = res => {
675678
signal,
676679
headers: {
677680
'Content-Type': 'application/json',
678-
'x-api-key': apiKey,
681+
'x-api-key': apiKey[Math.floor(Math.random() * apiKey.length)],
679682
'anthropic-version': '2023-06-01'
680683
},
681684
body: JSON.stringify({
682-
model: api_model,
683-
max_tokens_to_sample: api_max_tokens,
685+
...stop_sequences && {stop_sequences},
686+
model,
687+
max_tokens_to_sample,
684688
stream: true,
685689
prompt,
686690
temperature
@@ -723,7 +727,7 @@ const updateParams = res => {
723727
Accept: 'text/event-stream',
724728
Cookie: getCookies()
725729
};
726-
res = await (Config.Settings.Superfetch ? Superfetch : fetch)(Config.rProxy + '/api/append_message', {
730+
res = await (Config.Settings.Superfetch ? Superfetch : fetch)((Config.rProxy || AI.end()) + '/api/append_message', {
727731
stream: true,
728732
signal,
729733
method: 'POST',
@@ -779,7 +783,7 @@ const updateParams = res => {
779783
await deleteChat(Conversation.uuid);
780784
/******************************** */
781785
changeflag += 1;
782-
if (Config.CookieArray?.length > 0 && (429 == fetchAPI?.status || Config.Cookiecounter && changeflag >= Config.Cookiecounter)) {
786+
if (Config.CookieArray?.length > 0 && (429 == fetchAPI?.status || Config.Cookiecounter > 0 && changeflag >= Config.Cookiecounter)) {
783787
changeflag = 0;
784788
CookieChanger.emit('ChangeCookie');
785789
}
@@ -856,7 +860,7 @@ const updateParams = res => {
856860
Config[key] = process.env[key] ? convertToType(process.env[key]) : Config[key];
857861
}
858862
}
859-
Config.rProxy = Config.rProxy ? Config.rProxy.replace(/\/$/, '') : AI.end();
863+
Config.rProxy = Config.rProxy.replace(/\/$/, '');
860864
Config.CookieArray = [...new Set([Config.CookieArray].join('').match(/(sessionKey=)?sk-ant-sid01-[\w-]{86}-[\w-]{6}AA/g))];
861865
writeSettings(Config);
862866
currentIndex = Config.CookieIndex > 0 ? Config.CookieIndex - 1 : Config.Cookiecounter >= 0 ? Math.floor(Math.random() * Config.CookieArray.length) : 0;

lib/clewd-utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'use strict';
66

77
const {randomInt, randomBytes} = require('node:crypto'), {version: Version} = require('../package.json'), Encoder = (new TextDecoder,
8-
new TextEncoder), Main = 'clewd修改版 v' + Version + '(12) by tera', Replacements = {
8+
new TextEncoder), Main = 'clewd修改版 v' + Version + '(13) by tera', Replacements = {
99
user: 'Human',
1010
assistant: 'Assistant',
1111
system: '',

0 commit comments

Comments
 (0)