<commit_before>import { Conversation_conversation } from "v2/__generated__/Conversation_conversation.graphql"
import { SendConversationMessageMutation } from "v2/__generated__/SendConversationMessageMutation.graphql"
import {
  ConnectionHandler,
  Environment,
  MutationConfig,
  RecordSourceSelectorProxy,
  commitMutation,
  graphql,
} from "relay-runtime"

export const SendConversationMessage = (
  environment: Environment,
  conversation: Conversation_conversation,
  text: string,
  onCompleted: MutationConfig<any>["onCompleted"],
  onError: MutationConfig<any>["onError"]
) => {
  const storeUpdater = (store: RecordSourceSelectorProxy) => {
    const mutationPayload = store.getRootField("sendConversationMessage")
    const newMessageEdge = mutationPayload.getLinkedRecord("messageEdge")
    const conversationStore = store.get(conversation.id)
    const connection = ConnectionHandler.getConnection(
      conversationStore,
      "Messages_messages"
    )
    ConnectionHandler.insertEdgeBefore(connection, newMessageEdge)
  }
  return commitMutation<SendConversationMessageMutation>(environment, {
    onError,
    onCompleted,
    optimisticUpdater: storeUpdater,
    updater: storeUpdater,
    variables: {
      input: {
        id: conversation.internalID,
        from: conversation.from.email,
        bodyText: text,
        // Reply to the last message
        replyToMessageID: conversation.lastMessageID,
      },
    },
    mutation: graphql`
      mutation SendConversationMessageMutation(
        $input: SendConversationMessageMutationInput!
      ) {
        sendConversationMessage(input: $input) {
          messageEdge {
            node {
              impulseID
              isFromUser
              body
              id
              internalID
              ...Message_message
            }
          }
        }
      }
    `,
    configs: [
      {
        type: "RANGE_ADD",
        parentName: "conversation",
        parentID: "id",
        connectionName: "messages",
        edgeName: "messageEdge",
        rangeBehaviors: {
          "": "append",
        },
        connectionInfo: [
          {
            key: "Messages_messages",
            rangeBehavior: "append",
          },
        ],
      },
    ],

    optimisticResponse: {<commit_msg>Correct conversation optimistic response to use messagesConnection<commit_after>import { Conversation_conversation } from "v2/__generated__/Conversation_conversation.graphql"
import { SendConversationMessageMutation } from "v2/__generated__/SendConversationMessageMutation.graphql"
import {
  ConnectionHandler,
  Environment,
  MutationConfig,
  RecordSourceSelectorProxy,
  commitMutation,
  graphql,
} from "relay-runtime"

export const SendConversationMessage = (
  environment: Environment,
  conversation: Conversation_conversation,
  text: string,
  onCompleted: MutationConfig<any>["onCompleted"],
  onError: MutationConfig<any>["onError"]
) => {
  const storeUpdater = (store: RecordSourceSelectorProxy) => {
    const mutationPayload = store.getRootField("sendConversationMessage")
    const newMessageEdge = mutationPayload.getLinkedRecord("messageEdge")
    const conversationStore = store.get(conversation.id)
    const connection = ConnectionHandler.getConnection(
      conversationStore,
      "Messages_messagesConnection"
    )
    ConnectionHandler.insertEdgeBefore(connection, newMessageEdge)
  }
  return commitMutation<SendConversationMessageMutation>(environment, {
    onError,
    onCompleted,
    optimisticUpdater: storeUpdater,
    updater: storeUpdater,
    variables: {
      input: {
        id: conversation.internalID,
        from: conversation.from.email,
        bodyText: text,
        // Reply to the last message
        replyToMessageID: conversation.lastMessageID,
      },
    },
    mutation: graphql`
      mutation SendConversationMessageMutation(
        $input: SendConversationMessageMutationInput!
      ) {
        sendConversationMessage(input: $input) {
          messageEdge {
            node {
              impulseID
              isFromUser
              body
              id
              internalID
              ...Message_message
            }
          }
        }
      }
    `,
    configs: [
      {
        type: "RANGE_ADD",
        parentName: "conversation",
        parentID: "id",
        connectionName: "messagesConnection",
        edgeName: "messageEdge",
        rangeBehaviors: {
          "": "append",
        },
        connectionInfo: [
          {
            key: "Messages_messagesConnection",
            rangeBehavior: "append",
          },
        ],
      },
    ],

    optimisticResponse: {
{'fraction_non_alphanumeric': 0.08630634479812006, 'fraction_numerical': 0.0008545182653279214, 'mean_word_length': 2.5987701767870868, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767258', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1242, 'n_words': 222}