@receptron/graphai_express 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/express.d.ts CHANGED
@@ -2,6 +2,6 @@ import express from "./express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
3
3
  export declare const agentsList: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
4
4
  export declare const agentDoc: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
5
- export declare const agentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
6
- export declare const nonStreamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
7
- export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
5
+ export declare const agentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
6
+ export declare const nonStreamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
7
+ export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
package/lib/express.js CHANGED
@@ -55,48 +55,59 @@ exports.agentDoc = agentDoc;
55
55
  const agentDispatcher = (agentDictionary, agentFilters = []) => {
56
56
  const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters);
57
57
  const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters);
58
- return async (req, res) => {
58
+ return async (req, res, next) => {
59
59
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
60
60
  if (isStreaming) {
61
- return await stream(req, res);
61
+ return await stream(req, res, next);
62
62
  }
63
- return await nonStram(req, res);
63
+ return await nonStram(req, res, next);
64
64
  };
65
65
  };
66
66
  exports.agentDispatcher = agentDispatcher;
67
67
  // express middleware
68
68
  // run agent
69
69
  const nonStreamAgentDispatcher = (agentDictionary, agentFilters = []) => {
70
- return async (req, res) => {
71
- const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters);
72
- const result = await dispatcher(req, res);
73
- return res.json(result);
70
+ return async (req, res, next) => {
71
+ try {
72
+ const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters);
73
+ const result = await dispatcher(req, res);
74
+ return res.json(result);
75
+ }
76
+ catch (e) {
77
+ next(e);
78
+ }
79
+ ;
74
80
  };
75
81
  };
76
82
  exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
77
83
  // express middleware
78
84
  // run agent with streaming
79
85
  const streamAgentDispatcher = (agentDictionary, agentFilters = []) => {
80
- return async (req, res) => {
81
- res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
82
- res.setHeader("Cache-Control", "no-cache, no-transform");
83
- res.setHeader("X-Accel-Buffering", "no");
84
- const callback = (context, token) => {
85
- if (token) {
86
- res.write(token);
87
- }
88
- };
89
- const streamAgentFilter = {
90
- name: "streamAgentFilter",
91
- agent: (0, agent_filters_1.streamAgentFilterGenerator)(callback),
92
- };
93
- const filterList = [...agentFilters, streamAgentFilter];
94
- const dispatcher = agentDispatcherInternal(agentDictionary, filterList);
95
- const result = await dispatcher(req, res);
96
- const json_data = JSON.stringify(result);
97
- res.write("___END___");
98
- res.write(json_data);
99
- return res.end();
86
+ return async (req, res, next) => {
87
+ try {
88
+ res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
89
+ res.setHeader("Cache-Control", "no-cache, no-transform");
90
+ res.setHeader("X-Accel-Buffering", "no");
91
+ const callback = (context, token) => {
92
+ if (token) {
93
+ res.write(token);
94
+ }
95
+ };
96
+ const streamAgentFilter = {
97
+ name: "streamAgentFilter",
98
+ agent: (0, agent_filters_1.streamAgentFilterGenerator)(callback),
99
+ };
100
+ const filterList = [...agentFilters, streamAgentFilter];
101
+ const dispatcher = agentDispatcherInternal(agentDictionary, filterList);
102
+ const result = await dispatcher(req, res);
103
+ const json_data = JSON.stringify(result);
104
+ res.write("___END___");
105
+ res.write(json_data);
106
+ return res.end();
107
+ }
108
+ catch (e) {
109
+ next(e);
110
+ }
100
111
  };
101
112
  };
102
113
  exports.streamAgentDispatcher = streamAgentDispatcher;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/express.js",
6
6
  "files": [