function recordChatCompletionMessages({
  agent,
  shim,
  segment,
  request,
  response,
  err,
  transaction
}) {
  if (shouldSkipInstrumentation(agent.config, shim) === true) {
    shim.logger.debug('skipping sending of ai data')
    return
  }

  if (!response) {
    // If we get an error, it is possible that `response = null`.
    // In that case, we define it to be an empty object.
    response = {}
  }

  response.headers = segment[openAiHeaders]
  // explicitly end segment to consistent duration
  // for both LLM events and the segment
  segment.end()
  const completionSummary = new LlmChatCompletionSummary({
    agent,
    segment,
    transaction,
    request,
    response,
    withError: err != null
  })

  // Note: langchain still expects a message event even
  // when the response is empty, so we will not filter
  // the response message.
  const messages = [
    ...getMessagesFromRequest(request, shim),
    getMessageFromResponse(response)
  ]

  messages.forEach((message, index) => {
    const completionMsg = new LlmChatCompletionMessage({
      agent,
      segment,
      transaction,
      request,
      response,
      index,
      completionId: completionSummary.id,
      message
    })

    recordEvent({ agent, type: 'LlmChatCompletionMessage', msg: completionMsg })
  })

  recordEvent({ agent, type: 'LlmChatCompletionSummary', msg: completionSummary })

  if (err) {
    const llmError = new LlmErrorMessage({ cause: err, summary: completionSummary, response })
    agent.errors.add(transaction, err, llmError)
  }

  delete response.headers
}
Generates LlmChatCompletionSummary for a chat completion creation.
Also iterates over both input messages and the first response message
and creates LlmChatCompletionMessage.

Also assigns relevant ids by response id for LlmFeedbackEvent creation

@param {object} params input params
@param {Agent} params.agent NR agent instance
@param {Shim} params.shim the current shim instance
@param {TraceSegment} params.segment active segment from chat completion
@param {object} params.request chat completion params
@param {object} params.response chat completion response
@param {boolean} [params.err] err if it exists
@param {Transaction} params.transaction active transaction