@receptron/graphai_express 0.0.32 → 0.1.0

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/agents.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import express from "express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
3
- import { StreamChunkCallback } from "./type";
3
+ import { StreamChunkCallback, ContentCallback } from "./type";
4
4
  export declare const agentsList: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
5
5
  export declare const agentDoc: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
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>;
6
+ export declare const agentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback, contentCallback?: ContentCallback) => (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, contentCallback?: ContentCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
8
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>;
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>;
9
+ export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean, streamChunkCallback?: StreamChunkCallback, contentCallback?: ContentCallback, endOfStreamDelimiter?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
package/lib/agents.js CHANGED
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentRunner = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
4
4
  const agent_filters_1 = require("@graphai/agent_filters");
5
+ const type_1 = require("./type");
6
+ const utils_1 = require("./utils");
5
7
  // express middleware
6
8
  // return agent list
7
9
  const agentsList = (agentDictionary, hostName = "https://example.com", urlPath = "/agent") => {
@@ -53,12 +55,9 @@ const agentDoc = (agentDictionary, hostName = "https://example.com", urlPath = "
53
55
  };
54
56
  };
55
57
  exports.agentDoc = agentDoc;
56
- // express middleware
57
- // dispatch and run agent
58
- // app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
59
- const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback) => {
60
- const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, true);
61
- const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, true, streamChunkCallback);
58
+ const __agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, isDispatch = true) => {
59
+ const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, isDispatch);
60
+ const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, isDispatch, streamChunkCallback, contentCallback);
62
61
  return async (req, res, next) => {
63
62
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
64
63
  if (isStreaming) {
@@ -67,20 +66,18 @@ const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback
67
66
  return await nonStram(req, res, next);
68
67
  };
69
68
  };
69
+ // express middleware
70
+ // dispatch and run agent
71
+ // app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
72
+ const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback) => {
73
+ return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, true);
74
+ };
70
75
  exports.agentDispatcher = agentDispatcher;
71
76
  // express middleware
72
77
  // run agent
73
78
  // app.post(agentPrefix, agentRunner(agentDictionary));
74
- const agentRunner = (agentDictionary, agentFilters = [], streamChunkCallback) => {
75
- const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, false);
76
- const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, false, streamChunkCallback);
77
- return async (req, res, next) => {
78
- const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
79
- if (isStreaming) {
80
- return await stream(req, res, next);
81
- }
82
- return await nonStram(req, res, next);
83
- };
79
+ const agentRunner = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback) => {
80
+ return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, false);
84
81
  };
85
82
  exports.agentRunner = agentRunner;
86
83
  // express middleware
@@ -100,7 +97,7 @@ const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch
100
97
  exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
101
98
  // express middleware
102
99
  // run agent with streaming
103
- const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, streamChunkCallback) => {
100
+ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, streamChunkCallback, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter) => {
104
101
  return async (req, res, next) => {
105
102
  try {
106
103
  res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
@@ -123,9 +120,10 @@ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch =
123
120
  const filterList = [...agentFilters, streamAgentFilter];
124
121
  const dispatcher = agentDispatcherInternal(agentDictionary, filterList, isDispatch);
125
122
  const result = await dispatcher(req, res);
126
- const json_data = JSON.stringify(result);
127
- res.write("___END___");
128
- res.write(json_data);
123
+ if (endOfStreamDelimiter !== "") {
124
+ res.write(endOfStreamDelimiter);
125
+ }
126
+ res.write(contentCallback(result));
129
127
  return res.end();
130
128
  }
131
129
  catch (e) {
package/lib/graph.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import express from "express";
2
- import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
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>;
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>;
2
+ import type { AgentFunctionInfoDictionary, AgentFilterInfo, TransactionLog } from "graphai";
3
+ import type { StreamChunkCallback, ContentCallback } from "./type";
4
+ export declare const graphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback, contentCallback?: ContentCallback, endOfStreamDelimiter?: string, onLogCallback?: (__log: TransactionLog, __isUpdate: boolean) => void) => (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, contentCallback?: ContentCallback, endOfStreamDelimiter?: string, onLogCallback?: (__log: TransactionLog, __isUpdate: boolean) => void) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
6
+ export declare const nonStreamGraphRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], onLogCallback?: (__log: TransactionLog, __isUpdate: boolean) => void) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<express.Response<any, Record<string, any>> | undefined>;
package/lib/graph.js CHANGED
@@ -3,9 +3,11 @@ 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 = [], streamChunkCallback) => {
7
- const stream = (0, exports.streamGraphRunner)(agentDictionary, agentFilters, streamChunkCallback);
8
- const nonStream = (0, exports.nonStreamGraphRunner)(agentDictionary, agentFilters);
6
+ const type_1 = require("./type");
7
+ const utils_1 = require("./utils");
8
+ const graphRunner = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter, onLogCallback = (__log, __isUpdate) => { }) => {
9
+ const stream = (0, exports.streamGraphRunner)(agentDictionary, agentFilters, streamChunkCallback, contentCallback, endOfStreamDelimiter, onLogCallback);
10
+ const nonStream = (0, exports.nonStreamGraphRunner)(agentDictionary, agentFilters, onLogCallback);
9
11
  return async (req, res, next) => {
10
12
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
11
13
  if (isStreaming) {
@@ -15,7 +17,7 @@ const graphRunner = (agentDictionary, agentFilters = [], streamChunkCallback) =>
15
17
  };
16
18
  };
17
19
  exports.graphRunner = graphRunner;
18
- const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallback) => {
20
+ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter, onLogCallback = (__log, __isUpdate) => { }) => {
19
21
  return async (req, res, next) => {
20
22
  try {
21
23
  res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
@@ -36,11 +38,12 @@ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallba
36
38
  agent: (0, agent_filters_1.streamAgentFilterGenerator)(streamCallback),
37
39
  };
38
40
  const filterList = [...agentFilters, streamAgentFilter];
39
- const dispatcher = streamGraphRunnerInternal(agentDictionary, filterList);
41
+ const dispatcher = streamGraphRunnerInternal(agentDictionary, filterList, onLogCallback);
40
42
  const result = await dispatcher(req);
41
- const json_data = JSON.stringify(result);
42
- res.write("___END___");
43
- res.write(json_data);
43
+ if (endOfStreamDelimiter !== "") {
44
+ res.write(endOfStreamDelimiter);
45
+ }
46
+ res.write(contentCallback(result));
44
47
  return res.end();
45
48
  }
46
49
  catch (e) {
@@ -49,10 +52,10 @@ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallba
49
52
  };
50
53
  };
