Skip to content

Commit 28b279a

Browse files
committed
Add convo id to mship block
1 parent c21bb91 commit 28b279a

5 files changed

Lines changed: 71 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
171171
return NextResponse.json({
172172
content: result.content,
173173
model: 'mothership',
174+
conversationId: effectiveChatId,
174175
tokens: result.usage
175176
? {
176177
prompt: result.usage.prompt,

apps/sim/blocks/blocks/mothership.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface MothershipResponse extends ToolResponse {
66
output: {
77
content: string
88
model: string
9+
conversationId?: string
910
tokens?: {
1011
prompt?: number
1112
completion?: number
@@ -34,6 +35,12 @@ export const MothershipBlock: BlockConfig<MothershipResponse> = {
3435
type: 'long-input',
3536
placeholder: 'Enter your prompt for the Mothership...',
3637
},
38+
{
39+
id: 'conversationId',
40+
title: 'Conversation ID',
41+
type: 'short-input',
42+
placeholder: 'e.g., user-123, session-abc, customer-456',
43+
},
3744
],
3845
tools: {
3946
access: [],
@@ -43,10 +50,15 @@ export const MothershipBlock: BlockConfig<MothershipResponse> = {
4350
type: 'string',
4451
description: 'The prompt to send to the Mothership AI agent',
4552
},
53+
conversationId: {
54+
type: 'string',
55+
description: 'Mothership chat ID to continue; generated when omitted',
56+
},
4657
},
4758
outputs: {
4859
content: { type: 'string', description: 'Generated response content' },
4960
model: { type: 'string', description: 'Model used for generation' },
61+
conversationId: { type: 'string', description: 'Mothership chat ID used for this request' },
5062
tokens: { type: 'json', description: 'Token usage statistics' },
5163
toolCalls: { type: 'json', description: 'Tool calls made during execution' },
5264
cost: { type: 'json', description: 'Cost of the execution' },

apps/sim/executor/handlers/mothership/mothership-handler.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('MothershipBlockHandler', () => {
8484
metadata: { id: BlockType.MOTHERSHIP, name: 'Mothership' },
8585
position: { x: 0, y: 0 },
8686
config: { tool: BlockType.MOTHERSHIP, params: {} },
87-
inputs: { prompt: 'string' },
87+
inputs: { prompt: 'string', conversationId: 'string' },
8888
outputs: {},
8989
enabled: true,
9090
} as SerializedBlock
@@ -122,6 +122,7 @@ describe('MothershipBlockHandler', () => {
122122
JSON.stringify({
123123
content: 'done',
124124
model: 'mothership',
125+
conversationId: 'chat-uuid',
125126
tokens: { total: 5 },
126127
toolCalls: [],
127128
}),
@@ -137,6 +138,7 @@ describe('MothershipBlockHandler', () => {
137138
expect(result).toEqual({
138139
content: 'done',
139140
model: 'mothership',
141+
conversationId: 'chat-uuid',
140142
tokens: { total: 5 },
141143
toolCalls: { list: [], count: 0 },
142144
cost: undefined,
@@ -161,6 +163,55 @@ describe('MothershipBlockHandler', () => {
161163
})
162164
})
163165

166+
it('uses a provided conversation ID as the mothership chat ID', async () => {
167+
mockGenerateId.mockReturnValueOnce('message-uuid')
168+
mockGenerateId.mockReturnValueOnce('request-uuid')
169+
170+
fetchMock.mockResolvedValue(
171+
new Response(
172+
JSON.stringify({
173+
content: 'continued',
174+
model: 'mothership',
175+
conversationId: 'existing-chat-id',
176+
tokens: {},
177+
toolCalls: [],
178+
}),
179+
{
180+
status: 200,
181+
headers: { 'Content-Type': 'application/json' },
182+
}
183+
)
184+
)
185+
186+
const result = await handler.execute(context, block, {
187+
prompt: 'Continue this thread',
188+
conversationId: ' existing-chat-id ',
189+
})
190+
191+
expect(result).toEqual({
192+
content: 'continued',
193+
model: 'mothership',
194+
conversationId: 'existing-chat-id',
195+
tokens: {},
196+
toolCalls: { list: [], count: 0 },
197+
cost: undefined,
198+
})
199+
200+
const [, options] = fetchMock.mock.calls[0] as [string, RequestInit]
201+
const body = JSON.parse(String(options.body))
202+
expect(body).toEqual({
203+
messages: [{ role: 'user', content: 'Continue this thread' }],
204+
workspaceId: 'workspace-1',
205+
userId: 'user-1',
206+
chatId: 'existing-chat-id',
207+
messageId: 'message-uuid',
208+
requestId: 'request-uuid',
209+
workflowId: 'workflow-1',
210+
executionId: 'execution-1',
211+
})
212+
expect(mockGenerateId).toHaveBeenCalledTimes(2)
213+
})
214+
164215
it('propagates local aborts to the mothership request', async () => {
165216
const abortController = new AbortController()
166217
context.abortSignal = abortController.signal

apps/sim/executor/handlers/mothership/mothership-handler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export class MothershipBlockHandler implements BlockHandler {
3333
throw new Error('Prompt input is required')
3434
}
3535
const messages = [{ role: 'user' as const, content: prompt }]
36-
const chatId = generateId()
36+
const providedConversationId =
37+
typeof inputs.conversationId === 'string' ? inputs.conversationId.trim() : ''
38+
const chatId = providedConversationId || generateId()
3739
const messageId = generateId()
3840
const requestId = generateId()
3941

@@ -57,6 +59,7 @@ export class MothershipBlockHandler implements BlockHandler {
5759
requestId,
5860
workflowId: ctx.workflowId,
5961
executionId: ctx.executionId,
62+
chatId,
6063
})
6164

6265
const abortController = new AbortController()
@@ -135,6 +138,7 @@ export class MothershipBlockHandler implements BlockHandler {
135138
return {
136139
content: result.content || '',
137140
model: result.model || 'mothership',
141+
conversationId: result.conversationId || chatId,
138142
tokens: result.tokens || {},
139143
toolCalls,
140144
cost: result.cost || undefined,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ export const mothershipExecuteResponseSchema = z
256256
.object({
257257
content: z.string().optional(),
258258
model: z.literal('mothership'),
259+
conversationId: z.string(),
259260
tokens: z
260261
.object({
261262
prompt: z.number().optional(),

0 commit comments

Comments
 (0)