pi-openai-toolkit 0.14.4 → 0.14.6

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 (43) hide show
  1. package/README.md +119 -147
  2. package/README.zh.md +119 -151
  3. package/docs/internals.md +42 -21
  4. package/package.json +127 -127
  5. package/src/auto-mode/classifier.ts +165 -165
  6. package/src/auto-mode/policy.ts +146 -146
  7. package/src/auto-mode/prompt.ts +279 -279
  8. package/src/auto-mode/reviewer.ts +325 -325
  9. package/src/auto-mode/transcript.ts +133 -133
  10. package/src/auto-mode/types.ts +137 -137
  11. package/src/codex-astra/effort-planner.ts +175 -175
  12. package/src/codex-astra/extension.ts +122 -122
  13. package/src/config.ts +621 -592
  14. package/src/context-management/codex-provider.ts +260 -260
  15. package/src/context-management/history-notes.ts +357 -325
  16. package/src/context-management/messages.ts +112 -106
  17. package/src/context-management/namespace-tools.ts +426 -426
  18. package/src/context-management/tool-contract.ts +23 -23
  19. package/src/context-management/tools.ts +306 -311
  20. package/src/context-management/types.ts +168 -161
  21. package/src/context-management/window-budget.ts +83 -83
  22. package/src/context-management/window-manager.ts +60 -51
  23. package/src/context-management/window-request.ts +125 -125
  24. package/src/debug.ts +1 -1
  25. package/src/extension-runtime.ts +34 -9
  26. package/src/image-generation/artifacts.ts +237 -237
  27. package/src/image-generation/client.ts +248 -248
  28. package/src/image-generation/eligibility.ts +18 -18
  29. package/src/image-generation/extension.ts +19 -2
  30. package/src/image-generation/protocol.ts +473 -405
  31. package/src/image-generation/references.ts +194 -194
  32. package/src/image-generation/render.ts +252 -252
  33. package/src/image-generation/service.ts +249 -243
  34. package/src/image-generation/types.ts +189 -184
  35. package/src/model-scope.ts +17 -17
  36. package/src/provider-headers.ts +23 -23
  37. package/src/responses-headers.ts +173 -108
  38. package/src/runtime.ts +513 -477
  39. package/src/types.ts +11 -0
  40. package/src/web-search/extension.ts +80 -80
  41. package/src/web-search/payload.ts +96 -96
  42. package/src/web-search/prompt.ts +20 -20
  43. package/src/web-search/types.ts +45 -45
