@receptron/graphai_express 0.0.27 → 0.0.29

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 CHANGED
@@ -13,27 +13,38 @@ yarn add @receptron/graphai_express
13
13
 
14
14
  import express from "express";
15
15
  import * as agents from "@graphai/agents";
16
- import { agentDispatcher, nonStreamAgentDispatcher, streamAgentDispatcher, agentsList, agentDoc } from "@receptron/graphai_express";
16
+ import { agentDispatcher, nonStreamAgentDispatcher, streamAgentDispatcher, agentsList, agentDoc, graphRunner} from "@receptron/graphai_express";
17
17
  import { AgentFunctionInfoDictionary } from "graphai";
18
18
 
19
19
  const agentDictionary: AgentFunctionInfoDictionary = agents;
20
20
  const hostName = "https://example.net";
21
- const apiPrefix = "/api/agents";
21
+ const apiAgentPrefix = "/api/agents";
22
+ const apiGraphPrefix = "/api/graph";
22
23
 
23
- app.get(apiPrefix + "/:agentId", agentDoc(agentDictionary, hostName, apiPrefix)); // each API(agent) document
24
- app.get(apiPrefix + "/", agentsList(agentDictionary, hostName, apiPrefix)); // API(agent) list
24
+ app.get(apiAgentPrefix + "/:agentId", agentDoc(agentDictionary, hostName, apiAgentPrefix)); // each API(agent) document
25
+ app.get(apiAgentPrefix + "/", agentsList(agentDictionary, hostName, apiAgentPrefix)); // API(agent) list
25
26
 
26
- // non stream and strwam
27
- app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
27
+ // non stream and stream agent server
28
+ app.post(apiAgentPrefix + "/:agentId", agentDispatcher(agentDictionary));
28
29
 
29
- // non stream
30
- app.post(apiPrefix + "/nonstream/:agentId", nonStreamAgentDispatcher(agentDictionary));
30
+ // non stream agent server
31
+ app.post(apiAgentPrefix + "/nonstream/:agentId", nonStreamAgentDispatcher(agentDictionary));
32
+
33
+ // stream agent server
34
+ app.post(apiAgentPrefix + "/stream/:agentId", streamAgentDispatcher(agentDictionary));
35
+
36
+ // non stream and stream agent server
37
+ app.post(apiGraphPrefix + "/", graphRunner(agentDictionary));
31
38
 
32
- // stream
33
- app.post(apiPrefix + "/stream/:agentId", streamAgentDispatcher(agentDictionary));
34
39
 
