Skip to content

Commit c678109

Browse files
committed
Fix stream
1 parent 2ac0d04 commit c678109

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
200200

201201
const stream = new ReadableStream<Uint8Array>({
202202
start(controller) {
203+
let forwardedAssistantContent = ''
203204
const send = (event: unknown) => {
204205
if (!cancelled) {
205206
controller.enqueue(encodeNdjson(event))
@@ -221,7 +222,14 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
221222
event.payload.channel === MothershipStreamV1TextChannel.assistant &&
222223
event.payload.text
223224
) {
224-
send({ type: 'chunk', content: event.payload.text })
225+
const text = event.payload.text
226+
const content = text.startsWith(forwardedAssistantContent)
227+
? text.slice(forwardedAssistantContent.length)
228+
: text
229+
if (content) {
230+
forwardedAssistantContent += content
231+
send({ type: 'chunk', content })
232+
}
225233
}
226234
})
227235
allowExplicitAbort = false

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ async function readMothershipExecuteResponse(response: Response): Promise<Mother
164164
function createMothershipStreamingExecution(
165165
response: Response,
166166
fallbackChatId: string,
167+
blockId: string,
167168
options: {
168169
onCancel?: (reason?: unknown) => void
169170
onDone?: () => void
@@ -265,6 +266,7 @@ function createMothershipStreamingExecution(
265266
execution: {
266267
success: true,
267268
output,
269+
blockId,
268270
logs: [],
269271
metadata: {
270272
duration: 0,
@@ -438,7 +440,7 @@ export class MothershipBlockHandler implements BlockHandler {
438440

439441
if (isContentSelectedForStreaming(ctx, block)) {
440442
cleanupImmediately = false
441-
return createMothershipStreamingExecution(response, chatId, {
443+
return createMothershipStreamingExecution(response, chatId, block.id, {
442444
onCancel: (reason) => {
443445
if (!abortController.signal.aborted) {
444446
abortController.abort(reason ?? 'mothership_stream_cancelled')

0 commit comments

Comments
 (0)