@receptron/graphai_express 0.0.14 → 0.0.16
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 +3 -3
- package/lib/express.js +37 -27
- package/package.json +2 -2
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,58 @@ 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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
+
}
|
|
74
79
|
};
|
|
75
80
|
};
|
|
76
81
|
exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
|
|
77
82
|
// express middleware
|
|
78
83
|
// run agent with streaming
|
|
79
84
|
const streamAgentDispatcher = (agentDictionary, agentFilters = []) => {
|
|
80
|
-
return async (req, res) => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
85
|
+
return async (req, res, next) => {
|
|
86
|
+
try {
|
|
87
|
+
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
|
88
|
+
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
89
|
+
res.setHeader("X-Accel-Buffering", "no");
|
|
90
|
+
const callback = (context, token) => {
|
|
91
|
+
if (token) {
|
|
92
|
+
res.write(token);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const streamAgentFilter = {
|
|
96
|
+
name: "streamAgentFilter",
|
|
97
|
+
agent: (0, agent_filters_1.streamAgentFilterGenerator)(callback),
|
|
98
|
+
};
|
|
99
|
+
const filterList = [...agentFilters, streamAgentFilter];
|
|
100
|
+
const dispatcher = agentDispatcherInternal(agentDictionary, filterList);
|
|
101
|
+
const result = await dispatcher(req, res);
|
|
102
|
+
const json_data = JSON.stringify(result);
|
|
103
|
+
res.write("___END___");
|
|
104
|
+
res.write(json_data);
|
|
105
|
+
return res.end();
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
next(e);
|
|
109
|
+
}
|
|
100
110
|
};
|
|
101
111
|
};
|
|
102
112
|
exports.streamAgentDispatcher = streamAgentDispatcher;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@receptron/graphai_express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "GraphAI express web server middleware.",
|
|
5
5
|
"main": "lib/express.js",
|
|
6
6
|
"files": [
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/receptron/graphai_utils/tree/main/packages/express#readme",
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@graphai/agents": "^0.0.
|
|
29
|
+
"@graphai/agents": "^0.0.5",
|
|
30
30
|
"@types/express": "^4.17.21",
|
|
31
31
|
"@types/node": "^20.8.7",
|
|
32
32
|
"eslint": "^9.3.0",
|