Media playback is unsupported on your device Media caption "We are not nation building again. We are killing terrorists," President Trump said

The Afghan president has hailed Donald Trump's decision to commit US troops to the country to fight against the resurgent Taliban.

Ashraf Ghani thanked the US for supporting "the joint struggle against the threat of terrorism".

Donald Trump has committed the US Army to the open-ended conflict, despite previously advocating its withdrawal.

The Taliban responded by saying it would make Afghanistan a "graveyard" for US forces.

Mr Trump offered few details on the plan, but singled out Pakistan for criticism for offering "safe havens" to extremists.

Pakistani officials reject claims the country uses some militants as a tool of state policy.

What is the new strategy?

President Trump warned a hasty US withdrawal from Afghanistan would leave a vacuum for terrorists to fill and said he had decided to keep troops there to "fight to win" to avoid the mistakes made in Iraq.

He said his new approach would be more pragmatic and based on conditions on the ground rather than idealistic and time-based, and would switch from nation building to "killing terrorists".

Image copyright Reuters Image caption Mr Trump's daughter Ivanka, wife Melania and Vice-President Mike Pence watch the speech

But he refused to get drawn on how many extra troops, if any, would be deployed and gave no timeline for ending the US presence in the country.

Washington is expected to send up to 4,000 additional troops, but Mr Trump did not comment on this.

In June, he told the Department of Defense to decide on troop levels.

Visiting Baghdad on Tuesday, Defence Secretary Jim Mattis said he would wait for a plan from the chairman of the Joint Chiefs of Staff based on the president's South Asia strategy before deciding.

"It may or may not be the number that is bandied about," he told reporters.

Mr Trump also, for the first time, left the door open for an eventual peace deal with the Taliban, saying: "Someday, after an effective military effort, perhaps it will be possible to have a political settlement that includes elements of the Taliban in Afghanistan."

However, Mr Trump said there would be an escalation in the battle against groups like al-Qaeda and so-called Islamic State.

Meanwhile, Mr Trump made it clear he expects his existing allies - including India - to support him in his new strategy, and urged them to raise their countries' contributions "in line with our own".

Read more on Trump's presidency:

The key is in Islamabad, not Kabul

By Secunder Kermani, BBC correspondent in Kabul

Even with a few thousand extra US soldiers in Afghanistan - deployment levels would remain far lower than their peak in 2010/11 when there were around 100,000 US personnel in the country. So what is different this time?

Firstly, that there is no deadline by which the US will begin to scale operations back. Critics of President Obama's surge say that because he made it clear it was temporary - the Taliban were encouraged to wait the Americans out.

Image copyright Ronald Grant Image caption President Trump's announcement was watched closely by Afghans in Kabul

The second difference is that the US will put more pressure on Pakistan to end "safe havens" for the Taliban, according to President Trump.

One analyst told me that the key to solving the conflict lies in Islamabad not in Afghanistan. But it is not clear how much leverage the US still has over Pakistan - or how Pakistan will respond to the accusations, given its consistent denial that it operates a "good terrorist, bad terrorist" policy.

Pakistan has grown increasingly close to China, and has already had millions of dollars of US aid withheld for allegedly not taking enough action against the Taliban-allied Haqqani network.

Is Trump flip-flopping?

Before his presidency, Mr Trump was not shy about criticising his predecessors on their Afghanistan policy. He previously supported pulling US troops out of the conflict, which began under President George W Bush in 2001 after the 9/11 attacks.

Early on in his presidential campaign, however, he did acknowledge that US troops would have to stay in order to avoid the total collapse of the Afghan government.

And this long-awaited announcement came after a months-long review, with the president himself acknowledging that his original instinct to pull-out had been reversed after discussions with national security advisers.

BBC correspondent Aleem Maqbool in Washington says the people who might object to Mr Trump's strategy are the very ones who voted for him on his "America First" platform.

What is the reaction?

Welcoming the plan, Afghan President Ashraf Ghani said Mr Trump's comments showed America was "with us, without any time limit".

"I am grateful to President Trump and the American people for this affirmation of support... for our joint struggle to rid the region from the threat of terrorism," he said in a statement on Tuesday.

The new strategy, he said, would enhance the training of Afghan security forces.

Nato chief Jens Stoltenberg praised the move and said the alliance, which has about 12,000 troops in Afghanistan, would not allow the country to become "a safe haven for terrorists who would attack our own countries".

Media playback is unsupported on your device Media caption The BBC is given rare access to see life under the Taliban

India's foreign ministry said it shared Mr Trump's concerns over safe havens and "other forms of cross-border support enjoyed by terrorists".

But Taliban spokesman Zabiullah Mujahid dismissed Mr Trump's strategy as "nothing new", telling the US to think of an exit strategy "instead of continuing the war".

Pakistan's foreign minister met the US ambassador in Islamabad on Wednesday and highlighted "Pakistan's immense sacrifices in the enduring fight against terrorism", a statement said, as well as a desire to continue working with the international community "to eliminate the menace of terrorism".

US combat operations against the Taliban officially ended in 2014, but more than 8,000 special forces continue to provide support to Afghan troops.

The Afghan government continues to battle insurgency groups and controls just half of the country.

Retired UK General Sir Simon Mayall, who was responsible for Afghan operational policy from 2009 to 2012, told the BBC's World at One that even small numbers of troops would make a "big difference to the confidence and competence of the Afghan security forces".

"The high levels of ISAF troops were very helpful in giving the Afghans time to develop their own security forces, but then announcing we were going to leave in 2014 and running down very quickly... was very bad for competence and confidence.

"I hope that what President Trump has announced will serve to redress a number of those weaknesses."