Skip to content

Commit df19c5c

Browse files
committed
feat: 支持通过对话退出
1 parent 17c1c0f commit df19c5c

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aigc-detector",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Detect if content is generated by AI",
55
"keywords": [
66
"aigc",

src/cli/commands/chat.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AIMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
2+
import ansiEscapes from 'ansi-escapes';
23
import chalk from 'chalk';
34
import { ChatMessageHistory } from 'langchain/stores/message/in_memory';
45
import readline from 'node:readline';
@@ -59,27 +60,32 @@ class ChatCommand extends BaseCommand {
5960

6061
console.warn = () => {};
6162
process.stdout.write(
62-
this.getDisplayContent(PromptRole.SYSTEM) + `Type ${chalk.cyan('exit')} to end this conversation\n`
63+
this.getDisplayContent(PromptRole.SYSTEM) +
64+
`If you want to end this conversation, please tell the AI directly\n`
6365
);
6466
process.stdout.write(aiDisplay + lastMessage + '\n');
6567

6668
// eslint-disable-next-line no-constant-condition
6769
while (true) {
6870
const userMessage = await this.getUserMessage();
69-
70-
if (userMessage === 'exit') {
71-
close();
72-
73-
break;
74-
}
75-
7671
const stream = detector.chat(userMessage, await this.messages.getMessages());
7772

7873
process.stdout.write(aiDisplay);
7974
stream.pipe(process.stdout);
8075

8176
lastMessage = await stream.getData();
8277

78+
if (lastMessage === '$command:exit$') {
79+
process.stdout.write(ansiEscapes.eraseLine + ansiEscapes.cursorLeft);
80+
process.stdout.write(aiDisplay + 'Nice to chat with you. Bye~\n');
81+
82+
await this.wait(1000);
83+
84+
close();
85+
86+
break;
87+
}
88+
8389
process.stdout.write('\n');
8490

8591
await this.addMessage(PromptRole.USER, userMessage);

src/cli/extends/command.ts

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ abstract class BaseCommand extends Command {
6363

6464
this.log(bang + ' ' + text);
6565
}
66+
67+
protected wait(timeout: number): Promise<void> {
68+
return new Promise((resolve) => {
69+
setTimeout(resolve, timeout);
70+
});
71+
}
6672
}
6773

6874
export default BaseCommand;

src/core/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class AIGC {
3535
const platform = getPlatform(this.platform);
3636
const prompt = ChatPromptTemplate.fromMessages([
3737
SystemMessagePromptTemplate.fromTemplate(
38-
'You are a helpful assistant. Answer all questions to the best of your ability.'
38+
'You are a helpful assistant. Answer all questions to the best of your ability.\nIf the user wants to end the conversation or exit the current chat command, directly reply to the user "$command:exit$"'
3939
),
4040
new MessagesPlaceholder('messages'),
4141
HumanMessagePromptTemplate.fromTemplate('{question}')

0 commit comments

Comments
 (0)