Files
Artemis-Plugins/plugins/cua-driver/foreground.go

56 lines
2.6 KiB
Go

package main
// Use Cua's existing persistent activation before explicit foreground input.
// Cua then snapshots the target itself as its "previous foreground", so its
// asynchronous restore cannot race ARTEMIS's separately requested observation.
// Read-only calls and background delivery never activate or restore a window.
func callWithForeground(call func(string, obj) (obj, error), name string, args obj, current func() int64) (obj, error) {
if name != "bring_to_front" && args["delivery_mode"] != "foreground" {
return call(name, args)
}
target := obj{"pid": args["pid"], "window_id": args["window_id"]}
focus, err := call("bring_to_front", target)
if err != nil {
// Only activation was attempted. Do not send (or replay) the business
// action when the activation result is unknown.
return failure("Foreground activation could not be confirmed; requested input was not sent: "+err.Error(), "not_started", "possible", "foreground_unavailable"), nil
}
activation := object(focus["structuredContent"])
actual := current()
activation["verified_foreground_window_id"] = actual
activation["policy"] = "keep_target_foreground"
if focus["isError"] == true || activation["landed_on_target"] != true || actual != num(args["window_id"]) {
r := failure("Target window did not retain foreground; requested input was not sent. Inspect the window before retrying.", "not_started", "possible", "foreground_unavailable")
object(r["structuredContent"])["foreground_activation"] = activation
return r, nil
}
if name == "bring_to_front" {
activation["artemis"] = receipt("completed", "confirmed", "")
focus["structuredContent"] = activation
return focus, nil
}
r, err := call(name, args)
if err == nil {
s := object(r["structuredContent"])
s["foreground_activation"] = activation
r["structuredContent"] = s
}
return r, err
}
func annotateCaptureFocus(r obj, target, before, after int64) {
s := object(r["structuredContent"])
s["capture_focus"] = obj{
"target_window_id": target,
"foreground_window_id_before": before,
"foreground_window_id_after": after,
"target_foreground_before": before == target,
"target_foreground_after": after == target,
"changed_during_capture": before != after,
}
r["structuredContent"] = s
if before != target || after != target {
r["content"] = append(items(r["content"]), textBlock("Capture was not continuously foreground. If the page body is blank or missing from the tree, use bring_to_front for this same pid/window_id, then observe again; do not infer a website failure or repeat a business action."))
}
}