@narumitw/pi-codex-imagegen 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +126 -2
- package/docs/architecture.md +66 -0
- package/docs/protocol.md +64 -0
- package/docs/verification.md +65 -0
- package/docs/workflows.md +65 -0
- package/package.json +43 -4
- package/src/artifacts.ts +480 -0
- package/src/codex-imagegen.ts +408 -0
- package/src/image-client.ts +304 -0
- package/src/index.ts +1 -0
- package/src/menu.ts +132 -0
- package/src/output-path-input.ts +110 -0
- package/src/prompt-editor.ts +107 -0
- package/src/safe-input-render.ts +54 -0
- package/src/settings-screen.ts +163 -0
- package/src/settings.ts +211 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export const IMAGE_ENDPOINT = "https://chatgpt.com/backend-api/codex/images/generations";
|
|
4
|
+
export const AUTH_TIMEOUT_MS = 30_000;
|
|
5
|
+
export const REQUEST_TIMEOUT_MS = 300_000;
|
|
6
|
+
// Image endpoints can finish generation before sending headers. Allow the full
|
|
7
|
+
// request budget rather than applying an ordinary short metadata-request limit.
|
|
8
|
+
export const HEADERS_TIMEOUT_MS = REQUEST_TIMEOUT_MS;
|
|
9
|
+
export const MAX_PROMPT_CHARS = 32_000;
|
|
10
|
+
const MAX_IMAGE_BYTES = 32 * 1024 * 1024;
|
|
11
|
+
const MAX_BASE64_CHARS = Math.ceil(MAX_IMAGE_BYTES / 3) * 4;
|
|
12
|
+
export const MAX_RESPONSE_BYTES = MAX_BASE64_CHARS + 64 * 1024;
|
|
13
|
+
|
|
14
|
+
export type ImageAuthRegistry = Pick<
|
|
15
|
+
ModelRegistry,
|
|
16
|
+
"getAll" | "getProvider" | "isUsingOAuth" | "getProviderAuth" | "getProviderAuthStatus"
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
type ImageRequest = { prompt: string; transparentBackground: boolean };
|
|
20
|
+
type RequestOptions = {
|
|
21
|
+
signal: AbortSignal;
|
|
22
|
+
isCurrent?: () => boolean;
|
|
23
|
+
fetch?: typeof globalThis.fetch;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const PROVIDER = "openai-codex";
|
|
27
|
+
const AUTH_ERROR = "Cannot resolve compatible Pi Codex OAuth. Sign in to openai-codex through Pi and try again.";
|
|
28
|
+
const CANCELLED = "Image generation cancelled. The server may still consume quota.";
|
|
29
|
+
const INVALID_RESPONSE = "The image endpoint returned invalid image JSON. No automatic retry was made.";
|
|
30
|
+
const REQUEST_TIMEOUT_ERROR =
|
|
31
|
+
"Image generation timed out. No automatic retry was made; the server may still consume quota.";
|
|
32
|
+
|
|
33
|
+
class ImageClientError extends Error {}
|
|
34
|
+
|
|
35
|
+
function fail(message: string): never {
|
|
36
|
+
throw new ImageClientError(message);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isCanonicalBaseUrl(value: unknown): boolean {
|
|
40
|
+
// Accept Pi's provider root and Codex's API root, not URL-normalized aliases.
|
|
41
|
+
return (
|
|
42
|
+
value === "https://chatgpt.com/backend-api" ||
|
|
43
|
+
value === "https://chatgpt.com/backend-api/" ||
|
|
44
|
+
value === "https://chatgpt.com/backend-api/codex" ||
|
|
45
|
+
value === "https://chatgpt.com/backend-api/codex/"
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function compatibleMetadata(registry: ImageAuthRegistry) {
|
|
50
|
+
const model = registry.getAll().find((candidate) => candidate.provider === PROVIDER);
|
|
51
|
+
const provider = registry.getProvider(PROVIDER);
|
|
52
|
+
if (!model || !provider || provider.id !== PROVIDER) {
|
|
53
|
+
fail("Pi openai-codex is unavailable. Configure its provider and sign in through Pi.");
|
|
54
|
+
}
|
|
55
|
+
const status = registry.getProviderAuthStatus(PROVIDER);
|
|
56
|
+
if (!status.configured) fail("Pi openai-codex authentication is missing. Sign in through Pi.");
|
|
57
|
+
if (!registry.isUsingOAuth(model) || !provider.auth.oauth) {
|
|
58
|
+
fail("Image generation requires Pi openai-codex OAuth, not an API key or another OpenAI login.");
|
|
59
|
+
}
|
|
60
|
+
if (!isCanonicalBaseUrl(provider.baseUrl) || !isCanonicalBaseUrl(model.baseUrl)) {
|
|
61
|
+
fail("Image generation requires the canonical ChatGPT backend. Custom provider endpoints are not supported.");
|
|
62
|
+
}
|
|
63
|
+
return status;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Read public, non-secret metadata only. This does not resolve or refresh auth. */
|
|
67
|
+
export function getImageAuthStatus(registry: ImageAuthRegistry): string {
|
|
68
|
+
try {
|
|
69
|
+
const status = compatibleMetadata(registry);
|
|
70
|
+
const sources = {
|
|
71
|
+
stored: "stored Pi auth",
|
|
72
|
+
runtime: "runtime auth",
|
|
73
|
+
environment: "environment auth",
|
|
74
|
+
fallback: "fallback auth",
|
|
75
|
+
models_json_key: "models.json auth",
|
|
76
|
+
models_json_command: "models.json auth command",
|
|
77
|
+
};
|
|
78
|
+
// Never display arbitrary labels or provider-supplied strings.
|
|
79
|
+
const source = status.source && Object.hasOwn(sources, status.source) ? sources[status.source] : undefined;
|
|
80
|
+
return `Pi openai-codex OAuth configured${source ? ` (${source})` : ""}. Endpoint access is unverified.`;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return error instanceof ImageClientError ? error.message : "Pi Codex authentication metadata is unavailable.";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function accountIdFromToken(token: unknown): { token: string; accountId: string } {
|
|
87
|
+
if (typeof token !== "string" || token.length > 32 * 1024) fail(AUTH_ERROR);
|
|
88
|
+
const parts = token.split(".");
|
|
89
|
+
if (parts.length !== 3 || parts.some((part) => part.length % 4 === 1 || !/^[A-Za-z0-9_-]+$/.test(part)))
|
|
90
|
+
fail(AUTH_ERROR);
|
|
91
|
+
try {
|
|
92
|
+
const header: unknown = JSON.parse(Buffer.from(parts[0] ?? "", "base64url").toString("utf8"));
|
|
93
|
+
const payload: unknown = JSON.parse(Buffer.from(parts[1] ?? "", "base64url").toString("utf8"));
|
|
94
|
+
if (
|
|
95
|
+
!isObject(header) ||
|
|
96
|
+
typeof header.alg !== "string" ||
|
|
97
|
+
!/^[A-Za-z0-9_-]{1,128}$/.test(header.alg) ||
|
|
98
|
+
header.alg.toLowerCase() === "none"
|
|
99
|
+
)
|
|
100
|
+
fail(AUTH_ERROR);
|
|
101
|
+
const claim = isObject(payload) ? payload["https://api.openai.com/auth"] : undefined;
|
|
102
|
+
const accountId = isObject(claim) ? claim.chatgpt_account_id : undefined;
|
|
103
|
+
if (typeof accountId !== "string" || !/^[A-Za-z0-9_-]{1,256}$/.test(accountId)) fail(AUTH_ERROR);
|
|
104
|
+
// Pi owns token refresh. This parses the account claim, not a signature verification.
|
|
105
|
+
return { token, accountId };
|
|
106
|
+
} catch {
|
|
107
|
+
return fail(AUTH_ERROR);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
112
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function cancelBody(response: Response): void {
|
|
116
|
+
// Never wait for an uncooperative transport's cancellation acknowledgement.
|
|
117
|
+
try {
|
|
118
|
+
void response.body?.cancel().catch(() => undefined);
|
|
119
|
+
} catch {
|
|
120
|
+
// Cleanup cannot expose transport errors or replace the original failure.
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function statusError(status: number): never {
|
|
125
|
+
if (status === 401) fail("Image authentication was rejected (401). Sign in to openai-codex through Pi again.");
|
|
126
|
+
if (status === 403) fail("Image access was denied (403). Check account entitlement and organization policy.");
|
|
127
|
+
if (status === 429)
|
|
128
|
+
fail("The image quota or rate limit was reached (429). Check your account limits before trying again.");
|
|
129
|
+
if (status >= 300 && status < 400) fail("The image endpoint returned a redirect. Redirects are not allowed.");
|
|
130
|
+
fail(
|
|
131
|
+
"The image endpoint rejected the request. Check service availability before trying again; no automatic retry was made.",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Send exactly one request. Pi owns auth refresh; late auth results are not consumed. */
|
|
136
|
+
export async function generateImageRequest(
|
|
137
|
+
registry: ImageAuthRegistry,
|
|
138
|
+
request: ImageRequest,
|
|
139
|
+
options: RequestOptions,
|
|
140
|
+
): Promise<{ base64: string; requestId?: string }> {
|
|
141
|
+
const controller = new AbortController();
|
|
142
|
+
const abort = (message: string) => {
|
|
143
|
+
if (!controller.signal.aborted) controller.abort(new ImageClientError(message));
|
|
144
|
+
};
|
|
145
|
+
const onAbort = () => abort(CANCELLED);
|
|
146
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
147
|
+
const expiresAt = Date.now() + REQUEST_TIMEOUT_MS;
|
|
148
|
+
const totalTimer = setTimeout(() => abort(REQUEST_TIMEOUT_ERROR), REQUEST_TIMEOUT_MS);
|
|
149
|
+
let response: Response | undefined;
|
|
150
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
151
|
+
|
|
152
|
+
const checkCurrent = () => {
|
|
153
|
+
if (options.signal.aborted || options.isCurrent?.() === false) abort(CANCELLED);
|
|
154
|
+
if (Date.now() >= expiresAt) abort(REQUEST_TIMEOUT_ERROR);
|
|
155
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
156
|
+
};
|
|
157
|
+
// Race even APIs which ignore AbortSignal. Each race removes its own listener.
|
|
158
|
+
const wait = <T>(promise: Promise<T>): Promise<T> =>
|
|
159
|
+
new Promise<T>((resolve, reject) => {
|
|
160
|
+
const signal = controller.signal;
|
|
161
|
+
const aborted = () => {
|
|
162
|
+
signal.removeEventListener("abort", aborted);
|
|
163
|
+
reject(signal.reason);
|
|
164
|
+
};
|
|
165
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
166
|
+
if (signal.aborted) aborted();
|
|
167
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", aborted));
|
|
168
|
+
});
|
|
169
|
+
const deadline = async <T>(promise: Promise<T>, ms: number, message: string): Promise<T> => {
|
|
170
|
+
const timer = setTimeout(() => abort(message), ms);
|
|
171
|
+
try {
|
|
172
|
+
return await wait(promise);
|
|
173
|
+
} finally {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
checkCurrent();
|
|
180
|
+
const { prompt, transparentBackground } = request;
|
|
181
|
+
if (typeof prompt !== "string" || !prompt.trim() || prompt.length > MAX_PROMPT_CHARS) {
|
|
182
|
+
fail("The image prompt must contain 1 to 32,000 characters and cannot be blank.");
|
|
183
|
+
}
|
|
184
|
+
if (typeof transparentBackground !== "boolean") fail("The background choice must be a boolean.");
|
|
185
|
+
compatibleMetadata(registry);
|
|
186
|
+
let resolved: Awaited<ReturnType<ImageAuthRegistry["getProviderAuth"]>>;
|
|
187
|
+
try {
|
|
188
|
+
// The public facade has no signal argument. Only this wait can be cancelled.
|
|
189
|
+
resolved = await deadline(
|
|
190
|
+
registry.getProviderAuth(PROVIDER),
|
|
191
|
+
AUTH_TIMEOUT_MS,
|
|
192
|
+
"Pi Codex authentication timed out. Try signing in through Pi again.",
|
|
193
|
+
);
|
|
194
|
+
} catch {
|
|
195
|
+
checkCurrent();
|
|
196
|
+
fail(AUTH_ERROR);
|
|
197
|
+
}
|
|
198
|
+
checkCurrent();
|
|
199
|
+
compatibleMetadata(registry);
|
|
200
|
+
if (!resolved || (resolved.auth.baseUrl !== undefined && !isCanonicalBaseUrl(resolved.auth.baseUrl)))
|
|
201
|
+
fail(AUTH_ERROR);
|
|
202
|
+
const { token, accountId } = accountIdFromToken(resolved.auth.apiKey);
|
|
203
|
+
checkCurrent();
|
|
204
|
+
// There is no public Pi helper for arbitrary HTTP operations. Use runtime fetch;
|
|
205
|
+
// proxy support is runtime-dependent, not Pi's provider/WebSocket proxy handling.
|
|
206
|
+
const pending = (options.fetch ?? globalThis.fetch)(IMAGE_ENDPOINT, {
|
|
207
|
+
method: "POST",
|
|
208
|
+
redirect: "error",
|
|
209
|
+
signal: controller.signal,
|
|
210
|
+
headers: {
|
|
211
|
+
"Content-Type": "application/json",
|
|
212
|
+
Authorization: `Bearer ${token}`,
|
|
213
|
+
"ChatGPT-Account-ID": accountId,
|
|
214
|
+
originator: "pi-codex-imagegen",
|
|
215
|
+
"User-Agent": "pi-codex-imagegen",
|
|
216
|
+
},
|
|
217
|
+
body: JSON.stringify({
|
|
218
|
+
prompt,
|
|
219
|
+
background: transparentBackground ? "transparent" : "opaque",
|
|
220
|
+
model: "gpt-image-2",
|
|
221
|
+
quality: "auto",
|
|
222
|
+
size: "auto",
|
|
223
|
+
}),
|
|
224
|
+
});
|
|
225
|
+
// A fetch implementation may return after abort. Close its body without using it.
|
|
226
|
+
void pending.then(
|
|
227
|
+
(late) => {
|
|
228
|
+
if (controller.signal.aborted) cancelBody(late);
|
|
229
|
+
},
|
|
230
|
+
() => undefined,
|
|
231
|
+
);
|
|
232
|
+
response = await deadline(
|
|
233
|
+
pending,
|
|
234
|
+
HEADERS_TIMEOUT_MS,
|
|
235
|
+
"The image endpoint response headers timed out. No automatic retry was made.",
|
|
236
|
+
);
|
|
237
|
+
checkCurrent();
|
|
238
|
+
if (response.redirected) fail("The image endpoint returned a redirect. Redirects are not allowed.");
|
|
239
|
+
if (!response.ok) statusError(response.status);
|
|
240
|
+
if (response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() !== "application/json")
|
|
241
|
+
fail(INVALID_RESPONSE);
|
|
242
|
+
const length = response.headers.get("content-length");
|
|
243
|
+
if (length !== null && (!/^\d+$/.test(length) || Number(length) > MAX_RESPONSE_BYTES))
|
|
244
|
+
fail("The image response exceeds the size limit.");
|
|
245
|
+
if (!response.body) fail(INVALID_RESPONSE);
|
|
246
|
+
reader = response.body.getReader();
|
|
247
|
+
// A growing buffer also bounds allocation overhead for one-byte chunks.
|
|
248
|
+
let body = Buffer.allocUnsafe(64 * 1024);
|
|
249
|
+
let bytes = 0;
|
|
250
|
+
// The total deadline also bounds every body read, including stalled streams.
|
|
251
|
+
for (;;) {
|
|
252
|
+
const chunk = await wait(reader.read());
|
|
253
|
+
checkCurrent();
|
|
254
|
+
if (chunk.done) break;
|
|
255
|
+
const nextSize = bytes + chunk.value.byteLength;
|
|
256
|
+
if (nextSize > MAX_RESPONSE_BYTES) fail("The image response exceeds the size limit.");
|
|
257
|
+
if (nextSize > body.byteLength) {
|
|
258
|
+
const next = Buffer.allocUnsafe(Math.min(MAX_RESPONSE_BYTES, Math.max(nextSize, body.byteLength * 2)));
|
|
259
|
+
body.copy(next, 0, 0, bytes);
|
|
260
|
+
body = next;
|
|
261
|
+
}
|
|
262
|
+
body.set(chunk.value, bytes);
|
|
263
|
+
bytes = nextSize;
|
|
264
|
+
}
|
|
265
|
+
let parsed: unknown;
|
|
266
|
+
try {
|
|
267
|
+
parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body.subarray(0, bytes)));
|
|
268
|
+
} catch {
|
|
269
|
+
fail(INVALID_RESPONSE);
|
|
270
|
+
}
|
|
271
|
+
checkCurrent();
|
|
272
|
+
if (!isObject(parsed) || !Array.isArray(parsed.data) || parsed.data.length !== 1) fail(INVALID_RESPONSE);
|
|
273
|
+
const image: unknown = parsed.data[0];
|
|
274
|
+
if (!isObject(image) || typeof image.b64_json !== "string" || !image.b64_json.trim()) fail(INVALID_RESPONSE);
|
|
275
|
+
const base64 = image.b64_json;
|
|
276
|
+
const padding = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
|
|
277
|
+
if (base64.length > MAX_BASE64_CHARS || Math.floor((base64.length * 3) / 4) - padding > MAX_IMAGE_BYTES) {
|
|
278
|
+
fail("The generated image exceeds the size limit.");
|
|
279
|
+
}
|
|
280
|
+
const requestId = response.headers.get("x-codex-imagegen-request-id");
|
|
281
|
+
// Only the nested image ID is defined by Codex; do not expose arbitrary headers.
|
|
282
|
+
return {
|
|
283
|
+
base64,
|
|
284
|
+
...(requestId && /^[A-Za-z0-9_-]{1,128}$/.test(requestId) ? { requestId } : {}),
|
|
285
|
+
};
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (controller.signal.aborted) throw controller.signal.reason;
|
|
288
|
+
if (error instanceof ImageClientError) throw error;
|
|
289
|
+
return fail("Image generation failed. Check Pi authentication and network access; no automatic retry was made.");
|
|
290
|
+
} finally {
|
|
291
|
+
clearTimeout(totalTimer);
|
|
292
|
+
options.signal.removeEventListener("abort", onAbort);
|
|
293
|
+
if (reader) {
|
|
294
|
+
try {
|
|
295
|
+
void reader.cancel().catch(() => undefined);
|
|
296
|
+
reader.releaseLock();
|
|
297
|
+
} catch {
|
|
298
|
+
// Cleanup is best effort and must not disclose transport errors.
|
|
299
|
+
}
|
|
300
|
+
} else if (response) {
|
|
301
|
+
cancelBody(response);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./codex-imagegen.js";
|
package/src/menu.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { defineMenu, runMenu, runTask, sanitizeTerminalText } from "@narumitw/pi-tui-kit";
|
|
3
|
+
import { showImagegenOutputPath } from "./output-path-input.js";
|
|
4
|
+
import { showImagegenPrompt } from "./prompt-editor.js";
|
|
5
|
+
import type { ImagegenSettings } from "./settings.js";
|
|
6
|
+
import { showImagegenSettings } from "./settings-screen.js";
|
|
7
|
+
|
|
8
|
+
export interface ImagegenMenuOwner {
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
isCurrent(): boolean;
|
|
11
|
+
getSettings(): ImagegenSettings;
|
|
12
|
+
settingsPath: string;
|
|
13
|
+
/** Non-secret local metadata only; must not resolve or refresh credentials. */
|
|
14
|
+
getAuthStatus(): string;
|
|
15
|
+
isBusy(): boolean;
|
|
16
|
+
/** Resolves after persistence and runtime application; rejects after rollback. */
|
|
17
|
+
saveSettings(patch: Partial<ImagegenSettings>): Promise<void>;
|
|
18
|
+
/** Must honor the signal, including disposal, and publish only while current. */
|
|
19
|
+
generate(prompt: string, path: string, signal: AbortSignal): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The command owns mode and argument rejection; this function opens TUI only. */
|
|
23
|
+
export async function showImagegenMenu(ctx: ExtensionCommandContext, owner: ImagegenMenuOwner): Promise<void> {
|
|
24
|
+
if (ctx.mode !== "tui" || !ctx.hasUI) return;
|
|
25
|
+
const current = () => !owner.signal.aborted && owner.isCurrent();
|
|
26
|
+
while (current()) {
|
|
27
|
+
let route: "generate" | "settings" | undefined;
|
|
28
|
+
const menu = defineMenu<undefined, "main" | "status" | "help", "generate" | "settings">({
|
|
29
|
+
start: "main",
|
|
30
|
+
screens: {
|
|
31
|
+
main: () => ({
|
|
32
|
+
kind: "actions",
|
|
33
|
+
title: "Codex Image Generation",
|
|
34
|
+
lines: ["Experimental internal endpoint", `Activity: ${owner.isBusy() ? "Generating" : "Idle"}`],
|
|
35
|
+
items: [
|
|
36
|
+
{
|
|
37
|
+
id: "generate",
|
|
38
|
+
label: "Generate Image",
|
|
39
|
+
action: "generate",
|
|
40
|
+
description: "Send a prompt to ChatGPT and save one image",
|
|
41
|
+
disabled: owner.isBusy(),
|
|
42
|
+
disabledReason: "An image generation is already active",
|
|
43
|
+
},
|
|
44
|
+
{ id: "settings", label: "Settings", action: "settings" },
|
|
45
|
+
{ id: "status", label: "Status", to: "status" },
|
|
46
|
+
{ id: "help", label: "Help", to: "help" },
|
|
47
|
+
],
|
|
48
|
+
hint: "close",
|
|
49
|
+
}),
|
|
50
|
+
status: () => ({
|
|
51
|
+
kind: "detail",
|
|
52
|
+
title: "Image Generation Status",
|
|
53
|
+
lines: [
|
|
54
|
+
`Authentication: ${sanitizeTerminalText(owner.getAuthStatus())}`,
|
|
55
|
+
"Endpoint: experimental ChatGPT/Codex internal image API",
|
|
56
|
+
"Local metadata does not verify account entitlement or live readiness.",
|
|
57
|
+
`Tool access: ${owner.getSettings().toolEnabled ? "enabled" : "disabled"}`,
|
|
58
|
+
"Each generation requires an output path.",
|
|
59
|
+
`User settings: ${sanitizeTerminalText(owner.settingsPath)}`,
|
|
60
|
+
`Activity: ${owner.isBusy() ? "Generating" : "Idle"}`,
|
|
61
|
+
],
|
|
62
|
+
}),
|
|
63
|
+
help: () => ({
|
|
64
|
+
kind: "detail",
|
|
65
|
+
title: "Image Generation Help",
|
|
66
|
+
lines: [
|
|
67
|
+
"Generate Image sends your prompt to ChatGPT and saves one image at the exact path you enter. Provider quota and account restrictions apply.",
|
|
68
|
+
"Relative paths use the generation request's working directory. Existing files are overwritten and symlinks are followed.",
|
|
69
|
+
"Use Pi's compatible Codex OAuth login. A Codex CLI login alone is not imported. Status never refreshes credentials.",
|
|
70
|
+
"Settings save immediately in user scope. Closing Settings does not undo saved changes. Tool access does not control manual generation.",
|
|
71
|
+
"Tool access changes apply at an idle boundary and change the model-visible tool definitions.",
|
|
72
|
+
"Cancellation aborts the owned request, but cannot guarantee remote cancellation, a quota refund, or rollback of file writes. Partial or completed writes can remain.",
|
|
73
|
+
"Requests are not retried automatically. Image editing, reference images, masks, and batches are not supported.",
|
|
74
|
+
],
|
|
75
|
+
}),
|
|
76
|
+
},
|
|
77
|
+
actions: {
|
|
78
|
+
generate: () => {
|
|
79
|
+
route = "generate";
|
|
80
|
+
return { kind: "close" };
|
|
81
|
+
},
|
|
82
|
+
settings: () => {
|
|
83
|
+
route = "settings";
|
|
84
|
+
return { kind: "close" };
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
const navigation = await runMenu(ctx, menu, {
|
|
89
|
+
getState: () => undefined,
|
|
90
|
+
signal: owner.signal,
|
|
91
|
+
isCurrent: owner.isCurrent.bind(owner),
|
|
92
|
+
onError: () => {
|
|
93
|
+
if (current()) ctx.ui.notify("Image generation menu failed.", "error");
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
if (!current() || navigation.kind !== "closed" || !route) return;
|
|
97
|
+
// Standalone interactions start only after the root navigator releases the terminal.
|
|
98
|
+
if (route === "settings") {
|
|
99
|
+
const settings = await showImagegenSettings(ctx, owner);
|
|
100
|
+
if (!current() || settings !== "back") return;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (owner.isBusy()) continue;
|
|
104
|
+
const prompt = await showImagegenPrompt(ctx, owner);
|
|
105
|
+
if (!current()) return;
|
|
106
|
+
if (prompt.kind === "back") continue;
|
|
107
|
+
if (prompt.kind !== "submitted") return;
|
|
108
|
+
const destination = await showImagegenOutputPath(ctx, owner);
|
|
109
|
+
if (!current()) return;
|
|
110
|
+
if (destination.kind === "back") continue;
|
|
111
|
+
if (destination.kind !== "submitted") return;
|
|
112
|
+
if (owner.isBusy()) {
|
|
113
|
+
ctx.ui.notify("An image generation is already active.", "warning");
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const generation = await runTask(ctx, {
|
|
117
|
+
label: "Generating image…",
|
|
118
|
+
signal: owner.signal,
|
|
119
|
+
isCurrent: owner.isCurrent.bind(owner),
|
|
120
|
+
task: async ({ signal }) => {
|
|
121
|
+
if (signal.aborted || !current()) return;
|
|
122
|
+
await owner.generate(prompt.prompt, destination.path, signal);
|
|
123
|
+
},
|
|
124
|
+
// Never display raw provider errors or prompts through the generic error reporter.
|
|
125
|
+
onError: () => {
|
|
126
|
+
if (current())
|
|
127
|
+
ctx.ui.notify("Image generation failed. See the extension guidance before trying again.", "error");
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
if (!current() || generation.kind === "stale" || generation.kind === "cancelled") return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Input, isKeyRelease, matchesKey } from "@earendil-works/pi-tui";
|
|
3
|
+
import { HorizontalRule, renderBoundedFrameLayout, runCustomInteraction } from "@narumitw/pi-tui-kit";
|
|
4
|
+
import type { ImagegenMenuOwner } from "./menu.js";
|
|
5
|
+
import { renderSafeInput } from "./safe-input-render.js";
|
|
6
|
+
|
|
7
|
+
export type ImagegenOutputPathResult = { kind: "submitted"; path: string } | { kind: "back" | "close" | "stale" };
|
|
8
|
+
|
|
9
|
+
export async function showImagegenOutputPath(
|
|
10
|
+
ctx: ExtensionCommandContext,
|
|
11
|
+
owner: Pick<ImagegenMenuOwner, "signal" | "isCurrent">,
|
|
12
|
+
): Promise<ImagegenOutputPathResult> {
|
|
13
|
+
const result = await runCustomInteraction<ImagegenOutputPathResult>(ctx, {
|
|
14
|
+
signal: owner.signal,
|
|
15
|
+
isCurrent: owner.isCurrent.bind(owner),
|
|
16
|
+
onError: () => {
|
|
17
|
+
if (!owner.signal.aborted && owner.isCurrent()) ctx.ui.notify("Image output path input failed.", "error");
|
|
18
|
+
},
|
|
19
|
+
create: ({ tui, theme, keybindings, signal, complete }) => {
|
|
20
|
+
const input = new Input();
|
|
21
|
+
const rule = new HorizontalRule({ ruleStyle: (text) => theme.fg("border", text) });
|
|
22
|
+
let error = "";
|
|
23
|
+
let inPaste = false;
|
|
24
|
+
let rows = new Map<number, number>();
|
|
25
|
+
let width = 0;
|
|
26
|
+
let height = 0;
|
|
27
|
+
const current = () => !signal.aborted && owner.isCurrent();
|
|
28
|
+
const stale = () => {
|
|
29
|
+
if (current()) return false;
|
|
30
|
+
complete({ kind: "stale" });
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
const clearGeometry = () => {
|
|
34
|
+
rows.clear();
|
|
35
|
+
width = 0;
|
|
36
|
+
height = 0;
|
|
37
|
+
};
|
|
38
|
+
input.onSubmit = (path) => {
|
|
39
|
+
if (stale()) return;
|
|
40
|
+
if (path === "") error = "An output path is required.";
|
|
41
|
+
else complete({ kind: "submitted", path });
|
|
42
|
+
};
|
|
43
|
+
input.onEscape = () => complete({ kind: "back" });
|
|
44
|
+
return {
|
|
45
|
+
get focused() {
|
|
46
|
+
return input.focused;
|
|
47
|
+
},
|
|
48
|
+
set focused(value: boolean) {
|
|
49
|
+
input.focused = value;
|
|
50
|
+
},
|
|
51
|
+
render(availableWidth) {
|
|
52
|
+
const content = renderSafeInput(input, Math.max(1, availableWidth));
|
|
53
|
+
if (error) content.push(theme.fg("error", error));
|
|
54
|
+
const terminalRows = Number.isFinite(tui.terminal.rows) ? Math.floor(tui.terminal.rows) : 24;
|
|
55
|
+
const layout = renderBoundedFrameLayout({
|
|
56
|
+
width: availableWidth,
|
|
57
|
+
maxRows: Math.max(1, terminalRows - 3),
|
|
58
|
+
rule: rule.render(availableWidth)[0] ?? "",
|
|
59
|
+
title: [theme.fg("accent", "Image Output Path")],
|
|
60
|
+
context: [
|
|
61
|
+
theme.fg("muted", "Enter the exact output path. No filename or extension is added."),
|
|
62
|
+
theme.fg("muted", "Relative paths use the generation request's working directory."),
|
|
63
|
+
theme.fg("warning", "Existing files are overwritten. Symlinks are followed."),
|
|
64
|
+
],
|
|
65
|
+
content,
|
|
66
|
+
focusedRow: 0,
|
|
67
|
+
priorityRows: error ? [0, 1] : [0],
|
|
68
|
+
});
|
|
69
|
+
width = Math.max(0, Math.floor(availableWidth));
|
|
70
|
+
height = layout.lines.length;
|
|
71
|
+
rows = new Map(layout.contentRows.map(({ frameIndex, contentIndex }) => [frameIndex, contentIndex]));
|
|
72
|
+
return layout.lines;
|
|
73
|
+
},
|
|
74
|
+
handleInput(data) {
|
|
75
|
+
if (stale()) return;
|
|
76
|
+
// Let Input own bracketed paste, including split chunks that match shortcuts.
|
|
77
|
+
const pasted = inPaste || data.includes("\x1b[200~");
|
|
78
|
+
if (!pasted && isKeyRelease(data)) return;
|
|
79
|
+
if (data.includes("\x1b[200~")) inPaste = true;
|
|
80
|
+
if (data.includes("\x1b[201~")) inPaste = false;
|
|
81
|
+
clearGeometry();
|
|
82
|
+
if (!pasted && matchesKey(data, "ctrl+c")) complete({ kind: "close" });
|
|
83
|
+
else if (!pasted && keybindings.matches(data, "tui.select.cancel")) complete({ kind: "back" });
|
|
84
|
+
// Native Input uses tui.input.submit (not tui.select.confirm), editing and paste semantics.
|
|
85
|
+
else input.handleInput(data);
|
|
86
|
+
if (current()) tui.requestRender();
|
|
87
|
+
},
|
|
88
|
+
handleMouse(event) {
|
|
89
|
+
if (stale() || event.x < 0 || event.x >= width || event.y < 0 || event.y >= height) return undefined;
|
|
90
|
+
const y = rows.get(event.y);
|
|
91
|
+
if (y !== 0) return undefined;
|
|
92
|
+
const handled = input.handleMouse({ ...event, y, height: 1 });
|
|
93
|
+
// Release and synthesized click share Pi's last published geometry.
|
|
94
|
+
if (handled) tui.requestRender();
|
|
95
|
+
return handled;
|
|
96
|
+
},
|
|
97
|
+
invalidate() {
|
|
98
|
+
input.invalidate();
|
|
99
|
+
rule.invalidate();
|
|
100
|
+
clearGeometry();
|
|
101
|
+
},
|
|
102
|
+
dispose() {
|
|
103
|
+
clearGeometry();
|
|
104
|
+
input.focused = false;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
return result.kind === "completed" ? result.value : { kind: "stale" };
|
|
110
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type ExtensionCommandContext, ExtensionEditorComponent } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { CURSOR_MARKER, Editor, isKeyRelease, matchesKey } from "@earendil-works/pi-tui";
|
|
3
|
+
import { HorizontalRule, renderBoundedFrameLayout, runCustomInteraction } from "@narumitw/pi-tui-kit";
|
|
4
|
+
import type { ImagegenMenuOwner } from "./menu.js";
|
|
5
|
+
import { renderSafeInput } from "./safe-input-render.js";
|
|
6
|
+
|
|
7
|
+
export type ImagegenPromptResult = { kind: "submitted"; prompt: string } | { kind: "back" | "close" | "stale" };
|
|
8
|
+
|
|
9
|
+
export async function showImagegenPrompt(
|
|
10
|
+
ctx: ExtensionCommandContext,
|
|
11
|
+
owner: Pick<ImagegenMenuOwner, "signal" | "isCurrent">,
|
|
12
|
+
): Promise<ImagegenPromptResult> {
|
|
13
|
+
const result = await runCustomInteraction<ImagegenPromptResult>(ctx, {
|
|
14
|
+
signal: owner.signal,
|
|
15
|
+
isCurrent: owner.isCurrent.bind(owner),
|
|
16
|
+
onError: () => {
|
|
17
|
+
if (!owner.signal.aborted && owner.isCurrent()) ctx.ui.notify("Image prompt editor failed.", "error");
|
|
18
|
+
},
|
|
19
|
+
create: ({ tui, theme, keybindings, signal, complete }) => {
|
|
20
|
+
const native = new ExtensionEditorComponent(
|
|
21
|
+
tui,
|
|
22
|
+
keybindings,
|
|
23
|
+
"Image prompt",
|
|
24
|
+
undefined,
|
|
25
|
+
// Native submission expands large-paste markers before clearing the draft.
|
|
26
|
+
(prompt) => {
|
|
27
|
+
if (prompt.trim()) complete({ kind: "submitted", prompt });
|
|
28
|
+
},
|
|
29
|
+
() => complete({ kind: "back" }),
|
|
30
|
+
);
|
|
31
|
+
// Inspect the public Container children, not the native editor's private state.
|
|
32
|
+
// Keep native editing, paste expansion, focus, and mouse handling. The owned frame
|
|
33
|
+
// replaces native hints because external-editor launch is deliberately unavailable:
|
|
34
|
+
// Pi's external-editor handoff cannot honor dialog disposal or abort.
|
|
35
|
+
const editor = native.children.find((child): child is Editor => child instanceof Editor);
|
|
36
|
+
if (!editor) throw new Error("Pi multiline editor is unavailable");
|
|
37
|
+
const rule = new HorizontalRule({ ruleStyle: (text) => theme.fg("border", text) });
|
|
38
|
+
let rows = new Map<number, number>();
|
|
39
|
+
let width = 0;
|
|
40
|
+
let height = 0;
|
|
41
|
+
const clearGeometry = () => {
|
|
42
|
+
rows.clear();
|
|
43
|
+
width = 0;
|
|
44
|
+
height = 0;
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
get focused() {
|
|
48
|
+
return native.focused;
|
|
49
|
+
},
|
|
50
|
+
set focused(value: boolean) {
|
|
51
|
+
native.focused = value;
|
|
52
|
+
},
|
|
53
|
+
render(availableWidth) {
|
|
54
|
+
// Editing and wrapping remain Pi-owned; sanitize its display, not its draft.
|
|
55
|
+
// Pi Editor recurses when a wide grapheme cannot fit its layout.
|
|
56
|
+
// Reserve two native content cells plus its cursor cell, then let Kit
|
|
57
|
+
// clip the safe result to the actual (including zero) screen width.
|
|
58
|
+
const content = renderSafeInput(editor, Math.max(3, availableWidth));
|
|
59
|
+
const cursorRow = content.findIndex((line) => line.includes(CURSOR_MARKER));
|
|
60
|
+
const focusedRow = cursorRow < 0 ? Math.min(1, content.length - 1) : cursorRow;
|
|
61
|
+
const terminalRows = Number.isFinite(tui.terminal.rows) ? Math.floor(tui.terminal.rows) : 24;
|
|
62
|
+
const layout = renderBoundedFrameLayout({
|
|
63
|
+
width: availableWidth,
|
|
64
|
+
maxRows: Math.max(1, terminalRows - 3),
|
|
65
|
+
rule: rule.render(availableWidth)[0] ?? "",
|
|
66
|
+
title: [theme.fg("accent", "Generate Image")],
|
|
67
|
+
context: [theme.fg("muted", "Your prompt is sent to ChatGPT. Provider quota applies.")],
|
|
68
|
+
content,
|
|
69
|
+
focusedRow,
|
|
70
|
+
priorityRows: [focusedRow],
|
|
71
|
+
});
|
|
72
|
+
width = Math.max(0, Math.floor(availableWidth));
|
|
73
|
+
height = layout.lines.length;
|
|
74
|
+
rows = new Map(layout.contentRows.map(({ frameIndex, contentIndex }) => [frameIndex, contentIndex]));
|
|
75
|
+
return layout.lines;
|
|
76
|
+
},
|
|
77
|
+
handleInput(data) {
|
|
78
|
+
if (signal.aborted || isKeyRelease(data)) return;
|
|
79
|
+
clearGeometry();
|
|
80
|
+
if (matchesKey(data, "ctrl+c")) complete({ kind: "close" });
|
|
81
|
+
else if (keybindings.matches(data, "tui.select.cancel")) complete({ kind: "back" });
|
|
82
|
+
else if (!keybindings.matches(data, "app.editor.external")) native.handleInput(data);
|
|
83
|
+
tui.requestRender();
|
|
84
|
+
},
|
|
85
|
+
handleMouse(event) {
|
|
86
|
+
if (signal.aborted || event.x < 0 || event.x >= width || event.y < 0 || event.y >= height) return undefined;
|
|
87
|
+
const y = rows.get(event.y);
|
|
88
|
+
if (y === undefined) return undefined;
|
|
89
|
+
const handled = editor.handleMouse?.({ ...event, y, height: editor.render(Math.max(3, width)).length });
|
|
90
|
+
// Release and synthesized click share the last published frame in Pi.
|
|
91
|
+
if (handled) tui.requestRender();
|
|
92
|
+
return handled;
|
|
93
|
+
},
|
|
94
|
+
invalidate() {
|
|
95
|
+
native.invalidate();
|
|
96
|
+
rule.invalidate();
|
|
97
|
+
clearGeometry();
|
|
98
|
+
},
|
|
99
|
+
dispose() {
|
|
100
|
+
clearGeometry();
|
|
101
|
+
native.focused = false;
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
return result.kind === "completed" ? result.value : { kind: "stale" };
|
|
107
|
+
}
|