@axiom-lattice/gateway 1.0.11
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/.eslintrc.json +22 -0
- package/.turbo/turbo-build.log +21 -0
- package/LICENSE +201 -0
- package/README.md +45 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +691 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +659 -0
- package/dist/index.mjs.map +1 -0
- package/jest.config.js +23 -0
- package/package.json +61 -0
- package/src/__tests__/setup.ts +5 -0
- package/src/controllers/assistant.ts +26 -0
- package/src/controllers/memory.ts +242 -0
- package/src/controllers/run.ts +233 -0
- package/src/index.ts +101 -0
- package/src/logger/Logger.ts +186 -0
- package/src/routes/index.ts +70 -0
- package/src/services/AgentManager.ts +42 -0
- package/src/services/agent_service.ts +243 -0
- package/src/services/agent_task_types.ts +2 -0
- package/src/services/event_bus.ts +62 -0
- package/src/services/queue_service.ts +82 -0
- package/src/services/supabase.ts +26 -0
- package/src/types/index.ts +123 -0
- package/src/types/koa.d.ts +7 -0
- package/tsconfig.json +26 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
LatticeGateway: () => LatticeGateway
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_fastify = __toESM(require("fastify"));
|
|
37
|
+
var import_cors = __toESM(require("@fastify/cors"));
|
|
38
|
+
var import_sensible = __toESM(require("@fastify/sensible"));
|
|
39
|
+
|
|
40
|
+
// src/services/agent_service.ts
|
|
41
|
+
var import_messages = require("@langchain/core/messages");
|
|
42
|
+
var import_langgraph = require("@langchain/langgraph");
|
|
43
|
+
var import_uuid = require("uuid");
|
|
44
|
+
var import_core = require("@axiom-lattice/core");
|
|
45
|
+
async function agent_invoke({
|
|
46
|
+
input,
|
|
47
|
+
thread_id,
|
|
48
|
+
assistant_id,
|
|
49
|
+
tenant_id,
|
|
50
|
+
command,
|
|
51
|
+
run_id
|
|
52
|
+
}) {
|
|
53
|
+
const runnable_agent = (0, import_core.getAgentLattice)(assistant_id)?.client;
|
|
54
|
+
const { files, message, ...rest } = input;
|
|
55
|
+
const humanMessage = new import_messages.HumanMessage(message || "");
|
|
56
|
+
humanMessage.additional_kwargs = { files };
|
|
57
|
+
const messages = [humanMessage];
|
|
58
|
+
if (!runnable_agent) {
|
|
59
|
+
throw new Error(`Agent ${assistant_id} not found`);
|
|
60
|
+
}
|
|
61
|
+
const result = await runnable_agent.invoke(
|
|
62
|
+
command ? new import_langgraph.Command(command) : { ...rest, messages, "x-tenant-id": tenant_id },
|
|
63
|
+
{
|
|
64
|
+
configurable: {
|
|
65
|
+
thread_id,
|
|
66
|
+
run_id: run_id || (0, import_uuid.v4)(),
|
|
67
|
+
recursionLimit: 200,
|
|
68
|
+
"x-tenant-id": tenant_id,
|
|
69
|
+
"x-request-id": run_id,
|
|
70
|
+
"x-thread-id": thread_id
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
async function agent_stream({
|
|
77
|
+
input,
|
|
78
|
+
thread_id,
|
|
79
|
+
command,
|
|
80
|
+
tenant_id,
|
|
81
|
+
assistant_id,
|
|
82
|
+
run_id
|
|
83
|
+
}) {
|
|
84
|
+
const runnable_agent = (0, import_core.getAgentClient)(assistant_id);
|
|
85
|
+
const { files, message, ...rest } = input;
|
|
86
|
+
let messages = [];
|
|
87
|
+
if (!command) {
|
|
88
|
+
const humanMessage = new import_messages.HumanMessage(message);
|
|
89
|
+
humanMessage.additional_kwargs = { files };
|
|
90
|
+
messages = [humanMessage];
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
if (!runnable_agent) {
|
|
94
|
+
throw new Error(`Agent ${assistant_id} not found`);
|
|
95
|
+
}
|
|
96
|
+
const agentStream = await runnable_agent.stream(
|
|
97
|
+
command ? new import_langgraph.Command(command) : {
|
|
98
|
+
...rest,
|
|
99
|
+
messages,
|
|
100
|
+
"x-tenant-id": tenant_id
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
configurable: {
|
|
104
|
+
thread_id,
|
|
105
|
+
run_id: run_id || (0, import_uuid.v4)(),
|
|
106
|
+
"x-tenant-id": tenant_id,
|
|
107
|
+
"x-request-id": run_id,
|
|
108
|
+
"x-thread-id": thread_id
|
|
109
|
+
},
|
|
110
|
+
streamMode: ["updates", "messages"],
|
|
111
|
+
subgraphs: false
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
return {
|
|
115
|
+
[Symbol.asyncIterator]: async function* () {
|
|
116
|
+
try {
|
|
117
|
+
for await (const chunk of agentStream) {
|
|
118
|
+
let data;
|
|
119
|
+
if (chunk[0] === "updates") {
|
|
120
|
+
const update = chunk[1];
|
|
121
|
+
const values = Object.values(update);
|
|
122
|
+
const messages2 = values[0].messages;
|
|
123
|
+
if (messages2?.[0]?.tool_call_id) {
|
|
124
|
+
data = messages2[0].toDict();
|
|
125
|
+
}
|
|
126
|
+
} else if (chunk[0] === "messages") {
|
|
127
|
+
const messages2 = chunk[1];
|
|
128
|
+
data = messages2?.[0]?.toDict();
|
|
129
|
+
}
|
|
130
|
+
if (chunk?.[1]?.__interrupt__) {
|
|
131
|
+
data = chunk?.[1]?.[0]?.toDict();
|
|
132
|
+
}
|
|
133
|
+
if (data) {
|
|
134
|
+
yield data;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error("Stream error:", error);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
} catch (error) {
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async function agent_state({
|
|
148
|
+
thread_id,
|
|
149
|
+
assistant_id
|
|
150
|
+
}) {
|
|
151
|
+
const runnable_agent = (0, import_core.getAgentClient)(assistant_id);
|
|
152
|
+
if (!runnable_agent) {
|
|
153
|
+
throw new Error(`Agent ${assistant_id} not found`);
|
|
154
|
+
}
|
|
155
|
+
const state = await runnable_agent.getState({
|
|
156
|
+
configurable: { thread_id, subgraphs: false }
|
|
157
|
+
});
|
|
158
|
+
return state;
|
|
159
|
+
}
|
|
160
|
+
async function agent_messages({
|
|
161
|
+
thread_id,
|
|
162
|
+
tenant_id,
|
|
163
|
+
assistant_id
|
|
164
|
+
}) {
|
|
165
|
+
const runnable_agent = (0, import_core.getAgentClient)(assistant_id);
|
|
166
|
+
if (!runnable_agent) {
|
|
167
|
+
throw new Error(`Agent ${assistant_id} not found`);
|
|
168
|
+
}
|
|
169
|
+
const state = await runnable_agent.getState({
|
|
170
|
+
configurable: { thread_id, subgraphs: false }
|
|
171
|
+
});
|
|
172
|
+
const messages = state.values.messages || [];
|
|
173
|
+
const filteredMessages = (0, import_messages.filterMessages)(messages, {
|
|
174
|
+
includeTypes: ["ai", "human", "tool"]
|
|
175
|
+
//["human", "ai", "tool"],
|
|
176
|
+
});
|
|
177
|
+
let messagesArray = filteredMessages.map((message) => ({
|
|
178
|
+
id: message.id,
|
|
179
|
+
role: message.getType(),
|
|
180
|
+
content: message.content,
|
|
181
|
+
files: message.additional_kwargs.files,
|
|
182
|
+
...message.lc_kwargs
|
|
183
|
+
}));
|
|
184
|
+
const action_messages = state.tasks.flatMap((task) => {
|
|
185
|
+
return task.interrupts.map((interrupt) => {
|
|
186
|
+
return {
|
|
187
|
+
role: "ai",
|
|
188
|
+
content: interrupt.value,
|
|
189
|
+
type: "action"
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
const new_messages = [...messagesArray, ...action_messages];
|
|
194
|
+
return new_messages;
|
|
195
|
+
}
|
|
196
|
+
async function draw_graph(assistant_id) {
|
|
197
|
+
const runnable_agent = (0, import_core.getAgentClient)(assistant_id);
|
|
198
|
+
if (!runnable_agent) {
|
|
199
|
+
throw new Error(`Agent ${assistant_id} not found`);
|
|
200
|
+
}
|
|
201
|
+
const drawableGraph = await runnable_agent.getGraphAsync();
|
|
202
|
+
const image = await drawableGraph.drawMermaid();
|
|
203
|
+
return image;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/controllers/run.ts
|
|
207
|
+
var import_uuid2 = require("uuid");
|
|
208
|
+
var createRun = async (request, reply) => {
|
|
209
|
+
try {
|
|
210
|
+
const {
|
|
211
|
+
assistant_id,
|
|
212
|
+
thread_id,
|
|
213
|
+
command,
|
|
214
|
+
streaming,
|
|
215
|
+
background,
|
|
216
|
+
...input
|
|
217
|
+
} = request.body;
|
|
218
|
+
const tenant_id = request.headers["x-tenant-id"];
|
|
219
|
+
const x_request_id = request.headers["x-request-id"] || (0, import_uuid2.v4)();
|
|
220
|
+
if (!assistant_id) {
|
|
221
|
+
reply.status(400).send({
|
|
222
|
+
success: false,
|
|
223
|
+
error: "\u52A9\u624BID\u662F\u5FC5\u9700\u7684"
|
|
224
|
+
});
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (streaming) {
|
|
228
|
+
const stream = await agent_stream({
|
|
229
|
+
assistant_id,
|
|
230
|
+
input,
|
|
231
|
+
thread_id,
|
|
232
|
+
command,
|
|
233
|
+
tenant_id,
|
|
234
|
+
run_id: x_request_id
|
|
235
|
+
});
|
|
236
|
+
reply.raw.writeHead(200, {
|
|
237
|
+
"Content-Type": "text/event-stream",
|
|
238
|
+
"Cache-Control": "no-cache",
|
|
239
|
+
Connection: "keep-alive",
|
|
240
|
+
"Access-Control-Allow-Origin": "*"
|
|
241
|
+
});
|
|
242
|
+
try {
|
|
243
|
+
for await (const chunk of stream) {
|
|
244
|
+
reply.raw.write(`data: ${JSON.stringify(chunk)}
|
|
245
|
+
|
|
246
|
+
`);
|
|
247
|
+
}
|
|
248
|
+
} catch (error) {
|
|
249
|
+
} finally {
|
|
250
|
+
reply.raw.end();
|
|
251
|
+
return reply.hijack();
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
const result = await agent_invoke({
|
|
255
|
+
assistant_id,
|
|
256
|
+
input,
|
|
257
|
+
command,
|
|
258
|
+
thread_id,
|
|
259
|
+
tenant_id,
|
|
260
|
+
run_id: x_request_id
|
|
261
|
+
});
|
|
262
|
+
reply.status(200).send(result);
|
|
263
|
+
}
|
|
264
|
+
} catch (error) {
|
|
265
|
+
reply.status(500).send({
|
|
266
|
+
success: false,
|
|
267
|
+
error: `\u521B\u5EFA\u8FD0\u884C\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// src/controllers/memory.ts
|
|
273
|
+
var setMemoryItem = async (request, reply) => {
|
|
274
|
+
try {
|
|
275
|
+
const { assistantId, key } = request.params;
|
|
276
|
+
const value = request.body;
|
|
277
|
+
if (!assistantId || !key) {
|
|
278
|
+
reply.status(400).send({
|
|
279
|
+
success: false,
|
|
280
|
+
error: "\u52A9\u624BID\u548C\u952E\u662F\u5FC5\u9700\u7684"
|
|
281
|
+
});
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (value === void 0) {
|
|
285
|
+
reply.status(400).send({
|
|
286
|
+
success: false,
|
|
287
|
+
error: "\u503C\u662F\u5FC5\u9700\u7684"
|
|
288
|
+
});
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
reply.status(500).send();
|
|
292
|
+
return;
|
|
293
|
+
} catch (error) {
|
|
294
|
+
reply.status(500).send({
|
|
295
|
+
success: false,
|
|
296
|
+
error: `\u8BBE\u7F6E\u5185\u5B58\u9879\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
var getMemoryItem = async (request, reply) => {
|
|
301
|
+
try {
|
|
302
|
+
const { assistantId, key } = request.params;
|
|
303
|
+
if (!assistantId || !key) {
|
|
304
|
+
reply.status(400).send({
|
|
305
|
+
success: false,
|
|
306
|
+
error: "\u52A9\u624BID\u548C\u952E\u662F\u5FC5\u9700\u7684"
|
|
307
|
+
});
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
reply.status(404).send();
|
|
311
|
+
return;
|
|
312
|
+
} catch (error) {
|
|
313
|
+
reply.status(500).send({
|
|
314
|
+
success: false,
|
|
315
|
+
error: `\u83B7\u53D6\u5185\u5B58\u9879\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
var getAllMemoryItems = async (request, reply) => {
|
|
320
|
+
try {
|
|
321
|
+
const { assistantId, thread_id } = request.params;
|
|
322
|
+
const tenant_id = request.headers["x-tenant-id"];
|
|
323
|
+
if (!thread_id) {
|
|
324
|
+
reply.status(400).send({
|
|
325
|
+
success: false,
|
|
326
|
+
error: "\u7EBF\u7A0BID\u662F\u5FC5\u9700\u7684"
|
|
327
|
+
});
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (!assistantId) {
|
|
331
|
+
reply.status(400).send({
|
|
332
|
+
success: false,
|
|
333
|
+
error: "\u52A9\u624BID\u662F\u5FC5\u9700\u7684"
|
|
334
|
+
});
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const result = await agent_messages({
|
|
338
|
+
assistant_id: assistantId,
|
|
339
|
+
thread_id,
|
|
340
|
+
tenant_id
|
|
341
|
+
});
|
|
342
|
+
if (!result) {
|
|
343
|
+
reply.status(500).send(result);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
reply.send(result);
|
|
347
|
+
} catch (error) {
|
|
348
|
+
reply.status(500).send({
|
|
349
|
+
success: false,
|
|
350
|
+
error: `\u83B7\u53D6\u6240\u6709\u5185\u5B58\u9879\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
var getAgentState = async (request, reply) => {
|
|
355
|
+
try {
|
|
356
|
+
const { assistantId, thread_id } = request.params;
|
|
357
|
+
if (!thread_id) {
|
|
358
|
+
reply.status(400).send({
|
|
359
|
+
success: false,
|
|
360
|
+
error: "\u7EBF\u7A0BID\u662F\u5FC5\u9700\u7684"
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (!assistantId) {
|
|
365
|
+
reply.status(400).send({
|
|
366
|
+
success: false,
|
|
367
|
+
error: "\u52A9\u624BID\u662F\u5FC5\u9700\u7684"
|
|
368
|
+
});
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const result = await agent_state({
|
|
372
|
+
assistant_id: assistantId,
|
|
373
|
+
thread_id
|
|
374
|
+
});
|
|
375
|
+
if (!result) {
|
|
376
|
+
reply.status(500).send(result);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
reply.send(result);
|
|
380
|
+
} catch (error) {
|
|
381
|
+
reply.status(500).send({
|
|
382
|
+
success: false,
|
|
383
|
+
error: `\u83B7\u53D6\u52A9\u624B\u72B6\u6001\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var deleteMemoryItem = async (request, reply) => {
|
|
388
|
+
try {
|
|
389
|
+
const { assistantId, key } = request.params;
|
|
390
|
+
if (!assistantId || !key) {
|
|
391
|
+
reply.status(400).send({
|
|
392
|
+
success: false,
|
|
393
|
+
error: "\u52A9\u624BID\u548C\u952E\u662F\u5FC5\u9700\u7684"
|
|
394
|
+
});
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
reply.status(500).send();
|
|
398
|
+
return;
|
|
399
|
+
reply.status(204).send();
|
|
400
|
+
} catch (error) {
|
|
401
|
+
reply.status(500).send({
|
|
402
|
+
success: false,
|
|
403
|
+
error: `\u5220\u9664\u5185\u5B58\u9879\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
var clearMemory = async (request, reply) => {
|
|
408
|
+
try {
|
|
409
|
+
const { assistantId } = request.params;
|
|
410
|
+
if (!assistantId) {
|
|
411
|
+
reply.status(400).send({
|
|
412
|
+
success: false,
|
|
413
|
+
error: "\u52A9\u624BID\u662F\u5FC5\u9700\u7684"
|
|
414
|
+
});
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
let result;
|
|
418
|
+
if (!result.success) {
|
|
419
|
+
reply.status(500).send(result);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
reply.status(204).send();
|
|
423
|
+
} catch (error) {
|
|
424
|
+
reply.status(500).send({
|
|
425
|
+
success: false,
|
|
426
|
+
error: `\u6E05\u9664\u5185\u5B58\u65F6\u53D1\u751F\u9519\u8BEF: ${error.message}`
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// src/controllers/assistant.ts
|
|
432
|
+
var getAgentGraph = async (request, reply) => {
|
|
433
|
+
try {
|
|
434
|
+
const { assistantId } = request.params;
|
|
435
|
+
const imageData = await draw_graph(assistantId);
|
|
436
|
+
reply.header("Content-Type", "application/json").send({
|
|
437
|
+
image: imageData
|
|
438
|
+
});
|
|
439
|
+
} catch (error) {
|
|
440
|
+
reply.status(500).send({
|
|
441
|
+
success: false,
|
|
442
|
+
error: error.message || "\u83B7\u53D6\u4EE3\u7406\u56FE\u8868\u5931\u8D25"
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// src/routes/index.ts
|
|
448
|
+
var registerRoutes = (app2) => {
|
|
449
|
+
app2.post("/api/runs", createRun);
|
|
450
|
+
app2.get(
|
|
451
|
+
"/api/assistants/:assistantId/:thread_id/memory",
|
|
452
|
+
getAllMemoryItems
|
|
453
|
+
);
|
|
454
|
+
app2.get(
|
|
455
|
+
"/api/assistants/:assistantId/:thread_id/state",
|
|
456
|
+
getAgentState
|
|
457
|
+
);
|
|
458
|
+
app2.get(
|
|
459
|
+
"/api/assistants/:assistantId/memory/:key",
|
|
460
|
+
getMemoryItem
|
|
461
|
+
);
|
|
462
|
+
app2.put(
|
|
463
|
+
"/api/assistants/:assistantId/memory/:key",
|
|
464
|
+
setMemoryItem
|
|
465
|
+
);
|
|
466
|
+
app2.delete(
|
|
467
|
+
"/api/assistants/:assistantId/memory/:key",
|
|
468
|
+
deleteMemoryItem
|
|
469
|
+
);
|
|
470
|
+
app2.delete("/api/assistants/:assistantId/memory", clearMemory);
|
|
471
|
+
app2.get("/api/assistants/:assistantId/graph", getAgentGraph);
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// src/logger/Logger.ts
|
|
475
|
+
var import_pino = __toESM(require("pino"));
|
|
476
|
+
var import_pino_pretty = require("pino-pretty");
|
|
477
|
+
var import_pino_roll = require("pino-roll");
|
|
478
|
+
var PinoLoggerFactory = class _PinoLoggerFactory {
|
|
479
|
+
constructor() {
|
|
480
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
481
|
+
const loggerConfig = {
|
|
482
|
+
// 自定义时间戳格式
|
|
483
|
+
timestamp: () => `,"@timestamp":"${(/* @__PURE__ */ new Date()).toISOString()}"`,
|
|
484
|
+
// 关闭默认的时间戳键
|
|
485
|
+
base: {
|
|
486
|
+
"@version": "1",
|
|
487
|
+
app_name: "lattice",
|
|
488
|
+
service_name: "lattice/graph-server",
|
|
489
|
+
thread_name: "main",
|
|
490
|
+
logger_name: "lattice-graph-logger"
|
|
491
|
+
},
|
|
492
|
+
formatters: {
|
|
493
|
+
level: (label, number) => {
|
|
494
|
+
return {
|
|
495
|
+
level: label.toUpperCase(),
|
|
496
|
+
level_value: number * 1e3
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
if (isProd) {
|
|
502
|
+
try {
|
|
503
|
+
this.pinoLogger = (0, import_pino.default)(
|
|
504
|
+
loggerConfig,
|
|
505
|
+
import_pino.default.transport({
|
|
506
|
+
target: "pino-roll",
|
|
507
|
+
options: {
|
|
508
|
+
file: "./logs/fin_ai_graph_server",
|
|
509
|
+
frequency: "daily",
|
|
510
|
+
mkdir: true
|
|
511
|
+
}
|
|
512
|
+
})
|
|
513
|
+
);
|
|
514
|
+
} catch (error) {
|
|
515
|
+
console.error(
|
|
516
|
+
"\u65E0\u6CD5\u521D\u59CB\u5316 pino-roll \u65E5\u5FD7\u8BB0\u5F55\u5668\uFF0C\u56DE\u9000\u5230\u63A7\u5236\u53F0\u65E5\u5FD7",
|
|
517
|
+
error
|
|
518
|
+
);
|
|
519
|
+
this.pinoLogger = (0, import_pino.default)({
|
|
520
|
+
...loggerConfig,
|
|
521
|
+
transport: {
|
|
522
|
+
target: "pino-pretty",
|
|
523
|
+
options: {
|
|
524
|
+
colorize: true
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
} else {
|
|
530
|
+
this.pinoLogger = (0, import_pino.default)({
|
|
531
|
+
...loggerConfig,
|
|
532
|
+
transport: {
|
|
533
|
+
target: "pino-pretty",
|
|
534
|
+
options: {
|
|
535
|
+
colorize: true
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
static getInstance() {
|
|
542
|
+
if (!_PinoLoggerFactory.instance) {
|
|
543
|
+
_PinoLoggerFactory.instance = new _PinoLoggerFactory();
|
|
544
|
+
}
|
|
545
|
+
return _PinoLoggerFactory.instance;
|
|
546
|
+
}
|
|
547
|
+
getPinoLogger() {
|
|
548
|
+
return this.pinoLogger;
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
var Logger = class _Logger {
|
|
552
|
+
constructor(options) {
|
|
553
|
+
this.context = options?.context || {};
|
|
554
|
+
this.name = options?.name || "lattice-graph-logger";
|
|
555
|
+
this.serviceName = options?.serviceName || "lattice/graph-server";
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* 获取合并了上下文的日志对象
|
|
559
|
+
* @param additionalContext 额外的上下文数据
|
|
560
|
+
* @returns 带有上下文的pino日志对象
|
|
561
|
+
*/
|
|
562
|
+
getContextualLogger(additionalContext) {
|
|
563
|
+
const pinoLogger = PinoLoggerFactory.getInstance().getPinoLogger();
|
|
564
|
+
const contextObj = {
|
|
565
|
+
"x-user-id": this.context["x-user-id"] || "",
|
|
566
|
+
"x-tenant-id": this.context["x-tenant-id"] || "",
|
|
567
|
+
"x-request-id": this.context["x-request-id"] || "",
|
|
568
|
+
"x-task-id": this.context["x-task-id"] || "",
|
|
569
|
+
"x-thread-id": this.context["x-thread-id"] || "",
|
|
570
|
+
service_name: this.serviceName,
|
|
571
|
+
logger_name: this.name,
|
|
572
|
+
...additionalContext
|
|
573
|
+
};
|
|
574
|
+
return pinoLogger.child(contextObj);
|
|
575
|
+
}
|
|
576
|
+
info(msg, obj) {
|
|
577
|
+
this.getContextualLogger(obj).info(msg);
|
|
578
|
+
}
|
|
579
|
+
error(msg, obj) {
|
|
580
|
+
this.getContextualLogger(obj).error(msg);
|
|
581
|
+
}
|
|
582
|
+
warn(msg, obj) {
|
|
583
|
+
this.getContextualLogger(obj).warn(msg);
|
|
584
|
+
}
|
|
585
|
+
debug(msg, obj) {
|
|
586
|
+
this.getContextualLogger(obj).debug(msg);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 更新Logger实例的上下文
|
|
590
|
+
*/
|
|
591
|
+
updateContext(context) {
|
|
592
|
+
this.context = {
|
|
593
|
+
...this.context,
|
|
594
|
+
...context
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* 创建一个新的Logger实例,继承当前Logger的上下文
|
|
599
|
+
*/
|
|
600
|
+
child(options) {
|
|
601
|
+
return new _Logger({
|
|
602
|
+
name: options.name || this.name,
|
|
603
|
+
serviceName: options.serviceName || this.serviceName,
|
|
604
|
+
context: {
|
|
605
|
+
...this.context,
|
|
606
|
+
...options.context
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// src/index.ts
|
|
613
|
+
process.on("unhandledRejection", (reason, promise) => {
|
|
614
|
+
console.error("\u672A\u5904\u7406\u7684Promise\u62D2\u7EDD:", reason);
|
|
615
|
+
});
|
|
616
|
+
var logger = new Logger({
|
|
617
|
+
serviceName: "lattice-gateway",
|
|
618
|
+
name: "fastify-server"
|
|
619
|
+
});
|
|
620
|
+
var app = (0, import_fastify.default)({
|
|
621
|
+
logger: false
|
|
622
|
+
// 禁用内置日志记录器
|
|
623
|
+
});
|
|
624
|
+
app.addHook("onRequest", (request, reply, done) => {
|
|
625
|
+
const context = {
|
|
626
|
+
"x-tenant-id": request.headers["x-tenant-id"],
|
|
627
|
+
"x-request-id": request.headers["x-request-id"]
|
|
628
|
+
};
|
|
629
|
+
done();
|
|
630
|
+
});
|
|
631
|
+
app.addHook("onResponse", (request, reply, done) => {
|
|
632
|
+
const context = {
|
|
633
|
+
"x-tenant-id": request.headers["x-tenant-id"],
|
|
634
|
+
"x-request-id": request.headers["x-request-id"]
|
|
635
|
+
};
|
|
636
|
+
done();
|
|
637
|
+
});
|
|
638
|
+
app.register(import_cors.default, {
|
|
639
|
+
origin: true,
|
|
640
|
+
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
641
|
+
allowedHeaders: [
|
|
642
|
+
"Content-Type",
|
|
643
|
+
"Authorization",
|
|
644
|
+
"X-Requested-With",
|
|
645
|
+
"x-tenant-id",
|
|
646
|
+
"x-request-id"
|
|
647
|
+
],
|
|
648
|
+
exposedHeaders: ["Content-Type"],
|
|
649
|
+
credentials: true
|
|
650
|
+
});
|
|
651
|
+
app.register(import_sensible.default);
|
|
652
|
+
app.setErrorHandler((error, request, reply) => {
|
|
653
|
+
const context = {
|
|
654
|
+
"x-tenant-id": request.headers["x-tenant-id"],
|
|
655
|
+
"x-request-id": request.headers["x-request-id"]
|
|
656
|
+
};
|
|
657
|
+
logger.error(
|
|
658
|
+
`\u8BF7\u6C42\u9519\u8BEF: ${request.method} ${request.url} error:${error.message}`,
|
|
659
|
+
{
|
|
660
|
+
...context,
|
|
661
|
+
error: error.message,
|
|
662
|
+
stack: error.stack,
|
|
663
|
+
statusCode: error.statusCode || 500
|
|
664
|
+
}
|
|
665
|
+
);
|
|
666
|
+
reply.status(error.statusCode || 500).send({
|
|
667
|
+
success: false,
|
|
668
|
+
error: error.message || "\u670D\u52A1\u5668\u5185\u90E8\u9519\u8BEF"
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
app.decorate("logger", logger);
|
|
672
|
+
registerRoutes(app);
|
|
673
|
+
var start = async ({ port }) => {
|
|
674
|
+
try {
|
|
675
|
+
const target_port = port || Number(process.env.PORT) || 4001;
|
|
676
|
+
await app.listen({ port: target_port, host: "0.0.0.0" });
|
|
677
|
+
logger.info(`Lattice Gateway is running on port: ${port}`);
|
|
678
|
+
} catch (err) {
|
|
679
|
+
logger.error("Server start failed", { error: err });
|
|
680
|
+
process.exit(1);
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
var LatticeGateway = {
|
|
684
|
+
startAsHttpEndpoint: start,
|
|
685
|
+
app
|
|
686
|
+
};
|
|
687
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
688
|
+
0 && (module.exports = {
|
|
689
|
+
LatticeGateway
|
|
690
|
+
});
|
|
691
|
+
//# sourceMappingURL=index.js.map
|