@nlbridge/express
Advanced tools
Comparing version 0.2.7 to 0.3.0
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("@nlbridge/core");const s=e=>{console.error("[31m"+e+"[0m")},t=e=>{console.warn("[33m"+e+"[0m")},r=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},a=r,o=(e,s)=>{switch(e){case"chat":return r(s);case"chat-stream":return a(s);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(s);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"get-context-data":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"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const 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"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"register-task":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(3!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly 3 keys: contextId, taskId, and parameters."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;if("string"!=typeof t||!t)return{success:!1,error:"payload.taskId must be a non-empty string."};const r=e.parameters;return!Array.isArray(r)||r.some((e=>"string"!=typeof e||!e))?{success:!1,error:"payload.parameters must be an array of non-empty strings."}:{success:!0,payload:e}})(s);case"unregister-task":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 2 keys: contextId, taskId."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.taskId must be a non-empty string."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},n=(r,a,n)=>{if(!n.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const c=e.asOpenAiChatModel(n.chatModel),{run:d}=e.createRuntime(a,{apiKey:n.apiKey,chatModel:c});return async(r,a,n)=>{if("application/json"!==r.headers["content-type"])return void a.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!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 a.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 a.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 a.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:c,payload:u}=r.body,i=e.asValidActionId(c);if(!i)return void a.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+e.actionIds.join(", ")});const l=o(i,u);if(l.success)switch(i){case"chat-stream":return void((e,r,a,o)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?o.write(e):t("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||o.end()},error:e=>{s(e)}};o.setHeader("Cache-Control","no-cache"),o.setHeader("Content-Type","text/event-stream"),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Connection","keep-alive"),o.flushHeaders(),o.on("close",(()=>{n=!0})),e("chat-stream",r.message,c,{})})(d,u,0,a);case"chat":return void await(async(e,s,t,r)=>{const a=await e("chat",s.message,{contextId:s.contextId});a.success?r.status(200).send({success:!0,result:{response:a.message}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"set-context":return void await(async(e,s,t,r)=>{const a=await e("set-context",s?.data||void 0,{});a.success?r.status(200).send({success:!0,result:{contextId:a.contextId}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"get-context-data":return void await(async(e,s,t,r)=>{const a=await e("get-context-data",s.contextId||void 0,s.key||void 0,{});a.success?r.status(200).send({success:!0,result:{data:a.data,tasks:a.tasks}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"update-context":return void await(async(e,s,t,r)=>{const a=await e("update-context",s.contextId,s.data,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"clear-context":return void await(async(e,s,t,r)=>{const a=await e("clear-context",s.contextId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"register-task":return void await(async(e,s,t,r)=>{const a=await e("register-task",s.contextId,s.taskId,s.parameters,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"unregister-task":return void await(async(e,s,t,r)=>{const a=await e("unregister-task",s.contextId,s.taskId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"assist":return void await(async(e,s,t,r)=>{const a=await e("assist",s.message,{contextId: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})})(d,u,0,a);default:a.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else a.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}};Object.defineProperty(exports,"actionIds",{enumerable:!0,get:function(){return e.actionIds}}),Object.defineProperty(exports,"defaultActionHandlers",{enumerable:!0,get:function(){return e.defaultActionHandlers}}),exports.defaultMiddleware=(s,t)=>n(s,e.defaultActionHandlers,t),exports.middleware=n; | ||
"use strict";var e=require("@nlbridge/core");const s=e=>{console.error("[31m"+e+"[0m")},t=e=>{console.warn("[33m"+e+"[0m")},r=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},o=r,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]}})})); |
@@ -1,1 +0,1 @@ | ||
import{asOpenAiChatModel as e,createRuntime as s,asValidActionId as t,actionIds as r,defaultActionHandlers as a}from"@nlbridge/core";export{actionIds,defaultActionHandlers}from"@nlbridge/core";const o=e=>{console.error("[31m"+e+"[0m")},n=e=>{console.warn("[33m"+e+"[0m")},c=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},u=c,d=(e,s)=>{switch(e){case"chat":return c(s);case"chat-stream":return u(s);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(s);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"get-context-data":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"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const 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"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"register-task":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(3!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly 3 keys: contextId, taskId, and parameters."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;if("string"!=typeof t||!t)return{success:!1,error:"payload.taskId must be a non-empty string."};const r=e.parameters;return!Array.isArray(r)||r.some((e=>"string"!=typeof e||!e))?{success:!1,error:"payload.parameters must be an array of non-empty strings."}:{success:!0,payload:e}})(s);case"unregister-task":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 2 keys: contextId, taskId."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.taskId must be a non-empty string."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},i=(e,s)=>l(e,a,s),l=(a,c,u)=>{if(!u.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const i=e(u.chatModel),{run:l}=s(c,{apiKey:u.apiKey,chatModel:i});return async(e,s,a)=>{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 o("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void s.status(500).send("Internal server error.");if("string"==typeof e.body)try{e.body=JSON.parse(e.body)}catch(e){return n("@nlbridge/express middleware was unable to parse the request body as JSON."),void 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:c,payload:u}=e.body,i=t(c);if(!i)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 y=d(i,u);if(y.success)switch(i){case"chat-stream":return void((e,s,t,r)=>{let a=!1;const c={next:e=>{a||("string"==typeof e||"number"==typeof e?r.write(e):n("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{a||r.end()},error:e=>{o(e)}};r.setHeader("Cache-Control","no-cache"),r.setHeader("Content-Type","text/event-stream"),r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Connection","keep-alive"),r.flushHeaders(),r.on("close",(()=>{a=!0})),e("chat-stream",s.message,c,{})})(l,u,0,s);case"chat":return void await(async(e,s,t,r)=>{const a=await e("chat",s.message,{contextId:s.contextId});a.success?r.status(200).send({success:!0,result:{response:a.message}}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"set-context":return void await(async(e,s,t,r)=>{const a=await e("set-context",s?.data||void 0,{});a.success?r.status(200).send({success:!0,result:{contextId:a.contextId}}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"get-context-data":return void await(async(e,s,t,r)=>{const a=await e("get-context-data",s.contextId||void 0,s.key||void 0,{});a.success?r.status(200).send({success:!0,result:{data:a.data,tasks:a.tasks}}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"update-context":return void await(async(e,s,t,r)=>{const a=await e("update-context",s.contextId,s.data,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"clear-context":return void await(async(e,s,t,r)=>{const a=await e("clear-context",s.contextId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"register-task":return void await(async(e,s,t,r)=>{const a=await e("register-task",s.contextId,s.taskId,s.parameters,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"unregister-task":return void await(async(e,s,t,r)=>{const a=await e("unregister-task",s.contextId,s.taskId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(l,u,0,s);case"assist":return void await(async(e,s,t,r)=>{const a=await e("assist",s.message,{contextId: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})})(l,u,0,s);default:s.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else s.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${y.error}`})}};export{i as defaultMiddleware,l 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("[31m"+e+"[0m")},c=e=>{console.warn("[33m"+e+"[0m")},n=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},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 { ActionHandlerConfig } from '@nlbridge/core'; | ||
export { ActionExtras, ActionHandlerConfig, ChatHandler, ChatStreamHandler, ChatStreamObserver, ClearContextHandler, GetContextDataHandler, SetContextHandler, UpdateContextHandler, actionIds, defaultActionHandlers } from '@nlbridge/core'; | ||
export * from '@nlbridge/core'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
@@ -4,0 +4,0 @@ |
{ | ||
"name": "@nlbridge/express", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"description": "An express middleware to connect to several LLMs", | ||
@@ -59,3 +59,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlbridge/core": "0.2.7" | ||
"@nlbridge/core": "0.3.0" | ||
}, | ||
@@ -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("[31m"+e+"[0m")},r=e=>{console.warn("[33m"+e+"[0m")},a=e=>"object"!=typeof e||null===e?{success:!1,error:"payload is not an object"}:"string"!=typeof e.message?{success:!1,error:"payload.message is not a string"}:{success:!0,payload:e},o=a,n=(e,s)=>{switch(e){case"chat":return a(s);case"chat-stream":return o(s);case"set-context":return(e=>null==e?{success:!0,payload:null}:"object"==typeof e&&e&&1===Object.keys(e).length?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload must be an object with exactly one key."})(s);case"update-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(2!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly two keys: contextId and data."};const s=e.contextId;return"string"==typeof s&&s?"object"!=typeof e.data?{success:!1,error:"Payload.data must be an object or null."}:{success:!0,payload:e}:{success:!1,error:"Payload.contextId must be a non-empty string."}})(s);case"get-context-data":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"clear-context":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(1!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly one key: contextId."};const 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"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"register-task":return(e=>{if("object"!=typeof e||null===e)return{success:!1,error:"Payload must be an object."};if(3!==Object.keys(e).length)return{success:!1,error:"Payload must have exactly 3 keys: contextId, taskId, and parameters."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;if("string"!=typeof t||!t)return{success:!1,error:"payload.taskId must be a non-empty string."};const r=e.parameters;return!Array.isArray(r)||r.some((e=>"string"!=typeof e||!e))?{success:!1,error:"payload.parameters must be an array of non-empty strings."}:{success:!0,payload:e}})(s);case"unregister-task":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 2 keys: contextId, taskId."};const s=e.contextId;if("string"!=typeof s||!s)return{success:!1,error:"payload.contextId must be a non-empty string."};const t=e.taskId;return"string"==typeof t&&t?{success:!0,payload:e}:{success:!1,error:"payload.taskId must be a non-empty string."}})(s);default:return{success:!1,error:`The action provided "${e}" is not supported.`}}},c=(e,a,o)=>{if(!o.apiKey)throw new Error("Missing required API key for OpenAI middleware. Please provide an API key.");const c=s.asOpenAiChatModel(o.chatModel),{run:d}=s.createRuntime(a,{apiKey:o.apiKey,chatModel:c});return async(e,a,o)=>{if("application/json"!==e.headers["content-type"])return void a.status(400).send("Invalid Content-Type header. Please ensure that the request body is a valid JSON object and that the Content-Type header is set to application/json.");if(!e.body)return t("@nlbridge/express | 500\nInvalid use of @nlbridge/express middleware: The request body is not available in req.body.\nPlease ensure that the request body is available by using a body parser middleware before the @nlbridge/express middleware."),void a.status(500).send("Internal server error.");if("string"==typeof 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 a.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 a.status(400).send({status:"error",message:"@nlbridge/express middleware found invalid request body. Value must be a non-null non-empty JSON object."});const{action:c,payload:u}=e.body,i=s.asValidActionId(c);if(!i)return void a.status(400).send({status:"error",message:"Action is not valid. Please ensure that the action provided is a valid action ID. Valid action IDs are: "+s.actionIds.join(", ")});const l=n(i,u);if(l.success)switch(i){case"chat-stream":return void((e,s,a,o)=>{let n=!1;const c={next:e=>{n||("string"==typeof e||"number"==typeof e?o.write(e):r("Invalid chunk type in chat stream that cannot be sent to the client."))},complete:()=>{n||o.end()},error:e=>{t(e)}};o.setHeader("Cache-Control","no-cache"),o.setHeader("Content-Type","text/event-stream"),o.setHeader("Access-Control-Allow-Origin","*"),o.setHeader("Connection","keep-alive"),o.flushHeaders(),o.on("close",(()=>{n=!0})),e("chat-stream",s.message,c,{})})(d,u,0,a);case"chat":return void await(async(e,s,t,r)=>{const a=await e("chat",s.message,{contextId:s.contextId});a.success?r.status(200).send({success:!0,result:{response:a.message}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"set-context":return void await(async(e,s,t,r)=>{const a=await e("set-context",s?.data||void 0,{});a.success?r.status(200).send({success:!0,result:{contextId:a.contextId}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"get-context-data":return void await(async(e,s,t,r)=>{const a=await e("get-context-data",s.contextId||void 0,s.key||void 0,{});a.success?r.status(200).send({success:!0,result:{data:a.data,tasks:a.tasks}}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"update-context":return void await(async(e,s,t,r)=>{const a=await e("update-context",s.contextId,s.data,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"clear-context":return void await(async(e,s,t,r)=>{const a=await e("clear-context",s.contextId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"register-task":return void await(async(e,s,t,r)=>{const a=await e("register-task",s.contextId,s.taskId,s.parameters,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"unregister-task":return void await(async(e,s,t,r)=>{const a=await e("unregister-task",s.contextId,s.taskId,{});a.success?r.status(200).send({success:!0}):r.status(500).send({success:!1,error:a.error})})(d,u,0,a);case"assist":return void await(async(e,s,t,r)=>{const a=await e("assist",s.message,{contextId: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})})(d,u,0,a);default:a.status(500).send({status:"error",message:`The action provided "${i}" is not supported.`})}else a.status(400).send({status:"error",message:`Invalid payload for action "${i}". ${l.error}`})}};Object.defineProperty(e,"actionIds",{enumerable:!0,get:function(){return s.actionIds}}),Object.defineProperty(e,"defaultActionHandlers",{enumerable:!0,get:function(){return s.defaultActionHandlers}}),e.defaultMiddleware=(e,t)=>c(e,s.defaultActionHandlers,t),e.middleware=c})); | ||
!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("[31m"+e+"[0m")},r=e=>{console.warn("[33m"+e+"[0m")},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]}})}))})); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33564
124
1
+ Added@nlbridge/core@0.3.0(transitive)
- Removed@nlbridge/core@0.2.7(transitive)
Updated@nlbridge/core@0.3.0