Skip to content

Commit 2b72cda

Browse files
feat: Add an embedded form to ChatAI
1 parent fdeb3c1 commit 2b72cda

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/app/elements/chat/chat.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@if(isShowSetting && subViews.length === 1) {
44
<nz-float-button (click)="handleShowDrawer()" [nzIcon]="setting"></nz-float-button>
55
}
6-
<nz-float-button (click)="showChatAI()" [nzIcon]="customer"> </nz-float-button>
6+
<nz-float-button (click)="showChatAI()" [hidden]="!chatAiApiEnabled" [nzIcon]="customer"> </nz-float-button>
77
</nz-float-button-group>
88
<ng-template #setting>
99
<nz-icon nzTheme="outline" nzType="setting" />
@@ -12,7 +12,7 @@
1212
<img alt="" class="chat-img" src="assets/icons/chat-ai.png" />
1313
</ng-template>
1414

15-
<div *ngIf="chatAiEnabled" [style.display]="chatAIShown ? 'block' : 'none'" class="drawer-panel">
15+
<div *ngIf="chatAiApiEnabled" [style.display]="chatAIShown ? 'block' : 'none'" class="drawer-panel">
1616
<div class="content">
1717
<iframe
1818
#contentWindow

src/app/elements/chat/chat.component.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export class ElementChatComponent implements OnInit, OnDestroy, AfterViewInit {
7575
return this.currentView.hasOwnProperty('subViews') ? this.currentView.subViews : [];
7676
}
7777

78-
get chatAiEnabled() {
79-
return this._settingSvc.globalSetting.CHAT_AI_ENABLED;
78+
get chatAiApiEnabled() {
79+
const setting = this._settingSvc.globalSetting
80+
return setting.CHAT_AI_ENABLED && setting.CHAT_AI_METHOD === 'api';
8081
}
8182

8283
@HostListener('mousedown', ['$event'])
@@ -124,6 +125,38 @@ export class ElementChatComponent implements OnInit, OnDestroy, AfterViewInit {
124125
this.viewSrv.currentView$.subscribe((state: View) => {
125126
this.currentView = state;
126127
});
128+
129+
this._settingSvc.globalSetting$.subscribe(setting => {
130+
if (!setting.CHAT_AI_ENABLED) {
131+
return
132+
}
133+
134+
if (setting.CHAT_AI_METHOD === 'embed') {
135+
this.insertEmbedScript()
136+
} else if (setting.CHAT_AI_METHOD === 'api') {
137+
this.listenChatAI()
138+
}
139+
})
140+
}
141+
142+
private insertEmbedScript(): void {
143+
const embedScriptId = 'chat-ai-embed-id';
144+
if (document.getElementById(embedScriptId)) {
145+
return;
146+
}
147+
148+
const script = document.createElement('script');
149+
script.id = embedScriptId
150+
script.src = this._settingSvc.globalSetting.CHAT_AI_EMBED_URL;
151+
script.async = true;
152+
script.onload = () => {
153+
const loadEvent = new Event('load', { bubbles: false, cancelable: false });
154+
window.dispatchEvent(loadEvent);
155+
};
156+
document.body.appendChild(script);
157+
}
158+
159+
private listenChatAI(): void {
127160
this.iframeURL = '/ui/#/chat/chat-ai?from=luna';
128161

129162
window.addEventListener('message', event => {

src/app/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ export class GlobalSetting {
276276
TERMINAL_GRAPHICAL_RESOLUTION: string;
277277
CONNECTION_TOKEN_REUSABLE: boolean;
278278
CHAT_AI_ENABLED: boolean;
279+
CHAT_AI_METHOD: string;
280+
CHAT_AI_EMBED_URL: string;
279281
VIEW_ASSET_ONLINE_SESSION_INFO: boolean;
280282
LANGUAGES: any;
281283
}

src/app/services/setting.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class SettingService {
1717
public appletConnectMethod$ = new BehaviorSubject<string>("");
1818
public keyboardLayout$ = new BehaviorSubject<string>("");
1919
public isDirectNavigation$ = new BehaviorSubject<boolean>(false);
20+
public globalSetting$ = new BehaviorSubject<GlobalSetting>(new GlobalSetting());
2021

2122
constructor(
2223
private _localStorage: LocalStorageService,
@@ -34,6 +35,7 @@ export class SettingService {
3435
url += `?token=${connectionToken}`;
3536
}
3637
this.globalSetting = await this._http.get<any>(url).toPromise();
38+
this.globalSetting$.next(this.globalSetting);
3739
this.setting.commandExecution =
3840
this.globalSetting.SECURITY_COMMAND_EXECUTION;
3941
this.setLogo();

0 commit comments

Comments
 (0)