@oai404iao/pi-codex-core 0.1.0-alpha.1
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 +28 -0
- package/LICENSES/Apache-2.0.txt +201 -0
- package/LICENSES/OpenAI-Codex-NOTICE.txt +6 -0
- package/README.md +26 -0
- package/THIRD_PARTY_NOTICES.md +18 -0
- package/package.json +84 -0
- package/provenance/openai-codex-eb9dceba-reserved-tools.json +140 -0
- package/src/adapter/compaction/checkpoint.ts +159 -0
- package/src/adapter/compaction/collect.ts +51 -0
- package/src/adapter/compaction/http.ts +101 -0
- package/src/adapter/compaction/request.ts +159 -0
- package/src/adapter/compaction/transport.ts +125 -0
- package/src/adapter/compaction/websocket.ts +119 -0
- package/src/extension/prewarm-snapshot.ts +27 -0
- package/src/extension/provider-runtime.ts +101 -0
- package/src/extension/startup-prewarm.ts +264 -0
- package/src/fast-mode.ts +124 -0
- package/src/index.ts +257 -0
- package/src/native-compaction.ts +392 -0
- package/src/patch/apply.ts +338 -0
- package/src/patch/parser.ts +224 -0
- package/src/patch/render.ts +201 -0
- package/src/provider-native-tools.ts +75 -0
- package/src/providers/codex-apply-patch-tool.ts +23 -0
- package/src/providers/codex-apply-patch.lark +19 -0
- package/src/providers/openai-codex/cache-key.ts +52 -0
- package/src/providers/openai-codex/captured-stream.ts +50 -0
- package/src/providers/openai-codex/constants.ts +61 -0
- package/src/providers/openai-codex/continuation.ts +110 -0
- package/src/providers/openai-codex/errors.ts +130 -0
- package/src/providers/openai-codex/events.ts +123 -0
- package/src/providers/openai-codex/headers.ts +224 -0
- package/src/providers/openai-codex/lite.ts +24 -0
- package/src/providers/openai-codex/message.ts +33 -0
- package/src/providers/openai-codex/prewarm.ts +76 -0
- package/src/providers/openai-codex/proxy.ts +55 -0
- package/src/providers/openai-codex/reasoning.ts +54 -0
- package/src/providers/openai-codex/request-body.ts +149 -0
- package/src/providers/openai-codex/request-context.ts +20 -0
- package/src/providers/openai-codex/request-metadata.ts +137 -0
- package/src/providers/openai-codex/retry.ts +154 -0
- package/src/providers/openai-codex/runtime.ts +1 -0
- package/src/providers/openai-codex/sse.ts +93 -0
- package/src/providers/openai-codex/stream.ts +367 -0
- package/src/providers/openai-codex/urls.ts +24 -0
- package/src/providers/openai-codex/usage.ts +60 -0
- package/src/providers/openai-codex/websocket-connection.ts +216 -0
- package/src/providers/openai-codex/websocket-events.ts +210 -0
- package/src/providers/openai-codex/websocket-session.ts +192 -0
- package/src/providers/openai-codex/websocket-socket.ts +18 -0
- package/src/providers/openai-codex/websocket-stream.ts +151 -0
- package/src/tools/apply-patch.ts +84 -0
- package/src/tools/view-image.ts +98 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { stat, readFile, realpath } from "node:fs/promises";
|
|
2
|
+
import { extname, isAbsolute, relative, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
export type ImageDetail = "auto" | "low" | "high" | "original";
|
|
5
|
+
|
|
6
|
+
export interface ViewImageInput {
|
|
7
|
+
path: string;
|
|
8
|
+
detail?: ImageDetail;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ViewImageOptions {
|
|
12
|
+
workspaceOnly?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ValidatedImage {
|
|
16
|
+
absolutePath: string;
|
|
17
|
+
displayPath: string;
|
|
18
|
+
mimeType: string;
|
|
19
|
+
sizeBytes: number;
|
|
20
|
+
detail: ImageDetail;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const IMAGE_MIME_BY_EXT: Record<string, string> = {
|
|
24
|
+
".png": "image/png",
|
|
25
|
+
".jpg": "image/jpeg",
|
|
26
|
+
".jpeg": "image/jpeg",
|
|
27
|
+
".gif": "image/gif",
|
|
28
|
+
".webp": "image/webp",
|
|
29
|
+
".bmp": "image/bmp",
|
|
30
|
+
".tif": "image/tiff",
|
|
31
|
+
".tiff": "image/tiff",
|
|
32
|
+
".svg": "image/svg+xml",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function assertWithinCwd(absolutePath: string, cwd: string, displayPath: string): void {
|
|
36
|
+
const cwdAbsolute = resolve(cwd);
|
|
37
|
+
const rel = relative(cwdAbsolute, absolutePath);
|
|
38
|
+
if (rel === "" || (!rel.startsWith("..") && !isAbsolute(rel))) return;
|
|
39
|
+
throw new Error(`view_image path escapes the workspace: ${displayPath}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function normalizeImagePath(pathValue: string, cwd: string, options?: ViewImageOptions): { absolutePath: string; displayPath: string } {
|
|
43
|
+
let cleaned = pathValue.trim();
|
|
44
|
+
if ((cleaned.startsWith('"') && cleaned.endsWith('"')) || (cleaned.startsWith("'") && cleaned.endsWith("'"))) cleaned = cleaned.slice(1, -1);
|
|
45
|
+
if (cleaned.startsWith("@")) cleaned = cleaned.slice(1);
|
|
46
|
+
const absolutePath = resolve(cwd, cleaned);
|
|
47
|
+
if (options?.workspaceOnly) assertWithinCwd(absolutePath, cwd, cleaned);
|
|
48
|
+
return { absolutePath, displayPath: cleaned };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function mimeTypeForImagePath(path: string): string | undefined {
|
|
52
|
+
return IMAGE_MIME_BY_EXT[extname(path).toLowerCase()];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function validateImagePath(input: ViewImageInput, cwd: string, options?: ViewImageOptions): Promise<ValidatedImage> {
|
|
56
|
+
if (!input || typeof input.path !== "string" || input.path.trim().length === 0) throw new Error("view_image requires a non-empty path.");
|
|
57
|
+
const detail = input.detail ?? "auto";
|
|
58
|
+
if (!["auto", "low", "high", "original"].includes(detail)) throw new Error(`Unsupported image detail: ${String(input.detail)}`);
|
|
59
|
+
const normalized = normalizeImagePath(input.path, cwd, options);
|
|
60
|
+
let fileStat;
|
|
61
|
+
try {
|
|
62
|
+
fileStat = await stat(normalized.absolutePath);
|
|
63
|
+
} catch {
|
|
64
|
+
throw new Error(`Image not found: ${normalized.displayPath}`);
|
|
65
|
+
}
|
|
66
|
+
if (fileStat.isDirectory()) throw new Error(`view_image expected a file but got a directory: ${normalized.displayPath}`);
|
|
67
|
+
if (!fileStat.isFile()) throw new Error(`view_image expected a regular image file: ${normalized.displayPath}`);
|
|
68
|
+
if (options?.workspaceOnly) {
|
|
69
|
+
try {
|
|
70
|
+
assertWithinCwd(await realpath(normalized.absolutePath), await realpath(cwd), normalized.displayPath);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error instanceof Error && error.message.includes("escapes the workspace")) throw error;
|
|
73
|
+
throw new Error(`Unable to validate image path: ${normalized.displayPath}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const mimeType = mimeTypeForImagePath(normalized.absolutePath);
|
|
77
|
+
if (!mimeType) throw new Error(`Unsupported image file type for view_image: ${normalized.displayPath}`);
|
|
78
|
+
return { ...normalized, detail, mimeType, sizeBytes: fileStat.size };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function viewImage(input: ViewImageInput, cwd: string, options?: ViewImageOptions) {
|
|
82
|
+
const image = await validateImagePath(input, cwd, options);
|
|
83
|
+
const data = await readFile(image.absolutePath, "base64");
|
|
84
|
+
return {
|
|
85
|
+
content: [{ type: "image", data, mimeType: image.mimeType, detail: image.detail }],
|
|
86
|
+
details: image,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const viewImageToolSchema = {
|
|
91
|
+
type: "object",
|
|
92
|
+
additionalProperties: false,
|
|
93
|
+
properties: {
|
|
94
|
+
path: { type: "string", description: "Path to the local image file. Relative paths resolve against ctx.cwd; a leading @ is accepted and stripped." },
|
|
95
|
+
detail: { type: "string", enum: ["auto", "low", "high", "original"], description: "Image detail hint. Defaults to auto." },
|
|
96
|
+
},
|
|
97
|
+
required: ["path"],
|
|
98
|
+
};
|