@axiom-lattice/gateway 2.1.3 → 2.1.5
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 +10 -10
- package/CHANGELOG.md +14 -0
- package/dist/index.d.mts +65 -4
- package/dist/index.d.ts +65 -4
- package/dist/index.js +606 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +612 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/controllers/agent_task.ts +65 -0
- package/src/index.ts +30 -4
- package/src/routes/index.ts +11 -0
- package/src/schemas/index.ts +53 -0
- package/src/services/AgentManager.ts +25 -21
- package/src/services/agent_task_consumer.ts +325 -0
- package/src/services/queue_service.ts +32 -71
- package/src/services/queue_service_memory.ts +85 -0
- package/src/services/queue_service_redis.ts +116 -0
package/dist/index.js
CHANGED
|
@@ -534,6 +534,305 @@ var getAgentGraph = async (request, reply) => {
|
|
|
534
534
|
}
|
|
535
535
|
};
|
|
536
536
|
|
|
537
|
+
// src/services/agent_task_types.ts
|
|
538
|
+
var AGENT_TASK_EVENT = "agent:execute";
|
|
539
|
+
|
|
540
|
+
// src/services/event_bus.ts
|
|
541
|
+
var import_events = require("events");
|
|
542
|
+
|
|
543
|
+
// src/services/queue_service_memory.ts
|
|
544
|
+
var queue_service_memory_exports = {};
|
|
545
|
+
__export(queue_service_memory_exports, {
|
|
546
|
+
popAgentTaskFromQueue: () => popAgentTaskFromQueue,
|
|
547
|
+
pushAgentTaskToQueue: () => pushAgentTaskToQueue
|
|
548
|
+
});
|
|
549
|
+
var queue_name = process.env.QUEUE_NAME || "tasks";
|
|
550
|
+
var queues = /* @__PURE__ */ new Map();
|
|
551
|
+
var ensureQueue = (queueName) => {
|
|
552
|
+
if (!queues.has(queueName)) {
|
|
553
|
+
queues.set(queueName, []);
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
var sendToQueue = async (queueName, message) => {
|
|
557
|
+
try {
|
|
558
|
+
ensureQueue(queueName);
|
|
559
|
+
const queue = queues.get(queueName);
|
|
560
|
+
queue.unshift(message);
|
|
561
|
+
const result = queue.length;
|
|
562
|
+
console.log("lPush (memory)", result);
|
|
563
|
+
return { data: result, error: null };
|
|
564
|
+
} catch (error) {
|
|
565
|
+
console.error(error);
|
|
566
|
+
return { data: null, error };
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
var popFromQueue = async (queueName) => {
|
|
570
|
+
try {
|
|
571
|
+
ensureQueue(queueName);
|
|
572
|
+
const queue = queues.get(queueName);
|
|
573
|
+
const message = queue.pop();
|
|
574
|
+
if (message) {
|
|
575
|
+
return { data: message, error: null };
|
|
576
|
+
}
|
|
577
|
+
return { data: null, error: null };
|
|
578
|
+
} catch (error) {
|
|
579
|
+
console.error(error);
|
|
580
|
+
return { data: null, error };
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
var createTenantQueue = async () => {
|
|
584
|
+
try {
|
|
585
|
+
ensureQueue(queue_name);
|
|
586
|
+
const queue = queues.get(queue_name);
|
|
587
|
+
const exists = queue !== void 0;
|
|
588
|
+
return { success: true, queue_name };
|
|
589
|
+
} catch (error) {
|
|
590
|
+
console.error(error);
|
|
591
|
+
return { success: false, error };
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
var pushAgentTaskToQueue = async (agentTask) => {
|
|
595
|
+
const tenantId = agentTask["x-tenant-id"];
|
|
596
|
+
try {
|
|
597
|
+
await createTenantQueue();
|
|
598
|
+
const result = await sendToQueue(`${queue_name}`, agentTask);
|
|
599
|
+
return result;
|
|
600
|
+
} catch (error) {
|
|
601
|
+
console.error(error);
|
|
602
|
+
return null;
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
var popAgentTaskFromQueue = async () => {
|
|
606
|
+
const result = await popFromQueue(`${queue_name}`);
|
|
607
|
+
return result;
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
// src/services/queue_service_redis.ts
|
|
611
|
+
var queue_service_redis_exports = {};
|
|
612
|
+
__export(queue_service_redis_exports, {
|
|
613
|
+
popAgentTaskFromQueue: () => popAgentTaskFromQueue2,
|
|
614
|
+
pushAgentTaskToQueue: () => pushAgentTaskToQueue2
|
|
615
|
+
});
|
|
616
|
+
var import_redis = require("redis");
|
|
617
|
+
var queue_name2 = process.env.QUEUE_NAME || "tasks";
|
|
618
|
+
var redisClient = null;
|
|
619
|
+
var isConnecting = false;
|
|
620
|
+
var connectionPromise = null;
|
|
621
|
+
var getRedisClient = async () => {
|
|
622
|
+
if (redisClient?.isOpen) {
|
|
623
|
+
return redisClient;
|
|
624
|
+
}
|
|
625
|
+
if (isConnecting && connectionPromise) {
|
|
626
|
+
await connectionPromise;
|
|
627
|
+
if (redisClient?.isOpen) {
|
|
628
|
+
return redisClient;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
isConnecting = true;
|
|
632
|
+
connectionPromise = (async () => {
|
|
633
|
+
try {
|
|
634
|
+
const redisOptions = {
|
|
635
|
+
url: process.env.REDIS_URL || "redis://localhost:6379",
|
|
636
|
+
password: process.env.REDIS_PASSWORD
|
|
637
|
+
};
|
|
638
|
+
redisClient = (0, import_redis.createClient)(redisOptions);
|
|
639
|
+
redisClient.on(
|
|
640
|
+
"error",
|
|
641
|
+
(err) => console.error("Redis Client Error", err)
|
|
642
|
+
);
|
|
643
|
+
await redisClient.connect();
|
|
644
|
+
console.log("Redis connection successful");
|
|
645
|
+
isConnecting = false;
|
|
646
|
+
} catch (error) {
|
|
647
|
+
console.error("Redis connection failed:", error);
|
|
648
|
+
isConnecting = false;
|
|
649
|
+
throw error;
|
|
650
|
+
}
|
|
651
|
+
})();
|
|
652
|
+
await connectionPromise;
|
|
653
|
+
return redisClient;
|
|
654
|
+
};
|
|
655
|
+
var sendToQueue2 = async (queueName, message) => {
|
|
656
|
+
try {
|
|
657
|
+
const client = await getRedisClient();
|
|
658
|
+
const result = await client.lPush(queueName, JSON.stringify(message));
|
|
659
|
+
console.log("lPush", result);
|
|
660
|
+
return { data: result, error: null };
|
|
661
|
+
} catch (error) {
|
|
662
|
+
console.error(error);
|
|
663
|
+
return { data: null, error };
|
|
664
|
+
}
|
|
665
|
+
};
|
|
666
|
+
var popFromQueue2 = async (queueName) => {
|
|
667
|
+
try {
|
|
668
|
+
const client = await getRedisClient();
|
|
669
|
+
const message = await client.rPop(queueName);
|
|
670
|
+
if (message) {
|
|
671
|
+
return { data: JSON.parse(message), error: null };
|
|
672
|
+
}
|
|
673
|
+
return { data: null, error: null };
|
|
674
|
+
} catch (error) {
|
|
675
|
+
console.error(error);
|
|
676
|
+
return { data: null, error };
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
var createTenantQueue2 = async () => {
|
|
680
|
+
try {
|
|
681
|
+
const client = await getRedisClient();
|
|
682
|
+
const exists = await client.exists(queue_name2);
|
|
683
|
+
return { success: true, queue_name: queue_name2 };
|
|
684
|
+
} catch (error) {
|
|
685
|
+
console.error(error);
|
|
686
|
+
return { success: false, error };
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
var pushAgentTaskToQueue2 = async (agentTask) => {
|
|
690
|
+
const tenantId = agentTask["x-tenant-id"];
|
|
691
|
+
try {
|
|
692
|
+
await createTenantQueue2();
|
|
693
|
+
const result = await sendToQueue2(`${queue_name2}`, agentTask);
|
|
694
|
+
return result;
|
|
695
|
+
} catch (error) {
|
|
696
|
+
console.error(error);
|
|
697
|
+
return null;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
var popAgentTaskFromQueue2 = async () => {
|
|
701
|
+
const result = await popFromQueue2(`${queue_name2}`);
|
|
702
|
+
return result;
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// src/services/queue_service.ts
|
|
706
|
+
var queueServiceType = process.env.QUEUE_SERVICE_TYPE || "memory";
|
|
707
|
+
var setQueueServiceType = (type) => {
|
|
708
|
+
queueServiceType = type;
|
|
709
|
+
console.log(`Queue service type set to: ${type}`);
|
|
710
|
+
};
|
|
711
|
+
var getQueueService = () => {
|
|
712
|
+
return queueServiceType === "redis" ? queue_service_redis_exports : queue_service_memory_exports;
|
|
713
|
+
};
|
|
714
|
+
var pushAgentTaskToQueue3 = async (agentTask) => {
|
|
715
|
+
const service = getQueueService();
|
|
716
|
+
return service.pushAgentTaskToQueue(agentTask);
|
|
717
|
+
};
|
|
718
|
+
var popAgentTaskFromQueue3 = async () => {
|
|
719
|
+
const service = getQueueService();
|
|
720
|
+
return service.popAgentTaskFromQueue();
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// src/services/event_bus.ts
|
|
724
|
+
var EventBus = class {
|
|
725
|
+
constructor() {
|
|
726
|
+
this.emitter = new import_events.EventEmitter();
|
|
727
|
+
this.emitter.setMaxListeners(100);
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* 发布事件
|
|
731
|
+
* @param eventName 事件名称
|
|
732
|
+
* @param data 事件数据
|
|
733
|
+
*/
|
|
734
|
+
publish(eventName, data, useQueue = false) {
|
|
735
|
+
if (useQueue) {
|
|
736
|
+
pushAgentTaskToQueue3(data);
|
|
737
|
+
} else {
|
|
738
|
+
this.emitter.emit(eventName, data);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* 订阅事件
|
|
743
|
+
* @param eventName 事件名称
|
|
744
|
+
* @param callback 回调函数
|
|
745
|
+
*/
|
|
746
|
+
subscribe(eventName, callback) {
|
|
747
|
+
this.emitter.on(eventName, callback);
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* 取消订阅事件
|
|
751
|
+
* @param eventName 事件名称
|
|
752
|
+
* @param callback 回调函数
|
|
753
|
+
*/
|
|
754
|
+
unsubscribe(eventName, callback) {
|
|
755
|
+
this.emitter.off(eventName, callback);
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* 只订阅一次事件
|
|
759
|
+
* @param eventName 事件名称
|
|
760
|
+
* @param callback 回调函数
|
|
761
|
+
*/
|
|
762
|
+
subscribeOnce(eventName, callback) {
|
|
763
|
+
this.emitter.once(eventName, callback);
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
var eventBus = new EventBus();
|
|
767
|
+
var event_bus_default = eventBus;
|
|
768
|
+
|
|
769
|
+
// src/services/AgentManager.ts
|
|
770
|
+
var AgentManager = class _AgentManager {
|
|
771
|
+
constructor() {
|
|
772
|
+
this.agents = [];
|
|
773
|
+
}
|
|
774
|
+
static getInstance() {
|
|
775
|
+
if (!_AgentManager.instance) {
|
|
776
|
+
_AgentManager.instance = new _AgentManager();
|
|
777
|
+
}
|
|
778
|
+
return _AgentManager.instance;
|
|
779
|
+
}
|
|
780
|
+
callAgentInQueue(queue) {
|
|
781
|
+
return new Promise((resolve, reject) => {
|
|
782
|
+
try {
|
|
783
|
+
event_bus_default.publish(
|
|
784
|
+
AGENT_TASK_EVENT,
|
|
785
|
+
{
|
|
786
|
+
...queue
|
|
787
|
+
},
|
|
788
|
+
true
|
|
789
|
+
);
|
|
790
|
+
resolve(true);
|
|
791
|
+
} catch (error) {
|
|
792
|
+
reject(error);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
// src/controllers/agent_task.ts
|
|
799
|
+
var triggerAgentTask = async (request, reply) => {
|
|
800
|
+
try {
|
|
801
|
+
const { assistant_id, thread_id, input, command } = request.body;
|
|
802
|
+
const tenant_id = request.headers["x-tenant-id"];
|
|
803
|
+
if (!assistant_id) {
|
|
804
|
+
reply.status(400).send({
|
|
805
|
+
success: false,
|
|
806
|
+
error: "assistant_id is required"
|
|
807
|
+
});
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
if (!thread_id) {
|
|
811
|
+
reply.status(400).send({
|
|
812
|
+
success: false,
|
|
813
|
+
error: "thread_id is required"
|
|
814
|
+
});
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
const agentManager = AgentManager.getInstance();
|
|
818
|
+
const result = await agentManager.callAgentInQueue({
|
|
819
|
+
assistant_id,
|
|
820
|
+
thread_id,
|
|
821
|
+
input,
|
|
822
|
+
command,
|
|
823
|
+
"x-tenant-id": tenant_id
|
|
824
|
+
});
|
|
825
|
+
reply.status(200).send({
|
|
826
|
+
success: true
|
|
827
|
+
});
|
|
828
|
+
} catch (error) {
|
|
829
|
+
reply.status(500).send({
|
|
830
|
+
success: false,
|
|
831
|
+
error: `Failed to trigger agent task: ${error.message}`
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
|
|
537
836
|
// src/schemas/index.ts
|
|
538
837
|
var getAllMemoryItemsSchema = {
|
|
539
838
|
description: "Get all memory items for an assistant thread",
|
|
@@ -651,6 +950,57 @@ var getAgentGraphSchema = {
|
|
|
651
950
|
200: {}
|
|
652
951
|
}
|
|
653
952
|
};
|
|
953
|
+
var triggerAgentTaskSchema = {
|
|
954
|
+
description: "Trigger an agent task",
|
|
955
|
+
tags: ["Agent Tasks"],
|
|
956
|
+
summary: "Trigger Agent Task",
|
|
957
|
+
body: {
|
|
958
|
+
type: "object",
|
|
959
|
+
properties: {
|
|
960
|
+
assistant_id: {
|
|
961
|
+
type: "string",
|
|
962
|
+
description: "Assistant ID"
|
|
963
|
+
},
|
|
964
|
+
thread_id: {
|
|
965
|
+
type: "string",
|
|
966
|
+
description: "Thread ID"
|
|
967
|
+
},
|
|
968
|
+
input: {
|
|
969
|
+
type: "object",
|
|
970
|
+
description: "Task input data"
|
|
971
|
+
},
|
|
972
|
+
command: {
|
|
973
|
+
type: "object",
|
|
974
|
+
description: "Command data for the task",
|
|
975
|
+
nullable: true
|
|
976
|
+
}
|
|
977
|
+
},
|
|
978
|
+
required: ["assistant_id", "thread_id"]
|
|
979
|
+
},
|
|
980
|
+
response: {
|
|
981
|
+
200: {
|
|
982
|
+
type: "object",
|
|
983
|
+
properties: {
|
|
984
|
+
success: { type: "boolean" },
|
|
985
|
+
result: { type: "object" }
|
|
986
|
+
}
|
|
987
|
+
},
|
|
988
|
+
400: {
|
|
989
|
+
type: "object",
|
|
990
|
+
properties: {
|
|
991
|
+
success: { type: "boolean" },
|
|
992
|
+
error: { type: "string" }
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
500: {
|
|
996
|
+
type: "object",
|
|
997
|
+
properties: {
|
|
998
|
+
success: { type: "boolean" },
|
|
999
|
+
error: { type: "string" }
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
654
1004
|
|
|
655
1005
|
// src/routes/index.ts
|
|
656
1006
|
var registerLatticeRoutes = (app2) => {
|
|
@@ -691,6 +1041,11 @@ var registerLatticeRoutes = (app2) => {
|
|
|
691
1041
|
{ schema: getAgentGraphSchema },
|
|
692
1042
|
getAgentGraph
|
|
693
1043
|
);
|
|
1044
|
+
app2.post(
|
|
1045
|
+
"/api/agent-tasks/trigger",
|
|
1046
|
+
{ schema: triggerAgentTaskSchema },
|
|
1047
|
+
triggerAgentTask
|
|
1048
|
+
);
|
|
694
1049
|
};
|
|
695
1050
|
|
|
696
1051
|
// src/logger/Logger.ts
|
|
@@ -893,6 +1248,244 @@ var configureSwagger = async (app2, customSwaggerConfig, customSwaggerUiConfig)
|
|
|
893
1248
|
await app2.register(import_swagger_ui.default, swaggerUiConfig);
|
|
894
1249
|
};
|
|
895
1250
|
|
|
1251
|
+
// src/services/agent_task_consumer.ts
|
|
1252
|
+
var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
1253
|
+
const {
|
|
1254
|
+
assistant_id,
|
|
1255
|
+
input = {},
|
|
1256
|
+
thread_id,
|
|
1257
|
+
"x-tenant-id": tenant_id,
|
|
1258
|
+
command,
|
|
1259
|
+
callback_event
|
|
1260
|
+
} = taskRequest;
|
|
1261
|
+
try {
|
|
1262
|
+
console.log(
|
|
1263
|
+
`\u5F00\u59CB\u5904\u7406\u4EFB\u52A1 [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1264
|
+
);
|
|
1265
|
+
const apiUrl = AgentTaskConsumer.agent_run_endpoint;
|
|
1266
|
+
console.log(`apiUrl: ${apiUrl}`);
|
|
1267
|
+
const response = await fetch(apiUrl, {
|
|
1268
|
+
method: "POST",
|
|
1269
|
+
body: JSON.stringify({
|
|
1270
|
+
assistant_id,
|
|
1271
|
+
streaming: true,
|
|
1272
|
+
...input,
|
|
1273
|
+
thread_id,
|
|
1274
|
+
command
|
|
1275
|
+
}),
|
|
1276
|
+
headers: {
|
|
1277
|
+
"Content-Type": "application/json",
|
|
1278
|
+
"x-tenant-id": tenant_id
|
|
1279
|
+
}
|
|
1280
|
+
}).catch((err) => {
|
|
1281
|
+
console.error(`fetch\u8BF7\u6C42\u5931\u8D25: ${err.message || String(err)}`);
|
|
1282
|
+
throw new Error(`fetch\u5931\u8D25: ${err.message || String(err)}`);
|
|
1283
|
+
});
|
|
1284
|
+
if (!response.ok) {
|
|
1285
|
+
throw new Error(`API\u8BF7\u6C42\u5931\u8D25: ${response.status} ${response.statusText}`);
|
|
1286
|
+
}
|
|
1287
|
+
if (callback_event) {
|
|
1288
|
+
event_bus_default.publish(callback_event, {
|
|
1289
|
+
success: true,
|
|
1290
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
console.log(
|
|
1294
|
+
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1295
|
+
);
|
|
1296
|
+
return true;
|
|
1297
|
+
} catch (error) {
|
|
1298
|
+
console.error(
|
|
1299
|
+
`Agent\u4EFB\u52A1\u6267\u884C\u5931\u8D25: ${assistant_id}, \u7EBF\u7A0B: ${thread_id}`,
|
|
1300
|
+
error
|
|
1301
|
+
);
|
|
1302
|
+
const maxRetries = 0;
|
|
1303
|
+
if (retryCount < maxRetries) {
|
|
1304
|
+
const nextRetryCount = retryCount + 1;
|
|
1305
|
+
const delayMs = Math.pow(2, nextRetryCount) * 1e3;
|
|
1306
|
+
console.log(
|
|
1307
|
+
`\u5C06\u5728 ${delayMs}ms \u540E\u91CD\u8BD5\u4EFB\u52A1 (${nextRetryCount}/${maxRetries})`
|
|
1308
|
+
);
|
|
1309
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
1310
|
+
return handleAgentTask(taskRequest, nextRetryCount);
|
|
1311
|
+
}
|
|
1312
|
+
if (callback_event) {
|
|
1313
|
+
event_bus_default.publish(callback_event, {
|
|
1314
|
+
success: false,
|
|
1315
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1316
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
console.error(
|
|
1320
|
+
`\u4EFB\u52A1\u5904\u7406\u5931\u8D25\uFF0C\u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8BD5\u6B21\u6570 [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1321
|
+
);
|
|
1322
|
+
return false;
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
1326
|
+
constructor(gatewayPort, pollingIntervalMs) {
|
|
1327
|
+
this.isPolling = false;
|
|
1328
|
+
this.pollingInterval = null;
|
|
1329
|
+
this.pollingIntervalMs = 5e3;
|
|
1330
|
+
// 默认5秒轮询一次
|
|
1331
|
+
this.maxConcurrentTasks = 15;
|
|
1332
|
+
// 最大并发任务数
|
|
1333
|
+
this.activeTasks = 0;
|
|
1334
|
+
// 当前活跃的任务数
|
|
1335
|
+
this.processing = false;
|
|
1336
|
+
// 是否正在处理任务批次
|
|
1337
|
+
this.immediateProcessingEnabled = true;
|
|
1338
|
+
// 是否启用即时处理模式
|
|
1339
|
+
this.gatewayPort = 4001;
|
|
1340
|
+
this.gatewayPort = gatewayPort;
|
|
1341
|
+
_AgentTaskConsumer.agent_run_endpoint = `http://localhost:${this.gatewayPort}/api/runs`;
|
|
1342
|
+
if (pollingIntervalMs) {
|
|
1343
|
+
this.pollingIntervalMs = pollingIntervalMs;
|
|
1344
|
+
}
|
|
1345
|
+
this.initialize();
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* 初始化事件监听和队列轮询
|
|
1349
|
+
*/
|
|
1350
|
+
initialize() {
|
|
1351
|
+
event_bus_default.subscribe(AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
|
|
1352
|
+
this.startPollingQueue();
|
|
1353
|
+
console.log("Agent\u4EFB\u52A1\u6D88\u8D39\u8005\u5DF2\u542F\u52A8\u5E76\u76D1\u542C\u4EFB\u52A1\u4E8B\u4EF6\u548C\u961F\u5217");
|
|
1354
|
+
}
|
|
1355
|
+
/**
|
|
1356
|
+
* 启动队列轮询
|
|
1357
|
+
*/
|
|
1358
|
+
startPollingQueue() {
|
|
1359
|
+
if (this.isPolling) {
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
this.isPolling = true;
|
|
1363
|
+
this.pollingInterval = setInterval(async () => {
|
|
1364
|
+
try {
|
|
1365
|
+
if (this.processing) {
|
|
1366
|
+
console.log("\u961F\u5217\u5904\u7406\u4E2D\uFF0C\u8DF3\u8FC7\u672C\u6B21\u8F6E\u8BE2");
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
await this.consumeFromQueue();
|
|
1370
|
+
} catch (error) {
|
|
1371
|
+
console.error("\u961F\u5217\u8F6E\u8BE2\u51FA\u9519:", error);
|
|
1372
|
+
}
|
|
1373
|
+
}, this.pollingIntervalMs);
|
|
1374
|
+
console.log(
|
|
1375
|
+
`\u5F00\u59CB\u8F6E\u8BE2\u961F\u5217\uFF0C\u95F4\u9694: ${this.pollingIntervalMs}ms\uFF0C\u6700\u5927\u5E76\u53D1\u4EFB\u52A1\u6570: ${this.maxConcurrentTasks}`
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* 停止队列轮询
|
|
1380
|
+
*/
|
|
1381
|
+
stopPollingQueue() {
|
|
1382
|
+
if (this.pollingInterval) {
|
|
1383
|
+
clearInterval(this.pollingInterval);
|
|
1384
|
+
this.pollingInterval = null;
|
|
1385
|
+
this.isPolling = false;
|
|
1386
|
+
console.log("\u5DF2\u505C\u6B62\u961F\u5217\u8F6E\u8BE2");
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* 处理单个任务并在完成后立即尝试处理下一个
|
|
1391
|
+
*/
|
|
1392
|
+
async processNextTask() {
|
|
1393
|
+
try {
|
|
1394
|
+
const queueResult = await popAgentTaskFromQueue3();
|
|
1395
|
+
if (queueResult && queueResult.data) {
|
|
1396
|
+
const taskItem = queueResult.data;
|
|
1397
|
+
if (taskItem && typeof taskItem === "object") {
|
|
1398
|
+
const taskRequest = taskItem;
|
|
1399
|
+
console.log(
|
|
1400
|
+
`\u4ECE\u961F\u5217\u4E2D\u83B7\u53D6\u5230\u4EFB\u52A1 [assistant: ${taskRequest.assistant_id}, thread: ${taskRequest.thread_id}]`
|
|
1401
|
+
);
|
|
1402
|
+
this.activeTasks++;
|
|
1403
|
+
handleAgentTask(taskRequest).then((success) => {
|
|
1404
|
+
if (!success) {
|
|
1405
|
+
console.error(`\u4EFB\u52A1 \u5904\u7406\u5931\u8D25`);
|
|
1406
|
+
} else {
|
|
1407
|
+
console.log(`\u4EFB\u52A1 \u5904\u7406\u6210\u529F`);
|
|
1408
|
+
}
|
|
1409
|
+
}).catch((error) => {
|
|
1410
|
+
console.error(`\u4EFB\u52A1 \u5904\u7406\u65F6\u51FA\u9519:`, error);
|
|
1411
|
+
}).finally(() => {
|
|
1412
|
+
this.activeTasks--;
|
|
1413
|
+
if (this.immediateProcessingEnabled && !this.processing) {
|
|
1414
|
+
this.checkQueueForTasks();
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
return true;
|
|
1418
|
+
} else {
|
|
1419
|
+
console.log("\u961F\u5217\u4EFB\u52A1\u683C\u5F0F\u65E0\u6548:", taskItem);
|
|
1420
|
+
return false;
|
|
1421
|
+
}
|
|
1422
|
+
} else {
|
|
1423
|
+
return false;
|
|
1424
|
+
}
|
|
1425
|
+
} catch (error) {
|
|
1426
|
+
console.error("\u5904\u7406\u4EFB\u52A1\u5931\u8D25:", error);
|
|
1427
|
+
return false;
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* 检查队列中是否有任务并处理
|
|
1432
|
+
*/
|
|
1433
|
+
async checkQueueForTasks() {
|
|
1434
|
+
if (this.processing || this.activeTasks >= this.maxConcurrentTasks) {
|
|
1435
|
+
return;
|
|
1436
|
+
}
|
|
1437
|
+
this.processing = true;
|
|
1438
|
+
try {
|
|
1439
|
+
while (this.activeTasks < this.maxConcurrentTasks) {
|
|
1440
|
+
const taskProcessed = await this.processNextTask();
|
|
1441
|
+
if (!taskProcessed) {
|
|
1442
|
+
break;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
} catch (error) {
|
|
1446
|
+
console.error("\u68C0\u67E5\u961F\u5217\u4EFB\u52A1\u5931\u8D25:", error);
|
|
1447
|
+
} finally {
|
|
1448
|
+
this.processing = false;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* 从队列中消费任务
|
|
1453
|
+
*/
|
|
1454
|
+
async consumeFromQueue() {
|
|
1455
|
+
if (this.processing) {
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
await this.checkQueueForTasks();
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* 处理通过事件触发的任务
|
|
1462
|
+
*/
|
|
1463
|
+
trigger_agent_task(taskRequest) {
|
|
1464
|
+
console.log(
|
|
1465
|
+
`\u901A\u8FC7\u4E8B\u4EF6\u89E6\u53D1\u4EFB\u52A1: [assistant: ${taskRequest.assistant_id}, thread: ${taskRequest.thread_id}]`
|
|
1466
|
+
);
|
|
1467
|
+
handleAgentTask(taskRequest).catch((error) => {
|
|
1468
|
+
console.error("\u5904\u7406Agent\u4EFB\u52A1\u65F6\u53D1\u751F\u672A\u6355\u83B7\u7684\u9519\u8BEF:", error);
|
|
1469
|
+
if (taskRequest.callback_event) {
|
|
1470
|
+
event_bus_default.publish(taskRequest.callback_event, {
|
|
1471
|
+
success: false,
|
|
1472
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1473
|
+
config: {
|
|
1474
|
+
assistant_id: taskRequest.assistant_id,
|
|
1475
|
+
thread_id: taskRequest.thread_id,
|
|
1476
|
+
tenant_id: taskRequest["x-tenant-id"]
|
|
1477
|
+
}
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
});
|
|
1481
|
+
if (this.immediateProcessingEnabled && this.activeTasks < this.maxConcurrentTasks) {
|
|
1482
|
+
setImmediate(() => this.checkQueueForTasks());
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
};
|
|
1486
|
+
_AgentTaskConsumer.agent_run_endpoint = "http://localhost:4001/api/runs";
|
|
1487
|
+
var AgentTaskConsumer = _AgentTaskConsumer;
|
|
1488
|
+
|
|
896
1489
|
// src/index.ts
|
|
897
1490
|
process.on("unhandledRejection", (reason, promise) => {
|
|
898
1491
|
console.error("\u672A\u5904\u7406\u7684Promise\u62D2\u7EDD:", reason);
|
|
@@ -953,11 +1546,19 @@ app.setErrorHandler((error, request, reply) => {
|
|
|
953
1546
|
});
|
|
954
1547
|
});
|
|
955
1548
|
app.decorate("logger", logger);
|
|
956
|
-
var start = async (
|
|
1549
|
+
var start = async (config) => {
|
|
957
1550
|
try {
|
|
958
|
-
const target_port = port || Number(process.env.PORT) || 4001;
|
|
1551
|
+
const target_port = config?.port || Number(process.env.PORT) || 4001;
|
|
959
1552
|
await app.listen({ port: target_port, host: "0.0.0.0" });
|
|
960
|
-
logger.info(`Lattice Gateway is running on port: ${
|
|
1553
|
+
logger.info(`Lattice Gateway is running on port: ${target_port}`);
|
|
1554
|
+
const queueServiceConfig = config?.queueServiceConfig;
|
|
1555
|
+
if (queueServiceConfig) {
|
|
1556
|
+
setQueueServiceType(queueServiceConfig.type);
|
|
1557
|
+
if (queueServiceConfig.defaultStartPollingQueue) {
|
|
1558
|
+
const agentTaskConsumer = new AgentTaskConsumer(target_port);
|
|
1559
|
+
agentTaskConsumer.startPollingQueue();
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
961
1562
|
} catch (err) {
|
|
962
1563
|
logger.error("Server start failed", { error: err });
|
|
963
1564
|
process.exit(1);
|
|
@@ -967,7 +1568,8 @@ var LatticeGateway = {
|
|
|
967
1568
|
startAsHttpEndpoint: start,
|
|
968
1569
|
configureSwagger,
|
|
969
1570
|
registerLatticeRoutes,
|
|
970
|
-
app
|
|
1571
|
+
app,
|
|
1572
|
+
AgentTaskConsumer
|
|
971
1573
|
};
|
|
972
1574
|
// Annotate the CommonJS export names for ESM import in node:
|
|
973
1575
|
0 && (module.exports = {
|