In exchange for a gesture from El Salvador, Israel plans to reopen its embassy here and begin a cooperation program that could lead to Israeli military and internal security aid in the war against leftist guerrillas.

The arrangement also includes hopes in the Salvadoran government that the influential pro-Israel lobby in the United States will lend a discreet hand in congressional debates over the wisdom of administration policy on Central America and the level of military aid for the U.S.-supported government of provisional President Alvaro Magana.

According to sources here and in Jerusalem, the agreement fits into a sense of common security interests that has grown up between Israel and several Latin American countries allied with the United States. This shared view, in the words of a high Salvadoran official, revolves mostly around the Arab adage that "the enemy of my enemy is my friend."

This is a reference to ties between the revolutionary Sandinista government in Nicaragua, which Salvadoran officials depict as the main source of their civil war, and the Palestine Liberation Organization, which Israel seeks to counter wherever possible. The Salvadoran rebel movement also has forged its own links with the PLO, including a visit by several guerrilla leaders to PLO headquarters in Beirut before last year's Israeli invasion there.

El Salvador, in a decision that has delighted the Israeli leadership, plans to move its embassy from Tel Aviv back to Jerusalem around Sept. 15, the Salvadoran national day, according to top Salvadoran officials. In return, Israel will within a few months reopen its embassy here, closed for security reasons since 1979, they added.

A Salvadoran official familiar with the agreement said the Salvadoran delegation and Israeli officials did not discuss military or internal security aid. Their talks were confined to the embassy exchange and the possibility of agricultural aid and lobbying help in Washington, he said.

At the same time, the official acknowledged that once the Israeli Embassy resumes operations here, it is likely that a number of areas of cooperation will be discussed. And since little agricultural progress is possible during the civil war, it is reasonable to suppose military or security concerns could be part of the discussions, he conceded.

"What we have done is open the door to the ball," he said. "Now all those who want to dance can come in."

A Foreign Ministry official said the renewed ties, as they grow, are likely to include military or internal security areas since these are top Salvadoran concerns and Israel has long experience in dealing with insurgents.

"It is logical to think that this could lead to something concrete," he said.

It was unclear how Israeli agricultural or military advisers here would fit in with the extensive U.S. involvement, including about 50 U.S. military advisers and $81 million in military aid this year. One possibility appeared to be internal security, which U.S. advisers are under law barred from participating in.

Begin's government has for some time sought to play a security role in Central America in cooperation with the United States. Israeli officials have been quoted as saying they would be willing to act as a U.S. proxy in areas where congressional restraints or human rights concerns raise obstacles to direct U.S. aid.

During the time when U.S. military aid to Guatemala was cut off, for example, Israel sold a variety of military equipment to the Guatemalan Army and Israeli advisers--some official, others private--helped Guatemalan internal security agents hunt underground rebel groups.

More recently, Israel reportedly provided AK47 assault rifles captured from the PLO to Honduras for transfer to U.S.-sponsored anti-Sandinista guerrillas attacking targets in Nicaragua from headquarters in Honduras. The sale was carried out in coordination with the U.S. Central Intelligence Agency, published reports said. The CIA is the chief financier and supporter of the anti-Sandinista forces, whose main group is the Honduras-based Nicaraguan Democratic Force.

The Salvadoran decision to move its embassy to Jerusalem was conveyed to Prime Minister Menachem Begin by a delegation that visited Israel from Aug. 2 through Aug. 7. It comprised Francisco Jose Guerrero, Magana's top aide; Magana's son Ernesto, and Economy Minister Jose Manuel Pacas.

According to an official present at the meeting, Begin embraced the young Magana on hearing the Salvadoran pledge. The Israeli leadership long has sought to attract foreign embassies to Jerusalem rather than Tel Aviv. Until the Salvadoran decision, only Costa Rica had set up its diplomatic representation in the much-contested city.

The United States, contending that the status of Jerusalem must still be negotiated, has kept its embassy in Tel Aviv. The Netherlands and a dozen Latin American countries, including El Salvador, previously had been in Jerusalem. But they left in 1980 to dissociate themselves with Israel's "Jerusalem law" declaring the city the "undivided, eternal capital" of the Jewish state.

A Salvadoran Foreign Ministry official said Israel's pledge to reciprocate by reopening full diplomatic representation here is considered a gain in the government's struggle to depict San Salvador as a safe place for foreign embassies once again. Former president Napoleon Duarte urged West German officials during a recent European trip to intercede with Common Market partners to reactivate their embassies here, closed during the 1979-1981 urban terrorism. But there is no sign yet they are ready to return.