Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { organizationComputerUseAvailabilityContract } from '@/lib/api/contracts/computer-use'
import {
defineInternalJsonRoute,
internalOrchestrationErrorPolicy,
internalRateLimits,
internalSessionAuth,
} from '@/lib/api/server/routes'
import { organizationComputerUseAvailability } from '@/lib/computer-use/application/availability'

export const GET = defineInternalJsonRoute({
contract: organizationComputerUseAvailabilityContract,
auth: internalSessionAuth,
operation: organizationComputerUseAvailability.operation,
rateLimit: internalRateLimits.user({ bucketName: 'desktop-computer-use-availability' }),
errorPolicy: internalOrchestrationErrorPolicy,
mapInput: ({ params }) => ({ organizationId: params.id }),
useCase: organizationComputerUseAvailability,
present: (available) => ({ available }),
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { workspaceComputerUseAvailabilityContract } from '@/lib/api/contracts/computer-use'
import {
defineInternalJsonRoute,
internalOrchestrationErrorPolicy,
internalRateLimits,
internalSessionAuth,
} from '@/lib/api/server/routes'
import { workspaceComputerUseAvailability } from '@/lib/computer-use/application/availability'

export const GET = defineInternalJsonRoute({
contract: workspaceComputerUseAvailabilityContract,
auth: internalSessionAuth,
operation: workspaceComputerUseAvailability.operation,
rateLimit: internalRateLimits.user({ bucketName: 'desktop-computer-use-availability' }),
errorPolicy: internalOrchestrationErrorPolicy,
mapInput: ({ params }) => ({ workspaceId: params.id }),
useCase: workspaceComputerUseAvailability,
present: (available) => ({ available }),
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import type {
ComputerUseStatus,
} from '@sim/desktop-bridge'
import { toast } from '@sim/emcn'
import { flushMacrotask } from '@sim/testing/helpers/async'
import { createDeferred } from '@sim/testing/helpers/deferred'
import { jsonResponse } from '@sim/testing/helpers/http'
import { libDesktopMock, libDesktopMockFns } from '@sim/testing/mocks/lib-desktop.mock'
import { nextNavigationMockFns } from '@sim/testing/mocks/next-navigation.mock'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { DEFAULT_PERMISSION_GROUP_CONFIG } from '@/lib/permission-groups/fields'
import { ComputerUseSettings } from '@/app/workspace/[workspaceId]/settings/components/desktop/computer-use'

const native = vi.hoisted(() => ({
Expand All @@ -21,16 +23,6 @@ const native = vi.hoisted(() => ({
cancel: vi.fn<() => Promise<void>>(),
onActivity: vi.fn<(listener: (activity: ComputerUseActivity | null) => void) => () => void>(),
}))
const permission = vi.hoisted(() => ({
config: null as Record<string, unknown> | null,
ready: true,
}))
vi.mock('@/ee/access-control/hooks/permission-groups', () => ({
useUserPermissionConfig: () =>
permission.ready
? { isSuccess: true, data: { config: permission.config } }
: { isSuccess: false, data: undefined },
}))
vi.mock(
'next/navigation',
async () => (await import('@sim/testing/mocks/next-navigation.mock')).nextNavigationMock
Expand All @@ -46,10 +38,23 @@ let activity: ComputerUseActivity | null
let listeners: Set<(activity: ComputerUseActivity | null) => void>
let root: Root
let container: HTMLDivElement
let queryClient: QueryClient
let available: boolean
let availabilitySettles: boolean
let requestedPaths: string[]

beforeEach(() => {
permission.config = null
permission.ready = true
available = true
availabilitySettles = true
requestedPaths = []
queryClient = new QueryClient()
vi.stubGlobal(
'fetch',
vi.fn(async (input: RequestInfo | URL) => {
requestedPaths.push(new URL(String(input), 'http://localhost').pathname)
return availabilitySettles ? jsonResponse({ available }) : new Promise<Response>(() => {})
})
)
nextNavigationMockFns.mockUseParams.mockReturnValue({ workspaceId: 'ws-1' })
permissions = [...initialPermissions]
activity = null
Expand Down Expand Up @@ -84,23 +89,51 @@ afterEach(async () => {
container.remove()
})

function renderSettings() {
root.render(<ComputerUseSettings />)
/** Retries the assertion, committing React's pending updates before each attempt. */
async function eventually(assertion: () => void) {
await vi.waitFor(async () => {
await act(() => flushMacrotask())
assertion()
})
}

it.each(['disableComputerUse', 'hideCopilot'])(
'withholds the device controls when the permission group sets %s',
async (key) => {
permission.config = { ...DEFAULT_PERMISSION_GROUP_CONFIG, [key]: true }
await act(async () => renderSettings())
expect(appRow('com.example.First')).toBeUndefined()
expect(container.querySelector('[role="switch"]')).toBeNull()
}
)
async function renderSettings({ awaitAvailability = true } = {}) {
await act(async () => {
root.render(
<QueryClientProvider client={queryClient}>
<ComputerUseSettings />
</QueryClientProvider>
)
})
if (awaitAvailability) await eventually(() => expect(queryClient.isFetching()).toBe(0))
}

it('withholds the device controls when the workspace group withholds computer use', async () => {
available = false
await renderSettings()
expect(requestedPaths).toEqual(['/api/workspaces/ws-1/computer-use/availability'])
expect(appRow('com.example.First')).toBeUndefined()
expect(native.listAppPermissions).not.toHaveBeenCalled()
})

it('asks the organization on organization settings and shows its controls', async () => {
nextNavigationMockFns.mockUseParams.mockReturnValue({ organizationId: 'org-1' })
await renderSettings()
expect(requestedPaths).toEqual(['/api/organizations/org-1/computer-use/availability'])
await eventually(() => expect(native.listAppPermissions).toHaveBeenCalled())
})

it('withholds the device controls where no workspace or organization governs them', async () => {
nextNavigationMockFns.mockUseParams.mockReturnValue({})
await renderSettings()
expect(requestedPaths).toEqual([])
expect(native.listAppPermissions).not.toHaveBeenCalled()
})

it('withholds the device controls until the permission read succeeds', async () => {
permission.ready = false
await act(async () => renderSettings())
it('withholds the device controls until the availability read succeeds', async () => {
availabilitySettles = false
await renderSettings({ awaitAvailability: false })
await eventually(() => expect(requestedPaths).toHaveLength(1))
expect(appRow('com.example.First')).toBeUndefined()
expect(container.querySelector('[role="switch"]')).toBeNull()
expect(native.listAppPermissions).not.toHaveBeenCalled()
Expand All @@ -122,16 +155,18 @@ it('lists approved apps only while first use of an app needs approval', async ()
computerUse: { ...native, setAskForApps: vi.fn(async (ask: boolean) => status(ask)) },
})
native.getStatus.mockResolvedValue(status(false))
await act(async () => renderSettings())
expect(container.querySelector('[aria-label="Ask before using a new app"]')).not.toBeNull()
await renderSettings()
await eventually(() =>
expect(container.querySelector('[aria-label="Ask before using a new app"]')).not.toBeNull()
)
expect(appRow('com.example.First')).toBeUndefined()
native.getStatus.mockResolvedValue(status(true))
await act(async () => window.dispatchEvent(new Event('focus')))
expect(appRow('com.example.First')).toBeDefined()
})

it('does not restore a revoked app when an earlier activity refresh settles late', async () => {
await act(async () => renderSettings())
await renderSettings()
const stale = createDeferred<ComputerUseAppPermission[]>()
native.listAppPermissions.mockImplementationOnce(() => stale.promise)
await act(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { Chip, ChipSwitch, Label, toast } from '@sim/emcn'
import { useParams } from 'next/navigation'
import { ComputerUseActivity } from '@/components/computer-use/activity'
import { getDesktopBridge } from '@/lib/desktop'
import { CAPABILITY_RULES } from '@/lib/permission-groups/capabilities'
import { SettingsSection } from '@/app/workspace/[workspaceId]/settings/components/settings-section/settings-section'
import { useUserPermissionConfig } from '@/ee/access-control/hooks/permission-groups'
import { type ComputerUseOwner, useComputerUseAvailability } from '@/hooks/queries/computer-use'
import { useComputerUseStatus } from '@/hooks/use-computer-use-status'

const ON_OFF_OPTIONS = [
Expand All @@ -18,12 +17,16 @@ const ON_OFF_OPTIONS = [

/** The activity stays visible so an in-flight action can be stopped; the device controls need the permission group's consent. */
export function ComputerUseSettings() {
const { workspaceId } = useParams<{ workspaceId: string }>()
const permission = useUserPermissionConfig(workspaceId)
const allowed =
permission.isSuccess &&
(permission.data.config === null ||
!CAPABILITY_RULES['computer_use.use'].deniedBy(permission.data.config))
const { workspaceId, organizationId } = useParams<{
workspaceId?: string
organizationId?: string
}>()
const owner: ComputerUseOwner | null = workspaceId
? { workspaceId }
: organizationId
? { organizationId }
: null
const allowed = useComputerUseAvailability(owner).data === true
return (
<>
<ComputerUseActivity />
Expand Down
41 changes: 41 additions & 0 deletions apps/sim/hooks/queries/computer-use.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useQuery } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import {
organizationComputerUseAvailabilityContract,
workspaceComputerUseAvailabilityContract,
} from '@/lib/api/contracts/computer-use'
import {
PERMISSION_GROUPS_STALE_TIME,
permissionGroupKeys,
} from '@/hooks/queries/utils/permission-group-keys'
import { shouldRetrySettingsRead } from '@/hooks/queries/utils/settings-read-retry'

/** The workspace or organization whose permission group governs computer use for its chats. */
export type ComputerUseOwner = { workspaceId: string } | { organizationId: string }

/**
* Whether the owner's permission group allows computer use, asked of the same use case the chat
* payload consults. Kept under the permission-group keys so saving a group refreshes it. Disabled
* without an owner.
*/
export function useComputerUseAvailability(owner: ComputerUseOwner | null) {
const scope = owner && 'organizationId' in owner ? 'organization' : 'workspace'
const id = owner ? ('organizationId' in owner ? owner.organizationId : owner.workspaceId) : ''
return useQuery({

Check failure on line 24 in apps/sim/hooks/queries/computer-use.ts

View workflow job for this annotation

GitHub Actions / checks / test (2/2)

app/workspace/[workspaceId]/settings/components/desktop/desktop.test.tsx > Desktop settings: prevent sleep > shows the switch once this device runs chats in the background

Error: No QueryClient set, use QueryClientProvider to set one ❯ useQueryClient ../../node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:18:11 ❯ useBaseQuery ../../node_modules/@tanstack/react-query/src/useBaseQuery.ts:54:18 ❯ useQuery ../../node_modules/@tanstack/react-query/src/useQuery.ts:51:10 ❯ useComputerUseAvailability hooks/queries/computer-use.ts:24:10 ❯ ComputerUseSettings app/workspace/[workspaceId]/settings/components/desktop/computer-use.tsx:29:19 ❯ Object.react_stack_bottom_frame ../../node_modules/react-dom/cjs/react-dom-client.development.js:25904:20 ❯ renderWithHooks ../../node_modules/react-dom/cjs/react-dom-client.development.js:7662:22 ❯ updateFunctionComponent ../../node_modules/react-dom/cjs/react-dom-client.development.js:10166:19 ❯ beginWork ../../node_modules/react-dom/cjs/react-dom-client.development.js:11778:18 ❯ runWithFiberInDEV ../../node_modules/react-dom/cjs/react-dom-client.development.js:874:13

Check failure on line 24 in apps/sim/hooks/queries/computer-use.ts

View workflow job for this annotation

GitHub Actions / checks / test (2/2)

app/workspace/[workspaceId]/settings/components/desktop/desktop.test.tsx > Desktop settings: prevent sleep > hides the switch when Sim runs no chats on this device in the background

Error: No QueryClient set, use QueryClientProvider to set one ❯ useQueryClient ../../node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:18:11 ❯ useBaseQuery ../../node_modules/@tanstack/react-query/src/useBaseQuery.ts:54:18 ❯ useQuery ../../node_modules/@tanstack/react-query/src/useQuery.ts:51:10 ❯ useComputerUseAvailability hooks/queries/computer-use.ts:24:10 ❯ ComputerUseSettings app/workspace/[workspaceId]/settings/components/desktop/computer-use.tsx:29:19 ❯ Object.react_stack_bottom_frame ../../node_modules/react-dom/cjs/react-dom-client.development.js:25904:20 ❯ renderWithHooks ../../node_modules/react-dom/cjs/react-dom-client.development.js:7662:22 ❯ updateFunctionComponent ../../node_modules/react-dom/cjs/react-dom-client.development.js:10166:19 ❯ beginWork ../../node_modules/react-dom/cjs/react-dom-client.development.js:11778:18 ❯ runWithFiberInDEV ../../node_modules/react-dom/cjs/react-dom-client.development.js:874:13
queryKey: permissionGroupKeys.computerUseAvailability(scope, id),
queryFn: async ({ signal }) => {
const { available } =
scope === 'organization'
? await requestJson(organizationComputerUseAvailabilityContract, {
params: { id },
signal,
})
: await requestJson(workspaceComputerUseAvailabilityContract, { params: { id }, signal })
return available
},
enabled: owner !== null,
staleTime: PERMISSION_GROUPS_STALE_TIME,
retry: shouldRetrySettingsRead,
retryOnMount: true,
})
}
2 changes: 2 additions & 0 deletions apps/sim/hooks/queries/utils/permission-group-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export const permissionGroupKeys = {
[...permissionGroupKeys.all, 'userConfig', workspaceId ?? ''] as const,
orgWorkspaces: (organizationId?: string) =>
[...permissionGroupKeys.all, 'orgWorkspaces', organizationId ?? ''] as const,
computerUseAvailability: (scope: 'workspace' | 'organization', id?: string) =>
[...permissionGroupKeys.all, 'computerUseAvailability', scope, id ?? ''] as const,
}
19 changes: 19 additions & 0 deletions apps/sim/lib/api/contracts/computer-use.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComputerUseSchema } from '@sim/desktop-bridge/computer-use'
import { z } from 'zod'
import { desktopToolCallIdSchema } from '@/lib/api/contracts/desktop-tool-authorization'
import { organizationIdSchema, workspaceIdSchema } from '@/lib/api/contracts/primitives'
import { defineRouteContract } from '@/lib/api/contracts/types'

const authorizeComputerUseBodySchema = z.object({ toolCallId: desktopToolCallIdSchema }).strict()
Expand All @@ -17,3 +18,21 @@ export const authorizeComputerUseContract = defineRouteContract({
body: authorizeComputerUseBodySchema,
response: { mode: 'json', schema: authorizeComputerUseResponseSchema },
})

const computerUseAvailabilityResponseSchema = z.object({ available: z.boolean() }).strict()

/** Whether the permission group governing this workspace lets its chats use computer use. */
export const workspaceComputerUseAvailabilityContract = defineRouteContract({
method: 'GET',
path: '/api/workspaces/[id]/computer-use/availability',
params: z.object({ id: workspaceIdSchema }).strict(),
response: { mode: 'json', schema: computerUseAvailabilityResponseSchema },
})

/** Whether this organization's default permission group lets its chats use computer use. */
export const organizationComputerUseAvailabilityContract = defineRouteContract({
method: 'GET',
path: '/api/organizations/[id]/computer-use/availability',
params: z.object({ id: organizationIdSchema }).strict(),
response: { mode: 'json', schema: computerUseAvailabilityResponseSchema },
})
10 changes: 6 additions & 4 deletions apps/sim/lib/computer-use/application/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const organizationAvailabilityOperation = defineOrganizationOperation({
principalKinds: ['session'],
})

const workspaceAvailability = defineAuthorizedWorkspaceUseCase({
/** Whether the permission group governing this workspace lets its chats use computer use. */
export const workspaceComputerUseAvailability = defineAuthorizedWorkspaceUseCase({
operation: workspaceAvailabilityOperation,
resolveContext: ({ input }: { input: { workspaceId: string } }) =>
resolveActiveWorkspaceApplicationContext(input.workspaceId),
Expand All @@ -40,7 +41,8 @@ const workspaceAvailability = defineAuthorizedWorkspaceUseCase({
)),
})

const organizationAvailability = defineAuthorizedOrganizationUseCase({
/** Whether this organization's default permission group lets its chats use computer use. */
export const organizationComputerUseAvailability = defineAuthorizedOrganizationUseCase({
operation: organizationAvailabilityOperation,
execute: async ({ input }: { input: { organizationId: string } }) =>
// permission-group-enforced: computer_use.use — an availability read answers false rather than 403
Expand All @@ -59,6 +61,6 @@ export function isComputerUseAvailableFor(
owner: ComputerUseOwner
): Promise<boolean> {
return 'organizationId' in owner
? organizationAvailability.execute({ principal, input: owner })
: workspaceAvailability.execute({ principal, input: owner })
? organizationComputerUseAvailability.execute({ principal, input: owner })
: workspaceComputerUseAvailability.execute({ principal, input: owner })
}
10 changes: 10 additions & 0 deletions apps/sim/lib/permission-groups/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ describe('knowledge capability rules', () => {
expect(exportRule.deniedBy(configWith({ hideKnowledgeBaseTab: true }))).toBe(true)
})
})

describe('computer use capability rule', () => {
const computerUse = CAPABILITY_RULES['computer_use.use']

it('withholds computer use from its own key and from hiding Chat, which issues the actions', () => {
expect(computerUse.deniedBy(DEFAULT_PERMISSION_GROUP_CONFIG)).toBe(false)
expect(computerUse.deniedBy(configWith({ disableComputerUse: true }))).toBe(true)
expect(computerUse.deniedBy(configWith({ hideCopilot: true }))).toBe(true)
})
})
Loading