UNPKG

devexpress-reporting

Version:

DevExpress Reporting provides the capability to develop a reporting application to create and customize reports.

144 lines (143 loc) 5.35 kB
/** * DevExpress HTML/JS Reporting (designer\wizard\pages\fullscreen\ai\aiReasoningPage.js) * Version: 26.1.3 * Build date: Jun 16, 2026 * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * License: https://www.devexpress.com/Support/EULAs/universal.xml */ import { NotifyAboutWarning, createDeferred, tryDelay } from '@devexpress/analytics-core/analytics-internal-native'; import { getLocalization } from '@devexpress/analytics-core/analytics-utils'; import { FullscreenWizardPage, __finishActionFunctionName, __previousActionFunctionName } from '@devexpress/analytics-core/analytics-wizard'; import * as ko from 'knockout'; import { FullscreenReportWizardPageId } from '../../../pageId'; import { ReportAIService } from '../../../../services/_aiService'; const GENERATION_STATUS_TIMEOUT = 1000; export class AIReasoningPage extends FullscreenWizardPage { constructor() { super(...arguments); this._controller = new AbortController(); this._shouldDisposeReport = true; this._status = ko.observable(null); this._question = ko.observable(null); this._answer = ko.observable(null); this._generationCompleted = ko.observable(false); this._defaultErrorText = getLocalization('Report generation failed. Please try again.', 'ASPxReportsStringId.ReportDesigner_Wizard_AI_ReportGenerationWorkflow_GenerationFailed'); this._defaultStatusText = getLocalization('Analyzing the request...', 'ASPxReportsStringId.ReportDesigner_Wizard_AI_ReportGenerationWorkflow_AnalyzingRequest'); this._submitButtonText = getLocalization('Submit', 'ASPxReportsStringId.ReportDesigner_Wizard_AI_ReportGeneration_Submit'); } _shouldIgnoreError() { return this._shouldDisposeReport === false; } async _getReportGenerationStatus(id) { try { return await ReportAIService.getReportGenerationStatus(id, () => this._shouldIgnoreError()); } catch { return; } } async _disposeReport() { this._controller?.abort(); this._controller = null; if (!this._shouldDisposeReport) return; this._shouldDisposeReport = false; try { await ReportAIService.cancelReportGeneration(this._id); } catch { return; } } _startGenerationStatusPolling() { this._controller?.abort(); this._controller = new AbortController(); void this._updateGenerationStatus(this._id, this._controller); } async _updateGenerationStatus(id, controller) { while (controller.signal.aborted === false) { const status = await this._getReportGenerationStatus(id); if (!status || controller.signal.aborted) { return; } if (status.status) { this._status(status.status); } if (!status.requestAgain) { if (status.completed) { this._generationCompleted(true); this[__finishActionFunctionName](); } else { NotifyAboutWarning(status.faultMessage || this._defaultErrorText, true); this[__previousActionFunctionName](); } return; } if (status.question) { this._question(status.question); return; } await tryDelay(GENERATION_STATUS_TIMEOUT, controller.signal); } } initialize(state) { this._status(this._defaultStatusText); this._generationCompleted(false); this._id = state.id; this._shouldDisposeReport = true; this._startGenerationStatusPolling(); return createDeferred().resolve().promise(); } commit() { return createDeferred().resolve().promise(); } dispose() { super.dispose(); this._disposeReport(); } canNext() { return false; } canFinish() { return this._generationCompleted(); } async _onExit(goForward) { if (goForward) { this._shouldDisposeReport = false; } else { await this._disposeReport(); } } async _submitAnswer() { const answer = this._answer(); if (!answer) return; this._question(null); this._answer(null); try { await ReportAIService.answerClarificationQuestion(this._id, answer, () => this._shouldIgnoreError()); } catch { return; } this._startGenerationStatusPolling(); } get _options() { return this._question()?.options || []; } get _generationInProgress() { return !this._generationCompleted(); } } export function _registerAIReasoningPage(factory) { factory.registerMetadata(FullscreenReportWizardPageId.AIReasoningPage, { create: () => new AIReasoningPage(), getState: (state) => state, setState: () => void 0, resetState: () => void 0, template: 'dxrd-page-ai-report-generation', navigationPanelText: getLocalization('Building your report...', 'ASPxReportsStringId.ReportDesigner_Wizard_AI_ReportGeneration') }); }