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,184 +1,189 @@
1
- export const IMAGE_GENERATION_TOOL_NAME = "openai_generate_image";
2
- export const IMAGE_GENERATION_MODEL = "gpt-image-2" as const;
3
- export const IMAGE_GENERATION_MIME_TYPE = "image/png" as const;
4
- export const IMAGE_GENERATION_CAPABLE_APIS = [
5
- "openai-responses",
6
- "openai-codex-responses",
7
- ] as const;
8
- export const IMAGE_GENERATION_SIZES = [
9
- "auto",
10
- "1024x1024",
11
- "1536x1024",
12
- "1024x1536",
13
- ] as const;
14
- export const IMAGE_GENERATION_QUALITIES = ["auto", "low", "medium", "high"] as const;
15
-
16
- export const MAX_IMAGE_PROMPT_CHARS = 20_000;
17
- export const MAX_REFERENCE_IMAGE_COUNT = 5;
18
- export const MAX_REFERENCE_IMAGE_BYTES = 20 * 1024 * 1024;
19
- export const MAX_TOTAL_REFERENCE_IMAGE_BYTES = 50 * 1024 * 1024;
20
- export const MAX_GENERATED_IMAGE_BYTES = 32 * 1024 * 1024;
21
- export const MAX_IMAGE_RESPONSE_BYTES = 48 * 1024 * 1024;
22
- export const MAX_IMAGE_ERROR_BYTES = 64 * 1024;
23
- export const IMAGE_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
24
- export const MAX_IMAGE_PATH_CHARS = 4096;
25
- export const MAX_IMAGE_DIAGNOSTIC_CHARS = 4096;
26
- export const MAX_IMAGE_IDENTIFIER_CHARS = 256;
27
- export const MAX_IMAGE_DIMENSION = 100_000;
28
-
29
- export type ImageGenerationCapableApi = (typeof IMAGE_GENERATION_CAPABLE_APIS)[number];
30
- export type ImageGenerationSize = (typeof IMAGE_GENERATION_SIZES)[number];
31
- export type ImageGenerationQuality = (typeof IMAGE_GENERATION_QUALITIES)[number];
32
- export type ImageGenerationAction = "generate" | "edit";
33
- export type ReferenceImageMimeType = "image/png" | "image/jpeg" | "image/webp";
34
-
35
- export type ImageGenerationModel = {
36
- api?: string;
37
- provider?: string;
38
- id?: string;
39
- };
40
-
41
- export type GenerateImageParams = {
42
- prompt: string;
43
- referenceImagePaths?: string[] | null;
44
- outputPath?: string | null;
45
- size?: ImageGenerationSize;
46
- quality?: ImageGenerationQuality;
47
- };
48
-
49
- export type NormalizedGenerateImageParams = {
50
- prompt: string;
51
- referenceImagePaths: string[];
52
- outputPath?: string;
53
- size: ImageGenerationSize;
54
- quality: ImageGenerationQuality;
55
- action: ImageGenerationAction;
56
- };
57
-
58
- export type PreparedReferenceImage = {
59
- path: string;
60
- mimeType: ReferenceImageMimeType;
61
- bytes: Buffer;
62
- };
63
-
64
- export type ParsedGeneratedImage = {
65
- bytes: Buffer;
66
- imageCallId: string;
67
- responseId?: string;
68
- revisedPrompt?: string;
69
- width: number;
70
- height: number;
71
- };
72
-
73
- export type ImageGenerationDetails = {
74
- artifactPath: string;
75
- outputPath?: string;
76
- routingModel: string;
77
- imageModel: typeof IMAGE_GENERATION_MODEL;
78
- imageCallId: string;
79
- responseId?: string;
80
- mimeType: typeof IMAGE_GENERATION_MIME_TYPE;
81
- byteCount: number;
82
- width: number;
83
- height: number;
84
- edited: boolean;
85
- referenceCount: number;
86
- revisedPrompt?: string;
87
- warning?: string;
88
- };
89
-
90
- export type ImageGenerationExecutionResult = {
91
- details: ImageGenerationDetails;
92
- text: string;
93
- };
94
-
95
- export type ImageGenerationFailureCode =
96
- | "aborted"
97
- | "timeout"
98
- | "disabled"
99
- | "unsupported-model"
100
- | "missing-runtime"
101
- | "invalid-parameters"
102
- | "reference-input-invalid"
103
- | "reference-upload-approval-required"
104
- | "reference-upload-declined"
105
- | "output-path-invalid"
106
- | "output-path-approval-required"
107
- | "output-path-declined"
108
- | "authentication"
109
- | "rate-limit"
110
- | "request-rejected"
111
- | "backend-unavailable"
112
- | "network"
113
- | "oversized-response"
114
- | "malformed-response"
115
- | "no-image"
116
- | "artifact-write-failed";
117
-
118
- export class ImageGenerationError extends Error {
119
- readonly code: ImageGenerationFailureCode;
120
-
121
- constructor(code: ImageGenerationFailureCode, message: string) {
122
- super(message);
123
- this.name = "ImageGenerationError";
124
- this.code = code;
125
- }
126
- }
127
-
128
- function isRecord(value: unknown): value is Record<string, unknown> {
129
- return !!value && typeof value === "object" && !Array.isArray(value);
130
- }
131
-
132
- function isBoundedString(value: unknown, maxChars: number): value is string {
133
- return typeof value === "string" && value.length > 0 && value.length <= maxChars;
134
- }
135
-
136
- export function isImageGenerationDetails(value: unknown): value is ImageGenerationDetails {
137
- if (!isRecord(value)) return false;
138
- return (
139
- isBoundedString(value.artifactPath, MAX_IMAGE_PATH_CHARS) &&
140
- (value.outputPath === undefined || isBoundedString(value.outputPath, MAX_IMAGE_PATH_CHARS)) &&
141
- isBoundedString(value.routingModel, MAX_IMAGE_DIAGNOSTIC_CHARS) &&
142
- value.imageModel === IMAGE_GENERATION_MODEL &&
143
- isBoundedString(value.imageCallId, MAX_IMAGE_IDENTIFIER_CHARS) &&
144
- (value.responseId === undefined || isBoundedString(value.responseId, MAX_IMAGE_IDENTIFIER_CHARS)) &&
145
- value.mimeType === IMAGE_GENERATION_MIME_TYPE &&
146
- typeof value.byteCount === "number" &&
147
- Number.isInteger(value.byteCount) &&
148
- value.byteCount > 0 &&
149
- value.byteCount <= MAX_GENERATED_IMAGE_BYTES &&
150
- typeof value.width === "number" &&
151
- Number.isInteger(value.width) &&
152
- value.width > 0 &&
153
- value.width <= MAX_IMAGE_DIMENSION &&
154
- typeof value.height === "number" &&
155
- Number.isInteger(value.height) &&
156
- value.height > 0 &&
157
- value.height <= MAX_IMAGE_DIMENSION &&
158
- typeof value.edited === "boolean" &&
159
- typeof value.referenceCount === "number" &&
160
- Number.isInteger(value.referenceCount) &&
161
- value.referenceCount >= 0 &&
162
- value.referenceCount <= MAX_REFERENCE_IMAGE_COUNT &&
163
- (value.revisedPrompt === undefined || isBoundedString(value.revisedPrompt, MAX_IMAGE_DIAGNOSTIC_CHARS)) &&
164
- (value.warning === undefined || isBoundedString(value.warning, MAX_IMAGE_DIAGNOSTIC_CHARS))
165
- );
166
- }
167
-
168
- export function sanitizeImageDiagnostic(value: unknown, fallback: string): string {
169
- if (typeof value !== "string") return fallback;
170
- const normalized = value
171
- .replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/gi, "[REDACTED_IMAGE_DATA]")
172
- .replace(/[A-Za-z0-9+/]{128,}={0,2}/g, "[REDACTED_BASE64]")
173
- .replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [REDACTED]")
174
- .replace(
175
- /\b(authorization|api[-_ ]?key|access[-_ ]?token|refresh[-_ ]?token|account[-_ ]?id|cookie)\s*[:=]\s*[^\s,;]+/gi,
176
- "$1: [REDACTED]",
177
- )
178
- .replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "[REDACTED]")
179
- .replace(/[\u0000-\u001f\u007f]+/g, " ")
180
- .replace(/\s+/g, " ")
181
- .trim();
182
- if (!normalized) return fallback;
183
- return normalized.slice(0, MAX_IMAGE_DIAGNOSTIC_CHARS);
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
+ }
@@ -1,17 +1,17 @@
1
- export type ExactModelScopeModel = {
2
- provider?: string;
3
- id?: string;
4
- };
5
-
6
- export function getExactModelKey(model: ExactModelScopeModel | undefined): string | undefined {
7
- if (!model?.provider || !model.id) return undefined;
8
- return `${model.provider}/${model.id}`;
9
- }
10
-
11
- export function isExactModelAllowed(
12
- model: ExactModelScopeModel | undefined,
13
- allowlist: readonly string[],
14
- ): boolean {
15
- const modelKey = getExactModelKey(model);
16
- return modelKey !== undefined && allowlist.includes(modelKey);
17
- }
1
+ export type ExactModelScopeModel = {
2
+ provider?: string;
3
+ id?: string;
4
+ };
5
+
6
+ export function getExactModelKey(model: ExactModelScopeModel | undefined): string | undefined {
7
+ if (!model?.provider || !model.id) return undefined;
8
+ return `${model.provider}/${model.id}`;
9
+ }
10
+
11
+ export function isExactModelAllowed(
12
+ model: ExactModelScopeModel | undefined,
13
+ allowlist: readonly string[],
14
+ ): boolean {
15
+ const modelKey = getExactModelKey(model);
16
+ return modelKey !== undefined && allowlist.includes(modelKey);
17
+ }
@@ -1,23 +1,23 @@
1
- import type { ProviderHeaders } from "@earendil-works/pi-ai";
2
-
3
- /**
4
- * Merge Pi provider headers while preserving its nullable deletion semantics.
5
- * Later sources override earlier ones case-insensitively; null removes a header.
6
- */
7
- export function mergeProviderHeaders(
8
- ...sources: Array<ProviderHeaders | undefined>
9
- ): Record<string, string> {
10
- const headers = new Headers();
11
-
12
- for (const source of sources) {
13
- for (const [name, value] of Object.entries(source ?? {})) {
14
- if (value === null) {
15
- headers.delete(name);
16
- } else {
17
- headers.set(name, value);
18
- }
19
- }
20
- }
21
-
22
- return Object.fromEntries(headers.entries());
23
- }
1
+ import type { ProviderHeaders } from "@earendil-works/pi-ai";
2
+
3
+ /**
4
+ * Merge Pi provider headers while preserving its nullable deletion semantics.
5
+ * Later sources override earlier ones case-insensitively; null removes a header.
6
+ */
7
+ export function mergeProviderHeaders(
8
+ ...sources: Array<ProviderHeaders | undefined>
9
+ ): Record<string, string> {
10
+ const headers = new Headers();
11
+
12
+ for (const source of sources) {
13
+ for (const [name, value] of Object.entries(source ?? {})) {
14
+ if (value === null) {
15
+ headers.delete(name);
16
+ } else {
17
+ headers.set(name, value);
18
+ }
19
+ }
20
+ }
21
+
22
+ return Object.fromEntries(headers.entries());
23
+ }