Right, I think there's no CLI tools or libs to edit an existing TWC, except
my cli script to upgrade core of a TWC, which may be helpful to implement what you have described.
I'm not sure what TW5 is capable of in terms of editing, though. And also there's an old ruby script for "slicing" a TW called
ginsu.
To implement what you are describing, it would be best to find some html parser, read TW like my cli script does, parse the tiddlers and also parse .tid files, and then do things with the .tid files and the content to insert or edit. I.e. presumably it's best to implement "serializers", "deserializers" and some kind of internal model for editing.
It would be also good to build a cli that builds TWC without TW5 (from the recipes and .tid files), because in that case we could update the outdated core building flow (for example, if the js files import each other and we can build the core by throwing away the imports, we could use Jest/Vitest for tests instead of using outdated tools). But honestly, these things are too far in the "nice to have" area for me, and I don't think I'll take care of them soon.
If you want to implement something in this area, I'll be glad to help, though.