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

@nlux/nlbridge

Package Overview
Dependencies
Maintainers
1
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nlux/nlbridge - npm Package Compare versions

Comparing version 0.12.0 to 0.12.1

2

cjs/nlbridge.js

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

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

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

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

@@ -1,5 +0,15 @@

import { DataTransferMode, AdapterBuilder, StandardAdapter } from '@nlux/core';
export { Adapter, DataTransferMode, StandardAdapter, StreamingAdapterObserver, debug } from '@nlux/core';
import { DataTransferMode, AdapterBuilder, StandardAdapter, AiContextAdapter, ContextAdapterBuilder } from '@nlux/core';
export { Adapter, AiContextAdapter, ContextAdapterBuilder, DataTransferMode, StandardAdapter, StreamingAdapterObserver, debug } from '@nlux/core';
import { AssistResult } from '@nlux-dev/core/src/types/aiAssistant/assist';
import { RegisterTaskResult } from '@nlux-dev/core/src/types/aiAssistant/registerTask';
import { UnregisterTaskResult } from '@nlux-dev/core/src/types/aiAssistant/unregisterTask';
import { ClearContextResult } from '@nlux-dev/core/src/types/aiContext/clear';
import { ContextData } from '@nlux-dev/core/src/types/aiContext/data';
import { GetContextDataResult } from '@nlux-dev/core/src/types/aiContext/get';
import { SetContextResult } from '@nlux-dev/core/src/types/aiContext/set';
import { UpdateContextResult } from '@nlux-dev/core/src/types/aiContext/update';
type NlBridgeAdapterOptions = {
type AiTaskRunner = (taskId: string, params: Array<any>) => void;
type ChatAdapterOptions = {
/**

@@ -15,12 +25,42 @@ * The URL of the NlBridge endpoint.

dataTransferMode?: DataTransferMode;
/**
* The context ID to use when communicating with NlBridge.
* Optional. If not provided, the adapter will not use a context.
*/
contextId?: string;
/**
* A task runner function that can be used to execute tasks returned by AI.
*/
taskRunner?: AiTaskRunner;
};
interface NlBridgeAdapterBuilder extends AdapterBuilder<any, any> {
create(): StandardAdapter<any, any>;
withDataTransferMode(mode: DataTransferMode): NlBridgeAdapterBuilder;
withUrl(endpointUrl: string): NlBridgeAdapterBuilder;
interface ChatAdapterBuilder extends AdapterBuilder {
create(): StandardAdapter;
withContextId(contextId: string): ChatAdapterBuilder;
withDataTransferMode(mode: DataTransferMode): ChatAdapterBuilder;
withTaskRunner(taskRunner: AiTaskRunner): ChatAdapterBuilder;
withUrl(endpointUrl: string): ChatAdapterBuilder;
}
declare const createAdapter: () => NlBridgeAdapterBuilder;
declare const createChatAdapter: () => ChatAdapterBuilder;
export { type NlBridgeAdapterBuilder, type NlBridgeAdapterOptions, createAdapter };
declare class ContextAdapter implements AiContextAdapter {
private url;
constructor(url: string);
assist(contextId: string, message: string): Promise<AssistResult>;
clear(contextId: string): Promise<ClearContextResult>;
get(contextId: string, itemId: string | undefined): Promise<GetContextDataResult>;
registerTask(contextId: string, taskId: string, parameters: string[]): Promise<RegisterTaskResult>;
set(initialData: Record<string, any> | undefined): Promise<SetContextResult>;
unregisterTask(contextId: string, taskId: string): Promise<UnregisterTaskResult>;
update(contextId: string, data: ContextData | null): Promise<UpdateContextResult>;
}
interface NlBridgeContextAdapterBuilder extends ContextAdapterBuilder {
create(): AiContextAdapter;
withUrl(endpointUrl: string): NlBridgeContextAdapterBuilder;
}
declare const createContextAdapter: () => NlBridgeContextAdapterBuilder;
export { type AiTaskRunner, type ChatAdapterBuilder, type ChatAdapterOptions, ContextAdapter, createChatAdapter, createContextAdapter };
{
"name": "@nlux/nlbridge",
"version": "0.12.0",
"version": "0.12.1",
"description": "The nlbridge adapters for nlux, the javascript library for building conversational AI interfaces.",

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

"dependencies": {
"@nlux/core": "0.12.0"
"@nlux/core": "0.12.1"
},

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

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc