@sayknow-cli/agent-core 0.2.2
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 +588 -0
- package/README.md +473 -0
- package/dist/types/agent-loop.d.ts +56 -0
- package/dist/types/agent.d.ts +381 -0
- package/dist/types/append-only-context.d.ts +124 -0
- package/dist/types/compaction/branch-summarization.d.ts +103 -0
- package/dist/types/compaction/compaction.d.ts +253 -0
- package/dist/types/compaction/entries.d.ts +109 -0
- package/dist/types/compaction/errors.d.ts +26 -0
- package/dist/types/compaction/index.d.ts +11 -0
- package/dist/types/compaction/messages.d.ts +61 -0
- package/dist/types/compaction/openai.d.ts +62 -0
- package/dist/types/compaction/pruning.d.ts +37 -0
- package/dist/types/compaction/utils.d.ts +32 -0
- package/dist/types/compaction.d.ts +1 -0
- package/dist/types/harmony-leak.d.ts +99 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/proxy.d.ts +84 -0
- package/dist/types/run-collector.d.ts +196 -0
- package/dist/types/telemetry.d.ts +596 -0
- package/dist/types/thinking.d.ts +18 -0
- package/dist/types/types.d.ts +430 -0
- package/package.json +75 -0
- package/src/agent-loop.ts +1302 -0
- package/src/agent.ts +1531 -0
- package/src/append-only-context.ts +460 -0
- package/src/compaction/branch-summarization.ts +358 -0
- package/src/compaction/compaction.ts +1342 -0
- package/src/compaction/entries.ts +139 -0
- package/src/compaction/errors.ts +31 -0
- package/src/compaction/index.ts +12 -0
- package/src/compaction/messages.ts +212 -0
- package/src/compaction/openai.ts +570 -0
- package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
- package/src/compaction/prompts/branch-summary-context.md +5 -0
- package/src/compaction/prompts/branch-summary-preamble.md +2 -0
- package/src/compaction/prompts/branch-summary.md +30 -0
- package/src/compaction/prompts/compaction-short-summary.md +9 -0
- package/src/compaction/prompts/compaction-summary-context.md +5 -0
- package/src/compaction/prompts/compaction-summary.md +38 -0
- package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
- package/src/compaction/prompts/compaction-update-summary.md +45 -0
- package/src/compaction/prompts/file-operations.md +10 -0
- package/src/compaction/prompts/handoff-document.md +49 -0
- package/src/compaction/prompts/summarization-system.md +3 -0
- package/src/compaction/pruning.ts +431 -0
- package/src/compaction/utils.ts +185 -0
- package/src/compaction.ts +1 -0
- package/src/harmony-leak.ts +428 -0
- package/src/index.ts +19 -0
- package/src/proxy.ts +326 -0
- package/src/run-collector.ts +631 -0
- package/src/telemetry.ts +2049 -0
- package/src/thinking.ts +20 -0
- package/src/types.ts +490 -0
|
@@ -0,0 +1,1342 @@
|
|
|
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
|
+
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import {
|
|
10
|
+
type AssistantMessage,
|
|
11
|
+
Effort,
|
|
12
|
+
type Message,
|
|
13
|
+
type MessageAttribution,
|
|
14
|
+
type Model,
|
|
15
|
+
type ProviderSessionState,
|
|
16
|
+
type Usage,
|
|
17
|
+
} from "@sayknow-cli/ai";
|
|
18
|
+
import { isCompiledBinary, logger, prompt } from "@sayknow-cli/utils";
|
|
19
|
+
import { type AgentTelemetry, instrumentedCompleteSimple } from "../telemetry";
|
|
20
|
+
import type { AgentMessage, AgentTool } from "../types";
|
|
21
|
+
import type { CompactionEntry, SessionEntry } from "./entries";
|
|
22
|
+
import { type ConvertToLlm, convertToLlm, createBranchSummaryMessage, createCustomMessage } from "./messages";
|
|
23
|
+
import {
|
|
24
|
+
buildOpenAiNativeHistory,
|
|
25
|
+
getPreservedOpenAiRemoteCompactionData,
|
|
26
|
+
requestOpenAiRemoteCompaction,
|
|
27
|
+
requestRemoteCompaction,
|
|
28
|
+
shouldUseOpenAiRemoteCompaction,
|
|
29
|
+
withOpenAiRemoteCompactionPreserveData,
|
|
30
|
+
} from "./openai";
|
|
31
|
+
import autoHandoffThresholdFocusPrompt from "./prompts/auto-handoff-threshold-focus.md" with { type: "text" };
|
|
32
|
+
import compactionShortSummaryPrompt from "./prompts/compaction-short-summary.md" with { type: "text" };
|
|
33
|
+
import compactionSummaryPrompt from "./prompts/compaction-summary.md" with { type: "text" };
|
|
34
|
+
import compactionTurnPrefixPrompt from "./prompts/compaction-turn-prefix.md" with { type: "text" };
|
|
35
|
+
import compactionUpdateSummaryPrompt from "./prompts/compaction-update-summary.md" with { type: "text" };
|
|
36
|
+
import handoffDocumentPrompt from "./prompts/handoff-document.md" with { type: "text" };
|
|
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: the largest of 15% of the context window, the configured floor,
|
|
208
|
+
* and the model's reserved completion budget (`maxOutputTokens`).
|
|
209
|
+
*
|
|
210
|
+
* Reserving `maxOutputTokens` keeps the safe input/prompt-packing budget below the
|
|
211
|
+
* *total* context window for models whose completion reservation exceeds the 15%
|
|
212
|
+
* floor (e.g. a 400K-context model with 128K max output reserves 128K, not 60K, so
|
|
213
|
+
* input is capped near 272K instead of 340K).
|
|
214
|
+
*/
|
|
215
|
+
export function effectiveReserveTokens(
|
|
216
|
+
contextWindow: number,
|
|
217
|
+
settings: CompactionSettings,
|
|
218
|
+
maxOutputTokens = 0,
|
|
219
|
+
): number {
|
|
220
|
+
return Math.max(Math.floor(contextWindow * 0.15), settings.reserveTokens, Math.max(0, maxOutputTokens));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Check if compaction should trigger based on context usage.
|
|
225
|
+
*
|
|
226
|
+
* `maxOutputTokens` is the model's reserved completion budget; it is excluded from
|
|
227
|
+
* the safe input budget so prompt + reserved output cannot exceed the total window.
|
|
228
|
+
*/
|
|
229
|
+
export function shouldCompact(
|
|
230
|
+
contextTokens: number,
|
|
231
|
+
contextWindow: number,
|
|
232
|
+
settings: CompactionSettings,
|
|
233
|
+
maxOutputTokens = 0,
|
|
234
|
+
): boolean {
|
|
235
|
+
if (!settings.enabled || settings.strategy === "off" || contextWindow <= 0) return false;
|
|
236
|
+
const thresholdTokens = resolveThresholdTokens(contextWindow, settings, maxOutputTokens);
|
|
237
|
+
return contextTokens > thresholdTokens;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Reason a compaction was triggered. `token` is the normal user-configurable path; the rest are emergency floors. */
|
|
241
|
+
export type CompactionTriggerReason = "token" | "heap" | "providerBytes" | "messageCount" | "imageBytes";
|
|
242
|
+
|
|
243
|
+
/** A point-in-time resource sample. Supplied by an injectable sampler so tests never read real RSS. */
|
|
244
|
+
export interface EmergencyCompactionSample {
|
|
245
|
+
/** Resident heap bytes (e.g. process.memoryUsage().heapUsed). */
|
|
246
|
+
heapUsedBytes: number;
|
|
247
|
+
/** Approximate serialized provider-context bytes. */
|
|
248
|
+
providerBytes: number;
|
|
249
|
+
/** Provider-visible message count. */
|
|
250
|
+
messageCount: number;
|
|
251
|
+
/** Approximate inline image bytes in the provider context. */
|
|
252
|
+
imageBytes: number;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export interface EmergencyCompactionLimits {
|
|
256
|
+
heapUsedBytes: number;
|
|
257
|
+
providerBytes: number;
|
|
258
|
+
messageCount: number;
|
|
259
|
+
imageBytes: number;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Non-disableable emergency floors. These sit well above normal usage and exist so a
|
|
264
|
+
* long session on weak hardware compacts before OOM even when token-based compaction is
|
|
265
|
+
* disabled or its threshold is set too high. They are NOT user-tunable down to zero.
|
|
266
|
+
*/
|
|
267
|
+
export const DEFAULT_EMERGENCY_COMPACTION_LIMITS: EmergencyCompactionLimits = {
|
|
268
|
+
heapUsedBytes: 1_536 * 1024 * 1024, // 1.5 GiB resident heap
|
|
269
|
+
providerBytes: 24 * 1024 * 1024, // 24 MiB serialized provider context
|
|
270
|
+
messageCount: 4000,
|
|
271
|
+
imageBytes: 64 * 1024 * 1024, // 64 MiB inline image bytes
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Returns the first emergency limit exceeded (heap > providerBytes > imageBytes > messageCount),
|
|
276
|
+
* or null when none is. Pure and sampler-injected; the caller routes the result through the
|
|
277
|
+
* normal pair-safe `compact()` cut logic so a tool_use/tool_result pair is never split.
|
|
278
|
+
*/
|
|
279
|
+
export function emergencyCompactionReason(
|
|
280
|
+
sample: EmergencyCompactionSample,
|
|
281
|
+
limits: EmergencyCompactionLimits = DEFAULT_EMERGENCY_COMPACTION_LIMITS,
|
|
282
|
+
): CompactionTriggerReason | null {
|
|
283
|
+
if (sample.heapUsedBytes > limits.heapUsedBytes) return "heap";
|
|
284
|
+
if (sample.providerBytes > limits.providerBytes) return "providerBytes";
|
|
285
|
+
if (sample.imageBytes > limits.imageBytes) return "imageBytes";
|
|
286
|
+
if (sample.messageCount > limits.messageCount) return "messageCount";
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function resolveThresholdTokens(
|
|
291
|
+
contextWindow: number,
|
|
292
|
+
settings: CompactionSettings,
|
|
293
|
+
maxOutputTokens = 0,
|
|
294
|
+
): number {
|
|
295
|
+
// Fixed token limit takes priority over percentage
|
|
296
|
+
const thresholdTokens = settings.thresholdTokens;
|
|
297
|
+
if (typeof thresholdTokens === "number" && Number.isFinite(thresholdTokens) && thresholdTokens > 0) {
|
|
298
|
+
// Clamp to [1, contextWindow - 1] so there's always room
|
|
299
|
+
return Math.min(contextWindow - 1, Math.max(1, thresholdTokens));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Percentage-based threshold
|
|
303
|
+
const thresholdPercent = settings.thresholdPercent;
|
|
304
|
+
if (typeof thresholdPercent !== "number" || !Number.isFinite(thresholdPercent) || thresholdPercent <= 0) {
|
|
305
|
+
return contextWindow - effectiveReserveTokens(contextWindow, settings, maxOutputTokens);
|
|
306
|
+
}
|
|
307
|
+
const clampedThresholdPercent = Math.min(99, Math.max(1, thresholdPercent));
|
|
308
|
+
return Math.floor(contextWindow * (clampedThresholdPercent / 100));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ============================================================================
|
|
312
|
+
// Cut point detection
|
|
313
|
+
// ============================================================================
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Image content has no tokenizer representation; charge a fixed estimate
|
|
317
|
+
* matching what providers typically bill for inline images.
|
|
318
|
+
*/
|
|
319
|
+
const IMAGE_TOKEN_ESTIMATE = 1200;
|
|
320
|
+
const SOURCE_NATIVE_TOKENIZER_ENTRYPOINT = "../../../natives/native/index.js";
|
|
321
|
+
const COMPILED_NATIVE_TOKENIZER_ENTRYPOINT = "/$bunfs/root/packages/natives/native/index.js";
|
|
322
|
+
|
|
323
|
+
const requireFromCompaction = createRequire(import.meta.url);
|
|
324
|
+
|
|
325
|
+
interface NativeTokenizerModule {
|
|
326
|
+
countTokens(input: string | string[], encoding?: unknown): number;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Lazily-required native `countTokens`. `@sayknow-cli/natives` dlopens a ~39MB
|
|
331
|
+
* addon; importing it at module scope would put that cost on every cold path
|
|
332
|
+
* that touches compaction exports (status line, print mode, context report).
|
|
333
|
+
* Deferring the require to the first context-changing call keeps display paths
|
|
334
|
+
* native-free.
|
|
335
|
+
*
|
|
336
|
+
* Do not resolve this via a package-name dynamic require of
|
|
337
|
+
* `@sayknow-cli/natives`: Bun standalone binaries cannot satisfy those from
|
|
338
|
+
* `$bunfs`. The sibling-package source path is stable for workspace and
|
|
339
|
+
* package-install layouts:
|
|
340
|
+
*
|
|
341
|
+
* - workspace: `packages/agent` -> `packages/natives`
|
|
342
|
+
* - npm/bun install: `node_modules/@sayknow-cli/agent-core` ->
|
|
343
|
+
* `node_modules/@sayknow-cli/natives`
|
|
344
|
+
*
|
|
345
|
+
* Bun rewrites `createRequire(import.meta.url)` to the compiled executable
|
|
346
|
+
* root (`/$bunfs/root/skc-*`) in standalone binaries, so compiled mode uses the
|
|
347
|
+
* absolute bunfs module path emitted by the binary build scripts.
|
|
348
|
+
*/
|
|
349
|
+
let cachedNativeCountTokens: ((input: string | string[], encoding?: unknown) => number) | null = null;
|
|
350
|
+
|
|
351
|
+
function nativeTokenizerEntrypoint(): string {
|
|
352
|
+
return isCompiledBinary() ? COMPILED_NATIVE_TOKENIZER_ENTRYPOINT : SOURCE_NATIVE_TOKENIZER_ENTRYPOINT;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Max total fragment chars sent to the synchronous native tokenizer (F22). */
|
|
356
|
+
const MAX_NATIVE_TOKENIZE_CHARS = 2 * 1024 * 1024;
|
|
357
|
+
|
|
358
|
+
function nativeCountTokens(fragments: string[]): number {
|
|
359
|
+
let totalChars = 0;
|
|
360
|
+
for (const fragment of fragments) totalChars += fragment.length;
|
|
361
|
+
if (totalChars > MAX_NATIVE_TOKENIZE_CHARS) {
|
|
362
|
+
// F22: skip the synchronous native BPE tokenizer (materializes a ~39MB table and is
|
|
363
|
+
// O(text)) on pathologically large inputs; the cheap chars/token heuristic is more
|
|
364
|
+
// than accurate enough for size/budget decisions and never blocks the event loop.
|
|
365
|
+
return estimateTextTokensHeuristic(fragments);
|
|
366
|
+
}
|
|
367
|
+
if (!cachedNativeCountTokens) {
|
|
368
|
+
const natives = requireFromCompaction(nativeTokenizerEntrypoint()) as NativeTokenizerModule;
|
|
369
|
+
cachedNativeCountTokens = natives.countTokens;
|
|
370
|
+
}
|
|
371
|
+
return cachedNativeCountTokens(fragments);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function countCollectedMessageFragments(collected: { fragments: string[]; extra: number }): number {
|
|
375
|
+
return nativeCountTokens(collected.fragments) + collected.extra;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Estimate token count for a message using the native o200k tokenizer.
|
|
380
|
+
* Exact for o200k only; an approximation for Anthropic/other model families
|
|
381
|
+
* (Anthropic doesn't publish a tokenizer) within ~5–10% on English/code text.
|
|
382
|
+
*
|
|
383
|
+
* This materializes the native BPE table (~50MB RSS) on first call. Use it
|
|
384
|
+
* only for context-changing decisions (compaction trigger/cut points, pruning
|
|
385
|
+
* budgets, branch summarization, fork-context seeding, context-limit
|
|
386
|
+
* enforcement). For display-only totals use
|
|
387
|
+
* {@link estimateMessageTokensHeuristic}.
|
|
388
|
+
*/
|
|
389
|
+
export function countMessageTokensNativeO200k(message: AgentMessage): number {
|
|
390
|
+
return countCollectedMessageFragments(collectMessageFragments(message));
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Backwards-compatible alias for {@link countMessageTokensNativeO200k}.
|
|
395
|
+
* Existing callers treat this as the canonical message-token estimator for
|
|
396
|
+
* context-changing decisions.
|
|
397
|
+
*/
|
|
398
|
+
export const estimateTokens = countMessageTokensNativeO200k;
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Average bytes per token for the cheap heuristic. ~4 bytes/token is the
|
|
402
|
+
* conventional approximation for English/code text under modern BPE
|
|
403
|
+
* vocabularies; it intentionally errs slightly low-precision in exchange for
|
|
404
|
+
* never touching the native tokenizer (and its ~50MB BPE table).
|
|
405
|
+
*/
|
|
406
|
+
const HEURISTIC_BYTES_PER_TOKEN = 4;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Cheap, native-free token estimate for a message. Suitable ONLY for
|
|
410
|
+
* display/init surfaces (status line, /context report, HUD totals) — never
|
|
411
|
+
* for context-changing decisions, which must use
|
|
412
|
+
* {@link countMessageTokensNativeO200k}.
|
|
413
|
+
*/
|
|
414
|
+
export function estimateMessageTokensHeuristic(message: AgentMessage): number {
|
|
415
|
+
const { fragments, extra } = collectMessageFragments(message);
|
|
416
|
+
let bytes = 0;
|
|
417
|
+
for (const fragment of fragments) {
|
|
418
|
+
bytes += fragment.length;
|
|
419
|
+
}
|
|
420
|
+
return extra + Math.ceil(bytes / HEURISTIC_BYTES_PER_TOKEN);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Cheap, native-free token estimate for plain string fragments. Display-only
|
|
425
|
+
* counterpart of the native `countTokens(fragments)` aggregate.
|
|
426
|
+
*/
|
|
427
|
+
export function estimateTextTokensHeuristic(fragments: string | readonly string[]): number {
|
|
428
|
+
if (typeof fragments === "string") return Math.ceil(fragments.length / HEURISTIC_BYTES_PER_TOKEN);
|
|
429
|
+
let bytes = 0;
|
|
430
|
+
for (const fragment of fragments) {
|
|
431
|
+
bytes += fragment.length;
|
|
432
|
+
}
|
|
433
|
+
return Math.ceil(bytes / HEURISTIC_BYTES_PER_TOKEN);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Shared content walk for both the native and heuristic estimators. */
|
|
437
|
+
function collectMessageFragments(message: AgentMessage): { fragments: string[]; extra: number } {
|
|
438
|
+
const fragments: string[] = [];
|
|
439
|
+
let extra = 0;
|
|
440
|
+
if ((message as { role?: string }).role === "bashExecution") {
|
|
441
|
+
const bash = message as { command?: unknown; output?: unknown };
|
|
442
|
+
if (typeof bash.command === "string") fragments.push(bash.command);
|
|
443
|
+
if (typeof bash.output === "string") fragments.push(bash.output);
|
|
444
|
+
return { fragments, extra };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
switch (message.role) {
|
|
448
|
+
case "user": {
|
|
449
|
+
const content = (message as { content: string | Array<{ type: string; text?: string }> }).content;
|
|
450
|
+
if (typeof content === "string") {
|
|
451
|
+
fragments.push(content);
|
|
452
|
+
} else if (Array.isArray(content)) {
|
|
453
|
+
for (const block of content) {
|
|
454
|
+
if (block.type === "text" && block.text) {
|
|
455
|
+
fragments.push(block.text);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
case "assistant": {
|
|
462
|
+
const assistant = message as AssistantMessage;
|
|
463
|
+
for (const block of assistant.content) {
|
|
464
|
+
if (block.type === "text") {
|
|
465
|
+
fragments.push(block.text);
|
|
466
|
+
} else if (block.type === "thinking") {
|
|
467
|
+
fragments.push(block.thinking);
|
|
468
|
+
} else if (block.type === "toolCall") {
|
|
469
|
+
fragments.push(block.name);
|
|
470
|
+
fragments.push(JSON.stringify(block.arguments));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
case "hookMessage":
|
|
476
|
+
case "toolResult": {
|
|
477
|
+
if (typeof message.content === "string") {
|
|
478
|
+
fragments.push(message.content);
|
|
479
|
+
} else {
|
|
480
|
+
for (const block of message.content) {
|
|
481
|
+
if (block.type === "text" && block.text) {
|
|
482
|
+
fragments.push(block.text);
|
|
483
|
+
} else if (block.type === "image") {
|
|
484
|
+
extra += IMAGE_TOKEN_ESTIMATE;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
case "branchSummary":
|
|
491
|
+
case "compactionSummary": {
|
|
492
|
+
fragments.push(message.summary);
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
default:
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return { fragments, extra };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function entryTokenFingerprint(
|
|
503
|
+
entry: SessionEntry,
|
|
504
|
+
message: AgentMessage,
|
|
505
|
+
collected: { fragments: string[]; extra: number },
|
|
506
|
+
): string {
|
|
507
|
+
const maybePruned = message as { prunedAt?: unknown };
|
|
508
|
+
let fingerprint = `${entry.type.length}:${entry.type}${(entry.id ?? "").length}:${entry.id ?? ""}${message.role.length}:${message.role}${String(collected.extra).length}:${String(collected.extra)}${collected.fragments.length}:`;
|
|
509
|
+
for (const fragment of collected.fragments) fingerprint += `${fragment.length}:${fragment}`;
|
|
510
|
+
if (maybePruned.prunedAt !== undefined) {
|
|
511
|
+
const prunedAt = String(maybePruned.prunedAt);
|
|
512
|
+
fingerprint += `prunedAt${prunedAt.length}:${prunedAt}`;
|
|
513
|
+
}
|
|
514
|
+
return fingerprint;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const entryTokenCache = new WeakMap<SessionEntry, { fingerprint: string; tokens: number }>();
|
|
518
|
+
|
|
519
|
+
export function estimateEntryTokens(entry: SessionEntry): number {
|
|
520
|
+
const msg = getMessageFromEntry(entry);
|
|
521
|
+
if (!msg) return 0;
|
|
522
|
+
const collected = collectMessageFragments(msg);
|
|
523
|
+
const fingerprint = entryTokenFingerprint(entry, msg, collected);
|
|
524
|
+
const cached = entryTokenCache.get(entry);
|
|
525
|
+
if (cached?.fingerprint === fingerprint) return cached.tokens;
|
|
526
|
+
const tokens = countCollectedMessageFragments(collected);
|
|
527
|
+
entryTokenCache.set(entry, { fingerprint, tokens });
|
|
528
|
+
return tokens;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function estimateEntriesTokens(entries: SessionEntry[], startIndex: number, endIndex: number): number {
|
|
532
|
+
let total = 0;
|
|
533
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
534
|
+
total += estimateEntryTokens(entries[i]);
|
|
535
|
+
}
|
|
536
|
+
return total;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
|
|
541
|
+
* Never cut at tool results (they must follow their tool call).
|
|
542
|
+
* When we cut at an assistant message with tool calls, its tool results follow it
|
|
543
|
+
* and will be kept.
|
|
544
|
+
* BashExecutionMessage is treated like a user message (user-initiated context).
|
|
545
|
+
*/
|
|
546
|
+
function findValidCutPoints(entries: SessionEntry[], startIndex: number, endIndex: number): number[] {
|
|
547
|
+
const cutPoints: number[] = [];
|
|
548
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
549
|
+
const entry = entries[i];
|
|
550
|
+
switch (entry.type) {
|
|
551
|
+
case "message": {
|
|
552
|
+
const role = entry.message.role as string;
|
|
553
|
+
switch (role) {
|
|
554
|
+
case "bashExecution":
|
|
555
|
+
case "hookMessage":
|
|
556
|
+
case "branchSummary":
|
|
557
|
+
case "compactionSummary":
|
|
558
|
+
case "user":
|
|
559
|
+
case "assistant":
|
|
560
|
+
cutPoints.push(i);
|
|
561
|
+
break;
|
|
562
|
+
case "toolResult":
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
break;
|
|
566
|
+
}
|
|
567
|
+
case "thinking_level_change":
|
|
568
|
+
case "model_change":
|
|
569
|
+
case "compaction":
|
|
570
|
+
case "branch_summary":
|
|
571
|
+
case "custom":
|
|
572
|
+
case "custom_message":
|
|
573
|
+
case "label":
|
|
574
|
+
}
|
|
575
|
+
// branch_summary and custom_message are user-role messages, valid cut points
|
|
576
|
+
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
577
|
+
cutPoints.push(i);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return cutPoints;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
585
|
+
* Returns -1 if no turn start found before the index.
|
|
586
|
+
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
587
|
+
*/
|
|
588
|
+
export function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number {
|
|
589
|
+
for (let i = entryIndex; i >= startIndex; i--) {
|
|
590
|
+
const entry = entries[i];
|
|
591
|
+
// branch_summary and custom_message are user-role messages, can start a turn
|
|
592
|
+
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
593
|
+
return i;
|
|
594
|
+
}
|
|
595
|
+
if (entry.type === "message") {
|
|
596
|
+
const role = entry.message.role as string;
|
|
597
|
+
if (role === "user" || role === "bashExecution") {
|
|
598
|
+
return i;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return -1;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export interface CutPointResult {
|
|
606
|
+
/** Index of first entry to keep */
|
|
607
|
+
firstKeptEntryIndex: number;
|
|
608
|
+
/** Index of user message that starts the turn being split, or -1 if not splitting */
|
|
609
|
+
turnStartIndex: number;
|
|
610
|
+
/** Whether this cut splits a turn (cut point is not a user message) */
|
|
611
|
+
isSplitTurn: boolean;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
616
|
+
*
|
|
617
|
+
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
618
|
+
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
619
|
+
*
|
|
620
|
+
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
621
|
+
* assistant message with tool calls, its tool results come after and will be kept.
|
|
622
|
+
*
|
|
623
|
+
* Returns CutPointResult with:
|
|
624
|
+
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
625
|
+
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
626
|
+
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
627
|
+
*
|
|
628
|
+
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
629
|
+
*/
|
|
630
|
+
export function findCutPoint(
|
|
631
|
+
entries: SessionEntry[],
|
|
632
|
+
startIndex: number,
|
|
633
|
+
endIndex: number,
|
|
634
|
+
keepRecentTokens: number,
|
|
635
|
+
): CutPointResult {
|
|
636
|
+
const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
|
|
637
|
+
|
|
638
|
+
if (cutPoints.length === 0) {
|
|
639
|
+
return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Walk backwards from newest, accumulating estimated message sizes
|
|
643
|
+
let accumulatedTokens = 0;
|
|
644
|
+
let cutIndex = cutPoints[0]; // Default: keep from first message (not header)
|
|
645
|
+
|
|
646
|
+
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
647
|
+
const entry = entries[i];
|
|
648
|
+
if (entry.type !== "message") continue;
|
|
649
|
+
|
|
650
|
+
// Estimate this message's size
|
|
651
|
+
const messageTokens = estimateEntryTokens(entry);
|
|
652
|
+
accumulatedTokens += messageTokens;
|
|
653
|
+
|
|
654
|
+
// Check if we've exceeded the budget
|
|
655
|
+
if (accumulatedTokens >= keepRecentTokens) {
|
|
656
|
+
// Find the closest valid cut point at or after this entry
|
|
657
|
+
let foundCutPoint = false;
|
|
658
|
+
for (let c = 0; c < cutPoints.length; c++) {
|
|
659
|
+
if (cutPoints[c] >= i) {
|
|
660
|
+
cutIndex = cutPoints[c];
|
|
661
|
+
foundCutPoint = true;
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (!foundCutPoint) {
|
|
666
|
+
cutIndex = cutPoints[cutPoints.length - 1];
|
|
667
|
+
}
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
|
|
673
|
+
while (cutIndex > startIndex) {
|
|
674
|
+
const prevEntry = entries[cutIndex - 1];
|
|
675
|
+
// Stop at session header or compaction boundaries
|
|
676
|
+
if (prevEntry.type === "compaction") {
|
|
677
|
+
break;
|
|
678
|
+
}
|
|
679
|
+
if (prevEntry.type === "message") {
|
|
680
|
+
// Stop if we hit any message
|
|
681
|
+
break;
|
|
682
|
+
}
|
|
683
|
+
// Include this non-message entry (bash, settings change, etc.)
|
|
684
|
+
cutIndex--;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// Determine if this is a split turn
|
|
688
|
+
const cutEntry = entries[cutIndex];
|
|
689
|
+
const isUserMessage = cutEntry.type === "message" && cutEntry.message.role === "user";
|
|
690
|
+
const turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
|
|
691
|
+
|
|
692
|
+
return {
|
|
693
|
+
firstKeptEntryIndex: cutIndex,
|
|
694
|
+
turnStartIndex,
|
|
695
|
+
isSplitTurn: !isUserMessage && turnStartIndex !== -1,
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// ============================================================================
|
|
700
|
+
// Summarization
|
|
701
|
+
// ============================================================================
|
|
702
|
+
|
|
703
|
+
const SUMMARIZATION_PROMPT = prompt.render(compactionSummaryPrompt);
|
|
704
|
+
|
|
705
|
+
const UPDATE_SUMMARIZATION_PROMPT = prompt.render(compactionUpdateSummaryPrompt);
|
|
706
|
+
|
|
707
|
+
const SHORT_SUMMARY_PROMPT = prompt.render(compactionShortSummaryPrompt);
|
|
708
|
+
|
|
709
|
+
const HANDOFF_DOCUMENT_PROMPT = prompt.render(handoffDocumentPrompt);
|
|
710
|
+
|
|
711
|
+
export const AUTO_HANDOFF_THRESHOLD_FOCUS = prompt.render(autoHandoffThresholdFocusPrompt);
|
|
712
|
+
|
|
713
|
+
function formatAdditionalContext(context: string[] | undefined): string {
|
|
714
|
+
if (!context || context.length === 0) return "";
|
|
715
|
+
const lines = context.map(line => `- ${line}`).join("\n");
|
|
716
|
+
return `<additional-context>\n${lines}\n</additional-context>\n\n`;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Generate a summary of the conversation using the LLM.
|
|
721
|
+
* If previousSummary is provided, uses the update prompt to merge.
|
|
722
|
+
*/
|
|
723
|
+
export interface SummaryOptions {
|
|
724
|
+
promptOverride?: string;
|
|
725
|
+
extraContext?: string[];
|
|
726
|
+
remoteEndpoint?: string;
|
|
727
|
+
remoteInstructions?: string;
|
|
728
|
+
initiatorOverride?: MessageAttribution;
|
|
729
|
+
metadata?: Record<string, unknown>;
|
|
730
|
+
convertToLlm?: ConvertToLlm;
|
|
731
|
+
/**
|
|
732
|
+
* Optional telemetry handle. When provided, every LLM call emitted during
|
|
733
|
+
* compaction is wrapped in an OTEL chat span tagged with
|
|
734
|
+
* `pi.gen_ai.oneshot.kind` (`compaction_summary`, `compaction_short_summary`,
|
|
735
|
+
* or `compaction_turn_prefix`). `undefined` keeps the call paths zero-cost.
|
|
736
|
+
*/
|
|
737
|
+
telemetry?: AgentTelemetry;
|
|
738
|
+
authCredentialType?: "api_key" | "oauth";
|
|
739
|
+
/**
|
|
740
|
+
* Provider session affinity id forwarded to the maintenance LLM call so it
|
|
741
|
+
* reuses the live turn's provider/WebSocket session (matches the
|
|
742
|
+
* `providerSessionId ?? sessionId` the agent loop sends for normal turns).
|
|
743
|
+
*/
|
|
744
|
+
sessionId?: string;
|
|
745
|
+
/** Shared provider state map so maintenance calls reuse session-scoped transport/session caches. */
|
|
746
|
+
providerSessionState?: Map<string, ProviderSessionState>;
|
|
747
|
+
/** Hint that websocket transport should be preferred when supported by the provider implementation. */
|
|
748
|
+
preferWebsockets?: boolean;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
export async function generateSummary(
|
|
752
|
+
currentMessages: AgentMessage[],
|
|
753
|
+
model: Model,
|
|
754
|
+
reserveTokens: number,
|
|
755
|
+
apiKey: string,
|
|
756
|
+
signal?: AbortSignal,
|
|
757
|
+
customInstructions?: string,
|
|
758
|
+
previousSummary?: string,
|
|
759
|
+
options?: SummaryOptions,
|
|
760
|
+
): Promise<string> {
|
|
761
|
+
const maxTokens = Math.floor(0.8 * reserveTokens);
|
|
762
|
+
|
|
763
|
+
// Use update prompt if we have a previous summary, otherwise initial prompt
|
|
764
|
+
let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
|
|
765
|
+
if (options?.promptOverride) {
|
|
766
|
+
basePrompt = options.promptOverride;
|
|
767
|
+
}
|
|
768
|
+
if (customInstructions) {
|
|
769
|
+
basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// Serialize conversation to text so model doesn't try to continue it
|
|
773
|
+
// Convert to LLM messages first (handles custom app messages when caller provides a transformer).
|
|
774
|
+
const llmMessages = (options?.convertToLlm ?? convertToLlm)(currentMessages);
|
|
775
|
+
const conversationText = serializeConversation(llmMessages);
|
|
776
|
+
|
|
777
|
+
// Build the prompt with conversation wrapped in tags
|
|
778
|
+
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
779
|
+
if (previousSummary) {
|
|
780
|
+
promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
|
|
781
|
+
}
|
|
782
|
+
promptText += formatAdditionalContext(options?.extraContext);
|
|
783
|
+
promptText += basePrompt;
|
|
784
|
+
|
|
785
|
+
const summarizationMessages = [
|
|
786
|
+
{
|
|
787
|
+
role: "user" as const,
|
|
788
|
+
content: [{ type: "text" as const, text: promptText }],
|
|
789
|
+
timestamp: Date.now(),
|
|
790
|
+
},
|
|
791
|
+
];
|
|
792
|
+
|
|
793
|
+
if (options?.remoteEndpoint) {
|
|
794
|
+
const remote = await requestRemoteCompaction(
|
|
795
|
+
options.remoteEndpoint,
|
|
796
|
+
{
|
|
797
|
+
systemPrompt: SUMMARIZATION_SYSTEM_PROMPT,
|
|
798
|
+
prompt: promptText,
|
|
799
|
+
},
|
|
800
|
+
signal,
|
|
801
|
+
);
|
|
802
|
+
return remote.summary;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const response = await instrumentedCompleteSimple(
|
|
806
|
+
model,
|
|
807
|
+
{ systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
|
|
808
|
+
{
|
|
809
|
+
maxTokens,
|
|
810
|
+
signal,
|
|
811
|
+
apiKey,
|
|
812
|
+
reasoning: Effort.High,
|
|
813
|
+
initiatorOverride: options?.initiatorOverride,
|
|
814
|
+
metadata: options?.metadata,
|
|
815
|
+
sessionId: options?.sessionId,
|
|
816
|
+
providerSessionState: options?.providerSessionState,
|
|
817
|
+
preferWebsockets: options?.preferWebsockets,
|
|
818
|
+
},
|
|
819
|
+
{ telemetry: options?.telemetry, oneshotKind: "compaction_summary" },
|
|
820
|
+
);
|
|
821
|
+
|
|
822
|
+
if (response.stopReason === "error") {
|
|
823
|
+
throw new Error(`Summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const textContent = response.content
|
|
827
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
828
|
+
.map(c => c.text)
|
|
829
|
+
.join("\n");
|
|
830
|
+
|
|
831
|
+
return textContent;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// ============================================================================
|
|
835
|
+
// Handoff generation
|
|
836
|
+
// ============================================================================
|
|
837
|
+
|
|
838
|
+
export interface HandoffOptions {
|
|
839
|
+
/** Live agent system prompt — passed verbatim so providers hit the cached prefix. */
|
|
840
|
+
systemPrompt: string[];
|
|
841
|
+
/** Live agent tool list — same purpose. Forced to `toolChoice: "none"`. */
|
|
842
|
+
tools?: AgentTool<any>[];
|
|
843
|
+
customInstructions?: string;
|
|
844
|
+
convertToLlm?: ConvertToLlm;
|
|
845
|
+
initiatorOverride?: MessageAttribution;
|
|
846
|
+
metadata?: Record<string, unknown>;
|
|
847
|
+
/**
|
|
848
|
+
* Optional telemetry handle. When provided, the handoff LLM call is
|
|
849
|
+
* wrapped in an OTEL chat span tagged with `pi.gen_ai.oneshot.kind = "handoff"`.
|
|
850
|
+
*/
|
|
851
|
+
telemetry?: AgentTelemetry;
|
|
852
|
+
authCredentialType?: "api_key" | "oauth";
|
|
853
|
+
/**
|
|
854
|
+
* Provider session affinity id forwarded to the handoff LLM call so it
|
|
855
|
+
* reuses the live turn's provider/WebSocket session.
|
|
856
|
+
*/
|
|
857
|
+
sessionId?: string;
|
|
858
|
+
/** Shared provider state map so the handoff call reuses session-scoped transport/session caches. */
|
|
859
|
+
providerSessionState?: Map<string, ProviderSessionState>;
|
|
860
|
+
/** Hint that websocket transport should be preferred when supported by the provider implementation. */
|
|
861
|
+
preferWebsockets?: boolean;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export function renderHandoffPrompt(customInstructions?: string): string {
|
|
865
|
+
if (!customInstructions) return HANDOFF_DOCUMENT_PROMPT;
|
|
866
|
+
return prompt.render(handoffDocumentPrompt, {
|
|
867
|
+
additionalFocus: customInstructions,
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
export async function generateHandoff(
|
|
872
|
+
messages: AgentMessage[],
|
|
873
|
+
model: Model,
|
|
874
|
+
apiKey: string,
|
|
875
|
+
options: HandoffOptions,
|
|
876
|
+
signal?: AbortSignal,
|
|
877
|
+
): Promise<string> {
|
|
878
|
+
const llmMessages = (options.convertToLlm ?? convertToLlm)(messages);
|
|
879
|
+
const requestMessages: Message[] = [
|
|
880
|
+
...llmMessages,
|
|
881
|
+
{
|
|
882
|
+
role: "user",
|
|
883
|
+
content: [{ type: "text", text: renderHandoffPrompt(options.customInstructions) }],
|
|
884
|
+
attribution: "agent",
|
|
885
|
+
timestamp: Date.now(),
|
|
886
|
+
},
|
|
887
|
+
];
|
|
888
|
+
|
|
889
|
+
const response = await instrumentedCompleteSimple(
|
|
890
|
+
model,
|
|
891
|
+
{
|
|
892
|
+
systemPrompt: options.systemPrompt,
|
|
893
|
+
messages: requestMessages,
|
|
894
|
+
tools: options.tools,
|
|
895
|
+
},
|
|
896
|
+
{
|
|
897
|
+
apiKey,
|
|
898
|
+
signal,
|
|
899
|
+
reasoning: Effort.High,
|
|
900
|
+
toolChoice: "none",
|
|
901
|
+
initiatorOverride: options.initiatorOverride,
|
|
902
|
+
metadata: options.metadata,
|
|
903
|
+
sessionId: options.sessionId,
|
|
904
|
+
providerSessionState: options.providerSessionState,
|
|
905
|
+
preferWebsockets: options.preferWebsockets,
|
|
906
|
+
},
|
|
907
|
+
{ telemetry: options.telemetry, oneshotKind: "handoff" },
|
|
908
|
+
);
|
|
909
|
+
|
|
910
|
+
if (response.stopReason === "error") {
|
|
911
|
+
throw new Error(`Handoff generation failed: ${response.errorMessage || "Unknown error"}`);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
return response.content
|
|
915
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
916
|
+
.map(c => c.text)
|
|
917
|
+
.join("\n");
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async function generateShortSummary(
|
|
921
|
+
recentMessages: AgentMessage[],
|
|
922
|
+
historySummary: string | undefined,
|
|
923
|
+
model: Model,
|
|
924
|
+
reserveTokens: number,
|
|
925
|
+
apiKey: string,
|
|
926
|
+
signal?: AbortSignal,
|
|
927
|
+
options?: SummaryOptions,
|
|
928
|
+
): Promise<string> {
|
|
929
|
+
const maxTokens = Math.min(512, Math.floor(0.2 * reserveTokens));
|
|
930
|
+
const llmMessages = (options?.convertToLlm ?? convertToLlm)(recentMessages);
|
|
931
|
+
const conversationText = serializeConversation(llmMessages);
|
|
932
|
+
|
|
933
|
+
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
934
|
+
if (historySummary) {
|
|
935
|
+
promptText += `<previous-summary>\n${historySummary}\n</previous-summary>\n\n`;
|
|
936
|
+
}
|
|
937
|
+
promptText += formatAdditionalContext(options?.extraContext);
|
|
938
|
+
promptText += SHORT_SUMMARY_PROMPT;
|
|
939
|
+
|
|
940
|
+
if (options?.remoteEndpoint) {
|
|
941
|
+
const remote = await requestRemoteCompaction(
|
|
942
|
+
options.remoteEndpoint,
|
|
943
|
+
{
|
|
944
|
+
systemPrompt: SUMMARIZATION_SYSTEM_PROMPT,
|
|
945
|
+
prompt: promptText,
|
|
946
|
+
},
|
|
947
|
+
signal,
|
|
948
|
+
);
|
|
949
|
+
return remote.summary;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const response = await instrumentedCompleteSimple(
|
|
953
|
+
model,
|
|
954
|
+
{
|
|
955
|
+
systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT],
|
|
956
|
+
messages: [{ role: "user", content: [{ type: "text", text: promptText }], timestamp: Date.now() }],
|
|
957
|
+
},
|
|
958
|
+
{
|
|
959
|
+
maxTokens,
|
|
960
|
+
signal,
|
|
961
|
+
apiKey,
|
|
962
|
+
reasoning: Effort.High,
|
|
963
|
+
initiatorOverride: options?.initiatorOverride,
|
|
964
|
+
metadata: options?.metadata,
|
|
965
|
+
sessionId: options?.sessionId,
|
|
966
|
+
providerSessionState: options?.providerSessionState,
|
|
967
|
+
preferWebsockets: options?.preferWebsockets,
|
|
968
|
+
},
|
|
969
|
+
{ telemetry: options?.telemetry, oneshotKind: "compaction_short_summary" },
|
|
970
|
+
);
|
|
971
|
+
|
|
972
|
+
if (response.stopReason === "error") {
|
|
973
|
+
throw new Error(`Short summary failed: ${response.errorMessage || "Unknown error"}`);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
return response.content
|
|
977
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
978
|
+
.map(c => c.text)
|
|
979
|
+
.join("\n");
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// ============================================================================
|
|
983
|
+
// Compaction Preparation (for hooks)
|
|
984
|
+
// ============================================================================
|
|
985
|
+
|
|
986
|
+
export interface CompactionPreparation {
|
|
987
|
+
/** UUID of first entry to keep */
|
|
988
|
+
firstKeptEntryId: string;
|
|
989
|
+
/** Messages that will be summarized and discarded */
|
|
990
|
+
messagesToSummarize: AgentMessage[];
|
|
991
|
+
/** Messages that will be turned into turn prefix summary (if splitting) */
|
|
992
|
+
turnPrefixMessages: AgentMessage[];
|
|
993
|
+
/** Messages kept in full after compaction (recent history) */
|
|
994
|
+
recentMessages: AgentMessage[];
|
|
995
|
+
/** Whether this is a split turn (cut point in middle of turn) */
|
|
996
|
+
isSplitTurn: boolean;
|
|
997
|
+
tokensBefore: number;
|
|
998
|
+
/** Summary from previous compaction, for iterative update */
|
|
999
|
+
previousSummary?: string;
|
|
1000
|
+
/** Preserved opaque compaction payload from the previous compaction, if any. */
|
|
1001
|
+
previousPreserveData?: Record<string, unknown>;
|
|
1002
|
+
/** File operations extracted from messagesToSummarize */
|
|
1003
|
+
fileOps: FileOperations;
|
|
1004
|
+
/** Compaction settions from settings.jsonl */
|
|
1005
|
+
settings: CompactionSettings;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
export function prepareCompaction(
|
|
1009
|
+
pathEntries: SessionEntry[],
|
|
1010
|
+
settings: CompactionSettings,
|
|
1011
|
+
): CompactionPreparation | undefined {
|
|
1012
|
+
if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") {
|
|
1013
|
+
return undefined;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
let prevCompactionIndex = -1;
|
|
1017
|
+
for (let i = pathEntries.length - 1; i >= 0; i--) {
|
|
1018
|
+
if (pathEntries[i].type === "compaction") {
|
|
1019
|
+
prevCompactionIndex = i;
|
|
1020
|
+
break;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
const boundaryStart = prevCompactionIndex + 1;
|
|
1024
|
+
const boundaryEnd = pathEntries.length;
|
|
1025
|
+
|
|
1026
|
+
const lastUsage = getLastAssistantUsage(pathEntries);
|
|
1027
|
+
const tokensBefore = lastUsage ? calculateContextTokens(lastUsage) : 0;
|
|
1028
|
+
let keepRecentTokens = settings.keepRecentTokens;
|
|
1029
|
+
if (lastUsage) {
|
|
1030
|
+
const estimatedTokens = estimateEntriesTokens(pathEntries, boundaryStart, boundaryEnd);
|
|
1031
|
+
const promptTokens = calculatePromptTokens(lastUsage);
|
|
1032
|
+
const ratio = estimatedTokens > 0 ? promptTokens / estimatedTokens : 0;
|
|
1033
|
+
if (Number.isFinite(ratio) && ratio > 1) {
|
|
1034
|
+
keepRecentTokens = Math.max(1, Math.floor(keepRecentTokens / ratio));
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, keepRecentTokens);
|
|
1039
|
+
|
|
1040
|
+
// Get ID of first kept entry
|
|
1041
|
+
const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex];
|
|
1042
|
+
if (!firstKeptEntry?.id) {
|
|
1043
|
+
return undefined; // Session needs migration
|
|
1044
|
+
}
|
|
1045
|
+
const firstKeptEntryId = firstKeptEntry.id;
|
|
1046
|
+
|
|
1047
|
+
const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
|
|
1048
|
+
|
|
1049
|
+
// Messages to summarize (will be discarded after summary)
|
|
1050
|
+
const messagesToSummarize: AgentMessage[] = [];
|
|
1051
|
+
for (let i = boundaryStart; i < historyEnd; i++) {
|
|
1052
|
+
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1053
|
+
if (msg) messagesToSummarize.push(msg);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// Messages for turn prefix summary (if splitting a turn)
|
|
1057
|
+
const turnPrefixMessages: AgentMessage[] = [];
|
|
1058
|
+
if (cutPoint.isSplitTurn) {
|
|
1059
|
+
for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {
|
|
1060
|
+
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1061
|
+
if (msg) turnPrefixMessages.push(msg);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// Messages kept after compaction (recent history)
|
|
1066
|
+
const recentMessages: AgentMessage[] = [];
|
|
1067
|
+
for (let i = cutPoint.firstKeptEntryIndex; i < boundaryEnd; i++) {
|
|
1068
|
+
const msg = getMessageFromEntry(pathEntries[i]);
|
|
1069
|
+
if (msg) recentMessages.push(msg);
|
|
1070
|
+
}
|
|
1071
|
+
// Nothing to summarize means compaction would be a no-op.
|
|
1072
|
+
if (messagesToSummarize.length === 0 && turnPrefixMessages.length === 0) {
|
|
1073
|
+
return undefined;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// Get previous summary and preserved data for iterative updates
|
|
1077
|
+
let previousSummary: string | undefined;
|
|
1078
|
+
let previousPreserveData: Record<string, unknown> | undefined;
|
|
1079
|
+
if (prevCompactionIndex >= 0) {
|
|
1080
|
+
const prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry;
|
|
1081
|
+
previousSummary = prevCompaction.summary;
|
|
1082
|
+
previousPreserveData = prevCompaction.preserveData;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// Extract file operations from messages and previous compaction
|
|
1086
|
+
const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);
|
|
1087
|
+
|
|
1088
|
+
// Also extract file ops from turn prefix if splitting
|
|
1089
|
+
if (cutPoint.isSplitTurn) {
|
|
1090
|
+
for (const msg of turnPrefixMessages) {
|
|
1091
|
+
extractFileOpsFromMessage(msg, fileOps);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
return {
|
|
1096
|
+
firstKeptEntryId,
|
|
1097
|
+
messagesToSummarize,
|
|
1098
|
+
turnPrefixMessages,
|
|
1099
|
+
recentMessages,
|
|
1100
|
+
isSplitTurn: cutPoint.isSplitTurn,
|
|
1101
|
+
tokensBefore,
|
|
1102
|
+
previousSummary,
|
|
1103
|
+
previousPreserveData,
|
|
1104
|
+
fileOps,
|
|
1105
|
+
settings,
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
// ============================================================================
|
|
1110
|
+
// Main compaction function
|
|
1111
|
+
// ============================================================================
|
|
1112
|
+
|
|
1113
|
+
const TURN_PREFIX_SUMMARIZATION_PROMPT = prompt.render(compactionTurnPrefixPrompt);
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* Generate summaries for compaction using prepared data.
|
|
1117
|
+
* Returns CompactionResult - SessionManager adds id/parentId when saving.
|
|
1118
|
+
*
|
|
1119
|
+
* @param preparation - Pre-calculated preparation from prepareCompaction()
|
|
1120
|
+
* @param customInstructions - Optional custom focus for the summary
|
|
1121
|
+
*/
|
|
1122
|
+
export async function compact(
|
|
1123
|
+
preparation: CompactionPreparation,
|
|
1124
|
+
model: Model,
|
|
1125
|
+
apiKey: string,
|
|
1126
|
+
customInstructions?: string,
|
|
1127
|
+
signal?: AbortSignal,
|
|
1128
|
+
options?: SummaryOptions,
|
|
1129
|
+
): Promise<CompactionResult> {
|
|
1130
|
+
const {
|
|
1131
|
+
firstKeptEntryId,
|
|
1132
|
+
messagesToSummarize,
|
|
1133
|
+
turnPrefixMessages,
|
|
1134
|
+
recentMessages,
|
|
1135
|
+
isSplitTurn,
|
|
1136
|
+
tokensBefore,
|
|
1137
|
+
previousSummary,
|
|
1138
|
+
previousPreserveData,
|
|
1139
|
+
fileOps,
|
|
1140
|
+
settings,
|
|
1141
|
+
} = preparation;
|
|
1142
|
+
|
|
1143
|
+
const summaryOptions: SummaryOptions = {
|
|
1144
|
+
promptOverride: options?.promptOverride,
|
|
1145
|
+
extraContext: options?.extraContext,
|
|
1146
|
+
remoteEndpoint: settings.remoteEnabled === false ? undefined : settings.remoteEndpoint,
|
|
1147
|
+
remoteInstructions: options?.remoteInstructions,
|
|
1148
|
+
initiatorOverride: options?.initiatorOverride,
|
|
1149
|
+
metadata: options?.metadata,
|
|
1150
|
+
convertToLlm: options?.convertToLlm,
|
|
1151
|
+
telemetry: options?.telemetry,
|
|
1152
|
+
sessionId: options?.sessionId,
|
|
1153
|
+
providerSessionState: options?.providerSessionState,
|
|
1154
|
+
preferWebsockets: options?.preferWebsockets,
|
|
1155
|
+
};
|
|
1156
|
+
|
|
1157
|
+
let preserveData = withOpenAiRemoteCompactionPreserveData(previousPreserveData, undefined);
|
|
1158
|
+
if (settings.remoteEnabled !== false && shouldUseOpenAiRemoteCompaction(model)) {
|
|
1159
|
+
const previousRemoteCompaction = getPreservedOpenAiRemoteCompactionData(previousPreserveData);
|
|
1160
|
+
const remoteMessages = [...messagesToSummarize, ...turnPrefixMessages, ...recentMessages];
|
|
1161
|
+
const previousReplacementHistory =
|
|
1162
|
+
previousRemoteCompaction?.provider === model.provider
|
|
1163
|
+
? previousRemoteCompaction.replacementHistory
|
|
1164
|
+
: undefined;
|
|
1165
|
+
const remoteHistory = buildOpenAiNativeHistory(
|
|
1166
|
+
(summaryOptions.convertToLlm ?? convertToLlm)(remoteMessages),
|
|
1167
|
+
model,
|
|
1168
|
+
previousReplacementHistory,
|
|
1169
|
+
);
|
|
1170
|
+
if (remoteHistory.length > 0) {
|
|
1171
|
+
try {
|
|
1172
|
+
const remote = await requestOpenAiRemoteCompaction(
|
|
1173
|
+
model,
|
|
1174
|
+
apiKey,
|
|
1175
|
+
remoteHistory,
|
|
1176
|
+
summaryOptions.remoteInstructions ?? SUMMARIZATION_SYSTEM_PROMPT,
|
|
1177
|
+
signal,
|
|
1178
|
+
{ authCredentialType: options?.authCredentialType },
|
|
1179
|
+
);
|
|
1180
|
+
preserveData = withOpenAiRemoteCompactionPreserveData(previousPreserveData, remote);
|
|
1181
|
+
} catch (err) {
|
|
1182
|
+
logger.warn("OpenAI remote compaction failed, falling back to local summarization", {
|
|
1183
|
+
error: err instanceof Error ? err.message : String(err),
|
|
1184
|
+
model: model.id,
|
|
1185
|
+
provider: model.provider,
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
// Generate summaries (can be parallel if both needed) and merge into one
|
|
1192
|
+
let summary: string;
|
|
1193
|
+
|
|
1194
|
+
// A single active Codex WebSocket session cannot service two concurrent
|
|
1195
|
+
// requests ("websocket request already in progress"). When the maintenance
|
|
1196
|
+
// calls use the Codex Responses provider, share one provider session, and
|
|
1197
|
+
// websocket transport is not explicitly disabled, run the split-turn history
|
|
1198
|
+
// and turn-prefix summaries sequentially. This covers websocket activation
|
|
1199
|
+
// from config/env/model defaults too: the provider can select websockets even
|
|
1200
|
+
// when `preferWebsockets` is undefined, while non-Codex providers keep the
|
|
1201
|
+
// previous parallel behavior.
|
|
1202
|
+
const summariesMayShareWebSocketSession = Boolean(
|
|
1203
|
+
model.api === "openai-codex-responses" &&
|
|
1204
|
+
summaryOptions.providerSessionState &&
|
|
1205
|
+
summaryOptions.preferWebsockets !== false,
|
|
1206
|
+
);
|
|
1207
|
+
|
|
1208
|
+
if (isSplitTurn && turnPrefixMessages.length > 0) {
|
|
1209
|
+
const runHistorySummary = () =>
|
|
1210
|
+
messagesToSummarize.length > 0
|
|
1211
|
+
? generateSummary(
|
|
1212
|
+
messagesToSummarize,
|
|
1213
|
+
model,
|
|
1214
|
+
settings.reserveTokens,
|
|
1215
|
+
apiKey,
|
|
1216
|
+
signal,
|
|
1217
|
+
customInstructions,
|
|
1218
|
+
previousSummary,
|
|
1219
|
+
summaryOptions,
|
|
1220
|
+
)
|
|
1221
|
+
: Promise.resolve("No prior history.");
|
|
1222
|
+
const runTurnPrefixSummary = () =>
|
|
1223
|
+
generateTurnPrefixSummary(turnPrefixMessages, model, settings.reserveTokens, apiKey, signal, summaryOptions);
|
|
1224
|
+
|
|
1225
|
+
let historyResult: string;
|
|
1226
|
+
let turnPrefixResult: string;
|
|
1227
|
+
if (summariesMayShareWebSocketSession) {
|
|
1228
|
+
// Sequential: avoids concurrent requests on the same provider session.
|
|
1229
|
+
historyResult = await runHistorySummary();
|
|
1230
|
+
turnPrefixResult = await runTurnPrefixSummary();
|
|
1231
|
+
} else {
|
|
1232
|
+
[historyResult, turnPrefixResult] = await Promise.all([runHistorySummary(), runTurnPrefixSummary()]);
|
|
1233
|
+
}
|
|
1234
|
+
// Merge into single summary
|
|
1235
|
+
summary = `${historyResult}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult}`;
|
|
1236
|
+
} else if (messagesToSummarize.length > 0) {
|
|
1237
|
+
// Generate history summary from messages to summarize
|
|
1238
|
+
summary = await generateSummary(
|
|
1239
|
+
messagesToSummarize,
|
|
1240
|
+
model,
|
|
1241
|
+
settings.reserveTokens,
|
|
1242
|
+
apiKey,
|
|
1243
|
+
signal,
|
|
1244
|
+
customInstructions,
|
|
1245
|
+
previousSummary,
|
|
1246
|
+
summaryOptions,
|
|
1247
|
+
);
|
|
1248
|
+
} else if (previousSummary) {
|
|
1249
|
+
// No new messages to summarize, preserve previous summary
|
|
1250
|
+
summary = previousSummary;
|
|
1251
|
+
} else {
|
|
1252
|
+
// No messages and no previous summary
|
|
1253
|
+
summary = "No prior history.";
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
const shortSummary = await generateShortSummary(
|
|
1257
|
+
recentMessages,
|
|
1258
|
+
summary,
|
|
1259
|
+
model,
|
|
1260
|
+
settings.reserveTokens,
|
|
1261
|
+
apiKey,
|
|
1262
|
+
signal,
|
|
1263
|
+
{
|
|
1264
|
+
extraContext: options?.extraContext,
|
|
1265
|
+
remoteEndpoint: summaryOptions.remoteEndpoint,
|
|
1266
|
+
initiatorOverride: summaryOptions.initiatorOverride,
|
|
1267
|
+
metadata: summaryOptions.metadata,
|
|
1268
|
+
telemetry: summaryOptions.telemetry,
|
|
1269
|
+
sessionId: summaryOptions.sessionId,
|
|
1270
|
+
providerSessionState: summaryOptions.providerSessionState,
|
|
1271
|
+
preferWebsockets: summaryOptions.preferWebsockets,
|
|
1272
|
+
},
|
|
1273
|
+
);
|
|
1274
|
+
|
|
1275
|
+
// Compute file lists and append to summary
|
|
1276
|
+
const { readFiles, modifiedFiles } = computeFileLists(fileOps);
|
|
1277
|
+
summary = upsertFileOperations(summary, readFiles, modifiedFiles);
|
|
1278
|
+
|
|
1279
|
+
if (!firstKeptEntryId) {
|
|
1280
|
+
throw new Error("First kept entry has no ID - session may need migration");
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
return {
|
|
1284
|
+
summary,
|
|
1285
|
+
shortSummary,
|
|
1286
|
+
firstKeptEntryId,
|
|
1287
|
+
tokensBefore,
|
|
1288
|
+
details: { readFiles, modifiedFiles } as CompactionDetails,
|
|
1289
|
+
preserveData,
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* Generate a summary for a turn prefix (when splitting a turn).
|
|
1295
|
+
*/
|
|
1296
|
+
async function generateTurnPrefixSummary(
|
|
1297
|
+
messages: AgentMessage[],
|
|
1298
|
+
model: Model,
|
|
1299
|
+
reserveTokens: number,
|
|
1300
|
+
apiKey: string,
|
|
1301
|
+
signal?: AbortSignal,
|
|
1302
|
+
options?: SummaryOptions,
|
|
1303
|
+
): Promise<string> {
|
|
1304
|
+
const maxTokens = Math.floor(0.5 * reserveTokens); // Smaller budget for turn prefix
|
|
1305
|
+
|
|
1306
|
+
const llmMessages = (options?.convertToLlm ?? convertToLlm)(messages);
|
|
1307
|
+
const conversationText = serializeConversation(llmMessages);
|
|
1308
|
+
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
|
|
1309
|
+
const summarizationMessages = [
|
|
1310
|
+
{
|
|
1311
|
+
role: "user" as const,
|
|
1312
|
+
content: [{ type: "text" as const, text: promptText }],
|
|
1313
|
+
timestamp: Date.now(),
|
|
1314
|
+
},
|
|
1315
|
+
];
|
|
1316
|
+
|
|
1317
|
+
const response = await instrumentedCompleteSimple(
|
|
1318
|
+
model,
|
|
1319
|
+
{ systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
|
|
1320
|
+
{
|
|
1321
|
+
maxTokens,
|
|
1322
|
+
signal,
|
|
1323
|
+
apiKey,
|
|
1324
|
+
reasoning: Effort.High,
|
|
1325
|
+
initiatorOverride: options?.initiatorOverride,
|
|
1326
|
+
metadata: options?.metadata,
|
|
1327
|
+
sessionId: options?.sessionId,
|
|
1328
|
+
providerSessionState: options?.providerSessionState,
|
|
1329
|
+
preferWebsockets: options?.preferWebsockets,
|
|
1330
|
+
},
|
|
1331
|
+
{ telemetry: options?.telemetry, oneshotKind: "compaction_turn_prefix" },
|
|
1332
|
+
);
|
|
1333
|
+
|
|
1334
|
+
if (response.stopReason === "error") {
|
|
1335
|
+
throw new Error(`Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
return response.content
|
|
1339
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
1340
|
+
.map(c => c.text)
|
|
1341
|
+
.join("\n");
|
|
1342
|
+
}
|