feat(clients): sandbox: data plane (JWT proxy, CDP WebSocket), operations, SDK chunk shapes
Data plane per the computer use quickstart and SDK 2.6.0 source: signJwt minted JWT, /cdp /tabs /cdp_ws_endpoint over HTTP with routing headers, CDP over the live WsClient with capped reconnect. Operations methods wrapped, input/output chunk shapes settled from the SDK, ComputerUse moved off execute. Execution paths remain verified by Discovery, docs and SDK only. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+199
-69
@@ -2,7 +2,8 @@
|
||||
|
||||
Owned directory: `src/google/sandbox/`. Namespace `molo::google::sandbox`.
|
||||
Depends on the Google foundation (`GoogleClient`, `Service`, `GoogleError`,
|
||||
`wait_operation`, `list_all`, `CloudContext`) and the Base64 codec. No other
|
||||
`wait_operation`, `list_all`, `CloudContext`), the Base64 codec and the RFC 6455 client of
|
||||
`src/google/live` (`WsClient`, `open_stream`, used only by `CdpSession`). No other
|
||||
module is included.
|
||||
|
||||
Purpose: create, inspect, pause, snapshot and delete Agent Sandbox
|
||||
@@ -20,24 +21,46 @@ delete) and `sandboxEnvironmentSnapshots` (get, list, delete). A sandbox's
|
||||
category is one of `codeExecutionEnvironment` (Python or JavaScript),
|
||||
`computerUseEnvironment` (browser) or `shellEnvironment` (bash).
|
||||
|
||||
**Workspaces.** No resource, method or schema named "workspace" exists in the
|
||||
`aiplatform` Discovery documents (v1 and v1beta1, checked 2026-10-24) and the
|
||||
sandbox docs index lists no workspace page. The product-level "Workspaces"
|
||||
(bash commands and file management isolated from core systems) is what the
|
||||
docs call the **shell sandbox**: `execute_bash` returns `stdout`, `stderr`,
|
||||
`returncode`, `duration_ms`, runs as an unprivileged user in `/workspace`, one
|
||||
fresh shell per command. This module implements that as `ShellRunner`.
|
||||
File management has no REST surface: `WorkspaceFiles` is a **local
|
||||
abstraction** that does file work with shell commands through `ShellRunner`.
|
||||
It is named local everywhere and is not a Google API.
|
||||
**Workspaces.** Re-checked 2026-10-24 (second pass): no resource, method or
|
||||
schema named "workspace" exists in the `aiplatform` Discovery documents (v1
|
||||
and v1beta1, 4.0 MB and 5.3 MB, case-insensitive search), none in the Python
|
||||
SDK 2.6.0 (`agentplatform/_genai`, `vertexai/_genai`: only `sandboxes`,
|
||||
`sandbox_templates`, `sandbox_snapshots`), and the docs index lists no
|
||||
workspace page. The platform overview still describes Workspaces as sandboxed
|
||||
environments to "run bash commands and manage files"; the docs call that the
|
||||
**shell sandbox**, and the SDK's own agent tool catalog
|
||||
(`_evals_builtin_tools.py`) shows file tools (`view_file`, `create_file`,
|
||||
`list_dir`, ...) as agent tools, not as REST. So there is still no REST
|
||||
surface. `ShellRunner` runs bash (`/exec`, see section 4) and `WorkspaceFiles`
|
||||
stays a **local abstraction** that does file work with shell commands. It is
|
||||
named local everywhere and is not a Google API.
|
||||
|
||||
**Computer use.** The documented actions (navigate, click, type, screenshot)
|
||||
are Chrome DevTools Protocol commands sent to the sandbox data plane with a
|
||||
service-account JWT (`generate_access_token`, `send_command`, `/cdp`,
|
||||
`/tabs`, a CDP WebSocket for Playwright, VNC view). The data plane is not in
|
||||
Discovery. `ComputerUse` carries the same CDP commands through
|
||||
`sandboxEnvironments.execute`, the only sandbox call in the REST surface; that
|
||||
mapping is unverified (section 7).
|
||||
**Computer use.** Google documents two ways, both on the sandbox data plane
|
||||
(not in Discovery, documented by the computer use quickstart and implemented
|
||||
by the Python SDK 2.6.0, `Sandboxes.send_command` and
|
||||
`generate_browser_ws_headers`, read from the SDK source):
|
||||
|
||||
- HTTP proxy: `https://{connectionInfo.loadBalancerHostname}` (or
|
||||
`http://{loadBalancerIp}`, or the customer PSC endpoint for VPC-SC) with
|
||||
headers `Authorization: Bearer <JWT>`, `X-Sandbox-Routing-Token:
|
||||
<connectionInfo.routingToken>`, `X-Sandbox-Port: 8080`. Routes: `GET /`
|
||||
health, `POST /tabs`, `GET /tabs`, `POST /tabs/{id}/activate`, `DELETE
|
||||
/tabs/{id}`, `GET /cdp_ws_endpoint`, `POST /cdp` (one CDP command on the
|
||||
active tab, body `{"command","params"}`), `POST /cdps` (several).
|
||||
- CDP WebSocket for Playwright `connect_over_cdp`: URL is the data plane host
|
||||
(`wss://`) plus the `endpoint` path from `GET /cdp_ws_endpoint`; the only
|
||||
auth is the header `Sec-WebSocket-Protocol: v1.stream, <JWT>,
|
||||
<routingToken>, 9222`.
|
||||
|
||||
The JWT is minted by `iamcredentials.googleapis.com` `projects.serviceAccounts.signJwt`
|
||||
(`POST v1/projects/-/serviceAccounts/{email}:signJwt`, body `{"payload":
|
||||
"<json string>"}`) with claims `iat, exp (iat+3600), iss=sub=<service account
|
||||
email>, nonce, aud="https://aiplatform.googleapis.com/"`. The caller needs
|
||||
`roles/aiplatform.user` and `roles/iam.serviceAccountTokenCreator` (docs).
|
||||
`ComputerUse` (navigate, click, type, screenshot, tabs, health, any CDP
|
||||
command) and `CdpSession` (CDP over the WebSocket) implement this. The shell
|
||||
sandbox needs none of it: `execute_bash` authenticates with the caller's own
|
||||
credentials through `sandboxEnvironments.execute` (docs and SDK).
|
||||
|
||||
## 2. Module map (one class or concern per file)
|
||||
|
||||
@@ -55,8 +78,13 @@ mapping is unverified (section 7).
|
||||
| `code_executor.h/.cpp` | `CodeExecutor`: code plus input files in, typed result out |
|
||||
| `shell_runner.h/.cpp` | `ShellRunner`: bash command with cwd and timeout |
|
||||
| `workspace_files.h/.cpp` | `WorkspaceFiles` (local): write, read, list, remove_file |
|
||||
| `computer_use.h/.cpp` | `ComputerUse`: navigate, click, type_text, screenshot |
|
||||
| `stream_executor.h/.cpp` | `StreamExecutor`: `bidiExecute` (v1beta1), streamed reply |
|
||||
| `jwt.h/.cpp` | `ITokenSource`, `IamJwtSource`: signJwt-minted data plane JWT, cache, refresh margin, invalidate |
|
||||
| `data_plane.h/.cpp` | `DataPlaneTarget` (address from connectionInfo), `DataPlane` (HTTP proxy: health, tabs, `/cdp`, `/cdps`, `/cdp_ws_endpoint`, header placement, one refresh on 401) |
|
||||
| `cdp_session.h/.cpp` | `CdpSession`: CDP JSON-RPC over the WebSocket (reuses `live::WsClient`), events, reconnect cap |
|
||||
| `data_plane_connector.h/.cpp` | `DataPlaneOptions`, `DataPlaneConnector`: sandbox name to `DataPlane` / `CdpSession` |
|
||||
| `computer_use.h/.cpp` | `ComputerUse`: navigate, click, type_text, screenshot, tabs, health, CDP command, over the data plane |
|
||||
| `operations.h/.cpp` | `Operations`: get, wait, cancel, delete (v1), list (v1beta1) for the three resources |
|
||||
| `stream_executor.h/.cpp` | `StreamExecutor`: `bidiExecute` (v1beta1), one request message, streamed reply (REST form, see section 7) |
|
||||
| `tools/check_discovery.py` | Discovery check for `calls.h` |
|
||||
|
||||
## 3. Static caps (see caps.h)
|
||||
@@ -80,7 +108,15 @@ mapping is unverified (section 7).
|
||||
| List page size / pages | 100 (Google default and ceiling) / 10 |
|
||||
| Template ports / disk | 16 / 1..1024 GB |
|
||||
| URL / typed text / coordinate | 2048 / 4096 / 0..10000 |
|
||||
| Input chunks per execute | 17 (one code chunk plus 16 files) |
|
||||
| Stream events | 256 |
|
||||
| Data plane JWT ttl / refresh margin | 60..3600 s (default 3600) / refresh inside the last 300 s, one refresh per call after a 401 |
|
||||
| Data plane reply / error excerpt / batch | 8 MiB / 512 bytes / 32 commands |
|
||||
| Data plane timeout | default 30 s, max 120 s; no redirects, no automatic retries |
|
||||
| Routing token / host / path / tab id | 4096 / 253 / 256 / 128 chars |
|
||||
| CDP message / buffered events / call timeout | 8 MiB / 256 (oldest dropped and counted) / 30 s default, 120 s max |
|
||||
| CDP reconnects / floor between connects | 3 per session, ever / 250 ms |
|
||||
| Operations wait | 1..120 s server side |
|
||||
| LRO wait | foundation `wait_operation`: 600 s, 400 polls |
|
||||
|
||||
Google's own limit is 100 MB of files per request or response. Ours are
|
||||
@@ -109,24 +145,55 @@ again inside the Chunk, and the codec caps base64 input at 16 MiB.
|
||||
Over-cap or unsafe files are never dropped silently: each gets a reason in
|
||||
`skipped`. `ok()` is true when the exit status is absent or 0.
|
||||
- `Chunk.data` and `Metadata.attributes` values are base64 on the wire
|
||||
(Discovery: `format: byte`); the codec exposes raw bytes.
|
||||
- Computer use accepts only `http://` and `https://` URLs.
|
||||
(Discovery: `format: byte`; the SDK models both as bytes); the codec exposes
|
||||
raw bytes. An attribute value that is not base64 makes the chunk invalid and
|
||||
it is skipped loudly, never guessed.
|
||||
- Input chunks (SDK `execute_code` / `execute_bash`): code is one
|
||||
`application/json` chunk `{"code": ...}` followed by one chunk per input file
|
||||
(its mime type, raw bytes, attribute `file_name`); shell is three chunks, mime
|
||||
`application/x.sandbox-request-uri` data `/exec`, mime
|
||||
`application/x.sandbox-request-port` data `8080`, then `application/json`
|
||||
`{"command","cwd","timeout"}`.
|
||||
- Data plane: the JWT is sent only to the address derived from the sandbox's
|
||||
own `connectionInfo` (or a caller-supplied PSC endpoint); redirects are off;
|
||||
a loopback override is accepted for loopback hosts only. Order of checks in
|
||||
`DataPlaneConnector::open`: name, credential (503), signer (503), sandbox
|
||||
read, `STATE_RUNNING` (409), address. A 401 refreshes the JWT once.
|
||||
- CDP commands are never replayed: a connection lost during a call throws
|
||||
`CDP_CONNECTION_LOST`; the next call reconnects (fresh JWT, endpoint
|
||||
re-resolved, at most 3 reconnects per session). Session ids from before a
|
||||
reconnect are void.
|
||||
- Operations: `delete` removes the operation record, not the resource; `cancel`
|
||||
and `delete` are explicit methods.
|
||||
- Computer use accepts only `http://` and `https://` URLs. The data plane needs a service account to sign
|
||||
with (`GOOGLE_SERVICE_ACCOUNT_EMAIL`, else `client_email` of
|
||||
`GOOGLE_APPLICATION_CREDENTIALS`); without one calls answer 503 NOT_CONFIGURED.
|
||||
|
||||
## 5. Verified against discovery
|
||||
|
||||
`python3 -I src/google/sandbox/tools/check_discovery.py` fetches
|
||||
`https://aiplatform.googleapis.com/$discovery/rest?version=v1` and `v1beta1`
|
||||
and fails when any row of `calls.h` is absent: 18 calls (rpc id, HTTP method,
|
||||
path, request body field paths including nested ones), 21 schemas (field
|
||||
names) and 7 enums (values).
|
||||
(and `iamcredentials` `v1`) and fails when any row of `calls.h` is absent:
|
||||
34 calls (rpc id, HTTP method, path, request body field paths including nested
|
||||
ones), 27 schemas (field names) and 7 enums (values).
|
||||
|
||||
Result on 2026-10-24: `18 calls, 21 schemas, 7 enums, 204 checks, 0 missing`.
|
||||
Result on 2026-10-24: `34 calls, 27 schemas, 7 enums, 268 checks, 0 missing`.
|
||||
Findings from the check that shaped the code: `computerUseEnvironment`,
|
||||
`useGkeTd` and `persistentDiskConfig` exist only in `v1beta1`; `bidiExecute`
|
||||
exists only in `v1beta1`; `PauseSandboxEnvironmentRequest`,
|
||||
`ResumeSandboxEnvironmentRequest` and `AuthorizeSandboxEnvironmentAccessRequest`
|
||||
have no fields; the snapshot request body is a `SandboxEnvironmentSnapshot`
|
||||
whose `sourceSandboxEnvironment` is required.
|
||||
whose `sourceSandboxEnvironment` is required; the operations methods
|
||||
(`get`, `wait`, `cancel`, `delete`) exist in `v1` and `v1beta1`, `list` only in
|
||||
`v1beta1` (`GET v1beta1/{+name}/operations`, `name` is the sandbox, template or
|
||||
snapshot); `wait` takes a `timeout` query parameter (google-duration).
|
||||
|
||||
Method coverage: every Discovery method under `sandboxEnvironments`,
|
||||
`sandboxEnvironmentTemplates` and `sandboxEnvironmentSnapshots` of
|
||||
`projects.locations.reasoningEngines` is wrapped or declared: 10 sandbox
|
||||
methods, 4 template, 3 snapshot, 5 operations per resource (15). Not wrapped
|
||||
on purpose: the project-less aliases (`reasoningEngines.sandbox*`), and the
|
||||
operations of the reasoning engine itself (owned by the runtime module).
|
||||
|
||||
## 6. Verified live
|
||||
|
||||
@@ -155,8 +222,26 @@ statuses and bodies, so the key reaches these read routes too. A made-up
|
||||
collection (`.../bogusThings`) answers an HTML 404 page, and a non-numeric
|
||||
engine id (`none`) answers 400 INVALID_ARGUMENT; engine ids are numeric.
|
||||
|
||||
Second pass 2026-10-24 (same credentials, read-only):
|
||||
|
||||
| Call | Status and body |
|
||||
|---|---|
|
||||
| `GET v1beta1/.../sandboxEnvironments/{absent}/operations?pageSize=5` | 200 `{}` (empty list, not 404) |
|
||||
| `GET v1/.../sandboxEnvironments/{absent}/operations/1` | 404 NOT_FOUND "Operation projects/P/.../operations/1 not found." |
|
||||
| same for `.../sandboxEnvironmentTemplates/{absent}/operations/1` and `.../sandboxEnvironmentSnapshots/{absent}/operations/1` | 404 NOT_FOUND, same message shape |
|
||||
| `POST iamcredentials v1/projects/-/serviceAccounts/{email}:signJwt` with the SDK payload | 403 PERMISSION_DENIED: "IAM Service Account Credentials API has not been used in project N before or it is disabled" |
|
||||
|
||||
The signJwt answer proves the route, path and body shape reach the service;
|
||||
it stops at the project not having the IAM Credentials API enabled. It was not
|
||||
enabled here (that changes the project; an owner decision). Hence no data plane
|
||||
JWT has ever been minted by this module against Google, and the data plane was
|
||||
never reached: the project has no engine, so no sandbox, so no `connectionInfo`
|
||||
and no proxy address. The whole data plane is verified by the SDK source and
|
||||
docs (section 9) plus loopback mechanics tests, not by Google replies.
|
||||
|
||||
Never exercised live (Discovery only): create, pause, resume, snapshot,
|
||||
delete, execute, bidiExecute, authorizeAccess, template create and delete,
|
||||
delete, execute, bidiExecute, authorizeAccess, wait/cancel/delete of operations,
|
||||
template create and delete, the data plane (HTTP proxy and CDP WebSocket),
|
||||
and every response shape of an existing sandbox. Whether a key may mutate
|
||||
sandboxes is unknown.
|
||||
|
||||
@@ -166,57 +251,88 @@ google-test` (a key in `GOOGLE_API_KEY` also works). Test
|
||||
sandboxes of an engine id that cannot exist and gets a sandbox that cannot
|
||||
exist, expecting 404 NOT_FOUND through the real client; it creates nothing.
|
||||
It passed on 2026-10-24 with the OAuth token and with the key.
|
||||
`google_live_sandbox.operations_read_only` and `google_live_sandbox.sign_jwt_probe`
|
||||
cover the second-pass rows above (the second accepts 403 or 400 and prints the status).
|
||||
Gemini's built-in `codeExecution` tool inside `generateContent` belongs to the
|
||||
models module and is not used here.
|
||||
|
||||
## 7. Unverified
|
||||
## 7. Settled and Unverified
|
||||
|
||||
Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
`ChunkCodec` so one edit fixes it:
|
||||
Settled from primary sources on 2026-10-24 (no Google reply seen, because no
|
||||
sandbox exists and none may be created):
|
||||
|
||||
- The payload of an input Chunk. Docs show only the SDK call
|
||||
(`execute_code(input_data={"code":..., "files":[{"name","content"}]})`) and
|
||||
an older doc revision says input file content is base64. Assumed: one
|
||||
`application/json` Chunk holding `{"code", "files":[{"name","content"(base64)}]}`.
|
||||
- The payload of a shell input Chunk. Assumed `{"command","cwd","timeout"}`
|
||||
(names from the SDK helper `execute_bash(command, cwd, timeout)`).
|
||||
- The payload of a computer use input Chunk: `{"command":"Page.navigate",
|
||||
"params":{...}}` is the documented `/cdp` body; sending it through `execute`
|
||||
is assumed. Click is `Input.dispatchMouseEvent` pressed then released, typing
|
||||
is `Input.insertText`, screenshot is `Page.captureScreenshot` (CDP names, not
|
||||
Google names).
|
||||
- Output Chunk payloads: docs show an `application/json` Chunk with `msg_out`,
|
||||
`msg_err`, `output_files[{content (base64)}]`, plus Chunks with a `file_name`
|
||||
attribute. The `name` key inside `output_files` and the shell keys `stdout`,
|
||||
`stderr`, `returncode`, `duration_ms` (seen only in the SDK dict) are
|
||||
assumed. Unknown shapes are skipped loudly, never guessed.
|
||||
- Whether `attributes` values are really base64 on the wire (Discovery says
|
||||
`byte`; docs show plain text). The decoder tries base64 and falls back to the
|
||||
text as written.
|
||||
- Whether `bidiExecute` over this client's POST stream behaves as a server
|
||||
stream; it is a bidirectional method and may need a persistent channel.
|
||||
- Which responses carry which `@type` inside an Operation `response`
|
||||
(`SandboxEnvironment`, `SandboxEnvironmentSnapshot`, template); parsed by
|
||||
- Input chunks for code and shell, the shell output keys (`stdout`, `stderr`,
|
||||
`returncode`, `duration_ms`), the code output keys (`msg_out`, `msg_err`,
|
||||
`output_files[].content` base64) and file chunks with the `file_name`
|
||||
attribute: Python SDK 2.6.0 `execute_code` / `execute_bash` source, the shell
|
||||
and code execution quickstarts. Attribute values are bytes, base64 on the
|
||||
wire: Discovery `format: byte` for `Metadata.attributes`, SDK `bytes`.
|
||||
- CDP command body `{"command","params"}` for `POST /cdp`, the endpoint list,
|
||||
the `/cdp_ws_endpoint` reply key `endpoint`, the three headers, the
|
||||
`Sec-WebSocket-Protocol` form, the JWT claims: computer use quickstart and
|
||||
SDK source.
|
||||
- Workspaces have no REST surface (Discovery, SDK, docs index).
|
||||
|
||||
Still unverified; each is inferred and isolated in one function:
|
||||
|
||||
- The reply body shapes of the data plane. Docs and SDK show only that the
|
||||
body is returned as text. `ComputerUse::result_of` accepts `{"result": {...}}`
|
||||
or the object itself; the screenshot is the base64 `data` of a CDP
|
||||
`Page.captureScreenshot` result. A reply with neither is `EMPTY_RESPONSE`.
|
||||
- The request body of `POST /cdps` (`{"commands":[{"command","params"}...]}` is
|
||||
our reading of "runs multiple CDP commands") and whether `POST /tabs` takes a
|
||||
body (we send none).
|
||||
- The output key `name` inside `output_files` (docs show only `content`). The
|
||||
decoder reads `name` or `file_name` and skips a nameless file loudly.
|
||||
- `Chunk.mimeType` of file input chunks when the caller gives none: we guess
|
||||
from the extension, the SDK sends an empty string.
|
||||
- Whether the CDP WebSocket answers browser-level (`Target.*`) or page-level
|
||||
commands first; `CdpSession::attach_first_page` uses flat sessions
|
||||
(`Target.attachToTarget {flatten:true}`), the standard Chrome mode that
|
||||
Playwright also uses over this endpoint.
|
||||
- Frame size: `live::LiveCaps::kMaxFramePayload` (4 MiB) caps one WebSocket
|
||||
frame. A base64 screenshot larger than that fails over the WebSocket
|
||||
(`TooBig`); use `ComputerUse::screenshot` (HTTP) for large pages.
|
||||
- `bidiExecute`: Discovery gives one `POST ...:bidiExecute` with a request body
|
||||
of `inputs` and a response of `outputs`, described as "bidirectional
|
||||
streaming". Over REST the request is one message and the reply a stream;
|
||||
`StreamExecutor` does exactly that over the real HTTP transport (mechanics
|
||||
tested against a loopback server). Whether Google's REST gateway accepts
|
||||
further input messages on the same call is not documented, so true
|
||||
full-duplex input is not offered.
|
||||
- Which responses carry which `@type` inside an Operation `response`; parsed by
|
||||
field name only.
|
||||
- The `filter` list parameter exists (AIP-160) but its grammar is not
|
||||
documented; it is not exposed.
|
||||
- The `filter` list parameter (AIP-160 grammar not documented) and
|
||||
`returnPartialSuccess` of operations list are not exposed.
|
||||
- Execution paths (execute, bidiExecute, data plane) have never run against
|
||||
Google; they are verified by Discovery, docs and SDK source, and by local
|
||||
mechanics tests only.
|
||||
|
||||
## 8. Gaps
|
||||
|
||||
- The sandbox data plane (HTTP proxy with JWT, `/tabs`, `/cdp`, CDP
|
||||
WebSocket, VNC) is not implemented; it needs a service-account JWT signer
|
||||
(foundation gap in `credentials.cpp`) and a WebSocket client (module `live`).
|
||||
Owner of the follow-up: this module, `computer_use.cpp`.
|
||||
- True bidirectional `bidiExecute` input (see Unverified).
|
||||
- `generate_access_token` and `generate_browser_ws_headers` (SDK helpers) have
|
||||
no REST method in Discovery.
|
||||
- The data plane needs the IAM Credentials API enabled on the project and
|
||||
`roles/iam.serviceAccountTokenCreator` for the signing account; neither could
|
||||
be checked (the API is disabled on `lisa-448004`, section 6). Owner:
|
||||
project administrator.
|
||||
- The data plane cannot be reached without a running sandbox, which this
|
||||
project does not have and must not create from here. First live run:
|
||||
create an engine and a computer use template and sandbox, then
|
||||
`POST /agent/sandbox/dataplane {action:health}`; the reply shapes in
|
||||
section 7 are then read and settled.
|
||||
- VNC streaming view (noVNC over WebSocket) is documented but without any
|
||||
connection detail; not implemented.
|
||||
- Template update, custom container resources/requests, DNS peering, private
|
||||
service connect ingress, CMEK and VPC-SC settings are not typed. Templates
|
||||
keep the whole document in `SandboxTemplate::raw`.
|
||||
keep the whole document in `SandboxTemplate::raw`. VPC-SC data plane access
|
||||
works only with a caller-supplied PSC endpoint (`DataPlaneOptions::psc_endpoint`).
|
||||
- Foundation: `Service::call` always posts to the one endpoint a service was
|
||||
built with; this module needs a different location and version per call, so
|
||||
`SandboxService` builds endpoints itself and talks to `GoogleClient`
|
||||
directly.
|
||||
directly. The data plane host is not a Google API host, so it uses its own
|
||||
`clients::Client` over the real HTTP transport with the JWT and routing
|
||||
headers (the foundation client applies only Google credentials).
|
||||
- `live::LiveCaps` (module `live`, not editable here) fixes the WebSocket frame
|
||||
cap at 4 MiB; a request to raise it belongs to that module.
|
||||
- The example server's flat JSON reader does not decode `\n`; the demo uses
|
||||
the real JSON parser for request bodies.
|
||||
|
||||
@@ -229,6 +345,12 @@ Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/shell-sandbox-quickstart
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/manage-sandboxes
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/reference/rest/v1beta1/projects.locations.reasoningEngines.sandboxEnvironments (and its `create`, `execute`, `list`, `delete`, `pause`, `authorizeAccess` pages) and `.../v1beta1/Chunk`
|
||||
- Python SDK `google-cloud-aiplatform` 2.6.0 (PyPI), files `agentplatform/_genai/sandboxes.py` (`execute_code`, `execute_bash`, `generate_access_token`, `send_command`, `generate_browser_ws_headers`), `sandbox_templates.py`, `sandbox_snapshots.py`, `_evals_builtin_tools.py`
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/scale/sandbox/computer-use-quickstart (JWT, `send_command`, endpoint table) and `.../computer-use` (`generate_browser_ws_headers`, `connect_over_cdp`, VNC)
|
||||
- https://docs.cloud.google.com/gemini-enterprise-agent-platform/release-notes (2026-09-09: Computer Use and Shell sandboxes GA, direct API `/exec`)
|
||||
- https://iamcredentials.googleapis.com/$discovery/rest?version=v1 (`signJwt`)
|
||||
- https://chromedevtools.github.io/devtools-protocol/ (CDP JSON-RPC, flat sessions, `Target`, `Page`, `Input`)
|
||||
- https://www.rfc-editor.org/rfc/rfc6455 (WebSocket, via `src/google/live`)
|
||||
- https://aiplatform.googleapis.com/$discovery/rest?version=v1
|
||||
- https://aiplatform.googleapis.com/$discovery/rest?version=v1beta1
|
||||
- Not read (listed on the docs index): `manage-templates`, `manage-snapshots`,
|
||||
@@ -239,7 +361,15 @@ Neither Discovery nor the docs state these; each is inferred and isolated in
|
||||
|
||||
`example/app/agent/SandboxDemo.cpp`, page script `example/assets/agent_sandbox.js`.
|
||||
GET `/agent/sandbox` (page, matrix row `agent-sandbox`), GET
|
||||
`/agent/sandbox/status`. POST `/agent/sandbox/{create,list,get,pause,resume,
|
||||
snapshot,snapshots,templates,execute,shell,computer,files}`, DELETE
|
||||
`/agent/sandbox/delete?name=`. Without a credential every Google route is 503
|
||||
`google_not_configured`; bad input is 422. Tests: `tests/example/agent/sandbox/`.
|
||||
`/agent/sandbox/status` (includes `data_plane_signer`, a boolean). POST
|
||||
`/agent/sandbox/{create,list,get,pause,resume,snapshot,snapshots,templates,
|
||||
execute,shell,computer,files,dataplane,cdp,stream,operations,resource,template}`,
|
||||
DELETE `/agent/sandbox/delete?name=`, DELETE `/agent/sandbox/operation?name=`,
|
||||
DELETE `/agent/sandbox/resource?kind=template|snapshot&name=`. `dataplane`
|
||||
covers health, tabs and any CDP command over HTTP; `cdp` opens the WebSocket
|
||||
for one call (optionally attaching the first page) and closes it; `stream` is
|
||||
`bidiExecute`; `operations` is get, wait, list, cancel. Without a credential
|
||||
(or without a service account for the data plane) every Google route is 503
|
||||
`google_not_configured`; bad input is 422. Tests: `tests/example/agent/sandbox/`
|
||||
(route tests) and `tests/src/google/sandbox/` (module tests, loopback data
|
||||
plane in `test_sandbox_dataplane.cpp`).
|
||||
|
||||
@@ -53,6 +53,22 @@ inline constexpr DeclaredCall kDeclaredCalls[] = {
|
||||
{"snapshot.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.get", ""},
|
||||
{"snapshot.list", "GET", "{+parent}/sandboxEnvironmentSnapshots", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.list", ""},
|
||||
{"snapshot.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.delete", ""},
|
||||
{"sandbox.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.get", ""},
|
||||
{"sandbox.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.wait", ""},
|
||||
{"sandbox.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.cancel", ""},
|
||||
{"sandbox.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.delete", ""},
|
||||
{"sandbox.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironments.operations.list", ""},
|
||||
{"template.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.get", ""},
|
||||
{"template.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.wait", ""},
|
||||
{"template.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.cancel", ""},
|
||||
{"template.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.delete", ""},
|
||||
{"template.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentTemplates.operations.list", ""},
|
||||
{"snapshot.operation.get", "GET", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.get", ""},
|
||||
{"snapshot.operation.wait", "POST", "{+name}:wait", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.wait", ""},
|
||||
{"snapshot.operation.cancel", "POST", "{+name}:cancel", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.cancel", ""},
|
||||
{"snapshot.operation.delete", "DELETE", "{+name}", "aiplatform", "v1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.delete", ""},
|
||||
{"snapshot.operation.list", "GET", "{+name}/operations", "aiplatform", "v1beta1", "aiplatform.projects.locations.reasoningEngines.sandboxEnvironmentSnapshots.operations.list", ""},
|
||||
{"iam.signJwt", "POST", "{+name}:signJwt", "iamcredentials", "v1", "iamcredentials.projects.serviceAccounts.signJwt", "payload,delegates"},
|
||||
};
|
||||
// CALLS-END
|
||||
|
||||
@@ -79,6 +95,12 @@ inline constexpr DeclaredSchema kDeclaredSchemas[] = {
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentTemplateEgressControlConfig", "internetAccess,networkAttachment,dnsPeeringConfigs"},
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentTemplatePersistentDiskConfig", "enabled,mountPath,sizeGb"},
|
||||
{"aiplatform", "v1beta1", "ListSandboxEnvironmentTemplatesResponse", "sandboxEnvironmentTemplates,nextPageToken"},
|
||||
{"aiplatform", "v1beta1", "SandboxEnvironmentConnectionInfo", "loadBalancerIp,loadBalancerHostname,sandboxInternalIp,routingToken,serviceAttachment"},
|
||||
{"aiplatform", "v1beta1", "BidiExecuteSandboxEnvironmentRequest", "inputs"},
|
||||
{"aiplatform", "v1beta1", "Chunk", "mimeType,data,metadata"},
|
||||
{"aiplatform", "v1beta1", "Metadata", "attributes"},
|
||||
{"iamcredentials", "v1", "SignJwtRequest", "payload,delegates"},
|
||||
{"iamcredentials", "v1", "SignJwtResponse", "keyId,signedJwt"},
|
||||
};
|
||||
// SCHEMAS-END
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ struct SandboxCaps {
|
||||
static constexpr std::size_t kMaxCommandBytes = 8192;
|
||||
static constexpr std::size_t kMaxInternalCommandBytes = 65536; // Workspace helpers only.
|
||||
static constexpr std::size_t kMaxInputFiles = 16;
|
||||
// One JSON code chunk plus one chunk per file.
|
||||
static constexpr std::size_t kMaxInputChunks = 1 + kMaxInputFiles;
|
||||
static constexpr std::size_t kMaxFileBytes = 4u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxInputTotalBytes = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxFileNameChars = 255;
|
||||
@@ -49,6 +51,37 @@ struct SandboxCaps {
|
||||
static constexpr std::size_t kMaxUrlChars = 2048;
|
||||
static constexpr std::size_t kMaxTypedChars = 4096;
|
||||
static constexpr int kMaxCoordinate = 10000;
|
||||
|
||||
// Data plane (HTTP proxy and CDP WebSocket). All static ceilings.
|
||||
static constexpr long kJwtTtlSeconds = 3600; // SDK default
|
||||
static constexpr long kMinJwtTtlSeconds = 60;
|
||||
static constexpr long kMaxJwtTtlSeconds = 3600; // never longer than an hour
|
||||
static constexpr long kJwtRefreshMarginSeconds = 300; // refresh when this close to exp
|
||||
static constexpr std::size_t kMaxEmailChars = 254;
|
||||
static constexpr std::size_t kMaxRoutingTokenChars = 4096;
|
||||
static constexpr std::size_t kMaxHostChars = 253;
|
||||
static constexpr std::size_t kMaxDataPathChars = 256;
|
||||
static constexpr std::size_t kMaxTabIdChars = 128;
|
||||
static constexpr std::size_t kMaxDataReplyBytes = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxErrorExcerpt = 512;
|
||||
static constexpr std::size_t kMaxBatchCommands = 32;
|
||||
static constexpr int kDataPlaneTimeoutMs = 30000;
|
||||
static constexpr int kMaxDataPlaneTimeoutMs = 120000;
|
||||
static constexpr int kMaxAuthRetries = 1; // one JWT refresh after a 401
|
||||
|
||||
// CDP over WebSocket.
|
||||
static constexpr int kCdpConnectTimeoutMs = 10000;
|
||||
static constexpr int kCdpCallTimeoutMs = 30000;
|
||||
static constexpr int kMaxCdpCallTimeoutMs = 120000;
|
||||
static constexpr int kCdpPollMs = 200;
|
||||
static constexpr std::size_t kMaxCdpEvents = 256; // buffered, oldest dropped and counted
|
||||
static constexpr std::size_t kMaxCdpMessageChars = 8u * 1024u * 1024u;
|
||||
static constexpr std::size_t kMaxCdpMethodChars = 128;
|
||||
static constexpr int kMaxCdpReconnects = 3; // per session, ever
|
||||
static constexpr int kMinReconnectGapMs = 250; // floor between connects
|
||||
static constexpr std::size_t kMaxCdpSessionIdChars = 128;
|
||||
// Operations.
|
||||
static constexpr int kMaxOperationWaitSeconds = 120;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/live/ws_error.h"
|
||||
#include "google/live/ws_url.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kService = "sandbox-cdp";
|
||||
|
||||
long now_ms() {
|
||||
return static_cast<long>(std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
|
||||
GoogleError lost(const std::string& why) {
|
||||
return GoogleError(502, "CDP_CONNECTION_LOST", why, kService);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CdpSession::CdpSession(std::shared_ptr<DataPlane> plane, CdpOptions options)
|
||||
: plane_(std::move(plane)), options_(std::move(options)) {
|
||||
if (!plane_) throw GoogleError::invalid("a data plane is required");
|
||||
if (options_.connect_timeout_ms == 0) options_.connect_timeout_ms = SandboxCaps::kCdpConnectTimeoutMs;
|
||||
if (options_.call_timeout_ms == 0) options_.call_timeout_ms = SandboxCaps::kCdpCallTimeoutMs;
|
||||
if (options_.connect_timeout_ms < 100 || options_.connect_timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs ||
|
||||
options_.call_timeout_ms < 100 || options_.call_timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeouts must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
DataPlaneTarget::validate_port(options_.browser_port);
|
||||
}
|
||||
|
||||
CdpSession::~CdpSession() {
|
||||
try {
|
||||
close();
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::validate_session_id(const std::string& id) {
|
||||
if (id.size() > SandboxCaps::kMaxCdpSessionIdChars)
|
||||
throw GoogleError::invalid("session id too long");
|
||||
for (char c : id)
|
||||
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '-' && c != '_')
|
||||
throw GoogleError::invalid("session id has an invalid character");
|
||||
}
|
||||
|
||||
Json CdpSession::request(long id, const std::string& method, const Json& params,
|
||||
const std::string& session_id) {
|
||||
DataPlane::validate_command(method);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
validate_session_id(session_id);
|
||||
Json m = Json::object();
|
||||
m.set("id", Json(id));
|
||||
m.set("method", Json(method));
|
||||
m.set("params", params);
|
||||
if (!session_id.empty()) m.set("sessionId", Json(session_id));
|
||||
return m;
|
||||
}
|
||||
|
||||
bool CdpSession::connected() const { return ws_ && !ws_->closed(); }
|
||||
|
||||
void CdpSession::open_socket() {
|
||||
// Floor between connection attempts: a flapping peer is never hammered.
|
||||
if (last_connect_ms_ != 0) {
|
||||
long gap = now_ms() - last_connect_ms_;
|
||||
if (gap < SandboxCaps::kMinReconnectGapMs)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SandboxCaps::kMinReconnectGapMs - gap));
|
||||
}
|
||||
last_connect_ms_ = now_ms();
|
||||
if (endpoint_path_.empty()) endpoint_path_ = plane_->cdp_ws_endpoint();
|
||||
for (int attempt = 0;; ++attempt) {
|
||||
try {
|
||||
live::WsUrl url = live::WsUrl::parse(plane_->ws_url(endpoint_path_));
|
||||
live::WsHeaders headers = plane_->ws_headers(options_.browser_port);
|
||||
auto stream = live::open_stream(url, options_.connect_timeout_ms, 1000);
|
||||
auto ws = std::make_unique<live::WsClient>(std::move(stream));
|
||||
ws->handshake(url, headers, options_.connect_timeout_ms);
|
||||
ws->set_read_timeout_ms(SandboxCaps::kCdpPollMs);
|
||||
ws_ = std::move(ws);
|
||||
++connects_;
|
||||
return;
|
||||
} catch (const live::WsError& e) {
|
||||
bool auth = e.kind() == live::WsError::Kind::Handshake &&
|
||||
(e.http_status() == 401 || e.http_status() == 403);
|
||||
if (auth && attempt < SandboxCaps::kMaxAuthRetries) {
|
||||
plane_->tokens().invalidate();
|
||||
continue;
|
||||
}
|
||||
throw GoogleError(auth ? e.http_status() : 502, auth ? "CDP_UNAUTHORIZED" : "UNAVAILABLE",
|
||||
e.what(), kService);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::connect() {
|
||||
if (connected()) return;
|
||||
open_socket();
|
||||
}
|
||||
|
||||
void CdpSession::ensure_connected() {
|
||||
if (connected()) return;
|
||||
if (connects_ > 0) {
|
||||
// A socket that existed and died: a capped reconnect with a fresh JWT.
|
||||
if (reconnects_ >= SandboxCaps::kMaxCdpReconnects)
|
||||
throw GoogleError(502, "CDP_RECONNECT_LIMIT",
|
||||
"more than " + std::to_string(SandboxCaps::kMaxCdpReconnects) +
|
||||
" reconnects; open a new session",
|
||||
kService);
|
||||
++reconnects_;
|
||||
plane_->tokens().invalidate();
|
||||
endpoint_path_.clear();
|
||||
}
|
||||
open_socket();
|
||||
}
|
||||
|
||||
void CdpSession::close() {
|
||||
if (ws_) {
|
||||
try {
|
||||
ws_->close(1000, "");
|
||||
ws_->shutdown();
|
||||
} catch (...) {
|
||||
}
|
||||
ws_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void CdpSession::push_event(Json event) {
|
||||
if (events_.size() >= SandboxCaps::kMaxCdpEvents) {
|
||||
events_.pop_front();
|
||||
++dropped_events_;
|
||||
}
|
||||
events_.push_back(std::move(event));
|
||||
}
|
||||
|
||||
// Reads for up to budget_ms. Returns true once the reply with wanted_id
|
||||
// (>= 0) arrived in *reply. Every other message with an id is dropped, every
|
||||
// message without one is buffered as an event.
|
||||
bool CdpSession::pump(int budget_ms, long wanted_id, Json* reply) {
|
||||
long deadline = now_ms() + budget_ms;
|
||||
for (;;) {
|
||||
if (now_ms() >= deadline) return false;
|
||||
std::optional<live::WsMessage> m;
|
||||
try {
|
||||
m = ws_->receive();
|
||||
} catch (const live::WsError& e) {
|
||||
ws_.reset();
|
||||
throw lost(e.what());
|
||||
}
|
||||
if (!m) continue;
|
||||
if (m->kind == live::WsMessage::Kind::Close) {
|
||||
ws_.reset();
|
||||
throw lost("peer closed the CDP socket (code " + std::to_string(m->code) + ")");
|
||||
}
|
||||
if (m->data.size() > SandboxCaps::kMaxCdpMessageChars)
|
||||
throw GoogleError(502, "TOO_BIG", "CDP message over cap", kService);
|
||||
Json doc;
|
||||
try {
|
||||
doc = Json::parse(m->data);
|
||||
} catch (const clients::body::JsonError&) {
|
||||
continue; // Not CDP; never guessed at.
|
||||
}
|
||||
if (!doc.is_object()) continue;
|
||||
const Json* id = doc.find("id");
|
||||
if (id && id->is_number()) {
|
||||
if (wanted_id >= 0 && id->as_int() == wanted_id) {
|
||||
*reply = std::move(doc);
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
push_event(std::move(doc));
|
||||
if (wanted_id < 0) return true; // Caller scans the buffer.
|
||||
}
|
||||
}
|
||||
|
||||
Json CdpSession::call(const std::string& method, const Json& params, const std::string& session_id,
|
||||
int timeout_ms) {
|
||||
long id = next_id_;
|
||||
Json msg = request(id, method, params, session_id);
|
||||
if (timeout_ms == 0) timeout_ms = options_.call_timeout_ms;
|
||||
if (timeout_ms < 100 || timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeout must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
ensure_connected();
|
||||
++next_id_;
|
||||
try {
|
||||
ws_->send_text(msg.dump());
|
||||
} catch (const live::WsError& e) {
|
||||
ws_.reset();
|
||||
throw GoogleError(e.kind() == live::WsError::Kind::TooBig ? 400 : 502,
|
||||
e.kind() == live::WsError::Kind::TooBig ? "INVALID_ARGUMENT" : "CDP_CONNECTION_LOST",
|
||||
e.what(), e.kind() == live::WsError::Kind::TooBig ? "" : kService);
|
||||
}
|
||||
Json reply;
|
||||
if (!pump(timeout_ms, id, &reply))
|
||||
throw GoogleError(504, "DEADLINE_EXCEEDED", "no CDP reply to " + method + " in " +
|
||||
std::to_string(timeout_ms) + " ms", kService);
|
||||
if (const Json* err = reply.find("error"); err && err->is_object()) {
|
||||
const Json* m = err->find("message");
|
||||
throw GoogleError(502, "CDP_ERROR", m && m->is_string() ? m->as_string() : "CDP error", kService);
|
||||
}
|
||||
const Json* result = reply.find("result");
|
||||
return result && result->is_object() ? *result : Json::object();
|
||||
}
|
||||
|
||||
std::string CdpSession::attach_first_page() {
|
||||
Json targets = call("Target.getTargets", Json::object());
|
||||
const Json* infos = targets.find("targetInfos");
|
||||
if (!infos || !infos->is_array())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "Target.getTargets returned no targetInfos", kService);
|
||||
for (const Json& t : infos->as_array()) {
|
||||
const Json* type = t.find("type");
|
||||
const Json* tid = t.find("targetId");
|
||||
if (type && type->is_string() && type->as_string() == "page" && tid && tid->is_string()) {
|
||||
Json p = Json::object();
|
||||
p.set("targetId", Json(tid->as_string()));
|
||||
p.set("flatten", Json(true));
|
||||
Json r = call("Target.attachToTarget", p);
|
||||
const Json* sid = r.find("sessionId");
|
||||
if (!sid || !sid->is_string() || sid->as_string().empty())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "attachToTarget returned no sessionId", kService);
|
||||
validate_session_id(sid->as_string());
|
||||
return sid->as_string();
|
||||
}
|
||||
}
|
||||
throw GoogleError(404, "NOT_FOUND", "the browser has no page target", kService);
|
||||
}
|
||||
|
||||
std::vector<Json> CdpSession::take_events() {
|
||||
std::vector<Json> out(events_.begin(), events_.end());
|
||||
events_.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
Json CdpSession::wait_event(const std::string& method, int timeout_ms) {
|
||||
DataPlane::validate_command(method);
|
||||
if (timeout_ms == 0) timeout_ms = options_.call_timeout_ms;
|
||||
if (timeout_ms < 100 || timeout_ms > SandboxCaps::kMaxCdpCallTimeoutMs)
|
||||
throw GoogleError::invalid("timeout must be 100.." +
|
||||
std::to_string(SandboxCaps::kMaxCdpCallTimeoutMs) + " ms");
|
||||
ensure_connected();
|
||||
long deadline = now_ms() + timeout_ms;
|
||||
for (;;) {
|
||||
for (auto it = events_.begin(); it != events_.end(); ++it) {
|
||||
const Json* m = it->find("method");
|
||||
if (m && m->is_string() && m->as_string() == method) {
|
||||
Json ev = *it;
|
||||
events_.erase(it);
|
||||
return ev;
|
||||
}
|
||||
}
|
||||
long left = deadline - now_ms();
|
||||
if (left <= 0)
|
||||
throw GoogleError(504, "DEADLINE_EXCEEDED", "no CDP event " + method, kService);
|
||||
Json unused;
|
||||
pump(static_cast<int>(left), -1, &unused);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
|
||||
// Chrome DevTools Protocol over the sandbox's CDP WebSocket (computer use).
|
||||
// Connects like the Google SDK's `generate_browser_ws_headers` plus
|
||||
// Playwright's connect_over_cdp: GET /cdp_ws_endpoint on the HTTP data plane
|
||||
// gives a path, the WebSocket URL is the data plane host plus that path, and
|
||||
// the JWT travels in the Sec-WebSocket-Protocol header
|
||||
// ("v1.stream, <jwt>, <routing token>, 9222"). Messages are CDP JSON-RPC
|
||||
// ({"id","method","params"[,"sessionId"]}), see
|
||||
// https://chromedevtools.github.io/devtools-protocol/ (flat session mode).
|
||||
//
|
||||
// Reuses the RFC 6455 client of src/google/live (WsClient, open_stream).
|
||||
// Hard caps: message size, buffered events, call time, reconnects, and a
|
||||
// floor between connection attempts. One thread uses a session at a time.
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "google/live/ws_client.h"
|
||||
#include "google/sandbox/data_plane.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct CdpOptions {
|
||||
int connect_timeout_ms = 0; // 0: SandboxCaps default
|
||||
int call_timeout_ms = 0; // 0: SandboxCaps default
|
||||
std::string browser_port = "9222";
|
||||
};
|
||||
|
||||
class CdpSession {
|
||||
public:
|
||||
CdpSession(std::shared_ptr<DataPlane> plane, CdpOptions options = {});
|
||||
~CdpSession();
|
||||
|
||||
CdpSession(const CdpSession&) = delete;
|
||||
CdpSession& operator=(const CdpSession&) = delete;
|
||||
|
||||
// Resolves the endpoint, mints a JWT and runs the upgrade. Idempotent.
|
||||
void connect();
|
||||
bool connected() const;
|
||||
// Sends a polite close and drops the socket.
|
||||
void close();
|
||||
|
||||
// Sends one CDP command and waits for its reply (matched by id). Returns the
|
||||
// "result" object. A CDP "error" object throws GoogleError 502
|
||||
// CDP_ERROR with Chrome's message. A lost socket before the send is
|
||||
// reconnected (fresh JWT, capped); a loss mid-call throws CDP_CONNECTION_LOST
|
||||
// and the next call reconnects, because commands are never replayed. Session
|
||||
// ids from before a reconnect are void.
|
||||
clients::body::Json call(const std::string& method, const clients::body::Json& params,
|
||||
const std::string& session_id = "", int timeout_ms = 0);
|
||||
|
||||
// Browser-level helper: Target.getTargets, Target.attachToTarget
|
||||
// {flatten:true} for the first "page" target; returns the sessionId.
|
||||
std::string attach_first_page();
|
||||
|
||||
// Events (messages without id) seen while waiting, oldest first; at most
|
||||
// kMaxCdpEvents are kept, older ones are dropped and counted.
|
||||
std::vector<clients::body::Json> take_events();
|
||||
// Waits for an event with this method (already buffered or newly read).
|
||||
clients::body::Json wait_event(const std::string& method, int timeout_ms = 0);
|
||||
|
||||
int connects() const { return connects_; }
|
||||
int reconnects() const { return reconnects_; }
|
||||
std::size_t dropped_events() const { return dropped_events_; }
|
||||
const std::string& endpoint_path() const { return endpoint_path_; }
|
||||
|
||||
static void validate_session_id(const std::string& id);
|
||||
static clients::body::Json request(long id, const std::string& method,
|
||||
const clients::body::Json& params,
|
||||
const std::string& session_id);
|
||||
|
||||
private:
|
||||
void open_socket();
|
||||
void ensure_connected();
|
||||
bool pump(int budget_ms, long wanted_id, clients::body::Json* reply);
|
||||
void push_event(clients::body::Json event);
|
||||
|
||||
std::shared_ptr<DataPlane> plane_;
|
||||
CdpOptions options_;
|
||||
std::unique_ptr<live::WsClient> ws_;
|
||||
std::string endpoint_path_;
|
||||
long next_id_ = 1;
|
||||
int connects_ = 0;
|
||||
int reconnects_ = 0;
|
||||
long last_connect_ms_ = 0;
|
||||
std::deque<clients::body::Json> events_;
|
||||
std::size_t dropped_events_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -1,5 +1,10 @@
|
||||
#include "google/sandbox/chunk.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
#include "google/errors.h"
|
||||
|
||||
#include "google/sandbox/caps.h"
|
||||
#include "google/sandbox/names.h"
|
||||
#include "security/crypto/Base64.h"
|
||||
@@ -30,13 +35,6 @@ void append_capped(std::string& dst, const std::string& add, bool& truncated) {
|
||||
}
|
||||
}
|
||||
|
||||
// Attribute values are "bytes format" (base64). A value that is not valid
|
||||
// base64 is kept as written, so a plain-text server still works.
|
||||
std::string attribute_text(const std::string& wire) {
|
||||
std::string raw;
|
||||
if (!wire.empty() && Base64::decode(wire, raw) && !raw.empty()) return raw;
|
||||
return wire;
|
||||
}
|
||||
|
||||
struct FileBudget {
|
||||
std::size_t total = 0;
|
||||
@@ -123,9 +121,21 @@ Chunk Chunk::from_json(const Json& doc, bool* invalid) {
|
||||
}
|
||||
const Json* meta = doc.is_object() ? doc.find("metadata") : nullptr;
|
||||
const Json* attrs = meta && meta->is_object() ? meta->find("attributes") : nullptr;
|
||||
// Attribute values are "bytes format" (base64): Discovery says byte and the
|
||||
// SDK models them as bytes. A value that is not base64 marks the chunk
|
||||
// invalid, so the decoder skips it loudly instead of guessing.
|
||||
if (attrs && attrs->is_object())
|
||||
for (const auto& [k, v] : attrs->as_object())
|
||||
if (v.is_string()) c.attributes[k] = attribute_text(v.as_string());
|
||||
for (const auto& [k, v] : attrs->as_object()) {
|
||||
if (!v.is_string()) continue;
|
||||
std::string raw;
|
||||
if (!v.as_string().empty() && !Base64::decode(v.as_string(), raw)) {
|
||||
c.data.clear();
|
||||
c.attributes.clear();
|
||||
if (invalid) *invalid = true;
|
||||
return c;
|
||||
}
|
||||
c.attributes[k] = std::move(raw);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -137,6 +147,10 @@ Chunk Chunk::json_payload(const Json& payload) {
|
||||
}
|
||||
|
||||
Json ChunkCodec::request(const std::vector<Chunk>& inputs) {
|
||||
if (inputs.empty()) throw GoogleError::invalid("no input chunks");
|
||||
if (inputs.size() > SandboxCaps::kMaxInputChunks)
|
||||
throw GoogleError::invalid("more than " + std::to_string(SandboxCaps::kMaxInputChunks) +
|
||||
" input chunks");
|
||||
Json arr = Json::array();
|
||||
for (const Chunk& c : inputs) arr.push(c.to_json());
|
||||
Json body = Json::object();
|
||||
@@ -144,37 +158,50 @@ Json ChunkCodec::request(const std::vector<Chunk>& inputs) {
|
||||
return body;
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::code_input(const std::string& code,
|
||||
const std::vector<InputFile>& files) {
|
||||
Json payload = Json::object();
|
||||
payload.set("code", Json(code));
|
||||
if (!files.empty()) {
|
||||
Json arr = Json::array();
|
||||
for (const InputFile& f : files) {
|
||||
Json one = Json::object();
|
||||
one.set("name", Json(f.name));
|
||||
one.set("content", Json(Base64::encode(f.content)));
|
||||
arr.push(one);
|
||||
}
|
||||
payload.set("files", arr);
|
||||
}
|
||||
return Chunk::json_payload(payload);
|
||||
std::string ChunkCodec::guess_mime(const std::string& name) {
|
||||
std::size_t dot = name.rfind('.');
|
||||
std::string ext = dot == std::string::npos ? "" : name.substr(dot + 1);
|
||||
for (char& c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
static const std::map<std::string, const char*> kTypes = {
|
||||
{"txt", "text/plain"}, {"md", "text/markdown"}, {"csv", "text/csv"},
|
||||
{"json", "application/json"}, {"html", "text/html"}, {"xml", "application/xml"},
|
||||
{"py", "text/x-python"}, {"js", "text/javascript"}, {"png", "image/png"},
|
||||
{"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"}, {"gif", "image/gif"},
|
||||
{"svg", "image/svg+xml"}, {"pdf", "application/pdf"}, {"zip", "application/zip"}};
|
||||
auto it = kTypes.find(ext);
|
||||
return it == kTypes.end() ? "application/octet-stream" : it->second;
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::shell_input(const std::string& command, const std::string& cwd,
|
||||
int timeout_seconds) {
|
||||
std::vector<Chunk> ChunkCodec::code_inputs(const std::string& code,
|
||||
const std::vector<InputFile>& files) {
|
||||
Json payload = Json::object();
|
||||
payload.set("code", Json(code));
|
||||
std::vector<Chunk> out;
|
||||
out.push_back(Chunk::json_payload(payload));
|
||||
for (const InputFile& f : files) {
|
||||
Chunk c;
|
||||
c.mime_type = f.mime_type.empty() ? guess_mime(f.name) : f.mime_type;
|
||||
c.data = f.content;
|
||||
c.attributes["file_name"] = f.name;
|
||||
out.push_back(std::move(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<Chunk> ChunkCodec::shell_inputs(const std::string& command,
|
||||
const std::string& cwd, int timeout_seconds,
|
||||
const std::string& port) {
|
||||
Json payload = Json::object();
|
||||
payload.set("command", Json(command));
|
||||
if (!cwd.empty()) payload.set("cwd", Json(cwd));
|
||||
payload.set("timeout", Json(timeout_seconds));
|
||||
return Chunk::json_payload(payload);
|
||||
}
|
||||
|
||||
Chunk ChunkCodec::cdp_input(const std::string& command, const Json& params) {
|
||||
Json payload = Json::object();
|
||||
payload.set("command", Json(command));
|
||||
payload.set("params", params);
|
||||
return Chunk::json_payload(payload);
|
||||
Chunk uri;
|
||||
uri.mime_type = kUriMime;
|
||||
uri.data = "/exec";
|
||||
Chunk prt;
|
||||
prt.mime_type = kPortMime;
|
||||
prt.data = port;
|
||||
return {uri, prt, Chunk::json_payload(payload)};
|
||||
}
|
||||
|
||||
std::vector<Chunk> ChunkCodec::outputs(const Json& reply,
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
// The Chunk wire type of sandboxEnvironments.execute plus the codec between
|
||||
// it and the typed values. Chunk.data and Metadata.attributes values are
|
||||
// base64 on the wire (REST "bytes format"); here they are raw bytes.
|
||||
// base64 on the wire (REST "bytes format", also what the Google SDK sends for
|
||||
// its bytes fields); here they are raw bytes.
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -28,16 +29,24 @@ struct Chunk {
|
||||
|
||||
class ChunkCodec {
|
||||
public:
|
||||
// {"inputs":[chunk,...]}
|
||||
// {"inputs":[chunk,...]}; 1..SandboxCaps::kMaxInputChunks chunks.
|
||||
static clients::body::Json request(const std::vector<Chunk>& inputs);
|
||||
|
||||
// Input chunks. The payload shapes are inferred, see SPEC Unverified.
|
||||
static Chunk code_input(const std::string& code,
|
||||
const std::vector<InputFile>& files);
|
||||
static Chunk shell_input(const std::string& command, const std::string& cwd,
|
||||
int timeout_seconds);
|
||||
static Chunk cdp_input(const std::string& command,
|
||||
const clients::body::Json& params);
|
||||
// Input chunks, as the Google Python SDK builds them (SPEC section 7):
|
||||
// - code: one application/json chunk {"code": ...}, then one chunk per
|
||||
// file (its mime type, raw bytes, attribute file_name).
|
||||
// - shell: three chunks, the request URI "/exec" and the container port
|
||||
// (mime types below) then an application/json {"command","cwd","timeout"}.
|
||||
static std::vector<Chunk> code_inputs(const std::string& code,
|
||||
const std::vector<InputFile>& files);
|
||||
static std::vector<Chunk> shell_inputs(const std::string& command,
|
||||
const std::string& cwd, int timeout_seconds,
|
||||
const std::string& port = "8080");
|
||||
static constexpr const char* kUriMime = "application/x.sandbox-request-uri";
|
||||
static constexpr const char* kPortMime = "application/x.sandbox-request-port";
|
||||
// Extension to media type for the few types sandboxes produce; else
|
||||
// application/octet-stream.
|
||||
static std::string guess_mime(const std::string& file_name);
|
||||
|
||||
// reply["outputs"] to chunks, at most SandboxCaps::kMaxOutputChunks.
|
||||
static std::vector<Chunk> outputs(const clients::body::Json& reply,
|
||||
|
||||
@@ -41,7 +41,7 @@ ExecutionResult CodeExecutor::run(const std::string& sandbox_name,
|
||||
const std::vector<InputFile>& files) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate(code, files);
|
||||
return execute(sandbox_name, {ChunkCodec::code_input(code, files)});
|
||||
return execute(sandbox_name, ChunkCodec::code_inputs(code, files));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -8,8 +8,12 @@ namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
ComputerUse::ComputerUse(std::shared_ptr<GoogleClient> client)
|
||||
: SandboxService(std::move(client)) {}
|
||||
ComputerUse::ComputerUse(std::shared_ptr<GoogleClient> client,
|
||||
std::shared_ptr<DataPlaneConnector> connector)
|
||||
: SandboxService(client),
|
||||
connector_(connector ? std::move(connector)
|
||||
: std::make_shared<DataPlaneConnector>(
|
||||
client, DataPlaneOptions::from_env())) {}
|
||||
|
||||
void ComputerUse::validate_url(const std::string& url) {
|
||||
bool http = url.rfind("http://", 0) == 0;
|
||||
@@ -59,38 +63,52 @@ Json ComputerUse::insert_text_params(const std::string& text) {
|
||||
return p;
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::send(const std::string& sandbox_name, const std::string& command,
|
||||
const Json& params) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
Json reply = execute_raw(sandbox_name, {ChunkCodec::cdp_input(command, params)});
|
||||
Json ComputerUse::screenshot_params() {
|
||||
Json p = Json::object();
|
||||
p.set("format", Json("png"));
|
||||
return p;
|
||||
}
|
||||
|
||||
Json ComputerUse::result_of(const DataPlaneReply& reply) {
|
||||
if (!reply.has_json || !reply.json.is_object()) return Json::object();
|
||||
const Json* r = reply.json.find("result");
|
||||
return r && r->is_object() ? *r : reply.json;
|
||||
}
|
||||
|
||||
std::string ComputerUse::screenshot_of(const Json& result) {
|
||||
// CDP Page.captureScreenshot answers {"data": "<base64 png>"}.
|
||||
const Json* d = result.is_object() ? result.find("data") : nullptr;
|
||||
if (!d || !d->is_string()) return "";
|
||||
if (d->as_string().size() > (SandboxCaps::kMaxScreenshotBytes / 3 + 1) * 4) return "";
|
||||
std::string png;
|
||||
if (!security::crypto::Base64::decode(d->as_string(), png) ||
|
||||
png.size() > SandboxCaps::kMaxScreenshotBytes)
|
||||
return "";
|
||||
return png;
|
||||
}
|
||||
|
||||
namespace {
|
||||
ActionResult run(const DataPlane& plane, const std::string& cdp_method, const Json& params) {
|
||||
DataPlaneReply reply = plane.cdp(cdp_method, params);
|
||||
ActionResult out;
|
||||
out.result = Json::object();
|
||||
out.exec = ChunkCodec::decode(reply);
|
||||
for (const Chunk& c : ChunkCodec::outputs(reply)) {
|
||||
if (c.mime_type == "image/png" && out.screenshot_png.empty() &&
|
||||
c.data.size() <= SandboxCaps::kMaxScreenshotBytes)
|
||||
out.screenshot_png = c.data;
|
||||
if (c.mime_type == "application/json") {
|
||||
try {
|
||||
Json doc = Json::parse(c.data);
|
||||
if (!doc.is_object()) continue;
|
||||
const Json* r = doc.find("result");
|
||||
const Json& obj = r && r->is_object() ? *r : doc;
|
||||
out.result = obj;
|
||||
// CDP Page.captureScreenshot answers {"data": "<base64 png>"}.
|
||||
if (const Json* d = obj.find("data"); d && d->is_string() && out.screenshot_png.empty()) {
|
||||
std::string png;
|
||||
if (d->as_string().size() <= (SandboxCaps::kMaxScreenshotBytes / 3 + 1) * 4 &&
|
||||
security::crypto::Base64::decode(d->as_string(), png) &&
|
||||
png.size() <= SandboxCaps::kMaxScreenshotBytes)
|
||||
out.screenshot_png = std::move(png);
|
||||
}
|
||||
} catch (const clients::body::JsonError&) {
|
||||
}
|
||||
}
|
||||
}
|
||||
out.status = reply.status;
|
||||
out.result = ComputerUse::result_of(reply);
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ActionResult ComputerUse::send(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const Json& params) const {
|
||||
return run(*connector_->open(sandbox_name), cdp_method, params);
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::command(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const Json& params) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_command(cdp_method);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
return send(sandbox_name, cdp_method, params);
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::navigate(const std::string& sandbox_name,
|
||||
const std::string& url) const {
|
||||
@@ -102,8 +120,9 @@ ActionResult ComputerUse::navigate(const std::string& sandbox_name,
|
||||
ActionResult ComputerUse::click(const std::string& sandbox_name, int x, int y) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate_point(x, y);
|
||||
send(sandbox_name, "Input.dispatchMouseEvent", mouse_params("mousePressed", x, y));
|
||||
return send(sandbox_name, "Input.dispatchMouseEvent", mouse_params("mouseReleased", x, y));
|
||||
std::shared_ptr<DataPlane> plane = connector_->open(sandbox_name);
|
||||
run(*plane, "Input.dispatchMouseEvent", mouse_params("mousePressed", x, y));
|
||||
return run(*plane, "Input.dispatchMouseEvent", mouse_params("mouseReleased", x, y));
|
||||
}
|
||||
|
||||
ActionResult ComputerUse::type_text(const std::string& sandbox_name,
|
||||
@@ -115,12 +134,57 @@ ActionResult ComputerUse::type_text(const std::string& sandbox_name,
|
||||
|
||||
ActionResult ComputerUse::screenshot(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
Json p = Json::object();
|
||||
p.set("format", Json("png"));
|
||||
ActionResult r = send(sandbox_name, "Page.captureScreenshot", p);
|
||||
ActionResult r = send(sandbox_name, "Page.captureScreenshot", screenshot_params());
|
||||
r.screenshot_png = screenshot_of(r.result);
|
||||
if (r.screenshot_png.empty())
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "no screenshot in the reply", endpoint_.name);
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "no screenshot in the reply", "sandbox-data-plane");
|
||||
return r;
|
||||
}
|
||||
|
||||
Json ComputerUse::health(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlaneReply r = connector_->open(sandbox_name)->health();
|
||||
Json o = Json::object();
|
||||
o.set("status", Json(r.status));
|
||||
o.set("body", Json(r.body.substr(0, 1024)));
|
||||
return o;
|
||||
}
|
||||
|
||||
namespace {
|
||||
Json reply_json(const DataPlaneReply& r) {
|
||||
Json o = Json::object();
|
||||
o.set("status", Json(r.status));
|
||||
o.set("reply", r.has_json ? r.json : Json(r.body.substr(0, 4096)));
|
||||
return o;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json ComputerUse::list_tabs(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return reply_json(connector_->open(sandbox_name)->list_tabs());
|
||||
}
|
||||
|
||||
Json ComputerUse::create_tab(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return reply_json(connector_->open(sandbox_name)->create_tab());
|
||||
}
|
||||
|
||||
Json ComputerUse::activate_tab(const std::string& sandbox_name, const std::string& tab_id) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_tab_id(tab_id);
|
||||
return reply_json(connector_->open(sandbox_name)->activate_tab(tab_id));
|
||||
}
|
||||
|
||||
Json ComputerUse::close_tab(const std::string& sandbox_name, const std::string& tab_id) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
DataPlane::validate_tab_id(tab_id);
|
||||
return reply_json(connector_->open(sandbox_name)->close_tab(tab_id));
|
||||
}
|
||||
|
||||
std::unique_ptr<CdpSession> ComputerUse::open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
return connector_->open_cdp(sandbox_name, std::move(options));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -1,34 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
// Computer use: browser actions in a computer use sandbox (navigate, click,
|
||||
// type, screenshot). Docs describe the action set and that each action is a
|
||||
// Chrome DevTools Protocol command ({"command","params"}); they send it to
|
||||
// the sandbox data plane with a signed JWT. This class carries the same CDP
|
||||
// commands as an application/json Chunk through sandboxEnvironments.execute,
|
||||
// the only sandbox call in the REST surface. Whether the server routes that
|
||||
// Chunk to the browser is unverified (SPEC Unverified).
|
||||
// type, screenshot) over the sandbox data plane, as Google documents it:
|
||||
// POST /cdp with {"command": "<CDP method>", "params": {...}} and a signed
|
||||
// JWT (see data_plane.h). The commands are Chrome DevTools Protocol names.
|
||||
// Tabs and the CDP WebSocket are exposed through the same connector.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct ActionResult {
|
||||
clients::body::Json result; // The CDP result object ({} when none).
|
||||
ExecutionResult exec; // Text, files and skip reasons of the reply.
|
||||
int status = 0; // HTTP status of the data plane call.
|
||||
std::string screenshot_png; // Raw PNG bytes, set by screenshot() only.
|
||||
};
|
||||
|
||||
class ComputerUse : public SandboxService {
|
||||
public:
|
||||
explicit ComputerUse(std::shared_ptr<GoogleClient> client);
|
||||
// connector null: one built from DataPlaneOptions::from_env().
|
||||
explicit ComputerUse(std::shared_ptr<GoogleClient> client,
|
||||
std::shared_ptr<DataPlaneConnector> connector = nullptr);
|
||||
|
||||
ActionResult navigate(const std::string& sandbox_name, const std::string& url) const;
|
||||
// Left click at viewport coordinates: mousePressed then mouseReleased.
|
||||
ActionResult click(const std::string& sandbox_name, int x, int y) const;
|
||||
ActionResult type_text(const std::string& sandbox_name, const std::string& text) const;
|
||||
ActionResult screenshot(const std::string& sandbox_name) const;
|
||||
// Any CDP command (Domain.method) on the active tab.
|
||||
ActionResult command(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const clients::body::Json& params) const;
|
||||
|
||||
// Data plane routes.
|
||||
clients::body::Json health(const std::string& sandbox_name) const;
|
||||
clients::body::Json list_tabs(const std::string& sandbox_name) const;
|
||||
clients::body::Json create_tab(const std::string& sandbox_name) const;
|
||||
clients::body::Json activate_tab(const std::string& sandbox_name, const std::string& tab_id) const;
|
||||
clients::body::Json close_tab(const std::string& sandbox_name, const std::string& tab_id) const;
|
||||
// The CDP WebSocket (not yet connected; call connect()).
|
||||
std::unique_ptr<CdpSession> open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options = {}) const;
|
||||
|
||||
static void validate_url(const std::string& url); // http(s) only, capped
|
||||
static void validate_point(int x, int y);
|
||||
@@ -38,10 +54,19 @@ class ComputerUse : public SandboxService {
|
||||
static clients::body::Json navigate_params(const std::string& url);
|
||||
static clients::body::Json mouse_params(const char* type, int x, int y);
|
||||
static clients::body::Json insert_text_params(const std::string& text);
|
||||
static clients::body::Json screenshot_params();
|
||||
// Pulls the result object out of a /cdp reply: {"result": {...}} or the
|
||||
// object itself (the reply shape is unverified, SPEC section 7).
|
||||
static clients::body::Json result_of(const DataPlaneReply& reply);
|
||||
// Base64 PNG in "data" of a captureScreenshot result; empty when absent or
|
||||
// over SandboxCaps::kMaxScreenshotBytes.
|
||||
static std::string screenshot_of(const clients::body::Json& result);
|
||||
|
||||
private:
|
||||
ActionResult send(const std::string& sandbox_name, const std::string& command,
|
||||
ActionResult send(const std::string& sandbox_name, const std::string& cdp_method,
|
||||
const clients::body::Json& params) const;
|
||||
|
||||
std::shared_ptr<DataPlaneConnector> connector_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
#include "google/sandbox/data_plane.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
#include "clients/transports/http_transport.h"
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kService = "sandbox-data-plane";
|
||||
|
||||
bool plain_host_char(char c) {
|
||||
return std::isalnum(static_cast<unsigned char>(c)) || c == '.' || c == '-';
|
||||
}
|
||||
|
||||
bool loopback_host(const std::string& host) {
|
||||
return host == "localhost" || host.rfind("127.", 0) == 0;
|
||||
}
|
||||
|
||||
clients::ClientConfig make_config(int timeout_ms) {
|
||||
clients::ClientConfig config;
|
||||
config.persist_cookies = false;
|
||||
config.follow_redirects = false;
|
||||
config.proxy_from_env = false;
|
||||
config.retry.max_attempts(1);
|
||||
config.timeouts.total = std::chrono::milliseconds(timeout_ms);
|
||||
config.timeouts.read = std::chrono::milliseconds(timeout_ms);
|
||||
return config;
|
||||
}
|
||||
|
||||
std::string excerpt(const std::string& body) {
|
||||
std::string out;
|
||||
for (char c : body.substr(0, SandboxCaps::kMaxErrorExcerpt))
|
||||
out.push_back(static_cast<unsigned char>(c) < 0x20 && c != '\n' ? ' ' : c);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void DataPlaneTarget::validate_port(const std::string& port) {
|
||||
if (port.empty() || port.size() > 5) throw GoogleError::invalid("port must be 1..65535");
|
||||
int v = 0;
|
||||
for (char c : port) {
|
||||
if (!std::isdigit(static_cast<unsigned char>(c))) throw GoogleError::invalid("port must be 1..65535");
|
||||
v = v * 10 + (c - '0');
|
||||
}
|
||||
if (v < 1 || v > 65535) throw GoogleError::invalid("port must be 1..65535");
|
||||
}
|
||||
|
||||
void DataPlaneTarget::validate_host(const std::string& host) {
|
||||
if (host.empty() || host.size() > SandboxCaps::kMaxHostChars)
|
||||
throw GoogleError::invalid("data plane host is empty or too long");
|
||||
for (char c : host)
|
||||
if (!plain_host_char(c)) throw GoogleError::invalid("data plane host has an invalid character");
|
||||
}
|
||||
|
||||
DataPlaneTarget DataPlaneTarget::from_environment(const SandboxEnvironment& env,
|
||||
const std::string& psc_endpoint,
|
||||
const std::string& loopback_override,
|
||||
const std::string& port) {
|
||||
validate_port(port);
|
||||
DataPlaneTarget t;
|
||||
t.port = port;
|
||||
const std::string& token = env.routing_token;
|
||||
if (token.empty()) throw GoogleError::invalid("the sandbox has no routing token (is it running?)");
|
||||
if (token.size() > SandboxCaps::kMaxRoutingTokenChars)
|
||||
throw GoogleError::invalid("routing token too long");
|
||||
for (char c : token)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f || c == ',')
|
||||
throw GoogleError::invalid("routing token has an invalid character");
|
||||
t.routing_token = token;
|
||||
|
||||
if (!loopback_override.empty()) {
|
||||
std::size_t colon = loopback_override.rfind(':');
|
||||
std::string host = loopback_override.substr(0, colon);
|
||||
if (colon == std::string::npos || !loopback_host(host))
|
||||
throw GoogleError::invalid("loopback override must be localhost:PORT or 127.x.x.x:PORT");
|
||||
validate_host(host);
|
||||
validate_port(loopback_override.substr(colon + 1));
|
||||
t.http_base = "http://" + loopback_override;
|
||||
t.ws_base = "ws://" + loopback_override;
|
||||
return t;
|
||||
}
|
||||
if (!psc_endpoint.empty()) {
|
||||
validate_host(psc_endpoint);
|
||||
t.http_base = "https://" + psc_endpoint;
|
||||
t.ws_base = "wss://" + psc_endpoint;
|
||||
} else if (!env.load_balancer_hostname.empty()) {
|
||||
validate_host(env.load_balancer_hostname);
|
||||
t.http_base = "https://" + env.load_balancer_hostname;
|
||||
t.ws_base = "wss://" + env.load_balancer_hostname;
|
||||
} else if (!env.load_balancer_ip.empty()) {
|
||||
validate_host(env.load_balancer_ip);
|
||||
t.http_base = "http://" + env.load_balancer_ip;
|
||||
t.ws_base = "ws://" + env.load_balancer_ip;
|
||||
} else {
|
||||
throw GoogleError::invalid(
|
||||
"no data plane address: connectionInfo has no loadBalancerHostname or loadBalancerIp "
|
||||
"(VPC-SC sandboxes need a psc_endpoint for connectionInfo.serviceAttachment)");
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
DataPlane::DataPlane(DataPlaneTarget target, std::shared_ptr<ITokenSource> tokens,
|
||||
std::unique_ptr<clients::Transport> transport, int timeout_ms)
|
||||
: target_(std::move(target)),
|
||||
tokens_(std::move(tokens)),
|
||||
client_(transport ? std::move(transport)
|
||||
: std::make_unique<clients::transports::HttpTransport>(),
|
||||
make_config(timeout_ms > 0 ? timeout_ms : SandboxCaps::kDataPlaneTimeoutMs)),
|
||||
timeout_ms_(timeout_ms > 0 ? timeout_ms : SandboxCaps::kDataPlaneTimeoutMs) {
|
||||
if (!tokens_) throw GoogleError::invalid("a token source is required");
|
||||
if (timeout_ms_ > SandboxCaps::kMaxDataPlaneTimeoutMs)
|
||||
throw GoogleError::invalid("timeout over " +
|
||||
std::to_string(SandboxCaps::kMaxDataPlaneTimeoutMs) + " ms");
|
||||
}
|
||||
|
||||
void DataPlane::validate_path(const std::string& path) {
|
||||
if (path.empty() || path[0] != '/' || path.size() > SandboxCaps::kMaxDataPathChars)
|
||||
throw GoogleError::invalid("data plane path must start with / and stay under " +
|
||||
std::to_string(SandboxCaps::kMaxDataPathChars) + " chars");
|
||||
for (char c : path)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f)
|
||||
throw GoogleError::invalid("data plane path has an invalid character");
|
||||
if (path.find("..") != std::string::npos) throw GoogleError::invalid("data plane path has ..");
|
||||
}
|
||||
|
||||
void DataPlane::validate_tab_id(const std::string& id) {
|
||||
if (id.empty() || id.size() > SandboxCaps::kMaxTabIdChars || !valid_segment(id))
|
||||
throw GoogleError::invalid("tab id must be 1.." + std::to_string(SandboxCaps::kMaxTabIdChars) +
|
||||
" of [A-Za-z0-9._-]");
|
||||
}
|
||||
|
||||
void DataPlane::validate_command(const std::string& command) {
|
||||
// Chrome DevTools Protocol method: Domain.method.
|
||||
std::size_t dot = command.find('.');
|
||||
bool ok = !command.empty() && command.size() <= SandboxCaps::kMaxCdpMethodChars &&
|
||||
dot != std::string::npos && dot > 0 && dot + 1 < command.size() &&
|
||||
command.find('.', dot + 1) == std::string::npos;
|
||||
for (char c : command)
|
||||
if (c != '.' && !std::isalnum(static_cast<unsigned char>(c))) ok = false;
|
||||
if (!ok) throw GoogleError::invalid("CDP command must look like Domain.method");
|
||||
}
|
||||
|
||||
Json DataPlane::cdp_body(const std::string& command, const Json& params) {
|
||||
validate_command(command);
|
||||
if (!params.is_object()) throw GoogleError::invalid("CDP params must be a JSON object");
|
||||
Json b = Json::object();
|
||||
b.set("command", Json(command));
|
||||
b.set("params", params);
|
||||
return b;
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::send(clients::Method method, const std::string& path,
|
||||
const Json* body) const {
|
||||
validate_path(path);
|
||||
for (int attempt = 0;; ++attempt) {
|
||||
std::string jwt = tokens_->token();
|
||||
DataPlaneReply out;
|
||||
clients::ClientResponse response;
|
||||
try {
|
||||
clients::ClientRequest req(method, clients::Url::parse(target_.http_base + path));
|
||||
req.header("Authorization", "Bearer " + jwt);
|
||||
req.header("X-Sandbox-Routing-Token", target_.routing_token);
|
||||
req.header("X-Sandbox-Port", target_.port);
|
||||
if (body) req.json(*body);
|
||||
++requests_;
|
||||
response = client_.send_blocking(std::move(req));
|
||||
} catch (const GoogleError&) {
|
||||
throw;
|
||||
} catch (const std::exception& e) {
|
||||
throw GoogleError(502, "UNAVAILABLE", e.what(), kService);
|
||||
}
|
||||
if (response.status() == 401 && attempt < SandboxCaps::kMaxAuthRetries) {
|
||||
tokens_->invalidate();
|
||||
++refreshes_;
|
||||
continue;
|
||||
}
|
||||
if (response.body().size() > SandboxCaps::kMaxDataReplyBytes)
|
||||
throw GoogleError(502, "TOO_BIG",
|
||||
"data plane reply over " + std::to_string(SandboxCaps::kMaxDataReplyBytes) +
|
||||
" bytes",
|
||||
kService);
|
||||
if (!response.is_success())
|
||||
throw GoogleError(response.status() >= 400 && response.status() < 600 ? response.status() : 502,
|
||||
"DATA_PLANE_" + std::to_string(response.status()), excerpt(response.body()),
|
||||
kService);
|
||||
out.status = response.status();
|
||||
out.body = response.body();
|
||||
try {
|
||||
out.json = Json::parse(out.body);
|
||||
out.has_json = true;
|
||||
} catch (const clients::body::JsonError&) {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::health() const { return send(clients::Method::kGet, "/", nullptr); }
|
||||
DataPlaneReply DataPlane::create_tab() const { return send(clients::Method::kPost, "/tabs", nullptr); }
|
||||
DataPlaneReply DataPlane::list_tabs() const { return send(clients::Method::kGet, "/tabs", nullptr); }
|
||||
|
||||
DataPlaneReply DataPlane::activate_tab(const std::string& tab_id) const {
|
||||
validate_tab_id(tab_id);
|
||||
return send(clients::Method::kPost, "/tabs/" + tab_id + "/activate", nullptr);
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::close_tab(const std::string& tab_id) const {
|
||||
validate_tab_id(tab_id);
|
||||
return send(clients::Method::kDelete, "/tabs/" + tab_id, nullptr);
|
||||
}
|
||||
|
||||
std::string DataPlane::cdp_ws_endpoint() const {
|
||||
DataPlaneReply r = send(clients::Method::kGet, "/cdp_ws_endpoint", nullptr);
|
||||
const Json* e = r.has_json && r.json.is_object() ? r.json.find("endpoint") : nullptr;
|
||||
if (!e || !e->is_string() || e->as_string().empty())
|
||||
throw GoogleError(502, "BAD_RESPONSE", "cdp_ws_endpoint reply has no endpoint", kService);
|
||||
return e->as_string();
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::cdp(const std::string& command, const Json& params) const {
|
||||
Json b = cdp_body(command, params);
|
||||
return send(clients::Method::kPost, "/cdp", &b);
|
||||
}
|
||||
|
||||
DataPlaneReply DataPlane::cdps(const std::vector<CdpCommand>& commands) const {
|
||||
if (commands.empty() || commands.size() > SandboxCaps::kMaxBatchCommands)
|
||||
throw GoogleError::invalid("commands must be 1.." +
|
||||
std::to_string(SandboxCaps::kMaxBatchCommands));
|
||||
Json arr = Json::array();
|
||||
for (const CdpCommand& c : commands) arr.push(cdp_body(c.command, c.params));
|
||||
Json b = Json::object();
|
||||
b.set("commands", arr);
|
||||
return send(clients::Method::kPost, "/cdps", &b);
|
||||
}
|
||||
|
||||
std::string DataPlane::ws_url(const std::string& endpoint_path) const {
|
||||
if (endpoint_path.empty() || endpoint_path.size() > 512)
|
||||
throw GoogleError::invalid("websocket endpoint path is empty or too long");
|
||||
for (char c : endpoint_path)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f)
|
||||
throw GoogleError::invalid("websocket endpoint path has an invalid character");
|
||||
if (endpoint_path.find("..") != std::string::npos || endpoint_path.find("://") != std::string::npos)
|
||||
throw GoogleError::invalid("websocket endpoint path must be a plain path");
|
||||
std::string p = endpoint_path[0] == '/' ? endpoint_path.substr(1) : endpoint_path;
|
||||
return target_.ws_base + "/" + p;
|
||||
}
|
||||
|
||||
live::WsHeaders DataPlane::ws_headers(const std::string& browser_port) const {
|
||||
DataPlaneTarget::validate_port(browser_port);
|
||||
std::string jwt = tokens_->token();
|
||||
for (char c : jwt)
|
||||
if (static_cast<unsigned char>(c) <= 0x20 || static_cast<unsigned char>(c) >= 0x7f || c == ',')
|
||||
throw GoogleError::invalid("token has an invalid character for a header");
|
||||
live::WsHeaders h;
|
||||
h.emplace_back("Sec-WebSocket-Protocol",
|
||||
"v1.stream, " + jwt + ", " + target_.routing_token + ", " + browser_port);
|
||||
return h;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
// The sandbox data plane: the HTTP proxy in front of a running sandbox, as
|
||||
// the Google Python SDK (`Sandboxes.send_command`, google-cloud-aiplatform
|
||||
// 2.6.0) and the computer use quickstart document it. Not in Discovery.
|
||||
//
|
||||
// address https://{connectionInfo.loadBalancerHostname} (or
|
||||
// http://{connectionInfo.loadBalancerIp}, or https://{psc_endpoint}
|
||||
// for VPC-SC sandboxes)
|
||||
// headers Authorization: Bearer <signed JWT>
|
||||
// X-Sandbox-Routing-Token: <connectionInfo.routingToken>
|
||||
// X-Sandbox-Port: <container port, default 8080>
|
||||
// routes GET / POST /tabs GET /tabs POST /tabs/{id}/activate
|
||||
// DELETE /tabs/{id} GET /cdp_ws_endpoint POST /cdp POST /cdps
|
||||
//
|
||||
// Every call is capped (reply size, time, one JWT refresh on 401). A JWT is
|
||||
// sent only to the address derived from the sandbox's own connectionInfo.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "clients/client.h"
|
||||
#include "google/live/ws_handshake.h"
|
||||
#include "google/sandbox/jwt.h"
|
||||
#include "google/sandbox/types.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct DataPlaneTarget {
|
||||
std::string http_base; // scheme://host[:port], no trailing slash
|
||||
std::string ws_base; // ws(s)://host[:port]
|
||||
std::string routing_token;
|
||||
std::string port = "8080"; // X-Sandbox-Port (one of the template's ports)
|
||||
|
||||
// Throws GoogleError::invalid when the environment has no usable address or
|
||||
// routing token. `psc_endpoint` wins (VPC-SC), then the hostname (https),
|
||||
// then the IP (http, as the SDK does). `loopback_override` ("127.0.0.1:PORT"
|
||||
// or "localhost:PORT") replaces the address for local test servers and is
|
||||
// refused for any non-loopback host.
|
||||
static DataPlaneTarget from_environment(const SandboxEnvironment& env,
|
||||
const std::string& psc_endpoint = "",
|
||||
const std::string& loopback_override = "",
|
||||
const std::string& port = "8080");
|
||||
static void validate_port(const std::string& port);
|
||||
static void validate_host(const std::string& host);
|
||||
};
|
||||
|
||||
struct DataPlaneReply {
|
||||
int status = 0;
|
||||
std::string body;
|
||||
clients::body::Json json; // Parsed body; null when the body is not JSON.
|
||||
bool has_json = false;
|
||||
};
|
||||
|
||||
struct CdpCommand {
|
||||
std::string command; // e.g. "Page.navigate"
|
||||
clients::body::Json params = clients::body::Json::object();
|
||||
};
|
||||
|
||||
class DataPlane {
|
||||
public:
|
||||
// transport null: the real HTTP(S) transport. Redirects and retries are off
|
||||
// (a JWT must not follow a redirect; CDP commands are not idempotent).
|
||||
DataPlane(DataPlaneTarget target, std::shared_ptr<ITokenSource> tokens,
|
||||
std::unique_ptr<clients::Transport> transport = nullptr, int timeout_ms = 0);
|
||||
|
||||
// GET /
|
||||
DataPlaneReply health() const;
|
||||
// POST /tabs (no body), GET /tabs
|
||||
DataPlaneReply create_tab() const;
|
||||
DataPlaneReply list_tabs() const;
|
||||
// POST /tabs/{id}/activate, DELETE /tabs/{id}
|
||||
DataPlaneReply activate_tab(const std::string& tab_id) const;
|
||||
DataPlaneReply close_tab(const std::string& tab_id) const;
|
||||
// GET /cdp_ws_endpoint -> the "endpoint" path string of the reply.
|
||||
std::string cdp_ws_endpoint() const;
|
||||
// POST /cdp {"command","params"}: one command on the active tab.
|
||||
DataPlaneReply cdp(const std::string& command, const clients::body::Json& params) const;
|
||||
// POST /cdps: several commands on the active tab (body shape unverified).
|
||||
DataPlaneReply cdps(const std::vector<CdpCommand>& commands) const;
|
||||
|
||||
// The URL and upgrade headers of the CDP WebSocket for `endpoint_path`.
|
||||
std::string ws_url(const std::string& endpoint_path) const;
|
||||
// Sec-WebSocket-Protocol: "v1.stream, <jwt>, <routing token>, 9222".
|
||||
live::WsHeaders ws_headers(const std::string& browser_port = "9222") const;
|
||||
|
||||
const DataPlaneTarget& target() const { return target_; }
|
||||
ITokenSource& tokens() const { return *tokens_; }
|
||||
|
||||
static void validate_path(const std::string& path);
|
||||
static void validate_tab_id(const std::string& id);
|
||||
static clients::body::Json cdp_body(const std::string& command,
|
||||
const clients::body::Json& params);
|
||||
static void validate_command(const std::string& command);
|
||||
|
||||
int requests_sent() const { return requests_; }
|
||||
int auth_refreshes() const { return refreshes_; }
|
||||
|
||||
private:
|
||||
DataPlaneReply send(clients::Method method, const std::string& path,
|
||||
const clients::body::Json* body) const;
|
||||
|
||||
DataPlaneTarget target_;
|
||||
std::shared_ptr<ITokenSource> tokens_;
|
||||
mutable clients::Client client_;
|
||||
int timeout_ms_;
|
||||
mutable int requests_ = 0;
|
||||
mutable int refreshes_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "google/sandbox/data_plane_connector.h"
|
||||
|
||||
#include "google/credentials.h"
|
||||
#include "google/errors.h"
|
||||
#include "google/sandbox/sandboxes.h"
|
||||
#include "google/service_account.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
DataPlaneOptions DataPlaneOptions::from_env() {
|
||||
DataPlaneOptions o;
|
||||
o.service_account_email = Credentials::env("GOOGLE_SERVICE_ACCOUNT_EMAIL");
|
||||
if (o.service_account_email.empty()) {
|
||||
std::string path = Credentials::env("GOOGLE_APPLICATION_CREDENTIALS");
|
||||
if (!path.empty()) {
|
||||
try {
|
||||
o.service_account_email = ServiceAccountKey::load_file(path).client_email;
|
||||
} catch (const std::exception&) {
|
||||
// No usable key file: the data plane stays unavailable, loudly, at use.
|
||||
}
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
DataPlaneConnector::DataPlaneConnector(std::shared_ptr<GoogleClient> client,
|
||||
DataPlaneOptions options,
|
||||
std::shared_ptr<ITokenSource> tokens)
|
||||
: SandboxService(client), options_(std::move(options)), tokens_(std::move(tokens)) {
|
||||
if (!tokens_ && !options_.service_account_email.empty())
|
||||
tokens_ = std::make_shared<IamJwtSource>(client, options_.service_account_email,
|
||||
options_.jwt_ttl_seconds);
|
||||
}
|
||||
|
||||
SandboxEnvironment DataPlaneConnector::environment(const std::string& sandbox_name) const {
|
||||
return Sandboxes(client_).get(sandbox_name);
|
||||
}
|
||||
|
||||
std::shared_ptr<DataPlane> DataPlaneConnector::open(const std::string& sandbox_name) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
if (!client_->configured()) throw NotConfiguredError("sandbox-data-plane");
|
||||
// No signer is a configuration gap, not bad input: 503 like a missing credential.
|
||||
if (!tokens_) throw NotConfiguredError("sandbox-data-plane (set GOOGLE_SERVICE_ACCOUNT_EMAIL)");
|
||||
SandboxEnvironment env = environment(sandbox_name);
|
||||
if (!env.running())
|
||||
throw GoogleError(409, "FAILED_PRECONDITION",
|
||||
"sandbox is " + (env.state.empty() ? std::string("in an unknown state") : env.state) +
|
||||
", not STATE_RUNNING",
|
||||
"sandbox-data-plane");
|
||||
DataPlaneTarget target = DataPlaneTarget::from_environment(
|
||||
env, options_.psc_endpoint, options_.loopback_override, options_.port);
|
||||
return std::make_shared<DataPlane>(std::move(target), tokens_, nullptr, options_.timeout_ms);
|
||||
}
|
||||
|
||||
std::unique_ptr<CdpSession> DataPlaneConnector::open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options) const {
|
||||
return std::make_unique<CdpSession>(open(sandbox_name), std::move(options));
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
// Opens the data plane of a named sandbox: reads the sandbox resource (its
|
||||
// connectionInfo) through the control plane, derives the address, and signs
|
||||
// JWTs with a service account. One connector per service account.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "google/client.h"
|
||||
#include "google/sandbox/cdp_session.h"
|
||||
#include "google/sandbox/data_plane.h"
|
||||
#include "google/sandbox/jwt.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
struct DataPlaneOptions {
|
||||
// Signs the JWTs. Empty: the data plane is unavailable (calls throw 400).
|
||||
std::string service_account_email;
|
||||
std::string psc_endpoint; // VPC-SC sandboxes only
|
||||
std::string loopback_override; // tests: "127.0.0.1:PORT"
|
||||
std::string port = "8080";
|
||||
long jwt_ttl_seconds = 3600;
|
||||
int timeout_ms = 0;
|
||||
|
||||
// GOOGLE_SERVICE_ACCOUNT_EMAIL, else client_email of the key file named by
|
||||
// GOOGLE_APPLICATION_CREDENTIALS (only the email is read, never the key).
|
||||
static DataPlaneOptions from_env();
|
||||
};
|
||||
|
||||
class DataPlaneConnector : public SandboxService {
|
||||
public:
|
||||
// tokens null: IamJwtSource over `client` for options.service_account_email.
|
||||
DataPlaneConnector(std::shared_ptr<GoogleClient> client, DataPlaneOptions options = {},
|
||||
std::shared_ptr<ITokenSource> tokens = nullptr);
|
||||
|
||||
// Order: validate the name, require a credential (503), require a signer
|
||||
// (400), then read the sandbox. Nothing leaves the process before that.
|
||||
std::shared_ptr<DataPlane> open(const std::string& sandbox_name) const;
|
||||
std::unique_ptr<CdpSession> open_cdp(const std::string& sandbox_name,
|
||||
CdpOptions options = {}) const;
|
||||
|
||||
const DataPlaneOptions& options() const { return options_; }
|
||||
|
||||
protected:
|
||||
// The sandbox resource for the name. Virtual so a test can supply a local
|
||||
// environment pointing at a loopback server.
|
||||
virtual SandboxEnvironment environment(const std::string& sandbox_name) const;
|
||||
|
||||
private:
|
||||
DataPlaneOptions options_;
|
||||
std::shared_ptr<ITokenSource> tokens_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "google/sandbox/jwt.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
namespace {
|
||||
long wall_clock() {
|
||||
return static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
IamJwtSource::IamJwtSource(std::shared_ptr<GoogleClient> client,
|
||||
std::string service_account_email, long ttl_seconds, Clock clock,
|
||||
ServiceEndpoint endpoint)
|
||||
: client_(std::move(client)),
|
||||
email_(std::move(service_account_email)),
|
||||
ttl_(ttl_seconds),
|
||||
clock_(clock ? std::move(clock) : Clock(wall_clock)),
|
||||
endpoint_(std::move(endpoint)) {
|
||||
validate_email(email_);
|
||||
if (ttl_ < SandboxCaps::kMinJwtTtlSeconds || ttl_ > SandboxCaps::kMaxJwtTtlSeconds)
|
||||
throw GoogleError::invalid("jwt ttl must be " + std::to_string(SandboxCaps::kMinJwtTtlSeconds) +
|
||||
".." + std::to_string(SandboxCaps::kMaxJwtTtlSeconds) + " seconds");
|
||||
}
|
||||
|
||||
void IamJwtSource::validate_email(const std::string& email) {
|
||||
std::size_t at = email.find('@');
|
||||
if (email.empty() || email.size() > SandboxCaps::kMaxEmailChars || at == std::string::npos ||
|
||||
at == 0 || at + 1 >= email.size() || email.find('@', at + 1) != std::string::npos)
|
||||
throw GoogleError::invalid(
|
||||
"service account email must look like name@project.iam.gserviceaccount.com");
|
||||
for (char c : email)
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '.' || c == '-' || c == '_' || c == '@'))
|
||||
throw GoogleError::invalid("service account email has an invalid character");
|
||||
}
|
||||
|
||||
Json IamJwtSource::claims(const std::string& email, long issued_at, long ttl, long nonce) {
|
||||
Json c = Json::object();
|
||||
c.set("iat", Json(issued_at));
|
||||
c.set("exp", Json(issued_at + ttl));
|
||||
c.set("iss", Json(email));
|
||||
c.set("sub", Json(email));
|
||||
c.set("nonce", Json(nonce));
|
||||
c.set("aud", Json("https://aiplatform.googleapis.com/"));
|
||||
return c;
|
||||
}
|
||||
|
||||
std::string IamJwtSource::sign_path(const std::string& email) {
|
||||
return "/projects/-/serviceAccounts/" + email + ":signJwt";
|
||||
}
|
||||
|
||||
ServiceEndpoint IamJwtSource::iam_endpoint() {
|
||||
return endpoints::custom("iamcredentials", "https://iamcredentials.googleapis.com/v1");
|
||||
}
|
||||
|
||||
std::string IamJwtSource::token() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
long now = clock_();
|
||||
if (!jwt_.empty() && expires_at_ - now > SandboxCaps::kJwtRefreshMarginSeconds) return jwt_;
|
||||
static thread_local std::mt19937 rng{std::random_device{}()};
|
||||
long nonce = static_cast<long>(rng() % 1000000000u) + 1;
|
||||
Json body = Json::object();
|
||||
body.set("payload", Json(claims(email_, now, ttl_, nonce).dump()));
|
||||
Json reply = client_->request(clients::Method::kPost, endpoint_, sign_path(email_), &body);
|
||||
const Json* signed_jwt = reply.is_object() ? reply.find("signedJwt") : nullptr;
|
||||
if (!signed_jwt || !signed_jwt->is_string() || signed_jwt->as_string().empty())
|
||||
throw GoogleError(502, "EMPTY_RESPONSE", "signJwt returned no signedJwt", "iamcredentials");
|
||||
jwt_ = signed_jwt->as_string();
|
||||
expires_at_ = now + ttl_;
|
||||
++mints_;
|
||||
return jwt_;
|
||||
}
|
||||
|
||||
void IamJwtSource::invalidate() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
jwt_.clear();
|
||||
expires_at_ = 0;
|
||||
}
|
||||
|
||||
int IamJwtSource::mints() const {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return mints_;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
// Sandbox data plane tokens. Google's SDK (`generate_access_token`) asks the
|
||||
// IAM Credentials API to sign a small JWT with a service account
|
||||
// (projects.serviceAccounts.signJwt) and sends that JWT to the sandbox proxy.
|
||||
// The audience is "https://aiplatform.googleapis.com/". The caller needs
|
||||
// roles/iam.serviceAccountTokenCreator on the service account.
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "clients/body/json.h"
|
||||
#include "google/client.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
// Where a data plane JWT comes from. Production: IamJwtSource. Tests and
|
||||
// custom signers implement it directly.
|
||||
class ITokenSource {
|
||||
public:
|
||||
virtual ~ITokenSource() = default;
|
||||
virtual std::string token() = 0; // Valid for at least the refresh margin.
|
||||
virtual void invalidate() = 0; // The next token() mints a fresh JWT.
|
||||
};
|
||||
|
||||
class IamJwtSource : public ITokenSource {
|
||||
public:
|
||||
using Clock = std::function<long()>; // Seconds since the epoch.
|
||||
|
||||
// ttl: kMinJwtTtlSeconds..kMaxJwtTtlSeconds. Throws GoogleError::invalid.
|
||||
IamJwtSource(std::shared_ptr<GoogleClient> client, std::string service_account_email,
|
||||
long ttl_seconds = 3600, Clock clock = nullptr,
|
||||
ServiceEndpoint endpoint = iam_endpoint());
|
||||
|
||||
std::string token() override;
|
||||
void invalidate() override;
|
||||
|
||||
int mints() const; // How many signJwt calls were made.
|
||||
|
||||
static void validate_email(const std::string& email);
|
||||
// The JWT payload exactly as the SDK builds it (iat, exp, iss, sub, nonce, aud).
|
||||
static clients::body::Json claims(const std::string& email, long issued_at, long ttl,
|
||||
long nonce);
|
||||
// POST path under the IAM Credentials endpoint.
|
||||
static std::string sign_path(const std::string& email);
|
||||
static ServiceEndpoint iam_endpoint();
|
||||
|
||||
private:
|
||||
std::shared_ptr<GoogleClient> client_;
|
||||
std::string email_;
|
||||
long ttl_;
|
||||
Clock clock_;
|
||||
ServiceEndpoint endpoint_;
|
||||
mutable std::mutex mu_;
|
||||
std::string jwt_;
|
||||
long expires_at_ = 0;
|
||||
int mints_ = 0;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -132,6 +132,31 @@ SandboxNames::Parts SandboxNames::parse_sandbox(const std::string& name) {
|
||||
return p;
|
||||
}
|
||||
|
||||
SandboxNames::OperationParts SandboxNames::parse_operation_parent(const std::string& resource) {
|
||||
OperationParts o;
|
||||
Parts p;
|
||||
o.resource = resource;
|
||||
if (parse(resource, 8, p)) o.family = "sandboxEnvironments";
|
||||
else if (parse(resource, 8, p, "sandboxEnvironmentTemplates")) o.family = "sandboxEnvironmentTemplates";
|
||||
else if (parse(resource, 8, p, "sandboxEnvironmentSnapshots")) o.family = "sandboxEnvironmentSnapshots";
|
||||
else
|
||||
throw GoogleError::invalid(
|
||||
"resource must be a sandbox, template or snapshot name "
|
||||
"(projects/{p}/locations/{l}/reasoningEngines/{e}/{collection}/{id})");
|
||||
o.location = p.location;
|
||||
return o;
|
||||
}
|
||||
|
||||
SandboxNames::OperationParts SandboxNames::parse_operation(const std::string& name) {
|
||||
std::size_t at = name.rfind("/operations/");
|
||||
if (at == std::string::npos)
|
||||
throw GoogleError::invalid("operation must look like {sandbox|template|snapshot}/operations/{id}");
|
||||
OperationParts o = parse_operation_parent(name.substr(0, at));
|
||||
o.id = name.substr(at + 12);
|
||||
if (!valid_segment(o.id)) throw GoogleError::invalid("operation id must be 1..128 of [A-Za-z0-9._-]");
|
||||
return o;
|
||||
}
|
||||
|
||||
std::string SandboxNames::engine_of(const std::string& sandbox_name) {
|
||||
Parts p = parse_sandbox(sandbox_name);
|
||||
return "projects/" + p.project + "/locations/" + p.location +
|
||||
|
||||
@@ -31,6 +31,18 @@ struct SandboxNames {
|
||||
static Parts parse_template(const std::string& name);
|
||||
static Parts parse_snapshot(const std::string& name);
|
||||
|
||||
// Long-running operation of a sandbox, template or snapshot:
|
||||
// {resource}/operations/{id}.
|
||||
struct OperationParts {
|
||||
std::string resource; // The sandbox, template or snapshot name.
|
||||
std::string family; // sandboxEnvironments, sandboxEnvironmentTemplates, ...Snapshots
|
||||
std::string location;
|
||||
std::string id;
|
||||
};
|
||||
static OperationParts parse_operation(const std::string& name);
|
||||
// Any of the three resource names (the parent of an operations list).
|
||||
static OperationParts parse_operation_parent(const std::string& resource);
|
||||
|
||||
// "projects/p/locations/l/reasoningEngines/e" from a sandbox name.
|
||||
static std::string engine_of(const std::string& sandbox_name);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "google/sandbox/operations.h"
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/sandbox/caps.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
using clients::body::Json;
|
||||
|
||||
Operations::Operations(std::shared_ptr<GoogleClient> client)
|
||||
: SandboxService(std::move(client)) {}
|
||||
|
||||
std::string Operations::wait_query(int timeout_seconds) {
|
||||
if (timeout_seconds < 1 || timeout_seconds > SandboxCaps::kMaxOperationWaitSeconds)
|
||||
throw GoogleError::invalid("timeout must be 1.." +
|
||||
std::to_string(SandboxCaps::kMaxOperationWaitSeconds) + " seconds");
|
||||
return "?timeout=" + std::to_string(timeout_seconds) + "s";
|
||||
}
|
||||
|
||||
Operation Operations::get(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
return Operation::from_json(
|
||||
client_->get_json(endpoint_in(parts.location), "/" + operation_name));
|
||||
}
|
||||
|
||||
Operation Operations::wait(const std::string& operation_name, int timeout_seconds) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
std::string query = wait_query(timeout_seconds);
|
||||
Json body = Json::object();
|
||||
return Operation::from_json(client_->request(clients::Method::kPost, endpoint_in(parts.location),
|
||||
"/" + operation_name + ":wait" + query, &body));
|
||||
}
|
||||
|
||||
void Operations::cancel(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
Json body = Json::object();
|
||||
client_->request(clients::Method::kPost, endpoint_in(parts.location),
|
||||
"/" + operation_name + ":cancel", &body);
|
||||
}
|
||||
|
||||
void Operations::remove(const std::string& operation_name) const {
|
||||
auto parts = SandboxNames::parse_operation(operation_name);
|
||||
client_->delete_json(endpoint_in(parts.location), "/" + operation_name);
|
||||
}
|
||||
|
||||
std::vector<Operation> Operations::list(const std::string& resource_name, int page_size) const {
|
||||
auto parts = SandboxNames::parse_operation_parent(resource_name);
|
||||
if (page_size == 0) page_size = SandboxCaps::kDefaultPageSize;
|
||||
if (page_size < 1 || page_size > SandboxCaps::kMaxPageSize)
|
||||
throw GoogleError::invalid("page_size must be 1.." + std::to_string(SandboxCaps::kMaxPageSize));
|
||||
Json items = list_all(*client_, beta_endpoint_in(parts.location),
|
||||
"/" + resource_name + "/operations?pageSize=" + std::to_string(page_size),
|
||||
"operations", SandboxCaps::kMaxListPages);
|
||||
std::vector<Operation> out;
|
||||
for (const Json& item : items.as_array()) out.push_back(Operation::from_json(item));
|
||||
return out;
|
||||
}
|
||||
|
||||
Json Operations::to_json(const Operation& op) {
|
||||
Json o = Json::object();
|
||||
o.set("name", Json(op.name));
|
||||
o.set("done", Json(op.done));
|
||||
o.set("failed", Json(op.failed()));
|
||||
o.set("error_code", Json(op.error_code));
|
||||
o.set("error_message", Json(op.error_message));
|
||||
return o;
|
||||
}
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
// The operations methods Discovery offers on the three sandbox resources
|
||||
// (sandboxEnvironments, sandboxEnvironmentTemplates, sandboxEnvironmentSnapshots):
|
||||
// get, wait, cancel, delete (v1) and list (v1beta1 only). Lifecycle calls in
|
||||
// this module already poll operations internally; these are the explicit,
|
||||
// caller-driven forms. cancel and remove(delete) are explicit methods.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "google/operation.h"
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
|
||||
class Operations : public SandboxService {
|
||||
public:
|
||||
explicit Operations(std::shared_ptr<GoogleClient> client);
|
||||
|
||||
// GET {name}
|
||||
Operation get(const std::string& operation_name) const;
|
||||
// POST {name}:wait?timeout=Ns. Server-side wait of at most
|
||||
// SandboxCaps::kMaxOperationWaitSeconds (1..), then returns the operation as
|
||||
// it is (done or not).
|
||||
Operation wait(const std::string& operation_name, int timeout_seconds) const;
|
||||
// POST {name}:cancel (best effort on Google's side).
|
||||
void cancel(const std::string& operation_name) const;
|
||||
// DELETE {name}: removes the operation record, not the resource.
|
||||
void remove(const std::string& operation_name) const;
|
||||
// GET {resource}/operations (v1beta1), pages capped.
|
||||
std::vector<Operation> list(const std::string& resource_name, int page_size = 0) const;
|
||||
|
||||
static clients::body::Json to_json(const Operation& op);
|
||||
static std::string wait_query(int timeout_seconds); // "?timeout=30s"
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "google/sandbox/sandbox_service.h"
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/resource.h"
|
||||
|
||||
namespace molo::google::sandbox {
|
||||
@@ -11,15 +12,24 @@ SandboxService::SandboxService(std::shared_ptr<GoogleClient> client)
|
||||
endpoint_.name = "aiplatform-sandbox";
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::endpoint_in(const std::string& location) {
|
||||
void SandboxService::use_base_url(const std::string& base) {
|
||||
if (!base.empty() && base.rfind("http://", 0) != 0 && base.rfind("https://", 0) != 0)
|
||||
throw GoogleError::invalid("base url must be http:// or https://");
|
||||
if (!base.empty() && base.back() == '/') throw GoogleError::invalid("base url ends with /");
|
||||
base_url_ = base;
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::endpoint_in(const std::string& location) const {
|
||||
ServiceEndpoint e = endpoints::aiplatform(location);
|
||||
e.name = "aiplatform-sandbox";
|
||||
if (!base_url_.empty()) e.base_url = base_url_ + "/v1";
|
||||
return e;
|
||||
}
|
||||
|
||||
ServiceEndpoint SandboxService::beta_endpoint_in(const std::string& location) {
|
||||
ServiceEndpoint SandboxService::beta_endpoint_in(const std::string& location) const {
|
||||
ServiceEndpoint e = endpoints::aiplatform_beta(location);
|
||||
e.name = "aiplatform-sandbox-beta";
|
||||
if (!base_url_.empty()) e.base_url = base_url_ + "/v1beta1";
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,17 +18,25 @@ class SandboxService : public Service {
|
||||
public:
|
||||
explicit SandboxService(std::shared_ptr<GoogleClient> client);
|
||||
|
||||
// Points every call at `base` (scheme://host[:port], no path) instead of
|
||||
// https://{location}-aiplatform.googleapis.com. For tests against a local
|
||||
// server and for proxies; empty restores the default. Only http(s) URLs.
|
||||
void use_base_url(const std::string& base);
|
||||
|
||||
protected:
|
||||
// https://{location}-aiplatform.googleapis.com/v1 (every call but bidiExecute).
|
||||
static ServiceEndpoint endpoint_in(const std::string& location);
|
||||
ServiceEndpoint endpoint_in(const std::string& location) const;
|
||||
// .../v1beta1: bidiExecute exists only there (see calls.h).
|
||||
static ServiceEndpoint beta_endpoint_in(const std::string& location);
|
||||
ServiceEndpoint beta_endpoint_in(const std::string& location) const;
|
||||
|
||||
// POST {sandbox}:execute with the given inputs; returns the raw reply.
|
||||
clients::body::Json execute_raw(const std::string& sandbox_name,
|
||||
const std::vector<Chunk>& inputs) const;
|
||||
ExecutionResult execute(const std::string& sandbox_name,
|
||||
const std::vector<Chunk>& inputs) const;
|
||||
|
||||
private:
|
||||
std::string base_url_;
|
||||
};
|
||||
|
||||
} // namespace molo::google::sandbox
|
||||
|
||||
@@ -27,8 +27,8 @@ ExecutionResult ShellRunner::run_capped(const std::string& sandbox_name,
|
||||
std::size_t max_command) const {
|
||||
SandboxNames::parse_sandbox(sandbox_name);
|
||||
validate(request, max_command);
|
||||
return execute(sandbox_name, {ChunkCodec::shell_input(
|
||||
request.command, request.cwd, request.timeout_seconds)});
|
||||
return execute(sandbox_name, ChunkCodec::shell_inputs(request.command, request.cwd,
|
||||
request.timeout_seconds));
|
||||
}
|
||||
|
||||
ExecutionResult ShellRunner::run(const std::string& sandbox_name,
|
||||
|
||||
@@ -14,7 +14,6 @@ ExecutionResult StreamExecutor::run(
|
||||
const std::function<bool(const std::vector<Chunk>&)>& on_event) const {
|
||||
SandboxNames::Parts parts = SandboxNames::parse_sandbox(sandbox_name);
|
||||
if (inputs.empty()) throw GoogleError::invalid("no inputs");
|
||||
if (inputs.size() > 16) throw GoogleError::invalid("more than 16 input chunks");
|
||||
Json body = ChunkCodec::request(inputs);
|
||||
Json merged = Json::array();
|
||||
std::size_t events = 0;
|
||||
|
||||
@@ -69,8 +69,9 @@ def methods(doc):
|
||||
|
||||
|
||||
def schema_id(service, version, name):
|
||||
prefix = {"aiplatform": "GoogleCloudAiplatform"}[service]
|
||||
return prefix + version[0].upper() + version[1:] + name
|
||||
if service == "aiplatform":
|
||||
return "GoogleCloudAiplatform" + version[0].upper() + version[1:] + name
|
||||
return name # iamcredentials schemas carry no prefix
|
||||
|
||||
|
||||
def resolve(doc, prop):
|
||||
|
||||
@@ -82,6 +82,9 @@ SandboxEnvironment SandboxEnvironment::from_json(const Json& doc) {
|
||||
e.owner = str(doc, "owner");
|
||||
if (const Json* ci = doc.find("connectionInfo"); ci && ci->is_object()) {
|
||||
e.load_balancer_hostname = str(*ci, "loadBalancerHostname");
|
||||
e.load_balancer_ip = str(*ci, "loadBalancerIp");
|
||||
e.service_attachment = str(*ci, "serviceAttachment");
|
||||
e.sandbox_internal_ip = str(*ci, "sandboxInternalIp");
|
||||
e.routing_token = str(*ci, "routingToken");
|
||||
}
|
||||
if (const Json* spec = doc.find("spec"); spec && spec->is_object()) {
|
||||
|
||||
@@ -54,6 +54,9 @@ struct SandboxEnvironment {
|
||||
std::string latest_snapshot_name; // latestSandboxEnvironmentSnapshot
|
||||
std::string owner;
|
||||
std::string load_balancer_hostname; // connectionInfo (data plane address)
|
||||
std::string load_balancer_ip;
|
||||
std::string service_attachment; // PSC-E, VPC-SC sandboxes only
|
||||
std::string sandbox_internal_ip;
|
||||
std::string routing_token;
|
||||
std::optional<SandboxKind> kind;
|
||||
clients::body::Json spec; // Raw spec, unknown fields kept.
|
||||
@@ -66,6 +69,8 @@ struct SandboxEnvironment {
|
||||
struct InputFile {
|
||||
std::string name;
|
||||
std::string content; // Raw bytes.
|
||||
// Optional; empty: guessed from the extension, else application/octet-stream.
|
||||
std::string mime_type = {};
|
||||
};
|
||||
|
||||
struct ProducedFile {
|
||||
|
||||
Reference in New Issue
Block a user