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.
- package/LICENSE +21 -21
- package/NOTICE +83 -83
- package/README.md +8 -26
- package/README.zh.md +8 -26
- package/config.schema.json +278 -366
- package/docs/configuration.md +294 -310
- package/docs/internals.md +166 -175
- package/package.json +3 -13
- package/src/compact-client.ts +412 -389
- package/src/compaction-errors.ts +96 -0
- package/src/compaction-projection.ts +35 -35
- package/src/config/legacy.ts +519 -717
- package/src/config/migration.ts +103 -100
- package/src/config/notifications.ts +36 -28
- package/src/config/policy.ts +10 -18
- package/src/config/v2.ts +1167 -1380
- package/src/config.ts +310 -319
- package/src/context-management/codex-provider.ts +270 -270
- package/src/context-management/history-notes.ts +357 -357
- package/src/context-management/manual-compact.ts +0 -11
- package/src/context-management/tools.ts +371 -371
- package/src/context-management/types.ts +168 -168
- package/src/context-management/window-manager.ts +801 -801
- package/src/debug.ts +238 -237
- package/src/deferred-tool-carryover.ts +282 -282
- package/src/details-store.ts +151 -151
- package/src/extension-runtime.ts +1662 -1623
- package/src/image-generation/extension.ts +189 -189
- package/src/image-generation/protocol.ts +579 -579
- package/src/image-generation/types.ts +300 -300
- package/src/native-fallback.ts +4 -132
- package/src/payload-rewrite.ts +515 -518
- package/src/pi-context-hook.ts +117 -117
- package/src/remote-v2-client.ts +811 -814
- package/src/request-context-cache.ts +155 -155
- package/src/responses-headers.ts +180 -180
- package/src/runtime.ts +865 -832
- package/src/serializer.ts +882 -882
- package/src/supported-environment.ts +27 -27
- package/src/types.ts +565 -648
- package/src/web-search/alpha-client.ts +876 -876
- package/src/web-search/extension.ts +530 -530
- package/src/web-search/payload.ts +299 -299
- package/src/web-search/prompt.ts +60 -60
- package/src/web-search/types.ts +166 -166
- package/tsconfig.check.json +50 -54
- package/src/auto-mode/classifier.ts +0 -161
- package/src/auto-mode/extension.ts +0 -691
- package/src/auto-mode/model-selection-guard.ts +0 -52
- package/src/auto-mode/policy.ts +0 -146
- package/src/auto-mode/prompt.ts +0 -279
- package/src/auto-mode/reviewer.ts +0 -325
- package/src/auto-mode/tool-review-tui.ts +0 -211
- package/src/auto-mode/transcript.ts +0 -135
- package/src/auto-mode/types.ts +0 -137
package/src/debug.ts
CHANGED
|
@@ -1,237 +1,238 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
type
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
.replace(
|
|
52
|
-
.replace(
|
|
53
|
-
.replace(
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
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
|
-
case "
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
COMPACTION_EXTENSION_ID,
|
|
8
|
+
REDACTED_VALUE,
|
|
9
|
+
type ArtifactContext,
|
|
10
|
+
type ArtifactPaths,
|
|
11
|
+
type ArtifactSessionInfo,
|
|
12
|
+
type DebugArtifactEnvelope,
|
|
13
|
+
type DebugArtifactKind,
|
|
14
|
+
type DebugConfig,
|
|
15
|
+
type RedactOptions,
|
|
16
|
+
} from "./types";
|
|
17
|
+
|
|
18
|
+
const CRITICAL_KEY_RE = /(authorization|api[-_]?key|token|credential|oauth|auth|account[-_]?id|cookie|set[-_]?cookie|response(?:text)?|completed|encrypted[_-]?(content|output))/i;
|
|
19
|
+
const SENSITIVE_KEY_RE = /(authorization|api[-_]?key|token|secret|password|cookie|set-cookie|signature|credential|oauth|auth|account[-_]?id|encrypted[_-]?(content|output))/i;
|
|
20
|
+
const BEARER_RE = /\bBearer\s+[A-Za-z0-9._\-+/=]+/gi;
|
|
21
|
+
const OPENAI_KEY_RE = /\bsk-[A-Za-z0-9\-_]+\b/g;
|
|
22
|
+
const HEADER_TOKEN_RE = /\b(x-api-key|api-key|authorization|chatgpt-account-id)\b\s*[:=]\s*[^\s,;]+/gi;
|
|
23
|
+
const SENSITIVE_QUERY_RE = /([?&](?:api[-_]?key|access[-_]?token|token|secret|signature|credential|password)=)[^&#\s]*/gi;
|
|
24
|
+
|
|
25
|
+
function ensureDir(dirPath: string) {
|
|
26
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function toSessionInfo(context: ArtifactContext): ArtifactSessionInfo {
|
|
30
|
+
const maybeExtensionContext = context as Pick<ExtensionContext, "cwd" | "sessionManager">;
|
|
31
|
+
const sessionManager = maybeExtensionContext.sessionManager;
|
|
32
|
+
if (sessionManager) {
|
|
33
|
+
return {
|
|
34
|
+
cwd: context.cwd,
|
|
35
|
+
sessionId: sessionManager.getSessionId(),
|
|
36
|
+
sessionFile: sessionManager.getSessionFile(),
|
|
37
|
+
sessionDir: sessionManager.getSessionDir(),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return context;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sanitizePathSegment(value: string | undefined, fallback: string): string {
|
|
44
|
+
if (!value) return fallback;
|
|
45
|
+
const normalized = value.replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
46
|
+
return normalized.length > 0 ? normalized : fallback;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function redactInlineSecrets(value: string, placeholder: string): string {
|
|
50
|
+
return value
|
|
51
|
+
.replace(BEARER_RE, `Bearer ${placeholder}`)
|
|
52
|
+
.replace(OPENAI_KEY_RE, placeholder)
|
|
53
|
+
.replace(HEADER_TOKEN_RE, (_match, key: string) => `${key}: ${placeholder}`)
|
|
54
|
+
.replace(SENSITIVE_QUERY_RE, (_match, prefix: string) => `${prefix}${placeholder}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function redactWithKeyPattern(
|
|
58
|
+
value: unknown,
|
|
59
|
+
keyPattern: RegExp,
|
|
60
|
+
options: RedactOptions = {},
|
|
61
|
+
): unknown {
|
|
62
|
+
const placeholder = options.placeholder ?? REDACTED_VALUE;
|
|
63
|
+
const seen = new WeakSet<object>();
|
|
64
|
+
|
|
65
|
+
const visit = (input: unknown): unknown => {
|
|
66
|
+
if (typeof input === "string") {
|
|
67
|
+
return redactInlineSecrets(input, placeholder);
|
|
68
|
+
}
|
|
69
|
+
if (!input || typeof input !== "object") {
|
|
70
|
+
return input;
|
|
71
|
+
}
|
|
72
|
+
if (seen.has(input)) {
|
|
73
|
+
return "[Circular]";
|
|
74
|
+
}
|
|
75
|
+
seen.add(input);
|
|
76
|
+
|
|
77
|
+
if (Array.isArray(input)) {
|
|
78
|
+
return input.map((item) => visit(item));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const result: Record<string, unknown> = {};
|
|
82
|
+
for (const [key, item] of Object.entries(input)) {
|
|
83
|
+
result[key] = keyPattern.test(key) ? placeholder : visit(item);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return visit(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Credentials and opaque checkpoints are never written to artifacts, even when optional redaction is disabled. */
|
|
92
|
+
export function redactCriticalValue(value: unknown, options: RedactOptions = {}): unknown {
|
|
93
|
+
return redactWithKeyPattern(value, CRITICAL_KEY_RE, options);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function redactValue(value: unknown, options: RedactOptions = {}): unknown {
|
|
97
|
+
return redactWithKeyPattern(value, SENSITIVE_KEY_RE, options);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function resolveArtifactPaths(settings: DebugConfig, context: ArtifactContext): ArtifactPaths {
|
|
101
|
+
const sessionInfo = toSessionInfo(context);
|
|
102
|
+
const rootDir = settings.artifactRoot.startsWith("~/")
|
|
103
|
+
? path.join(os.homedir(), settings.artifactRoot.slice(2))
|
|
104
|
+
: path.resolve(settings.artifactRoot);
|
|
105
|
+
const sessionDir = path.join(rootDir, "sessions", sanitizePathSegment(sessionInfo.sessionId, "no-session"));
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
rootDir,
|
|
109
|
+
sessionDir,
|
|
110
|
+
providerRequestsDir: path.join(sessionDir, "provider-requests"),
|
|
111
|
+
compactResponsesDir: path.join(sessionDir, "compact-responses"),
|
|
112
|
+
compactionDir: path.join(sessionDir, "compaction-events"),
|
|
113
|
+
lifecycleDir: path.join(sessionDir, "lifecycle"),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function selectArtifactDirectory(paths: ArtifactPaths, kind: DebugArtifactKind): string {
|
|
118
|
+
switch (kind) {
|
|
119
|
+
case "provider-request":
|
|
120
|
+
return paths.providerRequestsDir;
|
|
121
|
+
case "compact-response":
|
|
122
|
+
return paths.compactResponsesDir;
|
|
123
|
+
case "compaction-event":
|
|
124
|
+
return paths.compactionDir;
|
|
125
|
+
case "lifecycle":
|
|
126
|
+
default:
|
|
127
|
+
return paths.lifecycleDir;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function shouldWriteArtifact(kind: DebugArtifactKind, settings: DebugConfig): boolean {
|
|
132
|
+
switch (kind) {
|
|
133
|
+
case "provider-request":
|
|
134
|
+
return settings.logProviderPayloads;
|
|
135
|
+
case "compact-response":
|
|
136
|
+
return settings.logCompactResponses;
|
|
137
|
+
case "compaction-event":
|
|
138
|
+
case "lifecycle":
|
|
139
|
+
return settings.debug;
|
|
140
|
+
default:
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function writeDebugArtifact(
|
|
146
|
+
kind: DebugArtifactKind,
|
|
147
|
+
data: unknown,
|
|
148
|
+
settings: DebugConfig,
|
|
149
|
+
context: ArtifactContext,
|
|
150
|
+
): string | undefined {
|
|
151
|
+
if (!shouldWriteArtifact(kind, settings)) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const sessionInfo = toSessionInfo(context);
|
|
156
|
+
const paths = resolveArtifactPaths(settings, context);
|
|
157
|
+
const targetDir = selectArtifactDirectory(paths, kind);
|
|
158
|
+
ensureDir(targetDir);
|
|
159
|
+
|
|
160
|
+
const timestamp = new Date().toISOString();
|
|
161
|
+
const fileName = `${timestamp.replace(/[.:]/g, "-")}-${kind}-${randomUUID()}.json`;
|
|
162
|
+
const filePath = path.join(targetDir, fileName);
|
|
163
|
+
const criticallyRedactedData = redactCriticalValue(data);
|
|
164
|
+
const envelope: DebugArtifactEnvelope = {
|
|
165
|
+
extension: COMPACTION_EXTENSION_ID,
|
|
166
|
+
kind,
|
|
167
|
+
timestamp,
|
|
168
|
+
cwd: sessionInfo.cwd,
|
|
169
|
+
sessionId: sessionInfo.sessionId,
|
|
170
|
+
sessionFile: sessionInfo.sessionFile,
|
|
171
|
+
sessionDir: sessionInfo.sessionDir,
|
|
172
|
+
redaction: {
|
|
173
|
+
enabled: settings.redactSensitiveData,
|
|
174
|
+
},
|
|
175
|
+
data: settings.redactSensitiveData ? redactValue(criticallyRedactedData) : criticallyRedactedData,
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
fs.writeFileSync(filePath, `${JSON.stringify(envelope, null, 2)}\n`, { encoding: "utf8", flag: "wx" });
|
|
179
|
+
return filePath;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Always-written, content-free failure record for an opaque replay failure.
|
|
184
|
+
*
|
|
185
|
+
* Unlike regular payload artifacts this is not gated by logProviderPayloads:
|
|
186
|
+
* a replay failure is a continuity-loss incident, not optional payload logging.
|
|
187
|
+
* It contains only structural parity signatures and identity metadata; prompt
|
|
188
|
+
* text, user content, credentials, and encrypted_content are never included
|
|
189
|
+
* (critical redaction is applied unconditionally).
|
|
190
|
+
*/
|
|
191
|
+
export function writeReplayFailureArtifact(
|
|
192
|
+
details: {
|
|
193
|
+
reason: string;
|
|
194
|
+
parity?: { actual: string[]; expected: string[]; mismatches: string[] };
|
|
195
|
+
compactionEntryId?: string;
|
|
196
|
+
provider?: string;
|
|
197
|
+
api?: string;
|
|
198
|
+
model?: string;
|
|
199
|
+
},
|
|
200
|
+
settings: DebugConfig,
|
|
201
|
+
context: ArtifactContext,
|
|
202
|
+
): string | undefined {
|
|
203
|
+
try {
|
|
204
|
+
const sessionInfo = toSessionInfo(context);
|
|
205
|
+
const paths = resolveArtifactPaths(settings, context);
|
|
206
|
+
const targetDir = selectArtifactDirectory(paths, "provider-request");
|
|
207
|
+
ensureDir(targetDir);
|
|
208
|
+
|
|
209
|
+
const timestamp = new Date().toISOString();
|
|
210
|
+
const filePath = path.join(targetDir, `${timestamp.replace(/[.:]/g, "-")}-replay-failure-${randomUUID()}.json`);
|
|
211
|
+
const data = redactCriticalValue({
|
|
212
|
+
event: "before_provider_request.rewrite-failed",
|
|
213
|
+
reason: details.reason,
|
|
214
|
+
parity: details.parity,
|
|
215
|
+
compactionEntryId: details.compactionEntryId,
|
|
216
|
+
provider: details.provider,
|
|
217
|
+
api: details.api,
|
|
218
|
+
model: details.model,
|
|
219
|
+
});
|
|
220
|
+
const envelope: DebugArtifactEnvelope = {
|
|
221
|
+
extension: COMPACTION_EXTENSION_ID,
|
|
222
|
+
kind: "provider-request",
|
|
223
|
+
timestamp,
|
|
224
|
+
cwd: sessionInfo.cwd,
|
|
225
|
+
sessionId: sessionInfo.sessionId,
|
|
226
|
+
sessionFile: sessionInfo.sessionFile,
|
|
227
|
+
sessionDir: sessionInfo.sessionDir,
|
|
228
|
+
redaction: { enabled: true },
|
|
229
|
+
data,
|
|
230
|
+
};
|
|
231
|
+
fs.writeFileSync(filePath, `${JSON.stringify(envelope, null, 2)}\n`, { encoding: "utf8", flag: "wx" });
|
|
232
|
+
return filePath;
|
|
233
|
+
} catch {
|
|
234
|
+
// The replay failure path itself must never be masked by an artifact write
|
|
235
|
+
// error; the caller still aborts the provider request.
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
}
|