fengmuxi commited on
Commit
eeb257a
·
1 Parent(s): d8bd515

增加猴狐gpt逆向接口

Browse files
app/api/newbing-image/route.ts ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { BingChat} from "../../bing-chat/build/index";
2
+
3
+ export async function POST(req: Request) {
4
+ try {
5
+ let cookies = process.env.COOKIES;
6
+ const api = new BingChat({
7
+ cookie: cookies,
8
+ });
9
+ const res = await api.createImage(await req.json());
10
+ console.log(res)
11
+ return new Response();
12
+ } catch (e) {
13
+ console.error("[NewBing] ", e);
14
+ return new Response(JSON.stringify(e));
15
+ }
16
+ }
app/api/revchat/route.ts ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { createParser } from "eventsource-parser";
2
+ import { NextRequest } from "next/server";
3
+
4
+ async function createStream(req: NextRequest) {
5
+ const encoder = new TextEncoder();
6
+ const decoder = new TextDecoder();
7
+
8
+ const res = await fetch("http://lemurchat.anfans.cn/api/chat/conversation-trial", {
9
+ headers: {
10
+ "Content-Type": "application/json"
11
+ },
12
+ method: "POST",
13
+ body: req.body,
14
+ });
15
+
16
+ const stream = new ReadableStream({
17
+ async start(controller) {
18
+ function onParse(event: any) {
19
+ if (event.type === "event") {
20
+ const data = event.data;
21
+ if(event.id=="1"){
22
+ let text1=data.slice(data.indexOf("content"))
23
+ const text = text1.slice(12,text1.indexOf("index")-6)
24
+ const queue = encoder.encode(text);
25
+ controller.enqueue(queue);
26
+ return;
27
+ }
28
+ // https://beta.openai.com/docs/api-reference/completions/create#completions/create-stream
29
+ try {
30
+ const json = JSON.parse(data);
31
+ // console.log(data.indexOf("content"))
32
+ if (data.indexOf("content")==-1) {
33
+ controller.close();
34
+ return;
35
+ }
36
+ // console.log(event.data)
37
+ const text = JSON.parse(json.data.slice(5)).choices[0].delta.content;
38
+ const queue = encoder.encode(text);
39
+ controller.enqueue(queue);
40
+ } catch (e) {
41
+ controller.error(e);
42
+ }
43
+ }
44
+ }
45
+
46
+ const parser = createParser(onParse);
47
+ for await (const chunk of res.body as any) {
48
+ parser.feed(decoder.decode(chunk));
49
+ }
50
+ },
51
+ });
52
+ return stream;
53
+ }
54
+
55
+ export async function POST(req: NextRequest) {
56
+ try {
57
+ const stream = await createStream(req);
58
+ return new Response(stream);
59
+ } catch (error) {
60
+ console.error("[Chat Stream]", error);
61
+ }
62
+ }
63
+
64
+ export const config = {
65
+ runtime: "edge",
66
+ };
app/bing-chat/build/index.d.ts CHANGED
@@ -240,6 +240,7 @@ declare class BingChat {
240
  */
241
  sendMessage(text: string, opts?: SendMessageOptions): Promise<ChatMessage>;
242
  createConversation(): Promise<ConversationResult>;
 
243
  }
244
 
245
  export {
 
240
  */
241
  sendMessage(text: string, opts?: SendMessageOptions): Promise<ChatMessage>;
242
  createConversation(): Promise<ConversationResult>;
243
+ createImage(message: string): Array;
244
  }
245
 
246
  export {
app/bing-chat/build/index.js CHANGED
@@ -1,4 +1,5 @@
1
  // src/bing-chat.ts
 
2
  import crypto from "node:crypto";
3
  import WebSocket from "ws";
4
 
@@ -260,6 +261,45 @@ var BingChat = class {
260
  }
261
  });
262
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  };
264
  export {
265
  BingChat
 
1
  // src/bing-chat.ts
2
+ import { time } from "node:console";
3
  import crypto from "node:crypto";
4
  import WebSocket from "ws";
5
 
 
261
  }
262
  });
263
  }
