@@ -13,7 +13,7 @@ import { OrchestrationError } from '@/lib/core/orchestration/types'
1313import { getLatestRunForStream } from '@/lib/mothership/async-runs/repository'
1414import { defineAuthorizedChatUseCase } from '@/lib/mothership/chat/application/authorized-chat-use-case'
1515import { resolveOwnedChatContext } from '@/lib/mothership/chat/application/context'
16- import type { ChatTurnLogContext } from '@/lib/mothership/chat/chat-log'
16+ import { type ChatTurnLogContext , readChatLogEmail } from '@/lib/mothership/chat/chat-log'
1717import { buildOnComplete , buildOnError } from '@/lib/mothership/chat/completion'
1818import { restoreBillingAdmission } from '@/lib/mothership/request/lifecycle/admission'
1919import { claimRunController } from '@/lib/mothership/request/lifecycle/controller-ownership'
@@ -108,7 +108,8 @@ export const readChatStream = defineAuthorizedChatUseCase({
108108 organizationId,
109109 } )
110110 : undefined
111- const [ events , billingAttribution , userPermission ] = await Promise . all ( [
111+ const logsChatTurn = config . data . goRoute === '/api/mothership'
112+ const [ events , billingAttribution , userPermission , userEmail ] = await Promise . all ( [
112113 readEvents ( run . streamId , '0' ) ,
113114 restoredAdmission
114115 ? Promise . resolve ( restoredAdmission . attribution )
@@ -118,6 +119,7 @@ export const readChatStream = defineAuthorizedChatUseCase({
118119 workspaceId
119120 ? getUserEntityPermissions ( userId , 'workspace' , workspaceId )
120121 : Promise . resolve ( undefined ) ,
122+ logsChatTurn ? readChatLogEmail ( userId ) : Promise . resolve ( undefined ) ,
121123 ] )
122124 if ( workspaceId && ! userPermission )
123125 throw new OrchestrationError ( 'forbidden' , 'Workspace access revoked' )
@@ -134,13 +136,14 @@ export const readChatStream = defineAuthorizedChatUseCase({
134136 requestMode,
135137 notifyWorkspaceStatus : true ,
136138 runController : { id : run . id , token : lease . value } ,
137- ...( config . data . goRoute === '/api/mothership'
139+ ...( logsChatTurn
138140 ? {
139141 chatLog : {
140142 chatId,
141143 messageId : run . streamId ,
142144 requestId,
143145 userId,
146+ ...( userEmail ? { userEmail } : { } ) ,
144147 userMessage : intent . message ,
145148 mode : requestMode ,
146149 startedAt : run . startedAt . getTime ( ) ,
0 commit comments