pi-openai-toolkit 0.14.8 → 0.14.9
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 +1 -1
- package/README.zh.md +1 -1
- package/docs/internals.md +18 -0
- package/extensions/auto-mode.ts +1 -1
- package/extensions/codex-astra.ts +1 -1
- package/extensions/compaction.ts +1 -1
- package/extensions/image-generation.ts +1 -1
- package/extensions/web-search.ts +1 -1
- package/package.json +2 -1
- package/src/auto-mode/classifier.ts +165 -165
- package/src/auto-mode/extension.ts +129 -20
- 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/tool-review-tui.ts +211 -0
- 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/compaction-projection.ts +31 -31
- package/src/context-management/messages.ts +112 -112
- package/src/context-management/namespace-tools.ts +426 -426
- package/src/context-management/tool-contract.ts +23 -23
- package/src/context-management/tools.ts +291 -291
- package/src/context-management/window-budget.ts +83 -83
- package/src/context-management/window-request.ts +125 -125
- 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/pi-context-hook.ts +116 -116
- package/src/provider-headers.ts +23 -23
- package/src/web-search/alpha-client.ts +875 -875
- package/src/web-search/extension.ts +523 -523
- package/src/web-search/payload.ts +295 -295
- package/src/web-search/prompt.ts +59 -59
- package/src/web-search/types.ts +144 -144
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|