@nlux/nlbridge
Advanced tools
Comparing version 2.0.4-alpha to 2.0.5-alpha
@@ -1,1 +0,1 @@ | ||
"use strict";var t=Object.defineProperty,e=(e,s,r)=>(((e,s,r)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[s]=r})(e,"symbol"!=typeof s?s+"":s,r),r);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class r extends s{}class o{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}o.defaultDataTransferMode="stream";class n extends o{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const r="copilot"===this.usageMode?"assist":"chat",o=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:r,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!o.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${o.status}`});const n=await o.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new r({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends o{constructor(t){super(t)}async fetchText(t,e){throw new r({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const r=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),r=new TextDecoder;let o=!1;for(;!o;){const{value:t,done:n}=await s.read();if(n)o=!0;else try{const s=r.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{r()})).catch((()=>{r()})):r()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new r({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??o.defaultDataTransferMode)?new a(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new r({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new r({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new r({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new r({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}class h{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const r=await s.json();return r?.result?.contextId?{success:!0,contextId:r.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,r){if(!t)return{success:!1,error:"Invalid context ID"};try{const o=await fetch(this.url,{method:"POST",headers:{...r?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!o.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await o.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class d{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new h(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}exports.createChatAdapter=()=>new c,exports.createContextAdapter=()=>new d; | ||
"use strict";var t=Object.defineProperty,e=(e,s,r)=>((e,s,r)=>s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[s]=r)(e,"symbol"!=typeof s?s+"":s,r);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class r extends s{}class o{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}o.defaultDataTransferMode="stream";class n extends o{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const r="copilot"===this.usageMode?"assist":"chat",o=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:r,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!o.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${o.status}`});const n=await o.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new r({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends o{constructor(t){super(t)}async fetchText(t,e){throw new r({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const r=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),r=new TextDecoder;let o=!1;for(;!o;){const{value:t,done:n}=await s.read();if(n)o=!0;else try{const s=r.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{r()})).catch((()=>{r()})):r()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new r({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??o.defaultDataTransferMode)?new a(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new r({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new r({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new r({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new r({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}class h{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const r=await s.json();return r?.result?.contextId?{success:!0,contextId:r.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,r){if(!t)return{success:!1,error:"Invalid context ID"};try{const o=await fetch(this.url,{method:"POST",headers:{...r?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!o.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await o.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class d{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new h(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}exports.createChatAdapter=()=>new c,exports.createContextAdapter=()=>new d; |
@@ -1,1 +0,1 @@ | ||
var t=Object.defineProperty,e=(e,s,o)=>(((e,s,o)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[s]=o})(e,"symbol"!=typeof s?s+"":s,o),o);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class o extends s{}class r{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}r.defaultDataTransferMode="stream";class n extends r{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const o="copilot"===this.usageMode?"assist":"chat",r=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:o,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!r.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new o({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends r{constructor(t){super(t)}async fetchText(t,e){throw new o({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),o=new TextDecoder;let r=!1;for(;!r;){const{value:t,done:n}=await s.read();if(n)r=!0;else try{const s=o.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new o({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??r.defaultDataTransferMode)?new a(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new o({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new o({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new o({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new o({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const h=()=>new c;class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const r=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!r.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await r.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}const x=()=>new u;export{h as createChatAdapter,x as createContextAdapter}; | ||
var t=Object.defineProperty,e=(e,s,o)=>((e,s,o)=>s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[s]=o)(e,"symbol"!=typeof s?s+"":s,o);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class o extends s{}class r{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}r.defaultDataTransferMode="stream";class n extends r{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const o="copilot"===this.usageMode?"assist":"chat",r=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:o,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!r.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new o({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends r{constructor(t){super(t)}async fetchText(t,e){throw new o({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),o=new TextDecoder;let r=!1;for(;!r;){const{value:t,done:n}=await s.read();if(n)r=!0;else try{const s=o.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new o({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??r.defaultDataTransferMode)?new a(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new o({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new o({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new o({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new o({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const h=()=>new c;class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const r=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!r.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await r.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}const x=()=>new u;export{h as createChatAdapter,x as createContextAdapter}; |
{ | ||
"name": "@nlux/nlbridge", | ||
"version": "2.0.4-alpha", | ||
"version": "2.0.5-alpha", | ||
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.", | ||
@@ -62,3 +62,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/core": "2.0.4-alpha" | ||
"@nlux/core": "2.0.5-alpha" | ||
}, | ||
@@ -65,0 +65,0 @@ "peerDependencies": {}, |
@@ -1,6 +0,6 @@ | ||
# `nlbridge` adapter for `nlux JS` | ||
# `nlbridge` adapter for `NLUX JS` | ||
[![Free And Open Source](https://img.shields.io/badge/Free%20%26%20Open%20Source-%2348c342)](https://github.com/nluxai/nlux) [![Docs nlux.ai](https://img.shields.io/badge/Docs_Website-nlux.dev-%23fa896b)](https://nlux.dev) | ||
[![Free And Open Source](https://img.shields.io/badge/Free%20%26%20Open%20Source-%2348c342)](https://github.com/nluxai/nlux) [![Docs nlux.dev](https://img.shields.io/badge/Docs_Website-nlux.dev-%23fa896b)](https://nlux.dev) | ||
This package enables the integration between `nlux JS` and `nlbridge` ― the middleware library | ||
This package enables the integration between `NLUX JS` and `nlbridge` ― the middleware library | ||
for building conversational AI interfaces. | ||
@@ -14,7 +14,7 @@ | ||
`nlbridge` is developed by the same team behind `nlux`, and is designed to work seamlessly with `nlux`. | ||
`nlbridge` is developed by the same team behind `NLUX`, and is designed to work seamlessly with `NLUX`. | ||
### Vanilla JS 🟨 vs React JS ⚛️ | ||
This package `@nlux/nlbridge` is meant for use with the vanilla JS version of `nlux`. | ||
This package `@nlux/nlbridge` is meant for use with the vanilla JS version of `NLUX`. | ||
If you're looking for the React JS version, please check | ||
@@ -21,0 +21,0 @@ the [`@nlux/nlbridge-react`](https://www.npmjs.com/package/@nlux/nlbridge-react) package. |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@nlux/nlbridge"]={})}(this,(function(t){"use strict";var e=Object.defineProperty,s=(t,s,o)=>(((t,s,o)=>{s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[s]=o})(t,"symbol"!=typeof s?s+"":s,o),o);class o extends Error{constructor(t={}){super(t.message),s(this,"exceptionId"),s(this,"message"),s(this,"source"),s(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class r extends o{}class n{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}n.defaultDataTransferMode="stream";class i extends n{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const s="copilot"===this.usageMode?"assist":"chat",r=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:s,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!r.ok)throw new o({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new o({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new r({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const a=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class c extends n{constructor(t){super(t)}async fetchText(t,e){throw new r({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),o=new TextDecoder;let r=!1;for(;!r;){const{value:t,done:n}=await s.read();if(n)r=!0;else try{const s=o.decode(t);e.next(s)}catch(t){a(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class h{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new r({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??n.defaultDataTransferMode)?new c(t):new i(t)}withContext(t){if(void 0!==this.theContext)throw new r({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new r({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new r({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new r({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const r=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!r.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await r.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}t.createChatAdapter=()=>new h,t.createContextAdapter=()=>new u})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@nlux/nlbridge"]={})}(this,(function(t){"use strict";var e=Object.defineProperty,s=(t,s,o)=>((t,s,o)=>s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[s]=o)(t,"symbol"!=typeof s?s+"":s,o);class o extends Error{constructor(t={}){super(t.message),s(this,"exceptionId"),s(this,"message"),s(this,"source"),s(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class r extends o{}class n{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream",this.theHeaders=t.headers??{}}get context(){return this.theAiContextToUse}get headers(){return this.theHeaders}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}n.defaultDataTransferMode="stream";class i extends n{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const s="copilot"===this.usageMode?"assist":"chat",r=await fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:s,payload:{message:t,conversationHistory:e.conversationHistory,contextId:this.context?.contextId}})});if(!r.ok)throw new o({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new o({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new r({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const a=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class c extends n{constructor(t){super(t)}async fetchText(t,e){throw new r({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),o=new TextDecoder;let r=!1;for(;!r;){const{value:t,done:n}=await s.read();if(n)r=!0;else try{const s=o.decode(t);e.next(s)}catch(t){a(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class h{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new r({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(t.mode??n.defaultDataTransferMode)?new c(t):new i(t)}withContext(t){if(void 0!==this.theContext)throw new r({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new r({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}withMode(t){if(void 0!==this.theMode)throw new r({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new r({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const r=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!r.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await r.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}t.createChatAdapter=()=>new h,t.createContextAdapter=()=>new u})); |
24340
+ Added@nlux/core@2.0.5-alpha(transitive)
- Removed@nlux/core@2.0.4-alpha(transitive)
Updated@nlux/core@2.0.5-alpha