CopilotKit / CopilotKit

React UI + elegant infrastructure for AI Copilots, in-app AI agents, AI chatbots, and AI-powered Textareas 🪁
https://copilotkit.ai
MIT License
12.68k stars 2.06k forks source link

Backend in NestJS #295

Open apperside opened 7 months ago

apperside commented 7 months ago

Describe the bug Hi, first of all thanks a lot for this fantastic piece of software! I have managed to integrate it in my NextJS app, I can see the chat sidebar appearing and the chat content be posted to my NestJS backend, where I am handling the request this way

  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.response(
      request,
      new OpenAIAdapter(),
    );
    return result
  }

I also have tried this

  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.stream(
      request,
      new OpenAIAdapter(),
    );
    return result
  }

and this


  @Post('/chat')
  public async chat(
    @Request() request,
    @Response() response,
  ) {
    const copilotKit = new CopilotBackend();

    const result = await copilotKit.streamHttpServerResponse(
      request,
      response,
      new OpenAIAdapter(),
    );
    return result
  }

By in any case the chat waits for the response indefinitely. By adding some breakpoint, the response is properly generated, but for some reason the clients does not receive it. What am I missing?

Many thanks

ikudev commented 7 months ago

@apperside Hello, I think you should modify your nestjs code like this:

@Post('chat')
  async chat(@Req() request, @Res() response): Promise<void> {
    const copilotKit = new CopilotBackend();
    await copilotKit.streamHttpServerResponse(request, response, new OpenAIAdapter());
  }

Because streamHttpServerResponse returns Promise<void>, you can't get anything using its return value. You should completely delegate everything to the CopilotBackend instance.

BTW, the above code is a simple version for demo. It'll be better to create the CopilotBackend instance in a service instead, in case of creating a new instance every time a request comes.

ikudev commented 7 months ago

Please refer to this demo I made. https://github.com/ikudev/copilotkit-nestjs-demo