@nlux/nlbridge
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,1 +0,1 @@ | ||
"use strict";var t=require("@nlux/core");const e=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};e.defaultDataTransferMode="stream";let r=e;class s extends r{constructor(t){super(t)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NLBridge adapter returned status code: ${s.status}`});const n=await s.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class n extends r{constructor(t){super(t)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NLBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NLBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let a=!1;for(;!a;){const{value:e,done:o}=await s.read();if(o)a=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??r.defaultDataTransferMode)?new n(e):new s(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class o{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,a=s?.result?.taskId,o=s?.result?.parameters;return a&&Array.isArray(o)?{success:!0,response:n,taskId:a,parameters:o}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const r=await e.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new o(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}Object.defineProperty(exports,"debug",{enumerable:!0,get:function(){return t.debug}}),exports.createChatAdapter=()=>new a,exports.createContextAdapter=()=>new c; | ||
"use strict";var t=require("@nlux/core");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; |
@@ -1,1 +0,1 @@ | ||
import{uid as t,NluxError as e,NluxUsageError as s,warn as r}from"@nlux/core";export{debug}from"@nlux/core";const n=class e{constructor(s){this.__instanceId=`${this.info.id}-${t()}`,this.theDataTransferModeToUse=s.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=s.url,this.theContextIdToUse=s.contextId,this.theTaskRunnerToUse=s.taskRunner}get contextId(){return this.theContextIdToUse}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get taskRunner(){return this.theTaskRunnerToUse}async decode(t){}async encode(t){}};n.defaultDataTransferMode="stream";let o=n;class a extends o{constructor(t){super(t)}async fetchText(t,s){const r=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:t,contextId:this.contextId}})});if(!r.ok)throw new e({source:this.constructor.name,message:`NLBridge adapter returned status code: ${r.status}`});const n=await r.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:t,task:e}=n.result;return this.taskRunner&&e&&"object"==typeof e&&"string"==typeof e.taskId&&Array.isArray(e.parameters)&&this.taskRunner(e.taskId,e.parameters),t}throw new e({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(t,e,r){throw new s({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class c extends o{constructor(t){super(t)}async fetchText(t,e){throw new s({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(t,e,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:t,contextId:this.contextId}})}).then((async t=>{if(!t.ok)throw new Error(`NLBridge adapter returned status code: ${t.status}`);if(!t.body)throw new Error(`NLBridge adapter returned status code: ${t.status}`);const s=t.body.getReader(),n=new TextDecoder;let o=!1;for(;!o;){const{value:t,done:a}=await s.read();if(a)o=!0;else try{const s=n.decode(t);e.next(s)}catch(t){r(`Error parsing chunk by NLBridgeStreamAdapter: ${t}`)}}e.complete()}))}}class i{constructor(t){t&&(this.theDataTransferMode=t.theDataTransferMode,this.theUrl=t.theUrl,this.theContextId=t.theContextId,this.theTaskRunner=t.theTaskRunner)}create(){if(!this.theUrl)throw new s({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const t={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(t.dataTransferMode??o.defaultDataTransferMode)?new c(t):new a(t)}withContextId(t){if(void 0!==this.theContextId)throw new s({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=t,this}withDataTransferMode(t){if(void 0!==this.theDataTransferMode)throw new s({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=t,this}withTaskRunner(t){if(void 0!==this.theTaskRunner)throw new s({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=t,this}withUrl(t){if(void 0!==this.theUrl)throw new s({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=t,this}}const d=()=>new i;class h{constructor(t){this.url=t}async assist(t,e){if(!t)return{success:!1,error:"Invalid context ID"};if(!e)return{success:!1,error:"Invalid message"};try{const s=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:t,message:e}})});if(!s.ok)return{success:!1,error:"Failed to assist"};const r=await s.json(),n=r?.result?.response,o=r?.result?.taskId,a=r?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(t){return{success:!1,error:"Failed to assist"}}}async clear(t){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(t){return{success:!1,error:"Failed to clear context"}}}async get(t,e){return{success:!1,error:"Not implemented"}}async registerTask(t,e,s){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:t,taskId:e,parameters:s}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(t){return{success:!1,error:"Failed to register task"}}}async set(t){try{const e=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:t||null}})});if(!e.ok)return{success:!1,error:"Failed to set context"};const s=await e.json(),r=s?.result?.contextId;return{success:!0,contextId:r}}catch(t){return{success:!1,error:"Failed to set context"}}}async unregisterTask(t,e){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:t,taskId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(t){return{success:!1,error:"Failed to unregister task"}}}async update(t,e){if(!t)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:t,data:e}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(t){return{success:!1,error:"Failed to update context"}}}}class u{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new h(this.endpointUrl)}withUrl(t){if(void 0!==this.endpointUrl&&this.endpointUrl!==t)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=t,this}}const l=()=>new u;export{d as createChatAdapter,l as createContextAdapter}; | ||
import{uid as t,NluxError as e,NluxUsageError as s,warn as 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}; |
@@ -1,4 +0,5 @@ | ||
import { DataTransferMode, AiTaskRunner, ChatAdapterBuilder as ChatAdapterBuilder$1, StandardChatAdapter, ContextAdapterBuilder as ContextAdapterBuilder$1, ContextAdapter } from '@nlux/core'; | ||
export { AiTaskRunner, ChatAdapter, ContextAdapter, DataTransferMode, StandardChatAdapter, StreamingAdapterObserver, debug } from '@nlux/core'; | ||
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'; | ||
type ChatAdapterUsageMode = 'chat' | 'copilot'; | ||
type ChatAdapterOptions = { | ||
@@ -11,6 +12,11 @@ /** | ||
/** | ||
* The data transfer mode to use when communicating with NLBridge. | ||
* If not provided, the adapter will use `stream` mode. | ||
* Indicates the usage mode of the adapter | ||
* | ||
* - When set to 'copilot', the adapter will additionally check for tasks that can be executed and trigger them. | ||
* - In copilot mode, data cannot be streamed and will be fetched in one request instead. | ||
* - The copilot mode requires the presence of a context. If not provided, the adapter will use 'chat' mode. | ||
* | ||
* Default: 'chat' | ||
*/ | ||
dataTransferMode?: DataTransferMode; | ||
mode?: ChatAdapterUsageMode; | ||
/** | ||
@@ -20,7 +26,3 @@ * The context ID to use when communicating with NLBridge. | ||
*/ | ||
contextId?: string; | ||
/** | ||
* A task runner function that can be used to execute tasks returned by AI. | ||
*/ | ||
taskRunner?: AiTaskRunner; | ||
context?: AiContext; | ||
}; | ||
@@ -30,5 +32,4 @@ | ||
create(): StandardChatAdapter; | ||
withContextId(contextId: string): ChatAdapterBuilder; | ||
withDataTransferMode(mode: DataTransferMode): ChatAdapterBuilder; | ||
withTaskRunner(taskRunner: AiTaskRunner): ChatAdapterBuilder; | ||
withContext(context: AiContext): ChatAdapterBuilder; | ||
withMode(mode: ChatAdapterUsageMode): ChatAdapterBuilder; | ||
withUrl(endpointUrl: string): ChatAdapterBuilder; | ||
@@ -40,3 +41,3 @@ } | ||
interface ContextAdapterBuilder extends ContextAdapterBuilder$1 { | ||
create(): ContextAdapter; | ||
build(): ContextAdapter; | ||
withUrl(endpointUrl: string): ContextAdapterBuilder; | ||
@@ -47,2 +48,2 @@ } | ||
export { type ChatAdapterBuilder, type ChatAdapterOptions, type ContextAdapterBuilder, createChatAdapter, createContextAdapter }; | ||
export { type ChatAdapterBuilder, type ChatAdapterOptions, type ChatAdapterUsageMode, type ContextAdapterBuilder, createChatAdapter, createContextAdapter }; |
{ | ||
"name": "@nlux/nlbridge", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.", | ||
@@ -62,3 +62,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/core": "1.0.1" | ||
"@nlux/core": "1.0.2" | ||
}, | ||
@@ -65,0 +65,0 @@ "peerDependencies": {}, |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/core")):"function"==typeof define&&define.amd?define(["exports","@nlux/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/nlbridge"]={},e.core)}(this,(function(e,t){"use strict";const r=class e{constructor(r){this.__instanceId=`${this.info.id}-${t.uid()}`,this.theDataTransferModeToUse=r.dataTransferMode??e.defaultDataTransferMode,this.theEndpointUrlToUse=r.url,this.theContextIdToUse=r.contextId,this.theTaskRunnerToUse=r.taskRunner}get contextId(){return this.theContextIdToUse}get dataTransferMode(){return this.theDataTransferModeToUse}get endpointUrl(){return this.theEndpointUrlToUse}get id(){return this.__instanceId}get info(){return{id:"nlbridge-adapter",capabilities:{chat:!0,fileUpload:!1,textToSpeech:!1,speechToText:!1}}}get taskRunner(){return this.theTaskRunnerToUse}async decode(e){}async encode(e){}};r.defaultDataTransferMode="stream";let s=r;class n extends s{constructor(e){super(e)}async fetchText(e,r){const s=await fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:this.contextId?"assist":"chat",payload:{message:e,contextId:this.contextId}})});if(!s.ok)throw new t.NluxError({source:this.constructor.name,message:`NLBridge adapter returned status code: ${s.status}`});const n=await s.json();if("object"==typeof n&&null!==n&&!0===n.success&&"object"==typeof n.result&&null!==n.result&&"string"==typeof n.result.response){const{response:e,task:t}=n.result;return this.taskRunner&&t&&"object"==typeof t&&"string"==typeof t.taskId&&Array.isArray(t.parameters)&&this.taskRunner(t.taskId,t.parameters),e}throw new t.NluxError({source:this.constructor.name,message:"Invalid response from NLBridge: String expected."})}streamText(e,r,s){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot stream text from the fetch adapter!"})}}class o extends s{constructor(e){super(e)}async fetchText(e,r){throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot fetch text using the stream adapter!"})}streamText(e,r,s){fetch(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"chat-stream",payload:{message:e,contextId:this.contextId}})}).then((async e=>{if(!e.ok)throw new Error(`NLBridge adapter returned status code: ${e.status}`);if(!e.body)throw new Error(`NLBridge adapter returned status code: ${e.status}`);const s=e.body.getReader(),n=new TextDecoder;let o=!1;for(;!o;){const{value:e,done:a}=await s.read();if(a)o=!0;else try{const t=n.decode(e);r.next(t)}catch(e){t.warn(`Error parsing chunk by NLBridgeStreamAdapter: ${e}`)}}r.complete()}))}}class a{constructor(e){e&&(this.theDataTransferMode=e.theDataTransferMode,this.theUrl=e.theUrl,this.theContextId=e.theContextId,this.theTaskRunner=e.theTaskRunner)}create(){if(!this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create NLBridge adapter. URL is missing. Make sure you are calling withUrl() before calling create()."});const e={url:this.theUrl,dataTransferMode:this.theDataTransferMode,contextId:this.theContextId,taskRunner:this.theTaskRunner};return"stream"===(e.dataTransferMode??s.defaultDataTransferMode)?new o(e):new n(e)}withContextId(e){if(void 0!==this.theContextId)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the context ID option more than once"});return this.theContextId=e,this}withDataTransferMode(e){if(void 0!==this.theDataTransferMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this}withTaskRunner(e){if(void 0!==this.theTaskRunner)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task runner option more than once"});return this.theTaskRunner=e,this}withUrl(e){if(void 0!==this.theUrl)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint URL option more than once"});return this.theUrl=e,this}}class i{constructor(e){this.url=e}async assist(e,t){if(!e)return{success:!1,error:"Invalid context ID"};if(!t)return{success:!1,error:"Invalid message"};try{const r=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"assist",payload:{contextId:e,message:t}})});if(!r.ok)return{success:!1,error:"Failed to assist"};const s=await r.json(),n=s?.result?.response,o=s?.result?.taskId,a=s?.result?.parameters;return o&&Array.isArray(a)?{success:!0,response:n,taskId:o,parameters:a}:{success:!0,response:n}}catch(e){return{success:!1,error:"Failed to assist"}}}async clear(e){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"clear-context",payload:{contextId:e}})})).ok?{success:!0}:{success:!1,error:"Failed to clear context"}}catch(e){return{success:!1,error:"Failed to clear context"}}}async get(e,t){return{success:!1,error:"Not implemented"}}async registerTask(e,t,r){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"register-task",payload:{contextId:e,taskId:t,parameters:r}})})).ok?{success:!0}:{success:!1,error:"Failed to register task"}}catch(e){return{success:!1,error:"Failed to register task"}}}async set(e){try{const t=await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"set-context",payload:{data:e||null}})});if(!t.ok)return{success:!1,error:"Failed to set context"};const r=await t.json(),s=r?.result?.contextId;return{success:!0,contextId:s}}catch(e){return{success:!1,error:"Failed to set context"}}}async unregisterTask(e,t){try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"unregister-task",payload:{contextId:e,taskId:t}})})).ok?{success:!0}:{success:!1,error:"Failed to unregister task"}}catch(e){return{success:!1,error:"Failed to unregister task"}}}async update(e,t){if(!e)return{success:!1,error:"Invalid context ID"};try{return(await fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"update-context",payload:{contextId:e,data:t}})})).ok?{success:!0}:{success:!1,error:"Failed to update context"}}catch(e){return{success:!1,error:"Failed to update context"}}}}class c{constructor(){this.endpointUrl=void 0}create(){if(!this.endpointUrl)throw new Error("Endpoint URL is required");return new i(this.endpointUrl)}withUrl(e){if(void 0!==this.endpointUrl&&this.endpointUrl!==e)throw new Error("Cannot set the endpoint URL more than once");return this.endpointUrl=e,this}}Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return t.debug}}),e.createChatAdapter=()=>new a,e.createContextAdapter=()=>new c})); | ||
!function(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})); |
12
21465
105
+ Added@nlux/core@1.0.2(transitive)
- Removed@nlux/core@1.0.1(transitive)
Updated@nlux/core@1.0.2