@@ -1,106 +1,112 @@
1
- import { randomUUID } from "node:crypto";
2
- import type { AgentMessage } from "@earendil-works/pi-agent-core";
3
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
4
- import {
5
- CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
6
- CONTEXT_MANAGEMENT_PROTOCOL,
7
- CONTEXT_WINDOW_COMPACTION_STRATEGY,
8
- CONTEXT_WINDOW_FALLBACK_BUFFER,
9
- CONTEXT_WINDOW_REMINDER_THRESHOLD,
10
- type CodexContextManagementMessageDetails,
11
- type ContextManagementMessageKind,
12
- type ContextWindowCompactionDetails,
13
- type ContextWindowIdentity,
14
- isCodexContextManagementMessageDetails,
15
- isContextWindowCompactionDetails,
16
- } from "./types";
17
-
18
- export {
19
- CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
20
- CONTEXT_WINDOW_COMPACTION_STRATEGY,
21
- CONTEXT_WINDOW_FALLBACK_BUFFER,
22
- CONTEXT_WINDOW_REMINDER_THRESHOLD,
23
- isCodexContextManagementMessageDetails,
24
- isContextWindowCompactionDetails,
25
- };
26
- export type {
27
- CodexContextManagementMessageDetails,
28
- ContextManagementMessageKind,
29
- ContextWindowCompactionDetails,
30
- ContextWindowIdentity,
31
- };
32
-
33
- export const CONTEXT_WINDOW_COMPACTION_SUMMARY =
34
- "[Pi Codex context-window boundary; no conversation summary was generated.]";
35
-
36
- const CONTEXT_WINDOW_GUIDANCE = `<context_window_guidance>
37
- Checkpoint the active request, known history IDs, decisions, progress, learnings and next steps in notes before new_context; no summary carries over. After rollover, read hinted notes. Use history only for a missing detail.
38
- </context_window_guidance>`;
39
-
40
- export function renderContextWindowMessage(
41
- identity: ContextWindowIdentity,
42
- threadHint?: string,
43
- ): string {
44
- const lines = [
45
- "<context_window>",
46
- "Agent name: /root",
47
- `First context window id: ${identity.firstWindowId}`,
48
- `Current context window id: ${identity.currentWindowId}`,
49
- ];
50
- if (identity.previousWindowId) lines.push(`Previous context window id: ${identity.previousWindowId}`);
51
- if (threadHint) lines.push(threadHint);
52
- lines.push("</context_window>");
53
- return `${CONTEXT_WINDOW_GUIDANCE}\n\n${lines.join("\n")}`;
54
- }
55
-
56
- export function renderContextWindowReminder(remainingTokens: number): string {
57
- return `<context_window_reminder>
58
- Only ${Math.max(0, Math.floor(remainingTokens))} context tokens remain. Checkpoint the active request, state and known history IDs in notes, then call new_context; no conversation summary carries over.
59
- </context_window_reminder>`;
60
- }
61
-
62
- export const CONTEXT_WINDOW_FALLBACK_MESSAGE = `<context_window_reminder>
63
- Context exhausted. Do not continue or answer. Make exactly one notes write or append call that checkpoints the active request, state and known history IDs, then call new_context. Use no other tools before rollover.
64
- </context_window_reminder>`;
65
-
66
- export function isContextWindowBoundary(
67
- message: AgentMessage,
68
- ): message is Extract<AgentMessage, { role: "custom" }> & {
69
- details: CodexContextManagementMessageDetails;
70
- } {
71
- return (
72
- message.role === "custom" &&
73
- message.customType === CODEX_CONTEXT_WINDOW_MESSAGE_TYPE &&
74
- isCodexContextManagementMessageDetails(message.details) &&
75
- message.details.contextManagement.kind === "window"
76
- );
77
- }
78
-
79
- export function sendContextWindowMessage(
80
- pi: ExtensionAPI,
81
- content: string,
82
- kind: ContextManagementMessageKind,
83
- identity: ContextWindowIdentity,
84
- options: { triggerTurn: boolean; sessionId?: string },
85
- trimPreviousWindow = false,
86
- ): void {
87
- const details: CodexContextManagementMessageDetails = {
88
- protocol: CONTEXT_MANAGEMENT_PROTOCOL,
89
- id: randomUUID(),
90
- ...(options.sessionId ? { sessionId: options.sessionId } : {}),
91
- contextManagement: {
92
- protocol: CONTEXT_MANAGEMENT_PROTOCOL,
93
- kind,
94
- ...identity,
95
- ...(trimPreviousWindow ? { trimPreviousWindow: true as const } : {}),
96
- },
97
- };
98
- pi.sendMessage({
99
- customType: CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
100
- content,
101
- display: true,
102
- details,
103
- }, options.triggerTurn
104
- ? { deliverAs: "steer", triggerTurn: true }
105
- : { triggerTurn: false });
106
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
3
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
4
+ import {
5
+ CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
6
+ CONTEXT_MANAGEMENT_PROTOCOL,
7
+ CONTEXT_WINDOW_COMPACTION_STRATEGY,
8
+ CONTEXT_WINDOW_FALLBACK_BUFFER,
9
+ CONTEXT_WINDOW_REMINDER_THRESHOLD,
10
+ type CodexContextManagementMessageDetails,
11
+ type ContextManagementMessageKind,
12
+ type ContextWindowCompactionDetails,
13
+ type ContextWindowIdentity,
14
+ type NotesCheckpointReceipt,
15
+ isCodexContextManagementMessageDetails,
16
+ isContextWindowCompactionDetails,
17
+ } from "./types";
18
+
19
+ export {
20
+ CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
21
+ CONTEXT_WINDOW_COMPACTION_STRATEGY,
22
+ CONTEXT_WINDOW_FALLBACK_BUFFER,
23
+ CONTEXT_WINDOW_REMINDER_THRESHOLD,
24
+ isCodexContextManagementMessageDetails,
25
+ isContextWindowCompactionDetails,
26
+ };
27
+ export type {
28
+ CodexContextManagementMessageDetails,
29
+ ContextManagementMessageKind,
30
+ ContextWindowCompactionDetails,
31
+ ContextWindowIdentity,
32
+ };
33
+
34
+ export const CONTEXT_WINDOW_COMPACTION_SUMMARY =
35
+ "[Pi Codex context-window boundary; no conversation summary was generated.]";
36
+
37
+ const CONTEXT_WINDOW_GUIDANCE = `<context_window_guidance>
38
+ Checkpoint the active request, known history IDs, decisions, progress, learnings and next steps in notes before new_context; no summary carries over. After rollover, read the checkpoint receipt note first when present; use a thread hint only as supplemental guidance. Use history only for a missing detail.
39
+ </context_window_guidance>`;
40
+
41
+ export function renderContextWindowMessage(
42
+ identity: ContextWindowIdentity,
43
+ threadHint?: string,
44
+ checkpoint?: NotesCheckpointReceipt,
45
+ ): string {
46
+ const lines = [
47
+ "<context_window>",
48
+ "Agent name: /root",
49
+ `First context window id: ${identity.firstWindowId}`,
50
+ `Current context window id: ${identity.currentWindowId}`,
51
+ ];
52
+ if (identity.previousWindowId) lines.push(`Previous context window id: ${identity.previousWindowId}`);
53
+ if (checkpoint) {
54
+ lines.push("Checkpoint successfully written:");
55
+ lines.push(` Read this note before doing anything else with notes action "read_file": ${JSON.stringify(checkpoint.path)}`);
56
+ }
57
+ if (threadHint) lines.push(threadHint);
58
+ lines.push("</context_window>");
59
+ return `${CONTEXT_WINDOW_GUIDANCE}\n\n${lines.join("\n")}`;
60
+ }
61
+
62
+ export function renderContextWindowReminder(remainingTokens: number): string {
63
+ return `<context_window_reminder>
64
+ Only ${Math.max(0, Math.floor(remainingTokens))} context tokens remain. Checkpoint the active request, state and known history IDs in notes, then call new_context; no conversation summary carries over.
65
+ </context_window_reminder>`;
66
+ }
67
+
68
+ export const CONTEXT_WINDOW_FALLBACK_MESSAGE = `<context_window_reminder>
69
+ Context exhausted. Do not continue or answer. Make exactly one notes write or append call that checkpoints the active request, state and known history IDs, then call new_context. Use no other tools before rollover.
70
+ </context_window_reminder>`;
71
+
72
+ export function isContextWindowBoundary(
73
+ message: AgentMessage,
74
+ ): message is Extract<AgentMessage, { role: "custom" }> & {
75
+ details: CodexContextManagementMessageDetails;
76
+ } {
77
+ return (
78
+ message.role === "custom" &&
79
+ message.customType === CODEX_CONTEXT_WINDOW_MESSAGE_TYPE &&
80
+ isCodexContextManagementMessageDetails(message.details) &&
81
+ message.details.contextManagement.kind === "window"
82
+ );
83
+ }
84
+
85
+ export function sendContextWindowMessage(
86
+ pi: ExtensionAPI,
87
+ content: string,
88
+ kind: ContextManagementMessageKind,
89
+ identity: ContextWindowIdentity,
90
+ options: { triggerTurn: boolean; sessionId?: string },
91
+ trimPreviousWindow = false,
92
+ ): void {
93
+ const details: CodexContextManagementMessageDetails = {
94
+ protocol: CONTEXT_MANAGEMENT_PROTOCOL,
95
+ id: randomUUID(),
96
+ ...(options.sessionId ? { sessionId: options.sessionId } : {}),
97
+ contextManagement: {
98
+ protocol: CONTEXT_MANAGEMENT_PROTOCOL,
99
+ kind,
100
+ ...identity,
101
+ ...(trimPreviousWindow ? { trimPreviousWindow: true as const } : {}),
102
+ },
103
+ };
104
+ pi.sendMessage({
105
+ customType: CODEX_CONTEXT_WINDOW_MESSAGE_TYPE,
106
+ content,
107
+ display: true,
108
+ details,
109
+ }, options.triggerTurn
110
+ ? { deliverAs: "steer", triggerTurn: true }
111
+ : { triggerTurn: false });
112
+ }