Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nlux/nlbridge

Package Overview
Dependencies
Maintainers
1
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nlux/nlbridge - npm Package Compare versions

Comparing version 0.12.2 to 1.0.0

2

cjs/nlbridge.js

@@ -1,1 +0,1 @@

"use strict";var t=require("@nlux/core");const e=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};e.defaultDataTransferMode="stream";let r=e;class s extends r{constructor(t){super(t)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NlBridge adapter returned status code: ${s.status}`});const n=await s.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.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NlBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class n extends r{constructor(t){super(t)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NlBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NlBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let a=!1;for(;!a;){const{value:e,done:o}=await s.read();if(o)a=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NlBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NlBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??r.defaultDataTransferMode)?new n(e):new s(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class o{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,a=s?.result?.taskId,o=s?.result?.parameters;return a&&Array.isArray(o)?{success:!0,response:n,taskId:a,parameters:o}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const r=await e.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new o(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}}Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.createChatAdapter=()=>new a,exports.createContextAdapter=()=>new c;
"use strict";var t=require("@nlux/core");const e=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};e.defaultDataTransferMode="stream";let r=e;class s extends r{constructor(t){super(t)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NLBridge adapter returned status code: ${s.status}`});const n=await s.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.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class n extends r{constructor(t){super(t)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NLBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NLBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let a=!1;for(;!a;){const{value:e,done:o}=await s.read();if(o)a=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??r.defaultDataTransferMode)?new n(e):new s(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class o{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,a=s?.result?.taskId,o=s?.result?.parameters;return a&&Array.isArray(o)?{success:!0,response:n,taskId:a,parameters:o}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const r=await e.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new o(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}}Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.createChatAdapter=()=>new a,exports.createContextAdapter=()=>new c;

@@ -1,1 +0,1 @@

import{uid as t,NluxError as e,NluxUsageError as s,warn as r}from"@nlux/core";export{debug}from"@nlux/core";const n=class e{constructor(s){this.__instanceId=`${this.info.id}-${t()}`,this.theDataTransferModeToUse=s.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=s.url,this.theContextIdToUse=s.contextId,this.theTaskRunnerToUse=s.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};n.defaultDataTransferMode="stream";let o=n;class a extends o{constructor(t){super(t)}async fetchText(t,s){const r=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:t,contextId:this.contextId}})});if(!r.ok)throw new e({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.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new e({source:this.constructor.name,message:"Invalid response from NlBridge: String expected."})}streamText(t,e,r){throw new s({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class c extends o{constructor(t){super(t)}async fetchText(t,e){throw new s({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,contextId:this.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(),n=new TextDecoder;let o=!1;for(;!o;){const{value:t,done:a}=await s.read();if(a)o=!0;else try{const s=n.decode(t);e.next(s)}catch(t){r(`Error parsing chunk by NlBridgeStreamAdapter: ${t}`)}}e.complete()}))}}class i{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new s({source:this.constructor.name,message:"Unable to create NlBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const t={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(t.dataTransferMode??o.defaultDataTransferMode)?new c(t):new a(t)}withContextId(t){if(void 0!==this.theContextId)throw new s({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=t,this}withDataTransferMode(t){if(void 0!==this.theDataTransferMode)throw new s({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=t,this}withTaskRunner(t){if(void 0!==this.theTaskRunner)throw new s({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=t,this}withUrl(t){if(void 0!==this.theUrl)throw new s({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const d=()=>new i;class h{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const s=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!s.ok)return{success:!1,error:"Failed to assist"};const r=await s.json(),n=r?.result?.response,o=r?.result?.taskId,a=r?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,s){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:s}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const s=await e.json(),r=s?.result?.contextId;return{success:!0,contextId:r}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class u{constructor(){this.endpointUrl=void 0}create(){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}}const l=()=>new u;export{d as createChatAdapter,l as createContextAdapter};
import{uid as t,NluxError as e,NluxUsageError as s,warn as r}from"@nlux/core";export{debug}from"@nlux/core";const n=class e{constructor(s){this.__instanceId=`${this.info.id}-${t()}`,this.theDataTransferModeToUse=s.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=s.url,this.theContextIdToUse=s.contextId,this.theTaskRunnerToUse=s.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};n.defaultDataTransferMode="stream";let o=n;class a extends o{constructor(t){super(t)}async fetchText(t,s){const r=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:t,contextId:this.contextId}})});if(!r.ok)throw new e({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.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new e({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,r){throw new s({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class c extends o{constructor(t){super(t)}async fetchText(t,e){throw new s({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,contextId:this.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(),n=new TextDecoder;let o=!1;for(;!o;){const{value:t,done:a}=await s.read();if(a)o=!0;else try{const s=n.decode(t);e.next(s)}catch(t){r(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}))}}class i{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new s({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const t={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(t.dataTransferMode??o.defaultDataTransferMode)?new c(t):new a(t)}withContextId(t){if(void 0!==this.theContextId)throw new s({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=t,this}withDataTransferMode(t){if(void 0!==this.theDataTransferMode)throw new s({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=t,this}withTaskRunner(t){if(void 0!==this.theTaskRunner)throw new s({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=t,this}withUrl(t){if(void 0!==this.theUrl)throw new s({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const d=()=>new i;class h{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const s=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!s.ok)return{success:!1,error:"Failed to assist"};const r=await s.json(),n=r?.result?.response,o=r?.result?.taskId,a=r?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,s){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:s}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const s=await e.json(),r=s?.result?.contextId;return{success:!0,contextId:r}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class u{constructor(){this.endpointUrl=void 0}create(){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}}const l=()=>new u;export{d as createChatAdapter,l as createContextAdapter};

@@ -1,17 +0,7 @@

import { DataTransferMode, AdapterBuilder, StandardAdapter, AiContextAdapter, ContextAdapterBuilder } from '@nlux/core';
export { Adapter, AiContextAdapter, ContextAdapterBuilder, DataTransferMode, StandardAdapter, StreamingAdapterObserver, debug } from '@nlux/core';
import { AssistResult } from '@nlux-dev/core/src/types/aiAssistant/assist';
import { RegisterTaskResult } from '@nlux-dev/core/src/types/aiAssistant/registerTask';
import { UnregisterTaskResult } from '@nlux-dev/core/src/types/aiAssistant/unregisterTask';
import { ClearContextResult } from '@nlux-dev/core/src/types/aiContext/clear';
import { ContextData } from '@nlux-dev/core/src/types/aiContext/data';
import { GetContextDataResult } from '@nlux-dev/core/src/types/aiContext/get';
import { SetContextResult } from '@nlux-dev/core/src/types/aiContext/set';
import { UpdateContextResult } from '@nlux-dev/core/src/types/aiContext/update';
import { DataTransferMode, AiTaskRunner, ChatAdapterBuilder as ChatAdapterBuilder$1, StandardChatAdapter, ContextAdapterBuilder as ContextAdapterBuilder$1, ContextAdapter } from '@nlux/core';
export { AiTaskRunner, ChatAdapter, ContextAdapter, DataTransferMode, StandardChatAdapter, StreamingAdapterObserver, debug } from '@nlux/core';
type AiTaskRunner = (taskId: string, params: Array<any>) => void;
type ChatAdapterOptions = {
/**
* The URL of the NlBridge endpoint.
* The URL of the NLBridge endpoint.
*

@@ -21,3 +11,3 @@ */

/**
* The data transfer mode to use when communicating with NlBridge.
* The data transfer mode to use when communicating with NLBridge.
* If not provided, the adapter will use `stream` mode.

@@ -27,3 +17,3 @@ */

/**
* The context ID to use when communicating with NlBridge.
* The context ID to use when communicating with NLBridge.
* Optional. If not provided, the adapter will not use a context.

@@ -38,4 +28,4 @@ */

interface ChatAdapterBuilder extends AdapterBuilder {
create(): StandardAdapter;
interface ChatAdapterBuilder extends ChatAdapterBuilder$1 {
create(): StandardChatAdapter;
withContextId(contextId: string): ChatAdapterBuilder;

@@ -49,21 +39,9 @@ withDataTransferMode(mode: DataTransferMode): ChatAdapterBuilder;

declare class ContextAdapter implements AiContextAdapter {
private url;
constructor(url: string);
assist(contextId: string, message: string): Promise<AssistResult>;
clear(contextId: string): Promise<ClearContextResult>;
get(contextId: string, itemId: string | undefined): Promise<GetContextDataResult>;
registerTask(contextId: string, taskId: string, parameters: string[]): Promise<RegisterTaskResult>;
set(initialData: Record<string, any> | undefined): Promise<SetContextResult>;
unregisterTask(contextId: string, taskId: string): Promise<UnregisterTaskResult>;
update(contextId: string, data: ContextData | null): Promise<UpdateContextResult>;
interface ContextAdapterBuilder extends ContextAdapterBuilder$1 {
create(): ContextAdapter;
withUrl(endpointUrl: string): ContextAdapterBuilder;
}
interface NlBridgeContextAdapterBuilder extends ContextAdapterBuilder {
create(): AiContextAdapter;
withUrl(endpointUrl: string): NlBridgeContextAdapterBuilder;
}
declare const createContextAdapter: () => ContextAdapterBuilder;
declare const createContextAdapter: () => NlBridgeContextAdapterBuilder;
export { type AiTaskRunner, type ChatAdapterBuilder, type ChatAdapterOptions, ContextAdapter, createChatAdapter, createContextAdapter };
export { type ChatAdapterBuilder, type ChatAdapterOptions, type ContextAdapterBuilder, createChatAdapter, createContextAdapter };
{
"name": "@nlux/nlbridge",
"version": "0.12.2",
"version": "1.0.0",
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.",

@@ -14,2 +14,5 @@ "keywords": [

"ai-bot",
"ai-assistant",
"ai-copilot",
"copilot",
"chat-gpt",

@@ -60,3 +63,3 @@ "openai",

"dependencies": {
"@nlux/core": "0.12.2"
"@nlux/core": "1.0.0"
},

@@ -63,0 +66,0 @@ "peerDependencies": {},

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/core")):"function"==typeof define&&define.amd?define(["exports","@nlux/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/nlbridge"]={},e.core)}(this,(function(e,t){"use strict";const r=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(e){}async encode(e){}};r.defaultDataTransferMode="stream";let s=r;class n extends s{constructor(e){super(e)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NlBridge adapter returned status code: ${s.status}`});const n=await s.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:e,task:t}=n.result;return this.taskRunner&&t&&"object"==typeof t&&"string"==typeof t.taskId&&Array.isArray(t.parameters)&&this.taskRunner(t.taskId,t.parameters),e}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NlBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class o extends s{constructor(e){super(e)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NlBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NlBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let o=!1;for(;!o;){const{value:e,done:a}=await s.read();if(a)o=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NlBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(e){e&&(this.theDataTransferMode=e.theDataTransferMode,this.theUrl=e.theUrl,this.theContextId=e.theContextId,this.theTaskRunner=e.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NlBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??s.defaultDataTransferMode)?new o(e):new n(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class i{constructor(e){this.url=e}async assist(e,t){if(!e)return{success:!1,error:"Invalid context ID"};if(!t)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:e,message:t}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,o=s?.result?.taskId,a=s?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(e){return{success:!1,error:"Failed to assist"}}}async clear(e){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(e){return{success:!1,error:"Failed to clear context"}}}async get(e,t){return{success:!1,error:"Not implemented"}}async registerTask(e,t,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:e,taskId:t,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(e){return{success:!1,error:"Failed to register task"}}}async set(e){try{const t=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:e||null}})});if(!t.ok)return{success:!1,error:"Failed to set context"};const r=await t.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(e){return{success:!1,error:"Failed to set context"}}}async unregisterTask(e,t){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:e,taskId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(e){return{success:!1,error:"Failed to unregister task"}}}async update(e,t){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:e,data:t}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(e){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new i(this.endpointUrl)}withUrl(e){if(void 0!==this.endpointUrl&&this.endpointUrl!==e)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=e,this}}Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return t.debug}}),e.createChatAdapter=()=>new a,e.createContextAdapter=()=>new c}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/core")):"function"==typeof define&&define.amd?define(["exports","@nlux/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/nlbridge"]={},e.core)}(this,(function(e,t){"use strict";const r=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}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 taskRunner(){return this.theTaskRunnerToUse}async decode(e){}async encode(e){}};r.defaultDataTransferMode="stream";let s=r;class n extends s{constructor(e){super(e)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NLBridge adapter returned status code: ${s.status}`});const n=await s.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:e,task:t}=n.result;return this.taskRunner&&t&&"object"==typeof t&&"string"==typeof t.taskId&&Array.isArray(t.parameters)&&this.taskRunner(t.taskId,t.parameters),e}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class o extends s{constructor(e){super(e)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NLBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NLBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let o=!1;for(;!o;){const{value:e,done:a}=await s.read();if(a)o=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(e){e&&(this.theDataTransferMode=e.theDataTransferMode,this.theUrl=e.theUrl,this.theContextId=e.theContextId,this.theTaskRunner=e.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??s.defaultDataTransferMode)?new o(e):new n(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class i{constructor(e){this.url=e}async assist(e,t){if(!e)return{success:!1,error:"Invalid context ID"};if(!t)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:e,message:t}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,o=s?.result?.taskId,a=s?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(e){return{success:!1,error:"Failed to assist"}}}async clear(e){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(e){return{success:!1,error:"Failed to clear context"}}}async get(e,t){return{success:!1,error:"Not implemented"}}async registerTask(e,t,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:e,taskId:t,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(e){return{success:!1,error:"Failed to register task"}}}async set(e){try{const t=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:e||null}})});if(!t.ok)return{success:!1,error:"Failed to set context"};const r=await t.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(e){return{success:!1,error:"Failed to set context"}}}async unregisterTask(e,t){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:e,taskId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(e){return{success:!1,error:"Failed to unregister task"}}}async update(e,t){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:e,data:t}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(e){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new i(this.endpointUrl)}withUrl(e){if(void 0!==this.endpointUrl&&this.endpointUrl!==e)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=e,this}}Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return t.debug}}),e.createChatAdapter=()=>new a,e.createContextAdapter=()=>new c}));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc