@axiom-lattice/gateway 2.1.5 → 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 +10 -0
- package/dist/index.js +103 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -280
- 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 -116
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,268 +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 redisClient = null;
|
|
600
|
-
var isConnecting = false;
|
|
601
|
-
var connectionPromise = null;
|
|
602
|
-
var getRedisClient = async () => {
|
|
603
|
-
if (redisClient?.isOpen) {
|
|
604
|
-
return redisClient;
|
|
605
|
-
}
|
|
606
|
-
if (isConnecting && connectionPromise) {
|
|
607
|
-
await connectionPromise;
|
|
608
|
-
if (redisClient?.isOpen) {
|
|
609
|
-
return redisClient;
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
isConnecting = true;
|
|
613
|
-
connectionPromise = (async () => {
|
|
614
|
-
try {
|
|
615
|
-
const redisOptions = {
|
|
616
|
-
url: process.env.REDIS_URL || "redis://localhost:6379",
|
|
617
|
-
password: process.env.REDIS_PASSWORD
|
|
618
|
-
};
|
|
619
|
-
redisClient = createClient(redisOptions);
|
|
620
|
-
redisClient.on(
|
|
621
|
-
"error",
|
|
622
|
-
(err) => console.error("Redis Client Error", err)
|
|
623
|
-
);
|
|
624
|
-
await redisClient.connect();
|
|
625
|
-
console.log("Redis connection successful");
|
|
626
|
-
isConnecting = false;
|
|
627
|
-
} catch (error) {
|
|
628
|
-
console.error("Redis connection failed:", error);
|
|
629
|
-
isConnecting = false;
|
|
630
|
-
throw error;
|
|
631
|
-
}
|
|
632
|
-
})();
|
|
633
|
-
await connectionPromise;
|
|
634
|
-
return redisClient;
|
|
635
|
-
};
|
|
636
|
-
var sendToQueue2 = async (queueName, message) => {
|
|
637
|
-
try {
|
|
638
|
-
const client = await getRedisClient();
|
|
639
|
-
const result = await client.lPush(queueName, JSON.stringify(message));
|
|
640
|
-
console.log("lPush", result);
|
|
641
|
-
return { data: result, error: null };
|
|
642
|
-
} catch (error) {
|
|
643
|
-
console.error(error);
|
|
644
|
-
return { data: null, error };
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
var popFromQueue2 = async (queueName) => {
|
|
648
|
-
try {
|
|
649
|
-
const client = await getRedisClient();
|
|
650
|
-
const message = await client.rPop(queueName);
|
|
651
|
-
if (message) {
|
|
652
|
-
return { data: JSON.parse(message), error: null };
|
|
653
|
-
}
|
|
654
|
-
return { data: null, error: null };
|
|
655
|
-
} catch (error) {
|
|
656
|
-
console.error(error);
|
|
657
|
-
return { data: null, error };
|
|
658
|
-
}
|
|
659
|
-
};
|
|
660
|
-
var createTenantQueue2 = async () => {
|
|
661
|
-
try {
|
|
662
|
-
const client = await getRedisClient();
|
|
663
|
-
const exists = await client.exists(queue_name2);
|
|
664
|
-
return { success: true, queue_name: queue_name2 };
|
|
665
|
-
} catch (error) {
|
|
666
|
-
console.error(error);
|
|
667
|
-
return { success: false, error };
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
var pushAgentTaskToQueue2 = async (agentTask) => {
|
|
671
|
-
const tenantId = agentTask["x-tenant-id"];
|
|
672
|
-
try {
|
|
673
|
-
await createTenantQueue2();
|
|
674
|
-
const result = await sendToQueue2(`${queue_name2}`, agentTask);
|
|
675
|
-
return result;
|
|
676
|
-
} catch (error) {
|
|
677
|
-
console.error(error);
|
|
678
|
-
return null;
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
|
-
var popAgentTaskFromQueue2 = async () => {
|
|
682
|
-
const result = await popFromQueue2(`${queue_name2}`);
|
|
683
|
-
return result;
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
// src/services/queue_service.ts
|
|
687
|
-
var queueServiceType = process.env.QUEUE_SERVICE_TYPE || "memory";
|
|
688
|
-
var setQueueServiceType = (type) => {
|
|
689
|
-
queueServiceType = type;
|
|
690
|
-
console.log(`Queue service type set to: ${type}`);
|
|
691
|
-
};
|
|
692
|
-
var getQueueService = () => {
|
|
693
|
-
return queueServiceType === "redis" ? queue_service_redis_exports : queue_service_memory_exports;
|
|
694
|
-
};
|
|
695
|
-
var pushAgentTaskToQueue3 = async (agentTask) => {
|
|
696
|
-
const service = getQueueService();
|
|
697
|
-
return service.pushAgentTaskToQueue(agentTask);
|
|
698
|
-
};
|
|
699
|
-
var popAgentTaskFromQueue3 = async () => {
|
|
700
|
-
const service = getQueueService();
|
|
701
|
-
return service.popAgentTaskFromQueue();
|
|
702
|
-
};
|
|
703
|
-
|
|
704
|
-
// src/services/event_bus.ts
|
|
705
|
-
var EventBus = class {
|
|
706
|
-
constructor() {
|
|
707
|
-
this.emitter = new EventEmitter();
|
|
708
|
-
this.emitter.setMaxListeners(100);
|
|
709
|
-
}
|
|
710
|
-
/**
|
|
711
|
-
* 发布事件
|
|
712
|
-
* @param eventName 事件名称
|
|
713
|
-
* @param data 事件数据
|
|
714
|
-
*/
|
|
715
|
-
publish(eventName, data, useQueue = false) {
|
|
716
|
-
if (useQueue) {
|
|
717
|
-
pushAgentTaskToQueue3(data);
|
|
718
|
-
} else {
|
|
719
|
-
this.emitter.emit(eventName, data);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* 订阅事件
|
|
724
|
-
* @param eventName 事件名称
|
|
725
|
-
* @param callback 回调函数
|
|
726
|
-
*/
|
|
727
|
-
subscribe(eventName, callback) {
|
|
728
|
-
this.emitter.on(eventName, callback);
|
|
729
|
-
}
|
|
730
|
-
/**
|
|
731
|
-
* 取消订阅事件
|
|
732
|
-
* @param eventName 事件名称
|
|
733
|
-
* @param callback 回调函数
|
|
734
|
-
*/
|
|
735
|
-
unsubscribe(eventName, callback) {
|
|
736
|
-
this.emitter.off(eventName, callback);
|
|
737
|
-
}
|
|
738
|
-
/**
|
|
739
|
-
* 只订阅一次事件
|
|
740
|
-
* @param eventName 事件名称
|
|
741
|
-
* @param callback 回调函数
|
|
742
|
-
*/
|
|
743
|
-
subscribeOnce(eventName, callback) {
|
|
744
|
-
this.emitter.once(eventName, callback);
|
|
745
|
-
}
|
|
746
|
-
};
|
|
747
|
-
var eventBus = new EventBus();
|
|
748
|
-
var event_bus_default = eventBus;
|
|
749
|
-
|
|
750
|
-
// src/services/AgentManager.ts
|
|
751
|
-
var AgentManager = class _AgentManager {
|
|
752
|
-
constructor() {
|
|
753
|
-
this.agents = [];
|
|
754
|
-
}
|
|
755
|
-
static getInstance() {
|
|
756
|
-
if (!_AgentManager.instance) {
|
|
757
|
-
_AgentManager.instance = new _AgentManager();
|
|
758
|
-
}
|
|
759
|
-
return _AgentManager.instance;
|
|
760
|
-
}
|
|
761
|
-
callAgentInQueue(queue) {
|
|
762
|
-
return new Promise((resolve, reject) => {
|
|
763
|
-
try {
|
|
764
|
-
event_bus_default.publish(
|
|
765
|
-
AGENT_TASK_EVENT,
|
|
766
|
-
{
|
|
767
|
-
...queue
|
|
768
|
-
},
|
|
769
|
-
true
|
|
770
|
-
);
|
|
771
|
-
resolve(true);
|
|
772
|
-
} catch (error) {
|
|
773
|
-
reject(error);
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
|
|
779
512
|
// src/controllers/agent_task.ts
|
|
513
|
+
import { AgentManager } from "@axiom-lattice/core";
|
|
780
514
|
var triggerAgentTask = async (request, reply) => {
|
|
781
515
|
try {
|
|
782
516
|
const { assistant_id, thread_id, input, command } = request.body;
|
|
@@ -1229,7 +963,56 @@ var configureSwagger = async (app2, customSwaggerConfig, customSwaggerUiConfig)
|
|
|
1229
963
|
await app2.register(swaggerUi, swaggerUiConfig);
|
|
1230
964
|
};
|
|
1231
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
|
+
|
|
1232
1014
|
// src/services/agent_task_consumer.ts
|
|
1015
|
+
import { eventBus, AGENT_TASK_EVENT } from "@axiom-lattice/core";
|
|
1233
1016
|
var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
1234
1017
|
const {
|
|
1235
1018
|
assistant_id,
|
|
@@ -1265,16 +1048,59 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
|
1265
1048
|
if (!response.ok) {
|
|
1266
1049
|
throw new Error(`API\u8BF7\u6C42\u5931\u8D25: ${response.status} ${response.statusText}`);
|
|
1267
1050
|
}
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
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;
|
|
1273
1103
|
}
|
|
1274
|
-
console.log(
|
|
1275
|
-
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1276
|
-
);
|
|
1277
|
-
return true;
|
|
1278
1104
|
} catch (error) {
|
|
1279
1105
|
console.error(
|
|
1280
1106
|
`Agent\u4EFB\u52A1\u6267\u884C\u5931\u8D25: ${assistant_id}, \u7EBF\u7A0B: ${thread_id}`,
|
|
@@ -1291,7 +1117,7 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
|
1291
1117
|
return handleAgentTask(taskRequest, nextRetryCount);
|
|
1292
1118
|
}
|
|
1293
1119
|
if (callback_event) {
|
|
1294
|
-
|
|
1120
|
+
eventBus.publish(callback_event, {
|
|
1295
1121
|
success: false,
|
|
1296
1122
|
error: error instanceof Error ? error.message : String(error),
|
|
1297
1123
|
config: { assistant_id, thread_id, tenant_id }
|
|
@@ -1329,7 +1155,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1329
1155
|
* 初始化事件监听和队列轮询
|
|
1330
1156
|
*/
|
|
1331
1157
|
initialize() {
|
|
1332
|
-
|
|
1158
|
+
eventBus.subscribe(AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
|
|
1333
1159
|
this.startPollingQueue();
|
|
1334
1160
|
console.log("Agent\u4EFB\u52A1\u6D88\u8D39\u8005\u5DF2\u542F\u52A8\u5E76\u76D1\u542C\u4EFB\u52A1\u4E8B\u4EF6\u548C\u961F\u5217");
|
|
1335
1161
|
}
|
|
@@ -1372,7 +1198,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1372
1198
|
*/
|
|
1373
1199
|
async processNextTask() {
|
|
1374
1200
|
try {
|
|
1375
|
-
const queueResult = await
|
|
1201
|
+
const queueResult = await popAgentTaskFromQueue();
|
|
1376
1202
|
if (queueResult && queueResult.data) {
|
|
1377
1203
|
const taskItem = queueResult.data;
|
|
1378
1204
|
if (taskItem && typeof taskItem === "object") {
|
|
@@ -1448,7 +1274,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
|
1448
1274
|
handleAgentTask(taskRequest).catch((error) => {
|
|
1449
1275
|
console.error("\u5904\u7406Agent\u4EFB\u52A1\u65F6\u53D1\u751F\u672A\u6355\u83B7\u7684\u9519\u8BEF:", error);
|
|
1450
1276
|
if (taskRequest.callback_event) {
|
|
1451
|
-
|
|
1277
|
+
eventBus.publish(taskRequest.callback_event, {
|
|
1452
1278
|
success: false,
|
|
1453
1279
|
error: error instanceof Error ? error.message : String(error),
|
|
1454
1280
|
config: {
|