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.
- package/README.md +119 -147
- package/README.zh.md +119 -151
- package/docs/internals.md +42 -21
- package/package.json +127 -127
- package/src/auto-mode/classifier.ts +165 -165
- package/src/auto-mode/policy.ts +146 -146
- package/src/auto-mode/prompt.ts +279 -279
- package/src/auto-mode/reviewer.ts +325 -325
- package/src/auto-mode/transcript.ts +133 -133
- package/src/auto-mode/types.ts +137 -137
- package/src/codex-astra/effort-planner.ts +175 -175
- package/src/codex-astra/extension.ts +122 -122
- package/src/config.ts +621 -592
- package/src/context-management/codex-provider.ts +260 -260
- package/src/context-management/history-notes.ts +357 -325
- package/src/context-management/messages.ts +112 -106
- package/src/context-management/namespace-tools.ts +426 -426
- package/src/context-management/tool-contract.ts +23 -23
- package/src/context-management/tools.ts +306 -311
- package/src/context-management/types.ts +168 -161
- package/src/context-management/window-budget.ts +83 -83
- package/src/context-management/window-manager.ts +60 -51
- package/src/context-management/window-request.ts +125 -125
- package/src/debug.ts +1 -1
- package/src/extension-runtime.ts +34 -9
- package/src/image-generation/artifacts.ts +237 -237
- package/src/image-generation/client.ts +248 -248
- package/src/image-generation/eligibility.ts +18 -18
- package/src/image-generation/extension.ts +19 -2
- package/src/image-generation/protocol.ts +473 -405
- package/src/image-generation/references.ts +194 -194
- package/src/image-generation/render.ts +252 -252
- package/src/image-generation/service.ts +249 -243
- package/src/image-generation/types.ts +189 -184
- package/src/model-scope.ts +17 -17
- package/src/provider-headers.ts +23 -23
- package/src/responses-headers.ts +173 -108
- package/src/runtime.ts +513 -477
- package/src/types.ts +11 -0
- package/src/web-search/extension.ts +80 -80
- package/src/web-search/payload.ts +96 -96
- package/src/web-search/prompt.ts +20 -20
- 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
|
|
3
|
-
export const
|
|
4
|
-
|
|
5
|
-
"openai-responses",
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
] as const;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export const
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
19
|
-
export const
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
22
|
-
export const
|
|
23
|
-
export const
|
|
24
|
-
export const
|
|
25
|
-
export const
|
|
26
|
-
|
|
27
|
-
export const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export type
|
|
31
|
-
export type
|
|
32
|
-
export type
|
|
33
|
-
export type
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export type
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
| "
|
|
102
|
-
| "
|
|
103
|
-
| "
|
|
104
|
-
| "
|
|
105
|
-
| "
|
|
106
|
-
| "
|
|
107
|
-
| "
|
|
108
|
-
| "
|
|
109
|
-
| "
|
|
110
|
-
| "
|
|
111
|
-
| "
|
|
112
|
-
| "
|
|
113
|
-
| "
|
|
114
|
-
| "
|
|
115
|
-
| "
|
|
116
|
-
| "
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
value.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
value.
|
|
149
|
-
value.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
value.
|
|
153
|
-
value.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
value.
|
|
157
|
-
value.
|
|
158
|
-
|
|
159
|
-
typeof value.
|
|
160
|
-
Number.isInteger(value.
|
|
161
|
-
value.
|
|
162
|
-
value.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
)
|
|
178
|
-
.replace(/\
|
|
179
|
-
.replace(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
+
}
|
package/src/model-scope.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/provider-headers.ts
CHANGED
|
@@ -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
|
+
}
|