@receptron/graphai_express 1.0.2 → 2.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/README.md CHANGED
@@ -36,6 +36,8 @@ app.post(apiAgentPrefix + "/stream/:agentId", streamAgentDispatcher(agentDiction
36
36
  // non stream and stream agent server
37
37
  app.post(apiGraphPrefix + "/", graphRunner(agentDictionary));
38
38
 
39
+ // OpenAI completions Compatible API
40
+ app.post("/api/chat/completions", completionRunner(agentDictionary, model2graphData, [], onLogCallback));
39
41
 
40
42
  ```
41
43
 
package/lib/agents.d.ts CHANGED
@@ -4,7 +4,7 @@ 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, contentCallback?: ContentCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
8
+ export declare const agentRunner: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], streamChunkCallback?: StreamChunkCallback, contentCallback?: ContentCallback) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
9
+ export declare const nonStreamAgentDispatcher: (agentDictionary: AgentFunctionInfoDictionary, agentFilters?: AgentFilterInfo[], isDispatch?: boolean) => (req: express.Request, res: express.Response, next: express.NextFunction) => Promise<void>;
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<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;
@@ -98,7 +99,7 @@ const nonStreamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch
98
99
  try {
99
100
  const dispatcher = agentDispatcherInternal(agentDictionary, agentFilters, isDispatch);
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);
@@ -129,7 +130,7 @@ 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
136
  const dispatcher = agentDispatcherInternal(agentDictionary, filterList, isDispatch);
@@ -138,7 +139,7 @@ const streamAgentDispatcher = (agentDictionary, agentFilters = [], isDispatch =
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);
@@ -171,7 +172,7 @@ const agentDispatcherInternal = (agentDictionary, agentFilters = [], isDispatch
171
172
  const { agents: __nonLog, ...logContext } = context;
172
173
  console.log("agentDispatcherInternal(context): ", logContext);
173
174
  }
174
- const agentFilterRunner = (0, agent_filters_1.agentFilterRunnerBuilder)(agentFilters);
175
+ const agentFilterRunner = (0, agent_filter_utils_1.agentFilterRunnerBuilder)(agentFilters);
175
176
  const result = await agentFilterRunner(context, agent.agent);
176
177
  return result;
177
178
  };
@@ -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/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { agentsList, agentDoc, agentDispatcher, agentRunner, nonStreamAgentDispatcher, streamAgentDispatcher, updateAgentVerbose } from "./agents";
2
2
  export { graphRunner, streamGraphRunner, nonStreamGraphRunner } from "./graph";
3
3
  export { completionRunner } from "./completions";
4
- export { ExpressAgentInfo, StreamChunkCallback, ContentCallback, Model2GraphData } from "./type";
4
+ export { modelList } from "./models";
5
+ export { ExpressAgentInfo, StreamChunkCallback, StreamCompletionChunkCallback, Model2GraphData, GraphDictionary, ContentCallback, DefaultEndOfStreamDelimiter, } from "./type";
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.completionRunner = exports.nonStreamGraphRunner = exports.streamGraphRunner = exports.graphRunner = exports.updateAgentVerbose = exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentRunner = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
3
+ exports.DefaultEndOfStreamDelimiter = exports.modelList = exports.completionRunner = exports.nonStreamGraphRunner = exports.streamGraphRunner = exports.graphRunner = exports.updateAgentVerbose = exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentRunner = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
4
4
  var agents_1 = require("./agents");
5
5
  Object.defineProperty(exports, "agentsList", { enumerable: true, get: function () { return agents_1.agentsList; } });
6
6
  Object.defineProperty(exports, "agentDoc", { enumerable: true, get: function () { return agents_1.agentDoc; } });
@@ -15,3 +15,7 @@ Object.defineProperty(exports, "streamGraphRunner", { enumerable: true, get: fun
15
15
  Object.defineProperty(exports, "nonStreamGraphRunner", { enumerable: true, get: function () { return graph_1.nonStreamGraphRunner; } });
16
16
  var completions_1 = require("./completions");
17
17
  Object.defineProperty(exports, "completionRunner", { enumerable: true, get: function () { return completions_1.completionRunner; } });
18
+ var models_1 = require("./models");
19
+ Object.defineProperty(exports, "modelList", { enumerable: true, get: function () { return models_1.modelList; } });
20
+ var type_1 = require("./type");
21
+ Object.defineProperty(exports, "DefaultEndOfStreamDelimiter", { enumerable: true, get: function () { return type_1.DefaultEndOfStreamDelimiter; } });
@@ -0,0 +1,3 @@
1
+ import express from "express";
2
+ import type { GraphDictionary } from "./type";
3
+ export declare const modelList: (modelDictionary: GraphDictionary) => (req: express.Request, res: express.Response) => Promise<void>;
package/lib/models.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.modelList = void 0;
4
+ const modelList = (modelDictionary) => {
5
+ return async (req, res) => {
6
+ const data = Object.keys(modelDictionary).map((id) => {
7
+ // const model = modelDictionary[id];
8
+ return {
9
+ id,
10
+ object: "model",
11
+ created: 1686935002,
12
+ owned_by: "graphai",
13
+ };
14
+ });
15
+ res.json({ object: "list", data });
16
+ };
17
+ };
18
+ exports.modelList = modelList;
package/lib/type.d.ts CHANGED
@@ -21,6 +21,7 @@ type BaseData = {
21
21
  export type StreamChunkCallback = <T = string | Record<string, string>>(context: AgentFunctionContext, token: T) => void;
22
22
  export type StreamCompletionChunkCallback = <T = string | Record<string, string>>(data: BaseData, status: string, token?: T) => void;
23
23
  export type Model2GraphData = (model: string) => GraphData;
24
+ export type GraphDictionary = Record<string, GraphData>;
24
25
  export type ContentCallback = <T = string | Record<string, string>>(token: T) => void;
25
26
  export declare const DefaultEndOfStreamDelimiter = "___END___";
26
27
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -27,15 +27,17 @@
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-serve-static-core": "^5.0.6",
32
+ "sinon": "^20.0.0",
32
33
  "sinon-express-mock": "^2.2.1"
33
34
  },
34
35
  "dependencies": {
35
- "@graphai/agent_filters": "^1.0.0",
36
+ "@graphai/agent_filter_utils": "^2.0.0",
37
+ "@graphai/stream_agent_filter": "^2.0.1",
36
38
  "cors": "^2.8.5",
37
- "dotenv": "^16.4.7",
38
- "express": "^4.21.2"
39
+ "dotenv": "^16.5.0",
40
+ "express": "^5.1.0"
39
41
  },
40
42
  "types": "./lib/index.d.ts",
41
43
  "directories": {