mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-10 10:53:07 +02:00
Fix chatbot
This commit is contained in:
+17
-2
@@ -1,9 +1,10 @@
|
||||
import { Telemetry } from './../helpers/Telemetry';
|
||||
import { TelemetryEvent, PreviewCommands, SETTING_EXPERIMENTAL } from './../constants';
|
||||
import { TelemetryEvent, PreviewCommands, GeneralCommands } from './../constants';
|
||||
import { join } from 'path';
|
||||
import { commands, Uri, ViewColumn, window } from 'vscode';
|
||||
import { Extension, Settings } from '../helpers';
|
||||
import { WebviewHelper } from '@estruyf/vscode';
|
||||
import { getLocalizationFile } from '../utils/getLocalizationFile';
|
||||
|
||||
export class Chatbot {
|
||||
/**
|
||||
@@ -30,13 +31,27 @@ export class Chatbot {
|
||||
|
||||
const cspSource = webView.webview.cspSource;
|
||||
|
||||
webView.webview.onDidReceiveMessage((message) => {
|
||||
webView.webview.onDidReceiveMessage(async (message) => {
|
||||
switch (message.command) {
|
||||
case PreviewCommands.toVSCode.open:
|
||||
if (message.data) {
|
||||
commands.executeCommand('vscode.open', message.data);
|
||||
}
|
||||
return;
|
||||
case GeneralCommands.toVSCode.getLocalization:
|
||||
const { requestId } = message;
|
||||
if (!requestId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileContents = await getLocalizationFile();
|
||||
|
||||
webView.webview.postMessage({
|
||||
command: GeneralCommands.toVSCode.getLocalization,
|
||||
requestId,
|
||||
payload: fileContents
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ import { useSettingsContext } from '../../providers/SettingsProvider';
|
||||
import { AiInitResponse } from './models/AiInitResponse';
|
||||
import * as l10n from '@vscode/l10n';
|
||||
import { LocalizationKey } from '../../../localization';
|
||||
import { messageHandler } from '@estruyf/vscode/dist/client';
|
||||
import { GeneralCommands } from '../../../constants';
|
||||
|
||||
export interface IChatbotProps { }
|
||||
|
||||
@@ -21,9 +23,19 @@ export const Chatbot: React.FunctionComponent<IChatbotProps> = ({ }: React.Props
|
||||
const [answerIds, setAnswerIds] = React.useState<number[]>([]);
|
||||
const [sources, setSources] = React.useState<string[][]>([]);
|
||||
const [loading, setLoading] = React.useState<boolean>(false);
|
||||
const [localeReady, setLocaleReady] = React.useState<boolean>(false);
|
||||
|
||||
const init = async () => {
|
||||
setLoading(true);
|
||||
messageHandler.request<any>(GeneralCommands.toVSCode.getLocalization).then((data) => {
|
||||
if (data) {
|
||||
l10n.config({
|
||||
contents: data
|
||||
});
|
||||
}
|
||||
setLocaleReady(true);
|
||||
});
|
||||
|
||||
const initResponse = await fetch(`${aiUrl}/api/ai-init`);
|
||||
|
||||
if (!initResponse.ok) {
|
||||
@@ -54,7 +66,7 @@ export const Chatbot: React.FunctionComponent<IChatbotProps> = ({ }: React.Props
|
||||
setSources(prev => [...prev, []])
|
||||
setAnswerIds(prev => [...prev, 0])
|
||||
|
||||
if (!company || !chatId) {
|
||||
if (!company || !chatId || !localeReady) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user