pi-openai-toolkit 0.20.8 → 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.
Files changed (54) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +8 -26
  4. package/README.zh.md +8 -26
  5. package/config.schema.json +278 -366
  6. package/docs/configuration.md +294 -310
  7. package/docs/internals.md +166 -175
  8. package/package.json +3 -13
  9. package/src/compact-client.ts +412 -389
  10. package/src/compaction-errors.ts +96 -0
  11. package/src/compaction-projection.ts +35 -35
  12. package/src/config/legacy.ts +519 -717
  13. package/src/config/migration.ts +103 -100
  14. package/src/config/notifications.ts +36 -28
  15. package/src/config/policy.ts +10 -18
  16. package/src/config/v2.ts +1167 -1380
  17. package/src/config.ts +310 -319
  18. package/src/context-management/codex-provider.ts +270 -270
  19. package/src/context-management/history-notes.ts +357 -357
  20. package/src/context-management/manual-compact.ts +0 -11
  21. package/src/context-management/tools.ts +371 -371
  22. package/src/context-management/types.ts +168 -168
  23. package/src/context-management/window-manager.ts +801 -801
  24. package/src/debug.ts +238 -237
  25. package/src/deferred-tool-carryover.ts +282 -282
  26. package/src/details-store.ts +151 -151
  27. package/src/extension-runtime.ts +1662 -1616
  28. package/src/image-generation/extension.ts +189 -189
  29. package/src/image-generation/protocol.ts +579 -579
  30. package/src/image-generation/types.ts +300 -300
  31. package/src/payload-rewrite.ts +515 -518
  32. package/src/pi-context-hook.ts +117 -117
  33. package/src/remote-v2-client.ts +811 -814
  34. package/src/request-context-cache.ts +155 -155
  35. package/src/responses-headers.ts +180 -180
  36. package/src/runtime.ts +865 -832
  37. package/src/serializer.ts +882 -882
  38. package/src/supported-environment.ts +27 -27
  39. package/src/types.ts +565 -648
  40. package/src/web-search/alpha-client.ts +876 -876
  41. package/src/web-search/extension.ts +530 -530
  42. package/src/web-search/payload.ts +299 -299
  43. package/src/web-search/prompt.ts +60 -60
  44. package/src/web-search/types.ts +166 -166
  45. package/tsconfig.check.json +50 -54
  46. package/src/auto-mode/classifier.ts +0 -161
  47. package/src/auto-mode/extension.ts +0 -691
  48. package/src/auto-mode/model-selection-guard.ts +0 -52
  49. package/src/auto-mode/policy.ts +0 -146
  50. package/src/auto-mode/prompt.ts +0 -279
  51. package/src/auto-mode/reviewer.ts +0 -325
  52. package/src/auto-mode/tool-review-tui.ts +0 -211
  53. package/src/auto-mode/transcript.ts +0 -135
  54. package/src/auto-mode/types.ts +0 -137
