MCPcopy Create free account
hub / github.com/WJX20/claude-code / createPostCompactFileAttachments

Function createPostCompactFileAttachments

src/services/compact/compact.ts:1418–1467  ·  view source on GitHub ↗
(
  readFileState: Record<string, { content: string; timestamp: number }>,
  toolUseContext: ToolUseContext,
  maxFiles: number,
  preservedMessages: Message[] = [],
)

Source from the content-addressed store, hash-verified

1416 * @returns Array of attachment messages for the most recently accessed files that fit within token budget
1417 */
1418export async function createPostCompactFileAttachments(
1419 readFileState: Record<string, { content: string; timestamp: number }>,
1420 toolUseContext: ToolUseContext,
1421 maxFiles: number,
1422 preservedMessages: Message[] = [],
1423): Promise<AttachmentMessage[]> {
1424 const preservedReadPaths = collectReadToolFilePaths(preservedMessages)
1425 const recentFiles = Object.entries(readFileState)
1426 .map(([filename, state]) => ({ filename, ...state }))
1427 .filter(
1428 file =>
1429 !shouldExcludeFromPostCompactRestore(
1430 file.filename,
1431 toolUseContext.agentId,
1432 ) && !preservedReadPaths.has(expandPath(file.filename)),
1433 )
1434 .sort((a, b) => b.timestamp - a.timestamp)
1435 .slice(0, maxFiles)
1436
1437 const results = await Promise.all(
1438 recentFiles.map(async file => {
1439 const attachment = await generateFileAttachment(
1440 file.filename,
1441 {
1442 ...toolUseContext,
1443 fileReadingLimits: {
1444 maxTokens: POST_COMPACT_MAX_TOKENS_PER_FILE,
1445 },
1446 },
1447 'tengu_post_compact_file_restore_success',
1448 'tengu_post_compact_file_restore_error',
1449 'compact',
1450 )
1451 return attachment ? createAttachmentMessage(attachment) : null
1452 }),
1453 )
1454
1455 let usedTokens = 0
1456 return results.filter((result): result is AttachmentMessage => {
1457 if (result === null) {
1458 return false
1459 }
1460 const attachmentTokens = roughTokenCountEstimation(jsonStringify(result))
1461 if (usedTokens + attachmentTokens <= POST_COMPACT_TOKEN_BUDGET) {
1462 usedTokens += attachmentTokens
1463 return true
1464 }
1465 return false
1466 })
1467}
1468
1469/**
1470 * Creates a plan file attachment if a plan file exists for the current session.

Callers 2

compactConversationFunction · 0.85

Calls 9

collectReadToolFilePathsFunction · 0.85
expandPathFunction · 0.85
generateFileAttachmentFunction · 0.85
createAttachmentMessageFunction · 0.85
jsonStringifyFunction · 0.85
entriesMethod · 0.80
hasMethod · 0.45

Tested by

no test coverage detected