Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit 709257c

Browse files
committed
feat: can set request optiosn
1 parent d731419 commit 709257c

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

src/ChatBot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ export class ChatBot {
1616

1717
let conversation = await createConversation(cookie);
1818
this.chatHub = new ChatHub(conversation);
19-
return this
19+
return this;
2020
}
2121

2222
async askAsync(
2323
prompt: string,
2424
handler: (response: Record<string, any>) => void = () => {}
2525
) {
2626
this.chatHub?.once("final", handler);
27-
return await this.chatHub?.askAsync(prompt);
27+
return await this.chatHub?.askAsync(prompt, this.config.requestOptions);
2828
}
29-
29+
3030
async ask(prompt: string, handler: (msg: string) => void = () => {}) {
3131
return new Promise(async (resolve) => {
3232
this.chatHub?.on("message", handler);
3333
this.chatHub?.once("final", (res) => {
3434
this.chatHub?.off("message", handler);
3535
resolve(res);
3636
});
37-
await this.chatHub?.ask(prompt);
37+
await this.chatHub?.ask(prompt, this.config.requestOptions);
3838
});
3939
}
4040

src/ChatHub.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import EventEmitter from "events";
22
import { WebSocket } from "ws";
33
import { DELIMITER } from "./constant";
44
import { createRequest } from "./request";
5-
import { Conversation, EdgeGPTResponse } from "./types";
5+
import { Conversation, EdgeGPTResponse, RequestOptions } from "./types";
66
import { appendIdentifier, createHeaders } from "./utils";
77
import TypedEmitter from "typed-emitter";
88

