pi-openai-toolkit 0.14.3 → 0.14.5
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 +135 -175
- package/README.zh.md +135 -175
- package/docs/internals.md +73 -0
- package/extensions/auto-mode.ts +1 -1
- package/extensions/codex-astra.ts +1 -1
- package/extensions/compaction.ts +1 -1
- package/extensions/image-generation.ts +1 -1
- package/extensions/web-search.ts +1 -1
- package/package.json +127 -126
- package/src/config.ts +621 -582
- package/src/context-management/codex-provider.ts +260 -262
- package/src/context-management/history-notes.ts +325 -319
- package/src/context-management/tools.ts +5 -3
- package/src/context-management/types.ts +161 -160
- package/src/extension-runtime.ts +9 -5
- package/src/image-generation/extension.ts +19 -2
- package/src/image-generation/protocol.ts +473 -405
- package/src/image-generation/service.ts +249 -243
- package/src/image-generation/types.ts +189 -184
- package/src/responses-headers.ts +108 -108
- package/src/runtime.ts +477 -477
- package/src/types.ts +18 -0
- package/src/web-search/prompt.ts +1 -1
|
@@ -1,184 +1,189 @@
|
|
|
1
|
-
export const IMAGE_GENERATION_TOOL_NAME = "openai_generate_image";
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
4
|
-
|
|
5
|
-
"openai-responses",
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
] as const;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export const
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
22
|
-
export const
|
|
23
|
-
export const
|
|
24
|
-
export const
|
|
25
|
-
export const
|
|
26
|
-
|
|
27
|
-
export const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export type
|
|
31
|
-
export type
|
|
32
|
-
export type
|
|
33
|
-
export type
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export type
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
| "
|
|
102
|
-
| "
|
|
103
|
-
| "
|
|
104
|
-
| "
|
|
105
|
-
| "
|
|
106
|
-
| "
|
|
107
|
-
| "
|
|
108
|
-
| "
|
|
109
|
-
| "
|
|
110
|
-
| "
|
|
111
|
-
| "
|
|
112
|
-
| "
|
|
113
|
-
| "
|
|
114
|
-
| "
|
|
115
|
-
| "
|
|
116
|
-
| "
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
value.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
value.
|
|
149
|
-
value.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
value.
|
|
153
|
-
value.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
value.
|
|
157
|
-
value.
|
|
158
|
-
|
|
159
|
-
typeof value.
|
|
160
|
-
Number.isInteger(value.
|
|
161
|
-
value.
|
|
162
|
-
value.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
)
|
|
178
|
-
.replace(/\
|
|
179
|
-
.replace(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
1
|
+
export const IMAGE_GENERATION_TOOL_NAME = "openai_generate_image";
|
|
2
|
+
export const IMAGE_GENERATION_MIME_TYPE = "image/png" as const;
|
|
3
|
+
export const IMAGE_GENERATION_CAPABLE_APIS = [
|
|
4
|
+
"openai-responses",
|
|
5
|
+
"openai-codex-responses",
|
|
6
|
+
] as const;
|
|
7
|
+
export const IMAGE_GENERATION_SIZES = [
|
|
8
|
+
"auto",
|
|
9
|
+
"1024x1024",
|
|
10
|
+
"1536x1024",
|
|
11
|
+
"1024x1536",
|
|
12
|
+
] as const;
|
|
13
|
+
export const IMAGE_GENERATION_QUALITIES = ["auto", "low", "medium", "high"] as const;
|
|
14
|
+
|
|
15
|
+
export const MAX_IMAGE_PROMPT_CHARS = 20_000;
|
|
16
|
+
export const MAX_REFERENCE_IMAGE_COUNT = 5;
|
|
17
|
+
export const MAX_REFERENCE_IMAGE_BYTES = 20 * 1024 * 1024;
|
|
18
|
+
export const MAX_TOTAL_REFERENCE_IMAGE_BYTES = 50 * 1024 * 1024;
|
|
19
|
+
export const MAX_GENERATED_IMAGE_BYTES = 32 * 1024 * 1024;
|
|
20
|
+
export const MAX_IMAGE_RESPONSE_BYTES = 48 * 1024 * 1024;
|
|
21
|
+
export const MAX_IMAGE_ERROR_BYTES = 64 * 1024;
|
|
22
|
+
export const IMAGE_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
|
|
23
|
+
export const MAX_IMAGE_PATH_CHARS = 4096;
|
|
24
|
+
export const MAX_IMAGE_DIAGNOSTIC_CHARS = 4096;
|
|
25
|
+
export const MAX_IMAGE_IDENTIFIER_CHARS = 256;
|
|
26
|
+
/** Upper bound for a configured or requested `image_generation` model id. */
|
|
27
|
+
export const MAX_IMAGE_MODEL_ID_CHARS = 256;
|
|
28
|
+
export const MAX_IMAGE_DIMENSION = 100_000;
|
|
29
|
+
|
|
30
|
+
export type ImageGenerationCapableApi = (typeof IMAGE_GENERATION_CAPABLE_APIS)[number];
|
|
31
|
+
export type ImageGenerationSize = (typeof IMAGE_GENERATION_SIZES)[number];
|
|
32
|
+
export type ImageGenerationQuality = (typeof IMAGE_GENERATION_QUALITIES)[number];
|
|
33
|
+
export type ImageGenerationAction = "generate" | "edit";
|
|
34
|
+
export type ReferenceImageMimeType = "image/png" | "image/jpeg" | "image/webp";
|
|
35
|
+
|
|
36
|
+
export type ImageGenerationModel = {
|
|
37
|
+
api?: string;
|
|
38
|
+
provider?: string;
|
|
39
|
+
id?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type GenerateImageParams = {
|
|
43
|
+
prompt: string;
|
|
44
|
+
referenceImagePaths?: string[] | null;
|
|
45
|
+
outputPath?: string | null;
|
|
46
|
+
size?: ImageGenerationSize;
|
|
47
|
+
quality?: ImageGenerationQuality;
|
|
48
|
+
/** Optional bare image model id that must exist in `imageGeneration.models`. */
|
|
49
|
+
model?: string | null;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export type NormalizedGenerateImageParams = {
|
|
53
|
+
prompt: string;
|
|
54
|
+
referenceImagePaths: string[];
|
|
55
|
+
outputPath?: string;
|
|
56
|
+
size: ImageGenerationSize;
|
|
57
|
+
quality: ImageGenerationQuality;
|
|
58
|
+
action: ImageGenerationAction;
|
|
59
|
+
/** Trimmed requested model id, or undefined when the caller omitted it. */
|
|
60
|
+
model?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type PreparedReferenceImage = {
|
|
64
|
+
path: string;
|
|
65
|
+
mimeType: ReferenceImageMimeType;
|
|
66
|
+
bytes: Buffer;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export type ParsedGeneratedImage = {
|
|
70
|
+
bytes: Buffer;
|
|
71
|
+
imageCallId: string;
|
|
72
|
+
responseId?: string;
|
|
73
|
+
revisedPrompt?: string;
|
|
74
|
+
width: number;
|
|
75
|
+
height: number;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type ImageGenerationDetails = {
|
|
79
|
+
artifactPath: string;
|
|
80
|
+
outputPath?: string;
|
|
81
|
+
routingModel: string;
|
|
82
|
+
imageModel: string;
|
|
83
|
+
imageCallId: string;
|
|
84
|
+
responseId?: string;
|
|
85
|
+
mimeType: typeof IMAGE_GENERATION_MIME_TYPE;
|
|
86
|
+
byteCount: number;
|
|
87
|
+
width: number;
|
|
88
|
+
height: number;
|
|
89
|
+
edited: boolean;
|
|
90
|
+
referenceCount: number;
|
|
91
|
+
revisedPrompt?: string;
|
|
92
|
+
warning?: string;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export type ImageGenerationExecutionResult = {
|
|
96
|
+
details: ImageGenerationDetails;
|
|
97
|
+
text: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type ImageGenerationFailureCode =
|
|
101
|
+
| "aborted"
|
|
102
|
+
| "timeout"
|
|
103
|
+
| "disabled"
|
|
104
|
+
| "unsupported-model"
|
|
105
|
+
| "missing-runtime"
|
|
106
|
+
| "invalid-parameters"
|
|
107
|
+
| "reference-input-invalid"
|
|
108
|
+
| "reference-upload-approval-required"
|
|
109
|
+
| "reference-upload-declined"
|
|
110
|
+
| "output-path-invalid"
|
|
111
|
+
| "output-path-approval-required"
|
|
112
|
+
| "output-path-declined"
|
|
113
|
+
| "authentication"
|
|
114
|
+
| "rate-limit"
|
|
115
|
+
| "request-rejected"
|
|
116
|
+
| "backend-unavailable"
|
|
117
|
+
| "network"
|
|
118
|
+
| "oversized-response"
|
|
119
|
+
| "malformed-response"
|
|
120
|
+
| "no-image"
|
|
121
|
+
| "artifact-write-failed";
|
|
122
|
+
|
|
123
|
+
export class ImageGenerationError extends Error {
|
|
124
|
+
readonly code: ImageGenerationFailureCode;
|
|
125
|
+
|
|
126
|
+
constructor(code: ImageGenerationFailureCode, message: string) {
|
|
127
|
+
super(message);
|
|
128
|
+
this.name = "ImageGenerationError";
|
|
129
|
+
this.code = code;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
134
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isBoundedString(value: unknown, maxChars: number): value is string {
|
|
138
|
+
return typeof value === "string" && value.trim().length > 0 && value.length <= maxChars;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function isImageGenerationDetails(value: unknown): value is ImageGenerationDetails {
|
|
142
|
+
if (!isRecord(value)) return false;
|
|
143
|
+
return (
|
|
144
|
+
isBoundedString(value.artifactPath, MAX_IMAGE_PATH_CHARS) &&
|
|
145
|
+
(value.outputPath === undefined || isBoundedString(value.outputPath, MAX_IMAGE_PATH_CHARS)) &&
|
|
146
|
+
isBoundedString(value.routingModel, MAX_IMAGE_DIAGNOSTIC_CHARS) &&
|
|
147
|
+
isBoundedString(value.imageModel, MAX_IMAGE_MODEL_ID_CHARS) &&
|
|
148
|
+
isBoundedString(value.imageCallId, MAX_IMAGE_IDENTIFIER_CHARS) &&
|
|
149
|
+
(value.responseId === undefined || isBoundedString(value.responseId, MAX_IMAGE_IDENTIFIER_CHARS)) &&
|
|
150
|
+
value.mimeType === IMAGE_GENERATION_MIME_TYPE &&
|
|
151
|
+
typeof value.byteCount === "number" &&
|
|
152
|
+
Number.isInteger(value.byteCount) &&
|
|
153
|
+
value.byteCount > 0 &&
|
|
154
|
+
value.byteCount <= MAX_GENERATED_IMAGE_BYTES &&
|
|
155
|
+
typeof value.width === "number" &&
|
|
156
|
+
Number.isInteger(value.width) &&
|
|
157
|
+
value.width > 0 &&
|
|
158
|
+
value.width <= MAX_IMAGE_DIMENSION &&
|
|
159
|
+
typeof value.height === "number" &&
|
|
160
|
+
Number.isInteger(value.height) &&
|
|
161
|
+
value.height > 0 &&
|
|
162
|
+
value.height <= MAX_IMAGE_DIMENSION &&
|
|
163
|
+
typeof value.edited === "boolean" &&
|
|
164
|
+
typeof value.referenceCount === "number" &&
|
|
165
|
+
Number.isInteger(value.referenceCount) &&
|
|
166
|
+
value.referenceCount >= 0 &&
|
|
167
|
+
value.referenceCount <= MAX_REFERENCE_IMAGE_COUNT &&
|
|
168
|
+
(value.revisedPrompt === undefined || isBoundedString(value.revisedPrompt, MAX_IMAGE_DIAGNOSTIC_CHARS)) &&
|
|
169
|
+
(value.warning === undefined || isBoundedString(value.warning, MAX_IMAGE_DIAGNOSTIC_CHARS))
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function sanitizeImageDiagnostic(value: unknown, fallback: string): string {
|
|
174
|
+
if (typeof value !== "string") return fallback;
|
|
175
|
+
const normalized = value
|
|
176
|
+
.replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/gi, "[REDACTED_IMAGE_DATA]")
|
|
177
|
+
.replace(/[A-Za-z0-9+/]{128,}={0,2}/g, "[REDACTED_BASE64]")
|
|
178
|
+
.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [REDACTED]")
|
|
179
|
+
.replace(
|
|
180
|
+
/\b(authorization|api[-_ ]?key|access[-_ ]?token|refresh[-_ ]?token|account[-_ ]?id|cookie)\s*[:=]\s*[^\s,;]+/gi,
|
|
181
|
+
"$1: [REDACTED]",
|
|
182
|
+
)
|
|
183
|
+
.replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "[REDACTED]")
|
|
184
|
+
.replace(/[\u0000-\u001f\u007f]+/g, " ")
|
|
185
|
+
.replace(/\s+/g, " ")
|
|
186
|
+
.trim();
|
|
187
|
+
if (!normalized) return fallback;
|
|
188
|
+
return normalized.slice(0, MAX_IMAGE_DIAGNOSTIC_CHARS);
|
|
189
|
+
}
|
package/src/responses-headers.ts
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import type { ProviderHeaders } from "@earendil-works/pi-ai";
|
|
2
|
-
import { mergeProviderHeaders } from "./provider-headers";
|
|
3
|
-
|
|
4
|
-
export type ResponsesHeaderRuntime = {
|
|
5
|
-
api: string;
|
|
6
|
-
apiKey: string;
|
|
7
|
-
headers?: ProviderHeaders;
|
|
8
|
-
sessionId?: string;
|
|
9
|
-
currentModel: {
|
|
10
|
-
headers?: ProviderHeaders;
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
15
|
-
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
|
|
19
|
-
const parts = token.split(".");
|
|
20
|
-
if (parts.length !== 3) return undefined;
|
|
21
|
-
try {
|
|
22
|
-
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8")) as unknown;
|
|
23
|
-
return isRecord(payload) ? payload : undefined;
|
|
24
|
-
} catch {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function extractCodexAccountId(token: string): string | undefined {
|
|
30
|
-
const authClaims = decodeJwtPayload(token)?.["https://api.openai.com/auth"];
|
|
31
|
-
if (!isRecord(authClaims)) return undefined;
|
|
32
|
-
const accountId = authClaims.chatgpt_account_id;
|
|
33
|
-
return typeof accountId === "string" && accountId.trim() ? accountId.trim() : undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function buildCodexUserAgent(): string {
|
|
37
|
-
const platform = typeof process !== "undefined" ? process.platform : "browser";
|
|
38
|
-
const arch = typeof process !== "undefined" ? process.arch : "unknown";
|
|
39
|
-
return `pi (${platform}; ${arch})`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The Codex backend version-gates model availability on the `version` header
|
|
44
|
-
* (the pinned @openai/codex client version). `gpt-6-astra` requires >= 0.153.0;
|
|
45
|
-
* an older or absent pin makes the SKU invisible to discovery and rejects
|
|
46
|
-
* requests that name it. Keep this in sync with the newest gate Oh My Pi pins
|
|
47
|
-
* in @oh-my-pi/pi-wire/codex (`CODEX_CLIENT_VERSION`).
|
|
48
|
-
*/
|
|
49
|
-
export const CODEX_CLIENT_VERSION = "0.153.0";
|
|
50
|
-
|
|
51
|
-
/** Prompt cache keys are clamped by the same host rule (`OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH`). */
|
|
52
|
-
const CODEX_SESSION_ID_MAX_LENGTH = 64;
|
|
53
|
-
|
|
54
|
-
export function clampCodexSessionId(sessionId: string | undefined): string | undefined {
|
|
55
|
-
if (!sessionId) return undefined;
|
|
56
|
-
const chars = Array.from(sessionId);
|
|
57
|
-
return chars.length <= CODEX_SESSION_ID_MAX_LENGTH ? sessionId : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export type ResponsesHeaderOptions = {
|
|
61
|
-
accept: string;
|
|
62
|
-
contentType?: string;
|
|
63
|
-
/** Conversation identity for Codex cache-affinity headers; clamped to 64 chars here. */
|
|
64
|
-
sessionId?: string;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export function buildResponsesRequestHeaders(
|
|
68
|
-
runtime: ResponsesHeaderRuntime,
|
|
69
|
-
options: ResponsesHeaderOptions,
|
|
70
|
-
): Headers {
|
|
71
|
-
const headers = new Headers(
|
|
72
|
-
mergeProviderHeaders(runtime.currentModel.headers, runtime.headers),
|
|
73
|
-
);
|
|
74
|
-
headers.set("accept", options.accept);
|
|
75
|
-
headers.set("content-type", options.contentType ?? "application/json");
|
|
76
|
-
if (!headers.has("authorization")) {
|
|
77
|
-
headers.set("authorization", `Bearer ${runtime.apiKey}`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (runtime.api === "openai-codex-responses") {
|
|
81
|
-
const accountId = extractCodexAccountId(runtime.apiKey);
|
|
82
|
-
if (accountId) headers.set("chatgpt-account-id", accountId);
|
|
83
|
-
headers.set("originator", "pi");
|
|
84
|
-
headers.set("user-agent", buildCodexUserAgent());
|
|
85
|
-
headers.set("openai-beta", "responses=experimental");
|
|
86
|
-
// Codex backend gating: a request without a new-enough `version` never
|
|
87
|
-
// reaches model SKUs that arrived behind the gate (gpt-6-astra).
|
|
88
|
-
headers.set("version", CODEX_CLIENT_VERSION);
|
|
89
|
-
// Prompt-cache affinity rides on the conversation identity: the backend
|
|
90
|
-
// keeps a conversation's cache prefix on the node that wrote it, so our
|
|
91
|
-
// synthetic compaction requests must present the same id the live
|
|
92
|
-
// transcript requests used, or every compact pays a full cache miss.
|
|
93
|
-
const sessionId = clampCodexSessionId(options.sessionId ?? runtime.sessionId);
|
|
94
|
-
if (sessionId) {
|
|
95
|
-
// Header names match the installed host's live-request builder
|
|
96
|
-
// (`buildSSEHeaders`: `session-id` + `x-client-request-id`) so our
|
|
97
|
-
// synthetic requests land in the same conversation affinity group.
|
|
98
|
-
headers.set("session-id", sessionId);
|
|
99
|
-
headers.set("x-client-request-id", sessionId);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return headers;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export const _responsesHeadersTest = {
|
|
107
|
-
extractCodexAccountId,
|
|
108
|
-
};
|
|
1
|
+
import type { ProviderHeaders } from "@earendil-works/pi-ai";
|
|
2
|
+
import { mergeProviderHeaders } from "./provider-headers";
|
|
3
|
+
|
|
4
|
+
export type ResponsesHeaderRuntime = {
|
|
5
|
+
api: string;
|
|
6
|
+
apiKey: string;
|
|
7
|
+
headers?: ProviderHeaders;
|
|
8
|
+
sessionId?: string;
|
|
9
|
+
currentModel: {
|
|
10
|
+
headers?: ProviderHeaders;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
15
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
|
|
19
|
+
const parts = token.split(".");
|
|
20
|
+
if (parts.length !== 3) return undefined;
|
|
21
|
+
try {
|
|
22
|
+
const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8")) as unknown;
|
|
23
|
+
return isRecord(payload) ? payload : undefined;
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function extractCodexAccountId(token: string): string | undefined {
|
|
30
|
+
const authClaims = decodeJwtPayload(token)?.["https://api.openai.com/auth"];
|
|
31
|
+
if (!isRecord(authClaims)) return undefined;
|
|
32
|
+
const accountId = authClaims.chatgpt_account_id;
|
|
33
|
+
return typeof accountId === "string" && accountId.trim() ? accountId.trim() : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function buildCodexUserAgent(): string {
|
|
37
|
+
const platform = typeof process !== "undefined" ? process.platform : "browser";
|
|
38
|
+
const arch = typeof process !== "undefined" ? process.arch : "unknown";
|
|
39
|
+
return `pi (${platform}; ${arch})`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The Codex backend version-gates model availability on the `version` header
|
|
44
|
+
* (the pinned @openai/codex client version). `gpt-6-astra` requires >= 0.153.0;
|
|
45
|
+
* an older or absent pin makes the SKU invisible to discovery and rejects
|
|
46
|
+
* requests that name it. Keep this in sync with the newest gate Oh My Pi pins
|
|
47
|
+
* in @oh-my-pi/pi-wire/codex (`CODEX_CLIENT_VERSION`).
|
|
48
|
+
*/
|
|
49
|
+
export const CODEX_CLIENT_VERSION = "0.153.0";
|
|
50
|
+
|
|
51
|
+
/** Prompt cache keys are clamped by the same host rule (`OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH`). */
|
|
52
|
+
const CODEX_SESSION_ID_MAX_LENGTH = 64;
|
|
53
|
+
|
|
54
|
+
export function clampCodexSessionId(sessionId: string | undefined): string | undefined {
|
|
55
|
+
if (!sessionId) return undefined;
|
|
56
|
+
const chars = Array.from(sessionId);
|
|
57
|
+
return chars.length <= CODEX_SESSION_ID_MAX_LENGTH ? sessionId : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type ResponsesHeaderOptions = {
|
|
61
|
+
accept: string;
|
|
62
|
+
contentType?: string;
|
|
63
|
+
/** Conversation identity for Codex cache-affinity headers; clamped to 64 chars here. */
|
|
64
|
+
sessionId?: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export function buildResponsesRequestHeaders(
|
|
68
|
+
runtime: ResponsesHeaderRuntime,
|
|
69
|
+
options: ResponsesHeaderOptions,
|
|
70
|
+
): Headers {
|
|
71
|
+
const headers = new Headers(
|
|
72
|
+
mergeProviderHeaders(runtime.currentModel.headers, runtime.headers),
|
|
73
|
+
);
|
|
74
|
+
headers.set("accept", options.accept);
|
|
75
|
+
headers.set("content-type", options.contentType ?? "application/json");
|
|
76
|
+
if (!headers.has("authorization")) {
|
|
77
|
+
headers.set("authorization", `Bearer ${runtime.apiKey}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (runtime.api === "openai-codex-responses") {
|
|
81
|
+
const accountId = extractCodexAccountId(runtime.apiKey);
|
|
82
|
+
if (accountId) headers.set("chatgpt-account-id", accountId);
|
|
83
|
+
headers.set("originator", "pi");
|
|
84
|
+
headers.set("user-agent", buildCodexUserAgent());
|
|
85
|
+
headers.set("openai-beta", "responses=experimental");
|
|
86
|
+
// Codex backend gating: a request without a new-enough `version` never
|
|
87
|
+
// reaches model SKUs that arrived behind the gate (gpt-6-astra).
|
|
88
|
+
headers.set("version", CODEX_CLIENT_VERSION);
|
|
89
|
+
// Prompt-cache affinity rides on the conversation identity: the backend
|
|
90
|
+
// keeps a conversation's cache prefix on the node that wrote it, so our
|
|
91
|
+
// synthetic compaction requests must present the same id the live
|
|
92
|
+
// transcript requests used, or every compact pays a full cache miss.
|
|
93
|
+
const sessionId = clampCodexSessionId(options.sessionId ?? runtime.sessionId);
|
|
94
|
+
if (sessionId) {
|
|
95
|
+
// Header names match the installed host's live-request builder
|
|
96
|
+
// (`buildSSEHeaders`: `session-id` + `x-client-request-id`) so our
|
|
97
|
+
// synthetic requests land in the same conversation affinity group.
|
|
98
|
+
headers.set("session-id", sessionId);
|
|
99
|
+
headers.set("x-client-request-id", sessionId);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return headers;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export const _responsesHeadersTest = {
|
|
107
|
+
extractCodexAccountId,
|
|
108
|
+
};
|