@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.
Files changed (177) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +5 -5
  3. package/THIRD_PARTY_NOTICES.md +3 -24
  4. package/node_modules/@estebanforge/pi-antigravity-bridge/CHANGELOG.md +956 -0
  5. package/node_modules/{pi-antigravity → @estebanforge/pi-antigravity-bridge}/LICENSE +1 -1
  6. package/node_modules/@estebanforge/pi-antigravity-bridge/README.md +240 -0
  7. package/node_modules/@estebanforge/pi-antigravity-bridge/extensions/index.ts +2117 -0
  8. package/node_modules/@estebanforge/pi-antigravity-bridge/package.json +61 -0
  9. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/connection.ts +644 -0
  10. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/driver.ts +1043 -0
  11. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/events.ts +323 -0
  12. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/home.ts +145 -0
  13. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/jsonrpc.ts +243 -0
  14. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/setup.ts +107 -0
  15. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/usage-estimate.ts +61 -0
  16. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agents.ts +64 -0
  17. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-install.ts +93 -0
  18. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-paths.ts +30 -0
  19. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-setup.ts +81 -0
  20. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-version.ts +289 -0
  21. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-detect.ts +161 -0
  22. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-gate.ts +233 -0
  23. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-hook.ts +325 -0
  24. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts-ui.ts +88 -0
  25. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts.ts +144 -0
  26. package/node_modules/@estebanforge/pi-antigravity-bridge/src/ask-tool.ts +1221 -0
  27. package/node_modules/@estebanforge/pi-antigravity-bridge/src/background-watch.ts +225 -0
  28. package/node_modules/@estebanforge/pi-antigravity-bridge/src/bridge-catalog.ts +197 -0
  29. package/node_modules/@estebanforge/pi-antigravity-bridge/src/config.ts +523 -0
  30. package/node_modules/@estebanforge/pi-antigravity-bridge/src/daily-log.ts +196 -0
  31. package/node_modules/@estebanforge/pi-antigravity-bridge/src/dialog-lock.ts +48 -0
  32. package/node_modules/@estebanforge/pi-antigravity-bridge/src/diff-render.ts +207 -0
  33. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver-types.ts +168 -0
  34. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver.ts +907 -0
  35. package/node_modules/@estebanforge/pi-antigravity-bridge/src/engine-picker.ts +160 -0
  36. package/node_modules/@estebanforge/pi-antigravity-bridge/src/frame-guard.ts +38 -0
  37. package/node_modules/@estebanforge/pi-antigravity-bridge/src/handoff-toast.ts +21 -0
  38. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-registration.ts +376 -0
  39. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-server.ts +618 -0
  40. package/node_modules/@estebanforge/pi-antigravity-bridge/src/models.ts +404 -0
  41. package/node_modules/@estebanforge/pi-antigravity-bridge/src/native-tools.ts +104 -0
  42. package/node_modules/@estebanforge/pi-antigravity-bridge/src/parked-turn.ts +125 -0
  43. package/node_modules/@estebanforge/pi-antigravity-bridge/src/patch-cleanup.ts +523 -0
  44. package/node_modules/@estebanforge/pi-antigravity-bridge/src/poll-fold.ts +55 -0
  45. package/node_modules/@estebanforge/pi-antigravity-bridge/src/prompt-images.ts +92 -0
  46. package/node_modules/@estebanforge/pi-antigravity-bridge/src/provider.ts +2214 -0
  47. package/node_modules/@estebanforge/pi-antigravity-bridge/src/queued-messages.ts +152 -0
  48. package/node_modules/@estebanforge/pi-antigravity-bridge/src/quota-notice.ts +40 -0
  49. package/node_modules/@estebanforge/pi-antigravity-bridge/src/redact.ts +27 -0
  50. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sessions.ts +197 -0
  51. package/node_modules/@estebanforge/pi-antigravity-bridge/src/skills.ts +207 -0
  52. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-events.ts +161 -0
  53. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-sanitizer.ts +180 -0
  54. package/node_modules/@estebanforge/pi-antigravity-bridge/src/subagent-roster.ts +245 -0
  55. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sysprompt-agent.ts +130 -0
  56. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks-ui.ts +90 -0
  57. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks.ts +174 -0
  58. package/node_modules/@estebanforge/pi-antigravity-bridge/src/usage.ts +174 -0
  59. package/node_modules/@estebanforge/pi-antigravity-bridge/src/web-tools.ts +320 -0
  60. package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +6 -0
  61. package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +1 -1
  62. package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +3 -1
  63. package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
  64. package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +317 -105
  65. package/node_modules/@herbertgao/pi-subagents/SECURITY.md +2 -2
  66. package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +240 -36
  67. package/node_modules/@herbertgao/pi-subagents/docs/translations.md +63 -0
  68. package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +1 -1
  69. package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
  70. package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +2 -0
  71. package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +89 -6
  72. package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +6 -2
  73. package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +20 -11
  74. package/node_modules/@herbertgao/pi-subagents/src/cache-retention.ts +39 -0
  75. package/node_modules/@herbertgao/pi-subagents/src/completion-outbox.ts +5 -0
  76. package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +185 -44
  77. package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +7 -0
  78. package/node_modules/@herbertgao/pi-subagents/src/group-join.ts +9 -0
  79. package/node_modules/@herbertgao/pi-subagents/src/index.ts +126 -116
  80. package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +3 -2
  81. package/node_modules/@herbertgao/pi-subagents/src/settings.ts +21 -0
  82. package/node_modules/@herbertgao/pi-subagents/src/subagent-info.ts +115 -0
  83. package/node_modules/@herbertgao/pi-subagents/src/types.ts +11 -7
  84. package/node_modules/@herbertgao/pi-subagents/src/ui/agent-widget.ts +2 -3
  85. package/node_modules/@herbertgao/pi-subagents/src/ui/conversation-viewer.ts +1 -1
  86. package/node_modules/@herbertgao/pi-subagents/src/workflow/meta.ts +2 -2
  87. package/node_modules/@herbertgao/pi-subagents/src/workflow-info.ts +184 -0
  88. package/node_modules/pi-commandcode-provider/CHANGELOG.md +10 -0
  89. package/node_modules/pi-commandcode-provider/README.md +3 -2
  90. package/node_modules/pi-commandcode-provider/index.ts +120 -13
  91. package/node_modules/pi-commandcode-provider/package.json +4 -3
  92. package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +11 -2
  93. package/node_modules/pi-commandcode-provider/src/pricing.ts +17 -1
  94. package/node_modules/pi-commandcode-provider/src/quota-command.ts +3 -2
  95. package/node_modules/pi-commandcode-provider/src/zdr.ts +163 -0
  96. package/node_modules/pi-typesafe/README.md +1 -1
  97. package/node_modules/pi-typesafe/dist/ask.js +6 -3
  98. package/node_modules/pi-typesafe/dist/auth.d.ts +31 -7
  99. package/node_modules/pi-typesafe/dist/auth.js +159 -55
  100. package/node_modules/pi-typesafe/dist/batch.d.ts +16 -2
  101. package/node_modules/pi-typesafe/dist/batch.js +62 -22
  102. package/node_modules/pi-typesafe/dist/calibrate.d.ts +38 -11
  103. package/node_modules/pi-typesafe/dist/calibrate.js +56 -24
  104. package/node_modules/pi-typesafe/dist/client.d.ts +13 -0
  105. package/node_modules/pi-typesafe/dist/client.js +67 -27
  106. package/node_modules/pi-typesafe/dist/errors.d.ts +20 -0
  107. package/node_modules/pi-typesafe/dist/errors.js +105 -2
  108. package/node_modules/pi-typesafe/dist/extension.d.ts +106 -0
  109. package/node_modules/pi-typesafe/dist/extension.js +206 -106
  110. package/node_modules/pi-typesafe/dist/index.d.ts +8 -8
  111. package/node_modules/pi-typesafe/dist/index.js +3 -3
  112. package/node_modules/pi-typesafe/dist/key-prompt.d.ts +20 -0
  113. package/node_modules/pi-typesafe/dist/key-prompt.js +23 -3
  114. package/node_modules/pi-typesafe/dist/login.d.ts +58 -1
  115. package/node_modules/pi-typesafe/dist/login.js +167 -13
  116. package/node_modules/pi-typesafe/dist/schema.d.ts +3 -0
  117. package/node_modules/pi-typesafe/dist/schema.js +47 -13
  118. package/node_modules/pi-typesafe/dist/ui.d.ts +2 -1
  119. package/node_modules/pi-typesafe/dist/usage.d.ts +53 -0
  120. package/node_modules/pi-typesafe/dist/usage.js +79 -40
  121. package/node_modules/pi-typesafe/package.json +3 -3
  122. package/package.json +12 -15
  123. package/node_modules/pi-antigravity/README.md +0 -298
  124. package/node_modules/pi-antigravity/docs/reader-guarantees.md +0 -35
  125. package/node_modules/pi-antigravity/package.json +0 -87
  126. package/node_modules/pi-antigravity/src/auth/accounts.ts +0 -327
  127. package/node_modules/pi-antigravity/src/auth/index.ts +0 -23
  128. package/node_modules/pi-antigravity/src/auth/oauth.ts +0 -445
  129. package/node_modules/pi-antigravity/src/client/client.ts +0 -602
  130. package/node_modules/pi-antigravity/src/client/enterprise.ts +0 -150
  131. package/node_modules/pi-antigravity/src/client/index.ts +0 -2
  132. package/node_modules/pi-antigravity/src/diagnostics/diagnostics.ts +0 -104
  133. package/node_modules/pi-antigravity/src/diagnostics/index.ts +0 -1
  134. package/node_modules/pi-antigravity/src/features/generate-image.ts +0 -132
  135. package/node_modules/pi-antigravity/src/features/google-search.ts +0 -72
  136. package/node_modules/pi-antigravity/src/features/read-url-content.ts +0 -124
  137. package/node_modules/pi-antigravity/src/image/image.ts +0 -427
  138. package/node_modules/pi-antigravity/src/image/index.ts +0 -1
  139. package/node_modules/pi-antigravity/src/image/native.ts +0 -69
  140. package/node_modules/pi-antigravity/src/index.ts +0 -547
  141. package/node_modules/pi-antigravity/src/models/discovery.ts +0 -176
  142. package/node_modules/pi-antigravity/src/models/grouping.ts +0 -487
  143. package/node_modules/pi-antigravity/src/models/index.ts +0 -3
  144. package/node_modules/pi-antigravity/src/models/models.ts +0 -647
  145. package/node_modules/pi-antigravity/src/search/index.ts +0 -1
  146. package/node_modules/pi-antigravity/src/search/search.ts +0 -967
  147. package/node_modules/pi-antigravity/src/stream/index.ts +0 -1
  148. package/node_modules/pi-antigravity/src/stream/stream.ts +0 -1961
  149. package/node_modules/pi-antigravity/src/types/enums.ts +0 -52
  150. package/node_modules/pi-antigravity/src/types/index.ts +0 -2
  151. package/node_modules/pi-antigravity/src/types/types.ts +0 -329
  152. package/node_modules/pi-antigravity/src/usage/index.ts +0 -1
  153. package/node_modules/pi-antigravity/src/usage/usage.ts +0 -437
  154. package/node_modules/pi-antigravity/src/utils/address.ts +0 -180
  155. package/node_modules/pi-antigravity/src/utils/credits.ts +0 -149
  156. package/node_modules/pi-antigravity/src/utils/html.ts +0 -31
  157. package/node_modules/pi-antigravity/src/utils/http.ts +0 -96
  158. package/node_modules/pi-antigravity/src/utils/index.ts +0 -5
  159. package/node_modules/pi-antigravity/src/utils/security.ts +0 -278
  160. package/node_modules/pi-antigravity/src/utils/text.ts +0 -66
  161. package/node_modules/pi-antigravity/src/utils/util.ts +0 -217
  162. package/node_modules/pi-antigravity/src/web-fetch/document.ts +0 -83
  163. package/node_modules/pi-antigravity/src/web-fetch/fences.ts +0 -24
  164. package/node_modules/pi-antigravity/src/web-fetch/header.ts +0 -97
  165. package/node_modules/pi-antigravity/src/web-fetch/index.ts +0 -3
  166. package/node_modules/pi-antigravity/src/web-fetch/inspect.ts +0 -328
  167. package/node_modules/pi-antigravity/src/web-fetch/limits.ts +0 -4
  168. package/node_modules/pi-antigravity/src/web-fetch/markdown.ts +0 -744
  169. package/node_modules/pi-antigravity/src/web-fetch/mdn.ts +0 -164
  170. package/node_modules/pi-antigravity/src/web-fetch/media.ts +0 -11
  171. package/node_modules/pi-antigravity/src/web-fetch/metadata.ts +0 -9
  172. package/node_modules/pi-antigravity/src/web-fetch/representation.ts +0 -93
  173. package/node_modules/pi-antigravity/src/web-fetch/save.ts +0 -71
  174. package/node_modules/pi-antigravity/src/web-fetch/sites.ts +0 -138
  175. package/node_modules/pi-antigravity/src/web-fetch/vendor.d.ts +0 -9
  176. package/node_modules/pi-antigravity/src/web-fetch/web-fetch.ts +0 -957
  177. package/node_modules/pi-antigravity/tsconfig.json +0 -21
