@sisense/sdk-ui 2.18.0 → 2.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai.cjs +1 -1
- package/dist/ai.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +3 -3
- package/dist/index.umd.js +3 -3
- package/dist/{use-common-filters-BGLYpqmt.cjs → use-common-filters-Cps0frVZ.cjs} +2 -2
- package/dist/{use-common-filters-BhFFlOqU.js → use-common-filters-Db1VFZvT.js} +4 -4
- package/package.json +8 -8
package/dist/ai.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var _t=Object.defineProperty,Kt=Object.defineProperties;var Xt=Object.getOwnPropertyDescriptors;var Ce=Object.getOwnPropertySymbols;var Ue=Object.prototype.hasOwnProperty,_e=Object.prototype.propertyIsEnumerable;var ke=(e,t,r)=>t in e?_t(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,j=(e,t)=>{for(var r in t||(t={}))Ue.call(t,r)&&ke(e,r,t[r]);if(Ce)for(var r of Ce(t))_e.call(t,r)&&ke(e,r,t[r]);return e},P=(e,t)=>Kt(e,Xt(t));var we=(e,t)=>{var r={};for(var s in e)Ue.call(e,s)&&t.indexOf(s)<0&&(r[s]=e[s]);if(e!=null&&Ce)for(var s of Ce(e))t.indexOf(s)<0&&_e.call(e,s)&&(r[s]=e[s]);return r};var N=(e,t,r)=>ke(e,typeof t!="symbol"?t+"":t,r);var Z=(e,t,r)=>new Promise((s,a)=>{var d=u=>{try{h(r.next(u))}catch(g){a(g)}},l=u=>{try{h(r.throw(u))}catch(g){a(g)}},h=u=>u.done?s(u.value):Promise.resolve(u.value).then(d,l);h((r=r.apply(e,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./use-common-filters-BGLYpqmt.cjs"),n=require("./derive-chart-family-C3PTAfzX.cjs"),f=require("react"),J=require("./execute-query-tRu-NooT.cjs"),de=require("./widget-composer-DsBF8OC2.cjs"),ie=require("react/jsx-runtime");function Jt(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const G=Jt(f);class Yt extends o.Subscribable{constructor(t,r){super(),this.client=t,this.setOptions(r),this.bindMethods(),this.updateResult()}bindMethods(){this.mutate=this.mutate.bind(this),this.reset=this.reset.bind(this)}setOptions(t){var r;const s=this.options;this.options=this.client.defaultMutationOptions(t),o.shallowEqualObjects(s,this.options)||this.client.getMutationCache().notify({type:"observerOptionsUpdated",mutation:this.currentMutation,observer:this}),(r=this.currentMutation)==null||r.setOptions(this.options)}onUnsubscribe(){if(!this.hasListeners()){var t;(t=this.currentMutation)==null||t.removeObserver(this)}}onMutationUpdate(t){this.updateResult();const r={listeners:!0};t.type==="success"?r.onSuccess=!0:t.type==="error"&&(r.onError=!0),this.notify(r)}getCurrentResult(){return this.currentResult}reset(){this.currentMutation=void 0,this.updateResult(),this.notify({listeners:!0})}mutate(t,r){return this.mutateOptions=r,this.currentMutation&&this.currentMutation.removeObserver(this),this.currentMutation=this.client.getMutationCache().build(this.client,P(j({},this.options),{variables:typeof t!="undefined"?t:this.options.variables})),this.currentMutation.addObserver(this),this.currentMutation.execute()}updateResult(){const t=this.currentMutation?this.currentMutation.state:o.getDefaultState(),r=t.status==="loading",s=P(j({},t),{isLoading:r,isPending:r,isSuccess:t.status==="success",isError:t.status==="error",isIdle:t.status==="idle",mutate:this.mutate,reset:this.reset});this.currentResult=s}notify(t){o.notifyManager.batch(()=>{if(this.mutateOptions&&this.hasListeners()){if(t.onSuccess){var r,s,a,d;(r=(s=this.mutateOptions).onSuccess)==null||r.call(s,this.currentResult.data,this.currentResult.variables,this.currentResult.context),(a=(d=this.mutateOptions).onSettled)==null||a.call(d,this.currentResult.data,null,this.currentResult.variables,this.currentResult.context)}else if(t.onError){var l,h,u,g;(l=(h=this.mutateOptions).onError)==null||l.call(h,this.currentResult.error,this.currentResult.variables,this.currentResult.context),(u=(g=this.mutateOptions).onSettled)==null||u.call(g,void 0,this.currentResult.error,this.currentResult.variables,this.currentResult.context)}}t.listeners&&this.listeners.forEach(({listener:m})=>{m(this.currentResult)})})}}function Ie(e,t,r){const s=o.parseMutationArgs(e,t),a=o.useQueryClient({context:s.context}),[d]=G.useState(()=>new Yt(a,s));G.useEffect(()=>{d.setOptions(s)},[d,s]);const l=o.useSyncExternalStore(G.useCallback(u=>d.subscribe(o.notifyManager.batchCalls(u)),[d]),()=>d.getCurrentResult(),()=>d.getCurrentResult()),h=G.useCallback((u,g)=>{d.mutate(u,g).catch(er)},[d]);if(l.error&&o.shouldThrowError(d.options.useErrorBoundary,[l.error]))throw l.error;return P(j({},l),{mutate:h,mutateAsync:l.mutate})}function er(){}const at=f.createContext({}),tr=()=>f.useContext(at),lt=({children:e})=>{const[t,r]=f.useState({}),s=f.useCallback((l,h)=>{var u;return`${l}_${(u=h==null?void 0:h.dashboardId)!=null?u:""}`},[]),a=f.useCallback((l,h,u)=>{r(g=>P(j({},g),{[s(h,u)]:l}))},[s]),d=f.useCallback((l,h)=>t[s(l,h)],[t,s]);return n.jsx(at.Provider,{value:{saveChatId:a,getChatId:d},children:e})};class ct{constructor(t,r=!1){N(this,"httpClient");N(this,"getChatContexts",()=>Z(this,null,function*(){return(yield this.httpClient.get("api/datasources//"))||[]}));N(this,"getNlgInsights",t=>this.httpClient.post("api/v2/ai/nlg/queryResult",t));N(this,"getQueryRecommendations",(t,r)=>this.httpClient.get(`api/v2/ai/recommendations/query/${t}/${r.numOfRecommendations}${r.userPrompt?"?userPrompt="+encodeURIComponent(r.userPrompt):""}`));N(this,"getNlqResult",(t,r)=>this.httpClient.post(`api/v2/ai/nlq/query/${t}`,r));N(this,"setLlmConfig",t=>this.httpClient.post("api/v2/settings/ai/llmProvider",t).catch(r=>{console.error("Unable to set llm config",r)}));N(this,"getAllChats",()=>Z(this,null,function*(){return this.volatile?[]:this.httpClient.get("api/v2/ai/chats")}));N(this,"getChatById",t=>this.httpClient.get(`api/v2/ai/chats/${t}`));N(this,"createChat",(t,r)=>this.httpClient.post("api/v2/ai/chats",{sourceId:t,volatile:this.volatile,contextDetails:r}));N(this,"postChat",(t,r)=>this.httpClient.post(`api/v2/ai/chats/${t}`,r));N(this,"deleteChatHistory",t=>this.httpClient.delete(`api/v2/ai/chats/${t}/history`));N(this,"sendFeedback",t=>this.httpClient.post("api/v2/ai/feedback",t));N(this,"ai",{getNlgInsights:this.getNlgInsights,getNlqResult:this.getNlqResult,getQueryRecommendations:this.getQueryRecommendations,setLlmConfig:this.setLlmConfig,sendFeedback:this.sendFeedback,chat:{getAll:this.getAllChats,getById:this.getChatById,create:this.createChat,post:this.postChat,clearHistory:this.deleteChatHistory}});this.volatile=r,this.httpClient=t}}const rr=new o.QueryClient({defaultOptions:{queries:{refetchOnWindowFocus:!1,refetchOnMount:!1}}}),Ne=f.createContext(void 0),Q=()=>f.useContext(Ne),nr=({children:e,volatile:t})=>{const{app:r}=o.useSisenseContext(),s=f.useMemo(()=>r?new ct(r.httpClient,t):void 0,[r,t]);return n.jsx(Ne.Provider,{value:s,children:n.jsx(o.QueryClientProvider,{client:rr,children:e})})};function or({children:e,volatile:t}){return n.jsx(lt,{children:n.jsx(nr,{volatile:t,children:e})})}function Fe(e){var t,r;if("jaql"in e)return e;{const s=J.convertJaqlDataSource(e.dataSource),{filters:a=[],relations:d}=J.getFilterListAndRelationsJaql(e.filters),{metadata:l,filterRelations:h}=J.getJaqlQueryPayload({dataSource:e.dataSource,attributes:(t=e.dimensions)!=null?t:[],measures:(r=e.measures)!=null?r:[],filters:a,filterRelations:d,highlights:[]},!0),u={jaql:{datasource:s,metadata:l,filterRelations:h}};return e.verbosity&&(u.verbosity=e.verbosity),u}}const ye=(e,t=!0)=>{const r=f.useMemo(()=>Fe(e),[e]),s=Q(),{data:a,error:d,isError:l,isLoading:h,isSuccess:u,refetch:g}=o.useQuery({queryKey:["getNlgInsights",r,s],queryFn:()=>s==null?void 0:s.ai.getNlgInsights(r),select:m=>{var b;return(b=m==null?void 0:m.data)==null?void 0:b.answer},enabled:!!s&&t});return{isLoading:h,isError:l,isSuccess:u,data:a,error:d,refetch:f.useCallback(()=>{g()},[g])}},sr=e=>{const s=e,{enabled:t}=s,r=we(s,["enabled"]);return ye(r,t)},ir=o.withTracking("useGetNlgInsights")(sr),dt=["indicator","column","line","table","pie","bar"];function Oe(e){const{dataSource:t,query:r,chartTypes:s}=e;return{contextTitle:typeof t=="string"?t:t.title,request:{text:r,timezone:"UTC",chartTypes:s!=null?s:[...dt]}}}function ut(e,t){return Z(this,null,function*(){const{contextTitle:r,request:s}=Oe(e),a=yield t.ai.getNlqResult(r,s);return{widgetProps:a?de.toWidgetProps(a,{useCustomizedStyleOptions:e.enableAxisTitlesInWidgetProps||!1}):void 0,nlqResult:a}})}const ar=e=>{const{contextTitle:t,request:r}=Oe(e),s=Q(),a=["getNlqResult",t,r,s],{isLoading:d,isError:l,isSuccess:h,data:u,error:g,refetch:m}=o.useQuery({queryKey:a,queryFn:()=>s?ut(e,s).then(b=>b.widgetProps):Promise.reject("No API available"),enabled:!!s&&e.enabled});return{isLoading:d,isError:l,isSuccess:h,data:u,error:g,refetch:f.useCallback(()=>{m()},[m])}},lr=o.withTracking("useGetNlqResult")(ar),pt=4,ht=e=>{const{contextTitle:t,count:r,enableAxisTitlesInWidgetProps:s,enabled:a,customPrompt:d}=e,l=Q(),h=r!=null?r:pt,u=(a===void 0||a===!0)&&h>0,{isLoading:g,isError:m,isSuccess:b,data:i,error:p,refetch:c}=o.useQuery({queryKey:["getQueryRecommendations",t,h,l],queryFn:()=>l==null?void 0:l.ai.getQueryRecommendations(t,j({numOfRecommendations:h},d?{userPrompt:d}:null)),enabled:!!l&&u}),x=f.useMemo(()=>i==null?void 0:i.map(C=>P(j({},C),{widgetProps:C.jaql?de.toWidgetProps(C,{useCustomizedStyleOptions:s}):void 0})),[i,s]);return{isLoading:u?g:!1,isError:m,isSuccess:b,data:x!=null?x:void 0,error:p,refetch:f.useCallback(()=>{c()},[c])}},cr=o.withTracking("useGetQueryRecommendations")(ht),gt=Object.freeze({enableFollowupQuestions:!1,enableHeader:!0,enableInsights:!0,numOfRecommendations:4,numOfRecentPrompts:5}),Ct=f.createContext(j({},gt)),U=()=>f.useContext(Ct),dr=({children:e,value:t})=>{const{t:r}=n.useTranslation(),s=Object.entries(t).reduce((a,[d,l])=>(l!==void 0&&(a[d]=l),a),P(j({},gt),{inputPromptText:r("ai.config.inputPromptText"),welcomeText:r("ai.config.welcomeText"),suggestionsWelcomeText:r("ai.config.suggestionsWelcomeText")}));return n.jsx(Ct.Provider,{value:s,children:e})},Be="chatHistory",ur=e=>{const t=Q(),{hideHistory:r}=U();return o.useQuery({queryKey:[Be,e,t],queryFn:()=>Z(exports,null,function*(){if(!t||!e)return;if(r)return[];const s=yield t.ai.chat.getById(e);return s==null?void 0:s.chatHistory}),enabled:!!t&&!!e})},ft=e=>{const t=Q(),r=o.useQueryClient();return Ie({mutationFn:()=>Z(exports,null,function*(){if(!(!t||!e))return t.ai.chat.clearHistory(e)}),onError:s=>{console.error("Error when clearing history:",s)},onSettled:()=>{e&&r.setQueriesData([Be,e],[])}})},He=e=>{const{data:t,isLoading:r,isError:s}=ur(e),{mutate:a}=ft(e);return f.useEffect(()=>{s&&a()},[s,a]),{history:t,isLoading:r,isError:s}},pr=()=>{const e=Q(),{data:t,isLoading:r,fetchStatus:s}=o.useQuery({queryKey:["getDataTopics",e],queryFn:()=>Z(exports,null,function*(){if(e)return e.getChatContexts()}),enabled:!!e});return{data:t,isLoading:r,fetchStatus:s}},hr=()=>{const e=Q(),{data:t,isLoading:r}=o.useQuery({queryKey:["getAllChats",e],queryFn:()=>e==null?void 0:e.ai.chat.getAll(),enabled:!!e});return{data:t!=null?t:[],isLoading:r}},gr=(e,t,r)=>{const s=o.useQueryClient(),a=Q(),d=Ie({mutationFn:()=>Z(exports,null,function*(){if(!(!a||!e))return a.ai.chat.create(e,r)}),onSuccess:()=>s.invalidateQueries(["getAllChats"])});return f.useEffect(()=>{t&&d.isIdle&&d.mutate()},[t,d]),d},Cr=(e,t)=>{switch(e.responseType=e.responseType.toLowerCase(),e.responseType){case"nlq":return{content:JSON.stringify(e.data),role:"assistant",type:"nlq"};case"text":case"error":return{content:e.data.answer,role:"assistant"};default:throw Error(`${t}${JSON.stringify(e)}`)}},fr=(e,t)=>{const r=o.useQueryClient(),s=f.useCallback(u=>{e&&r.setQueriesData([Be,e],g=>g&&[...g,u])},[r,e]),{t:a}=n.useTranslation(),d=Q(),{mutate:l,isLoading:h}=Ie({mutationFn:u=>Z(exports,null,function*(){if(!(!d||!e))return d.ai.chat.post(e,{text:u,options:{enableFollowup:t}})}),onMutate:u=>{s({content:u,role:"user"})},onError:u=>{u instanceof Error&&(console.error("Error when sending message:",u.message),s({content:a(`ai.errors.${u.interpolationOptions.errorCode}`,{defaultValue:a("ai.errors.unexpectedChatResponse")}),role:"assistant"}))},onSuccess:u=>{if(!u)return;const g=a("ai.errors.unknownResponse");s(Cr(u,g))}});return{mutate:l,isLoading:h}},De=(e,t,r=!1)=>{var m;const{data:s,isLoading:a}=hr(),d=tr(),l=d.getChatId(e,t)||((m=s.find(b=>{var i;return b.contextTitle===e&&((i=b.contextDetails)==null?void 0:i.dashboardId)===(t==null?void 0:t.dashboardId)}))==null?void 0:m.chatId),{isError:h,data:u,isSuccess:g}=gr(e,r&&!a&&!l,t);return f.useEffect(()=>{g&&u&&!l&&d.saveChatId(u.chatId,e,t)},[u,g,e,t,l,d]),{chatId:l,isLoading:a,isError:h}},xt=e=>f.useMemo(()=>{if(e!=null&&e.length){const t=e[e.length-1];if(Ae(t))return JSON.parse(t.content)}return null},[e]),Ae=e=>!!e&&"type"in e&&e.type==="nlq",xr=e=>!!e&&"type"in e&&e.type==="text",mt=(e,t)=>{const{t:r}=n.useTranslation(),{enableFollowupQuestions:s}=U(),{chatId:a,isError:d}=De(e,t,!0),[l,h]=f.useState(null),{history:u,isLoading:g,isError:m}=He(a);f.useEffect(()=>{d?h(new J.TranslatableError("ai.errors.chatUnavailable")):m&&h(new J.TranslatableError("ai.errors.fetchHistory"))},[d,m,r]);const{mutate:b,isLoading:i}=fr(a,s),p=xt(u);return{chatId:a,history:u!=null?u:[],lastNlqResponse:p,sendMessage:b,isAwaitingResponse:i,isLoading:g,lastError:l}},mr=({contextTitle:e,contextDetails:t})=>{const{chatId:r}=De(e,t),{history:s}=He(r);return xt(s)},yr=({contextTitle:e,contextDetails:t})=>{const{chatId:r}=De(e,t),{history:s}=He(r);return f.useMemo(()=>{if(s!=null&&s.length){const a=s.slice().reverse().find(Ae);if(a)return JSON.parse(a.content)}return null},[s])},br=500,vr=500,kr=500,wr=900,jr=o.styled.div`
|
|
1
|
+
"use strict";var _t=Object.defineProperty,Kt=Object.defineProperties;var Xt=Object.getOwnPropertyDescriptors;var Ce=Object.getOwnPropertySymbols;var Ue=Object.prototype.hasOwnProperty,_e=Object.prototype.propertyIsEnumerable;var ke=(e,t,r)=>t in e?_t(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,j=(e,t)=>{for(var r in t||(t={}))Ue.call(t,r)&&ke(e,r,t[r]);if(Ce)for(var r of Ce(t))_e.call(t,r)&&ke(e,r,t[r]);return e},P=(e,t)=>Kt(e,Xt(t));var we=(e,t)=>{var r={};for(var s in e)Ue.call(e,s)&&t.indexOf(s)<0&&(r[s]=e[s]);if(e!=null&&Ce)for(var s of Ce(e))t.indexOf(s)<0&&_e.call(e,s)&&(r[s]=e[s]);return r};var N=(e,t,r)=>ke(e,typeof t!="symbol"?t+"":t,r);var Z=(e,t,r)=>new Promise((s,a)=>{var d=u=>{try{h(r.next(u))}catch(g){a(g)}},l=u=>{try{h(r.throw(u))}catch(g){a(g)}},h=u=>u.done?s(u.value):Promise.resolve(u.value).then(d,l);h((r=r.apply(e,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./use-common-filters-Cps0frVZ.cjs"),n=require("./derive-chart-family-C3PTAfzX.cjs"),f=require("react"),J=require("./execute-query-tRu-NooT.cjs"),de=require("./widget-composer-DsBF8OC2.cjs"),ie=require("react/jsx-runtime");function Jt(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const G=Jt(f);class Yt extends o.Subscribable{constructor(t,r){super(),this.client=t,this.setOptions(r),this.bindMethods(),this.updateResult()}bindMethods(){this.mutate=this.mutate.bind(this),this.reset=this.reset.bind(this)}setOptions(t){var r;const s=this.options;this.options=this.client.defaultMutationOptions(t),o.shallowEqualObjects(s,this.options)||this.client.getMutationCache().notify({type:"observerOptionsUpdated",mutation:this.currentMutation,observer:this}),(r=this.currentMutation)==null||r.setOptions(this.options)}onUnsubscribe(){if(!this.hasListeners()){var t;(t=this.currentMutation)==null||t.removeObserver(this)}}onMutationUpdate(t){this.updateResult();const r={listeners:!0};t.type==="success"?r.onSuccess=!0:t.type==="error"&&(r.onError=!0),this.notify(r)}getCurrentResult(){return this.currentResult}reset(){this.currentMutation=void 0,this.updateResult(),this.notify({listeners:!0})}mutate(t,r){return this.mutateOptions=r,this.currentMutation&&this.currentMutation.removeObserver(this),this.currentMutation=this.client.getMutationCache().build(this.client,P(j({},this.options),{variables:typeof t!="undefined"?t:this.options.variables})),this.currentMutation.addObserver(this),this.currentMutation.execute()}updateResult(){const t=this.currentMutation?this.currentMutation.state:o.getDefaultState(),r=t.status==="loading",s=P(j({},t),{isLoading:r,isPending:r,isSuccess:t.status==="success",isError:t.status==="error",isIdle:t.status==="idle",mutate:this.mutate,reset:this.reset});this.currentResult=s}notify(t){o.notifyManager.batch(()=>{if(this.mutateOptions&&this.hasListeners()){if(t.onSuccess){var r,s,a,d;(r=(s=this.mutateOptions).onSuccess)==null||r.call(s,this.currentResult.data,this.currentResult.variables,this.currentResult.context),(a=(d=this.mutateOptions).onSettled)==null||a.call(d,this.currentResult.data,null,this.currentResult.variables,this.currentResult.context)}else if(t.onError){var l,h,u,g;(l=(h=this.mutateOptions).onError)==null||l.call(h,this.currentResult.error,this.currentResult.variables,this.currentResult.context),(u=(g=this.mutateOptions).onSettled)==null||u.call(g,void 0,this.currentResult.error,this.currentResult.variables,this.currentResult.context)}}t.listeners&&this.listeners.forEach(({listener:m})=>{m(this.currentResult)})})}}function Ie(e,t,r){const s=o.parseMutationArgs(e,t),a=o.useQueryClient({context:s.context}),[d]=G.useState(()=>new Yt(a,s));G.useEffect(()=>{d.setOptions(s)},[d,s]);const l=o.useSyncExternalStore(G.useCallback(u=>d.subscribe(o.notifyManager.batchCalls(u)),[d]),()=>d.getCurrentResult(),()=>d.getCurrentResult()),h=G.useCallback((u,g)=>{d.mutate(u,g).catch(er)},[d]);if(l.error&&o.shouldThrowError(d.options.useErrorBoundary,[l.error]))throw l.error;return P(j({},l),{mutate:h,mutateAsync:l.mutate})}function er(){}const at=f.createContext({}),tr=()=>f.useContext(at),lt=({children:e})=>{const[t,r]=f.useState({}),s=f.useCallback((l,h)=>{var u;return`${l}_${(u=h==null?void 0:h.dashboardId)!=null?u:""}`},[]),a=f.useCallback((l,h,u)=>{r(g=>P(j({},g),{[s(h,u)]:l}))},[s]),d=f.useCallback((l,h)=>t[s(l,h)],[t,s]);return n.jsx(at.Provider,{value:{saveChatId:a,getChatId:d},children:e})};class ct{constructor(t,r=!1){N(this,"httpClient");N(this,"getChatContexts",()=>Z(this,null,function*(){return(yield this.httpClient.get("api/datasources//"))||[]}));N(this,"getNlgInsights",t=>this.httpClient.post("api/v2/ai/nlg/queryResult",t));N(this,"getQueryRecommendations",(t,r)=>this.httpClient.get(`api/v2/ai/recommendations/query/${t}/${r.numOfRecommendations}${r.userPrompt?"?userPrompt="+encodeURIComponent(r.userPrompt):""}`));N(this,"getNlqResult",(t,r)=>this.httpClient.post(`api/v2/ai/nlq/query/${t}`,r));N(this,"setLlmConfig",t=>this.httpClient.post("api/v2/settings/ai/llmProvider",t).catch(r=>{console.error("Unable to set llm config",r)}));N(this,"getAllChats",()=>Z(this,null,function*(){return this.volatile?[]:this.httpClient.get("api/v2/ai/chats")}));N(this,"getChatById",t=>this.httpClient.get(`api/v2/ai/chats/${t}`));N(this,"createChat",(t,r)=>this.httpClient.post("api/v2/ai/chats",{sourceId:t,volatile:this.volatile,contextDetails:r}));N(this,"postChat",(t,r)=>this.httpClient.post(`api/v2/ai/chats/${t}`,r));N(this,"deleteChatHistory",t=>this.httpClient.delete(`api/v2/ai/chats/${t}/history`));N(this,"sendFeedback",t=>this.httpClient.post("api/v2/ai/feedback",t));N(this,"ai",{getNlgInsights:this.getNlgInsights,getNlqResult:this.getNlqResult,getQueryRecommendations:this.getQueryRecommendations,setLlmConfig:this.setLlmConfig,sendFeedback:this.sendFeedback,chat:{getAll:this.getAllChats,getById:this.getChatById,create:this.createChat,post:this.postChat,clearHistory:this.deleteChatHistory}});this.volatile=r,this.httpClient=t}}const rr=new o.QueryClient({defaultOptions:{queries:{refetchOnWindowFocus:!1,refetchOnMount:!1}}}),Ne=f.createContext(void 0),Q=()=>f.useContext(Ne),nr=({children:e,volatile:t})=>{const{app:r}=o.useSisenseContext(),s=f.useMemo(()=>r?new ct(r.httpClient,t):void 0,[r,t]);return n.jsx(Ne.Provider,{value:s,children:n.jsx(o.QueryClientProvider,{client:rr,children:e})})};function or({children:e,volatile:t}){return n.jsx(lt,{children:n.jsx(nr,{volatile:t,children:e})})}function Fe(e){var t,r;if("jaql"in e)return e;{const s=J.convertJaqlDataSource(e.dataSource),{filters:a=[],relations:d}=J.getFilterListAndRelationsJaql(e.filters),{metadata:l,filterRelations:h}=J.getJaqlQueryPayload({dataSource:e.dataSource,attributes:(t=e.dimensions)!=null?t:[],measures:(r=e.measures)!=null?r:[],filters:a,filterRelations:d,highlights:[]},!0),u={jaql:{datasource:s,metadata:l,filterRelations:h}};return e.verbosity&&(u.verbosity=e.verbosity),u}}const ye=(e,t=!0)=>{const r=f.useMemo(()=>Fe(e),[e]),s=Q(),{data:a,error:d,isError:l,isLoading:h,isSuccess:u,refetch:g}=o.useQuery({queryKey:["getNlgInsights",r,s],queryFn:()=>s==null?void 0:s.ai.getNlgInsights(r),select:m=>{var b;return(b=m==null?void 0:m.data)==null?void 0:b.answer},enabled:!!s&&t});return{isLoading:h,isError:l,isSuccess:u,data:a,error:d,refetch:f.useCallback(()=>{g()},[g])}},sr=e=>{const s=e,{enabled:t}=s,r=we(s,["enabled"]);return ye(r,t)},ir=o.withTracking("useGetNlgInsights")(sr),dt=["indicator","column","line","table","pie","bar"];function Oe(e){const{dataSource:t,query:r,chartTypes:s}=e;return{contextTitle:typeof t=="string"?t:t.title,request:{text:r,timezone:"UTC",chartTypes:s!=null?s:[...dt]}}}function ut(e,t){return Z(this,null,function*(){const{contextTitle:r,request:s}=Oe(e),a=yield t.ai.getNlqResult(r,s);return{widgetProps:a?de.toWidgetProps(a,{useCustomizedStyleOptions:e.enableAxisTitlesInWidgetProps||!1}):void 0,nlqResult:a}})}const ar=e=>{const{contextTitle:t,request:r}=Oe(e),s=Q(),a=["getNlqResult",t,r,s],{isLoading:d,isError:l,isSuccess:h,data:u,error:g,refetch:m}=o.useQuery({queryKey:a,queryFn:()=>s?ut(e,s).then(b=>b.widgetProps):Promise.reject("No API available"),enabled:!!s&&e.enabled});return{isLoading:d,isError:l,isSuccess:h,data:u,error:g,refetch:f.useCallback(()=>{m()},[m])}},lr=o.withTracking("useGetNlqResult")(ar),pt=4,ht=e=>{const{contextTitle:t,count:r,enableAxisTitlesInWidgetProps:s,enabled:a,customPrompt:d}=e,l=Q(),h=r!=null?r:pt,u=(a===void 0||a===!0)&&h>0,{isLoading:g,isError:m,isSuccess:b,data:i,error:p,refetch:c}=o.useQuery({queryKey:["getQueryRecommendations",t,h,l],queryFn:()=>l==null?void 0:l.ai.getQueryRecommendations(t,j({numOfRecommendations:h},d?{userPrompt:d}:null)),enabled:!!l&&u}),x=f.useMemo(()=>i==null?void 0:i.map(C=>P(j({},C),{widgetProps:C.jaql?de.toWidgetProps(C,{useCustomizedStyleOptions:s}):void 0})),[i,s]);return{isLoading:u?g:!1,isError:m,isSuccess:b,data:x!=null?x:void 0,error:p,refetch:f.useCallback(()=>{c()},[c])}},cr=o.withTracking("useGetQueryRecommendations")(ht),gt=Object.freeze({enableFollowupQuestions:!1,enableHeader:!0,enableInsights:!0,numOfRecommendations:4,numOfRecentPrompts:5}),Ct=f.createContext(j({},gt)),U=()=>f.useContext(Ct),dr=({children:e,value:t})=>{const{t:r}=n.useTranslation(),s=Object.entries(t).reduce((a,[d,l])=>(l!==void 0&&(a[d]=l),a),P(j({},gt),{inputPromptText:r("ai.config.inputPromptText"),welcomeText:r("ai.config.welcomeText"),suggestionsWelcomeText:r("ai.config.suggestionsWelcomeText")}));return n.jsx(Ct.Provider,{value:s,children:e})},Be="chatHistory",ur=e=>{const t=Q(),{hideHistory:r}=U();return o.useQuery({queryKey:[Be,e,t],queryFn:()=>Z(exports,null,function*(){if(!t||!e)return;if(r)return[];const s=yield t.ai.chat.getById(e);return s==null?void 0:s.chatHistory}),enabled:!!t&&!!e})},ft=e=>{const t=Q(),r=o.useQueryClient();return Ie({mutationFn:()=>Z(exports,null,function*(){if(!(!t||!e))return t.ai.chat.clearHistory(e)}),onError:s=>{console.error("Error when clearing history:",s)},onSettled:()=>{e&&r.setQueriesData([Be,e],[])}})},He=e=>{const{data:t,isLoading:r,isError:s}=ur(e),{mutate:a}=ft(e);return f.useEffect(()=>{s&&a()},[s,a]),{history:t,isLoading:r,isError:s}},pr=()=>{const e=Q(),{data:t,isLoading:r,fetchStatus:s}=o.useQuery({queryKey:["getDataTopics",e],queryFn:()=>Z(exports,null,function*(){if(e)return e.getChatContexts()}),enabled:!!e});return{data:t,isLoading:r,fetchStatus:s}},hr=()=>{const e=Q(),{data:t,isLoading:r}=o.useQuery({queryKey:["getAllChats",e],queryFn:()=>e==null?void 0:e.ai.chat.getAll(),enabled:!!e});return{data:t!=null?t:[],isLoading:r}},gr=(e,t,r)=>{const s=o.useQueryClient(),a=Q(),d=Ie({mutationFn:()=>Z(exports,null,function*(){if(!(!a||!e))return a.ai.chat.create(e,r)}),onSuccess:()=>s.invalidateQueries(["getAllChats"])});return f.useEffect(()=>{t&&d.isIdle&&d.mutate()},[t,d]),d},Cr=(e,t)=>{switch(e.responseType=e.responseType.toLowerCase(),e.responseType){case"nlq":return{content:JSON.stringify(e.data),role:"assistant",type:"nlq"};case"text":case"error":return{content:e.data.answer,role:"assistant"};default:throw Error(`${t}${JSON.stringify(e)}`)}},fr=(e,t)=>{const r=o.useQueryClient(),s=f.useCallback(u=>{e&&r.setQueriesData([Be,e],g=>g&&[...g,u])},[r,e]),{t:a}=n.useTranslation(),d=Q(),{mutate:l,isLoading:h}=Ie({mutationFn:u=>Z(exports,null,function*(){if(!(!d||!e))return d.ai.chat.post(e,{text:u,options:{enableFollowup:t}})}),onMutate:u=>{s({content:u,role:"user"})},onError:u=>{u instanceof Error&&(console.error("Error when sending message:",u.message),s({content:a(`ai.errors.${u.interpolationOptions.errorCode}`,{defaultValue:a("ai.errors.unexpectedChatResponse")}),role:"assistant"}))},onSuccess:u=>{if(!u)return;const g=a("ai.errors.unknownResponse");s(Cr(u,g))}});return{mutate:l,isLoading:h}},De=(e,t,r=!1)=>{var m;const{data:s,isLoading:a}=hr(),d=tr(),l=d.getChatId(e,t)||((m=s.find(b=>{var i;return b.contextTitle===e&&((i=b.contextDetails)==null?void 0:i.dashboardId)===(t==null?void 0:t.dashboardId)}))==null?void 0:m.chatId),{isError:h,data:u,isSuccess:g}=gr(e,r&&!a&&!l,t);return f.useEffect(()=>{g&&u&&!l&&d.saveChatId(u.chatId,e,t)},[u,g,e,t,l,d]),{chatId:l,isLoading:a,isError:h}},xt=e=>f.useMemo(()=>{if(e!=null&&e.length){const t=e[e.length-1];if(Ae(t))return JSON.parse(t.content)}return null},[e]),Ae=e=>!!e&&"type"in e&&e.type==="nlq",xr=e=>!!e&&"type"in e&&e.type==="text",mt=(e,t)=>{const{t:r}=n.useTranslation(),{enableFollowupQuestions:s}=U(),{chatId:a,isError:d}=De(e,t,!0),[l,h]=f.useState(null),{history:u,isLoading:g,isError:m}=He(a);f.useEffect(()=>{d?h(new J.TranslatableError("ai.errors.chatUnavailable")):m&&h(new J.TranslatableError("ai.errors.fetchHistory"))},[d,m,r]);const{mutate:b,isLoading:i}=fr(a,s),p=xt(u);return{chatId:a,history:u!=null?u:[],lastNlqResponse:p,sendMessage:b,isAwaitingResponse:i,isLoading:g,lastError:l}},mr=({contextTitle:e,contextDetails:t})=>{const{chatId:r}=De(e,t),{history:s}=He(r);return xt(s)},yr=({contextTitle:e,contextDetails:t})=>{const{chatId:r}=De(e,t),{history:s}=He(r);return f.useMemo(()=>{if(s!=null&&s.length){const a=s.slice().reverse().find(Ae);if(a)return JSON.parse(a.content)}return null},[s])},br=500,vr=500,kr=500,wr=900,jr=o.styled.div`
|
|
2
2
|
box-sizing: border-box;
|
|
3
3
|
position: relative;
|
|
4
4
|
display: flex;
|
package/dist/ai.js
CHANGED
|
@@ -36,7 +36,7 @@ var J = (e, t, n) => new Promise((r, s) => {
|
|
|
36
36
|
}, p = (d) => d.done ? r(d.value) : Promise.resolve(d.value).then(c, a);
|
|
37
37
|
p((n = n.apply(e, t)).next());
|
|
38
38
|
});
|
|
39
|
-
import { S as Tn, s as $n, n as Lt, g as Ln, u as ke, a as Rn, b as En, p as In, Q as Rt, c as Nn, d as Et, e as fe, w as Je, f as v, h as A, i as L, P as It, T as Pn, E as Bn, j as Fn, k as Hn, l as On, m as Be, B as Dn, M as An, o as pe, q as Nt, r as zn, t as qn, v as Wn, x as Zn, y as Ce, z as qe, F as Ct, A as C, C as jn, I as Pt, D as Gn, G as Vn, H as Un, J as xe, K as Qn, L as _n, N as Kn, O as Bt, R as Xn, U as Jn, V as Yn, W as er, X as tr } from "./use-common-filters-
|
|
39
|
+
import { S as Tn, s as $n, n as Lt, g as Ln, u as ke, a as Rn, b as En, p as In, Q as Rt, c as Nn, d as Et, e as fe, w as Je, f as v, h as A, i as L, P as It, T as Pn, E as Bn, j as Fn, k as Hn, l as On, m as Be, B as Dn, M as An, o as pe, q as Nt, r as zn, t as qn, v as Wn, x as Zn, y as Ce, z as qe, F as Ct, A as C, C as jn, I as Pt, D as Gn, G as Vn, H as Un, J as xe, K as Qn, L as _n, N as Kn, O as Bt, R as Xn, U as Jn, V as Yn, W as er, X as tr } from "./use-common-filters-Db1VFZvT.js";
|
|
40
40
|
import { j as o, u as F, c as w, F as Z, e as Ft, i as Ie, f as Ht, h as nr, k as rr, t as or } from "./derive-chart-family-BEjUaaLD.js";
|
|
41
41
|
import * as Y from "react";
|
|
42
42
|
import { createContext as Ye, useState as O, useCallback as B, useContext as et, useMemo as q, useEffect as we, useRef as he, useLayoutEffect as Ne, forwardRef as ir, memo as sr } from "react";
|