@nlux/react
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("react"),t=require("@nlux/core"),r=require("react-dom"),n=Object.prototype.hasOwnProperty;function o(e,t,r){for(r of e.keys())if(i(r,t))return r}function i(e,t){var r,s,u;if(e===t)return!0;if(e&&t&&(r=e.constructor)===t.constructor){if(r===Date)return e.getTime()===t.getTime();if(r===RegExp)return e.toString()===t.toString();if(r===Array){if((s=e.length)===t.length)for(;s--&&i(e[s],t[s]););return-1===s}if(r===Set){if(e.size!==t.size)return!1;for(s of e){if((u=s)&&"object"==typeof u&&!(u=o(t,u)))return!1;if(!t.has(u))return!1}return!0}if(r===Map){if(e.size!==t.size)return!1;for(s of e){if((u=s[0])&&"object"==typeof u&&!(u=o(t,u)))return!1;if(!i(s[1],t.get(u)))return!1}return!0}if(r===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(r===DataView){if((s=e.byteLength)===t.byteLength)for(;s--&&e.getInt8(s)===t.getInt8(s););return-1===s}if(ArrayBuffer.isView(e)){if((s=e.byteLength)===t.byteLength)for(;s--&&e[s]===t[s];);return-1===s}if(!r||"object"==typeof e){for(r in s=0,e){if(n.call(e,r)&&++s&&!n.call(t,r))return!1;if(!(r in t)||!i(e[r],t[r]))return!1}return Object.keys(t).length===s}}return e!=e&&t!=t}const s=e=>new Promise((t=>{const n=document.createElement("div");r.render(e,n,(()=>{if(1!==n.children.length||!n.firstElementChild)throw new Error("Expected exactly one child");t(n.firstElementChild)}))})),u=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await s(e.bot.picture);return{name:e.bot.name,tagline:e.bot.tagline,picture:t}})(),(async()=>{if(!e.user)return;const t="string"==typeof e.user.picture?e.user.picture:await s(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},a=(e,t)=>{if(void 0===e&&void 0===t)return;if(void 0!==e&&void 0===t){const t={},r=Object.keys(e);for(const e of r)t[e]=void 0;return t}if(void 0===e&&void 0!==t)return t;if(!t||!e)return;let r=!1;const n={},o=Object.keys(t);for(const i of o)e[i]!==t[i]&&(n[i]=t[i],r=!0);return r?n:void 0},c=async(e,r)=>{const n=a(e.events,r.events),o=a(e.layoutOptions,r.layoutOptions),i=a(e.conversationOptions,r.conversationOptions),s=a(e.promptBoxOptions,r.promptBoxOptions),c=await(async(e,t)=>{const r={};if((void 0!==e||void 0!==t)&&(e?.bot!==t?.bot&&(r.bot=void 0===t?.bot?void 0:(await u({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await u({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),f={};void 0!==n&&(f.events=n??{}),void 0!==o&&(f.layoutOptions=o??{}),void 0!==i&&(f.conversationOptions=i??{}),void 0!==s&&(f.promptBoxOptions=s??{}),void 0!==c&&(f.personaOptions=c??{});const l=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==l){const e=(e=>{let r="unknown";const n=e;if("function"==typeof n?.create)r="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return r="instance",e;if("unknown"!==r){if("builder"===r){const e=n.create();return"function"==typeof e?.fetchText||"function"==typeof e?.streamText?e:void t.warn("The adapter builder did not return a valid adapter.")}return e}t.warn("Unable to determine the type of the adapter.")})(l);e?f.adapter=e:t.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==r.className&&(f.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(f.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(f).length>0)return f},f=function(t,r){return e.useEffect(t,function(t){const r=e.useRef(t),n=e.useRef(0);return i(t,r.current)||(r.current=t,n.current+=1),e.useMemo((()=>r.current),[n.current])}(r))};Object.defineProperty(exports,"NluxConfigError",{enumerable:!0,get:function(){return t.NluxConfigError}}),Object.defineProperty(exports,"NluxError",{enumerable:!0,get:function(){return t.NluxError}}),Object.defineProperty(exports,"NluxRenderingError",{enumerable:!0,get:function(){return t.NluxRenderingError}}),Object.defineProperty(exports,"NluxUsageError",{enumerable:!0,get:function(){return t.NluxUsageError}}),Object.defineProperty(exports,"NluxValidationError",{enumerable:!0,get:function(){return t.NluxValidationError}}),Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.AiChat=r=>{const n=e.useRef(null),[o,i]=e.useState(null),s=e.useRef(null);return e.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let e=!0;const{adapter:o,className:i,syntaxHighlighter:a,layoutOptions:c,conversationOptions:f,promptBoxOptions:l,personaOptions:p,events:d,initialConversation:h}=r;let m=t.createAiChat().withAdapter(o);if(c&&(m=m.withLayoutOptions(c)),l&&(m=m.withPromptBoxOptions(l)),f&&(m=m.withConversationOptions(f)),i&&(m=m.withClassName(i)),a&&(m=m.withSyntaxHighlighter(a)),h&&(m=m.withInitialConversation(h)),d){const e=Object.keys(d);for(const t of e){const e=d[t];e&&m.on(t,e)}}return p?u(p).then((r=>{m=m.withPersonaOptions(r),e&&(n.current?(m.mount(n.current),s.current=m):t.warn("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(n.current),s.current=m),()=>{e=!1,m?.unmount()}}),[]),e.useEffect((()=>{let e=!1;if(o)return s.current&&c(o,r).then((n=>{!e&&n&&(s.current?(s.current.updateProps(n),i(r)):t.warn("AiChat is not defined! Cannot update."))})),()=>{e=!0};i(r)}),[r,o]),e.createElement("div",{ref:n})},exports.createAiContext=r=>{const n=t.createAiContext(),o=e.createContext(n);return{Provider:n=>{const[i,s]=e.useState(),[u,a]=e.useState(),[c,f]=e.useState();e.useEffect((()=>{let e=!0;const o=t.createAiContext().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:t.predefinedContextSize["100k"]});return f(o),o.initialize(n.initialItems||{}).then((t=>{e&&(t.success?s(t.contextId):a(new Error(t.error)))})),()=>{e=!1,o.destroy()}}),[]);const{children:l}=n;return u?n.errorComponent?e.createElement(n.errorComponent,{error:u.message}):e.createElement("div",null,e.createElement("h1",null,"Error initializing AI context"),e.createElement("p",null,u.message)):i&&c?e.createElement(o.Provider,{value:c},l):n.loadingComponent?e.createElement(n.loadingComponent,null):null},ref:o}},exports.useAiContext=(t,r,n)=>{const o=e.useContext(t.ref),[i]=e.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),s=e.useRef();e.useEffect((()=>(s.current=o.observeState(i,r,n),()=>{s.current?.discard(),s.current=void 0})),[]),e.useEffect((()=>{s.current?.setDescription(r)}),[r]),e.useEffect((()=>{s.current?.setData(n)}),[n])},exports.useDeepCompareEffect=f; | ||
"use strict";var e=require("react"),t=require("@nlux/core"),r=require("react-dom"),n=Object.prototype.hasOwnProperty;function o(e,t,r){for(r of e.keys())if(i(r,t))return r}function i(e,t){var r,s,u;if(e===t)return!0;if(e&&t&&(r=e.constructor)===t.constructor){if(r===Date)return e.getTime()===t.getTime();if(r===RegExp)return e.toString()===t.toString();if(r===Array){if((s=e.length)===t.length)for(;s--&&i(e[s],t[s]););return-1===s}if(r===Set){if(e.size!==t.size)return!1;for(s of e){if((u=s)&&"object"==typeof u&&!(u=o(t,u)))return!1;if(!t.has(u))return!1}return!0}if(r===Map){if(e.size!==t.size)return!1;for(s of e){if((u=s[0])&&"object"==typeof u&&!(u=o(t,u)))return!1;if(!i(s[1],t.get(u)))return!1}return!0}if(r===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(r===DataView){if((s=e.byteLength)===t.byteLength)for(;s--&&e.getInt8(s)===t.getInt8(s););return-1===s}if(ArrayBuffer.isView(e)){if((s=e.byteLength)===t.byteLength)for(;s--&&e[s]===t[s];);return-1===s}if(!r||"object"==typeof e){for(r in s=0,e){if(n.call(e,r)&&++s&&!n.call(t,r))return!1;if(!(r in t)||!i(e[r],t[r]))return!1}return Object.keys(t).length===s}}return e!=e&&t!=t}const s=e=>new Promise((t=>{const n=document.createElement("div");r.render(e,n,(()=>{if(1!==n.children.length||!n.firstElementChild)throw new Error("Expected exactly one child");t(n.firstElementChild)}))})),u=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await s(e.bot.picture);return{name:e.bot.name,tagline:e.bot.tagline,picture:t}})(),(async()=>{if(!e.user)return;const t="string"==typeof e.user.picture?e.user.picture:await s(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},a=(e,t)=>{if(void 0===e&&void 0===t)return;if(void 0!==e&&void 0===t){const t={},r=Object.keys(e);for(const e of r)t[e]=void 0;return t}if(void 0===e&&void 0!==t)return t;if(!t||!e)return;let r=!1;const n={},o=Object.keys(t);for(const i of o)e[i]!==t[i]&&(n[i]=t[i],r=!0);return r?n:void 0},c=async(e,r)=>{const n=a(e.events,r.events),o=a(e.layoutOptions,r.layoutOptions),i=a(e.conversationOptions,r.conversationOptions),s=a(e.promptBoxOptions,r.promptBoxOptions),c=await(async(e,t)=>{const r={};if((void 0!==e||void 0!==t)&&(e?.bot!==t?.bot&&(r.bot=void 0===t?.bot?void 0:(await u({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await u({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),f={};void 0!==n&&(f.events=n??{}),void 0!==o&&(f.layoutOptions=o??{}),void 0!==i&&(f.conversationOptions=i??{}),void 0!==s&&(f.promptBoxOptions=s??{}),void 0!==c&&(f.personaOptions=c??{});const l=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==l){const e=(e=>{let r="unknown";const n=e;if("function"==typeof n?.create)r="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return r="instance",e;if("unknown"!==r){if("builder"===r){const e=n.create();return"function"==typeof e?.fetchText||"function"==typeof e?.streamText?e:void t.warn("The adapter builder did not return a valid adapter.")}return e}t.warn("Unable to determine the type of the adapter.")})(l);e?f.adapter=e:t.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==r.className&&(f.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(f.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(f).length>0)return f},f=function(t,r){return e.useEffect(t,function(t){const r=e.useRef(t),n=e.useRef(0);return i(t,r.current)||(r.current=t,n.current+=1),e.useMemo((()=>r.current),[n.current])}(r))};Object.defineProperty(exports,"NluxConfigError",{enumerable:!0,get:function(){return t.NluxConfigError}}),Object.defineProperty(exports,"NluxError",{enumerable:!0,get:function(){return t.NluxError}}),Object.defineProperty(exports,"NluxRenderingError",{enumerable:!0,get:function(){return t.NluxRenderingError}}),Object.defineProperty(exports,"NluxUsageError",{enumerable:!0,get:function(){return t.NluxUsageError}}),Object.defineProperty(exports,"NluxValidationError",{enumerable:!0,get:function(){return t.NluxValidationError}}),Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.AiChat=r=>{const n=e.useRef(null),[o,i]=e.useState(null),s=e.useRef(null);return e.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let e=!0;const{adapter:o,className:i,syntaxHighlighter:a,layoutOptions:c,conversationOptions:f,promptBoxOptions:l,personaOptions:p,events:d,initialConversation:h}=r;let m=t.createAiChat().withAdapter(o);if(c&&(m=m.withLayoutOptions(c)),l&&(m=m.withPromptBoxOptions(l)),f&&(m=m.withConversationOptions(f)),i&&(m=m.withClassName(i)),a&&(m=m.withSyntaxHighlighter(a)),h&&(m=m.withInitialConversation(h)),d){const e=Object.keys(d);for(const t of e){const e=d[t];e&&m.on(t,e)}}return p?u(p).then((r=>{m=m.withPersonaOptions(r),e&&(n.current?(m.mount(n.current),s.current=m):t.warn("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(n.current),s.current=m),()=>{e=!1,m?.unmount()}}),[]),e.useEffect((()=>{let e=!1;if(o)return s.current&&c(o,r).then((n=>{!e&&n&&(s.current?(s.current.updateProps(n),i(r)):t.warn("AiChat is not defined! Cannot update."))})),()=>{e=!0};i(r)}),[r,o]),e.createElement("div",{ref:n})},exports.createAiContext=r=>{const n=t.createAiContext(),o=e.createContext(n);return{Provider:n=>{const[i,s]=e.useState(),[u,a]=e.useState(),[c,f]=e.useState();e.useEffect((()=>{let e=!0;const o=t.createAiContext().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:t.predefinedContextSize["100k"]});return f(o),o.initialize(n.initialItems||{}).then((t=>{e&&(t.success?s(t.contextId):a(new Error(t.error)))})),()=>{e=!1,o.destroy()}}),[]);const{children:l}=n;return u?n.errorComponent?e.createElement(n.errorComponent,{error:u.message}):e.createElement("div",null,e.createElement("h1",null,"Error initializing AI context"),e.createElement("p",null,u.message)):i&&c?e.createElement(o.Provider,{value:c},l):n.loadingComponent?e.createElement(n.loadingComponent,null):null},ref:o}},exports.useAiContext=(t,r,n)=>{const o=e.useContext(t.ref),[i]=e.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),s=e.useRef();e.useEffect((()=>(s.current=o.observeState(i,r,n),()=>{s.current?.discard(),s.current=void 0})),[]),e.useEffect((()=>{s.current?.setDescription(r)}),[r]),e.useEffect((()=>{s.current?.setData(n)}),[n])},exports.useAiTask=(t,r,n,o)=>{const i=e.useContext(t.ref),[s]=e.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(i.hasTask(e));return e})),u=e.useRef();e.useEffect((()=>(u.current=i.registerTask(s,r,n,o),()=>{u.current?.discard(),u.current=void 0})),[]),e.useEffect((()=>{u.current?.setDescription(r)}),[r]),e.useEffect((()=>{u.current?.setCallback(n)}),[n]),e.useEffect((()=>{u.current?.setParamDescriptions(o??[])}),[o])},exports.useDeepCompareEffect=f; |
@@ -1,1 +0,1 @@ | ||
import t,{useEffect as e,useRef as r,useMemo as n,useState as o,useContext as i,createContext as a}from"react";import{warn as s,createAiChat as u,createAiContext as c,predefinedContextSize as l}from"@nlux/core";export{NluxConfigError,NluxError,NluxRenderingError,NluxUsageError,NluxValidationError,debug}from"@nlux/core";import{render as f}from"react-dom";var p=Object.prototype.hasOwnProperty;function d(t,e,r){for(r of t.keys())if(h(r,e))return r}function h(t,e){var r,n,o;if(t===e)return!0;if(t&&e&&(r=t.constructor)===e.constructor){if(r===Date)return t.getTime()===e.getTime();if(r===RegExp)return t.toString()===e.toString();if(r===Array){if((n=t.length)===e.length)for(;n--&&h(t[n],e[n]););return-1===n}if(r===Set){if(t.size!==e.size)return!1;for(n of t){if((o=n)&&"object"==typeof o&&!(o=d(e,o)))return!1;if(!e.has(o))return!1}return!0}if(r===Map){if(t.size!==e.size)return!1;for(n of t){if((o=n[0])&&"object"==typeof o&&!(o=d(e,o)))return!1;if(!h(n[1],e.get(o)))return!1}return!0}if(r===ArrayBuffer)t=new Uint8Array(t),e=new Uint8Array(e);else if(r===DataView){if((n=t.byteLength)===e.byteLength)for(;n--&&t.getInt8(n)===e.getInt8(n););return-1===n}if(ArrayBuffer.isView(t)){if((n=t.byteLength)===e.byteLength)for(;n--&&t[n]===e[n];);return-1===n}if(!r||"object"==typeof t){for(r in n=0,t){if(p.call(t,r)&&++n&&!p.call(e,r))return!1;if(!(r in e)||!h(t[r],e[r]))return!1}return Object.keys(e).length===n}}return t!=t&&e!=e}const m=t=>new Promise((e=>{const r=document.createElement("div");f(t,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");e(r.firstElementChild)}))})),y=async t=>{const[e,r]=await Promise.all([(async()=>{if(!t.bot)return;const e="string"==typeof t.bot.picture?t.bot.picture:await m(t.bot.picture);return{name:t.bot.name,tagline:t.bot.tagline,picture:e}})(),(async()=>{if(!t.user)return;const e="string"==typeof t.user.picture?t.user.picture:await m(t.user.picture);return{name:t.user.name,picture:e}})()]);return{bot:e,user:r}},g=(t,e)=>{if(void 0===t&&void 0===e)return;if(void 0!==t&&void 0===e){const e={},r=Object.keys(t);for(const t of r)e[t]=void 0;return e}if(void 0===t&&void 0!==e)return e;if(!e||!t)return;let r=!1;const n={},o=Object.keys(e);for(const i of o)t[i]!==e[i]&&(n[i]=e[i],r=!0);return r?n:void 0},v=async(t,e)=>{const r=g(t.events,e.events),n=g(t.layoutOptions,e.layoutOptions),o=g(t.conversationOptions,e.conversationOptions),i=g(t.promptBoxOptions,e.promptBoxOptions),a=await(async(t,e)=>{const r={};if((void 0!==t||void 0!==e)&&(t?.bot!==e?.bot&&(r.bot=void 0===e?.bot?void 0:(await y({bot:e.bot})).bot),t?.user!==e?.user&&(r.user=void 0===e?.user?void 0:(await y({user:e.user})).user),0!==Object.keys(r).length))return r})(t.personaOptions,e.personaOptions),u={};void 0!==r&&(u.events=r??{}),void 0!==n&&(u.layoutOptions=n??{}),void 0!==o&&(u.conversationOptions=o??{}),void 0!==i&&(u.promptBoxOptions=i??{}),void 0!==a&&(u.personaOptions=a??{});const c=t.adapter!==e.adapter?e.adapter:void 0;if(void 0!==c){const t=(t=>{let e="unknown";const r=t;if("function"==typeof r?.create)e="builder";else if("function"==typeof r?.fetchText||"function"==typeof r?.streamText)return e="instance",t;if("unknown"!==e){if("builder"===e){const t=r.create();return"function"==typeof t?.fetchText||"function"==typeof t?.streamText?t:void s("The adapter builder did not return a valid adapter.")}return t}s("Unable to determine the type of the adapter.")})(c);t?u.adapter=t:s({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}t.className!==e.className&&(u.className=e.className),t.syntaxHighlighter!==e.syntaxHighlighter&&(u.syntaxHighlighter=e.syntaxHighlighter);if(Object.keys(u).length>0)return u},b=n=>{const i=r(null),[a,c]=o(null),l=r(null);return e((()=>{if(!i.current)throw new Error("Root element is not defined");let t=!0;const{adapter:e,className:r,syntaxHighlighter:o,layoutOptions:a,conversationOptions:c,promptBoxOptions:f,personaOptions:p,events:d,initialConversation:h}=n;let m=u().withAdapter(e);if(a&&(m=m.withLayoutOptions(a)),f&&(m=m.withPromptBoxOptions(f)),c&&(m=m.withConversationOptions(c)),r&&(m=m.withClassName(r)),o&&(m=m.withSyntaxHighlighter(o)),h&&(m=m.withInitialConversation(h)),d){const t=Object.keys(d);for(const e of t){const t=d[e];t&&m.on(e,t)}}return p?y(p).then((e=>{m=m.withPersonaOptions(e),t&&(i.current?(m.mount(i.current),l.current=m):s("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(i.current),l.current=m),()=>{t=!1,m?.unmount()}}),[]),e((()=>{let t=!1;if(a)return l.current&&v(a,n).then((e=>{!t&&e&&(l.current?(l.current.updateProps(e),c(n)):s("AiChat is not defined! Cannot update."))})),()=>{t=!0};c(n)}),[n,a]),t.createElement("div",{ref:i})},w=(t,n,a)=>{const s=i(t.ref),[u]=o((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(s.hasItem(t));return t})),c=r();e((()=>(c.current=s.observeState(u,n,a),()=>{c.current?.discard(),c.current=void 0})),[]),e((()=>{c.current?.setDescription(n)}),[n]),e((()=>{c.current?.setData(a)}),[a])},x=r=>{const n=c(),o=a(n);return{Provider:n=>{const[i,a]=t.useState(),[s,u]=t.useState(),[f,p]=t.useState();e((()=>{let t=!0;const e=c().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:l["100k"]});return p(e),e.initialize(n.initialItems||{}).then((e=>{t&&(e.success?a(e.contextId):u(new Error(e.error)))})),()=>{t=!1,e.destroy()}}),[]);const{children:d}=n;return s?n.errorComponent?t.createElement(n.errorComponent,{error:s.message}):t.createElement("div",null,t.createElement("h1",null,"Error initializing AI context"),t.createElement("p",null,s.message)):i&&f?t.createElement(o.Provider,{value:f},d):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},O=function(t,o){return e(t,function(t){const e=r(t),o=r(0);return h(t,e.current)||(e.current=t,o.current+=1),n((()=>e.current),[o.current])}(o))};export{b as AiChat,x as createAiContext,w as useAiContext,O as useDeepCompareEffect}; | ||
import t,{useEffect as e,useRef as r,useMemo as n,useState as o,useContext as i,createContext as a}from"react";import{warn as s,createAiChat as u,createAiContext as c,predefinedContextSize as l}from"@nlux/core";export{NluxConfigError,NluxError,NluxRenderingError,NluxUsageError,NluxValidationError,debug}from"@nlux/core";import{render as f}from"react-dom";var p=Object.prototype.hasOwnProperty;function d(t,e,r){for(r of t.keys())if(h(r,e))return r}function h(t,e){var r,n,o;if(t===e)return!0;if(t&&e&&(r=t.constructor)===e.constructor){if(r===Date)return t.getTime()===e.getTime();if(r===RegExp)return t.toString()===e.toString();if(r===Array){if((n=t.length)===e.length)for(;n--&&h(t[n],e[n]););return-1===n}if(r===Set){if(t.size!==e.size)return!1;for(n of t){if((o=n)&&"object"==typeof o&&!(o=d(e,o)))return!1;if(!e.has(o))return!1}return!0}if(r===Map){if(t.size!==e.size)return!1;for(n of t){if((o=n[0])&&"object"==typeof o&&!(o=d(e,o)))return!1;if(!h(n[1],e.get(o)))return!1}return!0}if(r===ArrayBuffer)t=new Uint8Array(t),e=new Uint8Array(e);else if(r===DataView){if((n=t.byteLength)===e.byteLength)for(;n--&&t.getInt8(n)===e.getInt8(n););return-1===n}if(ArrayBuffer.isView(t)){if((n=t.byteLength)===e.byteLength)for(;n--&&t[n]===e[n];);return-1===n}if(!r||"object"==typeof t){for(r in n=0,t){if(p.call(t,r)&&++n&&!p.call(e,r))return!1;if(!(r in e)||!h(t[r],e[r]))return!1}return Object.keys(e).length===n}}return t!=t&&e!=e}const m=t=>new Promise((e=>{const r=document.createElement("div");f(t,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");e(r.firstElementChild)}))})),y=async t=>{const[e,r]=await Promise.all([(async()=>{if(!t.bot)return;const e="string"==typeof t.bot.picture?t.bot.picture:await m(t.bot.picture);return{name:t.bot.name,tagline:t.bot.tagline,picture:e}})(),(async()=>{if(!t.user)return;const e="string"==typeof t.user.picture?t.user.picture:await m(t.user.picture);return{name:t.user.name,picture:e}})()]);return{bot:e,user:r}},g=(t,e)=>{if(void 0===t&&void 0===e)return;if(void 0!==t&&void 0===e){const e={},r=Object.keys(t);for(const t of r)e[t]=void 0;return e}if(void 0===t&&void 0!==e)return e;if(!e||!t)return;let r=!1;const n={},o=Object.keys(e);for(const i of o)t[i]!==e[i]&&(n[i]=e[i],r=!0);return r?n:void 0},v=async(t,e)=>{const r=g(t.events,e.events),n=g(t.layoutOptions,e.layoutOptions),o=g(t.conversationOptions,e.conversationOptions),i=g(t.promptBoxOptions,e.promptBoxOptions),a=await(async(t,e)=>{const r={};if((void 0!==t||void 0!==e)&&(t?.bot!==e?.bot&&(r.bot=void 0===e?.bot?void 0:(await y({bot:e.bot})).bot),t?.user!==e?.user&&(r.user=void 0===e?.user?void 0:(await y({user:e.user})).user),0!==Object.keys(r).length))return r})(t.personaOptions,e.personaOptions),u={};void 0!==r&&(u.events=r??{}),void 0!==n&&(u.layoutOptions=n??{}),void 0!==o&&(u.conversationOptions=o??{}),void 0!==i&&(u.promptBoxOptions=i??{}),void 0!==a&&(u.personaOptions=a??{});const c=t.adapter!==e.adapter?e.adapter:void 0;if(void 0!==c){const t=(t=>{let e="unknown";const r=t;if("function"==typeof r?.create)e="builder";else if("function"==typeof r?.fetchText||"function"==typeof r?.streamText)return e="instance",t;if("unknown"!==e){if("builder"===e){const t=r.create();return"function"==typeof t?.fetchText||"function"==typeof t?.streamText?t:void s("The adapter builder did not return a valid adapter.")}return t}s("Unable to determine the type of the adapter.")})(c);t?u.adapter=t:s({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}t.className!==e.className&&(u.className=e.className),t.syntaxHighlighter!==e.syntaxHighlighter&&(u.syntaxHighlighter=e.syntaxHighlighter);if(Object.keys(u).length>0)return u},b=n=>{const i=r(null),[a,c]=o(null),l=r(null);return e((()=>{if(!i.current)throw new Error("Root element is not defined");let t=!0;const{adapter:e,className:r,syntaxHighlighter:o,layoutOptions:a,conversationOptions:c,promptBoxOptions:f,personaOptions:p,events:d,initialConversation:h}=n;let m=u().withAdapter(e);if(a&&(m=m.withLayoutOptions(a)),f&&(m=m.withPromptBoxOptions(f)),c&&(m=m.withConversationOptions(c)),r&&(m=m.withClassName(r)),o&&(m=m.withSyntaxHighlighter(o)),h&&(m=m.withInitialConversation(h)),d){const t=Object.keys(d);for(const e of t){const t=d[e];t&&m.on(e,t)}}return p?y(p).then((e=>{m=m.withPersonaOptions(e),t&&(i.current?(m.mount(i.current),l.current=m):s("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(i.current),l.current=m),()=>{t=!1,m?.unmount()}}),[]),e((()=>{let t=!1;if(a)return l.current&&v(a,n).then((e=>{!t&&e&&(l.current?(l.current.updateProps(e),c(n)):s("AiChat is not defined! Cannot update."))})),()=>{t=!0};c(n)}),[n,a]),t.createElement("div",{ref:i})},w=(t,n,a)=>{const s=i(t.ref),[u]=o((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(s.hasItem(t));return t})),c=r();e((()=>(c.current=s.observeState(u,n,a),()=>{c.current?.discard(),c.current=void 0})),[]),e((()=>{c.current?.setDescription(n)}),[n]),e((()=>{c.current?.setData(a)}),[a])},x=(t,n,a,s)=>{const u=i(t.ref),[c]=o((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(u.hasTask(t));return t})),l=r();e((()=>(l.current=u.registerTask(c,n,a,s),()=>{l.current?.discard(),l.current=void 0})),[]),e((()=>{l.current?.setDescription(n)}),[n]),e((()=>{l.current?.setCallback(a)}),[a]),e((()=>{l.current?.setParamDescriptions(s??[])}),[s])},O=r=>{const n=c(),o=a(n);return{Provider:n=>{const[i,a]=t.useState(),[s,u]=t.useState(),[f,p]=t.useState();e((()=>{let t=!0;const e=c().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:l["100k"]});return p(e),e.initialize(n.initialItems||{}).then((e=>{t&&(e.success?a(e.contextId):u(new Error(e.error)))})),()=>{t=!1,e.destroy()}}),[]);const{children:d}=n;return s?n.errorComponent?t.createElement(n.errorComponent,{error:s.message}):t.createElement("div",null,t.createElement("h1",null,"Error initializing AI context"),t.createElement("p",null,s.message)):i&&f?t.createElement(o.Provider,{value:f},d):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},E=function(t,o){return e(t,function(t){const e=r(t),o=r(0);return h(t,e.current)||(e.current=t,o.current+=1),n((()=>e.current),[o.current])}(o))};export{b as AiChat,O as createAiContext,w as useAiContext,x as useAiTask,E as useDeepCompareEffect}; |
@@ -123,2 +123,33 @@ import React, { useEffect, JSX, FunctionComponent, ComponentClass, ReactNode, Context } from 'react'; | ||
/** | ||
* Use this hook to register a new task that can be trigger by the AI during <AiChat /> conversations. | ||
* It will create a new AI context task and will keep it in sync with the AI context. | ||
* The description is used by LLMs to understand the purpose of the task. | ||
* The callback is the function that will be called when the task is triggered. | ||
* The parametersDescription is used by LLMs to determine the value of each parameter to be passed to the task. | ||
* | ||
* @param {AiContext} aiContext The AI context instance to use, created with createAiContext() | ||
* @param {string} taskDescription The description of the task. This will be used by LLMs to understand context. | ||
* @param {Function} callback The function to be called when the task is triggered. | ||
* @param {string[]} parametersDescription An array of descriptions for each parameter of the task. | ||
* | ||
* Usage example: | ||
* | ||
* ```tsx | ||
* const MyComponent = () => { | ||
* const taskCallback = useCallback((param1, param2) => { | ||
* // Do something with the parameters | ||
* }, []); | ||
* | ||
* useAiTask( | ||
* MyAiContext, 'Description of the task', | ||
* taskCallback, ['Description of the first parameter', 'Description of the second parameter'] | ||
* ); | ||
* | ||
* return <div>...</div>; | ||
* }; | ||
* ``` | ||
*/ | ||
declare const useAiTask: (aiContext: AiContext, taskDescription: string, callback: Function, parametersDescription?: string[]) => void; | ||
/** | ||
* Creates a new AI context with a React context provider that can be used to sync application state | ||
@@ -158,2 +189,2 @@ * with the backend for AI processing. | ||
export { AiChat, type AiChatComponentProps, type AiContext, type AiContextProviderProps, type BotPersona, type DiscardContextItem, type PersonaOptions, type UpdateContextItem, type UserPersona, createAiContext, useAiContext, useDeepCompareEffect }; | ||
export { AiChat, type AiChatComponentProps, type AiContext, type AiContextProviderProps, type BotPersona, type DiscardContextItem, type PersonaOptions, type UpdateContextItem, type UserPersona, createAiContext, useAiContext, useAiTask, useDeepCompareEffect }; |
{ | ||
"name": "@nlux/react", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "nlux React is a library for building conversational AI interfaces, with support for OpenAI, HuggingFace, and more.", | ||
@@ -62,3 +62,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/core": "1.0.7" | ||
"@nlux/core": "1.0.8" | ||
}, | ||
@@ -65,0 +65,0 @@ "peerDependencies": { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@nlux/core"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","@nlux/core","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/react"]={},e.React,e.core,e.reactDom)}(this,(function(e,t,r,n){"use strict";var o=Object.prototype.hasOwnProperty;function i(e,t,r){for(r of e.keys())if(u(r,t))return r}function u(e,t){var r,n,a;if(e===t)return!0;if(e&&t&&(r=e.constructor)===t.constructor){if(r===Date)return e.getTime()===t.getTime();if(r===RegExp)return e.toString()===t.toString();if(r===Array){if((n=e.length)===t.length)for(;n--&&u(e[n],t[n]););return-1===n}if(r===Set){if(e.size!==t.size)return!1;for(n of e){if((a=n)&&"object"==typeof a&&!(a=i(t,a)))return!1;if(!t.has(a))return!1}return!0}if(r===Map){if(e.size!==t.size)return!1;for(n of e){if((a=n[0])&&"object"==typeof a&&!(a=i(t,a)))return!1;if(!u(n[1],t.get(a)))return!1}return!0}if(r===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(r===DataView){if((n=e.byteLength)===t.byteLength)for(;n--&&e.getInt8(n)===t.getInt8(n););return-1===n}if(ArrayBuffer.isView(e)){if((n=e.byteLength)===t.byteLength)for(;n--&&e[n]===t[n];);return-1===n}if(!r||"object"==typeof e){for(r in n=0,e){if(o.call(e,r)&&++n&&!o.call(t,r))return!1;if(!(r in t)||!u(e[r],t[r]))return!1}return Object.keys(t).length===n}}return e!=e&&t!=t}const a=e=>new Promise((t=>{const r=document.createElement("div");n.render(e,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");t(r.firstElementChild)}))})),s=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await a(e.bot.picture);return{name:e.bot.name,tagline:e.bot.tagline,picture:t}})(),(async()=>{if(!e.user)return;const t="string"==typeof e.user.picture?e.user.picture:await a(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},c=(e,t)=>{if(void 0===e&&void 0===t)return;if(void 0!==e&&void 0===t){const t={},r=Object.keys(e);for(const e of r)t[e]=void 0;return t}if(void 0===e&&void 0!==t)return t;if(!t||!e)return;let r=!1;const n={},o=Object.keys(t);for(const i of o)e[i]!==t[i]&&(n[i]=t[i],r=!0);return r?n:void 0},f=async(e,t)=>{const n=c(e.events,t.events),o=c(e.layoutOptions,t.layoutOptions),i=c(e.conversationOptions,t.conversationOptions),u=c(e.promptBoxOptions,t.promptBoxOptions),a=await(async(e,t)=>{const r={};if((void 0!==e||void 0!==t)&&(e?.bot!==t?.bot&&(r.bot=void 0===t?.bot?void 0:(await s({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await s({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,t.personaOptions),f={};void 0!==n&&(f.events=n??{}),void 0!==o&&(f.layoutOptions=o??{}),void 0!==i&&(f.conversationOptions=i??{}),void 0!==u&&(f.promptBoxOptions=u??{}),void 0!==a&&(f.personaOptions=a??{});const l=e.adapter!==t.adapter?t.adapter:void 0;if(void 0!==l){const e=(e=>{let t="unknown";const n=e;if("function"==typeof n?.create)t="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return t="instance",e;if("unknown"!==t){if("builder"===t){const e=n.create();return"function"==typeof e?.fetchText||"function"==typeof e?.streamText?e:void r.warn("The adapter builder did not return a valid adapter.")}return e}r.warn("Unable to determine the type of the adapter.")})(l);e?f.adapter=e:r.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==t.className&&(f.className=t.className),e.syntaxHighlighter!==t.syntaxHighlighter&&(f.syntaxHighlighter=t.syntaxHighlighter);if(Object.keys(f).length>0)return f},l=function(e,r){return t.useEffect(e,function(e){const r=t.useRef(e),n=t.useRef(0);return u(e,r.current)||(r.current=e,n.current+=1),t.useMemo((()=>r.current),[n.current])}(r))};Object.defineProperty(e,"NluxConfigError",{enumerable:!0,get:function(){return r.NluxConfigError}}),Object.defineProperty(e,"NluxError",{enumerable:!0,get:function(){return r.NluxError}}),Object.defineProperty(e,"NluxRenderingError",{enumerable:!0,get:function(){return r.NluxRenderingError}}),Object.defineProperty(e,"NluxUsageError",{enumerable:!0,get:function(){return r.NluxUsageError}}),Object.defineProperty(e,"NluxValidationError",{enumerable:!0,get:function(){return r.NluxValidationError}}),Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return r.debug}}),e.AiChat=e=>{const n=t.useRef(null),[o,i]=t.useState(null),u=t.useRef(null);return t.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let t=!0;const{adapter:o,className:i,syntaxHighlighter:a,layoutOptions:c,conversationOptions:f,promptBoxOptions:l,personaOptions:d,events:p,initialConversation:h}=e;let m=r.createAiChat().withAdapter(o);if(c&&(m=m.withLayoutOptions(c)),l&&(m=m.withPromptBoxOptions(l)),f&&(m=m.withConversationOptions(f)),i&&(m=m.withClassName(i)),a&&(m=m.withSyntaxHighlighter(a)),h&&(m=m.withInitialConversation(h)),p){const e=Object.keys(p);for(const t of e){const e=p[t];e&&m.on(t,e)}}return d?s(d).then((e=>{m=m.withPersonaOptions(e),t&&(n.current?(m.mount(n.current),u.current=m):r.warn("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(n.current),u.current=m),()=>{t=!1,m?.unmount()}}),[]),t.useEffect((()=>{let t=!1;if(o)return u.current&&f(o,e).then((n=>{!t&&n&&(u.current?(u.current.updateProps(n),i(e)):r.warn("AiChat is not defined! Cannot update."))})),()=>{t=!0};i(e)}),[e,o]),t.createElement("div",{ref:n})},e.createAiContext=e=>{const n=r.createAiContext(),o=t.createContext(n);return{Provider:n=>{const[i,u]=t.useState(),[a,s]=t.useState(),[c,f]=t.useState();t.useEffect((()=>{let t=!0;const o=r.createAiContext().withAdapter(e).withDataSyncOptions({syncStrategy:"auto",contextSize:r.predefinedContextSize["100k"]});return f(o),o.initialize(n.initialItems||{}).then((e=>{t&&(e.success?u(e.contextId):s(new Error(e.error)))})),()=>{t=!1,o.destroy()}}),[]);const{children:l}=n;return a?n.errorComponent?t.createElement(n.errorComponent,{error:a.message}):t.createElement("div",null,t.createElement("h1",null,"Error initializing AI context"),t.createElement("p",null,a.message)):i&&c?t.createElement(o.Provider,{value:c},l):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},e.useAiContext=(e,r,n)=>{const o=t.useContext(e.ref),[i]=t.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),u=t.useRef();t.useEffect((()=>(u.current=o.observeState(i,r,n),()=>{u.current?.discard(),u.current=void 0})),[]),t.useEffect((()=>{u.current?.setDescription(r)}),[r]),t.useEffect((()=>{u.current?.setData(n)}),[n])},e.useDeepCompareEffect=l})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@nlux/core"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","@nlux/core","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/react"]={},e.React,e.core,e.reactDom)}(this,(function(e,t,r,n){"use strict";var o=Object.prototype.hasOwnProperty;function i(e,t,r){for(r of e.keys())if(u(r,t))return r}function u(e,t){var r,n,s;if(e===t)return!0;if(e&&t&&(r=e.constructor)===t.constructor){if(r===Date)return e.getTime()===t.getTime();if(r===RegExp)return e.toString()===t.toString();if(r===Array){if((n=e.length)===t.length)for(;n--&&u(e[n],t[n]););return-1===n}if(r===Set){if(e.size!==t.size)return!1;for(n of e){if((s=n)&&"object"==typeof s&&!(s=i(t,s)))return!1;if(!t.has(s))return!1}return!0}if(r===Map){if(e.size!==t.size)return!1;for(n of e){if((s=n[0])&&"object"==typeof s&&!(s=i(t,s)))return!1;if(!u(n[1],t.get(s)))return!1}return!0}if(r===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(r===DataView){if((n=e.byteLength)===t.byteLength)for(;n--&&e.getInt8(n)===t.getInt8(n););return-1===n}if(ArrayBuffer.isView(e)){if((n=e.byteLength)===t.byteLength)for(;n--&&e[n]===t[n];);return-1===n}if(!r||"object"==typeof e){for(r in n=0,e){if(o.call(e,r)&&++n&&!o.call(t,r))return!1;if(!(r in t)||!u(e[r],t[r]))return!1}return Object.keys(t).length===n}}return e!=e&&t!=t}const s=e=>new Promise((t=>{const r=document.createElement("div");n.render(e,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");t(r.firstElementChild)}))})),a=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await s(e.bot.picture);return{name:e.bot.name,tagline:e.bot.tagline,picture:t}})(),(async()=>{if(!e.user)return;const t="string"==typeof e.user.picture?e.user.picture:await s(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},c=(e,t)=>{if(void 0===e&&void 0===t)return;if(void 0!==e&&void 0===t){const t={},r=Object.keys(e);for(const e of r)t[e]=void 0;return t}if(void 0===e&&void 0!==t)return t;if(!t||!e)return;let r=!1;const n={},o=Object.keys(t);for(const i of o)e[i]!==t[i]&&(n[i]=t[i],r=!0);return r?n:void 0},f=async(e,t)=>{const n=c(e.events,t.events),o=c(e.layoutOptions,t.layoutOptions),i=c(e.conversationOptions,t.conversationOptions),u=c(e.promptBoxOptions,t.promptBoxOptions),s=await(async(e,t)=>{const r={};if((void 0!==e||void 0!==t)&&(e?.bot!==t?.bot&&(r.bot=void 0===t?.bot?void 0:(await a({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await a({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,t.personaOptions),f={};void 0!==n&&(f.events=n??{}),void 0!==o&&(f.layoutOptions=o??{}),void 0!==i&&(f.conversationOptions=i??{}),void 0!==u&&(f.promptBoxOptions=u??{}),void 0!==s&&(f.personaOptions=s??{});const l=e.adapter!==t.adapter?t.adapter:void 0;if(void 0!==l){const e=(e=>{let t="unknown";const n=e;if("function"==typeof n?.create)t="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return t="instance",e;if("unknown"!==t){if("builder"===t){const e=n.create();return"function"==typeof e?.fetchText||"function"==typeof e?.streamText?e:void r.warn("The adapter builder did not return a valid adapter.")}return e}r.warn("Unable to determine the type of the adapter.")})(l);e?f.adapter=e:r.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==t.className&&(f.className=t.className),e.syntaxHighlighter!==t.syntaxHighlighter&&(f.syntaxHighlighter=t.syntaxHighlighter);if(Object.keys(f).length>0)return f},l=function(e,r){return t.useEffect(e,function(e){const r=t.useRef(e),n=t.useRef(0);return u(e,r.current)||(r.current=e,n.current+=1),t.useMemo((()=>r.current),[n.current])}(r))};Object.defineProperty(e,"NluxConfigError",{enumerable:!0,get:function(){return r.NluxConfigError}}),Object.defineProperty(e,"NluxError",{enumerable:!0,get:function(){return r.NluxError}}),Object.defineProperty(e,"NluxRenderingError",{enumerable:!0,get:function(){return r.NluxRenderingError}}),Object.defineProperty(e,"NluxUsageError",{enumerable:!0,get:function(){return r.NluxUsageError}}),Object.defineProperty(e,"NluxValidationError",{enumerable:!0,get:function(){return r.NluxValidationError}}),Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return r.debug}}),e.AiChat=e=>{const n=t.useRef(null),[o,i]=t.useState(null),u=t.useRef(null);return t.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let t=!0;const{adapter:o,className:i,syntaxHighlighter:s,layoutOptions:c,conversationOptions:f,promptBoxOptions:l,personaOptions:d,events:p,initialConversation:h}=e;let m=r.createAiChat().withAdapter(o);if(c&&(m=m.withLayoutOptions(c)),l&&(m=m.withPromptBoxOptions(l)),f&&(m=m.withConversationOptions(f)),i&&(m=m.withClassName(i)),s&&(m=m.withSyntaxHighlighter(s)),h&&(m=m.withInitialConversation(h)),p){const e=Object.keys(p);for(const t of e){const e=p[t];e&&m.on(t,e)}}return d?a(d).then((e=>{m=m.withPersonaOptions(e),t&&(n.current?(m.mount(n.current),u.current=m):r.warn("Root element is not defined! AiChat cannot be mounted."))})):(m.mount(n.current),u.current=m),()=>{t=!1,m?.unmount()}}),[]),t.useEffect((()=>{let t=!1;if(o)return u.current&&f(o,e).then((n=>{!t&&n&&(u.current?(u.current.updateProps(n),i(e)):r.warn("AiChat is not defined! Cannot update."))})),()=>{t=!0};i(e)}),[e,o]),t.createElement("div",{ref:n})},e.createAiContext=e=>{const n=r.createAiContext(),o=t.createContext(n);return{Provider:n=>{const[i,u]=t.useState(),[s,a]=t.useState(),[c,f]=t.useState();t.useEffect((()=>{let t=!0;const o=r.createAiContext().withAdapter(e).withDataSyncOptions({syncStrategy:"auto",contextSize:r.predefinedContextSize["100k"]});return f(o),o.initialize(n.initialItems||{}).then((e=>{t&&(e.success?u(e.contextId):a(new Error(e.error)))})),()=>{t=!1,o.destroy()}}),[]);const{children:l}=n;return s?n.errorComponent?t.createElement(n.errorComponent,{error:s.message}):t.createElement("div",null,t.createElement("h1",null,"Error initializing AI context"),t.createElement("p",null,s.message)):i&&c?t.createElement(o.Provider,{value:c},l):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},e.useAiContext=(e,r,n)=>{const o=t.useContext(e.ref),[i]=t.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),u=t.useRef();t.useEffect((()=>(u.current=o.observeState(i,r,n),()=>{u.current?.discard(),u.current=void 0})),[]),t.useEffect((()=>{u.current?.setDescription(r)}),[r]),t.useEffect((()=>{u.current?.setData(n)}),[n])},e.useAiTask=(e,r,n,o)=>{const i=t.useContext(e.ref),[u]=t.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(i.hasTask(e));return e})),s=t.useRef();t.useEffect((()=>(s.current=i.registerTask(u,r,n,o),()=>{s.current?.discard(),s.current=void 0})),[]),t.useEffect((()=>{s.current?.setDescription(r)}),[r]),t.useEffect((()=>{s.current?.setCallback(n)}),[n]),t.useEffect((()=>{s.current?.setParamDescriptions(o??[])}),[o])},e.useDeepCompareEffect=l})); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34304
261
+ Added@nlux/core@1.0.8(transitive)
- Removed@nlux/core@1.0.7(transitive)
Updated@nlux/core@1.0.8