pi-openai-toolkit 0.22.3 → 0.23.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 (40) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +4 -2
  4. package/README.zh.md +4 -2
  5. package/config.schema.json +278 -278
  6. package/docs/configuration.md +298 -298
  7. package/docs/internals.md +181 -181
  8. package/package.json +134 -134
  9. package/src/compact-client.ts +412 -412
  10. package/src/compaction-projection.ts +35 -35
  11. package/src/config/migration.ts +103 -103
  12. package/src/config.ts +310 -310
  13. package/src/context-management/codex-provider.ts +270 -270
  14. package/src/context-management/history-notes.ts +357 -357
  15. package/src/context-management/manual-compact.ts +181 -4
  16. package/src/context-management/tools.ts +371 -371
  17. package/src/context-management/types.ts +168 -168
  18. package/src/context-management/window-manager.ts +905 -811
  19. package/src/debug.ts +238 -238
  20. package/src/deferred-tool-carryover.ts +282 -282
  21. package/src/details-store.ts +151 -151
  22. package/src/extension-runtime.ts +1986 -1661
  23. package/src/image-generation/extension.ts +189 -189
  24. package/src/image-generation/protocol.ts +579 -579
  25. package/src/image-generation/types.ts +300 -300
  26. package/src/payload-rewrite.ts +515 -515
  27. package/src/pi-context-hook.ts +117 -117
  28. package/src/remote-v2-client.ts +811 -811
  29. package/src/request-context-cache.ts +155 -155
  30. package/src/responses-headers.ts +180 -180
  31. package/src/runtime.ts +865 -865
  32. package/src/serializer.ts +882 -882
  33. package/src/supported-environment.ts +27 -27
  34. package/src/types.ts +565 -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
@@ -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
+ }