@alonetech/gpt-image-mcp 0.2.0 → 0.3.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/package.json +1 -1
- package/src/image-api.js +42 -11
package/package.json
CHANGED
package/src/image-api.js
CHANGED
|
@@ -97,19 +97,50 @@ function commonOptions(input, outputFormat) {
|
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
function resultFromResponseBody(body, outputFormat, model, mode) {
|
|
101
|
-
const
|
|
100
|
+
async function resultFromResponseBody(body, outputFormat, model, mode, fetchImpl) {
|
|
101
|
+
const firstResult = body?.data?.[0];
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
// Try b64_json first
|
|
104
|
+
const b64Data = firstResult?.b64_json;
|
|
105
|
+
if (b64Data) {
|
|
106
|
+
return {
|
|
107
|
+
data: b64Data,
|
|
108
|
+
mimeType: mimeTypeForOutputFormat(outputFormat),
|
|
109
|
+
model,
|
|
110
|
+
mode,
|
|
111
|
+
};
|
|
105
112
|
}
|
|
106
113
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
114
|
+
// Try url - fetch image from URL and convert to base64
|
|
115
|
+
const url = firstResult?.url;
|
|
116
|
+
if (url) {
|
|
117
|
+
// Use globalThis.fetch for fetching image URLs (not fetchImpl which may have API-specific headers)
|
|
118
|
+
const response = await globalThis.fetch(url);
|
|
119
|
+
if (!response.ok) {
|
|
120
|
+
throw new Error(`Failed to fetch image from URL: ${response.status} ${response.statusText}`);
|
|
121
|
+
}
|
|
122
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
123
|
+
const base64 = Buffer.from(arrayBuffer).toString('base64');
|
|
124
|
+
// Get correct mimeType from response Content-Type
|
|
125
|
+
const contentType = response.headers.get('content-type') || '';
|
|
126
|
+
const actualMimeType = contentType.startsWith('image/') ? contentType : mimeTypeForOutputFormat(outputFormat);
|
|
127
|
+
return {
|
|
128
|
+
data: base64,
|
|
129
|
+
mimeType: actualMimeType,
|
|
130
|
+
model,
|
|
131
|
+
mode,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Debug: include response info in error message
|
|
136
|
+
const responseSummary = {
|
|
137
|
+
bodyKeys: Object.keys(body || {}),
|
|
138
|
+
dataType: typeof body?.data,
|
|
139
|
+
dataLength: body?.data?.length,
|
|
140
|
+
firstResultKeys: firstResult ? Object.keys(firstResult) : null,
|
|
141
|
+
bodyPreview: JSON.stringify(body).substring(0, 500)
|
|
112
142
|
};
|
|
143
|
+
throw new Error(`Image API response did not include base64 image data or URL. Response summary: ${JSON.stringify(responseSummary)}`);
|
|
113
144
|
}
|
|
114
145
|
|
|
115
146
|
export async function generateImage(input, config = {}) {
|
|
@@ -148,7 +179,7 @@ export async function generateImage(input, config = {}) {
|
|
|
148
179
|
throw new Error(`Image API request failed (${response.status} ${response.statusText}): ${message}`);
|
|
149
180
|
}
|
|
150
181
|
|
|
151
|
-
return resultFromResponseBody(await response.json(), outputFormat, options.model, "text");
|
|
182
|
+
return resultFromResponseBody(await response.json(), outputFormat, options.model, "text", fetchImpl);
|
|
152
183
|
}
|
|
153
184
|
|
|
154
185
|
const form = new FormData();
|
|
@@ -180,7 +211,7 @@ export async function generateImage(input, config = {}) {
|
|
|
180
211
|
throw new Error(`Image API request failed (${response.status} ${response.statusText}): ${message}`);
|
|
181
212
|
}
|
|
182
213
|
|
|
183
|
-
return resultFromResponseBody(await response.json(), outputFormat, options.model, "edit");
|
|
214
|
+
return resultFromResponseBody(await response.json(), outputFormat, options.model, "edit", fetchImpl);
|
|
184
215
|
}
|
|
185
216
|
|
|
186
217
|
export const generateEditedImage = generateImage;
|