pi-openai-toolkit 0.23.1 → 0.24.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.
Files changed (53) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +4 -11
  4. package/README.zh.md +4 -11
  5. package/config.schema.json +264 -278
  6. package/docs/configuration.md +304 -298
  7. package/docs/internals.md +98 -181
  8. package/package.json +123 -135
  9. package/src/compact-client.ts +412 -412
  10. package/src/compaction-projection.ts +35 -35
  11. package/src/config/legacy.ts +12 -62
  12. package/src/config/migration.ts +115 -103
  13. package/src/config/notifications.ts +10 -4
  14. package/src/config/policy.ts +9 -10
  15. package/src/config/v2.ts +12 -51
  16. package/src/config.ts +320 -310
  17. package/src/debug.ts +238 -238
  18. package/src/deferred-tool-carryover.ts +282 -282
  19. package/src/details-store.ts +151 -151
  20. package/src/extension-runtime.ts +1095 -1986
  21. package/src/image-generation/extension.ts +189 -189
  22. package/src/image-generation/protocol.ts +579 -579
  23. package/src/image-generation/types.ts +300 -300
  24. package/src/native-fallback.ts +14 -23
  25. package/src/payload-rewrite.ts +515 -515
  26. package/src/pi-context-hook.ts +117 -117
  27. package/src/remote-v2-client.ts +811 -811
  28. package/src/request-context-cache.ts +155 -155
  29. package/src/responses-headers.ts +180 -180
  30. package/src/retired-remote-windows.ts +35 -0
  31. package/src/runtime.ts +875 -865
  32. package/src/serializer.ts +882 -882
  33. package/src/supported-environment.ts +27 -27
  34. package/src/types.ts +541 -565
  35. package/src/web-search/alpha-client.ts +876 -876
  36. package/src/web-search/extension.ts +530 -530
  37. package/src/web-search/payload.ts +299 -299
  38. package/src/web-search/prompt.ts +60 -60
  39. package/src/web-search/types.ts +166 -166
  40. package/tsconfig.check.json +50 -50
  41. package/src/context-management/codex-provider.ts +0 -270
  42. package/src/context-management/history-notes.ts +0 -357
  43. package/src/context-management/manual-compact.ts +0 -599
  44. package/src/context-management/messages.ts +0 -238
  45. package/src/context-management/namespace-tools.ts +0 -426
  46. package/src/context-management/responses-tools.ts +0 -238
  47. package/src/context-management/tool-contract.ts +0 -62
  48. package/src/context-management/tools.ts +0 -371
  49. package/src/context-management/types.ts +0 -168
  50. package/src/context-management/window-budget.ts +0 -83
  51. package/src/context-management/window-bulk.ts +0 -155
  52. package/src/context-management/window-manager.ts +0 -905
  53. package/src/context-management/window-request.ts +0 -125
@@ -1,125 +0,0 @@
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
- }