NO ONE EVER thought it would be easy to draft a good foreign intelligence wiretapping and eavesdropping bill. The conflict seemed almost beyond resolution. On the side stood the need of the government to obtain - secretly - the information about the activities inside this country of foreign governments and their agents. On the other was the necessity to protect against unnecessary intrusions on the privacy of Americans. But after more than three years of study and negotiations, committees in both houses of Congress are now close to producing legislation that strikes a fair and workable balance between those interests.

It became clear some time ago that Congress would have to act in this field. Wiretaps, mail covers and burglaries had been undertaken against American officials, private citizens and organizations in the name of national security. Some of those may have had - by some stretch of the imagination - something to do with legitimate foreign intelligence or counterintelligence. But most of them were designed to obtain, for someone in the government, information of a purely political or personal nature. Almost all of those activities violated the spirit - and, in our view, the letter as well - of the Fourth Amendment's bar against unreasonable searches.

But the executive branch insisted there was a constitutional difference between searches for intelligence data and those for evidence of crime. And the White House, under Richard Nixon and Gerald Ford as well as earlier occupants, argued that every president has an inherent right to order whatever kind of surveillance he thought necessary to protect the national security. Those were the justifications for wiretaps and electronic bugs aimed at officials who might have leaked news stories, reporters who might have received those leaks, civil right leaders who were planning demonstrations and politicians whose political activities were of interest.

Under the legislation that has been approved by the Senate Intelligence Committee and by a subcommittee of its counterpart in the House, those activities would be illegal unless authorized by a federal judge or - in the case of an extreme emergency - approved by a judge within 24 hours after surveillance started. Although the Carter administraiton is still arguing in a current espionage case that presidents have an inherent power to act on their own, it has withdrawn the Nixon-Ford insistence that Congress specifically recognize that power.

That withdrawal was the first step toward a good foreign intelligence bill. The second was the acceptance by the major intelligence agencies of the principle that in most cases judges should use a regular Fourth Amendment standard in deciding whether to grant requests for wiretapping and eavesdropping warrants. That means that if the government wants to tap the phone of an American citizen in a national security case, it will have to convince the judge a crime has been or is about to be committed - the same showing it would have to make in any other criminal case. There are many exceptions in both bills. Surveillance of employees of foreign governments is one example.But almost all of them seem to be aimed at situations in which only legitimate foreign intelligence information is being sought. In those cases, the judges can issue the warrants on far less information than would be requried under regular Fourth Amendment standards.

There are still big difference between the bill that is ready for the Senate floor and the one that was passed by the House subcommittee. On most points of contention, the House version seems preferable. Among other things, it brings more cases under the tighter, criminal standard, and it requires greater efforts by the government to minimize the interception of innocent conversations. But both bills are a large stride toward getting the kind of legislation on the books that is essential to ensure that the intelligence agencies do not get out of hand again. They bode well for the other legislation that is still needed - new charters for the intelligence agencies and protection against unnecessary surveillance for American citizens abroad.