@axiom-lattice/gateway 2.1.3 → 2.1.4
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 +8 -0
- package/dist/index.d.mts +65 -4
- package/dist/index.d.ts +65 -4
- package/dist/index.js +583 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +589 -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 +86 -0
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
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
|
+
|
|
1
7
|
// src/index.ts
|
|
2
8
|
import fastify from "fastify";
|
|
3
9
|
import cors from "@fastify/cors";
|
|
@@ -509,6 +515,282 @@ var getAgentGraph = async (request, reply) => {
|
|
|
509
515
|
}
|
|
510
516
|
};
|
|
511
517
|
|
|
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
|
+
// src/controllers/agent_task.ts
|
|
757
|
+
var triggerAgentTask = async (request, reply) => {
|
|
758
|
+
try {
|
|
759
|
+
const { assistant_id, thread_id, input, command } = request.body;
|
|
760
|
+
const tenant_id = request.headers["x-tenant-id"];
|
|
761
|
+
if (!assistant_id) {
|
|
762
|
+
reply.status(400).send({
|
|
763
|
+
success: false,
|
|
764
|
+
error: "assistant_id is required"
|
|
765
|
+
});
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if (!thread_id) {
|
|
769
|
+
reply.status(400).send({
|
|
770
|
+
success: false,
|
|
771
|
+
error: "thread_id is required"
|
|
772
|
+
});
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
const agentManager = AgentManager.getInstance();
|
|
776
|
+
const result = await agentManager.callAgentInQueue({
|
|
777
|
+
assistant_id,
|
|
778
|
+
thread_id,
|
|
779
|
+
input,
|
|
780
|
+
command,
|
|
781
|
+
"x-tenant-id": tenant_id
|
|
782
|
+
});
|
|
783
|
+
reply.status(200).send({
|
|
784
|
+
success: true
|
|
785
|
+
});
|
|
786
|
+
} catch (error) {
|
|
787
|
+
reply.status(500).send({
|
|
788
|
+
success: false,
|
|
789
|
+
error: `Failed to trigger agent task: ${error.message}`
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
|
|
512
794
|
// src/schemas/index.ts
|
|
513
795
|
var getAllMemoryItemsSchema = {
|
|
514
796
|
description: "Get all memory items for an assistant thread",
|
|
@@ -626,6 +908,57 @@ var getAgentGraphSchema = {
|
|
|
626
908
|
200: {}
|
|
627
909
|
}
|
|
628
910
|
};
|
|
911
|
+
var triggerAgentTaskSchema = {
|
|
912
|
+
description: "Trigger an agent task",
|
|
913
|
+
tags: ["Agent Tasks"],
|
|
914
|
+
summary: "Trigger Agent Task",
|
|
915
|
+
body: {
|
|
916
|
+
type: "object",
|
|
917
|
+
properties: {
|
|
918
|
+
assistant_id: {
|
|
919
|
+
type: "string",
|
|
920
|
+
description: "Assistant ID"
|
|
921
|
+
},
|
|
922
|
+
thread_id: {
|
|
923
|
+
type: "string",
|
|
924
|
+
description: "Thread ID"
|
|
925
|
+
},
|
|
926
|
+
input: {
|
|
927
|
+
type: "object",
|
|
928
|
+
description: "Task input data"
|
|
929
|
+
},
|
|
930
|
+
command: {
|
|
931
|
+
type: "object",
|
|
932
|
+
description: "Command data for the task",
|
|
933
|
+
nullable: true
|
|
934
|
+
}
|
|
935
|
+
},
|
|
936
|
+
required: ["assistant_id", "thread_id"]
|
|
937
|
+
},
|
|
938
|
+
response: {
|
|
939
|
+
200: {
|
|
940
|
+
type: "object",
|
|
941
|
+
properties: {
|
|
942
|
+
success: { type: "boolean" },
|
|
943
|
+
result: { type: "object" }
|
|
944
|
+
}
|
|
945
|
+
},
|
|
946
|
+
400: {
|
|
947
|
+
type: "object",
|
|
948
|
+
properties: {
|
|
949
|
+
success: { type: "boolean" },
|
|
950
|
+
error: { type: "string" }
|
|
951
|
+
}
|
|
952
|
+
},
|
|
953
|
+
500: {
|
|
954
|
+
type: "object",
|
|
955
|
+
properties: {
|
|
956
|
+
success: { type: "boolean" },
|
|
957
|
+
error: { type: "string" }
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
};
|
|
629
962
|
|
|
630
963
|
// src/routes/index.ts
|
|
631
964
|
var registerLatticeRoutes = (app2) => {
|
|
@@ -666,6 +999,11 @@ var registerLatticeRoutes = (app2) => {
|
|
|
666
999
|
{ schema: getAgentGraphSchema },
|
|
667
1000
|
getAgentGraph
|
|
668
1001
|
);
|
|
1002
|
+
app2.post(
|
|
1003
|
+
"/api/agent-tasks/trigger",
|
|
1004
|
+
{ schema: triggerAgentTaskSchema },
|
|
1005
|
+
triggerAgentTask
|
|
1006
|
+
);
|
|
669
1007
|
};
|
|
670
1008
|
|
|
671
1009
|
// src/logger/Logger.ts
|
|
@@ -868,6 +1206,244 @@ var configureSwagger = async (app2, customSwaggerConfig, customSwaggerUiConfig)
|
|
|
868
1206
|
await app2.register(swaggerUi, swaggerUiConfig);
|
|
869
1207
|
};
|
|
870
1208
|
|
|
1209
|
+
// src/services/agent_task_consumer.ts
|
|
1210
|
+
var handleAgentTask = async (taskRequest, retryCount = 0) => {
|
|
1211
|
+
const {
|
|
1212
|
+
assistant_id,
|
|
1213
|
+
input = {},
|
|
1214
|
+
thread_id,
|
|
1215
|
+
"x-tenant-id": tenant_id,
|
|
1216
|
+
command,
|
|
1217
|
+
callback_event
|
|
1218
|
+
} = taskRequest;
|
|
1219
|
+
try {
|
|
1220
|
+
console.log(
|
|
1221
|
+
`\u5F00\u59CB\u5904\u7406\u4EFB\u52A1 [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1222
|
+
);
|
|
1223
|
+
const apiUrl = AgentTaskConsumer.agent_run_endpoint;
|
|
1224
|
+
console.log(`apiUrl: ${apiUrl}`);
|
|
1225
|
+
const response = await fetch(apiUrl, {
|
|
1226
|
+
method: "POST",
|
|
1227
|
+
body: JSON.stringify({
|
|
1228
|
+
assistant_id,
|
|
1229
|
+
streaming: true,
|
|
1230
|
+
...input,
|
|
1231
|
+
thread_id,
|
|
1232
|
+
command
|
|
1233
|
+
}),
|
|
1234
|
+
headers: {
|
|
1235
|
+
"Content-Type": "application/json",
|
|
1236
|
+
"x-tenant-id": tenant_id
|
|
1237
|
+
}
|
|
1238
|
+
}).catch((err) => {
|
|
1239
|
+
console.error(`fetch\u8BF7\u6C42\u5931\u8D25: ${err.message || String(err)}`);
|
|
1240
|
+
throw new Error(`fetch\u5931\u8D25: ${err.message || String(err)}`);
|
|
1241
|
+
});
|
|
1242
|
+
if (!response.ok) {
|
|
1243
|
+
throw new Error(`API\u8BF7\u6C42\u5931\u8D25: ${response.status} ${response.statusText}`);
|
|
1244
|
+
}
|
|
1245
|
+
if (callback_event) {
|
|
1246
|
+
event_bus_default.publish(callback_event, {
|
|
1247
|
+
success: true,
|
|
1248
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
console.log(
|
|
1252
|
+
`\u4EFB\u52A1\u5904\u7406\u6210\u529F [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1253
|
+
);
|
|
1254
|
+
return true;
|
|
1255
|
+
} catch (error) {
|
|
1256
|
+
console.error(
|
|
1257
|
+
`Agent\u4EFB\u52A1\u6267\u884C\u5931\u8D25: ${assistant_id}, \u7EBF\u7A0B: ${thread_id}`,
|
|
1258
|
+
error
|
|
1259
|
+
);
|
|
1260
|
+
const maxRetries = 0;
|
|
1261
|
+
if (retryCount < maxRetries) {
|
|
1262
|
+
const nextRetryCount = retryCount + 1;
|
|
1263
|
+
const delayMs = Math.pow(2, nextRetryCount) * 1e3;
|
|
1264
|
+
console.log(
|
|
1265
|
+
`\u5C06\u5728 ${delayMs}ms \u540E\u91CD\u8BD5\u4EFB\u52A1 (${nextRetryCount}/${maxRetries})`
|
|
1266
|
+
);
|
|
1267
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
1268
|
+
return handleAgentTask(taskRequest, nextRetryCount);
|
|
1269
|
+
}
|
|
1270
|
+
if (callback_event) {
|
|
1271
|
+
event_bus_default.publish(callback_event, {
|
|
1272
|
+
success: false,
|
|
1273
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1274
|
+
config: { assistant_id, thread_id, tenant_id }
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
console.error(
|
|
1278
|
+
`\u4EFB\u52A1\u5904\u7406\u5931\u8D25\uFF0C\u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8BD5\u6B21\u6570 [assistant_id: ${assistant_id}, thread_id: ${thread_id}]`
|
|
1279
|
+
);
|
|
1280
|
+
return false;
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
var _AgentTaskConsumer = class _AgentTaskConsumer {
|
|
1284
|
+
constructor(gatewayPort, pollingIntervalMs) {
|
|
1285
|
+
this.isPolling = false;
|
|
1286
|
+
this.pollingInterval = null;
|
|
1287
|
+
this.pollingIntervalMs = 5e3;
|
|
1288
|
+
// 默认5秒轮询一次
|
|
1289
|
+
this.maxConcurrentTasks = 15;
|
|
1290
|
+
// 最大并发任务数
|
|
1291
|
+
this.activeTasks = 0;
|
|
1292
|
+
// 当前活跃的任务数
|
|
1293
|
+
this.processing = false;
|
|
1294
|
+
// 是否正在处理任务批次
|
|
1295
|
+
this.immediateProcessingEnabled = true;
|
|
1296
|
+
// 是否启用即时处理模式
|
|
1297
|
+
this.gatewayPort = 4001;
|
|
1298
|
+
this.gatewayPort = gatewayPort;
|
|
1299
|
+
_AgentTaskConsumer.agent_run_endpoint = `http://localhost:${this.gatewayPort}/api/runs`;
|
|
1300
|
+
if (pollingIntervalMs) {
|
|
1301
|
+
this.pollingIntervalMs = pollingIntervalMs;
|
|
1302
|
+
}
|
|
1303
|
+
this.initialize();
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* 初始化事件监听和队列轮询
|
|
1307
|
+
*/
|
|
1308
|
+
initialize() {
|
|
1309
|
+
event_bus_default.subscribe(AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
|
|
1310
|
+
this.startPollingQueue();
|
|
1311
|
+
console.log("Agent\u4EFB\u52A1\u6D88\u8D39\u8005\u5DF2\u542F\u52A8\u5E76\u76D1\u542C\u4EFB\u52A1\u4E8B\u4EF6\u548C\u961F\u5217");
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* 启动队列轮询
|
|
1315
|
+
*/
|
|
1316
|
+
startPollingQueue() {
|
|
1317
|
+
if (this.isPolling) {
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
this.isPolling = true;
|
|
1321
|
+
this.pollingInterval = setInterval(async () => {
|
|
1322
|
+
try {
|
|
1323
|
+
if (this.processing) {
|
|
1324
|
+
console.log("\u961F\u5217\u5904\u7406\u4E2D\uFF0C\u8DF3\u8FC7\u672C\u6B21\u8F6E\u8BE2");
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
await this.consumeFromQueue();
|
|
1328
|
+
} catch (error) {
|
|
1329
|
+
console.error("\u961F\u5217\u8F6E\u8BE2\u51FA\u9519:", error);
|
|
1330
|
+
}
|
|
1331
|
+
}, this.pollingIntervalMs);
|
|
1332
|
+
console.log(
|
|
1333
|
+
`\u5F00\u59CB\u8F6E\u8BE2\u961F\u5217\uFF0C\u95F4\u9694: ${this.pollingIntervalMs}ms\uFF0C\u6700\u5927\u5E76\u53D1\u4EFB\u52A1\u6570: ${this.maxConcurrentTasks}`
|
|
1334
|
+
);
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* 停止队列轮询
|
|
1338
|
+
*/
|
|
1339
|
+
stopPollingQueue() {
|
|
1340
|
+
if (this.pollingInterval) {
|
|
1341
|
+
clearInterval(this.pollingInterval);
|
|
1342
|
+
this.pollingInterval = null;
|
|
1343
|
+
this.isPolling = false;
|
|
1344
|
+
console.log("\u5DF2\u505C\u6B62\u961F\u5217\u8F6E\u8BE2");
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* 处理单个任务并在完成后立即尝试处理下一个
|
|
1349
|
+
*/
|
|
1350
|
+
async processNextTask() {
|
|
1351
|
+
try {
|
|
1352
|
+
const queueResult = await popAgentTaskFromQueue3();
|
|
1353
|
+
if (queueResult && queueResult.data) {
|
|
1354
|
+
const taskItem = queueResult.data;
|
|
1355
|
+
if (taskItem && typeof taskItem === "object") {
|
|
1356
|
+
const taskRequest = taskItem;
|
|
1357
|
+
console.log(
|
|
1358
|
+
`\u4ECE\u961F\u5217\u4E2D\u83B7\u53D6\u5230\u4EFB\u52A1 [assistant: ${taskRequest.assistant_id}, thread: ${taskRequest.thread_id}]`
|
|
1359
|
+
);
|
|
1360
|
+
this.activeTasks++;
|
|
1361
|
+
handleAgentTask(taskRequest).then((success) => {
|
|
1362
|
+
if (!success) {
|
|
1363
|
+
console.error(`\u4EFB\u52A1 \u5904\u7406\u5931\u8D25`);
|
|
1364
|
+
} else {
|
|
1365
|
+
console.log(`\u4EFB\u52A1 \u5904\u7406\u6210\u529F`);
|
|
1366
|
+
}
|
|
1367
|
+
}).catch((error) => {
|
|
1368
|
+
console.error(`\u4EFB\u52A1 \u5904\u7406\u65F6\u51FA\u9519:`, error);
|
|
1369
|
+
}).finally(() => {
|
|
1370
|
+
this.activeTasks--;
|
|
1371
|
+
if (this.immediateProcessingEnabled && !this.processing) {
|
|
1372
|
+
this.checkQueueForTasks();
|
|
1373
|
+
}
|
|
1374
|
+
});
|
|
1375
|
+
return true;
|
|
1376
|
+
} else {
|
|
1377
|
+
console.log("\u961F\u5217\u4EFB\u52A1\u683C\u5F0F\u65E0\u6548:", taskItem);
|
|
1378
|
+
return false;
|
|
1379
|
+
}
|
|
1380
|
+
} else {
|
|
1381
|
+
return false;
|
|
1382
|
+
}
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
console.error("\u5904\u7406\u4EFB\u52A1\u5931\u8D25:", error);
|
|
1385
|
+
return false;
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
/**
|
|
1389
|
+
* 检查队列中是否有任务并处理
|
|
1390
|
+
*/
|
|
1391
|
+
async checkQueueForTasks() {
|
|
1392
|
+
if (this.processing || this.activeTasks >= this.maxConcurrentTasks) {
|
|
1393
|
+
return;
|
|
1394
|
+
}
|
|
1395
|
+
this.processing = true;
|
|
1396
|
+
try {
|
|
1397
|
+
while (this.activeTasks < this.maxConcurrentTasks) {
|
|
1398
|
+
const taskProcessed = await this.processNextTask();
|
|
1399
|
+
if (!taskProcessed) {
|
|
1400
|
+
break;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
} catch (error) {
|
|
1404
|
+
console.error("\u68C0\u67E5\u961F\u5217\u4EFB\u52A1\u5931\u8D25:", error);
|
|
1405
|
+
} finally {
|
|
1406
|
+
this.processing = false;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
/**
|
|
1410
|
+
* 从队列中消费任务
|
|
1411
|
+
*/
|
|
1412
|
+
async consumeFromQueue() {
|
|
1413
|
+
if (this.processing) {
|
|
1414
|
+
return;
|
|
1415
|
+
}
|
|
1416
|
+
await this.checkQueueForTasks();
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* 处理通过事件触发的任务
|
|
1420
|
+
*/
|
|
1421
|
+
trigger_agent_task(taskRequest) {
|
|
1422
|
+
console.log(
|
|
1423
|
+
`\u901A\u8FC7\u4E8B\u4EF6\u89E6\u53D1\u4EFB\u52A1: [assistant: ${taskRequest.assistant_id}, thread: ${taskRequest.thread_id}]`
|
|
1424
|
+
);
|
|
1425
|
+
handleAgentTask(taskRequest).catch((error) => {
|
|
1426
|
+
console.error("\u5904\u7406Agent\u4EFB\u52A1\u65F6\u53D1\u751F\u672A\u6355\u83B7\u7684\u9519\u8BEF:", error);
|
|
1427
|
+
if (taskRequest.callback_event) {
|
|
1428
|
+
event_bus_default.publish(taskRequest.callback_event, {
|
|
1429
|
+
success: false,
|
|
1430
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1431
|
+
config: {
|
|
1432
|
+
assistant_id: taskRequest.assistant_id,
|
|
1433
|
+
thread_id: taskRequest.thread_id,
|
|
1434
|
+
tenant_id: taskRequest["x-tenant-id"]
|
|
1435
|
+
}
|
|
1436
|
+
});
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
if (this.immediateProcessingEnabled && this.activeTasks < this.maxConcurrentTasks) {
|
|
1440
|
+
setImmediate(() => this.checkQueueForTasks());
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
_AgentTaskConsumer.agent_run_endpoint = "http://localhost:4001/api/runs";
|
|
1445
|
+
var AgentTaskConsumer = _AgentTaskConsumer;
|
|
1446
|
+
|
|
871
1447
|
// src/index.ts
|
|
872
1448
|
process.on("unhandledRejection", (reason, promise) => {
|
|
873
1449
|
console.error("\u672A\u5904\u7406\u7684Promise\u62D2\u7EDD:", reason);
|
|
@@ -928,11 +1504,19 @@ app.setErrorHandler((error, request, reply) => {
|
|
|
928
1504
|
});
|
|
929
1505
|
});
|
|
930
1506
|
app.decorate("logger", logger);
|
|
931
|
-
var start = async (
|
|
1507
|
+
var start = async (config) => {
|
|
932
1508
|
try {
|
|
933
|
-
const target_port = port || Number(process.env.PORT) || 4001;
|
|
1509
|
+
const target_port = config?.port || Number(process.env.PORT) || 4001;
|
|
934
1510
|
await app.listen({ port: target_port, host: "0.0.0.0" });
|
|
935
|
-
logger.info(`Lattice Gateway is running on port: ${
|
|
1511
|
+
logger.info(`Lattice Gateway is running on port: ${target_port}`);
|
|
1512
|
+
const queueServiceConfig = config?.queueServiceConfig;
|
|
1513
|
+
if (queueServiceConfig) {
|
|
1514
|
+
setQueueServiceType(queueServiceConfig.type);
|
|
1515
|
+
if (queueServiceConfig.defaultStartPollingQueue) {
|
|
1516
|
+
const agentTaskConsumer = new AgentTaskConsumer(target_port);
|
|
1517
|
+
agentTaskConsumer.startPollingQueue();
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
936
1520
|
} catch (err) {
|
|
937
1521
|
logger.error("Server start failed", { error: err });
|
|
938
1522
|
process.exit(1);
|
|
@@ -942,7 +1526,8 @@ var LatticeGateway = {
|
|
|
942
1526
|
startAsHttpEndpoint: start,
|
|
943
1527
|
configureSwagger,
|
|
944
1528
|
registerLatticeRoutes,
|
|
945
|
-
app
|
|
1529
|
+
app,
|
|
1530
|
+
AgentTaskConsumer
|
|
946
1531
|
};
|
|
947
1532
|
export {
|
|
948
1533
|
LatticeGateway
|