opencode-gpt-imagegen 1.2.3 → 1.2.5
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 +13 -3
- package/dist/index.js +19 -21
- package/package.json +15 -10
package/README.md
CHANGED
|
@@ -129,16 +129,26 @@ Ask for a transparent background, and the agent passes `background: "transparent
|
|
|
129
129
|
|
|
130
130
|
### Sizes
|
|
131
131
|
|
|
132
|
-
`gpt_imagegen`
|
|
132
|
+
`gpt_imagegen` accepts these seven sizes:
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
- `1254x1254` (1:1)
|
|
135
|
+
- `1536x1024` (3:2)
|
|
136
|
+
- `1024x1536` (2:3)
|
|
137
|
+
- `1448x1086` (4:3)
|
|
138
|
+
- `1086x1448` (3:4)
|
|
139
|
+
- `1672x941` (16:9)
|
|
140
|
+
- `941x1672` (9:16)
|
|
135
141
|
|
|
136
|
-
|
|
142
|
+
The tool adds the requested dimensions and aspect ratio to the prompt. In our tests with newly generated images ([#107](https://github.com/yuji-hatakeyama/opencode-gpt-imagegen/issues/107#issuecomment-6020015062)), the output closely matched the requested aspect ratio and was about 1.57 megapixels. For example, asking for `2048x1152` in the prompt produced a `1672x941` image at roughly 16:9.
|
|
143
|
+
|
|
144
|
+
The tool reports the dimensions of the saved image. If you need exact dimensions, generate at the closest aspect ratio, then resize or crop the image with a tool such as ImageMagick.
|
|
137
145
|
|
|
138
146
|
## How it works
|
|
139
147
|
|
|
140
148
|
This plugin calls the image generation endpoints of the OpenAI Codex backend (`images/generations`, or `images/edits` when reference images are given), the same endpoints the Codex CLI uses, and writes the returned PNG to disk. V1 reads `OPENCODE_AUTH_CONTENT` first, then OpenCode's `auth.json`. V2 resolves the active OpenAI connection through OpenCode's integration API.
|
|
141
149
|
|
|
150
|
+
With Codex OAuth, this plugin has the same limits as the Codex CLI's built-in image generation. The backend chooses the model and quality, so the request's `model` and `quality` fields cannot override them. The request's `size` field does not let you set exact pixel dimensions either.
|
|
151
|
+
|
|
142
152
|
## Contributing
|
|
143
153
|
|
|
144
154
|
Small bug fixes are welcome as direct pull requests. For features, refactors, or behavior changes, please open an issue first. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
package/dist/index.js
CHANGED
|
@@ -24,7 +24,14 @@ function appendSizeToPrompt(prompt, size) {
|
|
|
24
24
|
// src/codex.ts
|
|
25
25
|
var CODEX_IMAGES_BASE_URL = "https://chatgpt.com/backend-api/codex";
|
|
26
26
|
var CODEX_REQUEST_DEFAULTS = { model: "gpt-image-2", quality: "auto", size: "auto" };
|
|
27
|
-
function
|
|
27
|
+
async function resolveImagesTarget(connection, loadOpenAIAuth) {
|
|
28
|
+
if (connection.baseURL) {
|
|
29
|
+
return { baseURL: connection.baseURL, headers: {} };
|
|
30
|
+
}
|
|
31
|
+
const auth = await loadOpenAIAuth();
|
|
32
|
+
if (!auth) {
|
|
33
|
+
throw new Error("OpenAI ChatGPT OAuth credentials not configured.");
|
|
34
|
+
}
|
|
28
35
|
return {
|
|
29
36
|
baseURL: CODEX_IMAGES_BASE_URL,
|
|
30
37
|
headers: {
|
|
@@ -33,19 +40,17 @@ function chatGptImagesTarget(auth) {
|
|
|
33
40
|
}
|
|
34
41
|
};
|
|
35
42
|
}
|
|
36
|
-
function proxyImagesTarget(baseURL) {
|
|
37
|
-
return { baseURL, headers: {} };
|
|
38
|
-
}
|
|
39
43
|
function imagesEndpoint(baseURL, operation) {
|
|
40
44
|
return new URL(`images/${operation}`, baseURL.endsWith("/") ? baseURL : `${baseURL}/`).toString();
|
|
41
45
|
}
|
|
42
|
-
async function
|
|
43
|
-
const
|
|
46
|
+
async function requestCodexImage(connection, loadOpenAIAuth, request, signal) {
|
|
47
|
+
const target = await resolveImagesTarget(connection, loadOpenAIAuth);
|
|
48
|
+
const isEdit = request.images.length > 0;
|
|
44
49
|
const body = {
|
|
45
|
-
prompt: appendSizeToPrompt(
|
|
50
|
+
prompt: appendSizeToPrompt(request.prompt, request.size),
|
|
46
51
|
...CODEX_REQUEST_DEFAULTS,
|
|
47
|
-
background:
|
|
48
|
-
...isEdit ? { images:
|
|
52
|
+
background: request.background,
|
|
53
|
+
...isEdit ? { images: request.images.map((dataUrl) => ({ image_url: dataUrl })) } : {}
|
|
49
54
|
};
|
|
50
55
|
const res = await fetch(imagesEndpoint(target.baseURL, isEdit ? "edits" : "generations"), {
|
|
51
56
|
method: "POST",
|
|
@@ -169,19 +174,12 @@ var optionsSchema = z.object({
|
|
|
169
174
|
return url.search === "" && url.hash === "";
|
|
170
175
|
}, "baseURL must not have a query or a fragment").optional()
|
|
171
176
|
});
|
|
172
|
-
|
|
177
|
+
function parseOptions(options) {
|
|
173
178
|
const parsed = optionsSchema.safeParse(options ?? {});
|
|
174
179
|
if (!parsed.success) {
|
|
175
180
|
throw new Error(`invalid opencode-gpt-imagegen options: ${z.prettifyError(parsed.error)}`);
|
|
176
181
|
}
|
|
177
|
-
|
|
178
|
-
return proxyImagesTarget(parsed.data.baseURL);
|
|
179
|
-
}
|
|
180
|
-
const auth = await loadOpenAIAuth();
|
|
181
|
-
if (!auth) {
|
|
182
|
-
throw new Error("OpenAI ChatGPT OAuth credentials not configured.");
|
|
183
|
-
}
|
|
184
|
-
return chatGptImagesTarget(auth);
|
|
182
|
+
return parsed.data;
|
|
185
183
|
}
|
|
186
184
|
function createImagegenTool(options, loadOpenAIAuth) {
|
|
187
185
|
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.";
|
|
@@ -207,9 +205,9 @@ function createImagegenTool(options, loadOpenAIAuth) {
|
|
|
207
205
|
throw new Error(z.prettifyError(parsed.error));
|
|
208
206
|
}
|
|
209
207
|
const args = parsed.data;
|
|
210
|
-
const
|
|
211
|
-
const
|
|
212
|
-
const base64 = await
|
|
208
|
+
const connection = parseOptions(options);
|
|
209
|
+
const images = await readReferenceImages(args.images, directory);
|
|
210
|
+
const base64 = await requestCodexImage(connection, loadOpenAIAuth, { prompt: args.prompt, size: args.size, background: args.background ?? "opaque", images }, signal);
|
|
213
211
|
return saveGeneratedImage(args.out, directory, base64);
|
|
214
212
|
}
|
|
215
213
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-gpt-imagegen",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
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",
|
|
@@ -16,12 +16,11 @@
|
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
18
|
"typecheck": "tsc --noEmit",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"build": "bun build ./src/index.ts --outdir ./dist --target node --format esm --packages external",
|
|
19
|
+
"lint": "biome ci .",
|
|
20
|
+
"fix": "biome check --write .",
|
|
21
|
+
"build": "rm -rf dist && bun build ./src/index.ts --outdir ./dist --target node --format esm --packages external",
|
|
22
22
|
"test": "bun test tests/unit",
|
|
23
23
|
"test:e2e": "OPENCODE_MODEL=openai/gpt-6-sol bun test tests/e2e/gpt_imagegen.test.ts",
|
|
24
|
-
"prepublishOnly": "bun run build",
|
|
25
24
|
"release:patch": "bash scripts/prepare-release.sh patch && git push --follow-tags",
|
|
26
25
|
"release:minor": "bash scripts/prepare-release.sh minor && git push --follow-tags",
|
|
27
26
|
"release:major": "bash scripts/prepare-release.sh major && git push --follow-tags"
|
|
@@ -52,12 +51,18 @@
|
|
|
52
51
|
"bugs": {
|
|
53
52
|
"url": "https://github.com/yuji-hatakeyama/opencode-gpt-imagegen/issues"
|
|
54
53
|
},
|
|
54
|
+
"devEngines": {
|
|
55
|
+
"packageManager": {
|
|
56
|
+
"name": "npm",
|
|
57
|
+
"version": ">=11.10.0"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
55
60
|
"devDependencies": {
|
|
56
|
-
"@biomejs/biome": "^2.
|
|
57
|
-
"@opencode-ai/plugin": "^1.
|
|
58
|
-
"@opencode/plugin": "^2.1
|
|
59
|
-
"@tsconfig/node22": "^22.0.
|
|
60
|
-
"@types/bun": "^1.
|
|
61
|
+
"@biomejs/biome": "^2.6.8",
|
|
62
|
+
"@opencode-ai/plugin": "^1.19.6",
|
|
63
|
+
"@opencode/plugin": "^2.4.1",
|
|
64
|
+
"@tsconfig/node22": "^22.0.7",
|
|
65
|
+
"@types/bun": "^1.4.5",
|
|
61
66
|
"typescript": "^7.0.2"
|
|
62
67
|
},
|
|
63
68
|
"dependencies": {
|