Spaces:
Running
Running
File size: 5,401 Bytes
22cfd36 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #!/usr/bin/env python3
"""
Frontend-only half of patch_fix_approval_buttons.py.
Runs in the node:20-alpine frontend-builder stage where
/source/frontend/src/hooks/useAgentChat.ts exists. The backend half
(session.py / session_manager.py / routes/agent.py) runs in the
python:3.12 stage via patch_fix_approval_buttons.py.
Idempotent — safe to run in either order.
"""
import os
MARKER_TS = "// === PATCH: fix-approval-buttons ==="
FSRC = "/source/frontend/src/hooks/useAgentChat.ts"
def _read(path):
with open(path, encoding="utf-8") as f:
return f.read()
def _write(path, content):
with open(path, "w", encoding="utf-8") as f:
f.write(content)
def _replace_all(content, old, new):
if old in content:
return content.replace(old, new), True
return content, False
if not os.path.exists(FSRC):
print(f"SKIP: {FSRC} not found (not in this build stage)")
raise SystemExit(0)
content = _read(FSRC)
if MARKER_TS in content:
print("SKIP: useAgentChat.ts already patched")
raise SystemExit(0)
changed = False
# 4a. Helper: does this message list contain an approval-requested part?
old_waiting = "function waitingApprovalStatus(items: PendingApprovalItem[] | undefined) {"
helper = (
"// === PATCH: fix-approval-buttons ===\n"
"/** True if any tool part is currently awaiting approval. */\n"
"function _hasPendingApproval(msgs: UIMessage[]): boolean {\n"
" return msgs.some((m) =>\n"
" m.parts.some(\n"
" (p) => p.type === 'dynamic-tool' && p.state === 'approval-requested',\n"
" ),\n"
" );\n"
"}\n"
"// === PATCH: fix-approval-buttons ===\n"
"\n"
"function waitingApprovalStatus(items: PendingApprovalItem[] | undefined) {"
)
content, ok = _replace_all(content, old_waiting, helper)
if ok:
print(" + added _hasPendingApproval helper")
changed = True
# 4b. Replace `_catchupDone ? false :` in EVERY hydration guard with
# `_catchupDone ? _hasPendingApproval(uiMsgs) :` so approval rounds
# always hydrate, including the approval_required SSE branch.
variants = [
(
" const _shouldOverwrite = _catchupDone\n"
" ? false\n"
" : (uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0);\n",
" const _shouldOverwrite = _catchupDone\n"
" ? _hasPendingApproval(uiMsgs)\n"
" : (uiMsgs.length > _currentNonSystem.length || _currentNonSystem.length === 0);\n",
),
(
" const _shouldOverwrite2 = _catchupDone\n"
" ? false\n"
" : (uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0);\n",
" const _shouldOverwrite2 = _catchupDone\n"
" ? _hasPendingApproval(uiMsgs)\n"
" : (uiMsgs.length > _currentNonSystem2.length || _currentNonSystem2.length === 0);\n",
),
(
" const _shouldSetB = _catchupDone\n"
" ? false\n"
" : (uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0);\n",
" const _shouldSetB = _catchupDone\n"
" ? _hasPendingApproval(uiMsgs)\n"
" : (uiMsgs.length > _currentMsgsB.length || _currentMsgsB.length === 0);\n",
),
(
" const _shouldSetC = _catchupDone\n"
" ? false\n"
" : (uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0);\n",
" const _shouldSetC = _catchupDone\n"
" ? _hasPendingApproval(uiMsgs)\n"
" : (uiMsgs.length > _currentMsgsC.length || _currentMsgsC.length === 0);\n",
),
(
" const _shouldSet = _catchupDone\n"
" ? false\n"
" : (uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0);\n",
" const _shouldSet = _catchupDone\n"
" ? _hasPendingApproval(uiMsgs)\n"
" : (uiMsgs.length > _currentMsgs.length || _currentMsgs.length === 0);\n",
),
]
for old_g, new_g in variants:
content, ok = _replace_all(content, old_g, new_g)
if ok:
print(" + hydration guard -> _hasPendingApproval")
changed = True
# 4c. Any remaining generic `_catchupDone ? false` (loose form).
old_loose = "_catchupDone\n ? false\n"
while old_loose in content:
content = content.replace(
old_loose,
"_catchupDone\n ? _hasPendingApproval(uiMsgs)\n",
1,
)
changed = True
print(" + loose _catchupDone ? false -> _hasPendingApproval")
# 4d. event-stream consume guard (_c3a) -- add approval bypass.
old3 = (
" if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _c3a.length)) {\n"
)
new3 = (
" if (uiMsgs.length > 0 && (!_catchupDone || uiMsgs.length > _c3a.length || _hasPendingApproval(uiMsgs))) {\n"
)
content, ok = _replace_all(content, old3, new3)
if ok:
print(" + event-stream consume guard honors pending approvals")
changed = True
else:
print(" INFO: _c3a consume guard not found (may differ in fork)")
if changed:
_write(FSRC, content)
print("OK: useAgentChat.ts patched (frontend approval bypass)")
else:
print("SKIP: useAgentChat.ts - no changes needed") |