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

Commit

Permalink
fix: marked format error
Browse files Browse the repository at this point in the history
close #5
  • Loading branch information
KeJunMao committed Mar 3, 2023
1 parent 33def5c commit ba036f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"dependencies": {
"c12": "^1.1.2",
"chalk": "^5.2.0",
"consola": "^2.15.3",
"lodash": "^4.17.21",
"marked": "^4.2.12",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 33 additions & 12 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadEdgeGPTConfig } from "../config";
import { EdgeGPTConfig } from "../types";

import { EdgeGPTConfig, EdgeGPTResponseThrottling } from "../types";
import chalk from "chalk";
import prompts, { Choice } from "prompts";
import { ChatBot } from "../ChatBot";
import ora from "ora";
Expand All @@ -9,6 +9,15 @@ import { marked } from "marked";
// @ts-expect-error
import TerminalRenderer from "marked-terminal";

function createOrUpdateSpinnerPrefix(throttling?: EdgeGPTResponseThrottling) {
if (throttling) {
return chalk.bold(
`Bing(${throttling.numUserMessagesInConversation}/${throttling.maxNumUserMessagesInConversation}): `
);
}
return chalk.bold("Bing: ");
}

export const run = async (options: Partial<EdgeGPTConfig>) => {
const config = await loadEdgeGPTConfig({
cookies: options.cookies,
Expand All @@ -22,6 +31,7 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
renderer: new TerminalRenderer(),
});

let spinnerPrefix = createOrUpdateSpinnerPrefix();
while (true) {
const cmd = await prompts([
{
Expand Down Expand Up @@ -52,36 +62,47 @@ export const run = async (options: Partial<EdgeGPTConfig>) => {
} else if (cmd.prompt.startsWith("!options")) {
const [_c, optstr] = cmd.prompt.split(" ");
config.requestOptions = optstr.split(",").map((v: string) => v.trim());
console.log(`Update conversation request options to: ${config.requestOptions}`);
console.log(
`Update conversation request options to: ${config.requestOptions}`
);
continue;
}
if (cmd.prompt) {
if (!chatBot.chatHub) {
await chatBot.reset();
}
let response: any;
const spinnerPrefix = "Bing is typing...";

const spinner = ora(spinnerPrefix);
spinner.start();
if (config.stream) {
response = await chatBot.ask(cmd.prompt, (msg) => {
spinner.text = `${spinnerPrefix}\n${marked(msg)}`;
spinner.text = `${spinnerPrefix}${marked(msg ?? "")}`;
});
spinner.stop();
spinnerPrefix = createOrUpdateSpinnerPrefix(
response["item"]["throttling"]
);
console.log(
marked(
response["item"]?.["messages"]?.[1]?.["adaptiveCards"]?.[0]?.[
"body"
]?.[0]?.["text"]?.trim()
)
chalk.green("! ") +
spinnerPrefix +
marked(
response["item"]?.["messages"]?.[1]?.["adaptiveCards"]?.[0]?.[
"body"
]?.[0]?.["text"]?.trim() ?? ""
)
);
} else {
const msg = await chatBot.askAsync(cmd.prompt, (res) => {
spinner.stop();
response = res;
spinnerPrefix = createOrUpdateSpinnerPrefix(
response["item"]["throttling"]
);
});

console.log(marked(msg?.trim()));
console.log(
chalk.green("? ") + spinnerPrefix + marked(msg?.trim() ?? "")
);
}
try {
choices = response["item"]["messages"][1]["suggestedResponses"]
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ export interface EdgeGPTResponse {
arguments: Record<string, any>[];
[x: string]: any;
}

export interface EdgeGPTResponseThrottling {
maxNumUserMessagesInConversation: number;
numUserMessagesInConversation: number;
}

0 comments on commit ba036f5

Please sign in to comment.