In the United States, both primary elections and general elections are government-organized events. These elections are primarily established and regulated by statutes passed by state legislatures. Therefore, when a party has a primary election in a state, it is subject to all Constitutionally valid state election law.
Parties have First Amendment association rights that states must not infringe, e.g. it has been ruled unconstitutional for states to have a 'blanket primary' against the will of the parties involved (California Democratic Party v. Jones, 2000). However, there are various other constitutional issues that can come into play, so First Amendment association rights are not a trump card in this context.
Depending on state laws, political parties may have the option of choosing not to participate in a state primary election. ("Caucuses" are a well-known alternative used by the parties of some states. But sometimes, a state party may even just decide to have neither a primary or caucus; for discussion of this in the context of the 2020 election cycle, see "Republicans to scrap primaries and caucuses as Trump challengers cry foul", Politico, Alex Isenstadt, 09/06/2019. However, it seems some states, such as Michigan, may have election laws that do not allow parties to opt out of participating in a state primary: see CDJB♦'s answer to What steps could the US GOP take to block primary challenges to Trump in 2024 should he run again, and which of these were also taken in 2020?)
In fact, it seems the Colorado Republican party has expressed intent to cancel its primary for the 2024 election if Trump is not on the ballot, and have a caucus instead, but it is apparently disputed whether the party is legally allowed to withdraw from the primary process at this point: the Colorado Department of State made a statement that withdrawal is not allowed by Colorado law. ("Colorado GOP threatens to shift to caucus system over Trump ruling", The Hill, SARAH FORTINSKY - 12/20/23 12:20)
I wrote a longer post with more details as an answer to a broader past question here: How do parties really choose candidates in the US?
I found another question that has some information that may be of interest on the legal status of political parties in the United States: What type of organization is an American political party?
See also In the US, why does the government have the right to regulate how political parties hold their primaries? which might really be a prior answer to your question (I didn't see it before writing this one).