DEV Community

Cover image for Chatcraft Runtime Response Handling Progress
Amir Helali
Amir Helali

Posted on

Chatcraft Runtime Response Handling Progress

Progress So Far

So, in my previous post I explained my plan to contribute to an issue for the Chatcraft project. As I explained, my plan was to use the Strategy Pattern for solving this issue. I created a response handler interface:

interface ResponseHandler {
  handleResponse(data: any): Promise<any>;
}
Enter fullscreen mode Exit fullscreen mode

Then I created different response handlers:

class NonStreamingResponseHandler implements ResponseHandler {
  async handleResponse(response: OpenAI.Chat.ChatCompletion): Promise<any> {
    const { content, functionName, functionArgs } = parseOpenAIResponse(response);
    return handleOpenAIResponse(content, functionName, functionArgs);
  }
}
class StreamingResponseHandler implements ResponseHandler {
  async handleResponse(streamResponse: any): Promise<any> {
    const buffer = [];
    for await (const streamChunk of streamResponse) {
      const parsedData = parseOpenAIChunkResponse(streamChunk);
      await streamOpenAIResponse(
        parsedData.token,
        parsedData.functionName,
        parsedData.functionArgs
      );
      buffer.push(parsedData.chunkContent);
    }

    const content = buffer.join("");
    return handleOpenAIResponse(content);
  }
}
Enter fullscreen mode Exit fullscreen mode

After that I had a function to create the correct response handler based on the streaming flag:

function createResponseHandler(streaming: boolean): ResponseHandler {
  return streaming ? new StreamingResponseHandler() : new NonStreamingResponseHandler();
}
Enter fullscreen mode Exit fullscreen mode

The Problem & Readjustment

As it can be seen from the code I provided above, my plan was to use the class component pattern. However, the code base that I am contributing to uses functional components. So, I need to change my implementation plan to use the functional component pattern. To do this I plan to just define two separate functions for handling each type of response and use those functions appropriately based on the streaming flag.

To Be Continued

I plan to make another post about my final implementation for this issue.

Top comments (0)