@lobu/gateway 3.0.5 → 3.0.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/package.json +2 -2
- package/src/__tests__/agent-config-routes.test.ts +254 -0
- package/src/__tests__/agent-history-routes.test.ts +72 -0
- package/src/__tests__/agent-routes.test.ts +68 -0
- package/src/__tests__/agent-schedules-routes.test.ts +59 -0
- package/src/__tests__/agent-settings-store.test.ts +323 -0
- package/src/__tests__/chat-instance-manager-slack.test.ts +204 -0
- package/src/__tests__/chat-response-bridge.test.ts +131 -0
- package/src/__tests__/config-memory-plugins.test.ts +92 -0
- package/src/__tests__/config-request-store.test.ts +127 -0
- package/src/__tests__/connection-routes.test.ts +144 -0
- package/src/__tests__/core-services-store-selection.test.ts +92 -0
- package/src/__tests__/docker-deployment.test.ts +1211 -0
- package/src/__tests__/embedded-deployment.test.ts +342 -0
- package/src/__tests__/grant-store.test.ts +148 -0
- package/src/__tests__/http-proxy.test.ts +281 -0
- package/src/__tests__/instruction-service.test.ts +37 -0
- package/src/__tests__/link-buttons.test.ts +112 -0
- package/src/__tests__/lobu.test.ts +32 -0
- package/src/__tests__/mcp-config-service.test.ts +347 -0
- package/src/__tests__/mcp-proxy.test.ts +696 -0
- package/src/__tests__/message-handler-bridge.test.ts +17 -0
- package/src/__tests__/model-selection.test.ts +172 -0
- package/src/__tests__/oauth-templates.test.ts +39 -0
- package/src/__tests__/platform-adapter-slack-send.test.ts +114 -0
- package/src/__tests__/platform-helpers-model-resolution.test.ts +253 -0
- package/src/__tests__/provider-inheritance.test.ts +212 -0
- package/src/__tests__/routes/cli-auth.test.ts +337 -0
- package/src/__tests__/routes/interactions.test.ts +121 -0
- package/src/__tests__/secret-proxy.test.ts +85 -0
- package/src/__tests__/session-manager.test.ts +572 -0
- package/src/__tests__/setup.ts +133 -0
- package/src/__tests__/skill-and-mcp-registry.test.ts +203 -0
- package/src/__tests__/slack-routes.test.ts +161 -0
- package/src/__tests__/system-config-resolver.test.ts +75 -0
- package/src/__tests__/system-message-limiter.test.ts +89 -0
- package/src/__tests__/system-skills-service.test.ts +362 -0
- package/src/__tests__/transcription-service.test.ts +222 -0
- package/src/__tests__/utils/rate-limiter.test.ts +102 -0
- package/src/__tests__/worker-connection-manager.test.ts +497 -0
- package/src/__tests__/worker-job-router.test.ts +722 -0
- package/src/api/index.ts +1 -0
- package/src/api/platform.ts +292 -0
- package/src/api/response-renderer.ts +157 -0
- package/src/auth/agent-metadata-store.ts +168 -0
- package/src/auth/api-auth-middleware.ts +69 -0
- package/src/auth/api-key-provider-module.ts +213 -0
- package/src/auth/base-provider-module.ts +201 -0
- package/src/auth/chatgpt/chatgpt-oauth-module.ts +185 -0
- package/src/auth/chatgpt/device-code-client.ts +218 -0
- package/src/auth/chatgpt/index.ts +1 -0
- package/src/auth/claude/oauth-module.ts +280 -0
- package/src/auth/cli/token-service.ts +249 -0
- package/src/auth/external/client.ts +560 -0
- package/src/auth/external/device-code-client.ts +225 -0
- package/src/auth/mcp/config-service.ts +392 -0
- package/src/auth/mcp/proxy.ts +1088 -0
- package/src/auth/mcp/string-substitution.ts +17 -0
- package/src/auth/mcp/tool-cache.ts +90 -0
- package/src/auth/oauth/base-client.ts +267 -0
- package/src/auth/oauth/client.ts +153 -0
- package/src/auth/oauth/credentials.ts +7 -0
- package/src/auth/oauth/providers.ts +69 -0
- package/src/auth/oauth/state-store.ts +150 -0
- package/src/auth/oauth-templates.ts +179 -0
- package/src/auth/provider-catalog.ts +220 -0
- package/src/auth/provider-model-options.ts +41 -0
- package/src/auth/settings/agent-settings-store.ts +565 -0
- package/src/auth/settings/auth-profiles-manager.ts +216 -0
- package/src/auth/settings/index.ts +12 -0
- package/src/auth/settings/model-preference-store.ts +52 -0
- package/src/auth/settings/model-selection.ts +135 -0
- package/src/auth/settings/resolved-settings-view.ts +298 -0
- package/src/auth/settings/template-utils.ts +44 -0
- package/src/auth/settings/token-service.ts +88 -0
- package/src/auth/system-env-store.ts +98 -0
- package/src/auth/user-agents-store.ts +68 -0
- package/src/channels/binding-service.ts +214 -0
- package/src/channels/index.ts +4 -0
- package/src/cli/gateway.ts +1304 -0
- package/src/cli/index.ts +74 -0
- package/src/commands/built-in-commands.ts +80 -0
- package/src/commands/command-dispatcher.ts +94 -0
- package/src/commands/command-reply-adapters.ts +27 -0
- package/src/config/file-loader.ts +618 -0
- package/src/config/index.ts +588 -0
- package/src/config/network-allowlist.ts +71 -0
- package/src/connections/chat-instance-manager.ts +1284 -0
- package/src/connections/chat-response-bridge.ts +618 -0
- package/src/connections/index.ts +7 -0
- package/src/connections/interaction-bridge.ts +831 -0
- package/src/connections/message-handler-bridge.ts +415 -0
- package/src/connections/platform-auth-methods.ts +15 -0
- package/src/connections/types.ts +84 -0
- package/src/gateway/connection-manager.ts +291 -0
- package/src/gateway/index.ts +700 -0
- package/src/gateway/job-router.ts +201 -0
- package/src/gateway-main.ts +200 -0
- package/src/index.ts +41 -0
- package/src/infrastructure/queue/index.ts +12 -0
- package/src/infrastructure/queue/queue-producer.ts +148 -0
- package/src/infrastructure/queue/redis-queue.ts +361 -0
- package/src/infrastructure/queue/types.ts +133 -0
- package/src/infrastructure/redis/system-message-limiter.ts +94 -0
- package/src/interactions/config-request-store.ts +198 -0
- package/src/interactions.ts +363 -0
- package/src/lobu.ts +311 -0
- package/src/metrics/prometheus.ts +159 -0
- package/src/modules/module-system.ts +179 -0
- package/src/orchestration/base-deployment-manager.ts +900 -0
- package/src/orchestration/deployment-utils.ts +98 -0
- package/src/orchestration/impl/docker-deployment.ts +620 -0
- package/src/orchestration/impl/embedded-deployment.ts +268 -0
- package/src/orchestration/impl/index.ts +8 -0
- package/src/orchestration/impl/k8s/deployment.ts +1061 -0
- package/src/orchestration/impl/k8s/helpers.ts +610 -0
- package/src/orchestration/impl/k8s/index.ts +1 -0
- package/src/orchestration/index.ts +333 -0
- package/src/orchestration/message-consumer.ts +584 -0
- package/src/orchestration/scheduled-wakeup.ts +704 -0
- package/src/permissions/approval-policy.ts +36 -0
- package/src/permissions/grant-store.ts +219 -0
- package/src/platform/file-handler.ts +66 -0
- package/src/platform/link-buttons.ts +57 -0
- package/src/platform/renderer-utils.ts +44 -0
- package/src/platform/response-renderer.ts +84 -0
- package/src/platform/unified-thread-consumer.ts +187 -0
- package/src/platform.ts +318 -0
- package/src/proxy/http-proxy.ts +752 -0
- package/src/proxy/proxy-manager.ts +81 -0
- package/src/proxy/secret-proxy.ts +402 -0
- package/src/proxy/token-refresh-job.ts +143 -0
- package/src/routes/internal/audio.ts +141 -0
- package/src/routes/internal/device-auth.ts +566 -0
- package/src/routes/internal/files.ts +226 -0
- package/src/routes/internal/history.ts +69 -0
- package/src/routes/internal/images.ts +127 -0
- package/src/routes/internal/interactions.ts +84 -0
- package/src/routes/internal/middleware.ts +23 -0
- package/src/routes/internal/schedule.ts +226 -0
- package/src/routes/internal/types.ts +22 -0
- package/src/routes/openapi-auto.ts +239 -0
- package/src/routes/public/agent-access.ts +23 -0
- package/src/routes/public/agent-config.ts +675 -0
- package/src/routes/public/agent-history.ts +422 -0
- package/src/routes/public/agent-schedules.ts +296 -0
- package/src/routes/public/agent.ts +1086 -0
- package/src/routes/public/agents.ts +373 -0
- package/src/routes/public/channels.ts +191 -0
- package/src/routes/public/cli-auth.ts +883 -0
- package/src/routes/public/connections.ts +574 -0
- package/src/routes/public/landing.ts +16 -0
- package/src/routes/public/oauth.ts +147 -0
- package/src/routes/public/settings-auth.ts +104 -0
- package/src/routes/public/slack.ts +173 -0
- package/src/routes/shared/agent-ownership.ts +101 -0
- package/src/routes/shared/token-verifier.ts +34 -0
- package/src/services/core-services.ts +1053 -0
- package/src/services/image-generation-service.ts +257 -0
- package/src/services/instruction-service.ts +318 -0
- package/src/services/mcp-registry.ts +94 -0
- package/src/services/platform-helpers.ts +287 -0
- package/src/services/session-manager.ts +262 -0
- package/src/services/settings-resolver.ts +74 -0
- package/src/services/system-config-resolver.ts +90 -0
- package/src/services/system-skills-service.ts +229 -0
- package/src/services/transcription-service.ts +684 -0
- package/src/session.ts +110 -0
- package/src/spaces/index.ts +1 -0
- package/src/spaces/space-resolver.ts +17 -0
- package/src/stores/in-memory-agent-store.ts +403 -0
- package/src/stores/redis-agent-store.ts +279 -0
- package/src/utils/public-url.ts +44 -0
- package/src/utils/rate-limiter.ts +94 -0
- package/tsconfig.json +33 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentAccessStore,
|
|
3
|
+
AgentConfigStore,
|
|
4
|
+
AgentConnectionStore,
|
|
5
|
+
AgentMetadata,
|
|
6
|
+
AgentSettings,
|
|
7
|
+
ChannelBinding,
|
|
8
|
+
Grant,
|
|
9
|
+
StoredConnection,
|
|
10
|
+
} from "@lobu/core";
|
|
11
|
+
import type Redis from "ioredis";
|
|
12
|
+
import type { AgentMetadataStore } from "../auth/agent-metadata-store";
|
|
13
|
+
import type { AgentSettingsStore } from "../auth/settings";
|
|
14
|
+
import type { ChannelBindingService } from "../channels";
|
|
15
|
+
import type { GrantStore } from "../permissions/grant-store";
|
|
16
|
+
import type { UserAgentsStore } from "../auth/user-agents-store";
|
|
17
|
+
|
|
18
|
+
export class RedisAgentConfigStore implements AgentConfigStore {
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly settingsStore: AgentSettingsStore,
|
|
21
|
+
private readonly metadataStore: AgentMetadataStore
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
async getSettings(agentId: string): Promise<AgentSettings | null> {
|
|
25
|
+
return this.settingsStore.getSettings(agentId);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async saveSettings(agentId: string, settings: AgentSettings): Promise<void> {
|
|
29
|
+
await this.settingsStore.saveSettings(agentId, settings);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async updateSettings(
|
|
33
|
+
agentId: string,
|
|
34
|
+
updates: Partial<AgentSettings>
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
await this.settingsStore.updateSettings(agentId, updates);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async deleteSettings(agentId: string): Promise<void> {
|
|
40
|
+
await this.settingsStore.deleteSettings(agentId);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async hasSettings(agentId: string): Promise<boolean> {
|
|
44
|
+
return this.settingsStore.hasSettings(agentId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async getMetadata(agentId: string): Promise<AgentMetadata | null> {
|
|
48
|
+
return this.metadataStore.getMetadata(agentId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async saveMetadata(agentId: string, metadata: AgentMetadata): Promise<void> {
|
|
52
|
+
await this.metadataStore.createAgent(
|
|
53
|
+
agentId,
|
|
54
|
+
metadata.name,
|
|
55
|
+
metadata.owner.platform,
|
|
56
|
+
metadata.owner.userId,
|
|
57
|
+
{
|
|
58
|
+
description: metadata.description,
|
|
59
|
+
isWorkspaceAgent: metadata.isWorkspaceAgent,
|
|
60
|
+
workspaceId: metadata.workspaceId,
|
|
61
|
+
parentConnectionId: metadata.parentConnectionId,
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
if (metadata.lastUsedAt !== undefined) {
|
|
65
|
+
await this.metadataStore.updateMetadata(agentId, {
|
|
66
|
+
lastUsedAt: metadata.lastUsedAt,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async updateMetadata(
|
|
72
|
+
agentId: string,
|
|
73
|
+
updates: Partial<AgentMetadata>
|
|
74
|
+
): Promise<void> {
|
|
75
|
+
await this.metadataStore.updateMetadata(agentId, updates);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async deleteMetadata(agentId: string): Promise<void> {
|
|
79
|
+
await this.metadataStore.deleteAgent(agentId);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async hasAgent(agentId: string): Promise<boolean> {
|
|
83
|
+
return this.metadataStore.hasAgent(agentId);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async listAgents(): Promise<AgentMetadata[]> {
|
|
87
|
+
return this.metadataStore.listAllAgents();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async listSandboxes(connectionId: string): Promise<AgentMetadata[]> {
|
|
91
|
+
return this.metadataStore.listSandboxes(connectionId);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class RedisAgentConnectionStore implements AgentConnectionStore {
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly redis: Redis,
|
|
98
|
+
private readonly channelBindingService: ChannelBindingService
|
|
99
|
+
) {}
|
|
100
|
+
|
|
101
|
+
async getConnection(connectionId: string): Promise<StoredConnection | null> {
|
|
102
|
+
const raw = await this.redis.get(`connection:${connectionId}`);
|
|
103
|
+
return raw ? (JSON.parse(raw) as StoredConnection) : null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async listConnections(filter?: {
|
|
107
|
+
templateAgentId?: string;
|
|
108
|
+
platform?: string;
|
|
109
|
+
}): Promise<StoredConnection[]> {
|
|
110
|
+
const ids = filter?.templateAgentId
|
|
111
|
+
? await this.redis.smembers(`connections:agent:${filter.templateAgentId}`)
|
|
112
|
+
: await this.redis.smembers("connections:all");
|
|
113
|
+
|
|
114
|
+
const connections: StoredConnection[] = [];
|
|
115
|
+
for (const id of ids) {
|
|
116
|
+
const raw = await this.redis.get(`connection:${id}`);
|
|
117
|
+
if (!raw) continue;
|
|
118
|
+
const connection = JSON.parse(raw) as StoredConnection;
|
|
119
|
+
if (filter?.platform && connection.platform !== filter.platform) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
connections.push(connection);
|
|
123
|
+
}
|
|
124
|
+
return connections;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async saveConnection(connection: StoredConnection): Promise<void> {
|
|
128
|
+
const existing = await this.getConnection(connection.id);
|
|
129
|
+
await this.redis.set(
|
|
130
|
+
`connection:${connection.id}`,
|
|
131
|
+
JSON.stringify(connection)
|
|
132
|
+
);
|
|
133
|
+
await this.redis.sadd("connections:all", connection.id);
|
|
134
|
+
|
|
135
|
+
const previousTemplate = existing?.templateAgentId;
|
|
136
|
+
if (previousTemplate && previousTemplate !== connection.templateAgentId) {
|
|
137
|
+
await this.redis.srem(
|
|
138
|
+
`connections:agent:${previousTemplate}`,
|
|
139
|
+
connection.id
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
if (connection.templateAgentId) {
|
|
143
|
+
await this.redis.sadd(
|
|
144
|
+
`connections:agent:${connection.templateAgentId}`,
|
|
145
|
+
connection.id
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async updateConnection(
|
|
151
|
+
connectionId: string,
|
|
152
|
+
updates: Partial<StoredConnection>
|
|
153
|
+
): Promise<void> {
|
|
154
|
+
const existing = await this.getConnection(connectionId);
|
|
155
|
+
if (!existing) return;
|
|
156
|
+
await this.saveConnection({
|
|
157
|
+
...existing,
|
|
158
|
+
...updates,
|
|
159
|
+
id: connectionId,
|
|
160
|
+
updatedAt: Date.now(),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async deleteConnection(connectionId: string): Promise<void> {
|
|
165
|
+
const existing = await this.getConnection(connectionId);
|
|
166
|
+
await this.redis.del(`connection:${connectionId}`);
|
|
167
|
+
await this.redis.srem("connections:all", connectionId);
|
|
168
|
+
if (existing?.templateAgentId) {
|
|
169
|
+
await this.redis.srem(
|
|
170
|
+
`connections:agent:${existing.templateAgentId}`,
|
|
171
|
+
connectionId
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async getChannelBinding(
|
|
177
|
+
platform: string,
|
|
178
|
+
channelId: string,
|
|
179
|
+
teamId?: string
|
|
180
|
+
): Promise<ChannelBinding | null> {
|
|
181
|
+
return this.channelBindingService.getBinding(platform, channelId, teamId);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async createChannelBinding(binding: ChannelBinding): Promise<void> {
|
|
185
|
+
await this.channelBindingService.createBinding(
|
|
186
|
+
binding.agentId,
|
|
187
|
+
binding.platform,
|
|
188
|
+
binding.channelId,
|
|
189
|
+
binding.teamId
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async deleteChannelBinding(
|
|
194
|
+
platform: string,
|
|
195
|
+
channelId: string,
|
|
196
|
+
teamId?: string
|
|
197
|
+
): Promise<void> {
|
|
198
|
+
const existing = await this.channelBindingService.getBinding(
|
|
199
|
+
platform,
|
|
200
|
+
channelId,
|
|
201
|
+
teamId
|
|
202
|
+
);
|
|
203
|
+
if (!existing) return;
|
|
204
|
+
await this.channelBindingService.deleteBinding(
|
|
205
|
+
existing.agentId,
|
|
206
|
+
platform,
|
|
207
|
+
channelId,
|
|
208
|
+
teamId
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async listChannelBindings(agentId: string): Promise<ChannelBinding[]> {
|
|
213
|
+
return this.channelBindingService.listBindings(agentId);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async deleteAllChannelBindings(agentId: string): Promise<number> {
|
|
217
|
+
return this.channelBindingService.deleteAllBindings(agentId);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export class RedisAgentAccessStore implements AgentAccessStore {
|
|
222
|
+
constructor(
|
|
223
|
+
private readonly grantStore: GrantStore,
|
|
224
|
+
private readonly userAgentsStore: UserAgentsStore
|
|
225
|
+
) {}
|
|
226
|
+
|
|
227
|
+
async grant(
|
|
228
|
+
agentId: string,
|
|
229
|
+
pattern: string,
|
|
230
|
+
expiresAt: number | null,
|
|
231
|
+
denied?: boolean
|
|
232
|
+
): Promise<void> {
|
|
233
|
+
await this.grantStore.grant(agentId, pattern, expiresAt, denied);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async hasGrant(agentId: string, pattern: string): Promise<boolean> {
|
|
237
|
+
return this.grantStore.hasGrant(agentId, pattern);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async isDenied(agentId: string, pattern: string): Promise<boolean> {
|
|
241
|
+
return this.grantStore.isDenied(agentId, pattern);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async listGrants(agentId: string): Promise<Grant[]> {
|
|
245
|
+
return this.grantStore.listGrants(agentId);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async revokeGrant(agentId: string, pattern: string): Promise<void> {
|
|
249
|
+
await this.grantStore.revoke(agentId, pattern);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async addUserAgent(
|
|
253
|
+
platform: string,
|
|
254
|
+
userId: string,
|
|
255
|
+
agentId: string
|
|
256
|
+
): Promise<void> {
|
|
257
|
+
await this.userAgentsStore.addAgent(platform, userId, agentId);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async removeUserAgent(
|
|
261
|
+
platform: string,
|
|
262
|
+
userId: string,
|
|
263
|
+
agentId: string
|
|
264
|
+
): Promise<void> {
|
|
265
|
+
await this.userAgentsStore.removeAgent(platform, userId, agentId);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async listUserAgents(platform: string, userId: string): Promise<string[]> {
|
|
269
|
+
return this.userAgentsStore.listAgents(platform, userId);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async ownsAgent(
|
|
273
|
+
platform: string,
|
|
274
|
+
userId: string,
|
|
275
|
+
agentId: string
|
|
276
|
+
): Promise<boolean> {
|
|
277
|
+
return this.userAgentsStore.ownsAgent(platform, userId, agentId);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
function normalizeBaseUrl(url: string): string {
|
|
2
|
+
return url.replace(/\/+$/, "");
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function resolvePublicBaseUrl(options?: {
|
|
6
|
+
configuredUrl?: string;
|
|
7
|
+
requestUrl?: string;
|
|
8
|
+
forwardedProto?: string;
|
|
9
|
+
fallbackUrl?: string;
|
|
10
|
+
}): string {
|
|
11
|
+
// Explicit configuredUrl always wins (caller knows best)
|
|
12
|
+
if (options?.configuredUrl) {
|
|
13
|
+
return normalizeBaseUrl(options.configuredUrl);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// When only requestUrl is provided, prefer it over the env default
|
|
17
|
+
// so OAuth redirects match the actual browser origin.
|
|
18
|
+
// Respect X-Forwarded-Proto for TLS-terminating proxies.
|
|
19
|
+
if (options?.requestUrl) {
|
|
20
|
+
const origin = new URL(options.requestUrl);
|
|
21
|
+
if (options.forwardedProto) {
|
|
22
|
+
const proto = options.forwardedProto.split(",")[0]?.trim().toLowerCase();
|
|
23
|
+
if (proto) origin.protocol = `${proto}:`;
|
|
24
|
+
}
|
|
25
|
+
return normalizeBaseUrl(origin.origin);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (process.env.PUBLIC_GATEWAY_URL) {
|
|
29
|
+
return normalizeBaseUrl(process.env.PUBLIC_GATEWAY_URL);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return normalizeBaseUrl(options?.fallbackUrl || "http://localhost:8080");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function resolvePublicUrl(
|
|
36
|
+
path: string,
|
|
37
|
+
options?: {
|
|
38
|
+
configuredUrl?: string;
|
|
39
|
+
requestUrl?: string;
|
|
40
|
+
fallbackUrl?: string;
|
|
41
|
+
}
|
|
42
|
+
): string {
|
|
43
|
+
return new URL(path, `${resolvePublicBaseUrl(options)}/`).toString();
|
|
44
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export interface RedisRateLimitStore {
|
|
2
|
+
incr(key: string): Promise<number>;
|
|
3
|
+
expire(key: string, seconds: number): Promise<number>;
|
|
4
|
+
ttl?(key: string): Promise<number>;
|
|
5
|
+
del?(key: string): Promise<number>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface FixedWindowRateLimitOptions {
|
|
9
|
+
key: string;
|
|
10
|
+
limit: number;
|
|
11
|
+
windowSeconds: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface FixedWindowRateLimitResult {
|
|
15
|
+
allowed: boolean;
|
|
16
|
+
count: number;
|
|
17
|
+
limit: number;
|
|
18
|
+
remaining: number;
|
|
19
|
+
retryAfterSeconds: number;
|
|
20
|
+
resetAt: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class RedisFixedWindowRateLimiter {
|
|
24
|
+
constructor(private readonly redis: RedisRateLimitStore) {}
|
|
25
|
+
|
|
26
|
+
async consume(
|
|
27
|
+
options: FixedWindowRateLimitOptions
|
|
28
|
+
): Promise<FixedWindowRateLimitResult> {
|
|
29
|
+
const count = await this.redis.incr(options.key);
|
|
30
|
+
if (count === 1) {
|
|
31
|
+
await this.redis.expire(options.key, options.windowSeconds);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ttlSeconds = await this.getTtlSeconds(
|
|
35
|
+
options.key,
|
|
36
|
+
options.windowSeconds
|
|
37
|
+
);
|
|
38
|
+
return this.buildResult(options, count, ttlSeconds);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async reset(key: string): Promise<void> {
|
|
42
|
+
if (typeof this.redis.del === "function") {
|
|
43
|
+
await this.redis.del(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private async getTtlSeconds(
|
|
48
|
+
key: string,
|
|
49
|
+
windowSeconds: number
|
|
50
|
+
): Promise<number> {
|
|
51
|
+
if (typeof this.redis.ttl !== "function") {
|
|
52
|
+
return windowSeconds;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ttl = await this.redis.ttl(key);
|
|
56
|
+
if (ttl < 0) {
|
|
57
|
+
return windowSeconds;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return ttl;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private buildResult(
|
|
64
|
+
options: FixedWindowRateLimitOptions,
|
|
65
|
+
count: number,
|
|
66
|
+
ttlSeconds: number
|
|
67
|
+
): FixedWindowRateLimitResult {
|
|
68
|
+
return {
|
|
69
|
+
allowed: count <= options.limit,
|
|
70
|
+
count,
|
|
71
|
+
limit: options.limit,
|
|
72
|
+
remaining: Math.max(0, options.limit - count),
|
|
73
|
+
retryAfterSeconds: Math.max(1, ttlSeconds),
|
|
74
|
+
resetAt: Date.now() + Math.max(1, ttlSeconds) * 1000,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getClientIp(headers: {
|
|
80
|
+
forwardedFor?: string;
|
|
81
|
+
realIp?: string;
|
|
82
|
+
}): string {
|
|
83
|
+
const forwarded = headers.forwardedFor?.split(",")[0]?.trim().toLowerCase();
|
|
84
|
+
if (forwarded) {
|
|
85
|
+
return forwarded;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const realIp = headers.realIp?.trim().toLowerCase();
|
|
89
|
+
if (realIp) {
|
|
90
|
+
return realIp;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return "unknown";
|
|
94
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"module": "commonjs",
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"verbatimModuleSyntax": false,
|
|
13
|
+
"noEmit": false,
|
|
14
|
+
"allowImportingTsExtensions": false,
|
|
15
|
+
"downlevelIteration": true,
|
|
16
|
+
"target": "ES2017",
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"noUnusedLocals": false,
|
|
19
|
+
"noUnusedParameters": false,
|
|
20
|
+
"composite": true
|
|
21
|
+
},
|
|
22
|
+
"include": ["src/**/*", "../core/src/deployment/base-deployment-manager.ts"],
|
|
23
|
+
"exclude": [
|
|
24
|
+
"dist",
|
|
25
|
+
"node_modules",
|
|
26
|
+
"**/*.test.ts",
|
|
27
|
+
"**/__tests__/**",
|
|
28
|
+
"src/routes/public/history-page/**/*.tsx",
|
|
29
|
+
"src/routes/public/agent-page/**/*.tsx",
|
|
30
|
+
"src/routes/public/agents-page/**/*.tsx"
|
|
31
|
+
],
|
|
32
|
+
"references": [{ "path": "../core" }]
|
|
33
|
+
}
|