pi-openai-toolkit 0.23.0 → 0.23.1

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 (39) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/config.schema.json +278 -278
  4. package/docs/configuration.md +298 -298
  5. package/docs/internals.md +181 -181
  6. package/package.json +135 -134
  7. package/src/compact-client.ts +412 -412
  8. package/src/compaction-projection.ts +35 -35
  9. package/src/config/migration.ts +103 -103
  10. package/src/config.ts +310 -310
  11. package/src/context-management/codex-provider.ts +270 -270
  12. package/src/context-management/history-notes.ts +357 -357
  13. package/src/context-management/manual-compact.ts +599 -599
  14. package/src/context-management/responses-tools.ts +238 -0
  15. package/src/context-management/tools.ts +371 -371
  16. package/src/context-management/types.ts +168 -168
  17. package/src/context-management/window-manager.ts +905 -905
  18. package/src/debug.ts +238 -238
  19. package/src/deferred-tool-carryover.ts +282 -282
  20. package/src/details-store.ts +151 -151
  21. package/src/extension-runtime.ts +1986 -1986
  22. package/src/image-generation/extension.ts +189 -189
  23. package/src/image-generation/protocol.ts +579 -579
  24. package/src/image-generation/types.ts +300 -300
  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/runtime.ts +865 -865
  31. package/src/serializer.ts +882 -882
  32. package/src/supported-environment.ts +27 -27
  33. package/src/types.ts +565 -565
  34. package/src/web-search/alpha-client.ts +876 -876
  35. package/src/web-search/extension.ts +530 -530
  36. package/src/web-search/payload.ts +299 -299
  37. package/src/web-search/prompt.ts +60 -60
  38. package/src/web-search/types.ts +166 -166
  39. package/tsconfig.check.json +50 -50
