@receptron/graphai_express 1.0.3 → 3.0.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,10 +1,10 @@
1
1
  import express from "express";
2
- import type { AgentFunctionInfoDictionary, AgentFilterInfo } from "graphai";
2
+ import type { AgentFunctionInfoDictionary, AgentFilterInfo, ConfigDataDictionary } from "graphai";
3
3
  import { StreamChunkCallback, ContentCallback } from "./type";
4
4
  export declare const updateAgentVerbose: (val: boolean) => void;
5
5
  export declare const agentsList: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
6
6
  export declare const agentDoc: (agentDictionary: AgentFunctionInfoDictionary, hostName?: string, urlPath?: string) => (req: express.Request, res: express.Response) => Promise<void>;
7
- 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>;
8
- 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>;
9
- 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>;
10
- 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>;
7
+ export declare const agentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback, config?: ConfigDataDictionary, contentCallback?: ContentCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
8
+ export declare const agentRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback, config?: ConfigDataDictionary, contentCallback?: ContentCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
9
+ export declare const nonStreamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean, config?: ConfigDataDictionary) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
10
+ export declare const streamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean, streamChunkCallback?: StreamChunkCallback, config?: ConfigDataDictionary, contentCallback?: ContentCallback, endOfStreamDelimiter?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
package/lib/agents.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentRunner = exports.agentDispatcher = exports.agentDoc = exports.agentsList = exports.updateAgentVerbose = void 0;
4
- const agent_filters_1 = require("@graphai/agent_filters");
4
+ const stream_agent_filter_1 = require("@graphai/stream_agent_filter");
5
+ const agent_filter_utils_1 = require("@graphai/agent_filter_utils");
5
6
  const type_1 = require("./type");
6
7
  const utils_1 = require("./utils");
7
8
  let graphaiExpressVerbose = false;
@@ -60,9 +61,9 @@ const agentDoc = (agentDictionary, hostName = "https://example.com", urlPath = "
60
61
  };
61
62
  };
62
63
  exports.agentDoc = agentDoc;
63
- const __agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, isDispatch = true) => {
64
- const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, isDispatch);
65
- const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, isDispatch, streamChunkCallback, contentCallback);
64
+ const __agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, isDispatch = true, config = {}) => {
65
+ const nonStram = (0, exports.nonStreamAgentDispatcher)(agentDictionary, agentFilters, isDispatch, config);
66
+ const stream = (0, exports.streamAgentDispatcher)(agentDictionary, agentFilters, isDispatch, streamChunkCallback, config, contentCallback, type_1.DefaultEndOfStreamDelimiter);
66
67
  return async (req, res, next) => {
67
68
  const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream");
68
69
  if (graphaiExpressVerbose) {
@@ -77,28 +78,28 @@ const __agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallba
77
78
  // express middleware
78
79
  // dispatch and run agent
79
80
  // app.post(apiPrefix + "/:agentId", agentDispatcher(agentDictionary));
80
- const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback) => {
81
- return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, true);
81
+ const agentDispatcher = (agentDictionary, agentFilters = [], streamChunkCallback, config = {}, contentCallback = utils_1.defaultContentCallback) => {
82
+ return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, true, config);
82
83
  };
83
84
  exports.agentDispatcher = agentDispatcher;
84
85
  // express middleware
85
86
  // run agent
86
87
  // app.post(agentPrefix, agentRunner(agentDictionary));
87
- const agentRunner = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback) => {
88
- return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, false);
88
+ const agentRunner = (agentDictionary, agentFilters = [], streamChunkCallback, config = {}, contentCallback = utils_1.defaultContentCallback) => {
89
+ return __agentDispatcher(agentDictionary, agentFilters, streamChunkCallback, contentCallback, false, config);
89
90
  };
90
91
  exports.agentRunner = agentRunner;
91
92
  // express middleware
92
93
  // run agent
93
- const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true) => {
94
+ const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, config = {}) => {
94
95
  return async (req, res, next) => {
95
96
  if (graphaiExpressVerbose) {
96
97
  console.log("nonStreamAgentDispatcher");
97
98
  }
98
99
  try {
99
- const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters, isDispatch);
100
+ const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters, isDispatch, config);
100
101
  const result = await dispatcher(req, res);
101
- return res.json(result);
102
+ res.json(result);
102
103
  }
