Fix Toast __OO_STATE__ extraction: remove tag stripping and var keywords

Tag stripping via reReplace on 1.7M HTML was likely causing the silent
failure on biz server. Brace-counting doesn't need tag stripping since
HTML tags don't contain { or } and attribute quotes come in balanced
pairs. Also removed var keywords from page-level cfscript (may not be
supported in Lucee at template level) and added detailed error output
to the cfcatch for debugging.

Also auto-detect dev/prod environment from hostname instead of
hardcoded flag.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-03-01 20:28:02 -08:00
parent e7aaae58b7
commit 3a9f952d8b
2 changed files with 36 additions and 46 deletions

View file

@ -2,17 +2,14 @@
/**
* Environment Configuration
*
* Controls dev vs production behavior.
* This file should have DIFFERENT values on dev vs biz servers.
*
* dev.payfrit.com: isDevEnvironment = true
* biz.payfrit.com: isDevEnvironment = false
* Auto-detects dev vs production based on server hostname.
* No manual flag to change — safe to deploy via git pull to either server.
*/
// ============================================
// ENVIRONMENT FLAG - CHANGE PER SERVER
// ENVIRONMENT FLAG - AUTO-DETECTED
// ============================================
isDevEnvironment = true; // Set to FALSE on biz.payfrit.com
isDevEnvironment = (createObject("java", "java.net.InetAddress").getLocalHost().getHostName() != "biz");
// ============================================
// ERROR HANDLING

View file

