@axiom-lattice/gateway 2.1.4 → 2.1.6
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +16 -0
- package/dist/index.js +103 -252
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -257
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
- package/src/controllers/agent_task.ts +1 -1
- package/src/services/agent_task_consumer.ts +86 -12
- package/src/services/queue_service.ts +70 -10
- package/src/services/AgentManager.ts +0 -46
- package/src/services/agent_task_types.ts +0 -2
- package/src/services/event_bus.ts +0 -62
- package/src/services/queue_service_memory.ts +0 -85
- package/src/services/queue_service_redis.ts +0 -86
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
1
|
// src/index.ts
|
|
8
2
|
import fastify from "fastify";
|
|
9
3
|
import cors from "@fastify/cors";
|
|
@@ -515,245 +509,8 @@ var getAgentGraph = async (request, reply) => {
|
|
|
515
509
|
}
|
|
516
510
|
};
|
|
517
511
|
|
|
518
|
-
// src/services/agent_task_types.ts
|
|
519
|
-
var AGENT_TASK_EVENT = "agent:execute";
|
|
520
|
-
|
|
521
|
-
// src/services/event_bus.ts
|
|
522
|
-
import { EventEmitter } from "events";
|
|
523
|
-
|
|
524
|
-
// src/services/queue_service_memory.ts
|
|
525
|
-
var queue_service_memory_exports = {};
|
|
526
|
-
__export(queue_service_memory_exports, {
|
|
527
|
-
popAgentTaskFromQueue: () => popAgentTaskFromQueue,
|
|
528
|
-
pushAgentTaskToQueue: () => pushAgentTaskToQueue
|
|
529
|
-
});
|
|
530
|
-
var queue_name = process.env.QUEUE_NAME || "tasks";
|
|
531
|
-
var queues = /* @__PURE__ */ new Map();
|
|
532
|
-
var ensureQueue = (queueName) => {
|
|
533
|
-
if (!queues.has(queueName)) {
|
|
534
|
-
queues.set(queueName, []);
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
var sendToQueue = async (queueName, message) => {
|
|
538
|
-
try {
|
|
539
|
-
ensureQueue(queueName);
|
|
540
|
-
const queue = queues.get(queueName);
|
|
541
|
-
queue.unshift(message);
|
|
542
|
-
const result = queue.length;
|
|
543
|
-
console.log("lPush (memory)", result);
|
|
544
|
-
return { data: result, error: null };
|
|
545
|
-
} catch (error) {
|
|
546
|
-
console.error(error);
|
|
547
|
-
return { data: null, error };
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
var popFromQueue = async (queueName) => {
|
|
551
|
-
try {
|
|
552
|
-
ensureQueue(queueName);
|
|
553
|
-
const queue = queues.get(queueName);
|
|
554
|
-
const message = queue.pop();
|
|
555
|
-
if (message) {
|
|
556
|
-
return { data: message, error: null };
|
|
557
|
-
}
|
|
558
|
-
return { data: null, error: null };
|
|
559
|
-
} catch (error) {
|
|
560
|
-
console.error(error);
|
|
561
|
-
return { data: null, error };
|
|
562
|
-
}
|
|
563
|
-
};
|
|
564
|
-
var createTenantQueue = async () => {
|
|
565
|
-
try {
|
|
566
|
-
ensureQueue(queue_name);
|
|
567
|
-
const queue = queues.get(queue_name);
|
|
568
|
-
const exists = queue !== void 0;
|
|
569
|
-
return { success: true, queue_name };
|
|
570
|
-
} catch (error) {
|
|
571
|
-
console.error(error);
|
|
572
|
-
return { success: false, error };
|
|
573
|
-
}
|
|
574
|
-
};
|
|
575
|
-
var pushAgentTaskToQueue = async (agentTask) => {
|
|
576
|
-
const tenantId = agentTask["x-tenant-id"];
|
|
577
|
-
try {
|
|
578
|
-
await createTenantQueue();
|
|
579
|
-
const result = await sendToQueue(`${queue_name}`, agentTask);
|
|
580
|
-
return result;
|
|
581
|
-
} catch (error) {
|
|
582
|
-
console.error(error);
|
|
583
|
-
return null;
|
|
584
|
-
}
|
|
585
|
-
};
|
|
586
|
-
var popAgentTaskFromQueue = async () => {
|
|
587
|
-
const result = await popFromQueue(`${queue_name}`);
|
|
588
|
-
return result;
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
// src/services/queue_service_redis.ts
|
|
592
|
-
var queue_service_redis_exports = {};
|
|
593
|
-
__export(queue_service_redis_exports, {
|
|
594
|
-
popAgentTaskFromQueue: () => popAgentTaskFromQueue2,
|
|
595
|
-
pushAgentTaskToQueue: () => pushAgentTaskToQueue2
|
|
596
|
-
});
|
|
597
|
-
import { createClient } from "redis";
|
|
598
|
-
var queue_name2 = process.env.QUEUE_NAME || "tasks";
|
|
599
|
-
var redisOptions = {
|
|
600
|
-
url: process.env.REDIS_URL || "redis://localhost:6379",
|
|
601
|
-
password: process.env.REDIS_PASSWORD
|
|
602
|
-
};
|
|
603
|
-
var redisClient = createClient(redisOptions);
|
|
604
|
-
(async () => {
|
|
605
|
-
redisClient.on(
|
|
606
|
-
"error",
|
|
607
|
-
(err) => console.error("Redis Client Error", err)
|
|
608
|
-
);
|
|
609
|
-
try {
|
|
610
|
-
await redisClient.connect();
|
|
611
|
-
console.log("Redis connection successful");
|
|
612
|
-
} catch (error) {
|
|
613
|
-
console.error("Redis connection failed:", error);
|
|
614
|
-
}
|
|
615
|
-
})();
|
|
616
|
-
var sendToQueue2 = async (queueName, message) => {
|
|
617
|
-
try {
|
|
618
|
-
const result = await redisClient.lPush(queueName, JSON.stringify(message));
|
|
619
|
-
console.log("lPush", result);
|
|
620
|
-
return { data: result, error: null };
|
|
621
|
-
} catch (error) {
|
|
622
|
-
console.error(error);
|
|
623
|
-
return { data: null, error };
|
|
624
|
-
}
|
|
625
|
-
};
|
|
626
|
-
var popFromQueue2 = async (queueName) => {
|
|
627
|
-
try {
|
|
628
|
-
const message = await redisClient.rPop(queueName);
|
|
629
|
-
if (message) {
|
|
630
|
-
return { data: JSON.parse(message), error: null };
|
|
631
|
-
}
|
|
632
|
-
return { data: null, error: null };
|
|
633
|
-
} catch (error) {
|
|
634
|
-
console.error(error);
|
|
635
|
-
return { data: null, error };
|
|
636
|
-
}
|
|
637
|
-
};
|
|
638
|
-
var createTenantQueue2 = async () => {
|
|
639
|
-
try {
|
|
640
|
-
const exists = await redisClient.exists(queue_name2);
|
|
641
|
-
return { success: true, queue_name: queue_name2 };
|
|
642
|
-
} catch (error) {
|
|
643
|
-
console.error(error);
|
|
644
|
-
return { success: false, error };
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
var pushAgentTaskToQueue2 = async (agentTask) => {
|
|
648
|
-
const tenantId = agentTask["x-tenant-id"];
|
|
649
|
-
try {
|
|
650
|
-
await createTenantQueue2();
|
|
651
|
-
const result = await sendToQueue2(`${queue_name2}`, agentTask);
|
|
652
|
-
return result;
|
|
653
|
-
} catch (error) {
|
|
654
|
-
console.error(error);
|
|
655
|
-
return null;
|
|
656
|
-
}
|
|
657
|
-
};
|
|
658
|
-
var popAgentTaskFromQueue2 = async () => {
|
|
659
|
-
const result = await popFromQueue2(`${queue_name2}`);
|
|
660
|
-
return result;
|
|
661
|
-
};
|
|
662
|
-
|
|
663
|
-
// src/services/queue_service.ts
|
|
664
|
-
var queueServiceType = process.env.QUEUE_SERVICE_TYPE || "memory";
|
|
665
|
-
var setQueueServiceType = (type) => {
|
|
666
|
-
queueServiceType = type;
|
|
667
|
-
console.log(`Queue service type set to: ${type}`);
|
|
668
|
-
};
|
|
669
|
-
var getQueueService = () => {
|
|
670
|
-
return queueServiceType === "redis" ? queue_service_redis_exports : queue_service_memory_exports;
|
|
671
|
-
};
|
|
672
|
-
var pushAgentTaskToQueue3 = async (agentTask) => {
|
|
673
|
-
const service = getQueueService();
|
|
674
|
-
return service.pushAgentTaskToQueue(agentTask);
|
|
675
|
-
};
|
|
676
|
-
var popAgentTaskFromQueue3 = async () => {
|
|
677
|
-
const service = getQueueService();
|
|
678
|
-
return service.popAgentTaskFromQueue();
|
|
679
|
-
};
|
|
680
|
-
|
|
681
|
-
// src/services/event_bus.ts
|
|
682
|
-
var EventBus = class {
|
|
683
|
-
constructor() {
|
|
684
|
-
this.emitter = new EventEmitter();
|
|
685
|
-
this.emitter.setMaxListeners(100);
|
|
686
|
-
}
|
|
687
|
-
/**
|
|
688
|
-
* 发布事件
|
|
689
|
-
* @param eventName 事件名称
|
|
690
|
-
* @param data 事件数据
|
|
691
|
-
*/
|
|
692
|
-
publish(eventName, data, useQueue = false) {
|
|
693
|
-
if (useQueue) {
|
|
694
|
-
pushAgentTaskToQueue3(data);
|
|
695
|
-
} else {
|
|
696
|
-
this.emitter.emit(eventName, data);
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
/**
|
|
700
|
-
* 订阅事件
|
|
701
|
-
* @param eventName 事件名称
|
|
702
|
-
* @param callback 回调函数
|
|
703
|
-
*/
|
|
704
|
-
subscribe(eventName, callback) {
|
|
705
|
-
this.emitter.on(eventName, callback);
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* 取消订阅事件
|
|
709
|
-
* @param eventName 事件名称
|
|
710
|
-
* @param callback 回调函数
|
|
711
|
-
*/
|
|
712
|
-
unsubscribe(eventName, callback) {
|
|
713
|
-
this.emitter.off(eventName, callback);
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* 只订阅一次事件
|
|
717
|
-
* @param eventName 事件名称
|
|
718
|
-
* @param callback 回调函数
|
|
719
|
-
*/
|
|
720
|
-
subscribeOnce(eventName, callback) {
|
|
721
|
-
this.emitter.once(eventName, callback);
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
var eventBus = new EventBus();
|
|
725
|
-
var event_bus_default = eventBus;
|
|
726
|
-
|
|
727
|
-
// src/services/AgentManager.ts
|
|
728
|
-
var AgentManager = class _AgentManager {
|
|
729
|
-
constructor() {
|
|
730
|
-
this.agents = [];
|
|
731
|
-
}
|
|
732
|
-
static getInstance() {
|
|
733
|
-
if (!_AgentManager.instance) {
|
|
734
|
-
_AgentManager.instance = new _AgentManager();
|
|
735
|
-
}
|
|
736
|
-
return _AgentManager.instance;
|
|
737
|
-
}
|
|
738
|
-
callAgentInQueue(queue) {
|
|
739
|
-
return new Promise((resolve, reject) => {
|
|
740
|
-
try {
|
|
741
|
-
event_bus_default.publish(
|
|
742
|
-
AGENT_TASK_EVENT,
|
|
743
|
-
{
|
|
744
|
-
...queue
|
|
745
|
-
},
|
|
746
|
-
true
|
|
747
|
-
);
|
|
748
|
-
resolve(true);
|
|
749
|
-
} catch (error) {
|
|
750
|
-
reject(error);
|
|
751
|
-
}
|
|
752
|
-
});
|
|
753
|
-
}
|
|
754
|
-
};
|
|
755
|
-
|
|
756
512
|
// src/controllers/agent_task.ts
|
|
513
|
+
import { AgentManager } from "@axiom-lattice/core";
|
|
757
514
|
var triggerAgentTask = async (request, reply) => {
|
|
758
515
|
try {
|
|
759
516
|
const { assistant_id, thread_id, input, command } = request.body;
|
|
@@ -1206,7 +963,56 @@ var configureSwagger = async (app2, customSwaggerConfig, customSwaggerUiConfig)
|
|
|
1206
963
|
await app2.register(swaggerUi, swaggerUiConfig);
|
|
1207
964
|
};
|
|
1208
965
|
|
|
966
|
+
// src/services/queue_service.ts
|
|
967
|
+
import {
|
|
968
|
+
queueLatticeManager,
|
|
969
|
+
registerQueueLattice,
|
|
970
|
+
getQueueLattice
|
|
971
|
+
} from "@axiom-lattice/core";
|
|
972
|
+
import { QueueType } from "@axiom-lattice/protocols";
|
|
973
|
+
import { RedisQueueClient } from "@axiom-lattice/queue-redis";
|
|
974
|
+
var DEFAULT_QUEUE_KEY = "default";
|
|
975
|
+
var queueServiceType = process.env.QUEUE_SERVICE_TYPE || "memory";
|
|
976
|
+
var setQueueServiceType = (type) => {
|
|
977
|
+
queueServiceType = type;
|
|
978
|
+
console.log(`Queue service type set to: ${type}`);
|
|
979
|
+
const queueName = process.env.QUEUE_NAME || "tasks";
|
|
980
|
+
const config = {
|
|
981
|
+
name: "Default Queue Service",
|
|
982
|
+
description: `Default ${type} queue service`,
|
|
983
|
+
type: type === "redis" ? QueueType.REDIS : QueueType.MEMORY,
|
|
984
|
+
queueName,
|
|
985
|
+
options: type === "redis" ? {
|
|
986
|
+
redisUrl: process.env.REDIS_URL,
|
|
987
|
+
redisPassword: process.env.REDIS_PASSWORD
|
|
988
|
+
} : void 0
|
|
989
|
+
};
|
|
990
|
+
if (queueLatticeManager.hasLattice(DEFAULT_QUEUE_KEY)) {
|
|
991
|
+
queueLatticeManager.removeLattice(DEFAULT_QUEUE_KEY);
|
|
992
|
+
}
|
|
993
|
+
let client;
|
|
994
|
+
if (type === "redis") {
|
|
995
|
+
client = new RedisQueueClient(queueName, {
|
|
996
|
+
redisUrl: process.env.REDIS_URL,
|
|
997
|
+
redisPassword: process.env.REDIS_PASSWORD
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
registerQueueLattice(DEFAULT_QUEUE_KEY, config, client);
|
|
1001
|
+
};
|
|
1002
|
+
var getQueueService = () => {
|
|
1003
|
+
if (!queueLatticeManager.hasLattice(DEFAULT_QUEUE_KEY)) {
|
|
1004
|
+
setQueueServiceType(queueServiceType);
|
|
1005
|
+
}
|
|
1006
|
+
return getQueueLattice(DEFAULT_QUEUE_KEY);
|
|
1007
|
+
};
|
|
1008
|
+
var popAgentTaskFromQueue = async () => {
|
|
1009
|
+
const queue = getQueueService();
|
|
1010
|
+
const result = await queue.pop();
|
|
1011
|
+
return result;
|
|
1012
|
+
};
|
|
1013
|
+
|
|
1209
1014
|
// src/services/agent_task_consumer.ts
|
|
1015
|
+
import { eventBus, AGENT_TASK_EVENT } from "@axiom-lattice/core";
|
|
1210
1016
|
var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
1211
1017
|
const {
|
|
1212
1018
|
assistant_id,
|
|
@@ -1242,16 +1048,59 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
|
1242
1048
|
if (!response.ok) {
|
|
1243
1049
|
throw new Error(`API\u8BF7\u6C42\u5931\u8D25: ${response.status} ${response.statusText}`);
|
|
1244
1050
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1051
|
+
const contentType = response.headers.get("content-type");
|
|
1052
|
+
if (contentType?.includes("text/event-stream")) {
|
|
1053
|
+
const reader = response.body?.getReader();
|
|
1054
|
+
const decoder = new TextDecoder();
|
|
1055
|
+
if (!reader) {
|
|
1056
|
+
throw new Error("Response body is not readable");
|
|
1057
|
+
}
|
|
1058
|
+
let buffer = "";
|
|
1059
|
+
let streamEnded = false;
|
|
1060
|
+
try {
|
|
1061
|
+
while (true) {
|
|
1062
|
+
const { done, value } = await reader.read();
|
|
1063
|
+
if (done) {
|
|
1064
|
+
streamEnded = true;
|
|
1065
|
+
console.log(
|
|
1066
|
+
`SSE\u6D41\u5DF2\u7ED3\u675F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1067
|
+
);
|
|
1068
|
+
break;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
} catch (streamError) {
|
|
1072
|
+
console.error("Error reading SSE stream:", streamError);
|
|
1073
|
+
throw streamError;
|
|
1074
|
+
} finally {
|
|
1075
|
+
reader.releaseLock();
|
|
1076
|
+
}
|
|
1077
|
+
if (callback_event) {
|
|
1078
|
+
const state = await agent_state({ assistant_id, thread_id });
|
|
1079
|
+
eventBus.publish(callback_event, {
|
|
1080
|
+
success: true,
|
|
1081
|
+
state,
|
|
1082
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
console.log(
|
|
1086
|
+
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1087
|
+
);
|
|
1088
|
+
return true;
|
|
1089
|
+
} else {
|
|
1090
|
+
await response.text();
|
|
1091
|
+
if (callback_event) {
|
|
1092
|
+
const state = await agent_state({ assistant_id, thread_id });
|
|
1093
|
+
eventBus.publish(callback_event, {
|
|
1094
|
+
success: true,
|
|
1095
|
+
state,
|
|
1096
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
console.log(
|
|
1100
|
+
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1101
|
+
);
|
|
1102
|
+
return true;
|
|
1250
1103
|
}
|
|
1251
|
-
console.log(
|
|
1252
|
-
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1253
|
-
);
|
|
1254
|
-
return true;
|
|
1255
1104
|
} catch (error) {
|
|
1256
1105
|
console.error(
|
|
1257
1106
|
`Agent\u4EFB\u52A1\u6267\u884C\u5931\u8D25: ${assistant_id}, \u7EBF\u7A0B: ${thread_id}`,
|
|
@@ -1268,7 +1117,7 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
|
1268
1117
|
return handleAgentTask(taskRequest, nextRetryCount);
|
|
1269
1118
|
}
|
|
1270
1119
|
if (callback_event) {
|
|
1271
|
-
|
|
1120
|
+
eventBus.publish(callback_event, {
|
|
1272
1121
|
success: false,
|
|
1273
1122
|
error: error instanceof Error ? error.message : String(error),
|
|
1274
1123
|
config: { assistant_id, thread_id, tenant_id }
|
|
@@ -1306,7 +1155,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1306
1155
|
* 初始化事件监听和队列轮询
|
|
1307
1156
|
*/
|
|
1308
1157
|
initialize() {
|
|
1309
|
-
|
|
1158
|
+
eventBus.subscribe(AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
|
|
1310
1159
|
this.startPollingQueue();
|
|
1311
1160
|
console.log("Agent\u4EFB\u52A1\u6D88\u8D39\u8005\u5DF2\u542F\u52A8\u5E76\u76D1\u542C\u4EFB\u52A1\u4E8B\u4EF6\u548C\u961F\u5217");
|
|
1312
1161
|
}
|
|
@@ -1349,7 +1198,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1349
1198
|
*/
|
|
1350
1199
|
async processNextTask() {
|
|
1351
1200
|
try {
|
|
1352
|
-
const queueResult = await
|
|
1201
|
+
const queueResult = await popAgentTaskFromQueue();
|
|
1353
1202
|
if (queueResult && queueResult.data) {
|
|
1354
1203
|
const taskItem = queueResult.data;
|
|
1355
1204
|
if (taskItem && typeof taskItem === "object") {
|
|
@@ -1425,7 +1274,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1425
1274
|
handleAgentTask(taskRequest).catch((error) => {
|
|
1426
1275
|
console.error("\u5904\u7406Agent\u4EFB\u52A1\u65F6\u53D1\u751F\u672A\u6355\u83B7\u7684\u9519\u8BEF:", error);
|
|
1427
1276
|
if (taskRequest.callback_event) {
|
|
1428
|
-
|
|
1277
|
+
eventBus.publish(taskRequest.callback_event, {
|
|
1429
1278
|
success: false,
|
|
1430
1279
|
error: error instanceof Error ? error.message : String(error),
|
|
1431
1280
|
config: {
|