bep40 commited on
Commit
22cfd36
·
verified ·
1 Parent(s): 20d4252

Upload patch_fix_approval_buttons_frontend.py

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