Hi Lorraine,
I have the same problem every now and again, usually when I have brought in potted plants for the winter that have been sitting outside in the dirt all summer. Gross, isn't it. For the most part worms are very beneficial BUT it is possible to have too much of a good thing. And if their population explodes they can kill your plant. Been there, done that. I can give you a few suggestions to try.
I brought in my 2 Dragon Fruit plants (totally different than your Dragon plant) to winter inside about 2 months ago and very soon saw the worms. Dozens of small dead worms stuck to the tile floor around each one. Yuck! So gross! I only water the plants every 7-10 days and they would appear the next day. So, when there's a lot of water in the pot they quickly exit so they don't drown. Just an observation. Since my Dragons are a cactus I didn't water them for 2 weeks so the soil would seriously dry out and then I watered them with a small amount of hydrogen peroxide diluted in water. I've done this many times on plants over the years to get rid of fungus gnats. Peroxide is actually very good for your plants. You can look this up online. It seems to have worked. But according to this website it doesn't work on all worms.
https://www.sciencedaily.com/r...
It also happened to me a couple of years back when I brought in some potted tomato plants. Couldn't let them dry out much, they wilt too quickly. So I did a soil drench using permethrin. If your organic you can use organic pyrethrins but it is very expensive so I use the synthetic permethrin. Lots of companies manufacture it under different names. Just search for 'buy permethrins' and you will find many products. Hope this helps.