Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.6 KB
Raw Blame
import { canonicalizeCodeWorkspaceSelections } from 'librechat-data-provider';
import type {
  TConversation,
  CodeEnvironmentMode,
  CodeWorkspaceSelection,
} from 'librechat-data-provider';

function sameSelections(
  left?: CodeWorkspaceSelection[],
  right?: CodeWorkspaceSelection[],
): boolean {
  if (!left?.length || !right?.length) return !left?.length && !right?.length;
  if (left.length !== right.length) return false;
  return (
    JSON.stringify(canonicalizeCodeWorkspaceSelections(left)) ===
    JSON.stringify(canonicalizeCodeWorkspaceSelections(right))
  );
}

/**
 * Folds the decision a submitted run establishes into the conversation that run belongs to. The
 * server seals this pair for the rest of the chat, so the conversation has to carry it from the
 * moment it is sent: a chat that becomes saved mid-run would otherwise hold no decision while the
 * stored one is already sealed, and the composer would re-derive an agent default or a remembered
 * preference that the sealed decision refuses, reporting "choose a workspace", with Send
 * disabled, for a workspace the run is already using. Returns the same conversation when it
 * already holds the decision, so an unchanged send never re-renders the composer.
 */
export function withSubmittedCodeDecision(
  conversation: TConversation | null,
  submitted: {
    codeEnvironmentMode?: CodeEnvironmentMode;
    codeWorkspaces?: CodeWorkspaceSelection[];
  },
): TConversation | null {
  const { codeWorkspaces } = submitted;
  // Older replicas accept selections without a mode. Preserve that submitted selection locally
  // too, or the first saved-chat event drops an implicit default back to "Choose workspace".
  const codeEnvironmentMode =
    submitted.codeEnvironmentMode ?? ((codeWorkspaces?.length ?? 0) > 0 ? 'attached' : undefined);
  if (conversation == null || codeEnvironmentMode == null) return conversation;
  if (
    conversation.codeEnvironmentMode === codeEnvironmentMode &&
    sameSelections(conversation.codeWorkspaces, codeWorkspaces)
  ) {
    return conversation;
  }
  return { ...conversation, codeEnvironmentMode, codeWorkspaces };
}

/** Compare the pair, including absence: a failed first send may leave no persisted decision. */
export function hasSameCodeDecision(
  left: Pick<TConversation, 'codeEnvironmentMode' | 'codeWorkspaces'>,
  right: Pick<TConversation, 'codeEnvironmentMode' | 'codeWorkspaces'>,
): boolean {
  return (
    (left.codeEnvironmentMode ?? (left.codeWorkspaces?.length ? 'attached' : undefined)) ===
      (right.codeEnvironmentMode ?? (right.codeWorkspaces?.length ? 'attached' : undefined)) &&
    sameSelections(left.codeWorkspaces, right.codeWorkspaces)
  );
}