Repository navigation
Expand file tree
/
Copy pathpet_real_mouse.go
More file actions
277 lines (261 loc) · 8.7 KB
/
Copy pathpet_real_mouse.go
File metadata and controls
277 lines (261 loc) · 8.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"context"
"errors"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
const petV2SpriteDetectionScript = `
const isV2Sprite = async (mascot) => {
if (!mascot) return false;
if (Array.from(mascot.querySelectorAll("img")).some((image) =>
image.naturalWidth === 1536 && image.naturalHeight === 2288
)) return true;
for (const element of [mascot, ...mascot.querySelectorAll("*")]) {
const background = getComputedStyle(element).backgroundImage || "";
const match = background.match(/url\(["']?([^"')]+)/i);
if (!match) continue;
const source = match[1];
const cacheKey = "__codexPlusPetV2SpriteProbe";
let probe = window[cacheKey];
if (!probe || probe.source !== source) {
probe = { source, valid: false, pending: true };
probe.promise = (async () => {
try {
const image = new Image();
image.src = source;
await image.decode();
return image.naturalWidth === 1536 && image.naturalHeight === 2288;
} catch {
return false;
}
})().then((valid) => {
probe.valid = valid;
probe.pending = false;
return valid;
});
window[cacheKey] = probe;
}
const wasPending = probe.pending;
const valid = wasPending ? await probe.promise : probe.valid;
if (wasPending) {
const currentBackground = getComputedStyle(element).backgroundImage || "";
const currentMatch = currentBackground.match(/url\(["']?([^"')]+)/i);
if (currentMatch?.[1] !== source) continue;
}
if (window[cacheKey] === probe && valid) return true;
}
return false;
};
`
var (
petOverlaySyncFailed atomic.Bool
petCursorDriverFailed atomic.Bool
)
func petRealMouseCapabilityProbeScript() string {
return `(async () => {
const mascot = document.querySelector('[data-avatar-mascot="true"]');
` + petV2SpriteDetectionScript + `
if (!await isV2Sprite(mascot)) return false;
const urls = [
...Array.from(document.scripts || []).map((script) => script.src),
...Array.from(document.querySelectorAll("link[href]") || []).map((link) => link.href),
...performance.getEntriesByType("resource").map((entry) => entry.name),
].filter((url) => url && url.includes("/assets/") && url.split("?")[0].endsWith(".js"));
let dispatcherUrl = urls.find((url) => url.includes("vscode-api-"));
if (!dispatcherUrl) {
for (const url of urls) {
try {
const source = await fetch(url).then((response) => response.ok ? response.text() : "");
const match = source.match(/["'](\.\/(?:assets\/)?vscode-api-[^"']+\.js)["']/);
if (match) {
dispatcherUrl = new URL(match[1], url).href;
break;
}
} catch {
}
}
}
if (!dispatcherUrl) return false;
try {
const module = await import(dispatcherUrl);
return Object.values(module || {}).some((value) => value
&& typeof value.dispatchHostMessage === "function"
&& typeof value.subscribe === "function");
} catch {
return false;
}
})()`
}
func petRealMouseUpdateScript(x, y int32) string {
return fmt.Sprintf(`(async () => {
const mascot = document.querySelector('[data-avatar-mascot="true"]');
%s
return await isV2Sprite(mascot)
&& window.__codexPlusPetRealMouseLook?.updateScreenPoint?.({ x: %d, y: %d }) === true;
})()`, petV2SpriteDetectionScript, x, y)
}
func petRealMouseStopScript() string {
return `window.__codexPlusPetRealMouseLook?.stop?.();`
}
func evaluateCDPTarget(ctx context.Context, websocketURL, script string, awaitPromise bool) (bool, error) {
conn, _, err := websocket.DefaultDialer.DialContext(ctx, websocketURL, nil)
if err != nil {
return false, err
}
defer conn.Close()
session := newCDPSession(conn, nil)
result, err := session.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(script, awaitPromise))
if err != nil {
return false, err
}
return cdpResultBool(result), nil
}
func petOverlaySupportsV2Cursor(target cdpTarget) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), cdpCommandTimeout)
defer cancel()
return evaluateCDPTarget(ctx, target.WebSocketDebuggerURL, petRealMouseCapabilityProbeScript(), true)
}
func (r *launcherRuntime) syncPetRealMouseOverlays() error {
targetCtx, cancel := context.WithTimeout(context.Background(), cdpConnectTimeout)
defer cancel()
targets, err := listCDPTargets(targetCtx, r.debugPort)
if err != nil {
return err
}
settings := r.runtimeSettingsSnapshot()
enabled := settings.Enhancements && settings.CodexAppPetRealMouseLook
for _, target := range targets {
if !isAvatarOverlayCDPPageTarget(target) {
continue
}
script := petRealMouseStopScript()
if enabled {
supported, probeErr := petOverlaySupportsV2Cursor(target)
if probeErr != nil {
return fmt.Errorf("probe pet overlay %s: %w", target.ID, probeErr)
}
if supported {
script = petRealMouseInjectScript
}
}
ctx, stop := context.WithTimeout(context.Background(), cdpCommandTimeout)
_, evalErr := evaluateCDPTarget(ctx, target.WebSocketDebuggerURL, script, false)
stop()
if evalErr != nil {
return fmt.Errorf("sync pet overlay %s: %w", target.ID, evalErr)
}
}
return nil
}
func (r *launcherRuntime) confirmedPetOverlayTargets() []cdpTarget {
ctx, cancel := context.WithTimeout(context.Background(), cdpConnectTimeout)
defer cancel()
targets, err := listCDPTargets(ctx, r.debugPort)
if err != nil {
return nil
}
confirmed := make([]cdpTarget, 0, len(targets))
for _, target := range targets {
if !isAvatarOverlayCDPPageTarget(target) {
continue
}
supported, probeErr := petOverlaySupportsV2Cursor(target)
if probeErr == nil && supported {
confirmed = append(confirmed, target)
}
}
return confirmed
}
func (r *launcherRuntime) petRealMouseCursorDriver() {
for {
settings := r.runtimeSettingsSnapshot()
if !settings.Enhancements || !settings.CodexAppPetRealMouseLook {
time.Sleep(500 * time.Millisecond)
continue
}
targets := r.confirmedPetOverlayTargets()
if len(targets) == 0 {
time.Sleep(5 * time.Second)
continue
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, len(targets))
for _, target := range targets {
target := target
go func() { done <- r.runPetRealMouseTargetDriver(ctx, target) }()
}
err := <-done
cancel()
if err != nil && !strings.Contains(err.Error(), "disabled") && !petCursorDriverFailed.Swap(true) {
appendDiagnosticLog("pet.real_mouse_cursor_driver_disconnected", map[string]any{"debug_port": r.debugPort, "error": err.Error()})
}
time.Sleep(5 * time.Second)
}
}
func (r *launcherRuntime) runPetRealMouseTargetDriver(parent context.Context, target cdpTarget) error {
ctx, cancel := context.WithTimeout(parent, cdpConnectTimeout)
conn, _, err := websocket.DefaultDialer.DialContext(ctx, target.WebSocketDebuggerURL, nil)
cancel()
if err != nil {
return err
}
defer conn.Close()
session := newCDPSession(conn, nil)
if err := sendPetRuntimeEvaluation(parent, session, petRealMouseInjectScript, false); err != nil {
return err
}
defer func() { _ = sendPetRuntimeEvaluation(context.Background(), session, petRealMouseStopScript(), false) }()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
settingsCheck := 0
for {
select {
case <-parent.Done():
return nil
case <-ticker.C:
if settingsCheck == 0 {
settings := r.runtimeSettingsSnapshot()
if !settings.Enhancements || !settings.CodexAppPetRealMouseLook {
return errors.New("pet real-mouse disabled")
}
settingsCheck = 10
}
settingsCheck--
x, y, cursorErr := windowsLogicalCursorPosition()
if cursorErr != nil {
return cursorErr
}
if err := sendPetRuntimeEvaluation(parent, session, petRealMouseUpdateScript(x, y), true); err != nil {
return err
}
petCursorDriverFailed.Store(false)
}
}
}
func sendPetRuntimeEvaluation(parent context.Context, session *cdpSession, script string, awaitPromise bool) error {
ctx, cancel := context.WithTimeout(parent, cdpCommandTimeout)
defer cancel()
result, err := session.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(script, awaitPromise))
if err != nil {
return err
}
if awaitPromise && !cdpResultBool(result) {
return errors.New("pet overlay rejected cursor update")
}
return nil
}
func (r *launcherRuntime) recordPetRealMouseOverlaySync(err error) {
if err == nil {
if petOverlaySyncFailed.Swap(false) {
appendDiagnosticLog("pet.real_mouse_overlay_sync_recovered", map[string]any{"debug_port": r.debugPort})
}
return
}
if !petOverlaySyncFailed.Swap(true) {
appendDiagnosticLog("pet.real_mouse_overlay_sync_failed", map[string]any{"debug_port": r.debugPort, "error": err.Error()})
}
}