pi-openai-toolkit 0.14.4 → 0.14.6

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 (43) hide show
  1. package/README.md +119 -147
  2. package/README.zh.md +119 -151
  3. package/docs/internals.md +42 -21
  4. package/package.json +127 -127
  5. package/src/auto-mode/classifier.ts +165 -165
  6. package/src/auto-mode/policy.ts +146 -146
  7. package/src/auto-mode/prompt.ts +279 -279
  8. package/src/auto-mode/reviewer.ts +325 -325
  9. package/src/auto-mode/transcript.ts +133 -133
  10. package/src/auto-mode/types.ts +137 -137
  11. package/src/codex-astra/effort-planner.ts +175 -175
  12. package/src/codex-astra/extension.ts +122 -122
  13. package/src/config.ts +621 -592
  14. package/src/context-management/codex-provider.ts +260 -260
  15. package/src/context-management/history-notes.ts +357 -325
  16. package/src/context-management/messages.ts +112 -106
  17. package/src/context-management/namespace-tools.ts +426 -426
  18. package/src/context-management/tool-contract.ts +23 -23
  19. package/src/context-management/tools.ts +306 -311
  20. package/src/context-management/types.ts +168 -161
  21. package/src/context-management/window-budget.ts +83 -83
  22. package/src/context-management/window-manager.ts +60 -51
  23. package/src/context-management/window-request.ts +125 -125
  24. package/src/debug.ts +1 -1
  25. package/src/extension-runtime.ts +34 -9
  26. package/src/image-generation/artifacts.ts +237 -237
  27. package/src/image-generation/client.ts +248 -248
  28. package/src/image-generation/eligibility.ts +18 -18
  29. package/src/image-generation/extension.ts +19 -2
  30. package/src/image-generation/protocol.ts +473 -405
  31. package/src/image-generation/references.ts +194 -194
  32. package/src/image-generation/render.ts +252 -252
  33. package/src/image-generation/service.ts +249 -243
  34. package/src/image-generation/types.ts +189 -184
  35. package/src/model-scope.ts +17 -17
  36. package/src/provider-headers.ts +23 -23
  37. package/src/responses-headers.ts +173 -108
  38. package/src/runtime.ts +513 -477
  39. package/src/types.ts +11 -0
  40. package/src/web-search/extension.ts +80 -80
  41. package/src/web-search/payload.ts +96 -96
  42. package/src/web-search/prompt.ts +20 -20
  43. package/src/web-search/types.ts +45 -45
