fix: AI claims/preauths keep all codes past 10; reuse CCA/DentalHub Chrome

- Claim form: AI claim/preauth prefill adds rows beyond the initial 10
  instead of dropping codes 11+
- CDT lookup: "D6010 #5, 9, 11, 12" expands to one line per tooth, and
  the chat graph rebuilds such lists from the message if the LLM drops items
- Eligibility: match an existing patient by name+DOB when the insurance ID
  changed (e.g. MassHealth → CCA) instead of creating a duplicate patient
- CCA PreAuth: services grid scrolls to reach rows 11+
- CCA and Tufts SCO PreAuth: tentative service date = today + 3 days
- CCA (eligibility/claim/preauth) and DentalHub (claim/preauth): keep Chrome
  open, blank and minimized, after a job so the next job reuses it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 01:27:28 -04:00
co-authored by Claude Opus 5.5
parent 8937a3390c
commit 143319909f
13 changed files with 218 additions and 63 deletions
+29 -1
View File
@@ -630,6 +630,27 @@ function expandCompositeLists(procedureNames: string[]): string[] {
return out; return out;
} }
// Explicit code + tooth list with no surfaces, e.g. "D6010 #5, 9 , 11, 12" or "D6057 #5 #9 and 11"
const CODE_TOOTH_LIST_RE =
/\b(D\d{4})\s*#\s*(\d{1,2}(?:\s*(?:,|&|\band\b)?\s*#?\s*\d{1,2})*)\b(?!\s*[OMDBLFIV]{1,5}\b)/gi;
/**
* Split explicit CDT codes followed by a tooth list into one entry per tooth.
* Returns null when the text has no code with more than one tooth.
* e.g. "D6010 #5, 9, 11, 12 D6057 #5, 9" → ["D6010 #5", "D6010 #9", "D6010 #11", "D6010 #12", "D6057 #5", "D6057 #9"]
*/
export function expandCodeToothLists(text: string): string[] | null {
const out: string[] = [];
let multi = false;
for (const m of text.matchAll(CODE_TOOTH_LIST_RE)) {
const code = m[1]!.toUpperCase();
const teeth = m[2]!.match(/\d{1,2}/g) ?? [];
if (teeth.length > 1) multi = true;
for (const t of teeth) out.push(`${code} #${t}`);
}
return multi ? out : null;
}
/** /**
* Map an array of free-text procedure names to CDT codes. * Map an array of free-text procedure names to CDT codes.
* customAliases (from DB) are checked before the hardcoded ALIAS_MAP. * customAliases (from DB) are checked before the hardcoded ALIAS_MAP.
@@ -648,7 +669,14 @@ export function lookupCdtCodes(
// Pontics with no stated material — re-coded below to match the crowns // Pontics with no stated material — re-coded below to match the crowns
const inferredPontics: CdtMatch[] = []; const inferredPontics: CdtMatch[] = [];
const results = expandCompositeLists(procedureNames).flatMap((name): (CdtMatch | { code: null; description: string; input: string })[] => { // Only split an entry that is nothing but code + tooth lists, so no other text is lost
const expandedNames = procedureNames.flatMap((n) => {
const split = expandCodeToothLists(n);
const rest = n.replace(CODE_TOOTH_LIST_RE, "").replace(/[\s,;&]|\band\b/gi, "");
return split && rest === "" ? split : [n];
});
const results = expandCompositeLists(expandedNames).flatMap((name): (CdtMatch | { code: null; description: string; input: string })[] => {
const cleaned = name.trim().toLowerCase(); const cleaned = name.trim().toLowerCase();
// If the LLM emitted a bare tooth-number token like "# 32" or "#32" with no procedure, // If the LLM emitted a bare tooth-number token like "# 32" or "#32" with no procedure,
@@ -1,4 +1,5 @@
import { getLlm, type AiProvider } from "./llm-factory"; import { getLlm, type AiProvider } from "./llm-factory";
import { expandCodeToothLists } from "./cdt-lookup";
// ─── Intent types ───────────────────────────────────────────────────────────── // ─── Intent types ─────────────────────────────────────────────────────────────
@@ -254,6 +255,24 @@ function restoreBarePaCount(message: string, names: string[]): string[] {
return kept; return kept;
} }
// "D6010 #5, 9, 11, 12 D6057 #5, 9, 11, 12" — the LLM can drop or merge items when
// splitting long tooth lists, so rebuild those entries from the message itself.
export function restoreCodeToothLists(message: string, names: string[]): string[] {
const expanded = expandCodeToothLists(message);
if (!expanded) return names;
const codes = new Set(expanded.map((e) => e.split(" ")[0]!));
const isCovered = (n: string) => {
const s = n.trim();
if (/^#?\s*\d{1,2}$/.test(s)) return true; // stray tooth number from the split
const m = s.match(/^(D\d{4})\b/i);
return !!m && codes.has(m[1]!.toUpperCase());
};
const first = names.findIndex(isCovered);
const kept = names.filter((n) => !isCovered(n));
kept.splice(first === -1 ? kept.length : first, 0, ...expanded);
return kept;
}
function parseBareIdDob(message: string): { memberId: string; dob: string } | null { function parseBareIdDob(message: string): { memberId: string; dob: string } | null {
const text = message.trim(); const text = message.trim();
const dobPattern = /(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})/; const dobPattern = /(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})/;
@@ -347,6 +366,7 @@ export async function classifyInternalChat(
const parsed = JSON.parse(jsonStr) as ChatClassification; const parsed = JSON.parse(jsonStr) as ChatClassification;
if (!parsed.intent || !parsed.fallbackReply) return fallback; if (!parsed.intent || !parsed.fallbackReply) return fallback;
if (parsed.procedureNames) parsed.procedureNames = restoreBarePaCount(message, parsed.procedureNames); if (parsed.procedureNames) parsed.procedureNames = restoreBarePaCount(message, parsed.procedureNames);
if (parsed.procedureNames) parsed.procedureNames = restoreCodeToothLists(message, parsed.procedureNames);
return parsed; return parsed;
} catch (err) { } catch (err) {
const msg = err instanceof Error ? err.message : String(err); const msg = err instanceof Error ? err.message : String(err);
@@ -125,6 +125,15 @@ export async function createOrUpdatePatientByInsuranceId(options: {
console.log(`[createOrUpdatePatient] id-only fallback (no DOB on record): found id=${patient.id}`); console.log(`[createOrUpdatePatient] id-only fallback (no DOB on record): found id=${patient.id}`);
} }
} }
if (!patient && incomingFirst && incomingLast) {
// Same person whose insurance changed (e.g. MassHealth → CCA): match by
// name+DOB and move them to the new insuranceId instead of duplicating.
const byNameDob = await storage.getPatientByNameAndDob(incomingFirst, incomingLast, dobDate);
if (byNameDob) {
patient = byNameDob;
console.log(`[createOrUpdatePatient] name+DOB fallback: found id=${patient.id} (insuranceId "${patient.insuranceId}" → "${normalizedId}")`);
}
}
} else { } else {
// No DOB supplied — fall back to insuranceId-only (legacy / non-family plans) // No DOB supplied — fall back to insuranceId-only (legacy / non-family plans)
patient = await storage.getPatientByInsuranceId(normalizedId); patient = await storage.getPatientByInsuranceId(normalizedId);
@@ -138,6 +147,7 @@ export async function createOrUpdatePatientByInsuranceId(options: {
if (incomingLast && String(patient.lastName ?? "").trim() !== incomingLast) if (incomingLast && String(patient.lastName ?? "").trim() !== incomingLast)
updates.lastName = incomingLast; updates.lastName = incomingLast;
if (dobDate && !patient.dateOfBirth) updates.dateOfBirth = dobDate; if (dobDate && !patient.dateOfBirth) updates.dateOfBirth = dobDate;
if (String(patient.insuranceId ?? "") !== normalizedId) updates.insuranceId = normalizedId;
if (Object.keys(updates).length > 0) { if (Object.keys(updates).length > 0) {
console.log(`[createOrUpdatePatient] updating patient id=${patient.id} with`, updates); console.log(`[createOrUpdatePatient] updating patient id=${patient.id} with`, updates);
await storage.updatePatient(patient.id, updates); await storage.updatePatient(patient.id, updates);
@@ -12,6 +12,7 @@ export interface IStorage {
getPatientByInsuranceId(insuranceId: string): Promise<Patient | null>; getPatientByInsuranceId(insuranceId: string): Promise<Patient | null>;
getPatientByInsuranceIdAndDob(insuranceId: string, dob: Date): Promise<Patient | null>; getPatientByInsuranceIdAndDob(insuranceId: string, dob: Date): Promise<Patient | null>;
getPatientByInsuranceIdNoDob(insuranceId: string): Promise<Patient | null>; getPatientByInsuranceIdNoDob(insuranceId: string): Promise<Patient | null>;
getPatientByNameAndDob(firstName: string, lastName: string, dob: Date): Promise<Patient | null>;
getAllPatients(): Promise<Patient[]>; getAllPatients(): Promise<Patient[]>;
getRecentPatients(limit: number, offset: number): Promise<Patient[]>; getRecentPatients(limit: number, offset: number): Promise<Patient[]>;
getPatientsByIds(ids: number[]): Promise<Patient[]>; getPatientsByIds(ids: number[]): Promise<Patient[]>;
@@ -81,6 +82,21 @@ export const patientsStorage: IStorage = {
}); });
}, },
async getPatientByNameAndDob(firstName: string, lastName: string, dob: Date): Promise<Patient | null> {
const startOfDay = new Date(dob);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(dob);
endOfDay.setHours(23, 59, 59, 999);
return db.patient.findFirst({
where: {
firstName: { equals: firstName, mode: "insensitive" },
lastName: { equals: lastName, mode: "insensitive" },
dateOfBirth: { gte: startOfDay, lte: endOfDay },
},
orderBy: { updatedAt: "desc" },
});
},
async getRecentPatients(limit: number, offset: number): Promise<Patient[]> { async getRecentPatients(limit: number, offset: number): Promise<Patient[]> {
return db.patient.findMany({ return db.patient.findMany({
skip: offset, skip: offset,
@@ -576,16 +576,26 @@ export function ClaimForm({
const date = serviceDate ? serviceDate : prev.serviceDate; const date = serviceDate ? serviceDate : prev.serviceDate;
const updatedLines = [...prev.serviceLines]; const updatedLines = [...prev.serviceLines];
codes.forEach((c, i) => { codes.forEach((c, i) => {
if (i < updatedLines.length) { // The form starts with 10 blank rows — add rows for codes beyond that
updatedLines[i] = { const base = updatedLines[i] ?? {
...updatedLines[i]!, procedureCode: "",
procedureCode: c.code, procedureDate: date,
procedureDate: date, quad: "",
...(c.toothNumber ? { toothNumber: c.toothNumber } : {}), arch: "",
...(c.toothSurface ? { toothSurface: c.toothSurface } : {}), toothNumber: "",
...(c.quad ? { quad: c.quad } : {}), toothSurface: "",
}; totalBilled: new Decimal(0),
} totalAdjusted: new Decimal(0),
totalPaid: new Decimal(0),
};
updatedLines[i] = {
...base,
procedureCode: c.code,
procedureDate: date,
...(c.toothNumber ? { toothNumber: c.toothNumber } : {}),
...(c.toothSurface ? { toothSurface: c.toothSurface } : {}),
...(c.quad ? { quad: c.quad } : {}),
};
}); });
return { ...prev, serviceLines: updatedLines }; return { ...prev, serviceLines: updatedLines };
}); });
@@ -620,16 +630,26 @@ export function ClaimForm({
const insuranceSiteKey = siteKey ? siteKey.replace(/_/g, "").toLowerCase() : prev.insuranceSiteKey; const insuranceSiteKey = siteKey ? siteKey.replace(/_/g, "").toLowerCase() : prev.insuranceSiteKey;
const updatedLines = [...prev.serviceLines]; const updatedLines = [...prev.serviceLines];
codes.forEach((c, i) => { codes.forEach((c, i) => {
if (i < updatedLines.length) { // The form starts with 10 blank rows — add rows for codes beyond that
updatedLines[i] = { const base = updatedLines[i] ?? {
...updatedLines[i]!, procedureCode: "",
procedureCode: c.code, procedureDate: date,
procedureDate: date, quad: "",
...(c.toothNumber ? { toothNumber: c.toothNumber } : {}), arch: "",
...(c.toothSurface ? { toothSurface: c.toothSurface } : {}), toothNumber: "",
...(c.quad ? { quad: c.quad } : {}), toothSurface: "",
}; totalBilled: new Decimal(0),
} totalAdjusted: new Decimal(0),
totalPaid: new Decimal(0),
};
updatedLines[i] = {
...base,
procedureCode: c.code,
procedureDate: date,
...(c.toothNumber ? { toothNumber: c.toothNumber } : {}),
...(c.toothSurface ? { toothSurface: c.toothSurface } : {}),
...(c.quad ? { quad: c.quad } : {}),
};
}); });
const filled = { ...prev, insuranceSiteKey, serviceLines: updatedLines }; const filled = { ...prev, insuranceSiteKey, serviceLines: updatedLines };
return mapPricesForForm({ return mapPricesForForm({
+16 -5
View File
@@ -50,11 +50,22 @@ def _close_browser(bot):
bm.save_cookies() bm.save_cookies()
except Exception: except Exception:
pass pass
try: # Keep Chrome open (blank + minimized) so the next CCA job reuses it — faster
bm.quit_driver() # than starting a new browser. get_driver() recreates it if it was closed.
print("[CCA Claim] Browser closed") if bot and bot.driver:
except Exception: try:
pass bot.driver.get("about:blank")
except Exception:
pass
try:
bot.driver.minimize_window()
print("[CCA Claim] Browser kept open (minimized)")
except Exception:
try:
bot.driver.set_window_position(-10000, -10000)
print("[CCA Claim] Browser kept open (moved off-screen)")
except Exception:
pass
except Exception as e: except Exception as e:
print(f"[CCA Claim] Could not close browser: {e}") print(f"[CCA Claim] Could not close browser: {e}")
@@ -51,11 +51,22 @@ def _close_browser(bot):
bm.save_cookies() bm.save_cookies()
except Exception: except Exception:
pass pass
try: # Keep Chrome open (blank + minimized) so the next CCA job reuses it — faster
bm.quit_driver() # than starting a new browser. get_driver() recreates it if it was closed.
print("[CCA] Browser closed") if bot and bot.driver:
except Exception: try:
pass bot.driver.get("about:blank")
except Exception:
pass
try:
bot.driver.minimize_window()
print("[CCA] Browser kept open (minimized)")
except Exception:
try:
bot.driver.set_window_position(-10000, -10000)
print("[CCA] Browser kept open (moved off-screen)")
except Exception:
pass
except Exception as e: except Exception as e:
print(f"[CCA] Could not close browser: {e}") print(f"[CCA] Could not close browser: {e}")
+16 -5
View File
@@ -50,11 +50,22 @@ def _close_browser(bot):
bm.save_cookies() bm.save_cookies()
except Exception: except Exception:
pass pass
try: # Keep Chrome open (blank + minimized) so the next CCA job reuses it — faster
bm.quit_driver() # than starting a new browser. get_driver() recreates it if it was closed.
print("[CCA PreAuth] Browser closed") if bot and bot.driver:
except Exception: try:
pass bot.driver.get("about:blank")
except Exception:
pass
try:
bot.driver.minimize_window()
print("[CCA PreAuth] Browser kept open (minimized)")
except Exception:
try:
bot.driver.set_window_position(-10000, -10000)
print("[CCA PreAuth] Browser kept open (moved off-screen)")
except Exception:
pass
except Exception as e: except Exception as e:
print(f"[CCA PreAuth] Could not close browser: {e}") print(f"[CCA PreAuth] Could not close browser: {e}")
@@ -297,13 +297,14 @@ async def start_uniteddh_claim_run(sid: str, data: dict, url: str):
s["result"] = result s["result"] = result
s["message"] = "completed" s["message"] = "completed"
# Close browser window (session preserved in profile via UnitedSCO browser manager) # Keep Chrome open (blank + minimized) so the next job reuses it — faster than
# starting a new browser. The browser manager recreates it if it was closed.
try: try:
from unitedsco_browser_manager import get_browser_manager as _gbm bot.driver.get("about:blank")
_gbm().quit_driver() bot.driver.minimize_window()
print("[UnitedDH Claim] Browser closed - session preserved in profile") print("[UnitedDH Claim] Browser kept open (minimized)")
except Exception as close_err: except Exception as hide_err:
print(f"[UnitedDH Claim] Could not close browser (non-fatal): {close_err}") print(f"[UnitedDH Claim] Could not minimize browser (non-fatal): {hide_err}")
asyncio.create_task(_remove_session_later(sid, 60)) asyncio.create_task(_remove_session_later(sid, 60))
return result return result
@@ -294,13 +294,14 @@ async def start_uniteddh_preauth_run(sid: str, data: dict, url: str):
s["result"] = result s["result"] = result
s["message"] = "completed" s["message"] = "completed"
# Close browser window (session preserved in profile via UnitedSCO browser manager) # Keep Chrome open (blank + minimized) so the next job reuses it — faster than
# starting a new browser. The browser manager recreates it if it was closed.
try: try:
from unitedsco_browser_manager import get_browser_manager as _gbm bot.driver.get("about:blank")
_gbm().quit_driver() bot.driver.minimize_window()
print("[UnitedDH PreAuth] Browser closed - session preserved in profile") print("[UnitedDH PreAuth] Browser kept open (minimized)")
except Exception as close_err: except Exception as hide_err:
print(f"[UnitedDH PreAuth] Could not close browser (non-fatal): {close_err}") print(f"[UnitedDH PreAuth] Could not minimize browser (non-fatal): {hide_err}")
asyncio.create_task(_remove_session_later(sid, 60)) asyncio.create_task(_remove_session_later(sid, 60))
return result return result
@@ -45,11 +45,14 @@ class AutomationCCAEligibilityCheck:
browser_manager.save_cookies() browser_manager.save_cookies()
except Exception as e: except Exception as e:
print(f"[CCA] Failed to save cookies before close: {e}") print(f"[CCA] Failed to save cookies before close: {e}")
# Keep Chrome open (blank + minimized) so the next CCA job reuses it — faster
# than starting a new browser. get_driver() recreates it if it was closed.
try: try:
browser_manager.quit_driver() self.driver.get("about:blank")
print("[CCA] Browser closed") self.driver.minimize_window()
print("[CCA] Browser kept open (minimized)")
except Exception as e: except Exception as e:
print(f"[CCA] Could not close browser: {e}") print(f"[CCA] Could not minimize browser: {e}")
def _force_logout(self): def _force_logout(self):
try: try:
@@ -9,7 +9,7 @@ import re
import json import json
import base64 import base64
import tempfile import tempfile
from datetime import date from datetime import date, timedelta
from cca_browser_manager import get_browser_manager from cca_browser_manager import get_browser_manager
@@ -34,10 +34,10 @@ class AutomationCCAPreAuth:
self.cca_password = claim.get("cca_password", "") or raw.get("cca_password", "") self.cca_password = claim.get("cca_password", "") or raw.get("cca_password", "")
self.memberId = claim.get("memberId", "") self.memberId = claim.get("memberId", "")
self.dateOfBirth = claim.get("dateOfBirth", "") self.dateOfBirth = claim.get("dateOfBirth", "")
# PreAuth uses tentative service date; falls back to serviceDate for compatibility # PreAuth tentative service date: 3 days after today unless one is given explicitly
self.serviceDate = ( self.serviceDate = (
claim.get("tentativeServiceDate") claim.get("tentativeServiceDate")
or claim.get("serviceDate", "") or (date.today() + timedelta(days=3)).isoformat()
) )
self.firstName = claim.get("firstName", "") or first_name self.firstName = claim.get("firstName", "") or first_name
self.lastName = claim.get("lastName", "") or last_name self.lastName = claim.get("lastName", "") or last_name
@@ -677,17 +677,35 @@ class AutomationCCAPreAuth:
print(f"[CCA PreAuth step3] Line {idx+1}: code={code}, tooth={tooth}, billed={billed_str}") print(f"[CCA PreAuth step3] Line {idx+1}: code={code}, tooth={tooth}, billed={billed_str}")
# Find the row element scoped to Services grid # Find the row element scoped to Services grid
# The grid shows 10 rows; rows 11+ sit below its inner scrollbar.
# Scroll the grid's content pane down and retry if the row isn't found yet.
row_el = None row_el = None
for xpath in [ for _scroll_try in range(4):
f"//div[@id='Services']//tr[@aria-rowindex='{row_idx}']", for xpath in [
f"//div[@id='Services']//div[contains(@class,'e-gridcontent')]//tbody/tr[{idx+1}]", f"//div[@id='Services']//tr[@aria-rowindex='{row_idx}']",
f"//div[@id='Services']//div[contains(@class,'e-content')]//tr[{idx+1}]", f"//div[@id='Services']//div[contains(@class,'e-gridcontent')]//tbody/tr[{idx+1}]",
]: f"//div[@id='Services']//div[contains(@class,'e-content')]//tr[{idx+1}]",
try: ]:
row_el = self.driver.find_element(By.XPATH, xpath) try:
row_el = self.driver.find_element(By.XPATH, xpath)
break
except Exception:
continue
if row_el:
break break
try:
self.driver.execute_script(
"var c=document.querySelector('#Services .e-content');"
"if(c){c.scrollTop=c.scrollTop+c.clientHeight;}")
time.sleep(0.6)
except Exception: except Exception:
continue pass
if row_el:
try:
self.driver.execute_script(
"arguments[0].scrollIntoView({block:'nearest',inline:'nearest'});", row_el)
except Exception:
pass
if not row_el: if not row_el:
print(f"[CCA PreAuth step3] WARNING: row {idx+1} not found — skipping") print(f"[CCA PreAuth step3] WARNING: row {idx+1} not found — skipping")
@@ -19,6 +19,7 @@ import base64
import re import re
from dentaquest_browser_manager import get_browser_manager from dentaquest_browser_manager import get_browser_manager
from datetime import date, timedelta
MEMBERS_URL = "https://providers.dentaquest.com/members" MEMBERS_URL = "https://providers.dentaquest.com/members"
@@ -37,7 +38,11 @@ class AutomationTuftsSCOPreAuth:
self.dateOfBirth = claim.get("dateOfBirth", "") self.dateOfBirth = claim.get("dateOfBirth", "")
self.firstName = claim.get("firstName", "") self.firstName = claim.get("firstName", "")
self.lastName = claim.get("lastName", "") self.lastName = claim.get("lastName", "")
self.serviceDate = claim.get("serviceDate", "") # PreAuth tentative service date: 3 days after today unless one is given explicitly
self.serviceDate = (
claim.get("tentativeServiceDate")
or (date.today() + timedelta(days=3)).isoformat()
)
self.serviceLines = claim.get("serviceLines", []) self.serviceLines = claim.get("serviceLines", [])
self.claimFiles = claim.get("claimFiles", []) self.claimFiles = claim.get("claimFiles", [])
self.patientName = claim.get("patientName", "") self.patientName = claim.get("patientName", "")