264
+ async createImage(message) {
265
+ // const requestId = crypto.randomUUID();
266
+ console.log(1)
267
+ const cookie = this._cookie.includes(";") ? this._cookie : `_U=${this._cookie}`;
268
+ let res = await fetch(`https://www.bing.com/images/create?q=${message}&rt=3&FORM=GENCRE`, {
269
+ headers: {
270
+ cookie
271
+ },
272
+ method: "GET",
273
+ mode: "cors"
274
+ })
275
+ console.log(res.headers)
276
+ if (res.status!= 302) {
277
+ throw new Error(
278
+ `unexpected HTTP error createConversation ${res.status}: ${res.statusText}`
279
+ );
280
+ }
281
+ console.log(2)
282
+ redirect_url=res.headers["Location"].replace("&nfy=1", "")
283
+ request_id = redirect_url.split("id=")[-1]
284
+ await fetch("https://www.bing.com"+redirect_url,{method:"GET"})
285
+ polling_url = `https://www.bing.com/images/create/async/results/${request_id}?q=${message}`
286
+ while (true){
287
+ res = await fetch(polling_url,{method:"GET"})
288
+ if(res.json()!=''||res.json().indexOf("errorMessage") != -1){
289
+ await setTimeout(()=>{console.log(123)},1000)
290
+ continue
291
+ }else{
292
+ break
293
+ }
294
+ }
295
+ console.log(3)
296
+ image_links = res.json().match('src="([^"]+)"')
297
+ normal_image_links = []
298
+ for(let i=0 ;i<image_links.length;i++){
299
+ normal_image_links.push(image_links[i].split("?w=")[0])
300
+ }
301
+ return normal_image_links
302
+ }
303
  };
304
  export {
305
  BingChat
app/requests.ts CHANGED
@@ -38,6 +38,21 @@ const makeRequestParam = (
38
  };
39
  };
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  const makeImageRequestParam = (messages: Message[]): ChatImageRequest => {
42
  return {
43
  prompt: messages[messages.length - 1].content,
@@ -214,7 +229,7 @@ export async function requestChatStream(
214
  console.error("NetWork Error", err);
215
  options?.onMessage("请换一个问题试试吧", true);
216
  }
217
- } else {
218
  console.log("[Request] ", messages[messages.length - 1].content);
219
  const controller = new AbortController();
220
  const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
@@ -235,6 +250,95 @@ export async function requestChatStream(
235
  console.error("NetWork Error", err);
236
  options?.onMessage("请换一个问题试试吧", true);
237
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  }
239
  }
240
 
 
38
  };
39
  };
40
 
41
+ const makeRevChatRequestParam = (
42
+ messages: Message[]
43
+ ) => {
44
+ let sendMessages = messages.map((v) => ({
45
+ role: v.role,
46
+ content: v.content,
47
+ isSensitive: false,
48
+ needCheck: true
49
+ }));
50
+
51
+ return {
52
+ messages: JSON.stringify(sendMessages)
53
+ }
54
+ };
55
+
56
  const makeImageRequestParam = (messages: Message[]): ChatImageRequest => {
57
  return {
58
  prompt: messages[messages.length - 1].content,
 
229
  console.error("NetWork Error", err);
230
  options?.onMessage("请换一个问题试试吧", true);
231
  }
232
+ } else if(model=="万卷"){
233
  console.log("[Request] ", messages[messages.length - 1].content);
234
  const controller = new AbortController();
235
  const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
 
250
  console.error("NetWork Error", err);
251
  options?.onMessage("请换一个问题试试吧", true);
252
  }