@@ -1,117 +1,117 @@
1
- import type { AgentMessage } from "@earendil-works/pi-agent-core";
2
- import { ExtensionRunner, type ExtensionContext } from "@earendil-works/pi-coding-agent";
3
-
4
- /** Context property added by the toolkit's host patch. */
5
- export const PI_CONTEXT_PROJECTOR_FIELD = "projectContextForCompaction" as const;
6
- const PI_CONTEXT_PROJECTION_REPORTER_FIELD = "__piOpenAiToolkitReportContextProjectionFailure" as const;
7
- const PATCH_MARKER_FIELD = "__piOpenAiToolkitContextHookPatch" as const;
8
-
9
- type ProjectionState = {
10
- failure?: string;
11
- };
12
-
13
- const activeProjectionByRunner = new WeakMap<ExtensionRunner, ProjectionState>();
14
-
15
- type PatchedExtensionContext = ExtensionContext & {
16
- [PI_CONTEXT_PROJECTOR_FIELD]?: PiContextHookProjector;
17
- [PI_CONTEXT_PROJECTION_REPORTER_FIELD]?: (reason: string) => void;
18
- };
19
-
20
- type MarkedCreateContext = typeof ExtensionRunner.prototype.createContext & {
21
- [PATCH_MARKER_FIELD]?: true;
22
- };
23
-
24
- /** The ordered context-hook chain, applied to a cloned compaction context. */
25
- export type PiContextHookProjector = (
26
- messages: readonly AgentMessage[],
27
- ) => Promise<readonly AgentMessage[]>;
28
-
29
- export type PiContextHookPatchResult =
30
- | { ok: true; installed: boolean; reason?: "already-installed" }
31
- | { ok: false; reason: "emit-context-unavailable" | "create-context-unavailable" };
32
-
33
- /**
34
- * Add a narrow projection method to every ExtensionContext created by Pi.
35
- *
36
- * Pi 0.87's ordered `ExtensionRunner.emitContext()` runs both `context` and
37
- * `context_with_system` phases,
38
- * but the event-level ExtensionContext does not expose the runner. The patch
39
- * bridges only the method needed by Remote V2; it does not expose the runner
40
- * or alter Pi's scheduler.
41
- */
42
- export function installPiContextHookPatch(): PiContextHookPatchResult {
43
- const prototype = ExtensionRunner.prototype;
44
- if (typeof prototype.emitContext !== "function") {
45
- return { ok: false, reason: "emit-context-unavailable" };
46
- }
47
-
48
- const original = prototype.createContext;
49
- if (typeof original !== "function") {
50
- return { ok: false, reason: "create-context-unavailable" };
51
- }
52
-
53
- const markedOriginal = original as MarkedCreateContext;
54
- if (markedOriginal[PATCH_MARKER_FIELD] === true) {
55
- return { ok: true, installed: false, reason: "already-installed" };
56
- }
57
-
58
- const patched = function patchedCreateContext(this: ExtensionRunner): ExtensionContext {
59
- const context = original.call(this) as PatchedExtensionContext;
60
- if (!(PI_CONTEXT_PROJECTOR_FIELD in context)) {
61
- Object.defineProperty(context, PI_CONTEXT_PROJECTOR_FIELD, {
62
- configurable: false,
63
- enumerable: false,
64
- writable: false,
65
- value: async (messages: readonly AgentMessage[]) => {
66
- const state: ProjectionState = {};
67
- activeProjectionByRunner.set(this, state);
68
- try {
69
- const projected = await this.emitContext([...messages]);
70
- if (state.failure) {
71
- throw new Error(`context projection failed: ${state.failure}`);
72
- }
73
- return projected;
74
- } finally {
75
- if (activeProjectionByRunner.get(this) === state) {
76
- activeProjectionByRunner.delete(this);
77
- }
78
- }
79
- },
80
- });
81
- }
82
- if (!(PI_CONTEXT_PROJECTION_REPORTER_FIELD in context)) {
83
- Object.defineProperty(context, PI_CONTEXT_PROJECTION_REPORTER_FIELD, {
84
- configurable: false,
85
- enumerable: false,
86
- writable: false,
87
- value: (reason: string) => {
88
- const state = activeProjectionByRunner.get(this);
89
- if (state) state.failure = reason;
90
- },
91
- });
92
- }
93
- return context;
94
- };
95
-
96
- Object.defineProperty(patched, PATCH_MARKER_FIELD, {
97
- configurable: false,
98
- enumerable: false,
99
- writable: false,
100
- value: true,
101
- });
102
- prototype.createContext = patched;
103
- return { ok: true, installed: true };
104
- }
105
-
106
- /** Return the patched projection method, if this Pi process supports it. */
107
- export function getPiContextHookProjector(
108
- ctx: ExtensionContext,
109
- ): PiContextHookProjector | undefined {
110
- const candidate = (ctx as PatchedExtensionContext)[PI_CONTEXT_PROJECTOR_FIELD];
111
- return typeof candidate === "function" ? candidate : undefined;
112
- }
113
-
114
- /** Preserve fail-closed context-hook errors across Pi's emitContext boundary. */
115
- export function reportPiContextHookFailure(ctx: ExtensionContext, reason: string): void {
116
- (ctx as PatchedExtensionContext)[PI_CONTEXT_PROJECTION_REPORTER_FIELD]?.(reason);
117
- }
1
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
2
+ import { ExtensionRunner, type ExtensionContext } from "@earendil-works/pi-coding-agent";
3
+
4
+ /** Context property added by the toolkit's host patch. */
5
+ export const PI_CONTEXT_PROJECTOR_FIELD = "projectContextForCompaction" as const;
6
+ const PI_CONTEXT_PROJECTION_REPORTER_FIELD = "__piOpenAiToolkitReportContextProjectionFailure" as const;
7
+ const PATCH_MARKER_FIELD = "__piOpenAiToolkitContextHookPatch" as const;
8
+
9
+ type ProjectionState = {
10
+ failure?: string;
11
+ };
12
+
13
+ const activeProjectionByRunner = new WeakMap<ExtensionRunner, ProjectionState>();
14
+
15
+ type PatchedExtensionContext = ExtensionContext & {
16
+ [PI_CONTEXT_PROJECTOR_FIELD]?: PiContextHookProjector;
17
+ [PI_CONTEXT_PROJECTION_REPORTER_FIELD]?: (reason: string) => void;
18
+ };
19
+
20
+ type MarkedCreateContext = typeof ExtensionRunner.prototype.createContext & {
21
+ [PATCH_MARKER_FIELD]?: true;
22
+ };
23
+
24
+ /** The ordered context-hook chain, applied to a cloned compaction context. */
25
+ export type PiContextHookProjector = (
26
+ messages: readonly AgentMessage[],
27
+ ) => Promise<readonly AgentMessage[]>;
28
+
29
+ export type PiContextHookPatchResult =
30
+ | { ok: true; installed: boolean; reason?: "already-installed" }
31
+ | { ok: false; reason: "emit-context-unavailable" | "create-context-unavailable" };
32
+
33
+ /**
34
+ * Add a narrow projection method to every ExtensionContext created by Pi.
35
+ *
36
+ * Pi 0.87's ordered `ExtensionRunner.emitContext()` runs both `context` and
37
+ * `context_with_system` phases,
38
+ * but the event-level ExtensionContext does not expose the runner. The patch
39
+ * bridges only the method needed by Remote V2; it does not expose the runner
40
+ * or alter Pi's scheduler.
41
+ */
42
+ export function installPiContextHookPatch(): PiContextHookPatchResult {
43
+ const prototype = ExtensionRunner.prototype;
44
+ if (typeof prototype.emitContext !== "function") {
45
+ return { ok: false, reason: "emit-context-unavailable" };
46
+ }
47
+
48
+ const original = prototype.createContext;
49
+ if (typeof original !== "function") {
50
+ return { ok: false, reason: "create-context-unavailable" };
51
+ }
52
+
53
+ const markedOriginal = original as MarkedCreateContext;
54
+ if (markedOriginal[PATCH_MARKER_FIELD] === true) {
55
+ return { ok: true, installed: false, reason: "already-installed" };
56
+ }
57
+
58
+ const patched = function patchedCreateContext(this: ExtensionRunner): ExtensionContext {
59
+ const context = original.call(this) as PatchedExtensionContext;
60
+ if (!(PI_CONTEXT_PROJECTOR_FIELD in context)) {
61
+ Object.defineProperty(context, PI_CONTEXT_PROJECTOR_FIELD, {
62
+ configurable: false,
63
+ enumerable: false,
64
+ writable: false,
65
+ value: async (messages: readonly AgentMessage[]) => {
66
+ const state: ProjectionState = {};
67
+ activeProjectionByRunner.set(this, state);
68
+ try {
69
+ const projected = await this.emitContext([...messages]);
70
+ if (state.failure) {
71
+ throw new Error(`context projection failed: ${state.failure}`);
72
+ }
73
+ return projected;
74
+ } finally {
75
+ if (activeProjectionByRunner.get(this) === state) {
76
+ activeProjectionByRunner.delete(this);
77
+ }
78
+ }
79
+ },
80
+ });
81
+ }
82
+ if (!(PI_CONTEXT_PROJECTION_REPORTER_FIELD in context)) {
83
+ Object.defineProperty(context, PI_CONTEXT_PROJECTION_REPORTER_FIELD, {
84
+ configurable: false,
85
+ enumerable: false,
86
+ writable: false,
87
+ value: (reason: string) => {
88
+ const state = activeProjectionByRunner.get(this);
89
+ if (state) state.failure = reason;
90
+ },
91
+ });
92
+ }
93
+ return context;
94
+ };
95
+
96
+ Object.defineProperty(patched, PATCH_MARKER_FIELD, {
97
+ configurable: false,
98
+ enumerable: false,
99
+ writable: false,
100
+ value: true,
101
+ });
102
+ prototype.createContext = patched;
103
+ return { ok: true, installed: true };
104
+ }
105
+
106
+ /** Return the patched projection method, if this Pi process supports it. */
107
+ export function getPiContextHookProjector(
108
+ ctx: ExtensionContext,
109
+ ): PiContextHookProjector | undefined {
110
+ const candidate = (ctx as PatchedExtensionContext)[PI_CONTEXT_PROJECTOR_FIELD];
111
+ return typeof candidate === "function" ? candidate : undefined;
112
+ }
113
+
114
+ /** Preserve fail-closed context-hook errors across Pi's emitContext boundary. */
115
+ export function reportPiContextHookFailure(ctx: ExtensionContext, reason: string): void {
116
+ (ctx as PatchedExtensionContext)[PI_CONTEXT_PROJECTION_REPORTER_FIELD]?.(reason);
117
+ }