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 2.0.12-alpha to 2.1.0-beta

2

cjs/nlbridge.js

@@ -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);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=>{const 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,this.theHeaders=t.theHeaders)}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,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new h(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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{}const o=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};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=>{const 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 dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get headers(){return this.theHeaders}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}preProcessAiStreamedChunk(t,e){return"string"==typeof t?t:(o("NLBridge adapter received a non-string chunk from the server. Returning empty string."),"")}preProcessAiUnifiedMessage(t,e){return"string"==typeof t?t:(o("NLBridge adapter received a non-string message from the server. Returning empty string."),"")}}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 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!"})}}class a 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 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 n=!1;for(;!n;){const{value:t,done:i}=await s.read();if(i)n=!0;else try{const s=r.decode(t);e.next(s)}catch(t){o(`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,this.theHeaders=t.theHeaders)}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 a(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 h{constructor(t,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new h(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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,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=>{const 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,this.theHeaders=t.theHeaders)}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}}const h=()=>new c;class d{constructor(t,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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 u{constructor(){this.endpointUrl=void 0,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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,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{}const o=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};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=>{const 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 dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get headers(){return this.theHeaders}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}preProcessAiStreamedChunk(t,e){return"string"==typeof t?t:(o("NLBridge adapter received a non-string chunk from the server. Returning empty string."),"")}preProcessAiUnifiedMessage(t,e){return"string"==typeof t?t:(o("NLBridge adapter received a non-string message from the server. Returning empty string."),"")}}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 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!"})}}class a 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 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 n=!1;for(;!n;){const{value:t,done:i}=await s.read();if(i)n=!0;else try{const s=r.decode(t);e.next(s)}catch(t){o(`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,this.theHeaders=t.theHeaders)}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 a(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}}const h=()=>new c;class d{constructor(t,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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 u{constructor(){this.endpointUrl=void 0,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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.12-alpha",
"version": "2.1.0-beta",
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.",

@@ -62,3 +62,3 @@ "keywords": [

"dependencies": {
"@nlux/core": "2.0.12-alpha"
"@nlux/core": "2.1.0-beta"
},

@@ -65,0 +65,0 @@ "peerDependencies": {},

@@ -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);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=>{const 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,this.theHeaders=t.theHeaders)}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,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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,r)=>((t,s,r)=>s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[s]=r)(t,"symbol"!=typeof s?s+"":s,r);class r 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 o extends r{}const n=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class i{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{const 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 dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get headers(){return this.theHeaders}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}preProcessAiStreamedChunk(t,e){return"string"==typeof t?t:(n("NLBridge adapter received a non-string chunk from the server. Returning empty string."),"")}preProcessAiUnifiedMessage(t,e){return"string"==typeof t?t:(n("NLBridge adapter received a non-string message from the server. Returning empty string."),"")}}i.defaultDataTransferMode="stream";class a extends i{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const s="copilot"===this.usageMode?"assist":"chat",o=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(!o.ok)throw new r({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 r({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!"})}}class c extends i{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 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:i}=await s.read();if(i)o=!0;else try{const s=r.decode(t);e.next(s)}catch(t){n(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}))};this.context&&this.context.contextId?this.context.flush().then((()=>r())).catch((()=>r())):r()}}class h{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext,this.theHeaders=t.theHeaders)}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??i.defaultDataTransferMode)?new c(t):new a(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}}class d{constructor(t,e){this.url=t,this.headers=e??{}}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...this.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 u{constructor(){this.endpointUrl=void 0,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl,this.headers)}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}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}));
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