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

Commit ba036f5

Browse files
committed
fix: marked format error
close #5
1 parent 33def5c commit ba036f5

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"dependencies": {
6363
"c12": "^1.1.2",
64+
"chalk": "^5.2.0",
6465
"consola": "^2.15.3",
6566
"lodash": "^4.17.21",
6667
"marked": "^4.2.12",

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/run.ts

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { loadEdgeGPTConfig } from "../config";
2-
import { EdgeGPTConfig } from "../types";
3-
2+
import { EdgeGPTConfig, EdgeGPTResponseThrottling } from "../types";
3+
import chalk from "chalk";
44
import prompts, { Choice } from "prompts";
55
import { ChatBot } from "../ChatBot";
66
import ora from "ora";
@@ -9,6 +9,15 @@ import { marked } from "marked";
99
// @ts-expect-error
1010
import TerminalRenderer from "marked-terminal";
1111

12+
function createOrUpdateSpinnerPrefix(throttling?: EdgeGPTResponseThrottling) {
13+
if (throttling) {
14+
return chalk.bold(
15+
`Bing(${throttling.numUserMessagesInConversation}/${throttling.maxNumUserMessagesInConversation}): `
16+
);
17+
}
18+
return chalk.bold("Bing: ");
19+
}
20+
1221
export const run = async (options: Partial<EdgeGPTConfig>) => {
1322
const config = await loadEdgeGPTConfig({
1423
cookies: options.cookies,
@@ -22,6 +31,7 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
2231
renderer: new TerminalRenderer(),
2332
});
2433

34+
let spinnerPrefix = createOrUpdateSpinnerPrefix();
2535
while (true) {
2636
const cmd = await prompts([
2737
{
@@ -52,36 +62,47 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
5262
} else if (cmd.prompt.startsWith("!options")) {
5363
const [_c, optstr] = cmd.prompt.split(" ");
5464
config.requestOptions = optstr.split(",").map((v: string) => v.trim());
55-
console.log(`Update conversation request options to: ${config.requestOptions}`);
65+
console.log(
66+
`Update conversation request options to: ${config.requestOptions}`
67+
);
5668
continue;
5769
}
5870
if (cmd.prompt) {
5971
if (!chatBot.chatHub) {
6072
await chatBot.reset();
6173
}
6274
let response: any;
63-
const spinnerPrefix = "Bing is typing...";
75+
6476
const spinner = ora(spinnerPrefix);
6577
spinner.start();
6678
if (config.stream) {
6779
response = await chatBot.ask(cmd.prompt, (msg) => {
68-
spinner.text = `${spinnerPrefix}\n${marked(msg)}`;
80+
spinner.text = `${spinnerPrefix}${marked(msg ?? "")}`;
6981
});
7082
spinner.stop();
83+
spinnerPrefix = createOrUpdateSpinnerPrefix(
84+
response["item"]["throttling"]
85+
);
7186
console.log(
72-
marked(
73-
response["item"]?.["messages"]?.[1]?.["adaptiveCards"]?.[0]?.[
74-
"body"
75-
]?.[0]?.["text"]?.trim()
76-
)
87+
chalk.green("! ") +
88+
spinnerPrefix +
89+
marked(
90+
response["item"]?.["messages"]?.[1]?.["adaptiveCards"]?.[0]?.[
91+
"body"
92+
]?.[0]?.["text"]?.trim() ?? ""
93+
)
7794
);
7895
} else {
7996
const msg = await chatBot.askAsync(cmd.prompt, (res) => {
8097
spinner.stop();
8198
response = res;
99+
spinnerPrefix = createOrUpdateSpinnerPrefix(
100+
response["item"]["throttling"]
101+
);
82102
});
83-
84-
console.log(marked(msg?.trim()));
103+
console.log(
104+
chalk.green("? ") + spinnerPrefix + marked(msg?.trim() ?? "")
105+
);
85106
}
86107
try {
87108
choices = response["item"]["messages"][1]["suggestedResponses"]

src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ export interface EdgeGPTResponse {
5151
arguments: Record<string, any>[];
5252
[x: string]: any;
5353
}
54+
55+
export interface EdgeGPTResponseThrottling {
56+
maxNumUserMessagesInConversation: number;
57+
numUserMessagesInConversation: number;
58+
}

0 commit comments

Comments
 (0)