Files
sovereign_browser/src/agent_snapshot.h
T

94 lines
3.4 KiB
C

/*
* agent_snapshot.h — accessibility tree snapshot for agent tools
*
* Injects a JavaScript script into the active tab's webview that walks
* the DOM, assigns sequential refs (e1, e2, ...) to interactive elements,
* and returns a text representation of the accessibility tree.
*/
#ifndef AGENT_SNAPSHOT_H
#define AGENT_SNAPSHOT_H
#include <webkit2/webkit2.h>
#include <libsoup/soup.h>
#include "cjson/cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* The JS script injected for snapshot. It walks the DOM, assigns refs,
* stores the ref map in window.__agentRefs, and returns a JSON string
* with the tree text and ref metadata.
*/
extern const char *AGENT_SNAPSHOT_JS;
/*
* Take a snapshot of the given webview. This injects the snapshot JS
* and returns the result as a cJSON object:
* {"snapshot": "- heading ... [ref=e1]\n- link ... [ref=e2]",
* "refs": {"e1": {"role":"heading","name":"..."}, ...}}
*
* Returns NULL on failure. Caller frees the result.
*
* interactive: if TRUE, only include interactive elements
* compact: if TRUE, remove empty structural elements
*/
cJSON *agent_snapshot_take(WebKitWebView *webview,
gboolean interactive,
gboolean compact);
/*
* Async snapshot — starts JS evaluation and sends the response through
* the WebSocket connection when the JS completes. Returns TRUE if the
* async evaluation was started (response will be sent async), FALSE if
* it failed immediately (caller should send an error response).
*
* conn: WebSocket connection to send the response through
* request_id: the JSON request id to include in the response
*/
gboolean agent_snapshot_take_async(WebKitWebView *webview,
gboolean interactive,
gboolean compact,
SoupWebsocketConnection *conn,
int request_id);
/*
* Async JS evaluation — evaluates a script and sends the result as a
* tool response through the WebSocket connection when the JS completes.
* Returns TRUE if started async, FALSE on immediate failure.
*
* conn: WebSocket connection to send the response through
* request_id: the JSON request id to include in the response
* tool_name: the tool name (for logging)
* success_handler: optional callback to transform the JS result into
* a cJSON response before sending. If NULL, the raw
* JS result string is returned in data.result.
*/
typedef cJSON *(*js_result_handler_t)(const char *js_result);
gboolean agent_js_eval_async(WebKitWebView *webview,
const char *script,
SoupWebsocketConnection *conn,
int request_id,
const char *tool_name,
js_result_handler_t handler);
/*
* Execute JavaScript in the webview and wait for the result.
* This is a synchronous wrapper around the async evaluate_javascript API.
* Returns the result string (caller must free) or NULL on failure/timeout.
*
* script: the JavaScript to execute (must return a string)
* timeout_ms: how long to wait for the result
*/
char *agent_js_eval_sync(WebKitWebView *webview, const char *script,
int timeout_ms);
#ifdef __cplusplus
}
#endif
#endif /* AGENT_SNAPSHOT_H */