pi-openai-toolkit 0.22.3 → 0.23.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 (40) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +4 -2
  4. package/README.zh.md +4 -2
  5. package/config.schema.json +278 -278
  6. package/docs/configuration.md +298 -298
  7. package/docs/internals.md +181 -181
  8. package/package.json +134 -134
  9. package/src/compact-client.ts +412 -412
  10. package/src/compaction-projection.ts +35 -35
  11. package/src/config/migration.ts +103 -103
  12. package/src/config.ts +310 -310
  13. package/src/context-management/codex-provider.ts +270 -270
  14. package/src/context-management/history-notes.ts +357 -357
  15. package/src/context-management/manual-compact.ts +181 -4
  16. package/src/context-management/tools.ts +371 -371
  17. package/src/context-management/types.ts +168 -168
  18. package/src/context-management/window-manager.ts +905 -811
  19. package/src/debug.ts +238 -238
  20. package/src/deferred-tool-carryover.ts +282 -282
  21. package/src/details-store.ts +151 -151
  22. package/src/extension-runtime.ts +1986 -1661
  23. package/src/image-generation/extension.ts +189 -189
  24. package/src/image-generation/protocol.ts +579 -579
  25. package/src/image-generation/types.ts +300 -300
  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/runtime.ts +865 -865
  32. package/src/serializer.ts +882 -882
  33. package/src/supported-environment.ts +27 -27
  34. package/src/types.ts +565 -565
  35. package/src/web-search/alpha-client.ts +876 -876
  36. package/src/web-search/extension.ts +530 -530
  37. package/src/web-search/payload.ts +299 -299
  38. package/src/web-search/prompt.ts +60 -60
  39. package/src/web-search/types.ts +166 -166
  40. package/tsconfig.check.json +50 -50
