pi-openai-toolkit 0.24.1 → 0.24.2

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.
Files changed (41) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +1 -1
  4. package/README.zh.md +1 -1
  5. package/config.schema.json +264 -264
  6. package/docs/configuration.md +304 -304
  7. package/docs/internals.md +3 -1
  8. package/package.json +123 -123
  9. package/src/codex-fast/extension.ts +1 -1
  10. package/src/compact-client.ts +412 -412
  11. package/src/compaction-projection.ts +35 -35
  12. package/src/config/legacy.ts +469 -469
  13. package/src/config/migration.ts +115 -115
  14. package/src/config/notifications.ts +46 -46
  15. package/src/config/policy.ts +115 -115
  16. package/src/config/v2.ts +1128 -1128
  17. package/src/config.ts +320 -320
  18. package/src/debug.ts +238 -238
  19. package/src/deferred-tool-carryover.ts +282 -282
  20. package/src/details-store.ts +151 -151
  21. package/src/extension-runtime.ts +1095 -1095
  22. package/src/image-generation/extension.ts +189 -189
  23. package/src/image-generation/protocol.ts +579 -579
  24. package/src/image-generation/types.ts +300 -300
  25. package/src/native-fallback.ts +140 -140
  26. package/src/payload-rewrite.ts +515 -515
  27. package/src/pi-context-hook.ts +117 -117
  28. package/src/remote-v2-client.ts +811 -811
  29. package/src/request-context-cache.ts +155 -155
  30. package/src/responses-headers.ts +180 -180
  31. package/src/retired-remote-windows.ts +35 -35
  32. package/src/runtime.ts +875 -875
  33. package/src/serializer.ts +883 -882
  34. package/src/supported-environment.ts +27 -27
  35. package/src/types.ts +541 -541
  36. package/src/web-search/alpha-client.ts +876 -876
  37. package/src/web-search/extension.ts +530 -530
  38. package/src/web-search/payload.ts +299 -299
  39. package/src/web-search/prompt.ts +60 -60
  40. package/src/web-search/types.ts +166 -166
  41. package/tsconfig.check.json +50 -50
