@mariozechner/pi-coding-agent 0.49.2 → 0.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +126 -1
- package/README.md +310 -1229
- package/dist/cli/args.d.ts +5 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +57 -22
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/config-selector.d.ts +14 -0
- package/dist/cli/config-selector.d.ts.map +1 -0
- package/dist/cli/config-selector.js +31 -0
- package/dist/cli/config-selector.js.map +1 -0
- package/dist/cli/session-picker.d.ts.map +1 -1
- package/dist/cli/session-picker.js +1 -1
- package/dist/cli/session-picker.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +6 -0
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session.d.ts +53 -34
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +262 -67
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +8 -18
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +39 -55
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +2 -1
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.d.ts.map +1 -0
- package/dist/core/diagnostics.js +2 -0
- package/dist/core/diagnostics.js.map +1 -0
- package/dist/core/export-html/template.css +9 -0
- package/dist/core/export-html/template.js +6 -4
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +10 -1
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +9 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +39 -12
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +112 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts +9 -2
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +13 -0
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/model-registry.d.ts +42 -2
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +154 -44
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +3 -2
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/package-manager.d.ts +129 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1148 -0
- package/dist/core/package-manager.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +6 -0
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +114 -54
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts +160 -0
- package/dist/core/resource-loader.d.ts.map +1 -0
- package/dist/core/resource-loader.js +604 -0
- package/dist/core/resource-loader.js.map +1 -0
- package/dist/core/sdk.d.ts +14 -105
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +52 -304
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +45 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +39 -16
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +107 -25
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +18 -10
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +126 -93
- package/dist/core/skills.js.map +1 -1
- package/dist/core/system-prompt.d.ts +3 -27
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +16 -103
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +2 -1
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +4 -4
- package/dist/core/tools/read.js.map +1 -1
- package/dist/index.d.ts +12 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -6
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +209 -97
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts +3 -2
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +5 -3
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts +5 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.js +29 -9
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
- package/dist/modes/interactive/components/branch-summary-message.d.ts +3 -2
- package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/branch-summary-message.js +4 -2
- package/dist/modes/interactive/components/branch-summary-message.js.map +1 -1
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +3 -2
- package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/compaction-summary-message.js +4 -2
- package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts +71 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/config-selector.js +468 -0
- package/dist/modes/interactive/components/config-selector.js.map +1 -0
- package/dist/modes/interactive/components/custom-message.d.ts +3 -2
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-message.js +4 -2
- package/dist/modes/interactive/components/custom-message.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +9 -0
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +3 -4
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +18 -1
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +195 -87
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +12 -5
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector.js +2 -2
- package/dist/modes/interactive/components/tree-selector.js.map +1 -1
- package/dist/modes/interactive/components/user-message.d.ts +2 -2
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message.js +2 -2
- package/dist/modes/interactive/components/user-message.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +47 -2
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +566 -211
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/dark.json +1 -1
- package/dist/modes/interactive/theme/light.json +1 -1
- package/dist/modes/interactive/theme/theme-schema.json +8 -1
- package/dist/modes/interactive/theme/theme.d.ts +8 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +79 -28
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +25 -89
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +32 -92
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/utils/git.d.ts +2 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +6 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/shell.d.ts +1 -0
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +16 -2
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/sleep.d.ts +5 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +17 -0
- package/dist/utils/sleep.js.map +1 -0
- package/docs/compaction.md +23 -21
- package/docs/custom-provider.md +538 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +180 -118
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +79 -0
- package/docs/keybindings.md +162 -0
- package/docs/models.md +193 -0
- package/docs/packages.md +163 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +147 -0
- package/docs/sdk.md +111 -178
- package/docs/session.md +167 -16
- package/docs/settings.md +216 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +111 -202
- package/docs/terminal-setup.md +65 -0
- package/docs/themes.md +295 -0
- package/docs/tui.md +36 -5
- package/docs/windows.md +17 -0
- package/examples/README.md +1 -0
- package/examples/extensions/README.md +24 -2
- package/examples/extensions/antigravity-image-gen.ts +413 -0
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/doom-overlay/doom/build.sh +1 -1
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/inline-bash.ts +94 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/space-invaders.ts +560 -0
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/02-custom-model.ts +3 -3
- package/examples/sdk/03-custom-prompt.ts +20 -9
- package/examples/sdk/04-skills.ts +26 -27
- package/examples/sdk/06-extensions.ts +15 -6
- package/examples/sdk/07-context-files.ts +22 -18
- package/examples/sdk/08-prompt-templates.ts +19 -14
- package/examples/sdk/09-api-keys-and-oauth.ts +5 -12
- package/examples/sdk/10-settings.ts +3 -3
- package/examples/sdk/12-full-control.ts +16 -7
- package/examples/sdk/README.md +24 -30
- package/package.json +4 -4
- package/docs/theme.md +0 -617
- package/examples/extensions/chalk-logger.ts +0 -26
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Antigravity Image Generation
|
|
3
|
+
*
|
|
4
|
+
* Generates images via Google Antigravity's image models (gemini-3-pro-image, imagen-3).
|
|
5
|
+
* Returns images as tool result attachments for inline terminal rendering.
|
|
6
|
+
* Requires OAuth login via /login for google-antigravity.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* "Generate an image of a sunset over mountains"
|
|
10
|
+
* "Create a 16:9 wallpaper of a cyberpunk city"
|
|
11
|
+
*
|
|
12
|
+
* Save modes (tool param, env var, or config file):
|
|
13
|
+
* save=none - Don't save to disk (default)
|
|
14
|
+
* save=project - Save to <repo>/.pi/generated-images/
|
|
15
|
+
* save=global - Save to ~/.pi/agent/generated-images/
|
|
16
|
+
* save=custom - Save to saveDir param or PI_IMAGE_SAVE_DIR
|
|
17
|
+
*
|
|
18
|
+
* Environment variables:
|
|
19
|
+
* PI_IMAGE_SAVE_MODE - Default save mode (none|project|global|custom)
|
|
20
|
+
* PI_IMAGE_SAVE_DIR - Directory for custom save mode
|
|
21
|
+
*
|
|
22
|
+
* Config files (project overrides global):
|
|
23
|
+
* ~/.pi/agent/extensions/antigravity-image-gen.json
|
|
24
|
+
* <repo>/.pi/extensions/antigravity-image-gen.json
|
|
25
|
+
* Example: { "save": "global" }
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { randomUUID } from "node:crypto";
|
|
29
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
30
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
31
|
+
import { homedir } from "node:os";
|
|
32
|
+
import { join } from "node:path";
|
|
33
|
+
import { StringEnum } from "@mariozechner/pi-ai";
|
|
34
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
35
|
+
import { type Static, Type } from "@sinclair/typebox";
|
|
36
|
+
|
|
37
|
+
const PROVIDER = "google-antigravity";
|
|
38
|
+
|
|
39
|
+
const ASPECT_RATIOS = ["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"] as const;
|
|
40
|
+
|
|
41
|
+
type AspectRatio = (typeof ASPECT_RATIOS)[number];
|
|
42
|
+
|
|
43
|
+
const DEFAULT_MODEL = "gemini-3-pro-image";
|
|
44
|
+
const DEFAULT_ASPECT_RATIO: AspectRatio = "1:1";
|
|
45
|
+
const DEFAULT_SAVE_MODE = "none";
|
|
46
|
+
|
|
47
|
+
const SAVE_MODES = ["none", "project", "global", "custom"] as const;
|
|
48
|
+
type SaveMode = (typeof SAVE_MODES)[number];
|
|
49
|
+
|
|
50
|
+
const ANTIGRAVITY_ENDPOINT = "https://daily-cloudcode-pa.sandbox.googleapis.com";
|
|
51
|
+
|
|
52
|
+
const ANTIGRAVITY_HEADERS = {
|
|
53
|
+
"User-Agent": "antigravity/1.11.5 darwin/arm64",
|
|
54
|
+
"X-Goog-Api-Client": "google-cloud-sdk vscode_cloudshelleditor/0.1",
|
|
55
|
+
"Client-Metadata": JSON.stringify({
|
|
56
|
+
ideType: "IDE_UNSPECIFIED",
|
|
57
|
+
platform: "PLATFORM_UNSPECIFIED",
|
|
58
|
+
pluginType: "GEMINI",
|
|
59
|
+
}),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const IMAGE_SYSTEM_INSTRUCTION =
|
|
63
|
+
"You are an AI image generator. Generate images based on user descriptions. Focus on creating high-quality, visually appealing images that match the user's request.";
|
|
64
|
+
|
|
65
|
+
const TOOL_PARAMS = Type.Object({
|
|
66
|
+
prompt: Type.String({ description: "Image description." }),
|
|
67
|
+
model: Type.Optional(
|
|
68
|
+
Type.String({
|
|
69
|
+
description: "Image model id (e.g., gemini-3-pro-image, imagen-3). Default: gemini-3-pro-image.",
|
|
70
|
+
}),
|
|
71
|
+
),
|
|
72
|
+
aspectRatio: Type.Optional(StringEnum(ASPECT_RATIOS)),
|
|
73
|
+
save: Type.Optional(StringEnum(SAVE_MODES)),
|
|
74
|
+
saveDir: Type.Optional(
|
|
75
|
+
Type.String({
|
|
76
|
+
description: "Directory to save image when save=custom. Defaults to PI_IMAGE_SAVE_DIR if set.",
|
|
77
|
+
}),
|
|
78
|
+
),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
type ToolParams = Static<typeof TOOL_PARAMS>;
|
|
82
|
+
|
|
83
|
+
interface CloudCodeAssistRequest {
|
|
84
|
+
project: string;
|
|
85
|
+
model: string;
|
|
86
|
+
request: {
|
|
87
|
+
contents: Content[];
|
|
88
|
+
sessionId?: string;
|
|
89
|
+
systemInstruction?: { role?: string; parts: { text: string }[] };
|
|
90
|
+
generationConfig?: {
|
|
91
|
+
maxOutputTokens?: number;
|
|
92
|
+
temperature?: number;
|
|
93
|
+
imageConfig?: { aspectRatio?: string };
|
|
94
|
+
candidateCount?: number;
|
|
95
|
+
};
|
|
96
|
+
safetySettings?: Array<{ category: string; threshold: string }>;
|
|
97
|
+
};
|
|
98
|
+
requestType?: string;
|
|
99
|
+
userAgent?: string;
|
|
100
|
+
requestId?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface CloudCodeAssistResponseChunk {
|
|
104
|
+
response?: {
|
|
105
|
+
candidates?: Array<{
|
|
106
|
+
content?: {
|
|
107
|
+
role: string;
|
|
108
|
+
parts?: Array<{
|
|
109
|
+
text?: string;
|
|
110
|
+
inlineData?: {
|
|
111
|
+
mimeType?: string;
|
|
112
|
+
data?: string;
|
|
113
|
+
};
|
|
114
|
+
}>;
|
|
115
|
+
};
|
|
116
|
+
}>;
|
|
117
|
+
usageMetadata?: {
|
|
118
|
+
promptTokenCount?: number;
|
|
119
|
+
candidatesTokenCount?: number;
|
|
120
|
+
thoughtsTokenCount?: number;
|
|
121
|
+
totalTokenCount?: number;
|
|
122
|
+
cachedContentTokenCount?: number;
|
|
123
|
+
};
|
|
124
|
+
modelVersion?: string;
|
|
125
|
+
responseId?: string;
|
|
126
|
+
};
|
|
127
|
+
traceId?: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
interface Content {
|
|
131
|
+
role: "user" | "model";
|
|
132
|
+
parts: Part[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface Part {
|
|
136
|
+
text?: string;
|
|
137
|
+
inlineData?: {
|
|
138
|
+
mimeType?: string;
|
|
139
|
+
data?: string;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface ParsedCredentials {
|
|
144
|
+
accessToken: string;
|
|
145
|
+
projectId: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface ExtensionConfig {
|
|
149
|
+
save?: SaveMode;
|
|
150
|
+
saveDir?: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
interface SaveConfig {
|
|
154
|
+
mode: SaveMode;
|
|
155
|
+
outputDir?: string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function parseOAuthCredentials(raw: string): ParsedCredentials {
|
|
159
|
+
let parsed: { token?: string; projectId?: string };
|
|
160
|
+
try {
|
|
161
|
+
parsed = JSON.parse(raw) as { token?: string; projectId?: string };
|
|
162
|
+
} catch {
|
|
163
|
+
throw new Error("Invalid Google OAuth credentials. Run /login to re-authenticate.");
|
|
164
|
+
}
|
|
165
|
+
if (!parsed.token || !parsed.projectId) {
|
|
166
|
+
throw new Error("Missing token or projectId in Google OAuth credentials. Run /login.");
|
|
167
|
+
}
|
|
168
|
+
return { accessToken: parsed.token, projectId: parsed.projectId };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function readConfigFile(path: string): ExtensionConfig {
|
|
172
|
+
if (!existsSync(path)) {
|
|
173
|
+
return {};
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
const content = readFileSync(path, "utf-8");
|
|
177
|
+
const parsed = JSON.parse(content) as ExtensionConfig;
|
|
178
|
+
return parsed ?? {};
|
|
179
|
+
} catch {
|
|
180
|
+
return {};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function loadConfig(cwd: string): ExtensionConfig {
|
|
185
|
+
const globalConfig = readConfigFile(join(homedir(), ".pi", "agent", "extensions", "antigravity-image-gen.json"));
|
|
186
|
+
const projectConfig = readConfigFile(join(cwd, ".pi", "extensions", "antigravity-image-gen.json"));
|
|
187
|
+
return { ...globalConfig, ...projectConfig };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function resolveSaveConfig(params: ToolParams, cwd: string): SaveConfig {
|
|
191
|
+
const config = loadConfig(cwd);
|
|
192
|
+
const envMode = (process.env.PI_IMAGE_SAVE_MODE || "").toLowerCase();
|
|
193
|
+
const paramMode = params.save;
|
|
194
|
+
const mode = (paramMode || envMode || config.save || DEFAULT_SAVE_MODE) as SaveMode;
|
|
195
|
+
|
|
196
|
+
if (!SAVE_MODES.includes(mode)) {
|
|
197
|
+
return { mode: DEFAULT_SAVE_MODE as SaveMode };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (mode === "project") {
|
|
201
|
+
return { mode, outputDir: join(cwd, ".pi", "generated-images") };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (mode === "global") {
|
|
205
|
+
return { mode, outputDir: join(homedir(), ".pi", "agent", "generated-images") };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (mode === "custom") {
|
|
209
|
+
const dir = params.saveDir || process.env.PI_IMAGE_SAVE_DIR || config.saveDir;
|
|
210
|
+
if (!dir || !dir.trim()) {
|
|
211
|
+
throw new Error("save=custom requires saveDir or PI_IMAGE_SAVE_DIR.");
|
|
212
|
+
}
|
|
213
|
+
return { mode, outputDir: dir };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return { mode };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function imageExtension(mimeType: string): string {
|
|
220
|
+
const lower = mimeType.toLowerCase();
|
|
221
|
+
if (lower.includes("jpeg") || lower.includes("jpg")) return "jpg";
|
|
222
|
+
if (lower.includes("gif")) return "gif";
|
|
223
|
+
if (lower.includes("webp")) return "webp";
|
|
224
|
+
return "png";
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function saveImage(base64Data: string, mimeType: string, outputDir: string): Promise<string> {
|
|
228
|
+
await mkdir(outputDir, { recursive: true });
|
|
229
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
230
|
+
const ext = imageExtension(mimeType);
|
|
231
|
+
const filename = `image-${timestamp}-${randomUUID().slice(0, 8)}.${ext}`;
|
|
232
|
+
const filePath = join(outputDir, filename);
|
|
233
|
+
await writeFile(filePath, Buffer.from(base64Data, "base64"));
|
|
234
|
+
return filePath;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function buildRequest(prompt: string, model: string, projectId: string, aspectRatio: string): CloudCodeAssistRequest {
|
|
238
|
+
return {
|
|
239
|
+
project: projectId,
|
|
240
|
+
model,
|
|
241
|
+
request: {
|
|
242
|
+
contents: [
|
|
243
|
+
{
|
|
244
|
+
role: "user",
|
|
245
|
+
parts: [{ text: prompt }],
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
systemInstruction: {
|
|
249
|
+
parts: [{ text: IMAGE_SYSTEM_INSTRUCTION }],
|
|
250
|
+
},
|
|
251
|
+
generationConfig: {
|
|
252
|
+
imageConfig: { aspectRatio },
|
|
253
|
+
candidateCount: 1,
|
|
254
|
+
},
|
|
255
|
+
safetySettings: [
|
|
256
|
+
{ category: "HARM_CATEGORY_HARASSMENT", threshold: "BLOCK_ONLY_HIGH" },
|
|
257
|
+
{ category: "HARM_CATEGORY_HATE_SPEECH", threshold: "BLOCK_ONLY_HIGH" },
|
|
258
|
+
{ category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold: "BLOCK_ONLY_HIGH" },
|
|
259
|
+
{ category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_ONLY_HIGH" },
|
|
260
|
+
{ category: "HARM_CATEGORY_CIVIC_INTEGRITY", threshold: "BLOCK_ONLY_HIGH" },
|
|
261
|
+
],
|
|
262
|
+
},
|
|
263
|
+
requestType: "agent",
|
|
264
|
+
requestId: `agent-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
265
|
+
userAgent: "antigravity",
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function parseSseForImage(
|
|
270
|
+
response: Response,
|
|
271
|
+
signal?: AbortSignal,
|
|
272
|
+
): Promise<{ image: { data: string; mimeType: string }; text: string[] }> {
|
|
273
|
+
if (!response.body) {
|
|
274
|
+
throw new Error("No response body");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const reader = response.body.getReader();
|
|
278
|
+
const decoder = new TextDecoder();
|
|
279
|
+
let buffer = "";
|
|
280
|
+
const textParts: string[] = [];
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
while (true) {
|
|
284
|
+
if (signal?.aborted) {
|
|
285
|
+
throw new Error("Request was aborted");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const { done, value } = await reader.read();
|
|
289
|
+
if (done) break;
|
|
290
|
+
|
|
291
|
+
buffer += decoder.decode(value, { stream: true });
|
|
292
|
+
const lines = buffer.split("\n");
|
|
293
|
+
buffer = lines.pop() || "";
|
|
294
|
+
|
|
295
|
+
for (const line of lines) {
|
|
296
|
+
if (!line.startsWith("data:")) continue;
|
|
297
|
+
const jsonStr = line.slice(5).trim();
|
|
298
|
+
if (!jsonStr) continue;
|
|
299
|
+
|
|
300
|
+
let chunk: CloudCodeAssistResponseChunk;
|
|
301
|
+
try {
|
|
302
|
+
chunk = JSON.parse(jsonStr) as CloudCodeAssistResponseChunk;
|
|
303
|
+
} catch {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const responseData = chunk.response;
|
|
308
|
+
if (!responseData?.candidates) continue;
|
|
309
|
+
|
|
310
|
+
for (const candidate of responseData.candidates) {
|
|
311
|
+
const parts = candidate.content?.parts;
|
|
312
|
+
if (!parts) continue;
|
|
313
|
+
for (const part of parts) {
|
|
314
|
+
if (part.text) {
|
|
315
|
+
textParts.push(part.text);
|
|
316
|
+
}
|
|
317
|
+
if (part.inlineData?.data) {
|
|
318
|
+
await reader.cancel();
|
|
319
|
+
return {
|
|
320
|
+
image: {
|
|
321
|
+
data: part.inlineData.data,
|
|
322
|
+
mimeType: part.inlineData.mimeType || "image/png",
|
|
323
|
+
},
|
|
324
|
+
text: textParts,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
} finally {
|
|
332
|
+
reader.releaseLock();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
throw new Error("No image data returned by the model");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async function getCredentials(ctx: {
|
|
339
|
+
modelRegistry: { getApiKeyForProvider: (provider: string) => Promise<string | undefined> };
|
|
340
|
+
}): Promise<ParsedCredentials> {
|
|
341
|
+
const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER);
|
|
342
|
+
if (!apiKey) {
|
|
343
|
+
throw new Error("Missing Google Antigravity OAuth credentials. Run /login for google-antigravity.");
|
|
344
|
+
}
|
|
345
|
+
return parseOAuthCredentials(apiKey);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export default function antigravityImageGen(pi: ExtensionAPI) {
|
|
349
|
+
pi.registerTool({
|
|
350
|
+
name: "generate_image",
|
|
351
|
+
label: "Generate image",
|
|
352
|
+
description:
|
|
353
|
+
"Generate an image via Google Antigravity image models. Returns the image as a tool result attachment. Optional saving via save=project|global|custom|none, or PI_IMAGE_SAVE_MODE/PI_IMAGE_SAVE_DIR.",
|
|
354
|
+
parameters: TOOL_PARAMS,
|
|
355
|
+
async execute(_toolCallId, params: ToolParams, onUpdate, ctx, signal) {
|
|
356
|
+
const { accessToken, projectId } = await getCredentials(ctx);
|
|
357
|
+
const model = params.model || DEFAULT_MODEL;
|
|
358
|
+
const aspectRatio = params.aspectRatio || DEFAULT_ASPECT_RATIO;
|
|
359
|
+
|
|
360
|
+
const requestBody = buildRequest(params.prompt, model, projectId, aspectRatio);
|
|
361
|
+
onUpdate?.({
|
|
362
|
+
content: [{ type: "text", text: `Requesting image from ${PROVIDER}/${model}...` }],
|
|
363
|
+
details: { provider: PROVIDER, model, aspectRatio },
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
const response = await fetch(`${ANTIGRAVITY_ENDPOINT}/v1internal:streamGenerateContent?alt=sse`, {
|
|
367
|
+
method: "POST",
|
|
368
|
+
headers: {
|
|
369
|
+
Authorization: `Bearer ${accessToken}`,
|
|
370
|
+
"Content-Type": "application/json",
|
|
371
|
+
Accept: "text/event-stream",
|
|
372
|
+
...ANTIGRAVITY_HEADERS,
|
|
373
|
+
},
|
|
374
|
+
body: JSON.stringify(requestBody),
|
|
375
|
+
signal,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
if (!response.ok) {
|
|
379
|
+
const errorText = await response.text();
|
|
380
|
+
throw new Error(`Image request failed (${response.status}): ${errorText}`);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const parsed = await parseSseForImage(response, signal);
|
|
384
|
+
const saveConfig = resolveSaveConfig(params, ctx.cwd);
|
|
385
|
+
let savedPath: string | undefined;
|
|
386
|
+
let saveError: string | undefined;
|
|
387
|
+
if (saveConfig.mode !== "none" && saveConfig.outputDir) {
|
|
388
|
+
try {
|
|
389
|
+
savedPath = await saveImage(parsed.image.data, parsed.image.mimeType, saveConfig.outputDir);
|
|
390
|
+
} catch (error) {
|
|
391
|
+
saveError = error instanceof Error ? error.message : String(error);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const summaryParts = [`Generated image via ${PROVIDER}/${model}.`, `Aspect ratio: ${aspectRatio}.`];
|
|
395
|
+
if (savedPath) {
|
|
396
|
+
summaryParts.push(`Saved image to: ${savedPath}`);
|
|
397
|
+
} else if (saveError) {
|
|
398
|
+
summaryParts.push(`Failed to save image: ${saveError}`);
|
|
399
|
+
}
|
|
400
|
+
if (parsed.text.length > 0) {
|
|
401
|
+
summaryParts.push(`Model notes: ${parsed.text.join(" ")}`);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return {
|
|
405
|
+
content: [
|
|
406
|
+
{ type: "text", text: summaryParts.join(" ") },
|
|
407
|
+
{ type: "image", data: parsed.image.data, mimeType: parsed.image.mimeType },
|
|
408
|
+
],
|
|
409
|
+
details: { provider: PROVIDER, model, aspectRatio, savedPath, saveMode: saveConfig.mode },
|
|
410
|
+
};
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry bookmarking example.
|
|
3
|
+
*
|
|
4
|
+
* Shows setLabel to mark entries with labels for easy navigation in /tree.
|
|
5
|
+
* Labels appear in the tree view and help you find important points.
|
|
6
|
+
*
|
|
7
|
+
* Usage: /bookmark [label] - bookmark the last assistant message
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
11
|
+
|
|
12
|
+
export default function (pi: ExtensionAPI) {
|
|
13
|
+
pi.registerCommand("bookmark", {
|
|
14
|
+
description: "Bookmark last message (usage: /bookmark [label])",
|
|
15
|
+
handler: async (args, ctx) => {
|
|
16
|
+
const label = args.trim() || `bookmark-${Date.now()}`;
|
|
17
|
+
|
|
18
|
+
// Find the last assistant message entry
|
|
19
|
+
const entries = ctx.sessionManager.getEntries();
|
|
20
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
21
|
+
const entry = entries[i];
|
|
22
|
+
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
23
|
+
pi.setLabel(entry.id, label);
|
|
24
|
+
ctx.ui.notify(`Bookmarked as: ${label}`, "info");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
ctx.ui.notify("No assistant message to bookmark", "warning");
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Remove bookmark
|
|
34
|
+
pi.registerCommand("unbookmark", {
|
|
35
|
+
description: "Remove bookmark from last labeled entry",
|
|
36
|
+
handler: async (_args, ctx) => {
|
|
37
|
+
const entries = ctx.sessionManager.getEntries();
|
|
38
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
39
|
+
const entry = entries[i];
|
|
40
|
+
const label = ctx.sessionManager.getLabel(entry.id);
|
|
41
|
+
if (label) {
|
|
42
|
+
pi.setLabel(entry.id, undefined);
|
|
43
|
+
ctx.ui.notify(`Removed bookmark: ${label}`, "info");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
ctx.ui.notify("No bookmarked entry found", "warning");
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|