@lobu/gateway 3.0.5 → 3.0.7
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
package/src/api/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ApiPlatform, type ApiPlatformConfig } from "./platform";
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* API Platform Adapter
|
|
5
|
+
* Handles direct API access for browser extensions, CLI clients, etc.
|
|
6
|
+
* Does not require external platform integration (no Slack, Discord, etc.)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { createLogger, type InstructionProvider } from "@lobu/core";
|
|
11
|
+
import type { CoreServices, PlatformAdapter } from "../platform";
|
|
12
|
+
import type { ResponseRenderer } from "../platform/response-renderer";
|
|
13
|
+
import { broadcastToAgent } from "../routes/public/agent";
|
|
14
|
+
import { ApiResponseRenderer } from "./response-renderer";
|
|
15
|
+
|
|
16
|
+
const logger = createLogger("api-platform");
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* API Platform configuration
|
|
20
|
+
*/
|
|
21
|
+
export interface ApiPlatformConfig {
|
|
22
|
+
/** Whether the API platform is enabled */
|
|
23
|
+
enabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* API Platform adapter for direct access via HTTP/SSE
|
|
28
|
+
* This platform doesn't interact with external services like Slack or Discord.
|
|
29
|
+
* Instead, it provides endpoints for:
|
|
30
|
+
* - Creating sessions
|
|
31
|
+
* - Sending messages
|
|
32
|
+
* - Receiving streaming responses via SSE
|
|
33
|
+
* - Handling tool approvals
|
|
34
|
+
*/
|
|
35
|
+
export class ApiPlatform implements PlatformAdapter {
|
|
36
|
+
readonly name = "api";
|
|
37
|
+
|
|
38
|
+
private responseRenderer?: ApiResponseRenderer;
|
|
39
|
+
private isRunning = false;
|
|
40
|
+
private services?: CoreServices;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Initialize with core services
|
|
44
|
+
*/
|
|
45
|
+
async initialize(services: CoreServices): Promise<void> {
|
|
46
|
+
logger.debug("Initializing API platform...");
|
|
47
|
+
|
|
48
|
+
this.services = services;
|
|
49
|
+
|
|
50
|
+
// Create response renderer for routing worker responses to SSE clients
|
|
51
|
+
this.responseRenderer = new ApiResponseRenderer();
|
|
52
|
+
|
|
53
|
+
// Subscribe to interaction events to broadcast to SSE clients
|
|
54
|
+
const interactionService = services.getInteractionService();
|
|
55
|
+
|
|
56
|
+
interactionService.on("question:created", (event: any) => {
|
|
57
|
+
if (event.teamId !== "api") return;
|
|
58
|
+
broadcastToAgent(event.conversationId, "question", {
|
|
59
|
+
type: "question",
|
|
60
|
+
questionId: event.id,
|
|
61
|
+
question: event.question,
|
|
62
|
+
options: event.options,
|
|
63
|
+
timestamp: Date.now(),
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
interactionService.on("link-button:created", (event: any) => {
|
|
68
|
+
if (event.platform !== "api") return;
|
|
69
|
+
broadcastToAgent(event.conversationId, "link-button", {
|
|
70
|
+
type: "link-button",
|
|
71
|
+
url: event.url,
|
|
72
|
+
label: event.label,
|
|
73
|
+
linkType: event.linkType,
|
|
74
|
+
timestamp: Date.now(),
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
interactionService.on("grant:requested", (event: any) => {
|
|
79
|
+
if (event.teamId !== "api") return;
|
|
80
|
+
broadcastToAgent(event.conversationId, "grant-request", {
|
|
81
|
+
type: "grant-request",
|
|
82
|
+
requestId: event.id,
|
|
83
|
+
domains: event.domains,
|
|
84
|
+
reason: event.reason,
|
|
85
|
+
timestamp: Date.now(),
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
interactionService.on("package:requested", (event: any) => {
|
|
90
|
+
if (event.teamId !== "api") return;
|
|
91
|
+
broadcastToAgent(event.conversationId, "package-request", {
|
|
92
|
+
type: "package-request",
|
|
93
|
+
requestId: event.id,
|
|
94
|
+
packages: event.packages,
|
|
95
|
+
reason: event.reason,
|
|
96
|
+
timestamp: Date.now(),
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
interactionService.on("config:requested", (event: any) => {
|
|
101
|
+
if (event.teamId !== "api") return;
|
|
102
|
+
broadcastToAgent(event.conversationId, "config-request", {
|
|
103
|
+
type: "config-request",
|
|
104
|
+
requestId: event.id,
|
|
105
|
+
text: event.text,
|
|
106
|
+
timestamp: Date.now(),
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
interactionService.on("suggestion:created", (event: any) => {
|
|
111
|
+
if (event.teamId !== "api") return;
|
|
112
|
+
broadcastToAgent(event.conversationId, "suggestion", {
|
|
113
|
+
type: "suggestion",
|
|
114
|
+
prompts: event.prompts,
|
|
115
|
+
timestamp: Date.now(),
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
logger.debug("✅ API platform initialized");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Start the platform
|
|
124
|
+
* For API platform, this is mostly a no-op since routes are registered separately
|
|
125
|
+
*/
|
|
126
|
+
async start(): Promise<void> {
|
|
127
|
+
this.isRunning = true;
|
|
128
|
+
logger.debug("✅ API platform started");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Stop the platform
|
|
133
|
+
*/
|
|
134
|
+
async stop(): Promise<void> {
|
|
135
|
+
this.isRunning = false;
|
|
136
|
+
logger.debug("✅ API platform stopped");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if platform is healthy
|
|
141
|
+
*/
|
|
142
|
+
isHealthy(): boolean {
|
|
143
|
+
return this.isRunning;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* No custom instruction provider for API platform
|
|
148
|
+
*/
|
|
149
|
+
getInstructionProvider(): InstructionProvider | null {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Build deployment metadata
|
|
155
|
+
* For API sessions, we include session ID and source
|
|
156
|
+
*/
|
|
157
|
+
buildDeploymentMetadata(
|
|
158
|
+
conversationId: string,
|
|
159
|
+
channelId: string,
|
|
160
|
+
platformMetadata: Record<string, any>
|
|
161
|
+
): Record<string, string> {
|
|
162
|
+
return {
|
|
163
|
+
sessionId: platformMetadata.sessionId || conversationId,
|
|
164
|
+
source: "direct-api",
|
|
165
|
+
channelId,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Get the response renderer for routing worker responses
|
|
171
|
+
*/
|
|
172
|
+
getResponseRenderer(): ResponseRenderer | undefined {
|
|
173
|
+
return this.responseRenderer;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Suggestions are broadcast via interaction events above
|
|
178
|
+
*/
|
|
179
|
+
async renderSuggestion(): Promise<void> {
|
|
180
|
+
/* noop — suggestions broadcast via interaction events */
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* API platform doesn't have thread status indicators
|
|
185
|
+
*/
|
|
186
|
+
async setThreadStatus(): Promise<void> {
|
|
187
|
+
// Status is sent via SSE events
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Send a message via API platform
|
|
192
|
+
* Creates or reuses a session and queues the message for processing
|
|
193
|
+
*
|
|
194
|
+
* @param token - Auth token (used to derive userId)
|
|
195
|
+
* @param message - Message content
|
|
196
|
+
* @param options - Routing info (agentId = channelId = conversationId for API)
|
|
197
|
+
*/
|
|
198
|
+
async sendMessage(
|
|
199
|
+
token: string,
|
|
200
|
+
message: string,
|
|
201
|
+
options: {
|
|
202
|
+
agentId: string;
|
|
203
|
+
channelId: string;
|
|
204
|
+
conversationId: string;
|
|
205
|
+
teamId: string;
|
|
206
|
+
files?: Array<{ buffer: Buffer; filename: string }>;
|
|
207
|
+
}
|
|
208
|
+
): Promise<{
|
|
209
|
+
messageId: string;
|
|
210
|
+
eventsUrl?: string;
|
|
211
|
+
queued?: boolean;
|
|
212
|
+
}> {
|
|
213
|
+
if (!this.services) {
|
|
214
|
+
throw new Error("API platform not initialized");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const { agentId } = options;
|
|
218
|
+
const sessionManager = this.services.getSessionManager();
|
|
219
|
+
const queueProducer = this.services.getQueueProducer();
|
|
220
|
+
const messageId = randomUUID();
|
|
221
|
+
const userId = `api-${token.slice(0, 8) || "anonymous"}`;
|
|
222
|
+
|
|
223
|
+
// For API platform: agentId = channelId = conversationId (all same)
|
|
224
|
+
// Try to get existing session or create new one
|
|
225
|
+
let session = await sessionManager.getSession(agentId);
|
|
226
|
+
|
|
227
|
+
if (!session) {
|
|
228
|
+
session = {
|
|
229
|
+
conversationId: agentId,
|
|
230
|
+
channelId: agentId,
|
|
231
|
+
userId,
|
|
232
|
+
threadCreator: userId,
|
|
233
|
+
lastActivity: Date.now(),
|
|
234
|
+
createdAt: Date.now(),
|
|
235
|
+
status: "created",
|
|
236
|
+
provider: "claude",
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
await sessionManager.setSession(session);
|
|
240
|
+
logger.info(`Created new API session: ${agentId}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!session) {
|
|
244
|
+
throw new Error("Session not found after creation");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Update session activity
|
|
248
|
+
await sessionManager.touchSession(agentId);
|
|
249
|
+
|
|
250
|
+
// Prepare message with file info if provided
|
|
251
|
+
const platformMetadata: Record<string, any> = {
|
|
252
|
+
agentId,
|
|
253
|
+
source: "messaging-api",
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
if (options.files && options.files.length > 0) {
|
|
257
|
+
platformMetadata.fileCount = options.files.length;
|
|
258
|
+
platformMetadata.fileNames = options.files.map((f) => f.filename);
|
|
259
|
+
logger.info(
|
|
260
|
+
`Message includes ${options.files.length} file(s): ${platformMetadata.fileNames.join(", ")}`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Enqueue message for worker processing
|
|
265
|
+
await queueProducer.enqueueMessage({
|
|
266
|
+
userId,
|
|
267
|
+
conversationId: agentId,
|
|
268
|
+
messageId,
|
|
269
|
+
channelId: agentId,
|
|
270
|
+
teamId: "api",
|
|
271
|
+
agentId: agentId, // agentId is the isolation boundary
|
|
272
|
+
botId: "lobu-api",
|
|
273
|
+
platform: "api",
|
|
274
|
+
messageText: message,
|
|
275
|
+
platformMetadata,
|
|
276
|
+
agentOptions: {
|
|
277
|
+
provider: session.provider || "claude",
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
logger.info(`Queued message ${messageId} for agent ${agentId}`);
|
|
282
|
+
|
|
283
|
+
const publicUrl = this.services.getPublicGatewayUrl();
|
|
284
|
+
const baseUrl = publicUrl || "http://localhost:8080";
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
messageId,
|
|
288
|
+
eventsUrl: `${baseUrl}/api/v1/agents/${agentId}/events`,
|
|
289
|
+
queued: true,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* API Response Renderer
|
|
5
|
+
* Broadcasts worker responses to SSE connections for direct API clients
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createLogger } from "@lobu/core";
|
|
9
|
+
import type { ThreadResponsePayload } from "../infrastructure/queue/types";
|
|
10
|
+
import type { ResponseRenderer } from "../platform/response-renderer";
|
|
11
|
+
import { broadcastToAgent } from "../routes/public/agent";
|
|
12
|
+
|
|
13
|
+
const logger = createLogger("api-response-renderer");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Response renderer for API platform
|
|
17
|
+
* Broadcasts responses to SSE clients instead of external platforms
|
|
18
|
+
*/
|
|
19
|
+
export class ApiResponseRenderer implements ResponseRenderer {
|
|
20
|
+
/**
|
|
21
|
+
* Handle streaming delta content
|
|
22
|
+
* Broadcasts delta to SSE connections
|
|
23
|
+
*/
|
|
24
|
+
async handleDelta(
|
|
25
|
+
payload: ThreadResponsePayload,
|
|
26
|
+
_sessionKey: string
|
|
27
|
+
): Promise<string | null> {
|
|
28
|
+
const sessionId =
|
|
29
|
+
(payload.platformMetadata?.sessionId as string) || payload.conversationId;
|
|
30
|
+
|
|
31
|
+
if (!sessionId) {
|
|
32
|
+
logger.warn("No session ID found in payload for delta broadcast");
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Broadcast delta to SSE clients
|
|
37
|
+
broadcastToAgent(sessionId, "output", {
|
|
38
|
+
type: "delta",
|
|
39
|
+
content: payload.delta,
|
|
40
|
+
timestamp: payload.timestamp || Date.now(),
|
|
41
|
+
messageId: payload.messageId,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
logger.debug(
|
|
45
|
+
`Broadcast delta to session ${sessionId}: ${payload.delta?.length || 0} chars`
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return payload.messageId;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Handle completion of response processing
|
|
53
|
+
* Sends completion event to SSE clients
|
|
54
|
+
*/
|
|
55
|
+
async handleCompletion(
|
|
56
|
+
payload: ThreadResponsePayload,
|
|
57
|
+
_sessionKey: string
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
const sessionId =
|
|
60
|
+
(payload.platformMetadata?.sessionId as string) || payload.conversationId;
|
|
61
|
+
|
|
62
|
+
if (!sessionId) {
|
|
63
|
+
logger.warn("No session ID found in payload for completion broadcast");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Broadcast completion to SSE clients
|
|
68
|
+
broadcastToAgent(sessionId, "complete", {
|
|
69
|
+
type: "complete",
|
|
70
|
+
messageId: payload.messageId,
|
|
71
|
+
processedMessageIds: payload.processedMessageIds,
|
|
72
|
+
timestamp: payload.timestamp || Date.now(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
logger.info(`Broadcast completion to session ${sessionId}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Handle error response
|
|
80
|
+
* Sends error event to SSE clients
|
|
81
|
+
*/
|
|
82
|
+
async handleError(
|
|
83
|
+
payload: ThreadResponsePayload,
|
|
84
|
+
_sessionKey: string
|
|
85
|
+
): Promise<void> {
|
|
86
|
+
const sessionId =
|
|
87
|
+
(payload.platformMetadata?.sessionId as string) || payload.conversationId;
|
|
88
|
+
|
|
89
|
+
if (!sessionId) {
|
|
90
|
+
logger.warn("No session ID found in payload for error broadcast");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Broadcast error to SSE clients
|
|
95
|
+
broadcastToAgent(sessionId, "error", {
|
|
96
|
+
type: "error",
|
|
97
|
+
error: payload.error,
|
|
98
|
+
messageId: payload.messageId,
|
|
99
|
+
timestamp: payload.timestamp || Date.now(),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
logger.error(`Broadcast error to session ${sessionId}: ${payload.error}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Handle status updates (heartbeat with elapsed time)
|
|
107
|
+
* Sends status event to SSE clients
|
|
108
|
+
*/
|
|
109
|
+
async handleStatusUpdate(payload: ThreadResponsePayload): Promise<void> {
|
|
110
|
+
const sessionId =
|
|
111
|
+
(payload.platformMetadata?.sessionId as string) || payload.conversationId;
|
|
112
|
+
|
|
113
|
+
if (!sessionId) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Broadcast status to SSE clients
|
|
118
|
+
broadcastToAgent(sessionId, "status", {
|
|
119
|
+
type: "status",
|
|
120
|
+
status: payload.statusUpdate,
|
|
121
|
+
messageId: payload.messageId,
|
|
122
|
+
timestamp: payload.timestamp || Date.now(),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Handle ephemeral messages
|
|
128
|
+
* For API platform, these are just broadcast as regular events
|
|
129
|
+
*/
|
|
130
|
+
async handleEphemeral(payload: ThreadResponsePayload): Promise<void> {
|
|
131
|
+
const sessionId =
|
|
132
|
+
(payload.platformMetadata?.sessionId as string) || payload.conversationId;
|
|
133
|
+
|
|
134
|
+
if (!sessionId) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Broadcast ephemeral content to SSE clients
|
|
139
|
+
broadcastToAgent(sessionId, "ephemeral", {
|
|
140
|
+
type: "ephemeral",
|
|
141
|
+
content: payload.content,
|
|
142
|
+
messageId: payload.messageId,
|
|
143
|
+
timestamp: payload.timestamp || Date.now(),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Stop stream for conversation - no-op for API platform
|
|
149
|
+
* SSE connections handle their own lifecycle
|
|
150
|
+
*/
|
|
151
|
+
async stopStreamForConversation(
|
|
152
|
+
_userId: string,
|
|
153
|
+
_conversationId: string
|
|
154
|
+
): Promise<void> {
|
|
155
|
+
// No-op - SSE connections manage their own lifecycle
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { BaseRedisStore, createLogger } from "@lobu/core";
|
|
2
|
+
import type Redis from "ioredis";
|
|
3
|
+
|
|
4
|
+
const logger = createLogger("agent-metadata-store");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Agent metadata - user-facing info about an agent
|
|
8
|
+
*/
|
|
9
|
+
export interface AgentMetadata {
|
|
10
|
+
agentId: string;
|
|
11
|
+
/** User-friendly name (e.g., "Work Agent", "Personal Assistant") */
|
|
12
|
+
name: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
owner: {
|
|
15
|
+
platform: string;
|
|
16
|
+
userId: string;
|
|
17
|
+
};
|
|
18
|
+
/** Whether this is the workspace default agent */
|
|
19
|
+
isWorkspaceAgent?: boolean;
|
|
20
|
+
/** Workspace/team ID for workspace agents */
|
|
21
|
+
workspaceId?: string;
|
|
22
|
+
/** Connection that auto-created this agent (makes it a "sandbox") */
|
|
23
|
+
parentConnectionId?: string;
|
|
24
|
+
createdAt: number;
|
|
25
|
+
lastUsedAt?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Store agent metadata in Redis.
|
|
30
|
+
* Pattern: agent_metadata:{agentId}
|
|
31
|
+
*/
|
|
32
|
+
export class AgentMetadataStore extends BaseRedisStore<AgentMetadata> {
|
|
33
|
+
constructor(redis: Redis) {
|
|
34
|
+
super({
|
|
35
|
+
redis,
|
|
36
|
+
keyPrefix: "agent_metadata",
|
|
37
|
+
loggerName: "agent-metadata-store",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create a new agent with metadata
|
|
43
|
+
*/
|
|
44
|
+
async createAgent(
|
|
45
|
+
agentId: string,
|
|
46
|
+
name: string,
|
|
47
|
+
platform: string,
|
|
48
|
+
userId: string,
|
|
49
|
+
options?: {
|
|
50
|
+
description?: string;
|
|
51
|
+
isWorkspaceAgent?: boolean;
|
|
52
|
+
workspaceId?: string;
|
|
53
|
+
parentConnectionId?: string;
|
|
54
|
+
}
|
|
55
|
+
): Promise<AgentMetadata> {
|
|
56
|
+
const metadata: AgentMetadata = {
|
|
57
|
+
agentId,
|
|
58
|
+
name,
|
|
59
|
+
owner: { platform, userId },
|
|
60
|
+
isWorkspaceAgent: options?.isWorkspaceAgent,
|
|
61
|
+
workspaceId: options?.workspaceId,
|
|
62
|
+
parentConnectionId: options?.parentConnectionId,
|
|
63
|
+
createdAt: Date.now(),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if (options?.description) {
|
|
67
|
+
metadata.description = options.description;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const key = this.buildKey(agentId);
|
|
71
|
+
await this.set(key, metadata);
|
|
72
|
+
|
|
73
|
+
// Index sandbox under its parent connection
|
|
74
|
+
if (options?.parentConnectionId) {
|
|
75
|
+
await this.redis.sadd(
|
|
76
|
+
`sandboxes:connection:${options.parentConnectionId}`,
|
|
77
|
+
agentId
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
logger.info(`Created agent metadata for ${agentId}: "${name}"`);
|
|
82
|
+
return metadata;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get metadata for an agent
|
|
87
|
+
*/
|
|
88
|
+
async getMetadata(agentId: string): Promise<AgentMetadata | null> {
|
|
89
|
+
const key = this.buildKey(agentId);
|
|
90
|
+
return this.get(key);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Update agent metadata (partial update)
|
|
95
|
+
*/
|
|
96
|
+
async updateMetadata(
|
|
97
|
+
agentId: string,
|
|
98
|
+
updates: Partial<Pick<AgentMetadata, "name" | "description" | "lastUsedAt">>
|
|
99
|
+
): Promise<void> {
|
|
100
|
+
const existing = await this.getMetadata(agentId);
|
|
101
|
+
if (!existing) {
|
|
102
|
+
logger.warn(`Cannot update metadata: agent ${agentId} not found`);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const updated: AgentMetadata = { ...existing, ...updates };
|
|
107
|
+
const key = this.buildKey(agentId);
|
|
108
|
+
await this.set(key, updated);
|
|
109
|
+
logger.info(`Updated metadata for agent ${agentId}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Delete agent metadata
|
|
114
|
+
*/
|
|
115
|
+
async deleteAgent(agentId: string): Promise<void> {
|
|
116
|
+
// Clean up sandbox index if this agent has a parent connection
|
|
117
|
+
const metadata = await this.getMetadata(agentId);
|
|
118
|
+
if (metadata?.parentConnectionId) {
|
|
119
|
+
await this.redis.srem(
|
|
120
|
+
`sandboxes:connection:${metadata.parentConnectionId}`,
|
|
121
|
+
agentId
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const key = this.buildKey(agentId);
|
|
126
|
+
await this.delete(key);
|
|
127
|
+
logger.info(`Deleted metadata for agent ${agentId}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Check if agent exists
|
|
132
|
+
*/
|
|
133
|
+
async hasAgent(agentId: string): Promise<boolean> {
|
|
134
|
+
const key = this.buildKey(agentId);
|
|
135
|
+
return this.exists(key);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* List sandbox agents belonging to a connection
|
|
140
|
+
*/
|
|
141
|
+
async listSandboxes(connectionId: string): Promise<AgentMetadata[]> {
|
|
142
|
+
const agentIds = await this.redis.smembers(
|
|
143
|
+
`sandboxes:connection:${connectionId}`
|
|
144
|
+
);
|
|
145
|
+
const sandboxes: AgentMetadata[] = [];
|
|
146
|
+
for (const agentId of agentIds) {
|
|
147
|
+
const data = await this.getMetadata(agentId);
|
|
148
|
+
if (data) sandboxes.push(data);
|
|
149
|
+
}
|
|
150
|
+
sandboxes.sort((a, b) => (b.lastUsedAt ?? 0) - (a.lastUsedAt ?? 0));
|
|
151
|
+
return sandboxes;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* List all agents in the system, sorted by lastUsedAt descending
|
|
156
|
+
*/
|
|
157
|
+
async listAllAgents(): Promise<AgentMetadata[]> {
|
|
158
|
+
const prefix = `${this.keyPrefix}:`;
|
|
159
|
+
const keys = await this.scanByPrefix(prefix);
|
|
160
|
+
const agents: AgentMetadata[] = [];
|
|
161
|
+
for (const key of keys) {
|
|
162
|
+
const data = await this.get(key);
|
|
163
|
+
if (data) agents.push(data);
|
|
164
|
+
}
|
|
165
|
+
agents.sort((a, b) => (b.lastUsedAt ?? 0) - (a.lastUsedAt ?? 0));
|
|
166
|
+
return agents;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { verifyWorkerToken } from "@lobu/core";
|
|
3
|
+
import type { Context, Next } from "hono";
|
|
4
|
+
import { verifySettingsSession } from "../routes/public/settings-auth";
|
|
5
|
+
import type { CliTokenService } from "./cli/token-service";
|
|
6
|
+
import type { ExternalAuthClient } from "./external/client";
|
|
7
|
+
|
|
8
|
+
export const TOKEN_EXPIRATION_MS = 24 * 60 * 60 * 1000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Hono middleware that enforces the standard auth check:
|
|
12
|
+
* 1. Settings session cookie 2. CLI JWT 3. External OAuth 4. Admin password 5. Worker token
|
|
13
|
+
*/
|
|
14
|
+
export function createApiAuthMiddleware(opts: {
|
|
15
|
+
adminPassword?: string;
|
|
16
|
+
cliTokenService?: CliTokenService;
|
|
17
|
+
externalAuthClient?: ExternalAuthClient;
|
|
18
|
+
allowWorkerToken?: boolean;
|
|
19
|
+
allowSettingsSession?: boolean;
|
|
20
|
+
}) {
|
|
21
|
+
return async (c: Context, next: Next) => {
|
|
22
|
+
// 1. Try settings session cookie when explicitly allowed.
|
|
23
|
+
if (opts.allowSettingsSession && verifySettingsSession(c)) return next();
|
|
24
|
+
|
|
25
|
+
const authHeader = c.req.header("Authorization");
|
|
26
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
27
|
+
return c.json({ success: false, error: "Unauthorized" }, 401);
|
|
28
|
+
}
|
|
29
|
+
const token = authHeader.substring(7);
|
|
30
|
+
|
|
31
|
+
// 2. Try CLI JWT
|
|
32
|
+
if (opts.cliTokenService) {
|
|
33
|
+
const identity = await opts.cliTokenService.verifyAccessToken(token);
|
|
34
|
+
if (identity) return next();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 3. Try external OAuth token (validated against MEMORY_URL userinfo)
|
|
38
|
+
if (opts.externalAuthClient) {
|
|
39
|
+
try {
|
|
40
|
+
const userInfo = await opts.externalAuthClient.fetchUserInfo(token);
|
|
41
|
+
if (userInfo?.sub) return next();
|
|
42
|
+
} catch {
|
|
43
|
+
// Token not valid for external auth, continue to next method
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 4. Try admin password
|
|
48
|
+
if (opts.adminPassword) {
|
|
49
|
+
const a = Buffer.from(token);
|
|
50
|
+
const b = Buffer.from(opts.adminPassword);
|
|
51
|
+
if (a.length === b.length && timingSafeEqual(a, b)) {
|
|
52
|
+
return next();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 5. Try worker token when explicitly allowed for the route
|
|
57
|
+
if (opts.allowWorkerToken !== false) {
|
|
58
|
+
const workerData = verifyWorkerToken(token);
|
|
59
|
+
if (workerData) {
|
|
60
|
+
const tokenAge = Date.now() - workerData.timestamp;
|
|
61
|
+
if (tokenAge <= TOKEN_EXPIRATION_MS) {
|
|
62
|
+
return next();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return c.json({ success: false, error: "Unauthorized" }, 401);
|
|
68
|
+
};
|
|
69
|
+
}
|