@@ -1,427 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { constants } from "node:fs";
3
- import { lstat, mkdir, open, realpath } from "node:fs/promises";
4
- import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
- import {
6
- antigravityHeaders,
7
- endpointCandidates,
8
- jsonOrTextError,
9
- parseApiKey,
10
- } from "../client/client.js";
11
- import { resolveEnterpriseTarget } from "../client/enterprise.js";
12
- import { AntigravityRequestType, AntigravityUserAgent, GeminiRole } from "../types/enums.js";
13
- import { isCreditsEnabled } from "../utils/credits.js";
14
- import { antigravityFetch } from "../utils/http.js";
15
- import { safeError } from "../utils/security.js";
16
- import { sanitizeText } from "../utils/util.js";
17
-
18
- export const DEFAULT_IMAGE_MODEL = "gemini-3.1-flash-image";
19
- export const IMAGE_ASPECT_RATIOS = [
20
- "1:1",
21
- "2:3",
22
- "3:2",
23
- "3:4",
24
- "4:3",
25
- "4:5",
26
- "5:4",
27
- "9:16",
28
- "16:9",
29
- "21:9",
30
- ] as const;
31
- export type ImageAspectRatio = (typeof IMAGE_ASPECT_RATIOS)[number];
32
-
33
- const IMAGE_MODEL_FALLBACKS = [
34
- DEFAULT_IMAGE_MODEL,
35
- "gemini-3-pro-image",
36
- "gemini-3-pro-image-preview",
37
- ];
38
- const DEFAULT_IMAGE_DIR = join(".pi", "generated-images");
39
- const MAX_PROMPT_CHARS = 8000;
40
-
41
- export type GeneratedImage = { data: string; mimeType: string };
42
-
43
- export type ImageGenerateRequest = {
44
- project: string;
45
- model: string;
46
- request: {
47
- contents: Array<{ role: "user"; parts: Array<{ text?: string; inlineData?: GeneratedImage }> }>;
48
- generationConfig: {
49
- imageConfig: { aspectRatio: string };
50
- candidateCount: number;
51
- };
52
- };
53
- requestType: "image_gen";
54
- userAgent: "antigravity";
55
- requestId: string;
56
- enabledCreditTypes?: string[];
57
- };
58
-
59
- export type ImageCommandArgs = {
60
- prompt: string;
61
- aspectRatio?: string;
62
- model?: string;
63
- path?: string;
64
- };
65
-
66
- export type GenerateImageOptions = ImageCommandArgs & {
67
- apiKey: string;
68
- cwd: string;
69
- inputImages?: GeneratedImage[];
70
- signal?: AbortSignal;
71
- };
72
-
73
- export type GenerateImageResult = {
74
- images: GeneratedImage[];
75
- savedPaths: string[];
76
- text: string[];
77
- model: string;
78
- };
79
-
80
- type ImageStreamChunk = {
81
- error?: { message?: string };
82
- response?: {
83
- candidates?: Array<{
84
- content?: {
85
- parts?: Array<{ text?: string; inlineData?: { mimeType?: string; data?: string } }>;
86
- };
87
- }>;
88
- };
89
- candidates?: Array<{
90
- content?: {
91
- parts?: Array<{ text?: string; inlineData?: { mimeType?: string; data?: string } }>;
92
- };
93
- }>;
94
- };
95
-
96
- function imageExtension(mimeType: string): string {
97
- const lower = mimeType.toLowerCase();
98
- if (lower.includes("jpeg") || lower.includes("jpg")) return "jpg";
99
- if (lower.includes("webp")) return "webp";
100
- if (lower.includes("gif")) return "gif";
101
- return "png";
102
- }
103
-
104
- export function assertSafeImageModel(modelId: string): string {
105
- const id = modelId.trim();
106
- if (id.length === 0 || id.length > 80) {
107
- throw new Error("Unsupported image model id.");
108
- }
109
- if (!/^(gemini-[a-z0-9.+-]*image[a-z0-9.+-]*|imagen-[a-z0-9.+-]+)$/i.test(id)) {
110
- throw new Error(`Unsupported image model: ${id}`);
111
- }
112
- return id;
113
- }
114
-
115
- export function assertSafeAspectRatio(ratio: string): ImageAspectRatio {
116
- const value = ratio.trim();
117
- for (const allowed of IMAGE_ASPECT_RATIOS) {
118
- if (allowed === value) return allowed;
119
- }
120
- throw new Error(
121
- `Unsupported aspect ratio: ${value}. Use one of ${IMAGE_ASPECT_RATIOS.join(", ")}.`,
122
- );
123
- }
124
-
125
- export function parseImageCommandArgs(args: string): ImageCommandArgs {
126
- const tokens = args.trim().split(/\s+/).filter(Boolean);
127
- const out: ImageCommandArgs = { prompt: "" };
128
- const rest: string[] = [];
129
- for (let i = 0; i < tokens.length; i++) {
130
- const token = tokens[i];
131
- if (token === undefined) continue;
132
- const next = tokens[i + 1];
133
- if ((token === "--ratio" || token === "--aspect-ratio") && next) {
134
- out.aspectRatio = next;
135
- i += 1;
136
- continue;
137
- }
138
- if (token === "--model" && next) {
139
- out.model = next;
140
- i += 1;
141
- continue;
142
- }
143
- if (token === "--path" && next) {
144
- out.path = next;
145
- i += 1;
146
- continue;
147
- }
148
- rest.push(token);
149
- }
150
- out.prompt = rest.join(" ");
151
- return out;
152
- }
153
-
154
- export function resolveImageSavePath(
155
- cwd: string,
156
- requested?: string,
157
- mimeType = "image/png",
158
- index?: number,
159
- ): string {
160
- const ext = imageExtension(mimeType);
161
- const stamp = new Date().toISOString().replace(/[:.]/g, "-");
162
- const suffix = index === undefined ? "" : `-${index + 1}`;
163
- const defaultName = `image-${stamp}${suffix}.${ext}`;
164
- const root = resolve(cwd);
165
- const target = requested?.trim()
166
- ? resolve(root, requested.trim())
167
- : resolve(root, DEFAULT_IMAGE_DIR, defaultName);
168
- const rel = relative(root, target);
169
- if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
170
- throw new Error("Image save path must be inside the working directory.");
171
- }
172
- if (!extname(target)) return join(target, defaultName);
173
- if (index === undefined) return target;
174
- const currentExt = extname(target);
175
- return `${target.slice(0, -currentExt.length)}${suffix}${currentExt}`;
176
- }
177
-
178
- export function buildImageGenerateRequest(
179
- prompt: string,
180
- model: string,
181
- projectId: string,
182
- aspectRatio: string,
183
- inputImages: GeneratedImage[] = [],
184
- ): ImageGenerateRequest {
185
- const req: ImageGenerateRequest = {
186
- project: projectId,
187
- model,
188
- request: {
189
- contents: [
190
- {
191
- role: GeminiRole.User,
192
- parts: [
193
- { text: sanitizeText(prompt) },
194
- ...inputImages.map((inlineData) => ({ inlineData })),
195
- ],
196
- },
197
- ],
198
- generationConfig: {
199
- imageConfig: { aspectRatio },
200
- candidateCount: 1,
201
- },
202
- },
203
- requestType: AntigravityRequestType.ImageGen,
204
- userAgent: AntigravityUserAgent.Antigravity,
205
- requestId: `image_gen/${Date.now()}/${randomUUID()}/1`,
206
- };
207
- if (isCreditsEnabled()) {
208
- req.enabledCreditTypes = ["GOOGLE_ONE_AI"];
209
- }
210
- return req;
211
- }
212
-
213
- function collectImagesFromParts(
214
- parts: Array<{ text?: string; inlineData?: { mimeType?: string; data?: string } }> | undefined,
215
- images: GeneratedImage[],
216
- text: string[],
217
- ): void {
218
- for (const part of parts || []) {
219
- if (part.text) text.push(part.text);
220
- if (part.inlineData?.data) {
221
- images.push({
222
- data: part.inlineData.data,
223
- mimeType: part.inlineData.mimeType || "image/png",
224
- });
225
- }
226
- }
227
- }
228
-
229
- export async function collectImagesFromSse(
230
- response: Response,
231
- signal?: AbortSignal,
232
- ): Promise<{ images: GeneratedImage[]; text: string[] }> {
233
- if (!response.body) throw new Error("No response body");
234
- const reader = response.body.getReader();
235
- const decoder = new TextDecoder();
236
- let buffer = "";
237
- const images: GeneratedImage[] = [];
238
- const text: string[] = [];
239
- try {
240
- while (true) {
241
- if (signal?.aborted) throw new Error("Request was aborted");
242
- const result = await reader.read();
243
- if (result.done) break;
244
- if (!(result.value instanceof Uint8Array)) continue;
245
- buffer += decoder.decode(result.value, { stream: true });
246
- const lines = buffer.split("\n");
247
- buffer = lines.pop() || "";
248
- for (const line of lines) {
249
- if (!line.startsWith("data:")) continue;
250
- const json = line.slice(5).trim();
251
- if (!json || json === "[DONE]") continue;
252
- let chunk: ImageStreamChunk;
253
- try {
254
- chunk = JSON.parse(json) as ImageStreamChunk;
255
- } catch {
256
- continue;
257
- }
258
- if (chunk.error?.message) throw new Error(chunk.error.message);
259
- const responseData = chunk.response || chunk;
260
- for (const candidate of responseData.candidates || []) {
261
- collectImagesFromParts(candidate.content?.parts, images, text);
262
- }
263
- }
264
- }
265
- } finally {
266
- reader.releaseLock();
267
- }
268
- return { images, text };
269
- }
270
-
271
- /** Uses the direct image endpoint; only a 404 enables the legacy SSE endpoint. */
272
- export async function requestImageResponse(
273
- endpoint: string,
274
- init: RequestInit,
275
- fetcher: (url: string, init: RequestInit) => Promise<Response> = antigravityFetch,
276
- ): Promise<{ response: Response; streaming: boolean }> {
277
- const response = await fetcher(`${endpoint}/v1internal:generateContent`, init);
278
- if (response.status !== 404) return { response, streaming: false };
279
- await response.text();
280
- return {
281
- response: await fetcher(`${endpoint}/v1internal:streamGenerateContent?alt=sse`, init),
282
- streaming: true,
283
- };
284
- }
285
-
286
- /** Extracts images and notes from wrapped or unwrapped direct responses. */
287
- export function parseImageResponse(chunk: ImageStreamChunk): {
288
- images: GeneratedImage[];
289
- text: string[];
290
- } {
291
- if (chunk.error?.message) throw new Error(chunk.error.message);
292
- const images: GeneratedImage[] = [];
293
- const text: string[] = [];
294
- for (const candidate of (chunk.response || chunk).candidates || []) {
295
- collectImagesFromParts(candidate.content?.parts, images, text);
296
- }
297
- return { images, text };
298
- }
299
-
300
- export async function writeImage(
301
- filePath: string,
302
- image: GeneratedImage,
303
- cwd: string,
304
- ): Promise<string> {
305
- const root = await realpath(cwd);
306
- const rel = relative(resolve(cwd), filePath);
307
- if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
308
- throw new Error("Image path must stay inside the working directory");
309
- }
310
- let parent = root;
311
- for (const component of relative(resolve(cwd), dirname(filePath)).split(sep).filter(Boolean)) {
312
- parent = join(parent, component);
313
- try {
314
- await mkdir(parent);
315
- } catch (error) {
316
- if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
317
- }
318
- const info = await lstat(parent);
319
- if (info.isSymbolicLink() || !info.isDirectory())
320
- throw new Error("Image path must not traverse symlinks");
321
- }
322
- const target = join(root, rel);
323
- try {
324
- if ((await lstat(target)).isSymbolicLink())
325
- throw new Error("Image path must not follow symlinks");
326
- } catch (error) {
327
- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
328
- }
329
- const handle = await open(
330
- target,
331
- constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW,
332
- 0o600,
333
- );
334
- try {
335
- await handle.writeFile(Buffer.from(image.data, "base64"));
336
- } finally {
337
- await handle.close();
338
- }
339
- return filePath;
340
- }
341
-
342
- /** Generates in-memory content shared by the native API and file-saving wrapper. */
343
- export async function generateAntigravityImageContent(
344
- options: Omit<GenerateImageOptions, "cwd" | "path">,
345
- ): Promise<Omit<GenerateImageResult, "savedPaths">> {
346
- const prompt = options.prompt.trim();
347
- if (!prompt && !options.inputImages?.length)
348
- throw new Error("Image prompt or input image is required.");
349
- const enterprise = resolveEnterpriseTarget();
350
- if (enterprise) {
351
- throw new Error(
352
- `Image generation is not sent while Gemini Enterprise routing is active (location=${enterprise.location}). Google documents image generation on the global deployment only, and this extension does not upload prompts or reference images to a second host.`,
353
- );
354
- }
355
- if (prompt.length > MAX_PROMPT_CHARS) {
356
- throw new Error(`Image prompt is too long (max ${MAX_PROMPT_CHARS} characters).`);
357
- }
358
- const aspectRatio = assertSafeAspectRatio(options.aspectRatio || "1:1");
359
- const preferred = assertSafeImageModel(options.model || DEFAULT_IMAGE_MODEL);
360
- const models = [preferred, ...IMAGE_MODEL_FALLBACKS.filter((id) => id !== preferred)];
361
- const creds = parseApiKey(options.apiKey);
362
- const headers = antigravityHeaders(creds.token);
363
-
364
- let lastError = "no endpoint available";
365
- for (const model of models) {
366
- const body = JSON.stringify(
367
- buildImageGenerateRequest(prompt, model, creds.projectId, aspectRatio, options.inputImages),
368
- );
369
- for (const endpoint of endpointCandidates()) {
370
- if (options.signal?.aborted) throw new Error("Request was aborted");
371
- try {
372
- const { response, streaming } = await requestImageResponse(endpoint, {
373
- method: "POST",
374
- headers,
375
- body,
376
- signal: options.signal,
377
- });
378
- if (!response.ok) {
379
- lastError = jsonOrTextError(await response.text()).slice(0, 400);
380
- if (response.status === 404 || [403, 429, 500, 502, 503, 504].includes(response.status)) {
381
- continue;
382
- }
383
- throw new Error(
384
- `Antigravity image request failed (${response.status}): ${safeError(lastError)}`,
385
- );
386
- }
387
- const parsed = streaming
388
- ? await collectImagesFromSse(response, options.signal)
389
- : parseImageResponse((await response.json()) as ImageStreamChunk);
390
- if (!parsed.images.length) {
391
- lastError = parsed.text.join(" ").trim() || "No image data returned.";
392
- continue;
393
- }
394
- return { images: parsed.images, text: parsed.text, model };
395
- } catch (error) {
396
- lastError = safeError(error);
397
- if (options.signal?.aborted) {
398
- throw new Error("Request was aborted", { cause: error });
399
- }
400
- }
401
- }
402
- }
403
- throw new Error(`Antigravity image generation failed: ${safeError(lastError)}`);
404
- }
405
-
406
- /** Saves generated images for the existing tool and slash command. */
407
- export async function generateAntigravityImage(
408
- options: GenerateImageOptions,
409
- ): Promise<GenerateImageResult> {
410
- const result = await generateAntigravityImageContent(options);
411
- const savedPaths: string[] = [];
412
- for (const [index, image] of result.images.entries()) {
413
- savedPaths.push(
414
- await writeImage(
415
- resolveImageSavePath(
416
- options.cwd,
417
- options.path,
418
- image.mimeType,
419
- result.images.length > 1 ? index : undefined,
420
- ),
421
- image,
422
- options.cwd,
423
- ),
424
- );
425
- }
426
- return { ...result, savedPaths };
427
- }
@@ -1 +0,0 @@
1
- export * from "./image.js";
@@ -1,69 +0,0 @@
1
- import type { AssistantImages, ImagesContext, ImagesOptions } from "@earendil-works/pi-ai";
2
- import { DEFAULT_IMAGE_MODEL, generateAntigravityImageContent } from "./image.js";
3
- import { safeError } from "../utils/security.js";
4
-
5
- export const ANTIGRAVITY_IMAGE_API = "antigravity-images";
6
- export const ANTIGRAVITY_IMAGE_MODELS = [
7
- {
8
- type: "image" as const,
9
- id: DEFAULT_IMAGE_MODEL,
10
- name: "Gemini 3.1 Flash Image (Antigravity)",
11
- api: ANTIGRAVITY_IMAGE_API,
12
- input: ["text", "image"] as ("text" | "image")[],
13
- output: ["text", "image"] as ("text" | "image")[],
14
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
15
- },
16
- ];
17
-
18
- type ImageModelIdentity = { id: string; api: string; provider: string };
19
-
20
- /** Implements Pi's in-memory image API using the existing Antigravity credentials and transport. */
21
- export async function generateAntigravityImages(
22
- model: ImageModelIdentity,
23
- context: ImagesContext,
24
- options?: ImagesOptions,
25
- ): Promise<AssistantImages> {
26
- const identity = { api: model.api, provider: model.provider, model: model.id };
27
- try {
28
- if (options?.signal?.aborted) throw new Error("Request was aborted");
29
- if (!options?.apiKey)
30
- throw new Error("No Antigravity credentials. Run /login antigravity first.");
31
- const ratio = options.metadata?.aspectRatio;
32
- if (ratio !== undefined && typeof ratio !== "string")
33
- throw new Error("aspectRatio must be a string.");
34
- const result = await generateAntigravityImageContent({
35
- apiKey: options.apiKey,
36
- model: model.id,
37
- signal: options.signal,
38
- aspectRatio: ratio,
39
- prompt: context.input
40
- .filter((item) => item.type === "text")
41
- .map((item) => item.text)
42
- .join("\n\n"),
43
- inputImages: context.input
44
- .filter((item) => item.type === "image")
45
- .map((item) => ({
46
- data: item.data,
47
- mimeType: item.mimeType,
48
- })),
49
- });
50
- return {
51
- ...identity,
52
- model: result.model,
53
- stopReason: "stop",
54
- timestamp: Date.now(),
55
- output: [
56
- ...result.text.map((text) => ({ type: "text" as const, text })),
57
- ...result.images.map((image) => ({ type: "image" as const, ...image })),
58
- ],
59
- };
60
- } catch (error) {
61
- return {
62
- ...identity,
63
- output: [],
64
- timestamp: Date.now(),
65
- stopReason: options?.signal?.aborted ? "aborted" : "error",
66
- errorMessage: safeError(error),
67
- };
68
- }
69
- }