Speech-to-Text in React Inline AI Assist
23 Sep 202624 minutes to read
The Syncfusion React Inline AI Assist component integrates Speech-to-Text functionality through the browser’s Web Speech API. This enables the conversion of spoken words into text using the device’s microphone, allowing users to provide voice input directly in the Inline AI Assist prompt area.
When content is selected, the recognized speech can be used as a prompt along with the selected content to provide contextual AI assistance. The generated response is displayed inline, where users can review and either accept or discard the suggestion.
Prerequisites
Before integrating Speech-to-Text, ensure the following:
- The Syncfusion Inline AI Assist component is properly set up in your React application.
- The Inline AI Assist component is integrated with Azure OpenAI.
Enable built-in speech-to-text
You can enable speech-to-text support using the speechToTextSettings property. Set the enable property to true within the speechToTextSettings configuration to activate this feature.
Once enabled, a microphone button appears in the Inline AI Assist prompt area. The recognized speech is converted into prompt text and passed through the promptRequest event when the prompt is submitted.
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
const isAcceptedRef = React.useRef(false);
// Enables the built-in speech-to-text functionality using Web Speech API
const speechToTextSettings = {
enable: true
};
// Opens the Inline AI Assist popup when the Summarize button is clicked
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when a prompt is submitted from voice input
const handlePromptRequest = (args) => {
setTimeout(() => {
// Default response displayed in the AI Assist popup before Accept and Discard
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
isAcceptedRef.current = true;
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
isAcceptedRef.current = false;
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="stt"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, InlinePromptRequestEventArgs, ResponseItemSelectEventArgs, SpeechToTextSettingsModel } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
const isAcceptedRef = React.useRef<boolean>(false);
// Enables the built-in speech-to-text functionality using Web Speech API
const speechToTextSettings: SpeechToTextSettingsModel = {
enable: true
};
// Opens the Inline AI Assist popup when the Summarize button is clicked
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when a prompt is submitted from voice input
const handlePromptRequest = (args: InlinePromptRequestEventArgs): void => {
setTimeout(() => {
// Default response displayed in the AI Assist popup before Accept and Discard
const defaultResponse: string = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
isAcceptedRef.current = true;
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
isAcceptedRef.current = false;
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="stt"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));Configure speech recognition language
The lang property allows you to set the language code for speech recognition. By default, it uses the browser’s language settings, but you can specify a custom language code, such as en-US, es-ES, or fr-FR. This ensures that the speech recognition engine recognizes and transcribes speech in the specified language accurately.
Configure speech button settings
The buttonSettings property lets you customize the microphone button’s appearance and text content by configuring the content property for the text displayed when idle, stopContent for the text displayed while recording, iconCss for the icon shown when idle, and stopIconCss for the icon shown while recording. This allows you to customize the user interface and provide clear visual feedback while speech recognition is active.
Enable interim results
The allowInterimResults property enables real-time transcription while the user is speaking. When set to true, the speech recognition engine returns interim transcripts as it processes the audio. This provides immediate feedback by displaying partially recognized speech before the final transcript is generated.
This example demonstrates how to configure Inline AI Assist with the speech recognition language, microphone button settings, and interim results:
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
const isAcceptedRef = React.useRef(false);
// Configures language, interim results, and custom microphone button labels and icons
const speechToTextSettings = {
enable: true,
lang: 'en-US',
allowInterimResults: true,
buttonSettings: {
content: 'Start Recording',
stopContent: 'Stop Recording',
iconCss: 'e-icons e-microphone',
stopIconCss: 'e-icons e-microphone-off'
}
};
// Opens the Inline AI Assist popup on Summarize button click
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered once the recognized voice prompt is submitted
const handlePromptRequest = (args) => {
setTimeout(() => {
// Default response displayed in the AI Assist popup before Accept and Discard
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
isAcceptedRef.current = true;
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
isAcceptedRef.current = false;
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="speech-configuration"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, InlinePromptRequestEventArgs, ResponseItemSelectEventArgs, SpeechToTextSettingsModel } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
const isAcceptedRef = React.useRef<boolean>(false);
// Configures language, interim results, and custom microphone button labels and icons
const speechToTextSettings: SpeechToTextSettingsModel = {
enable: true,
lang: 'en-US',
allowInterimResults: true,
buttonSettings: {
content: 'Start Recording',
stopContent: 'Stop Recording',
iconCss: 'e-icons e-microphone',
stopIconCss: 'e-icons e-microphone-off'
}
};
// Opens the Inline AI Assist popup on Summarize button click
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered once the recognized voice prompt is submitted
const handlePromptRequest = (args: InlinePromptRequestEventArgs): void => {
setTimeout(() => {
// Default response displayed in the AI Assist popup before Accept and Discard
const defaultResponse: string = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
isAcceptedRef.current = true;
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
isAcceptedRef.current = false;
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="speech-configuration"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));Configure tooltip settings
You can customize the tooltips displayed for the microphone button using the tooltipSettings property. The tooltip can provide different information based on whether speech recognition is idle or active.
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
// Custom tooltips shown for the microphone button while idle and recording
const speechToTextSettings = {
enable: true,
tooltipSettings: {
content: 'Click to start recording',
stopContent: 'Click to stop recording'
}
};
// Opens the popup on Summarize button click
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when the voice prompt is submitted
const handlePromptRequest = (args) => {
setTimeout(() => {
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="tooltipSettings"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
placeholder="Type or speak a prompt"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, InlinePromptRequestEventArgs, ResponseItemSelectEventArgs, SpeechToTextSettingsModel } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
// Custom tooltips shown for the microphone button while idle and recording
const speechToTextSettings: SpeechToTextSettingsModel = {
enable: true,
tooltipSettings: {
content: 'Click to start recording',
stopContent: 'Click to stop recording'
}
};
// Opens the popup on Summarize button click
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when the voice prompt is submitted
const handlePromptRequest = (args: InlinePromptRequestEventArgs): void => {
setTimeout(() => {
const defaultResponse: string = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="tooltipSettings"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
placeholder="Type or speak a prompt"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));Speech-to-text events
The speech-to-text functionality provides the onStart, onStop, transcriptChanged, and onError events. The onStart event is triggered when speech recognition starts, onStop is triggered when recognition stops, transcriptChanged is triggered when the recognized transcript is updated, and onError is triggered when an error occurs during speech recognition.
The recognized transcript is updated in the Inline AI Assist prompt area. When the prompt is submitted, both typed and speech-recognized input are available through the prompt property of the promptRequest event arguments.
import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
function App() {
const assistRef = React.useRef(null);
const editableRef = React.useRef(null);
const recordingStatusRef = React.useRef(null);
const transcriptDisplayRef = React.useRef(null);
const errorMessageRef = React.useRef(null);
const transcriptTimerRef = React.useRef(null);
const lastTranscriptRef = React.useRef('');
// Updates the status panel when speech recognition begins
const onSpeechStart = (args) => {
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Recording...';
recordingStatusRef.current.className = 'status-indicator recording';
}
if (errorMessageRef.current) {
errorMessageRef.current.style.display = 'none';
}
};
// Resets the status and displays the captured transcript after recognition stops
const onSpeechStop = (args) => {
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Ready to record';
recordingStatusRef.current.className = 'status-indicator ready';
}
if (transcriptDisplayRef.current && lastTranscriptRef.current) {
transcriptDisplayRef.current.textContent = lastTranscriptRef.current;
transcriptDisplayRef.current.style.fontStyle = 'normal';
}
if (transcriptTimerRef.current) {
clearTimeout(transcriptTimerRef.current);
}
transcriptTimerRef.current = setTimeout(() => {
lastTranscriptRef.current = '';
if (transcriptDisplayRef.current) {
transcriptDisplayRef.current.textContent = 'Waiting for speech input...';
}
}, 2000);
};
// Streams interim and final transcripts to the status panel during recognition
const onTranscriptChanged = (args) => {
const currentTranscript = args.text || args.value || args.transcript || (args.result && args.result.transcript) || '';
const isFinal = args.isFinal || args.final || (args.result && args.result.isFinal) || false;
if (currentTranscript) {
lastTranscriptRef.current = currentTranscript;
}
if (transcriptDisplayRef.current) {
transcriptDisplayRef.current.textContent = currentTranscript;
transcriptDisplayRef.current.style.fontStyle = isFinal ? 'normal' : 'italic';
}
};
// Surfaces recognition errors in the status panel and resets the recording state
const onSpeechError = (args) => {
if (errorMessageRef.current) {
const errorText = errorMessageRef.current.querySelector('.error-text');
if (errorText) {
errorText.textContent = 'Error: ' + (args.error || 'Speech recognition error occurred');
}
errorMessageRef.current.style.display = 'block';
}
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Ready to record';
recordingStatusRef.current.className = 'status-indicator ready';
}
};
// Wires up all speech recognition lifecycle event handlers
const speechToTextSettings = {
enable: true,
onStart: onSpeechStart,
onStop: onSpeechStop,
transcriptChanged: onTranscriptChanged,
onError: onSpeechError
};
// Opens the popup on Summarize button click
const handleSummarizeBtnClick = () => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when the recognized voice prompt is submitted
const handlePromptRequest = (args) => {
setTimeout(() => {
// Default placeholder response shown after the thinking delay
const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args) => {
if (args.command.label === 'Accept') {
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<div className="integration-speechtotext-section">
<div className="speech-events-container">
<div className="speech-info-panel">
<div className="speech-status">
<label>Recording Status:</label>
<span id="recordingStatus" className="status-indicator ready" ref={recordingStatusRef}>Ready to record</span>
</div>
<div className="transcript-section">
<label>Live Transcript:</label>
<div id="transcriptDisplay" className="transcript-display" ref={transcriptDisplayRef}></div>
</div>
<div className="error-section" id="errorMessage" style= ref={errorMessageRef}>
<span className="error-text"></span>
</div>
</div>
</div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="speechEvents"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
placeholder="Type or speak a prompt"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('container'));import { InlineAIAssistComponent, InlinePromptRequestEventArgs, ResponseItemSelectEventArgs, SpeechToTextSettingsModel } from '@syncfusion/ej2-react-interactive-chat';
import { enableRipple } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
enableRipple(true);
const App: React.FC = () => {
const assistRef = React.useRef<InlineAIAssistComponent>(null);
const editableRef = React.useRef<HTMLDivElement>(null);
const recordingStatusRef = React.useRef<HTMLSpanElement>(null);
const transcriptDisplayRef = React.useRef<HTMLDivElement>(null);
const errorMessageRef = React.useRef<HTMLDivElement>(null);
const transcriptTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
const lastTranscriptRef = React.useRef<string>('');
// Updates the status panel when speech recognition begins
const onSpeechStart = (args: any): void => {
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Recording...';
recordingStatusRef.current.className = 'status-indicator recording';
}
if (errorMessageRef.current) {
errorMessageRef.current.style.display = 'none';
}
};
// Resets the status and displays the captured transcript after recognition stops
const onSpeechStop = (args: any): void => {
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Ready to record';
recordingStatusRef.current.className = 'status-indicator ready';
}
if (transcriptDisplayRef.current && lastTranscriptRef.current) {
transcriptDisplayRef.current.textContent = lastTranscriptRef.current;
transcriptDisplayRef.current.style.fontStyle = 'normal';
}
if (transcriptTimerRef.current) {
clearTimeout(transcriptTimerRef.current);
}
transcriptTimerRef.current = setTimeout(() => {
lastTranscriptRef.current = '';
if (transcriptDisplayRef.current) {
transcriptDisplayRef.current.textContent = 'Waiting for speech input...';
}
}, 2000);
};
// Streams interim and final transcripts to the status panel during recognition
const onTranscriptChanged = (args: any): void => {
const currentTranscript: string = args.text || args.value || args.transcript || (args.result && args.result.transcript) || '';
const isFinal: boolean = args.isFinal || args.final || (args.result && args.result.isFinal) || false;
if (currentTranscript) {
lastTranscriptRef.current = currentTranscript;
}
if (transcriptDisplayRef.current) {
transcriptDisplayRef.current.textContent = currentTranscript;
transcriptDisplayRef.current.style.fontStyle = isFinal ? 'normal' : 'italic';
}
};
// Surfaces recognition errors in the status panel and resets the recording state
const onSpeechError = (args: any): void => {
if (errorMessageRef.current) {
const errorText: HTMLElement | null = errorMessageRef.current.querySelector('.error-text');
if (errorText) {
errorText.textContent = 'Error: ' + (args.error || 'Speech recognition error occurred');
}
errorMessageRef.current.style.display = 'block';
}
if (recordingStatusRef.current) {
recordingStatusRef.current.textContent = 'Ready to record';
recordingStatusRef.current.className = 'status-indicator ready';
}
};
// Wires up all speech recognition lifecycle event handlers
const speechToTextSettings: SpeechToTextSettingsModel = {
enable: true,
onStart: onSpeechStart,
onStop: onSpeechStop,
transcriptChanged: onTranscriptChanged,
onError: onSpeechError
};
// Opens the popup on Summarize button click
const handleSummarizeBtnClick = (): void => {
if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
assistRef.current.showPopup();
}
};
// Triggered when the recognized voice prompt is submitted
const handlePromptRequest = (args: InlinePromptRequestEventArgs): void => {
setTimeout(() => {
// Default placeholder response shown after the thinking delay
const defaultResponse: string = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
assistRef.current.addResponse(defaultResponse, true);
}
}, 1000);
};
// Handles Accept and Discard buttons on the inline response
const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
if (args.command.label === 'Accept') {
if (editableRef.current) {
editableRef.current.innerHTML = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
}
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
} else if (args.command.label === 'Discard') {
if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
assistRef.current.hidePopup();
}
}
};
return (
<div>
<div className="integration-speechtotext-section">
<div className="speech-events-container">
<div className="speech-info-panel">
<div className="speech-status">
<label>Recording Status:</label>
<span id="recordingStatus" className="status-indicator ready" ref={recordingStatusRef}>Ready to record</span>
</div>
<div className="transcript-section">
<label>Live Transcript:</label>
<div id="transcriptDisplay" className="transcript-display" ref={transcriptDisplayRef}></div>
</div>
<div className="error-section" id="errorMessage" style= ref={errorMessageRef}>
<span className="error-text"></span>
</div>
</div>
</div>
<button
id="summarizeBtn"
className="e-btn e-primary"
style=
onClick={handleSummarizeBtnClick}
>
Content Summarize
</button>
<div id="editableText" contentEditable="true" ref={editableRef}>
<p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
<p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
</div>
<InlineAIAssistComponent
id="speechEvents"
ref={assistRef}
relateTo="#summarizeBtn"
responseMode="Inline"
placeholder="Type or speak a prompt"
promptRequest={handlePromptRequest}
speechToTextSettings={speechToTextSettings}
responseSettings=
/>
</div>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('container'));Browser compatibility
The speech-to-text functionality in Inline AI Assist relies on the browser’s Speech Recognition API, which has limited browser support. Refer to the Browser Compatibility section for detailed information.
The browser may request permission to access the device’s microphone when speech recognition starts. Speech-to-text functionality is available only when microphone permission is granted and the browser supports the Speech Recognition API.