2026-09-11 09:59:27 +00:00
|
|
|
package browserskill
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/hmac"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const internalPath = "/api/v1/local-browser/internal"
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
localRPCKey struct{}
|
|
|
|
|
clusterRequest struct {
|
|
|
|
|
Node string `json:"node"`
|
|
|
|
|
Scope Scope `json:"scope"`
|
|
|
|
|
Session string `json:"session"`
|
|
|
|
|
Operation string `json:"operation"`
|
|
|
|
|
Method string `json:"method"`
|
|
|
|
|
Params map[string]any `json:"params,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type clusterResponse struct {
|
feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193)
* feat(browser): enhance BrowserIcon component and update localization strings
- Updated BrowserIcon component to accept dynamic width and height props.
- Revised localization strings in English, Japanese, Korean, Russian, and Chinese for improved clarity and user guidance regarding browser connection and usage instructions.
- Enhanced BrowserConnectionSettings view with better structure and user feedback elements.
* Default BrowserSkill tasks to a separate window
* refactor(prompts): streamline agent system prompts and context templates
- Updated agent system prompts to enhance clarity and specificity in user interactions.
- Revised context templates to better separate metadata and user requests, improving overall structure.
- Adjusted fallback and intent prompts to ensure consistent language handling and response requirements.
- Introduced new localization strings for browser search instructions across multiple languages.
- Added tests for agent prompt serialization and hydration to ensure template integrity and functionality.
* chore: wrap prompt strings to satisfy golangci-lint
Keep the settings, separate-window, and prompt commits pushable together
without rewriting their history.
* fix(frontend): match agent editor save test to serialized payload
The prompt-inheritance save path still closes after update; the test
should assert that payload, not the unsanitized form object.
* feat(browser): implement browser connection management and related tests
- Introduced a new `browserConnection` store to manage the state of browser connections, including online status and device information.
- Updated the `Input-field` component to handle browser connection states, enabling/disabling features based on the connection status.
- Added tests for the `browserConnection` store to ensure correct behavior under various scenarios.
- Enhanced the `Input-field` and `BrowserConnectionSettings` components to provide user feedback based on the browser connection state.
- Adjusted related components and views to integrate the new browser connection logic, improving overall user experience.
* fix(frontend): update model-save assertion for serialized agent payload
The agent editor still persists the selected model ID via a spread of
formData; the source test should match that payload, not the raw form.
2026-09-11 13:28:58 +00:00
|
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
|
RPCError *RPCError `json:"rpc_error,omitempty"`
|
2026-09-11 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func signRPC(secret, timestamp string, body []byte) string {
|
|
|
|
|
h := hmac.New(sha256.New, []byte(secret))
|
|
|
|
|
h.Write([]byte(timestamp + "\n"))
|
|
|
|
|
h.Write(body)
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validInternalURL(raw string) bool {
|
|
|
|
|
u, e := url.Parse(raw)
|
|
|
|
|
return e == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != "" && u.User == nil &&
|
|
|
|
|
u.RawQuery == "" &&
|
|
|
|
|
u.Fragment == "" &&
|
|
|
|
|
(u.Path == "" || u.Path == "/")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateConfiguration checks storage, transport and replica routing requirements.
|
|
|
|
|
func (m *Manager) ValidateConfiguration() error {
|
|
|
|
|
if !m.Enabled() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if m.store == nil {
|
|
|
|
|
return errors.New("BrowserSkill requires persistent authorization storage")
|
|
|
|
|
}
|
|
|
|
|
if m.publicURL != "" {
|
|
|
|
|
if _, err := pairingEndpoint(m.publicURL); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if m.internalURL != "" && (!validInternalURL(m.internalURL) || len(m.clusterSecret) < 32) {
|
|
|
|
|
return errors.New(
|
|
|
|
|
"BrowserSkill multi-replica mode requires a valid internal URL and a shared cluster secret of " +
|
|
|
|
|
"at least 32 characters",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Manager) route(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
s Scope,
|
|
|
|
|
session, operation, method string,
|
|
|
|
|
params map[string]any,
|
|
|
|
|
) (json.RawMessage, bool, error) {
|
|
|
|
|
if m == nil || m.store == nil {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := m.store.member(ctx, s); err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
r, err := m.store.account(ctx, s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
if r == nil || r.RevokedAt != nil || time.Now().After(r.ExpiresAt) {
|
|
|
|
|
if operation == "call" || (operation == "preview" || operation == "focus") ||
|
|
|
|
|
(operation == "control" && method != "stop" && method != "select") {
|
|
|
|
|
return nil, false, ErrAuthorization
|
|
|
|
|
}
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
if r.Owner == "" || time.Now().After(r.LeaseUntil) {
|
|
|
|
|
if operation == "call" || operation == "preview" || operation == "focus" ||
|
|
|
|
|
(operation == "control" && (method == "start" || method == "resume")) {
|
|
|
|
|
return nil, false, errors.New("browser disconnected; wait for reconnection and resume the task")
|
|
|
|
|
}
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
if r.Owner == m.nodeID {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
if ctx.Value(localRPCKey{}) != nil {
|
|
|
|
|
return nil, false, errors.New("browser owner changed; refresh status")
|
|
|
|
|
}
|
|
|
|
|
if len(m.clusterSecret) < 32 || !validInternalURL(r.OwnerURL) {
|
|
|
|
|
return nil, false, errors.New("browser is connected to another node; configure BrowserSkill cluster routing")
|
|
|
|
|
}
|
|
|
|
|
req := clusterRequest{m.nodeID, s, session, operation, method, params}
|
|
|
|
|
req.Node = r.Owner
|
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
2026-09-11 15:46:01 +00:00
|
|
|
forwardCtx, cancel := context.WithTimeout(ctx, clusterTimeout(operation, method))
|
2026-09-11 09:59:27 +00:00
|
|
|
defer cancel()
|
|
|
|
|
request, err := http.NewRequestWithContext(
|
|
|
|
|
forwardCtx,
|
|
|
|
|
http.MethodPost,
|
|
|
|
|
strings.TrimRight(r.OwnerURL, "/")+internalPath,
|
|
|
|
|
bytes.NewReader(body),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
|
nonce := randomID()
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
request.Header.Set("X-Browser-Timestamp", timestamp)
|
|
|
|
|
request.Header.Set("X-Browser-Nonce", nonce)
|
|
|
|
|
request.Header.Set("X-Browser-Signature", signRPC(m.clusterSecret, timestamp+"\n"+nonce, body))
|
|
|
|
|
response, err := m.forwardClient.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, true, errors.New("browser owner unavailable; do not replay interrupted operations")
|
|
|
|
|
}
|
|
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
return nil, true, errors.New("browser owner rejected the request; refresh connection status")
|
|
|
|
|
}
|
|
|
|
|
var result clusterResponse
|
|
|
|
|
if json.NewDecoder(io.LimitReader(response.Body, maxFrame)).Decode(&result) != nil {
|
|
|
|
|
return nil, true, errors.New("invalid browser owner response")
|
|
|
|
|
}
|
feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193)
* feat(browser): enhance BrowserIcon component and update localization strings
- Updated BrowserIcon component to accept dynamic width and height props.
- Revised localization strings in English, Japanese, Korean, Russian, and Chinese for improved clarity and user guidance regarding browser connection and usage instructions.
- Enhanced BrowserConnectionSettings view with better structure and user feedback elements.
* Default BrowserSkill tasks to a separate window
* refactor(prompts): streamline agent system prompts and context templates
- Updated agent system prompts to enhance clarity and specificity in user interactions.
- Revised context templates to better separate metadata and user requests, improving overall structure.
- Adjusted fallback and intent prompts to ensure consistent language handling and response requirements.
- Introduced new localization strings for browser search instructions across multiple languages.
- Added tests for agent prompt serialization and hydration to ensure template integrity and functionality.
* chore: wrap prompt strings to satisfy golangci-lint
Keep the settings, separate-window, and prompt commits pushable together
without rewriting their history.
* fix(frontend): match agent editor save test to serialized payload
The prompt-inheritance save path still closes after update; the test
should assert that payload, not the unsanitized form object.
* feat(browser): implement browser connection management and related tests
- Introduced a new `browserConnection` store to manage the state of browser connections, including online status and device information.
- Updated the `Input-field` component to handle browser connection states, enabling/disabling features based on the connection status.
- Added tests for the `browserConnection` store to ensure correct behavior under various scenarios.
- Enhanced the `Input-field` and `BrowserConnectionSettings` components to provide user feedback based on the browser connection state.
- Adjusted related components and views to integrate the new browser connection logic, improving overall user experience.
* fix(frontend): update model-save assertion for serialized agent payload
The agent editor still persists the selected model ID via a spread of
formData; the source test should match that payload, not the raw form.
2026-09-11 13:28:58 +00:00
|
|
|
if result.RPCError != nil {
|
|
|
|
|
result.RPCError.BoundDetails()
|
|
|
|
|
return nil, true, result.RPCError
|
|
|
|
|
}
|
2026-09-11 09:59:27 +00:00
|
|
|
if result.Error != "" {
|
|
|
|
|
return nil, true, errors.New(result.Error)
|
|
|
|
|
}
|
|
|
|
|
return result.Data, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InternalHTTP accepts authenticated, replay-protected requests for this node's connections.
|
|
|
|
|
func (m *Manager) InternalHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
|
|
|
if r.Method != http.MethodPost || len(m.clusterSecret) < 32 {
|
|
|
|
|
http.Error(w, "unavailable", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
timestamp := r.Header.Get("X-Browser-Timestamp")
|
|
|
|
|
nonce := r.Header.Get("X-Browser-Nonce")
|
|
|
|
|
unix, err := strconv.ParseInt(timestamp, 10, 64)
|
|
|
|
|
if err != nil || time.Since(time.Unix(unix, 0)) > time.Minute || time.Until(time.Unix(unix, 0)) > time.Minute {
|
|
|
|
|
http.Error(w, "expired request", http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1<<20))
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sig, err := hex.DecodeString(r.Header.Get("X-Browser-Signature"))
|
|
|
|
|
want, _ := hex.DecodeString(signRPC(m.clusterSecret, timestamp+"\n"+nonce, body))
|
|
|
|
|
if err != nil || !hmac.Equal(sig, want) {
|
|
|
|
|
http.Error(w, "invalid signature", http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(nonce) != 32 {
|
|
|
|
|
http.Error(w, "invalid nonce", http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
if m.seenRPC == nil {
|
|
|
|
|
m.seenRPC = map[string]time.Time{}
|
|
|
|
|
}
|
|
|
|
|
for key, expiry := range m.seenRPC {
|
|
|
|
|
if time.Now().After(expiry) {
|
|
|
|
|
delete(m.seenRPC, key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_, replayed := m.seenRPC[nonce]
|
|
|
|
|
full := len(m.seenRPC) >= 8192
|
|
|
|
|
if !replayed && !full {
|
|
|
|
|
m.seenRPC[nonce] = time.Now().Add(2 * time.Minute)
|
|
|
|
|
}
|
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
if replayed || full {
|
|
|
|
|
http.Error(w, "replayed or throttled request", http.StatusConflict)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var input clusterRequest
|
|
|
|
|
if json.Unmarshal(body, &input) != nil || input.Node != m.nodeID || !input.Scope.valid() {
|
|
|
|
|
http.Error(w, "invalid owner request", http.StatusConflict)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Signed internal RPC is executed locally, never forwarded a second time.
|
2026-09-11 15:46:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(
|
|
|
|
|
context.WithValue(r.Context(), localRPCKey{}, true), clusterTimeout(input.Operation, input.Method),
|
|
|
|
|
)
|
2026-09-11 09:59:27 +00:00
|
|
|
defer cancel()
|
|
|
|
|
result := clusterResponse{}
|
|
|
|
|
switch input.Operation {
|
|
|
|
|
case "status":
|
|
|
|
|
var status Status
|
|
|
|
|
status, err = m.GetStatus(ctx, input.Scope, input.Session)
|
|
|
|
|
if err == nil {
|
|
|
|
|
result.Data, _ = json.Marshal(status)
|
|
|
|
|
}
|
|
|
|
|
case "call":
|
|
|
|
|
result.Data, err = m.Call(ctx, input.Scope, input.Session, input.Method, input.Params)
|
|
|
|
|
case "control":
|
|
|
|
|
err = m.Control(ctx, input.Scope, input.Session, input.Method)
|
|
|
|
|
if err == nil {
|
|
|
|
|
result.Data = json.RawMessage(`{}`)
|
|
|
|
|
}
|
|
|
|
|
case "preview":
|
|
|
|
|
result.Data, err = m.Preview(ctx, input.Scope, input.Session)
|
2026-09-19 15:37:22 +00:00
|
|
|
case "finish_turn":
|
|
|
|
|
keepOpen, valid := input.Params["keep_open"].(bool)
|
|
|
|
|
if !valid {
|
|
|
|
|
err = errors.New("keep_open must be a boolean")
|
|
|
|
|
} else {
|
|
|
|
|
err = m.FinishTurn(ctx, input.Scope, input.Session, keepOpen)
|
|
|
|
|
}
|
2026-09-11 09:59:27 +00:00
|
|
|
case "focus":
|
|
|
|
|
err = m.Focus(ctx, input.Scope, input.Session)
|
|
|
|
|
case "forget":
|
|
|
|
|
m.forgetLocal(input.Scope, []string{input.Session})
|
|
|
|
|
default:
|
|
|
|
|
err = errors.New("unsupported browser operation")
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
result.Error = err.Error()
|
feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193)
* feat(browser): enhance BrowserIcon component and update localization strings
- Updated BrowserIcon component to accept dynamic width and height props.
- Revised localization strings in English, Japanese, Korean, Russian, and Chinese for improved clarity and user guidance regarding browser connection and usage instructions.
- Enhanced BrowserConnectionSettings view with better structure and user feedback elements.
* Default BrowserSkill tasks to a separate window
* refactor(prompts): streamline agent system prompts and context templates
- Updated agent system prompts to enhance clarity and specificity in user interactions.
- Revised context templates to better separate metadata and user requests, improving overall structure.
- Adjusted fallback and intent prompts to ensure consistent language handling and response requirements.
- Introduced new localization strings for browser search instructions across multiple languages.
- Added tests for agent prompt serialization and hydration to ensure template integrity and functionality.
* chore: wrap prompt strings to satisfy golangci-lint
Keep the settings, separate-window, and prompt commits pushable together
without rewriting their history.
* fix(frontend): match agent editor save test to serialized payload
The prompt-inheritance save path still closes after update; the test
should assert that payload, not the unsanitized form object.
* feat(browser): implement browser connection management and related tests
- Introduced a new `browserConnection` store to manage the state of browser connections, including online status and device information.
- Updated the `Input-field` component to handle browser connection states, enabling/disabling features based on the connection status.
- Added tests for the `browserConnection` store to ensure correct behavior under various scenarios.
- Enhanced the `Input-field` and `BrowserConnectionSettings` components to provide user feedback based on the browser connection state.
- Adjusted related components and views to integrate the new browser connection logic, improving overall user experience.
* fix(frontend): update model-save assertion for serialized agent payload
The agent editor still persists the selected model ID via a spread of
formData; the source test should match that payload, not the raw form.
2026-09-11 13:28:58 +00:00
|
|
|
if errors.As(err, &result.RPCError) {
|
|
|
|
|
result.RPCError.BoundDetails()
|
|
|
|
|
}
|
2026-09-11 09:59:27 +00:00
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_ = json.NewEncoder(w).Encode(result)
|
|
|
|
|
}
|