Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

4.6 KB
Raw Blame
-- Agent sandbox: sandboxed code execution and the RPC bridge (api/_lib/sandbox/,
-- docs/agent-sandbox.md).
--
--   sandbox_runs         one row per run that has touched the sandbox. run_id is
--                        the agent_runs id for a v1 run, or the completion id of
--                        a chat completion that executed code. It carries the
--                        backend the run is pinned to, the tools a script may
--                        call through the bridge, running totals, and the kill
--                        switch the anomaly guard (or the owner) flips.
--   sandbox_executions   every execute_code and terminal call: the source, the
--                        limits it ran under, captured output, files it
--                        changed, the bridge calls it made, wall and CPU time,
--                        and what it cost. A Cloud Run execution is also the
--                        rendezvous between the API instance that started it
--                        and the instance the runner reports back to.
--   sandbox_settings     per account: the default backend, extra hosts scripts
--                        may fetch, and the ssh and docker targets. The ssh
--                        private key is generated by the platform and stored
--                        encrypted (secret-box); only its public half is shown.

create table if not exists sandbox_runs (
	run_id          text primary key,
	user_id         uuid not null references users(id) on delete cascade,
	agent_id        uuid references agent_identities(id) on delete set null,
	backend         text not null check (backend in ('local', 'cloudrun', 'docker', 'ssh')),
	title           text,
	tools_allowed   text[],
	status          text not null default 'active' check (status in ('active', 'killed')),
	killed_reason   text,
	killed_at       timestamptz,
	execution_count int not null default 0,
	bridge_calls    int not null default 0,
	refusals        int not null default 0,
	failures_streak int not null default 0,
	cost_usd        numeric(20, 6) not null default 0,
	wall_ms         bigint not null default 0,
	created_at      timestamptz not null default now(),
	updated_at      timestamptz not null default now()
);
create index if not exists sandbox_runs_user_idx on sandbox_runs (user_id, updated_at desc);

create table if not exists sandbox_executions (
	id                uuid primary key default gen_random_uuid(),
	run_id            text not null references sandbox_runs(run_id) on delete cascade,
	user_id           uuid not null references users(id) on delete cascade,
	seq               int not null,
	kind              text not null check (kind in ('code', 'terminal')),
	backend           text not null,
	language          text,
	code              text not null,
	status            text not null default 'queued'
		check (status in ('queued', 'running', 'succeeded', 'failed', 'timeout', 'killed', 'error')),
	exit_code         int,
	stdout            text,
	stderr            text,
	stdout_truncated  boolean not null default false,
	stderr_truncated  boolean not null default false,
	files_out         jsonb not null default '[]'::jsonb,
	files_deleted     jsonb not null default '[]'::jsonb,
	bridge_calls      int not null default 0,
	bridge_log        jsonb not null default '[]'::jsonb,
	limits            jsonb not null,
	wall_ms           int,
	cpu_ms            int,
	peak_memory_mb    int,
	cost_usd          numeric(20, 6) not null default 0,
	charged_usd       numeric(20, 6) not null default 0,
	error             text,
	external_id       text,
	created_at        timestamptz not null default now(),
	started_at        timestamptz,
	finished_at       timestamptz,
	unique (run_id, seq)
);
create index if not exists sandbox_executions_run_idx on sandbox_executions (run_id, seq desc);
create index if not exists sandbox_executions_user_idx on sandbox_executions (user_id, created_at desc);
create index if not exists sandbox_executions_live_idx on sandbox_executions (status, created_at)
	where status in ('queued', 'running');

create table if not exists sandbox_settings (
	user_id             uuid primary key references users(id) on delete cascade,
	default_backend     text not null default 'cloudrun' check (default_backend in ('local', 'cloudrun', 'docker', 'ssh')),
	allow_hosts         text[] not null default '{}',
	ssh_host            text,
	ssh_port            int not null default 22 check (ssh_port between 1 and 65535),
	ssh_username        text,
	ssh_host_key_sha256 text,
	ssh_public_key      text,
	ssh_private_key_enc text,
	ssh_verified_at     timestamptz,
	docker_image        text,
	updated_at          timestamptz not null default now()
);