Repository navigation
feat(desktop): show background desktop chats and keep the chat view display-only for them #8670
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
7 commits
Select commit
Hold shift + click to select a range
433051f
feat(desktop): show which chats a desktop runs in the background
waleedlatif1 69c4dfb
fix(desktop): learn a turn's executor from its stream and announce ba…
waleedlatif1 c278450
fix(desktop): keep background chat signals behind the flag and to wor…
waleedlatif1 b34c8cf
test(desktop): observe what the chat view starts and announces instea…
waleedlatif1 50bb444
fix(desktop): keep the old notification rule with the flag off, and a…
waleedlatif1 9518d43
fix(desktop): announce a background completion only for the very turn…
waleedlatif1 684261b
style(desktop): color the background activity dot with Tailwind classes
waleedlatif1 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,91 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('electron', () => import('@/test/electron-mock')) | ||
|
|
||
| import type { BrowserWindow } from 'electron' | ||
| import { createDesktopSettingsService } from '@/main/desktop-settings' | ||
| import { Notification } from '@/test/electron-mock' | ||
|
|
||
| function service(window: { url: string; focused: boolean } | null) { | ||
| const config = new Map<string, unknown>() | ||
| return createDesktopSettingsService({ | ||
| config: { | ||
| get: (key: string) => config.get(key), | ||
| set: (key: string, value: unknown) => config.set(key, value), | ||
| flush: () => true, | ||
| } as never, | ||
| getMainWindow: () => | ||
| window | ||
| ? ({ | ||
| isFocused: () => window.focused, | ||
| webContents: { getURL: () => window.url }, | ||
| } as unknown as BrowserWindow) | ||
| : null, | ||
| openMainWindowAt: vi.fn(), | ||
| setAutoDownloadUpdates: vi.fn(), | ||
| setTrayEnabled: vi.fn(), | ||
| setBrowserEnabled: vi.fn(), | ||
| setTerminalEnabled: vi.fn(), | ||
| setBrowserTheme: vi.fn(), | ||
| setBrowserDefaultZoom: vi.fn(), | ||
| setTerminalDefaultZoom: vi.fn(), | ||
| getDefaultBrowserDownloadDirectory: () => '/tmp', | ||
| chooseBrowserDownloadDirectory: async () => null, | ||
| }) | ||
| } | ||
|
|
||
| const BACKGROUND_CHAT = '/workspace/ws-1/chat/chat-b' | ||
|
|
||
| describe('desktop notifications in the default "background only" mode', () => { | ||
| beforeEach(() => { | ||
| Notification.instances.length = 0 | ||
| }) | ||
|
|
||
| it('announces a chat in the background while the user works in another one', () => { | ||
| const shown = service({ | ||
| url: 'https://sim.ai/workspace/ws-1/chat/chat-c', | ||
| focused: true, | ||
| }).notify({ | ||
| title: 'Fix CI', | ||
| body: 'Sim finished responding.', | ||
| route: BACKGROUND_CHAT, | ||
| background: true, | ||
| }) | ||
|
|
||
| expect(shown).toBe(true) | ||
| expect(Notification.instances).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('stays quiet about the chat the focused window shows', () => { | ||
| const shown = service({ url: `https://sim.ai${BACKGROUND_CHAT}`, focused: true }).notify({ | ||
| title: 'Fix CI', | ||
| body: 'Sim finished responding.', | ||
| route: BACKGROUND_CHAT, | ||
| background: true, | ||
| }) | ||
|
|
||
| expect(shown).toBe(false) | ||
| expect(Notification.instances).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('stays quiet about the app as a whole while it is focused', () => { | ||
| const shown = service({ url: 'https://sim.ai/workspace/ws-1/home', focused: true }).notify({ | ||
| title: 'Update ready', | ||
| body: 'Restart to update.', | ||
| }) | ||
|
|
||
| expect(shown).toBe(false) | ||
| }) | ||
|
|
||
| it('holds back an ordinary notification whenever the window is focused, wherever it is', () => { | ||
| // The workflow panel's chat completes while the user watches it on the workflow page. | ||
| const shown = service({ url: 'https://sim.ai/workspace/ws-1/w/wf-1', focused: true }).notify({ | ||
| title: 'Task complete', | ||
| body: 'Sim finished responding.', | ||
| route: BACKGROUND_CHAT, | ||
| }) | ||
|
|
||
| expect(shown).toBe(false) | ||
| expect(Notification.instances).toHaveLength(0) | ||
| }) | ||
| }) |
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
134 changes: 134 additions & 0 deletions
134
apps/sim/app/api/copilot/chat/stream/desktop-executor.integration.ts
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,134 @@ | ||
| /** | ||
| * A chat view learns from the stream it reads whether the desktop app's background executor runs | ||
| * the turn's desktop tools, so it only shows those calls. Exercised against real PostgreSQL and | ||
| * Redis through the production reconnect route, for the live tail and the replay batch a view | ||
| * attaches with after a reload. | ||
| */ | ||
| import { authMock, authMockFns } from '@sim/testing/mocks/auth.mock' | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { redisUrl } = await vi.hoisted(async () => { | ||
| const { readTestRedisUrl } = await import('@sim/db/testing/test-infrastructure') | ||
| const url = readTestRedisUrl() | ||
| if (url) process.env.REDIS_URL = url | ||
| return { redisUrl: url } | ||
| }) | ||
|
|
||
| vi.mock('@/lib/auth', () => authMock) | ||
|
|
||
| import { db } from '@sim/db' | ||
| import { | ||
| copilotChats, | ||
| copilotRuns, | ||
| desktopDevices, | ||
| permissions, | ||
| user, | ||
| workspace, | ||
| } from '@sim/db/schema' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { eq } from 'drizzle-orm' | ||
| import { NextRequest } from 'next/server' | ||
| import { closeRedisConnection } from '@/lib/core/config/redis' | ||
| import { MOTHERSHIP_DESKTOP_EXECUTOR_HEADER } from '@/lib/mothership/constants' | ||
| import { GET as streamGET } from '@/app/api/copilot/chat/stream/route' | ||
|
|
||
| const userId = generateId() | ||
| const workspaceId = generateId() | ||
| const chatId = generateId() | ||
| const deviceId = generateId() | ||
|
|
||
| async function finishedRun(desktopDeviceId: string | null): Promise<string> { | ||
| const streamId = generateId() | ||
| await db.insert(copilotRuns).values({ | ||
| id: generateId(), | ||
| executionId: generateId(), | ||
| chatId, | ||
| userId, | ||
| workspaceId, | ||
| streamId, | ||
| status: 'complete', | ||
| desktopDeviceId, | ||
| }) | ||
| return streamId | ||
| } | ||
|
|
||
| function reconnect(streamId: string, batch: boolean) { | ||
| return streamGET( | ||
| new NextRequest( | ||
| `http://localhost:3000/api/copilot/chat/stream?streamId=${streamId}&after=0${batch ? '&batch=true' : ''}` | ||
| ), | ||
| { params: Promise.resolve({}) } | ||
| ) | ||
| } | ||
|
|
||
| describe.runIf(Boolean(redisUrl))('desktop executor binding on a chat stream', () => { | ||
| beforeAll(async () => { | ||
| const now = new Date() | ||
| await db.insert(user).values({ | ||
| id: userId, | ||
| name: 'Desktop stream fixture', | ||
| email: `${userId}@desktop-stream.test`, | ||
| emailVerified: true, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| }) | ||
| await db.insert(workspace).values({ | ||
| id: workspaceId, | ||
| name: 'Desktop stream fixture', | ||
| ownerId: userId, | ||
| billedAccountUserId: userId, | ||
| }) | ||
| await db.insert(permissions).values({ | ||
| id: generateId(), | ||
| userId, | ||
| entityType: 'workspace', | ||
| entityId: workspaceId, | ||
| permissionType: 'admin', | ||
| }) | ||
| await db.insert(copilotChats).values({ id: chatId, userId, workspaceId, type: 'mothership' }) | ||
| await db.insert(desktopDevices).values({ | ||
| id: deviceId, | ||
| userId, | ||
| name: 'Studio Mac', | ||
| appVersion: '0.9.0', | ||
| platform: 'darwin-arm64', | ||
| capabilities: { executor: 1, browser: true, terminal: true, localFiles: true }, | ||
| }) | ||
| authMockFns.mockGetSession.mockResolvedValue({ | ||
| user: { id: userId }, | ||
| session: { id: generateId() }, | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await db.delete(copilotRuns).where(eq(copilotRuns.chatId, chatId)) | ||
| await db.delete(copilotChats).where(eq(copilotChats.id, chatId)) | ||
| await db.delete(desktopDevices).where(eq(desktopDevices.id, deviceId)) | ||
| await db.delete(permissions).where(eq(permissions.userId, userId)) | ||
| await db.delete(workspace).where(eq(workspace.id, workspaceId)) | ||
| await db.delete(user).where(eq(user.id, userId)) | ||
| await closeRedisConnection() | ||
| }) | ||
|
|
||
| it('marks the live tail and the replay batch of a turn a desktop runs', async () => { | ||
| const streamId = await finishedRun(deviceId) | ||
|
|
||
| const tail = await reconnect(streamId, false) | ||
| await tail.text() | ||
| const batch = await (await reconnect(streamId, true)).json() | ||
|
|
||
| expect(tail.headers.get(MOTHERSHIP_DESKTOP_EXECUTOR_HEADER)).toBe('device') | ||
| expect(batch.desktopToolsOnDevice).toBe(true) | ||
| }) | ||
|
|
||
| it('leaves a turn the chat view runs unmarked', async () => { | ||
| const streamId = await finishedRun(null) | ||
|
|
||
| const tail = await reconnect(streamId, false) | ||
| await tail.text() | ||
| const batch = await (await reconnect(streamId, true)).json() | ||
|
|
||
| expect(tail.headers.get(MOTHERSHIP_DESKTOP_EXECUTOR_HEADER)).toBeNull() | ||
| expect(batch.desktopToolsOnDevice).toBeUndefined() | ||
| }) | ||
| }) |
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,20 @@ | ||
| import { listDesktopActivityContract } from '@/lib/api/contracts/desktop-executor' | ||
| import { defineInternalJsonRoute, internalSessionAuth } from '@/lib/api/server/routes' | ||
| import { | ||
| desktopActivityRateLimit, | ||
| desktopExecutorErrorPolicy, | ||
| } from '@/lib/api/server/routes/desktop-executor' | ||
| import { listDesktopActivity } from '@/lib/desktop/application/activity' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| export const GET = defineInternalJsonRoute({ | ||
| contract: listDesktopActivityContract, | ||
| auth: internalSessionAuth, | ||
| operation: listDesktopActivity.operation, | ||
| rateLimit: desktopActivityRateLimit, | ||
| errorPolicy: desktopExecutorErrorPolicy, | ||
| mapInput: ({ query }) => ({ workspaceId: query.workspaceId }), | ||
| useCase: listDesktopActivity, | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.