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.
- package/LICENSE +21 -21
- package/NOTICE +83 -83
- package/README.md +8 -26
- package/README.zh.md +8 -26
- package/config.schema.json +278 -366
- package/docs/configuration.md +294 -310
- package/docs/internals.md +166 -175
- package/package.json +3 -13
- package/src/compact-client.ts +412 -389
- package/src/compaction-errors.ts +96 -0
- package/src/compaction-projection.ts +35 -35
- package/src/config/legacy.ts +519 -717
- package/src/config/migration.ts +103 -100
- package/src/config/notifications.ts +36 -28
- package/src/config/policy.ts +10 -18
- package/src/config/v2.ts +1167 -1380
- package/src/config.ts +310 -319
- package/src/context-management/codex-provider.ts +270 -270
- package/src/context-management/history-notes.ts +357 -357
- package/src/context-management/manual-compact.ts +0 -11
- package/src/context-management/tools.ts +371 -371
- package/src/context-management/types.ts +168 -168
- package/src/context-management/window-manager.ts +801 -801
- package/src/debug.ts +238 -237
- package/src/deferred-tool-carryover.ts +282 -282
- package/src/details-store.ts +151 -151
- package/src/extension-runtime.ts +1662 -1616
- package/src/image-generation/extension.ts +189 -189
- package/src/image-generation/protocol.ts +579 -579
- package/src/image-generation/types.ts +300 -300
- package/src/payload-rewrite.ts +515 -518
- package/src/pi-context-hook.ts +117 -117
- package/src/remote-v2-client.ts +811 -814
- package/src/request-context-cache.ts +155 -155
- package/src/responses-headers.ts +180 -180
- package/src/runtime.ts +865 -832
- package/src/serializer.ts +882 -882
- package/src/supported-environment.ts +27 -27
- package/src/types.ts +565 -648
- package/src/web-search/alpha-client.ts +876 -876
- package/src/web-search/extension.ts +530 -530
- package/src/web-search/payload.ts +299 -299
- package/src/web-search/prompt.ts +60 -60
- package/src/web-search/types.ts +166 -166
- package/tsconfig.check.json +50 -54
- package/src/auto-mode/classifier.ts +0 -161
- package/src/auto-mode/extension.ts +0 -691
- package/src/auto-mode/model-selection-guard.ts +0 -52
- package/src/auto-mode/policy.ts +0 -146
- package/src/auto-mode/prompt.ts +0 -279
- package/src/auto-mode/reviewer.ts +0 -325
- package/src/auto-mode/tool-review-tui.ts +0 -211
- package/src/auto-mode/transcript.ts +0 -135
- package/src/auto-mode/types.ts +0 -137
package/src/pi-context-hook.ts
CHANGED
|
@@ -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
|
+
}
|