@receptron/graphai_express 0.0.2 → 0.0.3
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/README.md +25 -2
- package/lib/express.d.ts +4 -2
- package/lib/express.js +84 -38
- package/package.json +9 -12
package/README.md
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
|
|
2
2
|
## GraphAI express middleware.
|
|
3
3
|
|
|
4
|
+
## Install
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
yarn add @receptron/graphai_express
|
|
8
|
+
```
|
|
9
|
+
|
|
4
10
|
## Usage
|
|
5
11
|
|
|
6
12
|
```TypeScript
|
|
7
13
|
|
|
8
14
|
import express from "express";
|
|
9
|
-
import { agentDispatcher, agentsList, agentDoc } from "@receptron/graphai_express";
|
|
15
|
+
import { agentDispatcher, streamAgentDispatcher, agentsList, agentDoc } from "@receptron/graphai_express";
|
|
10
16
|
|
|
11
17
|
const hostName = "https://example.net";
|
|
12
18
|
const apiPrefix = "/api/agents";
|
|
13
19
|
|
|
14
|
-
app.post(apiPrefix + "/:agentId", agentDispatcher); // dispatch agents
|
|
15
20
|
app.get(apiPrefix + "/:agentId", agentDoc(hostName, apiPrefix)); // each API(agent) document
|
|
16
21
|
app.get(apiPrefix + "/", agentsList(hostName, apiPrefix)); // API(agent) list
|
|
17
22
|
|
|
23
|
+
// non stream
|
|
24
|
+
app.post(apiPrefix + "/:agentId", agentDispatcher()); // dispatch agents
|
|
25
|
+
|
|
26
|
+
// stream
|
|
27
|
+
app.post(apiPrefix + "/stream/:agentId", streamAgentDispatcher());
|
|
28
|
+
|
|
18
29
|
```
|
|
19
30
|
|
|
20
31
|
|
|
@@ -25,3 +36,15 @@ T.B.D.
|
|
|
25
36
|
### from GraphAI Client
|
|
26
37
|
|
|
27
38
|
T.B.D.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### for this middleware development
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
git clone https://github.com/receptron/graphai_utils
|
|
45
|
+
cd packages/express
|
|
46
|
+
yarn install
|
|
47
|
+
yarn run server # then run test express server
|
|
48
|
+
yarn run test_stream
|
|
49
|
+
```
|
|
50
|
+
|
package/lib/express.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import express from "./express";
|
|
2
|
-
|
|
3
|
-
export declare const agentDispatcher: (req: express.Request, res: express.Response) => Promise<void>;
|
|
2
|
+
import { AgentFilterInfo } from "graphai/lib/type";
|
|
4
3
|
export declare const agentsList: (hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
|
|
4
|
+
export declare const agentDoc: (hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
|
|
5
|
+
export declare const agentDispatcher: (agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
|
|
6
|
+
export declare const streamAgentDispatcher: (agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
|
package/lib/express.js
CHANGED
|
@@ -23,9 +23,34 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.streamAgentDispatcher = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
|
|
27
|
+
const test_utils_1 = require("graphai/lib/utils/test_utils");
|
|
28
|
+
const stream_1 = require("graphai/lib/experimental_agent_filters/stream");
|
|
27
29
|
const agents = __importStar(require("graphai/lib/experimental_agents"));
|
|
28
30
|
const agentDictionary = agents;
|
|
31
|
+
// express middleware
|
|
32
|
+
// return agent list
|
|
33
|
+
const agentsList = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
34
|
+
return async (req, res) => {
|
|
35
|
+
const list = Object.keys(agentDictionary).map((agentName) => {
|
|
36
|
+
const agent = agentDictionary[agentName];
|
|
37
|
+
return {
|
|
38
|
+
agentId: agentName,
|
|
39
|
+
name: agent.name,
|
|
40
|
+
url: hostName + urlPath + "/" + agentName,
|
|
41
|
+
description: agent.description,
|
|
42
|
+
category: agent.category,
|
|
43
|
+
author: agent.author,
|
|
44
|
+
license: agent.license,
|
|
45
|
+
repository: agent.repository,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
res.json({ agents: list });
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
exports.agentsList = agentsList;
|
|
52
|
+
// express middleware
|
|
53
|
+
// return agent detail info
|
|
29
54
|
const agentDoc = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
30
55
|
return async (req, res) => {
|
|
31
56
|
const { params } = req;
|
|
@@ -50,45 +75,66 @@ const agentDoc = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
|
50
75
|
};
|
|
51
76
|
};
|
|
52
77
|
exports.agentDoc = agentDoc;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
res.
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
const result = await agent.agent({
|
|
63
|
-
params: agentParams,
|
|
64
|
-
inputs,
|
|
65
|
-
debugInfo: {
|
|
66
|
-
nodeId,
|
|
67
|
-
retry,
|
|
68
|
-
verbose: false,
|
|
69
|
-
},
|
|
70
|
-
agents,
|
|
71
|
-
filterParams: {},
|
|
72
|
-
});
|
|
73
|
-
res.json(result);
|
|
78
|
+
// express middleware
|
|
79
|
+
// run agent
|
|
80
|
+
const agentDispatcher = (agentFilters = []) => {
|
|
81
|
+
return async (req, res) => {
|
|
82
|
+
const dispatcher = agentDispatcherInternal(agentFilters);
|
|
83
|
+
const result = await dispatcher(req, res);
|
|
84
|
+
return res.json(result);
|
|
85
|
+
};
|
|
74
86
|
};
|
|
75
87
|
exports.agentDispatcher = agentDispatcher;
|
|
76
|
-
|
|
88
|
+
// express middleware
|
|
89
|
+
// run agent with streaming
|
|
90
|
+
const streamAgentDispatcher = (agentFilters = []) => {
|
|
77
91
|
return async (req, res) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
|
93
|
+
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
94
|
+
res.setHeader("X-Accel-Buffering", "no");
|
|
95
|
+
const callback = (context, token) => {
|
|
96
|
+
if (token) {
|
|
97
|
+
res.write(token);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const streamAgentFilter = {
|
|
101
|
+
name: "streamAgentFilter",
|
|
102
|
+
agent: (0, stream_1.streamAgentFilterGenerator)(callback),
|
|
103
|
+
};
|
|
104
|
+
const filterList = [...agentFilters, streamAgentFilter];
|
|
105
|
+
const dispatcher = agentDispatcherInternal(filterList);
|
|
106
|
+
const result = await dispatcher(req, res);
|
|
107
|
+
const json_data = JSON.stringify(result);
|
|
108
|
+
res.write("___END___");
|
|
109
|
+
res.write(json_data);
|
|
110
|
+
return res.end();
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
exports.streamAgentDispatcher = streamAgentDispatcher;
|
|
114
|
+
// dispatcher internal function
|
|
115
|
+
const agentDispatcherInternal = (agentFilters = []) => {
|
|
116
|
+
return async (req, res) => {
|
|
117
|
+
const { params } = req;
|
|
118
|
+
const { agentId } = params;
|
|
119
|
+
const { nodeId, retry, params: agentParams, inputs } = req.body;
|
|
120
|
+
const agent = agentDictionary[agentId];
|
|
121
|
+
if (agent === undefined) {
|
|
122
|
+
res.status(404).send("Not found");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const context = {
|
|
126
|
+
params: agentParams || {},
|
|
127
|
+
inputs,
|
|
128
|
+
debugInfo: {
|
|
129
|
+
nodeId,
|
|
130
|
+
retry,
|
|
131
|
+
verbose: false,
|
|
132
|
+
},
|
|
133
|
+
agents,
|
|
134
|
+
filterParams: {},
|
|
135
|
+
};
|
|
136
|
+
const agentFilterRunner = (0, test_utils_1.agentFilterRunnerBuilder)(agentFilters);
|
|
137
|
+
const result = await agentFilterRunner(context, agent.agent);
|
|
138
|
+
return result;
|
|
92
139
|
};
|
|
93
140
|
};
|
|
94
|
-
exports.agentsList = agentsList;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@receptron/graphai_express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "GraphAI express web server middleware.",
|
|
5
5
|
"main": "lib/express.js",
|
|
6
6
|
"files": [
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"build": "tsc && tsc-alias",
|
|
11
11
|
"eslint": "eslint --fix",
|
|
12
12
|
"server": "ts-node -r tsconfig-paths/register test/express.ts",
|
|
13
|
+
"test_stream": "ts-node -r tsconfig-paths/register test/stream_client.ts",
|
|
13
14
|
"format": "prettier --write '{src,test}/**/*.{yaml,ts,json}'",
|
|
14
15
|
"test": "node --test -r tsconfig-paths/register --require ts-node/register ./test/test_*.ts",
|
|
15
16
|
"b": "yarn run format && yarn run eslint && yarn run build"
|
|
@@ -23,19 +24,17 @@
|
|
|
23
24
|
"bugs": {
|
|
24
25
|
"url": "https://github.com/receptron/graphai_utils/issues"
|
|
25
26
|
},
|
|
26
|
-
"homepage": "https://github.com/receptron/graphai_utils#readme",
|
|
27
|
+
"homepage": "https://github.com/receptron/graphai_utils/tree/main/packages/express#readme",
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@types/express": "^4.17.21",
|
|
29
|
-
"@types/json-schema-generator": "^2.0.3",
|
|
30
30
|
"@types/node": "^20.8.7",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"ts-node": "^10.9.1",
|
|
31
|
+
"eslint": "^9.3.0",
|
|
32
|
+
"prettier": "^3.2.5",
|
|
33
|
+
"ts-node": "^10.9.2",
|
|
35
34
|
"tsc-alias": "^1.8.8",
|
|
36
35
|
"tsconfig-paths": "^4.2.0",
|
|
37
|
-
"typescript": "^5.4.
|
|
38
|
-
"typescript-eslint": "^7.
|
|
36
|
+
"typescript": "^5.4.5",
|
|
37
|
+
"typescript-eslint": "^7.9.0"
|
|
39
38
|
},
|
|
40
39
|
"dependencies": {
|
|
41
40
|
"@inquirer/prompts": "^5.0.0",
|
|
@@ -45,14 +44,12 @@
|
|
|
45
44
|
"express": "^4.19.2",
|
|
46
45
|
"graphai": "0.4.2",
|
|
47
46
|
"groq-sdk": "^0.3.3",
|
|
48
|
-
"json-schema-generator": "^2.0.6",
|
|
49
47
|
"openai": "^4.12.4",
|
|
50
48
|
"slashgpt": "^0.0.8",
|
|
51
49
|
"tiktoken": "^1.0.14",
|
|
52
50
|
"wikipedia": "^2.1.2",
|
|
53
51
|
"xml2js": "^0.6.2",
|
|
54
|
-
"yaml": "^2.3.3"
|
|
55
|
-
"yargs": "^17.7.2"
|
|
52
|
+
"yaml": "^2.3.3"
|
|
56
53
|
},
|
|
57
54
|
"types": "./lib/express.d.ts",
|
|
58
55
|
"directories": {
|