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

@nlbridge/express

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nlbridge/express - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

2

cjs/nlbridge-express.js

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

"use strict";var e=require("@nlbridge/core");const s=e=>{console.error(""+e+"")},t=e=>{console.warn(""+e+"")},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,a=(e,s)=>{switch(e){case"chat":return r(s);case"chat-stream":return o(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},n={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,r,o,a)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?a.write(e):t("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||a.end()},error:e=>{s(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",(()=>{n=!0})),e("chat-stream",r.message,c,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},c=(r,o,c)=>{if(!c.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=e.asOpenAiChatModel(c.chatModel),{run:d}=e.createRuntime(o,{apiKey:c.apiKey,chatModel:u});return async(r,o,c)=>{if("application/json"!==r.headers["content-type"])return void o.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(!r.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 o.status(500).send("Internal server error.");if("string"==typeof r.body)try{r.body=JSON.parse(r.body)}catch(e){return t("@nlbridge/express middleware was unable to parse the request body as JSON."),void o.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof r.body||null===r.body||0===Object.keys(r.body).length)return void o.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:u,payload:i}=r.body,l=e.asValidActionId(u);if(!l)return void o.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 y=a(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const p=n[l];if(p)try{await p(d,i,r,o)}catch(e){s(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),o.status(500).send("Internal server error.")}else o.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};exports.defaultMiddleware=(s,t)=>c(s,e.defaultActionHandlers,t),exports.middleware=c,Object.keys(e).forEach((function(s){"default"===s||Object.prototype.hasOwnProperty.call(exports,s)||Object.defineProperty(exports,s,{enumerable:!0,get:function(){return e[s]}})}));
"use strict";var e=require("@nlbridge/core");const s=e=>{console.error(""+e+"")},t=e=>{console.warn(""+e+"")},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,a=(e,s)=>{switch(e){case"chat":return r(s);case"chat-stream":return o(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},n={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,r,o,a)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?a.write(e):t("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||a.end()},error:e=>{s(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",(()=>{n=!0})),e("chat-stream",r.message,c,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},c=(r,o,c)=>{if(!c.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=e.asOpenAiChatModel(c.chatModel),{run:d}=e.createRuntime(o,{apiKey:c.apiKey,chatModel:u});return async(r,o,c)=>{if("application/json"!==r.headers["content-type"])return void o.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(!r.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 o.status(500).send("Internal nlbridge middleware error occurred.");if("string"==typeof r.body)try{r.body=JSON.parse(r.body)}catch(e){return t("@nlbridge/express middleware was unable to parse the request body as JSON."),void o.status(400).send("Invalid request body. Please ensure that the request body is a valid JSON object.")}if("object"!=typeof r.body||null===r.body||0===Object.keys(r.body).length)return void o.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:u,payload:i}=r.body,l=e.asValidActionId(u);if(!l)return void o.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 y=a(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const p=n[l];if(p)try{await p(d,i,r,o)}catch(e){s(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),o.status(500).send("Internal server error while executing action.")}else o.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};exports.defaultMiddleware=(s,t)=>c(s,e.defaultActionHandlers,t),exports.middleware=c,Object.keys(e).forEach((function(s){"default"===s||Object.prototype.hasOwnProperty.call(exports,s)||Object.defineProperty(exports,s,{enumerable:!0,get:function(){return e[s]}})}));

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

import{asOpenAiChatModel as e,createRuntime as s,asValidActionId as t,actionIds as r,defaultActionHandlers as o}from"@nlbridge/core";export*from"@nlbridge/core";const a=e=>{console.error(""+e+"")},c=e=>{console.warn(""+e+"")},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},u=n,d=(e,s)=>{switch(e){case"chat":return n(s);case"chat-stream":return u(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},i={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,s,t,r)=>{let o=!1;const n={next:e=>{o||("string"==typeof e||"number"==typeof e?r.write(e):c("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{o||r.end()},error:e=>{a(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,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},l=(e,s)=>y(e,o,s),y=(o,n,u)=>{if(!u.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const l=e(u.chatModel),{run:y}=s(n,{apiKey:u.apiKey,chatModel:l});return async(e,s,o)=>{if("application/json"!==e.headers["content-type"])return void s.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 a("@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 s.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."),void s.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 s.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:n,payload:u}=e.body,l=t(n);if(!l)return void s.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 m=d(l,u);if(!m.success)return void s.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${m.error}`});const p=i[l];if(p)try{await p(y,u,e,s)}catch(e){a(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),s.status(500).send("Internal server error.")}else s.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};export{l as defaultMiddleware,y as middleware};
import{asOpenAiChatModel as e,createRuntime as s,asValidActionId as t,actionIds as r,defaultActionHandlers as o}from"@nlbridge/core";export*from"@nlbridge/core";const a=e=>{console.error(""+e+"")},c=e=>{console.warn(""+e+"")},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},u=n,d=(e,s)=>{switch(e){case"chat":return n(s);case"chat-stream":return u(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},i={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,s,t,r)=>{let o=!1;const n={next:e=>{o||("string"==typeof e||"number"==typeof e?r.write(e):c("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{o||r.end()},error:e=>{a(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,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},l=(e,s)=>y(e,o,s),y=(o,n,u)=>{if(!u.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const l=e(u.chatModel),{run:y}=s(n,{apiKey:u.apiKey,chatModel:l});return async(e,s,o)=>{if("application/json"!==e.headers["content-type"])return void s.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 a("@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 s.status(500).send("Internal nlbridge middleware error occurred.");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."),void s.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 s.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:n,payload:u}=e.body,l=t(n);if(!l)return void s.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 m=d(l,u);if(!m.success)return void s.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${m.error}`});const p=i[l];if(p)try{await p(y,u,e,s)}catch(e){a(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),s.status(500).send("Internal server error while executing action.")}else s.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};export{l as defaultMiddleware,y as middleware};
{
"name": "@nlbridge/express",
"version": "0.3.0",
"version": "0.3.1",
"description": "An express middleware to connect to several LLMs",

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

"dependencies": {
"@nlbridge/core": "0.3.0"
"@nlbridge/core": "0.3.1"
},

@@ -62,0 +62,0 @@ "devDependencies": {

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

!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],s):s((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,s){"use strict";const t=e=>{console.error(""+e+"")},r=e=>{console.warn(""+e+"")},o=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=o,n=(e,s)=>{switch(e){case"chat":return o(s);case"chat-stream":return a(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},c={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,s,o,a)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?a.write(e):r("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||a.end()},error:e=>{t(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",(()=>{n=!0})),e("chat-stream",s.message,c,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},u=(e,o,a)=>{if(!a.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=s.asOpenAiChatModel(a.chatModel),{run:d}=s.createRuntime(o,{apiKey:a.apiKey,chatModel:u});return async(e,o,a)=>{if("application/json"!==e.headers["content-type"])return void o.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 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 o.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return r("@nlbridge/express middleware was unable to parse the request body as JSON."),void o.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 o.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:u,payload:i}=e.body,l=s.asValidActionId(u);if(!l)return void o.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: "+s.actionIds.join(", ")});const y=n(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const p=c[l];if(p)try{await p(d,i,e,o)}catch(e){t(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),o.status(500).send("Internal server error.")}else o.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};e.defaultMiddleware=(e,t)=>u(e,s.defaultActionHandlers,t),e.middleware=u,Object.keys(s).forEach((function(t){"default"===t||Object.prototype.hasOwnProperty.call(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:function(){return s[t]}})}))}));
!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@nlbridge/core")):"function"==typeof define&&define.amd?define(["exports","@nlbridge/core"],s):s((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlbridge/express"]={},e.core)}(this,(function(e,s){"use strict";const t=e=>{console.error(""+e+"")},r=e=>{console.warn(""+e+"")},o=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=o,n=(e,s)=>{switch(e){case"chat":return o(s);case"chat-stream":return a(s);case"assist":return(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})(s);case"create-context":return(e=>{if(null==e)return{success:!0,payload:null};if("object"!=typeof e||!e||Object.keys(e).length>1)return{success:!1,error:"Payload can only have one optional key: items."};const s=e.items;return"object"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"Payload.items must be a valid object."}})(s);case"get-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};const s=Object.keys(e).length;if(1!==s&&2!==s)return{success:!1,error:"Payload must have exactly one or two keys: contextId and key."};const t=e.contextId;if("string"!=typeof t||!t)return{success:!1,error:"payload.contextId must be a non-empty string."};if(1===s)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."}})(s);case"discard-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 s=e.contextId;return"string"==typeof s&&s?{success:!0,payload:e}:{success:!1,error:"payload.contextId must be a non-empty string."}})(s);case"reset-context-items":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and items (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.items must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-items":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 items."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.items?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-items":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 itemIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.itemIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All itemIds must be strings."}:{success:!1,error:"Payload.itemIds must be an array."}})(s);case"reset-context-tasks":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(Object.keys(e).length<0||Object.keys(e).length>2)return{success:!1,error:"Payload must have exactly two keys: contextId (required) and tasks (optional)."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.items;return void 0!==t&&"object"!=typeof t?{success:!1,error:"When provided, Payload.tasks must be an object or null."}:{success:!0,payload:e}})(s);case"update-context-tasks":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 tasks."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.tasks?{success:!1,error:"Payload.items must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"remove-context-tasks":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 taskIds."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"Payload.contextId must be a non-empty string."};const t=e.taskIds;return Array.isArray(t)?t.every((e=>"string"==typeof e))?{success:!0,payload:e}:{success:!1,error:"All taskIds must be strings."}:{success:!1,error:"Payload.taskIds must be an array."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},c={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message}}):r.status(500).send({success:!1,error:o.error})},"chat-stream":(e,s,o,a)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?a.write(e):r("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||a.end()},error:e=>{t(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",(()=>{n=!0})),e("chat-stream",s.message,c,{})},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,{contextId:s.contextId});o.success?r.status(200).send({success:!0,result:{response:o.message,task:o.task}}):r.status(500).send({success:!1,error:o.error})},"create-context":async(e,s,t,r)=>{const o=await e("create-context",s?.items,{});o.success?r.status(200).send({success:!0,result:{contextId:o.contextId}}):r.status(500).send({success:!1,error:o.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,{});o.success?r.status(200).send({success:!0,result:{items:o.items,tasks:o.tasks}}):r.status(500).send({success:!1,error:o.error})},"discard-context":async(e,s,t,r)=>{const o=await e("discard-context",s.contextId,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-items":async(e,s,t,r)=>{const o=await e("reset-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-items":async(e,s,t,r)=>{const o=await e("update-items",s.contextId,s.items,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-items":async(e,s,t,r)=>{const o=await e("remove-items",s.contextId,s.itemIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"reset-context-tasks":async(e,s,t,r)=>{const o=await e("reset-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"update-context-tasks":async(e,s,t,r)=>{const o=await e("update-tasks",s.contextId,s.tasks,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})},"remove-context-tasks":async(e,s,t,r)=>{const o=await e("remove-tasks",s.contextId,s.taskIds,{});o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},u=(e,o,a)=>{if(!a.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=s.asOpenAiChatModel(a.chatModel),{run:d}=s.createRuntime(o,{apiKey:a.apiKey,chatModel:u});return async(e,o,a)=>{if("application/json"!==e.headers["content-type"])return void o.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 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 o.status(500).send("Internal nlbridge middleware error occurred.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return r("@nlbridge/express middleware was unable to parse the request body as JSON."),void o.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 o.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:u,payload:i}=e.body,l=s.asValidActionId(u);if(!l)return void o.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: "+s.actionIds.join(", ")});const y=n(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const p=c[l];if(p)try{await p(d,i,e,o)}catch(e){t(`@nlbridge/express middleware encountered an error when handling the request: ${e?.toString()}`),o.status(500).send("Internal server error while executing action.")}else o.status(500).send({status:"error",message:`The action provided "${l}" is not supported.`})}};e.defaultMiddleware=(e,t)=>u(e,s.defaultActionHandlers,t),e.middleware=u,Object.keys(s).forEach((function(t){"default"===t||Object.prototype.hasOwnProperty.call(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:function(){return s[t]}})}))}));
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