@@ -1,180 +1,180 @@
1
- import type { ProviderHeaders } from "@earendil-works/pi-ai";
2
- import { mergeProviderHeaders } from "./provider-headers";
3
-
4
- export const CODEX_AFFINITY_SCOPE = "codex-session-v1" as const;
5
- export const CODEX_GATEWAY_ORIGINATOR = "codex_cli_rs" as const;
6
-
7
- /** Headers permitted from Pi/provider configuration onto the gateway hop. */
8
- export const CODEX_GATEWAY_FORWARD_HEADERS = [
9
- "accept",
10
- "content-type",
11
- "session-id",
12
- "thread-id",
13
- "x-parent-session-id",
14
- "x-codex-parent-thread-id",
15
- "x-client-request-id",
16
- "x-codex-model",
17
- "x-codex-affinity-scope",
18
- "x-codex-window-id",
19
- "x-codex-turn-metadata",
20
- "x-codex-beta-features",
21
- "x-openai-encrypted-tool-arguments",
22
- "x-openai-tool-output-truncation-policy",
23
- "x-openai-internal-codex-responses-lite",
24
- "version",
25
- "originator",
26
- "user-agent",
27
- ] as const;
28
-
29
- const CODEX_GATEWAY_FORWARD_HEADER_SET = new Set<string>(CODEX_GATEWAY_FORWARD_HEADERS);
30
-
31
- export function filterCodexGatewayHeaders(headers: ProviderHeaders | undefined): Record<string, string> {
32
- const result: Record<string, string> = {};
33
- for (const [name, value] of Object.entries(headers ?? {})) {
34
- if (typeof value !== "string" || !CODEX_GATEWAY_FORWARD_HEADER_SET.has(name.toLowerCase())) continue;
35
- result[name] = value;
36
- }
37
- return result;
38
- }
39
-
40
- export type CodexAffinity = {
41
- /** Bare model id used by the gateway route selector, never provider/model. */
42
- model: string;
43
- scope: typeof CODEX_AFFINITY_SCOPE;
44
- };
45
-
46
- export type ResponsesHeaderRuntime = {
47
- api: string;
48
- apiKey: string;
49
- headers?: ProviderHeaders;
50
- sessionId?: string;
51
- /** Opt-in gateway Codex routing metadata for synthetic requests. */
52
- codexAffinity?: CodexAffinity;
53
- currentModel: {
54
- headers?: ProviderHeaders;
55
- };
56
- };
57
-
58
- function isRecord(value: unknown): value is Record<string, unknown> {
59
- return !!value && typeof value === "object" && !Array.isArray(value);
60
- }
61
-
62
- function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
63
- const parts = token.split(".");
64
- if (parts.length !== 3) return undefined;
65
- try {
66
- const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8")) as unknown;
67
- return isRecord(payload) ? payload : undefined;
68
- } catch {
69
- return undefined;
70
- }
71
- }
72
-
73
- function extractCodexAccountId(token: string): string | undefined {
74
- const authClaims = decodeJwtPayload(token)?.["https://api.openai.com/auth"];
75
- if (!isRecord(authClaims)) return undefined;
76
- const accountId = authClaims.chatgpt_account_id;
77
- return typeof accountId === "string" && accountId.trim() ? accountId.trim() : undefined;
78
- }
79
-
80
- function buildCodexUserAgent(): string {
81
- const platform = typeof process !== "undefined" ? process.platform : "browser";
82
- const arch = typeof process !== "undefined" ? process.arch : "unknown";
83
- return `pi (${platform}; ${arch})`;
84
- }
85
-
86
- export function buildCodexCliUserAgent(version: string): string {
87
- const platform = typeof process !== "undefined" ? process.platform : "browser";
88
- const arch = typeof process !== "undefined" ? process.arch : "unknown";
89
- return `codex_cli_rs/${version} (${platform}; ${arch})`;
90
- }
91
-
92
- /**
93
- * Codex client identity used by synthetic gateway requests in their User-Agent.
94
- * Native live and shared synthetic requests do not generate a `version`
95
- * header; explicitly configured provider values remain caller-owned.
96
- */
97
- export const CODEX_CLIENT_VERSION = "0.153.0";
98
-
99
- /** Prompt cache keys are clamped by the same host rule (`OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH`). */
100
- const CODEX_SESSION_ID_MAX_LENGTH = 64;
101
-
102
- export function clampCodexSessionId(sessionId: string | undefined): string | undefined {
103
- if (!sessionId) return undefined;
104
- const chars = Array.from(sessionId);
105
- return chars.length <= CODEX_SESSION_ID_MAX_LENGTH ? sessionId : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
106
- }
107
-
108
- export type ResponsesHeaderOptions = {
109
- accept: string;
110
- /** `null` omits content-type so a multipart FormData body can set its boundary. */
111
- contentType?: string | null;
112
- /** Conversation identity for Codex cache-affinity headers; clamped to 64 chars here. */
113
- sessionId?: string;
114
- };
115
-
116
- export function buildResponsesRequestHeaders(
117
- runtime: ResponsesHeaderRuntime,
118
- options: ResponsesHeaderOptions,
119
- ): Headers {
120
- const mergedHeaders = mergeProviderHeaders(runtime.currentModel.headers, runtime.headers);
121
- const headers = new Headers(
122
- runtime.codexAffinity ? filterCodexGatewayHeaders(mergedHeaders) : mergedHeaders,
123
- );
124
- headers.set("accept", options.accept);
125
- if (options.contentType === null) {
126
- headers.delete("content-type");
127
- } else {
128
- headers.set("content-type", options.contentType ?? "application/json");
129
- }
130
- if (runtime.api === "azure-openai-responses") {
131
- // Azure OpenAI authenticates Responses requests with `api-key`; do not
132
- // forward an inherited bearer token from a model/provider header.
133
- headers.delete("authorization");
134
- headers.set("api-key", runtime.apiKey);
135
- } else if (!headers.has("authorization")) {
136
- headers.set("authorization", `Bearer ${runtime.apiKey}`);
137
- }
138
-
139
- const isNativeCodex = runtime.api === "openai-codex-responses";
140
- const gatewayAffinity = runtime.codexAffinity;
141
- if (isNativeCodex || gatewayAffinity) {
142
- if (gatewayAffinity) {
143
- // A gateway request must use the configured data-plane key, never an
144
- // inherited OAuth/account header from the model registry.
145
- headers.set("authorization", `Bearer ${runtime.apiKey}`);
146
- headers.delete("chatgpt-account-id");
147
- headers.delete("cookie");
148
- headers.delete("x-api-key");
149
- headers.set("originator", CODEX_GATEWAY_ORIGINATOR);
150
- headers.set("user-agent", buildCodexCliUserAgent(CODEX_CLIENT_VERSION));
151
- headers.set("x-codex-affinity-scope", gatewayAffinity.scope);
152
- headers.set("x-codex-model", gatewayAffinity.model);
153
- } else {
154
- const accountId = extractCodexAccountId(runtime.apiKey);
155
- if (accountId) headers.set("chatgpt-account-id", accountId);
156
- headers.set("originator", "pi");
157
- headers.set("user-agent", buildCodexUserAgent());
158
- headers.set("openai-beta", "responses=experimental");
159
- }
160
- // Gateways own their version policy. Explicit versions still pass through
161
- // the header merge and allowlist, but Toolkit does not generate one.
162
- // Prompt-cache/session affinity rides on the conversation identity. The
163
- // gateway path also uses the bare model hint above so NEWapi can route
164
- // context/compact requests without inspecting their body.
165
- const sessionId = clampCodexSessionId(options.sessionId ?? runtime.sessionId);
166
- if (sessionId) {
167
- // Header names match the installed host's live-request builder
168
- // (`buildSSEHeaders`: `session-id` + `x-client-request-id`) so our
169
- // synthetic requests land in the same conversation affinity group.
170
- headers.set("session-id", sessionId);
171
- headers.set("x-client-request-id", sessionId);
172
- }
173
- }
174
-
175
- return headers;
176
- }
177
-
178
- export const _responsesHeadersTest = {
179
- extractCodexAccountId,
180
- };
1
+ import type { ProviderHeaders } from "@earendil-works/pi-ai";
2
+ import { mergeProviderHeaders } from "./provider-headers";
3
+
4
+ export const CODEX_AFFINITY_SCOPE = "codex-session-v1" as const;
5
+ export const CODEX_GATEWAY_ORIGINATOR = "codex_cli_rs" as const;
6
+
7
+ /** Headers permitted from Pi/provider configuration onto the gateway hop. */
8
+ export const CODEX_GATEWAY_FORWARD_HEADERS = [
9
+ "accept",
10
+ "content-type",
11
+ "session-id",
12
+ "thread-id",
13
+ "x-parent-session-id",
14
+ "x-codex-parent-thread-id",
15
+ "x-client-request-id",
16
+ "x-codex-model",
17
+ "x-codex-affinity-scope",
18
+ "x-codex-window-id",
19
+ "x-codex-turn-metadata",
20
+ "x-codex-beta-features",
21
+ "x-openai-encrypted-tool-arguments",
22
+ "x-openai-tool-output-truncation-policy",
23
+ "x-openai-internal-codex-responses-lite",
24
+ "version",
25
+ "originator",
26
+ "user-agent",
27
+ ] as const;
28
+
29
+ const CODEX_GATEWAY_FORWARD_HEADER_SET = new Set<string>(CODEX_GATEWAY_FORWARD_HEADERS);
30
+
31
+ export function filterCodexGatewayHeaders(headers: ProviderHeaders | undefined): Record<string, string> {
32
+ const result: Record<string, string> = {};
33
+ for (const [name, value] of Object.entries(headers ?? {})) {
34
+ if (typeof value !== "string" || !CODEX_GATEWAY_FORWARD_HEADER_SET.has(name.toLowerCase())) continue;
35
+ result[name] = value;
36
+ }
37
+ return result;
38
+ }
39
+
40
+ export type CodexAffinity = {
41
+ /** Bare model id used by the gateway route selector, never provider/model. */
42
+ model: string;
43
+ scope: typeof CODEX_AFFINITY_SCOPE;
44
+ };
45
+
46
+ export type ResponsesHeaderRuntime = {
47
+ api: string;
48
+ apiKey: string;
49
+ headers?: ProviderHeaders;
50
+ sessionId?: string;
51
+ /** Opt-in gateway Codex routing metadata for synthetic requests. */
52
+ codexAffinity?: CodexAffinity;
53
+ currentModel: {
54
+ headers?: ProviderHeaders;
55
+ };
56
+ };
57
+
58
+ function isRecord(value: unknown): value is Record<string, unknown> {
59
+ return !!value && typeof value === "object" && !Array.isArray(value);
60
+ }
61
+
62
+ function decodeJwtPayload(token: string): Record<string, unknown> | undefined {
63
+ const parts = token.split(".");
64
+ if (parts.length !== 3) return undefined;
65
+ try {
66
+ const payload = JSON.parse(Buffer.from(parts[1]!, "base64url").toString("utf8")) as unknown;
67
+ return isRecord(payload) ? payload : undefined;
68
+ } catch {
69
+ return undefined;
70
+ }
71
+ }
72
+
73
+ function extractCodexAccountId(token: string): string | undefined {
74
+ const authClaims = decodeJwtPayload(token)?.["https://api.openai.com/auth"];
75
+ if (!isRecord(authClaims)) return undefined;
76
+ const accountId = authClaims.chatgpt_account_id;
77
+ return typeof accountId === "string" && accountId.trim() ? accountId.trim() : undefined;
78
+ }
79
+
80
+ function buildCodexUserAgent(): string {
81
+ const platform = typeof process !== "undefined" ? process.platform : "browser";
82
+ const arch = typeof process !== "undefined" ? process.arch : "unknown";
83
+ return `pi (${platform}; ${arch})`;
84
+ }
85
+
86
+ export function buildCodexCliUserAgent(version: string): string {
87
+ const platform = typeof process !== "undefined" ? process.platform : "browser";
88
+ const arch = typeof process !== "undefined" ? process.arch : "unknown";
89
+ return `codex_cli_rs/${version} (${platform}; ${arch})`;
90
+ }
91
+
92
+ /**
93
+ * Codex client identity used by synthetic gateway requests in their User-Agent.
94
+ * Native live and shared synthetic requests do not generate a `version`
95
+ * header; explicitly configured provider values remain caller-owned.
96
+ */
97
+ export const CODEX_CLIENT_VERSION = "0.153.0";
98
+
99
+ /** Prompt cache keys are clamped by the same host rule (`OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH`). */
100
+ const CODEX_SESSION_ID_MAX_LENGTH = 64;
101
+
102
+ export function clampCodexSessionId(sessionId: string | undefined): string | undefined {
103
+ if (!sessionId) return undefined;
104
+ const chars = Array.from(sessionId);
105
+ return chars.length <= CODEX_SESSION_ID_MAX_LENGTH ? sessionId : chars.slice(0, CODEX_SESSION_ID_MAX_LENGTH).join("");
106
+ }
107
+
108
+ export type ResponsesHeaderOptions = {
109
+ accept: string;
110
+ /** `null` omits content-type so a multipart FormData body can set its boundary. */
111
+ contentType?: string | null;
112
+ /** Conversation identity for Codex cache-affinity headers; clamped to 64 chars here. */
113
+ sessionId?: string;
114
+ };
115
+
116
+ export function buildResponsesRequestHeaders(
117
+ runtime: ResponsesHeaderRuntime,
118
+ options: ResponsesHeaderOptions,
119
+ ): Headers {
120
+ const mergedHeaders = mergeProviderHeaders(runtime.currentModel.headers, runtime.headers);
121
+ const headers = new Headers(
122
+ runtime.codexAffinity ? filterCodexGatewayHeaders(mergedHeaders) : mergedHeaders,
123
+ );
124
+ headers.set("accept", options.accept);
125
+ if (options.contentType === null) {
126
+ headers.delete("content-type");
127
+ } else {
128
+ headers.set("content-type", options.contentType ?? "application/json");
129
+ }
130
+ if (runtime.api === "azure-openai-responses") {
131
+ // Azure OpenAI authenticates Responses requests with `api-key`; do not
132
+ // forward an inherited bearer token from a model/provider header.
133
+ headers.delete("authorization");
134
+ headers.set("api-key", runtime.apiKey);
135
+ } else if (!headers.has("authorization")) {
136
+ headers.set("authorization", `Bearer ${runtime.apiKey}`);
137
+ }
138
+
139
+ const isNativeCodex = runtime.api === "openai-codex-responses";
140
+ const gatewayAffinity = runtime.codexAffinity;
141
+ if (isNativeCodex || gatewayAffinity) {
142
+ if (gatewayAffinity) {
143
+ // A gateway request must use the configured data-plane key, never an
144
+ // inherited OAuth/account header from the model registry.
145
+ headers.set("authorization", `Bearer ${runtime.apiKey}`);
146
+ headers.delete("chatgpt-account-id");
147
+ headers.delete("cookie");
148
+ headers.delete("x-api-key");
149
+ headers.set("originator", CODEX_GATEWAY_ORIGINATOR);
150
+ headers.set("user-agent", buildCodexCliUserAgent(CODEX_CLIENT_VERSION));
151
+ headers.set("x-codex-affinity-scope", gatewayAffinity.scope);
152
+ headers.set("x-codex-model", gatewayAffinity.model);
153
+ } else {
154
+ const accountId = extractCodexAccountId(runtime.apiKey);
155
+ if (accountId) headers.set("chatgpt-account-id", accountId);
156
+ headers.set("originator", "pi");
157
+ headers.set("user-agent", buildCodexUserAgent());
158
+ headers.set("openai-beta", "responses=experimental");
159
+ }
160
+ // Gateways own their version policy. Explicit versions still pass through
161
+ // the header merge and allowlist, but Toolkit does not generate one.
162
+ // Prompt-cache/session affinity rides on the conversation identity. The
163
+ // gateway path also uses the bare model hint above so NEWapi can route
164
+ // context/compact requests without inspecting their body.
165
+ const sessionId = clampCodexSessionId(options.sessionId ?? runtime.sessionId);
166
+ if (sessionId) {
167
+ // Header names match the installed host's live-request builder
168
+ // (`buildSSEHeaders`: `session-id` + `x-client-request-id`) so our
169
+ // synthetic requests land in the same conversation affinity group.
170
+ headers.set("session-id", sessionId);
171
+ headers.set("x-client-request-id", sessionId);
172
+ }
173
+ }
174
+
175
+ return headers;
176
+ }
177
+
178
+ export const _responsesHeadersTest = {
179
+ extractCodexAccountId,
180
+ };
@@ -1,35 +1,35 @@
1
- /** Reserved persisted identifiers, retained only to refuse retired sessions. */
2
- export const RETIRED_REMOTE_WINDOW_TYPE = "codex-context-window" as const;
3
- export const RETIRED_MANUAL_COMPACT_TYPE = "pi-openai-toolkit:manual-compact" as const;
4
- export const RETIRED_REMOTE_WINDOW_SUMMARY =
5
- "[Pi Codex context-window boundary; no conversation summary was generated.]";
6
-
7
- export type RetiredRemoteWindowsReason =
8
- | "window-message"
9
- | "window-compaction"
10
- | "window-summary"
11
- | "manual-compact-record";
12
-
13
- export const RETIRED_REMOTE_WINDOWS_MESSAGE =
14
- "Remote Windows has been removed. This branch contains retired window or manual-compaction records and cannot continue. Start a new session and explicitly choose the model you want to use. Existing history is unchanged; no model or thinking level was restored.";
15
-
16
- function isRecord(value: unknown): value is Record<string, unknown> {
17
- return !!value && typeof value === "object" && !Array.isArray(value);
18
- }
19
-
20
- /**
21
- * Inspect only the supplied active branch. Reserved identifiers suffice even
22
- * when their data is damaged, terminal or superseded by a newer checkpoint.
23
- * This deliberately interprets no ownership, recovery or migration state.
24
- */
25
- export function detectRetiredRemoteWindows(entries: readonly unknown[]): RetiredRemoteWindowsReason | undefined {
26
- for (const entry of entries) {
27
- if (!isRecord(entry)) continue;
28
- if (entry.type === "custom_message" && entry.customType === RETIRED_REMOTE_WINDOW_TYPE) return "window-message";
29
- if (entry.type === "custom" && entry.customType === RETIRED_MANUAL_COMPACT_TYPE) return "manual-compact-record";
30
- if (entry.type !== "compaction") continue;
31
- if (isRecord(entry.details) && entry.details.strategy === RETIRED_REMOTE_WINDOW_TYPE) return "window-compaction";
32
- if (entry.summary === RETIRED_REMOTE_WINDOW_SUMMARY) return "window-summary";
33
- }
34
- return undefined;
35
- }
1
+ /** Reserved persisted identifiers, retained only to refuse retired sessions. */
2
+ export const RETIRED_REMOTE_WINDOW_TYPE = "codex-context-window" as const;
3
+ export const RETIRED_MANUAL_COMPACT_TYPE = "pi-openai-toolkit:manual-compact" as const;
4
+ export const RETIRED_REMOTE_WINDOW_SUMMARY =
5
+ "[Pi Codex context-window boundary; no conversation summary was generated.]";
6
+
7
+ export type RetiredRemoteWindowsReason =
8
+ | "window-message"
9
+ | "window-compaction"
10
+ | "window-summary"
11
+ | "manual-compact-record";
12
+
13
+ export const RETIRED_REMOTE_WINDOWS_MESSAGE =
14
+ "Remote Windows has been removed. This branch contains retired window or manual-compaction records and cannot continue. Start a new session and explicitly choose the model you want to use. Existing history is unchanged; no model or thinking level was restored.";
15
+
16
+ function isRecord(value: unknown): value is Record<string, unknown> {
17
+ return !!value && typeof value === "object" && !Array.isArray(value);
18
+ }
19
+
20
+ /**
21
+ * Inspect only the supplied active branch. Reserved identifiers suffice even
22
+ * when their data is damaged, terminal or superseded by a newer checkpoint.
23
+ * This deliberately interprets no ownership, recovery or migration state.
24
+ */
25
+ export function detectRetiredRemoteWindows(entries: readonly unknown[]): RetiredRemoteWindowsReason | undefined {
26
+ for (const entry of entries) {
27
+ if (!isRecord(entry)) continue;
28
+ if (entry.type === "custom_message" && entry.customType === RETIRED_REMOTE_WINDOW_TYPE) return "window-message";
29
+ if (entry.type === "custom" && entry.customType === RETIRED_MANUAL_COMPACT_TYPE) return "manual-compact-record";
30
+ if (entry.type !== "compaction") continue;
31
+ if (isRecord(entry.details) && entry.details.strategy === RETIRED_REMOTE_WINDOW_TYPE) return "window-compaction";
32
+ if (entry.summary === RETIRED_REMOTE_WINDOW_SUMMARY) return "window-summary";
33
+ }
34
+ return undefined;
35
+ }