51
54
  exports.streamGraphRunner = streamGraphRunner;
52
- const nonStreamGraphRunner = (agentDictionary, agentFilters = []) => {
55
+ const nonStreamGraphRunner = (agentDictionary, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
53
56
  return async (req, res, next) => {
54
57
  try {
55
- const dispatcher = streamGraphRunnerInternal(agentDictionary, agentFilters);
58
+ const dispatcher = streamGraphRunnerInternal(agentDictionary, agentFilters, onLogCallback);
56
59
  const result = await dispatcher(req);
57
60
  return res.json(result);
58
61
  }
@@ -63,11 +66,12 @@ const nonStreamGraphRunner = (agentDictionary, agentFilters = []) => {
63
66
  };
64
67
  exports.nonStreamGraphRunner = nonStreamGraphRunner;
65
68
  // internal function
66
- const streamGraphRunnerInternal = (agentDictionary, agentFilters = []) => {
69
+ const streamGraphRunnerInternal = (agentDictionary, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
67
70
  return async (req) => {
68
71
  const { graphData } = req.body;
69
72
  const { config } = req;
70
73
  const graphai = new graphai_1.GraphAI(graphData, agentDictionary, { agentFilters, config: config ?? {} });
74
+ graphai.onLogCallback = onLogCallback;
71
75
  const result = await graphai.run();
72
76
  return result;
73
77
  };
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, StreamChunkCallback } from "./type";
3
+ export { ExpressAgentInfo, StreamChunkCallback, ContentCallback } from "./type";
package/lib/type.d.ts CHANGED
@@ -13,4 +13,6 @@ export type ExpressAgentInfo = {
13
13
  output: any;
14
14
  stream: boolean;
15
15
  };
16
- export type StreamChunkCallback = (context: AgentFunctionContext, token: string | Record<string, string>) => void;
16
+ export type StreamChunkCallback = <T = string | Record<string, string>>(context: AgentFunctionContext, token: T) => void;
17
+ export type ContentCallback = <T = string | Record<string, string>>(token: T) => void;
18
+ export declare const DefaultEndOfStreamDelimiter = "___END___";
package/lib/type.js CHANGED
@@ -1,2 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultEndOfStreamDelimiter = void 0;
4
+ exports.DefaultEndOfStreamDelimiter = "___END___";
package/lib/utils.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { ContentCallback } from "./type";
2
+ export declare const defaultContentCallback: ContentCallback;
package/lib/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultContentCallback = void 0;
4
+ const defaultContentCallback = (result) => {
5
+ return JSON.stringify(result);
6
+ };
7
+ exports.defaultContentCallback = defaultContentCallback;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "0.0.32",
3
+ "version": "0.1.0",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -31,10 +31,10 @@
31
31
  "sinon-express-mock": "^2.2.1"
32
32
  },
33
33
  "dependencies": {
34
- "@graphai/agent_filters": "^0.0.6",
34
+ "@graphai/agent_filters": "^0.0.8",
35
35
  "dotenv": "^16.4.5",
36
- "express": "^4.21.0",
37
- "graphai": "^0.5.8"
36
+ "express": "^4.21.1",
37
+ "graphai": "^0.6.2"
38
38
  },
39
39
  "types": "./lib/index.d.ts",
40
40
  "directories": {