103
104
  catch (e) {
104
105
  next(e);
@@ -108,7 +109,7 @@ const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch
108
109
  exports.nonStreamAgentDispatcher = nonStreamAgentDispatcher;
109
110
  // express middleware
110
111
  // run agent with streaming
111
- const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, streamChunkCallback, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter) => {
112
+ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch = true, streamChunkCallback, config = {}, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter) => {
112
113
  return async (req, res, next) => {
113
114
  if (graphaiExpressVerbose) {
114
115
  console.log("streamAgentDispatcher");
@@ -129,16 +130,16 @@ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch =
129
130
  };
130
131
  const streamAgentFilter = {
131
132
  name: "streamAgentFilter",
132
- agent: (0, agent_filters_1.streamAgentFilterGenerator)(callback),
133
+ agent: (0, stream_agent_filter_1.streamAgentFilterGenerator)(callback),
133
134
  };
134
135
  const filterList = [...agentFilters, streamAgentFilter];
135
- const dispatcher = agentDispatcherInternal(agentDictionary, filterList, isDispatch);
136
+ const dispatcher = agentDispatcherInternal(agentDictionary, filterList, isDispatch, config);
136
137
  const result = await dispatcher(req, res);
137
138
  if (endOfStreamDelimiter !== "") {
138
139
  res.write(endOfStreamDelimiter);
139
140
  }
140
141
  res.write(contentCallback(result));
141
- return res.end();
142
+ res.end();
142
143
  }
143
144
  catch (e) {
144
145
  next(e);
@@ -146,8 +147,17 @@ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch =
146
147
  };
147
148
  };
148
149
  exports.streamAgentDispatcher = streamAgentDispatcher;
150
+ const getConfig = (config, agentId) => {
151
+ if (agentId) {
152
+ return {
153
+ ...(config["global"] ?? {}),
154
+ ...(config[agentId] ?? {}),
155
+ };
156
+ }
157
+ return {};
158
+ };
149
159
  // dispatcher internal function
