codex-copilot-dx 0.7.9 → 0.7.10
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 +7 -3
- package/package.json +1 -1
- package/src/adapter.mjs +1 -1
- package/src/cli-animation.mjs +43 -8
- package/src/cli-options.mjs +1 -1
- package/src/image-mcp.mjs +51 -7
- package/src/image-provider.mjs +51 -4
- package/src/image-references.mjs +41 -0
- package/src/image-skill.mjs +11 -7
- package/src/image-tool-client.mjs +16 -7
- package/src/terminal-animation-gallery.mjs +85 -0
package/README.md
CHANGED
|
@@ -131,7 +131,7 @@ The selector is ordered numerically:
|
|
|
131
131
|
8. Relay — fixed nodes receive and pass a short light trail
|
|
132
132
|
9. Split — one incoming light trail divides into two outgoing trails
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
All nine rows preview their animations together while you enter a number to select one. The live previews keep each theme's original timing and leave the input cursor in place; selection ends them immediately. Small terminals, disabled animation, CI and dumb terminals retain static previews. Resizing or clearing the terminal pauses the live gallery so input stays usable. When the gallery cannot run, CCDX retains the selected animation's one-cycle confirmation when supported. The setting is saved in `~/.config/codex-copilot-dx/config.json`, or under `XDG_CONFIG_HOME` when set.
|
|
135
135
|
|
|
136
136
|
All nine animations use the same 20-column colon track and cyan/blue palette. They indicate activity, not completion percentage. Braille was removed in 0.7.7. A saved `terminal_animation: "braille"` falls back to Comet without rewriting the settings file; choosing another animation replaces it, and explicitly choosing Comet clears it. Other settings remain intact.
|
|
137
137
|
|
|
@@ -147,7 +147,7 @@ ccdx enable-image
|
|
|
147
147
|
|
|
148
148
|
The command asks for an HTTPS `/images/generations` endpoint and an API key without echoing the key. It reads the endpoint's `/models` catalog, detects either the standard OpenAI Images request format or the supported `input.messages` format, and asks for a numeric model selection only when multiple image models are available. Its validation request omits the prompt and does not generate an image.
|
|
149
149
|
|
|
150
|
-
The provider credential is stored in `~/.config/codex-copilot-dx/image-provider.json`, or under `XDG_CONFIG_HOME`, with mode `0600`. The API key is never written to `~/.codex/config.toml`; that file receives only a marked local MCP entry pointing to the running CCDX adapter with a 210-second tool timeout. Enabling also installs CCDX-owned `ccdx-image` guidance under `~/.codex/skills`, with a self-contained Node helper. Codex uses the MCP tool directly, including through deferred tool search; an already-open task without that tool can use the helper to call the same local service and display the saved image. Neither route requires Python, an OpenAI SDK, or copying credentials. The original built-in imagegen skill remains available for capabilities such as editing and reference
|
|
150
|
+
The provider credential is stored in `~/.config/codex-copilot-dx/image-provider.json`, or under `XDG_CONFIG_HOME`, with mode `0600`. The API key is never written to `~/.codex/config.toml`; that file receives only a marked local MCP entry pointing to the running CCDX adapter with a 210-second tool timeout. Enabling also installs CCDX-owned `ccdx-image` guidance under `~/.codex/skills`, with a self-contained Node helper. Codex uses the MCP tool directly, including through deferred tool search; an already-open task without that tool can use the helper to call the same local service and display the saved image. Neither route requires Python, an OpenAI SDK, or copying credentials. The original built-in imagegen skill remains available for capabilities such as editing uploaded images and other reference-image workflows.
|
|
151
151
|
|
|
152
152
|
The enable command verifies the local MCP handshake and generation-tool directory without generating an image. It reports provider configuration separately from local service readiness; it cannot assert that every running Codex task has refreshed its catalog. Codex normally detects changed skills; restart Codex App once if an open task has not refreshed them. Normal CCDX startup installs or updates this guidance for providers already enabled in 0.7.8, without repeating API setup. Unchanged guidance is not rewritten. The existing adapter can remain running because provider settings are loaded when the tool is invoked.
|
|
153
153
|
|
|
@@ -160,7 +160,11 @@ ccdx disable-image
|
|
|
160
160
|
|
|
161
161
|
`image-status` never prints the API key and performs only a local, non-generating tool-readiness check. Disabling removes the stored provider credential, the CCDX-owned MCP block, and unmodified CCDX image guidance; user-modified skill files are preserved. Cached tool calls then fail closed. The Node helper creates a new file under the task's `output/imagegen` directory by default and reports its absolute path; it never overwrites an existing file or automatically repeats a failed generation. The image endpoint is contacted only by `enable-image` validation and by explicit `generate_image` tool calls. Ordinary Responses, models, authentication, Fast, Auto-review, compaction, and image-input paths remain on their existing routes.
|
|
162
162
|
|
|
163
|
-
Generated image downloads must use public HTTPS addresses, do not follow redirects, are DNS-checked against private and reserved addresses, and are capped at 32 MiB. The local MCP route accepts calls only from the same device and permits at most two concurrent
|
|
163
|
+
Generated image downloads must use public HTTPS addresses, do not follow redirects, are DNS-checked against private and reserved addresses, and are capped at 32 MiB. The local MCP route accepts calls only from the same device and permits at most two concurrent image operations. The tool supports new-image generation at `1024x1024`, `1536x1024`, or `1024x1536`.
|
|
164
|
+
|
|
165
|
+
With `qwen-image-3.0-pro` or `qwen-image-3.0` using either supported protocol, CCDX also exposes `edit_image`. Ask Codex to modify a specific CCDX-generated image; it passes that image's returned `image_id` and the edit instruction to the same configured provider. Each edit returns a new image and ID while preserving the original; omitting `size` retains the source's requested dimensions. The Qwen request contains the source image again on every edit, as required by its [single-turn image editing API](https://help.aliyun.com/zh/model-studio/qwen-image-generation-and-editing-api-reference).
|
|
166
|
+
|
|
167
|
+
Editing references are opaque IDs bound to the provider configuration, not paths or URLs. Source images up to 10 MiB are kept in a 64 MiB byte-bounded in-memory LRU. IDs expire on eviction, provider/credential changes, or adapter restart; restarting Codex App alone does not clear them. A missing or expired ID fails before upstream dispatch. This feature does not import arbitrary local images or provide masks, transparent output, or an `/images/edits` endpoint. Other providers retain their existing generation behavior. The helper accepts `--image-id` for the same edit operation; its stdout remains the saved absolute file path and stderr reports the new ID. Normal host approvals still apply, and failed edits are never replaced with an automatic new generation.
|
|
164
168
|
|
|
165
169
|
### Update
|
|
166
170
|
|
package/package.json
CHANGED
package/src/adapter.mjs
CHANGED
|
@@ -196,7 +196,7 @@ export function createAdapterHandler(options = {}) {
|
|
|
196
196
|
if (pathname === "/mcp/image") {
|
|
197
197
|
if (imageMcpHandler) return imageMcpHandler(req, res);
|
|
198
198
|
return import("./image-mcp.mjs").then(({ createImageMcpHandler }) => {
|
|
199
|
-
imageMcpHandler
|
|
199
|
+
imageMcpHandler ||= createImageMcpHandler();
|
|
200
200
|
return imageMcpHandler(req, res);
|
|
201
201
|
});
|
|
202
202
|
}
|
package/src/cli-animation.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
renderTerminalAnimationFrame,
|
|
7
7
|
TERMINAL_ANIMATION_THEMES,
|
|
8
8
|
} from "./terminal-animation.mjs";
|
|
9
|
+
import { startTerminalAnimationGallery } from "./terminal-animation-gallery.mjs";
|
|
9
10
|
import {
|
|
10
11
|
readUserSettings,
|
|
11
12
|
terminalAnimationPreference,
|
|
@@ -34,6 +35,7 @@ export async function runAnimationCommand({
|
|
|
34
35
|
output = process.stdout,
|
|
35
36
|
prompt,
|
|
36
37
|
preview = playTerminalAnimationPreview,
|
|
38
|
+
gallery = startTerminalAnimationGallery,
|
|
37
39
|
} = {}) {
|
|
38
40
|
if (!prompt && (!input.isTTY || !output.isTTY)) {
|
|
39
41
|
throw new Error(`${commandName} animation requires an interactive terminal`);
|
|
@@ -45,24 +47,49 @@ export async function runAnimationCommand({
|
|
|
45
47
|
|
|
46
48
|
output.write(`${commandName} animation\n`);
|
|
47
49
|
output.write(`Current: ${TERMINAL_ANIMATION_THEMES[currentIndex].label} (${current.source})\n\n`);
|
|
50
|
+
const labels = TERMINAL_ANIMATION_THEMES.map((theme, index) => ` ${index + 1}. ${theme.label}${markers(theme, current.theme)}`);
|
|
51
|
+
const prefixWidth = Math.max(...labels.map((label) => label.length)) + 2;
|
|
48
52
|
TERMINAL_ANIMATION_THEMES.forEach((theme, index) => {
|
|
49
|
-
|
|
50
|
-
? ` ${renderTerminalAnimationFrame(theme.id, 10)}`
|
|
51
|
-
: "";
|
|
52
|
-
output.write(` ${index + 1}. ${theme.label}${markers(theme, current.theme)}${staticPreview}\n`);
|
|
53
|
+
output.write(`${labels[index].padEnd(prefixWidth)}${renderTerminalAnimationFrame(theme.id, theme.startFrame)}\n`);
|
|
53
54
|
});
|
|
54
55
|
|
|
55
56
|
let readline;
|
|
57
|
+
const abort = new AbortController();
|
|
56
58
|
const ask = prompt || (async (question) => {
|
|
57
|
-
readline
|
|
58
|
-
|
|
59
|
+
if (!readline) {
|
|
60
|
+
readline = createInterface({ input, output });
|
|
61
|
+
readline.once("SIGINT", () => abort.abort());
|
|
62
|
+
readline.once("close", () => abort.abort());
|
|
63
|
+
}
|
|
64
|
+
return readline.question(question, { signal: abort.signal });
|
|
59
65
|
});
|
|
60
66
|
|
|
61
67
|
let selectedTheme = current.theme;
|
|
62
68
|
let keepCurrent = false;
|
|
69
|
+
let livePreviewed = false;
|
|
70
|
+
let rowsBelow = 0;
|
|
71
|
+
let activeGallery;
|
|
72
|
+
let layoutChanged = false;
|
|
73
|
+
const stopForLayoutChange = () => {
|
|
74
|
+
layoutChanged = true;
|
|
75
|
+
activeGallery?.stop();
|
|
76
|
+
};
|
|
77
|
+
const onKeypress = (_text, key) => {
|
|
78
|
+
if ((key?.ctrl && key.name === "l") || (readline?.getCursorPos().rows ?? 0) > 0) stopForLayoutChange();
|
|
79
|
+
};
|
|
80
|
+
output.on?.("resize", stopForLayoutChange);
|
|
81
|
+
input.on?.("keypress", onKeypress);
|
|
63
82
|
try {
|
|
64
83
|
while (true) {
|
|
65
|
-
|
|
84
|
+
activeGallery = layoutChanged ? { enabled: false, stop() {} }
|
|
85
|
+
: gallery({ output, env, prefixWidth, rowsBelow, getInputRows: () => readline?.getCursorPos().rows ?? 0 });
|
|
86
|
+
livePreviewed ||= activeGallery.enabled;
|
|
87
|
+
let answer;
|
|
88
|
+
try {
|
|
89
|
+
answer = String(await ask(`Select [${currentIndex + 1}], or q to cancel: `) || "").trim();
|
|
90
|
+
} finally {
|
|
91
|
+
activeGallery.stop();
|
|
92
|
+
}
|
|
66
93
|
if (answer.toLowerCase() === "q") {
|
|
67
94
|
output.write("No changes made.\n");
|
|
68
95
|
return { changed: false, cancelled: true, theme: current.theme };
|
|
@@ -79,8 +106,16 @@ export async function runAnimationCommand({
|
|
|
79
106
|
break;
|
|
80
107
|
}
|
|
81
108
|
output.write(`Enter a number from 1 to ${TERMINAL_ANIMATION_THEMES.length}, or q to cancel.\n`);
|
|
109
|
+
rowsBelow += 2;
|
|
82
110
|
}
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (!abort.signal.aborted) throw error;
|
|
113
|
+
output.write("\nNo changes made.\n");
|
|
114
|
+
return { changed: false, cancelled: true, theme: current.theme };
|
|
83
115
|
} finally {
|
|
116
|
+
activeGallery?.stop();
|
|
117
|
+
output.off?.("resize", stopForLayoutChange);
|
|
118
|
+
input.off?.("keypress", onKeypress);
|
|
84
119
|
readline?.close();
|
|
85
120
|
}
|
|
86
121
|
|
|
@@ -88,7 +123,7 @@ export async function runAnimationCommand({
|
|
|
88
123
|
? { changed: false, theme: current.theme }
|
|
89
124
|
: writeTerminalAnimationTheme(selectedTheme, { env, home });
|
|
90
125
|
const disabledValue = disabledEnvironmentValue(env);
|
|
91
|
-
if (!disabledValue) {
|
|
126
|
+
if (!disabledValue && !livePreviewed) {
|
|
92
127
|
try {
|
|
93
128
|
await preview(selectedTheme, { output });
|
|
94
129
|
} catch {
|
package/src/cli-options.mjs
CHANGED
|
@@ -301,7 +301,7 @@ function topicHelp(name, topic) {
|
|
|
301
301
|
status: `Usage:\n ${name} status\n\nShows bounded runtime, routing, performance, queue, and cache metrics from a running adapter.`,
|
|
302
302
|
models: `Usage:\n ${name} models [--format table|plain]\n\nPerforms a fresh, read-only Copilot model-directory lookup for the saved account. Interactive terminals use a table by default.`,
|
|
303
303
|
usage: `Usage:\n ${name} usage [--format table|plain]\n\nSummarizes local token usage metadata without reading prompt or completion content. Interactive terminals use a table by default.`,
|
|
304
|
-
animation: `Usage:\n ${name} animation\n\
|
|
304
|
+
animation: `Usage:\n ${name} animation\n\nPreviews all terminal activity animations together and selects the theme used the next time the adapter starts.`,
|
|
305
305
|
"enable-image": `Usage:\n ${name} enable-image\n\nInteractively configures an HTTPS image API endpoint and API key, detects its supported image model and protocol, installs CCDX image guidance, and checks the local image tool for Codex App without generating an image.`,
|
|
306
306
|
"disable-image": `Usage:\n ${name} disable-image\n\nDisables the local CCDX image tool and removes its stored API credential and unmodified CCDX image guidance.`,
|
|
307
307
|
"image-status": `Usage:\n ${name} image-status\n\nShows provider configuration and probes local image-tool readiness without generating an image or exposing its API key.`,
|
package/src/image-mcp.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { isLoopbackAddress } from "./observability.mjs";
|
|
2
|
-
import { generateImage } from "./image-provider.mjs";
|
|
2
|
+
import { generateImage, supportsImageEditing } from "./image-provider.mjs";
|
|
3
3
|
import { readImageProviderConfig } from "./image-provider-config.mjs";
|
|
4
|
+
import { createImageReferenceStore } from "./image-references.mjs";
|
|
4
5
|
|
|
5
6
|
const MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
6
7
|
const MCP_PROTOCOL_VERSIONS = new Set(["2025-06-18", "2025-03-26", "2024-11-05"]);
|
|
7
8
|
const MCP_MAX_BODY_BYTES = 256 * 1024;
|
|
8
9
|
const IMAGE_TOOL_NAME = "generate_image";
|
|
10
|
+
const EDIT_TOOL_NAME = "edit_image";
|
|
9
11
|
|
|
10
12
|
function sameHostSocket(socket) {
|
|
11
13
|
const remote = String(socket?.remoteAddress || "").replace(/^::ffff:/, "");
|
|
@@ -66,6 +68,25 @@ function imageTool() {
|
|
|
66
68
|
};
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
function editTool() {
|
|
72
|
+
const tool = imageTool();
|
|
73
|
+
return {
|
|
74
|
+
...tool,
|
|
75
|
+
name: EDIT_TOOL_NAME,
|
|
76
|
+
title: "Edit image",
|
|
77
|
+
description: "Edit a previously generated CCDX image using its exact image_id and a change instruction. Preserve the source and return a new image with a new ID for further edits. Source images must still be retained by this running adapter. Does not accept file paths, URLs, masks, or arbitrary uploads.",
|
|
78
|
+
inputSchema: {
|
|
79
|
+
...tool.inputSchema,
|
|
80
|
+
properties: {
|
|
81
|
+
image_id: { type: "string", description: "The image_id returned in the result of the specific CCDX image to edit." },
|
|
82
|
+
prompt: { type: "string", description: "Describe the requested changes and what to preserve from the source image." },
|
|
83
|
+
size: { type: "string", enum: [...tool.inputSchema.properties.size.enum], description: "Optional output dimensions; defaults to the source image's requested size." },
|
|
84
|
+
},
|
|
85
|
+
required: ["image_id", "prompt"],
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
69
90
|
function safeToolError(error, apiKey) {
|
|
70
91
|
if (typeof error?.message === "string" && error.message) {
|
|
71
92
|
let message = error.message.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ");
|
|
@@ -79,6 +100,7 @@ export function createImageMcpHandler({
|
|
|
79
100
|
configLoader = () => readImageProviderConfig(),
|
|
80
101
|
generateImageFn = generateImage,
|
|
81
102
|
maxConcurrent = 2,
|
|
103
|
+
imageReferences = createImageReferenceStore(),
|
|
82
104
|
} = {}) {
|
|
83
105
|
let active = 0;
|
|
84
106
|
return async function imageMcpHandler(req, res) {
|
|
@@ -109,12 +131,14 @@ export function createImageMcpHandler({
|
|
|
109
131
|
const reply = (result) => writeJson(res, 200, { jsonrpc: "2.0", id: request.id, result });
|
|
110
132
|
if (request.method === "initialize") {
|
|
111
133
|
const requestedVersion = String(request.params?.protocolVersion || "");
|
|
134
|
+
const config = configLoader();
|
|
135
|
+
if (!config) imageReferences.clear();
|
|
112
136
|
reply({
|
|
113
137
|
protocolVersion: MCP_PROTOCOL_VERSIONS.has(requestedVersion) ? requestedVersion : MCP_PROTOCOL_VERSION,
|
|
114
138
|
capabilities: { tools: { listChanged: false } },
|
|
115
139
|
serverInfo: { name: "ccdx-image", version: "1" },
|
|
116
|
-
instructions:
|
|
117
|
-
?
|
|
140
|
+
instructions: config
|
|
141
|
+
? `The user enabled CCDX as their image provider. Call generate_image for new images.${supportsImageEditing(config) ? " For changes to a CCDX image, call edit_image with that image's returned image_id. Keep each returned ID with its image; there is no global last image." : " Editing is not supported by this configured provider."} Display the returned image. CCDX handles credentials and the API; no Python, SDK installation, extra API key or alternate endpoint is needed. Do not automatically retry failed generations or edits.`
|
|
118
142
|
: "Image generation is disabled. Enable it with ccdx enable-image before requesting images.",
|
|
119
143
|
});
|
|
120
144
|
return;
|
|
@@ -125,22 +149,29 @@ export function createImageMcpHandler({
|
|
|
125
149
|
}
|
|
126
150
|
if (request.method === "tools/list") {
|
|
127
151
|
const config = configLoader();
|
|
128
|
-
|
|
152
|
+
if (!config) imageReferences.clear();
|
|
153
|
+
reply({ tools: config ? [imageTool(), ...(supportsImageEditing(config) ? [editTool()] : [])] : [] });
|
|
129
154
|
return;
|
|
130
155
|
}
|
|
131
156
|
if (request.method !== "tools/call") {
|
|
132
157
|
writeJson(res, 200, jsonRpcError(request.id, -32601, "Method not found"));
|
|
133
158
|
return;
|
|
134
159
|
}
|
|
135
|
-
if (request.params?.name
|
|
160
|
+
if (![IMAGE_TOOL_NAME, EDIT_TOOL_NAME].includes(request.params?.name)) {
|
|
136
161
|
writeJson(res, 200, jsonRpcError(request.id, -32602, "Unknown image tool"));
|
|
137
162
|
return;
|
|
138
163
|
}
|
|
139
164
|
const config = configLoader();
|
|
140
165
|
if (!config) {
|
|
166
|
+
imageReferences.clear();
|
|
141
167
|
reply({ content: [{ type: "text", text: "Image generation is disabled. Run ccdx enable-image first." }], isError: true });
|
|
142
168
|
return;
|
|
143
169
|
}
|
|
170
|
+
const editing = request.params.name === EDIT_TOOL_NAME;
|
|
171
|
+
if (editing && !supportsImageEditing(config)) {
|
|
172
|
+
reply({ content: [{ type: "text", text: "The configured image provider does not support CCDX editing. No edit was submitted." }], isError: true });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
144
175
|
if (active >= maxConcurrent) {
|
|
145
176
|
reply({ content: [{ type: "text", text: "Image generation is busy. Try again after the current request finishes." }], isError: true });
|
|
146
177
|
return;
|
|
@@ -151,11 +182,24 @@ export function createImageMcpHandler({
|
|
|
151
182
|
req.once?.("aborted", cancel);
|
|
152
183
|
res.once?.("close", cancel);
|
|
153
184
|
try {
|
|
154
|
-
const
|
|
185
|
+
const args = request.params?.arguments || {};
|
|
186
|
+
// Only an explicit retained handle can add source pixels to a request.
|
|
187
|
+
const source = editing ? imageReferences.resolve(config, args.image_id) : null;
|
|
188
|
+
const parameters = { prompt: args.prompt, size: args.size ?? source?.size ?? "1024x1024" };
|
|
189
|
+
if (source) parameters.image = source.image;
|
|
190
|
+
const image = await generateImageFn(config, parameters, { signal: abort.signal });
|
|
191
|
+
let imageId = null;
|
|
192
|
+
if (supportsImageEditing(config)) {
|
|
193
|
+
try { imageId = imageReferences.remember(config, { ...image, size: parameters.size }); } catch {}
|
|
194
|
+
}
|
|
195
|
+
const note = imageId ? ` Image ID: ${imageId}. Use this ID for further edits while retained by this adapter.`
|
|
196
|
+
: supportsImageEditing(config) ? " The image is available, but an editing reference could not be retained." : "";
|
|
155
197
|
reply({
|
|
198
|
+
// Codex prioritizes structuredContent over image blocks. Keep IDs in metadata/text.
|
|
199
|
+
...(imageId ? { _meta: { "ccdx/image_id": imageId } } : {}),
|
|
156
200
|
content: [
|
|
157
201
|
{ type: "image", data: image.data, mimeType: image.mimeType },
|
|
158
|
-
{ type: "text", text:
|
|
202
|
+
{ type: "text", text: `${editing ? "Edited" : "Generated"} ${image.width || ""}${image.width && image.height ? "×" : ""}${image.height || image.size} image with ${image.model}.${note}` },
|
|
159
203
|
],
|
|
160
204
|
});
|
|
161
205
|
} catch (error) {
|
package/src/image-provider.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import https from "node:https";
|
|
|
3
3
|
import net from "node:net";
|
|
4
4
|
|
|
5
5
|
export const IMAGE_MAX_BYTES = 32 * 1024 * 1024;
|
|
6
|
+
export const IMAGE_INPUT_MAX_BYTES = 10 * 1024 * 1024;
|
|
6
7
|
export const IMAGE_GENERATION_TIMEOUT_MS = 180_000;
|
|
7
8
|
export const IMAGE_DOWNLOAD_TIMEOUT_MS = 60_000;
|
|
8
9
|
export const IMAGE_SIZES = new Set(["1024x1024", "1536x1024", "1024x1536"]);
|
|
@@ -28,15 +29,59 @@ function checkedSize(value) {
|
|
|
28
29
|
return size;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
function
|
|
32
|
+
export function supportsImageEditing(config) {
|
|
33
|
+
return ["qwen-image-3.0-pro", "qwen-image-3.0"].includes(config?.model)
|
|
34
|
+
&& ["qwen-messages", "openai-images"].includes(config?.protocol);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function checkedImage(config, value) {
|
|
38
|
+
if (value === undefined) return undefined;
|
|
39
|
+
if (!supportsImageEditing(config)) {
|
|
40
|
+
throw providerError("Image editing is not supported by the configured provider", "ccdx_image_edit_unsupported");
|
|
41
|
+
}
|
|
42
|
+
if (typeof value !== "string") {
|
|
43
|
+
throw providerError("Source image must be a PNG, JPEG, or WebP base64 data URI", "ccdx_image_input_invalid");
|
|
44
|
+
}
|
|
45
|
+
if (value.length > 4 * Math.ceil(IMAGE_INPUT_MAX_BYTES / 3) + 32) {
|
|
46
|
+
throw providerError("Source image exceeds the 10 MiB size limit", "ccdx_image_input_too_large");
|
|
47
|
+
}
|
|
48
|
+
const match = /^data:(image\/(?:png|jpeg|webp));base64,/.exec(value);
|
|
49
|
+
if (!match) {
|
|
50
|
+
throw providerError("Source image must be a PNG, JPEG, or WebP base64 data URI", "ccdx_image_input_invalid");
|
|
51
|
+
}
|
|
52
|
+
const encoded = value.slice(match[0].length);
|
|
53
|
+
if (encoded.length > 4 * Math.ceil(IMAGE_INPUT_MAX_BYTES / 3)) {
|
|
54
|
+
throw providerError("Source image exceeds the 10 MiB size limit", "ccdx_image_input_too_large");
|
|
55
|
+
}
|
|
56
|
+
if (!encoded || encoded.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)) {
|
|
57
|
+
throw providerError("Source image contains invalid base64 data", "ccdx_image_input_invalid");
|
|
58
|
+
}
|
|
59
|
+
const bytes = Buffer.from(encoded, "base64");
|
|
60
|
+
if (bytes.length > IMAGE_INPUT_MAX_BYTES) {
|
|
61
|
+
throw providerError("Source image exceeds the 10 MiB size limit", "ccdx_image_input_too_large");
|
|
62
|
+
}
|
|
63
|
+
if (bytes.toString("base64") !== encoded) {
|
|
64
|
+
throw providerError("Source image contains non-canonical base64 data", "ccdx_image_input_invalid");
|
|
65
|
+
}
|
|
66
|
+
let metadata;
|
|
67
|
+
try { metadata = imageMetadata(bytes); } catch {
|
|
68
|
+
throw providerError("Source image has an unsupported image format", "ccdx_image_input_invalid");
|
|
69
|
+
}
|
|
70
|
+
if (metadata.mimeType !== match[1]) {
|
|
71
|
+
throw providerError("Source image format does not match its data URI", "ccdx_image_input_invalid");
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function generationBody(config, prompt, size, image) {
|
|
32
77
|
if (config.protocol === "qwen-messages") {
|
|
33
78
|
return {
|
|
34
79
|
model: config.model,
|
|
35
|
-
input: { messages: [{ role: "user", content: [{ text: prompt }] }] },
|
|
80
|
+
input: { messages: [{ role: "user", content: [...(image ? [{ image }] : []), { text: prompt }] }] },
|
|
36
81
|
parameters: { n: 1, size: size.replace("x", "*"), watermark: false },
|
|
37
82
|
};
|
|
38
83
|
}
|
|
39
|
-
return { model: config.model, prompt, n: 1, size };
|
|
84
|
+
return { model: config.model, prompt, n: 1, size, ...(image ? { image } : {}) };
|
|
40
85
|
}
|
|
41
86
|
|
|
42
87
|
async function boundedJson(response) {
|
|
@@ -210,6 +255,7 @@ export async function downloadPublicImage(urlValue, {
|
|
|
210
255
|
export async function generateImage(config, {
|
|
211
256
|
prompt,
|
|
212
257
|
size = "1024x1024",
|
|
258
|
+
image,
|
|
213
259
|
} = {}, {
|
|
214
260
|
fetchImpl = fetch,
|
|
215
261
|
downloadImage = downloadPublicImage,
|
|
@@ -218,6 +264,7 @@ export async function generateImage(config, {
|
|
|
218
264
|
} = {}) {
|
|
219
265
|
const normalizedPrompt = checkedPrompt(prompt);
|
|
220
266
|
const normalizedSize = checkedSize(size);
|
|
267
|
+
const normalizedImage = checkedImage(config, image);
|
|
221
268
|
const timeout = AbortSignal.timeout(timeoutMs);
|
|
222
269
|
const requestSignal = signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
223
270
|
let response;
|
|
@@ -229,7 +276,7 @@ export async function generateImage(config, {
|
|
|
229
276
|
"Content-Type": "application/json",
|
|
230
277
|
Accept: "application/json",
|
|
231
278
|
},
|
|
232
|
-
body: JSON.stringify(generationBody(config, normalizedPrompt, normalizedSize)),
|
|
279
|
+
body: JSON.stringify(generationBody(config, normalizedPrompt, normalizedSize, normalizedImage)),
|
|
233
280
|
signal: requestSignal,
|
|
234
281
|
});
|
|
235
282
|
} catch (error) {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { createByteLruCache } from "./bounded-result-cache.mjs";
|
|
3
|
+
import { IMAGE_INPUT_MAX_BYTES } from "./image-provider.mjs";
|
|
4
|
+
|
|
5
|
+
const IMAGE_ID = /^ccdx_img_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
6
|
+
|
|
7
|
+
function providerScope(config) {
|
|
8
|
+
return createHash("sha256").update(JSON.stringify([
|
|
9
|
+
config.endpoint, config.model, config.protocol, config.api_key,
|
|
10
|
+
])).digest("hex");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createImageReferenceStore({ maxBytes = 64 * 1024 * 1024 } = {}) {
|
|
14
|
+
const cache = createByteLruCache({ maxBytes });
|
|
15
|
+
return {
|
|
16
|
+
remember(config, image) {
|
|
17
|
+
if (!["image/png", "image/jpeg", "image/webp"].includes(image?.mimeType)
|
|
18
|
+
|| typeof image?.data !== "string" || !image.data
|
|
19
|
+
|| image.data.length > Math.ceil(IMAGE_INPUT_MAX_BYTES / 3) * 4
|
|
20
|
+
|| Buffer.byteLength(image.data, "base64") > IMAGE_INPUT_MAX_BYTES) return null;
|
|
21
|
+
const imageId = `ccdx_img_${randomUUID()}`;
|
|
22
|
+
const retained = cache.set(`${providerScope(config)}:${imageId}`, JSON.stringify({
|
|
23
|
+
image: `data:${image.mimeType};base64,${image.data}`,
|
|
24
|
+
size: image.size,
|
|
25
|
+
}));
|
|
26
|
+
return retained ? imageId : null;
|
|
27
|
+
},
|
|
28
|
+
resolve(config, imageId) {
|
|
29
|
+
if (typeof imageId !== "string" || !IMAGE_ID.test(imageId)) {
|
|
30
|
+
throw new Error("Provide the exact image_id returned by CCDX for the image to edit.");
|
|
31
|
+
}
|
|
32
|
+
const value = cache.get(`${providerScope(config)}:${imageId}`);
|
|
33
|
+
if (value === undefined) {
|
|
34
|
+
throw new Error("This image reference is unavailable: the adapter restarted, the image was evicted, or the provider changed. No edit was submitted.");
|
|
35
|
+
}
|
|
36
|
+
return JSON.parse(value);
|
|
37
|
+
},
|
|
38
|
+
clear() { cache.clear(); },
|
|
39
|
+
stats() { return cache.stats(); },
|
|
40
|
+
};
|
|
41
|
+
}
|
package/src/image-skill.mjs
CHANGED
|
@@ -108,28 +108,32 @@ export function updateImageSkill({
|
|
|
108
108
|
const endpoint = `${adapterBaseUrl(host, adapterPort)}/mcp/image`;
|
|
109
109
|
const skillBody = `---
|
|
110
110
|
name: ccdx-image
|
|
111
|
-
description: Generate
|
|
111
|
+
description: Generate images from text and edit previously generated CCDX images using the user's configured provider. Use for drawing a new image or modifying a CCDX result identified in the conversation. Excludes arbitrary image uploads, masks, transparent output, and code-native SVG or diagrams.
|
|
112
112
|
---
|
|
113
113
|
|
|
114
114
|
# CCDX image generation
|
|
115
115
|
|
|
116
|
-
The user enabled this provider through ccdx enable-image.
|
|
116
|
+
The user enabled this provider through ccdx enable-image. Use the configured CCDX provider directly for text-to-image requests and supported edits of its previous results.
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
For a new image, use the ccdx_image server's generate_image tool. Supply a complete prompt and one size: 1024x1024 (square), 1536x1024 (landscape), or 1024x1536 (portrait). Display its returned image and retain the returned image_id in the conversation for later edits. No Python, SDK installation, extra API key, or provider discovery is needed.
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
For an edit, use edit_image with the image_id of the specific CCDX result the user wants to change and a prompt describing the changes and what to preserve. Omit size to keep the source dimensions unless the user requests another size. Select the ID from this conversation; never assume a global last image or substitute a different result. Each edit returns a new image and image_id and preserves the source. IDs belong to the running CCDX process and can expire after cache eviction or restart. If the source ID is missing or expired, explain that the original image is unavailable for this edit. The provider must support edit_image; an unsupported edit is not permission to generate a replacement.
|
|
121
|
+
|
|
122
|
+
If the desired tool is not directly callable and the host provides tool search, make one targeted search for ccdx_image generate_image or ccdx_image edit_image as appropriate and use that tool if it becomes callable. If it is still unavailable, run the bundled Node helper once. To generate:
|
|
121
123
|
|
|
122
124
|
\`\`\`sh
|
|
123
125
|
${quote(nodePath)} ${quote(helperPath)} --prompt 'A complete image prompt' --size 1024x1024
|
|
124
126
|
\`\`\`
|
|
125
127
|
|
|
126
|
-
|
|
128
|
+
To edit, add --image-id with the exact source ID returned earlier and supply the editing prompt. Omit --size to preserve the source dimensions. This sends one edit_image call to the same service.
|
|
129
|
+
|
|
130
|
+
Quote the actual prompt safely for the shell. The helper uses the same local CCDX service and prints only the saved absolute image path to stdout; stderr reports CCDX image_id for subsequent edits. An optional --out argument selects a new output file; existing files are never overwritten. Inspect and display the saved image with the available image viewer or an absolute-path Markdown image, and retain its new ID.
|
|
127
131
|
|
|
128
132
|
The helper needs loopback HTTP access. If the task reports restricted network access, use the execution tool's normal permission-request mechanism for this exact command before running it; localhost is also restricted. Keep the host's approval policy unchanged.
|
|
129
133
|
|
|
130
|
-
Only use the helper when the MCP tool is absent, not after
|
|
134
|
+
Only use the helper when the MCP tool is absent, not after generation or editing fails or times out: the first request may already have consumed a generation or edit. A helper error explicitly saying generation or editing has not started means no image request was submitted; resolve its stated local connection or permission issue before trying again. Otherwise report the failure once without switching providers, installing dependencies, or repeating the image request.
|
|
131
135
|
|
|
132
|
-
|
|
136
|
+
For arbitrary uploaded images, reference-image workflows, masks, transparent output, or explicitly requested other providers, keep the relevant native tools and skills; do not substitute a fresh CCDX generation.
|
|
133
137
|
`;
|
|
134
138
|
const skillContent = skillBody.replace("# CCDX image generation", `<!-- ccdx:image-skill:${digest(skillBody)} -->\n# CCDX image generation`);
|
|
135
139
|
const helperBody = `${fs.readFileSync(new URL("./image-tool-client.mjs", import.meta.url), "utf8")}\ntry {\n await runImageToolClient({ endpoint: ${JSON.stringify(endpoint)} });\n} catch (error) {\n console.error(String(error?.message || "Image generation failed").replace(/[\\u0000-\\u001f\\u007f-\\u009f]/g, " ").slice(0, 1000));\n process.exitCode = 1;\n}\n`;
|
|
@@ -7,22 +7,25 @@ const IMAGE_SIZES = new Set(["1024x1024", "1536x1024", "1024x1536"]);
|
|
|
7
7
|
const IMAGE_EXTENSIONS = new Map([["image/png", ".png"], ["image/jpeg", ".jpg"], ["image/webp", ".webp"]]);
|
|
8
8
|
const MAX_IMAGE_BYTES = 32 * 1024 * 1024;
|
|
9
9
|
const MAX_RESPONSE_BYTES = 48 * 1024 * 1024;
|
|
10
|
-
const
|
|
10
|
+
const IMAGE_ID = /^ccdx_img_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
11
|
+
const USAGE = "Usage: generate.mjs --prompt <text> [--image-id <previous-CCDX-image-id>] [--size 1024x1024|1536x1024|1024x1536] [--out <new-file>]";
|
|
11
12
|
|
|
12
13
|
export function parseImageToolArgs(args = []) {
|
|
13
14
|
if (args.length === 1 && ["--help", "-h"].includes(args[0])) return { help: true };
|
|
14
15
|
const values = {};
|
|
15
16
|
for (let index = 0; index < args.length; index += 2) {
|
|
16
17
|
const option = args[index];
|
|
17
|
-
if (!["--prompt", "--size", "--out"].includes(option) || option in values) throw new Error(USAGE);
|
|
18
|
+
if (!["--prompt", "--image-id", "--size", "--out"].includes(option) || option in values) throw new Error(USAGE);
|
|
18
19
|
if (typeof args[index + 1] !== "string" || !args[index + 1].trim()) throw new Error(`Missing value for ${option}`);
|
|
19
20
|
values[option] = args[index + 1];
|
|
20
21
|
}
|
|
21
22
|
const prompt = String(values["--prompt"] || "").trim();
|
|
22
23
|
if (!prompt || prompt.length > 16_000) throw new Error("Image prompt must contain 1 to 16000 characters");
|
|
23
|
-
const
|
|
24
|
-
if (!
|
|
25
|
-
|
|
24
|
+
const imageId = values["--image-id"]?.trim();
|
|
25
|
+
if (imageId !== undefined && !IMAGE_ID.test(imageId)) throw new Error("Invalid CCDX image ID");
|
|
26
|
+
const size = values["--size"] ? values["--size"].trim().toLowerCase() : (imageId ? undefined : "1024x1024");
|
|
27
|
+
if (size !== undefined && !IMAGE_SIZES.has(size)) throw new Error("Unsupported image size");
|
|
28
|
+
return { prompt, size, out: values["--out"], imageId };
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
function localImageEndpoint(value) {
|
|
@@ -79,6 +82,7 @@ export async function runImageToolClient({
|
|
|
79
82
|
cwd = process.cwd(),
|
|
80
83
|
fetchImpl = fetch,
|
|
81
84
|
output = process.stdout,
|
|
85
|
+
metadataOutput = process.stderr,
|
|
82
86
|
signal = AbortSignal.timeout(210_000),
|
|
83
87
|
} = {}) {
|
|
84
88
|
const options = parseImageToolArgs(args);
|
|
@@ -114,11 +118,14 @@ export async function runImageToolClient({
|
|
|
114
118
|
const detail = ["EPERM", "EACCES"].includes(code)
|
|
115
119
|
? "Local network access was denied. Request normal execution permission for this helper command."
|
|
116
120
|
: cleanError(error.message);
|
|
117
|
-
throw Object.assign(new Error(`Image generation has not started: ${detail}`), { code: "CCDX_IMAGE_NOT_STARTED" });
|
|
121
|
+
throw Object.assign(new Error(`Image ${options.imageId ? "editing" : "generation"} has not started: ${detail}`), { code: "CCDX_IMAGE_NOT_STARTED" });
|
|
118
122
|
}
|
|
123
|
+
const argumentsValue = { prompt: options.prompt };
|
|
124
|
+
if (options.size !== undefined) argumentsValue.size = options.size;
|
|
125
|
+
if (options.imageId) argumentsValue.image_id = options.imageId;
|
|
119
126
|
const result = await readRpcResponse(await post({
|
|
120
127
|
jsonrpc: "2.0", id: 2, method: "tools/call",
|
|
121
|
-
params: { name: "
|
|
128
|
+
params: { name: options.imageId ? "edit_image" : "generate_image", arguments: argumentsValue },
|
|
122
129
|
}), 2);
|
|
123
130
|
if (result.isError) {
|
|
124
131
|
throw new Error(cleanError(result.content?.find((part) => part.type === "text")?.text));
|
|
@@ -137,5 +144,7 @@ export async function runImageToolClient({
|
|
|
137
144
|
const filePath = requestedOutput || path.join(directory, `image-${randomUUID()}${extension}`);
|
|
138
145
|
fs.writeFileSync(filePath, bytes, { flag: "wx", mode: 0o600 });
|
|
139
146
|
output.write(`${filePath}\n`);
|
|
147
|
+
const imageId = result._meta?.["ccdx/image_id"];
|
|
148
|
+
if (typeof imageId === "string" && IMAGE_ID.test(imageId)) metadataOutput.write(`CCDX image_id: ${imageId}\n`);
|
|
140
149
|
return filePath;
|
|
141
150
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TERMINAL_ANIMATION_THEMES,
|
|
3
|
+
TERMINAL_ANIMATION_TRACK_WIDTH,
|
|
4
|
+
getTerminalAnimationFrameDelay,
|
|
5
|
+
renderTerminalAnimationFrame,
|
|
6
|
+
} from "./terminal-animation.mjs";
|
|
7
|
+
|
|
8
|
+
const ESC = "\u001b";
|
|
9
|
+
const TICK_MS = 32;
|
|
10
|
+
|
|
11
|
+
export function startTerminalAnimationGallery({
|
|
12
|
+
output,
|
|
13
|
+
env = process.env,
|
|
14
|
+
prefixWidth,
|
|
15
|
+
rowsBelow = 0,
|
|
16
|
+
getInputRows = () => 0,
|
|
17
|
+
timers = { setTimeout, clearTimeout },
|
|
18
|
+
now = () => performance.now(),
|
|
19
|
+
} = {}) {
|
|
20
|
+
const columns = Number(output?.columns);
|
|
21
|
+
const rows = Number(output?.rows);
|
|
22
|
+
const disabled = /^(0|false|no|off)$/i.test(String(env.CCDX_TERMINAL_ANIMATION || "").trim());
|
|
23
|
+
const ci = String(env.CI || "").trim();
|
|
24
|
+
if (output?.isTTY !== true || disabled || (ci && !/^(0|false|no|off)$/i.test(ci))
|
|
25
|
+
|| String(env.TERM || "").toLowerCase() === "dumb" || !Number.isFinite(columns) || !Number.isFinite(rows)
|
|
26
|
+
|| columns <= prefixWidth + TERMINAL_ANIMATION_TRACK_WIDTH + 2
|
|
27
|
+
|| rows < TERMINAL_ANIMATION_THEMES.length + rowsBelow + 2) {
|
|
28
|
+
return { enabled: false, stop() {} };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const timelines = TERMINAL_ANIMATION_THEMES.map((theme) => {
|
|
32
|
+
let duration = 0;
|
|
33
|
+
const frames = Array.from({ length: theme.frameCount }, (_, offset) => {
|
|
34
|
+
const frame = theme.startFrame + offset;
|
|
35
|
+
duration += getTerminalAnimationFrameDelay(theme.id, frame);
|
|
36
|
+
return { frame, until: duration };
|
|
37
|
+
});
|
|
38
|
+
return { theme, frames, duration: duration + theme.loopPauseMs, lastFrame: theme.startFrame };
|
|
39
|
+
});
|
|
40
|
+
const startedAt = now();
|
|
41
|
+
let timer;
|
|
42
|
+
let stopped = false;
|
|
43
|
+
const stop = () => {
|
|
44
|
+
if (stopped) return;
|
|
45
|
+
stopped = true;
|
|
46
|
+
if (timer !== undefined) timers.clearTimeout(timer);
|
|
47
|
+
output.off?.("resize", stop);
|
|
48
|
+
output.off?.("error", stop);
|
|
49
|
+
output.off?.("close", stop);
|
|
50
|
+
};
|
|
51
|
+
const draw = () => {
|
|
52
|
+
timer = undefined;
|
|
53
|
+
if (stopped) return;
|
|
54
|
+
try {
|
|
55
|
+
const inputRows = getInputRows();
|
|
56
|
+
// Once input scrolls the menu out of view its old row anchors are no longer safe.
|
|
57
|
+
if (!Number.isSafeInteger(inputRows) || inputRows < 0
|
|
58
|
+
|| Number(output.rows) < timelines.length + rowsBelow + inputRows + 2) {
|
|
59
|
+
stop();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const elapsed = now() - startedAt;
|
|
63
|
+
let update = "";
|
|
64
|
+
timelines.forEach((timeline, index) => {
|
|
65
|
+
const position = elapsed % timeline.duration;
|
|
66
|
+
const frame = timeline.frames.find(({ until }) => position < until)?.frame ?? null;
|
|
67
|
+
if (frame === timeline.lastFrame) return;
|
|
68
|
+
timeline.lastFrame = frame;
|
|
69
|
+
const track = frame === null ? `[${" ".repeat(TERMINAL_ANIMATION_TRACK_WIDTH)}]`
|
|
70
|
+
: renderTerminalAnimationFrame(timeline.theme.id, frame);
|
|
71
|
+
const up = timelines.length - index + rowsBelow + inputRows;
|
|
72
|
+
update += `${ESC}7${ESC}[${up}A${ESC}[${prefixWidth + 1}G${track}${ESC}8`;
|
|
73
|
+
});
|
|
74
|
+
if (update && output.write(update) === false) { stop(); return; }
|
|
75
|
+
timer = timers.setTimeout(draw, TICK_MS);
|
|
76
|
+
timer?.unref?.();
|
|
77
|
+
} catch { stop(); }
|
|
78
|
+
};
|
|
79
|
+
output.on?.("resize", stop);
|
|
80
|
+
output.on?.("error", stop);
|
|
81
|
+
output.on?.("close", stop);
|
|
82
|
+
timer = timers.setTimeout(draw, TICK_MS);
|
|
83
|
+
timer?.unref?.();
|
|
84
|
+
return { enabled: true, stop };
|
|
85
|
+
}
|