@swifty.js/swifty 0.0.26 → 0.0.28
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/README.md +14 -6
- package/dist/agent-3NON2EXE.js +4 -0
- package/dist/anthropic-BJ5GN2VT.js +4 -0
- package/dist/checker-N5TIJEN5.js +4 -0
- package/dist/{chunk-H7VXSMJR.js → chunk-C3WGLVVZ.js} +10 -10
- package/dist/chunk-D34FUVGU.js +4 -0
- package/dist/chunk-FPQ2LBL4.js +10 -0
- package/dist/chunk-JGSIQXEN.js +4 -0
- package/dist/{chunk-5KXBVL2A.js → chunk-K5ZK27BX.js} +1 -1
- package/dist/chunk-LZBOXJX2.js +301 -0
- package/dist/chunk-OTVZGPHD.js +121 -0
- package/dist/chunk-QCKJLO6X.js +4 -0
- package/dist/chunk-R63ASIIW.js +407 -0
- package/dist/chunk-WX3B64R4.js +4 -0
- package/dist/chunk-Y6SQB5FG.js +4 -0
- package/dist/{devtools-3OAJUZGE.js → devtools-OJ75EP3T.js} +1 -1
- package/dist/glob.wasm +0 -0
- package/dist/lib/agent-XYO2MXXK.js +11 -0
- package/dist/lib/{anthropic-RYPJDHQM.js → anthropic-TGJGAP5R.js} +4 -5
- package/dist/lib/{checker-6BW4222R.js → checker-AXHPKVBY.js} +2 -2
- package/dist/lib/{chunk-HK2Z6WP4.js → chunk-2IVEVG5N.js} +5 -1
- package/dist/lib/{chunk-2AUSNVIB.js → chunk-ALJAVRRX.js} +28 -34
- package/dist/lib/{chunk-GNI7YX6F.js → chunk-DJ6AILPN.js} +70 -24
- package/dist/lib/chunk-DV6RV5WU.js +2345 -0
- package/dist/lib/{chunk-3LU4APFB.js → chunk-FBSPZQGC.js} +126 -73
- package/dist/lib/chunk-IB5IQK2S.js +136 -0
- package/dist/lib/{chunk-PZ42NAFA.js → chunk-KG4MJGKQ.js} +19 -21
- package/dist/lib/{chunk-XFSN4LMA.js → chunk-P4F46PEF.js} +43 -14
- package/dist/lib/glob.wasm +0 -0
- package/dist/lib/index.d.ts +145 -123
- package/dist/lib/index.js +2186 -1726
- package/dist/lib/{openai-VX5VBXZ4.js → openai-5UCIIDPO.js} +2 -3
- package/dist/lib/{tool-filter-VF7TZRE5.js → tool-filter-R6HDDJHE.js} +1 -1
- package/dist/main.js +226 -196
- package/dist/{node-OES65R33.js → node-4LTSRXL2.js} +1 -1
- package/dist/{openai-6MXNADYN.js → openai-YLS2LUAI.js} +14 -14
- package/dist/server-GX72MJQF.js +244 -0
- package/dist/{tool-filter-NO7I3HFD.js → tool-filter-VBP6WOGO.js} +1 -1
- package/package.json +36 -32
- package/dist/agent-GAABBZRW.js +0 -4
- package/dist/anthropic-7QRAPLOY.js +0 -4
- package/dist/checker-3RWAYF2U.js +0 -4
- package/dist/chunk-7MHXMDYC.js +0 -35
- package/dist/chunk-A2VR3BC4.js +0 -4
- package/dist/chunk-FZPTNGTU.js +0 -4
- package/dist/chunk-MWZIARZE.js +0 -130
- package/dist/chunk-NR62AA5K.js +0 -4
- package/dist/chunk-QN27QEFS.js +0 -4
- package/dist/chunk-RMMK4PO5.js +0 -389
- package/dist/chunk-SIWLMWHH.js +0 -93
- package/dist/chunk-VC6OXOMP.js +0 -238
- package/dist/chunk-W37Y53LX.js +0 -4
- package/dist/chunk-XBHVVB5A.js +0 -10
- package/dist/lib/agent-U7MIUCKT.js +0 -9
- package/dist/lib/chunk-2QILP24G.js +0 -1120
- package/dist/lib/chunk-OO2CLOEE.js +0 -88
- package/dist/lib/chunk-RKJYTYQM.js +0 -1285
- package/dist/server-P2VE5HLJ.js +0 -244
|
@@ -1,1120 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ContextTooLongError
|
|
3
|
-
} from "./chunk-OO2CLOEE.js";
|
|
4
|
-
import {
|
|
5
|
-
asErrorString,
|
|
6
|
-
contentToText,
|
|
7
|
-
createChildLogger,
|
|
8
|
-
strArg
|
|
9
|
-
} from "./chunk-EY7HE52Q.js";
|
|
10
|
-
|
|
11
|
-
// src/conversation/conversation.ts
|
|
12
|
-
var ConversationManager = class {
|
|
13
|
-
history = [];
|
|
14
|
-
longTermMemoryInjected = false;
|
|
15
|
-
baselineTokens = 0;
|
|
16
|
-
_anchorCount = 0;
|
|
17
|
-
addUserMessage(content) {
|
|
18
|
-
this.history.push({
|
|
19
|
-
role: "user",
|
|
20
|
-
content
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
addAssistantMessage(content) {
|
|
24
|
-
this.history.push({ role: "assistant", content });
|
|
25
|
-
}
|
|
26
|
-
addToolUseMessage(text, toolUseId, toolName, args) {
|
|
27
|
-
this.history.push({
|
|
28
|
-
role: "assistant",
|
|
29
|
-
content: text,
|
|
30
|
-
toolUses: [{ toolUseId, toolName, arguments: args }]
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
addAssistantMessageWithTools(text, toolUses) {
|
|
34
|
-
this.history.push({ role: "assistant", content: text, toolUses });
|
|
35
|
-
}
|
|
36
|
-
addAssistantFull(text, thinking, toolUses) {
|
|
37
|
-
this.history.push({
|
|
38
|
-
role: "assistant",
|
|
39
|
-
content: text,
|
|
40
|
-
thinkingBlocks: thinking.length > 0 ? thinking : void 0,
|
|
41
|
-
toolUses: toolUses.length > 0 ? toolUses : void 0
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
addToolResultMessage(toolUseId, content, isError, contentBlocks) {
|
|
45
|
-
this.history.push({
|
|
46
|
-
role: "user",
|
|
47
|
-
content: "",
|
|
48
|
-
toolResults: [
|
|
49
|
-
{
|
|
50
|
-
toolUseId,
|
|
51
|
-
content,
|
|
52
|
-
...contentBlocks?.length ? { contentBlocks } : {},
|
|
53
|
-
isError
|
|
54
|
-
}
|
|
55
|
-
]
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
addToolResultsMessage(results) {
|
|
59
|
-
this.history.push({
|
|
60
|
-
role: "user",
|
|
61
|
-
content: "",
|
|
62
|
-
toolResults: results
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
addSystemReminder(content) {
|
|
66
|
-
this.history.push({
|
|
67
|
-
role: "user",
|
|
68
|
-
content: `<system-reminder>
|
|
69
|
-
${content}
|
|
70
|
-
</system-reminder>`
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Check whether a reminder containing `marker` still exists in history.
|
|
75
|
-
*
|
|
76
|
-
* Used to decide whether a "say-once" reminder needs re-injection. Compaction
|
|
77
|
-
* collapses history into a summary, which removes the original reminder; the
|
|
78
|
-
* caller uses this result to re-inject without hooking into the compaction path.
|
|
79
|
-
*/
|
|
80
|
-
hasReminderContaining(marker) {
|
|
81
|
-
return this.history.some((m) => m.role === "user" && contentToText(m.content).includes(marker));
|
|
82
|
-
}
|
|
83
|
-
injectLongTermMemory(instructions, memories, skills = "") {
|
|
84
|
-
if (this.longTermMemoryInjected) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const sections = [];
|
|
88
|
-
if (instructions) {
|
|
89
|
-
sections.push(
|
|
90
|
-
`# SWIFTY.md
|
|
91
|
-
Codebase and user instructions are shown below. Be sure to adhere to these instructions. IMPORTANT: These instructions OVERRIDE any default behavior and you MUST follow them exactly as written.
|
|
92
|
-
|
|
93
|
-
${instructions}}`
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
if (memories) {
|
|
97
|
-
sections.push("# Auto Memory\n" + memories);
|
|
98
|
-
}
|
|
99
|
-
if (skills) {
|
|
100
|
-
sections.push("# Available Skills\n" + skills);
|
|
101
|
-
}
|
|
102
|
-
if (sections.length === 0) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
106
|
-
sections.push(`# Current Date
|
|
107
|
-
|
|
108
|
-
Today's date is ${today}.`);
|
|
109
|
-
const body = sections.join("\n\n");
|
|
110
|
-
const wrapped = `
|
|
111
|
-
<system-reminder>
|
|
112
|
-
As you answer the user's questions, you can use the following context:
|
|
113
|
-
|
|
114
|
-
${body}
|
|
115
|
-
|
|
116
|
-
IMPORTANT: this context may or may not be relevant to your tasks. You should not respond to this context unless it is highly relevant to your task.
|
|
117
|
-
</system-reminder>`;
|
|
118
|
-
this.history.unshift({ role: "user", content: wrapped });
|
|
119
|
-
this.longTermMemoryInjected = true;
|
|
120
|
-
}
|
|
121
|
-
appendMessages(msgs) {
|
|
122
|
-
this.history.push(...msgs);
|
|
123
|
-
}
|
|
124
|
-
len() {
|
|
125
|
-
return this.history.length;
|
|
126
|
-
}
|
|
127
|
-
truncateTo(index) {
|
|
128
|
-
if (index < 0) {
|
|
129
|
-
index = 0;
|
|
130
|
-
}
|
|
131
|
-
if (index > this.history.length) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
this.history = this.history.slice(0, index);
|
|
135
|
-
}
|
|
136
|
-
// Empties the conversation in place. Replacing the manager would strand every
|
|
137
|
-
// component that captured the old instance — AgentTool holds one for its fork
|
|
138
|
-
// path — so a reset has to happen behind the same object. Clearing
|
|
139
|
-
// longTermMemoryInjected lets instructions, memories and skills be re-injected,
|
|
140
|
-
// exactly as they are for a brand-new conversation.
|
|
141
|
-
reset() {
|
|
142
|
-
this.history = [];
|
|
143
|
-
this.longTermMemoryInjected = false;
|
|
144
|
-
this.clearUsageAnchor();
|
|
145
|
-
}
|
|
146
|
-
getMessages() {
|
|
147
|
-
return [...this.history];
|
|
148
|
-
}
|
|
149
|
-
// Rebuild the history after a compaction: a summary user message followed by
|
|
150
|
-
// the verbatim recent tail (kept messages, structure preserved —
|
|
151
|
-
// tool_use/tool_result blocks intact). Used by doCompact so recent original
|
|
152
|
-
// messages survive instead of being collapsed into the summary. Ordering:
|
|
153
|
-
// summary first, then kept messages. No assistant ack — the kept tail already starts with an
|
|
154
|
-
// assistant message in most cases, and injecting an artificial ack wastes
|
|
155
|
-
// tokens and confuses the model's sense of conversation flow.
|
|
156
|
-
replaceWithCompacted(summaryContent, messagesToKeep) {
|
|
157
|
-
this.history = [{ role: "user", content: summaryContent }, ...messagesToKeep];
|
|
158
|
-
this.longTermMemoryInjected = false;
|
|
159
|
-
this.clearUsageAnchor();
|
|
160
|
-
}
|
|
161
|
-
recordUsageAnchor(input, output, cacheRead, cacheCreation) {
|
|
162
|
-
const baseline = input + cacheRead + cacheCreation + output;
|
|
163
|
-
if (baseline <= 0) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
this.baselineTokens = baseline;
|
|
167
|
-
this._anchorCount = this.history.length;
|
|
168
|
-
}
|
|
169
|
-
clearUsageAnchor() {
|
|
170
|
-
this.baselineTokens = 0;
|
|
171
|
-
this._anchorCount = 0;
|
|
172
|
-
}
|
|
173
|
-
usageAnchorState() {
|
|
174
|
-
if (this.baselineTokens <= 0) {
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
return {
|
|
178
|
-
baselineTokens: this.baselineTokens,
|
|
179
|
-
anchorCount: this._anchorCount
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
// src/tools/types.ts
|
|
185
|
-
function isRecord(value) {
|
|
186
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
187
|
-
}
|
|
188
|
-
var IMAGE_MEDIA_TYPES = /* @__PURE__ */ new Set([
|
|
189
|
-
"image/jpeg",
|
|
190
|
-
"image/png",
|
|
191
|
-
"image/gif",
|
|
192
|
-
"image/webp"
|
|
193
|
-
]);
|
|
194
|
-
function isImageMediaType(value) {
|
|
195
|
-
return IMAGE_MEDIA_TYPES.has(value);
|
|
196
|
-
}
|
|
197
|
-
function normalizeTextBlock(value) {
|
|
198
|
-
return isRecord(value) && value.type === "text" && typeof value.text === "string" ? { type: "text", text: value.text } : null;
|
|
199
|
-
}
|
|
200
|
-
function normalizeImageBlock(value) {
|
|
201
|
-
if (!isRecord(value) || value.type !== "image" || !isRecord(value.source)) {
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
if (value.source.type === "url" && typeof value.source.url === "string") {
|
|
205
|
-
return { type: "image", source: { type: "url", url: value.source.url } };
|
|
206
|
-
}
|
|
207
|
-
if (value.source.type === "base64" && typeof value.source.media_type === "string" && isImageMediaType(value.source.media_type) && typeof value.source.data === "string") {
|
|
208
|
-
return {
|
|
209
|
-
type: "image",
|
|
210
|
-
source: {
|
|
211
|
-
type: "base64",
|
|
212
|
-
media_type: value.source.media_type,
|
|
213
|
-
data: value.source.data
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
function normalizeDocumentBlock(value) {
|
|
220
|
-
if (value.type !== "document" || !isRecord(value.source)) {
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
let source = null;
|
|
224
|
-
if (value.source.type === "url" && typeof value.source.url === "string") {
|
|
225
|
-
source = { type: "url", url: value.source.url };
|
|
226
|
-
} else if (value.source.type === "base64" && value.source.media_type === "application/pdf" && typeof value.source.data === "string") {
|
|
227
|
-
source = { type: "base64", media_type: "application/pdf", data: value.source.data };
|
|
228
|
-
} else if (value.source.type === "text" && value.source.media_type === "text/plain" && typeof value.source.data === "string") {
|
|
229
|
-
source = { type: "text", media_type: "text/plain", data: value.source.data };
|
|
230
|
-
} else if (value.source.type === "content") {
|
|
231
|
-
if (typeof value.source.content === "string") {
|
|
232
|
-
source = { type: "content", content: value.source.content };
|
|
233
|
-
} else if (Array.isArray(value.source.content)) {
|
|
234
|
-
const content = [];
|
|
235
|
-
for (const raw of value.source.content) {
|
|
236
|
-
const block = normalizeTextBlock(raw) ?? normalizeImageBlock(raw);
|
|
237
|
-
if (!block) {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
content.push(block);
|
|
241
|
-
}
|
|
242
|
-
source = { type: "content", content };
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
if (!source) {
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
return {
|
|
249
|
-
type: "document",
|
|
250
|
-
source,
|
|
251
|
-
...typeof value.title === "string" || value.title === null ? { title: value.title } : {},
|
|
252
|
-
...typeof value.context === "string" || value.context === null ? { context: value.context } : {}
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
function normalizeToolResultContentBlock(value) {
|
|
256
|
-
const text = normalizeTextBlock(value);
|
|
257
|
-
if (text) {
|
|
258
|
-
return text;
|
|
259
|
-
}
|
|
260
|
-
const image = normalizeImageBlock(value);
|
|
261
|
-
if (image) {
|
|
262
|
-
return image;
|
|
263
|
-
}
|
|
264
|
-
if (!isRecord(value)) {
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
if (value.type === "tool_reference" && typeof value.tool_name === "string") {
|
|
268
|
-
return { type: "tool_reference", tool_name: value.tool_name };
|
|
269
|
-
}
|
|
270
|
-
if (value.type === "search_result" && typeof value.source === "string" && typeof value.title === "string" && Array.isArray(value.content)) {
|
|
271
|
-
const content = [];
|
|
272
|
-
for (const raw of value.content) {
|
|
273
|
-
const block = normalizeTextBlock(raw);
|
|
274
|
-
if (!block) {
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
277
|
-
content.push(block);
|
|
278
|
-
}
|
|
279
|
-
return { type: "search_result", source: value.source, title: value.title, content };
|
|
280
|
-
}
|
|
281
|
-
return normalizeDocumentBlock(value);
|
|
282
|
-
}
|
|
283
|
-
function isToolResultContentBlock(value) {
|
|
284
|
-
return normalizeToolResultContentBlock(value) !== null;
|
|
285
|
-
}
|
|
286
|
-
var SKIP_DIRS = /* @__PURE__ */ new Set([
|
|
287
|
-
".claude",
|
|
288
|
-
// Claude Code
|
|
289
|
-
".git",
|
|
290
|
-
// Git
|
|
291
|
-
".swifty",
|
|
292
|
-
// Swifty
|
|
293
|
-
".next",
|
|
294
|
-
// Next.js
|
|
295
|
-
".venv",
|
|
296
|
-
// Python venv
|
|
297
|
-
".mypy_cache",
|
|
298
|
-
// Python mypy
|
|
299
|
-
".tox",
|
|
300
|
-
// Python tox
|
|
301
|
-
"__pycache__",
|
|
302
|
-
// Python
|
|
303
|
-
"build",
|
|
304
|
-
// C++
|
|
305
|
-
"dist",
|
|
306
|
-
// Webpack, Vite
|
|
307
|
-
"node_modules",
|
|
308
|
-
// Node.js
|
|
309
|
-
"vendor",
|
|
310
|
-
// Go
|
|
311
|
-
"venv"
|
|
312
|
-
// Python venv
|
|
313
|
-
]);
|
|
314
|
-
|
|
315
|
-
// src/session/session.ts
|
|
316
|
-
import { randomBytes } from "crypto";
|
|
317
|
-
import {
|
|
318
|
-
readFileSync,
|
|
319
|
-
writeFileSync,
|
|
320
|
-
readdirSync,
|
|
321
|
-
mkdirSync,
|
|
322
|
-
statSync,
|
|
323
|
-
existsSync,
|
|
324
|
-
unlinkSync,
|
|
325
|
-
rmSync
|
|
326
|
-
} from "fs";
|
|
327
|
-
import { join } from "path";
|
|
328
|
-
import z, { parse, safeParse } from "zod";
|
|
329
|
-
var COMPACT_BOUNDARY = "compact_boundary";
|
|
330
|
-
var SESSION_EXPIRY_DAYS = 30;
|
|
331
|
-
var ToolUseRecordSchema = z.object({
|
|
332
|
-
tool_use_id: z.string(),
|
|
333
|
-
tool_name: z.string(),
|
|
334
|
-
arguments: z.record(z.string(), z.unknown()).optional()
|
|
335
|
-
});
|
|
336
|
-
var ContentSchema = z.union([z.string(), z.array(z.record(z.string(), z.unknown()))]);
|
|
337
|
-
var ToolResultRecordSchema = z.object({
|
|
338
|
-
tool_use_id: z.string(),
|
|
339
|
-
content: ContentSchema,
|
|
340
|
-
content_blocks: z.array(z.unknown()).optional(),
|
|
341
|
-
is_error: z.boolean().optional()
|
|
342
|
-
});
|
|
343
|
-
var SessionMessageSchema = z.object({
|
|
344
|
-
role: z.string(),
|
|
345
|
-
content: ContentSchema.default(""),
|
|
346
|
-
timestamp: z.number(),
|
|
347
|
-
type: z.string().optional(),
|
|
348
|
-
tool_uses: z.array(ToolUseRecordSchema).optional(),
|
|
349
|
-
tool_results: z.array(ToolResultRecordSchema).optional()
|
|
350
|
-
});
|
|
351
|
-
var KeptMessageSchema = z.object({
|
|
352
|
-
role: z.string(),
|
|
353
|
-
content: ContentSchema,
|
|
354
|
-
tool_uses: z.array(ToolUseRecordSchema).optional(),
|
|
355
|
-
tool_results: z.array(ToolResultRecordSchema).optional()
|
|
356
|
-
});
|
|
357
|
-
function toolUsesToRecords(toolUses) {
|
|
358
|
-
return (toolUses ?? []).map((tu) => ({
|
|
359
|
-
tool_use_id: tu.toolUseId,
|
|
360
|
-
tool_name: tu.toolName,
|
|
361
|
-
...tu.arguments && Object.keys(tu.arguments).length ? { arguments: tu.arguments } : {}
|
|
362
|
-
}));
|
|
363
|
-
}
|
|
364
|
-
function toolResultsToRecords(toolResults) {
|
|
365
|
-
return (toolResults ?? []).map((tr) => ({
|
|
366
|
-
tool_use_id: tr.toolUseId,
|
|
367
|
-
content: tr.content,
|
|
368
|
-
...tr.contentBlocks?.length ? { content_blocks: tr.contentBlocks } : {},
|
|
369
|
-
...tr.isError ? { is_error: true } : {}
|
|
370
|
-
}));
|
|
371
|
-
}
|
|
372
|
-
var CompactBoundaryPayloadSchema = z.object({
|
|
373
|
-
summary: z.string(),
|
|
374
|
-
keep: z.array(KeptMessageSchema)
|
|
375
|
-
});
|
|
376
|
-
var log = createChildLogger({ module: "session" });
|
|
377
|
-
function sessionsDir(workDir) {
|
|
378
|
-
return join(workDir, ".swifty", "sessions");
|
|
379
|
-
}
|
|
380
|
-
function getSessionFilePath(workDir, sessionId) {
|
|
381
|
-
return join(sessionsDir(workDir), sessionId + ".jsonl");
|
|
382
|
-
}
|
|
383
|
-
function newSessionId() {
|
|
384
|
-
const ts = Date.now().toString(36);
|
|
385
|
-
const rand = randomBytes(4).toString("hex");
|
|
386
|
-
return `${ts}-${rand}`;
|
|
387
|
-
}
|
|
388
|
-
function saveMessage(workDir, sessionId, msg) {
|
|
389
|
-
const dir = sessionsDir(workDir);
|
|
390
|
-
mkdirSync(dir, { recursive: true });
|
|
391
|
-
const filePath = join(dir, `${sessionId}.jsonl`);
|
|
392
|
-
const line = JSON.stringify(msg) + "\n";
|
|
393
|
-
writeFileSync(filePath, line, { flag: (
|
|
394
|
-
/* append */
|
|
395
|
-
"a"
|
|
396
|
-
), encoding: "utf-8" });
|
|
397
|
-
}
|
|
398
|
-
function saveCompactBoundary(workDir, sessionId, payload) {
|
|
399
|
-
saveMessage(workDir, sessionId, {
|
|
400
|
-
role: "system",
|
|
401
|
-
content: JSON.stringify(payload),
|
|
402
|
-
timestamp: Math.floor(Date.now() / 1e3),
|
|
403
|
-
type: COMPACT_BOUNDARY
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
function loadSession(workDir, sessionId) {
|
|
407
|
-
const filePath = join(sessionsDir(workDir), `${sessionId}.jsonl`);
|
|
408
|
-
if (!existsSync(filePath)) {
|
|
409
|
-
return [];
|
|
410
|
-
}
|
|
411
|
-
const out = [];
|
|
412
|
-
for (const line of readFileSync(filePath, "utf-8").split("\n")) {
|
|
413
|
-
if (!line.trim()) {
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
try {
|
|
417
|
-
const message = JSON.parse(line);
|
|
418
|
-
const { success, data, error } = safeParse(SessionMessageSchema, message);
|
|
419
|
-
if (success) {
|
|
420
|
-
const isEmpty = !data.content && // empty content
|
|
421
|
-
!(data.tool_uses?.length ?? 0) && // empty tool uses
|
|
422
|
-
!(data.tool_results?.length ?? 0);
|
|
423
|
-
if (!isEmpty) {
|
|
424
|
-
out.push(data);
|
|
425
|
-
}
|
|
426
|
-
} else {
|
|
427
|
-
log.error({ err: error }, "session operation failed");
|
|
428
|
-
}
|
|
429
|
-
} catch (err) {
|
|
430
|
-
log.error({ err }, "session operation failed");
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
return out;
|
|
434
|
-
}
|
|
435
|
-
function recordsToCamelUses(recs) {
|
|
436
|
-
return recs?.map((tu) => ({
|
|
437
|
-
toolUseId: tu.tool_use_id,
|
|
438
|
-
toolName: tu.tool_name,
|
|
439
|
-
arguments: tu.arguments
|
|
440
|
-
}));
|
|
441
|
-
}
|
|
442
|
-
function validContentBlocks(value) {
|
|
443
|
-
if (!value) {
|
|
444
|
-
return void 0;
|
|
445
|
-
}
|
|
446
|
-
const blocks = [];
|
|
447
|
-
for (const raw of value) {
|
|
448
|
-
const block = normalizeToolResultContentBlock(raw);
|
|
449
|
-
if (block) {
|
|
450
|
-
blocks.push(block);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
return blocks.length > 0 ? blocks : void 0;
|
|
454
|
-
}
|
|
455
|
-
function recordsToCamelResults(recs) {
|
|
456
|
-
return recs?.map((tr) => {
|
|
457
|
-
const legacyBlocks = Array.isArray(tr.content) ? validContentBlocks(tr.content) : void 0;
|
|
458
|
-
const contentBlocks = validContentBlocks(tr.content_blocks) ?? legacyBlocks;
|
|
459
|
-
return {
|
|
460
|
-
toolUseId: tr.tool_use_id,
|
|
461
|
-
content: typeof tr.content === "string" ? tr.content : contentToText(tr.content),
|
|
462
|
-
...contentBlocks ? { contentBlocks } : {},
|
|
463
|
-
isError: tr.is_error ?? false
|
|
464
|
-
};
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
function rebuildFromSession(saved) {
|
|
468
|
-
let lastBoundary = -1;
|
|
469
|
-
for (let i = saved.length - 1; i >= 0; i--) {
|
|
470
|
-
if (saved[i].type === COMPACT_BOUNDARY) {
|
|
471
|
-
lastBoundary = i;
|
|
472
|
-
break;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
const out = [];
|
|
476
|
-
if (lastBoundary >= 0) {
|
|
477
|
-
let payload = null;
|
|
478
|
-
try {
|
|
479
|
-
const raw = saved[lastBoundary].content;
|
|
480
|
-
const payload_ = typeof raw === "string" ? JSON.parse(raw) : null;
|
|
481
|
-
payload = parse(CompactBoundaryPayloadSchema, payload_);
|
|
482
|
-
} catch {
|
|
483
|
-
payload = null;
|
|
484
|
-
}
|
|
485
|
-
if (payload) {
|
|
486
|
-
let resumeSummary = "This session continues from a previous conversation, which has been compressed due to context limitations. Here is a summary of the earlier messages:\n\n" + payload.summary;
|
|
487
|
-
if (payload.keep.length > 0) {
|
|
488
|
-
resumeSummary += "\n\nRecent messages have been preserved verbatim.";
|
|
489
|
-
}
|
|
490
|
-
out.push({ role: "user", content: resumeSummary });
|
|
491
|
-
for (const k of payload.keep) {
|
|
492
|
-
if (k.role !== "user" && k.role !== "assistant" || !k.content && // empty content
|
|
493
|
-
!(k.tool_uses?.length ?? 0) && // empty tool uses
|
|
494
|
-
!(k.tool_results?.length ?? 0)) {
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
out.push({
|
|
498
|
-
role: k.role,
|
|
499
|
-
content: k.content,
|
|
500
|
-
toolUses: recordsToCamelUses(k.tool_uses),
|
|
501
|
-
toolResults: recordsToCamelResults(k.tool_results)
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
for (let i = lastBoundary + 1; i < saved.length; i++) {
|
|
506
|
-
const m = saved[i];
|
|
507
|
-
if (m.type === COMPACT_BOUNDARY) {
|
|
508
|
-
continue;
|
|
509
|
-
}
|
|
510
|
-
const restored = toRestored(m);
|
|
511
|
-
if (restored) {
|
|
512
|
-
out.push(restored);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
return out;
|
|
516
|
-
}
|
|
517
|
-
for (const m of saved) {
|
|
518
|
-
const restored = toRestored(m);
|
|
519
|
-
if (restored) {
|
|
520
|
-
out.push(restored);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
return out;
|
|
524
|
-
}
|
|
525
|
-
function toRestored(m) {
|
|
526
|
-
if (m.role !== "user" && m.role !== "assistant") {
|
|
527
|
-
return null;
|
|
528
|
-
}
|
|
529
|
-
if (!m.content && !(m.tool_uses?.length ?? 0) && // empty tool uses
|
|
530
|
-
!(m.tool_results?.length ?? 0)) {
|
|
531
|
-
return null;
|
|
532
|
-
}
|
|
533
|
-
return {
|
|
534
|
-
role: m.role,
|
|
535
|
-
content: m.content,
|
|
536
|
-
toolUses: recordsToCamelUses(m.tool_uses),
|
|
537
|
-
toolResults: recordsToCamelResults(m.tool_results)
|
|
538
|
-
};
|
|
539
|
-
}
|
|
540
|
-
function listSessions(workDir) {
|
|
541
|
-
const dir = sessionsDir(workDir);
|
|
542
|
-
if (!existsSync(dir)) {
|
|
543
|
-
return [];
|
|
544
|
-
}
|
|
545
|
-
const files = readdirSync(dir).filter((f) => f.endsWith(".jsonl"));
|
|
546
|
-
const sessions = [];
|
|
547
|
-
for (const file of files) {
|
|
548
|
-
const filePath = join(dir, file);
|
|
549
|
-
const stat = statSync(filePath);
|
|
550
|
-
const id = file.replace(".jsonl", "");
|
|
551
|
-
let firstMessage = "";
|
|
552
|
-
let messageCount = 0;
|
|
553
|
-
try {
|
|
554
|
-
for (const line of readFileSync(filePath, "utf-8").split("\n")) {
|
|
555
|
-
if (!line.trim()) {
|
|
556
|
-
continue;
|
|
557
|
-
}
|
|
558
|
-
let m;
|
|
559
|
-
try {
|
|
560
|
-
const raw = JSON.parse(line);
|
|
561
|
-
m = parse(SessionMessageSchema, raw);
|
|
562
|
-
} catch (err) {
|
|
563
|
-
log.error({ err }, "session operation failed");
|
|
564
|
-
continue;
|
|
565
|
-
}
|
|
566
|
-
messageCount++;
|
|
567
|
-
if (!firstMessage && m.role === "user" && m.content) {
|
|
568
|
-
firstMessage = contentToText(m.content).slice(0, 100);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
} catch (err2) {
|
|
572
|
-
log.error({ err: err2 }, "session operation failed");
|
|
573
|
-
continue;
|
|
574
|
-
}
|
|
575
|
-
sessions.push({
|
|
576
|
-
id,
|
|
577
|
-
firstMessage,
|
|
578
|
-
messageCount,
|
|
579
|
-
size: stat.size,
|
|
580
|
-
modTime: stat.mtime
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
sessions.sort((a, b) => b.modTime.getTime() - a.modTime.getTime());
|
|
584
|
-
return sessions;
|
|
585
|
-
}
|
|
586
|
-
function cleanExpiredSessions(workDir) {
|
|
587
|
-
const dir = sessionsDir(workDir);
|
|
588
|
-
if (!existsSync(dir)) {
|
|
589
|
-
return 0;
|
|
590
|
-
}
|
|
591
|
-
const now = Date.now();
|
|
592
|
-
const expiryMs = SESSION_EXPIRY_DAYS * 24 * 60 * 60 * 1e3;
|
|
593
|
-
let removed = 0;
|
|
594
|
-
let files;
|
|
595
|
-
try {
|
|
596
|
-
files = readdirSync(dir).filter((f) => f.endsWith(".jsonl"));
|
|
597
|
-
} catch (err) {
|
|
598
|
-
log.error({ err }, "session operation failed");
|
|
599
|
-
return 0;
|
|
600
|
-
}
|
|
601
|
-
for (const file of files) {
|
|
602
|
-
const filePath = join(dir, file);
|
|
603
|
-
try {
|
|
604
|
-
const stat = statSync(filePath);
|
|
605
|
-
if (now - stat.mtimeMs > expiryMs) {
|
|
606
|
-
unlinkSync(filePath);
|
|
607
|
-
const id = file.replace(".jsonl", "");
|
|
608
|
-
const toolResultsDir = join(workDir, ".swifty", "sessions", id, "tool_results");
|
|
609
|
-
try {
|
|
610
|
-
rmSync(toolResultsDir, { recursive: true, force: true });
|
|
611
|
-
} catch {
|
|
612
|
-
}
|
|
613
|
-
const sessionSubdir = join(workDir, ".swifty", "sessions", id);
|
|
614
|
-
try {
|
|
615
|
-
rmSync(sessionSubdir, { recursive: false });
|
|
616
|
-
} catch {
|
|
617
|
-
}
|
|
618
|
-
removed++;
|
|
619
|
-
}
|
|
620
|
-
} catch (err) {
|
|
621
|
-
log.error({ err }, "session operation failed");
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
return removed;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// src/compact/compact.ts
|
|
628
|
-
var MAX_CONSECUTIVE_FAILURES = 3;
|
|
629
|
-
var MAX_PTL_RETRIES = 3;
|
|
630
|
-
var PTL_RETRY_MARKER = "[earlier conversation truncated for compaction retry]";
|
|
631
|
-
var CHARS_PER_TOKEN = 3.5;
|
|
632
|
-
var KEEP_RECENT_TOKENS = 1e4;
|
|
633
|
-
var MIN_KEEP_MESSAGES = 5;
|
|
634
|
-
var KEEP_MAX_TOKENS = 4e4;
|
|
635
|
-
var MIN_COMPACT_PREFIX = 2;
|
|
636
|
-
var SUMMARY_OUTPUT_RESERVE = 2e4;
|
|
637
|
-
var AUTO_COMPACT_SAFETY_MARGIN = 13e3;
|
|
638
|
-
var MANUAL_COMPACT_SAFETY_MARGIN = 3e3;
|
|
639
|
-
function computeCompactThreshold(contextWindow, maxOutput, manual = false) {
|
|
640
|
-
const effective = contextWindow - Math.min(maxOutput, SUMMARY_OUTPUT_RESERVE);
|
|
641
|
-
const margin = manual ? MANUAL_COMPACT_SAFETY_MARGIN : AUTO_COMPACT_SAFETY_MARGIN;
|
|
642
|
-
return effective - margin;
|
|
643
|
-
}
|
|
644
|
-
var AutoCompactTrackingState = class {
|
|
645
|
-
consecutiveFailures = 0;
|
|
646
|
-
};
|
|
647
|
-
var IMAGE_CHAR_EQUIV = 7e3;
|
|
648
|
-
function contentChars(content) {
|
|
649
|
-
if (typeof content === "string") {
|
|
650
|
-
return content.length;
|
|
651
|
-
}
|
|
652
|
-
let chars = 0;
|
|
653
|
-
for (const block of content) {
|
|
654
|
-
if (block.type === "text") {
|
|
655
|
-
chars += strArg(block, "text").length;
|
|
656
|
-
} else if (block.type === "image") {
|
|
657
|
-
chars += IMAGE_CHAR_EQUIV;
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
return chars;
|
|
661
|
-
}
|
|
662
|
-
function toolResultBlocksChars(blocks) {
|
|
663
|
-
let textChars = 0;
|
|
664
|
-
let richChars = 0;
|
|
665
|
-
for (const block of blocks) {
|
|
666
|
-
switch (block.type) {
|
|
667
|
-
case "text": {
|
|
668
|
-
textChars += block.text.length;
|
|
669
|
-
break;
|
|
670
|
-
}
|
|
671
|
-
case "image": {
|
|
672
|
-
richChars += IMAGE_CHAR_EQUIV;
|
|
673
|
-
break;
|
|
674
|
-
}
|
|
675
|
-
case "tool_reference": {
|
|
676
|
-
richChars += block.tool_name.length;
|
|
677
|
-
break;
|
|
678
|
-
}
|
|
679
|
-
case "search_result": {
|
|
680
|
-
richChars += block.source.length + block.title.length;
|
|
681
|
-
richChars += block.content.reduce((sum, content) => sum + content.text.length, 0);
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
case "document": {
|
|
685
|
-
switch (block.source.type) {
|
|
686
|
-
case "base64": {
|
|
687
|
-
richChars += IMAGE_CHAR_EQUIV;
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
case "url": {
|
|
691
|
-
richChars += block.source.url.length;
|
|
692
|
-
break;
|
|
693
|
-
}
|
|
694
|
-
case "text": {
|
|
695
|
-
richChars += block.source.data.length;
|
|
696
|
-
break;
|
|
697
|
-
}
|
|
698
|
-
case "content": {
|
|
699
|
-
if (typeof block.source.content === "string") {
|
|
700
|
-
richChars += block.source.content.length;
|
|
701
|
-
break;
|
|
702
|
-
}
|
|
703
|
-
for (const content of block.source.content) {
|
|
704
|
-
richChars += content.type === "text" ? content.text.length : IMAGE_CHAR_EQUIV;
|
|
705
|
-
}
|
|
706
|
-
break;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
break;
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
return { textChars, richChars };
|
|
714
|
-
}
|
|
715
|
-
function estimateMessages(messages) {
|
|
716
|
-
let totalChars = 0;
|
|
717
|
-
for (const msg of messages) {
|
|
718
|
-
totalChars += contentChars(msg.content);
|
|
719
|
-
if (msg.toolUses) {
|
|
720
|
-
totalChars += JSON.stringify(msg.toolUses).length;
|
|
721
|
-
}
|
|
722
|
-
if (msg.toolResults) {
|
|
723
|
-
for (const tr of msg.toolResults) {
|
|
724
|
-
if (tr.contentBlocks?.length) {
|
|
725
|
-
const blockChars = toolResultBlocksChars(tr.contentBlocks);
|
|
726
|
-
totalChars += Math.max(tr.content.length, blockChars.textChars) + blockChars.richChars;
|
|
727
|
-
} else {
|
|
728
|
-
totalChars += tr.content.length;
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
if (msg.thinkingBlocks) {
|
|
733
|
-
for (const tb of msg.thinkingBlocks) {
|
|
734
|
-
totalChars += tb.thinking.length;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
return Math.ceil(totalChars / CHARS_PER_TOKEN);
|
|
739
|
-
}
|
|
740
|
-
function estimateTokens(conv) {
|
|
741
|
-
return estimateMessages(conv.getMessages());
|
|
742
|
-
}
|
|
743
|
-
function estimateOne(msg) {
|
|
744
|
-
return estimateMessages([msg]);
|
|
745
|
-
}
|
|
746
|
-
function hasToolResult(msg) {
|
|
747
|
-
return msg.role === "user" && !!msg.toolResults && msg.toolResults.length > 0;
|
|
748
|
-
}
|
|
749
|
-
function computeKeepStartIndex(messages) {
|
|
750
|
-
let keepTokens = 0;
|
|
751
|
-
let keepCount = 0;
|
|
752
|
-
let keepStart = messages.length;
|
|
753
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
754
|
-
const t = estimateOne(messages[i]);
|
|
755
|
-
if (keepCount > 0 && keepTokens + t > KEEP_MAX_TOKENS) {
|
|
756
|
-
break;
|
|
757
|
-
}
|
|
758
|
-
keepStart = i;
|
|
759
|
-
keepTokens += t;
|
|
760
|
-
keepCount++;
|
|
761
|
-
if (keepTokens >= KEEP_RECENT_TOKENS || keepCount >= MIN_KEEP_MESSAGES) {
|
|
762
|
-
break;
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
keepStart = backUpPastToolUse(messages, keepStart);
|
|
766
|
-
return keepStart;
|
|
767
|
-
}
|
|
768
|
-
function backUpPastToolUse(messages, keepStart) {
|
|
769
|
-
if (keepStart <= 0 || keepStart >= messages.length) {
|
|
770
|
-
return keepStart;
|
|
771
|
-
}
|
|
772
|
-
if (!hasToolResult(messages[keepStart])) {
|
|
773
|
-
return keepStart;
|
|
774
|
-
}
|
|
775
|
-
const ids = new Set((messages[keepStart].toolResults ?? []).map((tr) => tr.toolUseId));
|
|
776
|
-
for (let i = keepStart - 1; i >= 0; i--) {
|
|
777
|
-
const m = messages[i];
|
|
778
|
-
if (m.role === "assistant" && m.toolUses?.some((tu) => ids.has(tu.toolUseId))) {
|
|
779
|
-
return i;
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
return keepStart;
|
|
783
|
-
}
|
|
784
|
-
function currentContextTokens(conv, anchor) {
|
|
785
|
-
const a = anchor ?? conv.usageAnchorState();
|
|
786
|
-
if (!a) {
|
|
787
|
-
return estimateTokens(conv);
|
|
788
|
-
}
|
|
789
|
-
const messages = conv.getMessages();
|
|
790
|
-
const start = Math.min(a.anchorCount, messages.length);
|
|
791
|
-
return a.baselineTokens + estimateMessages(messages.slice(start));
|
|
792
|
-
}
|
|
793
|
-
async function manageContext(conv, client, contextWindow, maxOutput, trackingState, recoveryState, toolSchemaNames, toolSchemas, sessionFilePath = "") {
|
|
794
|
-
const tokens = currentContextTokens(conv);
|
|
795
|
-
const autoThreshold = computeCompactThreshold(contextWindow, maxOutput);
|
|
796
|
-
const hardBlock = computeCompactThreshold(contextWindow, maxOutput, true);
|
|
797
|
-
if (tokens < autoThreshold) {
|
|
798
|
-
return { compacted: false, message: "" };
|
|
799
|
-
}
|
|
800
|
-
const forced = tokens >= hardBlock;
|
|
801
|
-
if (!forced && trackingState.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
|
|
802
|
-
return {
|
|
803
|
-
compacted: false,
|
|
804
|
-
message: `Auto-compact circuit breaker: ${String(MAX_CONSECUTIVE_FAILURES)} consecutive failures`
|
|
805
|
-
};
|
|
806
|
-
}
|
|
807
|
-
try {
|
|
808
|
-
const result = await doCompact(
|
|
809
|
-
conv,
|
|
810
|
-
client,
|
|
811
|
-
recoveryState,
|
|
812
|
-
toolSchemaNames,
|
|
813
|
-
toolSchemas,
|
|
814
|
-
sessionFilePath
|
|
815
|
-
);
|
|
816
|
-
trackingState.consecutiveFailures = 0;
|
|
817
|
-
return result;
|
|
818
|
-
} catch (err) {
|
|
819
|
-
trackingState.consecutiveFailures++;
|
|
820
|
-
return {
|
|
821
|
-
compacted: false,
|
|
822
|
-
message: `Auto-compact failed: ${asErrorString(err)}`
|
|
823
|
-
};
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
async function forceCompact(conv, client, recoveryState, toolSchemaNames, toolSchemas, sessionFilePath = "") {
|
|
827
|
-
return doCompact(conv, client, recoveryState, toolSchemaNames, toolSchemas, sessionFilePath);
|
|
828
|
-
}
|
|
829
|
-
var SUMMARY_SYSTEM_PROMPT = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
|
|
830
|
-
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
|
|
831
|
-
|
|
832
|
-
Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
|
|
833
|
-
|
|
834
|
-
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
|
835
|
-
- The user's explicit requests and intents
|
|
836
|
-
- Your approach to addressing the user's requests
|
|
837
|
-
- Key decisions, technical concepts and code patterns
|
|
838
|
-
- Specific details like:
|
|
839
|
-
- file names
|
|
840
|
-
- full code snippets
|
|
841
|
-
- function signatures
|
|
842
|
-
- file edits
|
|
843
|
-
- Errors that you ran into and how you fixed them
|
|
844
|
-
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
845
|
-
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
|
|
846
|
-
|
|
847
|
-
After your analysis, output your final summary wrapped in <summary> tags. Your summary should include the following sections:
|
|
848
|
-
|
|
849
|
-
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
|
850
|
-
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
|
851
|
-
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
|
|
852
|
-
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
853
|
-
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
854
|
-
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
|
|
855
|
-
7. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
|
856
|
-
8. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
|
|
857
|
-
9. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
|
|
858
|
-
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
|
|
859
|
-
|
|
860
|
-
Here's an example of how your output should be structured:
|
|
861
|
-
|
|
862
|
-
<example>
|
|
863
|
-
<analysis>
|
|
864
|
-
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
865
|
-
</analysis>
|
|
866
|
-
|
|
867
|
-
<summary>
|
|
868
|
-
1. Primary Request and Intent:
|
|
869
|
-
[Detailed description]
|
|
870
|
-
|
|
871
|
-
2. Key Technical Concepts:
|
|
872
|
-
- [Concept 1]
|
|
873
|
-
- [Concept 2]
|
|
874
|
-
- [...]
|
|
875
|
-
|
|
876
|
-
3. Files and Code Sections:
|
|
877
|
-
- [File Name 1]
|
|
878
|
-
- [Summary of why this file is important]
|
|
879
|
-
- [Summary of the changes made to this file, if any]
|
|
880
|
-
- [Important Code Snippet]
|
|
881
|
-
- [File Name 2]
|
|
882
|
-
- [Important Code Snippet]
|
|
883
|
-
- [...]
|
|
884
|
-
|
|
885
|
-
4. Errors and fixes:
|
|
886
|
-
- [Detailed description of error 1]:
|
|
887
|
-
- [How you fixed the error]
|
|
888
|
-
- [User feedback on the error if any]
|
|
889
|
-
- [...]
|
|
890
|
-
|
|
891
|
-
5. Problem Solving:
|
|
892
|
-
[Description of solved problems and ongoing troubleshooting]
|
|
893
|
-
|
|
894
|
-
6. All user messages:
|
|
895
|
-
- [Detailed non tool use user message]
|
|
896
|
-
- [...]
|
|
897
|
-
|
|
898
|
-
7. Pending Tasks:
|
|
899
|
-
- [Task 1]
|
|
900
|
-
- [Task 2]
|
|
901
|
-
- [...]
|
|
902
|
-
|
|
903
|
-
8. Current Work:
|
|
904
|
-
[Precise description of current work]
|
|
905
|
-
|
|
906
|
-
9. Optional Next Step:
|
|
907
|
-
[Optional Next step to take]
|
|
908
|
-
|
|
909
|
-
</summary>
|
|
910
|
-
</example>
|
|
911
|
-
|
|
912
|
-
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.`;
|
|
913
|
-
function buildSummaryPrompt(conversationText) {
|
|
914
|
-
return SUMMARY_SYSTEM_PROMPT + "\n\n" + conversationText;
|
|
915
|
-
}
|
|
916
|
-
function groupMessagesByAPIRound(messages) {
|
|
917
|
-
const groups = [];
|
|
918
|
-
let current = [];
|
|
919
|
-
let prevHadToolResult = false;
|
|
920
|
-
for (const m of messages) {
|
|
921
|
-
if (m.role === "assistant" && prevHadToolResult && current.length > 0) {
|
|
922
|
-
groups.push(current);
|
|
923
|
-
current = [];
|
|
924
|
-
}
|
|
925
|
-
current.push(m);
|
|
926
|
-
prevHadToolResult = !!(m.toolResults && m.toolResults.length > 0);
|
|
927
|
-
}
|
|
928
|
-
if (current.length > 0) {
|
|
929
|
-
groups.push(current);
|
|
930
|
-
}
|
|
931
|
-
return groups;
|
|
932
|
-
}
|
|
933
|
-
function truncateHeadForPTL(prefix, tokenGap) {
|
|
934
|
-
const groups = groupMessagesByAPIRound(prefix);
|
|
935
|
-
if (groups.length < 2) {
|
|
936
|
-
return null;
|
|
937
|
-
}
|
|
938
|
-
let dropCount;
|
|
939
|
-
if (tokenGap > 0) {
|
|
940
|
-
let acc = 0;
|
|
941
|
-
dropCount = 0;
|
|
942
|
-
for (const g of groups) {
|
|
943
|
-
acc += g.reduce((sum, m) => sum + estimateOne(m), 0);
|
|
944
|
-
dropCount++;
|
|
945
|
-
if (acc >= tokenGap) {
|
|
946
|
-
break;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
} else {
|
|
950
|
-
dropCount = Math.max(1, Math.floor(groups.length / 5));
|
|
951
|
-
}
|
|
952
|
-
dropCount = Math.min(dropCount, groups.length - 1);
|
|
953
|
-
if (dropCount < 1) {
|
|
954
|
-
return null;
|
|
955
|
-
}
|
|
956
|
-
const result = groups.slice(dropCount).flat();
|
|
957
|
-
if (result.length > 0 && result[0].role !== "user") {
|
|
958
|
-
result.unshift({ role: "user", content: PTL_RETRY_MARKER });
|
|
959
|
-
}
|
|
960
|
-
return result;
|
|
961
|
-
}
|
|
962
|
-
function serializePrefixText(messages) {
|
|
963
|
-
return messages.map((m) => {
|
|
964
|
-
let text = `[${m.role}]: ${contentToText(m.content)}`;
|
|
965
|
-
if (m.toolUses) {
|
|
966
|
-
text += `
|
|
967
|
-
[tools: ${m.toolUses.map((t) => t.toolName).join(", ")}]`;
|
|
968
|
-
}
|
|
969
|
-
if (m.toolResults) {
|
|
970
|
-
text += `
|
|
971
|
-
[tool results]
|
|
972
|
-
${m.toolResults.map((result) => result.content).join("\n")}`;
|
|
973
|
-
}
|
|
974
|
-
return text;
|
|
975
|
-
}).join("\n\n");
|
|
976
|
-
}
|
|
977
|
-
function formatCompactSummary(raw) {
|
|
978
|
-
const summaryMatch = /<summary>([\s\S]*?)<\/summary>/.exec(raw);
|
|
979
|
-
if (summaryMatch) {
|
|
980
|
-
return summaryMatch[1].trim();
|
|
981
|
-
}
|
|
982
|
-
const analysisMatch = /<analysis>[\s\S]*?<\/analysis>/.exec(raw);
|
|
983
|
-
if (analysisMatch) {
|
|
984
|
-
return raw.replace(analysisMatch[0], "").trim();
|
|
985
|
-
}
|
|
986
|
-
return raw.trim();
|
|
987
|
-
}
|
|
988
|
-
async function callSummaryWithCacheSharing(client, messages, toolSchemas) {
|
|
989
|
-
let lastAssistant = -1;
|
|
990
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
991
|
-
if (messages[i].role === "assistant") {
|
|
992
|
-
lastAssistant = i;
|
|
993
|
-
break;
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
if (lastAssistant < 0) {
|
|
997
|
-
throw new Error("no assistant message found for cache-sharing compact");
|
|
998
|
-
}
|
|
999
|
-
const summaryConv = new ConversationManager();
|
|
1000
|
-
summaryConv.appendMessages(messages.slice(0, lastAssistant + 1));
|
|
1001
|
-
summaryConv.addUserMessage(SUMMARY_SYSTEM_PROMPT);
|
|
1002
|
-
let summaryText = "";
|
|
1003
|
-
const stream = client.stream(summaryConv, toolSchemas);
|
|
1004
|
-
for await (const event of stream) {
|
|
1005
|
-
if (event.type === "text_delta") {
|
|
1006
|
-
summaryText += event.text;
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
return formatCompactSummary(summaryText);
|
|
1010
|
-
}
|
|
1011
|
-
async function requestSummaryWithPTLRetry(client, prefix, toolSchemas) {
|
|
1012
|
-
let currentPrefix = prefix;
|
|
1013
|
-
for (let attempt = 0; ; attempt++) {
|
|
1014
|
-
const text = serializePrefixText(currentPrefix);
|
|
1015
|
-
const summaryConv = new ConversationManager();
|
|
1016
|
-
summaryConv.addUserMessage(buildSummaryPrompt(text));
|
|
1017
|
-
try {
|
|
1018
|
-
let summaryText = "";
|
|
1019
|
-
const stream = client.stream(summaryConv, toolSchemas);
|
|
1020
|
-
for await (const event of stream) {
|
|
1021
|
-
if (event.type === "text_delta") {
|
|
1022
|
-
summaryText += event.text;
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
return formatCompactSummary(summaryText);
|
|
1026
|
-
} catch (e) {
|
|
1027
|
-
const msg = e instanceof Error ? e.message.toLowerCase() : "";
|
|
1028
|
-
const isPTL = msg.includes("prompt") && msg.includes("long") || msg.includes("too many") || msg.includes("context_length");
|
|
1029
|
-
if (!isPTL || attempt >= MAX_PTL_RETRIES) {
|
|
1030
|
-
throw e;
|
|
1031
|
-
}
|
|
1032
|
-
const tokenGap = currentPrefix.reduce((sum, m) => sum + estimateOne(m), 0) / 5;
|
|
1033
|
-
const truncated = truncateHeadForPTL(currentPrefix, tokenGap);
|
|
1034
|
-
if (!truncated) {
|
|
1035
|
-
throw e;
|
|
1036
|
-
}
|
|
1037
|
-
currentPrefix = truncated;
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
async function doCompact(conv, client, recoveryState, toolSchemaNames, toolSchemas, sessionFilePath = "") {
|
|
1042
|
-
const estimationMessages = conv.getMessages();
|
|
1043
|
-
const keepStart = computeKeepStartIndex(estimationMessages);
|
|
1044
|
-
if (keepStart <= 0 || keepStart < MIN_COMPACT_PREFIX) {
|
|
1045
|
-
return {
|
|
1046
|
-
compacted: false,
|
|
1047
|
-
message: `Compaction skipped: only ${String(keepStart)} message(s) to summarize, kept verbatim`
|
|
1048
|
-
};
|
|
1049
|
-
}
|
|
1050
|
-
const toSummarize = estimationMessages.slice(0, keepStart);
|
|
1051
|
-
const toKeep = estimationMessages.slice(keepStart);
|
|
1052
|
-
let summary;
|
|
1053
|
-
try {
|
|
1054
|
-
summary = await callSummaryWithCacheSharing(client, estimationMessages, toolSchemas);
|
|
1055
|
-
} catch (err) {
|
|
1056
|
-
if (!(err instanceof ContextTooLongError)) {
|
|
1057
|
-
throw err;
|
|
1058
|
-
}
|
|
1059
|
-
summary = await requestSummaryWithPTLRetry(client, toSummarize, toolSchemas);
|
|
1060
|
-
}
|
|
1061
|
-
const recoveryAttachment = recoveryState ? recoveryState.buildRecoveryAttachment(toolSchemaNames) : "";
|
|
1062
|
-
let summaryContent = "This session continues from a previous conversation, which has been compressed due to context limitations. Here is a summary of the earlier messages:\n\n" + summary;
|
|
1063
|
-
if (toKeep.length > 0) {
|
|
1064
|
-
summaryContent += "\n\nRecent messages have been preserved verbatim.";
|
|
1065
|
-
}
|
|
1066
|
-
if (sessionFilePath) {
|
|
1067
|
-
summaryContent += `
|
|
1068
|
-
|
|
1069
|
-
If you need specific details from before compaction (code snippets, error messages, etc.), use ReadFile to read the full session transcript: ${sessionFilePath}`;
|
|
1070
|
-
}
|
|
1071
|
-
if (recoveryAttachment) {
|
|
1072
|
-
summaryContent += `
|
|
1073
|
-
|
|
1074
|
-
---
|
|
1075
|
-
|
|
1076
|
-
${recoveryAttachment}`;
|
|
1077
|
-
}
|
|
1078
|
-
conv.replaceWithCompacted(summaryContent, toKeep);
|
|
1079
|
-
const keep = toKeep.filter(
|
|
1080
|
-
(m) => (m.role === "user" || m.role === "assistant") && (m.content || (m.toolUses?.length ?? 0) || (m.toolResults?.length ?? 0))
|
|
1081
|
-
).map((m) => ({
|
|
1082
|
-
role: m.role,
|
|
1083
|
-
content: m.content,
|
|
1084
|
-
...m.toolUses?.length ? { tool_uses: toolUsesToRecords(m.toolUses) } : {},
|
|
1085
|
-
...m.toolResults?.length ? {
|
|
1086
|
-
tool_results: toolResultsToRecords(m.toolResults)
|
|
1087
|
-
} : {}
|
|
1088
|
-
}));
|
|
1089
|
-
return {
|
|
1090
|
-
compacted: true,
|
|
1091
|
-
message: `Compacted ${String(toSummarize.length)} messages into summary (${String(summary.length)} chars), kept ${String(toKeep.length)} recent messages verbatim`,
|
|
1092
|
-
boundary: { summary, keep }
|
|
1093
|
-
};
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
export {
|
|
1097
|
-
ConversationManager,
|
|
1098
|
-
normalizeToolResultContentBlock,
|
|
1099
|
-
isToolResultContentBlock,
|
|
1100
|
-
SKIP_DIRS,
|
|
1101
|
-
COMPACT_BOUNDARY,
|
|
1102
|
-
toolUsesToRecords,
|
|
1103
|
-
toolResultsToRecords,
|
|
1104
|
-
getSessionFilePath,
|
|
1105
|
-
newSessionId,
|
|
1106
|
-
saveMessage,
|
|
1107
|
-
saveCompactBoundary,
|
|
1108
|
-
loadSession,
|
|
1109
|
-
rebuildFromSession,
|
|
1110
|
-
listSessions,
|
|
1111
|
-
cleanExpiredSessions,
|
|
1112
|
-
computeCompactThreshold,
|
|
1113
|
-
AutoCompactTrackingState,
|
|
1114
|
-
estimateMessages,
|
|
1115
|
-
estimateTokens,
|
|
1116
|
-
computeKeepStartIndex,
|
|
1117
|
-
currentContextTokens,
|
|
1118
|
-
manageContext,
|
|
1119
|
-
forceCompact
|
|
1120
|
-
};
|