@@ -17,28 +17,28 @@ type ChatHubEvents = {
1717

1818
export class ChatHub extends (EventEmitter as new () => TypedEmitter<ChatHubEvents>) {
1919
protected ws!: WebSocket;
20-
protected request: (prompt: string) => any;
20+
protected request: (prompt: string, options?: RequestOptions) => any;
2121

2222
constructor(protected conversation: Conversation) {
2323
super();
2424
this.request = createRequest(conversation);
2525
}
2626

27-
async ask(prompt: string) {
27+
async ask(prompt: string, options?: RequestOptions) {
2828
if (!this.ws || this.ws.readyState === WebSocket.CLOSED) {
2929
await this.createWs();
3030
}
31-
this.send(this.request(prompt));
31+
this.send(this.request(prompt, options));
3232
}
3333

34-
askAsync(prompt: string) {
34+
askAsync(prompt: string, options?: RequestOptions) {
3535
return new Promise<string>((resolve) => {
3636
this.once("final", (response) => {
3737
resolve(
3838
response["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
3939
);
4040
});
41-
this.ask(prompt);
41+
this.ask(prompt, options);
4242
});
4343
}
4444

src/cli.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import yargs from "yargs";
33
import { hideBin } from "yargs/helpers";
44
import { version } from "../package.json";
55
import { run } from "./commands/run";
6+
import { defaultRequestOptions } from "./request";
67
import { logger } from "./utils";
78

89
(async () => {
@@ -21,6 +22,11 @@ import { logger } from "./utils";
2122
.boolean("stream")
2223
.describe("stream", "Used stream mode")
2324
.default("stream", undefined, "true")
25+
.option("options", {
26+
type: "array",
27+
})
28+
.default("options", undefined, defaultRequestOptions.join(","))
29+
.describe("options", "Conversation request options")
2430
.alias("h", "help")
2531
.version("version", version)
2632
.alias("v", "version")
@@ -32,5 +38,6 @@ import { logger } from "./utils";
3238
run({
3339
cookies: args["cookieFile"],
3440
stream: args["stream"],
41+
requestOptions: args["options"],
3542
}).catch(logger.error);
3643
})();

src/commands/run.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
1313
const config = await loadEdgeGPTConfig({
1414
cookies: options.cookies,
1515
stream: options.stream,
16+
requestOptions: options.requestOptions,
1617
});
1718

1819
const chatBot = new ChatBot(config);
19-
await chatBot.create();
2020
let choices: Choice[] = [];
2121
marked.setOptions({
2222
renderer: new TerminalRenderer(),
@@ -49,8 +49,16 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
4949
} else if (cmd.prompt === "!reset") {
5050
await chatBot.reset();
5151
continue;
52+
} else if (cmd.prompt.startsWith("!options")) {
53+
const [_c, optstr] = cmd.prompt.split(" ");
54+
config.requestOptions = optstr.split(",").map((v: string) => v.trim());
55+
console.log(`Update conversation request options to: ${config.requestOptions}`);
56+
continue;
5257
}
5358
if (cmd.prompt) {
59+
if (!chatBot.chatHub) {
60+
await chatBot.reset();
61+
}
5462
let response: any;
5563
const spinnerPrefix = "Bing is typing...";
5664
const spinner = ora(spinnerPrefix);

src/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { loadConfig } from "c12";
22
import { readFileSync } from "fs";
33
import { resolve } from "path";
4+
import { defaultRequestOptions } from "./request";
45
import { EdgeGPTConfig, ResolvedEdgeGPTConfig } from "./types";
56
import { logger } from "./utils";
67

78
const configDefaults: EdgeGPTConfig = {
89
cookies: "cookie.json",
910
stream: true,
11+
requestOptions: defaultRequestOptions,
1012
};
1113

1214
export const loadEdgeGPTConfig = async (
1315
overrides?: Partial<EdgeGPTConfig>,
1416
cwd = process.cwd()
1517
) => {
16-
const { config, layers, configFile } = await loadConfig<EdgeGPTConfig>({
18+
const { config } = await loadConfig<EdgeGPTConfig>({
1719
name: "edgegpt",
1820
defaults: configDefaults,
1921
globalRc: true,
@@ -36,5 +38,6 @@ export const loadEdgeGPTConfig = async (
3638
});
3739
return JSON.parse(f);
3840
});
41+
config.requestOptions = config.requestOptions.filter((v) => v);
3942
return config as unknown as ResolvedEdgeGPTConfig;
4043
};

src/request.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { Conversation } from "./types";
1+
import { Conversation, RequestOptions } from "./types";
2+
3+
export const defaultRequestOptions: RequestOptions = [
4+
"deepleo",
5+
"enable_debug_commands",
6+
"disable_emoji_spoken_text",
7+
"enablemm",
8+
];
29

310
export const createRequest = (con: Conversation) => {
411
let invocationId = 0;
5-
return (
6-
prompt: string,
7-
options: string[] = [
8-
"deepleo",
9-
"enable_debug_commands",
10-
"disable_emoji_spoken_text",
11-
"enablemm",
12-
]
13-
) => {
12+
return (prompt: string, options: RequestOptions = defaultRequestOptions) => {
1413
const request = {
1514
arguments: [
1615
{

src/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,33 @@ export interface CookieItem {
66

77
export type Cookie = CookieItem[];
88

9+
export interface IRequestOptions {
10+
nlu_direct_response_filter: boolean;
11+
deepleo: boolean;
12+
disable_emoji_spoken_text: boolean;
13+
enable_debug_commands: boolean;
14+
responsible_ai_policy_235: boolean;
15+
enablemm: boolean;
16+
// style start
17+
h3imaginative: boolean;
18+
h3precise: boolean;
19+
harmonyv3: boolean;
20+
// style end
21+
dv3sugg: boolean;
22+
}
23+
24+
export type RequestOptions = (keyof IRequestOptions)[];
25+
926
export interface EdgeGPTConfig {
1027
cookies: Path | Path[];
1128
stream: boolean;
29+
requestOptions: RequestOptions;
1230
}
1331

1432
export interface ResolvedEdgeGPTConfig {
1533
cookies: Cookie[];
1634
stream: boolean;
35+
requestOptions: RequestOptions;
1736
}
1837

1938
export interface Conversation {

0 commit comments

Comments
 (0)