@receptron/graphai_express 1.0.0 → 1.0.2

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.js CHANGED
@@ -168,7 +168,8 @@ const agentDispatcherInternal = (agentDictionary, agentFilters = [], isDispatch
168
168
  filterParams,
169
169
  };
170
170
  if (graphaiExpressVerbose) {
171
- console.log("agentDispatcherInternal(context): ", context);
171
+ const { agents: __nonLog, ...logContext } = context;
172
+ console.log("agentDispatcherInternal(context): ", logContext);
172
173
  }
173
174
  const agentFilterRunner = (0, agent_filters_1.agentFilterRunnerBuilder)(agentFilters);
174
175
  const result = await agentFilterRunner(context, agent.agent);
@@ -0,0 +1,4 @@
1
+ import express from "express";
2
+ import type { AgentFunctionInfoDictionary, AgentFilterInfo, TransactionLog } from "graphai";
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>;
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.completionRunner = void 0;
4
+ const graphai_1 = require("graphai");
5
+ const agent_filters_1 = require("@graphai/agent_filters");
6
+ const crypto_1 = require("crypto");
7
+ // TODO choise graph(done)
8
+ // stream flag(done);
9
+ // non stream api(done);
10
+ const streamCompletionChunkCallback = (data, status, token) => {
11
+ if (status === "done") {
12
+ return "data: [DONE]\n\n";
13
+ }
14
+ const payload = (() => {
15
+ if (status === "start") {
16
+ return {
17
+ object: "chat.completion.chunk",
18
+ choices: [{ index: 0, delta: { role: "assistant", content: "" }, logprobs: null, finish_reason: null }],
19
+ };
20
+ }
21
+ if (status === "end") {
22
+ return {
23
+ object: "chat.completion.chunk",
24
+ choices: [{ index: 0, delta: {}, logprobs: null, finish_reason: "stop" }],
25
+ };
26
+ }
27
+ return {
28
+ object: "chat.completion.chunk",
29
+ choices: [{ index: 0, delta: { content: token }, logprobs: null, finish_reason: null }],
30
+ };
31
+ })();
32
+ return "data: " + JSON.stringify({ ...data, ...payload }) + "\n\n";
33
+ };
34
+ const completionRunner = (agentDictionary, model2GraphData, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
35
+ const streamRunner = streamGraphRunner(agentDictionary, model2GraphData, agentFilters, onLogCallback);
36
+ const nonStreamRunner = nonStreamGraphRunner(agentDictionary, model2GraphData, agentFilters, onLogCallback);
37
+ return async (req, res, next) => {
38
+ const { stream, model, messages } = req.body;
39
+ // validation
40
+ if (!model || typeof model !== "string") {
41
+ return res.status(400).json({
42
+ error: {
43
+ message: "`model` is required and must be a string",
44
+ type: "invalid_request_error",
45
+ param: "model",
46
+ code: "invalid_model",
47
+ },
48
+ });
49
+ }
50
+ if (!messages || !Array.isArray(messages) || messages.length === 0) {
51
+ return res.status(400).json({
52
+ error: {
53
+ message: "`messages` must be an array of objects with `role` and `content` as strings",
54
+ type: "invalid_request_error",
55
+ param: "messages",
56
+ code: "invalid_messages",
57
+ },
58
+ });
59
+ }
60
+ // const isStreaming = (req.headers["content-type"] || "").startsWith("text/event-stream")
61
+ if (stream) {
62
+ return await streamRunner(req, res, next);
63
+ }
64
+ return await nonStreamRunner(req, res, next);
65
+ };
66
+ };
67
+ exports.completionRunner = completionRunner;
68
+ const streamGraphRunner = (agentDictionary, model2GraphData, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
69
+ return async (req, res, next) => {
70
+ const { model } = req.body;
71
+ try {
72
+ res.setHeader("Content-Type", "text/event-stream;charset=utf-8");
73
+ res.setHeader("Cache-Control", "no-cache, no-transform");
74
+ res.setHeader("X-Accel-Buffering", "no");
75
+ const baseData = {
76
+ id: (0, crypto_1.randomUUID)(),
77
+ created: Math.floor(Date.now() / 1000),
78
+ model,
79
+ };
80
+ const streamCallback = (context, token) => {
81
+ if (token) {
82
+ res.write(streamCompletionChunkCallback(baseData, "payload", token));
83
+ }
84
+ };
85
+ const streamAgentFilter = {
86
+ name: "streamAgentFilter",
87
+ agent: (0, agent_filters_1.streamAgentFilterGenerator)(streamCallback),
88
+ };
89
+ const filterList = [...agentFilters, streamAgentFilter];
90
+ res.write(streamCompletionChunkCallback(baseData, "start"));
91
+ try {
92
+ const dispatcher = streamGraphRunnerInternal(agentDictionary, model2GraphData, filterList, onLogCallback);
93
+ await dispatcher(req);
94
+ res.write(streamCompletionChunkCallback(baseData, "end"));
95
+ res.write(streamCompletionChunkCallback(baseData, "done"));
96
+ }
97
+ catch (__err) {
98
+ res.write(`data: ${JSON.stringify({ error: "GraphAI Something went wrong" })}\n\n`);
99
+ }
100
+ return res.end();
101
+ }
102
+ catch (e) {
103
+ next(e);
104
+ }
105
+ };
106
+ };
107
+ const nonStreamGraphRunner = (agentDictionary, model2GraphData, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
108
+ return async (req, res, next) => {
109
+ try {
110
+ const dispatcher = streamGraphRunnerInternal(agentDictionary, model2GraphData, agentFilters, onLogCallback);
111
+ const result = await dispatcher(req);
112
+ return res.json(result);
113
+ }
114
+ catch (e) {
115
+ next(e);
116
+ }
117
+ };
118
+ };
119
+ // internal function
120
+ const streamGraphRunnerInternal = (agentDictionary, model2GraphData, agentFilters = [], onLogCallback = (__log, __isUpdate) => { }) => {
121
+ return async (req) => {
122
+ const { messages, model } = req.body;
123
+ const { config } = req;
124
+ const graphData = model2GraphData(model);
125
+ const graphai = new graphai_1.GraphAI(graphData, agentDictionary, { agentFilters, config: config ?? {} });
126
+ // injectValue
127
+ graphai.injectValue("messages", messages);
128
+ graphai.onLogCallback = onLogCallback;
129
+ const result = await graphai.run();
130
+ return result;
131
+ };
132
+ };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { agentsList, agentDoc, agentDispatcher, agentRunner, nonStreamAgentDispatcher, streamAgentDispatcher, updateAgentVerbose } from "./agents";
2
2
  export { graphRunner, streamGraphRunner, nonStreamGraphRunner } from "./graph";