35
40
  ```
36
41
 
42
+ ### Run Test server
43
+
44
+ ```
45
+ yarn run server
46
+ ```
47
+
37
48
 
38
49
  ### Test from curl
39
50
 
@@ -43,8 +54,9 @@ curl -X POST -H "Content-Type: application/json" -d '{"params": {"message" : "he
43
54
 
44
55
  ### from GraphAI Client
45
56
 
46
- T.B.D.
47
-
57
+ ```
58
+ npx ts-node test/stream_graph.ts
59
+ ```
48
60
 
49
61
  ### for this middleware development
50
62
 
package/lib/agents.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import express from "express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
3
+ import { StreamChunkCallback } from "./type";
3
4
  export declare const agentsList: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
4
5
  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, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
6
- export declare const agentRunner: (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 agentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
7
+ export declare const agentRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
7
8
  export declare const nonStreamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
8
- export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
9
+ export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean, streamChunkCallback?: StreamChunkCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
package/lib/agents.js CHANGED
@@ -56,9 +56,9 @@ exports.agentDoc = agentDoc;
56
56
  // express middleware
57
57
  // dispatch and run agent
58
58
  // app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
59
- const agentDispatcher = (agentDictionary, agentFilters = []) => {
59
+ const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback) => {
60
60
  const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, true);
61
- const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, true);
61
+ const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, true, streamChunkCallback);
62
62
  return async (req, res, next) => {
63
63
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
64
64
  if (isStreaming) {
@@ -71,9 +71,9 @@ exports.agentDispatcher = agentDispatcher;
71
71
  // express middleware
72
72
  // run agent
73
73
  // app.post(agentPrefix, agentRunner(agentDictionary));
74
- const agentRunner = (agentDictionary, agentFilters = []) => {
74
+ const agentRunner = (agentDictionary, agentFilters = [], streamChunkCallback) => {
75
75
  const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, false);
76
- const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, false);
76
+ const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, false, streamChunkCallback);
77
77
  return async (req, res, next) => {
78
78
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
79
79
  if (isStreaming) {
@@ -100,7 +100,7 @@ const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch
100
100
  exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
101
101
  // express middleware
102
102
  // run agent with streaming
103
- const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true) => {
103
+ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, streamChunkCallback) => {
104
104
  return async (req, res, next) => {
105
105
  try {
106
106
  res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
@@ -108,7 +108,12 @@ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch =
108
108
  res.setHeader("X-Accel-Buffering", "no");
109
109
  const callback = (context, token) => {
110
110
  if (token) {
111
- res.write(token);
111
+ if (streamChunkCallback) {
112
+ res.write(streamChunkCallback(context, token));
113
+ }
114
+ else {
115
+ res.write(token);
116
+ }
112
117
  }
113
118
  };
114
119
  const streamAgentFilter = {
package/lib/graph.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import express from "express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
3
- export declare const graphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
4
- export declare const streamGraphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
3
+ import { StreamChunkCallback } from "./type";
4
+ export declare const graphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
5
+ export declare const streamGraphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
5
6
  export declare const nonStreamGraphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[]) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
package/lib/graph.js CHANGED
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nonStreamGraphRunner = exports.streamGraphRunner = exports.graphRunner = void 0;
4
4
  const graphai_1 = require("graphai");
5
5
  const agent_filters_1 = require("@graphai/agent_filters");
6
- const graphRunner = (agentDictionary, agentFilters = []) => {
7
- const stream = (0, exports.streamGraphRunner)(agentDictionary, agentFilters);
6
+ const graphRunner = (agentDictionary, agentFilters = [], streamChunkCallback) => {
7
+ const stream = (0, exports.streamGraphRunner)(agentDictionary, agentFilters, streamChunkCallback);
8
8
  const nonStream = (0, exports.nonStreamGraphRunner)(agentDictionary, agentFilters);
9
9
  return async (req, res, next) => {
10
10
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
@@ -15,20 +15,25 @@ const graphRunner = (agentDictionary, agentFilters = []) => {
15
15
  };
16
16
  };
17
17
  exports.graphRunner = graphRunner;
18
- const streamGraphRunner = (agentDictionary, agentFilters = []) => {
18
+ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallback) => {
19
19
  return async (req, res, next) => {
20
20
  try {
21
21
  res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
22
22
  res.setHeader("Cache-Control", "no-cache, no-transform");
23
23
  res.setHeader("X-Accel-Buffering", "no");
24
- const callback = (context, token) => {
24
+ const streamCallback = (context, token) => {
25
25
  if (token) {
26
- res.write(token);
26
+ if (streamChunkCallback) {
27
+ res.write(streamChunkCallback(context, token));
28
+ }
29
+ else {
30
+ res.write(token);
31
+ }
27
32
  }
28
33
  };
29
34
  const streamAgentFilter = {
30
35
  name: "streamAgentFilter",
31
- agent: (0, agent_filters_1.streamAgentFilterGenerator)(callback),
36
+ agent: (0, agent_filters_1.streamAgentFilterGenerator)(streamCallback),
32
37
  };
33
38
  const filterList = [...agentFilters, streamAgentFilter];
34
39
  const dispatcher = streamGraphRunnerInternal(agentDictionary, filterList);
@@ -59,9 +64,10 @@ const nonStreamGraphRunner = (agentDictionary, agentFilters = []) => {
59
64
  exports.nonStreamGraphRunner = nonStreamGraphRunner;
60
65
  // internal function
61
66
  const streamGraphRunnerInternal = (agentDictionary, agentFilters = []) => {
62
- return async (req, __res) => {
67
+ return async (req, res) => {
63
68
  const { graphData } = req.body;
64
- const graphai = new graphai_1.GraphAI(graphData, agentDictionary, { agentFilters });
69
+ const { config } = req;
70
+ const graphai = new graphai_1.GraphAI(graphData, agentDictionary, { agentFilters, config: config ?? {} });
65
71
  const result = await graphai.run(true);
66
72
  return result;
67
73
  };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { agentsList, agentDoc, agentDispatcher, agentRunner, nonStreamAgentDispatcher, streamAgentDispatcher } from "./agents";
2
2
  export { graphRunner, streamGraphRunner, nonStreamGraphRunner } from "./graph";
3
- export { ExpressAgentInfo } from "./type";
3
+ export { ExpressAgentInfo, StreamChunkCallback } from "./type";
package/lib/type.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AgentFunctionInfoSample } from "graphai";
1
+ import type { AgentFunctionInfoSample, AgentFunctionContext } from "graphai";
2
2
  export type ExpressAgentInfo = {
3
3
  agentId: string;
4
4
  name: string;
@@ -13,3 +13,4 @@ export type ExpressAgentInfo = {
13
13
  output: any;
14
14
  stream: boolean;
15
15
  };
16
+ export type StreamChunkCallback = (context: AgentFunctionContext, token: string) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -30,8 +30,12 @@
30
30
  "@graphai/agents": "^0.0.21",
31
31
  "@types/express": "^4.17.21",
32
32
  "@types/node": "^20.14.12",
33
+ "@types/sinon": "^17.0.3",
34
+ "@types/sinon-express-mock": "^1.3.12",
33
35
  "eslint": "^9.7.0",
34
36
  "prettier": "^3.3.3",
37
+ "sinon": "^18.0.0",
38
+ "sinon-express-mock": "^2.2.1",
35
39
  "ts-node": "^10.9.2",
36
40
  "tsc-alias": "^1.8.8",
37
41
  "tsconfig-paths": "^4.2.0",