@kelceyp/chrome-control-mcp 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.lock +40 -0
- package/handover.md +302 -0
- package/package.json +11 -21
- package/server/index.js +903 -0
- package/test-runner.js +170 -0
- package/test-simple.js +166 -0
- package/test-v1.0.2-adapter.js +207 -0
- package/dist/index.js +0 -143
package/test-runner.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Test runner that installs adapter if needed and sends test message
|
|
2
|
+
const testNumber = arguments[0] || 2;
|
|
3
|
+
|
|
4
|
+
// Only install if not already present or wrong version
|
|
5
|
+
if (!window.ChatBridge || window.ChatBridge.version !== '1.0.2' || !window.ChatBridge.sendMessage) {
|
|
6
|
+
const ADAPTER_VERSION = '1.0.2';
|
|
7
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
8
|
+
const now = () => performance.now();
|
|
9
|
+
const qAll = (sel, root = document) => Array.from(root.querySelectorAll(sel));
|
|
10
|
+
|
|
11
|
+
const countUser = () =>
|
|
12
|
+
qAll('[data-message-author-role="user"],[data-author-role="user"]').length;
|
|
13
|
+
const countAssistant = () =>
|
|
14
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').length;
|
|
15
|
+
const lastAssistantNode = () =>
|
|
16
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').at(-1) || null;
|
|
17
|
+
const inputEnabled = () => {
|
|
18
|
+
const tb =
|
|
19
|
+
document.querySelector('[role="textbox"][contenteditable="true"]') ||
|
|
20
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
21
|
+
return !!tb && tb.getAttribute('aria-disabled') !== 'true' && !tb.closest('[aria-disabled="true"]');
|
|
22
|
+
};
|
|
23
|
+
const pickText = (node) => {
|
|
24
|
+
const n = node || lastAssistantNode();
|
|
25
|
+
if (!n) return '';
|
|
26
|
+
const c = n.querySelector('.markdown, article') || n;
|
|
27
|
+
const t = c.innerText ?? c.textContent ?? '';
|
|
28
|
+
return (t || '').trim();
|
|
29
|
+
};
|
|
30
|
+
const lastAssistantTurn = () => {
|
|
31
|
+
const turns = qAll('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]');
|
|
32
|
+
for (let i = turns.length - 1; i >= 0; i--) {
|
|
33
|
+
if (turns[i].querySelector('[data-message-author-role="assistant"], [data-author-role="assistant"]')) {
|
|
34
|
+
return turns[i];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const content = lastAssistantNode();
|
|
38
|
+
return (
|
|
39
|
+
content?.closest('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]') ||
|
|
40
|
+
content ||
|
|
41
|
+
null
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
const hasTurnActions = (turn) => {
|
|
45
|
+
if (!turn) return false;
|
|
46
|
+
const toolbar =
|
|
47
|
+
turn.querySelector('[data-testid*="turn-actions"]') ||
|
|
48
|
+
turn.querySelector('[role="toolbar"]') ||
|
|
49
|
+
null;
|
|
50
|
+
if (!toolbar) return false;
|
|
51
|
+
const btns = toolbar.querySelectorAll(
|
|
52
|
+
'button[aria-label*="Regenerate"],' +
|
|
53
|
+
'button[aria-label*="Copy"],' +
|
|
54
|
+
'button[aria-label*="Share"],' +
|
|
55
|
+
'button[aria-label*="Like"],' +
|
|
56
|
+
'button[aria-label*="Dislike"],' +
|
|
57
|
+
'[data-testid*="thumb"]'
|
|
58
|
+
);
|
|
59
|
+
return btns.length > 0;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const waitForResponseWithBaseline = async (
|
|
63
|
+
baseline,
|
|
64
|
+
{
|
|
65
|
+
timeoutMs = 120000,
|
|
66
|
+
idlePrimaryMs = 1200,
|
|
67
|
+
idleConfirmMs = 3300,
|
|
68
|
+
finalButtonGraceMs = 2000
|
|
69
|
+
} = {}
|
|
70
|
+
) => {
|
|
71
|
+
const t0 = now();
|
|
72
|
+
const preUser = baseline?.user ?? countUser();
|
|
73
|
+
const preAsst = baseline?.asst ?? countAssistant();
|
|
74
|
+
|
|
75
|
+
if (countUser() < preUser + 1) {
|
|
76
|
+
while (now() - t0 < timeoutMs) {
|
|
77
|
+
if (countUser() >= preUser + 1) break;
|
|
78
|
+
await sleep(60);
|
|
79
|
+
}
|
|
80
|
+
if (countUser() < preUser + 1) {
|
|
81
|
+
return { ok: false, reason: 'userMessageNotCommitted', ms: now() - t0 };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (countAssistant() < preAsst + 1) {
|
|
86
|
+
while (now() - t0 < timeoutMs) {
|
|
87
|
+
if (countAssistant() >= preAsst + 1) break;
|
|
88
|
+
await sleep(60);
|
|
89
|
+
}
|
|
90
|
+
if (countAssistant() < preAsst + 1) {
|
|
91
|
+
return { ok: false, reason: 'assistantNeverStarted', ms: now() - t0 };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let lastLen = -1;
|
|
96
|
+
let lastChange = now();
|
|
97
|
+
let confirmStart = null;
|
|
98
|
+
|
|
99
|
+
while (now() - t0 < timeoutMs) {
|
|
100
|
+
const node = lastAssistantNode();
|
|
101
|
+
const txt = pickText(node);
|
|
102
|
+
const len = txt.length;
|
|
103
|
+
|
|
104
|
+
if (len !== lastLen) {
|
|
105
|
+
lastLen = len;
|
|
106
|
+
lastChange = now();
|
|
107
|
+
confirmStart = null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const idle = now() - lastChange;
|
|
111
|
+
|
|
112
|
+
if (idle >= idlePrimaryMs && inputEnabled()) {
|
|
113
|
+
if (idle >= idleConfirmMs) {
|
|
114
|
+
const turn = lastAssistantTurn();
|
|
115
|
+
if (hasTurnActions(turn)) {
|
|
116
|
+
return { ok: true, text: txt, ms: now() - t0 };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (confirmStart == null) confirmStart = now();
|
|
120
|
+
if (now() - confirmStart >= finalButtonGraceMs) {
|
|
121
|
+
return { ok: true, text: txt, ms: now() - t0, note: 'noButtonsConfirm' };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
await sleep(100);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { ok: false, reason: 'timeout', ms: now() - t0 };
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const sendMessage = async (message) => {
|
|
133
|
+
const baseline = { user: countUser(), asst: countAssistant() };
|
|
134
|
+
|
|
135
|
+
const textbox =
|
|
136
|
+
document.querySelector('[role="textbox")[contenteditable="true"]') ||
|
|
137
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
138
|
+
|
|
139
|
+
if (!textbox) {
|
|
140
|
+
return { ok: false, reason: 'noTextbox' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
textbox.focus();
|
|
144
|
+
textbox.innerText = message;
|
|
145
|
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
|
146
|
+
|
|
147
|
+
await sleep(100);
|
|
148
|
+
const sendBtn = document.querySelector('[data-testid="send-button"]') ||
|
|
149
|
+
qAll('button').find(b =>
|
|
150
|
+
b.getAttribute('aria-label')?.includes('Send') ||
|
|
151
|
+
b.innerText?.includes('Send')
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
if (!sendBtn) {
|
|
155
|
+
return { ok: false, reason: 'noSendButton' };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
sendBtn.click();
|
|
159
|
+
return await waitForResponseWithBaseline(baseline);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Install adapter
|
|
163
|
+
window.ChatBridge = {
|
|
164
|
+
version: ADAPTER_VERSION,
|
|
165
|
+
sendMessage
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Run test
|
|
170
|
+
return await window.ChatBridge.sendMessage(`[CC] Test ${testNumber}/5: Consistency check - reply TEST-OK-${testNumber}`);
|
package/test-simple.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// First install the adapter, then send a test message
|
|
2
|
+
const ADAPTER_VERSION = '1.0.2';
|
|
3
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
4
|
+
const now = () => performance.now();
|
|
5
|
+
const qAll = (sel, root = document) => Array.from(root.querySelectorAll(sel));
|
|
6
|
+
|
|
7
|
+
const countUser = () =>
|
|
8
|
+
qAll('[data-message-author-role="user"],[data-author-role="user"]').length;
|
|
9
|
+
const countAssistant = () =>
|
|
10
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').length;
|
|
11
|
+
const lastAssistantNode = () =>
|
|
12
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').at(-1) || null;
|
|
13
|
+
const inputEnabled = () => {
|
|
14
|
+
const tb =
|
|
15
|
+
document.querySelector('[role="textbox"][contenteditable="true"]') ||
|
|
16
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
17
|
+
return !!tb && tb.getAttribute('aria-disabled') !== 'true' && !tb.closest('[aria-disabled="true"]');
|
|
18
|
+
};
|
|
19
|
+
const pickText = (node) => {
|
|
20
|
+
const n = node || lastAssistantNode();
|
|
21
|
+
if (!n) return '';
|
|
22
|
+
const c = n.querySelector('.markdown, article') || n;
|
|
23
|
+
const t = c.innerText ?? c.textContent ?? '';
|
|
24
|
+
return (t || '').trim();
|
|
25
|
+
};
|
|
26
|
+
const lastAssistantTurn = () => {
|
|
27
|
+
const turns = qAll('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]');
|
|
28
|
+
for (let i = turns.length - 1; i >= 0; i--) {
|
|
29
|
+
if (turns[i].querySelector('[data-message-author-role="assistant"], [data-author-role="assistant"]')) {
|
|
30
|
+
return turns[i];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const content = lastAssistantNode();
|
|
34
|
+
return (
|
|
35
|
+
content?.closest('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]') ||
|
|
36
|
+
content ||
|
|
37
|
+
null
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
const hasTurnActions = (turn) => {
|
|
41
|
+
if (!turn) return false;
|
|
42
|
+
const toolbar =
|
|
43
|
+
turn.querySelector('[data-testid*="turn-actions"]') ||
|
|
44
|
+
turn.querySelector('[role="toolbar"]') ||
|
|
45
|
+
null;
|
|
46
|
+
if (!toolbar) return false;
|
|
47
|
+
const btns = toolbar.querySelectorAll(
|
|
48
|
+
'button[aria-label*="Regenerate"],' +
|
|
49
|
+
'button[aria-label*="Copy"],' +
|
|
50
|
+
'button[aria-label*="Share"],' +
|
|
51
|
+
'button[aria-label*="Like"],' +
|
|
52
|
+
'button[aria-label*="Dislike"],' +
|
|
53
|
+
'[data-testid*="thumb"]'
|
|
54
|
+
);
|
|
55
|
+
return btns.length > 0;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const waitForResponseWithBaseline = async (
|
|
59
|
+
baseline,
|
|
60
|
+
{
|
|
61
|
+
timeoutMs = 120000,
|
|
62
|
+
idlePrimaryMs = 1200,
|
|
63
|
+
idleConfirmMs = 3300,
|
|
64
|
+
finalButtonGraceMs = 2000
|
|
65
|
+
} = {}
|
|
66
|
+
) => {
|
|
67
|
+
const t0 = now();
|
|
68
|
+
const preUser = baseline?.user ?? countUser();
|
|
69
|
+
const preAsst = baseline?.asst ?? countAssistant();
|
|
70
|
+
|
|
71
|
+
if (countUser() < preUser + 1) {
|
|
72
|
+
while (now() - t0 < timeoutMs) {
|
|
73
|
+
if (countUser() >= preUser + 1) break;
|
|
74
|
+
await sleep(60);
|
|
75
|
+
}
|
|
76
|
+
if (countUser() < preUser + 1) {
|
|
77
|
+
return { ok: false, reason: 'userMessageNotCommitted', ms: now() - t0 };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (countAssistant() < preAsst + 1) {
|
|
82
|
+
while (now() - t0 < timeoutMs) {
|
|
83
|
+
if (countAssistant() >= preAsst + 1) break;
|
|
84
|
+
await sleep(60);
|
|
85
|
+
}
|
|
86
|
+
if (countAssistant() < preAsst + 1) {
|
|
87
|
+
return { ok: false, reason: 'assistantNeverStarted', ms: now() - t0 };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let lastLen = -1;
|
|
92
|
+
let lastChange = now();
|
|
93
|
+
let confirmStart = null;
|
|
94
|
+
|
|
95
|
+
while (now() - t0 < timeoutMs) {
|
|
96
|
+
const node = lastAssistantNode();
|
|
97
|
+
const txt = pickText(node);
|
|
98
|
+
const len = txt.length;
|
|
99
|
+
|
|
100
|
+
if (len !== lastLen) {
|
|
101
|
+
lastLen = len;
|
|
102
|
+
lastChange = now();
|
|
103
|
+
confirmStart = null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const idle = now() - lastChange;
|
|
107
|
+
|
|
108
|
+
if (idle >= idlePrimaryMs && inputEnabled()) {
|
|
109
|
+
if (idle >= idleConfirmMs) {
|
|
110
|
+
const turn = lastAssistantTurn();
|
|
111
|
+
if (hasTurnActions(turn)) {
|
|
112
|
+
return { ok: true, text: txt, ms: now() - t0 };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (confirmStart == null) confirmStart = now();
|
|
116
|
+
if (now() - confirmStart >= finalButtonGraceMs) {
|
|
117
|
+
return { ok: true, text: txt, ms: now() - t0, note: 'noButtonsConfirm' };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
await sleep(100);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return { ok: false, reason: 'timeout', ms: now() - t0 };
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const sendMessage = async (message) => {
|
|
129
|
+
const baseline = { user: countUser(), asst: countAssistant() };
|
|
130
|
+
|
|
131
|
+
const textbox =
|
|
132
|
+
document.querySelector('[role="textbox"][contenteditable="true"]') ||
|
|
133
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
134
|
+
|
|
135
|
+
if (!textbox) {
|
|
136
|
+
return { ok: false, reason: 'noTextbox' };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
textbox.focus();
|
|
140
|
+
textbox.innerText = message;
|
|
141
|
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
|
142
|
+
|
|
143
|
+
await sleep(100);
|
|
144
|
+
const sendBtn = document.querySelector('[data-testid="send-button"]') ||
|
|
145
|
+
qAll('button').find(b =>
|
|
146
|
+
b.getAttribute('aria-label')?.includes('Send') ||
|
|
147
|
+
b.innerText?.includes('Send')
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (!sendBtn) {
|
|
151
|
+
return { ok: false, reason: 'noSendButton' };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
sendBtn.click();
|
|
155
|
+
return await waitForResponseWithBaseline(baseline);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Install adapter
|
|
159
|
+
if (!window.ChatBridge) {
|
|
160
|
+
window.ChatBridge = {};
|
|
161
|
+
}
|
|
162
|
+
window.ChatBridge.version = ADAPTER_VERSION;
|
|
163
|
+
window.ChatBridge.sendMessage = sendMessage;
|
|
164
|
+
|
|
165
|
+
// Send test message and return result
|
|
166
|
+
return await sendMessage('[CC] Test 1/5: Verify v1.0.2 adapter - reply TEST-OK-1');
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// Test script for v1.0.2 ChatGPT adapter
|
|
2
|
+
// This injects the new hybrid approach and sends a test message
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
const ADAPTER_VERSION = '1.0.2';
|
|
6
|
+
|
|
7
|
+
// === Helper functions ===
|
|
8
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
const now = () => performance.now();
|
|
10
|
+
const qAll = (sel, root = document) => Array.from(root.querySelectorAll(sel));
|
|
11
|
+
|
|
12
|
+
const countUser = () =>
|
|
13
|
+
qAll('[data-message-author-role="user"],[data-author-role="user"]').length;
|
|
14
|
+
|
|
15
|
+
const countAssistant = () =>
|
|
16
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').length;
|
|
17
|
+
|
|
18
|
+
const lastAssistantNode = () =>
|
|
19
|
+
qAll('[data-message-author-role="assistant"],[data-author-role="assistant"]').at(-1) || null;
|
|
20
|
+
|
|
21
|
+
const inputEnabled = () => {
|
|
22
|
+
const tb =
|
|
23
|
+
document.querySelector('[role="textbox"][contenteditable="true"]') ||
|
|
24
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
25
|
+
return !!tb && tb.getAttribute('aria-disabled') !== 'true' && !tb.closest('[aria-disabled="true"]');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const pickText = (node) => {
|
|
29
|
+
const n = node || lastAssistantNode();
|
|
30
|
+
if (!n) return '';
|
|
31
|
+
const c = n.querySelector('.markdown, article') || n;
|
|
32
|
+
const t = c.innerText ?? c.textContent ?? '';
|
|
33
|
+
return (t || '').trim();
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const lastAssistantTurn = () => {
|
|
37
|
+
const turns = qAll('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]');
|
|
38
|
+
for (let i = turns.length - 1; i >= 0; i--) {
|
|
39
|
+
if (turns[i].querySelector('[data-message-author-role="assistant"], [data-author-role="assistant"]')) {
|
|
40
|
+
return turns[i];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const content = lastAssistantNode();
|
|
44
|
+
return (
|
|
45
|
+
content?.closest('[data-testid^="conversation-turn"], [data-testid*="conversation-turn"]') ||
|
|
46
|
+
content ||
|
|
47
|
+
null
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const hasTurnActions = (turn) => {
|
|
52
|
+
if (!turn) return false;
|
|
53
|
+
const toolbar =
|
|
54
|
+
turn.querySelector('[data-testid*="turn-actions"]') ||
|
|
55
|
+
turn.querySelector('[role="toolbar"]') ||
|
|
56
|
+
null;
|
|
57
|
+
if (!toolbar) return false;
|
|
58
|
+
const btns = toolbar.querySelectorAll(
|
|
59
|
+
'button[aria-label*="Regenerate"],' +
|
|
60
|
+
'button[aria-label*="Copy"],' +
|
|
61
|
+
'button[aria-label*="Share"],' +
|
|
62
|
+
'button[aria-label*="Like"],' +
|
|
63
|
+
'button[aria-label*="Dislike"],' +
|
|
64
|
+
'[data-testid*="thumb"]'
|
|
65
|
+
);
|
|
66
|
+
return btns.length > 0;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// === v1.0.2 waitForResponseWithBaseline ===
|
|
70
|
+
const waitForResponseWithBaseline = async (
|
|
71
|
+
baseline,
|
|
72
|
+
{
|
|
73
|
+
timeoutMs = 120000,
|
|
74
|
+
idlePrimaryMs = 1200, // normal token-stop idle
|
|
75
|
+
idleConfirmMs = 3300, // thinking-lull idle before checking buttons
|
|
76
|
+
finalButtonGraceMs = 2000 // how long to keep trying buttons before accepting stable text
|
|
77
|
+
} = {}
|
|
78
|
+
) => {
|
|
79
|
+
// ----- Conversation guards -----
|
|
80
|
+
const t0 = now();
|
|
81
|
+
const preUser = baseline?.user ?? countUser();
|
|
82
|
+
const preAsst = baseline?.asst ?? countAssistant();
|
|
83
|
+
|
|
84
|
+
// Require: user bubble committed (+1), tolerate if already present
|
|
85
|
+
if (countUser() < preUser + 1) {
|
|
86
|
+
while (now() - t0 < timeoutMs) {
|
|
87
|
+
if (countUser() >= preUser + 1) break;
|
|
88
|
+
await sleep(60);
|
|
89
|
+
}
|
|
90
|
+
if (countUser() < preUser + 1) {
|
|
91
|
+
return { ok: false, reason: 'userMessageNotCommitted', ms: now() - t0 };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Require: assistant bubble appeared (+1)
|
|
96
|
+
if (countAssistant() < preAsst + 1) {
|
|
97
|
+
while (now() - t0 < timeoutMs) {
|
|
98
|
+
if (countAssistant() >= preAsst + 1) break;
|
|
99
|
+
await sleep(60);
|
|
100
|
+
}
|
|
101
|
+
if (countAssistant() < preAsst + 1) {
|
|
102
|
+
return { ok: false, reason: 'assistantNeverStarted', ms: now() - t0 };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ----- Hybrid completion loop -----
|
|
107
|
+
let lastLen = -1;
|
|
108
|
+
let lastChange = now();
|
|
109
|
+
let confirmStart = null;
|
|
110
|
+
|
|
111
|
+
while (now() - t0 < timeoutMs) {
|
|
112
|
+
const node = lastAssistantNode();
|
|
113
|
+
const txt = pickText(node);
|
|
114
|
+
const len = txt.length;
|
|
115
|
+
|
|
116
|
+
if (len !== lastLen) {
|
|
117
|
+
lastLen = len;
|
|
118
|
+
lastChange = now();
|
|
119
|
+
confirmStart = null; // reset confirmation window on new tokens/layout changes
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const idle = now() - lastChange;
|
|
123
|
+
|
|
124
|
+
// Primary: text-length stability
|
|
125
|
+
if (idle >= idlePrimaryMs && inputEnabled()) {
|
|
126
|
+
// After a longer lull, attempt toolbar confirmation
|
|
127
|
+
if (idle >= idleConfirmMs) {
|
|
128
|
+
const turn = lastAssistantTurn();
|
|
129
|
+
if (hasTurnActions(turn)) {
|
|
130
|
+
return { ok: true, text: txt, ms: now() - t0 };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Start/continue a brief grace window for toolbar to show; then accept stable text
|
|
134
|
+
if (confirmStart == null) confirmStart = now();
|
|
135
|
+
if (now() - confirmStart >= finalButtonGraceMs) {
|
|
136
|
+
return { ok: true, text: txt, ms: now() - t0, note: 'noButtonsConfirm' };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// Else: keep accumulating idle time
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await sleep(100); // poll cadence
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return { ok: false, reason: 'timeout', ms: now() - t0 };
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// === sendMessage function ===
|
|
149
|
+
const sendMessage = async (message) => {
|
|
150
|
+
const baseline = { user: countUser(), asst: countAssistant() };
|
|
151
|
+
|
|
152
|
+
const textbox =
|
|
153
|
+
document.querySelector('[role="textbox"][contenteditable="true"]') ||
|
|
154
|
+
qAll('[contenteditable="true"]').at(-1);
|
|
155
|
+
|
|
156
|
+
if (!textbox) {
|
|
157
|
+
return { ok: false, reason: 'noTextbox' };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Set message
|
|
161
|
+
textbox.focus();
|
|
162
|
+
textbox.innerText = message;
|
|
163
|
+
|
|
164
|
+
// Trigger input event
|
|
165
|
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
|
166
|
+
|
|
167
|
+
// Find and click send button
|
|
168
|
+
await sleep(100);
|
|
169
|
+
const sendBtn = document.querySelector('[data-testid="send-button"]') ||
|
|
170
|
+
qAll('button').find(b =>
|
|
171
|
+
b.getAttribute('aria-label')?.includes('Send') ||
|
|
172
|
+
b.innerText?.includes('Send')
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
if (!sendBtn) {
|
|
176
|
+
return { ok: false, reason: 'noSendButton' };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
sendBtn.click();
|
|
180
|
+
|
|
181
|
+
// Wait for response
|
|
182
|
+
return await waitForResponseWithBaseline(baseline);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// === Install adapter in window ===
|
|
186
|
+
if (!window.ChatBridge) {
|
|
187
|
+
window.ChatBridge = {};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
window.ChatBridge.version = ADAPTER_VERSION;
|
|
191
|
+
window.ChatBridge.sendMessage = sendMessage;
|
|
192
|
+
window.ChatBridge.waitForResponseWithBaseline = waitForResponseWithBaseline;
|
|
193
|
+
window.ChatBridge.getBaseline = () => ({
|
|
194
|
+
user: countUser(),
|
|
195
|
+
asst: countAssistant()
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// === Send test message ===
|
|
199
|
+
const testMessage = '[CC] Test 1/5: Hybrid approach - reply HYBRID-OK-1';
|
|
200
|
+
const result = await sendMessage(testMessage);
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
adapterVersion: ADAPTER_VERSION,
|
|
204
|
+
testMessage,
|
|
205
|
+
result
|
|
206
|
+
};
|
|
207
|
+
})();
|