go/pkg/webview/actions.go

548 lines
14 KiB
Go
Raw Permalink Normal View History

feat: BugSETI app, WebSocket hub, browser automation, and MCP tools (#336) * feat: add security logging and fix framework regressions This commit implements comprehensive security event logging and resolves critical regressions in the core framework. Security Logging: - Enhanced `pkg/log` with a `Security` level and helper. - Added `log.Username()` to consistently identify the executing user. - Instrumented GitHub CLI auth, Agentic configuration, filesystem sandbox, MCP handlers, and MCP TCP transport with security logs. - Added `SecurityStyle` to the CLI for consistent visual representation of security events. UniFi Security (CodeQL): - Refactored `pkg/unifi` to remove hardcoded `InsecureSkipVerify`, resolving a high-severity alert. - Added a `--verify-tls` flag and configuration option to control TLS verification. - Updated command handlers to support the new verification parameter. Framework Fixes: - Restored original signatures for `MustServiceFor`, `Config()`, and `Display()` in `pkg/framework/core`, which had been corrupted during a merge. - Fixed `pkg/framework/framework.go` and `pkg/framework/core/runtime_pkg.go` to match the restored signatures. - These fixes resolve project-wide compilation errors caused by the signature mismatches. I encountered significant blockers due to a corrupted state of the `dev` branch after a merge, which introduced breaking changes in the core framework's DI system. I had to manually reconcile these signatures with the expected usage across the codebase to restore build stability. * feat(mcp): add RAG tools (query, ingest, collections) Add vector database tools to the MCP server for RAG operations: - rag_query: Search for relevant documentation using semantic similarity - rag_ingest: Ingest files or directories into the vector database - rag_collections: List available collections Uses existing internal/cmd/rag exports (QueryDocs, IngestDirectory, IngestFile) and pkg/rag for Qdrant client access. Default collection is "hostuk-docs" with topK=5 for queries. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(mcp): add metrics tools (record, query) Add MCP tools for recording and querying AI/security metrics events. The metrics_record tool writes events to daily JSONL files, and the metrics_query tool provides aggregated statistics by type, repo, and agent. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat: add 'core mcp serve' command Add CLI command to start the MCP server for AI tool integration. - Create internal/cmd/mcpcmd package with serve subcommand - Support --workspace flag for directory restriction - Handle SIGINT/SIGTERM for clean shutdown - Register in full.go build variant Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(ws): add WebSocket hub package for real-time streaming Add pkg/ws package implementing a hub pattern for WebSocket connections: - Hub manages client connections, broadcasts, and channel subscriptions - Client struct represents connected WebSocket clients - Message types: process_output, process_status, event, error, ping/pong - Channel-based subscription system (subscribe/unsubscribe) - SendProcessOutput and SendProcessStatus for process streaming integration - Full test coverage including concurrency tests Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(mcp): add process management and WebSocket MCP tools Add MCP tools for process management: - process_start: Start a new external process - process_stop: Gracefully stop a running process - process_kill: Force kill a process - process_list: List all managed processes - process_output: Get captured process output - process_input: Send input to process stdin Add MCP tools for WebSocket: - ws_start: Start WebSocket server for real-time streaming - ws_info: Get hub statistics (clients, channels) Update Service struct with optional process.Service and ws.Hub fields, new WithProcessService and WithWSHub options, getter methods, and Shutdown method for cleanup. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(webview): add browser automation package via Chrome DevTools Protocol Add pkg/webview package for browser automation: - webview.go: Main interface with Connect, Navigate, Click, Type, QuerySelector, Screenshot, Evaluate - cdp.go: Chrome DevTools Protocol WebSocket client implementation - actions.go: DOM action types (Click, Type, Hover, Scroll, etc.) and ActionSequence builder - console.go: Console message capture and filtering with ConsoleWatcher and ExceptionWatcher - angular.go: Angular-specific helpers for router navigation, component access, and Zone.js stability Add MCP tools for webview: - webview_connect/disconnect: Connection management - webview_navigate: Page navigation - webview_click/type/query/wait: DOM interaction - webview_console: Console output capture - webview_eval: JavaScript execution - webview_screenshot: Screenshot capture Add documentation: - docs/mcp/angular-testing.md: Guide for Angular application testing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: document new packages and BugSETI application - Update CLAUDE.md with documentation for: - pkg/ws (WebSocket hub for real-time streaming) - pkg/webview (Browser automation via CDP) - pkg/mcp (MCP server tools: process, ws, webview) - BugSETI application overview - Add comprehensive README for BugSETI with: - Installation and configuration guide - Usage workflow documentation - Architecture overview - Contributing guidelines Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(bugseti): add BugSETI system tray app with auto-update BugSETI - Distributed Bug Fixing like SETI@home but for code Features: - System tray app with Wails v3 - GitHub issue fetching with label filters - Issue queue with priority management - AI context seeding via seed-agent-developer skill - Automated PR submission flow - Stats tracking and leaderboard - Cross-platform notifications - Self-updating with stable/beta/nightly channels Includes: - cmd/bugseti: Main application with Angular frontend - internal/bugseti: Core services (fetcher, queue, seeder, submit, config, stats, notify) - internal/bugseti/updater: Auto-update system (checker, downloader, installer) - .github/workflows/bugseti-release.yml: CI/CD for all platforms Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: resolve import cycle and code duplication - Remove pkg/log import from pkg/io/local to break import cycle (pkg/log/rotation.go imports pkg/io, creating circular dependency) - Use stderr logging for security events in sandbox escape detection - Remove unused sync/atomic import from core.go - Fix duplicate LogSecurity function declarations in cli/log.go - Update workspace/service.go Crypt() call to match interface Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: update tests for new function signatures and format code - Update core_test.go: Config(), Display() now panic instead of returning error - Update runtime_pkg_test.go: sr.Config() now panics instead of returning error - Update MustServiceFor tests to use assert.Panics - Format BugSETI, MCP tools, and webview packages with gofmt Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Snider <631881+Snider@users.noreply.github.com> Co-authored-by: Claude <developers@lethean.io> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 17:22:05 +00:00
package webview
import (
"context"
"fmt"
"time"
)
// Action represents a browser action that can be performed.
type Action interface {
Execute(ctx context.Context, wv *Webview) error
}
// ClickAction represents a click action.
type ClickAction struct {
Selector string
}
// Execute performs the click action.
func (a ClickAction) Execute(ctx context.Context, wv *Webview) error {
return wv.click(ctx, a.Selector)
}
// TypeAction represents a typing action.
type TypeAction struct {
Selector string
Text string
}
// Execute performs the type action.
func (a TypeAction) Execute(ctx context.Context, wv *Webview) error {
return wv.typeText(ctx, a.Selector, a.Text)
}
// NavigateAction represents a navigation action.
type NavigateAction struct {
URL string
}
// Execute performs the navigate action.
func (a NavigateAction) Execute(ctx context.Context, wv *Webview) error {
_, err := wv.client.Call(ctx, "Page.navigate", map[string]any{
"url": a.URL,
})
if err != nil {
return fmt.Errorf("failed to navigate: %w", err)
}
return wv.waitForLoad(ctx)
}
// WaitAction represents a wait action.
type WaitAction struct {
Duration time.Duration
}
// Execute performs the wait action.
func (a WaitAction) Execute(ctx context.Context, wv *Webview) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(a.Duration):
return nil
}
}
// WaitForSelectorAction represents waiting for a selector.
type WaitForSelectorAction struct {
Selector string
}
// Execute waits for the selector to appear.
func (a WaitForSelectorAction) Execute(ctx context.Context, wv *Webview) error {
return wv.waitForSelector(ctx, a.Selector)
}
// ScrollAction represents a scroll action.
type ScrollAction struct {
X int
Y int
}
// Execute performs the scroll action.
func (a ScrollAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("window.scrollTo(%d, %d)", a.X, a.Y)
_, err := wv.evaluate(ctx, script)
return err
}
// ScrollIntoViewAction scrolls an element into view.
type ScrollIntoViewAction struct {
Selector string
}
// Execute scrolls the element into view.
func (a ScrollIntoViewAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("document.querySelector(%q)?.scrollIntoView({behavior: 'smooth', block: 'center'})", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// FocusAction focuses an element.
type FocusAction struct {
Selector string
}
// Execute focuses the element.
func (a FocusAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("document.querySelector(%q)?.focus()", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// BlurAction removes focus from an element.
type BlurAction struct {
Selector string
}
// Execute removes focus from the element.
func (a BlurAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("document.querySelector(%q)?.blur()", a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// ClearAction clears the value of an input element.
type ClearAction struct {
Selector string
}
// Execute clears the input value.
func (a ClearAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = '';
el.dispatchEvent(new Event('input', {bubbles: true}));
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
// SelectAction selects an option in a select element.
type SelectAction struct {
Selector string
Value string
}
// Execute selects the option.
func (a SelectAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = %q;
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// CheckAction checks or unchecks a checkbox.
type CheckAction struct {
Selector string
Checked bool
}
// Execute checks/unchecks the checkbox.
func (a CheckAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el && el.checked !== %t) {
el.click();
}
`, a.Selector, a.Checked)
_, err := wv.evaluate(ctx, script)
return err
}
// HoverAction hovers over an element.
type HoverAction struct {
Selector string
}
// Execute hovers over the element.
func (a HoverAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
return fmt.Errorf("element has no bounding box")
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseMoved",
"x": x,
"y": y,
})
return err
}
// DoubleClickAction double-clicks an element.
type DoubleClickAction struct {
Selector string
}
// Execute double-clicks the element.
func (a DoubleClickAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
// Fallback to JavaScript
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el) {
const event = new MouseEvent('dblclick', {bubbles: true, cancelable: true, view: window});
el.dispatchEvent(event);
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
// Double click sequence
for i := 0; i < 2; i++ {
for _, eventType := range []string{"mousePressed", "mouseReleased"} {
_, err := wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": eventType,
"x": x,
"y": y,
"button": "left",
"clickCount": i + 1,
})
if err != nil {
return err
}
}
}
return nil
}
// RightClickAction right-clicks an element.
type RightClickAction struct {
Selector string
}
// Execute right-clicks the element.
func (a RightClickAction) Execute(ctx context.Context, wv *Webview) error {
elem, err := wv.querySelector(ctx, a.Selector)
if err != nil {
return err
}
if elem.BoundingBox == nil {
// Fallback to JavaScript
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el) {
const event = new MouseEvent('contextmenu', {bubbles: true, cancelable: true, view: window});
el.dispatchEvent(event);
}
`, a.Selector)
_, err := wv.evaluate(ctx, script)
return err
}
x := elem.BoundingBox.X + elem.BoundingBox.Width/2
y := elem.BoundingBox.Y + elem.BoundingBox.Height/2
for _, eventType := range []string{"mousePressed", "mouseReleased"} {
_, err := wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": eventType,
"x": x,
"y": y,
"button": "right",
"clickCount": 1,
})
if err != nil {
return err
}
}
return nil
}
// PressKeyAction presses a key.
type PressKeyAction struct {
Key string // e.g., "Enter", "Tab", "Escape"
}
// Execute presses the key.
func (a PressKeyAction) Execute(ctx context.Context, wv *Webview) error {
// Map common key names to CDP key codes
keyMap := map[string]struct {
code string
keyCode int
text string
unmodified string
}{
"Enter": {"Enter", 13, "\r", "\r"},
"Tab": {"Tab", 9, "", ""},
"Escape": {"Escape", 27, "", ""},
"Backspace": {"Backspace", 8, "", ""},
"Delete": {"Delete", 46, "", ""},
"ArrowUp": {"ArrowUp", 38, "", ""},
"ArrowDown": {"ArrowDown", 40, "", ""},
"ArrowLeft": {"ArrowLeft", 37, "", ""},
"ArrowRight": {"ArrowRight", 39, "", ""},
"Home": {"Home", 36, "", ""},
"End": {"End", 35, "", ""},
"PageUp": {"PageUp", 33, "", ""},
"PageDown": {"PageDown", 34, "", ""},
}
keyInfo, ok := keyMap[a.Key]
if !ok {
// For simple characters, just send key events
_, err := wv.client.Call(ctx, "Input.dispatchKeyEvent", map[string]any{
"type": "keyDown",
"text": a.Key,
})
if err != nil {
return err
}
_, err = wv.client.Call(ctx, "Input.dispatchKeyEvent", map[string]any{
"type": "keyUp",
})
return err
}
params := map[string]any{
"type": "keyDown",
"code": keyInfo.code,
"key": a.Key,
"windowsVirtualKeyCode": keyInfo.keyCode,
"nativeVirtualKeyCode": keyInfo.keyCode,
}
if keyInfo.text != "" {
params["text"] = keyInfo.text
params["unmodifiedText"] = keyInfo.unmodified
}
_, err := wv.client.Call(ctx, "Input.dispatchKeyEvent", params)
if err != nil {
return err
}
params["type"] = "keyUp"
delete(params, "text")
delete(params, "unmodifiedText")
_, err = wv.client.Call(ctx, "Input.dispatchKeyEvent", params)
return err
}
// SetAttributeAction sets an attribute on an element.
type SetAttributeAction struct {
Selector string
Attribute string
Value string
}
// Execute sets the attribute.
func (a SetAttributeAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("document.querySelector(%q)?.setAttribute(%q, %q)", a.Selector, a.Attribute, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// RemoveAttributeAction removes an attribute from an element.
type RemoveAttributeAction struct {
Selector string
Attribute string
}
// Execute removes the attribute.
func (a RemoveAttributeAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf("document.querySelector(%q)?.removeAttribute(%q)", a.Selector, a.Attribute)
_, err := wv.evaluate(ctx, script)
return err
}
// SetValueAction sets the value of an input element.
type SetValueAction struct {
Selector string
Value string
}
// Execute sets the value.
func (a SetValueAction) Execute(ctx context.Context, wv *Webview) error {
script := fmt.Sprintf(`
const el = document.querySelector(%q);
if (el) {
el.value = %q;
el.dispatchEvent(new Event('input', {bubbles: true}));
el.dispatchEvent(new Event('change', {bubbles: true}));
}
`, a.Selector, a.Value)
_, err := wv.evaluate(ctx, script)
return err
}
// ActionSequence represents a sequence of actions to execute.
type ActionSequence struct {
actions []Action
}
// NewActionSequence creates a new action sequence.
func NewActionSequence() *ActionSequence {
return &ActionSequence{
actions: make([]Action, 0),
}
}
// Add adds an action to the sequence.
func (s *ActionSequence) Add(action Action) *ActionSequence {
s.actions = append(s.actions, action)
return s
}
// Click adds a click action.
func (s *ActionSequence) Click(selector string) *ActionSequence {
return s.Add(ClickAction{Selector: selector})
}
// Type adds a type action.
func (s *ActionSequence) Type(selector, text string) *ActionSequence {
return s.Add(TypeAction{Selector: selector, Text: text})
}
// Navigate adds a navigate action.
func (s *ActionSequence) Navigate(url string) *ActionSequence {
return s.Add(NavigateAction{URL: url})
}
// Wait adds a wait action.
func (s *ActionSequence) Wait(d time.Duration) *ActionSequence {
return s.Add(WaitAction{Duration: d})
}
// WaitForSelector adds a wait for selector action.
func (s *ActionSequence) WaitForSelector(selector string) *ActionSequence {
return s.Add(WaitForSelectorAction{Selector: selector})
}
// Execute executes all actions in the sequence.
func (s *ActionSequence) Execute(ctx context.Context, wv *Webview) error {
for i, action := range s.actions {
if err := action.Execute(ctx, wv); err != nil {
return fmt.Errorf("action %d failed: %w", i, err)
}
}
return nil
}
// UploadFile uploads a file to a file input element.
func (wv *Webview) UploadFile(selector string, filePaths []string) error {
ctx, cancel := context.WithTimeout(wv.ctx, wv.timeout)
defer cancel()
// Get the element's node ID
elem, err := wv.querySelector(ctx, selector)
if err != nil {
return err
}
// Use DOM.setFileInputFiles to set the files
_, err = wv.client.Call(ctx, "DOM.setFileInputFiles", map[string]any{
"nodeId": elem.NodeID,
"files": filePaths,
})
return err
}
// DragAndDrop performs a drag and drop operation.
func (wv *Webview) DragAndDrop(sourceSelector, targetSelector string) error {
ctx, cancel := context.WithTimeout(wv.ctx, wv.timeout)
defer cancel()
// Get source and target elements
source, err := wv.querySelector(ctx, sourceSelector)
if err != nil {
return fmt.Errorf("source element not found: %w", err)
}
if source.BoundingBox == nil {
return fmt.Errorf("source element has no bounding box")
}
target, err := wv.querySelector(ctx, targetSelector)
if err != nil {
return fmt.Errorf("target element not found: %w", err)
}
if target.BoundingBox == nil {
return fmt.Errorf("target element has no bounding box")
}
// Calculate center points
sourceX := source.BoundingBox.X + source.BoundingBox.Width/2
sourceY := source.BoundingBox.Y + source.BoundingBox.Height/2
targetX := target.BoundingBox.X + target.BoundingBox.Width/2
targetY := target.BoundingBox.Y + target.BoundingBox.Height/2
// Mouse down on source
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mousePressed",
"x": sourceX,
"y": sourceY,
"button": "left",
"clickCount": 1,
})
if err != nil {
return err
}
// Move to target
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseMoved",
"x": targetX,
"y": targetY,
"button": "left",
})
if err != nil {
return err
}
// Mouse up on target
_, err = wv.client.Call(ctx, "Input.dispatchMouseEvent", map[string]any{
"type": "mouseReleased",
"x": targetX,
"y": targetY,
"button": "left",
"clickCount": 1,
})
return err
}