Skip to content

Commit 0695a8f

Browse files
committed
Add dynamic user info
1 parent b61089d commit 0695a8f

5 files changed

Lines changed: 64 additions & 9 deletions

File tree

apps/sim/app/api/mothership/execute/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
102102
fileAttachments,
103103
workflowId,
104104
executionId,
105+
userMetadata,
105106
} = validation.data.body
106107

107108
await assertActiveWorkspaceAccess(workspaceId, userId)
@@ -131,6 +132,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
131132
messageId,
132133
isHosted: true,
133134
workspaceContext,
135+
...(userMetadata ? { userMetadata } : {}),
134136
...(fileAttachments && fileAttachments.length > 0 ? { fileAttachments } : {}),
135137
...(integrationTools.length > 0 ? { integrationTools } : {}),
136138
...(mothershipToolRuntime.tools.length > 0

apps/sim/lib/api/contracts/mothership-tasks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export const mothershipExecuteBodySchema = z.object({
7878
fileAttachments: z.array(mothershipExecuteFileAttachmentSchema).optional(),
7979
workflowId: z.string().optional(),
8080
executionId: z.string().optional(),
81+
userMetadata: z
82+
.object({
83+
name: z.string().optional(),
84+
timezone: z.string().optional(),
85+
})
86+
.optional(),
8187
})
8288
export type MothershipExecuteBody = z.input<typeof mothershipExecuteBodySchema>
8389

apps/sim/lib/copilot/chat/payload.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,33 @@ describe('buildCopilotRequestPayload', () => {
150150
})
151151
)
152152
})
153+
154+
it('passes user metadata through to the Go request payload', async () => {
155+
const payload = await buildCopilotRequestPayload(
156+
{
157+
message: 'what time is it',
158+
userId: 'user-1',
159+
userMessageId: 'msg-1',
160+
mode: 'agent',
161+
model: 'claude-opus-4-8',
162+
workspaceId: 'ws-1',
163+
userTimezone: 'America/Los_Angeles',
164+
userMetadata: {
165+
name: 'Sid',
166+
timezone: 'America/Los_Angeles',
167+
},
168+
},
169+
{ selectedModel: 'claude-opus-4-8' }
170+
)
171+
172+
expect(payload).toEqual(
173+
expect.objectContaining({
174+
userTimezone: 'America/Los_Angeles',
175+
userMetadata: {
176+
name: 'Sid',
177+
timezone: 'America/Los_Angeles',
178+
},
179+
})
180+
)
181+
})
153182
})

apps/sim/lib/copilot/chat/payload.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ interface BuildPayloadParams {
4141
workspaceContext?: string
4242
userPermission?: string
4343
userTimezone?: string
44+
userMetadata?: {
45+
name?: string
46+
timezone?: string
47+
}
4448
includeMothershipTools?: boolean
4549
}
4650

@@ -317,6 +321,9 @@ export async function buildCopilotRequestPayload(
317321
...(params.workspaceContext ? { workspaceContext: params.workspaceContext } : {}),
318322
...(params.userPermission ? { userPermission: params.userPermission } : {}),
319323
...(params.userTimezone ? { userTimezone: params.userTimezone } : {}),
324+
...(params.userMetadata && (params.userMetadata.name || params.userMetadata.timezone)
325+
? { userMetadata: params.userMetadata }
326+
: {}),
320327
isHosted,
321328
}
322329
}

apps/sim/lib/copilot/chat/post.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ type UnifiedChatBranch =
164164
fileAttachments?: UnifiedChatRequest['fileAttachments']
165165
userPermission?: string
166166
userTimezone?: string
167+
userMetadata?: { name?: string; timezone?: string }
167168
workflowId: string
168169
workflowName?: string
169170
workspaceId?: string
@@ -198,6 +199,7 @@ type UnifiedChatBranch =
198199
fileAttachments?: UnifiedChatRequest['fileAttachments']
199200
userPermission?: string
200201
userTimezone?: string
202+
userMetadata?: { name?: string; timezone?: string }
201203
workspaceContext?: string
202204
}) => Promise<Record<string, unknown>>
203205
buildExecutionContext: (params: {
@@ -586,6 +588,7 @@ async function resolveBranch(params: {
586588
workspaceContext: payloadParams.workspaceContext,
587589
userPermission: payloadParams.userPermission,
588590
userTimezone: payloadParams.userTimezone,
591+
userMetadata: payloadParams.userMetadata,
589592
},
590593
{ selectedModel }
591594
),
@@ -644,6 +647,7 @@ async function resolveBranch(params: {
644647
workspaceContext: payloadParams.workspaceContext,
645648
userPermission: payloadParams.userPermission,
646649
userTimezone: payloadParams.userTimezone,
650+
userMetadata: payloadParams.userMetadata,
647651
includeMothershipTools: true,
648652
},
649653
{ selectedModel: '' }
@@ -686,8 +690,14 @@ export async function handleUnifiedChatPost(req: NextRequest) {
686690
}
687691
const authenticatedUserId = session.user.id
688692
const authenticatedUserEmail = session.user.email
693+
const authenticatedUserName =
694+
typeof session.user.name === 'string' ? session.user.name : undefined
689695

690696
const body = ChatMessageSchema.parse(await req.json())
697+
const userMetadata = {
698+
...(authenticatedUserName ? { name: authenticatedUserName } : {}),
699+
...(body.userTimezone ? { timezone: body.userTimezone } : {}),
700+
}
691701
const normalizedContexts = normalizeContexts(body.contexts) ?? []
692702
userMessageId = body.userMessageId || generateId()
693703

@@ -853,15 +863,14 @@ export async function handleUnifiedChatPost(req: NextRequest) {
853863
// opens". Previously these ran bare under the root and inflated the
854864
// apparent "gap" before the model call. Each promise is its own
855865
// span; they run concurrently under Promise.all below.
856-
const workspaceContextPromise =
857-
workspaceId
858-
? withCopilotSpan(
859-
TraceSpan.CopilotChatBuildWorkspaceContext,
860-
{ [TraceAttr.WorkspaceId]: workspaceId },
861-
() => generateWorkspaceContext(workspaceId, authenticatedUserId),
862-
activeOtelRoot.context
863-
)
864-
: Promise.resolve(undefined)
866+
const workspaceContextPromise = workspaceId
867+
? withCopilotSpan(
868+
TraceSpan.CopilotChatBuildWorkspaceContext,
869+
{ [TraceAttr.WorkspaceId]: workspaceId },
870+
() => generateWorkspaceContext(workspaceId, authenticatedUserId),
871+
activeOtelRoot.context
872+
)
873+
: Promise.resolve(undefined)
865874
const agentContextsPromise = withCopilotSpan(
866875
TraceSpan.CopilotChatResolveAgentContexts,
867876
{
@@ -944,6 +953,7 @@ export async function handleUnifiedChatPost(req: NextRequest) {
944953
fileAttachments: body.fileAttachments,
945954
userPermission: userPermission ?? undefined,
946955
userTimezone: body.userTimezone,
956+
userMetadata,
947957
workflowId: branch.workflowId,
948958
workflowName: branch.workflowName,
949959
workspaceId: branch.workspaceId,
@@ -963,6 +973,7 @@ export async function handleUnifiedChatPost(req: NextRequest) {
963973
fileAttachments: body.fileAttachments,
964974
userPermission: userPermission ?? undefined,
965975
userTimezone: body.userTimezone,
976+
userMetadata,
966977
workspaceContext,
967978
}),
968979
activeOtelRoot.context

0 commit comments

Comments
 (0)