-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add direct sessions API for browser/CLI clients #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { ApiPlatform, type ApiPlatformConfig } from "./platform"; | ||
| export { ApiResponseRenderer } from "./response-renderer"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #!/usr/bin/env bun | ||
|
|
||
| /** | ||
| * API Platform Adapter | ||
| * Handles direct API access for browser extensions, CLI clients, etc. | ||
| * Does not require external platform integration (no Slack, Discord, etc.) | ||
| */ | ||
|
|
||
| import { createLogger, type InstructionProvider, type UserInteraction } from "@peerbot/core"; | ||
| import type { CoreServices, PlatformAdapter } from "../platform"; | ||
| import { ApiResponseRenderer } from "./response-renderer"; | ||
| import type { ResponseRenderer } from "../platform/response-renderer"; | ||
| import { broadcastToSession } from "../routes/public/sessions"; | ||
|
|
||
| const logger = createLogger("api-platform"); | ||
|
|
||
| /** | ||
| * API Platform configuration | ||
| */ | ||
| export interface ApiPlatformConfig { | ||
| /** Whether the API platform is enabled */ | ||
| enabled?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * API Platform adapter for direct access via HTTP/SSE | ||
| * This platform doesn't interact with external services like Slack or Discord. | ||
| * Instead, it provides endpoints for: | ||
| * - Creating sessions | ||
| * - Sending messages | ||
| * - Receiving streaming responses via SSE | ||
| * - Handling tool approvals | ||
| */ | ||
| export class ApiPlatform implements PlatformAdapter { | ||
| readonly name = "api"; | ||
|
|
||
| private services?: CoreServices; | ||
| private responseRenderer?: ApiResponseRenderer; | ||
| private isRunning = false; | ||
|
|
||
| constructor(private readonly config: ApiPlatformConfig = {}) {} | ||
|
|
||
| /** | ||
| * Initialize with core services | ||
| */ | ||
| async initialize(services: CoreServices): Promise<void> { | ||
| logger.info("Initializing API platform..."); | ||
| this.services = services; | ||
|
|
||
| // Create response renderer for routing worker responses to SSE clients | ||
| this.responseRenderer = new ApiResponseRenderer(); | ||
|
|
||
| // Subscribe to interaction events to handle tool approvals | ||
| const interactionService = services.getInteractionService(); | ||
| interactionService.on("interaction:created", (interaction) => { | ||
| // Only handle API platform interactions | ||
| if (interaction.teamId === "api" || interaction.spaceId?.startsWith("api-")) { | ||
| this.handleToolApproval(interaction).catch((error) => { | ||
| logger.error("Failed to handle tool approval:", error); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| logger.info("✅ API platform initialized"); | ||
| } | ||
|
|
||
| /** | ||
| * Start the platform | ||
| * For API platform, this is mostly a no-op since routes are registered separately | ||
| */ | ||
| async start(): Promise<void> { | ||
| this.isRunning = true; | ||
| logger.info("✅ API platform started"); | ||
| } | ||
|
|
||
| /** | ||
| * Stop the platform | ||
| */ | ||
| async stop(): Promise<void> { | ||
| this.isRunning = false; | ||
| logger.info("✅ API platform stopped"); | ||
| } | ||
|
|
||
| /** | ||
| * Check if platform is healthy | ||
| */ | ||
| isHealthy(): boolean { | ||
| return this.isRunning; | ||
| } | ||
|
|
||
| /** | ||
| * No custom instruction provider for API platform | ||
| */ | ||
| getInstructionProvider(): InstructionProvider | null { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Build deployment metadata | ||
| * For API sessions, we include session ID and source | ||
| */ | ||
| buildDeploymentMetadata( | ||
| threadId: string, | ||
| channelId: string, | ||
| platformMetadata: Record<string, any> | ||
| ): Record<string, string> { | ||
| return { | ||
| sessionId: platformMetadata.sessionId || threadId, | ||
| source: "direct-api", | ||
| channelId, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the response renderer for routing worker responses | ||
| */ | ||
| getResponseRenderer(): ResponseRenderer | undefined { | ||
| return this.responseRenderer; | ||
| } | ||
|
|
||
| /** | ||
| * Handle tool approval requests by sending them via SSE | ||
| */ | ||
| private async handleToolApproval(interaction: UserInteraction): Promise<void> { | ||
| const sessionId = interaction.threadId; | ||
| if (!sessionId) { | ||
| logger.warn("No session ID found for tool approval interaction"); | ||
| return; | ||
| } | ||
|
|
||
| // Send tool approval request to SSE clients | ||
| broadcastToSession(sessionId, "tool_approval", { | ||
| type: "tool_approval", | ||
| interactionId: interaction.id, | ||
| title: interaction.title, | ||
| message: interaction.message, | ||
| fields: interaction.fields, | ||
| buttons: interaction.buttons, | ||
| expiresAt: interaction.expiresAt, | ||
| timestamp: Date.now(), | ||
| }); | ||
|
|
||
| logger.info(`Sent tool approval to session ${sessionId}: ${interaction.id}`); | ||
| } | ||
|
|
||
| /** | ||
| * API platform doesn't render interactions via platform UI | ||
| * Instead, interactions are sent via SSE to the client | ||
| */ | ||
| async renderInteraction(): Promise<void> { | ||
| // Interactions are handled via SSE in the response renderer | ||
| } | ||
|
|
||
| /** | ||
| * API platform doesn't render suggestions via platform UI | ||
| */ | ||
| async renderSuggestion(): Promise<void> { | ||
| // Suggestions are handled via SSE in the response renderer | ||
| } | ||
|
|
||
| /** | ||
| * API platform doesn't have thread status indicators | ||
| */ | ||
| async setThreadStatus(): Promise<void> { | ||
| // Status is sent via SSE events | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/usr/bin/env bun | ||
|
|
||
| /** | ||
| * API Response Renderer | ||
| * Broadcasts worker responses to SSE connections for direct API clients | ||
| */ | ||
|
|
||
| import { createLogger } from "@peerbot/core"; | ||
| import type { ThreadResponsePayload } from "../infrastructure/queue/types"; | ||
| import type { ResponseRenderer } from "../platform/response-renderer"; | ||
| import { broadcastToSession } from "../routes/public/sessions"; | ||
|
|
||
| const logger = createLogger("api-response-renderer"); | ||
|
|
||
| /** | ||
| * Response renderer for API platform | ||
| * Broadcasts responses to SSE clients instead of external platforms | ||
| */ | ||
| export class ApiResponseRenderer implements ResponseRenderer { | ||
| /** | ||
| * Handle streaming delta content | ||
| * Broadcasts delta to SSE connections | ||
| */ | ||
| async handleDelta( | ||
| payload: ThreadResponsePayload, | ||
| sessionKey: string | ||
| ): Promise<string | null> { | ||
| // Extract session ID from platformMetadata or thread ID | ||
| const sessionId = | ||
| (payload.platformMetadata?.sessionId as string) || payload.threadId; | ||
|
|
||
| if (!sessionId) { | ||
| logger.warn("No session ID found in payload for delta broadcast"); | ||
| return null; | ||
| } | ||
|
|
||
| // Broadcast delta to SSE clients | ||
| broadcastToSession(sessionId, "output", { | ||
| type: "delta", | ||
| content: payload.delta, | ||
| timestamp: payload.timestamp || Date.now(), | ||
| messageId: payload.messageId, | ||
| }); | ||
|
|
||
| logger.debug( | ||
| `Broadcast delta to session ${sessionId}: ${payload.delta?.length || 0} chars` | ||
| ); | ||
|
|
||
| return payload.messageId; | ||
| } | ||
|
|
||
| /** | ||
| * Handle completion of response processing | ||
| * Sends completion event to SSE clients | ||
| */ | ||
| async handleCompletion( | ||
| payload: ThreadResponsePayload, | ||
| sessionKey: string | ||
| ): Promise<void> { | ||
| const sessionId = | ||
| (payload.platformMetadata?.sessionId as string) || payload.threadId; | ||
|
|
||
| if (!sessionId) { | ||
| logger.warn("No session ID found in payload for completion broadcast"); | ||
| return; | ||
| } | ||
|
|
||
| // Broadcast completion to SSE clients | ||
| broadcastToSession(sessionId, "complete", { | ||
| type: "complete", | ||
| messageId: payload.messageId, | ||
| processedMessageIds: payload.processedMessageIds, | ||
| timestamp: payload.timestamp || Date.now(), | ||
| }); | ||
|
|
||
| logger.info(`Broadcast completion to session ${sessionId}`); | ||
| } | ||
|
|
||
| /** | ||
| * Handle error response | ||
| * Sends error event to SSE clients | ||
| */ | ||
| async handleError( | ||
| payload: ThreadResponsePayload, | ||
| sessionKey: string | ||
| ): Promise<void> { | ||
| const sessionId = | ||
| (payload.platformMetadata?.sessionId as string) || payload.threadId; | ||
|
|
||
| if (!sessionId) { | ||
| logger.warn("No session ID found in payload for error broadcast"); | ||
| return; | ||
| } | ||
|
|
||
| // Broadcast error to SSE clients | ||
| broadcastToSession(sessionId, "error", { | ||
| type: "error", | ||
| error: payload.error, | ||
| messageId: payload.messageId, | ||
| timestamp: payload.timestamp || Date.now(), | ||
| }); | ||
|
|
||
| logger.error(`Broadcast error to session ${sessionId}: ${payload.error}`); | ||
| } | ||
|
|
||
| /** | ||
| * Handle status updates (heartbeat with elapsed time) | ||
| * Sends status event to SSE clients | ||
| */ | ||
| async handleStatusUpdate(payload: ThreadResponsePayload): Promise<void> { | ||
| const sessionId = | ||
| (payload.platformMetadata?.sessionId as string) || payload.threadId; | ||
|
|
||
| if (!sessionId) { | ||
| return; | ||
| } | ||
|
|
||
| // Broadcast status to SSE clients | ||
| broadcastToSession(sessionId, "status", { | ||
| type: "status", | ||
| status: payload.statusUpdate, | ||
| messageId: payload.messageId, | ||
| timestamp: payload.timestamp || Date.now(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Handle ephemeral messages | ||
| * For API platform, these are just broadcast as regular events | ||
| */ | ||
| async handleEphemeral(payload: ThreadResponsePayload): Promise<void> { | ||
| const sessionId = | ||
| (payload.platformMetadata?.sessionId as string) || payload.threadId; | ||
|
|
||
| if (!sessionId) { | ||
| return; | ||
| } | ||
|
|
||
| // Broadcast ephemeral content to SSE clients | ||
| broadcastToSession(sessionId, "ephemeral", { | ||
| type: "ephemeral", | ||
| content: payload.content, | ||
| messageId: payload.messageId, | ||
| timestamp: payload.timestamp || Date.now(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Stop stream for thread - no-op for API platform | ||
| * SSE connections handle their own lifecycle | ||
| */ | ||
| async stopStreamForThread(_userId: string, _threadId: string): Promise<void> { | ||
| // No-op - SSE connections manage their own lifecycle | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tool approval requests never reach SSE clients
High Severity
The
ApiPlatformnever subscribes tointeraction:createdevents fromInteractionService, unlike Slack and WhatsApp platforms which both listen for this event to render tool approvals. TherenderInteraction()method is a no-op with a comment claiming "Interactions are handled via SSE in the response renderer," butApiResponseRendererhas no method to handle interactions. This means when a worker requests tool approval, the SSE client never receives thetool_approvalevent documented in the PR, breaking the entire approval flow.Additional Locations (1)
packages/gateway/src/api/platform.ts#L44-L52