Repository navigation
feat(desktop): device registry, inbox and leased claims for a background executor #8644
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
+33,217
−85
Merged
Changes from all commits
Commits
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
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,19 @@ | ||
| import { registerDesktopDeviceContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { desktopExecutorErrorPolicy } from '@/lib/api/server/routes/desktop-executor' | ||
| import { registerDesktopDevice } from '@/lib/desktop/application/executor' | ||
|
|
||
| export const POST = defineInternalJsonRoute({ | ||
| contract: registerDesktopDeviceContract, | ||
| auth: internalSessionAuth, | ||
| operation: registerDesktopDevice.operation, | ||
| rateLimit: internalRateLimits.user({ bucketName: 'desktop-device-register' }), | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ body }) => body, | ||
| useCase: registerDesktopDevice, | ||
| staticResponseHeaders: { 'Cache-Control': 'no-store' }, | ||
| }) |
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,25 @@ | ||
| import { listDesktopInboxContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes' | ||
| import { | ||
| desktopExecutorErrorPolicy, | ||
| desktopExecutorRateLimit, | ||
| } from '@/lib/api/server/routes/desktop-executor' | ||
| import { listDesktopInbox } from '@/lib/desktop/application/executor' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| export const GET = defineInternalJsonRoute({ | ||
| contract: listDesktopInboxContract, | ||
| auth: internalSessionAuth, | ||
| operation: listDesktopInbox.operation, | ||
| rateLimit: desktopExecutorRateLimit, | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ query }) => ({ deviceId: query.deviceId }), | ||
| useCase: listDesktopInbox, | ||
| present: ({ items }) => ({ | ||
| items: items.map((item) => | ||
| item.kind === 'call' ? { ...item, createdAt: item.createdAt.toISOString() } : item | ||
| ), | ||
| }), | ||
| staticResponseHeaders: { 'Cache-Control': 'no-store' }, | ||
| }) |
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,45 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import type { NextRequest } from 'next/server' | ||
| import { desktopInboxStreamContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { desktopExecutorRateLimit } from '@/lib/api/server/routes/desktop-executor' | ||
| import { | ||
| InternalUnauthenticatedError, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes/internal-json-route' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { openDesktopInboxStream } from '@/lib/desktop/application/executor' | ||
| import { DesktopDeviceUnrecognizedError } from '@/lib/desktop/executor/errors' | ||
| import { createSSEStream } from '@/lib/events/sse-endpoint' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const logger = createLogger('DesktopInboxStream') | ||
|
|
||
| /** | ||
| * The background executor's doorbell. A raw route because it streams: authentication, the device | ||
| * check, and presence all run through the `openDesktopInboxStream` use case. | ||
| */ | ||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| const principal = await internalSessionAuth.authenticate() | ||
| const limited = await desktopExecutorRateLimit.enforce(request, principal) | ||
| if (limited) return limited | ||
| const parsed = await parseRequest(desktopInboxStreamContract, request, {}) | ||
| if (!parsed.success) return parsed.response | ||
| const { deviceId } = parsed.data.query | ||
| const inbox = await openDesktopInboxStream.execute({ principal, input: { deviceId } }) | ||
| return createSSEStream(request, { | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| label: 'desktop-inbox', | ||
| revalidate: inbox.revalidate, | ||
| subscriptions: [{ subscribe: inbox.subscribe }], | ||
| }) | ||
| } catch (error) { | ||
| if (error instanceof InternalUnauthenticatedError) | ||
| return new Response('Unauthorized', { status: 401 }) | ||
| if (error instanceof DesktopDeviceUnrecognizedError) | ||
| return new Response(error.message, { status: 401 }) | ||
| logger.error('Failed to open the desktop inbox stream', error) | ||
| return new Response('Unable to open the desktop inbox', { status: 500 }) | ||
| } | ||
| }) | ||
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,18 @@ | ||
| import { claimDesktopToolContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes' | ||
| import { | ||
| desktopExecutorErrorPolicy, | ||
| desktopExecutorRateLimit, | ||
| } from '@/lib/api/server/routes/desktop-executor' | ||
| import { claimDesktopTool } from '@/lib/desktop/application/executor' | ||
|
|
||
| export const POST = defineInternalJsonRoute({ | ||
| contract: claimDesktopToolContract, | ||
| auth: internalSessionAuth, | ||
| operation: claimDesktopTool.operation, | ||
| rateLimit: desktopExecutorRateLimit, | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ body }) => body, | ||
| useCase: claimDesktopTool, | ||
| staticResponseHeaders: { 'Cache-Control': 'no-store' }, | ||
| }) |
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,19 @@ | ||
| import { completeDesktopToolContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes' | ||
| import { | ||
| desktopExecutorErrorPolicy, | ||
| desktopExecutorRateLimit, | ||
| } from '@/lib/api/server/routes/desktop-executor' | ||
| import { completeDesktopTool } from '@/lib/desktop/application/executor' | ||
|
|
||
| export const POST = defineInternalJsonRoute({ | ||
| contract: completeDesktopToolContract, | ||
| auth: internalSessionAuth, | ||
| operation: completeDesktopTool.operation, | ||
| rateLimit: desktopExecutorRateLimit, | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ body }) => body, | ||
| useCase: completeDesktopTool, | ||
| present: ({ outcome, status }) => ({ outcome, status }), | ||
| staticResponseHeaders: { 'Cache-Control': 'no-store' }, | ||
| }) |
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,18 @@ | ||
| import { renewDesktopToolLeaseContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes' | ||
| import { | ||
| desktopExecutorErrorPolicy, | ||
| desktopExecutorRateLimit, | ||
| } from '@/lib/api/server/routes/desktop-executor' | ||
| import { renewDesktopToolLease } from '@/lib/desktop/application/executor' | ||
|
|
||
| export const POST = defineInternalJsonRoute({ | ||
| contract: renewDesktopToolLeaseContract, | ||
| auth: internalSessionAuth, | ||
| operation: renewDesktopToolLease.operation, | ||
| rateLimit: desktopExecutorRateLimit, | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ body }) => body, | ||
| useCase: renewDesktopToolLease, | ||
| staticResponseHeaders: { 'Cache-Control': 'no-store' }, | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.