@@ -10,6 +10,10 @@ import { z } from 'zod'
1010import { isZodError , validationErrorResponse } from '@/lib/api/server'
1111import { getSession } from '@/lib/auth'
1212import { resolveBillingAttribution } from '@/lib/billing/core/billing-attribution'
13+ import {
14+ type AccessibleWorkspace ,
15+ getAccessibleWorkspacesForCopilot ,
16+ } from '@/lib/copilot/chat/accessible-workspaces'
1317import { type ChatLoadResult , resolveOrCreateChat } from '@/lib/copilot/chat/lifecycle'
1418import { appendCopilotChatMessages } from '@/lib/copilot/chat/messages-store'
1519import { buildCopilotRequestPayload } from '@/lib/copilot/chat/payload'
@@ -278,6 +282,7 @@ type UnifiedChatBranch =
278282 prefetch ?: boolean
279283 implicitFeedback ?: string
280284 workspaceContext ?: string
285+ accessibleWorkspaces ?: AccessibleWorkspace [ ]
281286 vfs ?: VfsSnapshotV1
282287 desktopLocalFilesystem ?: boolean
283288 browserCapable ?: boolean
@@ -314,6 +319,7 @@ type UnifiedChatBranch =
314319 userTimezone ?: string
315320 userMetadata ?: { name ?: string ; email ?: string ; timezone ?: string }
316321 workspaceContext ?: string
322+ accessibleWorkspaces ?: AccessibleWorkspace [ ]
317323 vfs ?: VfsSnapshotV1
318324 desktopLocalFilesystem ?: boolean
319325 browserCapable ?: boolean
@@ -787,6 +793,7 @@ async function resolveBranch(params: {
787793 prefetch : payloadParams . prefetch ,
788794 implicitFeedback : payloadParams . implicitFeedback ,
789795 workspaceContext : payloadParams . workspaceContext ,
796+ accessibleWorkspaces : payloadParams . accessibleWorkspaces ,
790797 vfs : payloadParams . vfs ,
791798 userPermission : payloadParams . userPermission ,
792799 entitlements : payloadParams . entitlements ,
@@ -849,6 +856,7 @@ async function resolveBranch(params: {
849856 fileAttachments : payloadParams . fileAttachments ,
850857 chatId : payloadParams . chatId ,
851858 workspaceContext : payloadParams . workspaceContext ,
859+ accessibleWorkspaces : payloadParams . accessibleWorkspaces ,
852860 vfs : payloadParams . vfs ,
853861 userPermission : payloadParams . userPermission ,
854862 entitlements : payloadParams . entitlements ,
@@ -1082,6 +1090,7 @@ export async function handleUnifiedChatPost(req: NextRequest) {
10821090 const entitlementsPromise = workspaceId
10831091 ? computeWorkspaceEntitlements ( workspaceId , authenticatedUserId )
10841092 : Promise . resolve ( [ ] )
1093+ const accessibleWorkspacesPromise = getAccessibleWorkspacesForCopilot ( authenticatedUserId )
10851094 // Wrap the pre-LLM prep work in spans so the trace waterfall shows
10861095 // where time is going between "request received" and "llm.stream
10871096 // opens". Previously these ran bare under the root and inflated the
@@ -1136,15 +1145,23 @@ export async function handleUnifiedChatPost(req: NextRequest) {
11361145 activeOtelRoot . context
11371146 )
11381147
1139- const [ agentContexts , userPermission , entitlements , workspaceSnapshot , , executionContext ] =
1140- await Promise . all ( [
1141- agentContextsPromise ,
1142- userPermissionPromise ,
1143- entitlementsPromise ,
1144- workspaceContextPromise ,
1145- persistUserMessagePromise ,
1146- executionContextPromise ,
1147- ] )
1148+ const [
1149+ agentContexts ,
1150+ userPermission ,
1151+ entitlements ,
1152+ accessibleWorkspaces ,
1153+ workspaceSnapshot ,
1154+ ,
1155+ executionContext ,
1156+ ] = await Promise . all ( [
1157+ agentContextsPromise ,
1158+ userPermissionPromise ,
1159+ entitlementsPromise ,
1160+ accessibleWorkspacesPromise ,
1161+ workspaceContextPromise ,
1162+ persistUserMessagePromise ,
1163+ executionContextPromise ,
1164+ ] )
11481165 // Both halves come from one primary-db fetch (workspace-context.ts):
11491166 // `workspaceContext` is the markdown transition fallback, `vfs` is the
11501167 // typed snapshot Go diffs into baseline+delta messages.
@@ -1189,6 +1206,7 @@ export async function handleUnifiedChatPost(req: NextRequest) {
11891206 prefetch : body . prefetch ,
11901207 implicitFeedback : body . implicitFeedback ,
11911208 workspaceContext,
1209+ accessibleWorkspaces,
11921210 vfs,
11931211 desktopLocalFilesystem : body . desktopCapabilities ?. localFilesystem === true ,
11941212 browserCapable :
@@ -1210,6 +1228,7 @@ export async function handleUnifiedChatPost(req: NextRequest) {
12101228 userTimezone : body . userTimezone ,
12111229 userMetadata,
12121230 workspaceContext,
1231+ accessibleWorkspaces,
12131232 vfs,
12141233 desktopLocalFilesystem : body . desktopCapabilities ?. localFilesystem === true ,
12151234 browserCapable :
0 commit comments