117
118 if (Math.abs(originalAspectRatio - adjustedAspectRatio) > ASPECT_RATIO_TOLERANCE) {
119 warnings.push(`Original aspect ratio (${originalAspectRatio.toFixed(2)}) adjusted to match API constraints.`);
120 }
121
122 // Add specific warnings
123 if (safeWidth !== adjustedWidth) {
124 warnings.push(`Width adjusted from ${safeWidth} to ${adjustedWidth} to meet API requirements.`);
125 }
126 if (safeHeight !== adjustedHeight) {
127 warnings.push(`Height adjusted from ${safeHeight} to ${adjustedHeight} to meet API requirements.`);
128 }
129
151
152 if (url.pathname === "/generate" && request.method === "POST") {
153 const apiKey = Deno.env.get("TOGETHER_API_KEY");
154 if (!apiKey) {
155 console.error("API key not found in environment variables");
156 return new Response(JSON.stringify({ error: "API key not found" }), {
157 status: 500,
158 headers: { "Content-Type": "application/json" },
164 console.log("Received prompt:", prompt);
165
166 const togetherApiUrl = "https://api.together.xyz/v1/images/generations";
167 console.log("Sending request to Together AI API:", togetherApiUrl);
168
169 const response = await fetch(togetherApiUrl, {
170 method: "POST",
171 headers: {
172 "Content-Type": "application/json",
173 "Authorization": `Bearer ${apiKey}`,
174 },
175 body: JSON.stringify({
183 });
184
185 console.log("Together AI API response status:", response.status);
186
187 if (!response.ok) {
188 const errorText = await response.text();
189 console.error("Together AI API error:", errorText);
190 throw new Error(`API request failed with status ${response.status}: ${errorText}`);
191 }
192
193 const result = await response.json();
194 console.log("Together AI API full response:", JSON.stringify(result, null, 2));
195
196 // Robust extraction of base64 image data
204 } else {
205 console.error("Unexpected response structure:", result);
206 throw new Error("No image found in API response");
207 }
208
231 // Handle direct image generation from URL query
232 if (imagePrompt) {
233 const apiKey = Deno.env.get("TOGETHER_API_KEY");
234 if (!apiKey) {
235 return new Response("API key not found", { status: 500 });
236 }
237
238 try {
239 const togetherApiUrl = "https://api.together.xyz/v1/images/generations";
240 const response = await fetch(togetherApiUrl, {
241 method: "POST",
242 headers: {
243 "Content-Type": "application/json",
244 "Authorization": `Bearer ${apiKey}`,
245 },
246 body: JSON.stringify({
256 if (!response.ok) {
257 const errorText = await response.text();
258 throw new Error(`API request failed with status ${response.status}: ${errorText}`);
259 }
260
268 imageBase64 = result.data.b64_json;
269 } else {
270 throw new Error("No image found in API response");
271 }
272