dsh-antigravity-auth 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.
Files changed (114) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/LICENSE +21 -0
  3. package/README.md +168 -0
  4. package/README.zh.md +159 -0
  5. package/cordis.patch.yml +12 -0
  6. package/lib/capability-lifecycle--tgGKajs.js +1896 -0
  7. package/lib/client.cjs +2123 -0
  8. package/lib/client.cjs.map +1 -0
  9. package/lib/image.js +564 -0
  10. package/lib/index.js +176 -0
  11. package/lib/invariant.js +2 -0
  12. package/lib/live-gate-runner.js +542 -0
  13. package/lib/live-gates.js +97 -0
  14. package/lib/llm-adapter.js +916 -0
  15. package/lib/media-admission.js +206 -0
  16. package/lib/private-failure-B9vzUkQ7.js +23 -0
  17. package/lib/private-transport-urF138qW.js +641 -0
  18. package/lib/private-transport.js +2 -0
  19. package/lib/project-context.js +133 -0
  20. package/lib/quota.js +283 -0
  21. package/lib/replay.js +156 -0
  22. package/lib/rpc-contract-BFWfdKKs.js +491 -0
  23. package/lib/rpc-contract.js +2 -0
  24. package/lib/safe-text-AlEyN8q_.js +12 -0
  25. package/lib/search.js +259 -0
  26. package/lib/status-D0-em3Ru.js +125 -0
  27. package/lib/types/auth-service.d.ts +64 -0
  28. package/lib/types/auth-service.d.ts.map +1 -0
  29. package/lib/types/auth-store.d.ts +45 -0
  30. package/lib/types/auth-store.d.ts.map +1 -0
  31. package/lib/types/bootstrap-service.d.ts +6 -0
  32. package/lib/types/bootstrap-service.d.ts.map +1 -0
  33. package/lib/types/capability-gates.d.ts +19 -0
  34. package/lib/types/capability-gates.d.ts.map +1 -0
  35. package/lib/types/capability-lifecycle.d.ts +26 -0
  36. package/lib/types/capability-lifecycle.d.ts.map +1 -0
  37. package/lib/types/client/AntigravityAuthSettings.d.ts +19 -0
  38. package/lib/types/client/AntigravityAuthSettings.d.ts.map +1 -0
  39. package/lib/types/client/index.d.ts +18 -0
  40. package/lib/types/client/index.d.ts.map +1 -0
  41. package/lib/types/client/locales.d.ts +121 -0
  42. package/lib/types/client/locales.d.ts.map +1 -0
  43. package/lib/types/client/styles.d.ts +4 -0
  44. package/lib/types/client/styles.d.ts.map +1 -0
  45. package/lib/types/credential-coordinator.d.ts +84 -0
  46. package/lib/types/credential-coordinator.d.ts.map +1 -0
  47. package/lib/types/image.d.ts +39 -0
  48. package/lib/types/image.d.ts.map +1 -0
  49. package/lib/types/index.d.ts +21 -0
  50. package/lib/types/index.d.ts.map +1 -0
  51. package/lib/types/invariant.d.ts +3 -0
  52. package/lib/types/invariant.d.ts.map +1 -0
  53. package/lib/types/live-gate-attachments.d.ts +6 -0
  54. package/lib/types/live-gate-attachments.d.ts.map +1 -0
  55. package/lib/types/live-gate-auth.d.ts +5 -0
  56. package/lib/types/live-gate-auth.d.ts.map +1 -0
  57. package/lib/types/live-gate-image.d.ts +5 -0
  58. package/lib/types/live-gate-image.d.ts.map +1 -0
  59. package/lib/types/live-gate-llm.d.ts +10 -0
  60. package/lib/types/live-gate-llm.d.ts.map +1 -0
  61. package/lib/types/live-gate-outcome.d.ts +8 -0
  62. package/lib/types/live-gate-outcome.d.ts.map +1 -0
  63. package/lib/types/live-gate-runner.d.ts +15 -0
  64. package/lib/types/live-gate-runner.d.ts.map +1 -0
  65. package/lib/types/live-gate-search.d.ts +5 -0
  66. package/lib/types/live-gate-search.d.ts.map +1 -0
  67. package/lib/types/live-gate-video.d.ts +8 -0
  68. package/lib/types/live-gate-video.d.ts.map +1 -0
  69. package/lib/types/live-gates.d.ts +25 -0
  70. package/lib/types/live-gates.d.ts.map +1 -0
  71. package/lib/types/llm-adapter.d.ts +65 -0
  72. package/lib/types/llm-adapter.d.ts.map +1 -0
  73. package/lib/types/login-types.d.ts +36 -0
  74. package/lib/types/login-types.d.ts.map +1 -0
  75. package/lib/types/media-admission.d.ts +54 -0
  76. package/lib/types/media-admission.d.ts.map +1 -0
  77. package/lib/types/model-catalog.d.ts +19 -0
  78. package/lib/types/model-catalog.d.ts.map +1 -0
  79. package/lib/types/oauth-flow.d.ts +87 -0
  80. package/lib/types/oauth-flow.d.ts.map +1 -0
  81. package/lib/types/private-failure.d.ts +6 -0
  82. package/lib/types/private-failure.d.ts.map +1 -0
  83. package/lib/types/private-transport-error.d.ts +13 -0
  84. package/lib/types/private-transport-error.d.ts.map +1 -0
  85. package/lib/types/private-transport.d.ts +48 -0
  86. package/lib/types/private-transport.d.ts.map +1 -0
  87. package/lib/types/project-context.d.ts +30 -0
  88. package/lib/types/project-context.d.ts.map +1 -0
  89. package/lib/types/quota.d.ts +43 -0
  90. package/lib/types/quota.d.ts.map +1 -0
  91. package/lib/types/raw-http.d.ts +11 -0
  92. package/lib/types/raw-http.d.ts.map +1 -0
  93. package/lib/types/replay.d.ts +30 -0
  94. package/lib/types/replay.d.ts.map +1 -0
  95. package/lib/types/rpc-contract.d.ts +33 -0
  96. package/lib/types/rpc-contract.d.ts.map +1 -0
  97. package/lib/types/rpc-vocabulary.d.ts +7 -0
  98. package/lib/types/rpc-vocabulary.d.ts.map +1 -0
  99. package/lib/types/rpc.d.ts +11 -0
  100. package/lib/types/rpc.d.ts.map +1 -0
  101. package/lib/types/safe-text.d.ts +3 -0
  102. package/lib/types/safe-text.d.ts.map +1 -0
  103. package/lib/types/search.d.ts +42 -0
  104. package/lib/types/search.d.ts.map +1 -0
  105. package/lib/types/status.d.ts +62 -0
  106. package/lib/types/status.d.ts.map +1 -0
  107. package/lib/types/video.d.ts +42 -0
  108. package/lib/types/video.d.ts.map +1 -0
  109. package/lib/types/wire-identity.d.ts +49 -0
  110. package/lib/types/wire-identity.d.ts.map +1 -0
  111. package/lib/video.js +320 -0
  112. package/lib/wire-identity.js +162 -0
  113. package/package.json +189 -0
  114. package/scripts/live-gates.mjs +4 -0
