From ba036f5ac1935a16fecad1f30e979881204a74e3 Mon Sep 17 00:00:00 2001 From: KeJunMao Date: Fri, 3 Mar 2023 16:54:39 +0800 Subject: [PATCH] fix: marked format error close #5 --- package.json | 1 + pnpm-lock.yaml | 2 ++ src/commands/run.ts | 45 +++++++++++++++++++++++++++++++++------------ src/types.ts | 5 +++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 1d22515..130a6d4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ea1bb46..a6e01ef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,6 +12,7 @@ importers: '@types/yargs': ^17.0.22 bumpp: ^8.2.1 c12: ^1.1.2 + chalk: ^5.2.0 consola: ^2.15.3 esno: ^0.16.3 lodash: ^4.17.21 @@ -29,6 +30,7 @@ importers: yargs: ^17.7.0 dependencies: c12: 1.1.2 + chalk: 5.2.0 consola: 2.15.3 lodash: 4.17.21 marked: 4.2.12 diff --git a/src/commands/run.ts b/src/commands/run.ts index f1d579e..681ca86 100644 --- a/src/commands/run.ts +++ b/src/commands/run.ts @@ -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"; @@ -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) => { const config = await loadEdgeGPTConfig({ cookies: options.cookies, @@ -22,6 +31,7 @@ export const run = async (options: Partial) => { renderer: new TerminalRenderer(), }); + let spinnerPrefix = createOrUpdateSpinnerPrefix(); while (true) { const cmd = await prompts([ { @@ -52,7 +62,9 @@ export const run = async (options: Partial) => { } 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) { @@ -60,28 +72,37 @@ export const run = async (options: Partial) => { 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"] diff --git a/src/types.ts b/src/types.ts index c337220..3e3b385 100644 --- a/src/types.ts +++ b/src/types.ts @@ -51,3 +51,8 @@ export interface EdgeGPTResponse { arguments: Record[]; [x: string]: any; } + +export interface EdgeGPTResponseThrottling { + maxNumUserMessagesInConversation: number; + numUserMessagesInConversation: number; +}