@oh-my-pi/pi-coding-agent 15.0.2 → 15.1.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.
- package/CHANGELOG.md +56 -1
- package/examples/custom-tools/README.md +11 -7
- package/examples/custom-tools/hello/index.ts +2 -2
- package/examples/extensions/README.md +19 -8
- package/examples/extensions/api-demo.ts +15 -19
- package/examples/extensions/hello.ts +5 -6
- package/examples/extensions/plan-mode.ts +1 -1
- package/examples/extensions/reload-runtime.ts +4 -3
- package/examples/extensions/with-deps/index.ts +4 -3
- package/examples/sdk/06-extensions.ts +4 -2
- package/package.json +7 -17
- package/src/autoresearch/tools/init-experiment.ts +38 -41
- package/src/autoresearch/tools/log-experiment.ts +32 -41
- package/src/autoresearch/tools/run-experiment.ts +3 -3
- package/src/autoresearch/tools/update-notes.ts +11 -11
- package/src/commit/agentic/tools/analyze-file.ts +4 -4
- package/src/commit/agentic/tools/git-file-diff.ts +4 -4
- package/src/commit/agentic/tools/git-hunk.ts +5 -5
- package/src/commit/agentic/tools/git-overview.ts +4 -4
- package/src/commit/agentic/tools/propose-changelog.ts +13 -13
- package/src/commit/agentic/tools/propose-commit.ts +6 -6
- package/src/commit/agentic/tools/recent-commits.ts +3 -3
- package/src/commit/agentic/tools/schemas.ts +28 -28
- package/src/commit/agentic/tools/split-commit.ts +22 -21
- package/src/commit/analysis/summary.ts +4 -4
- package/src/commit/changelog/generate.ts +7 -11
- package/src/commit/shared-llm.ts +22 -34
- package/src/config/config-file.ts +35 -13
- package/src/config/model-registry.ts +9 -190
- package/src/config/models-config-schema.ts +166 -0
- package/src/config/settings-schema.ts +18 -0
- package/src/edit/index.ts +2 -2
- package/src/edit/modes/apply-patch.ts +7 -6
- package/src/edit/modes/patch.ts +18 -25
- package/src/edit/modes/replace.ts +18 -20
- package/src/eval/js/shared/rewrite-imports.ts +131 -10
- package/src/eval/py/executor.ts +233 -623
- package/src/eval/py/kernel.ts +27 -2
- package/src/exa/factory.ts +5 -4
- package/src/exa/mcp-client.ts +1 -1
- package/src/exa/researcher.ts +9 -20
- package/src/exa/search.ts +26 -52
- package/src/exa/types.ts +1 -1
- package/src/exa/websets.ts +54 -53
- package/src/exec/bash-executor.ts +2 -1
- package/src/extensibility/custom-commands/loader.ts +5 -3
- package/src/extensibility/custom-commands/types.ts +4 -2
- package/src/extensibility/custom-tools/loader.ts +5 -3
- package/src/extensibility/custom-tools/types.ts +7 -6
- package/src/extensibility/custom-tools/wrapper.ts +1 -1
- package/src/extensibility/extensions/loader.ts +7 -3
- package/src/extensibility/extensions/types.ts +9 -5
- package/src/extensibility/extensions/wrapper.ts +1 -2
- package/src/extensibility/hooks/loader.ts +3 -1
- package/src/extensibility/hooks/tool-wrapper.ts +1 -1
- package/src/extensibility/hooks/types.ts +4 -2
- package/src/extensibility/plugins/legacy-pi-compat.ts +30 -0
- package/src/extensibility/shared-events.ts +1 -1
- package/src/extensibility/typebox.ts +391 -0
- package/src/goals/tools/goal-tool.ts +6 -12
- package/src/hashline/types.ts +4 -4
- package/src/hindsight/state.ts +2 -2
- package/src/index.ts +0 -2
- package/src/internal-urls/docs-index.generated.ts +7 -7
- package/src/lsp/types.ts +30 -38
- package/src/mcp/manager.ts +1 -1
- package/src/mcp/tool-bridge.ts +1 -1
- package/src/modes/components/session-observer-overlay.ts +12 -1
- package/src/modes/components/status-line/segments.ts +2 -1
- package/src/modes/controllers/command-controller.ts +27 -2
- package/src/modes/controllers/event-controller.ts +3 -4
- package/src/modes/interactive-mode.ts +1 -1
- package/src/modes/rpc/host-tools.ts +1 -1
- package/src/modes/rpc/rpc-client.ts +1 -1
- package/src/modes/rpc/rpc-types.ts +1 -1
- package/src/modes/theme/theme.ts +111 -117
- package/src/modes/types.ts +1 -1
- package/src/modes/utils/context-usage.ts +2 -2
- package/src/sdk.ts +31 -8
- package/src/session/agent-session.ts +74 -104
- package/src/session/messages.ts +16 -51
- package/src/session/session-manager.ts +22 -2
- package/src/session/streaming-output.ts +16 -6
- package/src/task/executor.ts +208 -86
- package/src/task/index.ts +15 -11
- package/src/task/render.ts +32 -5
- package/src/task/types.ts +54 -39
- package/src/tools/ask.ts +12 -12
- package/src/tools/ast-edit.ts +11 -15
- package/src/tools/ast-grep.ts +9 -10
- package/src/tools/bash.ts +9 -23
- package/src/tools/browser.ts +39 -53
- package/src/tools/calculator.ts +12 -11
- package/src/tools/checkpoint.ts +7 -7
- package/src/tools/debug.ts +40 -43
- package/src/tools/eval.ts +6 -8
- package/src/tools/find.ts +10 -13
- package/src/tools/gh.ts +71 -128
- package/src/tools/hindsight-recall.ts +4 -6
- package/src/tools/hindsight-reflect.ts +5 -5
- package/src/tools/hindsight-retain.ts +15 -17
- package/src/tools/image-gen.ts +32 -82
- package/src/tools/index.ts +4 -1
- package/src/tools/inspect-image.ts +8 -9
- package/src/tools/irc.ts +15 -27
- package/src/tools/job.ts +14 -21
- package/src/tools/read.ts +7 -8
- package/src/tools/recipe/index.ts +7 -9
- package/src/tools/render-mermaid.ts +12 -12
- package/src/tools/report-tool-issue.ts +4 -4
- package/src/tools/resolve.ts +11 -11
- package/src/tools/review.ts +14 -26
- package/src/tools/search-tool-bm25.ts +7 -9
- package/src/tools/search.ts +19 -22
- package/src/tools/ssh.ts +7 -7
- package/src/tools/todo-write.ts +26 -34
- package/src/tools/vim.ts +10 -26
- package/src/tools/write.ts +5 -5
- package/src/tools/yield.ts +100 -54
- package/src/web/search/index.ts +9 -24
- package/src/prompts/compaction/branch-summary-context.md +0 -5
- package/src/prompts/compaction/branch-summary-preamble.md +0 -2
- package/src/prompts/compaction/branch-summary.md +0 -30
- package/src/prompts/compaction/compaction-short-summary.md +0 -9
- package/src/prompts/compaction/compaction-summary-context.md +0 -5
- package/src/prompts/compaction/compaction-summary.md +0 -38
- package/src/prompts/compaction/compaction-turn-prefix.md +0 -17
- package/src/prompts/compaction/compaction-update-summary.md +0 -45
- package/src/prompts/system/auto-handoff-threshold-focus.md +0 -1
- package/src/prompts/system/file-operations.md +0 -10
- package/src/prompts/system/handoff-document.md +0 -49
- package/src/prompts/system/summarization-system.md +0 -3
- package/src/session/compaction/branch-summarization.ts +0 -324
- package/src/session/compaction/compaction.ts +0 -1420
- package/src/session/compaction/errors.ts +0 -31
- package/src/session/compaction/index.ts +0 -8
- package/src/session/compaction/pruning.ts +0 -91
- package/src/session/compaction/utils.ts +0 -184
|
@@ -1,1420 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Context compaction for long sessions.
|
|
3
|
-
*
|
|
4
|
-
* Pure functions for compaction logic. The session manager handles I/O,
|
|
5
|
-
* and after compaction the session is reloaded.
|
|
6
|
-
*/
|
|
7
|
-
import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
|
|
8
|
-
import {
|
|
9
|
-
type AssistantMessage,
|
|
10
|
-
completeSimple,
|
|
11
|
-
Effort,
|
|
12
|
-
type MessageAttribution,
|
|
13
|
-
type Model,
|
|
14
|
-
type Usage,
|
|
15
|
-
} from "@oh-my-pi/pi-ai";
|
|
16
|
-
import {
|
|
17
|
-
CODEX_BASE_URL,
|
|
18
|
-
getCodexAccountId,
|
|
19
|
-
OPENAI_HEADER_VALUES,
|
|
20
|
-
OPENAI_HEADERS,
|
|
21
|
-
} from "@oh-my-pi/pi-ai/providers/openai-codex/constants";
|
|
22
|
-
import { parseTextSignature } from "@oh-my-pi/pi-ai/providers/openai-responses-shared";
|
|
23
|
-
import { transformMessages } from "@oh-my-pi/pi-ai/providers/transform-messages";
|
|
24
|
-
import {
|
|
25
|
-
getOpenAIResponsesHistoryItems,
|
|
26
|
-
getOpenAIResponsesHistoryPayload,
|
|
27
|
-
normalizeResponsesToolCallId,
|
|
28
|
-
} from "@oh-my-pi/pi-ai/utils";
|
|
29
|
-
import { countTokens } from "@oh-my-pi/pi-natives";
|
|
30
|
-
import { logger, prompt } from "@oh-my-pi/pi-utils";
|
|
31
|
-
import compactionShortSummaryPrompt from "../../prompts/compaction/compaction-short-summary.md" with { type: "text" };
|
|
32
|
-
import compactionSummaryPrompt from "../../prompts/compaction/compaction-summary.md" with { type: "text" };
|
|
33
|
-
import compactionTurnPrefixPrompt from "../../prompts/compaction/compaction-turn-prefix.md" with { type: "text" };
|
|
34
|
-
import compactionUpdateSummaryPrompt from "../../prompts/compaction/compaction-update-summary.md" with { type: "text" };
|
|
35
|
-
import { convertToLlm, createBranchSummaryMessage, createCustomMessage } from "../../session/messages";
|
|
36
|
-
import type { CompactionEntry, SessionEntry } from "../../session/session-manager";
|
|
37
|
-
|
|
38
|
-
import {
|
|
39
|
-
computeFileLists,
|
|
40
|
-
createFileOps,
|
|
41
|
-
extractFileOpsFromMessage,
|
|
42
|
-
type FileOperations,
|
|
43
|
-
SUMMARIZATION_SYSTEM_PROMPT,
|
|
44
|
-
serializeConversation,
|
|
45
|
-
upsertFileOperations,
|
|
46
|
-
} from "./utils";
|
|
47
|
-
|
|
48
|
-
// ============================================================================
|
|
49
|
-
// File Operation Tracking
|
|
50
|
-
// ============================================================================
|
|
51
|
-
|
|
52
|
-
/** Details stored in CompactionEntry.details for file tracking */
|
|
53
|
-
export interface CompactionDetails {
|
|
54
|
-
readFiles: string[];
|
|
55
|
-
modifiedFiles: string[];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Extract file operations from messages and previous compaction entries.
|
|
60
|
-
*/
|
|
61
|
-
function extractFileOperations(
|
|
62
|
-
messages: AgentMessage[],
|
|
63
|
-
entries: SessionEntry[],
|
|
64
|
-
prevCompactionIndex: number,
|
|
65
|
-
): FileOperations {
|
|
66
|
-
const fileOps = createFileOps();
|
|
67
|
-
|
|
68
|
-
// Collect from previous compaction's details (if pi-generated)
|
|
69
|
-
if (prevCompactionIndex >= 0) {
|
|
70
|
-
const prevCompaction = entries[prevCompactionIndex] as CompactionEntry;
|
|
71
|
-
if (!prevCompaction.fromExtension && prevCompaction.details) {
|
|
72
|
-
const details = prevCompaction.details as CompactionDetails;
|
|
73
|
-
if (Array.isArray(details.readFiles)) {
|
|
74
|
-
for (const f of details.readFiles) fileOps.read.add(f);
|
|
75
|
-
}
|
|
76
|
-
if (Array.isArray(details.modifiedFiles)) {
|
|
77
|
-
for (const f of details.modifiedFiles) fileOps.edited.add(f);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Extract from tool calls in messages
|
|
83
|
-
for (const msg of messages) {
|
|
84
|
-
extractFileOpsFromMessage(msg, fileOps);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return fileOps;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// ============================================================================
|
|
91
|
-
// Message Extraction
|
|
92
|
-
// ============================================================================
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Extract AgentMessage from an entry if it produces one.
|
|
96
|
-
* Returns undefined for entries that don't contribute to LLM context.
|
|
97
|
-
*/
|
|
98
|
-
function getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {
|
|
99
|
-
if (entry.type === "message") {
|
|
100
|
-
return entry.message;
|
|
101
|
-
}
|
|
102
|
-
if (entry.type === "custom_message") {
|
|
103
|
-
return createCustomMessage(
|
|
104
|
-
entry.customType,
|
|
105
|
-
entry.content,
|
|
106
|
-
entry.display,
|
|
107
|
-
entry.details,
|
|
108
|
-
entry.timestamp,
|
|
109
|
-
entry.attribution,
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
if (entry.type === "branch_summary") {
|
|
113
|
-
return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
|
|
114
|
-
}
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** Result from compact() - SessionManager adds uuid/parentUuid when saving */
|
|
119
|
-
export interface CompactionResult<T = unknown> {
|
|
120
|
-
summary: string;
|
|
121
|
-
/** Short PR-style summary for display purposes. */
|
|
122
|
-
shortSummary?: string;
|
|
123
|
-
firstKeptEntryId: string;
|
|
124
|
-
tokensBefore: number;
|
|
125
|
-
/** Hook-specific data (e.g., ArtifactIndex, version markers for structured compaction) */
|
|
126
|
-
details?: T;
|
|
127
|
-
/** Hook-provided data to persist alongside compaction entry. */
|
|
128
|
-
preserveData?: Record<string, unknown>;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// ============================================================================
|
|
132
|
-
// Types
|
|
133
|
-
// ============================================================================
|
|
134
|
-
|
|
135
|
-
export interface CompactionSettings {
|
|
136
|
-
enabled: boolean;
|
|
137
|
-
strategy?: "context-full" | "handoff" | "off";
|
|
138
|
-
thresholdPercent?: number;
|
|
139
|
-
thresholdTokens?: number;
|
|
140
|
-
reserveTokens: number;
|
|
141
|
-
keepRecentTokens: number;
|
|
142
|
-
autoContinue?: boolean;
|
|
143
|
-
remoteEnabled?: boolean;
|
|
144
|
-
remoteEndpoint?: string;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {
|
|
148
|
-
enabled: true,
|
|
149
|
-
strategy: "context-full",
|
|
150
|
-
thresholdPercent: -1,
|
|
151
|
-
thresholdTokens: -1,
|
|
152
|
-
reserveTokens: 16384,
|
|
153
|
-
keepRecentTokens: 20000,
|
|
154
|
-
autoContinue: true,
|
|
155
|
-
remoteEnabled: true,
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
// ============================================================================
|
|
159
|
-
// Token calculation
|
|
160
|
-
// ============================================================================
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Calculate total context tokens from usage.
|
|
164
|
-
* Uses the native totalTokens field when available, falls back to computing from components.
|
|
165
|
-
*/
|
|
166
|
-
export function calculateContextTokens(usage: Usage): number {
|
|
167
|
-
return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export function calculatePromptTokens(usage: Usage): number {
|
|
171
|
-
const promptTokens = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
172
|
-
if (promptTokens > 0) {
|
|
173
|
-
return promptTokens;
|
|
174
|
-
}
|
|
175
|
-
return calculateContextTokens(usage);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Get usage from an assistant message if available.
|
|
180
|
-
* Skips aborted and error messages as they don't have valid usage data.
|
|
181
|
-
*/
|
|
182
|
-
function getAssistantUsage(msg: AgentMessage): Usage | undefined {
|
|
183
|
-
if (msg.role === "assistant" && "usage" in msg) {
|
|
184
|
-
const assistantMsg = msg as AssistantMessage;
|
|
185
|
-
if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
|
|
186
|
-
return assistantMsg.usage;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return undefined;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Find the last non-aborted assistant message usage from session entries.
|
|
194
|
-
*/
|
|
195
|
-
export function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined {
|
|
196
|
-
for (let i = entries.length - 1; i >= 0; i--) {
|
|
197
|
-
const entry = entries[i];
|
|
198
|
-
if (entry.type === "message") {
|
|
199
|
-
const usage = getAssistantUsage(entry.message);
|
|
200
|
-
if (usage) return usage;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return undefined;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Effective reserve: at least 15% of context window or the configured floor, whichever is larger.
|
|
208
|
-
*/
|
|
209
|
-
export function effectiveReserveTokens(contextWindow: number, settings: CompactionSettings): number {
|
|
210
|
-
return Math.max(Math.floor(contextWindow * 0.15), settings.reserveTokens);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Check if compaction should trigger based on context usage.
|
|
215
|
-
*/
|
|
216
|
-
export function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean {
|
|
217
|
-
if (!settings.enabled || settings.strategy === "off" || contextWindow <= 0) return false;
|
|
218
|
-
const thresholdTokens = resolveThresholdTokens(contextWindow, settings);
|
|
219
|
-
return contextTokens > thresholdTokens;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function resolveThresholdTokens(contextWindow: number, settings: CompactionSettings): number {
|
|
223
|
-
// Fixed token limit takes priority over percentage
|
|
224
|
-
const thresholdTokens = settings.thresholdTokens;
|
|
225
|
-
if (typeof thresholdTokens === "number" && Number.isFinite(thresholdTokens) && thresholdTokens > 0) {
|
|
226
|
-
// Clamp to [1, contextWindow - 1] so there's always room
|
|
227
|
-
return Math.min(contextWindow - 1, Math.max(1, thresholdTokens));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Percentage-based threshold
|
|
231
|
-
const thresholdPercent = settings.thresholdPercent;
|
|
232
|
-
if (typeof thresholdPercent !== "number" || !Number.isFinite(thresholdPercent) || thresholdPercent <= 0) {
|
|
233
|
-
return contextWindow - effectiveReserveTokens(contextWindow, settings);
|
|
234
|
-
}
|
|
235
|
-
const clampedThresholdPercent = Math.min(99, Math.max(1, thresholdPercent));
|
|
236
|
-
return Math.floor(contextWindow * (clampedThresholdPercent / 100));
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// ============================================================================
|
|
240
|
-
// Cut point detection
|
|
241
|
-
// ============================================================================
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Image content has no tokenizer representation; charge a fixed estimate
|
|
245
|
-
* matching what providers typically bill for inline images.
|
|
246
|
-
*/
|
|
247
|
-
const IMAGE_TOKEN_ESTIMATE = 1200;
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Estimate token count for a message using cl100k_base via the native
|
|
251
|
-
* tokenizer. This is not Claude's first-party tokenizer (Anthropic doesn't
|
|
252
|
-
* publish one) but is within ~5–10% across English/code text.
|
|
253
|
-
*/
|
|
254
|
-
export function estimateTokens(message: AgentMessage): number {
|
|
255
|
-
const fragments: string[] = [];
|
|
256
|
-
let extra = 0;
|
|
257
|
-
|
|
258
|
-
switch (message.role) {
|
|
259
|
-
case "user": {
|
|
260
|
-
const content = (message as { content: string | Array<{ type: string; text?: string }> }).content;
|
|
261
|
-
if (typeof content === "string") {
|
|
262
|
-
fragments.push(content);
|
|
263
|
-
} else if (Array.isArray(content)) {
|
|
264
|
-
for (const block of content) {
|
|
265
|
-
if (block.type === "text" && block.text) {
|
|
266
|
-
fragments.push(block.text);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
case "assistant": {
|
|
273
|
-
const assistant = message as AssistantMessage;
|
|
274
|
-
for (const block of assistant.content) {
|
|
275
|
-
if (block.type === "text") {
|
|
276
|
-
fragments.push(block.text);
|
|
277
|
-
} else if (block.type === "thinking") {
|
|
278
|
-
fragments.push(block.thinking);
|
|
279
|
-
} else if (block.type === "toolCall") {
|
|
280
|
-
fragments.push(block.name);
|
|
281
|
-
fragments.push(JSON.stringify(block.arguments));
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
case "hookMessage":
|
|
287
|
-
case "toolResult": {
|
|
288
|
-
if (typeof message.content === "string") {
|
|
289
|
-
fragments.push(message.content);
|
|
290
|
-
} else {
|
|
291
|
-
for (const block of message.content) {
|
|
292
|
-
if (block.type === "text" && block.text) {
|
|
293
|
-
fragments.push(block.text);
|
|
294
|
-
} else if (block.type === "image") {
|
|
295
|
-
extra += IMAGE_TOKEN_ESTIMATE;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
case "bashExecution": {
|
|
302
|
-
fragments.push(message.command);
|
|
303
|
-
fragments.push(message.output);
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
case "branchSummary":
|
|
307
|
-
case "compactionSummary": {
|
|
308
|
-
fragments.push(message.summary);
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
default:
|
|
312
|
-
return 0;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
if (fragments.length === 0) return extra;
|
|
316
|
-
return extra + countTokens(fragments);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function estimateEntriesTokens(entries: SessionEntry[], startIndex: number, endIndex: number): number {
|
|
320
|
-
let total = 0;
|
|
321
|
-
for (let i = startIndex; i < endIndex; i++) {
|
|
322
|
-
const msg = getMessageFromEntry(entries[i]);
|
|
323
|
-
if (msg) {
|
|
324
|
-
total += estimateTokens(msg);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return total;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
|
|
332
|
-
* Never cut at tool results (they must follow their tool call).
|
|
333
|
-
* When we cut at an assistant message with tool calls, its tool results follow it
|
|
334
|
-
* and will be kept.
|
|
335
|
-
* BashExecutionMessage is treated like a user message (user-initiated context).
|
|
336
|
-
*/
|
|
337
|
-
function findValidCutPoints(entries: SessionEntry[], startIndex: number, endIndex: number): number[] {
|
|
338
|
-
const cutPoints: number[] = [];
|
|
339
|
-
for (let i = startIndex; i < endIndex; i++) {
|
|
340
|
-
const entry = entries[i];
|
|
341
|
-
switch (entry.type) {
|
|
342
|
-
case "message": {
|
|
343
|
-
const role = entry.message.role;
|
|
344
|
-
switch (role) {
|
|
345
|
-
case "bashExecution":
|
|
346
|
-
case "hookMessage":
|
|
347
|
-
case "branchSummary":
|
|
348
|
-
case "compactionSummary":
|
|
349
|
-
case "user":
|
|
350
|
-
case "assistant":
|
|
351
|
-
cutPoints.push(i);
|
|
352
|
-
break;
|
|
353
|
-
case "toolResult":
|
|
354
|
-
break;
|
|
355
|
-
}
|
|
356
|
-
break;
|
|
357
|
-
}
|
|
358
|
-
case "thinking_level_change":
|
|
359
|
-
case "model_change":
|
|
360
|
-
case "compaction":
|
|
361
|
-
case "branch_summary":
|
|
362
|
-
case "custom":
|
|
363
|
-
case "custom_message":
|
|
364
|
-
case "label":
|
|
365
|
-
}
|
|
366
|
-
// branch_summary and custom_message are user-role messages, valid cut points
|
|
367
|
-
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
368
|
-
cutPoints.push(i);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return cutPoints;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
376
|
-
* Returns -1 if no turn start found before the index.
|
|
377
|
-
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
378
|
-
*/
|
|
379
|
-
export function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number {
|
|
380
|
-
for (let i = entryIndex; i >= startIndex; i--) {
|
|
381
|
-
const entry = entries[i];
|
|
382
|
-
// branch_summary and custom_message are user-role messages, can start a turn
|
|
383
|
-
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
384
|
-
return i;
|
|
385
|
-
}
|
|
386
|
-
if (entry.type === "message") {
|
|
387
|
-
const role = entry.message.role;
|
|
388
|
-
if (role === "user" || role === "bashExecution") {
|
|
389
|
-
return i;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return -1;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export interface CutPointResult {
|
|
397
|
-
/** Index of first entry to keep */
|
|
398
|
-
firstKeptEntryIndex: number;
|
|
399
|
-
/** Index of user message that starts the turn being split, or -1 if not splitting */
|
|
400
|
-
turnStartIndex: number;
|
|
401
|
-
/** Whether this cut splits a turn (cut point is not a user message) */
|
|
402
|
-
isSplitTurn: boolean;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
407
|
-
*
|
|
408
|
-
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
409
|
-
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
410
|
-
*
|
|
411
|
-
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
412
|
-
* assistant message with tool calls, its tool results come after and will be kept.
|
|
413
|
-
*
|
|
414
|
-
* Returns CutPointResult with:
|
|
415
|
-
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
416
|
-
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
417
|
-
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
418
|
-
*
|
|
419
|
-
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
420
|
-
*/
|
|
421
|
-
export function findCutPoint(
|
|
422
|
-
entries: SessionEntry[],
|
|
423
|
-
startIndex: number,
|
|
424
|
-
endIndex: number,
|
|
425
|
-
keepRecentTokens: number,
|
|
426
|
-
): CutPointResult {
|
|
427
|
-
const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
|
|
428
|
-
|
|
429
|
-
if (cutPoints.length === 0) {
|
|
430
|
-
return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Walk backwards from newest, accumulating estimated message sizes
|
|
434
|
-
let accumulatedTokens = 0;
|
|
435
|
-
let cutIndex = cutPoints[0]; // Default: keep from first message (not header)
|
|
436
|
-
|
|
437
|
-
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
438
|
-
const entry = entries[i];
|
|
439
|
-
if (entry.type !== "message") continue;
|
|
440
|
-
|
|
441
|
-
// Estimate this message's size
|
|
442
|
-
const messageTokens = estimateTokens(entry.message);
|
|
443
|
-
accumulatedTokens += messageTokens;
|
|
444
|
-
|
|
445
|
-
// Check if we've exceeded the budget
|
|
446
|
-
if (accumulatedTokens >= keepRecentTokens) {
|
|
447
|
-
// Find the closest valid cut point at or after this entry
|
|
448
|
-
for (let c = 0; c < cutPoints.length; c++) {
|
|
449
|
-
if (cutPoints[c] >= i) {
|
|
450
|
-
cutIndex = cutPoints[c];
|
|
451
|
-
break;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
break;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
|
|
459
|
-
while (cutIndex > startIndex) {
|
|
460
|
-
const prevEntry = entries[cutIndex - 1];
|
|
461
|
-
// Stop at session header or compaction boundaries
|
|
462
|
-
if (prevEntry.type === "compaction") {
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
if (prevEntry.type === "message") {
|
|
466
|
-
// Stop if we hit any message
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
// Include this non-message entry (bash, settings change, etc.)
|
|
470
|
-
cutIndex--;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// Determine if this is a split turn
|
|
474
|
-
const cutEntry = entries[cutIndex];
|
|
475
|
-
const isUserMessage = cutEntry.type === "message" && cutEntry.message.role === "user";
|
|
476
|
-
const turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
|
|
477
|
-
|
|
478
|
-
return {
|
|
479
|
-
firstKeptEntryIndex: cutIndex,
|
|
480
|
-
turnStartIndex,
|
|
481
|
-
isSplitTurn: !isUserMessage && turnStartIndex !== -1,
|
|
482
|
-
};
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// ============================================================================
|
|
486
|
-
// Summarization
|
|
487
|
-
// ============================================================================
|
|
488
|
-
|
|
489
|
-
const SUMMARIZATION_PROMPT = prompt.render(compactionSummaryPrompt);
|
|
490
|
-
|
|
491
|
-
const UPDATE_SUMMARIZATION_PROMPT = prompt.render(compactionUpdateSummaryPrompt);
|
|
492
|
-
|
|
493
|
-
const SHORT_SUMMARY_PROMPT = prompt.render(compactionShortSummaryPrompt);
|
|
494
|
-
|
|
495
|
-
function formatAdditionalContext(context: string[] | undefined): string {
|
|
496
|
-
if (!context || context.length === 0) return "";
|
|
497
|
-
const lines = context.map(line => `- ${line}`).join("\n");
|
|
498
|
-
return `<additional-context>\n${lines}\n</additional-context>\n\n`;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
const OPENAI_REMOTE_COMPACTION_PRESERVE_KEY = "openaiRemoteCompaction";
|
|
502
|
-
|
|
503
|
-
type OpenAiRemoteCompactionItem = {
|
|
504
|
-
type: "compaction" | "compaction_summary";
|
|
505
|
-
encrypted_content?: string;
|
|
506
|
-
summary?: string;
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
interface OpenAiRemoteCompactionPreserveData {
|
|
510
|
-
provider?: string;
|
|
511
|
-
replacementHistory: Array<Record<string, unknown>>;
|
|
512
|
-
compactionItem: OpenAiRemoteCompactionItem;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
interface OpenAiRemoteCompactionRequest {
|
|
516
|
-
model: string;
|
|
517
|
-
input: Array<Record<string, unknown>>;
|
|
518
|
-
instructions: string;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
interface OpenAiRemoteCompactionResponse extends OpenAiRemoteCompactionPreserveData {}
|
|
522
|
-
|
|
523
|
-
interface RemoteCompactionResponse {
|
|
524
|
-
summary: string;
|
|
525
|
-
shortSummary?: string;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function shouldUseOpenAiRemoteCompaction(model: Model): boolean {
|
|
529
|
-
return model.provider === "openai" || model.provider === "openai-codex";
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function resolveOpenAiCompactEndpoint(model: Model): string {
|
|
533
|
-
if (model.provider === "openai-codex") {
|
|
534
|
-
return resolveOpenAiCodexCompactEndpoint(model.baseUrl);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const defaultBase = "https://api.openai.com/v1";
|
|
538
|
-
const rawBase = model.baseUrl && model.baseUrl.length > 0 ? model.baseUrl : defaultBase;
|
|
539
|
-
const normalizedBase = rawBase.endsWith("/") ? rawBase.slice(0, -1) : rawBase;
|
|
540
|
-
if (normalizedBase.endsWith("/v1")) return `${normalizedBase}/responses/compact`;
|
|
541
|
-
return `${normalizedBase}/v1/responses/compact`;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
function resolveOpenAiCodexCompactEndpoint(baseUrl: string | undefined): string {
|
|
545
|
-
const rawBase = baseUrl && baseUrl.length > 0 ? baseUrl : CODEX_BASE_URL;
|
|
546
|
-
const normalizedBase = rawBase.endsWith("/") ? rawBase.slice(0, -1) : rawBase;
|
|
547
|
-
if (/\/codex(?:\/v\d+)?$/.test(normalizedBase)) return `${normalizedBase}/responses/compact`;
|
|
548
|
-
return `${normalizedBase}/codex/responses/compact`;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function normalizeOpenAiCompactionToolCallId(id: string): string {
|
|
552
|
-
const normalized = normalizeResponsesToolCallId(id);
|
|
553
|
-
return `${normalized.callId}|${normalized.itemId ?? normalized.callId}`;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
function getPreservedOpenAiRemoteCompactionData(
|
|
557
|
-
preserveData: Record<string, unknown> | undefined,
|
|
558
|
-
): OpenAiRemoteCompactionPreserveData | undefined {
|
|
559
|
-
const candidate = preserveData?.[OPENAI_REMOTE_COMPACTION_PRESERVE_KEY];
|
|
560
|
-
if (!candidate || typeof candidate !== "object") return undefined;
|
|
561
|
-
const maybeData = candidate as { provider?: unknown; replacementHistory?: unknown; compactionItem?: unknown };
|
|
562
|
-
if (!Array.isArray(maybeData.replacementHistory)) return undefined;
|
|
563
|
-
const maybeItem = maybeData.compactionItem;
|
|
564
|
-
if (!maybeItem || typeof maybeItem !== "object") return undefined;
|
|
565
|
-
const compactionItem = maybeItem as { type?: unknown; encrypted_content?: unknown; summary?: unknown };
|
|
566
|
-
const isClassicCompaction =
|
|
567
|
-
compactionItem.type === "compaction" && typeof compactionItem.encrypted_content === "string";
|
|
568
|
-
const isSummaryCompaction = compactionItem.type === "compaction_summary";
|
|
569
|
-
if (!isClassicCompaction && !isSummaryCompaction) {
|
|
570
|
-
return undefined;
|
|
571
|
-
}
|
|
572
|
-
return {
|
|
573
|
-
provider: typeof maybeData.provider === "string" ? maybeData.provider : undefined,
|
|
574
|
-
replacementHistory: maybeData.replacementHistory as Array<Record<string, unknown>>,
|
|
575
|
-
compactionItem: compactionItem as unknown as OpenAiRemoteCompactionItem,
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
function withOpenAiRemoteCompactionPreserveData(
|
|
580
|
-
preserveData: Record<string, unknown> | undefined,
|
|
581
|
-
remoteCompaction: OpenAiRemoteCompactionPreserveData | undefined,
|
|
582
|
-
): Record<string, unknown> | undefined {
|
|
583
|
-
if (remoteCompaction) {
|
|
584
|
-
return {
|
|
585
|
-
...(preserveData ?? {}),
|
|
586
|
-
[OPENAI_REMOTE_COMPACTION_PRESERVE_KEY]: remoteCompaction,
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
if (!preserveData || !(OPENAI_REMOTE_COMPACTION_PRESERVE_KEY in preserveData)) {
|
|
591
|
-
return preserveData;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
const { [OPENAI_REMOTE_COMPACTION_PRESERVE_KEY]: _removed, ...rest } = preserveData;
|
|
595
|
-
return Object.keys(rest).length > 0 ? rest : undefined;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
function estimateOpenAiCompactInputTokens(input: Array<Record<string, unknown>>, instructions: string): number {
|
|
599
|
-
let chars = instructions.length;
|
|
600
|
-
for (const item of input) {
|
|
601
|
-
chars += JSON.stringify(item).length;
|
|
602
|
-
}
|
|
603
|
-
return Math.ceil(chars / 4);
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
function shouldTrimOpenAiCompactInputItem(item: Record<string, unknown>): boolean {
|
|
607
|
-
return item.type === "function_call_output" || (item.type === "message" && item.role === "developer");
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
function shouldKeepOpenAiCompactOutputUserMessage(item: Record<string, unknown>): boolean {
|
|
611
|
-
if (item.role !== "user") return false;
|
|
612
|
-
const content = item.content;
|
|
613
|
-
if (!Array.isArray(content) || content.length === 0) return false;
|
|
614
|
-
const contextualFragmentPatterns = [
|
|
615
|
-
[/^<system-reminder>[\s\S]*<\/system-reminder>$/i, /<system-reminder>/i],
|
|
616
|
-
[/^#\s*AGENTS\.md instructions for\b[\s\S]*<\/INSTRUCTIONS>$/i, /# AGENTS.md instructions/],
|
|
617
|
-
[/^<environment-context>[\s\S]*<\/environment-context>$/i, /<environment-context>/i],
|
|
618
|
-
[/^<skill>[\s\S]*<\/skill>$/i, /<skill>/i],
|
|
619
|
-
[/^<user-shell-command>[\s\S]*<\/user-shell-command>$/i, /<user-shell-command>/i],
|
|
620
|
-
[/^<turn-aborted>[\s\S]*<\/turn-aborted>$/i, /<turn-aborted>/i],
|
|
621
|
-
[/^<subagent-notification>[\s\S]*<\/subagent-notification>$/i, /<subagent-notification>/i],
|
|
622
|
-
] as const;
|
|
623
|
-
return content.every(part => {
|
|
624
|
-
if (!part || typeof part !== "object") return false;
|
|
625
|
-
const candidate = part as { type?: unknown; text?: unknown };
|
|
626
|
-
if (candidate.type === "input_image") return true;
|
|
627
|
-
if (candidate.type !== "input_text" || typeof candidate.text !== "string") return false;
|
|
628
|
-
const trimmed = candidate.text.trim();
|
|
629
|
-
if (trimmed.length === 0) return false;
|
|
630
|
-
return !contextualFragmentPatterns.some(([strictPattern, markerPattern]) => {
|
|
631
|
-
return strictPattern.test(trimmed) || markerPattern.test(trimmed);
|
|
632
|
-
});
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function shouldKeepOpenAiCompactOutputItem(item: Record<string, unknown>): boolean {
|
|
637
|
-
if (item.type === "compaction" || item.type === "compaction_summary") return true;
|
|
638
|
-
if (item.type !== "message") return false;
|
|
639
|
-
if (item.role === "developer") return false;
|
|
640
|
-
if (item.role === "assistant") return true;
|
|
641
|
-
return shouldKeepOpenAiCompactOutputUserMessage(item);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
function trimOpenAiCompactInput(
|
|
645
|
-
input: Array<Record<string, unknown>>,
|
|
646
|
-
contextWindow: number,
|
|
647
|
-
instructions: string,
|
|
648
|
-
): Array<Record<string, unknown>> {
|
|
649
|
-
const trimmed = [...input];
|
|
650
|
-
while (trimmed.length > 0 && estimateOpenAiCompactInputTokens(trimmed, instructions) > contextWindow) {
|
|
651
|
-
const last = trimmed[trimmed.length - 1];
|
|
652
|
-
if (last?.type === "function_call_output") {
|
|
653
|
-
const callId = typeof last.call_id === "string" ? last.call_id : undefined;
|
|
654
|
-
trimmed.pop();
|
|
655
|
-
if (callId) {
|
|
656
|
-
const matchingCallIndex = trimmed.findLastIndex(
|
|
657
|
-
item => item.type === "function_call" && item.call_id === callId,
|
|
658
|
-
);
|
|
659
|
-
if (matchingCallIndex >= 0) {
|
|
660
|
-
trimmed.splice(matchingCallIndex, 1);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
continue;
|
|
664
|
-
}
|
|
665
|
-
if (!last || !shouldTrimOpenAiCompactInputItem(last)) {
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
trimmed.pop();
|
|
669
|
-
}
|
|
670
|
-
return trimmed;
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
function collectKnownOpenAiCallIds(items: Array<Record<string, unknown>>): Set<string> {
|
|
674
|
-
const knownCallIds = new Set<string>();
|
|
675
|
-
for (const item of items) {
|
|
676
|
-
if (item.type === "function_call" && typeof item.call_id === "string") {
|
|
677
|
-
knownCallIds.add(item.call_id);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
return knownCallIds;
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
function buildOpenAiNativeHistory(
|
|
684
|
-
messages: AgentMessage[],
|
|
685
|
-
model: Model,
|
|
686
|
-
previousReplacementHistory?: Array<Record<string, unknown>>,
|
|
687
|
-
): Array<Record<string, unknown>> {
|
|
688
|
-
const input: Array<Record<string, unknown>> = previousReplacementHistory ? [...previousReplacementHistory] : [];
|
|
689
|
-
const transformedMessages = transformMessages(convertToLlm(messages), model, id =>
|
|
690
|
-
normalizeOpenAiCompactionToolCallId(id),
|
|
691
|
-
);
|
|
692
|
-
|
|
693
|
-
let msgIndex = 0;
|
|
694
|
-
let knownCallIds = collectKnownOpenAiCallIds(input);
|
|
695
|
-
for (const message of transformedMessages) {
|
|
696
|
-
if (message.role === "user" || message.role === "developer") {
|
|
697
|
-
const providerPayload = (message as { providerPayload?: AssistantMessage["providerPayload"] }).providerPayload;
|
|
698
|
-
const historyItems = getOpenAIResponsesHistoryItems(providerPayload, model.provider);
|
|
699
|
-
if (historyItems) {
|
|
700
|
-
input.push(...historyItems);
|
|
701
|
-
knownCallIds = collectKnownOpenAiCallIds(input);
|
|
702
|
-
msgIndex++;
|
|
703
|
-
continue;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
const contentBlocks: Array<Record<string, unknown>> = [];
|
|
707
|
-
if (typeof message.content === "string") {
|
|
708
|
-
if (message.content.trim().length > 0) {
|
|
709
|
-
contentBlocks.push({ type: "input_text", text: message.content.toWellFormed() });
|
|
710
|
-
}
|
|
711
|
-
} else {
|
|
712
|
-
for (const block of message.content) {
|
|
713
|
-
if (block.type === "text") {
|
|
714
|
-
if (!block.text || block.text.trim().length === 0) continue;
|
|
715
|
-
contentBlocks.push({ type: "input_text", text: block.text.toWellFormed() });
|
|
716
|
-
continue;
|
|
717
|
-
}
|
|
718
|
-
if (block.type === "image") {
|
|
719
|
-
contentBlocks.push({
|
|
720
|
-
type: "input_image",
|
|
721
|
-
detail: "auto",
|
|
722
|
-
image_url: `data:${block.mimeType};base64,${block.data}`,
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
if (contentBlocks.length > 0) {
|
|
728
|
-
input.push({ type: "message", role: message.role, content: contentBlocks });
|
|
729
|
-
}
|
|
730
|
-
msgIndex++;
|
|
731
|
-
continue;
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
if (message.role === "assistant") {
|
|
735
|
-
const assistant = message as AssistantMessage;
|
|
736
|
-
const providerPayload = getOpenAIResponsesHistoryPayload(
|
|
737
|
-
assistant.providerPayload,
|
|
738
|
-
model.provider,
|
|
739
|
-
assistant.provider,
|
|
740
|
-
);
|
|
741
|
-
if (providerPayload) {
|
|
742
|
-
if (providerPayload.dt) {
|
|
743
|
-
input.push(...providerPayload.items);
|
|
744
|
-
} else {
|
|
745
|
-
input.splice(0, input.length, ...providerPayload.items);
|
|
746
|
-
}
|
|
747
|
-
knownCallIds = collectKnownOpenAiCallIds(input);
|
|
748
|
-
msgIndex++;
|
|
749
|
-
continue;
|
|
750
|
-
}
|
|
751
|
-
const isDifferentModel =
|
|
752
|
-
assistant.model !== model.id && assistant.provider === model.provider && assistant.api === model.api;
|
|
753
|
-
|
|
754
|
-
for (const block of assistant.content) {
|
|
755
|
-
if (block.type === "thinking" && assistant.stopReason !== "error" && block.thinkingSignature) {
|
|
756
|
-
try {
|
|
757
|
-
const reasoningItem = JSON.parse(block.thinkingSignature) as Record<string, unknown>;
|
|
758
|
-
if (reasoningItem && typeof reasoningItem === "object") {
|
|
759
|
-
input.push(reasoningItem);
|
|
760
|
-
}
|
|
761
|
-
} catch {
|
|
762
|
-
logger.warn("Failed to parse assistant reasoning for remote compaction", {
|
|
763
|
-
model: assistant.model,
|
|
764
|
-
provider: assistant.provider,
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
continue;
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
if (block.type === "text") {
|
|
771
|
-
if (!block.text || block.text.trim().length === 0) continue;
|
|
772
|
-
const parsedSignature = parseTextSignature(block.textSignature);
|
|
773
|
-
let msgId = parsedSignature?.id;
|
|
774
|
-
if (!msgId) {
|
|
775
|
-
msgId = `msg_${msgIndex}`;
|
|
776
|
-
} else if (msgId.length > 64) {
|
|
777
|
-
msgId = `msg_${Bun.hash(msgId).toString(36)}`;
|
|
778
|
-
}
|
|
779
|
-
input.push({
|
|
780
|
-
type: "message",
|
|
781
|
-
role: "assistant",
|
|
782
|
-
content: [{ type: "output_text", text: block.text.toWellFormed(), annotations: [] }],
|
|
783
|
-
status: "completed",
|
|
784
|
-
id: msgId,
|
|
785
|
-
phase: parsedSignature?.phase,
|
|
786
|
-
});
|
|
787
|
-
continue;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
if (block.type === "toolCall") {
|
|
791
|
-
const normalized = normalizeResponsesToolCallId(block.id);
|
|
792
|
-
let itemId: string | undefined = normalized.itemId;
|
|
793
|
-
if (isDifferentModel && (itemId?.startsWith("fc_") || itemId?.startsWith("fcr_"))) {
|
|
794
|
-
itemId = undefined;
|
|
795
|
-
}
|
|
796
|
-
knownCallIds.add(normalized.callId);
|
|
797
|
-
input.push({
|
|
798
|
-
type: "function_call",
|
|
799
|
-
id: itemId,
|
|
800
|
-
call_id: normalized.callId,
|
|
801
|
-
name: block.name,
|
|
802
|
-
arguments: JSON.stringify(block.arguments),
|
|
803
|
-
});
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
msgIndex++;
|
|
808
|
-
continue;
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
if (message.role === "toolResult") {
|
|
812
|
-
const normalized = normalizeResponsesToolCallId(message.toolCallId);
|
|
813
|
-
if (!knownCallIds.has(normalized.callId)) {
|
|
814
|
-
msgIndex++;
|
|
815
|
-
continue;
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
const textOutput = message.content
|
|
819
|
-
.filter(block => block.type === "text")
|
|
820
|
-
.map(block => block.text)
|
|
821
|
-
.join("\n");
|
|
822
|
-
const hasImages = message.content.some(block => block.type === "image");
|
|
823
|
-
input.push({
|
|
824
|
-
type: "function_call_output",
|
|
825
|
-
call_id: normalized.callId,
|
|
826
|
-
output: (textOutput.length > 0 ? textOutput : "(see attached image)").toWellFormed(),
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
if (hasImages && model.input.includes("image")) {
|
|
830
|
-
const contentBlocks: Array<Record<string, unknown>> = [
|
|
831
|
-
{ type: "input_text", text: "Attached image(s) from tool result:" },
|
|
832
|
-
];
|
|
833
|
-
for (const block of message.content) {
|
|
834
|
-
if (block.type !== "image") continue;
|
|
835
|
-
contentBlocks.push({
|
|
836
|
-
type: "input_image",
|
|
837
|
-
detail: "auto",
|
|
838
|
-
image_url: `data:${block.mimeType};base64,${block.data}`,
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
input.push({ type: "message", role: "user", content: contentBlocks });
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
msgIndex++;
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
return input;
|
|
849
|
-
}
|
|
850
|
-
|
|
851
|
-
async function requestOpenAiRemoteCompaction(
|
|
852
|
-
model: Model,
|
|
853
|
-
apiKey: string,
|
|
854
|
-
compactInput: Array<Record<string, unknown>>,
|
|
855
|
-
instructions: string,
|
|
856
|
-
): Promise<OpenAiRemoteCompactionResponse> {
|
|
857
|
-
const endpoint = resolveOpenAiCompactEndpoint(model);
|
|
858
|
-
const request: OpenAiRemoteCompactionRequest = {
|
|
859
|
-
model: model.id,
|
|
860
|
-
input: trimOpenAiCompactInput(compactInput, model.contextWindow, instructions),
|
|
861
|
-
instructions,
|
|
862
|
-
};
|
|
863
|
-
const headers: Record<string, string> = {
|
|
864
|
-
"content-type": "application/json",
|
|
865
|
-
Authorization: `Bearer ${apiKey}`,
|
|
866
|
-
...(model.headers ?? {}),
|
|
867
|
-
};
|
|
868
|
-
|
|
869
|
-
// Codex endpoints require additional auth headers
|
|
870
|
-
if (model.provider === "openai-codex") {
|
|
871
|
-
const accountId = getCodexAccountId(apiKey);
|
|
872
|
-
if (accountId) {
|
|
873
|
-
headers[OPENAI_HEADERS.ACCOUNT_ID] = accountId;
|
|
874
|
-
}
|
|
875
|
-
headers[OPENAI_HEADERS.BETA] = OPENAI_HEADER_VALUES.BETA_RESPONSES;
|
|
876
|
-
headers[OPENAI_HEADERS.ORIGINATOR] = OPENAI_HEADER_VALUES.ORIGINATOR_CODEX;
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
const response = await fetch(endpoint, {
|
|
880
|
-
method: "POST",
|
|
881
|
-
headers,
|
|
882
|
-
body: JSON.stringify(request),
|
|
883
|
-
});
|
|
884
|
-
|
|
885
|
-
if (!response.ok) {
|
|
886
|
-
const errorText = await response.text().catch(() => "");
|
|
887
|
-
logger.warn("OpenAI remote compaction failed", {
|
|
888
|
-
endpoint,
|
|
889
|
-
status: response.status,
|
|
890
|
-
statusText: response.statusText,
|
|
891
|
-
errorText,
|
|
892
|
-
});
|
|
893
|
-
throw new Error(`Remote compaction failed (${response.status} ${response.statusText})`);
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
const data = (await response.json()) as { output?: unknown[] } | undefined;
|
|
897
|
-
const rawOutput = data?.output ?? [];
|
|
898
|
-
const replacementHistory = rawOutput.filter(
|
|
899
|
-
(item): item is Record<string, unknown> =>
|
|
900
|
-
!!item && typeof item === "object" && shouldKeepOpenAiCompactOutputItem(item as Record<string, unknown>),
|
|
901
|
-
);
|
|
902
|
-
const compactionItem = [...replacementHistory].reverse().find((item): item is OpenAiRemoteCompactionItem => {
|
|
903
|
-
if (item.type === "compaction" && typeof item.encrypted_content === "string") return true;
|
|
904
|
-
if (item.type === "compaction_summary") return true;
|
|
905
|
-
return false;
|
|
906
|
-
});
|
|
907
|
-
if (!compactionItem) {
|
|
908
|
-
const outputTypes = rawOutput.map(item =>
|
|
909
|
-
typeof item === "object" && item !== null ? (item as Record<string, unknown>).type : typeof item,
|
|
910
|
-
);
|
|
911
|
-
logger.warn("Remote compaction response missing compaction item", {
|
|
912
|
-
endpoint,
|
|
913
|
-
model: model.id,
|
|
914
|
-
provider: model.provider,
|
|
915
|
-
rawOutputLength: rawOutput.length,
|
|
916
|
-
outputTypes,
|
|
917
|
-
replacementHistoryLength: replacementHistory.length,
|
|
918
|
-
});
|
|
919
|
-
throw new Error("Remote compaction response missing compaction item");
|
|
920
|
-
}
|
|
921
|
-
return { provider: model.provider, replacementHistory, compactionItem };
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
interface RemoteCompactionRequest {
|
|
925
|
-
systemPrompt: string;
|
|
926
|
-
prompt: string;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
async function requestRemoteCompaction(
|
|
930
|
-
endpoint: string,
|
|
931
|
-
request: RemoteCompactionRequest,
|
|
932
|
-
): Promise<RemoteCompactionResponse> {
|
|
933
|
-
const response = await fetch(endpoint, {
|
|
934
|
-
method: "POST",
|
|
935
|
-
headers: { "content-type": "application/json" },
|
|
936
|
-
body: JSON.stringify(request),
|
|
937
|
-
});
|
|
938
|
-
|
|
939
|
-
if (!response.ok) {
|
|
940
|
-
const errorText = await response.text().catch(() => "");
|
|
941
|
-
logger.warn("Remote compaction failed", {
|
|
942
|
-
endpoint,
|
|
943
|
-
status: response.status,
|
|
944
|
-
statusText: response.statusText,
|
|
945
|
-
errorText,
|
|
946
|
-
});
|
|
947
|
-
throw new Error(`Remote compaction failed (${response.status} ${response.statusText})`);
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
const data = (await response.json()) as RemoteCompactionResponse | undefined;
|
|
951
|
-
if (!data || typeof data.summary !== "string") {
|
|
952
|
-
throw new Error("Remote compaction response missing summary");
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
return data;
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
/**
|
|
959
|
-
* Generate a summary of the conversation using the LLM.
|
|
960
|
-
* If previousSummary is provided, uses the update prompt to merge.
|
|
961
|
-
*/
|
|
962
|
-
export interface SummaryOptions {
|
|
963
|
-
promptOverride?: string;
|
|
964
|
-
extraContext?: string[];
|
|
965
|
-
remoteEndpoint?: string;
|
|
966
|
-
remoteInstructions?: string;
|
|
967
|
-
initiatorOverride?: MessageAttribution;
|
|
968
|
-
metadata?: Record<string, unknown>;
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
export async function generateSummary(
|
|
972
|
-
currentMessages: AgentMessage[],
|
|
973
|
-
model: Model,
|
|
974
|
-
reserveTokens: number,
|
|
975
|
-
apiKey: string,
|
|
976
|
-
signal?: AbortSignal,
|
|
977
|
-
customInstructions?: string,
|
|
978
|
-
previousSummary?: string,
|
|
979
|
-
options?: SummaryOptions,
|
|
980
|
-
): Promise<string> {
|
|
981
|
-
const maxTokens = Math.floor(0.8 * reserveTokens);
|
|
982
|
-
|
|
983
|
-
// Use update prompt if we have a previous summary, otherwise initial prompt
|
|
984
|
-
let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
|
|
985
|
-
if (options?.promptOverride) {
|
|
986
|
-
basePrompt = options.promptOverride;
|
|
987
|
-
}
|
|
988
|
-
if (customInstructions) {
|
|
989
|
-
basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
|
|
990
|
-
}
|
|
991
|
-
|
|
992
|
-
// Serialize conversation to text so model doesn't try to continue it
|
|
993
|
-
// Convert to LLM messages first (handles custom types like bashExecution, hookMessage, etc.)
|
|
994
|
-
const llmMessages = convertToLlm(currentMessages);
|
|
995
|
-
const conversationText = serializeConversation(llmMessages);
|
|
996
|
-
|
|
997
|
-
// Build the prompt with conversation wrapped in tags
|
|
998
|
-
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
999
|
-
if (previousSummary) {
|
|
1000
|
-
promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
|
|
1001
|
-
}
|
|
1002
|
-
promptText += formatAdditionalContext(options?.extraContext);
|
|
1003
|
-
promptText += basePrompt;
|
|
1004
|
-
|
|
1005
|
-
const summarizationMessages = [
|
|
1006
|
-
{
|
|
1007
|
-
role: "user" as const,
|
|
1008
|
-
content: [{ type: "text" as const, text: promptText }],
|
|
1009
|
-
timestamp: Date.now(),
|
|
1010
|
-
},
|
|
1011
|
-
];
|
|
1012
|
-
|
|
1013
|
-
if (options?.remoteEndpoint) {
|
|
1014
|
-
const remote = await requestRemoteCompaction(options.remoteEndpoint, {
|
|
1015
|
-
systemPrompt: SUMMARIZATION_SYSTEM_PROMPT,
|
|
1016
|
-
prompt: promptText,
|
|
1017
|
-
});
|
|
1018
|
-
return remote.summary;
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
const response = await completeSimple(
|
|
1022
|
-
model,
|
|
1023
|
-
{ systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
|
|
1024
|
-
{
|
|
1025
|
-
maxTokens,
|
|
1026
|
-
signal,
|
|
1027
|
-
apiKey,
|
|
1028
|
-
reasoning: Effort.High,
|
|
1029
|
-
initiatorOverride: options?.initiatorOverride,
|
|
1030
|
-
metadata: options?.metadata,
|
|
1031
|
-
},
|
|
1032
|
-
);
|
|
1033
|
-
|
|
1034
|
-
if (response.stopReason === "error") {
|
|
1035
|
-
throw new Error(`Summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
const textContent = response.content
|
|
1039
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
1040
|
-
.map(c => c.text)
|
|
1041
|
-
.join("\n");
|
|
1042
|
-
|
|
1043
|
-
return textContent;
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
|
-
async function generateShortSummary(
|
|
1047
|
-
recentMessages: AgentMessage[],
|
|
1048
|
-
historySummary: string | undefined,
|
|
1049
|
-
model: Model,
|
|
1050
|
-
reserveTokens: number,
|
|
1051
|
-
apiKey: string,
|
|
1052
|
-
signal?: AbortSignal,
|
|
1053
|
-
options?: SummaryOptions,
|
|
1054
|
-
): Promise<string> {
|
|
1055
|
-
const maxTokens = Math.min(512, Math.floor(0.2 * reserveTokens));
|
|
1056
|
-
const llmMessages = convertToLlm(recentMessages);
|
|
1057
|
-
const conversationText = serializeConversation(llmMessages);
|
|
1058
|
-
|
|
1059
|
-
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
1060
|
-
if (historySummary) {
|
|
1061
|
-
promptText += `<previous-summary>\n${historySummary}\n</previous-summary>\n\n`;
|
|
1062
|
-
}
|
|
1063
|
-
promptText += formatAdditionalContext(options?.extraContext);
|
|
1064
|
-
promptText += SHORT_SUMMARY_PROMPT;
|
|
1065
|
-
|
|
1066
|
-
if (options?.remoteEndpoint) {
|
|
1067
|
-
const remote = await requestRemoteCompaction(options.remoteEndpoint, {
|
|
1068
|
-
systemPrompt: SUMMARIZATION_SYSTEM_PROMPT,
|
|
1069
|
-
prompt: promptText,
|
|
1070
|
-
});
|
|
1071
|
-
return remote.summary;
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
const response = await completeSimple(
|
|
1075
|
-
model,
|
|
1076
|
-
{
|
|
1077
|
-
systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT],
|
|
1078
|
-
messages: [{ role: "user", content: [{ type: "text", text: promptText }], timestamp: Date.now() }],
|
|
1079
|
-
},
|
|
1080
|
-
{
|
|
1081
|
-
maxTokens,
|
|
1082
|
-
signal,
|
|
1083
|
-
apiKey,
|
|
1084
|
-
reasoning: Effort.High,
|
|
1085
|
-
initiatorOverride: options?.initiatorOverride,
|
|
1086
|
-
metadata: options?.metadata,
|
|
1087
|
-
},
|
|
1088
|
-
);
|
|
1089
|
-
|
|
1090
|
-
if (response.stopReason === "error") {
|
|
1091
|
-
throw new Error(`Short summary failed: ${response.errorMessage || "Unknown error"}`);
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
return response.content
|
|
1095
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
1096
|
-
.map(c => c.text)
|
|
1097
|
-
.join("\n");
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
// ============================================================================
|
|
1101
|
-
// Compaction Preparation (for hooks)
|
|
1102
|
-
// ============================================================================
|
|
1103
|
-
|
|
1104
|
-
export interface CompactionPreparation {
|
|
1105
|
-
/** UUID of first entry to keep */
|
|
1106
|
-
firstKeptEntryId: string;
|
|
1107
|
-
/** Messages that will be summarized and discarded */
|
|
1108
|
-
messagesToSummarize: AgentMessage[];
|
|
1109
|
-
/** Messages that will be turned into turn prefix summary (if splitting) */
|
|
1110
|
-
turnPrefixMessages: AgentMessage[];
|
|
1111
|
-
/** Messages kept in full after compaction (recent history) */
|
|
1112
|
-
recentMessages: AgentMessage[];
|
|
1113
|
-
/** Whether this is a split turn (cut point in middle of turn) */
|
|
1114
|
-
isSplitTurn: boolean;
|
|
1115
|
-
tokensBefore: number;
|
|
1116
|
-
/** Summary from previous compaction, for iterative update */
|
|
1117
|
-
previousSummary?: string;
|
|
1118
|
-
/** Preserved opaque compaction payload from the previous compaction, if any. */
|
|
1119
|
-
previousPreserveData?: Record<string, unknown>;
|
|
1120
|
-
/** File operations extracted from messagesToSummarize */
|
|
1121
|
-
fileOps: FileOperations;
|
|
1122
|
-
/** Compaction settions from settings.jsonl */
|
|
1123
|
-
settings: CompactionSettings;
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
export function prepareCompaction(
|
|
1127
|
-
pathEntries: SessionEntry[],
|
|
1128
|
-
settings: CompactionSettings,
|
|
1129
|
-
): CompactionPreparation | undefined {
|
|
1130
|
-
if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") {
|
|
1131
|
-
return undefined;
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
let prevCompactionIndex = -1;
|
|
1135
|
-
for (let i = pathEntries.length - 1; i >= 0; i--) {
|
|
1136
|
-
if (pathEntries[i].type === "compaction") {
|
|
1137
|
-
prevCompactionIndex = i;
|
|
1138
|
-
break;
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
const boundaryStart = prevCompactionIndex + 1;
|
|
1142
|
-
const boundaryEnd = pathEntries.length;
|
|
1143
|
-
|
|
1144
|
-
const lastUsage = getLastAssistantUsage(pathEntries);
|
|
1145
|
-
const tokensBefore = lastUsage ? calculateContextTokens(lastUsage) : 0;
|
|
1146
|
-
let keepRecentTokens = settings.keepRecentTokens;
|
|
1147
|
-
if (lastUsage) {
|
|
1148
|
-
const estimatedTokens = estimateEntriesTokens(pathEntries, boundaryStart, boundaryEnd);
|
|
1149
|
-
const promptTokens = calculatePromptTokens(lastUsage);
|
|
1150
|
-
const ratio = estimatedTokens > 0 ? promptTokens / estimatedTokens : 0;
|
|
1151
|
-
if (Number.isFinite(ratio) && ratio > 1) {
|
|
1152
|
-
keepRecentTokens = Math.max(1, Math.floor(keepRecentTokens / ratio));
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, keepRecentTokens);
|
|
1157
|
-
|
|
1158
|
-
// Get ID of first kept entry
|
|
1159
|
-
const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex];
|
|
1160
|
-
if (!firstKeptEntry?.id) {
|
|
1161
|
-
return undefined; // Session needs migration
|
|
1162
|
-
}
|
|
1163
|
-
const firstKeptEntryId = firstKeptEntry.id;
|
|
1164
|
-
|
|
1165
|
-
const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
|
|
1166
|
-
|
|
1167
|
-
// Messages to summarize (will be discarded after summary)
|
|
1168
|
-
const messagesToSummarize: AgentMessage[] = [];
|
|
1169
|
-
for (let i = boundaryStart; i < historyEnd; i++) {
|
|
1170
|
-
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1171
|
-
if (msg) messagesToSummarize.push(msg);
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
// Messages for turn prefix summary (if splitting a turn)
|
|
1175
|
-
const turnPrefixMessages: AgentMessage[] = [];
|
|
1176
|
-
if (cutPoint.isSplitTurn) {
|
|
1177
|
-
for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {
|
|
1178
|
-
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1179
|
-
if (msg) turnPrefixMessages.push(msg);
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
// Messages kept after compaction (recent history)
|
|
1184
|
-
const recentMessages: AgentMessage[] = [];
|
|
1185
|
-
for (let i = cutPoint.firstKeptEntryIndex; i < boundaryEnd; i++) {
|
|
1186
|
-
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1187
|
-
if (msg) recentMessages.push(msg);
|
|
1188
|
-
}
|
|
1189
|
-
// Nothing to summarize means compaction would be a no-op.
|
|
1190
|
-
if (messagesToSummarize.length === 0 && turnPrefixMessages.length === 0) {
|
|
1191
|
-
return undefined;
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
// Get previous summary and preserved data for iterative updates
|
|
1195
|
-
let previousSummary: string | undefined;
|
|
1196
|
-
let previousPreserveData: Record<string, unknown> | undefined;
|
|
1197
|
-
if (prevCompactionIndex >= 0) {
|
|
1198
|
-
const prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry;
|
|
1199
|
-
previousSummary = prevCompaction.summary;
|
|
1200
|
-
previousPreserveData = prevCompaction.preserveData;
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
// Extract file operations from messages and previous compaction
|
|
1204
|
-
const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);
|
|
1205
|
-
|
|
1206
|
-
// Also extract file ops from turn prefix if splitting
|
|
1207
|
-
if (cutPoint.isSplitTurn) {
|
|
1208
|
-
for (const msg of turnPrefixMessages) {
|
|
1209
|
-
extractFileOpsFromMessage(msg, fileOps);
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
|
|
1213
|
-
return {
|
|
1214
|
-
firstKeptEntryId,
|
|
1215
|
-
messagesToSummarize,
|
|
1216
|
-
turnPrefixMessages,
|
|
1217
|
-
recentMessages,
|
|
1218
|
-
isSplitTurn: cutPoint.isSplitTurn,
|
|
1219
|
-
tokensBefore,
|
|
1220
|
-
previousSummary,
|
|
1221
|
-
previousPreserveData,
|
|
1222
|
-
fileOps,
|
|
1223
|
-
settings,
|
|
1224
|
-
};
|
|
1225
|
-
}
|
|
1226
|
-
|
|
1227
|
-
// ============================================================================
|
|
1228
|
-
// Main compaction function
|
|
1229
|
-
// ============================================================================
|
|
1230
|
-
|
|
1231
|
-
const TURN_PREFIX_SUMMARIZATION_PROMPT = prompt.render(compactionTurnPrefixPrompt);
|
|
1232
|
-
|
|
1233
|
-
/**
|
|
1234
|
-
* Generate summaries for compaction using prepared data.
|
|
1235
|
-
* Returns CompactionResult - SessionManager adds id/parentId when saving.
|
|
1236
|
-
*
|
|
1237
|
-
* @param preparation - Pre-calculated preparation from prepareCompaction()
|
|
1238
|
-
* @param customInstructions - Optional custom focus for the summary
|
|
1239
|
-
*/
|
|
1240
|
-
export async function compact(
|
|
1241
|
-
preparation: CompactionPreparation,
|
|
1242
|
-
model: Model,
|
|
1243
|
-
apiKey: string,
|
|
1244
|
-
customInstructions?: string,
|
|
1245
|
-
signal?: AbortSignal,
|
|
1246
|
-
options?: SummaryOptions,
|
|
1247
|
-
): Promise<CompactionResult> {
|
|
1248
|
-
const {
|
|
1249
|
-
firstKeptEntryId,
|
|
1250
|
-
messagesToSummarize,
|
|
1251
|
-
turnPrefixMessages,
|
|
1252
|
-
recentMessages,
|
|
1253
|
-
isSplitTurn,
|
|
1254
|
-
tokensBefore,
|
|
1255
|
-
previousSummary,
|
|
1256
|
-
previousPreserveData,
|
|
1257
|
-
fileOps,
|
|
1258
|
-
settings,
|
|
1259
|
-
} = preparation;
|
|
1260
|
-
|
|
1261
|
-
const summaryOptions: SummaryOptions = {
|
|
1262
|
-
promptOverride: options?.promptOverride,
|
|
1263
|
-
extraContext: options?.extraContext,
|
|
1264
|
-
remoteEndpoint: settings.remoteEnabled === false ? undefined : settings.remoteEndpoint,
|
|
1265
|
-
remoteInstructions: options?.remoteInstructions,
|
|
1266
|
-
initiatorOverride: options?.initiatorOverride,
|
|
1267
|
-
metadata: options?.metadata,
|
|
1268
|
-
};
|
|
1269
|
-
|
|
1270
|
-
let preserveData = withOpenAiRemoteCompactionPreserveData(previousPreserveData, undefined);
|
|
1271
|
-
if (settings.remoteEnabled !== false && shouldUseOpenAiRemoteCompaction(model)) {
|
|
1272
|
-
const previousRemoteCompaction = getPreservedOpenAiRemoteCompactionData(previousPreserveData);
|
|
1273
|
-
const remoteMessages = [...messagesToSummarize, ...turnPrefixMessages, ...recentMessages];
|
|
1274
|
-
const previousReplacementHistory =
|
|
1275
|
-
previousRemoteCompaction?.provider === model.provider
|
|
1276
|
-
? previousRemoteCompaction.replacementHistory
|
|
1277
|
-
: undefined;
|
|
1278
|
-
const remoteHistory = buildOpenAiNativeHistory(remoteMessages, model, previousReplacementHistory);
|
|
1279
|
-
if (remoteHistory.length > 0) {
|
|
1280
|
-
try {
|
|
1281
|
-
const remote = await requestOpenAiRemoteCompaction(
|
|
1282
|
-
model,
|
|
1283
|
-
apiKey,
|
|
1284
|
-
remoteHistory,
|
|
1285
|
-
summaryOptions.remoteInstructions ?? SUMMARIZATION_SYSTEM_PROMPT,
|
|
1286
|
-
);
|
|
1287
|
-
preserveData = withOpenAiRemoteCompactionPreserveData(previousPreserveData, remote);
|
|
1288
|
-
} catch (err) {
|
|
1289
|
-
logger.warn("OpenAI remote compaction failed, falling back to local summarization", {
|
|
1290
|
-
error: err instanceof Error ? err.message : String(err),
|
|
1291
|
-
model: model.id,
|
|
1292
|
-
provider: model.provider,
|
|
1293
|
-
});
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
1296
|
-
}
|
|
1297
|
-
|
|
1298
|
-
// Generate summaries (can be parallel if both needed) and merge into one
|
|
1299
|
-
let summary: string;
|
|
1300
|
-
|
|
1301
|
-
if (isSplitTurn && turnPrefixMessages.length > 0) {
|
|
1302
|
-
// Generate both summaries in parallel
|
|
1303
|
-
const [historyResult, turnPrefixResult] = await Promise.all([
|
|
1304
|
-
messagesToSummarize.length > 0
|
|
1305
|
-
? generateSummary(
|
|
1306
|
-
messagesToSummarize,
|
|
1307
|
-
model,
|
|
1308
|
-
settings.reserveTokens,
|
|
1309
|
-
apiKey,
|
|
1310
|
-
signal,
|
|
1311
|
-
customInstructions,
|
|
1312
|
-
previousSummary,
|
|
1313
|
-
summaryOptions,
|
|
1314
|
-
)
|
|
1315
|
-
: Promise.resolve("No prior history."),
|
|
1316
|
-
generateTurnPrefixSummary(
|
|
1317
|
-
turnPrefixMessages,
|
|
1318
|
-
model,
|
|
1319
|
-
settings.reserveTokens,
|
|
1320
|
-
apiKey,
|
|
1321
|
-
signal,
|
|
1322
|
-
summaryOptions.initiatorOverride,
|
|
1323
|
-
summaryOptions.metadata,
|
|
1324
|
-
),
|
|
1325
|
-
]);
|
|
1326
|
-
// Merge into single summary
|
|
1327
|
-
summary = `${historyResult}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult}`;
|
|
1328
|
-
} else if (messagesToSummarize.length > 0) {
|
|
1329
|
-
// Generate history summary from messages to summarize
|
|
1330
|
-
summary = await generateSummary(
|
|
1331
|
-
messagesToSummarize,
|
|
1332
|
-
model,
|
|
1333
|
-
settings.reserveTokens,
|
|
1334
|
-
apiKey,
|
|
1335
|
-
signal,
|
|
1336
|
-
customInstructions,
|
|
1337
|
-
previousSummary,
|
|
1338
|
-
summaryOptions,
|
|
1339
|
-
);
|
|
1340
|
-
} else if (previousSummary) {
|
|
1341
|
-
// No new messages to summarize, preserve previous summary
|
|
1342
|
-
summary = previousSummary;
|
|
1343
|
-
} else {
|
|
1344
|
-
// No messages and no previous summary
|
|
1345
|
-
summary = "No prior history.";
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
const shortSummary = await generateShortSummary(
|
|
1349
|
-
recentMessages,
|
|
1350
|
-
summary,
|
|
1351
|
-
model,
|
|
1352
|
-
settings.reserveTokens,
|
|
1353
|
-
apiKey,
|
|
1354
|
-
signal,
|
|
1355
|
-
{
|
|
1356
|
-
extraContext: options?.extraContext,
|
|
1357
|
-
remoteEndpoint: summaryOptions.remoteEndpoint,
|
|
1358
|
-
initiatorOverride: summaryOptions.initiatorOverride,
|
|
1359
|
-
metadata: summaryOptions.metadata,
|
|
1360
|
-
},
|
|
1361
|
-
);
|
|
1362
|
-
|
|
1363
|
-
// Compute file lists and append to summary
|
|
1364
|
-
const { readFiles, modifiedFiles } = computeFileLists(fileOps);
|
|
1365
|
-
summary = upsertFileOperations(summary, readFiles, modifiedFiles);
|
|
1366
|
-
|
|
1367
|
-
if (!firstKeptEntryId) {
|
|
1368
|
-
throw new Error("First kept entry has no ID - session may need migration");
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
return {
|
|
1372
|
-
summary,
|
|
1373
|
-
shortSummary,
|
|
1374
|
-
firstKeptEntryId,
|
|
1375
|
-
tokensBefore,
|
|
1376
|
-
details: { readFiles, modifiedFiles } as CompactionDetails,
|
|
1377
|
-
preserveData,
|
|
1378
|
-
};
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
/**
|
|
1382
|
-
* Generate a summary for a turn prefix (when splitting a turn).
|
|
1383
|
-
*/
|
|
1384
|
-
async function generateTurnPrefixSummary(
|
|
1385
|
-
messages: AgentMessage[],
|
|
1386
|
-
model: Model,
|
|
1387
|
-
reserveTokens: number,
|
|
1388
|
-
apiKey: string,
|
|
1389
|
-
signal?: AbortSignal,
|
|
1390
|
-
initiatorOverride?: MessageAttribution,
|
|
1391
|
-
metadata?: Record<string, unknown>,
|
|
1392
|
-
): Promise<string> {
|
|
1393
|
-
const maxTokens = Math.floor(0.5 * reserveTokens); // Smaller budget for turn prefix
|
|
1394
|
-
|
|
1395
|
-
const llmMessages = convertToLlm(messages);
|
|
1396
|
-
const conversationText = serializeConversation(llmMessages);
|
|
1397
|
-
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
|
|
1398
|
-
const summarizationMessages = [
|
|
1399
|
-
{
|
|
1400
|
-
role: "user" as const,
|
|
1401
|
-
content: [{ type: "text" as const, text: promptText }],
|
|
1402
|
-
timestamp: Date.now(),
|
|
1403
|
-
},
|
|
1404
|
-
];
|
|
1405
|
-
|
|
1406
|
-
const response = await completeSimple(
|
|
1407
|
-
model,
|
|
1408
|
-
{ systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
|
|
1409
|
-
{ maxTokens, signal, apiKey, reasoning: Effort.High, initiatorOverride, metadata },
|
|
1410
|
-
);
|
|
1411
|
-
|
|
1412
|
-
if (response.stopReason === "error") {
|
|
1413
|
-
throw new Error(`Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
return response.content
|
|
1417
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
1418
|
-
.map(c => c.text)
|
|
1419
|
-
.join("\n");
|
|
1420
|
-
}
|