@lobu/gateway 3.0.8 → 3.0.12

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.
Files changed (219) hide show
  1. package/dist/api/platform.d.ts.map +1 -1
  2. package/dist/api/platform.js +8 -26
  3. package/dist/api/platform.js.map +1 -1
  4. package/dist/auth/mcp/proxy.d.ts +14 -0
  5. package/dist/auth/mcp/proxy.d.ts.map +1 -1
  6. package/dist/auth/mcp/proxy.js +149 -13
  7. package/dist/auth/mcp/proxy.js.map +1 -1
  8. package/dist/cli/gateway.d.ts.map +1 -1
  9. package/dist/cli/gateway.js +29 -0
  10. package/dist/cli/gateway.js.map +1 -1
  11. package/dist/cli/index.js +2 -2
  12. package/dist/cli/index.js.map +1 -1
  13. package/dist/connections/chat-instance-manager.d.ts.map +1 -1
  14. package/dist/connections/chat-instance-manager.js +2 -1
  15. package/dist/connections/chat-instance-manager.js.map +1 -1
  16. package/dist/connections/interaction-bridge.d.ts +9 -2
  17. package/dist/connections/interaction-bridge.d.ts.map +1 -1
  18. package/dist/connections/interaction-bridge.js +132 -230
  19. package/dist/connections/interaction-bridge.js.map +1 -1
  20. package/dist/connections/message-handler-bridge.d.ts.map +1 -1
  21. package/dist/connections/message-handler-bridge.js +44 -26
  22. package/dist/connections/message-handler-bridge.js.map +1 -1
  23. package/dist/interactions.d.ts +9 -43
  24. package/dist/interactions.d.ts.map +1 -1
  25. package/dist/interactions.js +10 -52
  26. package/dist/interactions.js.map +1 -1
  27. package/dist/orchestration/base-deployment-manager.js +7 -7
  28. package/dist/orchestration/base-deployment-manager.js.map +1 -1
  29. package/dist/platform/unified-thread-consumer.d.ts.map +1 -1
  30. package/dist/platform/unified-thread-consumer.js +38 -34
  31. package/dist/platform/unified-thread-consumer.js.map +1 -1
  32. package/dist/routes/public/agent.d.ts +4 -0
  33. package/dist/routes/public/agent.d.ts.map +1 -1
  34. package/dist/routes/public/agent.js +21 -0
  35. package/dist/routes/public/agent.js.map +1 -1
  36. package/dist/services/core-services.d.ts.map +1 -1
  37. package/dist/services/core-services.js +4 -0
  38. package/dist/services/core-services.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/__tests__/agent-config-routes.test.ts +0 -254
  41. package/src/__tests__/agent-history-routes.test.ts +0 -72
  42. package/src/__tests__/agent-routes.test.ts +0 -68
  43. package/src/__tests__/agent-schedules-routes.test.ts +0 -59
  44. package/src/__tests__/agent-settings-store.test.ts +0 -323
  45. package/src/__tests__/bedrock-model-catalog.test.ts +0 -40
  46. package/src/__tests__/bedrock-openai-service.test.ts +0 -157
  47. package/src/__tests__/bedrock-provider-module.test.ts +0 -56
  48. package/src/__tests__/chat-instance-manager-slack.test.ts +0 -204
  49. package/src/__tests__/chat-response-bridge.test.ts +0 -131
  50. package/src/__tests__/config-memory-plugins.test.ts +0 -92
  51. package/src/__tests__/config-request-store.test.ts +0 -127
  52. package/src/__tests__/connection-routes.test.ts +0 -144
  53. package/src/__tests__/core-services-store-selection.test.ts +0 -92
  54. package/src/__tests__/docker-deployment.test.ts +0 -1211
  55. package/src/__tests__/embedded-deployment.test.ts +0 -342
  56. package/src/__tests__/grant-store.test.ts +0 -148
  57. package/src/__tests__/http-proxy.test.ts +0 -281
  58. package/src/__tests__/instruction-service.test.ts +0 -37
  59. package/src/__tests__/link-buttons.test.ts +0 -112
  60. package/src/__tests__/lobu.test.ts +0 -32
  61. package/src/__tests__/mcp-config-service.test.ts +0 -347
  62. package/src/__tests__/mcp-proxy.test.ts +0 -694
  63. package/src/__tests__/message-handler-bridge.test.ts +0 -17
  64. package/src/__tests__/model-selection.test.ts +0 -172
  65. package/src/__tests__/oauth-templates.test.ts +0 -39
  66. package/src/__tests__/platform-adapter-slack-send.test.ts +0 -114
  67. package/src/__tests__/platform-helpers-model-resolution.test.ts +0 -253
  68. package/src/__tests__/provider-inheritance.test.ts +0 -212
  69. package/src/__tests__/routes/cli-auth.test.ts +0 -337
  70. package/src/__tests__/routes/interactions.test.ts +0 -121
  71. package/src/__tests__/secret-proxy.test.ts +0 -85
  72. package/src/__tests__/session-manager.test.ts +0 -572
  73. package/src/__tests__/setup.ts +0 -133
  74. package/src/__tests__/skill-and-mcp-registry.test.ts +0 -203
  75. package/src/__tests__/slack-routes.test.ts +0 -161
  76. package/src/__tests__/system-config-resolver.test.ts +0 -75
  77. package/src/__tests__/system-message-limiter.test.ts +0 -89
  78. package/src/__tests__/system-skills-service.test.ts +0 -362
  79. package/src/__tests__/transcription-service.test.ts +0 -222
  80. package/src/__tests__/utils/rate-limiter.test.ts +0 -102
  81. package/src/__tests__/worker-connection-manager.test.ts +0 -497
  82. package/src/__tests__/worker-job-router.test.ts +0 -722
  83. package/src/api/index.ts +0 -1
  84. package/src/api/platform.ts +0 -292
  85. package/src/api/response-renderer.ts +0 -157
  86. package/src/auth/agent-metadata-store.ts +0 -168
  87. package/src/auth/api-auth-middleware.ts +0 -69
  88. package/src/auth/api-key-provider-module.ts +0 -213
  89. package/src/auth/base-provider-module.ts +0 -201
  90. package/src/auth/bedrock/provider-module.ts +0 -110
  91. package/src/auth/chatgpt/chatgpt-oauth-module.ts +0 -185
  92. package/src/auth/chatgpt/device-code-client.ts +0 -218
  93. package/src/auth/chatgpt/index.ts +0 -1
  94. package/src/auth/claude/oauth-module.ts +0 -280
  95. package/src/auth/cli/token-service.ts +0 -249
  96. package/src/auth/external/client.ts +0 -560
  97. package/src/auth/external/device-code-client.ts +0 -235
  98. package/src/auth/mcp/config-service.ts +0 -420
  99. package/src/auth/mcp/proxy.ts +0 -1086
  100. package/src/auth/mcp/string-substitution.ts +0 -17
  101. package/src/auth/mcp/tool-cache.ts +0 -90
  102. package/src/auth/oauth/base-client.ts +0 -267
  103. package/src/auth/oauth/client.ts +0 -153
  104. package/src/auth/oauth/credentials.ts +0 -7
  105. package/src/auth/oauth/providers.ts +0 -69
  106. package/src/auth/oauth/state-store.ts +0 -150
  107. package/src/auth/oauth-templates.ts +0 -179
  108. package/src/auth/provider-catalog.ts +0 -220
  109. package/src/auth/provider-model-options.ts +0 -41
  110. package/src/auth/settings/agent-settings-store.ts +0 -565
  111. package/src/auth/settings/auth-profiles-manager.ts +0 -216
  112. package/src/auth/settings/index.ts +0 -12
  113. package/src/auth/settings/model-preference-store.ts +0 -52
  114. package/src/auth/settings/model-selection.ts +0 -135
  115. package/src/auth/settings/resolved-settings-view.ts +0 -298
  116. package/src/auth/settings/template-utils.ts +0 -44
  117. package/src/auth/settings/token-service.ts +0 -88
  118. package/src/auth/system-env-store.ts +0 -98
  119. package/src/auth/user-agents-store.ts +0 -68
  120. package/src/channels/binding-service.ts +0 -214
  121. package/src/channels/index.ts +0 -4
  122. package/src/cli/gateway.ts +0 -1312
  123. package/src/cli/index.ts +0 -74
  124. package/src/commands/built-in-commands.ts +0 -80
  125. package/src/commands/command-dispatcher.ts +0 -94
  126. package/src/commands/command-reply-adapters.ts +0 -27
  127. package/src/config/file-loader.ts +0 -618
  128. package/src/config/index.ts +0 -588
  129. package/src/config/network-allowlist.ts +0 -71
  130. package/src/connections/chat-instance-manager.ts +0 -1284
  131. package/src/connections/chat-response-bridge.ts +0 -618
  132. package/src/connections/index.ts +0 -7
  133. package/src/connections/interaction-bridge.ts +0 -831
  134. package/src/connections/message-handler-bridge.ts +0 -415
  135. package/src/connections/platform-auth-methods.ts +0 -15
  136. package/src/connections/types.ts +0 -84
  137. package/src/gateway/connection-manager.ts +0 -291
  138. package/src/gateway/index.ts +0 -698
  139. package/src/gateway/job-router.ts +0 -201
  140. package/src/gateway-main.ts +0 -200
  141. package/src/index.ts +0 -41
  142. package/src/infrastructure/queue/index.ts +0 -12
  143. package/src/infrastructure/queue/queue-producer.ts +0 -148
  144. package/src/infrastructure/queue/redis-queue.ts +0 -361
  145. package/src/infrastructure/queue/types.ts +0 -133
  146. package/src/infrastructure/redis/system-message-limiter.ts +0 -94
  147. package/src/interactions/config-request-store.ts +0 -198
  148. package/src/interactions.ts +0 -363
  149. package/src/lobu.ts +0 -311
  150. package/src/metrics/prometheus.ts +0 -159
  151. package/src/modules/module-system.ts +0 -179
  152. package/src/orchestration/base-deployment-manager.ts +0 -900
  153. package/src/orchestration/deployment-utils.ts +0 -98
  154. package/src/orchestration/impl/docker-deployment.ts +0 -620
  155. package/src/orchestration/impl/embedded-deployment.ts +0 -268
  156. package/src/orchestration/impl/index.ts +0 -8
  157. package/src/orchestration/impl/k8s/deployment.ts +0 -1061
  158. package/src/orchestration/impl/k8s/helpers.ts +0 -610
  159. package/src/orchestration/impl/k8s/index.ts +0 -1
  160. package/src/orchestration/index.ts +0 -333
  161. package/src/orchestration/message-consumer.ts +0 -584
  162. package/src/orchestration/scheduled-wakeup.ts +0 -704
  163. package/src/permissions/approval-policy.ts +0 -36
  164. package/src/permissions/grant-store.ts +0 -219
  165. package/src/platform/file-handler.ts +0 -66
  166. package/src/platform/link-buttons.ts +0 -57
  167. package/src/platform/renderer-utils.ts +0 -44
  168. package/src/platform/response-renderer.ts +0 -84
  169. package/src/platform/unified-thread-consumer.ts +0 -187
  170. package/src/platform.ts +0 -318
  171. package/src/proxy/http-proxy.ts +0 -752
  172. package/src/proxy/proxy-manager.ts +0 -81
  173. package/src/proxy/secret-proxy.ts +0 -402
  174. package/src/proxy/token-refresh-job.ts +0 -143
  175. package/src/routes/internal/audio.ts +0 -141
  176. package/src/routes/internal/device-auth.ts +0 -652
  177. package/src/routes/internal/files.ts +0 -226
  178. package/src/routes/internal/history.ts +0 -69
  179. package/src/routes/internal/images.ts +0 -127
  180. package/src/routes/internal/interactions.ts +0 -84
  181. package/src/routes/internal/middleware.ts +0 -23
  182. package/src/routes/internal/schedule.ts +0 -226
  183. package/src/routes/internal/types.ts +0 -22
  184. package/src/routes/openapi-auto.ts +0 -239
  185. package/src/routes/public/agent-access.ts +0 -23
  186. package/src/routes/public/agent-config.ts +0 -675
  187. package/src/routes/public/agent-history.ts +0 -422
  188. package/src/routes/public/agent-schedules.ts +0 -296
  189. package/src/routes/public/agent.ts +0 -1086
  190. package/src/routes/public/agents.ts +0 -373
  191. package/src/routes/public/channels.ts +0 -191
  192. package/src/routes/public/cli-auth.ts +0 -896
  193. package/src/routes/public/connections.ts +0 -574
  194. package/src/routes/public/landing.ts +0 -16
  195. package/src/routes/public/oauth.ts +0 -147
  196. package/src/routes/public/settings-auth.ts +0 -104
  197. package/src/routes/public/slack.ts +0 -173
  198. package/src/routes/shared/agent-ownership.ts +0 -101
  199. package/src/routes/shared/token-verifier.ts +0 -34
  200. package/src/services/bedrock-model-catalog.ts +0 -217
  201. package/src/services/bedrock-openai-service.ts +0 -658
  202. package/src/services/core-services.ts +0 -1072
  203. package/src/services/image-generation-service.ts +0 -257
  204. package/src/services/instruction-service.ts +0 -318
  205. package/src/services/mcp-registry.ts +0 -94
  206. package/src/services/platform-helpers.ts +0 -287
  207. package/src/services/session-manager.ts +0 -262
  208. package/src/services/settings-resolver.ts +0 -74
  209. package/src/services/system-config-resolver.ts +0 -89
  210. package/src/services/system-skills-service.ts +0 -229
  211. package/src/services/transcription-service.ts +0 -684
  212. package/src/session.ts +0 -110
  213. package/src/spaces/index.ts +0 -1
  214. package/src/spaces/space-resolver.ts +0 -17
  215. package/src/stores/in-memory-agent-store.ts +0 -403
  216. package/src/stores/redis-agent-store.ts +0 -279
  217. package/src/utils/public-url.ts +0 -44
  218. package/src/utils/rate-limiter.ts +0 -94
  219. package/tsconfig.json +0 -33
package/src/api/index.ts DELETED
@@ -1 +0,0 @@
1
- export { ApiPlatform, type ApiPlatformConfig } from "./platform";
@@ -1,292 +0,0 @@
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
- }
@@ -1,157 +0,0 @@
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
- }
@@ -1,168 +0,0 @@
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
- }
@@ -1,69 +0,0 @@
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
- }