@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
30 lines • 37.8 kB
JavaScript
/* @license */
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`kendo.core.min.js`),require(`kendo.data.min.js`),require(`kendo.icons.min.js`),require(`kendo.textarea.min.js`),require(`kendo.button.min.js`),require(`kendo.toolbar.min.js`),require(`kendo.skeletoncontainer.min.js`),require(`kendo.speechtotextbutton.min.js`),require(`kendo.panelbar.min.js`),require(`kendo.floatingactionbutton.min.js`)):typeof define==`function`&&define.amd?define([`exports`,`kendo.core.min`,`kendo.data.min`,`kendo.icons.min`,`kendo.textarea.min`,`kendo.button.min`,`kendo.toolbar.min`,`kendo.skeletoncontainer.min`,`kendo.speechtotextbutton.min`,`kendo.panelbar.min`,`kendo.floatingactionbutton.min`],t):(e=typeof globalThis<`u`?globalThis:e||self,t((e.kendo=e.kendo||{},e.kendo._globals=e.kendo._globals||{},e.kendo._globals.Aiprompt={}),e.kendo._globals.Core,e.kendo._globals.Data,e.kendo._globals.Icons,e.kendo._globals.Textarea,e.kendo._globals.Button,e.kendo._globals.Toolbar,e.kendo._globals.Skeletoncontainer,e.kendo._globals.Speechtotextbutton,e.kendo._globals.Panelbar,e.kendo._globals.Floatingactionbutton))})(this,function(e,t,n,r,i,a,o,s,c,l,u){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}}),(function(e){let t={ai:`assistant`,system:`system`,user:`user`,tool:`tool`};class n{constructor(e,t={}){this.aiprompt=e,this.options=t,this.buttonDefinitions=this._createButtonDefinitions()}static getBuiltInActionDefinitions(){return{copy:{command:`copy`,icon:`copy`,fillMode:`flat`,themeColor:`primary`,text:null},retry:{command:`retry`,icon:`arrow-rotate-cw`,fillMode:`flat`,text:null},ratePositive:{command:`ratePositive`,icon:`thumb-up-outline`,fillMode:`flat`,iconButton:!0,text:null},rateNegative:{command:`rateNegative`,icon:`thumb-down-outline`,fillMode:`flat`,iconButton:!0,text:null},stop:{command:`stop`,icon:`stop`,fillMode:`flat`,text:null}}}static processOutputActions(e){if(!e)return null;let t=n.getBuiltInActionDefinitions();return e.map(e=>typeof e==`string`?e===`rating`?[t.ratePositive,t.rateNegative]:e===`spacer`?{type:`spacer`}:t[e]||{command:e,type:`button`}:kendo.isPresent(e.command)&&e.command==`rating`?[t.ratePositive,t.rateNegative]:e).flat()}_createButtonDefinitions(){let e=this,t=n.getBuiltInActionDefinitions();return{copy:{...t.copy,getMessage:()=>e.view.options.messages.copyOutput,handler:(t,n)=>e._handleCopyAction(t,n)},retry:{...t.retry,getMessage:()=>e.view.options.messages.retryGeneration,handler:(t,n)=>e._handleRetryAction(t,n)},ratePositive:{...t.ratePositive,getMessage:()=>e.view.options.messages.ratePositive,handler:(t,n)=>e._handleRatePositiveAction(t,n)},rateNegative:{...t.rateNegative,getMessage:()=>e.view.options.messages.rateNegative,handler:(t,n)=>e._handleRateNegativeAction(t,n)},stop:{...t.stop,getMessage:()=>e.view.options.messages.stopGeneration,handler:(t,n)=>e._handleStopAction(t,n)}}}initializeButtons(e,t=null){let n=this;t&&t.forEach(t=>{if(t.type===`spacer`)return;let r=`[data-action-command="${t.command}"]`;n._initializeButton(e,r,t)})}_initializeButton(e,t,n){let r=this,i=r.buttonDefinitions[n.command];e.find(t).kendoButton({icon:n.icon||i?.icon,fillMode:n.fillMode||i?.fillMode||`flat`,themeColor:n.themeColor||i?.themeColor,rounded:n.rounded,click:function(e){let t=r.aiprompt.outputManager.getOutputFromElement(e.target),a={command:n.command,outputId:t.id,output:t.output||t.text||t||``,prompt:t.prompt||``,button:e.sender.element};i?.handler&&!r.aiprompt.trigger(`outputAction`,a)?i.handler(e,t):i||r.aiprompt.trigger(`outputAction`,a)}})}_handleCopyAction(e,t){let n=this.aiprompt._events&&this.aiprompt._events.outputCopy&&this.aiprompt._events.outputCopy.length>0;n&&kendo.logToConsole(`The outputCopy event is deprecated. Use the outputAction event instead.`,`warn`);let r={output:t};n&&this.aiprompt.trigger(`outputCopy`,r)||navigator.clipboard&&typeof navigator.clipboard.writeText==`function`&&navigator.clipboard.writeText(t.output||t.text||t||``)}_handleRetryAction(e,n){let{prompt:r,output:i}=this.aiprompt.outputManager.extractOutputData(n),a=this,o={prompt:r,output:n,isRetry:!0,history:[{role:t.ai,contents:[{$type:`text`,text:i}]}]},s=a.aiprompt?._selectedView?.service;s&&(o.service=s),!a.aiprompt.trigger(`promptRequest`,o)&&a.aiprompt?.options.service&&a.aiprompt?.transport?.read({prompt:r,history:o.history,isRetry:!0,service:o.service})}_handleRatePositiveAction(t,n){this.aiprompt._events&&this.aiprompt._events.outputRating&&this.aiprompt._events.outputRating.length>0&&kendo.logToConsole(`The outputRating event is deprecated. Use the outputAction event instead.`,`warn`),this.aiprompt.trigger(`outputRating`,{rateType:`positive`,output:n}),kendo.ui.icon(t.sender.element.find(`.k-icon`),`thumb-up`);let r=e(t.target).siblings(`[data-action-command='rateNegative'], [ref-rate-negative]`);kendo.ui.icon(r.find(`.k-icon`),`thumb-down-outline`)}_handleRateNegativeAction(t,n){this.aiprompt._events&&this.aiprompt._events.outputRating&&this.aiprompt._events.outputRating.length>0&&kendo.logToConsole(`The outputRating event is deprecated. Use the outputAction event instead.`,`warn`),this.aiprompt.trigger(`outputRating`,{rateType:`negative`,output:n}),kendo.ui.icon(t.sender.element.find(`.k-icon`),`thumb-down`);let r=e(t.target).siblings(`[data-action-command='ratePositive'], [ref-rate-positive]`);kendo.ui.icon(r.find(`.k-icon`),`thumb-up-outline`)}_handleStopAction(t,n){this.aiprompt.trigger(`promptRequestCancel`,{output:n}),e(t.target).siblings(`.k-hidden`).removeClass(`k-hidden`),e(t.target).addClass(`k-hidden`)}destroy(){this.aiprompt=null,this.options=null,this.buttonDefinitions=null}}kendo.ui.AIPromptOutputActionManager=n})(window.kendo.jQuery),(function(e){let t={PROMPT_VIEW:`k-prompt-view`,PROMPT_EXPANDER:`k-prompt-expander`,SUGGESTION_GROUP:`k-suggestion-group`,SUGGESTION:`k-suggestion`,CARD:`k-card`,CARD_LIST:`k-card-list`,CARD_HEADER:`k-card-header`,CARD_TITLE:`k-card-title`,CARD_SUBTITLE:`k-card-subtitle`,CARD_BODY:`k-card-body`,CARD_ACTIONS:`k-card-actions`,ACTIONS:`k-actions k-actions-start k-actions-horizontal`,SPACER:`k-spacer`,HIDDEN:`k-hidden`},n={PROMPT_INPUT:`ref-prompt-input`,SUGGESTIONS_BUTTON:`ref-prompt-suggestions-button`,GENERATE_BUTTON:`ref-generate-output-button`,OUTPUT_BODY:`ref-output-body`,COPY_BUTTON:`ref-copy-button`,RETRY_BUTTON:`ref-retry-button`,RATE_POSITIVE:`ref-rate-positive`,RATE_NEGATIVE:`ref-rate-negative`,STOP_GENERATION:`ref-stop-generation`};class r{static createPromptView({suggestions:e,promptSuggestionItemTemplate:i,messages:a}){let o=e?.length?r._createSuggestionsSection(e,i,a):``;return`<div class="${t.PROMPT_VIEW}">
<textarea ${n.PROMPT_INPUT}></textarea>
${o}
</div>`}static createPromptFooter({messages:e}){return`<div class="${t.ACTIONS} k-prompt-actions">
<button ${n.GENERATE_BUTTON}>${e.generateOutput}</button>
</div>`}static createSuggestionItem({suggestion:e}){return`<span role="listitem" class="${t.SUGGESTION}">${e}</span>`}static createOutputCard({output:e,showOutputRating:n,messages:i,showOutputSubtitleTooltip:a,encodedPromptOutputs:o,isStreaming:s,outputActions:c,outputTemplate:l}){let u=r._generateContentHtml({output:e,outputTemplate:l,encodedPromptOutputs:o}),d=r._generateActionsHtml({outputActions:c,showOutputRating:n,messages:i,isStreaming:s}),f=e.id?` data-id="${e.id}"`:``;return`<div role="listitem" tabindex="0" class="${t.CARD}"${f}>
${e.skipHeader?``:r._createCardHeader(e,i,a)}
${e.skipBody?``:r._createCardBody(u,e.isLoading)}
${e.skipActions?``:d}
</div>`}static createOutputView({promptOutputs:e,showOutputRating:n,messages:i,showOutputSubtitleTooltip:a,encodedPromptOutputs:o,outputActions:s,outputTemplate:c}){let l=e?e.map(e=>r.createOutputCard({output:e,showOutputRating:n,messages:i,showOutputSubtitleTooltip:a,encodedPromptOutputs:o,outputActions:s,outputTemplate:c})).join(``):``;return`<div class="${t.PROMPT_VIEW}">
<div role="list" class="${t.CARD_LIST}">
${l}
</div>
</div>`}static _createSuggestionsSection(e,r,i){let a=e.map(e=>r({suggestion:e})).join(``);return`<div class="${t.PROMPT_EXPANDER}">
<button ${n.SUGGESTIONS_BUTTON} aria-expanded="true">${i.promptSuggestions}</button>
<div class="k-prompt-expander-content" role="list">
<div class="${t.SUGGESTION_GROUP}">
${a}
</div>
</div>
</div>`}static _createCardHeader(e,n,r){let i=r?`title="${kendo.htmlEncode(e.prompt)}"`:``;return`<div class="${t.CARD_HEADER}">
<div class="${t.CARD_TITLE}">${n.outputTitle}</div>
<div class="${t.CARD_SUBTITLE}" ${i}>${kendo.htmlEncode(e.prompt)}</div>
</div>`}static _createCardBody(e,r){return`<div class="${t.CARD_BODY}" ${n.OUTPUT_BODY}>
${e}
</div>`}static _generateContentHtml({output:e,outputTemplate:t,encodedPromptOutputs:n}){if(t&&typeof t==`function`&&!e.isLoading&&e.output)return t({output:e,content:e.output});let r=e.output||``;return`<p ref-output-content${e.isLoading?` data-loading="true"`:` data-loading="false"`}>${n?kendo.htmlEncode(r):r}</p>`}static _generateActionsHtml({outputActions:e,showOutputRating:t,messages:n,isStreaming:i}){return e||=t?[`copy`,`retry`,`spacer`,`rating`]:[`copy`,`retry`],r._createCustomActions(e,t,n,i)}static _createCustomActions(e,n,i,a){let o=e.filter(e=>e.command!==`stop`),s=o.some(e=>e.command===`ratePositive`||e.command===`rateNegative`),c=[...o];n&&!s&&(c.some(e=>e.type===`spacer`)||c.push({type:`spacer`}),c.push({command:`ratePositive`,text:i.ratePositive,type:`button`},{command:`rateNegative`,text:i.rateNegative,type:`button`}));let l=c.map(e=>r._createActionButton(e,i,a)).join(``);return`<div class="${t.ACTIONS} ${t.CARD_ACTIONS}">
${l}
</div>`}static _createActionButton(e,n,i){if(e.type===`spacer`)return`<span class="${t.SPACER}"></span>`;let a=e.text||r._getActionText(e.command,n),o=e.title||a;return`<button data-action-command="${e.command}" title="${o}">${e.iconButton?``:a}</button>`}static _getActionText(e,t){return{copy:t.copyOutput,retry:t.retryGeneration,ratePositive:t.ratePositive,rateNegative:t.rateNegative}[e]||e}}kendo.ui.AIPromptTemplateBuilder=r})(window.kendo.jQuery),(function(e){class t{constructor(e,t={}){this.view=e,this.aiprompt=e.aiprompt,this.options=this._processSettings(t),this._speechButton=null}_processSettings(t){let n={integrationMode:`webSpeech`,lang:`en-US`,continuous:!1,interimResults:!1,maxAlternatives:1};return t===!1||t===null?{enabled:!1,options:null}:t===!0?{enabled:!0,options:n}:typeof t==`object`?{enabled:!0,options:e.extend({},n,t)}:{enabled:!0,options:n}}isEnabled(){return this.options.enabled}getTextAreaSuffixOptions(){return this.isEnabled()?{suffixOptions:{template:function(){return`<button ref-speech-to-text-button title="Speech to Text"></button>`},separator:!1}}:{}}initialize(e){if(!this.isEnabled()||!e)return!1;let t=e.wrapper.find(`button[ref-speech-to-text-button]`);return t.length===0?!1:(this._speechButton=t.kendoSpeechToTextButton({...this.options.options,fillMode:`flat`}).getKendoSpeechToTextButton(),this._speechButton.bind(`result`,t=>this._handleResult(t,e)),this.aiprompt.speechToTextButton=this._speechButton,!0)}_handleResult(e,t){if(e.isFinal||!this.options.options.interimResults){let n=e.alternatives[0]?.transcript||``,r=t.value(),i=r?r+` `+n:n,a=t.options.maxlength;a&&i.length>a&&(i=i.substring(0,a)),t.value(i)}}startRecognition(){this._speechButton&&this._speechButton.startRecognition()}stopRecognition(){this._speechButton&&this._speechButton.stopRecognition()}abortRecognition(){this._speechButton&&this._speechButton.abortRecognition()}isListening(){return this._speechButton?this._speechButton.isListening():!1}destroy(){this._speechButton&&=(this._speechButton.destroy(),null)}}kendo.ui.AIPromptSpeechManager=t})(window.kendo.jQuery),(function(e){class t{constructor(e,t){this.id=e.id,this.data=e,this._element=null,this._bodyElement=null,this._aiprompt=t.aiprompt,this._isLoading=e.isLoading||!1}get isLoading(){return this._isLoading}set isLoading(e){let t=this._isLoading;this._isLoading=e,this.data.isLoading=e,e===!0?this.showSkeleton():e===!1&&(this.hideSkeleton(),(t||this.data.output)&&this.applyFinalTemplate())}getElement(){return this._element}setElement(e){return this._element=e,this}updateContent(e){if(!this._element||this._element.length===0)return this;this.data.content=e,this.data.output=e;let t=this._element.find(`.k-card-body`),n=t.find(`[ref-output-content]`);if(e&&e.trim()&&n.length>0){t.find(`.k-skeleton`).remove(),n.attr(`data-loading`,`false`).show();let r=this._aiprompt.options.encodedPromptOutputs;n.html(r?kendo.htmlEncode(e):e)}return this}showSkeleton(){return this.showHeaderSkeleton(),this.showBodySkeleton(),this.showActionSkeleton(),this}hideSkeleton(){return this.hideHeaderSkeleton(),this.hideBodySkeleton(),this.hideActionSkeleton(),this}applyFinalTemplate(){if(!this._element||this._element.length===0)return this;let e=this._element.find(`.k-card-body`),t=e.find(`[ref-output-content]`),n=this._aiprompt?.options?.outputTemplate;if(n)typeof n==`string`&&(n=kendo.template(n));else return this;if(n&&typeof n==`function`&&this.data.output){let t=n({output:this.data,content:this.data.output});e.html(t)}else t.length>0&&t.attr(`data-loading`,`false`).show();return this}showHeaderSkeleton(){if(!this._element||this._element.length===0)return this;let t=this._element.find(`.k-card-header`);if(t.length===0)return this;if(t.children().hide(),t.find(`.k-skeleton`).length===0){let n=e(`<span class="k-skeleton k-skeleton-text k-skeleton-pulse"></span>`);n.css(`width`,`60%`).css(`height`,`24px`),t.prepend(n)}return this}hideHeaderSkeleton(){if(!this._element||this._element.length===0)return this;let e=this._element.find(`.k-card-header`);return e.length===0?this:(e.find(`.k-skeleton`).remove(),e.children().removeClass(`k-hidden`).show(),this)}showBodySkeleton(){if(!this._element||this._element.length===0)return this;let t=this._element.find(`.k-card-body`);if(t.find(`[ref-output-content]`).attr(`data-loading`,`true`).hide(),t.find(`.k-skeleton`).length===0){let n=e(`<span class="k-skeleton k-skeleton-rect k-skeleton-pulse"></span>`);n.css(`height`,`80px`),t.prepend(n)}return this}hideBodySkeleton(){return!this._element||this._element.length===0?this:(this._element.find(`.k-card-body`).find(`.k-skeleton`).remove(),this.applyFinalTemplate(),this)}showActionSkeleton(){if(!this._element||this._element.length===0)return this;let t=this._element.find(`.k-card-actions`);if(t.length>0&&(t.children().hide(),t.find(`.k-skeleton`).length===0)){let n=e(`<span class="k-skeleton k-skeleton-text k-skeleton-pulse"></span>`);n.css(`width`,`100%`).css(`height`,`32px`),t.prepend(n)}return this}hideActionSkeleton(){if(!this._element||this._element.length===0)return this;let e=this._element.find(`.k-card-actions`);return e.length>0&&(e.find(`.k-skeleton`).remove(),e.children().removeClass(`k-hidden`).show()),this}toggleActionButtons(e,t){if(!this._element||this._element.length===0)return this;if(t){let t=this._element.find(`[data-action-command]`);e?t.addClass(`k-hidden`):t.removeClass(`k-hidden`)}else{let t=this._element.find(`[ref-copy-button], [ref-retry-button]`);e?t.addClass(`k-hidden`):t.removeClass(`k-hidden`)}return this}destroy(){this._element=null,this._bodyElement=null,this._aiprompt=null,this.data=null,this.id=null,this._isLoading=!1}}class n{constructor(e){this.aiprompt=e}createOutputObject(e){return new t(e,this)}getLastOutputObject(){if(this.aiprompt.promptOutputs.length>0){let e=this.aiprompt.promptOutputs[0];return this.aiprompt.outputObjects.get(e.id)}return null}updatePromptOutputContent(e,t){let n;return n=t?this.aiprompt.outputObjects.get(t):this.getLastOutputObject(),n?(n.updateContent(e),n):null}stopLoading(e){let t=this.aiprompt.outputObjects.get(e);t?t.isLoading=!1:(t=this.getLastOutputObject(),t&&(t.isLoading=!1))}stopAllLoading(){this.aiprompt.outputObjects.forEach(e=>{e.isLoading=!1})}getOutputFromElement(t){let n=e(t).closest(`.k-card`).data(`id`),r=this.aiprompt.promptOutputs.find(e=>e.id==n);return!r&&this.aiprompt.outputObjects&&(r=this.aiprompt.outputObjects.get(n)),r}extractOutputData(e){return e?e.data?{prompt:e.data.prompt,output:e.data.output}:{prompt:e.prompt,output:e.output}:{prompt:null,output:null}}destroy(){this.aiprompt&&this.aiprompt.outputObjects&&(this.aiprompt.outputObjects.forEach(e=>{e&&(e._element=null,e._bodyElement=null,e._aiprompt=null,e.data=null)}),this.aiprompt.outputObjects.clear()),this.aiprompt=null}}kendo.ui.AIPromptOutputObject=t,kendo.ui.AIPromptOutputManager=n})(window.kendo.jQuery),(function(e){let t=kendo.ui.Widget,n={ai:`assistant`,system:`system`,user:`user`,tool:`tool`},r={PROMPT_VIEW:`k-prompt-view`,PROMPT_EXPANDER:`k-prompt-expander`,SUGGESTION_GROUP:`k-suggestion-group`,SUGGESTION:`k-suggestion`,CARD:`k-card`,CARD_LIST:`k-card-list`},i={PROMPT_SUGGESTIONS_BUTTON:`ref-prompt-suggestions-button`,PROMPT_INPUT:`ref-prompt-input`,GENERATE_OUTPUT_BUTTON:`ref-generate-output-button`,OUTPUT_CONTENT:`ref-output-content`,STOP_GENERATION_BUTTON:`ref-stop-generation-button`},a=kendo.ui.AIPromptBaseView=t.extend({init:function(e,n){let r=this;t.fn.init.call(r,e,n),r.aiprompt=e.getKendoAIPrompt(),r.contentElement=r.options.contentElement,r.footerElement=r.options.footerElement,r.buttonText=r.options.buttonText,r.buttonIcon=r.options.buttonIcon,r.service=r.options.service},options:{name:`AIPromptBaseView`,buttonText:``,buttonIcon:``},render:function(){let e=this;e._renderContent(),e._renderFooter()},_renderContentElement:function(){let t=this,n=e(`<div></div>`).addClass(`k-prompt-content`);return t.contentElement=n,t.element.append(n),t.contentElement},_renderFooterElement:function(){let t=this,n=e(`<div></div>`).addClass(`k-prompt-footer`);return t.footerElement=n,t.element.append(n),t.footerElement},_ajaxRequest:function(t,n,r){let i=this,a=i.service,o=i._getAjaxData(t,n,r),s={url:typeof a==`string`?a:a.url,type:`POST`,contentType:`application/json`,data:JSON.stringify(o),success:e=>i._ajaxSuccessHandler(e,n,t)};return a?.headers&&(s.headers=a.headers),kendo.ui.progress(i.contentElement,!0),e.ajax(s)},_ajaxSuccessHandler:function(e,t,n){let r=this,a=r.service?.outputGetter||r._getResponseMessageText,o={id:kendo.guid(),output:a(e),prompt:n,isRetry:t,activeView:1};r.aiprompt.trigger(`promptResponse`,{output:o.output,prompt:o.prompt,outputId:o.id,isRetry:o.isRetry,response:o.response}),r.aiprompt.addPromptOutput(o),r.aiprompt.activeView(o.activeView),t||(r.footerElement?.find(`button[${i.GENERATE_OUTPUT_BUTTON}]`))?.removeClass(`k-disabled`),kendo.ui.progress(r.contentElement,!1)},_getResponseMessageText:function(e){return e?.Message?.Text||`An error occurred while processing the request.`},_getAjaxData:function(t,r,i){let a=this.service,o=[{role:{value:n.user},text:t}];if(i?.length&&(o=i.concat(o)),typeof a==`string`)return o;if(kendo.isPresent(a.data)&&Object.keys(a.data).length)return a.data.messages=o,a.data;if(kendo.isFunction(a?.data))return a.data(t,r,i);if(e.isPlainObject(a)&&kendo.isPresent(a.url))return o;throw Error(`Invalid AIPrompt service configuration.`)},destroy:function(){let e=this;t.fn.destroy.call(e),e.contentElement&&(e.contentElement.off(),kendo.destroy(e.contentElement),e.contentElement.remove()),e.footerElement&&(e.footerElement.off(),kendo.destroy(e.footerElement),e.footerElement.remove()),e.aiprompt.speechToTextButton=null}});kendo.ui.AIPromptPromptView=a.extend({init:function(e,t){let n=this;a.fn.init.call(n,e,t),n.promptSuggestions=n.options.promptSuggestions,n.promptSuggestionItemTemplate=n.options.promptSuggestionItemTemplate?kendo.template(n.options.promptSuggestionItemTemplate):kendo.ui.AIPromptTemplateBuilder.createSuggestionItem,n.speechManager=new kendo.ui.AIPromptSpeechManager(n,n.options.speechToText)},options:{name:`AIPromptPromptView`,buttonIcon:`sparkles`},_renderContent:function(){let e=this,t=e.promptSuggestions,n=e.promptSuggestionItemTemplate,r;r=e.options.viewTemplate?kendo.template(e.options.viewTemplate)({suggestions:t,promptSuggestionItemTemplate:n,messages:e.options.messages}):kendo.ui.AIPromptTemplateBuilder.createPromptView({suggestions:t,promptSuggestionItemTemplate:n,messages:e.options.messages}),e._renderContentElement(),e.contentElement.append(r)},_renderFooter:function(){let e=this,t;t=e.options.footerTemplate?kendo.template(e.options.footerTemplate)({messages:e.options.messages}):kendo.ui.AIPromptTemplateBuilder.createPromptFooter({messages:e.options.messages}),e._renderFooterElement(),e.footerElement.append(t)},setTextAreaValue:function(e){let t=this,n=t.contentElement.find(`textarea[${i.PROMPT_INPUT}]`).getKendoTextArea();n?n.value(e):t.contentElement.find(`textarea[${i.PROMPT_INPUT}]`).val(e)},_focusSuggestion(e){let t=this;!e||!e.length||(t.contentElement.find(`.${r.SUGGESTION_GROUP} .${r.SUGGESTION}[tabindex=0]`).attr(`tabindex`,`-1`),e.attr(`tabindex`,`0`).trigger(`focus`))},startSpeechRecognition:function(){this.speechManager.startRecognition()},stopSpeechRecognition:function(){this.speechManager.stopRecognition()},abortSpeechRecognition:function(){this.speechManager.abortRecognition()},isSpeechListening:function(){return this.speechManager.isListening()},initializeComponents:function(){let t=this,n=t.promptSuggestions,a=t.footerElement.find(`button[${i.GENERATE_OUTPUT_BUTTON}]`),o=e.extend({resize:`vertical`,placeholder:t.options.messages.promptPlaceholder},t.options.promptTextArea||{});t.speechManager.isEnabled()&&(o=e.extend(!0,o,t.speechManager.getTextAreaSuffixOptions()));let s=t.contentElement.find(`textarea[${i.PROMPT_INPUT}]`).kendoTextArea(o).getKendoTextArea();if(t.speechManager.isEnabled()&&t.speechManager.initialize(s),a.kendoButton({icon:`sparkles`,themeColor:`primary`,rounded:`full`,click:function(e){let n=t.contentElement.find(`textarea[${i.PROMPT_INPUT}]`).getKendoTextArea(),r={prompt:n?n.value():t.contentElement.find(`textarea[${i.PROMPT_INPUT}]`).val(),isRetry:!1,history:[]};t.service&&(r.service=t.service),!t.aiprompt.trigger(`promptRequest`,r)&&t.service&&t.aiprompt.transport.read({prompt:r.prompt,history:r.history,isRetry:!1,service:t.service})}}),n?.length){t.contentElement.find(`.${r.SUGGESTION_GROUP} .${r.SUGGESTION}`).first().attr(`tabindex`,`0`);let n=kendo.guid(),a=t.contentElement.find(`.${r.PROMPT_EXPANDER} button[${i.PROMPT_SUGGESTIONS_BUTTON}]`);t.contentElement.find(`.${r.PROMPT_EXPANDER} button[${i.PROMPT_SUGGESTIONS_BUTTON}]`).attr(`aria-controls`,n),a.next(`.${r.PROMPT_EXPANDER_CONTENT}`).attr(`id`,n),t.contentElement.find(`.${r.PROMPT_EXPANDER} button[${i.PROMPT_SUGGESTIONS_BUTTON}]`).kendoButton({icon:`chevron-up`,fillMode:`flat`,click:function(t){let n=e(t.target).closest(`.k-prompt-expander`).find(`.k-prompt-expander-content`),r=t.sender.element.find(`.k-icon`);kendo.ui.icon(r,n.is(`:visible`)?`chevron-down`:`chevron-up`),n.toggle(),t.sender.element.attr(`aria-expanded`,n.is(`:visible`))}}),t.contentElement.on(`click`,`.k-suggestion-group .k-suggestion`,function(n){t.setTextAreaValue(e(n.target).text())}),t.contentElement.on(`keydown`,`.k-suggestion-group .k-suggestion`,function(n){if(n.keyCode===40||n.keyCode===38||n.keyCode===36||n.keyCode===35||n.keyCode===13||n.keyCode===32){n.preventDefault();let r=e(n.target),i=r.siblings(),a,o;n.keyCode===40&&(a=r.next(),t._focusSuggestion(a)),n.keyCode===38&&(o=r.prev(),t._focusSuggestion(o)),n.keyCode===36&&(o=i.first(),t._focusSuggestion(o)),n.keyCode===35&&(a=i.last(),t._focusSuggestion(a)),(n.keyCode===13||n.keyCode===32)&&t.setTextAreaValue(e(n.target).text())}})}kendo.isFunction(t.options.initializeComponents)&&t.options.initializeComponents({view:t})},render:function(){let e=this;e._renderContent(),e._renderFooter(),e.initializeComponents()},destroy:function(){let e=this;e.speechManager&&=(e.speechManager.destroy(),null),a.fn.destroy.call(e)}}),kendo.ui.AIPromptOutputView=a.extend({init:function(e,t){let n=this;a.fn.init.call(n,e,t),n.promptOutputs=n.aiprompt&&n.aiprompt.promptOutputs?n.aiprompt.promptOutputs:[],n.showOutputRating=n.options.showOutputRating,n.isStreaming=n.options.isStreaming||!1,n.outputActions=n.options.outputActions,n.outputTemplate=n.options.outputTemplate,n.outputActionManager=n.options.outputActionManager},options:{name:`AIPromptOutputView`,buttonIcon:`comment`,isStreaming:!1,promptOutputs:[]},startStreaming:function(){let e=this;e.isStreaming=!0,e._showStopButton()},stopStreaming:function(){let e=this;e.isStreaming=!1,e._hideStopButton()},_showStopButton:function(){let e=this;(e.stopGenerationButton||e._initStopGenerationButton())&&e.stopGenerationButton.show()},_hideStopButton:function(){let e=this;e.stopGenerationButton&&e.stopGenerationButton.hide()},renderPromptOutput:function(t){let n=this,r=n.options.showOutputRating,i=n.options.encodedPromptOutputs,a=n.options.messages,o=n.isStreaming||!1,s=n.outputActions;if((!n.cardListContainer||n.cardListContainer.length===0)&&(n.outputsContainer&&(n.cardListContainer=n.outputsContainer.find(`.k-card-list`)),!n.cardListContainer||n.cardListContainer.length===0))return;o&&!n.stopGenerationButton&&n._initStopGenerationButton();let c=n.aiprompt.outputObjects.get(t.id);if(c){let l=e(kendo.ui.AIPromptTemplateBuilder.createOutputCard({output:t,showOutputRating:r,messages:a,showOutputSubtitleTooltip:!0,encodedPromptOutputs:i,isStreaming:o,outputActions:s,outputTemplate:n.outputTemplate}));c._element=l,c._bodyElement=l.find(`.k-card-body`),n.cardListContainer.prepend(l),t.isLoading?c.showSkeleton():t.output&&c.applyFinalTemplate(),n.initializeComponents(l)}else{let c=e(kendo.ui.AIPromptTemplateBuilder.createOutputCard({output:t,showOutputRating:r,messages:a,showOutputSubtitleTooltip:!0,encodedPromptOutputs:i,isStreaming:o,outputActions:s,outputTemplate:n.outputTemplate}));n.cardListContainer.prepend(c),n.initializeComponents(c)}},updatePromptOutputContent:function(e,t){let n=this.aiprompt.outputObjects.get(e);n&&n.updateContent(t)},_initStopGenerationButton:function(){let t=this,n=t.contentElement;if(!n||n.length===0)return!1;if(t.stopGenerationButton)return!0;let r=e(`<button class='k-prompt-stop-fab k-generating'></button>`);return r.attr({"aria-label":t.options.messages.stopGeneration,title:t.options.messages.stopGeneration}),n.prepend(r),t.stopGenerationButton=r.kendoFloatingActionButton({_classNames:[`k-prompt-stop-fab`,`k-generating`],icon:`stop-sm`,positionMode:`absolute`,align:`bottom end`,rounded:`full`,click:function(e){t.stopStreaming(),t.aiprompt.trigger(`promptRequestCancel`,{})}}).getKendoFloatingActionButton(),t.stopGenerationButton.hide(),!0},_renderContent:function(){let t=this,n=t.promptOutputs,r=t.options.showOutputRating,i=t.options.showOutputSubtitleTooltip,a=t.options.messages,o=t.options.encodedPromptOutputs,s=t.outputActions,c;c=t.viewTemplate?kendo.template(t.viewTemplate)({promptOutputs:n,showOutputRating:r,messages:a,showOutputSubtitleTooltip:i,encodedPromptOutputs:o,outputActions:s,outputTemplate:t.outputTemplate}):kendo.ui.AIPromptTemplateBuilder.createOutputView({promptOutputs:n,showOutputRating:r,messages:a,showOutputSubtitleTooltip:i,encodedPromptOutputs:o,outputActions:s,outputTemplate:t.outputTemplate}),t.outputsContainer=e(c),t.cardListContainer=t.outputsContainer.find(`.k-card-list`),t._renderContentElement(),t.contentElement.append(t.outputsContainer),t._initStopGenerationButton()},initializeComponents:function(e){let t=this;e||=t.contentElement,t.outputActionManager.initializeButtons(e,t.outputActions),e.find(`[data-loading="true"]`).hide(),e.find(`[data-loading="false"]`).show(),t.aiprompt&&t.aiprompt.outputObjects&&t.aiprompt.outputObjects.forEach((n,r)=>{let i=e.find(`.k-card[data-id="${r}"]`);if(i.length>0){if(n._element=i,n._bodyElement=i.find(`.k-card-body`),t.outputTemplate&&typeof t.outputTemplate==`function`&&n.data&&n.data.output&&!n.data.isLoading){let e=t.outputTemplate({output:n.data,content:n.data.output});n._bodyElement.html(e)}let e=i.find(`.k-button`);!(e.length>0&&e.first().data(`kendoButton`))&&e.length>0&&t.outputActionManager.initializeButtons(i,t.outputActions)}})},_initializeCardButtons:function(e){let t=this;t.outputActionManager.initializeButtons(e,t.outputActions)},render:function(){let t=this;t._renderContent(),t.initializeComponents(),t.contentElement.on(`keydown`,`.k-card`,function(n){let r=e(n.target);(n.keyCode===40||n.keyCode===38||n.keyCode===36||n.keyCode===35)&&(n.preventDefault(),n.keyCode===40&&r.next(`.k-card`).trigger(`focus`),n.keyCode===38&&r.prev(`.k-card`).trigger(`focus`),n.keyCode===36&&t.contentElement.find(`.k-card`).first().trigger(`focus`),n.keyCode===35&&t.contentElement.find(`.k-card`).last().trigger(`focus`))})},destroy:function(){let e=this;a.fn.destroy.call(e)}}),kendo.ui.AIPromptCommandsView=a.extend({options:{name:`AIPromptCommandsView`,buttonText:``,buttonIcon:`more-horizontal`,promptCommands:[]},initializeComponents:function(){let t=this,n=t.options.promptCommands,r=e(`<div></div>`).kendoPanelBar({animation:!1,dataSource:n,selectable:!1,select:function(n){let r=e(n.item),i=this.dataItem(r);i.hasChildren||t.aiprompt.trigger(`commandExecute`,{sender:t.aiprompt,item:i})}}),i=e(`<div class='k-prompt-view'>`);i.append(r),t.contentElement.append(i)},render:function(){let e=this;e._renderContentElement(),e.initializeComponents()}});let o=()=>``;kendo.ui.AIPromptCustomView=a.extend({options:{name:`AIPromptCustomView`,buttonText:``,buttonIcon:``,viewTemplate:o,footerTemplate:o},initializeComponents:function(){let e=this;typeof e.options.initializeComponents==`function`&&e.options.initializeComponents.call(e)},_renderContent:function(){let e=this,t=kendo.template(e.options.viewTemplate)({aiprompt:e});e._renderContentElement(),e.contentElement.append(t)},_renderFooter:function(){let e=this;if(e.options.footerTemplate===o)return;let t=kendo.template(e.options.footerTemplate)({messages:e.options.messages});e._renderFooterElement(),e.footerElement.append(t)},render:function(){let e=this;e._renderContent(),e._renderFooter(),e.initializeComponents()}})})(window.kendo.jQuery),kendo;let d={id:`aiprompt`,name:`AIPrompt`,category:`web`,description:`The AIPrompt component simplifies the incorporation of external AI services into apps.`,depends:[`core`,`icons`,`textarea`,`button`,`toolbar`,`panelbar`,`data`,`floatingactionbutton`,`skeletoncontainer`,`speechtotextbutton`]};(function(e){let t=window.kendo,n=t.ui.Widget,r=t.ui,i=e.extend,a=`promptResponse`,o=[`copy`,`retry`,`spacer`,`rating`],s=[`copy`,`retry`],c=`focus`,l={menuButton:`k-menu-button`,aIPrompt:`k-prompt`},u={prompt:{type:`kendo.ui.AIPromptPromptView`,name:`prompt`,buttonIcon:`sparkles`},output:{type:`kendo.ui.AIPromptOutputView`,name:`output`,buttonIcon:`comment`},commands:{type:`kendo.ui.AIPromptCommandsView`,name:`commands`,buttonIcon:`more-horizontal`},custom:{type:`kendo.ui.AIPromptCustomView`,name:`custom`}},d=n.extend({init:function(e,r){let i=this;r||={},n.fn.init.call(i,e,r),i.options.views.length==0&&(i.options.views=[`prompt`,`output`],this.options.promptCommands&&this.options.promptCommands.length&&this.options.views.push(`commands`)),i.options.outputActions=r.outputActions&&r.outputActions.length>0?r.outputActions:s,i.promptOutputs=i.options.promptOutputs||[],i.outputObjects=new Map,i.outputManager=new t.ui.AIPromptOutputManager(i),i.outputActions=t.ui.AIPromptOutputActionManager.processOutputActions(i.options.outputActions),i.outputActionManager=new t.ui.AIPromptOutputActionManager(i,{outputActions:i.outputActions}),Array.isArray(i.promptOutputs)&&i.promptOutputs.length>0&&i.promptOutputs.forEach(e=>{e.id||=t.guid();let n=i.outputManager.createOutputObject(e);i.outputObjects.set(e.id,n)}),i._initLayout(),i._initViews(),i._initToolbar(),i.activeView(i.options.activeView),i.options.service&&(i.transport=new t.data.AiTransport({service:i.options.service,success:i._serviceSuccess.bind(i),requestStart:()=>t.ui.progress(i.element,!0)})),t.notify(i)},options:{name:`AIPrompt`,enabled:!0,toolbarItems:[],promptOutputs:[],encodedPromptOutputs:!0,activeView:0,views:[],popup:null,speechToText:!1,promptTextArea:null,messages:{promptView:`Ask AI`,outputView:`Output`,commandsView:``,customView:`Custom View`,promptPlaceholder:`Ask or generate content with AI`,promptSuggestions:`Prompt Suggestions`,generateOutput:`Generate`,outputTitle:`Generated with AI`,outputRetryTitle:`Generated with AI`,copyOutput:`Copy`,retryGeneration:`Retry`,ratePositive:``,rateNegative:``,stopGeneration:`Stop Generation`},showOutputRating:!0,service:null,suffixTemplate:null,outputTemplate:null,outputActions:o},events:[`commandExecute`,`promptRequest`,a,`promptRequestCancel`,`outputRating`,`outputCopy`,`outputAction`],_serviceSuccess:function(e){let n=this;e.activeView=n.viewsArray.findIndex(e=>e.name===`output`),n.trigger(a,{output:e.output,prompt:e.prompt,outputId:e.id,isRetry:e.isRetry||!1,response:e.response})||(n.addPromptOutput(e),n.activeView(e.activeView),t.ui.progress(n.element,!1))},_initializeView:function(e){let n=this.views[e],r;if(n){let e=n.type;if(typeof e==`string`&&(e=t.getter(n.type)(window)),e)r=new e(this.element,i(!0,{promptSuggestions:this.options.promptSuggestions,promptCommands:this.options.promptCommands,promptOutputs:this.promptOutputs,showOutputRating:this.showOutputRating,messages:this.options.messages,showOutputSubtitleTooltip:this.options.showOutputSubtitleTooltip,encodedPromptOutputs:this.options.encodedPromptOutputs,promptSuggestionItemTemplate:this.options.promptSuggestionItemTemplate,service:this.options.service,speechToText:this.options.speechToText,promptTextArea:this.options.promptTextArea,outputActions:this.outputActions,outputActionManager:this.outputActionManager,outputTemplate:this.options.outputTemplate},n));else throw Error(`There is no such view`)}return r},_unbindView:function(e){e&&e.destroy()},_initViews:function(){let e=this,t=e.options.views;e.views={},e.viewsArray=[];for(let n=0,r=t.length;n<r;n++){let r=t[n],i=typeof r==`object`,a=r;i&&(r={...r},a=typeof r.type==`string`?r.type:r.name);let o=u[a];o&&(i&&(r.type=o.type),o.buttonText=e.options.messages[`${a}View`]),r=Object.assign({title:r.title,name:a,index:n},o,i?r:{}),e.viewsArray.push(r),a&&(e.views[a]=r)}},getViews:function(){return this.viewsArray},activeView:function(t){let n=this;if(t===void 0)return n._activeViewIndex;if(Number.isInteger(t)&&(t=n.viewsArray[t].name),t&&n.views[t]){n._selectedView&&n._unbindView(n._selectedView),n._selectedView=n._initializeView(t),n._activeViewIndex=n.viewsArray.findIndex(e=>e.name===t),n._selectedView.render(),n._updateToolbarState(n._activeViewIndex);let r=e(n.toolbar._getAllItems()[n._activeViewIndex]);n.toolbar._resetTabIndex(r),r.trigger(c)}},addPromptOutput:function(e){typeof e==`string`&&(e={output:e}),e.id=e.id||t.guid();let n=this.outputManager.createOutputObject(e);this.promptOutputs.unshift(e),this.outputObjects.set(e.id,n),this._selectedView&&typeof this._selectedView.renderPromptOutput==`function`&&(this._selectedView.renderPromptOutput(e),e.isLoading&&this.startStreaming())},removePromptOutput:function(e){let t=this,n=e,r=e=>{t.promptOutputs.find(t=>t.id===e)&&(t.promptOutputs=t.promptOutputs.filter(t=>t.id!==e)),t.outputObjects.delete(e)};n instanceof jQuery?(n=e.data(`id`),e.remove(),r(n)):(t.element.find(`[data-id='`+n+`']`).remove(),r(n))},clearOutput:function(){let t=this,n=t.element.find(`[data-id]`);n.each(function(){let n=e(this).data(`id`);t.outputObjects.delete(n)}),n.remove(),t.promptOutputs=[]},_updateToolbarState:function(t){let n=this.toolbar;n.element.find(`.k-toolbar-toggle-button`).each(function(r,i){n.toggle(e(i),r==t)})},_initLayout:function(){let t=this,n=e(`<div></div>`).addClass(`k-prompt-header`);t.header=n,t.element.addClass(l.aIPrompt),t.element.append(n);let r=t.element.closest(`.k-popup`);r.length&&e(r).addClass(`k-prompt-popup`)},_getViewTools:function(){let e=this;return e.viewsArray.map(t=>(t.name===`commands`&&(t.buttonText?t.title=t.buttonText:t.title=`More Actions`),{type:`button`,text:t.buttonText,icon:t.buttonIcon,fillMode:`flat`,themeColor:t.themeColor||`primary`,rounded:`full`,togglable:!0,attributes:{title:t.title},toggle:function(){e.activeView(t.name)}}))},_initToolbar:function(){let t=this,n=t.options.toolbarItems;n=Array.isArray(n)?n:[n];let r=n.find(e=>e.icon===`x`);r&&(r.themeColor=`base`),t.toolbar=e(`<div></div>`).kendoToolBar({resizable:!1,fillMode:`flat`,items:t._getViewTools().concat(n)}).appendTo(t.header).data(`kendoToolBar`)},focus:function(){this.element.trigger(c)},updatePromptOutputContent:function(e,t){return this.outputManager.updatePromptOutputContent(e,t)},startStreaming:function(){let e=this;e._selectedView&&typeof e._selectedView.startStreaming==`function`&&e._selectedView.startStreaming()},stopStreaming:function(){let e=this;e._selectedView&&typeof e._selectedView.stopStreaming==`function`&&e._selectedView.stopStreaming(),e.outputManager&&e.outputManager.stopAllLoading()},setOptions:function(n){let r=this,a=r.element,o=r.options,s=i({},o,n);t.destroy(a),e(a).empty(),r.init(a,s)},destroy:function(){let e=this;e.toolbar&&=(e.toolbar.destroy(),null),e._selectedView&&=(e._selectedView.destroy(),null),e.outputManager&&=(e.outputManager.destroy(),null),e.outputActionManager&&=(e.outputActionManager.destroy(),null),e.promptOutputs=null,e.outputObjects&&=(e.outputObjects.clear(),null),e.transport&&=null,e.element.off(`.kendoAIPrompt`),n.fn.destroy.call(e)}});r.plugin(d)})(window.kendo.jQuery);var f=kendo;e.__meta__=d,e.default=f});
//# sourceMappingURL=kendo.aiprompt.min.js.map