@open-document/core 0.14.3 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-4K9V8ysT.js → build-CBMroMqd.js} +1 -1
- package/dist/{check-B40JlWME.js → check-C2kIZr3f.js} +3 -3
- package/dist/cli/bin.js +9 -9
- package/dist/{context-Clm_nmr1.js → context-DGp1jwN5.js} +1 -1
- package/dist/{dev-De3pLYn_.js → dev-DmzGncKf.js} +1 -1
- package/dist/{diff-nU9ybiMk.js → diff-CMI_PVzz.js} +2 -2
- package/dist/{export-B9rrNBx2.js → export-BQMUBoBB.js} +3 -3
- package/dist/{import-C3542Wry.js → import-CiSRqZyo.js} +3 -3
- package/dist/ops/index.js +2 -2
- package/dist/{ops-ByJnGyuE.js → ops-DQbZmYRF.js} +1 -1
- package/dist/{preview-id9mj3IU.js → preview-CERhZ_FX.js} +1 -1
- package/dist/{session-CdkoJhi1.js → session-DdaKKd7_.js} +1007 -4
- package/dist/{templates-UJ_v7H-a.js → templates-CBeC-v7e.js} +3 -3
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/agent/consent.ts +42 -0
- package/src/agent/provider.ts +24 -1
- package/src/agent/providers/antigravity-local.ts +193 -0
- package/src/agent/providers/antigravity.ts +656 -0
- package/src/agent/providers/index.ts +2 -1
- package/src/agent/runner.ts +4 -0
- package/src/agent/sandbox-sync.ts +178 -0
- package/src/agent/status.ts +33 -1
- package/src/agent/types.ts +21 -3
- package/src/app/components/agent/agent-panel.tsx +180 -42
- package/src/app/components/agent/connect-view.tsx +294 -26
- package/src/app/components/agent/provider-logo.tsx +4 -0
- package/src/app/lib/agent/use-agent.ts +14 -1
- package/src/app/lib/i18n-ja/agent-panel.ts +49 -0
- package/src/app/lib/i18n-ko/agent-panel.ts +48 -0
- package/src/app/lib/i18n-zh-cn/agent-panel.ts +46 -0
- package/src/app/lib/i18n-zh-tw/agent-panel.ts +46 -0
- package/src/app/lib/use-locale.ts +43 -0
|
@@ -17,6 +17,7 @@ import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
|
17
17
|
import { Buffer as Buffer$1 } from "node:buffer";
|
|
18
18
|
import tailwindcss from "@tailwindcss/vite";
|
|
19
19
|
import react from "@vitejs/plugin-react";
|
|
20
|
+
import { gunzipSync } from "node:zlib";
|
|
20
21
|
import os from "node:os";
|
|
21
22
|
//#region src/vite/routes/context.ts
|
|
22
23
|
function makeContext(opts) {
|
|
@@ -1027,6 +1028,33 @@ function checkItemResult(report) {
|
|
|
1027
1028
|
};
|
|
1028
1029
|
}
|
|
1029
1030
|
//#endregion
|
|
1031
|
+
//#region src/agent/consent.ts
|
|
1032
|
+
function consentFile(userCwd) {
|
|
1033
|
+
return path.join(userCwd, "node_modules", ".open-doc", "agent-consent.json");
|
|
1034
|
+
}
|
|
1035
|
+
async function readAll(userCwd) {
|
|
1036
|
+
try {
|
|
1037
|
+
const raw = JSON.parse(await fs.readFile(consentFile(userCwd), "utf8"));
|
|
1038
|
+
return raw && typeof raw === "object" ? raw : {};
|
|
1039
|
+
} catch {
|
|
1040
|
+
return {};
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
async function hasConsent(userCwd, provider, version) {
|
|
1044
|
+
return (await readAll(userCwd))[provider]?.version === version;
|
|
1045
|
+
}
|
|
1046
|
+
async function setConsent(userCwd, provider, version) {
|
|
1047
|
+
const all = await readAll(userCwd);
|
|
1048
|
+
if (version) all[provider] = {
|
|
1049
|
+
version,
|
|
1050
|
+
acceptedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1051
|
+
};
|
|
1052
|
+
else delete all[provider];
|
|
1053
|
+
const file = consentFile(userCwd);
|
|
1054
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
1055
|
+
await fs.writeFile(file, `${JSON.stringify(all, null, 2)}\n`);
|
|
1056
|
+
}
|
|
1057
|
+
//#endregion
|
|
1030
1058
|
//#region src/agent/install.ts
|
|
1031
1059
|
const LOCKFILES = [
|
|
1032
1060
|
["pnpm-lock.yaml", "pnpm"],
|
|
@@ -1117,6 +1145,14 @@ function safeJson(value) {
|
|
|
1117
1145
|
return String(value);
|
|
1118
1146
|
}
|
|
1119
1147
|
}
|
|
1148
|
+
function isInsideRoots(cwd, file, roots) {
|
|
1149
|
+
if (typeof file !== "string" || !file) return false;
|
|
1150
|
+
const resolved = path.resolve(cwd, file);
|
|
1151
|
+
return roots.some((root) => {
|
|
1152
|
+
const rel = path.relative(root, resolved);
|
|
1153
|
+
return rel === "" || !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1120
1156
|
function parseQuestions(input) {
|
|
1121
1157
|
const raw = Array.isArray(input.questions) ? input.questions : [];
|
|
1122
1158
|
const questions = [];
|
|
@@ -1222,6 +1258,920 @@ function editedPrompt(original, text) {
|
|
|
1222
1258
|
function rewoundPrompt(text) {
|
|
1223
1259
|
return `${text}\n\n<open-doc-note>I went back and edited this message. Files keep the changes made after it in the discarded part of this conversation, so read the current files before editing them.</open-doc-note>`;
|
|
1224
1260
|
}
|
|
1261
|
+
const SANDBOX_ROOT = "/workspace";
|
|
1262
|
+
function hashBytes(data) {
|
|
1263
|
+
return createHash("sha1").update(data).digest("hex");
|
|
1264
|
+
}
|
|
1265
|
+
const SKIP_DIRS = /* @__PURE__ */ new Set([
|
|
1266
|
+
"node_modules",
|
|
1267
|
+
".git",
|
|
1268
|
+
"dist",
|
|
1269
|
+
"out"
|
|
1270
|
+
]);
|
|
1271
|
+
async function walk(dir) {
|
|
1272
|
+
let entries;
|
|
1273
|
+
try {
|
|
1274
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
1275
|
+
} catch {
|
|
1276
|
+
return [];
|
|
1277
|
+
}
|
|
1278
|
+
const files = [];
|
|
1279
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1280
|
+
const full = path.join(dir, entry.name);
|
|
1281
|
+
if (entry.isDirectory()) {
|
|
1282
|
+
if (!SKIP_DIRS.has(entry.name)) files.push(...await walk(full));
|
|
1283
|
+
} else if (entry.isFile()) files.push(full);
|
|
1284
|
+
}
|
|
1285
|
+
return files;
|
|
1286
|
+
}
|
|
1287
|
+
function toPosix(rel) {
|
|
1288
|
+
return rel.split(path.sep).join("/");
|
|
1289
|
+
}
|
|
1290
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
1291
|
+
function decodeText(data) {
|
|
1292
|
+
if (data.includes(0)) return null;
|
|
1293
|
+
try {
|
|
1294
|
+
return decoder.decode(data);
|
|
1295
|
+
} catch {
|
|
1296
|
+
return null;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
async function collectUploads(cwd, roots) {
|
|
1300
|
+
const files = [];
|
|
1301
|
+
const skipped = [];
|
|
1302
|
+
let total = 0;
|
|
1303
|
+
for (const root of roots) for (const file of await walk(root)) {
|
|
1304
|
+
const rel = toPosix(path.relative(cwd, file));
|
|
1305
|
+
const data = await fs.readFile(file).catch(() => null);
|
|
1306
|
+
const content = data && data.byteLength <= 1048576 ? decodeText(data) : null;
|
|
1307
|
+
if (!data || content === null || total + data.byteLength > 2097152) {
|
|
1308
|
+
skipped.push(rel);
|
|
1309
|
+
continue;
|
|
1310
|
+
}
|
|
1311
|
+
total += data.byteLength;
|
|
1312
|
+
files.push({
|
|
1313
|
+
rel,
|
|
1314
|
+
content,
|
|
1315
|
+
hash: hashBytes(data)
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
return {
|
|
1319
|
+
files,
|
|
1320
|
+
skipped
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
function readString(block, start, length) {
|
|
1324
|
+
const slice = block.subarray(start, start + length);
|
|
1325
|
+
const end = slice.indexOf(0);
|
|
1326
|
+
return new TextDecoder().decode(end === -1 ? slice : slice.subarray(0, end));
|
|
1327
|
+
}
|
|
1328
|
+
function parsePax(data) {
|
|
1329
|
+
const out = {};
|
|
1330
|
+
const text = new TextDecoder().decode(data);
|
|
1331
|
+
let i = 0;
|
|
1332
|
+
while (i < text.length) {
|
|
1333
|
+
const space = text.indexOf(" ", i);
|
|
1334
|
+
if (space === -1) break;
|
|
1335
|
+
const len = Number.parseInt(text.slice(i, space), 10);
|
|
1336
|
+
if (!Number.isFinite(len) || len <= 0) break;
|
|
1337
|
+
const record = text.slice(space + 1, i + len - 1);
|
|
1338
|
+
const eq = record.indexOf("=");
|
|
1339
|
+
if (eq !== -1) out[record.slice(0, eq)] = record.slice(eq + 1);
|
|
1340
|
+
i += len;
|
|
1341
|
+
}
|
|
1342
|
+
return out;
|
|
1343
|
+
}
|
|
1344
|
+
function readTar(buffer) {
|
|
1345
|
+
const entries = [];
|
|
1346
|
+
let offset = 0;
|
|
1347
|
+
let longName = null;
|
|
1348
|
+
while (offset + 512 <= buffer.length) {
|
|
1349
|
+
const header = buffer.subarray(offset, offset + 512);
|
|
1350
|
+
if (header.every((b) => b === 0)) break;
|
|
1351
|
+
const size = Number.parseInt(readString(header, 124, 12).trim() || "0", 8);
|
|
1352
|
+
const type = String.fromCharCode(header[156] || 48);
|
|
1353
|
+
const dataStart = offset + 512;
|
|
1354
|
+
const data = buffer.subarray(dataStart, dataStart + size);
|
|
1355
|
+
offset = dataStart + Math.ceil(size / 512) * 512;
|
|
1356
|
+
if (type === "x") {
|
|
1357
|
+
longName = parsePax(data).path ?? longName;
|
|
1358
|
+
continue;
|
|
1359
|
+
}
|
|
1360
|
+
if (type === "L") {
|
|
1361
|
+
longName = readString(data, 0, data.length);
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
if (type === "g") continue;
|
|
1365
|
+
const prefix = readString(header, 345, 155);
|
|
1366
|
+
const name = longName ?? (prefix ? `${prefix}/${readString(header, 0, 100)}` : readString(header, 0, 100));
|
|
1367
|
+
longName = null;
|
|
1368
|
+
if (type === "0" || type === "\0" || type === "7") entries.push({
|
|
1369
|
+
path: name,
|
|
1370
|
+
data
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
return entries;
|
|
1374
|
+
}
|
|
1375
|
+
function snapshotRel(entryPath, writableRel) {
|
|
1376
|
+
let p = entryPath.replace(/\\/g, "/").replace(/^(\.\/)+/, "").replace(/^\/+/, "");
|
|
1377
|
+
const root = SANDBOX_ROOT.replace(/^\/+/, "");
|
|
1378
|
+
if (!p.startsWith(`${root}/`)) return null;
|
|
1379
|
+
p = path.posix.normalize(p.slice(root.length + 1));
|
|
1380
|
+
if (p.startsWith("../") || p === ".." || path.posix.isAbsolute(p)) return null;
|
|
1381
|
+
const base = writableRel.replace(/\/+$/, "");
|
|
1382
|
+
return p.startsWith(`${base}/`) ? p : null;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* What to do with each file the agent left in its sandbox. A file it did not
|
|
1386
|
+
* change is skipped; a changed or new one is written unless the local copy
|
|
1387
|
+
* moved on since the upload (someone edited it during the turn), in which
|
|
1388
|
+
* case the agent's version is a conflict and the local file is left alone.
|
|
1389
|
+
* `uploaded` and `local` map workspace-relative paths to content hashes; a
|
|
1390
|
+
* missing key means the file did not exist.
|
|
1391
|
+
*/
|
|
1392
|
+
function decideWrites(entries, uploaded, local) {
|
|
1393
|
+
const decisions = [];
|
|
1394
|
+
for (const { rel, data } of entries) {
|
|
1395
|
+
const before = uploaded.get(rel);
|
|
1396
|
+
if (before !== void 0 && before === hashBytes(data)) continue;
|
|
1397
|
+
const action = local.get(rel) === before ? "write" : "conflict";
|
|
1398
|
+
decisions.push({
|
|
1399
|
+
rel,
|
|
1400
|
+
action,
|
|
1401
|
+
data
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
return decisions;
|
|
1405
|
+
}
|
|
1406
|
+
//#endregion
|
|
1407
|
+
//#region src/agent/providers/antigravity-local.ts
|
|
1408
|
+
const GEMINI_API_CLI_VERSION = "0.4.6";
|
|
1409
|
+
const RELEASE = `https://github.com/google-gemini/gemini-api-cli/releases/download/v${GEMINI_API_CLI_VERSION}`;
|
|
1410
|
+
const CHECKSUMS = {
|
|
1411
|
+
"darwin-arm64": "c80b5ec2d0a6feee0cf18943427cfdaf9aab7183b7f62f50b956cd2431b98018",
|
|
1412
|
+
"darwin-x64": "009a83ea43b1cd25f87375efc01c9418c12fcd7ea779a26a483eb4e875da4a70",
|
|
1413
|
+
"linux-arm64": "6bf48dc0b6479b157d4dc35d074d06286c2c6e2f09ff99236bde9829763576f7",
|
|
1414
|
+
"linux-x64": "78862dde519d9b47a9fb70b1f0340d19fa1245934d65e83f0e2dbc1d17cd2fa6"
|
|
1415
|
+
};
|
|
1416
|
+
const ASSETS = {
|
|
1417
|
+
"darwin-arm64": "gemini-api_Darwin_arm64.tar.gz",
|
|
1418
|
+
"darwin-x64": "gemini-api_Darwin_x86_64.tar.gz",
|
|
1419
|
+
"linux-arm64": "gemini-api_Linux_arm64.tar.gz",
|
|
1420
|
+
"linux-x64": "gemini-api_Linux_x86_64.tar.gz"
|
|
1421
|
+
};
|
|
1422
|
+
const API_BASE$1 = "https://generativelanguage.googleapis.com/v1beta";
|
|
1423
|
+
const READY_TIMEOUT_MS = 1e4;
|
|
1424
|
+
function platformKey() {
|
|
1425
|
+
return `${process.platform}-${process.arch}`;
|
|
1426
|
+
}
|
|
1427
|
+
function localModeSupported() {
|
|
1428
|
+
return platformKey() in ASSETS;
|
|
1429
|
+
}
|
|
1430
|
+
function stateDir(cwd) {
|
|
1431
|
+
return path.join(cwd, "node_modules", ".open-doc");
|
|
1432
|
+
}
|
|
1433
|
+
function cliPath(cwd) {
|
|
1434
|
+
return path.join(stateDir(cwd), "bin", "gemini-api");
|
|
1435
|
+
}
|
|
1436
|
+
function versionFile(cwd) {
|
|
1437
|
+
return path.join(stateDir(cwd), "bin", "gemini-api.version");
|
|
1438
|
+
}
|
|
1439
|
+
async function installedCliVersion(cwd) {
|
|
1440
|
+
try {
|
|
1441
|
+
await fs.access(cliPath(cwd));
|
|
1442
|
+
return (await fs.readFile(versionFile(cwd), "utf8")).trim() || null;
|
|
1443
|
+
} catch {
|
|
1444
|
+
return null;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
let installing = null;
|
|
1448
|
+
function installCli(cwd) {
|
|
1449
|
+
installing ??= (async () => {
|
|
1450
|
+
const key = platformKey();
|
|
1451
|
+
const asset = ASSETS[key];
|
|
1452
|
+
if (!asset) throw new Error(`Local mode is not available on ${key} yet.`);
|
|
1453
|
+
const res = await fetch(`${RELEASE}/${asset}`);
|
|
1454
|
+
if (!res.ok) throw new Error(`Could not download the gemini-api CLI (${res.status}).`);
|
|
1455
|
+
const archive = new Uint8Array(await res.arrayBuffer());
|
|
1456
|
+
if (createHash("sha256").update(archive).digest("hex") !== CHECKSUMS[key]) throw new Error("The downloaded gemini-api CLI failed its checksum; nothing was installed.");
|
|
1457
|
+
const entry = readTar(gunzipSync(archive)).find((e) => path.posix.basename(e.path) === "gemini-api");
|
|
1458
|
+
if (!entry) throw new Error("The gemini-api archive did not contain the CLI.");
|
|
1459
|
+
const target = cliPath(cwd);
|
|
1460
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
1461
|
+
const partial = `${target}.partial`;
|
|
1462
|
+
await fs.writeFile(partial, entry.data, { mode: 493 });
|
|
1463
|
+
await fs.rename(partial, target);
|
|
1464
|
+
await fs.writeFile(versionFile(cwd), `${GEMINI_API_CLI_VERSION}\n`);
|
|
1465
|
+
})().finally(() => {
|
|
1466
|
+
installing = null;
|
|
1467
|
+
});
|
|
1468
|
+
return installing;
|
|
1469
|
+
}
|
|
1470
|
+
function environmentFile(cwd) {
|
|
1471
|
+
return path.join(stateDir(cwd), "antigravity-environment.json");
|
|
1472
|
+
}
|
|
1473
|
+
async function readEnvironment(cwd) {
|
|
1474
|
+
try {
|
|
1475
|
+
const raw = JSON.parse(await fs.readFile(environmentFile(cwd), "utf8"));
|
|
1476
|
+
return typeof raw?.id === "string" && typeof raw?.environment_key === "string" ? raw : null;
|
|
1477
|
+
} catch {
|
|
1478
|
+
return null;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
async function ensureEnvironment(cwd, apiKey) {
|
|
1482
|
+
const existing = await readEnvironment(cwd);
|
|
1483
|
+
if (existing) return existing;
|
|
1484
|
+
const res = await fetch(`${API_BASE$1}/environments`, {
|
|
1485
|
+
method: "POST",
|
|
1486
|
+
headers: {
|
|
1487
|
+
"x-goog-api-key": apiKey,
|
|
1488
|
+
"content-type": "application/json"
|
|
1489
|
+
},
|
|
1490
|
+
body: JSON.stringify({ mode: "self_hosted" })
|
|
1491
|
+
});
|
|
1492
|
+
if (!res.ok) throw new Error(`Could not create a local environment (Gemini API ${res.status}).`);
|
|
1493
|
+
const body = await res.json();
|
|
1494
|
+
if (!body.id || !body.environment_key) throw new Error("The Gemini API returned an environment without a key.");
|
|
1495
|
+
const record = {
|
|
1496
|
+
id: body.id,
|
|
1497
|
+
environment_key: body.environment_key
|
|
1498
|
+
};
|
|
1499
|
+
await fs.mkdir(stateDir(cwd), { recursive: true });
|
|
1500
|
+
await fs.writeFile(environmentFile(cwd), `${JSON.stringify(record, null, 2)}\n`, { mode: 384 });
|
|
1501
|
+
return record;
|
|
1502
|
+
}
|
|
1503
|
+
async function forgetEnvironment(cwd) {
|
|
1504
|
+
stopWorker();
|
|
1505
|
+
await fs.rm(environmentFile(cwd), { force: true });
|
|
1506
|
+
}
|
|
1507
|
+
let worker = null;
|
|
1508
|
+
function stopWorker() {
|
|
1509
|
+
worker?.process.kill();
|
|
1510
|
+
worker = null;
|
|
1511
|
+
}
|
|
1512
|
+
process.once("exit", stopWorker);
|
|
1513
|
+
/**
|
|
1514
|
+
* One worker per dev server, rooted at the workspace so the agent finds the
|
|
1515
|
+
* project skills. `--disable-bash` leaves it only the file tools, which the CLI
|
|
1516
|
+
* confines to the root with os.Root, so a model can never run a command here.
|
|
1517
|
+
* The project-wide API key is withheld; the worker only needs its environment key.
|
|
1518
|
+
*/
|
|
1519
|
+
function ensureWorker(cwd, env) {
|
|
1520
|
+
if (worker && worker.key === env.environment_key && worker.process.exitCode === null) return worker.ready;
|
|
1521
|
+
stopWorker();
|
|
1522
|
+
const { GEMINI_API_KEY: _withheld, ...rest } = process.env;
|
|
1523
|
+
const child = spawn(cliPath(cwd), [
|
|
1524
|
+
"environments",
|
|
1525
|
+
"connect",
|
|
1526
|
+
"--root-dir",
|
|
1527
|
+
cwd,
|
|
1528
|
+
"--workdir",
|
|
1529
|
+
cwd,
|
|
1530
|
+
"--disable-bash"
|
|
1531
|
+
], {
|
|
1532
|
+
cwd,
|
|
1533
|
+
env: {
|
|
1534
|
+
...rest,
|
|
1535
|
+
GEMINI_ENVIRONMENT_KEY: env.environment_key
|
|
1536
|
+
},
|
|
1537
|
+
stdio: [
|
|
1538
|
+
"ignore",
|
|
1539
|
+
"ignore",
|
|
1540
|
+
"pipe"
|
|
1541
|
+
]
|
|
1542
|
+
});
|
|
1543
|
+
const log = [];
|
|
1544
|
+
const ready = new Promise((resolve, reject) => {
|
|
1545
|
+
const timer = setTimeout(resolve, READY_TIMEOUT_MS);
|
|
1546
|
+
child.stderr?.setEncoding("utf8");
|
|
1547
|
+
child.stderr?.on("data", (chunk) => {
|
|
1548
|
+
for (const line of chunk.split("\n").filter(Boolean)) {
|
|
1549
|
+
log.push(line);
|
|
1550
|
+
if (log.length > 50) log.shift();
|
|
1551
|
+
if (/polling/i.test(line)) {
|
|
1552
|
+
clearTimeout(timer);
|
|
1553
|
+
resolve();
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
});
|
|
1557
|
+
child.once("error", (err) => {
|
|
1558
|
+
clearTimeout(timer);
|
|
1559
|
+
reject(err);
|
|
1560
|
+
});
|
|
1561
|
+
child.once("exit", (code) => {
|
|
1562
|
+
clearTimeout(timer);
|
|
1563
|
+
if (worker?.process === child) worker = null;
|
|
1564
|
+
reject(/* @__PURE__ */ new Error(`The local worker stopped (exit ${code}). ${log.slice(-3).join(" ")}`));
|
|
1565
|
+
});
|
|
1566
|
+
});
|
|
1567
|
+
ready.catch(() => {});
|
|
1568
|
+
worker = {
|
|
1569
|
+
process: child,
|
|
1570
|
+
key: env.environment_key,
|
|
1571
|
+
ready,
|
|
1572
|
+
log
|
|
1573
|
+
};
|
|
1574
|
+
return ready;
|
|
1575
|
+
}
|
|
1576
|
+
//#endregion
|
|
1577
|
+
//#region src/agent/providers/antigravity.ts
|
|
1578
|
+
const ANTIGRAVITY_AGENT = "antigravity-preview-09-2026";
|
|
1579
|
+
const ANTIGRAVITY_CONSENT_VERSION = "2026-11-14.2";
|
|
1580
|
+
const API_BASE = "https://generativelanguage.googleapis.com/v1beta";
|
|
1581
|
+
const KEY_ENV = "GEMINI_API_KEY";
|
|
1582
|
+
const MAX_TOTAL_TOKENS = 5e5;
|
|
1583
|
+
const SNAPSHOT_ATTEMPTS = 4;
|
|
1584
|
+
const MODELS = [
|
|
1585
|
+
{
|
|
1586
|
+
id: "gemini-4-hydrogen",
|
|
1587
|
+
label: "Gemini 4 Hydrogen",
|
|
1588
|
+
efforts: []
|
|
1589
|
+
},
|
|
1590
|
+
{
|
|
1591
|
+
id: "gemini-4-carbon",
|
|
1592
|
+
label: "Gemini 4 Carbon",
|
|
1593
|
+
efforts: []
|
|
1594
|
+
},
|
|
1595
|
+
{
|
|
1596
|
+
id: "gemini-4-argon",
|
|
1597
|
+
label: "Gemini 4 Argon",
|
|
1598
|
+
efforts: []
|
|
1599
|
+
},
|
|
1600
|
+
{
|
|
1601
|
+
id: "gemini-3.8-flash",
|
|
1602
|
+
label: "Gemini 3.8 Flash",
|
|
1603
|
+
efforts: []
|
|
1604
|
+
},
|
|
1605
|
+
{
|
|
1606
|
+
id: "gemini-3.6-flash",
|
|
1607
|
+
label: "Gemini 3.6 Flash",
|
|
1608
|
+
efforts: []
|
|
1609
|
+
},
|
|
1610
|
+
{
|
|
1611
|
+
id: "gemini-3.5-flash-lite",
|
|
1612
|
+
label: "Gemini 3.5 Flash-Lite",
|
|
1613
|
+
efforts: []
|
|
1614
|
+
}
|
|
1615
|
+
];
|
|
1616
|
+
const PRICES = {
|
|
1617
|
+
"gemini-4-hydrogen": [{
|
|
1618
|
+
from: "",
|
|
1619
|
+
input: 1.5,
|
|
1620
|
+
cached: .075,
|
|
1621
|
+
output: 7.5
|
|
1622
|
+
}],
|
|
1623
|
+
"gemini-4-carbon": [{
|
|
1624
|
+
from: "2026-12-01",
|
|
1625
|
+
input: 4,
|
|
1626
|
+
cached: .2,
|
|
1627
|
+
output: 20
|
|
1628
|
+
}, {
|
|
1629
|
+
from: "",
|
|
1630
|
+
input: 2,
|
|
1631
|
+
cached: .1,
|
|
1632
|
+
output: 10
|
|
1633
|
+
}],
|
|
1634
|
+
"gemini-4-argon": [{
|
|
1635
|
+
from: "2026-12-01",
|
|
1636
|
+
input: 4,
|
|
1637
|
+
cached: .2,
|
|
1638
|
+
output: 20
|
|
1639
|
+
}, {
|
|
1640
|
+
from: "",
|
|
1641
|
+
input: 2,
|
|
1642
|
+
cached: .1,
|
|
1643
|
+
output: 10
|
|
1644
|
+
}],
|
|
1645
|
+
"gemini-3.8-flash": [{
|
|
1646
|
+
from: "2027-01-01",
|
|
1647
|
+
input: 1.5,
|
|
1648
|
+
cached: .15,
|
|
1649
|
+
output: 7.5
|
|
1650
|
+
}, {
|
|
1651
|
+
from: "",
|
|
1652
|
+
input: .75,
|
|
1653
|
+
cached: .075,
|
|
1654
|
+
output: 3.75
|
|
1655
|
+
}]
|
|
1656
|
+
};
|
|
1657
|
+
function estimateCostUsd(model, usage, now = /* @__PURE__ */ new Date()) {
|
|
1658
|
+
const tiers = model ? PRICES[model] : void 0;
|
|
1659
|
+
if (!tiers || !usage) return null;
|
|
1660
|
+
const day = now.toISOString().slice(0, 10);
|
|
1661
|
+
const price = tiers.find((t) => day >= t.from);
|
|
1662
|
+
if (!price) return null;
|
|
1663
|
+
const input = usage.total_input_tokens ?? 0;
|
|
1664
|
+
const cached = Math.min(usage.total_cached_tokens ?? 0, input);
|
|
1665
|
+
const output = (usage.total_output_tokens ?? 0) + (usage.total_thought_tokens ?? 0);
|
|
1666
|
+
return ((input - cached) * price.input + cached * price.cached + output * price.output) / 1e6;
|
|
1667
|
+
}
|
|
1668
|
+
function parseDotenv(source, key) {
|
|
1669
|
+
for (const line of source.split(/\r?\n/)) {
|
|
1670
|
+
const match = /^\s*(?:export\s+)?([\w.]+)\s*=\s*(.*)\s*$/.exec(line);
|
|
1671
|
+
if (!match || match[1] !== key) continue;
|
|
1672
|
+
return match[2].replace(/\s+#.*$/, "").replace(/^(['"])(.*)\1$/, "$2") || null;
|
|
1673
|
+
}
|
|
1674
|
+
return null;
|
|
1675
|
+
}
|
|
1676
|
+
async function resolveApiKey(cwd) {
|
|
1677
|
+
const fromEnv = process.env[KEY_ENV]?.trim();
|
|
1678
|
+
if (fromEnv) return fromEnv;
|
|
1679
|
+
for (const name of [".env.local", ".env"]) {
|
|
1680
|
+
const source = await fs.readFile(path.join(cwd, name), "utf8").catch(() => null);
|
|
1681
|
+
const value = source ? parseDotenv(source, KEY_ENV) : null;
|
|
1682
|
+
if (value) return value;
|
|
1683
|
+
}
|
|
1684
|
+
return null;
|
|
1685
|
+
}
|
|
1686
|
+
function sandboxInstruction(docRel, skipped) {
|
|
1687
|
+
const lines = [
|
|
1688
|
+
`You run in a cloud Linux sandbox, not on the user's machine. A copy of their open-doc workspace is at ${SANDBOX_ROOT}: work there (cd ${SANDBOX_ROOT}) and use paths relative to it.`,
|
|
1689
|
+
`The project skills are at ${SANDBOX_ROOT}/.agents/skills/<name>/SKILL.md; read the ones you need from there.`,
|
|
1690
|
+
`When your turn ends, only files under ${SANDBOX_ROOT}/${docRel}/ are copied back to the user's machine. Changes anywhere else are lost, so never edit other documents, and never delete files: deletions are not copied back.`,
|
|
1691
|
+
"There is no dev server, open-doc CLI or browser here, and none is needed: do not install packages or try to run the project.",
|
|
1692
|
+
"In replies, name files by their workspace-relative path as plain text, never as file:// links: the user cannot open sandbox paths."
|
|
1693
|
+
];
|
|
1694
|
+
if (skipped.length > 0) {
|
|
1695
|
+
const shown = skipped.slice(0, 20).join(", ");
|
|
1696
|
+
lines.push(`These files exist on the user's machine but were not uploaded (binary or too large): ${shown}${skipped.length > 20 ? ", …" : ""}. Reference them by path; do not recreate them.`);
|
|
1697
|
+
}
|
|
1698
|
+
return lines.join(" ");
|
|
1699
|
+
}
|
|
1700
|
+
function localInstruction(docRel) {
|
|
1701
|
+
return [
|
|
1702
|
+
"Your file tools run on the user's own computer, confined to their open-doc project folder, and edits appear in their viewer as you save them.",
|
|
1703
|
+
"The shell is disabled: never call execute_bash, it always fails. Commands, package installs and the dev server are unavailable, and none is needed.",
|
|
1704
|
+
`Open ${docRel}/index.tsx directly as your first step: do not list folders or search for it. Work only in ${docRel}/, never in other documents or project files such as package.json or open-doc.config.ts.`,
|
|
1705
|
+
"For a small change (wording, a translation, a value in a style) edit it straight away; that needs no skill. Read .agents/skills/doc-authoring/SKILL.md (or create-doc, apply-comments) only before adding pages, sections or structures you have not written here before. This overrides the general advice to read the skills before every edit.",
|
|
1706
|
+
"view_file shows no line numbers, so never guess a line range: when you call replace_file_content, set StartLine to 1 and EndLine to the file's last line, and let a TargetContent that occurs once in the file pick the spot.",
|
|
1707
|
+
"Never list, search or open node_modules, dist or out.",
|
|
1708
|
+
"In replies, name files by their project-relative path as plain text, never as file:// links."
|
|
1709
|
+
].join(" ");
|
|
1710
|
+
}
|
|
1711
|
+
const TOOL_TITLES = {
|
|
1712
|
+
view_file: "Read",
|
|
1713
|
+
list_dir: "List folder",
|
|
1714
|
+
replace_file_content: "Edit",
|
|
1715
|
+
multi_replace_file_content: "Edit",
|
|
1716
|
+
write_to_file: "Write",
|
|
1717
|
+
create_file: "Write",
|
|
1718
|
+
run_command: "Run command",
|
|
1719
|
+
execute_bash: "Run command",
|
|
1720
|
+
read_file: "Read",
|
|
1721
|
+
write_file: "Write",
|
|
1722
|
+
edit_file: "Edit",
|
|
1723
|
+
grep_search: "Search",
|
|
1724
|
+
find_by_name: "Find files",
|
|
1725
|
+
search_web: "Web search",
|
|
1726
|
+
read_url_content: "Read URL"
|
|
1727
|
+
};
|
|
1728
|
+
function toolDetail(args, roots) {
|
|
1729
|
+
if (!args) return null;
|
|
1730
|
+
const value = args.TargetFile ?? args.AbsolutePath ?? args.DirectoryPath ?? args.SearchPath ?? args.SearchDirectory ?? args.path ?? args.file_path ?? args.CommandLine ?? args.command ?? args.Query ?? args.Pattern;
|
|
1731
|
+
if (typeof value !== "string") return null;
|
|
1732
|
+
const root = roots.find((r) => value.startsWith(`${r.replace(/[\\/]+$/, "")}/`));
|
|
1733
|
+
return clipDetail(root ? value.slice(root.replace(/[\\/]+$/, "").length + 1) : value);
|
|
1734
|
+
}
|
|
1735
|
+
const SHELL_TOOL = "execute_bash";
|
|
1736
|
+
function isBlockedShell(call, result) {
|
|
1737
|
+
if (call.name !== SHELL_TOOL || result.is_error !== true) return false;
|
|
1738
|
+
return /disable-bash|is disabled/i.test(JSON.stringify(result.result ?? ""));
|
|
1739
|
+
}
|
|
1740
|
+
function createAntigravityMapper(emit, roots = [SANDBOX_ROOT]) {
|
|
1741
|
+
const steps = /* @__PURE__ */ new Map();
|
|
1742
|
+
const texts = /* @__PURE__ */ new Map();
|
|
1743
|
+
let interactionId = null;
|
|
1744
|
+
let environmentId = null;
|
|
1745
|
+
let status = null;
|
|
1746
|
+
let usage;
|
|
1747
|
+
const textId = (index) => `${interactionId ?? "agy"}-t${index}`;
|
|
1748
|
+
const calls = /* @__PURE__ */ new Map();
|
|
1749
|
+
const startCall = (index) => {
|
|
1750
|
+
const call = calls.get(index);
|
|
1751
|
+
if (!call?.step.id) return;
|
|
1752
|
+
calls.delete(index);
|
|
1753
|
+
let args = call.step.arguments;
|
|
1754
|
+
if (call.args) try {
|
|
1755
|
+
args = {
|
|
1756
|
+
...args,
|
|
1757
|
+
...JSON.parse(call.args)
|
|
1758
|
+
};
|
|
1759
|
+
} catch {}
|
|
1760
|
+
const name = call.step.name ?? "tool";
|
|
1761
|
+
emit({
|
|
1762
|
+
type: "tool-start",
|
|
1763
|
+
id: call.step.id,
|
|
1764
|
+
name,
|
|
1765
|
+
title: TOOL_TITLES[name] ?? name,
|
|
1766
|
+
detail: toolDetail(args, roots)
|
|
1767
|
+
});
|
|
1768
|
+
};
|
|
1769
|
+
const finishText = (index) => {
|
|
1770
|
+
const text = texts.get(index);
|
|
1771
|
+
if (text === void 0) return;
|
|
1772
|
+
texts.delete(index);
|
|
1773
|
+
const asked = extractQuestionBlock(text);
|
|
1774
|
+
emit({
|
|
1775
|
+
type: "text-end",
|
|
1776
|
+
id: textId(index),
|
|
1777
|
+
text: asked ? asked.text : text
|
|
1778
|
+
});
|
|
1779
|
+
if (asked) emit({
|
|
1780
|
+
type: "questions",
|
|
1781
|
+
questions: asked.questions
|
|
1782
|
+
});
|
|
1783
|
+
};
|
|
1784
|
+
return {
|
|
1785
|
+
get interactionId() {
|
|
1786
|
+
return interactionId;
|
|
1787
|
+
},
|
|
1788
|
+
get environmentId() {
|
|
1789
|
+
return environmentId;
|
|
1790
|
+
},
|
|
1791
|
+
get status() {
|
|
1792
|
+
return status;
|
|
1793
|
+
},
|
|
1794
|
+
get usage() {
|
|
1795
|
+
return usage;
|
|
1796
|
+
},
|
|
1797
|
+
handle(event) {
|
|
1798
|
+
switch (event.event_type) {
|
|
1799
|
+
case "interaction.created":
|
|
1800
|
+
case "interaction.completed": {
|
|
1801
|
+
const it = event.interaction;
|
|
1802
|
+
if (it?.id && it.id !== interactionId) {
|
|
1803
|
+
interactionId = it.id;
|
|
1804
|
+
emit({
|
|
1805
|
+
type: "session-id",
|
|
1806
|
+
id: it.id
|
|
1807
|
+
});
|
|
1808
|
+
}
|
|
1809
|
+
if (it?.environment_id) environmentId = it.environment_id;
|
|
1810
|
+
if (it?.status) status = it.status;
|
|
1811
|
+
if (it?.usage) usage = it.usage;
|
|
1812
|
+
return;
|
|
1813
|
+
}
|
|
1814
|
+
case "interaction.status_update":
|
|
1815
|
+
if (event.status) status = event.status;
|
|
1816
|
+
return;
|
|
1817
|
+
case "step.start": {
|
|
1818
|
+
const step = event.step;
|
|
1819
|
+
if (event.index === void 0 || !step) return;
|
|
1820
|
+
steps.set(event.index, step);
|
|
1821
|
+
if (step.type === "function_call" && step.id) calls.set(event.index, {
|
|
1822
|
+
step,
|
|
1823
|
+
args: ""
|
|
1824
|
+
});
|
|
1825
|
+
else if (step.type === "function_result" && step.call_id) {
|
|
1826
|
+
const entry = [...calls].find(([, call]) => call.step.id === step.call_id);
|
|
1827
|
+
if (entry && isBlockedShell(entry[1].step, step)) {
|
|
1828
|
+
calls.delete(entry[0]);
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
if (entry) startCall(entry[0]);
|
|
1832
|
+
emit({
|
|
1833
|
+
type: "tool-end",
|
|
1834
|
+
id: step.call_id,
|
|
1835
|
+
ok: step.is_error !== true
|
|
1836
|
+
});
|
|
1837
|
+
} else if (step.type === "model_output") {
|
|
1838
|
+
texts.set(event.index, "");
|
|
1839
|
+
emit({
|
|
1840
|
+
type: "text-start",
|
|
1841
|
+
id: textId(event.index)
|
|
1842
|
+
});
|
|
1843
|
+
}
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
case "step.delta": {
|
|
1847
|
+
if (event.index === void 0) return;
|
|
1848
|
+
const call = calls.get(event.index);
|
|
1849
|
+
if (call && event.delta?.type === "arguments_delta") {
|
|
1850
|
+
call.args += event.delta.arguments ?? "";
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
if (steps.get(event.index)?.type !== "model_output") return;
|
|
1854
|
+
const text = event.delta?.type === "text" ? event.delta.text : void 0;
|
|
1855
|
+
if (!text) return;
|
|
1856
|
+
texts.set(event.index, (texts.get(event.index) ?? "") + text);
|
|
1857
|
+
emit({
|
|
1858
|
+
type: "text-delta",
|
|
1859
|
+
id: textId(event.index),
|
|
1860
|
+
text
|
|
1861
|
+
});
|
|
1862
|
+
return;
|
|
1863
|
+
}
|
|
1864
|
+
case "step.stop":
|
|
1865
|
+
if (event.index === void 0) return;
|
|
1866
|
+
if (calls.get(event.index)?.step.name !== SHELL_TOOL) startCall(event.index);
|
|
1867
|
+
finishText(event.index);
|
|
1868
|
+
return;
|
|
1869
|
+
case "error":
|
|
1870
|
+
emit({
|
|
1871
|
+
type: "error",
|
|
1872
|
+
text: event.error?.message ?? "Antigravity reported an error."
|
|
1873
|
+
});
|
|
1874
|
+
return;
|
|
1875
|
+
default: return;
|
|
1876
|
+
}
|
|
1877
|
+
},
|
|
1878
|
+
flush() {
|
|
1879
|
+
for (const index of Array.from(calls.keys())) startCall(index);
|
|
1880
|
+
for (const index of Array.from(texts.keys())) finishText(index);
|
|
1881
|
+
}
|
|
1882
|
+
};
|
|
1883
|
+
}
|
|
1884
|
+
async function* sseEvents(body) {
|
|
1885
|
+
const reader = body.getReader();
|
|
1886
|
+
const decoder = new TextDecoder();
|
|
1887
|
+
let buffer = "";
|
|
1888
|
+
for (;;) {
|
|
1889
|
+
const { done, value } = await reader.read();
|
|
1890
|
+
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
1891
|
+
let newline = buffer.indexOf("\n");
|
|
1892
|
+
while (newline !== -1) {
|
|
1893
|
+
const line = buffer.slice(0, newline).trimEnd();
|
|
1894
|
+
buffer = buffer.slice(newline + 1);
|
|
1895
|
+
if (line.startsWith("data:")) try {
|
|
1896
|
+
yield JSON.parse(line.slice(5));
|
|
1897
|
+
} catch {}
|
|
1898
|
+
newline = buffer.indexOf("\n");
|
|
1899
|
+
}
|
|
1900
|
+
if (done) return;
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
async function apiError(res) {
|
|
1904
|
+
const text = await res.text().catch(() => "");
|
|
1905
|
+
try {
|
|
1906
|
+
const parsed = JSON.parse(text);
|
|
1907
|
+
if (parsed.error?.message) return `Gemini API ${res.status}: ${parsed.error.message}`;
|
|
1908
|
+
} catch {}
|
|
1909
|
+
return `Gemini API ${res.status}${text ? `: ${text.slice(0, 300)}` : ""}`;
|
|
1910
|
+
}
|
|
1911
|
+
async function downloadSnapshot(key, environmentId, signal) {
|
|
1912
|
+
const url = `${API_BASE}/files/environment-${encodeURIComponent(environmentId)}:download?alt=media`;
|
|
1913
|
+
let last = "";
|
|
1914
|
+
for (let attempt = 0; attempt < SNAPSHOT_ATTEMPTS; attempt++) {
|
|
1915
|
+
if (attempt > 0) await new Promise((r) => setTimeout(r, 1500 * attempt));
|
|
1916
|
+
const res = await fetch(url, {
|
|
1917
|
+
headers: { "x-goog-api-key": key },
|
|
1918
|
+
signal
|
|
1919
|
+
});
|
|
1920
|
+
if (res.ok) return new Uint8Array(await res.arrayBuffer());
|
|
1921
|
+
last = await apiError(res);
|
|
1922
|
+
}
|
|
1923
|
+
throw new Error(`Could not download the sandbox: ${last}`);
|
|
1924
|
+
}
|
|
1925
|
+
async function localHashes(cwd, rels) {
|
|
1926
|
+
const out = /* @__PURE__ */ new Map();
|
|
1927
|
+
for (const rel of rels) {
|
|
1928
|
+
const data = await fs.readFile(path.join(cwd, rel)).catch(() => null);
|
|
1929
|
+
if (data) out.set(rel, hashBytes(data));
|
|
1930
|
+
}
|
|
1931
|
+
return out;
|
|
1932
|
+
}
|
|
1933
|
+
async function writeBack(opts, docRel, plan, tar, interactionId) {
|
|
1934
|
+
const entries = readTar(tar).flatMap((e) => {
|
|
1935
|
+
const rel = snapshotRel(e.path, docRel);
|
|
1936
|
+
return rel ? [{
|
|
1937
|
+
rel,
|
|
1938
|
+
data: e.data
|
|
1939
|
+
}] : [];
|
|
1940
|
+
});
|
|
1941
|
+
const uploaded = new Map(plan.files.map((f) => [f.rel, f.hash]));
|
|
1942
|
+
const local = await localHashes(opts.cwd, entries.map((e) => e.rel));
|
|
1943
|
+
const written = [];
|
|
1944
|
+
const conflicts = [];
|
|
1945
|
+
const conflictDir = path.join(opts.cwd, "node_modules", ".open-doc", "antigravity-conflicts", interactionId.replace(/[^\w-]/g, "").slice(-24));
|
|
1946
|
+
for (const decision of decideWrites(entries, uploaded, local)) {
|
|
1947
|
+
const target = decision.action === "write" ? path.join(opts.cwd, decision.rel) : path.join(conflictDir, decision.rel);
|
|
1948
|
+
const root = decision.action === "write" ? opts.docDir : conflictDir;
|
|
1949
|
+
if (!root || !isInsideRoots(opts.cwd, target, [root])) continue;
|
|
1950
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
1951
|
+
await fs.writeFile(target, decision.data);
|
|
1952
|
+
(decision.action === "write" ? written : conflicts).push(decision.rel);
|
|
1953
|
+
}
|
|
1954
|
+
return {
|
|
1955
|
+
written,
|
|
1956
|
+
conflicts,
|
|
1957
|
+
conflictDir
|
|
1958
|
+
};
|
|
1959
|
+
}
|
|
1960
|
+
var EnvironmentGoneError = class extends Error {};
|
|
1961
|
+
async function streamTurn(opts, key, body, roots) {
|
|
1962
|
+
const mapper = createAntigravityMapper(opts.emit, roots);
|
|
1963
|
+
const cancel = () => {
|
|
1964
|
+
const id = mapper.interactionId;
|
|
1965
|
+
if (!id) return;
|
|
1966
|
+
fetch(`${API_BASE}/interactions/${encodeURIComponent(id)}:cancel`, {
|
|
1967
|
+
method: "POST",
|
|
1968
|
+
headers: { "x-goog-api-key": key }
|
|
1969
|
+
}).catch(() => {});
|
|
1970
|
+
};
|
|
1971
|
+
opts.signal.addEventListener("abort", cancel, { once: true });
|
|
1972
|
+
const res = await fetch(`${API_BASE}/interactions`, {
|
|
1973
|
+
method: "POST",
|
|
1974
|
+
headers: {
|
|
1975
|
+
"x-goog-api-key": key,
|
|
1976
|
+
"content-type": "application/json"
|
|
1977
|
+
},
|
|
1978
|
+
body: JSON.stringify(body),
|
|
1979
|
+
signal: opts.signal
|
|
1980
|
+
});
|
|
1981
|
+
if (!res.ok || !res.body) {
|
|
1982
|
+
const message = await apiError(res);
|
|
1983
|
+
if (typeof body.environment === "string" && (res.status === 404 || res.status === 403)) throw new EnvironmentGoneError(`${message} The local environment was reset; send the message again.`);
|
|
1984
|
+
throw new Error(message);
|
|
1985
|
+
}
|
|
1986
|
+
try {
|
|
1987
|
+
for await (const event of sseEvents(res.body)) mapper.handle(event);
|
|
1988
|
+
} catch (err) {
|
|
1989
|
+
if (opts.signal.aborted) return null;
|
|
1990
|
+
throw err;
|
|
1991
|
+
} finally {
|
|
1992
|
+
mapper.flush();
|
|
1993
|
+
}
|
|
1994
|
+
if (opts.signal.aborted) return null;
|
|
1995
|
+
if (mapper.status === "failed" || mapper.status === "cancelled") throw new Error(`The Antigravity turn ${mapper.status}.`);
|
|
1996
|
+
if (mapper.status === "incomplete") opts.emit({
|
|
1997
|
+
type: "error",
|
|
1998
|
+
text: `Stopped at the ${MAX_TOTAL_TOKENS.toLocaleString("en-US")}-token budget for one turn. Send "continue" to go on.`
|
|
1999
|
+
});
|
|
2000
|
+
return mapper;
|
|
2001
|
+
}
|
|
2002
|
+
function createAntigravityProvider() {
|
|
2003
|
+
return {
|
|
2004
|
+
id: "antigravity",
|
|
2005
|
+
name: "Antigravity",
|
|
2006
|
+
sdkPackage: "",
|
|
2007
|
+
cliCommand: "",
|
|
2008
|
+
askInstruction: TEXT_QUESTIONS_INSTRUCTION,
|
|
2009
|
+
canRewind: false,
|
|
2010
|
+
canOpenBrowser: false,
|
|
2011
|
+
auth: "api-key",
|
|
2012
|
+
preview: true,
|
|
2013
|
+
consentVersion: ANTIGRAVITY_CONSENT_VERSION,
|
|
2014
|
+
hasKey: async (cwd) => await resolveApiKey(cwd) !== null,
|
|
2015
|
+
models: async () => MODELS,
|
|
2016
|
+
async runtime(cwd) {
|
|
2017
|
+
const version = await installedCliVersion(cwd);
|
|
2018
|
+
return {
|
|
2019
|
+
mode: version ? "local" : "cloud",
|
|
2020
|
+
localSupported: localModeSupported(),
|
|
2021
|
+
localVersion: version
|
|
2022
|
+
};
|
|
2023
|
+
},
|
|
2024
|
+
async install(cwd) {
|
|
2025
|
+
await installCli(cwd);
|
|
2026
|
+
},
|
|
2027
|
+
dispose: stopWorker,
|
|
2028
|
+
async run(opts) {
|
|
2029
|
+
if (!await hasConsent(opts.cwd, "antigravity", "2026-11-14.2")) throw new Error("Accept the Antigravity preview terms in the agent panel first.");
|
|
2030
|
+
const key = await resolveApiKey(opts.cwd);
|
|
2031
|
+
if (!key) throw new Error(`Set ${KEY_ENV} in the environment or .env to use Antigravity.`);
|
|
2032
|
+
if (!opts.docDir) throw new Error("Open a document first: Antigravity works on one document at a time.");
|
|
2033
|
+
const docRel = path.relative(opts.cwd, opts.docDir).split(path.sep).join("/");
|
|
2034
|
+
const turn = Date.now().toString(36);
|
|
2035
|
+
const local = await installedCliVersion(opts.cwd) !== null;
|
|
2036
|
+
const model = opts.model || null;
|
|
2037
|
+
const agentConfig = {
|
|
2038
|
+
type: "antigravity",
|
|
2039
|
+
max_total_tokens: MAX_TOTAL_TOKENS,
|
|
2040
|
+
...model ? { model } : {}
|
|
2041
|
+
};
|
|
2042
|
+
if (local) {
|
|
2043
|
+
const step = `agy-local-${turn}`;
|
|
2044
|
+
opts.emit({
|
|
2045
|
+
type: "tool-start",
|
|
2046
|
+
id: step,
|
|
2047
|
+
name: "sandbox",
|
|
2048
|
+
title: "Run on this computer",
|
|
2049
|
+
detail: docRel
|
|
2050
|
+
});
|
|
2051
|
+
let environment;
|
|
2052
|
+
try {
|
|
2053
|
+
environment = await ensureEnvironment(opts.cwd, key);
|
|
2054
|
+
await ensureWorker(opts.cwd, environment);
|
|
2055
|
+
opts.emit({
|
|
2056
|
+
type: "tool-end",
|
|
2057
|
+
id: step,
|
|
2058
|
+
ok: true
|
|
2059
|
+
});
|
|
2060
|
+
} catch (err) {
|
|
2061
|
+
opts.emit({
|
|
2062
|
+
type: "tool-end",
|
|
2063
|
+
id: step,
|
|
2064
|
+
ok: false
|
|
2065
|
+
});
|
|
2066
|
+
throw err;
|
|
2067
|
+
}
|
|
2068
|
+
const body = {
|
|
2069
|
+
agent: ANTIGRAVITY_AGENT,
|
|
2070
|
+
input: opts.prompt,
|
|
2071
|
+
system_instruction: `${opts.systemPromptAppend} ${localInstruction(docRel)}`,
|
|
2072
|
+
agent_config: agentConfig,
|
|
2073
|
+
environment: environment.id,
|
|
2074
|
+
...opts.resume ? { previous_interaction_id: opts.resume } : {},
|
|
2075
|
+
stream: true
|
|
2076
|
+
};
|
|
2077
|
+
let mapper;
|
|
2078
|
+
try {
|
|
2079
|
+
mapper = await streamTurn(opts, key, body, [opts.cwd]);
|
|
2080
|
+
} catch (err) {
|
|
2081
|
+
if (err instanceof EnvironmentGoneError) await forgetEnvironment(opts.cwd);
|
|
2082
|
+
throw err;
|
|
2083
|
+
}
|
|
2084
|
+
if (opts.signal.aborted || !mapper) return;
|
|
2085
|
+
opts.emit({
|
|
2086
|
+
type: "result",
|
|
2087
|
+
durationMs: null,
|
|
2088
|
+
costUsd: estimateCostUsd(model, mapper.usage)
|
|
2089
|
+
});
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
const upload = `agy-upload-${turn}`;
|
|
2093
|
+
opts.emit({
|
|
2094
|
+
type: "tool-start",
|
|
2095
|
+
id: upload,
|
|
2096
|
+
name: "sandbox",
|
|
2097
|
+
title: "Upload to cloud sandbox",
|
|
2098
|
+
detail: docRel
|
|
2099
|
+
});
|
|
2100
|
+
const plan = await collectUploads(opts.cwd, [opts.docDir, path.join(opts.cwd, ".agents", "skills")]);
|
|
2101
|
+
opts.emit({
|
|
2102
|
+
type: "tool-end",
|
|
2103
|
+
id: upload,
|
|
2104
|
+
ok: true
|
|
2105
|
+
});
|
|
2106
|
+
const mapper = await streamTurn(opts, key, {
|
|
2107
|
+
agent: ANTIGRAVITY_AGENT,
|
|
2108
|
+
input: opts.prompt,
|
|
2109
|
+
system_instruction: `${opts.systemPromptAppend} ${sandboxInstruction(docRel, plan.skipped)}`,
|
|
2110
|
+
agent_config: agentConfig,
|
|
2111
|
+
environment: {
|
|
2112
|
+
type: "remote",
|
|
2113
|
+
sources: plan.files.map((f) => ({
|
|
2114
|
+
type: "inline",
|
|
2115
|
+
content: f.content,
|
|
2116
|
+
target: `${SANDBOX_ROOT}/${f.rel}`
|
|
2117
|
+
}))
|
|
2118
|
+
},
|
|
2119
|
+
...opts.resume ? { previous_interaction_id: opts.resume } : {},
|
|
2120
|
+
stream: true
|
|
2121
|
+
}, [SANDBOX_ROOT]);
|
|
2122
|
+
if (opts.signal.aborted || !mapper) return;
|
|
2123
|
+
const environmentId = mapper.environmentId;
|
|
2124
|
+
if (environmentId && mapper.interactionId) {
|
|
2125
|
+
const sync = `agy-sync-${turn}`;
|
|
2126
|
+
opts.emit({
|
|
2127
|
+
type: "tool-start",
|
|
2128
|
+
id: sync,
|
|
2129
|
+
name: "sandbox",
|
|
2130
|
+
title: "Copy changes back",
|
|
2131
|
+
detail: docRel
|
|
2132
|
+
});
|
|
2133
|
+
try {
|
|
2134
|
+
const { written, conflicts, conflictDir } = await writeBack(opts, docRel, plan, await downloadSnapshot(key, environmentId, opts.signal), mapper.interactionId);
|
|
2135
|
+
opts.emit({
|
|
2136
|
+
type: "tool-end",
|
|
2137
|
+
id: sync,
|
|
2138
|
+
ok: true
|
|
2139
|
+
});
|
|
2140
|
+
if (written.length > 0) {
|
|
2141
|
+
opts.emit({
|
|
2142
|
+
type: "tool-start",
|
|
2143
|
+
id: `agy-written-${turn}`,
|
|
2144
|
+
name: "file_change",
|
|
2145
|
+
title: "Edit",
|
|
2146
|
+
detail: clipDetail(written.join(", "))
|
|
2147
|
+
});
|
|
2148
|
+
opts.emit({
|
|
2149
|
+
type: "tool-end",
|
|
2150
|
+
id: `agy-written-${turn}`,
|
|
2151
|
+
ok: true
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
if (conflicts.length > 0) opts.emit({
|
|
2155
|
+
type: "error",
|
|
2156
|
+
text: `Not copied back, because they changed on this machine during the turn: ${conflicts.join(", ")}. The agent's versions are in ${path.relative(opts.cwd, conflictDir)}.`
|
|
2157
|
+
});
|
|
2158
|
+
} catch (err) {
|
|
2159
|
+
opts.emit({
|
|
2160
|
+
type: "tool-end",
|
|
2161
|
+
id: sync,
|
|
2162
|
+
ok: false
|
|
2163
|
+
});
|
|
2164
|
+
throw err;
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
opts.emit({
|
|
2168
|
+
type: "result",
|
|
2169
|
+
durationMs: null,
|
|
2170
|
+
costUsd: estimateCostUsd(model, mapper.usage)
|
|
2171
|
+
});
|
|
2172
|
+
}
|
|
2173
|
+
};
|
|
2174
|
+
}
|
|
1225
2175
|
//#endregion
|
|
1226
2176
|
//#region src/agent/detect.ts
|
|
1227
2177
|
const IS_WINDOWS = process.platform === "win32";
|
|
@@ -1360,7 +2310,7 @@ async function importWorkspacePackage(userCwd, name) {
|
|
|
1360
2310
|
}
|
|
1361
2311
|
//#endregion
|
|
1362
2312
|
//#region src/agent/types.ts
|
|
1363
|
-
const AGENT_PROVIDER_IDS = ["codex"];
|
|
2313
|
+
const AGENT_PROVIDER_IDS = ["codex", "antigravity"];
|
|
1364
2314
|
const AGENT_EFFORTS = [
|
|
1365
2315
|
"minimal",
|
|
1366
2316
|
"low",
|
|
@@ -1608,7 +2558,10 @@ function createCodexProvider() {
|
|
|
1608
2558
|
//#endregion
|
|
1609
2559
|
//#region src/agent/providers/index.ts
|
|
1610
2560
|
function createProviders() {
|
|
1611
|
-
return {
|
|
2561
|
+
return {
|
|
2562
|
+
codex: createCodexProvider(),
|
|
2563
|
+
antigravity: createAntigravityProvider()
|
|
2564
|
+
};
|
|
1612
2565
|
}
|
|
1613
2566
|
//#endregion
|
|
1614
2567
|
//#region src/agent/store.ts
|
|
@@ -2078,8 +3031,11 @@ function createAgentRunner(opts) {
|
|
|
2078
3031
|
};
|
|
2079
3032
|
const promptOpts = promptOptions();
|
|
2080
3033
|
const prompt = buildTurnPrompt(promptText, context, promptOpts.docsDir);
|
|
3034
|
+
const docId = context?.docId ?? session.docId;
|
|
2081
3035
|
provider.run({
|
|
2082
3036
|
cwd: userCwd,
|
|
3037
|
+
docsDir: promptOpts.docsDir,
|
|
3038
|
+
docDir: docId ? path.join(userCwd, promptOpts.docsDir, docId) : null,
|
|
2083
3039
|
writableRoots,
|
|
2084
3040
|
prompt,
|
|
2085
3041
|
systemPromptAppend: systemPromptAppend(provider.askInstruction, {
|
|
@@ -2173,6 +3129,33 @@ function createAgentRunner(opts) {
|
|
|
2173
3129
|
//#region src/agent/status.ts
|
|
2174
3130
|
const INSTALL_TIMEOUT_MS = 6e5;
|
|
2175
3131
|
async function providerStatus(userCwd, provider) {
|
|
3132
|
+
const consent = provider.consentVersion ? {
|
|
3133
|
+
version: provider.consentVersion,
|
|
3134
|
+
accepted: await hasConsent(userCwd, provider.id, provider.consentVersion)
|
|
3135
|
+
} : null;
|
|
3136
|
+
const runtime = provider.runtime ? await provider.runtime(userCwd) : null;
|
|
3137
|
+
const shared = {
|
|
3138
|
+
preview: provider.preview === true,
|
|
3139
|
+
consent,
|
|
3140
|
+
runtime
|
|
3141
|
+
};
|
|
3142
|
+
if (provider.auth === "api-key") {
|
|
3143
|
+
const [keyFound, models] = await Promise.all([provider.hasKey ? provider.hasKey(userCwd) : Promise.resolve(false), provider.models()]);
|
|
3144
|
+
return {
|
|
3145
|
+
id: provider.id,
|
|
3146
|
+
name: provider.name,
|
|
3147
|
+
sdkPackage: "",
|
|
3148
|
+
sdkVersion: null,
|
|
3149
|
+
cliCommand: "",
|
|
3150
|
+
cliFound: false,
|
|
3151
|
+
cliVersion: null,
|
|
3152
|
+
ready: keyFound && consent?.accepted !== false,
|
|
3153
|
+
models,
|
|
3154
|
+
auth: "api-key",
|
|
3155
|
+
keyFound,
|
|
3156
|
+
...shared
|
|
3157
|
+
};
|
|
3158
|
+
}
|
|
2176
3159
|
const sdk = resolveWorkspacePackage(userCwd, provider.sdkPackage);
|
|
2177
3160
|
const cliPath = findOnPath(provider.cliCommand);
|
|
2178
3161
|
const [cliVersion, models] = await Promise.all([cliPath ? commandVersion(cliPath) : null, provider.models()]);
|
|
@@ -2184,8 +3167,11 @@ async function providerStatus(userCwd, provider) {
|
|
|
2184
3167
|
cliCommand: provider.cliCommand,
|
|
2185
3168
|
cliFound: cliPath !== null,
|
|
2186
3169
|
cliVersion,
|
|
2187
|
-
ready: sdk !== null,
|
|
2188
|
-
models
|
|
3170
|
+
ready: sdk !== null && consent?.accepted !== false,
|
|
3171
|
+
models,
|
|
3172
|
+
auth: "sdk",
|
|
3173
|
+
keyFound: null,
|
|
3174
|
+
...shared
|
|
2189
3175
|
};
|
|
2190
3176
|
}
|
|
2191
3177
|
async function agentStatus(userCwd, providers, packageManager) {
|
|
@@ -4015,6 +5001,7 @@ function registerAgentRoutes(server, ctx) {
|
|
|
4015
5001
|
clearInterval(heartbeat);
|
|
4016
5002
|
for (const stream of streams) stream.end();
|
|
4017
5003
|
runner.stopAll();
|
|
5004
|
+
for (const provider of Object.values(providers)) provider.dispose?.();
|
|
4018
5005
|
store.flush();
|
|
4019
5006
|
});
|
|
4020
5007
|
const installsInFlight = /* @__PURE__ */ new Map();
|
|
@@ -4054,6 +5041,11 @@ function registerAgentRoutes(server, ctx) {
|
|
|
4054
5041
|
const body = await readBody(req, AGENT_BODY_LIMIT);
|
|
4055
5042
|
if (!isAgentProviderId(body.provider)) return json(res, 400, { error: "invalid provider" });
|
|
4056
5043
|
const provider = providers[body.provider];
|
|
5044
|
+
if (provider.install) {
|
|
5045
|
+
await provider.install(ctx.userCwd);
|
|
5046
|
+
return json(res, 200, { ok: true });
|
|
5047
|
+
}
|
|
5048
|
+
if (provider.auth === "api-key") return json(res, 400, { error: `${provider.name} needs an API key, not an install` });
|
|
4057
5049
|
const pm = await detectPackageManager(ctx.userCwd);
|
|
4058
5050
|
const pkg = provider.sdkPackage;
|
|
4059
5051
|
let install = installsInFlight.get(pkg);
|
|
@@ -4064,6 +5056,17 @@ function registerAgentRoutes(server, ctx) {
|
|
|
4064
5056
|
await install;
|
|
4065
5057
|
return json(res, 200, { ok: true });
|
|
4066
5058
|
}
|
|
5059
|
+
if (method === "POST" && url.pathname === "/consent") {
|
|
5060
|
+
const guard = validateMutationRequest(req, { requireJsonBody: true });
|
|
5061
|
+
if (!guard.ok) return json(res, guard.status, { error: guard.error });
|
|
5062
|
+
const body = await readBody(req, AGENT_BODY_LIMIT);
|
|
5063
|
+
if (!isAgentProviderId(body.provider)) return json(res, 400, { error: "invalid provider" });
|
|
5064
|
+
const version = providers[body.provider].consentVersion;
|
|
5065
|
+
if (!version) return json(res, 400, { error: "this agent has no terms to accept" });
|
|
5066
|
+
if (body.accept === true && body.version !== version) return json(res, 409, { error: "the terms changed; reload to read the new version" });
|
|
5067
|
+
await setConsent(ctx.userCwd, body.provider, body.accept === true ? version : null);
|
|
5068
|
+
return json(res, 200, { ok: true });
|
|
5069
|
+
}
|
|
4067
5070
|
if (method === "GET" && url.pathname === "/sessions") {
|
|
4068
5071
|
res.setHeader("cache-control", "no-store");
|
|
4069
5072
|
return json(res, 200, { sessions: store.list() });
|