@herbertgao/pi-extensions 2026.11.18 → 2026.11.20
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 +18 -0
- package/README.md +5 -5
- package/THIRD_PARTY_NOTICES.md +3 -24
- package/node_modules/@estebanforge/pi-antigravity-bridge/CHANGELOG.md +956 -0
- package/node_modules/{pi-antigravity → @estebanforge/pi-antigravity-bridge}/LICENSE +1 -1
- package/node_modules/@estebanforge/pi-antigravity-bridge/README.md +240 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/extensions/index.ts +2117 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/package.json +61 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/connection.ts +644 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/driver.ts +1043 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/events.ts +323 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/home.ts +145 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/jsonrpc.ts +243 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/setup.ts +107 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/usage-estimate.ts +61 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agents.ts +64 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-install.ts +93 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-paths.ts +30 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-setup.ts +81 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-version.ts +289 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-detect.ts +161 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-gate.ts +233 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-hook.ts +325 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts-ui.ts +88 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts.ts +144 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/ask-tool.ts +1221 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/background-watch.ts +225 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/bridge-catalog.ts +197 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/config.ts +523 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/daily-log.ts +196 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/dialog-lock.ts +48 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/diff-render.ts +207 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver-types.ts +168 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver.ts +907 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/engine-picker.ts +160 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/frame-guard.ts +38 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/handoff-toast.ts +21 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-registration.ts +376 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-server.ts +618 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/models.ts +404 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/native-tools.ts +104 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/parked-turn.ts +125 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/patch-cleanup.ts +523 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/poll-fold.ts +55 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/prompt-images.ts +92 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/provider.ts +2214 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/queued-messages.ts +152 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/quota-notice.ts +40 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/redact.ts +27 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/sessions.ts +197 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/skills.ts +207 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-events.ts +161 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-sanitizer.ts +180 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/subagent-roster.ts +245 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/sysprompt-agent.ts +130 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks-ui.ts +90 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks.ts +174 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/usage.ts +174 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/web-tools.ts +320 -0
- package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +1 -1
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +3 -1
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +317 -105
- package/node_modules/@herbertgao/pi-subagents/SECURITY.md +2 -2
- package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +240 -36
- package/node_modules/@herbertgao/pi-subagents/docs/translations.md +63 -0
- package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +1 -1
- package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
- package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +2 -0
- package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +89 -6
- package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +6 -2
- package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +20 -11
- package/node_modules/@herbertgao/pi-subagents/src/cache-retention.ts +39 -0
- package/node_modules/@herbertgao/pi-subagents/src/completion-outbox.ts +5 -0
- package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +185 -44
- package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +7 -0
- package/node_modules/@herbertgao/pi-subagents/src/group-join.ts +9 -0
- package/node_modules/@herbertgao/pi-subagents/src/index.ts +126 -116
- package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +3 -2
- package/node_modules/@herbertgao/pi-subagents/src/settings.ts +21 -0
- package/node_modules/@herbertgao/pi-subagents/src/subagent-info.ts +115 -0
- package/node_modules/@herbertgao/pi-subagents/src/types.ts +11 -7
- package/node_modules/@herbertgao/pi-subagents/src/ui/agent-widget.ts +2 -3
- package/node_modules/@herbertgao/pi-subagents/src/ui/conversation-viewer.ts +1 -1
- package/node_modules/@herbertgao/pi-subagents/src/workflow/meta.ts +2 -2
- package/node_modules/@herbertgao/pi-subagents/src/workflow-info.ts +184 -0
- package/node_modules/pi-commandcode-provider/CHANGELOG.md +10 -0
- package/node_modules/pi-commandcode-provider/README.md +3 -2
- package/node_modules/pi-commandcode-provider/index.ts +120 -13
- package/node_modules/pi-commandcode-provider/package.json +4 -3
- package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +11 -2
- package/node_modules/pi-commandcode-provider/src/pricing.ts +17 -1
- package/node_modules/pi-commandcode-provider/src/quota-command.ts +3 -2
- package/node_modules/pi-commandcode-provider/src/zdr.ts +163 -0
- package/node_modules/pi-typesafe/README.md +1 -1
- package/node_modules/pi-typesafe/dist/ask.js +6 -3
- package/node_modules/pi-typesafe/dist/auth.d.ts +31 -7
- package/node_modules/pi-typesafe/dist/auth.js +159 -55
- package/node_modules/pi-typesafe/dist/batch.d.ts +16 -2
- package/node_modules/pi-typesafe/dist/batch.js +62 -22
- package/node_modules/pi-typesafe/dist/calibrate.d.ts +38 -11
- package/node_modules/pi-typesafe/dist/calibrate.js +56 -24
- package/node_modules/pi-typesafe/dist/client.d.ts +13 -0
- package/node_modules/pi-typesafe/dist/client.js +67 -27
- package/node_modules/pi-typesafe/dist/errors.d.ts +20 -0
- package/node_modules/pi-typesafe/dist/errors.js +105 -2
- package/node_modules/pi-typesafe/dist/extension.d.ts +106 -0
- package/node_modules/pi-typesafe/dist/extension.js +206 -106
- package/node_modules/pi-typesafe/dist/index.d.ts +8 -8
- package/node_modules/pi-typesafe/dist/index.js +3 -3
- package/node_modules/pi-typesafe/dist/key-prompt.d.ts +20 -0
- package/node_modules/pi-typesafe/dist/key-prompt.js +23 -3
- package/node_modules/pi-typesafe/dist/login.d.ts +58 -1
- package/node_modules/pi-typesafe/dist/login.js +167 -13
- package/node_modules/pi-typesafe/dist/schema.d.ts +3 -0
- package/node_modules/pi-typesafe/dist/schema.js +47 -13
- package/node_modules/pi-typesafe/dist/ui.d.ts +2 -1
- package/node_modules/pi-typesafe/dist/usage.d.ts +53 -0
- package/node_modules/pi-typesafe/dist/usage.js +79 -40
- package/node_modules/pi-typesafe/package.json +3 -3
- package/package.json +12 -15
- package/node_modules/pi-antigravity/README.md +0 -298
- package/node_modules/pi-antigravity/docs/reader-guarantees.md +0 -35
- package/node_modules/pi-antigravity/package.json +0 -87
- package/node_modules/pi-antigravity/src/auth/accounts.ts +0 -327
- package/node_modules/pi-antigravity/src/auth/index.ts +0 -23
- package/node_modules/pi-antigravity/src/auth/oauth.ts +0 -445
- package/node_modules/pi-antigravity/src/client/client.ts +0 -602
- package/node_modules/pi-antigravity/src/client/enterprise.ts +0 -150
- package/node_modules/pi-antigravity/src/client/index.ts +0 -2
- package/node_modules/pi-antigravity/src/diagnostics/diagnostics.ts +0 -104
- package/node_modules/pi-antigravity/src/diagnostics/index.ts +0 -1
- package/node_modules/pi-antigravity/src/features/generate-image.ts +0 -132
- package/node_modules/pi-antigravity/src/features/google-search.ts +0 -72
- package/node_modules/pi-antigravity/src/features/read-url-content.ts +0 -124
- package/node_modules/pi-antigravity/src/image/image.ts +0 -427
- package/node_modules/pi-antigravity/src/image/index.ts +0 -1
- package/node_modules/pi-antigravity/src/image/native.ts +0 -69
- package/node_modules/pi-antigravity/src/index.ts +0 -547
- package/node_modules/pi-antigravity/src/models/discovery.ts +0 -176
- package/node_modules/pi-antigravity/src/models/grouping.ts +0 -487
- package/node_modules/pi-antigravity/src/models/index.ts +0 -3
- package/node_modules/pi-antigravity/src/models/models.ts +0 -647
- package/node_modules/pi-antigravity/src/search/index.ts +0 -1
- package/node_modules/pi-antigravity/src/search/search.ts +0 -967
- package/node_modules/pi-antigravity/src/stream/index.ts +0 -1
- package/node_modules/pi-antigravity/src/stream/stream.ts +0 -1961
- package/node_modules/pi-antigravity/src/types/enums.ts +0 -52
- package/node_modules/pi-antigravity/src/types/index.ts +0 -2
- package/node_modules/pi-antigravity/src/types/types.ts +0 -329
- package/node_modules/pi-antigravity/src/usage/index.ts +0 -1
- package/node_modules/pi-antigravity/src/usage/usage.ts +0 -437
- package/node_modules/pi-antigravity/src/utils/address.ts +0 -180
- package/node_modules/pi-antigravity/src/utils/credits.ts +0 -149
- package/node_modules/pi-antigravity/src/utils/html.ts +0 -31
- package/node_modules/pi-antigravity/src/utils/http.ts +0 -96
- package/node_modules/pi-antigravity/src/utils/index.ts +0 -5
- package/node_modules/pi-antigravity/src/utils/security.ts +0 -278
- package/node_modules/pi-antigravity/src/utils/text.ts +0 -66
- package/node_modules/pi-antigravity/src/utils/util.ts +0 -217
- package/node_modules/pi-antigravity/src/web-fetch/document.ts +0 -83
- package/node_modules/pi-antigravity/src/web-fetch/fences.ts +0 -24
- package/node_modules/pi-antigravity/src/web-fetch/header.ts +0 -97
- package/node_modules/pi-antigravity/src/web-fetch/index.ts +0 -3
- package/node_modules/pi-antigravity/src/web-fetch/inspect.ts +0 -328
- package/node_modules/pi-antigravity/src/web-fetch/limits.ts +0 -4
- package/node_modules/pi-antigravity/src/web-fetch/markdown.ts +0 -744
- package/node_modules/pi-antigravity/src/web-fetch/mdn.ts +0 -164
- package/node_modules/pi-antigravity/src/web-fetch/media.ts +0 -11
- package/node_modules/pi-antigravity/src/web-fetch/metadata.ts +0 -9
- package/node_modules/pi-antigravity/src/web-fetch/representation.ts +0 -93
- package/node_modules/pi-antigravity/src/web-fetch/save.ts +0 -71
- package/node_modules/pi-antigravity/src/web-fetch/sites.ts +0 -138
- package/node_modules/pi-antigravity/src/web-fetch/vendor.d.ts +0 -9
- package/node_modules/pi-antigravity/src/web-fetch/web-fetch.ts +0 -957
- package/node_modules/pi-antigravity/tsconfig.json +0 -21
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { antigravityEnv, isRecord } from "./util.js";
|
|
5
|
-
|
|
6
|
-
const AGY_SETTINGS_FILE = join(homedir(), ".gemini", "antigravity-cli", "settings.json");
|
|
7
|
-
|
|
8
|
-
function getPiSettingsPath(): string {
|
|
9
|
-
const agentDir = process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
10
|
-
return join(agentDir, "settings.json");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type CreditsStatus = {
|
|
14
|
-
enabled: boolean;
|
|
15
|
-
source: "env" | "pi_settings" | "antigravity_settings" | "default";
|
|
16
|
-
detail: string;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Check whether credit overages are explicitly configured via env or settings.
|
|
21
|
-
*/
|
|
22
|
-
export function getCreditsStatus(): CreditsStatus {
|
|
23
|
-
// 1. Environment variable check
|
|
24
|
-
const envVal =
|
|
25
|
-
antigravityEnv("USE_CREDITS") ?? antigravityEnv("USE_G1_CREDITS") ?? process.env.USE_G1_CREDITS;
|
|
26
|
-
if (envVal !== undefined) {
|
|
27
|
-
const trimmed = envVal.trim().toLowerCase();
|
|
28
|
-
const isTrue = ["1", "true", "yes", "on", "enable", "enabled"].includes(trimmed);
|
|
29
|
-
const isFalse = ["0", "false", "no", "off", "disable", "disabled"].includes(trimmed);
|
|
30
|
-
if (isTrue || isFalse) {
|
|
31
|
-
return {
|
|
32
|
-
enabled: isTrue,
|
|
33
|
-
source: "env",
|
|
34
|
-
detail: `Environment variable (ANTIGRAVITY_USE_CREDITS=${envVal})`,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// 2. Pi settings (~/.pi/agent/settings.json)
|
|
40
|
-
const piSettingsPath = getPiSettingsPath();
|
|
41
|
-
try {
|
|
42
|
-
if (existsSync(piSettingsPath)) {
|
|
43
|
-
const parsed = JSON.parse(readFileSync(piSettingsPath, "utf8")) as Record<string, unknown>;
|
|
44
|
-
if (typeof parsed.useG1Credits === "boolean") {
|
|
45
|
-
return {
|
|
46
|
-
enabled: parsed.useG1Credits,
|
|
47
|
-
source: "pi_settings",
|
|
48
|
-
detail: `~/.pi/agent/settings.json (useG1Credits: ${parsed.useG1Credits})`,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
if (typeof parsed.useCredits === "boolean") {
|
|
52
|
-
return {
|
|
53
|
-
enabled: parsed.useCredits,
|
|
54
|
-
source: "pi_settings",
|
|
55
|
-
detail: `~/.pi/agent/settings.json (useCredits: ${parsed.useCredits})`,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
if (isRecord(parsed.antigravity)) {
|
|
59
|
-
if (typeof parsed.antigravity.useCredits === "boolean") {
|
|
60
|
-
return {
|
|
61
|
-
enabled: parsed.antigravity.useCredits,
|
|
62
|
-
source: "pi_settings",
|
|
63
|
-
detail: `~/.pi/agent/settings.json (antigravity.useCredits: ${parsed.antigravity.useCredits})`,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
if (typeof parsed.antigravity.useG1Credits === "boolean") {
|
|
67
|
-
return {
|
|
68
|
-
enabled: parsed.antigravity.useG1Credits,
|
|
69
|
-
source: "pi_settings",
|
|
70
|
-
detail: `~/.pi/agent/settings.json (antigravity.useG1Credits: ${parsed.antigravity.useG1Credits})`,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
} catch {
|
|
76
|
-
// Unreadable Pi settings leave credit overages disabled.
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 3. Antigravity CLI / IDE settings (~/.gemini/antigravity-cli/settings.json)
|
|
80
|
-
try {
|
|
81
|
-
if (existsSync(AGY_SETTINGS_FILE)) {
|
|
82
|
-
const agyData = JSON.parse(readFileSync(AGY_SETTINGS_FILE, "utf8")) as Record<
|
|
83
|
-
string,
|
|
84
|
-
unknown
|
|
85
|
-
>;
|
|
86
|
-
if (agyData.useG1Credits === true || agyData.useAiCredits === true) {
|
|
87
|
-
return {
|
|
88
|
-
enabled: true,
|
|
89
|
-
source: "antigravity_settings",
|
|
90
|
-
detail: `~/.gemini/antigravity-cli/settings.json (useG1Credits: true)`,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
if (agyData.useG1Credits === false || agyData.useAiCredits === false) {
|
|
94
|
-
return {
|
|
95
|
-
enabled: false,
|
|
96
|
-
source: "antigravity_settings",
|
|
97
|
-
detail: `~/.gemini/antigravity-cli/settings.json (useG1Credits: false)`,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
} catch {
|
|
102
|
-
// Unreadable CLI settings leave credit overages disabled.
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
enabled: false,
|
|
107
|
-
source: "default",
|
|
108
|
-
detail: "Default (disabled; enable with /antigravity.credits on)",
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function isCreditsEnabled(): boolean {
|
|
113
|
-
return getCreditsStatus().enabled;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function readSettingsObject(path: string): Record<string, unknown> {
|
|
117
|
-
const parsed = JSON.parse(readFileSync(path, "utf8")) as unknown;
|
|
118
|
-
if (!isRecord(parsed)) {
|
|
119
|
-
throw new Error(`${path} is not a JSON object`);
|
|
120
|
-
}
|
|
121
|
-
return parsed;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function setCreditsEnabled(enabled: boolean): void {
|
|
125
|
-
// Update Pi's settings only. A parse failure must not replace the file with `{ useG1Credits }`.
|
|
126
|
-
const piSettingsPath = getPiSettingsPath();
|
|
127
|
-
try {
|
|
128
|
-
const piData = existsSync(piSettingsPath) ? readSettingsObject(piSettingsPath) : {};
|
|
129
|
-
piData.useG1Credits = enabled;
|
|
130
|
-
writeFileSync(piSettingsPath, `${JSON.stringify(piData, null, 2)}\n`, {
|
|
131
|
-
encoding: "utf8",
|
|
132
|
-
mode: 0o600,
|
|
133
|
-
});
|
|
134
|
-
} catch (error) {
|
|
135
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
136
|
-
throw new Error(`Failed to update ${piSettingsPath}: ${message}`, { cause: error });
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Keep the official CLI flag in sync only when that file already exists and parses.
|
|
140
|
-
// Never create it, and never overwrite it after a parse error.
|
|
141
|
-
if (!existsSync(AGY_SETTINGS_FILE)) return;
|
|
142
|
-
try {
|
|
143
|
-
const agyData = readSettingsObject(AGY_SETTINGS_FILE);
|
|
144
|
-
agyData.useG1Credits = enabled;
|
|
145
|
-
writeFileSync(AGY_SETTINGS_FILE, `${JSON.stringify(agyData, null, 2)}\n`, "utf8");
|
|
146
|
-
} catch {
|
|
147
|
-
// Leave the CLI file untouched.
|
|
148
|
-
}
|
|
149
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared HTML text helpers.
|
|
3
|
-
*
|
|
4
|
-
* Search titles and fetched pages both come out of Google's services as markup, so
|
|
5
|
-
* entity decoding lives here rather than in either feature.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** Decoded form of a numeric entity, or the entity as written when the value is not a code point. */
|
|
9
|
-
function fromCodePoint(match: string, code: number): string {
|
|
10
|
-
return Number.isInteger(code) && code >= 0 && code <= 0x10ffff
|
|
11
|
-
? String.fromCodePoint(code)
|
|
12
|
-
: match;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** Replace HTML entities with their characters, leaving whitespace exactly as written. */
|
|
16
|
-
function decodeEntityText(text: string): string {
|
|
17
|
-
return text
|
|
18
|
-
.replace(/&#(\d+);/g, (match, code: string) => fromCodePoint(match, Number(code)))
|
|
19
|
-
.replace(/&#x([0-9a-f]+);/gi, (match, code: string) => fromCodePoint(match, parseInt(code, 16)))
|
|
20
|
-
.replace(/"/g, '"')
|
|
21
|
-
.replace(/'|'/g, "'")
|
|
22
|
-
.replace(/</g, "<")
|
|
23
|
-
.replace(/>/g, ">")
|
|
24
|
-
.replace(/ /g, " ")
|
|
25
|
-
.replace(/&/g, "&");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Entity decoding for one line of prose: collapsed to single spaces and trimmed. */
|
|
29
|
-
export function decodeHtmlEntities(text: string): string {
|
|
30
|
-
return decodeEntityText(text).replace(/\s+/g, " ").trim();
|
|
31
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { antigravityEnv } from "./util.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Node's built-in fetch keeps an idle socket for only 4 seconds unless the server
|
|
5
|
-
* advertises a longer `Keep-Alive: timeout=`. The Cloud Code Assist endpoint sends no
|
|
6
|
-
* such header, and interactive coding turns are almost always more than 4 seconds
|
|
7
|
-
* apart, so without a dedicated pool every message pays a fresh DNS + TCP + TLS
|
|
8
|
-
* handshake. A long-lived dispatcher removes that per-turn setup cost.
|
|
9
|
-
*
|
|
10
|
-
* The dispatcher is scoped to this provider's requests rather than installed with
|
|
11
|
-
* `setGlobalDispatcher`, so it never changes HTTP behaviour for the rest of the host
|
|
12
|
-
* process or for other extensions.
|
|
13
|
-
*
|
|
14
|
-
* Node 22+ global `fetch` cannot take an npm `undici.Agent` (handler mismatch).
|
|
15
|
-
* Those runtimes already pool connections natively, so we skip the custom Agent.
|
|
16
|
-
*/
|
|
17
|
-
const KEEP_ALIVE_TIMEOUT_MS = 60_000;
|
|
18
|
-
const KEEP_ALIVE_MAX_TIMEOUT_MS = 5 * 60_000;
|
|
19
|
-
const CONNECT_TIMEOUT_MS = 10_000;
|
|
20
|
-
const PREWARM_TIMEOUT_MS = 5_000;
|
|
21
|
-
|
|
22
|
-
/** `dispatcher` is an undici extension to RequestInit that Node's fetch honours. */
|
|
23
|
-
type DispatcherInit = RequestInit & { dispatcher?: unknown };
|
|
24
|
-
|
|
25
|
-
let dispatcherPromise: Promise<unknown> | undefined;
|
|
26
|
-
let prewarmStarted = false;
|
|
27
|
-
|
|
28
|
-
function hasProxyConfiguration(): boolean {
|
|
29
|
-
return ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"].some(
|
|
30
|
-
(name) => Boolean(process.env[name]?.trim()),
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function getDispatcher(): Promise<unknown> {
|
|
35
|
-
dispatcherPromise ??= (async () => {
|
|
36
|
-
// Pi configures a proxy-aware global dispatcher. Passing a private Agent here
|
|
37
|
-
// would bypass it and make Antigravity requests connect directly instead.
|
|
38
|
-
if (antigravityEnv("NO_KEEPALIVE") === "1" || hasProxyConfiguration()) {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
const nodeMajor = Number(process.versions.node?.split(".")[0]);
|
|
42
|
-
if (!Number.isNaN(nodeMajor) && nodeMajor >= 22) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
const { Agent } = await import("undici");
|
|
47
|
-
return new Agent({
|
|
48
|
-
keepAliveTimeout: KEEP_ALIVE_TIMEOUT_MS,
|
|
49
|
-
keepAliveMaxTimeout: KEEP_ALIVE_MAX_TIMEOUT_MS,
|
|
50
|
-
connections: 8,
|
|
51
|
-
connect: { timeout: CONNECT_TIMEOUT_MS },
|
|
52
|
-
// HTTP/2 is opt-in: the endpoint negotiates it, but moving SSE onto h2 is a
|
|
53
|
-
// transport change we do not want to force on every user.
|
|
54
|
-
allowH2: antigravityEnv("HTTP2") === "1",
|
|
55
|
-
});
|
|
56
|
-
} catch {
|
|
57
|
-
// undici unavailable — fall back to Node's default fetch behaviour.
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
})();
|
|
61
|
-
return dispatcherPromise;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** fetch() bound to this provider's keep-alive connection pool when available. */
|
|
65
|
-
export async function antigravityFetch(
|
|
66
|
-
input: string | URL,
|
|
67
|
-
init: RequestInit = {},
|
|
68
|
-
): Promise<Response> {
|
|
69
|
-
const dispatcher = await getDispatcher();
|
|
70
|
-
if (!dispatcher) return fetch(input, init);
|
|
71
|
-
return fetch(input, { ...init, dispatcher } as DispatcherInit);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Open the TLS connection on the first Antigravity request so that turn does not
|
|
76
|
-
* pay the handshake. Not called at extension load: an unauthenticated HEAD to the
|
|
77
|
-
* endpoint can stall Node's event loop until connect timeout and freeze Pi startup.
|
|
78
|
-
* Best-effort: failures are ignored. Subsequent calls are no-ops.
|
|
79
|
-
*/
|
|
80
|
-
export function prewarmConnection(url: string): void {
|
|
81
|
-
if (antigravityEnv("NO_PREWARM") === "1") return;
|
|
82
|
-
if (prewarmStarted) return;
|
|
83
|
-
prewarmStarted = true;
|
|
84
|
-
void (async () => {
|
|
85
|
-
try {
|
|
86
|
-
const res = await antigravityFetch(url, {
|
|
87
|
-
method: "HEAD",
|
|
88
|
-
signal: AbortSignal.timeout(PREWARM_TIMEOUT_MS),
|
|
89
|
-
});
|
|
90
|
-
// Release the socket back to the pool even though HEAD carries no body.
|
|
91
|
-
await res.arrayBuffer();
|
|
92
|
-
} catch {
|
|
93
|
-
// Warm-up only; the real request will establish the connection instead.
|
|
94
|
-
}
|
|
95
|
-
})();
|
|
96
|
-
}
|
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { antigravityEnv, isRecord } from "./util.js";
|
|
2
|
-
|
|
3
|
-
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
4
|
-
const ALLOWED_API_HOST_SUFFIXES = [".googleapis.com", ".sandbox.googleapis.com"];
|
|
5
|
-
|
|
6
|
-
/** Only loopback binds are allowed so OAuth codes cannot be stolen off-machine. */
|
|
7
|
-
export function resolveCallbackHost(raw = antigravityEnv("CALLBACK_HOST")): string {
|
|
8
|
-
const host = (raw || "127.0.0.1").trim().toLowerCase();
|
|
9
|
-
if (!LOOPBACK_HOSTS.has(host)) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
`Unsafe ANTIGRAVITY_CALLBACK_HOST="${host}". Only loopback hosts are allowed: 127.0.0.1, ::1, localhost.`,
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
return host === "localhost" ? "127.0.0.1" : host;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Prevent token exfiltration via poisoned BASE_URL (SSRF / credential leak). */
|
|
18
|
-
export function assertSafeApiBaseUrl(raw: string): string {
|
|
19
|
-
let url: URL;
|
|
20
|
-
try {
|
|
21
|
-
url = new URL(raw);
|
|
22
|
-
} catch {
|
|
23
|
-
throw new Error(`Invalid ANTIGRAVITY_BASE_URL: ${raw}`);
|
|
24
|
-
}
|
|
25
|
-
if (url.protocol !== "https:") {
|
|
26
|
-
throw new Error(`ANTIGRAVITY_BASE_URL must use https (got ${url.protocol})`);
|
|
27
|
-
}
|
|
28
|
-
if (url.username || url.password) {
|
|
29
|
-
throw new Error("ANTIGRAVITY_BASE_URL must not include credentials");
|
|
30
|
-
}
|
|
31
|
-
const host = url.hostname.toLowerCase();
|
|
32
|
-
const allowed =
|
|
33
|
-
host === "googleapis.com" || ALLOWED_API_HOST_SUFFIXES.some((suffix) => host.endsWith(suffix));
|
|
34
|
-
if (!allowed) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
`ANTIGRAVITY_BASE_URL host "${host}" is not allowed. Use a *.googleapis.com endpoint.`,
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
const path = url.pathname.replace(/\/+$/, "");
|
|
40
|
-
return `${url.origin}${path === "/" ? "" : path}`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* One redaction policy, expressed as contexts rather than as one list of names.
|
|
45
|
-
*
|
|
46
|
-
* - Shapes: values Google issues have stable shapes (access tokens `ya29.`, refresh
|
|
47
|
-
* tokens `1//`, authorization codes `4/0A`, JWTs), so they are hidden wherever
|
|
48
|
-
* they appear, with or without a name.
|
|
49
|
-
* - URL parameters: in a query or fragment a name is secret when its last word is a
|
|
50
|
-
* credential word (`?subscription_key=`, `#access_token=`). A value is looked
|
|
51
|
-
* inside only when it carries the syntax of a context itself: a nested query or URL,
|
|
52
|
-
* or a bare `name=value` assignment.
|
|
53
|
-
* - Assignments in free text (`token=...`, `"password": "..."`): the same words,
|
|
54
|
-
* minus the ones that are ordinary prose or diagnostics ({@link PROSE_WORDS}).
|
|
55
|
-
*
|
|
56
|
-
* Strings a page supplies are content, not secrets, and are never passed through here.
|
|
57
|
-
*/
|
|
58
|
-
const CREDENTIAL_WORDS = new Set([
|
|
59
|
-
"token",
|
|
60
|
-
"secret",
|
|
61
|
-
"password",
|
|
62
|
-
"passwd",
|
|
63
|
-
"pwd",
|
|
64
|
-
"key",
|
|
65
|
-
"apikey",
|
|
66
|
-
"signature",
|
|
67
|
-
"sig",
|
|
68
|
-
"credential",
|
|
69
|
-
"credentials",
|
|
70
|
-
"bearer",
|
|
71
|
-
"jwt",
|
|
72
|
-
"auth",
|
|
73
|
-
"authorization",
|
|
74
|
-
"session",
|
|
75
|
-
"sessionid",
|
|
76
|
-
"sid",
|
|
77
|
-
"code",
|
|
78
|
-
"state",
|
|
79
|
-
"nonce",
|
|
80
|
-
"verifier",
|
|
81
|
-
"otp",
|
|
82
|
-
]);
|
|
83
|
-
|
|
84
|
-
/** Compound names whose last word alone is not a credential word. */
|
|
85
|
-
const CREDENTIAL_SUFFIXES = ["accesskeyid", "accesskey", "privatekey", "clientsecret"];
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Words that name credentials in a URL but are ordinary in prose and diagnostics
|
|
89
|
-
* ("exit code=1", "model state=UNAVAILABLE", "session=expired"). In free text they
|
|
90
|
-
* are only redacted when the value has a credential shape.
|
|
91
|
-
*/
|
|
92
|
-
const PROSE_WORDS = new Set([
|
|
93
|
-
"code",
|
|
94
|
-
"state",
|
|
95
|
-
"nonce",
|
|
96
|
-
"session",
|
|
97
|
-
"sessionid",
|
|
98
|
-
"sid",
|
|
99
|
-
"auth",
|
|
100
|
-
"key",
|
|
101
|
-
"sig",
|
|
102
|
-
"signature",
|
|
103
|
-
"credential",
|
|
104
|
-
"jwt",
|
|
105
|
-
]);
|
|
106
|
-
|
|
107
|
-
function nameWords(name: string): string[] {
|
|
108
|
-
return name
|
|
109
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
110
|
-
.toLowerCase()
|
|
111
|
-
.split(/[^a-z0-9]+/)
|
|
112
|
-
.filter(Boolean);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Whether a parameter or field name carries a credential. */
|
|
116
|
-
function isCredentialName(name: string, inProse: boolean): boolean {
|
|
117
|
-
const words = nameWords(name);
|
|
118
|
-
const last = words.at(-1);
|
|
119
|
-
if (!last) return false;
|
|
120
|
-
const credentialQualifier =
|
|
121
|
-
words.length > 1 &&
|
|
122
|
-
words.some((word) =>
|
|
123
|
-
/^(?:oauth|amz|goog|api|private|subscription|signing|access|secret|client)$/.test(word),
|
|
124
|
-
);
|
|
125
|
-
if (CREDENTIAL_WORDS.has(last)) return !inProse || !PROSE_WORDS.has(last) || credentialQualifier;
|
|
126
|
-
const compact = words.join("");
|
|
127
|
-
return CREDENTIAL_SUFFIXES.some((suffix) => compact.endsWith(suffix));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** A value long and random enough to be a credential rather than a word. */
|
|
131
|
-
function looksLikeCredential(value: string): boolean {
|
|
132
|
-
return value.length >= 16 && /[0-9]/.test(value) && /^[A-Za-z0-9._~+/=-]+$/.test(value);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const TOKEN_SHAPES: Array<[RegExp, string]> = [
|
|
136
|
-
[/\bya29\.[A-Za-z0-9._~+/-]+=*/g, "[redacted-access-token]"],
|
|
137
|
-
[/\b1\/\/?[A-Za-z0-9_-]{20,}/g, "[redacted-refresh-token]"],
|
|
138
|
-
[/\b4\/0A[A-Za-z0-9_-]{10,}/g, "[redacted-authorization-code]"],
|
|
139
|
-
[/\bGOCSPX-[A-Za-z0-9_-]{10,}/g, "[redacted-client-secret]"],
|
|
140
|
-
[/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, "[redacted-jwt]"],
|
|
141
|
-
[/\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi, "Bearer [redacted]"],
|
|
142
|
-
];
|
|
143
|
-
|
|
144
|
-
function decodeComponent(value: string): string | undefined {
|
|
145
|
-
try {
|
|
146
|
-
return decodeURIComponent(value.replace(/\+/g, " "));
|
|
147
|
-
} catch {
|
|
148
|
-
return undefined;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* A query or fragment parameter. The name excludes `?`, so each separator is looked
|
|
154
|
-
* at once and a URL with many separators is redacted in linear time.
|
|
155
|
-
*/
|
|
156
|
-
const URL_PARAMETER = /([?&#])([^=&#?\s]{1,256})=([^&#\s"'<>]*)/g;
|
|
157
|
-
|
|
158
|
-
function redactParameters(text: string, depth = 0): string {
|
|
159
|
-
return text.replace(URL_PARAMETER, (match, separator: string, name: string, value: string) => {
|
|
160
|
-
const decodedName = decodeComponent(name);
|
|
161
|
-
if (decodedName === undefined) return match;
|
|
162
|
-
if (isCredentialName(decodedName, false)) return `${separator}${name}=[redacted]`;
|
|
163
|
-
// Look inside a value only when it carries a context's own syntax.
|
|
164
|
-
const decoded = decodeComponent(value);
|
|
165
|
-
if (!decoded) return match;
|
|
166
|
-
const nested =
|
|
167
|
-
/^[?&#]/.test(decoded) || /:\/\/[^\s]*[?#]/.test(decoded)
|
|
168
|
-
? depth >= 8 || redactParameters(decoded, depth + 1) !== decoded
|
|
169
|
-
: /^[\w.-]{1,64}=\S+$/.test(decoded) &&
|
|
170
|
-
isCredentialName(decoded.slice(0, decoded.indexOf("=")), false);
|
|
171
|
-
return nested ? `${separator}${name}=[redacted]` : match;
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* A name followed by `:` or `=` and a value. A quoted value keeps its quotes, so a
|
|
177
|
-
* JSON document stays valid JSON after redaction.
|
|
178
|
-
*/
|
|
179
|
-
const ASSIGNMENT =
|
|
180
|
-
/(["']?)\b([A-Za-z][\w.-]{0,63})\1(\s*[:=]\s*)(?:"((?:[^"\\\n]|\\.)*)"|'([^'\n]*)'|([^\s"',;&}\]]+))/g;
|
|
181
|
-
|
|
182
|
-
function redactAssignments(text: string): string {
|
|
183
|
-
return text.replace(
|
|
184
|
-
ASSIGNMENT,
|
|
185
|
-
(
|
|
186
|
-
match,
|
|
187
|
-
quote: string,
|
|
188
|
-
name: string,
|
|
189
|
-
separator: string,
|
|
190
|
-
double?: string,
|
|
191
|
-
single?: string,
|
|
192
|
-
bare?: string,
|
|
193
|
-
) => {
|
|
194
|
-
const value = double ?? single ?? bare ?? "";
|
|
195
|
-
if (!value || value.startsWith("[redacted")) return match;
|
|
196
|
-
const words = nameWords(name);
|
|
197
|
-
const last = words.at(-1) ?? "";
|
|
198
|
-
if (!isCredentialName(name, true) && !(PROSE_WORDS.has(last) && looksLikeCredential(value)))
|
|
199
|
-
return match;
|
|
200
|
-
const redacted =
|
|
201
|
-
double !== undefined
|
|
202
|
-
? '"[redacted]"'
|
|
203
|
-
: single !== undefined
|
|
204
|
-
? "'[redacted]'"
|
|
205
|
-
: "[redacted]";
|
|
206
|
-
return `${quote}${name}${quote}${separator}${redacted}`;
|
|
207
|
-
},
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** Redact credentials from text the extension renders: errors, diagnostics and URLs. */
|
|
212
|
-
export function redactSecrets(text: string): string {
|
|
213
|
-
let redacted = text.replace(/\b(https?:\/\/)[^/?#\s]*@/gi, "$1[redacted]@");
|
|
214
|
-
for (const [shape, replacement] of TOKEN_SHAPES) redacted = redacted.replace(shape, replacement);
|
|
215
|
-
return redactAssignments(redactParameters(redacted));
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const VALIDATION_URL_HOST = "accounts.google.com";
|
|
219
|
-
|
|
220
|
-
/** Accept only the Google account-verification host, never an arbitrary redirect. */
|
|
221
|
-
export function safeValidationUrl(value: unknown): string | undefined {
|
|
222
|
-
if (typeof value !== "string" || value.length === 0 || value.length > 2048 || /\s/.test(value))
|
|
223
|
-
return undefined;
|
|
224
|
-
let url: URL;
|
|
225
|
-
try {
|
|
226
|
-
url = new URL(value);
|
|
227
|
-
} catch {
|
|
228
|
-
return undefined;
|
|
229
|
-
}
|
|
230
|
-
if (url.protocol !== "https:" || url.username || url.password) return undefined;
|
|
231
|
-
if (url.hostname !== VALIDATION_URL_HOST) return undefined;
|
|
232
|
-
return url.toString();
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** Pull `validation_url` from a 403 body before message slicing can drop it. */
|
|
236
|
-
export function extractValidationUrl(text: string): string | undefined {
|
|
237
|
-
try {
|
|
238
|
-
const parsed = JSON.parse(text) as { error?: { details?: unknown } };
|
|
239
|
-
const details = parsed.error?.details;
|
|
240
|
-
if (Array.isArray(details)) {
|
|
241
|
-
for (const detail of details) {
|
|
242
|
-
if (!isRecord(detail) || !isRecord(detail.metadata)) continue;
|
|
243
|
-
const safe = safeValidationUrl(detail.metadata.validation_url);
|
|
244
|
-
if (safe) return safe;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
} catch {
|
|
248
|
-
// not JSON
|
|
249
|
-
}
|
|
250
|
-
const match = text.match(/https:\/\/accounts\.google\.com\/[^\s"'<>\\]+/);
|
|
251
|
-
return match ? safeValidationUrl(match[0].replace(/[),.;]+$/, "")) : undefined;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/** Keep session-bearing verification links out of doctor output and stored diagnostics. */
|
|
255
|
-
export function stripValidationUrls(text: string): string {
|
|
256
|
-
return text
|
|
257
|
-
.replace(/https:\/\/accounts\.google\.com\/[^\s"'<>\\]+/g, "[redacted-validation-url]")
|
|
258
|
-
.replace(
|
|
259
|
-
/ Open this link in a browser signed in to the same Google account to verify: \[redacted-validation-url\]/g,
|
|
260
|
-
"",
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export function maskEmail(email: string | undefined): string | undefined {
|
|
265
|
-
if (!email || typeof email !== "string") return undefined;
|
|
266
|
-
const parts = email.split("@");
|
|
267
|
-
if (parts.length !== 2 || !parts[0] || !parts[1]) return "[redacted-email]";
|
|
268
|
-
const name = parts[0];
|
|
269
|
-
const domain = parts[1];
|
|
270
|
-
const lastChar = name.at(-1) || "";
|
|
271
|
-
const maskedName = name.length > 2 ? `${name[0]}***${lastChar}` : `${name[0]}***`;
|
|
272
|
-
return `${maskedName}@${domain}`;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
export function safeError(error: unknown): string {
|
|
276
|
-
const raw = error instanceof Error ? error.message : String(error);
|
|
277
|
-
return redactSecrets(raw);
|
|
278
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared handling for text that came from somewhere else: a page, the fetch service,
|
|
3
|
-
* or a quota report.
|
|
4
|
-
*
|
|
5
|
-
* Two operations with two scopes:
|
|
6
|
-
* - {@link repairText} keeps structure and length, replacing only ill-formed UTF-16.
|
|
7
|
-
* It applies to every third-party string, including page bodies.
|
|
8
|
-
* - {@link cleanText} flattens a value to one bounded line. It applies only to labels
|
|
9
|
-
* and metadata, never to a page body, whose line structure is the content.
|
|
10
|
-
*
|
|
11
|
-
* The length cuts never split a surrogate pair. Each caller chooses the direction:
|
|
12
|
-
* an end that must stay inside a limit moves back, a range end moves forward, and a
|
|
13
|
-
* range start moves back.
|
|
14
|
-
*/
|
|
15
|
-
import { sanitizeText } from "./util.js";
|
|
16
|
-
|
|
17
|
-
/** Replace lone surrogates with U+FFFD. One code unit in, one code unit out. */
|
|
18
|
-
export function repairText(text: string): string {
|
|
19
|
-
return sanitizeText(text);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Whether index falls between the two halves of a surrogate pair. */
|
|
23
|
-
function splitsPair(text: string, index: number): boolean {
|
|
24
|
-
if (index <= 0 || index >= text.length) return false;
|
|
25
|
-
const before = text.charCodeAt(index - 1);
|
|
26
|
-
const after = text.charCodeAt(index);
|
|
27
|
-
return before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Move a boundary back off a surrogate pair. */
|
|
31
|
-
export function boundaryBack(text: string, index: number): number {
|
|
32
|
-
return splitsPair(text, index) ? index - 1 : index;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Move a boundary forward off a surrogate pair. */
|
|
36
|
-
export function boundaryForward(text: string, index: number): number {
|
|
37
|
-
return splitsPair(text, index) ? index + 1 : index;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Cut text to at most limit code units without splitting a character. */
|
|
41
|
-
export function capText(text: string, limit: number): string {
|
|
42
|
-
if (text.length <= limit) return text;
|
|
43
|
-
return text.slice(0, boundaryBack(text, Math.max(0, limit)));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** One bounded, single-line value: control characters and line breaks become spaces. */
|
|
47
|
-
export function cleanText(value: string | undefined, limit: number): string | undefined {
|
|
48
|
-
if (!value) return undefined;
|
|
49
|
-
const cleaned = repairText(value)
|
|
50
|
-
.replace(/[\p{Cc}\u2028\u2029]+/gu, " ")
|
|
51
|
-
.replace(/\s+/g, " ")
|
|
52
|
-
.trim();
|
|
53
|
-
if (!cleaned) return undefined;
|
|
54
|
-
if (cleaned.length <= limit) return cleaned;
|
|
55
|
-
return `${capText(cleaned, Math.max(0, limit - 1))}…`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Keep the head and the tail of a long value, eliding the middle. */
|
|
59
|
-
export function elideMiddle(value: string, limit: number): string {
|
|
60
|
-
if (value.length <= limit) return value;
|
|
61
|
-
if (limit < 3) return capText(value, limit);
|
|
62
|
-
const keep = limit - 1;
|
|
63
|
-
const headEnd = boundaryBack(value, Math.ceil(keep / 2));
|
|
64
|
-
const tailStart = boundaryForward(value, value.length - (keep - Math.ceil(keep / 2)));
|
|
65
|
-
return `${value.slice(0, headEnd)}…${value.slice(tailStart)}`;
|
|
66
|
-
}
|