Skip to content

Commit

Permalink
feat(gpt-robot): add limit
Browse files Browse the repository at this point in the history
  • Loading branch information
scarqin committed Mar 8, 2023
1 parent 80f9ef3 commit 9187853
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
15 changes: 11 additions & 4 deletions src/workbench/browser/locale/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">67</context>
<context context-type="linenumber">77</context>
</context-group>
</trans-unit>
<trans-unit id="507532210608593689" datatype="html">
Expand Down Expand Up @@ -1385,21 +1385,28 @@
<source>ChatGPT Extensions</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">45</context>
<context context-type="linenumber">50</context>
</context-group>
</trans-unit>
<trans-unit id="8551068906778129794" datatype="html">
<source> You have reached the maximum usage of ChatGPT for today. Please <x id="START_TAG_BUTTON" ctype="x-button" equiv-text="&lt;button eo-ng-button nz=&quot;primary&quot; (click)=&quot;login()&quot;&gt;"/>Sign in<x id="CLOSE_TAG_BUTTON" ctype="x-button" equiv-text="&lt;/button&gt;"/> to your Github account to star/fork to get more times. </source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">62,65</context>
</context-group>
</trans-unit>
<trans-unit id="3964296369329537542" datatype="html">
<source>Send message to AI</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">59</context>
<context context-type="linenumber">69</context>
</context-group>
</trans-unit>
<trans-unit id="2895747618881739814" datatype="html">
<source>Visitor</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">154</context>
<context context-type="linenumber">182</context>
</context-group>
</trans-unit>
<trans-unit id="6627551976444260400" datatype="html">
Expand Down
16 changes: 12 additions & 4 deletions src/workbench/browser/locale/messages.zh.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">67</context>
<context context-type="linenumber">77</context>
</context-group>
<target state="translated">ChatGPT 机器人</target>
</trans-unit>
Expand Down Expand Up @@ -1542,23 +1542,31 @@
<source>ChatGPT Extensions</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">45</context>
<context context-type="linenumber">50</context>
</context-group>
<target state="translated">ChatGPT 插件</target>
</trans-unit>
<trans-unit id="8551068906778129794" datatype="html">
<source> You have reached the maximum usage of ChatGPT for today. Please <x id="START_TAG_BUTTON" ctype="x-button" equiv-text="&lt;button eo-ng-button nz=&quot;primary&quot; (click)=&quot;login()&quot;&gt;"/>Sign in<x id="CLOSE_TAG_BUTTON" ctype="x-button" equiv-text="&lt;/button&gt;"/> to your Github account to star/fork to get more times. </source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">62,65</context>
</context-group>
<target state="translated"> 您今天 ChatGPT 次数已使用完。请 <x id="START_TAG_BUTTON" ctype="x-button" equiv-text="&lt;button eo-ng-button nz=&quot;primary&quot; (click)=&quot;login()&quot;&gt;"/>登录<x id="CLOSE_TAG_BUTTON" ctype="x-button" equiv-text="&lt;/button&gt;"/> 您的 Github 账号 Star/Fork 项目获取更多次数。 </target>
</trans-unit>
<trans-unit id="3964296369329537542" datatype="html">
<source>Send message to AI</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">59</context>
<context context-type="linenumber">69</context>
</context-group>
<target state="translated">发送消息给 AI</target>
</trans-unit>
<trans-unit id="2895747618881739814" datatype="html">
<source>Visitor</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/pages/components/chatgpt-robot/chatgpt-robot.component.ts</context>
<context context-type="linenumber">154</context>
<context context-type="linenumber">182</context>
</context-group>
<target state="translated">游客</target>
</trans-unit>
Expand Down
4 changes: 4 additions & 0 deletions src/workbench/browser/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AfterViewInit, Component, ElementRef, HostBinding, Input, OnInit, Templ
</p>
<div class="message-content">
<ng-container *ngTemplateOutlet="messageContent"></ng-container>
<pre class="text !mb-0"> {{ message }} </pre>
<pre *ngIf="message" class="text !mb-0"> {{ message }} </pre>
</div>
</div>
`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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';
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<any>;
type: string;
user: {
name: string;
Expand All @@ -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)' }))]),
Expand All @@ -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"
></pc-chat-robot-message>
<ng-template #moreTwice i18n>
You have reached the maximum usage of ChatGPT for today. Please
<button eo-ng-button class="mb-[2px]" nz="primary" (click)="login()" nzSize="small">Sign in</button> to your Github account to
star/fork to get more times.
</ng-template>
<pc-chat-robot-form
(send)="sendMessage($event)"
[loading]="loading"
Expand All @@ -66,6 +77,8 @@ type messageItem = {
export class ChatgptRobotComponent implements OnInit {
title = $localize`ChatGPT Robot`;
loading = false;
MAX_LIMIT = 10;
nowUsage = StorageUtil.get('cr_usage');
initMessage = {
date: new Date(),
reply: true,
Expand All @@ -76,23 +89,37 @@ export class ChatgptRobotComponent implements OnInit {
}
};
powerBy = {
title: 'APISPace',
title: 'APISpace',
link: 'https://www.apispace.com?utm_source=postcat&utm_medium=robot&utm_term=chatgptturbo'
};
messages: messageItem[] = [];
@ViewChild('moreTwice') moreTwiceTmp: TemplateRef<any>;
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 => {
Expand All @@ -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(),
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 9187853

Please sign in to comment.