pi-openai-codex-compat 0.0.2 → 0.0.3
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/CHANGELOG.md +50 -0
- package/README.md +30 -16
- package/extensions/openai-codex-compat/apply-patch.ts +4 -5
- package/extensions/openai-codex-compat/codex-cache-key.ts +9 -0
- package/extensions/openai-codex-compat/codex-protocol.ts +3 -2
- package/extensions/openai-codex-compat/codex-provider.ts +221 -107
- package/extensions/openai-codex-compat/codex-stream.ts +85 -14
- package/extensions/openai-codex-compat/codex-transport.ts +714 -161
- package/extensions/openai-codex-compat/config.ts +2 -2
- package/extensions/openai-codex-compat/image-generation-schema.ts +37 -0
- package/extensions/openai-codex-compat/image-generation.ts +25 -48
- package/extensions/openai-codex-compat/index.ts +7 -0
- package/extensions/openai-codex-compat/provider-error.ts +79 -0
- package/extensions/openai-codex-compat/responses-replay.ts +0 -7
- package/extensions/openai-codex-compat/web-run.ts +7 -0
- package/package.json +2 -1
|
@@ -73,8 +73,8 @@ export const DEFAULT_CONFIG: CodexCompatConfig = {
|
|
|
73
73
|
toolBackground: "subtle",
|
|
74
74
|
imageGeneration: true,
|
|
75
75
|
imageDetail: "auto",
|
|
76
|
-
webRun:
|
|
77
|
-
webSearch: "
|
|
76
|
+
webRun: false,
|
|
77
|
+
webSearch: "disabled",
|
|
78
78
|
textVerbosity: "low",
|
|
79
79
|
reasoningSummary: "auto",
|
|
80
80
|
reasoningMode: "standard",
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
|
|
3
|
+
export const MAX_EDIT_IMAGES = 5;
|
|
4
|
+
|
|
5
|
+
export type ImageGenerationParameters = {
|
|
6
|
+
prompt: string;
|
|
7
|
+
referenced_image_paths?: string[] | null;
|
|
8
|
+
num_last_images_to_include?: number | null;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const REFERENCED_IMAGE_PATH_DESCRIPTION =
|
|
12
|
+
"Absolute path to a local PNG, JPEG, GIF, or WebP image to include in an edit. Convert relative paths to absolute paths before calling the tool; the file must exist and be readable.";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Server-reserved image-generation schema. Range and selector constraints are
|
|
16
|
+
* enforced by the executor because OpenAI rejects additional schema keywords.
|
|
17
|
+
*/
|
|
18
|
+
export const IMAGE_GENERATION_PARAMETERS = Type.Unsafe<ImageGenerationParameters>({
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
num_last_images_to_include: {
|
|
22
|
+
type: ["integer", "null"],
|
|
23
|
+
},
|
|
24
|
+
prompt: {
|
|
25
|
+
type: "string",
|
|
26
|
+
},
|
|
27
|
+
referenced_image_paths: {
|
|
28
|
+
type: ["array", "null"],
|
|
29
|
+
items: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: REFERENCED_IMAGE_PATH_DESCRIPTION,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
required: ["prompt"],
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
});
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import { readFile, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
-
import { dirname, isAbsolute, join } from "node:path";
|
|
2
|
+
import { dirname, isAbsolute, join, normalize } from "node:path";
|
|
3
3
|
import {
|
|
4
4
|
getAgentDir,
|
|
5
5
|
type ExtensionAPI,
|
|
6
6
|
type ExtensionContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import type { Model } from "@earendil-works/pi-ai";
|
|
9
|
-
import { Type } from "typebox";
|
|
10
9
|
import type { CodexCompatConfig } from "./config.ts";
|
|
11
10
|
import type { CodexToolBackgroundResolver } from "./codex-tool-surface.ts";
|
|
12
11
|
import { DEFAULT_CONFIG } from "./config.ts";
|
|
13
12
|
import { isObject, type JsonRecord, type ResponsesItem } from "./codex-protocol.ts";
|
|
14
13
|
import { requestCodexJson, type CodexJsonRequestOptions } from "./codex-transport.ts";
|
|
14
|
+
import {
|
|
15
|
+
IMAGE_GENERATION_PARAMETERS,
|
|
16
|
+
MAX_EDIT_IMAGES,
|
|
17
|
+
type ImageGenerationParameters,
|
|
18
|
+
} from "./image-generation-schema.ts";
|
|
15
19
|
import { IMAGE_GENERATION_TOOL_NAME } from "./namespaced-tools.ts";
|
|
16
20
|
import {
|
|
17
21
|
renderImageGenerationCall,
|
|
@@ -22,55 +26,25 @@ import { isCodexModel } from "./request-options.ts";
|
|
|
22
26
|
import { codexToolAuthentication, codexToolHistory } from "./tool-runtime.ts";
|
|
23
27
|
|
|
24
28
|
const IMAGE_MODEL = "gpt-image-2";
|
|
25
|
-
const MAX_EDIT_IMAGES = 5;
|
|
26
29
|
const GENERATION_ENDPOINT = "images/generations";
|
|
27
30
|
const EDIT_ENDPOINT = "images/edits";
|
|
28
31
|
const GENERATED_IMAGES_DIRECTORY = "generated_images";
|
|
29
32
|
|
|
30
|
-
const IMAGE_GENERATION_DESCRIPTION = `The \`image_gen.imagegen\` tool
|
|
33
|
+
const IMAGE_GENERATION_DESCRIPTION = `The \`image_gen.imagegen\` tool generates new images from descriptions and edits existing images according to specific instructions. Use it when:
|
|
31
34
|
|
|
32
35
|
- The user requests an image based on a scene description, such as a diagram, portrait, comic, meme, or any other visual.
|
|
33
|
-
- The user wants to modify
|
|
36
|
+
- The user wants to modify a local, attached, or previously generated image by adding or removing elements, changing colors, improving quality or resolution, or transforming its style.
|
|
34
37
|
|
|
35
38
|
Guidelines:
|
|
36
|
-
-
|
|
39
|
+
- Call \`image_gen.imagegen\` directly without reconfirmation unless required source images are unavailable.
|
|
37
40
|
- Omit both \`referenced_image_paths\` and \`num_last_images_to_include\` when generating a brand new image.
|
|
38
|
-
- For edits, use \`referenced_image_paths\` when every target image has
|
|
39
|
-
-
|
|
40
|
-
- Use \`num_last_images_to_include\` only when at least one target image has no local file path.
|
|
41
|
-
- Set \`num_last_images_to_include\` to the smallest number of recent conversation images that includes every target image, up to 5.
|
|
41
|
+
- For edits, use \`referenced_image_paths\` when every target image has an absolute local path, with at most 5 paths. Use \`read\` first when you need to inspect a local image.
|
|
42
|
+
- Use \`num_last_images_to_include\` only when at least one target image has no local path, and set it to the smallest number of recent conversation images that includes every target, up to 5.
|
|
42
43
|
- Never provide both \`referenced_image_paths\` and \`num_last_images_to_include\`.
|
|
43
44
|
- If neither mechanism can include every target image, ask the user to attach the missing images again.
|
|
44
|
-
-
|
|
45
|
-
- Always use this tool for image editing unless the user explicitly requests otherwise. Do not use the \`python\` tool for image editing unless specifically instructed.
|
|
45
|
+
- Generated images are returned, displayed, and saved automatically. Do not embed the image in the final response unless the user asks.
|
|
46
46
|
`;
|
|
47
47
|
|
|
48
|
-
const imageGenerationParameters = Type.Unsafe<{
|
|
49
|
-
prompt: string;
|
|
50
|
-
referenced_image_paths?: string[] | null;
|
|
51
|
-
num_last_images_to_include?: number | null;
|
|
52
|
-
}>({
|
|
53
|
-
type: "object",
|
|
54
|
-
properties: {
|
|
55
|
-
num_last_images_to_include: {
|
|
56
|
-
type: ["integer", "null"],
|
|
57
|
-
},
|
|
58
|
-
prompt: {
|
|
59
|
-
type: "string",
|
|
60
|
-
},
|
|
61
|
-
referenced_image_paths: {
|
|
62
|
-
type: ["array", "null"],
|
|
63
|
-
items: {
|
|
64
|
-
type: "string",
|
|
65
|
-
description:
|
|
66
|
-
"A path that is guaranteed to be absolute and normalized (though it is not guaranteed to be canonicalized or exist on the filesystem).\n\nIMPORTANT: When deserializing an `AbsolutePathBuf`, a base path must be set using [AbsolutePathBufGuard::new]. If no base path is set, the deserialization will fail unless the path being deserialized is already absolute.",
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
required: ["prompt"],
|
|
71
|
-
additionalProperties: false,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
48
|
export type { ImageGenerationDetails } from "./image-generation-render.ts";
|
|
75
49
|
|
|
76
50
|
type JsonRequester = (
|
|
@@ -137,12 +111,12 @@ export function recentImageUrls(history: readonly ResponsesItem[], count: number
|
|
|
137
111
|
return newestFirst.reverse();
|
|
138
112
|
}
|
|
139
113
|
|
|
140
|
-
function normalizeImagePath(path: string): string {
|
|
141
|
-
const
|
|
142
|
-
if (!isAbsolute(
|
|
114
|
+
export function normalizeImagePath(path: string): string {
|
|
115
|
+
const unprefixed = path.startsWith("@") ? path.slice(1) : path;
|
|
116
|
+
if (!isAbsolute(unprefixed)) {
|
|
143
117
|
throw new Error(`referenced image path must be absolute: ${path}`);
|
|
144
118
|
}
|
|
145
|
-
return
|
|
119
|
+
return normalize(unprefixed);
|
|
146
120
|
}
|
|
147
121
|
|
|
148
122
|
function imageMimeType(bytes: Uint8Array, path: string): string {
|
|
@@ -176,11 +150,7 @@ async function localImageUrl(path: string): Promise<string> {
|
|
|
176
150
|
}
|
|
177
151
|
|
|
178
152
|
async function imageRequest(
|
|
179
|
-
params:
|
|
180
|
-
prompt: string;
|
|
181
|
-
referenced_image_paths?: string[] | null;
|
|
182
|
-
num_last_images_to_include?: number | null;
|
|
183
|
-
},
|
|
153
|
+
params: ImageGenerationParameters,
|
|
184
154
|
history: readonly ResponsesItem[],
|
|
185
155
|
): Promise<ImageRequest> {
|
|
186
156
|
const paths = params.referenced_image_paths ?? [];
|
|
@@ -290,7 +260,14 @@ export default function registerImageGeneration(
|
|
|
290
260
|
name: IMAGE_GENERATION_TOOL_NAME,
|
|
291
261
|
label: IMAGE_GENERATION_TOOL_NAME,
|
|
292
262
|
description: IMAGE_GENERATION_DESCRIPTION,
|
|
293
|
-
|
|
263
|
+
promptSnippet: "Generate new images or edit existing images",
|
|
264
|
+
promptGuidelines: [
|
|
265
|
+
"Use image_gen.imagegen directly to generate new images or edit existing images without reconfirmation unless required source images are unavailable.",
|
|
266
|
+
"For new images, call image_gen.imagegen without referenced_image_paths or num_last_images_to_include.",
|
|
267
|
+
"For image_gen.imagegen edits, use up to five absolute referenced_image_paths when every target is local; otherwise use num_last_images_to_include from 1 to 5, and use read when you need to inspect a local image.",
|
|
268
|
+
"Never pass both image selectors to image_gen.imagegen; ask the user to reattach images when every target cannot be referenced.",
|
|
269
|
+
],
|
|
270
|
+
parameters: IMAGE_GENERATION_PARAMETERS,
|
|
294
271
|
executionMode: "sequential",
|
|
295
272
|
renderShell: "self",
|
|
296
273
|
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
@@ -13,6 +13,13 @@ import registerCodexRequestOptions from "./request-options.ts";
|
|
|
13
13
|
import registerCodexSettings from "./settings-pane.ts";
|
|
14
14
|
import registerCodexTools, { syncCodexTools } from "./tools.ts";
|
|
15
15
|
|
|
16
|
+
export {
|
|
17
|
+
closeOpenAICodexWebSocketSessions,
|
|
18
|
+
getOpenAICodexWebSocketDebugStats,
|
|
19
|
+
resetOpenAICodexWebSocketDebugStats,
|
|
20
|
+
type OpenAICodexWebSocketDebugStats,
|
|
21
|
+
} from "./codex-transport.ts";
|
|
22
|
+
|
|
16
23
|
const DISPLAY_NAME = "OpenAI Codex Compat";
|
|
17
24
|
|
|
18
25
|
function settingsSummary(ctx: ExtensionContext, config: CodexCompatConfig): string {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const MAX_PROVIDER_ERROR_BODY_CHARS = 4_000;
|
|
2
|
+
|
|
3
|
+
type ProviderErrorShape = Error & {
|
|
4
|
+
statusCode?: unknown;
|
|
5
|
+
status?: unknown;
|
|
6
|
+
body?: unknown;
|
|
7
|
+
error?: unknown;
|
|
8
|
+
$metadata?: { httpStatusCode?: unknown };
|
|
9
|
+
$response?: { statusCode?: unknown; body?: unknown };
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function safeJsonStringify(value: unknown): string {
|
|
13
|
+
try {
|
|
14
|
+
const serialized = JSON.stringify(value);
|
|
15
|
+
return serialized === undefined ? String(value) : serialized;
|
|
16
|
+
} catch {
|
|
17
|
+
return String(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function truncateErrorText(text: string): string {
|
|
22
|
+
return text.length <= MAX_PROVIDER_ERROR_BODY_CHARS
|
|
23
|
+
? text
|
|
24
|
+
: `${text.slice(0, MAX_PROVIDER_ERROR_BODY_CHARS)}... [truncated ${
|
|
25
|
+
text.length - MAX_PROVIDER_ERROR_BODY_CHARS
|
|
26
|
+
} chars]`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isPlainNonEmptyObject(value: unknown): value is Record<string, unknown> {
|
|
30
|
+
if (typeof value !== "object" || value === null) return false;
|
|
31
|
+
const prototype = Object.getPrototypeOf(value);
|
|
32
|
+
return (prototype === Object.prototype || prototype === null) && Object.keys(value).length > 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isReadableStreamLike(value: unknown): boolean {
|
|
36
|
+
return (
|
|
37
|
+
typeof value === "object" &&
|
|
38
|
+
value !== null &&
|
|
39
|
+
"pipe" in value &&
|
|
40
|
+
typeof value.pipe === "function"
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function errorStatus(error: ProviderErrorShape): number | undefined {
|
|
45
|
+
if (typeof error.statusCode === "number") return error.statusCode;
|
|
46
|
+
if (typeof error.status === "number") return error.status;
|
|
47
|
+
if (typeof error.$metadata?.httpStatusCode === "number") {
|
|
48
|
+
return error.$metadata.httpStatusCode;
|
|
49
|
+
}
|
|
50
|
+
if (typeof error.$response?.statusCode === "number") return error.$response.statusCode;
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function errorBody(error: ProviderErrorShape): string | undefined {
|
|
55
|
+
let body: string | undefined;
|
|
56
|
+
if (typeof error.body === "string") body = error.body;
|
|
57
|
+
else if (isPlainNonEmptyObject(error.error)) body = safeJsonStringify(error.error);
|
|
58
|
+
else if (typeof error.$response?.body === "string") body = error.$response.body;
|
|
59
|
+
else if (
|
|
60
|
+
!isReadableStreamLike(error.$response?.body) &&
|
|
61
|
+
isPlainNonEmptyObject(error.$response?.body)
|
|
62
|
+
) {
|
|
63
|
+
body = safeJsonStringify(error.$response.body);
|
|
64
|
+
}
|
|
65
|
+
const trimmed = body?.trim();
|
|
66
|
+
return trimmed ? truncateErrorText(trimmed) : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Match Pi AI's provider error normalization without importing a private package subpath. */
|
|
70
|
+
export function formatProviderError(error: unknown): string {
|
|
71
|
+
if (!(error instanceof Error)) return safeJsonStringify(error);
|
|
72
|
+
const providerError = error as ProviderErrorShape;
|
|
73
|
+
const status = errorStatus(providerError);
|
|
74
|
+
const body = errorBody(providerError);
|
|
75
|
+
if (status !== undefined && body !== undefined && !error.message.includes(body)) {
|
|
76
|
+
return `${status}: ${body}`;
|
|
77
|
+
}
|
|
78
|
+
return error.message;
|
|
79
|
+
}
|
|
@@ -24,10 +24,3 @@ export function normalizeReplayItem(item: JsonRecord): JsonRecord {
|
|
|
24
24
|
}
|
|
25
25
|
return normalized;
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
export function replayItemsEqual(
|
|
29
|
-
left: readonly JsonRecord[] | undefined,
|
|
30
|
-
right: readonly JsonRecord[] | undefined,
|
|
31
|
-
): boolean {
|
|
32
|
-
return stableResponsesJson(left ?? []) === stableResponsesJson(right ?? []);
|
|
33
|
-
}
|
|
@@ -116,6 +116,13 @@ export default function registerWebRun(
|
|
|
116
116
|
name: WEB_RUN_TOOL_NAME,
|
|
117
117
|
label: WEB_RUN_TOOL_NAME,
|
|
118
118
|
description: WEB_RUN_DESCRIPTION,
|
|
119
|
+
promptSnippet: "Search and browse the internet",
|
|
120
|
+
promptGuidelines: [
|
|
121
|
+
"Use `web.run` when the user explicitly asks to browse or when answering requires current, niche, high-stakes, or precisely sourced information, including recommendations that may change over time.",
|
|
122
|
+
'Batch independent `web.run` operations in one call, pass only required parameters, and keep `search_query` to at most four queries; use `response_length: "medium"` or `"long"` when sending four.',
|
|
123
|
+
"For technical research, prefer primary sources; for OpenAI product questions, inspect local code first and restrict fallback browsing to official OpenAI sites.",
|
|
124
|
+
"Cite supported claims with direct Markdown links near the relevant text, never expose internal reference IDs, and respect the description's quotation and source word limits.",
|
|
125
|
+
],
|
|
119
126
|
parameters: WEB_RUN_PARAMETERS,
|
|
120
127
|
executionMode: "parallel",
|
|
121
128
|
renderShell: "self",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-openai-codex-compat",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "OpenAI Codex compatibility for Pi with native compaction, fast mode, and Codex-optimized capabilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"check": "hk check --all --check",
|
|
33
33
|
"test": "node --test --test-concurrency=1 test/*.test.ts",
|
|
34
|
+
"test:live:codex": "PI_CODEX_LIVE_TEST=1 node --test --test-concurrency=1 test/codex-transport.live.test.ts",
|
|
34
35
|
"fmt": "oxfmt",
|
|
35
36
|
"lint": "oxlint",
|
|
36
37
|
"lint:fix": "oxlint --fix"
|