package/lib/image.js ADDED
@@ -0,0 +1,564 @@
1
+ import { i as createAntigravityAuthService, n as registerCapabilitySet, t as mountCapabilityLifecycle } from "./capability-lifecycle--tgGKajs.js";
2
+ import { d as privateStatusError, i as DEFAULT_PRIVATE_RESPONSE_BYTES, l as createPrivateTransport, m as PrivateTransportError, p as readPrivateText } from "./private-transport-urF138qW.js";
3
+ import { ANTIGRAVITY_WIRE_ORIGIN } from "./wire-identity.js";
4
+ import { t as classifyPrivateFailure } from "./private-failure-B9vzUkQ7.js";
5
+ import { IMAGE_HANDLE_PATTERN, admitBase64Image, admitSessionImage, admitWorkspaceImage, imageHandle, sessionImageCatalog } from "./media-admission.js";
6
+ import { resolveModelWithTier } from "@cortexkit/antigravity-auth-core";
7
+ import { Buffer } from "node:buffer";
8
+ import { HarnessError } from "@deepseek-ai/dsh-llm";
9
+ import z from "@deepseek-ai/schemastery";
10
+ import { installSettingsSection, settingsNamespace } from "@deepseek-ai/dsh-settings";
11
+ //#region src/image.ts
12
+ /** Antigravity image generation/editing tools and session-authorized image catalog. */
13
+ const name = "antigravity-image";
14
+ const inject = [
15
+ "tools",
16
+ "attachments",
17
+ "fs",
18
+ "antigravityAuth"
19
+ ];
20
+ const GENERATE_IMAGE_TOOL_NAME = "generate_image";
21
+ const LIST_IMAGES_TOOL_NAME = "list_images";
22
+ const ANTIGRAVITY_IMAGE_ENDPOINT = `${ANTIGRAVITY_WIRE_ORIGIN}/v1internal:generateContent`;
23
+ const ANTIGRAVITY_IMAGE_MODEL = "antigravity-gemini-3.1-flash-image";
24
+ const ANTIGRAVITY_IMAGE_SETTINGS_NAMESPACE = settingsNamespace("antigravity-image");
25
+ const Config = z.object({
26
+ enabled: z.boolean().default(true),
27
+ model: z.string().default(ANTIGRAVITY_IMAGE_MODEL),
28
+ n: z.number().step(1).min(1).max(4).default(1)
29
+ });
30
+ const MAX_REFERENCES = 5;
31
+ const MAX_IMAGES = 4;
32
+ const IMAGE_ORIGINS = [
33
+ "all",
34
+ "generated",
35
+ "reference",
36
+ "user"
37
+ ];
38
+ const generateSchema = {
39
+ type: "object",
40
+ properties: {
41
+ operation: {
42
+ type: "string",
43
+ enum: ["generate", "edit"]
44
+ },
45
+ images: {
46
+ type: "array",
47
+ items: { type: "object" }
48
+ },
49
+ references: {
50
+ type: "array",
51
+ items: { type: "object" }
52
+ },
53
+ warnings: {
54
+ type: "array",
55
+ items: { type: "object" }
56
+ }
57
+ },
58
+ required: [
59
+ "operation",
60
+ "images",
61
+ "references",
62
+ "warnings"
63
+ ],
64
+ additionalProperties: false
65
+ };
66
+ const listSchema = {
67
+ type: "object",
68
+ properties: {
69
+ items: {
70
+ type: "array",
71
+ items: { type: "object" }
72
+ },
73
+ nextCursor: { type: "string" }
74
+ },
75
+ required: ["items"],
76
+ additionalProperties: false
77
+ };
78
+ var AntigravityImageError = class extends HarnessError {};
79
+ function createAntigravityImageTools(options) {
80
+ const fixedOptions = {
81
+ ...options,
82
+ transport: options.transport ?? createPrivateTransport()
83
+ };
84
+ return [createGenerateTool(fixedOptions), createListTool(fixedOptions)];
85
+ }
86
+ function createGenerateTool(options) {
87
+ return {
88
+ name: GENERATE_IMAGE_TOOL_NAME,
89
+ description: "Generate or edit bounded Antigravity images and return durable session image handles.",
90
+ parameters: {
91
+ type: "object",
92
+ properties: {
93
+ prompt: {
94
+ type: "string",
95
+ minLength: 1,
96
+ maxLength: 16384
97
+ },
98
+ references: {
99
+ type: "array",
100
+ maxItems: MAX_REFERENCES,
101
+ items: { oneOf: [{
102
+ type: "object",
103
+ properties: {
104
+ kind: { const: "session" },
105
+ handle: { type: "string" }
106
+ },
107
+ required: ["kind", "handle"],
108
+ additionalProperties: false
109
+ }, {
110
+ type: "object",
111
+ properties: {
112
+ kind: { const: "workspace" },
113
+ path: { type: "string" }
114
+ },
115
+ required: ["kind", "path"],
116
+ additionalProperties: false
117
+ }] }
118
+ },
119
+ model: { type: "string" },
120
+ n: {
121
+ type: "integer",
122
+ minimum: 1,
123
+ maximum: MAX_IMAGES
124
+ }
125
+ },
126
+ required: ["prompt"],
127
+ additionalProperties: false
128
+ },
129
+ output: {
130
+ schema: generateSchema,
131
+ render: (_args, value) => renderGenerate(value),
132
+ presentationMeta: (_args, value) => value
133
+ },
134
+ execute: async (args, exec) => executeGenerate(options, args, exec),
135
+ isConcurrencySafe: () => false
136
+ };
137
+ }
138
+ function createListTool(options) {
139
+ return {
140
+ name: LIST_IMAGES_TOOL_NAME,
141
+ description: "List durable image handles authorized by the current session with bounded pagination.",
142
+ parameters: {
143
+ type: "object",
144
+ properties: {
145
+ limit: {
146
+ type: "integer",
147
+ minimum: 1,
148
+ maximum: 20
149
+ },
150
+ cursor: { type: "string" },
151
+ origin: {
152
+ type: "string",
153
+ enum: [...IMAGE_ORIGINS]
154
+ }
155
+ },
156
+ additionalProperties: false
157
+ },
158
+ output: {
159
+ schema: listSchema,
160
+ render: (_args, value) => renderList(value),
161
+ presentationMeta: (_args, value) => value
162
+ },
163
+ execute: async (args, exec) => executeList(options, args, exec),
164
+ isConcurrencySafe: () => true
165
+ };
166
+ }
167
+ async function executeGenerate(options, rawArgs, exec) {
168
+ const settings = options.settings?.() ?? {
169
+ enabled: true,
170
+ model: "antigravity-gemini-3.1-flash-image",
171
+ n: 1
172
+ };
173
+ if (!settings.enabled) throw new AntigravityImageError("Antigravity Image is disabled by its capability gate", "IMAGE_DISABLED");
174
+ const args = parseGenerateArgs(rawArgs, settings);
175
+ const credential = await requireCredential(options.auth, exec.signal);
176
+ const agent = requireAgent(exec);
177
+ const cwd = workspaceCwd(agent);
178
+ const admission = {
179
+ attachments: options.attachments,
180
+ fs: options.fs
181
+ };
182
+ const references = [];
183
+ const referenceParts = [];
184
+ for (const [index, reference] of args.references.entries()) {
185
+ const admitted = reference.kind === "session" ? await admitSessionImage(admission, agent, reference.handle, exec.signal) : await admitWorkspaceImage(admission, cwd, reference.path, exec.signal);
186
+ const stored = admitted.stored ?? {
187
+ ref: await options.attachments.saveImage(admitted.input),
188
+ data: admitted.input.data
189
+ };
190
+ const item = {
191
+ handle: imageHandle(stored.ref),
192
+ attachment: stored.ref,
193
+ origin: "reference",
194
+ seq: index
195
+ };
196
+ references.push(item);
197
+ referenceParts.push({ inlineData: {
198
+ mimeType: stored.ref.mediaType,
199
+ data: Buffer.from(stored.data).toString("base64")
200
+ } });
201
+ }
202
+ const transport = options.transport;
203
+ if (transport === void 0) throw new AntigravityImageError("The Antigravity Image transport is unavailable", "IMAGE_FAILED");
204
+ const body = buildImagePayload(args.prompt, args.model, credential, referenceParts);
205
+ const images = [];
206
+ const warnings = [];
207
+ let firstFailure;
208
+ for (let index = 0; index < args.n; index += 1) {
209
+ let response;
210
+ try {
211
+ response = await transport.request({
212
+ url: ANTIGRAVITY_IMAGE_ENDPOINT,
213
+ accessToken: credential.accessToken,
214
+ body: JSON.stringify(body),
215
+ signal: exec.signal
216
+ });
217
+ } catch (error) {
218
+ const failure = toImageError(error);
219
+ if (failure.code === "IMAGE_CANCELLED") throw failure;
220
+ firstFailure ??= failure;
221
+ warnings.push({
222
+ index,
223
+ code: imageRequestWarning(failure.code)
224
+ });
225
+ continue;
226
+ }
227
+ const statusError = privateStatusError(response.status);
228
+ if (statusError !== void 0) {
229
+ await response.body?.cancel().catch(() => {});
230
+ const failure = toImageError(statusError);
231
+ firstFailure ??= failure;
232
+ warnings.push({
233
+ index,
234
+ code: imageRequestWarning(failure.code)
235
+ });
236
+ continue;
237
+ }
238
+ let envelope;
239
+ try {
240
+ envelope = JSON.parse(await readPrivateText(response, {
241
+ signal: exec.signal,
242
+ maxBytes: DEFAULT_PRIVATE_RESPONSE_BYTES
243
+ }));
244
+ } catch (error) {
245
+ const failure = toImageError(error);
246
+ if (failure.code === "IMAGE_CANCELLED") throw failure;
247
+ firstFailure ??= failure;
248
+ warnings.push({
249
+ index,
250
+ code: "IMAGE_RESPONSE_INVALID"
251
+ });
252
+ continue;
253
+ }
254
+ const candidate = collectInlineData(envelope)[0];
255
+ if (candidate === void 0) {
256
+ firstFailure ??= new AntigravityImageError("Antigravity Image returned no admitted inlineData", "IMAGE_RESPONSE_EMPTY");
257
+ warnings.push({
258
+ index,
259
+ code: "IMAGE_RESPONSE_EMPTY"
260
+ });
261
+ continue;
262
+ }
263
+ try {
264
+ const admitted = await admitBase64Image(options, candidate.data, "inline", `generated-${String(index + 1)}`, exec.signal);
265
+ if (candidate.mediaType !== void 0 && normalizeImageMime(candidate.mediaType) !== admitted.input.mediaType) throw new AntigravityImageError("The declared image MIME did not match the admitted magic bytes", "IMAGE_MIME_MISMATCH");
266
+ const attachment = await options.attachments.saveImage(admitted.input);
267
+ images.push({
268
+ handle: imageHandle(attachment),
269
+ attachment,
270
+ origin: "generated",
271
+ seq: index
272
+ });
273
+ } catch (error) {
274
+ if (exec.signal?.aborted === true) throw new AntigravityImageError("Antigravity Image generation was cancelled", "IMAGE_CANCELLED");
275
+ if (error instanceof AntigravityImageError && error.code === "MEDIA_CANCELLED") throw new AntigravityImageError("Antigravity Image generation was cancelled", "IMAGE_CANCELLED");
276
+ const failure = error instanceof AntigravityImageError && error.code === "IMAGE_MIME_MISMATCH" ? error : new AntigravityImageError("The generated image failed media admission", "IMAGE_ADMISSION_FAILED");
277
+ firstFailure ??= failure;
278
+ warnings.push({
279
+ index,
280
+ code: failure.code
281
+ });
282
+ }
283
+ }
284
+ if (images.length === 0) {
285
+ if (firstFailure !== void 0) throw firstFailure;
286
+ throw new AntigravityImageError("No generated image passed media admission", "IMAGE_RESPONSE_INVALID");
287
+ }
288
+ return {
289
+ operation: references.length === 0 ? "generate" : "edit",
290
+ images,
291
+ references,
292
+ warnings
293
+ };
294
+ }
295
+ async function executeList(options, rawArgs, exec) {
296
+ if (!(options.settings?.() ?? {
297
+ enabled: true,
298
+ model: "antigravity-gemini-3.1-flash-image",
299
+ n: 1
300
+ }).enabled) throw new AntigravityImageError("Antigravity Image is disabled by its capability gate", "IMAGE_DISABLED");
301
+ await requireCredential(options.auth, exec.signal);
302
+ const agent = requireAgent(exec);
303
+ const args = parseListArgs(rawArgs);
304
+ let items = collectImages(agent);
305
+ if (args.origin !== "all") items = items.filter((item) => item.origin === args.origin);
306
+ if (args.cursor !== void 0) items = afterCursor(items, args.cursor, args.origin);
307
+ const selected = items.slice(0, args.limit);
308
+ return {
309
+ items: selected,
310
+ ...items.length > selected.length && selected.length > 0 ? { nextCursor: encodeCursor(selected[selected.length - 1], args.origin) } : {}
311
+ };
312
+ }
313
+ function buildImagePayload(prompt, model, credential, referenceParts) {
314
+ const resolved = resolveModelWithTier(model, { cli_first: false });
315
+ if (resolved.isImageModel !== true) throw new AntigravityImageError("The selected Antigravity model is not an image model", "INVALID_ARGS");
316
+ const project = credential.projectId === "inductive-dreamer-qrkws" || !credential.projectId ? void 0 : credential.projectId;
317
+ return {
318
+ ...project === void 0 ? {} : { project },
319
+ model: resolved.actualModel,
320
+ request: {
321
+ contents: [{
322
+ role: "user",
323
+ parts: [{ text: prompt }, ...referenceParts]
324
+ }],
325
+ generationConfig: { responseModalities: ["TEXT", "IMAGE"] }
326
+ }
327
+ };
328
+ }
329
+ function normalizeImageMime(value) {
330
+ const normalized = value.trim().toLowerCase();
331
+ return normalized === "image/png" || normalized === "image/jpeg" || normalized === "image/webp" || normalized === "image/gif" ? normalized : void 0;
332
+ }
333
+ function collectInlineData(value) {
334
+ const output = [];
335
+ const visit = (item, depth = 0) => {
336
+ if (output.length >= MAX_IMAGES || depth > 32) return;
337
+ if (Array.isArray(item)) {
338
+ for (const child of item) visit(child, depth + 1);
339
+ return;
340
+ }
341
+ if (!isRecord(item)) return;
342
+ const inline = isRecord(item.inlineData) ? item.inlineData : isRecord(item.inline_data) ? item.inline_data : void 0;
343
+ if (inline !== void 0 && typeof inline.data === "string") {
344
+ const mediaType = inline.mimeType ?? inline.mime_type;
345
+ output.push({
346
+ data: inline.data,
347
+ ...typeof mediaType === "string" ? { mediaType } : {}
348
+ });
349
+ return;
350
+ }
351
+ for (const child of Object.values(item)) visit(child, depth + 1);
352
+ };
353
+ visit(value);
354
+ return output;
355
+ }
356
+ function collectImages(agent) {
357
+ return sessionImageCatalog(agent).map((entry) => ({
358
+ handle: entry.handle,
359
+ attachment: entry.attachment,
360
+ origin: entry.origin,
361
+ seq: entry.sequence
362
+ })).sort((left, right) => right.seq - left.seq || String(right.attachment.attachmentId).localeCompare(String(left.attachment.attachmentId)));
363
+ }
364
+ function renderGenerate(value) {
365
+ return [
366
+ {
367
+ type: "text",
368
+ text: `Created ${String(value.images.length)} durable image(s): ${value.images.map((item) => item.handle).join(", ")}.`
369
+ },
370
+ ...value.references.map((item) => ({
371
+ type: "image",
372
+ attachment: item.attachment
373
+ })),
374
+ ...value.images.map((item) => ({
375
+ type: "image",
376
+ attachment: item.attachment
377
+ }))
378
+ ];
379
+ }
380
+ function renderList(value) {
381
+ return [{
382
+ type: "text",
383
+ text: value.items.length === 0 ? "No authorized session images matched." : value.items.map((item) => `${item.handle} (${item.origin})`).join(", ")
384
+ }, ...value.items.map((item) => ({
385
+ type: "image",
386
+ attachment: item.attachment
387
+ }))];
388
+ }
389
+ /** Mount tools when a ToolRuntime is available; the tool bodies recheck the credential gate. */
390
+ function apply(ctx, config = {
391
+ enabled: true,
392
+ model: ANTIGRAVITY_IMAGE_MODEL,
393
+ n: 1
394
+ }) {
395
+ if (ctx === void 0) return;
396
+ const candidate = ctx;
397
+ if (candidate.tools === void 0 || candidate.attachments === void 0 || candidate.fs === void 0) return;
398
+ let current = () => config;
399
+ let lifecycle;
400
+ installSettingsSection(ctx, ANTIGRAVITY_IMAGE_SETTINGS_NAMESPACE, Config, config, {
401
+ setSource: (source) => {
402
+ current = source;
403
+ lifecycle?.sync();
404
+ },
405
+ onChange: () => {
406
+ lifecycle?.sync();
407
+ }
408
+ });
409
+ const provided = candidate.get?.("antigravityAuth");
410
+ const auth = isAuthService(provided) ? provided : createAntigravityAuthService();
411
+ const options = {
412
+ auth,
413
+ attachments: candidate.attachments,
414
+ fs: candidate.fs,
415
+ settings: current
416
+ };
417
+ lifecycle = mountCapabilityLifecycle({
418
+ ctx,
419
+ auth,
420
+ id: "image",
421
+ enabled: () => current().enabled,
422
+ register: () => registerCapabilitySet(createAntigravityImageTools(options), (tool) => candidate.tools.register(tool)),
423
+ ownsAuth: auth !== provided,
424
+ label: "antigravity-image: tool lifecycle"
425
+ });
426
+ }
427
+ async function requireCredential(auth, signal) {
428
+ const credential = await auth.credential(signal);
429
+ if (credential === void 0) throw new AntigravityImageError("Antigravity Image requires a logged-in account", "IMAGE_AUTH_REQUIRED");
430
+ return credential;
431
+ }
432
+ function requireAgent(exec) {
433
+ if (exec.agent === void 0) throw new AntigravityImageError("Antigravity Image requires an active session", "IMAGE_AGENT_REQUIRED");
434
+ workspaceCwd(exec.agent);
435
+ return exec.agent;
436
+ }
437
+ function workspaceCwd(agent) {
438
+ const cwd = agent.session.header.cwd;
439
+ if (typeof cwd !== "string" || cwd.length === 0) throw new AntigravityImageError("Antigravity Image requires an active workspace", "IMAGE_WORKSPACE_REQUIRED");
440
+ return cwd;
441
+ }
442
+ function parseGenerateArgs(value, settings) {
443
+ if (!isRecord(value) || hasExtra(value, [
444
+ "prompt",
445
+ "references",
446
+ "model",
447
+ "n"
448
+ ]) || typeof value.prompt !== "string" || value.prompt.trim().length === 0 || value.prompt.length > 16384) throw new AntigravityImageError("generate_image expects a closed prompt object", "INVALID_ARGS");
449
+ const refs = value.references === void 0 ? [] : parseReferences(value.references);
450
+ const model = nonBlank(value.model ?? settings.model);
451
+ const n = integer(value.n ?? settings.n, 1, MAX_IMAGES);
452
+ return {
453
+ prompt: value.prompt.trim().slice(0, 16384),
454
+ references: refs,
455
+ model,
456
+ n
457
+ };
458
+ }
459
+ function parseReferences(value) {
460
+ if (!Array.isArray(value) || value.length > MAX_REFERENCES) throw new AntigravityImageError("references exceed the bounded limit", "INVALID_ARGS");
461
+ return value.map((item) => {
462
+ if (!isRecord(item) || hasExtra(item, [
463
+ "kind",
464
+ "handle",
465
+ "path"
466
+ ]) || typeof item.kind !== "string") throw new AntigravityImageError("reference is invalid", "INVALID_ARGS");
467
+ if (item.kind === "session" && typeof item.handle === "string" && IMAGE_HANDLE_PATTERN.test(item.handle)) return {
468
+ kind: "session",
469
+ handle: item.handle
470
+ };
471
+ if (item.kind === "workspace" && typeof item.path === "string" && item.path.length > 0) return {
472
+ kind: "workspace",
473
+ path: item.path
474
+ };
475
+ throw new AntigravityImageError("reference is invalid", "INVALID_ARGS");
476
+ });
477
+ }
478
+ function parseListArgs(value) {
479
+ if (!isRecord(value) || hasExtra(value, [
480
+ "limit",
481
+ "cursor",
482
+ "origin"
483
+ ])) throw new AntigravityImageError("list_images expects a closed object", "INVALID_ARGS");
484
+ const limit = value.limit === void 0 ? 5 : integer(value.limit, 1, 20);
485
+ const cursor = value.cursor === void 0 ? void 0 : nonBlank(value.cursor);
486
+ const origin = value.origin === void 0 ? "all" : enumValue(value.origin, IMAGE_ORIGINS);
487
+ return {
488
+ limit,
489
+ ...cursor === void 0 ? {} : { cursor },
490
+ origin
491
+ };
492
+ }
493
+ function afterCursor(items, cursor, origin) {
494
+ let value;
495
+ try {
496
+ value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
497
+ } catch {
498
+ throw new AntigravityImageError("The image cursor is invalid", "IMAGE_CURSOR_INVALID");
499
+ }
500
+ if (!isRecord(value) || Object.keys(value).length !== 3 || hasExtra(value, [
501
+ "id",
502
+ "seq",
503
+ "origin"
504
+ ]) || typeof value.id !== "string" || value.id.length === 0 || value.id.length > 256 || !Number.isSafeInteger(value.seq) || value.origin !== origin) throw new AntigravityImageError("The image cursor is invalid", "IMAGE_CURSOR_INVALID");
505
+ const index = items.findIndex((item) => item.seq === value.seq && String(item.attachment.attachmentId) === value.id);
506
+ if (index < 0) throw new AntigravityImageError("The image cursor is stale", "IMAGE_CURSOR_INVALID");
507
+ return items.slice(index + 1);
508
+ }
509
+ function encodeCursor(item, origin) {
510
+ return Buffer.from(JSON.stringify({
511
+ id: String(item.attachment.attachmentId),
512
+ seq: item.seq,
513
+ origin
514
+ })).toString("base64url");
515
+ }
516
+ function isAuthService(value) {
517
+ return isRecord(value) && typeof value.credential === "function" && typeof value.status === "function";
518
+ }
519
+ function imageRequestWarning(code) {
520
+ if (code === "IMAGE_AUTH_REQUIRED") return "IMAGE_REQUEST_UNAUTHENTICATED";
521
+ if (code === "IMAGE_RATE_LIMITED") return "IMAGE_REQUEST_RATE_LIMITED";
522
+ if (code === "IMAGE_TIMEOUT") return "IMAGE_REQUEST_TIMEOUT";
523
+ if (code === "IMAGE_PROTOCOL_DRIFT") return "IMAGE_REQUEST_PROTOCOL_DRIFT";
524
+ return "IMAGE_REQUEST_FAILED";
525
+ }
526
+ const IMAGE_FAILURE_CODES = {
527
+ authentication: "IMAGE_AUTH_REQUIRED",
528
+ forbidden: "IMAGE_FORBIDDEN",
529
+ "rate-limited": "IMAGE_RATE_LIMITED",
530
+ cancelled: "IMAGE_CANCELLED",
531
+ timeout: "IMAGE_TIMEOUT",
532
+ "attribution-rejected": "IMAGE_PROTOCOL_DRIFT",
533
+ "protocol-drift": "IMAGE_PROTOCOL_DRIFT",
534
+ "response-limit": "IMAGE_PROTOCOL_DRIFT",
535
+ "request-limit": "IMAGE_PROTOCOL_DRIFT",
536
+ upstream: "IMAGE_FAILED",
537
+ network: "IMAGE_FAILED",
538
+ failed: "IMAGE_FAILED"
539
+ };
540
+ function toImageError(error) {
541
+ if (error instanceof AntigravityImageError) return error;
542
+ if (error instanceof PrivateTransportError) return new AntigravityImageError("The Antigravity Image request failed safely", IMAGE_FAILURE_CODES[classifyPrivateFailure(error)]);
543
+ return new AntigravityImageError("The Antigravity Image request failed safely", "IMAGE_FAILED");
544
+ }
545
+ function hasExtra(value, allowed) {
546
+ return Object.keys(value).some((key) => !allowed.includes(key));
547
+ }
548
+ function nonBlank(value) {
549
+ if (typeof value !== "string" || value.trim().length === 0 || value.length > 512) throw new AntigravityImageError("The image option is invalid", "INVALID_ARGS");
550
+ return value.trim();
551
+ }
552
+ function integer(value, min, max) {
553
+ if (!Number.isSafeInteger(value) || value < min || value > max) throw new AntigravityImageError("The image option is invalid", "INVALID_ARGS");
554
+ return value;
555
+ }
556
+ function enumValue(value, values) {
557
+ if (typeof value !== "string" || !values.includes(value)) throw new AntigravityImageError("The image option is invalid", "INVALID_ARGS");
558
+ return value;
559
+ }
560
+ function isRecord(value) {
561
+ return typeof value === "object" && value !== null && !Array.isArray(value);
562
+ }
563
+ //#endregion
564
+ export { ANTIGRAVITY_IMAGE_ENDPOINT, ANTIGRAVITY_IMAGE_MODEL, ANTIGRAVITY_IMAGE_SETTINGS_NAMESPACE, AntigravityImageError, Config, GENERATE_IMAGE_TOOL_NAME, LIST_IMAGES_TOOL_NAME, apply, createAntigravityImageTools, inject, name };