import ProjectManager from "@/LLMProviders/projectManager";
import {
  ChatHistoryEntry,
  extractTextFromChunk,
  removeErrorTags,
  removeThinkTags,
  withSuppressedTokenWarnings,
} from "@/utils";

export async function getStandaloneQuestion(
  question: string,
  chatHistory: ChatHistoryEntry[]
): Promise<string> {
  const condenseQuestionTemplate = `Given the following conversation and a follow up question,
    summarize the conversation as context and keep the follow up question unchanged, in its original language.
    If the follow up question is unrelated to its preceding messages, return this follow up question directly.
    If it is related, then combine the summary and the follow up question to construct a standalone question.
    Make sure to keep any [[]] wrapped note titles in the question unchanged.
    If there's nothing in the chat history, just return the follow up question.

    Chat History:
    {chat_history}
    Follow Up Input: {question}
    Standalone question:`;

  const formattedChatHistory = chatHistory
    .map(({ role, content }) => `${role}: ${content}`)
    .join("\n");

  // Wrap the model call with token warning suppression
  return await withSuppressedTokenWarnings(async () => {
    // Use temperature=0 for deterministic question condensation
    const chatModel = await ProjectManager.instance
      .getCurrentChainManager()
      .chatModelManager.getChatModelWithTemperature(0);

    // Use stream() instead of invoke() to avoid LangChain's _generate() path,
    // which triggers tiktoken CDN fetch via _getEstimatedTokenCountFromPrompt.
    let text = "";
    const stream = await chatModel.stream([
      {
        role: "user",
        content: condenseQuestionTemplate
          .replace("{chat_history}", formattedChatHistory)
          .replace("{question}", question),
      },
    ]);
    for await (const chunk of stream) {
      text += extractTextFromChunk(chunk.content);
    }

    const cleanedResponse = removeThinkTags(text);
    return removeErrorTags(cleanedResponse);
  });
}
