Jaswant Singh Chail, 21, climbed into the castle grounds with the weapon and later declared “I’m here to kill the Queen”, on Christmas Day 2021.
The former supermarket worker, from Southampton in Hampshire, had exchanged more than 5,000 sexual messages with his AI chatbot “girlfriend” Sarai before embarking on what he had described as his mission in life.
He also fantasised about being the “Sith” character from the films during the attack, the court heard.
Sarai became a “running commentary” on what he should do, and both encouraged and discouraged him from carrying out the attack, the hearing was told.
In a Snapchat video posted minutes before he entered the grounds, he said he was sorry for what he would do.
In the same video he said he was seeking revenge for the 1919 Amritsar massacre, where British troops opened fire on thousands of Indians and left up to 1,500 dead.
Wearing dark clothes and a home-made metal face mask, he said: “I’m sorry for what I’ve done and what I will do.
“I’m going to attempt to assassinate Elizabeth, queen of the royal family. This is revenge for those who have died in the 1919 Jallianwala Bagh massacre.”
The defendant became the first person in the UK to be convicted of treason in more than 40 years after admitting the charge at a hearing in February.
He also pleaded guilty to making threats to kill and possession of the crossbow.
The Old Bailey was told on Thursday that the attack had been “carefully planned” rather than being the result of any psychotic episode.
Professor Nigel Blackwood told the court it was instead part of an “omnipotent fantasy”, the wrongness of which the defendant was “well aware”.
The psychiatrist said under questioning by Mr Justice Hilliard: “This was part of an omnipotent fantasy. The fact he sought to apologise for his actions, I think that is important.
“He is well aware of the wrongness of his acts and he seeks to apologise for them before demonstrating why he sought to go through with them.
“He was aware of reality, aware of the nature of his acts. They were carefully planned, a degree of subterfuge was required, they were carefully planned and executed.
“Even if you consider that there was a delusional process, he remained lucid and in control of his actions.”
When the judge asked whether Chail’s actions could have been voluntary, the expert said his journal showed he had asked the chatbot “is this my purpose?”.
He added: “He said he remembered the bot’s instructions that his purpose was to live. That doesn’t read to me like someone in the grip of an overpowering delusion.”
The expert also said it was “unlikely” the defendant was suffering from any delusions if he had been seeking “notoriety” and to spread his beliefs through the press.
The hearing was told he began preparing for the attack in late 2021 and at the time described a sense of his “patience running out”.
In a journal, he wrote that if Queen Elizabeth II was “unobtainable” he would “go for” the “prince” as a “suitable figurehead”, in an apparent reference to the King, an earlier hearing was told.
The prosecution says that in late 2021 he applied to join the Ministry of Defence, the police, British Army, the Royal Marines and the Royal Navy, in a bid to get in close contact with the royal family.
He made internet searches for “Sandringham Christmas” and carried out research before buying a Supersonic crossbow in November 2021, and began communicating with Sarai via the Replika app the following month.
His barrister, Nadia Chbat, told the court he had described the attack as his “purpose” and something he had “always thought about”.
She added that he had had “imaginary friends” since childhood and one of them manifested themselves as the Sarai chatbot character.
Professor Blackwood said Chail talked about being able to make the voices “appear and disappear”, which he said could not mean he was suffering from delusions at the time.
He also said the defendant did not in fact believe he was “Sith” at the time and it was instead his “omnipotent fantasy”.
He added: “He is frustrated, mildly depressed and has a powerful belief he has been wronged, a great degree of self-pity and self-hatred.”
The judge will continue to hear evidence about Chail’s mental state at the time before deciding whether to sentence him to prison, a hospital order or a combination of the two.
Two other experts, Dr Christian Brown and Dr Jonathan Hafferty, have said that Chail was not acting rationally at the time.