@nlux/nlbridge
Advanced tools
Comparing version 1.0.8 to 2.0.0-alpha.1
@@ -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"}get context(){return this.theAiContextToUse}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:{"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:{"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)}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};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}}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; | ||
"use strict";var t=Object.defineProperty,e=(e,s,o)=>(((e,s,o)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[s]=o})(e,"symbol"!=typeof s?s+"":s,o),o);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class o extends s{}class r{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream"}get context(){return this.theAiContextToUse}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get usageMode(){return this.theUsageMode}}r.defaultDataTransferMode="stream";class n extends r{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const o="copilot"===this.usageMode?"assist":"chat",r=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:o,payload:{message:t,contextId:this.context?.contextId}})});if(!r.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new o({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends r{constructor(t){super(t)}async fetchText(t,e){throw new o({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{"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 r=!1;for(;!r;){const{value:t,done:n}=await s.read();if(n)r=!0;else try{const s=o.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new o({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext};return"stream"===(t.mode??r.defaultDataTransferMode)?new a(t):new n(t)}withContext(t){if(void 0!==this.theContext)throw new o({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContext=t,this}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 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}}exports.createChatAdapter=()=>new c,exports.createContextAdapter=()=>new d; |
@@ -1,1 +0,1 @@ | ||
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"}get context(){return this.theAiContextToUse}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:{"Content-Type":"application/json"},body:JSON.stringify({action:o,payload:{message:t,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:{"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(),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};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}}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}; | ||
var t=Object.defineProperty,e=(e,s,o)=>(((e,s,o)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[s]=o})(e,"symbol"!=typeof s?s+"":s,o),o);class s extends Error{constructor(t={}){super(t.message),e(this,"exceptionId"),e(this,"message"),e(this,"source"),e(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class o extends s{}class n{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream"}get context(){return this.theAiContextToUse}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 r extends n{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const o="copilot"===this.usageMode?"assist":"chat",n=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:o,payload:{message:t,contextId:this.context?.contextId}})});if(!n.ok)throw new s({source:this.constructor.name,message:`NLBridge adapter returned status code: ${n.status}`});const r=await n.json();if("object"==typeof r&&null!==r&&!0===r.success&&"object"==typeof r.result&&null!==r.result&&"string"==typeof r.result.response){const{response:t,task:e}=r.result;return this.context&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.context.runTask(e.taskId,e.parameters),t}throw new s({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,s){throw new o({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}const i=t=>{"string"!=typeof t?t&&"function"==typeof t.toString?console.warn(`[nlux] ${t.toString()}`):console.warn("[nlux]"):console.warn(`[nlux] ${t}`)};class a extends n{constructor(t){super(t)}async fetchText(t,e){throw new o({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",headers:{"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:r}=await s.read();if(r)n=!0;else try{const s=o.decode(t);e.next(s)}catch(t){i(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class c{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new o({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are call withUrl() or provide url option before calling creating the adapter."});const t={url:this.theUrl,mode:this.theMode,context:this.theContext};return"stream"===(t.mode??n.defaultDataTransferMode)?new a(t):new r(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}withMode(t){if(void 0!==this.theMode)throw new o({source:this.constructor.name,message:"Cannot set the usage mode option more than once"});return this.theMode=t,this}withUrl(t){if(void 0!==this.theUrl)throw new o({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const h=()=>new c;class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const n=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!n.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await n.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}const x=()=>new u;export{h as createChatAdapter,x as createContextAdapter}; |
import { AiContext, ChatAdapterBuilder as ChatAdapterBuilder$1, StandardChatAdapter, ContextAdapterBuilder as ContextAdapterBuilder$1, ContextAdapter } from '@nlux/core'; | ||
export { ChatAdapter, ContextAdapter, DataTransferMode, StandardChatAdapter, StreamingAdapterObserver, debug } from '@nlux/core'; | ||
export { ChatAdapter, ContextAdapter, DataTransferMode, StandardChatAdapter, StreamingAdapterObserver } from '@nlux/core'; | ||
@@ -28,10 +28,10 @@ type ChatAdapterUsageMode = 'chat' | 'copilot'; | ||
interface ChatAdapterBuilder extends ChatAdapterBuilder$1 { | ||
create(): StandardChatAdapter; | ||
withContext(context: AiContext): ChatAdapterBuilder; | ||
withMode(mode: ChatAdapterUsageMode): ChatAdapterBuilder; | ||
withUrl(endpointUrl: string): ChatAdapterBuilder; | ||
interface ChatAdapterBuilder<AiMsg> extends ChatAdapterBuilder$1<AiMsg> { | ||
create(): StandardChatAdapter<AiMsg>; | ||
withContext(context: AiContext): ChatAdapterBuilder<AiMsg>; | ||
withMode(mode: ChatAdapterUsageMode): ChatAdapterBuilder<AiMsg>; | ||
withUrl(endpointUrl: string): ChatAdapterBuilder<AiMsg>; | ||
} | ||
declare const createChatAdapter: () => ChatAdapterBuilder; | ||
declare const createChatAdapter: <AiMsg = string>() => ChatAdapterBuilder<AiMsg>; | ||
@@ -38,0 +38,0 @@ interface ContextAdapterBuilder extends ContextAdapterBuilder$1 { |
{ | ||
"name": "@nlux/nlbridge", | ||
"version": "1.0.8", | ||
"version": "2.0.0-alpha.1", | ||
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.", | ||
@@ -62,3 +62,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/core": "1.0.8" | ||
"@nlux/core": "2.0.0-alpha.1" | ||
}, | ||
@@ -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"}get context(){return this.theAiContextToUse}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:{"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:{"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)}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};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}}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})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@nlux/nlbridge"]={})}(this,(function(t){"use strict";var e=Object.defineProperty,s=(t,s,o)=>(((t,s,o)=>{s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[s]=o})(t,"symbol"!=typeof s?s+"":s,o),o);class o extends Error{constructor(t={}){super(t.message),s(this,"exceptionId"),s(this,"message"),s(this,"source"),s(this,"type"),this.message=t.message??"",this.source=t.source,this.type=this.constructor.name,this.exceptionId=t.exceptionId}}class n extends o{}class r{constructor(t){this.theAiContextToUse=void 0,this.theUsageMode=void 0,this.__instanceId=`${this.info.id}-${"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{let e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)}))}`,this.theUsageMode=t.mode,this.theEndpointUrlToUse=t.url,this.theAiContextToUse=t.context,this.theDataTransferModeToUse="copilot"===t.mode&&t.context?"fetch":"stream"}get context(){return this.theAiContextToUse}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 i extends r{constructor(t){super(t)}async fetchText(t,e){this.context&&this.context.contextId&&await this.context.flush();const s="copilot"===this.usageMode?"assist":"chat",n=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:s,payload:{message:t,contextId:this.context?.contextId}})});if(!n.ok)throw new o({source:this.constructor.name,message:`NLBridge adapter returned status code: ${n.status}`});const r=await n.json();if("object"==typeof r&&null!==r&&!0===r.success&&"object"==typeof r.result&&null!==r.result&&"string"==typeof r.result.response){const{response:t,task:e}=r.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 n({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 r{constructor(t){super(t)}async fetchText(t,e){throw new n({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){const o=()=>fetch(this.endpointUrl,{method:"POST",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:r}=await s.read();if(r)n=!0;else try{const s=o.decode(t);e.next(s)}catch(t){a(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}));this.context&&this.context.contextId?this.context.flush().then((()=>{o()})).catch((()=>{o()})):o()}}class h{constructor(t){t&&(this.theUrl=t.theUrl,this.theMode=t.theMode,this.theContext=t.theContext)}create(){if(!this.theUrl)throw new n({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};return"stream"===(t.mode??r.defaultDataTransferMode)?new c(t):new i(t)}withContext(t){if(void 0!==this.theContext)throw new n({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 n({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 n({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}class d{constructor(t){this.url=t}async create(t,e){try{const s=await fetch(this.url,{method:"POST",headers:{...e?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:"create-context",payload:t?{items:t}:void 0})});if(!s.ok)return{success:!1,error:"Failed to set context"};const o=await s.json();return o?.result?.contextId?{success:!0,contextId:o.result.contextId}:{success:!1,error:"Invalid context ID"}}catch(t){return{success:!1,error:"Failed to set context"}}}discard(t,e){return this.sendAction(t,"discard-context",void 0,e)}removeItems(t,e,s){return this.sendAction(t,"remove-context-items",{itemIds:e},s)}async removeTasks(t,e,s){return this.sendAction(t,"remove-context-tasks",{taskIds:e},s)}resetItems(t,e,s){return this.sendAction(t,"reset-context-items",e?{items:e}:void 0,s)}resetTasks(t,e,s){return this.sendAction(t,"reset-context-tasks",e,s)}async sendAction(t,e,s,o){if(!t)return{success:!1,error:"Invalid context ID"};try{const n=await fetch(this.url,{method:"POST",headers:{...o?.headers,"Content-Type":"application/json"},body:JSON.stringify({action:e,payload:{...s,contextId:t}})});if(!n.ok)return{success:!1,error:"Failed to send action"};return{success:!0,items:await n.json()}}catch(t){return{success:!1,error:"Failed to send action"}}}async updateItems(t,e,s){return this.sendAction(t,"update-context-items",{items:e},s)}async updateTasks(t,e,s){return this.sendAction(t,"update-context-tasks",{tasks:e},s)}}class u{constructor(){this.endpointUrl=void 0}build(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new d(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}t.createChatAdapter=()=>new h,t.createContextAdapter=()=>new u})); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
23040
110
2
+ Added@nlux/core@2.0.0-alpha.1(transitive)
- Removed@nlux/core@1.0.8(transitive)
Updated@nlux/core@2.0.0-alpha.1