@axiom-lattice/gateway 2.1.80 → 2.1.82
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/CHANGELOG.md +16 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +110 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -27
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +3 -2
- package/src/router/MessageRouter.ts +83 -18
- package/src/router/middlewares/deduplication.ts +19 -6
- package/src/router/middlewares/rateLimit.ts +8 -0
- package/src/routes/index.ts +6 -1
- package/.turbo/turbo-build.log +0 -32
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @axiom-lattice/gateway
|
|
2
2
|
|
|
3
|
+
## 2.1.82
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- dc6643c: add log
|
|
8
|
+
|
|
9
|
+
## 2.1.81
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 919961e: add log
|
|
14
|
+
- Updated dependencies [919961e]
|
|
15
|
+
- @axiom-lattice/core@2.1.71
|
|
16
|
+
- @axiom-lattice/agent-eval@2.1.65
|
|
17
|
+
- @axiom-lattice/pg-stores@1.0.61
|
|
18
|
+
|
|
3
19
|
## 2.1.80
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -67,12 +67,14 @@ interface MessageRouterConfig {
|
|
|
67
67
|
bindingRegistry: BindingRegistry;
|
|
68
68
|
adapterRegistry: ChannelAdapterRegistry;
|
|
69
69
|
installationStore: ChannelInstallationStore;
|
|
70
|
+
logger?: any;
|
|
70
71
|
}
|
|
71
72
|
declare class MessageRouter {
|
|
72
73
|
private middlewares;
|
|
73
74
|
private bindingRegistry;
|
|
74
75
|
private adapterRegistry;
|
|
75
76
|
private installationStore;
|
|
77
|
+
private logger?;
|
|
76
78
|
constructor(config: MessageRouterConfig);
|
|
77
79
|
use(middleware: MessageMiddleware): void;
|
|
78
80
|
dispatch(message: InboundMessage): Promise<DispatchResult>;
|
package/dist/index.d.ts
CHANGED
|
@@ -67,12 +67,14 @@ interface MessageRouterConfig {
|
|
|
67
67
|
bindingRegistry: BindingRegistry;
|
|
68
68
|
adapterRegistry: ChannelAdapterRegistry;
|
|
69
69
|
installationStore: ChannelInstallationStore;
|
|
70
|
+
logger?: any;
|
|
70
71
|
}
|
|
71
72
|
declare class MessageRouter {
|
|
72
73
|
private middlewares;
|
|
73
74
|
private bindingRegistry;
|
|
74
75
|
private adapterRegistry;
|
|
75
76
|
private installationStore;
|
|
77
|
+
private logger?;
|
|
76
78
|
constructor(config: MessageRouterConfig);
|
|
77
79
|
use(middleware: MessageMiddleware): void;
|
|
78
80
|
dispatch(message: InboundMessage): Promise<DispatchResult>;
|
package/dist/index.js
CHANGED
|
@@ -6642,7 +6642,12 @@ var registerLatticeRoutes = (app2, channelDeps) => {
|
|
|
6642
6642
|
replyTarget: msg.replyTarget
|
|
6643
6643
|
};
|
|
6644
6644
|
await router.dispatch(inboundMessage).catch((error) => {
|
|
6645
|
-
console.error(
|
|
6645
|
+
console.error(JSON.stringify({
|
|
6646
|
+
event: "inbound:dispatch_error",
|
|
6647
|
+
error: error instanceof Error ? error.message : String(error),
|
|
6648
|
+
channel: inboundMessage.channel,
|
|
6649
|
+
senderId: inboundMessage.sender.id
|
|
6650
|
+
}));
|
|
6646
6651
|
});
|
|
6647
6652
|
reply.status(200).send({ accepted: true });
|
|
6648
6653
|
} catch (error) {
|
|
@@ -6697,6 +6702,7 @@ var MessageRouter = class {
|
|
|
6697
6702
|
this.bindingRegistry = config.bindingRegistry;
|
|
6698
6703
|
this.adapterRegistry = config.adapterRegistry;
|
|
6699
6704
|
this.installationStore = config.installationStore;
|
|
6705
|
+
this.logger = config.logger;
|
|
6700
6706
|
}
|
|
6701
6707
|
use(middleware) {
|
|
6702
6708
|
this.middlewares.push(middleware);
|
|
@@ -6710,10 +6716,18 @@ var MessageRouter = class {
|
|
|
6710
6716
|
await this.runMiddlewares(ctx, async () => {
|
|
6711
6717
|
const tenantId = message.tenantId || (await this.installationStore.getInstallationById(message.channelInstallationId))?.tenantId;
|
|
6712
6718
|
if (!tenantId) {
|
|
6719
|
+
this.logger?.error({
|
|
6720
|
+
event: "dispatch:error",
|
|
6721
|
+
error: "tenantId missing",
|
|
6722
|
+
channel: message.channel,
|
|
6723
|
+
channelInstallationId: message.channelInstallationId,
|
|
6724
|
+
senderId: message.sender.id
|
|
6725
|
+
}, "tenantId is required");
|
|
6713
6726
|
throw new Error(
|
|
6714
6727
|
"tenantId is required: provide it in the message or ensure the channelInstallation has a tenantId"
|
|
6715
6728
|
);
|
|
6716
6729
|
}
|
|
6730
|
+
this.logger?.info({ event: "dispatch:start", channel: message.channel, senderId: message.sender.id, tenantId }, "Message dispatch started");
|
|
6717
6731
|
let binding = await this.bindingRegistry.resolve({
|
|
6718
6732
|
channel: message.channel,
|
|
6719
6733
|
senderId: message.sender.id,
|
|
@@ -6725,11 +6739,24 @@ var MessageRouter = class {
|
|
|
6725
6739
|
message.channelInstallationId
|
|
6726
6740
|
);
|
|
6727
6741
|
if (installation?.rejectWhenNoBinding) {
|
|
6742
|
+
this.logger?.warn({
|
|
6743
|
+
event: "dispatch:no_binding",
|
|
6744
|
+
channel: message.channel,
|
|
6745
|
+
senderId: message.sender.id,
|
|
6746
|
+
tenantId,
|
|
6747
|
+
channelInstallationId: message.channelInstallationId
|
|
6748
|
+
}, "No binding found and rejectWhenNoBinding is enabled");
|
|
6728
6749
|
throw new BindingNotFoundError(
|
|
6729
6750
|
`No binding for sender "${message.sender.id}" on channel "${message.channel}"`
|
|
6730
6751
|
);
|
|
6731
6752
|
}
|
|
6732
6753
|
if (installation?.fallbackAgentId) {
|
|
6754
|
+
this.logger?.warn({
|
|
6755
|
+
event: "dispatch:fallback",
|
|
6756
|
+
channel: message.channel,
|
|
6757
|
+
senderId: message.sender.id,
|
|
6758
|
+
fallbackAgentId: installation.fallbackAgentId
|
|
6759
|
+
}, "No binding found, falling back to fallbackAgentId");
|
|
6733
6760
|
binding = {
|
|
6734
6761
|
id: "fallback",
|
|
6735
6762
|
channel: message.channel,
|
|
@@ -6744,13 +6771,41 @@ var MessageRouter = class {
|
|
|
6744
6771
|
updatedAt: /* @__PURE__ */ new Date()
|
|
6745
6772
|
};
|
|
6746
6773
|
} else {
|
|
6774
|
+
this.logger?.error({
|
|
6775
|
+
event: "dispatch:no_fallback",
|
|
6776
|
+
channel: message.channel,
|
|
6777
|
+
senderId: message.sender.id,
|
|
6778
|
+
tenantId
|
|
6779
|
+
}, "No binding found and no fallbackAgentId configured");
|
|
6747
6780
|
throw new BindingNotFoundError(
|
|
6748
6781
|
`No binding for sender "${message.sender.id}" and no fallback configured`
|
|
6749
6782
|
);
|
|
6750
6783
|
}
|
|
6751
6784
|
}
|
|
6752
6785
|
ctx.binding = binding;
|
|
6786
|
+
this.logger?.info({
|
|
6787
|
+
event: "dispatch:binding",
|
|
6788
|
+
bindingId: binding.id,
|
|
6789
|
+
agentId: binding.agentId,
|
|
6790
|
+
threadId: binding.threadId,
|
|
6791
|
+
threadMode: binding.threadMode,
|
|
6792
|
+
workspaceId: binding.workspaceId,
|
|
6793
|
+
projectId: binding.projectId
|
|
6794
|
+
}, "Binding resolved");
|
|
6795
|
+
if (binding.threadMode === "per_conversation") {
|
|
6796
|
+
this.logger?.warn({
|
|
6797
|
+
event: "dispatch:per_conversation",
|
|
6798
|
+
bindingId: binding.id,
|
|
6799
|
+
conversationId: message.conversation?.id
|
|
6800
|
+
}, "per_conversation mode active \u2014 thread lookup by conversation not yet implemented, using binding.threadId");
|
|
6801
|
+
}
|
|
6753
6802
|
if (!binding.enabled) {
|
|
6803
|
+
this.logger?.warn({
|
|
6804
|
+
event: "dispatch:binding_disabled",
|
|
6805
|
+
bindingId: binding.id,
|
|
6806
|
+
agentId: binding.agentId,
|
|
6807
|
+
senderId: message.sender.id
|
|
6808
|
+
}, "Binding is disabled, rejecting message");
|
|
6754
6809
|
throw new BindingNotFoundError(
|
|
6755
6810
|
`Binding for sender "${message.sender.id}" is disabled`
|
|
6756
6811
|
);
|
|
@@ -6759,6 +6814,12 @@ var MessageRouter = class {
|
|
|
6759
6814
|
if (!threadId) {
|
|
6760
6815
|
const threadStore = (0, import_core28.getStoreLattice)("default", "thread").store;
|
|
6761
6816
|
const newThreadId = (0, import_crypto8.randomUUID)();
|
|
6817
|
+
this.logger?.info({
|
|
6818
|
+
event: "dispatch:thread:create",
|
|
6819
|
+
agentId: ctx.binding.agentId,
|
|
6820
|
+
newThreadId,
|
|
6821
|
+
tenantId
|
|
6822
|
+
}, "Creating new thread for binding");
|
|
6762
6823
|
const newThread = await threadStore.createThread(
|
|
6763
6824
|
tenantId,
|
|
6764
6825
|
ctx.binding.agentId,
|
|
@@ -6780,6 +6841,14 @@ var MessageRouter = class {
|
|
|
6780
6841
|
ctx.binding.threadId = threadId;
|
|
6781
6842
|
}
|
|
6782
6843
|
}
|
|
6844
|
+
this.logger?.info({
|
|
6845
|
+
event: "dispatch:agent",
|
|
6846
|
+
agentId: ctx.binding.agentId,
|
|
6847
|
+
threadId,
|
|
6848
|
+
threadMode: ctx.binding.threadMode,
|
|
6849
|
+
senderId: message.sender.id,
|
|
6850
|
+
contentLength: message.content.text.length
|
|
6851
|
+
}, "Dispatching to agent");
|
|
6783
6852
|
const agent = import_core28.agentInstanceManager.getAgent({
|
|
6784
6853
|
tenant_id: tenantId,
|
|
6785
6854
|
assistant_id: ctx.binding.agentId,
|
|
@@ -6787,10 +6856,17 @@ var MessageRouter = class {
|
|
|
6787
6856
|
workspace_id: ctx.binding.workspaceId || "",
|
|
6788
6857
|
project_id: ctx.binding.projectId || ""
|
|
6789
6858
|
});
|
|
6790
|
-
const
|
|
6791
|
-
input: { message: message.content.text }
|
|
6859
|
+
const addResult = await agent.addMessage({
|
|
6860
|
+
input: { message: message.content.text },
|
|
6861
|
+
custom_run_config: message.content.metadata || {}
|
|
6792
6862
|
});
|
|
6793
|
-
|
|
6863
|
+
this.logger?.info({
|
|
6864
|
+
event: "dispatch:complete",
|
|
6865
|
+
agentId: ctx.binding.agentId,
|
|
6866
|
+
threadId,
|
|
6867
|
+
messageId: addResult?.messageId,
|
|
6868
|
+
result: JSON.stringify(addResult)
|
|
6869
|
+
}, "Agent dispatch complete \u2014 messageId = " + (addResult?.messageId || "N/A"));
|
|
6794
6870
|
if (message.replyTarget) {
|
|
6795
6871
|
const adapter = this.adapterRegistry.get(message.replyTarget.adapterChannel);
|
|
6796
6872
|
if (adapter) {
|
|
@@ -6800,7 +6876,7 @@ var MessageRouter = class {
|
|
|
6800
6876
|
if (installation) {
|
|
6801
6877
|
await adapter.sendReply(
|
|
6802
6878
|
message.replyTarget,
|
|
6803
|
-
{ text: ctx.result },
|
|
6879
|
+
{ text: ctx.result || "" },
|
|
6804
6880
|
installation
|
|
6805
6881
|
);
|
|
6806
6882
|
}
|
|
@@ -6815,6 +6891,7 @@ var MessageRouter = class {
|
|
|
6815
6891
|
};
|
|
6816
6892
|
} catch (error) {
|
|
6817
6893
|
ctx.error = error instanceof Error ? error : new Error(String(error));
|
|
6894
|
+
this.logger?.error({ event: "dispatch:error", error: ctx.error.message, channel: message.channel, senderId: message.sender.id }, "Message dispatch failed");
|
|
6818
6895
|
return {
|
|
6819
6896
|
success: false,
|
|
6820
6897
|
bindingId: ctx.binding?.id,
|
|
@@ -6834,18 +6911,6 @@ var MessageRouter = class {
|
|
|
6834
6911
|
return dispatch(0);
|
|
6835
6912
|
}
|
|
6836
6913
|
};
|
|
6837
|
-
function extractTextFromInvokeResult(result) {
|
|
6838
|
-
if (result && typeof result === "object" && "messages" in result) {
|
|
6839
|
-
const messages = result.messages;
|
|
6840
|
-
if (Array.isArray(messages)) {
|
|
6841
|
-
const aiMessages = messages.filter((m) => m.role === "ai");
|
|
6842
|
-
if (aiMessages.length > 0) {
|
|
6843
|
-
return aiMessages.map((m) => m.content).join("\n");
|
|
6844
|
-
}
|
|
6845
|
-
}
|
|
6846
|
-
}
|
|
6847
|
-
return JSON.stringify(result);
|
|
6848
|
-
}
|
|
6849
6914
|
|
|
6850
6915
|
// src/channels/registry.ts
|
|
6851
6916
|
var ChannelAdapterRegistry = class {
|
|
@@ -6868,15 +6933,25 @@ var ChannelAdapterRegistry = class {
|
|
|
6868
6933
|
|
|
6869
6934
|
// src/router/middlewares/deduplication.ts
|
|
6870
6935
|
var processedMessages = /* @__PURE__ */ new Map();
|
|
6871
|
-
function createDeduplicationMiddleware(ttlMs = 5 * 60 * 1e3) {
|
|
6936
|
+
function createDeduplicationMiddleware(ttlMs = 5 * 60 * 1e3, logger4) {
|
|
6872
6937
|
return async (ctx, next) => {
|
|
6873
6938
|
const msg = ctx.inboundMessage;
|
|
6874
6939
|
const msgId = msg.content.metadata?.messageId;
|
|
6875
|
-
const key = msgId ? `${msg.channel}:${msg.channelInstallationId}:${msgId}` :
|
|
6876
|
-
|
|
6877
|
-
|
|
6878
|
-
|
|
6879
|
-
|
|
6940
|
+
const key = msgId ? `${msg.channel}:${msg.channelInstallationId}:${msgId}` : null;
|
|
6941
|
+
if (key) {
|
|
6942
|
+
const now = Date.now();
|
|
6943
|
+
const lastProcessed = processedMessages.get(key);
|
|
6944
|
+
if (lastProcessed && now - lastProcessed < ttlMs) {
|
|
6945
|
+
logger4?.warn({
|
|
6946
|
+
event: "dedup:blocked",
|
|
6947
|
+
channel: msg.channel,
|
|
6948
|
+
senderId: msg.sender.id,
|
|
6949
|
+
messageId: msgId
|
|
6950
|
+
}, "Duplicate message blocked by deduplication");
|
|
6951
|
+
return;
|
|
6952
|
+
}
|
|
6953
|
+
processedMessages.set(key, now);
|
|
6954
|
+
}
|
|
6880
6955
|
if (processedMessages.size > 1e4) {
|
|
6881
6956
|
const oldest = Array.from(processedMessages.entries()).sort((a, b) => a[1] - b[1])[0];
|
|
6882
6957
|
if (oldest) processedMessages.delete(oldest[0]);
|
|
@@ -6893,7 +6968,7 @@ var RateLimitError = class extends Error {
|
|
|
6893
6968
|
}
|
|
6894
6969
|
};
|
|
6895
6970
|
var rateCounters = /* @__PURE__ */ new Map();
|
|
6896
|
-
function createRateLimitMiddleware(maxRequests = 10, windowMs = 60 * 1e3, maxEntries = 1e4) {
|
|
6971
|
+
function createRateLimitMiddleware(maxRequests = 10, windowMs = 60 * 1e3, maxEntries = 1e4, logger4) {
|
|
6897
6972
|
return async (ctx, next) => {
|
|
6898
6973
|
const senderKey = `${ctx.inboundMessage.channel}:${ctx.inboundMessage.sender.id}`;
|
|
6899
6974
|
const now = Date.now();
|
|
@@ -6908,6 +6983,13 @@ function createRateLimitMiddleware(maxRequests = 10, windowMs = 60 * 1e3, maxEnt
|
|
|
6908
6983
|
if (oldest) rateCounters.delete(oldest[0]);
|
|
6909
6984
|
}
|
|
6910
6985
|
if (counter.count > maxRequests) {
|
|
6986
|
+
logger4?.warn({
|
|
6987
|
+
event: "ratelimit:blocked",
|
|
6988
|
+
channel: ctx.inboundMessage.channel,
|
|
6989
|
+
senderId: ctx.inboundMessage.sender.id,
|
|
6990
|
+
count: counter.count,
|
|
6991
|
+
maxRequests
|
|
6992
|
+
}, "Rate limit exceeded");
|
|
6911
6993
|
throw new RateLimitError(`Rate limit exceeded`);
|
|
6912
6994
|
}
|
|
6913
6995
|
await next();
|
|
@@ -7431,13 +7513,14 @@ var start = async (config) => {
|
|
|
7431
7513
|
adapterRegistry.register(larkChannelAdapter);
|
|
7432
7514
|
const router = new MessageRouter({
|
|
7433
7515
|
middlewares: [
|
|
7434
|
-
createDeduplicationMiddleware(),
|
|
7435
|
-
createRateLimitMiddleware(),
|
|
7516
|
+
createDeduplicationMiddleware(void 0, logger3),
|
|
7517
|
+
createRateLimitMiddleware(void 0, void 0, void 0, logger3),
|
|
7436
7518
|
createAuditLoggerMiddleware()
|
|
7437
7519
|
],
|
|
7438
7520
|
bindingRegistry: bindingStore,
|
|
7439
7521
|
adapterRegistry,
|
|
7440
|
-
installationStore
|
|
7522
|
+
installationStore,
|
|
7523
|
+
logger: logger3
|
|
7441
7524
|
});
|
|
7442
7525
|
channelDeps = { router, installationStore };
|
|
7443
7526
|
} catch {
|