pi-openai-toolkit 0.20.7 → 0.21.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.
Files changed (55) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +8 -26
  4. package/README.zh.md +8 -26
  5. package/config.schema.json +278 -366
  6. package/docs/configuration.md +294 -310
  7. package/docs/internals.md +166 -175
  8. package/package.json +3 -13
  9. package/src/compact-client.ts +412 -389
  10. package/src/compaction-errors.ts +96 -0
  11. package/src/compaction-projection.ts +35 -35
  12. package/src/config/legacy.ts +519 -717
  13. package/src/config/migration.ts +103 -100
  14. package/src/config/notifications.ts +36 -28
  15. package/src/config/policy.ts +10 -18
  16. package/src/config/v2.ts +1167 -1380
  17. package/src/config.ts +310 -319
  18. package/src/context-management/codex-provider.ts +270 -270
  19. package/src/context-management/history-notes.ts +357 -357
  20. package/src/context-management/manual-compact.ts +0 -11
  21. package/src/context-management/tools.ts +371 -371
  22. package/src/context-management/types.ts +168 -168
  23. package/src/context-management/window-manager.ts +801 -801
  24. package/src/debug.ts +238 -237
  25. package/src/deferred-tool-carryover.ts +282 -282
  26. package/src/details-store.ts +151 -151
  27. package/src/extension-runtime.ts +1662 -1623
  28. package/src/image-generation/extension.ts +189 -189
  29. package/src/image-generation/protocol.ts +579 -579
  30. package/src/image-generation/types.ts +300 -300
  31. package/src/native-fallback.ts +4 -132
  32. package/src/payload-rewrite.ts +515 -518
  33. package/src/pi-context-hook.ts +117 -117
  34. package/src/remote-v2-client.ts +811 -814
  35. package/src/request-context-cache.ts +155 -155
  36. package/src/responses-headers.ts +180 -180
  37. package/src/runtime.ts +865 -832
  38. package/src/serializer.ts +882 -882
  39. package/src/supported-environment.ts +27 -27
  40. package/src/types.ts +565 -648
  41. package/src/web-search/alpha-client.ts +876 -876
  42. package/src/web-search/extension.ts +530 -530
  43. package/src/web-search/payload.ts +299 -299
  44. package/src/web-search/prompt.ts +60 -60
  45. package/src/web-search/types.ts +166 -166
  46. package/tsconfig.check.json +50 -54
  47. package/src/auto-mode/classifier.ts +0 -161
  48. package/src/auto-mode/extension.ts +0 -691
  49. package/src/auto-mode/model-selection-guard.ts +0 -52
  50. package/src/auto-mode/policy.ts +0 -146
  51. package/src/auto-mode/prompt.ts +0 -279
  52. package/src/auto-mode/reviewer.ts +0 -325
  53. package/src/auto-mode/tool-review-tui.ts +0 -211
  54. package/src/auto-mode/transcript.ts +0 -135
  55. package/src/auto-mode/types.ts +0 -137
@@ -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 context-management 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 context-management 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
+ };