opencode-gpt-imagegen 1.2.2 → 1.2.4

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.
Files changed (3) hide show
  1. package/README.md +13 -8
  2. package/dist/index.js +19 -21
  3. package/package.json +15 -10
package/README.md CHANGED
@@ -25,13 +25,10 @@
25
25
 
26
26
  The same package supports OpenCode V1 (1.18.29 or newer) and V2 (2.0.0 or newer).
27
27
 
28
- For V2, add this plugin to your [OpenCode config](https://opencode.ai/v2/docs/plugins/):
28
+ For V2, install this plugin with the [OpenCode CLI](https://opencode.ai/v2/docs/plugins/#manage):
29
29
 
30
- ```json
31
- {
32
- "$schema": "https://opencode.ai/config.json",
33
- "plugins": ["opencode-gpt-imagegen"]
34
- }
30
+ ```sh
31
+ opencode plugin add opencode-gpt-imagegen
35
32
  ```
36
33
 
37
34
  For V1, add this plugin to your [OpenCode config](https://opencode.ai/docs/plugins/) with the singular `plugin` field:
@@ -49,7 +46,7 @@ OpenCode installs the package automatically. Unless you use a proxy, sign in to
49
46
 
50
47
  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.
51
48
 
52
- On V2, pass options with the package object:
49
+ On V2, pass options with the package object in your [OpenCode config](https://opencode.ai/v2/docs/plugins/#configure):
53
50
 
54
51
  ```json
55
52
  {
@@ -66,6 +63,7 @@ On V1, use the package-and-options tuple:
66
63
  ```
67
64
 
68
65
  With `baseURL` set, the plugin skips authentication lookup and sends the proxy no credentials on both versions. 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:
66
+
69
67
  - accepts those two `POST` routes without an `Authorization` header;
70
68
  - 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;
71
69
  - 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.
@@ -74,9 +72,16 @@ The request takes as long as the backend needs to draw the image, often 30 secon
74
72
 
75
73
  ### Updating the plugin
76
74
 
77
- On V2, update an unpinned package with `opencode plugin update opencode-gpt-imagegen`. If you pinned an exact version, update that version in your configuration. See [OpenCode's plugin management guide](https://opencode.ai/v2/docs/plugins/#manage).
75
+ On V2, update this plugin with the OpenCode CLI:
76
+
77
+ ```sh
78
+ opencode plugin update opencode-gpt-imagegen
79
+ ```
80
+
81
+ See [OpenCode's plugin management guide](https://opencode.ai/v2/docs/plugins/#manage).
78
82
 
79
83
  On V1, OpenCode caches npm plugins, so restarting it may keep an older version. To update this plugin:
84
+
80
85
  1. Check the `plugin` entry in your OpenCode config. Use `"opencode-gpt-imagegen"` or `"opencode-gpt-imagegen@latest"` to request the latest version. If you pinned a version, remove the version suffix or change it to the version you want to install.
81
86
  2. Quit OpenCode completely.
82
87
  3. If your entry is unversioned or uses `@latest`, delete the cached directory `~/.cache/opencode/packages/opencode-gpt-imagegen@latest/`.
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 chatGptImagesTarget(auth) {
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 callViaCodexImages(target, args, inputImageDataUrls, signal) {
43
- const isEdit = inputImageDataUrls.length > 0;
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(args.prompt, args.size),
50
+ prompt: appendSizeToPrompt(request.prompt, request.size),
46
51
  ...CODEX_REQUEST_DEFAULTS,
47
- background: args.background ?? "opaque",
48
- ...isEdit ? { images: inputImageDataUrls.map((dataUrl) => ({ image_url: dataUrl })) } : {}
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
- async function resolveImagesTarget(options, loadOpenAIAuth) {
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
- if (parsed.data.baseURL) {
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 target = await resolveImagesTarget(options, loadOpenAIAuth);
211
- const inputImageDataUrls = await readReferenceImages(args.images, directory);
212
- const base64 = await callViaCodexImages(target, args, inputImageDataUrls, signal);
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.2",
3
+ "version": "1.2.4",
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
- "check": "biome check --write .",
20
- "prebuild": "rm -rf dist",
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.5.3",
57
- "@opencode-ai/plugin": "^1.14.41",
58
- "@opencode/plugin": "^2.1.4",
59
- "@tsconfig/node22": "^22.0.2",
60
- "@types/bun": "^1.3.14",
61
+ "@biomejs/biome": "^2.6.8",
62
+ "@opencode-ai/plugin": "^1.19.6",
63
+ "@opencode/plugin": "^2.3.2",
64
+ "@tsconfig/node22": "^22.0.7",
65
+ "@types/bun": "^1.4.5",
61
66
  "typescript": "^7.0.2"
62
67
  },
63
68
  "dependencies": {