Skip to content

AssistantResponse from nestjs not sending stream to the client #6156

Closed
@0xKaizendev

Description

@0xKaizendev

Description

I have a nestjs backend that implement a route for using AssistantResponse, the message in useAssistant on the client is not being updaped on realtime
` @post('/run')
async run(
@Body() body: RunAssistantDto,
@Req() req: Request,
@res() res: Response,
) {
console.log('Running assistant');

const userId = req.dbUser.id;
const { message, assistantId, id } = body;
console.log('body ', body);
const assistant =
  assistantId === 'default'
    ? await this.assistantService.findDefault()
    : await this.assistantService.findOne(assistantId);

if (!assistant) {
  throw new HttpException('Assistant not found', 404);
}


const threadId =
  id ?? (await this.openaiService.openai.beta.threads.create({})).id;

console.log('threadId ', threadId);

const thread = await this.threadService.findOne(threadId, userId);

console.log('thread ', thread);
if (!thread) {
  const title = await generateTitleFromUserMessage({
    message: {
      content: message,
      role: 'user',
    },
  });

  await this.threadService.create({
    id: threadId,
    title,

    user: { connect: { id: userId } },
  });
}
const messages =
  await this.openaiService.openai.beta.threads.messages.list(threadId);
console.log('messages ', messages.data);

// Add a message to the thread
const createdMessage =
  await this.openaiService.openai.beta.threads.messages.create(threadId, {
    role: 'user',
    content: message,
  });

return AssistantResponse(
  { threadId, messageId: createdMessage.id },
  async ({ forwardStream, sendDataMessage, sendMessage }) => {

    let runStream = this.openaiService.openai.beta.threads.runs.stream(
      threadId,
      {
        assistant_id: assistant.assistantId,
      
      },
    );

    // forward run status would stream message deltas
    let runResult = await forwardStream(runStream);
    console.log('runResult ', runResult);


  },
);

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions