-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
151 lines (124 loc) · 4.42 KB
/
api.ts
File metadata and controls
151 lines (124 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { loadAuth } from "./auth.js";
const ANTIGRAVITY_ENDPOINT = "https://daily-cloudcode-pa.sandbox.googleapis.com";
// Headers mimicking the Cloud Code extension
const HEADERS = {
"User-Agent": "antigravity/1.11.5 darwin/arm64",
"X-Goog-Api-Client": "google-cloud-sdk vscode_cloudshelleditor/0.1",
"Client-Metadata": JSON.stringify({
ideType: "IDE_UNSPECIFIED",
platform: "PLATFORM_UNSPECIFIED",
pluginType: "GEMINI",
}),
};
export async function enhancePrompt(draft: string): Promise<string> {
const auth = loadAuth();
if (!auth?.access || !auth?.projectId) throw new Error("No Antigravity credentials found.");
const model = "gemini-3-flash";
const url = `${ANTIGRAVITY_ENDPOINT}/v1internal:streamGenerateContent?alt=sse`;
const systemPrompt = "You are an expert AI art prompter. Take the user's rough idea and transform it into a highly detailed, descriptive prompt suitable for a high-end image generation model. Focus on lighting, style, composition, and texture. Output ONLY the enhanced prompt, no preamble.";
const body = {
project: auth.projectId,
model,
request: {
contents: [{ role: "user", parts: [{ text: draft }] }],
systemInstruction: { parts: [{ text: systemPrompt }] },
},
requestType: "agent",
userAgent: "antigravity",
};
const response = await fetch(url, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.access}`,
"Content-Type": "application/json",
"Accept": "text/event-stream",
...HEADERS,
},
body: JSON.stringify(body),
});
if (!response.ok) throw new Error(`Prompt enhancement failed: ${response.status}`);
// Simple SSE parser for text
const reader = response.body?.getReader();
if (!reader) throw new Error("No response body");
const decoder = new TextDecoder();
let fullText = "";
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.startsWith("data:")) continue;
try {
const json = JSON.parse(line.slice(5).trim());
const text = json.response?.candidates?.[0]?.content?.parts?.[0]?.text;
if (text) fullText += text;
} catch {}
}
}
return fullText.trim();
}
export async function generateImage(prompt: string, aspectRatio: string = "1:1", stylePrompt: string = ""): Promise<{ data: string, mimeType: string }> {
const auth = loadAuth();
if (!auth?.access || !auth?.projectId) throw new Error("No Antigravity credentials found.");
const model = "gemini-3-pro-image";
const url = `${ANTIGRAVITY_ENDPOINT}/v1internal:streamGenerateContent?alt=sse`;
const request: any = {
contents: [{ role: "user", parts: [{ text: prompt }] }],
generationConfig: {
imageConfig: { aspectRatio },
candidateCount: 1,
},
};
// Add system instruction if style prompt is present
if (stylePrompt.trim()) {
request.systemInstruction = {
parts: [{ text: stylePrompt.trim() }]
};
}
const body = {
project: auth.projectId,
model,
request,
requestType: "agent",
userAgent: "antigravity",
};
const response = await fetch(url, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.access}`,
"Content-Type": "application/json",
"Accept": "text/event-stream",
...HEADERS,
},
body: JSON.stringify(body),
});
if (!response.ok) throw new Error(`Image generation failed: ${response.status}`);
const reader = response.body?.getReader();
if (!reader) throw new Error("No response body");
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.startsWith("data:")) continue;
try {
const json = JSON.parse(line.slice(5).trim());
const part = json.response?.candidates?.[0]?.content?.parts?.[0];
if (part?.inlineData?.data) {
return {
data: part.inlineData.data,
mimeType: part.inlineData.mimeType || "image/png"
};
}
} catch {}
}
}
throw new Error("No image data received from API");
}