opencode-gpt-imagegen 1.0.2 → 1.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.
- package/README.md +19 -1
- package/dist/index.js +44 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,25 @@ Add this plugin to your [OpenCode config](https://opencode.ai/docs/plugins/). Fo
|
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
OpenCode auto-installs the package via Bun on next launch — no separate `npm install` step is needed. The plugin requires OpenCode to be authenticated with ChatGPT.
|
|
35
|
+
OpenCode auto-installs the package via Bun on next launch — no separate `npm install` step is needed. The plugin requires OpenCode to be authenticated with ChatGPT, unless you send requests through a proxy as described in [Sending requests through a proxy](#sending-requests-through-a-proxy).
|
|
36
|
+
|
|
37
|
+
### Sending requests through a proxy
|
|
38
|
+
|
|
39
|
+
Set `baseURL` if you reach ChatGPT through a gateway rather than signing OpenCode in to ChatGPT yourself, for example a local proxy that holds one or more ChatGPT sign-ins and spreads requests across them. In that setup OpenCode's `auth.json` usually has no ChatGPT sign-in, and calling chatgpt.com directly would bypass the gateway.
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"plugin": [["opencode-gpt-imagegen", { "baseURL": "http://127.0.0.1:3457/v1" }]]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
With `baseURL` set, the plugin does not read `auth.json` and sends the proxy no credentials. It sends the same requests it would send to ChatGPT, to `<baseURL>/images/generations`, or to `<baseURL>/images/edits` when reference images are given. Your proxy works with this option if it:
|
|
48
|
+
|
|
49
|
+
- accepts those two `POST` routes without an `Authorization` header;
|
|
50
|
+
- forwards the JSON body unchanged to the Codex images endpoints (`https://chatgpt.com/backend-api/codex/images/generations` and `/edits`), signed in with a ChatGPT account. The body has `prompt`, `model`, `quality`, `size` and `background`, plus `images` (a list of `{ "image_url": "data:..." }`) for edits;
|
|
51
|
+
- answers with the backend's JSON, in which `data[0].b64_json` holds the PNG. Any non-2xx status is reported as an error together with the start of the response body.
|
|
52
|
+
|
|
53
|
+
The request takes as long as the backend needs to draw the image, often 30 seconds or more, so the proxy must not time it out early.
|
|
36
54
|
|
|
37
55
|
### Updating the plugin
|
|
38
56
|
|
package/dist/index.js
CHANGED
|
@@ -45,10 +45,24 @@ function appendSizeToPrompt(prompt, size) {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
// src/codex.ts
|
|
48
|
-
var
|
|
49
|
-
var CODEX_IMAGES_EDITS_ENDPOINT = "https://chatgpt.com/backend-api/codex/images/edits";
|
|
48
|
+
var CODEX_IMAGES_BASE_URL = "https://chatgpt.com/backend-api/codex";
|
|
50
49
|
var CODEX_REQUEST_DEFAULTS = { model: "gpt-image-2", quality: "auto", size: "auto" };
|
|
51
|
-
|
|
50
|
+
function chatGptImagesTarget(auth) {
|
|
51
|
+
return {
|
|
52
|
+
baseURL: CODEX_IMAGES_BASE_URL,
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${auth.access}`,
|
|
55
|
+
...auth.accountId ? { "ChatGPT-Account-Id": auth.accountId } : {}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function proxyImagesTarget(baseURL) {
|
|
60
|
+
return { baseURL, headers: {} };
|
|
61
|
+
}
|
|
62
|
+
function imagesEndpoint(baseURL, operation) {
|
|
63
|
+
return new URL(`images/${operation}`, baseURL.endsWith("/") ? baseURL : `${baseURL}/`).toString();
|
|
64
|
+
}
|
|
65
|
+
async function callViaCodexImages(target, args, inputImageDataUrls) {
|
|
52
66
|
const isEdit = inputImageDataUrls.length > 0;
|
|
53
67
|
const body = {
|
|
54
68
|
prompt: appendSizeToPrompt(args.prompt, args.size),
|
|
@@ -56,12 +70,11 @@ async function callViaCodexImages(auth, args, inputImageDataUrls) {
|
|
|
56
70
|
background: args.background ?? "opaque",
|
|
57
71
|
...isEdit ? { images: inputImageDataUrls.map((dataUrl) => ({ image_url: dataUrl })) } : {}
|
|
58
72
|
};
|
|
59
|
-
const res = await fetch(isEdit ?
|
|
73
|
+
const res = await fetch(imagesEndpoint(target.baseURL, isEdit ? "edits" : "generations"), {
|
|
60
74
|
method: "POST",
|
|
61
75
|
headers: {
|
|
62
76
|
"Content-Type": "application/json",
|
|
63
|
-
|
|
64
|
-
...auth.accountId ? { "ChatGPT-Account-Id": auth.accountId } : {},
|
|
77
|
+
...target.headers,
|
|
65
78
|
originator: "opencode"
|
|
66
79
|
},
|
|
67
80
|
body: JSON.stringify(body)
|
|
@@ -172,7 +185,28 @@ var argsShape = {
|
|
|
172
185
|
].join(" "))
|
|
173
186
|
};
|
|
174
187
|
var argsSchema = tool.schema.object(argsShape);
|
|
175
|
-
var
|
|
188
|
+
var optionsSchema = tool.schema.object({
|
|
189
|
+
baseURL: tool.schema.url({ protocol: /^https?$/ }).refine((value) => {
|
|
190
|
+
const url = new URL(value);
|
|
191
|
+
return url.search === "" && url.hash === "";
|
|
192
|
+
}, "baseURL must not have a query or a fragment").optional()
|
|
193
|
+
});
|
|
194
|
+
async function resolveImagesTarget(options) {
|
|
195
|
+
const parsed = optionsSchema.safeParse(options ?? {});
|
|
196
|
+
if (!parsed.success) {
|
|
197
|
+
throw new Error(`invalid opencode-gpt-imagegen options: ${tool.schema.prettifyError(parsed.error)}`);
|
|
198
|
+
}
|
|
199
|
+
if (parsed.data.baseURL) {
|
|
200
|
+
return proxyImagesTarget(parsed.data.baseURL);
|
|
201
|
+
}
|
|
202
|
+
const auth = await loadOpenAIAuth();
|
|
203
|
+
if (!auth) {
|
|
204
|
+
throw new Error("OpenAI ChatGPT OAuth credentials not configured.");
|
|
205
|
+
}
|
|
206
|
+
return chatGptImagesTarget(auth);
|
|
207
|
+
}
|
|
208
|
+
var GptImagePlugin = async (_input, options) => {
|
|
209
|
+
const authNote = options?.baseURL ? "Sends requests to the proxy set by the plugin's `baseURL` option instead of ChatGPT, so ChatGPT OAuth is not used." : "Requires OpenCode to be authenticated with ChatGPT OAuth.";
|
|
176
210
|
return {
|
|
177
211
|
tool: {
|
|
178
212
|
gpt_imagegen: tool({
|
|
@@ -182,7 +216,7 @@ var GptImagePlugin = async (_input) => {
|
|
|
182
216
|
"Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas.",
|
|
183
217
|
"Reference images may be attached through `images`; label each image's role inline in `prompt`, for example: 'Image 1: reference image'.",
|
|
184
218
|
"For many distinct assets, invoke gpt_imagegen once per requested asset rather than relying on multi-image output; gpt_imagegen returns one image per call.",
|
|
185
|
-
|
|
219
|
+
`${authNote} Returns the absolute path of the saved PNG.`,
|
|
186
220
|
"Before generating, decide the size. If the user named a size that is not supported, or gave no size and the use does not clearly imply a shape, ask the user which size to use (with the question tool when available).",
|
|
187
221
|
"In that question, offer every supported size as an option labeled like `1672x941 (16:9)`, put the one you recommend first with ` (Recommended)` appended to its label, and give each option a short description of what it suits.",
|
|
188
222
|
"Do not add catch-all options such as 'Other' or 'Let me decide'.",
|
|
@@ -197,12 +231,9 @@ var GptImagePlugin = async (_input) => {
|
|
|
197
231
|
throw new Error(tool.schema.prettifyError(parsed.error));
|
|
198
232
|
}
|
|
199
233
|
const args = parsed.data;
|
|
200
|
-
const
|
|
201
|
-
if (!auth) {
|
|
202
|
-
throw new Error("OpenAI ChatGPT OAuth credentials not configured.");
|
|
203
|
-
}
|
|
234
|
+
const target = await resolveImagesTarget(options);
|
|
204
235
|
const inputImageDataUrls = await readReferenceImages(args.images, ctx.directory);
|
|
205
|
-
const base64 = await callViaCodexImages(
|
|
236
|
+
const base64 = await callViaCodexImages(target, args, inputImageDataUrls);
|
|
206
237
|
const { savedPath, versioned, message } = await saveGeneratedImage(args.out, ctx.directory, base64);
|
|
207
238
|
return {
|
|
208
239
|
output: message,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-gpt-imagegen",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "OpenCode plugin for GPT Image 2 generation through your ChatGPT subscription, with reference images and safe PNG output.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|