From 91878535c617744383484104c46c1fe4e0340f1a Mon Sep 17 00:00:00 2001 From: scarqin <1054139596@qq.com> Date: Wed, 8 Mar 2023 14:20:30 +0800 Subject: [PATCH] feat(gpt-robot): add limit --- src/workbench/browser/locale/messages.xlf | 15 +++-- src/workbench/browser/locale/messages.zh.xlf | 16 ++++-- src/workbench/browser/src/app/app.module.ts | 4 ++ .../feature-control.service.ts | 4 +- .../chat-robot-message.component.ts | 2 +- .../chatgpt-robot/chatgpt-robot.component.ts | 56 ++++++++++++++++--- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/workbench/browser/locale/messages.xlf b/src/workbench/browser/locale/messages.xlf index e57c19f91..64e132619 100644 --- a/src/workbench/browser/locale/messages.xlf +++ b/src/workbench/browser/locale/messages.xlf @@ -390,7 +390,7 @@ src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 67 + 77 @@ -1385,21 +1385,28 @@ ChatGPT Extensions src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 45 + 50 + + + + You have reached the maximum usage of ChatGPT for today. Please Sign in to your Github account to star/fork to get more times. + + src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts + 62,65 Send message to AI src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 59 + 69 Visitor src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 154 + 182 diff --git a/src/workbench/browser/locale/messages.zh.xlf b/src/workbench/browser/locale/messages.zh.xlf index 185080598..c01edbfeb 100644 --- a/src/workbench/browser/locale/messages.zh.xlf +++ b/src/workbench/browser/locale/messages.zh.xlf @@ -438,7 +438,7 @@ src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 67 + 77 ChatGPT 机器人 @@ -1542,15 +1542,23 @@ ChatGPT Extensions src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 45 + 50 ChatGPT 插件 + + You have reached the maximum usage of ChatGPT for today. Please Sign in to your Github account to star/fork to get more times. + + src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts + 62,65 + + 您今天 ChatGPT 次数已使用完。请 登录 您的 Github 账号 Star/Fork 项目获取更多次数。 + Send message to AI src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 59 + 69 发送消息给 AI @@ -1558,7 +1566,7 @@ Visitor src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts - 154 + 182 游客 diff --git a/src/workbench/browser/src/app/app.module.ts b/src/workbench/browser/src/app/app.module.ts index b481c568e..e3d8dbfd4 100644 --- a/src/workbench/browser/src/app/app.module.ts +++ b/src/workbench/browser/src/app/app.module.ts @@ -7,6 +7,7 @@ import { ReactiveFormsModule } from '@angular/forms'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { WarningFill } from '@ant-design/icons-angular/icons'; import { EoNgFeedbackTooltipModule, EoNgFeedbackMessageModule } from 'eo-ng-feedback'; +import { FeatureControlService } from 'eo/workbench/browser/src/app/core/services/feature-control/feature-control.service'; import { LanguageService } from 'eo/workbench/browser/src/app/core/services/language/language.service'; import { NotificationService } from 'eo/workbench/browser/src/app/core/services/notification.service'; import { ExtensionService } from 'eo/workbench/browser/src/app/shared/services/extensions/extension.service'; @@ -83,10 +84,13 @@ export class AppModule { private mockService: MockService, private global: GlobalProvider, private theme: ThemeService, + private feature: FeatureControlService, private extensionService: ExtensionService, private notification: NotificationService ) { this.init(); + //! Feature show init before extensionService init + this.feature.init(); } async init() { //* Init language diff --git a/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts b/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts index db4519596..41acd7e88 100644 --- a/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts +++ b/src/workbench/browser/src/app/core/services/feature-control/feature-control.service.ts @@ -1,5 +1,4 @@ import { Injectable } from '@angular/core'; -import { ExtensionInfo } from 'eo/workbench/browser/src/app/shared/models/extension-manager'; import { Message, MessageService } from 'eo/workbench/browser/src/app/shared/services/message'; import featureJSON from './feature.json'; @@ -9,7 +8,8 @@ type configKey = keyof typeof featureJSON; }) export class FeatureControlService { config: { [key: configKey | string]: boolean }; - constructor(private message: MessageService) { + constructor(private message: MessageService) {} + init() { this.config = featureJSON; this.watchExtensionChange(); } diff --git a/src/workbench/browser/src/app/modules/chat-robot/chat-robot-message/chat-robot-message.component.ts b/src/workbench/browser/src/app/modules/chat-robot/chat-robot-message/chat-robot-message.component.ts index 205d88fdc..ec7c482dd 100644 --- a/src/workbench/browser/src/app/modules/chat-robot/chat-robot-message/chat-robot-message.component.ts +++ b/src/workbench/browser/src/app/modules/chat-robot/chat-robot-message/chat-robot-message.component.ts @@ -11,7 +11,7 @@ import { AfterViewInit, Component, ElementRef, HostBinding, Input, OnInit, Templ

-
 {{ message }} 
+
 {{ message }} 
`, diff --git a/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts b/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts index 4bb630d8b..63552bac5 100644 --- a/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts +++ b/src/workbench/browser/src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts @@ -1,10 +1,14 @@ import { animate, style, transition, trigger } from '@angular/animations'; import { CommonModule } from '@angular/common'; import { HttpClient } from '@angular/common/http'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core'; +import { EoNgButtonModule } from 'eo-ng-button'; import { FeatureControlService } from 'eo/workbench/browser/src/app/core/services/feature-control/feature-control.service'; import { ExtensionInfo } from 'eo/workbench/browser/src/app/shared/models/extension-manager'; import { Message, MessageService } from 'eo/workbench/browser/src/app/shared/services/message'; +import { TraceService } from 'eo/workbench/browser/src/app/shared/services/trace.service'; +import { StoreService } from 'eo/workbench/browser/src/app/shared/store/state.service'; +import StorageUtil from 'eo/workbench/browser/src/app/utils/storage/storage.utils'; import { APP_CONFIG } from 'eo/workbench/browser/src/environments/environment'; import { ChatRobotModule } from '../../../modules/chat-robot/chat-robot.module'; @@ -12,9 +16,10 @@ import { ChatRobotService } from '../../../modules/chat-robot/chat-robot.service import { StarMotivationComponent } from '../../../modules/star-motivation/star-motivation.component'; type messageItem = { - text: string; + text?: string; date: Date; reply: boolean; + content?: TemplateRef; type: string; user: { name: string; @@ -25,7 +30,7 @@ type messageItem = { @Component({ selector: 'pc-chatgpt-robot', standalone: true, - imports: [StarMotivationComponent, CommonModule, ChatRobotModule], + imports: [StarMotivationComponent, CommonModule, EoNgButtonModule, ChatRobotModule], animations: [ trigger('slideInOut', [ transition(':enter', [style({ transform: 'translateX(100%)' }), animate('300ms ease-in', style({ transform: 'translateX(0)' }))]), @@ -49,10 +54,16 @@ type messageItem = { *ngFor="let msg of messages" [message]="msg.text" [reply]="msg.reply" + [messageContent]="msg.content" [sender]="msg.user.name" [avatar]="msg.user.avatar" [date]="msg.date" > + + You have reached the maximum usage of ChatGPT for today. Please + to your Github account to + star/fork to get more times. + ; constructor( private http: HttpClient, public chat: ChatRobotService, public feature: FeatureControlService, - private message: MessageService + private message: MessageService, + private trace: TraceService, + private store: StoreService ) {} ngOnInit() { setTimeout(() => { this.watchExtensionChange(); - }, 5 * 1000); + }, 5000); + } + login() { + window.open(APP_CONFIG.GITHUB_REPO_URL, '_blank'); + this.trace.report('jump_to_github', { + where_jump_to_github: 'chatGPT_extension' + }); + setTimeout(() => { + this.nowUsage = 0; + StorageUtil.set('cr_usage', this.nowUsage); + }, 5000); } sendChatGPTMessage($event) { this.loading = true; + this.trace.report('send_chatGPT'); this.http .post(`${APP_CONFIG.EXTENSION_URL}/chatGPT`, { message: this.messages.map(val => { @@ -118,6 +145,8 @@ export class ChatgptRobotComponent implements OnInit { }); return; } + this.nowUsage++; + StorageUtil.set('cr_usage', this.nowUsage, 24 * 60 * 60); this.messages.push({ text: res.result, date: new Date(), @@ -151,10 +180,23 @@ export class ChatgptRobotComponent implements OnInit { reply: false, type: 'text', user: { - name: $localize`Visitor`, + name: this.store.getUserProfile?.email || $localize`Visitor`, avatar: 'https://data.eolink.com/PXMbLGmc2f0b29596764f7456eefb75478ed77b4fd172d9' } }); + if (this.nowUsage >= this.MAX_LIMIT) { + this.messages.push({ + content: this.moreTwiceTmp, + date: new Date(), + reply: true, + type: 'text', + user: { + name: 'Postcat', + avatar: './assets/images/logo.svg' + } + }); + return; + } this.sendChatGPTMessage($event); } watchExtensionChange() {