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 1.0.14 to 1.0.15

2

cjs/nlbridge.js

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

"use strict";var t=require("@nlux/core");class e{constructor(e){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${t.uid()}`,this.theUsageMode=e.mode,this.theEndpointUrlToUse=e.url,this.theAiContextToUse=e.context,this.theDataTransferModeToUse="copilot"===e.mode&&e.context?"fetch":"stream",this.theHeaders=e.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}}e.defaultDataTransferMode="stream";class s extends e{constructor(t){super(t)}async fetchText(e,s){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:e,contextId:this.context?.contextId}})});if(!o.ok)throw new t.NluxError({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 t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,s,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class r extends e{constructor(t){super(t)}async fetchText(e,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,s,r){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.context?.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 r=e.body.getReader(),o=new TextDecoder;let n=!1;for(;!n;){const{value:e,done:a}=await r.read();if(a)n=!0;else try{const t=o.decode(e);s.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}s.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class o{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 t.NluxUsageError({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 o={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(o.mode??e.defaultDataTransferMode)?new r(o):new s(o)}withContext(e){if(void 0!==this.theContext)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=e,this}withMode(e){if(void 0!==this.theMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=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}withHeaders(e){if(void 0!==this.theHeaders)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=e,this}}class n{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:{...this.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 a{constructor(){this.endpointUrl=void 0,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new n(this.endpointUrl,this.headers)}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}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}}Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.createChatAdapter=()=>new o,exports.createContextAdapter=()=>new a;
"use strict";var t=require("@nlux/core");class e{constructor(e){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${t.uid()}`,this.theUsageMode=e.mode,this.theEndpointUrlToUse=e.url,this.theAiContextToUse=e.context,this.theDataTransferModeToUse="copilot"===e.mode&&e.context?"fetch":"stream",this.theHeaders=e.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}}e.defaultDataTransferMode="stream";class s extends e{constructor(t){super(t)}async fetchText(e,s){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:e,conversationHistory:s.conversationHistory,contextId:this.context?.contextId}})});if(!o.ok)throw new t.NluxError({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 t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,s,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class r extends e{constructor(t){super(t)}async fetchText(e,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,s,r){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,conversationHistory:r.conversationHistory,contextId:this.context?.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 r=e.body.getReader(),o=new TextDecoder;let n=!1;for(;!n;){const{value:e,done:a}=await r.read();if(a)n=!0;else try{const t=o.decode(e);s.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}s.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class o{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}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 call withUrl() or provide url option before calling creating the adapter."});const o={url:this.theUrl,mode:this.theMode,context:this.theContext,headers:this.theHeaders};return"stream"===(o.mode??e.defaultDataTransferMode)?new r(o):new s(o)}withContext(e){if(void 0!==this.theContext)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=e,this}withMode(e){if(void 0!==this.theMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=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}withHeaders(e){if(void 0!==this.theHeaders)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=e,this}}class n{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 a{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new n(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 o,exports.createContextAdapter=()=>new a;

@@ -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";class o{constructor(e){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${t()}`,this.theUsageMode=e.mode,this.theEndpointUrlToUse=e.url,this.theAiContextToUse=e.context,this.theDataTransferModeToUse="copilot"===e.mode&&e.context?"fetch":"stream",this.theHeaders=e.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,s){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,contextId:this.context?.contextId}})});if(!o.ok)throw new e({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 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 i 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){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,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 n=!1;for(;!n;){const{value:t,done:i}=await s.read();if(i)n=!0;else try{const s=o.decode(t);e.next(s)}catch(t){r(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class a{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 s({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 i(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new s({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withMode(t){if(void 0!==this.theMode)throw new s({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 s({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new s({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}}const h=()=>new a;class c{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:{...this.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 c(this.endpointUrl,this.headers)}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}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}}const u=()=>new d;export{h as createChatAdapter,u as createContextAdapter};
import{uid as t,NluxError as e,NluxUsageError as s,warn as o}from"@nlux/core";export{debug}from"@nlux/core";class r{constructor(e){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${t()}`,this.theUsageMode=e.mode,this.theEndpointUrlToUse=e.url,this.theAiContextToUse=e.context,this.theDataTransferModeToUse="copilot"===e.mode&&e.context?"fetch":"stream",this.theHeaders=e.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,s){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:s.conversationHistory,contextId:this.context?.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.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new e({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,o){throw new s({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class i extends r{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){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 a{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new s({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 i(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new s({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withMode(t){if(void 0!==this.theMode)throw new s({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 s({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new s({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}}const c=()=>new a;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 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 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}}const u=()=>new d;export{c as createChatAdapter,u as createContextAdapter};

@@ -45,3 +45,2 @@ import { AiContext, ChatAdapterBuilder as ChatAdapterBuilder$1, StandardChatAdapter, ContextAdapterBuilder as ContextAdapterBuilder$1, ContextAdapter } from '@nlux/core';

withUrl(endpointUrl: string): ContextAdapterBuilder;
withHeaders(headers: Record<string, string>): ContextAdapterBuilder;
}

@@ -48,0 +47,0 @@

{
"name": "@nlux/nlbridge",
"version": "1.0.14",
"version": "1.0.15",
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.",

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

"dependencies": {
"@nlux/core": "1.0.14"
"@nlux/core": "1.0.15"
},

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

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

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@nlux/core")):"function"==typeof define&&define.amd?define(["exports","@nlux/core"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@nlux/nlbridge"]={},t.core)}(this,(function(t,e){"use strict";class s{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${e.uid()}`,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}}s.defaultDataTransferMode="stream";class r extends s{constructor(t){super(t)}async fetchText(t,s){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,contextId:this.context?.contextId}})});if(!o.ok)throw new e.NluxError({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 e.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,s,r){throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class o extends s{constructor(t){super(t)}async fetchText(t,s){throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,s,r){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,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 r=t.body.getReader(),o=new TextDecoder;let n=!1;for(;!n;){const{value:t,done:i}=await r.read();if(i)n=!0;else try{const e=o.decode(t);s.next(e)}catch(t){e.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}s.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class n{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 e.NluxUsageError({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??s.defaultDataTransferMode)?new o(t):new r(t)}withContext(t){if(void 0!==this.theContext)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withMode(t){if(void 0!==this.theMode)throw new e.NluxUsageError({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 e.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}}class i{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:{...this.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 a{constructor(){this.endpointUrl=void 0,this.headers=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new i(this.endpointUrl,this.headers)}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}withHeaders(t){if(void 0!==this.headers)throw new Error("Cannot set the headers more than once");return this.headers=t,this}}Object.defineProperty(t,"debug",{enumerable:!0,get:function(){return e.debug}}),t.createChatAdapter=()=>new n,t.createContextAdapter=()=>new a}));
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@nlux/core")):"function"==typeof define&&define.amd?define(["exports","@nlux/core"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@nlux/nlbridge"]={},t.core)}(this,(function(t,e){"use strict";class s{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${e.uid()}`,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}}s.defaultDataTransferMode="stream";class r extends s{constructor(t){super(t)}async fetchText(t,s){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:s.conversationHistory,contextId:this.context?.contextId}})});if(!o.ok)throw new e.NluxError({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 e.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,s,r){throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class o extends s{constructor(t){super(t)}async fetchText(t,s){throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,s,r){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,conversationHistory:r.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 r=t.body.getReader(),o=new TextDecoder;let n=!1;for(;!n;){const{value:t,done:i}=await r.read();if(i)n=!0;else try{const e=o.decode(t);s.next(e)}catch(t){e.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}s.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class n{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new e.NluxUsageError({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??s.defaultDataTransferMode)?new o(t):new r(t)}withContext(t){if(void 0!==this.theContext)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}withMode(t){if(void 0!==this.theMode)throw new e.NluxUsageError({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 e.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}withHeaders(t){if(void 0!==this.theHeaders)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the headers option more than once"});return this.theHeaders=t,this}}class i{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 a{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new i(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(t,"debug",{enumerable:!0,get:function(){return e.debug}}),t.createChatAdapter=()=>new n,t.createContextAdapter=()=>new a}));
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