@@ -1,161 +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
- export type CodexContextManagementMessageDetails = {
21
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
22
- id: string;
23
- sessionId?: string;
24
- contextManagement: ContextWindowIdentity & {
25
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
26
- kind: ContextManagementMessageKind;
27
- trimPreviousWindow?: true;
28
- };
29
- };
30
-
31
- export type ContextWindowCompactionDetails = {
32
- protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
33
- strategy: typeof CONTEXT_WINDOW_COMPACTION_STRATEGY;
34
- windowId?: string;
35
- };
36
-
37
- export type RemoteContextFailureReason =
38
- | "context-management-inactive"
39
- | "unsupported-model"
40
- | "unsupported-api"
41
- | "unsupported-backend"
42
- | "auth-resolution-failed"
43
- | "missing-token"
44
- | "missing-api-key"
45
- | "missing-account-id"
46
- | "missing-base-url"
47
- | "invalid-account-token"
48
- | "backend-timeout"
49
- | "aborted"
50
- | "http-error"
51
- | "invalid-json"
52
- | "invalid-response"
53
- | "protocol-error";
54
-
55
- export type NativeCodexContextProvider = {
56
- kind: "native-codex";
57
- route: "openai-codex";
58
- provider: "openai-codex";
59
- api: "openai-codex-responses";
60
- model: string;
61
- baseUrl: string;
62
- token: string;
63
- accountId: string;
64
- headers: Record<string, string>;
65
- };
66
-
67
- export type GatewayCodexContextProvider = {
68
- kind: "codex-gateway";
69
- route: "codex-gateway";
70
- /** Operator-defined provider name; matched via compaction.gatewayContextModels. */
71
- provider: string;
72
- api: "openai-responses";
73
- model: string;
74
- baseUrl: string;
75
- apiKey: string;
76
- headers: Record<string, string>;
77
- };
78
-
79
- export type CodexContextProvider = NativeCodexContextProvider | GatewayCodexContextProvider;
80
-
81
- export type CodexContextProviderResolution =
82
- | { ok: true; provider: CodexContextProvider }
83
- | {
84
- ok: false;
85
- reason: RemoteContextFailureReason;
86
- provider?: string;
87
- api?: string;
88
- model?: string;
89
- baseUrl?: string;
90
- };
91
-
92
- export type HistoryAction =
93
- | "list_windows"
94
- | "list_items"
95
- | "read_item"
96
- | "search_contents";
97
-
98
- export type NotesAction =
99
- | "list_files_by_prefix"
100
- | "read_file"
101
- | "search_contents"
102
- | "append_to_file"
103
- | "write_file";
104
-
105
- export type HistoryNotesResponse = Record<string, unknown>;
106
-
107
- export type HistoryNotesResult =
108
- | { ok: true; value: HistoryNotesResponse }
109
- | {
110
- ok: false;
111
- reason: Exclude<RemoteContextFailureReason, "context-management-inactive" | "unsupported-model" | "unsupported-api">;
112
- status?: number;
113
- };
114
-
115
- export function isRecord(value: unknown): value is Record<string, unknown> {
116
- return Boolean(value && typeof value === "object" && !Array.isArray(value));
117
- }
118
-
119
- export function isNonEmptyString(value: unknown): value is string {
120
- return typeof value === "string" && value.trim().length > 0;
121
- }
122
-
123
- export function isContextWindowIdentity(value: unknown): value is ContextWindowIdentity {
124
- if (!isRecord(value)) return false;
125
- return (
126
- isNonEmptyString(value.firstWindowId) &&
127
- isNonEmptyString(value.currentWindowId) &&
128
- (value.previousWindowId === undefined || isNonEmptyString(value.previousWindowId)) &&
129
- Number.isInteger(value.windowNumber) &&
130
- (value.windowNumber as number) >= 0
131
- );
132
- }
133
-
134
- export function isCodexContextManagementMessageDetails(
135
- value: unknown,
136
- ): value is CodexContextManagementMessageDetails {
137
- if (!isRecord(value) || value.protocol !== CONTEXT_MANAGEMENT_PROTOCOL || !isNonEmptyString(value.id)) {
138
- return false;
139
- }
140
- if (value.sessionId !== undefined && !isNonEmptyString(value.sessionId)) return false;
141
- if (!isRecord(value.contextManagement) || value.contextManagement.protocol !== CONTEXT_MANAGEMENT_PROTOCOL) {
142
- return false;
143
- }
144
- const context = value.contextManagement;
145
- if (!isRecord(context)) return false;
146
- const trimPreviousWindow = context.trimPreviousWindow;
147
- return (
148
- (context.kind === "window" || context.kind === "reminder" || context.kind === "fallback") &&
149
- isContextWindowIdentity(context) &&
150
- (trimPreviousWindow === undefined || trimPreviousWindow === true)
151
- );
152
- }
153
-
154
- export function isContextWindowCompactionDetails(value: unknown): value is ContextWindowCompactionDetails {
155
- return (
156
- isRecord(value) &&
157
- value.protocol === CONTEXT_MANAGEMENT_PROTOCOL &&
158
- value.strategy === CONTEXT_WINDOW_COMPACTION_STRATEGY &&
159
- (value.windowId === undefined || isNonEmptyString(value.windowId))
160
- );
161
- }
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,83 +1,83 @@
1
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import {
3
- CONTEXT_WINDOW_FALLBACK_BUFFER,
4
- CONTEXT_WINDOW_FALLBACK_MESSAGE,
5
- renderContextWindowReminder,
6
- type ContextManagementMessageKind,
7
- type ContextWindowIdentity,
8
- } from "./messages";
9
-
10
- export interface ContextRemaining {
11
- remainingTokens: number | undefined;
12
- windowId: string | undefined;
13
- contextWindow: number;
14
- }
15
-
16
- export class ContextWindowBudget {
17
- private readonly remindedWindows = new Set<string>();
18
- private readonly exhaustedWindows = new Set<string>();
19
-
20
- reset(): void {
21
- this.remindedWindows.clear();
22
- this.exhaustedWindows.clear();
23
- }
24
-
25
- restore(kind: ContextManagementMessageKind, windowId: string): void {
26
- if (kind === "reminder") this.remindedWindows.add(windowId);
27
- if (kind === "fallback") this.exhaustedWindows.add(windowId);
28
- }
29
-
30
- record(
31
- ctx: ExtensionContext,
32
- identity: ContextWindowIdentity,
33
- contextTokens: number | undefined,
34
- contextReminderThresholdPercent: number,
35
- ): { content: string; kind: "fallback" | "reminder" } | undefined {
36
- const remaining = this.remaining(ctx, identity, contextTokens);
37
- if (remaining.remainingTokens === undefined) return undefined;
38
- const windowId = identity.currentWindowId;
39
- if (remaining.remainingTokens <= 0 && !this.exhaustedWindows.has(windowId)) {
40
- this.exhaustedWindows.add(windowId);
41
- return { content: CONTEXT_WINDOW_FALLBACK_MESSAGE, kind: "fallback" };
42
- }
43
- const reminderThreshold =
44
- contextReminderThresholdPercent > 0
45
- ? Math.floor((remaining.contextWindow * contextReminderThresholdPercent) / 100)
46
- : 0;
47
- if (
48
- contextReminderThresholdPercent > 0 &&
49
- remaining.remainingTokens <= reminderThreshold &&
50
- !this.remindedWindows.has(windowId)
51
- ) {
52
- this.remindedWindows.add(windowId);
53
- return { content: renderContextWindowReminder(remaining.remainingTokens), kind: "reminder" };
54
- }
55
- return undefined;
56
- }
57
-
58
- remaining(
59
- ctx: ExtensionContext,
60
- identity: ContextWindowIdentity | undefined,
61
- contextTokens?: number,
62
- ): ContextRemaining {
63
- const usage = ctx.getContextUsage();
64
- const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow ?? 0;
65
- const limit = Math.max(0, contextWindow - CONTEXT_WINDOW_FALLBACK_BUFFER);
66
- if (usage === undefined && contextTokens === undefined) {
67
- return {
68
- remainingTokens: undefined,
69
- windowId: identity?.currentWindowId,
70
- contextWindow: limit,
71
- };
72
- }
73
- const used = contextTokens ?? usage?.tokens;
74
- return {
75
- remainingTokens:
76
- used === null || used === undefined
77
- ? undefined
78
- : Math.max(0, limit - used),
79
- windowId: identity?.currentWindowId,
80
- contextWindow: limit,
81
- };
82
- }
83
- }
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ CONTEXT_WINDOW_FALLBACK_BUFFER,
4
+ CONTEXT_WINDOW_FALLBACK_MESSAGE,
5
+ renderContextWindowReminder,
6
+ type ContextManagementMessageKind,
7
+ type ContextWindowIdentity,
8
+ } from "./messages";
9
+
10
+ export interface ContextRemaining {
11
+ remainingTokens: number | undefined;
12
+ windowId: string | undefined;
13
+ contextWindow: number;
14
+ }
15
+
16
+ export class ContextWindowBudget {
17
+ private readonly remindedWindows = new Set<string>();
18
+ private readonly exhaustedWindows = new Set<string>();
19
+
20
+ reset(): void {
21
+ this.remindedWindows.clear();
22
+ this.exhaustedWindows.clear();
23
+ }
24
+
25
+ restore(kind: ContextManagementMessageKind, windowId: string): void {
26
+ if (kind === "reminder") this.remindedWindows.add(windowId);
27
+ if (kind === "fallback") this.exhaustedWindows.add(windowId);
28
+ }
29
+
30
+ record(
31
+ ctx: ExtensionContext,
32
+ identity: ContextWindowIdentity,
33
+ contextTokens: number | undefined,
34
+ contextReminderThresholdPercent: number,
35
+ ): { content: string; kind: "fallback" | "reminder" } | undefined {
36
+ const remaining = this.remaining(ctx, identity, contextTokens);
37
+ if (remaining.remainingTokens === undefined) return undefined;
38
+ const windowId = identity.currentWindowId;
39
+ if (remaining.remainingTokens <= 0 && !this.exhaustedWindows.has(windowId)) {
40
+ this.exhaustedWindows.add(windowId);
41
+ return { content: CONTEXT_WINDOW_FALLBACK_MESSAGE, kind: "fallback" };
42
+ }
43
+ const reminderThreshold =
44
+ contextReminderThresholdPercent > 0
45
+ ? Math.floor((remaining.contextWindow * contextReminderThresholdPercent) / 100)
46
+ : 0;
47
+ if (
48
+ contextReminderThresholdPercent > 0 &&
49
+ remaining.remainingTokens <= reminderThreshold &&
50
+ !this.remindedWindows.has(windowId)
51
+ ) {
52
+ this.remindedWindows.add(windowId);
53
+ return { content: renderContextWindowReminder(remaining.remainingTokens), kind: "reminder" };
54
+ }
55
+ return undefined;
56
+ }
57
+
58
+ remaining(
59
+ ctx: ExtensionContext,
60
+ identity: ContextWindowIdentity | undefined,
61
+ contextTokens?: number,
62
+ ): ContextRemaining {
63
+ const usage = ctx.getContextUsage();
64
+ const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow ?? 0;
65
+ const limit = Math.max(0, contextWindow - CONTEXT_WINDOW_FALLBACK_BUFFER);
66
+ if (usage === undefined && contextTokens === undefined) {
67
+ return {
68
+ remainingTokens: undefined,
69
+ windowId: identity?.currentWindowId,
70
+ contextWindow: limit,
71
+ };
72
+ }
73
+ const used = contextTokens ?? usage?.tokens;
74
+ return {
75
+ remainingTokens:
76
+ used === null || used === undefined
77
+ ? undefined
78
+ : Math.max(0, limit - used),
79
+ windowId: identity?.currentWindowId,
80
+ contextWindow: limit,
81
+ };
82
+ }
83
+ }