@receptron/graphai_express 0.0.6 → 0.0.8
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 +6 -2
- package/lib/express.d.ts +6 -5
- package/lib/express.js +25 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,8 @@ yarn add @receptron/graphai_express
|
|
|
13
13
|
|
|
14
14
|
import express from "express";
|
|
15
15
|
import * as agents from "graphai/lib/experimental_agents";
|
|
16
|
-
import { agentDispatcher, streamAgentDispatcher, agentsList, agentDoc } from "@receptron/graphai_express";
|
|
16
|
+
import { agentDispatcher, nonStreamAgentDispatcher, streamAgentDispatcher, agentsList, agentDoc } from "@receptron/graphai_express";
|
|
17
|
+
import { AgentFunctionInfoDictionary } from "graphai";
|
|
17
18
|
|
|
18
19
|
const agentDictionary: AgentFunctionInfoDictionary = agents;
|
|
19
20
|
const hostName = "https://example.net";
|
|
@@ -22,8 +23,11 @@ const apiPrefix = "/api/agents";
|
|
|
22
23
|
app.get(apiPrefix + "/:agentId", agentDoc(agentDictionary, hostName, apiPrefix)); // each API(agent) document
|
|
23
24
|
app.get(apiPrefix + "/", agentsList(agentDictionary, hostName, apiPrefix)); // API(agent) list
|
|
24
25
|
|
|
26
|
+
// non stream and strwam
|
|
27
|
+
app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
|
|
28
|
+
|
|
25
29
|
// non stream
|
|
26
|
-
app.post(apiPrefix + "/:agentId",
|
|
30
|
+
app.post(apiPrefix + "/nonstream/:agentId", nonStreamAgentDispatcher(agentDictionary));
|
|
27
31
|
|
|
28
32
|
// stream
|
|
29
33
|
app.post(apiPrefix + "/stream/:agentId", streamAgentDispatcher(agentDictionary));
|
package/lib/express.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import express from "./express";
|
|
2
|
-
import type { AgentFilterInfo } from "graphai";
|
|
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
|
|
2
|
+
import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
|
|
3
|
+
export declare const agentsList: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
|
|
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>>>;
|
package/lib/express.js
CHANGED
|
@@ -1,36 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.streamAgentDispatcher = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
|
|
3
|
+
exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
|
|
27
4
|
const graphai_1 = require("graphai");
|
|
28
5
|
const stream_1 = require("graphai/lib/experimental_agent_filters/stream");
|
|
29
|
-
const agents = __importStar(require("graphai/lib/experimental_agents"));
|
|
30
|
-
const agentDictionary = agents;
|
|
31
6
|
// express middleware
|
|
32
7
|
// return agent list
|
|
33
|
-
const agentsList = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
8
|
+
const agentsList = (agentDictionary, hostName = "https://example.com", urlPath = "/agent") => {
|
|
34
9
|
return async (req, res) => {
|
|
35
10
|
const list = Object.keys(agentDictionary).map((agentName) => {
|
|
36
11
|
const agent = agentDictionary[agentName];
|
|
@@ -51,7 +26,7 @@ const agentsList = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
|
51
26
|
exports.agentsList = agentsList;
|
|
52
27
|
// express middleware
|
|
53
28
|
// return agent detail info
|
|
54
|
-
const agentDoc = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
29
|
+
const agentDoc = (agentDictionary, hostName = "https://example.com", urlPath = "/agent") => {
|
|
55
30
|
return async (req, res) => {
|
|
56
31
|
const { params } = req;
|
|
57
32
|
const { agentId } = params;
|
|
@@ -77,17 +52,32 @@ const agentDoc = (hostName = "https://example.com", urlPath = "/agent") => {
|
|
|
77
52
|
exports.agentDoc = agentDoc;
|
|
78
53
|
// express middleware
|
|
79
54
|
// run agent
|
|
80
|
-
const agentDispatcher = (agentFilters = []) => {
|
|
55
|
+
const agentDispatcher = (agentDictionary, agentFilters = []) => {
|
|
56
|
+
const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters);
|
|
57
|
+
const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters);
|
|
81
58
|
return async (req, res) => {
|
|
82
|
-
const
|
|
59
|
+
const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
|
|
60
|
+
console.log(isStreaming);
|
|
61
|
+
if (isStreaming) {
|
|
62
|
+
return await stream(req, res);
|
|
63
|
+
}
|
|
64
|
+
return await nonStram(req, res);
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
exports.agentDispatcher = agentDispatcher;
|
|
68
|
+
// express middleware
|
|
69
|
+
// run agent
|
|
70
|
+
const nonStreamAgentDispatcher = (agentDictionary, agentFilters = []) => {
|
|
71
|
+
return async (req, res) => {
|
|
72
|
+
const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters);
|
|
83
73
|
const result = await dispatcher(req, res);
|
|
84
74
|
return res.json(result);
|
|
85
75
|
};
|
|
86
76
|
};
|
|
87
|
-
exports.
|
|
77
|
+
exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
|
|
88
78
|
// express middleware
|
|
89
79
|
// run agent with streaming
|
|
90
|
-
const streamAgentDispatcher = (agentFilters = []) => {
|
|
80
|
+
const streamAgentDispatcher = (agentDictionary, agentFilters = []) => {
|
|
91
81
|
return async (req, res) => {
|
|
92
82
|
res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
|
|
93
83
|
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
@@ -102,7 +92,7 @@ const streamAgentDispatcher = (agentFilters = []) => {
|
|
|
102
92
|
agent: (0, stream_1.streamAgentFilterGenerator)(callback),
|
|
103
93
|
};
|
|
104
94
|
const filterList = [...agentFilters, streamAgentFilter];
|
|
105
|
-
const dispatcher = agentDispatcherInternal(filterList);
|
|
95
|
+
const dispatcher = agentDispatcherInternal(agentDictionary, filterList);
|
|
106
96
|
const result = await dispatcher(req, res);
|
|
107
97
|
const json_data = JSON.stringify(result);
|
|
108
98
|
res.write("___END___");
|
|
@@ -112,7 +102,7 @@ const streamAgentDispatcher = (agentFilters = []) => {
|
|
|
112
102
|
};
|
|
113
103
|
exports.streamAgentDispatcher = streamAgentDispatcher;
|
|
114
104
|
// dispatcher internal function
|
|
115
|
-
const agentDispatcherInternal = (agentFilters = []) => {
|
|
105
|
+
const agentDispatcherInternal = (agentDictionary, agentFilters = []) => {
|
|
116
106
|
return async (req, res) => {
|
|
117
107
|
const { params } = req;
|
|
118
108
|
const { agentId } = params;
|
|
@@ -130,7 +120,7 @@ const agentDispatcherInternal = (agentFilters = []) => {
|
|
|
130
120
|
retry,
|
|
131
121
|
verbose: false,
|
|
132
122
|
},
|
|
133
|
-
agents,
|
|
123
|
+
agents: agentDictionary,
|
|
134
124
|
filterParams: {},
|
|
135
125
|
};
|
|
136
126
|
const agentFilterRunner = (0, graphai_1.agentFilterRunnerBuilder)(agentFilters);
|