pi-openai-toolkit 0.14.5 → 0.14.7
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/docs/internals.md +42 -21
- package/package.json +1 -1
- 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/context-management/codex-provider.ts +20 -20
- package/src/context-management/history-notes.ts +32 -0
- 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 +279 -311
- package/src/context-management/types.ts +7 -0
- 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/references.ts +194 -194
- package/src/image-generation/render.ts +252 -252
- package/src/model-scope.ts +17 -17
- package/src/provider-headers.ts +23 -23
- package/src/responses-headers.ts +76 -11
- package/src/runtime.ts +37 -1
- 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,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
|
+
}
|
|
@@ -31,8 +31,13 @@ import {
|
|
|
31
31
|
isNonEmptyString,
|
|
32
32
|
isRecord,
|
|
33
33
|
isContextWindowCompactionDetails,
|
|
34
|
+
type NotesCheckpointReceipt,
|
|
34
35
|
} from "./types";
|
|
35
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
encodeEncryptedOutputForContext,
|
|
38
|
+
isSuccessfulHistoryNotesToolResult,
|
|
39
|
+
loadHistoryNotesThreadHint,
|
|
40
|
+
} from "./history-notes";
|
|
36
41
|
import { rewriteContextNamespaceTools } from "./namespace-tools";
|
|
37
42
|
|
|
38
43
|
interface StartContextWindowOptions {
|
|
@@ -171,6 +176,9 @@ export class CodexContextWindowManager {
|
|
|
171
176
|
this.rolloverPending = true;
|
|
172
177
|
try {
|
|
173
178
|
const current = this.identity;
|
|
179
|
+
const checkpoint = current
|
|
180
|
+
? findLatestNotesCheckpointSinceBoundary(ctx.sessionManager.getBranch(), this.sessionId)
|
|
181
|
+
: undefined;
|
|
174
182
|
let threadHint: string | undefined;
|
|
175
183
|
if (current) {
|
|
176
184
|
try {
|
|
@@ -189,7 +197,7 @@ export class CodexContextWindowManager {
|
|
|
189
197
|
windowNumber: current.windowNumber + 1,
|
|
190
198
|
}
|
|
191
199
|
: { firstWindowId: currentWindowId, currentWindowId, windowNumber: 0 };
|
|
192
|
-
this.sendWindowMessage(pi, ctx, next, options, threadHint);
|
|
200
|
+
this.sendWindowMessage(pi, ctx, next, options, threadHint, checkpoint);
|
|
193
201
|
return true;
|
|
194
202
|
} catch (error) {
|
|
195
203
|
this.rolloverPending = false;
|
|
@@ -289,10 +297,11 @@ export class CodexContextWindowManager {
|
|
|
289
297
|
identity: ContextWindowIdentity,
|
|
290
298
|
options: StartContextWindowOptions,
|
|
291
299
|
threadHint?: string,
|
|
300
|
+
checkpoint?: NotesCheckpointReceipt,
|
|
292
301
|
): void {
|
|
293
302
|
sendContextWindowMessage(
|
|
294
303
|
pi,
|
|
295
|
-
renderContextWindowMessage(identity, threadHint),
|
|
304
|
+
renderContextWindowMessage(identity, threadHint, checkpoint),
|
|
296
305
|
"window",
|
|
297
306
|
identity,
|
|
298
307
|
{ triggerTurn: options.triggerTurn, sessionId: ctx.sessionManager.getSessionId() },
|
|
@@ -329,20 +338,7 @@ export function hasAssistantUsageSinceWindowBoundary(
|
|
|
329
338
|
entries: readonly SessionEntry[],
|
|
330
339
|
sessionId?: string,
|
|
331
340
|
): boolean {
|
|
332
|
-
|
|
333
|
-
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
334
|
-
const entry = entries[index]!;
|
|
335
|
-
if (entry.type !== "custom_message" || entry.customType !== CODEX_CONTEXT_WINDOW_MESSAGE_TYPE) continue;
|
|
336
|
-
if (!couldBelongToSession(entry.details, sessionId)) continue;
|
|
337
|
-
if (!isCodexContextManagementMessageDetails(entry.details)) {
|
|
338
|
-
throw new Error("Malformed persisted Codex context-window message");
|
|
339
|
-
}
|
|
340
|
-
if (!matchesSession(entry.details.sessionId, sessionId)) continue;
|
|
341
|
-
if (entry.details.contextManagement.kind === "window") {
|
|
342
|
-
boundaryIndex = index;
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
341
|
+
const boundaryIndex = findLatestWindowBoundaryIndex(entries, sessionId);
|
|
346
342
|
if (boundaryIndex < 0) return false;
|
|
347
343
|
for (let index = boundaryIndex + 1; index < entries.length; index += 1) {
|
|
348
344
|
const entry = entries[index]!;
|
|
@@ -363,29 +359,18 @@ export function hasAssistantUsageSinceWindowBoundary(
|
|
|
363
359
|
}
|
|
364
360
|
|
|
365
361
|
/**
|
|
366
|
-
*
|
|
367
|
-
*
|
|
362
|
+
* Return the most recent successful notes write after the latest window
|
|
363
|
+
* boundary. The receipt is derived from persisted session entries so it
|
|
364
|
+
* survives restarts and can be used for an explicit rollover handoff.
|
|
368
365
|
*/
|
|
369
|
-
export function
|
|
366
|
+
export function findLatestNotesCheckpointSinceBoundary(
|
|
370
367
|
entries: readonly SessionEntry[],
|
|
371
368
|
sessionId?: string,
|
|
372
|
-
):
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (!couldBelongToSession(entry.details, sessionId)) continue;
|
|
378
|
-
if (!isCodexContextManagementMessageDetails(entry.details)) {
|
|
379
|
-
throw new Error("Malformed persisted Codex context-window message");
|
|
380
|
-
}
|
|
381
|
-
if (!matchesSession(entry.details.sessionId, sessionId)) continue;
|
|
382
|
-
if (entry.details.contextManagement.kind === "window") {
|
|
383
|
-
boundaryIndex = index;
|
|
384
|
-
break;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (boundaryIndex < 0) return false;
|
|
388
|
-
const checkpointCalls = new Set<string>();
|
|
369
|
+
): NotesCheckpointReceipt | undefined {
|
|
370
|
+
const boundaryIndex = findLatestWindowBoundaryIndex(entries, sessionId);
|
|
371
|
+
if (boundaryIndex < 0) return undefined;
|
|
372
|
+
const checkpointCalls = new Map<string, NotesCheckpointReceipt>();
|
|
373
|
+
let latest: NotesCheckpointReceipt | undefined;
|
|
389
374
|
for (let index = boundaryIndex + 1; index < entries.length; index += 1) {
|
|
390
375
|
const entry = entries[index]!;
|
|
391
376
|
if (entry.type !== "message") continue;
|
|
@@ -393,28 +378,52 @@ export function findNotesCheckpointSinceBoundary(
|
|
|
393
378
|
if (message.role === "assistant") {
|
|
394
379
|
const parts = Array.isArray(message.content) ? message.content : [];
|
|
395
380
|
for (const part of parts) {
|
|
396
|
-
if (!isRecord(part) || part.type !== "toolCall") continue;
|
|
397
|
-
// Match by call id only; the provider-side namespace rewrite never
|
|
398
|
-
// affects the names persisted in the Pi session branch.
|
|
399
|
-
if (part.name !== "notes") continue;
|
|
381
|
+
if (!isRecord(part) || part.type !== "toolCall" || part.name !== "notes") continue;
|
|
400
382
|
const args = isRecord(part.arguments) ? part.arguments : undefined;
|
|
401
383
|
const action = typeof args?.action === "string" ? args.action : "";
|
|
402
|
-
|
|
403
|
-
|
|
384
|
+
const path = isNonEmptyString(args?.path) ? args.path : undefined;
|
|
385
|
+
if (isNonEmptyString(part.id) && path !== undefined && NOTES_CHECKPOINT_ACTIONS.has(action)) {
|
|
386
|
+
checkpointCalls.set(part.id, { path, toolCallId: part.id });
|
|
404
387
|
}
|
|
405
388
|
}
|
|
406
389
|
continue;
|
|
407
390
|
}
|
|
408
|
-
if (message.role
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
391
|
+
if (message.role !== "toolResult" || message.toolName !== "notes") continue;
|
|
392
|
+
const receipt = checkpointCalls.get(message.toolCallId);
|
|
393
|
+
if (!receipt) continue;
|
|
394
|
+
checkpointCalls.delete(message.toolCallId);
|
|
395
|
+
if (message.isError !== false || !isSuccessfulHistoryNotesToolResult(message.details)) continue;
|
|
396
|
+
latest = receipt;
|
|
397
|
+
}
|
|
398
|
+
return latest;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Whether the branch contains a successful notes append/write result after the
|
|
403
|
+
* latest window boundary. Reads and failed writes never count as checkpoints.
|
|
404
|
+
*/
|
|
405
|
+
export function findNotesCheckpointSinceBoundary(
|
|
406
|
+
entries: readonly SessionEntry[],
|
|
407
|
+
sessionId?: string,
|
|
408
|
+
): boolean {
|
|
409
|
+
return findLatestNotesCheckpointSinceBoundary(entries, sessionId) !== undefined;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function findLatestWindowBoundaryIndex(
|
|
413
|
+
entries: readonly SessionEntry[],
|
|
414
|
+
sessionId?: string,
|
|
415
|
+
): number {
|
|
416
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
417
|
+
const entry = entries[index]!;
|
|
418
|
+
if (entry.type !== "custom_message" || entry.customType !== CODEX_CONTEXT_WINDOW_MESSAGE_TYPE) continue;
|
|
419
|
+
if (!couldBelongToSession(entry.details, sessionId)) continue;
|
|
420
|
+
if (!isCodexContextManagementMessageDetails(entry.details)) {
|
|
421
|
+
throw new Error("Malformed persisted Codex context-window message");
|
|
415
422
|
}
|
|
423
|
+
if (!matchesSession(entry.details.sessionId, sessionId)) continue;
|
|
424
|
+
if (entry.details.contextManagement.kind === "window") return index;
|
|
416
425
|
}
|
|
417
|
-
return
|
|
426
|
+
return -1;
|
|
418
427
|
}
|
|
419
428
|
|
|
420
429
|
export function findLatestWindowBoundaryEntry(
|
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
import type { ProviderHeaders } from "@earendil-works/pi-ai";
|
|
2
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import type { ContextWindowIdentity } from "./types";
|
|
4
|
-
import { CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER } from "./history-notes";
|
|
5
|
-
import { rewriteContextNamespaceCalls } from "./namespace-tools";
|
|
6
|
-
|
|
7
|
-
export type ContextWindowRequestMetadata = {
|
|
8
|
-
session_id: string;
|
|
9
|
-
thread_id: string;
|
|
10
|
-
agent_name: "/root";
|
|
11
|
-
window_id: string;
|
|
12
|
-
window_number: number;
|
|
13
|
-
context_window_id: string;
|
|
14
|
-
request_kind: "turn";
|
|
15
|
-
history_ingest_requested: true;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export function buildContextWindowRequestMetadata(
|
|
19
|
-
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
20
|
-
identity: ContextWindowIdentity,
|
|
21
|
-
): ContextWindowRequestMetadata {
|
|
22
|
-
const sessionId = ctx.sessionManager.getSessionId();
|
|
23
|
-
return {
|
|
24
|
-
session_id: sessionId,
|
|
25
|
-
thread_id: sessionId,
|
|
26
|
-
agent_name: "/root",
|
|
27
|
-
window_id: `${sessionId}:${identity.windowNumber}`,
|
|
28
|
-
window_number: identity.windowNumber,
|
|
29
|
-
context_window_id: identity.currentWindowId,
|
|
30
|
-
request_kind: "turn",
|
|
31
|
-
history_ingest_requested: true,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function rewriteWindowPayload(
|
|
36
|
-
payload: unknown,
|
|
37
|
-
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
38
|
-
identity: ContextWindowIdentity | undefined,
|
|
39
|
-
): unknown {
|
|
40
|
-
if (!identity || !isResponsesPayload(payload)) return payload;
|
|
41
|
-
const clientMetadata = payload.client_metadata;
|
|
42
|
-
if (clientMetadata !== undefined && !isRecord(clientMetadata)) return payload;
|
|
43
|
-
const existingTurnMetadata = parseTurnMetadata(clientMetadata?.["x-codex-turn-metadata"]);
|
|
44
|
-
if (clientMetadata?.["x-codex-turn-metadata"] !== undefined && !existingTurnMetadata) return payload;
|
|
45
|
-
const metadata = buildContextWindowRequestMetadata(ctx, identity);
|
|
46
|
-
const turnMetadata = { ...existingTurnMetadata, ...metadata };
|
|
47
|
-
const rewritten: Record<string, unknown> = {
|
|
48
|
-
...payload,
|
|
49
|
-
client_metadata: {
|
|
50
|
-
...(clientMetadata ?? {}),
|
|
51
|
-
"x-codex-window-id": metadata.window_id,
|
|
52
|
-
"x-codex-turn-metadata": JSON.stringify(turnMetadata),
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
return rewriteEncryptedToolOutputs(rewriteContextNamespaceCalls(rewritten));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function rewriteEncryptedToolOutputs(payload: unknown): unknown {
|
|
59
|
-
if (!isRecord(payload) || !Array.isArray(payload.input)) return payload;
|
|
60
|
-
let changed = false;
|
|
61
|
-
const input = payload.input.map((item) => {
|
|
62
|
-
if (!isRecord(item) || item.type !== "function_call_output") return item;
|
|
63
|
-
const output = item.output;
|
|
64
|
-
if (typeof output === "string") {
|
|
65
|
-
const encrypted = decodeEncryptedOutputContext(output);
|
|
66
|
-
if (encrypted === undefined) return item;
|
|
67
|
-
changed = true;
|
|
68
|
-
return { ...item, output: [{ type: "encrypted_content", encrypted_content: encrypted }] };
|
|
69
|
-
}
|
|
70
|
-
if (!Array.isArray(output)) return item;
|
|
71
|
-
let outputChanged = false;
|
|
72
|
-
const rewrittenOutput = output.map((part) => {
|
|
73
|
-
if (!isRecord(part) || part.type !== "input_text" || typeof part.text !== "string") return part;
|
|
74
|
-
const encrypted = decodeEncryptedOutputContext(part.text);
|
|
75
|
-
if (encrypted === undefined) return part;
|
|
76
|
-
outputChanged = true;
|
|
77
|
-
return { type: "encrypted_content", encrypted_content: encrypted };
|
|
78
|
-
});
|
|
79
|
-
if (!outputChanged) return item;
|
|
80
|
-
changed = true;
|
|
81
|
-
return { ...item, output: rewrittenOutput };
|
|
82
|
-
});
|
|
83
|
-
return changed ? { ...payload, input } : payload;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function decodeEncryptedOutputContext(value: string): string | undefined {
|
|
87
|
-
if (!value.startsWith(CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER)) return undefined;
|
|
88
|
-
try {
|
|
89
|
-
const decoded: unknown = JSON.parse(value.slice(CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER.length));
|
|
90
|
-
if (typeof decoded !== "string" || decoded.length === 0) throw new Error("invalid encrypted output marker");
|
|
91
|
-
return decoded;
|
|
92
|
-
} catch {
|
|
93
|
-
throw new Error("Malformed encrypted history/notes output marker");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function parseTurnMetadata(value: unknown): Record<string, unknown> | undefined {
|
|
98
|
-
if (value === undefined) return {};
|
|
99
|
-
if (typeof value !== "string") return undefined;
|
|
100
|
-
try {
|
|
101
|
-
const parsed: unknown = JSON.parse(value);
|
|
102
|
-
return isRecord(parsed) ? parsed : undefined;
|
|
103
|
-
} catch {
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function isResponsesPayload(value: unknown): value is Record<string, unknown> & { model: string; input: unknown[] } {
|
|
109
|
-
return isRecord(value) && typeof value.model === "string" && Array.isArray(value.input);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function rewriteWindowHeaders(
|
|
113
|
-
headers: ProviderHeaders,
|
|
114
|
-
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
115
|
-
identity: ContextWindowIdentity | undefined,
|
|
116
|
-
): void {
|
|
117
|
-
if (!identity) return;
|
|
118
|
-
const metadata = buildContextWindowRequestMetadata(ctx, identity);
|
|
119
|
-
headers["x-codex-window-id"] = metadata.window_id;
|
|
120
|
-
headers["x-codex-turn-metadata"] = JSON.stringify(metadata);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
124
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
125
|
-
}
|
|
1
|
+
import type { ProviderHeaders } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { ContextWindowIdentity } from "./types";
|
|
4
|
+
import { CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER } from "./history-notes";
|
|
5
|
+
import { rewriteContextNamespaceCalls } from "./namespace-tools";
|
|
6
|
+
|
|
7
|
+
export type ContextWindowRequestMetadata = {
|
|
8
|
+
session_id: string;
|
|
9
|
+
thread_id: string;
|
|
10
|
+
agent_name: "/root";
|
|
11
|
+
window_id: string;
|
|
12
|
+
window_number: number;
|
|
13
|
+
context_window_id: string;
|
|
14
|
+
request_kind: "turn";
|
|
15
|
+
history_ingest_requested: true;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function buildContextWindowRequestMetadata(
|
|
19
|
+
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
20
|
+
identity: ContextWindowIdentity,
|
|
21
|
+
): ContextWindowRequestMetadata {
|
|
22
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
23
|
+
return {
|
|
24
|
+
session_id: sessionId,
|
|
25
|
+
thread_id: sessionId,
|
|
26
|
+
agent_name: "/root",
|
|
27
|
+
window_id: `${sessionId}:${identity.windowNumber}`,
|
|
28
|
+
window_number: identity.windowNumber,
|
|
29
|
+
context_window_id: identity.currentWindowId,
|
|
30
|
+
request_kind: "turn",
|
|
31
|
+
history_ingest_requested: true,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function rewriteWindowPayload(
|
|
36
|
+
payload: unknown,
|
|
37
|
+
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
38
|
+
identity: ContextWindowIdentity | undefined,
|
|
39
|
+
): unknown {
|
|
40
|
+
if (!identity || !isResponsesPayload(payload)) return payload;
|
|
41
|
+
const clientMetadata = payload.client_metadata;
|
|
42
|
+
if (clientMetadata !== undefined && !isRecord(clientMetadata)) return payload;
|
|
43
|
+
const existingTurnMetadata = parseTurnMetadata(clientMetadata?.["x-codex-turn-metadata"]);
|
|
44
|
+
if (clientMetadata?.["x-codex-turn-metadata"] !== undefined && !existingTurnMetadata) return payload;
|
|
45
|
+
const metadata = buildContextWindowRequestMetadata(ctx, identity);
|
|
46
|
+
const turnMetadata = { ...existingTurnMetadata, ...metadata };
|
|
47
|
+
const rewritten: Record<string, unknown> = {
|
|
48
|
+
...payload,
|
|
49
|
+
client_metadata: {
|
|
50
|
+
...(clientMetadata ?? {}),
|
|
51
|
+
"x-codex-window-id": metadata.window_id,
|
|
52
|
+
"x-codex-turn-metadata": JSON.stringify(turnMetadata),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
return rewriteEncryptedToolOutputs(rewriteContextNamespaceCalls(rewritten));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function rewriteEncryptedToolOutputs(payload: unknown): unknown {
|
|
59
|
+
if (!isRecord(payload) || !Array.isArray(payload.input)) return payload;
|
|
60
|
+
let changed = false;
|
|
61
|
+
const input = payload.input.map((item) => {
|
|
62
|
+
if (!isRecord(item) || item.type !== "function_call_output") return item;
|
|
63
|
+
const output = item.output;
|
|
64
|
+
if (typeof output === "string") {
|
|
65
|
+
const encrypted = decodeEncryptedOutputContext(output);
|
|
66
|
+
if (encrypted === undefined) return item;
|
|
67
|
+
changed = true;
|
|
68
|
+
return { ...item, output: [{ type: "encrypted_content", encrypted_content: encrypted }] };
|
|
69
|
+
}
|
|
70
|
+
if (!Array.isArray(output)) return item;
|
|
71
|
+
let outputChanged = false;
|
|
72
|
+
const rewrittenOutput = output.map((part) => {
|
|
73
|
+
if (!isRecord(part) || part.type !== "input_text" || typeof part.text !== "string") return part;
|
|
74
|
+
const encrypted = decodeEncryptedOutputContext(part.text);
|
|
75
|
+
if (encrypted === undefined) return part;
|
|
76
|
+
outputChanged = true;
|
|
77
|
+
return { type: "encrypted_content", encrypted_content: encrypted };
|
|
78
|
+
});
|
|
79
|
+
if (!outputChanged) return item;
|
|
80
|
+
changed = true;
|
|
81
|
+
return { ...item, output: rewrittenOutput };
|
|
82
|
+
});
|
|
83
|
+
return changed ? { ...payload, input } : payload;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function decodeEncryptedOutputContext(value: string): string | undefined {
|
|
87
|
+
if (!value.startsWith(CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER)) return undefined;
|
|
88
|
+
try {
|
|
89
|
+
const decoded: unknown = JSON.parse(value.slice(CODEX_ENCRYPTED_OUTPUT_CONTEXT_MARKER.length));
|
|
90
|
+
if (typeof decoded !== "string" || decoded.length === 0) throw new Error("invalid encrypted output marker");
|
|
91
|
+
return decoded;
|
|
92
|
+
} catch {
|
|
93
|
+
throw new Error("Malformed encrypted history/notes output marker");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseTurnMetadata(value: unknown): Record<string, unknown> | undefined {
|
|
98
|
+
if (value === undefined) return {};
|
|
99
|
+
if (typeof value !== "string") return undefined;
|
|
100
|
+
try {
|
|
101
|
+
const parsed: unknown = JSON.parse(value);
|
|
102
|
+
return isRecord(parsed) ? parsed : undefined;
|
|
103
|
+
} catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isResponsesPayload(value: unknown): value is Record<string, unknown> & { model: string; input: unknown[] } {
|
|
109
|
+
return isRecord(value) && typeof value.model === "string" && Array.isArray(value.input);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function rewriteWindowHeaders(
|
|
113
|
+
headers: ProviderHeaders,
|
|
114
|
+
ctx: Pick<ExtensionContext, "sessionManager">,
|
|
115
|
+
identity: ContextWindowIdentity | undefined,
|
|
116
|
+
): void {
|
|
117
|
+
if (!identity) return;
|
|
118
|
+
const metadata = buildContextWindowRequestMetadata(ctx, identity);
|
|
119
|
+
headers["x-codex-window-id"] = metadata.window_id;
|
|
120
|
+
headers["x-codex-turn-metadata"] = JSON.stringify(metadata);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
124
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
125
|
+
}
|
package/src/debug.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
type RedactOptions,
|
|
15
15
|
} from "./types";
|
|
16
16
|
|
|
17
|
-
const CRITICAL_KEY_RE = /(authorization|api[-_]?key|token|credential|oauth|auth|account[-_]?id|encrypted[_-]?(content|output))/i;
|
|
17
|
+
const CRITICAL_KEY_RE = /(authorization|api[-_]?key|token|credential|oauth|auth|account[-_]?id|cookie|set[-_]?cookie|response(?:text)?|completed|encrypted[_-]?(content|output))/i;
|
|
18
18
|
const SENSITIVE_KEY_RE = /(authorization|api[-_]?key|token|secret|password|cookie|set-cookie|signature|credential|oauth|auth|account[-_]?id|encrypted[_-]?(content|output))/i;
|
|
19
19
|
const BEARER_RE = /\bBearer\s+[A-Za-z0-9._\-+/=]+/gi;
|
|
20
20
|
const OPENAI_KEY_RE = /\bsk-[A-Za-z0-9\-_]+\b/g;
|