@nlbridge/express
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("@nlbridge/core");const t=e=>{console.error("[31m"+e+"[0m")},s=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},a=s,r=(e,t)=>{switch(e){case"chat":return s(t);case"chat-stream":return a(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const a=e.key;return"string"==typeof a&&a?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}};exports.middleware=(s,a)=>{if(!a.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const o=e.asOpenAiChatModel(a.chatModel),{run:n}=e.createRuntime(e.defaultHandlers,{apiKey:a.apiKey,chatModel:o});return async(s,a,o)=>{if("application/json"!==s.headers["content-type"])return void a.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!s.body)return t("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void a.status(500).send("Internal server error.");if("string"==typeof s.body)try{s.body=JSON.parse(s.body)}catch(e){return c="@nlbridge/express middleware was unable to parse the request body as JSON.",console.warn("[33m"+c+"[0m"),void a.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}var c;if("object"!=typeof s.body||null===s.body||0===Object.keys(s.body).length)return void a.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:d,payload:u}=s.body,i=e.asValidActionId(d);if(!i)return void a.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+e.actionIds.join(", ")});const l=r(i,u);if(l.success)switch(i){case"chat-stream":return void((e,s,a,r)=>{let o=!1;const n={next:e=>{o||r.write(`data: ${JSON.stringify(e)}\n\n`)},complete:()=>{o||r.end()},error:e=>{t(e)}};r.setHeader("Cache-Control","no-cache"),r.setHeader("Content-Type","text/event-stream"),r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Connection","keep-alive"),r.flushHeaders(),r.on("close",(()=>{o=!0})),e("chat-stream",s.message,n,{})})(n,u,0,a);case"chat":return void await(async(e,t,s,a)=>{const r=await e("chat",t.message,{contextId:t.contextId});r.success?a.status(200).send({success:!0,result:{response:r.message}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"set-context":return void await(async(e,t,s,a)=>{const r=await e("set-context",t?.data||void 0,{});r.success?a.status(200).send({success:!0,result:{contextId:r.contextId}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"get-context-data":return void await(async(e,t,s,a)=>{const r=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});r.success?a.status(200).send({success:!0,result:{data:r.data}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"update-context":return void await(async(e,t,s,a)=>{const r=await e("update-context",t.contextId,t.data,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"clear-context":return void await(async(e,t,s,a)=>{const r=await e("clear-context",t.contextId,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);default:a.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else a.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}}; | ||
"use strict";var e=require("@nlbridge/core");const t=e=>{console.error("[31m"+e+"[0m")},s=e=>{console.warn("[33m"+e+"[0m")},a=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},r=a,o=(e,t)=>{switch(e){case"chat":return a(t);case"chat-stream":return r(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const a=e.key;return"string"==typeof a&&a?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}};exports.middleware=(a,r)=>{if(!r.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const n=e.asOpenAiChatModel(r.chatModel),{run:c}=e.createRuntime(e.defaultHandlers,{apiKey:r.apiKey,chatModel:n});return async(a,r,n)=>{if("application/json"!==a.headers["content-type"])return void r.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!a.body)return t("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void r.status(500).send("Internal server error.");if("string"==typeof a.body)try{a.body=JSON.parse(a.body)}catch(e){return s("@nlbridge/express middleware was unable to parse the request body as JSON."),void r.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof a.body||null===a.body||0===Object.keys(a.body).length)return void r.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:d,payload:u}=a.body,i=e.asValidActionId(d);if(!i)return void r.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+e.actionIds.join(", ")});const l=o(i,u);if(l.success)switch(i){case"chat-stream":return void((e,a,r,o)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?o.write(e):s("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||o.end()},error:e=>{t(e)}};o.setHeader("Cache-Control","no-cache"),o.setHeader("Content-Type","text/event-stream"),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Connection","keep-alive"),o.flushHeaders(),o.on("close",(()=>{n=!0})),e("chat-stream",a.message,c,{})})(c,u,0,r);case"chat":return void await(async(e,t,s,a)=>{const r=await e("chat",t.message,{contextId:t.contextId});r.success?a.status(200).send({success:!0,result:{response:r.message}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"set-context":return void await(async(e,t,s,a)=>{const r=await e("set-context",t?.data||void 0,{});r.success?a.status(200).send({success:!0,result:{contextId:r.contextId}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"get-context-data":return void await(async(e,t,s,a)=>{const r=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});r.success?a.status(200).send({success:!0,result:{data:r.data}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"update-context":return void await(async(e,t,s,a)=>{const r=await e("update-context",t.contextId,t.data,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"clear-context":return void await(async(e,t,s,a)=>{const r=await e("clear-context",t.contextId,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);default:r.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else r.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}}; |
@@ -1,1 +0,1 @@ | ||
import{asOpenAiChatModel as e,createRuntime as t,defaultHandlers as s,asValidActionId as r,actionIds as a}from"@nlbridge/core";const o=e=>{console.error("[31m"+e+"[0m")},n=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},c=n,d=(e,t)=>{switch(e){case"chat":return n(t);case"chat-stream":return c(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const r=e.key;return"string"==typeof r&&r?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},u=(n,c)=>{if(!c.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=e(c.chatModel),{run:i}=t(s,{apiKey:c.apiKey,chatModel:u});return async(e,t,s)=>{if("application/json"!==e.headers["content-type"])return void t.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return o("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void t.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return n="@nlbridge/express middleware was unable to parse the request body as JSON.",console.warn("[33m"+n+"[0m"),void t.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}var n;if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void t.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:c,payload:u}=e.body,l=r(c);if(!l)return void t.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+a.join(", ")});const y=d(l,u);if(y.success)switch(l){case"chat-stream":return void((e,t,s,r)=>{let a=!1;const n={next:e=>{a||r.write(`data: ${JSON.stringify(e)}\n\n`)},complete:()=>{a||r.end()},error:e=>{o(e)}};r.setHeader("Cache-Control","no-cache"),r.setHeader("Content-Type","text/event-stream"),r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Connection","keep-alive"),r.flushHeaders(),r.on("close",(()=>{a=!0})),e("chat-stream",t.message,n,{})})(i,u,0,t);case"chat":return void await(async(e,t,s,r)=>{const a=await e("chat",t.message,{contextId:t.contextId});a.success?r.status(200).send({success:!0,result:{response:a.message}}):r.status(500).send({success:!1,error:a.error})})(i,u,0,t);case"set-context":return void await(async(e,t,s,r)=>{const a=await e("set-context",t?.data||void 0,{});a.success?r.status(200).send({success:!0,result:{contextId:a.contextId}}):r.status(500).send({success:!1,error:a.error})})(i,u,0,t);case"get-context-data":return void await(async(e,t,s,r)=>{const a=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});a.success?r.status(200).send({success:!0,result:{data:a.data}}):r.status(500).send({success:!1,error:a.error})})(i,u,0,t);case"update-context":return void await(async(e,t,s,r)=>{const a=await e("update-context",t.contextId,t.data,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(i,u,0,t);case"clear-context":return void await(async(e,t,s,r)=>{const a=await e("clear-context",t.contextId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(i,u,0,t);default:t.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}else t.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`})}};export{u as middleware}; | ||
import{asOpenAiChatModel as e,createRuntime as t,defaultHandlers as s,asValidActionId as a,actionIds as r}from"@nlbridge/core";const o=e=>{console.error("[31m"+e+"[0m")},n=e=>{console.warn("[33m"+e+"[0m")},c=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},d=c,u=(e,t)=>{switch(e){case"chat":return c(t);case"chat-stream":return d(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const a=e.key;return"string"==typeof a&&a?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},i=(c,d)=>{if(!d.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const i=e(d.chatModel),{run:l}=t(s,{apiKey:d.apiKey,chatModel:i});return async(e,t,s)=>{if("application/json"!==e.headers["content-type"])return void t.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return o("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void t.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return n("@nlbridge/express middleware was unable to parse the request body as JSON."),void t.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void t.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:c,payload:d}=e.body,i=a(c);if(!i)return void t.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+r.join(", ")});const y=u(i,d);if(y.success)switch(i){case"chat-stream":return void((e,t,s,a)=>{let r=!1;const c={next:e=>{r||("string"==typeof e||"number"==typeof e?a.write(e):n("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{r||a.end()},error:e=>{o(e)}};a.setHeader("Cache-Control","no-cache"),a.setHeader("Content-Type","text/event-stream"),a.setHeader("Access-Control-Allow-Origin","*"),a.setHeader("Connection","keep-alive"),a.flushHeaders(),a.on("close",(()=>{r=!0})),e("chat-stream",t.message,c,{})})(l,d,0,t);case"chat":return void await(async(e,t,s,a)=>{const r=await e("chat",t.message,{contextId:t.contextId});r.success?a.status(200).send({success:!0,result:{response:r.message}}):a.status(500).send({success:!1,error:r.error})})(l,d,0,t);case"set-context":return void await(async(e,t,s,a)=>{const r=await e("set-context",t?.data||void 0,{});r.success?a.status(200).send({success:!0,result:{contextId:r.contextId}}):a.status(500).send({success:!1,error:r.error})})(l,d,0,t);case"get-context-data":return void await(async(e,t,s,a)=>{const r=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});r.success?a.status(200).send({success:!0,result:{data:r.data}}):a.status(500).send({success:!1,error:r.error})})(l,d,0,t);case"update-context":return void await(async(e,t,s,a)=>{const r=await e("update-context",t.contextId,t.data,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(l,d,0,t);case"clear-context":return void await(async(e,t,s,a)=>{const r=await e("clear-context",t.contextId,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(l,d,0,t);default:t.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else t.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${y.error}`})}};export{i as middleware}; |
{ | ||
"name": "@nlbridge/express", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "An express middleware to connect to several LLMs", | ||
@@ -59,3 +59,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlbridge/core": "0.2.4" | ||
"@nlbridge/core": "0.2.5" | ||
}, | ||
@@ -62,0 +62,0 @@ "devDependencies": { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,t){"use strict";const s=e=>{console.error("[31m"+e+"[0m")},a=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},r=a,o=(e,t)=>{switch(e){case"chat":return a(t);case"chat-stream":return r(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const a=e.key;return"string"==typeof a&&a?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}};e.middleware=(e,a)=>{if(!a.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const r=t.asOpenAiChatModel(a.chatModel),{run:n}=t.createRuntime(t.defaultHandlers,{apiKey:a.apiKey,chatModel:r});return async(e,a,r)=>{if("application/json"!==e.headers["content-type"])return void a.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return s("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void a.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return c="@nlbridge/express middleware was unable to parse the request body as JSON.",console.warn("[33m"+c+"[0m"),void a.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}var c;if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void a.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:d,payload:u}=e.body,i=t.asValidActionId(d);if(!i)return void a.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+t.actionIds.join(", ")});const l=o(i,u);if(l.success)switch(i){case"chat-stream":return void((e,t,a,r)=>{let o=!1;const n={next:e=>{o||r.write(`data: ${JSON.stringify(e)}\n\n`)},complete:()=>{o||r.end()},error:e=>{s(e)}};r.setHeader("Cache-Control","no-cache"),r.setHeader("Content-Type","text/event-stream"),r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Connection","keep-alive"),r.flushHeaders(),r.on("close",(()=>{o=!0})),e("chat-stream",t.message,n,{})})(n,u,0,a);case"chat":return void await(async(e,t,s,a)=>{const r=await e("chat",t.message,{contextId:t.contextId});r.success?a.status(200).send({success:!0,result:{response:r.message}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"set-context":return void await(async(e,t,s,a)=>{const r=await e("set-context",t?.data||void 0,{});r.success?a.status(200).send({success:!0,result:{contextId:r.contextId}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"get-context-data":return void await(async(e,t,s,a)=>{const r=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});r.success?a.status(200).send({success:!0,result:{data:r.data}}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"update-context":return void await(async(e,t,s,a)=>{const r=await e("update-context",t.contextId,t.data,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);case"clear-context":return void await(async(e,t,s,a)=>{const r=await e("clear-context",t.contextId,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(n,u,0,a);default:a.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else a.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,t){"use strict";const s=e=>{console.error("[31m"+e+"[0m")},a=e=>{console.warn("[33m"+e+"[0m")},r=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},o=r,n=(e,t)=>{switch(e){case"chat":return r(t);case"chat-stream":return o(t);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(t);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const t=e.contextId;return"string"==typeof t&&t?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(t);case"get-context-data":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const t=Object.keys(e).length;if(1!==t&&2!==t)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===t)return{success:!0,payload:e};const a=e.key;return"string"==typeof a&&a?{success:!0,payload:e}:{success:!1,error:"payload.key must be a non-empty string."}})(t);case"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const t=e.contextId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(t);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}};e.middleware=(e,r)=>{if(!r.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const o=t.asOpenAiChatModel(r.chatModel),{run:c}=t.createRuntime(t.defaultHandlers,{apiKey:r.apiKey,chatModel:o});return async(e,r,o)=>{if("application/json"!==e.headers["content-type"])return void r.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return s("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void r.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return a("@nlbridge/express middleware was unable to parse the request body as JSON."),void r.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof e.body||null===e.body||0===Object.keys(e.body).length)return void r.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:d,payload:u}=e.body,i=t.asValidActionId(d);if(!i)return void r.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+t.actionIds.join(", ")});const l=n(i,u);if(l.success)switch(i){case"chat-stream":return void((e,t,r,o)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?o.write(e):a("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||o.end()},error:e=>{s(e)}};o.setHeader("Cache-Control","no-cache"),o.setHeader("Content-Type","text/event-stream"),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Connection","keep-alive"),o.flushHeaders(),o.on("close",(()=>{n=!0})),e("chat-stream",t.message,c,{})})(c,u,0,r);case"chat":return void await(async(e,t,s,a)=>{const r=await e("chat",t.message,{contextId:t.contextId});r.success?a.status(200).send({success:!0,result:{response:r.message}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"set-context":return void await(async(e,t,s,a)=>{const r=await e("set-context",t?.data||void 0,{});r.success?a.status(200).send({success:!0,result:{contextId:r.contextId}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"get-context-data":return void await(async(e,t,s,a)=>{const r=await e("get-context-data",t.contextId||void 0,t.key||void 0,{});r.success?a.status(200).send({success:!0,result:{data:r.data}}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"update-context":return void await(async(e,t,s,a)=>{const r=await e("update-context",t.contextId,t.data,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);case"clear-context":return void await(async(e,t,s,a)=>{const r=await e("clear-context",t.contextId,{});r.success?a.status(200).send({success:!0}):a.status(500).send({success:!1,error:r.error})})(c,u,0,r);default:r.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else r.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}}})); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20332
+ Added@nlbridge/core@0.2.5(transitive)
+ Addedopenai@4.77.4(transitive)
- Removed@nlbridge/core@0.2.4(transitive)
- Removedopenai@4.78.1(transitive)
Updated@nlbridge/core@0.2.5