@ -278,20 +278,16 @@
<!--- Also try to extract from __OO_STATE__ for images and business info --->
<cfif hasOoState>
<!--- Strip View Source HTML tags if present (Chrome Ctrl+U) --->
<cfset ooExtractHtml2 = pageHtml>
<cfif findNoCase('<span id="line', pageHtml)>
<cfset ooExtractHtml2 = reReplace(pageHtml, "<[^>]+>", "", "all")>
</cfif>
<cfscript>
ooStateJson2 = "";
ooStart2 = findNoCase("window.__OO_STATE__", ooExtractHtml2);
ooStart2 = findNoCase("window.__OO_STATE__", pageHtml);
if (ooStart2 > 0) {
var bs2 = find("{", ooExtractHtml2, ooStart2);
bs2 = find("{", pageHtml, ooStart2);
if (bs2 > 0) {
var d2 = 0; var inS2 = false; var esc2 = false; var be2 = 0;
for (var i2 = bs2; i2 <= len(ooExtractHtml2); i2++) {
var c2 = mid(ooExtractHtml2, i2, 1);
d2 = 0; inS2 = false; esc2 = false; be2 = 0;
totalLen2 = len(pageHtml);
for (i2 = bs2; i2 <= totalLen2; i2++) {
c2 = mid(pageHtml, i2, 1);
if (esc2) { esc2 = false; continue; }
if (c2 == chr(92) && inS2) { esc2 = true; continue; }
if (c2 == '"') { inS2 = !inS2; continue; }
@ -300,7 +296,7 @@
else if (c2 == "}") { d2 = d2 - 1; if (d2 == 0) { be2 = i2; break; } }
}
}
if (be2 > 0) ooStateJson2 = mid(ooExtractHtml2, bs2, be2 - bs2 + 1);
if (be2 > 0) ooStateJson2 = mid(pageHtml, bs2, be2 - bs2 + 1);
}
}
if (len(ooStateJson2)) {
@ -586,20 +582,16 @@
<!--- Check for __OO_STATE__ in other files too (might have Restaurant info) --->
<cfif findNoCase("window.__OO_STATE__", otherHtml)>
<!--- Strip View Source HTML tags if present --->
<cfset otherCleanHtml = otherHtml>
<cfif findNoCase('<span id="line', otherHtml)>
<cfset otherCleanHtml = reReplace(otherHtml, "<[^>]+>", "", "all")>
</cfif>
<cfscript>
otherOoJson = "";
var otherOoStart = findNoCase("window.__OO_STATE__", otherCleanHtml);
otherOoStart = findNoCase("window.__OO_STATE__", otherHtml);
if (otherOoStart > 0) {
var obs = find("{", otherCleanHtml, otherOoStart);
obs = find("{", otherHtml, otherOoStart);
if (obs > 0) {
var od = 0; var ois = false; var oesc = false; var obe = 0;
for (var oi = obs; oi <= len(otherCleanHtml); oi++) {
var oc = mid(otherCleanHtml, oi, 1);
od = 0; ois = false; oesc = false; obe = 0;
otherLen = len(otherHtml);
for (oi = obs; oi <= otherLen; oi++) {
oc = mid(otherHtml, oi, 1);
if (oesc) { oesc = false; continue; }
if (oc == chr(92) && ois) { oesc = true; continue; }
if (oc == '"') { ois = !ois; continue; }
@ -608,7 +600,7 @@
else if (oc == "}") { od = od - 1; if (od == 0) { obe = oi; break; } }
}
}
if (obe > 0) otherOoJson = mid(otherCleanHtml, obs, obe - obs + 1);
if (obe > 0) otherOoJson = mid(otherHtml, obs, obe - obs + 1);
}
}
if (len(otherOoJson)) {
@ -998,25 +990,20 @@
<cfif findNoCase("window.__OO_STATE__", pageHtml) AND findNoCase("toasttab", pageHtml)>
<cfset arrayAppend(response.steps, "Toast page detected - extracting menu data from __OO_STATE__")>
<cftry>
<!--- Strip View Source HTML tags (Chrome Ctrl+U saves with <span> wrappers that break regex) --->
<cfset ooExtractHtml = pageHtml>
<cfif findNoCase('<span id="line', pageHtml)>
<cfset ooExtractHtml = reReplace(pageHtml, "<[^>]+>", "", "all")>
</cfif>
<!--- Extract JSON using brace-counting (regex .*? fails on 500K+ JSON due to backtracking) --->
<!--- Extract JSON using brace-counting (no tag stripping needed - braces don't appear in HTML tags) --->
<cfscript>
ooStateJson = "";
ooStartPos = findNoCase("window.__OO_STATE__", ooExtractHtml);
ooStartPos = findNoCase("window.__OO_STATE__", pageHtml);
if (ooStartPos > 0) {
braceStart = find("{", ooExtractHtml, ooStartPos);
braceStart = find("{", pageHtml, ooStartPos);
if (braceStart > 0) {
var depth = 0;
var inStr = false;
var esc = false;
var braceEnd = 0;
var totalLen = len(ooExtractHtml);
for (var ci = braceStart; ci <= totalLen; ci++) {
var ch = mid(ooExtractHtml, ci, 1);
depth = 0;
inStr = false;
esc = false;
braceEnd = 0;
totalLen = len(pageHtml);
for (ci = braceStart; ci <= totalLen; ci++) {
ch = mid(pageHtml, ci, 1);
if (esc) { esc = false; continue; }
if (ch == chr(92) && inStr) { esc = true; continue; }
if (ch == '"') { inStr = !inStr; continue; }
@ -1029,7 +1016,7 @@
}
}
if (braceEnd > 0) {
ooStateJson = mid(ooExtractHtml, braceStart, braceEnd - braceStart + 1);
ooStateJson = mid(pageHtml, braceStart, braceEnd - braceStart + 1);
}
}
}
@ -1406,7 +1393,13 @@
</cfif>
</cfif>
<cfcatch>
<cfset arrayAppend(response.steps, "Toast __OO_STATE__ parsing failed: " & cfcatch.message & " - falling back to Claude")>
<cfset toastError = "Toast __OO_STATE__ parsing failed: " & cfcatch.message>
<cfif len(cfcatch.detail)><cfset toastError = toastError & " | Detail: " & cfcatch.detail></cfif>
<cfif structKeyExists(cfcatch, "tagContext") AND isArray(cfcatch.tagContext) AND arrayLen(cfcatch.tagContext) GT 0>
<cfset toastError = toastError & " | Line: " & cfcatch.tagContext[1].line & " in " & listLast(cfcatch.tagContext[1].template, "/")>
</cfif>
<cfset arrayAppend(response.steps, toastError & " - falling back to Claude")>
<cfset response["DEBUG_TOAST_ERROR"] = toastError>
</cfcatch>
</cftry>
</cfif>