@@ -1,168 +1,168 @@
1
- import type { ContextManagementMode } from "../types";
2
-
3
- export type { ContextManagementMode };
4
-
5
- export const CONTEXT_MANAGEMENT_PROTOCOL = 1 as const;
6
- export const CODEX_CONTEXT_WINDOW_MESSAGE_TYPE = "codex-context-window" as const;
7
- export const CONTEXT_WINDOW_COMPACTION_STRATEGY = "codex-context-window" as const;
8
- export const CONTEXT_WINDOW_REMINDER_THRESHOLD = 6_144;
9
- export const CONTEXT_WINDOW_FALLBACK_BUFFER = 16_384;
10
-
11
- export type ContextManagementMessageKind = "window" | "reminder" | "fallback";
12
-
13
- export type ContextWindowIdentity = {
14
- firstWindowId: string;
15
- currentWindowId: string;
16
- previousWindowId?: string;
17
- windowNumber: number;
18
- };
19
-
20
- /** Persisted-session evidence identifying the note that can restore a rollover. */
21
- export type NotesCheckpointReceipt = {
22
- path: string;
23
- toolCallId: string;
24
- };
25
-
26
- export type CodexContextManagementMessageDetails = {
27
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
28
- id: string;
29
- sessionId?: string;
30
- contextManagement: ContextWindowIdentity & {
31
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
32
- kind: ContextManagementMessageKind;
33
- trimPreviousWindow?: true;
34
- };
35
- };
36
-
37
- export type ContextWindowCompactionDetails = {
38
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
39
- strategy: typeof CONTEXT_WINDOW_COMPACTION_STRATEGY;
40
- windowId?: string;
41
- };
42
-
43
- export type RemoteContextFailureReason =
44
- | "context-management-inactive"
45
- | "unsupported-model"
46
- | "unsupported-api"
47
- | "unsupported-backend"
48
- | "auth-resolution-failed"
49
- | "missing-token"
50
- | "missing-api-key"
51
- | "missing-account-id"
52
- | "missing-base-url"
53
- | "missing-session-id"
54
- | "invalid-account-token"
55
- | "backend-timeout"
56
- | "aborted"
57
- | "http-error"
58
- | "invalid-json"
59
- | "invalid-response"
60
- | "protocol-error";
61
-
62
- export type NativeCodexContextProvider = {
63
- kind: "native-codex";
64
- route: "openai-codex";
65
- provider: "openai-codex";
66
- api: "openai-codex-responses";
67
- model: string;
68
- baseUrl: string;
69
- token: string;
70
- accountId: string;
71
- headers: Record<string, string>;
72
- };
73
-
74
- export type GatewayCodexContextProvider = {
75
- kind: "codex-gateway";
76
- route: "codex-gateway";
77
- /** Operator-defined provider name; matched via compaction.gatewayContextModels. */
78
- provider: string;
79
- api: "openai-responses";
80
- model: string;
81
- baseUrl: string;
82
- apiKey: string;
83
- headers: Record<string, string>;
84
- };
85
-
86
- export type CodexContextProvider = NativeCodexContextProvider | GatewayCodexContextProvider;
87
-
88
- export type CodexContextProviderResolution =
89
- | { ok: true; provider: CodexContextProvider }
90
- | {
91
- ok: false;
92
- reason: RemoteContextFailureReason;
93
- provider?: string;
94
- api?: string;
95
- model?: string;
96
- baseUrl?: string;
97
- };
98
-
99
- export type HistoryAction =
100
- | "list_windows"
101
- | "list_items"
102
- | "read_item"
103
- | "search_contents";
104
-
105
- export type NotesAction =
106
- | "list_files_by_prefix"
107
- | "read_file"
108
- | "search_contents"
109
- | "append_to_file"
110
- | "write_file";
111
-
112
- export type HistoryNotesResponse = Record<string, unknown>;
113
-
114
- export type HistoryNotesResult =
115
- | { ok: true; value: HistoryNotesResponse }
116
- | {
117
- ok: false;
118
- reason: Exclude<RemoteContextFailureReason, "context-management-inactive" | "unsupported-model" | "unsupported-api">;
119
- status?: number;
120
- };
121
-
122
- export function isRecord(value: unknown): value is Record<string, unknown> {
123
- return Boolean(value && typeof value === "object" && !Array.isArray(value));
124
- }
125
-
126
- export function isNonEmptyString(value: unknown): value is string {
127
- return typeof value === "string" && value.trim().length > 0;
128
- }
129
-
130
- export function isContextWindowIdentity(value: unknown): value is ContextWindowIdentity {
131
- if (!isRecord(value)) return false;
132
- return (
133
- isNonEmptyString(value.firstWindowId) &&
134
- isNonEmptyString(value.currentWindowId) &&
135
- (value.previousWindowId === undefined || isNonEmptyString(value.previousWindowId)) &&
136
- Number.isInteger(value.windowNumber) &&
137
- (value.windowNumber as number) >= 0
138
- );
139
- }
140
-
141
- export function isCodexContextManagementMessageDetails(
142
- value: unknown,
143
- ): value is CodexContextManagementMessageDetails {
144
- if (!isRecord(value) || value.protocol !== CONTEXT_MANAGEMENT_PROTOCOL || !isNonEmptyString(value.id)) {
145
- return false;
146
- }
147
- if (value.sessionId !== undefined && !isNonEmptyString(value.sessionId)) return false;
148
- if (!isRecord(value.contextManagement) || value.contextManagement.protocol !== CONTEXT_MANAGEMENT_PROTOCOL) {
149
- return false;
150
- }
151
- const context = value.contextManagement;
152
- if (!isRecord(context)) return false;
153
- const trimPreviousWindow = context.trimPreviousWindow;
154
- return (
155
- (context.kind === "window" || context.kind === "reminder" || context.kind === "fallback") &&
156
- isContextWindowIdentity(context) &&
157
- (trimPreviousWindow === undefined || trimPreviousWindow === true)
158
- );
159
- }
160
-
161
- export function isContextWindowCompactionDetails(value: unknown): value is ContextWindowCompactionDetails {
162
- return (
163
- isRecord(value) &&
164
- value.protocol === CONTEXT_MANAGEMENT_PROTOCOL &&
165
- value.strategy === CONTEXT_WINDOW_COMPACTION_STRATEGY &&
166
- (value.windowId === undefined || isNonEmptyString(value.windowId))
167
- );
168
- }
1
+ import type { ContextManagementMode } from "../types";
2
+
3
+ export type { ContextManagementMode };
4
+
5
+ export const CONTEXT_MANAGEMENT_PROTOCOL = 1 as const;
6
+ export const CODEX_CONTEXT_WINDOW_MESSAGE_TYPE = "codex-context-window" as const;
7
+ export const CONTEXT_WINDOW_COMPACTION_STRATEGY = "codex-context-window" as const;
8
+ export const CONTEXT_WINDOW_REMINDER_THRESHOLD = 6_144;
9
+ export const CONTEXT_WINDOW_FALLBACK_BUFFER = 16_384;
10
+
11
+ export type ContextManagementMessageKind = "window" | "reminder" | "fallback";
12
+
13
+ export type ContextWindowIdentity = {
14
+ firstWindowId: string;
15
+ currentWindowId: string;
16
+ previousWindowId?: string;
17
+ windowNumber: number;
18
+ };
19
+
20
+ /** Persisted-session evidence identifying the note that can restore a rollover. */
21
+ export type NotesCheckpointReceipt = {
22
+ path: string;
23
+ toolCallId: string;
24
+ };
25
+
26
+ export type CodexContextManagementMessageDetails = {
27
+ protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
28
+ id: string;
29
+ sessionId?: string;
30
+ contextManagement: ContextWindowIdentity & {
31
+ protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
32
+ kind: ContextManagementMessageKind;
33
+ trimPreviousWindow?: true;
34
+ };
35
+ };
36
+
37
+ export type ContextWindowCompactionDetails = {
38
+ protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
39
+ strategy: typeof CONTEXT_WINDOW_COMPACTION_STRATEGY;
40
+ windowId?: string;
41
+ };
42
+
43
+ export type RemoteContextFailureReason =
44
+ | "context-management-inactive"
45
+ | "unsupported-model"
46
+ | "unsupported-api"
47
+ | "unsupported-backend"
48
+ | "auth-resolution-failed"
49
+ | "missing-token"
50
+ | "missing-api-key"
51
+ | "missing-account-id"
52
+ | "missing-base-url"
53
+ | "missing-session-id"
54
+ | "invalid-account-token"
55
+ | "backend-timeout"
56
+ | "aborted"
57
+ | "http-error"
58
+ | "invalid-json"
59
+ | "invalid-response"
60
+ | "protocol-error";
61
+
62
+ export type NativeCodexContextProvider = {
63
+ kind: "native-codex";
64
+ route: "openai-codex";
65
+ provider: "openai-codex";
66
+ api: "openai-codex-responses";
67
+ model: string;
68
+ baseUrl: string;
69
+ token: string;
70
+ accountId: string;
71
+ headers: Record<string, string>;
72
+ };
73
+
74
+ export type GatewayCodexContextProvider = {
75
+ kind: "codex-gateway";
76
+ route: "codex-gateway";
77
+ /** Operator-defined provider name; matched via compaction.gatewayContextModels. */
78
+ provider: string;
79
+ api: "openai-responses";
80
+ model: string;
81
+ baseUrl: string;
82
+ apiKey: string;
83
+ headers: Record<string, string>;
84
+ };
85
+
86
+ export type CodexContextProvider = NativeCodexContextProvider | GatewayCodexContextProvider;
87
+
88
+ export type CodexContextProviderResolution =
89
+ | { ok: true; provider: CodexContextProvider }
90
+ | {
91
+ ok: false;
92
+ reason: RemoteContextFailureReason;
93
+ provider?: string;
94
+ api?: string;
95
+ model?: string;
96
+ baseUrl?: string;
97
+ };
98
+
99
+ export type HistoryAction =
100
+ | "list_windows"
101
+ | "list_items"
102
+ | "read_item"
103
+ | "search_contents";
104
+
105
+ export type NotesAction =
106
+ | "list_files_by_prefix"
107
+ | "read_file"
108
+ | "search_contents"
109
+ | "append_to_file"
110
+ | "write_file";
111
+
112
+ export type HistoryNotesResponse = Record<string, unknown>;
113
+
114
+ export type HistoryNotesResult =
115
+ | { ok: true; value: HistoryNotesResponse }
116
+ | {
117
+ ok: false;
118
+ reason: Exclude<RemoteContextFailureReason, "context-management-inactive" | "unsupported-model" | "unsupported-api">;
119
+ status?: number;
120
+ };
121
+
122
+ export function isRecord(value: unknown): value is Record<string, unknown> {
123
+ return Boolean(value && typeof value === "object" && !Array.isArray(value));
124
+ }
125
+
126
+ export function isNonEmptyString(value: unknown): value is string {
127
+ return typeof value === "string" && value.trim().length > 0;
128
+ }
129
+
130
+ export function isContextWindowIdentity(value: unknown): value is ContextWindowIdentity {
131
+ if (!isRecord(value)) return false;
132
+ return (
133
+ isNonEmptyString(value.firstWindowId) &&
134
+ isNonEmptyString(value.currentWindowId) &&
135
+ (value.previousWindowId === undefined || isNonEmptyString(value.previousWindowId)) &&
136
+ Number.isInteger(value.windowNumber) &&
137
+ (value.windowNumber as number) >= 0
138
+ );
139
+ }
140
+
141
+ export function isCodexContextManagementMessageDetails(
142
+ value: unknown,
143
+ ): value is CodexContextManagementMessageDetails {
144
+ if (!isRecord(value) || value.protocol !== CONTEXT_MANAGEMENT_PROTOCOL || !isNonEmptyString(value.id)) {
145
+ return false;
146
+ }
147
+ if (value.sessionId !== undefined && !isNonEmptyString(value.sessionId)) return false;
148
+ if (!isRecord(value.contextManagement) || value.contextManagement.protocol !== CONTEXT_MANAGEMENT_PROTOCOL) {
149
+ return false;
150
+ }
151
+ const context = value.contextManagement;
152
+ if (!isRecord(context)) return false;
153
+ const trimPreviousWindow = context.trimPreviousWindow;
154
+ return (
155
+ (context.kind === "window" || context.kind === "reminder" || context.kind === "fallback") &&
156
+ isContextWindowIdentity(context) &&
157
+ (trimPreviousWindow === undefined || trimPreviousWindow === true)
158
+ );
159
+ }
160
+
161
+ export function isContextWindowCompactionDetails(value: unknown): value is ContextWindowCompactionDetails {
162
+ return (
163
+ isRecord(value) &&
164
+ value.protocol === CONTEXT_MANAGEMENT_PROTOCOL &&
165
+ value.strategy === CONTEXT_WINDOW_COMPACTION_STRATEGY &&
166
+ (value.windowId === undefined || isNonEmptyString(value.windowId))
167
+ );
168
+ }