From 311197873b11bae0583fda3a9856e534cf834761 Mon Sep 17 00:00:00 2001 From: xiang <1984871009@qq.com> Date: Thu, 4 May 2023 22:01:55 +0800 Subject: [PATCH] feat: add proxy config --- index.ts | 7 +++---- model/index.ts | 11 +++++++++++ model/you/index.ts | 10 ++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 0f7f0f2..f437515 100644 --- a/index.ts +++ b/index.ts @@ -2,12 +2,11 @@ import {You} from "./model/you"; import Koa from 'koa'; import Router from 'koa-router' import bodyParser from 'koa-bodyparser'; -import {Readable} from "stream"; const app = new Koa(); const router = new Router(); app.use(bodyParser()); -const you = new You(); +const you = new You({proxy: process.env.https_proxy || process.env.http_proxy}); interface AskReq { prompt: string; @@ -23,7 +22,7 @@ router.get('/ask', async (ctx) => { ctx.body = res.text; }); -router.get('/ask/stream',async(ctx)=>{ +router.get('/ask/stream', async (ctx) => { const {prompt} = ctx.query; if (!prompt) { ctx.body = 'please input prompt'; @@ -34,7 +33,7 @@ router.get('/ask/stream',async(ctx)=>{ "Cache-Control": "no-cache", "Connection": "keep-alive", }) - const res = await you.askStream({prompt: prompt as string}); + const res = await you.askStream({prompt: prompt as string}); ctx.body = res.text; }) diff --git a/model/index.ts b/model/index.ts index 5de90c2..831b9c0 100644 --- a/model/index.ts +++ b/model/index.ts @@ -1,5 +1,9 @@ import {Stream} from "stream"; +export interface ChatOptions { + proxy?: string; +} + export interface Response { text: string | null; other: any; @@ -23,6 +27,13 @@ export interface HistoryItem { export abstract class Chat { + protected proxy: string | undefined; + + constructor(options?: ChatOptions) { + this.proxy = options?.proxy; + } + public abstract ask(req: Request): Promise + public abstract askStream(req: Request): Promise } diff --git a/model/you/index.ts b/model/you/index.ts index 52abba2..c448c83 100644 --- a/model/you/index.ts +++ b/model/you/index.ts @@ -5,7 +5,7 @@ import tlsClient from 'tls-client'; import {Session} from "tls-client/dist/esm/sessions"; import {Params} from "tls-client/dist/esm/types"; import {toEventCB, toEventStream} from "../../utils"; -import {Chat, Request, Response, ResponseStream} from "../index"; +import {Chat, ChatOptions, Request, Response, ResponseStream} from "../index"; const userAgent = new UserAgent(); @@ -65,11 +65,13 @@ interface SearchResult { export class You extends Chat { private session: Session; - constructor() { - super(); + constructor(props: ChatOptions) { + super(props); this.session = new tlsClient.Session({clientIdentifier: 'chrome_108'}); this.session.headers = this.getHeaders(); - this.session.proxy = "http://192.168.0.155:10811"; + if (this.proxy) { + this.session.proxy = this.proxy; + } } private async request(req: Request) {