@@ -6,9 +6,11 @@ import (
66 "context"
77 "encoding/json"
88 "errors"
9- "github.com/jackc/pgx/v5 "
9+ "regexp "
1010 "time"
1111
12+ "github.com/jackc/pgx/v5"
13+
1214 "github.com/Database-Hosting-Services/AI-Agent/RAG"
1315)
1416
@@ -99,6 +101,9 @@ func AgentQuery(projectUUID string, userID int64, prompt string, AI RAG.RAGmodel
99101 return nil , err
100102 }
101103
104+ // remove the json code segment with all the code from the response
105+ response .Response = removeJSONCodeSegments (response .Response )
106+
102107 // add the schema changes to the cache
103108 err = config .VerifyCache .Set ("schema-changes:" + projectUUID , response .SchemaDDL , 10 * time .Minute )
104109 if err != nil {
@@ -156,4 +161,20 @@ func ClearCacheForProject(projectUUID string) error {
156161 return err
157162 }
158163 return nil
159- }
164+ }
165+
166+ func removeJSONCodeSegments (text string ) string {
167+ // Remove JSON code blocks (```json...``` or ```...``` containing JSON)
168+ jsonCodeBlockRegex := regexp .MustCompile ("(?s)```(?:json)?\\ s*\\ {.*?\\ }```" )
169+ text = jsonCodeBlockRegex .ReplaceAllString (text , "" )
170+
171+ // Remove any remaining triple backticks blocks that might contain JSON
172+ codeBlockRegex := regexp .MustCompile ("(?s)```[^`]*```" )
173+ text = codeBlockRegex .ReplaceAllString (text , "" )
174+
175+ // Remove inline JSON code segments (single backticks containing JSON-like content)
176+ inlineJSONRegex := regexp .MustCompile ("`[^`]*\\ {[^}]*\\ }[^`]*`" )
177+ text = inlineJSONRegex .ReplaceAllString (text , "" )
178+
179+ return text
180+ }
0 commit comments