Skip to content
  1. Home
  2. workspace
Checking…
Demo view — sanitized data only. Sign in over HTTPS to create projects and manage the workspace.

AI Engineering Workspace

Partial

Chat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.

Project files

read-only

app/src/app/api/cloud/servers/route.ts

1import { db } from "@/lib/db";
2import { auditT, instanceOf, nodeOf, notFound, tenant } from "@/lib/tenant";
3import { newToken, nodeState, orgLimits, sha, uid, now } from "@/lib/cloud";
4export const dynamic = "force-dynamic";
5export async function GET() {
6 const g = await tenant("viewer"); if (g.res) return g.res; const o = g.ctx.orgId;
7 const nodes = (await db().prepare("SELECT id,instance_id,label,kind,hostname,os,agent_version,exec_authorized,metrics,last_seen,enrolled_at,revoked_at,enroll_used_at,enroll_expires FROM nodes WHERE org_id=? ORDER BY created_at DESC").all(o) as any[]).map(n => ({ ...n, state: nodeState(n), metrics: n.metrics ? JSON.parse(n.metrics) : null }));
8 const managed = await db().prepare("SELECT id,label,region,cpu,ram_gb,disk_gb,assigned_instance_id FROM managed_inventory WHERE assigned_org_id=?").all(o);
9 const jobs = await db().prepare("SELECT id,node_id,kind,status,result,created_at FROM node_jobs WHERE org_id=? ORDER BY created_at DESC LIMIT 20").all(o);
10 return Response.json({ nodes, managed, jobs });
11}
12export async function POST(req: Request) {
13 const g = await tenant("developer", { write: true }); if (g.res) return g.res; const ctx = g.ctx; const b = await req.json().catch(() => ({})); const d = db();
14 if (b.action === "enroll") { // tenant-bound, short-lived (15 min), single-use enrollment token. Shown once; only its hash is stored.
15 if (ctx.role !== "admin" && ctx.role !== "owner") return Response.json({ error: "requires admin role" }, { status: 403 });
16 const inst = await instanceOf(ctx, String(b.instanceId)); if (!inst) return notFound();
17 if (inst.target !== "external") return Response.json({ error: "external servers can only be added to an External-VPS instance" }, { status: 409 });
18 if (inst.status === "terminated" || inst.status === "suspended") return Response.json({ error: `instance is ${inst.status}` }, { status: 409 });
19 const label = String(b.label ?? "").trim(); if (label.length < 2 || label.length > 80) return Response.json({ error: "label must be 2–80 characters" }, { status: 422 });
20 const lim = await orgLimits(ctx.orgId); const n = Number((await d.prepare("SELECT COUNT(*) n FROM nodes WHERE org_id=? AND revoked_at IS NULL").get(ctx.orgId) as any).n);
21 if (n >= lim.max_nodes) return Response.json({ error: `server quota reached (${lim.max_nodes})` }, { status: 409 });
22 const id = uid("node_"), token = newToken("zce"), exp = Date.now() + 15 * 60_000;
23 await d.prepare("INSERT INTO nodes(id,instance_id,org_id,label,kind,enroll_hash,enroll_expires,created_at) VALUES(?,?,?,?,?,?,?,?)").run(id, inst.id, ctx.orgId, label, "external-vps", sha(token), exp, now());
24 await auditT(ctx, "node.enroll_token", id, { instance: inst.id }); return Response.json({ id, token, expires: new Date(exp).toISOString(), singleUse: true }, { status: 201 });
25 }
26 if (b.action === "revoke") {
27 if (ctx.role !== "admin" && ctx.role !== "owner") return Response.json({ error: "requires admin role" }, { status: 403 });
28 const n = await nodeOf(ctx, String(b.nodeId)); if (!n) return notFound();
29 await d.prepare("UPDATE nodes SET revoked_at=?, secret_hash=NULL, enroll_hash=NULL WHERE id=? AND org_id=?").run(now(), n.id, ctx.orgId); await auditT(ctx, "node.revoke", n.id); return Response.json({ ok: true });
30 }
31 if (b.action === "authorize") { // per-node execution authorization: an admin must explicitly allow application/proxy jobs on a server
32 if (ctx.role !== "admin" && ctx.role !== "owner") return Response.json({ error: "requires admin role" }, { status: 403 });
33 const n = await nodeOf(ctx, String(b.nodeId)); if (!n) return notFound(); if (n.revoked_at || !n.enroll_used_at) return Response.json({ error: "node is not enrolled" }, { status: 409 });
34 await d.prepare("UPDATE nodes SET exec_authorized=? WHERE id=? AND org_id=?").run(b.on === false ? 0 : 1, n.id, ctx.orgId); await auditT(ctx, b.on === false ? "node.exec_revoked" : "node.exec_authorized", n.id); return Response.json({ ok: true });
35 }
36 if (b.action === "job") { // safe job kinds only: no remote command execution exists
37 const n = await nodeOf(ctx, String(b.nodeId)); if (!n) return notFound(); if (n.revoked_at || !n.enroll_used_at) return Response.json({ error: "node is not enrolled" }, { status: 409 });
38 const kind = String(b.kind); if (!["echo", "collect-info", "agent.rotate", "agent.upgrade"].includes(kind)) return Response.json({ error: "job kind must be echo, collect-info, agent.rotate or agent.upgrade" }, { status: 422 }); if (kind.startsWith("agent.") && (ctx.role !== "admin" && ctx.role !== "owner" || !n.exec_authorized)) return Response.json({ error: "agent maintenance jobs need an admin and an execution-authorized server" }, { status: 403 });
39 const id = uid("job_"); await d.prepare("INSERT INTO node_jobs(id,org_id,node_id,kind,payload,created_at) VALUES(?,?,?,?,?,?)").run(id, ctx.orgId, n.id, kind, JSON.stringify({ text: String(b.text ?? "").slice(0, 200) }), now()); await auditT(ctx, "node.job", id, { kind }); return Response.json({ id }, { status: 201 });
40 }
41 return Response.json({ error: "unknown action" }, { status: 400 });
42}
43

Conversation

Partial
Sign in to start AI sessions.

Task checklist

  • Application shell deployed
  • Persistent projects
  • Agent runtime
  • Sandboxed terminal

Execution activity

No agent sessions. Git HEAD 1c27633 on master.

Approval requests

None pending.