@@ -1,35 +1,35 @@
1
- import type {
2
- NativeCompactionDetails,
3
- NativeCompactionInputProvenance,
4
- } from "./types";
5
-
6
- /** The configured Pi context-hook projector is unavailable or failed. */
7
- export const COMPACTION_PROJECTION_UNAVAILABLE = "no-supported-projection-seam" as const;
8
- /** The current session context could not be read before projection. */
9
- export const COMPACTION_SESSION_CONTEXT_UNAVAILABLE = "session-context-unavailable" as const;
10
- /** A projected recursive context did not contain the current compaction summary. */
11
- export const COMPACTION_PROJECTED_CONTEXT_UNAVAILABLE = "projected-compaction-context-unavailable" as const;
12
- /** The latest opaque checkpoint was created with no or a different input marker. */
13
- export const COMPACTION_CHECKPOINT_PROVENANCE_UNAVAILABLE = "unverified-compaction-checkpoint" as const;
14
-
15
- /** Persisted edits changed input already sealed inside an opaque checkpoint. */
16
- export const COMPACTION_CHECKPOINT_CONTEXT_EDITED = "checkpoint-context-edited" as const;
17
-
18
- export type UnprojectedCompactionReason =
19
- | typeof COMPACTION_CHECKPOINT_CONTEXT_EDITED
20
- | typeof COMPACTION_PROJECTION_UNAVAILABLE
21
- | typeof COMPACTION_SESSION_CONTEXT_UNAVAILABLE
22
- | typeof COMPACTION_CHECKPOINT_PROVENANCE_UNAVAILABLE
23
- | typeof COMPACTION_PROJECTED_CONTEXT_UNAVAILABLE;
24
-
25
- /**
26
- * A Remote V2 checkpoint is safe for the current replay chain only when its
27
- * marker matches the configured input source. Structural persistence validation
28
- * intentionally remains more permissive for old session files.
29
- */
30
- export function hasVerifiedCompactionInputProvenance(
31
- details: Pick<NativeCompactionDetails, "inputProvenance"> | undefined,
32
- expected: NativeCompactionInputProvenance,
33
- ): boolean {
34
- return details?.inputProvenance === expected;
35
- }
1
+ import type {
2
+ NativeCompactionDetails,
3
+ NativeCompactionInputProvenance,
4
+ } from "./types";
5
+
6
+ /** The configured Pi context-hook projector is unavailable or failed. */
7
+ export const COMPACTION_PROJECTION_UNAVAILABLE = "no-supported-projection-seam" as const;
8
+ /** The current session context could not be read before projection. */
9
+ export const COMPACTION_SESSION_CONTEXT_UNAVAILABLE = "session-context-unavailable" as const;
10
+ /** A projected recursive context did not contain the current compaction summary. */
11
+ export const COMPACTION_PROJECTED_CONTEXT_UNAVAILABLE = "projected-compaction-context-unavailable" as const;
12
+ /** The latest opaque checkpoint was created with no or a different input marker. */
13
+ export const COMPACTION_CHECKPOINT_PROVENANCE_UNAVAILABLE = "unverified-compaction-checkpoint" as const;
14
+
15
+ /** Persisted edits changed input already sealed inside an opaque checkpoint. */
16
+ export const COMPACTION_CHECKPOINT_CONTEXT_EDITED = "checkpoint-context-edited" as const;
17
+
18
+ export type UnprojectedCompactionReason =
19
+ | typeof COMPACTION_CHECKPOINT_CONTEXT_EDITED
20
+ | typeof COMPACTION_PROJECTION_UNAVAILABLE
21
+ | typeof COMPACTION_SESSION_CONTEXT_UNAVAILABLE
22
+ | typeof COMPACTION_CHECKPOINT_PROVENANCE_UNAVAILABLE
23
+ | typeof COMPACTION_PROJECTED_CONTEXT_UNAVAILABLE;
24
+
25
+ /**
26
+ * A Remote V2 checkpoint is safe for the current replay chain only when its
27
+ * marker matches the configured input source. Structural persistence validation
28
+ * intentionally remains more permissive for old session files.
29
+ */
30
+ export function hasVerifiedCompactionInputProvenance(
31
+ details: Pick<NativeCompactionDetails, "inputProvenance"> | undefined,
32
+ expected: NativeCompactionInputProvenance,
33
+ ): boolean {
34
+ return details?.inputProvenance === expected;
35
+ }
@@ -1,103 +1,103 @@
1
- import { resolveToolkitConfig } from "../config";
2
- import type { LoadedToolkitConfig } from "../types";
3
- import { RETIRED_AUTO_MODE_ISSUE, RETIRED_AUTO_MODE_MESSAGE, type ConfigOrigin } from "./policy";
4
- import { resolveV2Config } from "./v2";
5
-
6
- export type MigrationPreview = {
7
- status: "ready" | "needs-review" | "already-v2" | "unavailable";
8
- candidate?: Record<string, unknown>;
9
- /** Controlled explanations, never arbitrary raw input values. */
10
- reasons: string[];
11
- /** Unknown names are masked; paths never expose unknown values. */
12
- unmappedPaths: string[];
13
- /** Candidate leaf -> origin in the unchanged legacy source (or built-in). */
14
- origins?: Record<string, ConfigOrigin>;
15
- };
16
-
17
- function modelRef(key: string): { provider: string; id: string; api: string } {
18
- const slash = key.indexOf("/");
19
- return { provider: key.slice(0, slash), id: key.slice(slash + 1), api: "openai-responses" };
20
- }
21
-
22
- /** Read-only analysis: never edits a source file or claims a lossy candidate is equivalent. */
23
- export function previewToolkitMigration(loaded: LoadedToolkitConfig): MigrationPreview {
24
- if (loaded.document?.format === "v2") {
25
- const retired = resolveToolkitConfig(loaded).issues.filter((issue) => issue.code === RETIRED_AUTO_MODE_ISSUE);
26
- return { status: "already-v2", reasons: ["The document already uses schemaVersion 2.",
27
- ...(retired.length ? [RETIRED_AUTO_MODE_MESSAGE] : [])], unmappedPaths: retired.map((issue) => issue.path) };
28
- }
29
- if (loaded.document?.format === "invalid" || loaded.document?.format === "missing") {
30
- return { status: "unavailable", reasons: ["A readable legacy document is required; no candidate was produced."], unmappedPaths: [] };
31
- }
32
- const baseline = resolveToolkitConfig(loaded);
33
- const legacy = baseline.config;
34
- const { compatibility: _compatibility, diagnostics, ...defaults } = structuredClone(baseline.policy);
35
- const models: Record<string, unknown> = Object.create(null);
36
- const origins: Record<string, ConfigOrigin> = Object.create(null);
37
- const unmappedPaths = new Set(baseline.issues.map((issue) => issue.path));
38
- for (const [leaf, origin] of Object.entries(baseline.origins)) {
39
- if (leaf.startsWith("compatibility.")) continue;
40
- origins[leaf.startsWith("diagnostics.") ? leaf : `defaults.${leaf}`] = origin;
41
- }
42
- const keys = new Set([
43
- ...legacy.webSearch.models,
44
- ...Object.keys(legacy.webSearch.routes ?? {}),
45
- ...legacy.compaction.gatewayContextModels,
46
- ]);
47
- for (const key of keys) {
48
- const resolved = resolveToolkitConfig(loaded, modelRef(key));
49
- const override: Record<string, unknown> = {};
50
- if (resolved.policy.webSearch.route !== defaults.webSearch.route) override.webSearch = resolved.policy.webSearch;
51
- if (resolved.policy.compatibility.transport !== "standard") override.compatibility = resolved.policy.compatibility;
52
- if (Object.keys(override).length > 0) {
53
- models[key] = override;
54
- for (const [feature, fields] of Object.entries(override)) {
55
- for (const field of Object.keys(fields as Record<string, unknown>)) {
56
- const leaf = `${feature}.${field}`;
57
- origins[`models[${JSON.stringify(key)}].${leaf}`] = resolved.origins[leaf];
58
- }
59
- }
60
- }
61
- }
62
- const candidate = { schemaVersion: 2, defaults, models, diagnostics };
63
- const reasons: string[] = [];
64
- // Retired sections are never migrated; their presence is a controlled removal reason.
65
- if (baseline.issues.some((issue) => issue.code === RETIRED_AUTO_MODE_ISSUE)) {
66
- reasons.push(RETIRED_AUTO_MODE_MESSAGE + " No Auto Mode settings were migrated; review the removal before applying this candidate.");
67
- unmappedPaths.add("autoMode");
68
- }
69
- if (loaded.warnings.length || baseline.issues.length) {
70
- reasons.push("Legacy warnings, unknown fields or normalized invalid values require review. Their original contents remain in the untouched source file.");
71
- }
72
- if (legacy.webSearch.enabled && legacy.webSearch.defaultRoute === undefined &&
73
- legacy.webSearch.models.some((key) => !Object.prototype.hasOwnProperty.call(legacy.webSearch.routes ?? {}, key))) {
74
- reasons.push("Legacy hosted allowlists ignore unsupported APIs and malformed payloads; explicit v2 hosted routes fail closed. This candidate is not behavior-equivalent on those paths.");
75
- }
76
- if (!legacy.webSearch.enabled && (legacy.webSearch.models.length || Object.keys(legacy.webSearch.routes ?? {}).length || legacy.webSearch.defaultRoute)) {
77
- reasons.push("Disabled legacy search contains dormant routes or a model list; simplified v2 policy does not retain that enable-later intent.");
78
- if (legacy.webSearch.models.length) unmappedPaths.add("webSearch.models");
79
- if (Object.keys(legacy.webSearch.routes ?? {}).length) unmappedPaths.add("webSearch.routes");
80
- if (legacy.webSearch.defaultRoute) unmappedPaths.add("webSearch.defaultRoute");
81
- }
82
- if (legacy.webSearch.models.length && (legacy.webSearch.defaultRoute !== undefined ||
83
- legacy.webSearch.models.some((key) => Object.hasOwn(legacy.webSearch.routes ?? {}, key)))) {
84
- unmappedPaths.add("webSearch.models");
85
- }
86
- if (!legacy.compaction.enabled && legacy.compaction.contextManagement === "remote") {
87
- reasons.push("Disabled legacy context management retains a dormant remote mode; the candidate selects Pi ownership.");
88
- unmappedPaths.add("compaction.contextManagement");
89
- }
90
- // Gateway affinity was previously context-mode gated for compaction, and not used by image.
91
- if (legacy.compaction.gatewayContextModels.length) {
92
- reasons.push("Gateway compatibility is now shared across features; review synthetic compaction/image affinity before using this candidate.");
93
- }
94
- const candidateChecks = [resolveV2Config(candidate, undefined), ...[...keys].map((key) => resolveV2Config(candidate, key))];
95
- if (candidateChecks.some((resolved) => resolved.issues.length)) reasons.push("The candidate has v2 validation issues; it cannot be applied as-is.");
96
- return {
97
- status: reasons.length ? "needs-review" : "ready",
98
- candidate,
99
- origins,
100
- unmappedPaths: [...unmappedPaths],
101
- reasons: reasons.length ? reasons : ["Recognized active policies map without a known semantic difference. Preview only; no source file was changed."],
102
- };
103
- }
1
+ import { resolveToolkitConfig } from "../config";
2
+ import type { LoadedToolkitConfig } from "../types";
3
+ import { RETIRED_AUTO_MODE_ISSUE, RETIRED_AUTO_MODE_MESSAGE, type ConfigOrigin } from "./policy";
4
+ import { resolveV2Config } from "./v2";
5
+
6
+ export type MigrationPreview = {
7
+ status: "ready" | "needs-review" | "already-v2" | "unavailable";
8
+ candidate?: Record<string, unknown>;
9
+ /** Controlled explanations, never arbitrary raw input values. */
10
+ reasons: string[];
11
+ /** Unknown names are masked; paths never expose unknown values. */
12
+ unmappedPaths: string[];
13
+ /** Candidate leaf -> origin in the unchanged legacy source (or built-in). */
14
+ origins?: Record<string, ConfigOrigin>;
15
+ };
16
+
17
+ function modelRef(key: string): { provider: string; id: string; api: string } {
18
+ const slash = key.indexOf("/");
19
+ return { provider: key.slice(0, slash), id: key.slice(slash + 1), api: "openai-responses" };
20
+ }
21
+
22
+ /** Read-only analysis: never edits a source file or claims a lossy candidate is equivalent. */
23
+ export function previewToolkitMigration(loaded: LoadedToolkitConfig): MigrationPreview {
24
+ if (loaded.document?.format === "v2") {
25
+ const retired = resolveToolkitConfig(loaded).issues.filter((issue) => issue.code === RETIRED_AUTO_MODE_ISSUE);
26
+ return { status: "already-v2", reasons: ["The document already uses schemaVersion 2.",
27
+ ...(retired.length ? [RETIRED_AUTO_MODE_MESSAGE] : [])], unmappedPaths: retired.map((issue) => issue.path) };
28
+ }
29
+ if (loaded.document?.format === "invalid" || loaded.document?.format === "missing") {
30
+ return { status: "unavailable", reasons: ["A readable legacy document is required; no candidate was produced."], unmappedPaths: [] };
31
+ }
32
+ const baseline = resolveToolkitConfig(loaded);
33
+ const legacy = baseline.config;
34
+ const { compatibility: _compatibility, diagnostics, ...defaults } = structuredClone(baseline.policy);
35
+ const models: Record<string, unknown> = Object.create(null);
36
+ const origins: Record<string, ConfigOrigin> = Object.create(null);
37
+ const unmappedPaths = new Set(baseline.issues.map((issue) => issue.path));
38
+ for (const [leaf, origin] of Object.entries(baseline.origins)) {
39
+ if (leaf.startsWith("compatibility.")) continue;
40
+ origins[leaf.startsWith("diagnostics.") ? leaf : `defaults.${leaf}`] = origin;
41
+ }
42
+ const keys = new Set([
43
+ ...legacy.webSearch.models,
44
+ ...Object.keys(legacy.webSearch.routes ?? {}),
45
+ ...legacy.compaction.gatewayContextModels,
46
+ ]);
47
+ for (const key of keys) {
48
+ const resolved = resolveToolkitConfig(loaded, modelRef(key));
49
+ const override: Record<string, unknown> = {};
50
+ if (resolved.policy.webSearch.route !== defaults.webSearch.route) override.webSearch = resolved.policy.webSearch;
51
+ if (resolved.policy.compatibility.transport !== "standard") override.compatibility = resolved.policy.compatibility;
52
+ if (Object.keys(override).length > 0) {
53
+ models[key] = override;
54
+ for (const [feature, fields] of Object.entries(override)) {
55
+ for (const field of Object.keys(fields as Record<string, unknown>)) {
56
+ const leaf = `${feature}.${field}`;
57
+ origins[`models[${JSON.stringify(key)}].${leaf}`] = resolved.origins[leaf];
58
+ }
59
+ }
60
+ }
61
+ }
62
+ const candidate = { schemaVersion: 2, defaults, models, diagnostics };
63
+ const reasons: string[] = [];
64
+ // Retired sections are never migrated; their presence is a controlled removal reason.
65
+ if (baseline.issues.some((issue) => issue.code === RETIRED_AUTO_MODE_ISSUE)) {
66
+ reasons.push(RETIRED_AUTO_MODE_MESSAGE + " No Auto Mode settings were migrated; review the removal before applying this candidate.");
67
+ unmappedPaths.add("autoMode");
68
+ }
69
+ if (loaded.warnings.length || baseline.issues.length) {
70
+ reasons.push("Legacy warnings, unknown fields or normalized invalid values require review. Their original contents remain in the untouched source file.");
71
+ }
72
+ if (legacy.webSearch.enabled && legacy.webSearch.defaultRoute === undefined &&
73
+ legacy.webSearch.models.some((key) => !Object.prototype.hasOwnProperty.call(legacy.webSearch.routes ?? {}, key))) {
74
+ reasons.push("Legacy hosted allowlists ignore unsupported APIs and malformed payloads; explicit v2 hosted routes fail closed. This candidate is not behavior-equivalent on those paths.");
75
+ }
76
+ if (!legacy.webSearch.enabled && (legacy.webSearch.models.length || Object.keys(legacy.webSearch.routes ?? {}).length || legacy.webSearch.defaultRoute)) {
77
+ reasons.push("Disabled legacy search contains dormant routes or a model list; simplified v2 policy does not retain that enable-later intent.");
78
+ if (legacy.webSearch.models.length) unmappedPaths.add("webSearch.models");
79
+ if (Object.keys(legacy.webSearch.routes ?? {}).length) unmappedPaths.add("webSearch.routes");
80
+ if (legacy.webSearch.defaultRoute) unmappedPaths.add("webSearch.defaultRoute");
81
+ }
82
+ if (legacy.webSearch.models.length && (legacy.webSearch.defaultRoute !== undefined ||
83
+ legacy.webSearch.models.some((key) => Object.hasOwn(legacy.webSearch.routes ?? {}, key)))) {
84
+ unmappedPaths.add("webSearch.models");
85
+ }
86
+ if (!legacy.compaction.enabled && legacy.compaction.contextManagement === "remote") {
87
+ reasons.push("Disabled legacy context management retains a dormant remote mode; the candidate selects Pi ownership.");
88
+ unmappedPaths.add("compaction.contextManagement");
89
+ }
90
+ // Gateway affinity was previously context-mode gated for compaction, and not used by image.
91
+ if (legacy.compaction.gatewayContextModels.length) {
92
+ reasons.push("Gateway compatibility is now shared across features; review synthetic compaction/image affinity before using this candidate.");
93
+ }
94
+ const candidateChecks = [resolveV2Config(candidate, undefined), ...[...keys].map((key) => resolveV2Config(candidate, key))];
95
+ if (candidateChecks.some((resolved) => resolved.issues.length)) reasons.push("The candidate has v2 validation issues; it cannot be applied as-is.");
96
+ return {
97
+ status: reasons.length ? "needs-review" : "ready",
98
+ candidate,
99
+ origins,
100
+ unmappedPaths: [...unmappedPaths],
101
+ reasons: reasons.length ? reasons : ["Recognized active policies map without a known semantic difference. Preview only; no source file was changed."],
102
+ };
103
+ }