150
- const agentDispatcherInternal = (agentDictionary, agentFilters = [], isDispatch = true) => {
160
+ const agentDispatcherInternal = (agentDictionary, agentFilters = [], isDispatch = true, config = {}) => {
151
161
  return async (req, res) => {
152
162
  if (graphaiExpressVerbose) {
153
163
  console.log("agentDispatcherInternal");
@@ -166,12 +176,13 @@ const agentDispatcherInternal = (agentDictionary, agentFilters = [], isDispatch
166
176
  debugInfo,
167
177
  agents: agentDictionary,
168
178
  filterParams,
179
+ config: getConfig(config, agentId),
169
180
  };
170
181
  if (graphaiExpressVerbose) {
171
182
  const { agents: __nonLog, ...logContext } = context;
172
183
  console.log("agentDispatcherInternal(context): ", logContext);
173
184
  }
174
- const agentFilterRunner = (0, agent_filters_1.agentFilterRunnerBuilder)(agentFilters);
185
+ const agentFilterRunner = (0, agent_filter_utils_1.agentFilterRunnerBuilder)(agentFilters);
175
186
  const result = await agentFilterRunner(context, agent.agent);
176
187
  return result;
177
188
  };
@@ -1,4 +1,4 @@
1
1
  import express from "express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo, TransactionLog } from "graphai";
3
3
  import type { Model2GraphData } from "./type";
4
- export declare const completionRunner: (agentDictionary: AgentFunctionInfoDictionary, model2GraphData: Model2GraphData, 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>;
4
+ export declare const completionRunner: (agentDictionary: AgentFunctionInfoDictionary, model2GraphData: Model2GraphData, agentFilters?: AgentFilterInfo[], onLogCallback?: (__log: TransactionLog, __isUpdate: boolean) => void) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.completionRunner = void 0;
4
4
  const graphai_1 = require("graphai");
5
- const agent_filters_1 = require("@graphai/agent_filters");
5
+ const stream_agent_filter_1 = require("@graphai/stream_agent_filter");
6
6
  const crypto_1 = require("crypto");
7
7
  // TODO choise graph(done)
8
8
  // stream flag(done);
@@ -38,7 +38,7 @@ const completionRunner = (agentDictionary, model2GraphData, agentFilters = [], o
38
38
  const { stream, model, messages } = req.body;
39
39
  // validation
40
40
  if (!model || typeof model !== "string") {
41
- return res.status(400).json({
41
+ res.status(400).json({
42
42
  error: {
43
43
  message: "`model` is required and must be a string",
44
44
  type: "invalid_request_error",
@@ -46,9 +46,10 @@ const completionRunner = (agentDictionary, model2GraphData, agentFilters = [], o
46
46
  code: "invalid_model",
47
47
  },
48
48
  });
49
+ return;
49
50
  }
50
51
  if (!messages || !Array.isArray(messages) || messages.length === 0) {
51
- return res.status(400).json({
52
+ res.status(400).json({
52
53
  error: {
53
54
  message: "`messages` must be an array of objects with `role` and `content` as strings",
54
55
  type: "invalid_request_error",
@@ -56,6 +57,7 @@ const completionRunner = (agentDictionary, model2GraphData, agentFilters = [], o
56
57
  code: "invalid_messages",
57
58
  },
58
59
  });
60
+ return;
59
61
  }
60
62
  // const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream")
61
63
  if (stream) {
@@ -84,7 +86,7 @@ const streamGraphRunner = (agentDictionary, model2GraphData, agentFilters = [],
84
86
  };
85
87
  const streamAgentFilter = {
86
88
  name: "streamAgentFilter",
87
- agent: (0, agent_filters_1.streamAgentFilterGenerator)(streamCallback),
89
+ agent: (0, stream_agent_filter_1.streamAgentFilterGenerator)(streamCallback),
88
90
  };
89
91
  const filterList = [...agentFilters, streamAgentFilter];
90
92
  res.write(streamCompletionChunkCallback(baseData, "start"));
@@ -97,7 +99,7 @@ const streamGraphRunner = (agentDictionary, model2GraphData, agentFilters = [],
97
99
  catch (__err) {
98
100
  res.write(`data: ${JSON.stringify({ error: "GraphAI Something went wrong" })}\n\n`);
99
101
  }
100
- return res.end();
102
+ res.end();
101
103
  }
102
104
  catch (e) {
103
105
  next(e);
@@ -109,7 +111,7 @@ const nonStreamGraphRunner = (agentDictionary, model2GraphData, agentFilters = [
109
111
  try {
110
112
  const dispatcher = streamGraphRunnerInternal(agentDictionary, model2GraphData, agentFilters, onLogCallback);
111
113
  const result = await dispatcher(req);
112
- return res.json(result);
114
+ res.json(result);
113
115
  }
114
116
  catch (e) {
115
117
  next(e);
package/lib/graph.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import express from "express";
2
2
  import type { AgentFunctionInfoDictionary, AgentFilterInfo, TransactionLog } from "graphai";
3
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>;
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<void>;
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<void>;
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<void>;
package/lib/graph.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nonStreamGraphRunner = exports.streamGraphRunner = exports.graphRunner = void 0;
4
4
  const graphai_1 = require("graphai");
5
- const agent_filters_1 = require("@graphai/agent_filters");
5
+ const stream_agent_filter_1 = require("@graphai/stream_agent_filter");
6
6
  const type_1 = require("./type");
7
7
  const utils_1 = require("./utils");
8
8
  const graphRunner = (agentDictionary, agentFilters = [], streamChunkCallback, contentCallback = utils_1.defaultContentCallback, endOfStreamDelimiter = type_1.DefaultEndOfStreamDelimiter, onLogCallback = (__log, __isUpdate) => { }) => {
@@ -35,7 +35,7 @@ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallba
35
35
  };
36
36
  const streamAgentFilter = {
37
37
  name: "streamAgentFilter",
38
- agent: (0, agent_filters_1.streamAgentFilterGenerator)(streamCallback),
38
+ agent: (0, stream_agent_filter_1.streamAgentFilterGenerator)(streamCallback),
39
39
  };
40
40
  const filterList = [...agentFilters, streamAgentFilter];
41
41
  const dispatcher = streamGraphRunnerInternal(agentDictionary, filterList, onLogCallback);
@@ -44,7 +44,7 @@ const streamGraphRunner = (agentDictionary, agentFilters = [], streamChunkCallba
44
44
  res.write(endOfStreamDelimiter);
45
45
  }
46
46
  res.write(contentCallback(result));
47
- return res.end();
47
+ res.end();
48
48
  }
49
49
  catch (e) {
50
50
  next(e);
@@ -57,7 +57,7 @@ const nonStreamGraphRunner = (agentDictionary, agentFilters = [], onLogCallback
57
57
  try {
58
58
  const dispatcher = streamGraphRunnerInternal(agentDictionary, agentFilters, onLogCallback);
59
59
  const result = await dispatcher(req);
60
- return res.json(result);
60
+ res.json(result);
61
61
  }
62
62
  catch (e) {
63
63
  next(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "1.0.3",
3
+ "version": "3.0.0",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -27,15 +27,21 @@
27
27
  },
28
28
  "homepage": "https://github.com/receptron/graphai_utils/tree/main/packages/express#readme",
29
29
  "devDependencies": {
30
- "@types/cors": "^2.8.17",
31
- "sinon": "^19.0.2",
30
+ "@types/cors": "^2.8.18",
31
+ "@types/express": "^5.0.3",
32
+ "@types/express-serve-static-core": "^5.0.6",
33
+ "@types/node": "^24.0.3",
34
+ "eslint": "^9.29.0",
35
+ "graphai": "^2.0.6",
36
+ "sinon": "^20.0.0",
32
37
  "sinon-express-mock": "^2.2.1"
33
38
  },
34
39
  "dependencies": {
35
- "@graphai/agent_filters": "^1.0.0",
40
+ "@graphai/agent_filter_utils": "^2.0.0",
41
+ "@graphai/stream_agent_filter": "^2.0.2",
36
42
  "cors": "^2.8.5",
37
- "dotenv": "^16.4.7",
38
- "express": "^4.21.2"
43
+ "dotenv": "^16.5.0",
44
+ "express": "^5.1.0"
39
45
  },
40
46
  "types": "./lib/index.d.ts",
41
47
  "directories": {