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.2 to 0.3.4

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 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. Error: ${e?.toString()}`)}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={getLlmInstructions:()=>({})},o=e=>({...r,contextId:e}),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},n=a,c=(e,s)=>{switch(e){case"chat":return a(s);case"chat-stream":return n(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.`}}},u={chat:async(e,s,t,r)=>{const a=await e("chat",s.message,o(s.contextId));a.success?r.status(200).send({success:!0,result:{response:a.message}}):r.status(500).send({success:!1,error:a.error})},"chat-stream":(e,r,a,n)=>{let c=!1;const u={next:e=>{c||("string"==typeof e||"number"==typeof e?n.write(e):t("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{c||n.end()},error:e=>{s(e)}};n.setHeader("Cache-Control","no-cache"),n.setHeader("Content-Type","text/event-stream"),n.setHeader("Access-Control-Allow-Origin","*"),n.setHeader("Connection","keep-alive"),n.flushHeaders(),n.on("close",(()=>{c=!0})),e("chat-stream",r.message,u,o(r.contextId))},assist:async(e,s,t,r)=>{const a=await e("assist",s.message,o(s.contextId));a.success?r.status(200).send({success:!0,result:{response:a.message,task:a.task}}):r.status(500).send({success:!1,error:a.error})},"create-context":async(e,s,t,o)=>{const a=await e("create-context",s?.items,r);a.success?o.status(200).send({success:!0,result:{contextId:a.contextId}}):o.status(500).send({success:!1,error:a.error})},"get-context":async(e,s,t,r)=>{const a=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,o(s.contextId));a.success?r.status(200).send({success:!0,result:{items:a.items,tasks:a.tasks}}):r.status(500).send({success:!1,error:a.error})},"discard-context":async(e,s,t,r)=>{const a=await e("discard-context",s.contextId,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"reset-context-items":async(e,s,t,r)=>{const a=await e("reset-items",s.contextId,s.items,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"update-context-items":async(e,s,t,r)=>{const a=await e("update-items",s.contextId,s.items,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"remove-context-items":async(e,s,t,r)=>{const a=await e("remove-items",s.contextId,s.itemIds,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"reset-context-tasks":async(e,s,t,r)=>{const a=await e("reset-tasks",s.contextId,s.tasks,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"update-context-tasks":async(e,s,t,r)=>{const a=await e("update-tasks",s.contextId,s.tasks,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})},"remove-context-tasks":async(e,s,t,r)=>{const a=await e("remove-tasks",s.contextId,s.taskIds,o(s.contextId));a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})}},d=(r,o,a)=>{if(!a.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const n=e.asOpenAiChatModel(a.chatModel),{run:d}=e.createRuntime(o,a.llmInstructions,{apiKey:a.apiKey,chatModel:n});return async(r,o,a)=>{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:n,payload:i}=r.body,l=e.asValidActionId(n);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=c(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const m=u[l];if(m)try{await m(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)=>d(s,e.defaultActionHandlers,t),exports.middleware=d,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 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. Error: ${e?.toString()}`)}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 n=e=>{console.error(""+e+"")},c=e=>{console.warn(""+e+"")},a={getLlmInstructions:()=>({})},u=e=>({...a,contextId:e}),d=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},i=d,l=(e,s)=>{switch(e){case"chat":return d(s);case"chat-stream":return i(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.`}}},y={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,u(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 a={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=>{n(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,a,u(s.contextId))},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,u(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,a);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,u(s.contextId));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,u(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,u(s.contextId));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,u(s.contextId));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,u(s.contextId));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,u(s.contextId));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,u(s.contextId));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,u(s.contextId));o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},m=(e,s)=>p(e,o,s),p=(o,a,u)=>{if(!u.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const d=e(u.chatModel),{run:i}=s(a,u.llmInstructions,{apiKey:u.apiKey,chatModel:d});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 n("@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:a,payload:u}=e.body,d=t(a);if(!d)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=l(d,u);if(!m.success)return void s.status(400).send({status:"error",message:`Invalid payload for action "${d}". ${m.error}`});const p=y[d];if(p)try{await p(i,u,e,s)}catch(e){n(`@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 "${d}" is not supported.`})}};export{m as defaultMiddleware,p as middleware};

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

import { ActionHandlerConfig } from '@nlbridge/core';
import { LlmInstructions, ActionHandlerConfig } from '@nlbridge/core';
export * from '@nlbridge/core';

@@ -8,2 +8,3 @@ import { Request, Response, NextFunction } from 'express';

chatModel?: string;
llmInstructions?: LlmInstructions;
};

@@ -14,2 +15,2 @@

export { defaultMiddleware, middleware };
export { type MiddlewareConfig, defaultMiddleware, middleware };
{
"name": "@nlbridge/express",
"version": "0.3.2",
"version": "0.3.4",
"description": "An express middleware to connect to several LLMs",

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

"dependencies": {
"@nlbridge/core": "0.3.2"
"@nlbridge/core": "0.3.4"
},

@@ -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},n=o,a=(e,s)=>{switch(e){case"chat":return o(s);case"chat-stream":return n(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,n)=>{let a=!1;const c={next:e=>{a||("string"==typeof e||"number"==typeof e?n.write(e):r("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{a||n.end()},error:e=>{t(e)}};n.setHeader("Cache-Control","no-cache"),n.setHeader("Content-Type","text/event-stream"),n.setHeader("Access-Control-Allow-Origin","*"),n.setHeader("Connection","keep-alive"),n.flushHeaders(),n.on("close",(()=>{a=!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,n)=>{if(!n.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const u=s.asOpenAiChatModel(n.chatModel),{run:d}=s.createRuntime(o,{apiKey:n.apiKey,chatModel:u});return async(e,o,n)=>{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=a(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. Error: ${e?.toString()}`)}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={getLlmInstructions:()=>({})},n=e=>({...o,contextId:e}),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},c=a,u=(e,s)=>{switch(e){case"chat":return a(s);case"chat-stream":return c(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.`}}},d={chat:async(e,s,t,r)=>{const o=await e("chat",s.message,n(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 c=!1;const u={next:e=>{c||("string"==typeof e||"number"==typeof e?a.write(e):r("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{c||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",(()=>{c=!0})),e("chat-stream",s.message,u,n(s.contextId))},assist:async(e,s,t,r)=>{const o=await e("assist",s.message,n(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 n=await e("create-context",s?.items,o);n.success?r.status(200).send({success:!0,result:{contextId:n.contextId}}):r.status(500).send({success:!1,error:n.error})},"get-context":async(e,s,t,r)=>{const o=await e("get-context",s.contextId,s.itemId,s.itemId?"task":void 0,n(s.contextId));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,n(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,n(s.contextId));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,n(s.contextId));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,n(s.contextId));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,n(s.contextId));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,n(s.contextId));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,n(s.contextId));o.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:o.error})}},i=(e,o,n)=>{if(!n.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const a=s.asOpenAiChatModel(n.chatModel),{run:c}=s.createRuntime(o,n.llmInstructions,{apiKey:n.apiKey,chatModel:a});return async(e,o,n)=>{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:a,payload:i}=e.body,l=s.asValidActionId(a);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=u(l,i);if(!y.success)return void o.status(400).send({status:"error",message:`Invalid payload for action "${l}". ${y.error}`});const p=d[l];if(p)try{await p(c,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)=>i(e,s.defaultActionHandlers,t),e.middleware=i,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