@heyhuynhgiabuu/pi-oauth-antigravity 0.1.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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +91 -0
- package/dist/auth/index.d.ts +2 -0
- package/dist/auth/index.js +1 -0
- package/dist/auth/oauth.d.ts +18 -0
- package/dist/auth/oauth.js +374 -0
- package/dist/client/client.d.ts +35 -0
- package/dist/client/client.js +448 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/diagnostics/diagnostics.d.ts +31 -0
- package/dist/diagnostics/diagnostics.js +68 -0
- package/dist/diagnostics/index.d.ts +1 -0
- package/dist/diagnostics/index.js +1 -0
- package/dist/image/image.d.ts +62 -0
- package/dist/image/image.js +244 -0
- package/dist/image/index.d.ts +1 -0
- package/dist/image/index.js +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +182 -0
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.js +1 -0
- package/dist/models/models.d.ts +44 -0
- package/dist/models/models.js +377 -0
- package/dist/stream/index.d.ts +1 -0
- package/dist/stream/index.js +1 -0
- package/dist/stream/stream.d.ts +24 -0
- package/dist/stream/stream.js +879 -0
- package/dist/types/enums.d.ts +41 -0
- package/dist/types/enums.js +49 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/types.d.ts +259 -0
- package/dist/types/types.js +2 -0
- package/dist/usage/index.d.ts +1 -0
- package/dist/usage/index.js +1 -0
- package/dist/usage/usage.d.ts +8 -0
- package/dist/usage/usage.js +388 -0
- package/dist/utils/http.d.ts +7 -0
- package/dist/utils/http.js +81 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/security.d.ts +8 -0
- package/dist/utils/security.js +60 -0
- package/dist/utils/util.d.ts +40 -0
- package/dist/utils/util.js +176 -0
- package/package.json +73 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { ANTIGRAVITY_MODEL_ENUM } from "../models/models.js";
|
|
6
|
+
export function antigravityEnv(name) {
|
|
7
|
+
return process.env[`ANTIGRAVITY_${name}`] || process.env[`NOAGY_${name}`];
|
|
8
|
+
}
|
|
9
|
+
export function isRecord(value) {
|
|
10
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
export function asString(value) {
|
|
13
|
+
return typeof value === "string" && value ? value : undefined;
|
|
14
|
+
}
|
|
15
|
+
export function sanitizeText(text) {
|
|
16
|
+
return String(text ?? "").replace(/[\uD800-\uDFFF]/g, "\uFFFD");
|
|
17
|
+
}
|
|
18
|
+
export function escapeHtml(text) {
|
|
19
|
+
return text
|
|
20
|
+
.replace(/&/g, "&")
|
|
21
|
+
.replace(/</g, "<")
|
|
22
|
+
.replace(/>/g, ">")
|
|
23
|
+
.replace(/"/g, """)
|
|
24
|
+
.replace(/'/g, "'");
|
|
25
|
+
}
|
|
26
|
+
export function escapeRegExp(text) {
|
|
27
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
28
|
+
}
|
|
29
|
+
const INT63_MASK = (1n << 63n) - 1n;
|
|
30
|
+
function formatSignedDecimalSessionId(value) {
|
|
31
|
+
return `-${(value & INT63_MASK).toString()}`;
|
|
32
|
+
}
|
|
33
|
+
export function deriveSignedDecimalFromHash(text) {
|
|
34
|
+
const digest = createHash("sha256").update(text).digest();
|
|
35
|
+
return formatSignedDecimalSessionId(digest.readBigUInt64BE(0));
|
|
36
|
+
}
|
|
37
|
+
export function randomSignedDecimalSessionId() {
|
|
38
|
+
return formatSignedDecimalSessionId(randomBytes(8).readBigUInt64BE(0));
|
|
39
|
+
}
|
|
40
|
+
export function getFirstUserTextForAntigravitySession(context) {
|
|
41
|
+
for (const message of context?.messages ?? []) {
|
|
42
|
+
if (message.role !== "user")
|
|
43
|
+
continue;
|
|
44
|
+
if (typeof message.content === "string")
|
|
45
|
+
return message.content;
|
|
46
|
+
if (Array.isArray(message.content)) {
|
|
47
|
+
for (const item of message.content) {
|
|
48
|
+
if (typeof item === "object" && item !== null && item.type === "text") {
|
|
49
|
+
return item.text;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
export function deriveAntigravitySessionId(context) {
|
|
57
|
+
const text = getFirstUserTextForAntigravitySession(context)?.trim();
|
|
58
|
+
return text ? deriveSignedDecimalFromHash(text) : randomSignedDecimalSessionId();
|
|
59
|
+
}
|
|
60
|
+
const sessionStates = new Map();
|
|
61
|
+
const MAX_SESSIONS = 200;
|
|
62
|
+
let sessionsLoaded = false;
|
|
63
|
+
function getSessionsFilePath() {
|
|
64
|
+
const envPath = antigravityEnv("SESSIONS_FILE");
|
|
65
|
+
if (envPath)
|
|
66
|
+
return envPath;
|
|
67
|
+
const agentCache = join(homedir(), ".pi", "agent", "cache");
|
|
68
|
+
if (existsSync(join(homedir(), ".pi", "agent"))) {
|
|
69
|
+
return join(agentCache, "antigravity-sessions.json");
|
|
70
|
+
}
|
|
71
|
+
return join(homedir(), ".pi", "cache", "antigravity-sessions.json");
|
|
72
|
+
}
|
|
73
|
+
function loadPersistedSessions() {
|
|
74
|
+
if (sessionsLoaded)
|
|
75
|
+
return;
|
|
76
|
+
sessionsLoaded = true;
|
|
77
|
+
try {
|
|
78
|
+
const file = getSessionsFilePath();
|
|
79
|
+
if (!existsSync(file))
|
|
80
|
+
return;
|
|
81
|
+
const content = readFileSync(file, "utf8");
|
|
82
|
+
const parsed = JSON.parse(content);
|
|
83
|
+
if (Array.isArray(parsed)) {
|
|
84
|
+
for (const item of parsed) {
|
|
85
|
+
if (item && typeof item === "object" && typeof item.sessionId === "string") {
|
|
86
|
+
sessionStates.set(item.sessionId, item);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Ignore corrupt or unreadable cache file
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export function persistAntigravitySessions() {
|
|
96
|
+
try {
|
|
97
|
+
const file = getSessionsFilePath();
|
|
98
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
99
|
+
const data = [...sessionStates.values()];
|
|
100
|
+
writeFileSync(file, JSON.stringify(data), "utf8");
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// Ignore write errors
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Reset in-memory cache and load state to simulate a fresh process restart. */
|
|
107
|
+
export function resetAntigravitySessionMemory() {
|
|
108
|
+
sessionStates.clear();
|
|
109
|
+
sessionsLoaded = false;
|
|
110
|
+
}
|
|
111
|
+
export function clearAntigravitySessions() {
|
|
112
|
+
sessionStates.clear();
|
|
113
|
+
sessionsLoaded = true;
|
|
114
|
+
try {
|
|
115
|
+
const file = getSessionsFilePath();
|
|
116
|
+
if (existsSync(file))
|
|
117
|
+
unlinkSync(file);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// Ignore error
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export function getOrCreateAntigravitySession(sessionId) {
|
|
124
|
+
loadPersistedSessions();
|
|
125
|
+
let state = sessionStates.get(sessionId);
|
|
126
|
+
if (!state) {
|
|
127
|
+
state = {
|
|
128
|
+
agentId: randomUUID(),
|
|
129
|
+
trajectoryId: randomUUID(),
|
|
130
|
+
sessionId,
|
|
131
|
+
stepIndex: 1,
|
|
132
|
+
lastUsedAt: Date.now(),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
state.stepIndex += 1;
|
|
137
|
+
state.lastUsedAt = Date.now();
|
|
138
|
+
sessionStates.delete(sessionId);
|
|
139
|
+
}
|
|
140
|
+
sessionStates.set(sessionId, state);
|
|
141
|
+
while (sessionStates.size > MAX_SESSIONS) {
|
|
142
|
+
const oldest = sessionStates.keys().next().value;
|
|
143
|
+
if (!oldest)
|
|
144
|
+
break;
|
|
145
|
+
sessionStates.delete(oldest);
|
|
146
|
+
}
|
|
147
|
+
persistAntigravitySessions();
|
|
148
|
+
return state;
|
|
149
|
+
}
|
|
150
|
+
export function nowRequestId() {
|
|
151
|
+
return antigravityRequestEnvelope("unknown", false).requestId;
|
|
152
|
+
}
|
|
153
|
+
export function antigravityRequestEnvelope(wireModelId, isClaude, state) {
|
|
154
|
+
const agentId = state?.agentId ?? randomUUID();
|
|
155
|
+
const trajectoryId = state?.trajectoryId ?? randomUUID();
|
|
156
|
+
const step = state?.stepIndex ?? 2;
|
|
157
|
+
const sessionId = state?.sessionId ?? randomSignedDecimalSessionId();
|
|
158
|
+
const usageLabel = isClaude ? "true" : "false";
|
|
159
|
+
const labels = {
|
|
160
|
+
last_step_index: String(step - 1),
|
|
161
|
+
trajectory_id: trajectoryId,
|
|
162
|
+
used_claude: usageLabel,
|
|
163
|
+
used_claude_conservative: usageLabel,
|
|
164
|
+
};
|
|
165
|
+
if (state?.lastExecutionId) {
|
|
166
|
+
labels.last_execution_id = state.lastExecutionId;
|
|
167
|
+
}
|
|
168
|
+
const modelEnum = ANTIGRAVITY_MODEL_ENUM[wireModelId];
|
|
169
|
+
if (modelEnum)
|
|
170
|
+
labels.model_enum = modelEnum;
|
|
171
|
+
return {
|
|
172
|
+
requestId: `agent/${agentId}/${Date.now()}/${trajectoryId}/${step}`,
|
|
173
|
+
sessionId,
|
|
174
|
+
labels,
|
|
175
|
+
};
|
|
176
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@heyhuynhgiabuu/pi-oauth-antigravity",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Google Antigravity OAuth provider extension for Pi with Gemini 3.8, prompt cache session affinity, and trajectory chaining.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"CHANGELOG.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "tsx --test test/**/*.test.ts",
|
|
24
|
+
"prepack": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"pi",
|
|
28
|
+
"pi-extension",
|
|
29
|
+
"pi-package",
|
|
30
|
+
"antigravity",
|
|
31
|
+
"gemini",
|
|
32
|
+
"google-cloud-code-assist",
|
|
33
|
+
"oauth",
|
|
34
|
+
"prompt-caching"
|
|
35
|
+
],
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"author": "huynhgiabuu",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/heyhuynhgiabuu/pi-oauth-antigravity.git"
|
|
41
|
+
},
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/heyhuynhgiabuu/pi-oauth-antigravity/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/heyhuynhgiabuu/pi-oauth-antigravity#readme",
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@earendil-works/pi-ai": "*",
|
|
51
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@earendil-works/pi-coding-agent": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"undici": "^8.10.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@earendil-works/pi-ai": "^0.84.4",
|
|
63
|
+
"@earendil-works/pi-coding-agent": "^0.84.4",
|
|
64
|
+
"@types/node": "^22.20.1",
|
|
65
|
+
"tsx": "^4.20.6",
|
|
66
|
+
"typescript": "^5.9.3"
|
|
67
|
+
},
|
|
68
|
+
"pi": {
|
|
69
|
+
"extensions": [
|
|
70
|
+
"./dist/index.js"
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
}
|