code: def agent_heartbeat(self, agent_id, metrics, run_states):
          
          mutation = gql('''
          mutation Heartbeat(
              $id: ID!,
              $metrics: JSONString,
              $runState: JSONString
          ) {
              agentHeartbeat(input: {
                  id: $id,
                  metrics: $metrics,
                  runState: $runState
              }) {
                  agent {
                      id
                  }
                  commands
              }
          }
          ''')
          try:
              response = self.gql(mutation, variable_values={
                  'id': agent_id,
                  'metrics': json.dumps(metrics),
                  'runState': json.dumps(run_states)})
          except Exception as e:
              # GQL raises exceptions with stringified python dictionaries :/
              message = ast.literal_eval(e.args[0])["message"]
              logger.error('Error communicating with W&B: %s', message)
              return []
          else:
              return json.loads(response['agentHeartbeat']['commands'])
docs: Notify server about agent state, receive commands.
  
          Args:
              agent_id (str): agent_id
              metrics (dict): system metrics
              run_states (dict): run_id: state mapping
          Returns:
              List of commands to execute.