3
- export { ExpressAgentInfo, StreamChunkCallback, ContentCallback } from "./type";
3
+ export { completionRunner } from "./completions";
4
+ export { ExpressAgentInfo, StreamChunkCallback, ContentCallback, Model2GraphData } 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.nonStreamGraphRunner = exports.streamGraphRunner = exports.graphRunner = exports.updateAgentVerbose = exports.streamAgentDispatcher = exports.nonStreamAgentDispatcher = exports.agentRunner = exports.agentDispatcher = exports.agentDoc = exports.agentsList = void 0;
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;
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; } });
@@ -13,3 +13,5 @@ var graph_1 = require("./graph");
13
13
  Object.defineProperty(exports, "graphRunner", { enumerable: true, get: function () { return graph_1.graphRunner; } });
14
14
  Object.defineProperty(exports, "streamGraphRunner", { enumerable: true, get: function () { return graph_1.streamGraphRunner; } });
15
15
  Object.defineProperty(exports, "nonStreamGraphRunner", { enumerable: true, get: function () { return graph_1.nonStreamGraphRunner; } });
16
+ var completions_1 = require("./completions");
17
+ Object.defineProperty(exports, "completionRunner", { enumerable: true, get: function () { return completions_1.completionRunner; } });
package/lib/type.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AgentFunctionInfoSample, AgentFunctionContext } from "graphai";
1
+ import type { AgentFunctionInfoSample, AgentFunctionContext, GraphData } from "graphai";
2
2
  export type ExpressAgentInfo = {
3
3
  agentId: string;
4
4
  name: string;
@@ -13,6 +13,14 @@ export type ExpressAgentInfo = {
13
13
  output: any;
14
14
  stream: boolean;
15
15
  };
16
+ type BaseData = {
17
+ id: string;
18
+ created: number;
19
+ model: string;
20
+ };
16
21
  export type StreamChunkCallback = <T = string | Record<string, string>>(context: AgentFunctionContext, token: T) => void;
22
+ export type StreamCompletionChunkCallback = <T = string | Record<string, string>>(data: BaseData, status: string, token?: T) => void;
23
+ export type Model2GraphData = (model: string) => GraphData;
17
24
  export type ContentCallback = <T = string | Record<string, string>>(token: T) => void;
18
25
  export declare const DefaultEndOfStreamDelimiter = "___END___";
26
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@receptron/graphai_express",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "GraphAI express web server middleware.",
5
5
  "main": "lib/index.js",
6
6
  "files": [