@nlux/react
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("@nlux/core"),t=require("react"),r=require("react-dom");const n=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)}))})),o=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await n(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 n(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},i=(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},a=async(t,r)=>{const n=i(t.events,r.events),a=i(t.layoutOptions,r.layoutOptions),s=i(t.conversationOptions,r.conversationOptions),u=i(t.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 o({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await o({user:t.user})).user),0!==Object.keys(r).length))return r})(t.personaOptions,r.personaOptions),l={};void 0!==n&&(l.events=n??{}),void 0!==a&&(l.layoutOptions=a??{}),void 0!==s&&(l.conversationOptions=s??{}),void 0!==u&&(l.promptBoxOptions=u??{}),void 0!==c&&(l.personaOptions=c??{});const d=t.adapter!==r.adapter?r.adapter:void 0;if(void 0!==d){const t=(t=>{let r="unknown";const n=t;if("function"==typeof n?.create)r="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return r="instance",t;if("unknown"!==r){if("builder"===r){const t=n.create();return"function"==typeof t?.fetchText||"function"==typeof t?.streamText?t:void e.warn("The adapter builder did not return a valid adapter.")}return t}e.warn("Unable to determine the type of the adapter.")})(d);t?l.adapter=t:e.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}t.className!==r.className&&(l.className=r.className),t.syntaxHighlighter!==r.syntaxHighlighter&&(l.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(l).length>0)return l};Object.defineProperty(exports,"NluxConfigError",{enumerable:!0,get:function(){return e.NluxConfigError}}),Object.defineProperty(exports,"NluxError",{enumerable:!0,get:function(){return e.NluxError}}),Object.defineProperty(exports,"NluxRenderingError",{enumerable:!0,get:function(){return e.NluxRenderingError}}),Object.defineProperty(exports,"NluxUsageError",{enumerable:!0,get:function(){return e.NluxUsageError}}),Object.defineProperty(exports,"NluxValidationError",{enumerable:!0,get:function(){return e.NluxValidationError}}),Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return e.debug}}),exports.AiChat=r=>{const n=t.useRef(null),[i,s]=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:i,className:a,syntaxHighlighter:s,layoutOptions:c,conversationOptions:l,promptBoxOptions:d,personaOptions:p,events:f,initialConversation:m}=r;let h=e.createAiChat().withAdapter(i);if(c&&(h=h.withLayoutOptions(c)),d&&(h=h.withPromptBoxOptions(d)),l&&(h=h.withConversationOptions(l)),a&&(h=h.withClassName(a)),s&&(h=h.withSyntaxHighlighter(s)),m&&(h=h.withInitialConversation(m)),f){const e=Object.keys(f);for(const t of e){const e=f[t];e&&h.on(t,e)}}return p?o(p).then((r=>{h=h.withPersonaOptions(r),t&&(n.current?(h.mount(n.current),u.current=h):e.warn("Root element is not defined! AiChat cannot be mounted."))})):(h.mount(n.current),u.current=h),()=>{t=!1,h?.unmount()}}),[]),t.useEffect((()=>{let t=!1;if(i)return u.current&&a(i,r).then((n=>{!t&&n&&(u.current?(u.current.updateProps(n),s(r)):e.warn("AiChat is not defined! Cannot update."))})),()=>{t=!0};s(r)}),[r,i]),t.createElement("div",{ref:n})},exports.createAiContext=r=>{const n=e.createAiContext(),o=t.createContext(n);return{Provider:n=>{const[i,a]=t.useState(),[s,u]=t.useState(),[c,l]=t.useState();t.useEffect((()=>{let t=!0;const o=e.createAiContext().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:e.predefinedContextSize["100k"]});return l(o),o.initialize(n.initialItems||{}).then((e=>{t&&(e.success?a(e.contextId):u(new Error(e.error)))})),()=>{t=!1,o.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&&c?t.createElement(o.Provider,{value:c},d):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},exports.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})),a=t.useRef();t.useEffect((()=>(a.current=o.observeState(i,r,n),()=>{a.current?.discard(),a.current=void 0})),[]),t.useEffect((()=>{a.current?.setDescription(r)}),[r]),t.useEffect((()=>{a.current?.setData(n)}),[n])}; | ||
"use strict";var e=require("@nlux/core"),t=require("react"),r=require("react-dom");const n=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)}))})),o=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await n(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 n(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},i=(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},s=async(t,r)=>{const n=i(t.events,r.events),s=i(t.layoutOptions,r.layoutOptions),a=i(t.conversationOptions,r.conversationOptions),u=i(t.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 o({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await o({user:t.user})).user),0!==Object.keys(r).length))return r})(t.personaOptions,r.personaOptions),l={};void 0!==n&&(l.events=n??{}),void 0!==s&&(l.layoutOptions=s??{}),void 0!==a&&(l.conversationOptions=a??{}),void 0!==u&&(l.promptBoxOptions=u??{}),void 0!==c&&(l.personaOptions=c??{});const d=t.adapter!==r.adapter?r.adapter:void 0;if(void 0!==d){const t=(t=>{let r="unknown";const n=t;if("function"==typeof n?.create)r="builder";else if("function"==typeof n?.fetchText||"function"==typeof n?.streamText)return r="instance",t;if("unknown"!==r){if("builder"===r){const t=n.create();return"function"==typeof t?.fetchText||"function"==typeof t?.streamText?t:void e.warn("The adapter builder did not return a valid adapter.")}return t}e.warn("Unable to determine the type of the adapter.")})(d);t?l.adapter=t:e.warn({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}t.className!==r.className&&(l.className=r.className),t.syntaxHighlighter!==r.syntaxHighlighter&&(l.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(l).length>0)return l};Object.defineProperty(exports,"NluxConfigError",{enumerable:!0,get:function(){return e.NluxConfigError}}),Object.defineProperty(exports,"NluxError",{enumerable:!0,get:function(){return e.NluxError}}),Object.defineProperty(exports,"NluxRenderingError",{enumerable:!0,get:function(){return e.NluxRenderingError}}),Object.defineProperty(exports,"NluxUsageError",{enumerable:!0,get:function(){return e.NluxUsageError}}),Object.defineProperty(exports,"NluxValidationError",{enumerable:!0,get:function(){return e.NluxValidationError}}),Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return e.debug}}),exports.AiChat=r=>{const n=t.useRef(null),[i,a]=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:i,className:s,syntaxHighlighter:a,layoutOptions:c,conversationOptions:l,promptBoxOptions:d,personaOptions:p,events:f,initialConversation:m}=r;let h=e.createAiChat().withAdapter(i);if(c&&(h=h.withLayoutOptions(c)),d&&(h=h.withPromptBoxOptions(d)),l&&(h=h.withConversationOptions(l)),s&&(h=h.withClassName(s)),a&&(h=h.withSyntaxHighlighter(a)),m&&(h=h.withInitialConversation(m)),f){const e=Object.keys(f);for(const t of e){const e=f[t];e&&h.on(t,e)}}return p?o(p).then((r=>{h=h.withPersonaOptions(r),t&&(n.current?(h.mount(n.current),u.current=h):e.warn("Root element is not defined! AiChat cannot be mounted."))})):(h.mount(n.current),u.current=h),()=>{t=!1,h?.unmount()}}),[]),t.useEffect((()=>{let t=!1;if(i)return u.current&&s(i,r).then((n=>{!t&&n&&(u.current?(u.current.updateProps(n),a(r)):e.warn("AiChat is not defined! Cannot update."))})),()=>{t=!0};a(r)}),[r,i]),t.createElement("div",{ref:n})},exports.createAiContext=r=>{const n=e.createAiContext(),o=t.createContext(n);return{Provider:n=>{const[i,s]=t.useState(),[a,u]=t.useState(),[c,l]=t.useState();t.useEffect((()=>{let t=!0;const o=e.createAiContext().withAdapter(r).withDataSyncOptions({syncStrategy:"auto",contextSize:e.predefinedContextSize["100k"]});return l(o),o.initialize(n.initialItems||{}).then((e=>{t&&(e.success?s(e.contextId):u(new Error(e.error)))})),()=>{t=!1,o.destroy()}}),[]);const{children:d}=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},d):n.loadingComponent?t.createElement(n.loadingComponent,null):null},ref:o}},exports.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})),s=t.useRef();t.useEffect((()=>(s.current=o.observeState(i,r,n),()=>{s.current?.discard(),s.current=void 0})),[]),t.useEffect((()=>{s.current?.setDescription(r)}),[r]),t.useEffect((()=>{s.current?.setData(n)}),[n])},exports.useAiTask=(e,r,n,o)=>{const i=t.useContext(e.ref),[s]=t.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(i.hasTask(e));return e})),a=t.useRef();t.useEffect((()=>(a.current=i.registerTask(s,r,n,o),()=>{a.current?.discard(),a.current=void 0})),[]),t.useEffect((()=>{a.current?.setDescription(r)}),[r]),t.useEffect((()=>{a.current?.setCallback(n)}),[n]),t.useEffect((()=>{a.current?.setParamDescriptions(o??[])}),[o])}; |
@@ -1,1 +0,1 @@ | ||
import{warn as t,createAiChat as e,createAiContext as r,predefinedContextSize as n}from"@nlux/core";export{NluxConfigError,NluxError,NluxRenderingError,NluxUsageError,NluxValidationError,debug}from"@nlux/core";import o,{useRef as i,useState as a,useEffect as s,useContext as c,createContext as u}from"react";import{render as l}from"react-dom";const p=t=>new Promise((e=>{const r=document.createElement("div");l(t,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");e(r.firstElementChild)}))})),d=async t=>{const[e,r]=await Promise.all([(async()=>{if(!t.bot)return;const e="string"==typeof t.bot.picture?t.bot.picture:await p(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 p(t.user.picture);return{name:t.user.name,picture:e}})()]);return{bot:e,user:r}},m=(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},f=async(e,r)=>{const n=m(e.events,r.events),o=m(e.layoutOptions,r.layoutOptions),i=m(e.conversationOptions,r.conversationOptions),a=m(e.promptBoxOptions,r.promptBoxOptions),s=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 d({bot:e.bot})).bot),t?.user!==e?.user&&(r.user=void 0===e?.user?void 0:(await d({user:e.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),c={};void 0!==n&&(c.events=n??{}),void 0!==o&&(c.layoutOptions=o??{}),void 0!==i&&(c.conversationOptions=i??{}),void 0!==a&&(c.promptBoxOptions=a??{}),void 0!==s&&(c.personaOptions=s??{});const u=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==u){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("The adapter builder did not return a valid adapter.")}return e}t("Unable to determine the type of the adapter.")})(u);e?c.adapter=e:t({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==r.className&&(c.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(c.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(c).length>0)return c},h=r=>{const n=i(null),[c,u]=a(null),l=i(null);return s((()=>{if(!n.current)throw new Error("Root element is not defined");let o=!0;const{adapter:i,className:a,syntaxHighlighter:s,layoutOptions:c,conversationOptions:u,promptBoxOptions:p,personaOptions:m,events:f,initialConversation:h}=r;let v=e().withAdapter(i);if(c&&(v=v.withLayoutOptions(c)),p&&(v=v.withPromptBoxOptions(p)),u&&(v=v.withConversationOptions(u)),a&&(v=v.withClassName(a)),s&&(v=v.withSyntaxHighlighter(s)),h&&(v=v.withInitialConversation(h)),f){const t=Object.keys(f);for(const e of t){const t=f[e];t&&v.on(e,t)}}return m?d(m).then((e=>{v=v.withPersonaOptions(e),o&&(n.current?(v.mount(n.current),l.current=v):t("Root element is not defined! AiChat cannot be mounted."))})):(v.mount(n.current),l.current=v),()=>{o=!1,v?.unmount()}}),[]),s((()=>{let e=!1;if(c)return l.current&&f(c,r).then((n=>{!e&&n&&(l.current?(l.current.updateProps(n),u(r)):t("AiChat is not defined! Cannot update."))})),()=>{e=!0};u(r)}),[r,c]),o.createElement("div",{ref:n})},v=(t,e,r)=>{const n=c(t.ref),[o]=a((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(n.hasItem(t));return t})),u=i();s((()=>(u.current=n.observeState(o,e,r),()=>{u.current?.discard(),u.current=void 0})),[]),s((()=>{u.current?.setDescription(e)}),[e]),s((()=>{u.current?.setData(r)}),[r])},y=t=>{const e=r(),i=u(e);return{Provider:e=>{const[a,c]=o.useState(),[u,l]=o.useState(),[p,d]=o.useState();s((()=>{let o=!0;const i=r().withAdapter(t).withDataSyncOptions({syncStrategy:"auto",contextSize:n["100k"]});return d(i),i.initialize(e.initialItems||{}).then((t=>{o&&(t.success?c(t.contextId):l(new Error(t.error)))})),()=>{o=!1,i.destroy()}}),[]);const{children:m}=e;return u?e.errorComponent?o.createElement(e.errorComponent,{error:u.message}):o.createElement("div",null,o.createElement("h1",null,"Error initializing AI context"),o.createElement("p",null,u.message)):a&&p?o.createElement(i.Provider,{value:p},m):e.loadingComponent?o.createElement(e.loadingComponent,null):null},ref:i}};export{h as AiChat,y as createAiContext,v as useAiContext}; | ||
import{warn as t,createAiChat as e,createAiContext as r,predefinedContextSize as n}from"@nlux/core";export{NluxConfigError,NluxError,NluxRenderingError,NluxUsageError,NluxValidationError,debug}from"@nlux/core";import o,{useRef as i,useState as s,useEffect as a,useContext as c,createContext as u}from"react";import{render as l}from"react-dom";const d=t=>new Promise((e=>{const r=document.createElement("div");l(t,r,(()=>{if(1!==r.children.length||!r.firstElementChild)throw new Error("Expected exactly one child");e(r.firstElementChild)}))})),p=async t=>{const[e,r]=await Promise.all([(async()=>{if(!t.bot)return;const e="string"==typeof t.bot.picture?t.bot.picture:await d(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 d(t.user.picture);return{name:t.user.name,picture:e}})()]);return{bot:e,user:r}},m=(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},f=async(e,r)=>{const n=m(e.events,r.events),o=m(e.layoutOptions,r.layoutOptions),i=m(e.conversationOptions,r.conversationOptions),s=m(e.promptBoxOptions,r.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 p({bot:e.bot})).bot),t?.user!==e?.user&&(r.user=void 0===e?.user?void 0:(await p({user:e.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),c={};void 0!==n&&(c.events=n??{}),void 0!==o&&(c.layoutOptions=o??{}),void 0!==i&&(c.conversationOptions=i??{}),void 0!==s&&(c.promptBoxOptions=s??{}),void 0!==a&&(c.personaOptions=a??{});const u=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==u){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("The adapter builder did not return a valid adapter.")}return e}t("Unable to determine the type of the adapter.")})(u);e?c.adapter=e:t({message:"Invalid new adapter property provided! The adapter must be an instance of ChatAdapter or ChatAdapterBuilder.",type:"invalid-adapter"})}e.className!==r.className&&(c.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(c.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(c).length>0)return c},h=r=>{const n=i(null),[c,u]=s(null),l=i(null);return a((()=>{if(!n.current)throw new Error("Root element is not defined");let o=!0;const{adapter:i,className:s,syntaxHighlighter:a,layoutOptions:c,conversationOptions:u,promptBoxOptions:d,personaOptions:m,events:f,initialConversation:h}=r;let v=e().withAdapter(i);if(c&&(v=v.withLayoutOptions(c)),d&&(v=v.withPromptBoxOptions(d)),u&&(v=v.withConversationOptions(u)),s&&(v=v.withClassName(s)),a&&(v=v.withSyntaxHighlighter(a)),h&&(v=v.withInitialConversation(h)),f){const t=Object.keys(f);for(const e of t){const t=f[e];t&&v.on(e,t)}}return m?p(m).then((e=>{v=v.withPersonaOptions(e),o&&(n.current?(v.mount(n.current),l.current=v):t("Root element is not defined! AiChat cannot be mounted."))})):(v.mount(n.current),l.current=v),()=>{o=!1,v?.unmount()}}),[]),a((()=>{let e=!1;if(c)return l.current&&f(c,r).then((n=>{!e&&n&&(l.current?(l.current.updateProps(n),u(r)):t("AiChat is not defined! Cannot update."))})),()=>{e=!0};u(r)}),[r,c]),o.createElement("div",{ref:n})},v=(t,e,r)=>{const n=c(t.ref),[o]=s((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(n.hasItem(t));return t})),u=i();a((()=>(u.current=n.observeState(o,e,r),()=>{u.current?.discard(),u.current=void 0})),[]),a((()=>{u.current?.setDescription(e)}),[e]),a((()=>{u.current?.setData(r)}),[r])},y=(t,e,r,n)=>{const o=c(t.ref),[u]=s((()=>{let t;do{t=Math.random().toString(36).substring(2,15)}while(o.hasTask(t));return t})),l=i();a((()=>(l.current=o.registerTask(u,e,r,n),()=>{l.current?.discard(),l.current=void 0})),[]),a((()=>{l.current?.setDescription(e)}),[e]),a((()=>{l.current?.setCallback(r)}),[r]),a((()=>{l.current?.setParamDescriptions(n??[])}),[n])},g=t=>{const e=r(),i=u(e);return{Provider:e=>{const[s,c]=o.useState(),[u,l]=o.useState(),[d,p]=o.useState();a((()=>{let o=!0;const i=r().withAdapter(t).withDataSyncOptions({syncStrategy:"auto",contextSize:n["100k"]});return p(i),i.initialize(e.initialItems||{}).then((t=>{o&&(t.success?c(t.contextId):l(new Error(t.error)))})),()=>{o=!1,i.destroy()}}),[]);const{children:m}=e;return u?e.errorComponent?o.createElement(e.errorComponent,{error:u.message}):o.createElement("div",null,o.createElement("h1",null,"Error initializing AI context"),o.createElement("p",null,u.message)):s&&d?o.createElement(i.Provider,{value:d},m):e.loadingComponent?o.createElement(e.loadingComponent,null):null},ref:i}};export{h as AiChat,g as createAiContext,v as useAiContext,y as useAiTask}; |
@@ -117,2 +117,33 @@ import { ChatAdapter, ChatAdapterBuilder, EventsMap, ConversationItem, HighlighterExtension, ConversationOptions, LayoutOptions, PromptBoxOptions, ContextItems, AiContext as AiContext$1, ContextItemDataType, ContextAdapter, ContextAdapterBuilder } from '@nlux/core'; | ||
/** | ||
* 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 | ||
@@ -150,2 +181,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 }; | ||
export { AiChat, type AiChatComponentProps, type AiContext, type AiContextProviderProps, type BotPersona, type DiscardContextItem, type PersonaOptions, type UpdateContextItem, type UserPersona, createAiContext, useAiContext, useAiTask }; |
{ | ||
"name": "@nlux/react", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"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.2" | ||
"@nlux/core": "1.0.3" | ||
}, | ||
@@ -65,0 +65,0 @@ "peerDependencies": { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/core"),require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","@nlux/core","react","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/react"]={},e.core,e.React,e.reactDom)}(this,(function(e,t,r,n){"use strict";const o=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)}))})),i=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await o(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 o(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},s=async(e,r)=>{const n=a(e.events,r.events),o=a(e.layoutOptions,r.layoutOptions),s=a(e.conversationOptions,r.conversationOptions),u=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 i({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await i({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),l={};void 0!==n&&(l.events=n??{}),void 0!==o&&(l.layoutOptions=o??{}),void 0!==s&&(l.conversationOptions=s??{}),void 0!==u&&(l.promptBoxOptions=u??{}),void 0!==c&&(l.personaOptions=c??{});const d=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==d){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.")})(d);e?l.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&&(l.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(l.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(l).length>0)return l};Object.defineProperty(e,"NluxConfigError",{enumerable:!0,get:function(){return t.NluxConfigError}}),Object.defineProperty(e,"NluxError",{enumerable:!0,get:function(){return t.NluxError}}),Object.defineProperty(e,"NluxRenderingError",{enumerable:!0,get:function(){return t.NluxRenderingError}}),Object.defineProperty(e,"NluxUsageError",{enumerable:!0,get:function(){return t.NluxUsageError}}),Object.defineProperty(e,"NluxValidationError",{enumerable:!0,get:function(){return t.NluxValidationError}}),Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return t.debug}}),e.AiChat=e=>{const n=r.useRef(null),[o,a]=r.useState(null),u=r.useRef(null);return r.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let r=!0;const{adapter:o,className:a,syntaxHighlighter:s,layoutOptions:c,conversationOptions:l,promptBoxOptions:d,personaOptions:p,events:f,initialConversation:m}=e;let h=t.createAiChat().withAdapter(o);if(c&&(h=h.withLayoutOptions(c)),d&&(h=h.withPromptBoxOptions(d)),l&&(h=h.withConversationOptions(l)),a&&(h=h.withClassName(a)),s&&(h=h.withSyntaxHighlighter(s)),m&&(h=h.withInitialConversation(m)),f){const e=Object.keys(f);for(const t of e){const e=f[t];e&&h.on(t,e)}}return p?i(p).then((e=>{h=h.withPersonaOptions(e),r&&(n.current?(h.mount(n.current),u.current=h):t.warn("Root element is not defined! AiChat cannot be mounted."))})):(h.mount(n.current),u.current=h),()=>{r=!1,h?.unmount()}}),[]),r.useEffect((()=>{let r=!1;if(o)return u.current&&s(o,e).then((n=>{!r&&n&&(u.current?(u.current.updateProps(n),a(e)):t.warn("AiChat is not defined! Cannot update."))})),()=>{r=!0};a(e)}),[e,o]),r.createElement("div",{ref:n})},e.createAiContext=e=>{const n=t.createAiContext(),o=r.createContext(n);return{Provider:n=>{const[i,a]=r.useState(),[s,u]=r.useState(),[c,l]=r.useState();r.useEffect((()=>{let r=!0;const o=t.createAiContext().withAdapter(e).withDataSyncOptions({syncStrategy:"auto",contextSize:t.predefinedContextSize["100k"]});return l(o),o.initialize(n.initialItems||{}).then((e=>{r&&(e.success?a(e.contextId):u(new Error(e.error)))})),()=>{r=!1,o.destroy()}}),[]);const{children:d}=n;return s?n.errorComponent?r.createElement(n.errorComponent,{error:s.message}):r.createElement("div",null,r.createElement("h1",null,"Error initializing AI context"),r.createElement("p",null,s.message)):i&&c?r.createElement(o.Provider,{value:c},d):n.loadingComponent?r.createElement(n.loadingComponent,null):null},ref:o}},e.useAiContext=(e,t,n)=>{const o=r.useContext(e.ref),[i]=r.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),a=r.useRef();r.useEffect((()=>(a.current=o.observeState(i,t,n),()=>{a.current?.discard(),a.current=void 0})),[]),r.useEffect((()=>{a.current?.setDescription(t)}),[t]),r.useEffect((()=>{a.current?.setData(n)}),[n])}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/core"),require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","@nlux/core","react","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/react"]={},e.core,e.React,e.reactDom)}(this,(function(e,t,r,n){"use strict";const o=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)}))})),i=async e=>{const[t,r]=await Promise.all([(async()=>{if(!e.bot)return;const t="string"==typeof e.bot.picture?e.bot.picture:await o(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 o(e.user.picture);return{name:e.user.name,picture:t}})()]);return{bot:t,user:r}},s=(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},a=async(e,r)=>{const n=s(e.events,r.events),o=s(e.layoutOptions,r.layoutOptions),a=s(e.conversationOptions,r.conversationOptions),u=s(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 i({bot:t.bot})).bot),e?.user!==t?.user&&(r.user=void 0===t?.user?void 0:(await i({user:t.user})).user),0!==Object.keys(r).length))return r})(e.personaOptions,r.personaOptions),l={};void 0!==n&&(l.events=n??{}),void 0!==o&&(l.layoutOptions=o??{}),void 0!==a&&(l.conversationOptions=a??{}),void 0!==u&&(l.promptBoxOptions=u??{}),void 0!==c&&(l.personaOptions=c??{});const d=e.adapter!==r.adapter?r.adapter:void 0;if(void 0!==d){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.")})(d);e?l.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&&(l.className=r.className),e.syntaxHighlighter!==r.syntaxHighlighter&&(l.syntaxHighlighter=r.syntaxHighlighter);if(Object.keys(l).length>0)return l};Object.defineProperty(e,"NluxConfigError",{enumerable:!0,get:function(){return t.NluxConfigError}}),Object.defineProperty(e,"NluxError",{enumerable:!0,get:function(){return t.NluxError}}),Object.defineProperty(e,"NluxRenderingError",{enumerable:!0,get:function(){return t.NluxRenderingError}}),Object.defineProperty(e,"NluxUsageError",{enumerable:!0,get:function(){return t.NluxUsageError}}),Object.defineProperty(e,"NluxValidationError",{enumerable:!0,get:function(){return t.NluxValidationError}}),Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return t.debug}}),e.AiChat=e=>{const n=r.useRef(null),[o,s]=r.useState(null),u=r.useRef(null);return r.useEffect((()=>{if(!n.current)throw new Error("Root element is not defined");let r=!0;const{adapter:o,className:s,syntaxHighlighter:a,layoutOptions:c,conversationOptions:l,promptBoxOptions:d,personaOptions:f,events:p,initialConversation:m}=e;let h=t.createAiChat().withAdapter(o);if(c&&(h=h.withLayoutOptions(c)),d&&(h=h.withPromptBoxOptions(d)),l&&(h=h.withConversationOptions(l)),s&&(h=h.withClassName(s)),a&&(h=h.withSyntaxHighlighter(a)),m&&(h=h.withInitialConversation(m)),p){const e=Object.keys(p);for(const t of e){const e=p[t];e&&h.on(t,e)}}return f?i(f).then((e=>{h=h.withPersonaOptions(e),r&&(n.current?(h.mount(n.current),u.current=h):t.warn("Root element is not defined! AiChat cannot be mounted."))})):(h.mount(n.current),u.current=h),()=>{r=!1,h?.unmount()}}),[]),r.useEffect((()=>{let r=!1;if(o)return u.current&&a(o,e).then((n=>{!r&&n&&(u.current?(u.current.updateProps(n),s(e)):t.warn("AiChat is not defined! Cannot update."))})),()=>{r=!0};s(e)}),[e,o]),r.createElement("div",{ref:n})},e.createAiContext=e=>{const n=t.createAiContext(),o=r.createContext(n);return{Provider:n=>{const[i,s]=r.useState(),[a,u]=r.useState(),[c,l]=r.useState();r.useEffect((()=>{let r=!0;const o=t.createAiContext().withAdapter(e).withDataSyncOptions({syncStrategy:"auto",contextSize:t.predefinedContextSize["100k"]});return l(o),o.initialize(n.initialItems||{}).then((e=>{r&&(e.success?s(e.contextId):u(new Error(e.error)))})),()=>{r=!1,o.destroy()}}),[]);const{children:d}=n;return a?n.errorComponent?r.createElement(n.errorComponent,{error:a.message}):r.createElement("div",null,r.createElement("h1",null,"Error initializing AI context"),r.createElement("p",null,a.message)):i&&c?r.createElement(o.Provider,{value:c},d):n.loadingComponent?r.createElement(n.loadingComponent,null):null},ref:o}},e.useAiContext=(e,t,n)=>{const o=r.useContext(e.ref),[i]=r.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(o.hasItem(e));return e})),s=r.useRef();r.useEffect((()=>(s.current=o.observeState(i,t,n),()=>{s.current?.discard(),s.current=void 0})),[]),r.useEffect((()=>{s.current?.setDescription(t)}),[t]),r.useEffect((()=>{s.current?.setData(n)}),[n])},e.useAiTask=(e,t,n,o)=>{const i=r.useContext(e.ref),[s]=r.useState((()=>{let e;do{e=Math.random().toString(36).substring(2,15)}while(i.hasTask(e));return e})),a=r.useRef();r.useEffect((()=>(a.current=i.registerTask(s,t,n,o),()=>{a.current?.discard(),a.current=void 0})),[]),r.useEffect((()=>{a.current?.setDescription(t)}),[t]),r.useEffect((()=>{a.current?.setCallback(n)}),[n]),r.useEffect((()=>{a.current?.setParamDescriptions(o??[])}),[o])}})); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30080
241
0
+ Added@nlux/core@1.0.3(transitive)
- Removed@nlux/core@1.0.2(transitive)
Updated@nlux/core@1.0.3