253
+ }else if(model=="必应绘画"){
254
+ console.log("[Request] ", messages[messages.length - 1].content);
255
+ const req = makeImageRequestParam(messages);
256
+ const controller = new AbortController();
257
+ const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
258
+ try {
259
+ const res = await fetch("/api/newbing-image", {
260
+ method: "POST",
261
+ headers: {
262
+ "Content-Type": "application/json",
263
+ ...getHeaders(),
264
+ },
265
+ body: JSON.stringify(req),
266
+ });
267
+
268
+ clearTimeout(reqTimeoutId);
269
+ const reg = /^['|"](.*)['|"]$/;
270
+ const response = (await res.json()) as ChatImagesResponse;
271
+ options?.onMessage(
272
+ JSON.stringify(response.data[0].url).replace(reg, "$1"),
273
+ true,
274
+ );
275
+ controller.abort();
276
+ } catch (err) {
277
+ console.error("NetWork Error", err);
278
+ options?.onMessage("请换一个问题试试吧", true);
279
+ }
280
+ }else{
281
+ const req = makeRevChatRequestParam(messages);
282
+
283
+ console.log("[Request] ", req);
284
+
285
+ const controller = new AbortController();
286
+ const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
287
+
288
+ try {
289
+ const res = await fetch("/api/revchat", {
290
+ method: "POST",
291
+ headers: {
292
+ "Content-Type": "application/json",
293
+ ...getHeaders(),
294
+ },
295
+ body: JSON.stringify(req),
296
+ signal: controller.signal,
297
+ });
298
+ clearTimeout(reqTimeoutId);
299
+
300
+ let responseText = "";
301
+
302
+ const finish = () => {
303
+ options?.onMessage(responseText, true);
304
+ controller.abort();
305
+ };
306
+
307
+ if (res.ok) {
308
+ const reader = res.body?.getReader();
309
+ const decoder = new TextDecoder();
310
+
311
+ options?.onController?.(controller);
312
+
313
+ while (true) {
314
+ // handle time out, will stop if no response in 10 secs
315
+ const resTimeoutId = setTimeout(() => finish(), TIME_OUT_MS);
316
+ const content = await reader?.read();
317
+ clearTimeout(resTimeoutId);
318
+ const text = decoder.decode(content?.value);
319
+ responseText += text;
320
+
321
+ const done = !content || content.done;
322
+ options?.onMessage(responseText, false);
323
+
324
+ if (done) {
325
+ break;
326
+ }
327
+ }
328
+
329
+ finish();
330
+ } else if (res.status === 401) {
331
+ console.error("Anauthorized");
332
+ responseText = Locale.Error.Unauthorized;
333
+ finish();
334
+ } else {
335
+ console.error("Stream Error");
336
+ options?.onError(new Error("Stream Error"));
337
+ }
338
+ } catch (err) {
339
+ console.error("NetWork Error", err);
340
+ options?.onError(err as Error);
341
+ }
342
  }
343
  }
344
 
app/store/app.ts CHANGED
@@ -92,10 +92,18 @@ export const ALL_MODEL = [
92
  name: "AI绘画",
93
  available: true,
94
  },
 
 
 
 
95
  {
96
  name: "必应",
97
  available: true,
98
  },
 
 
 
 
99
  {
100
  name: "万卷",
101
  available: true,
@@ -139,7 +147,7 @@ export function filterConfig(config: ModelConfig): Partial<ModelConfig> {
139
  }
140
 
141
  const DEFAULT_CONFIG: ChatConfig = {
142
- model: "聊天",
143
  historyMessageCount: 8,
144
  compressMessageLengthThreshold: 1000,
145
  sendBotMessages: true as boolean,
 
92
  name: "AI绘画",
93
  available: true,
94
  },
95
+ {
96
+ name: "逆向Gpt",
97
+ available: true,
98
+ },
99
  {
100
  name: "必应",
101
  available: true,
102
  },
103
+ // {
104
+ // name: "必应绘画",
105
+ // available: true,
106
+ // },
107
  {
108
  name: "万卷",
109
  available: true,
 
147
  }
148
 
149
  const DEFAULT_CONFIG: ChatConfig = {
150
+ model: "逆向Gpt",
151
  historyMessageCount: 8,
152
  compressMessageLengthThreshold: 1000,
153
  sendBotMessages: true as boolean,
middleware.ts CHANGED
@@ -3,7 +3,7 @@ import { ACCESS_CODES } from "./app/api/access";
3
  import md5 from "spark-md5";
4
 
5
  export const config = {
6
- matcher: ["/api/chat", "/api/chat-stream","/api/chat-image","/api/newbing","/api/wanjuan"],
7
  };
8
 
9
  export function middleware(req: NextRequest, res: NextResponse) {
 
3
  import md5 from "spark-md5";
4
 
5
  export const config = {
6
+ matcher: ["/api/chat", "/api/chat-stream","/api/chat-image","/api/revchat","/api/newbing","/api/newbing-image","/api/wanjuan"],
7
  };
8
 
9
  export function middleware(req: NextRequest, res: NextResponse) {