void icon indicating copy to clipboard operation
void copied to clipboard

Code Refactor for `sendLLMMessage.ts`

Open SOUMITRO-SAHA opened this issue 1 year ago • 0 comments

I have updated the code by incorporating an enum for better management of the AI providers:

export const sendLLMMessage: SendLLMMessageFnTypeExternal = ({
  messages,
  onText,
  onFinalMessage,
  apiConfig,
}) => {
  if (!apiConfig) return { abort: () => {} };

  switch (apiConfig.whichApi) {
    case IAiProvider.ANTHROPIC:
      return sendClaudeMsg({ messages, onText, onFinalMessage, apiConfig });
    case IAiProvider.OPENAI:
      return sendOpenAIMsg({ messages, onText, onFinalMessage, apiConfig });
    case IAiProvider.GREPTILE:
      return sendGreptileMsg({ messages, onText, onFinalMessage, apiConfig });
    case IAiProvider.OLLAMA:
      return sendOllamaMsg({ messages, onText, onFinalMessage, apiConfig });
    default:
      console.error(
        `Error: whichApi was ${apiConfig.whichApi}, which is not recognized!`
      );
      return { abort: () => {} };
  }
};
export enum IAiProvider {
  ANTHROPIC = "anthropic",
  OPENAI = "openai",
  GREPTILE = "greptile",
  OLLAMA = "ollama",
}

The key reason for using an enum is to prevent potential typos, especially since we will be using these values in multiple places (such as in a select option in the UI, which I am planning to implement). Using an enum ensures consistency across the codebase.

I have also removed the previous pull request, where I performed multiple refactoring steps within this file. As per the refactoring guidelines mentioned in the documentation, I am now submitting smaller updates one at a time.

SOUMITRO-SAHA avatar Oct 02 '24 10:10 SOUMITRO-SAHA