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

Function generateFileAttachment

src/utils/attachments.ts:3021–3200  ·  view source on GitHub ↗
(
  filename: string,
  toolUseContext: ToolUseContext,
  successEventName: string,
  errorEventName: string,
  mode: 'compact' | 'at-mention',
  options?: {
    offset?: number
    limit?: number
  },
)

Source from the content-addressed store, hash-verified

3019}
3020
3021export async function generateFileAttachment(
3022 filename: string,
3023 toolUseContext: ToolUseContext,
3024 successEventName: string,
3025 errorEventName: string,
3026 mode: 'compact' | 'at-mention',
3027 options?: {
3028 offset?: number
3029 limit?: number
3030 },
3031): Promise<
3032 | FileAttachment
3033 | CompactFileReferenceAttachment
3034 | PDFReferenceAttachment
3035 | AlreadyReadFileAttachment
3036 | null
3037> {
3038 const { offset, limit } = options ?? {}
3039
3040 // Check if file has a deny rule configured
3041 const appState = toolUseContext.getAppState()
3042 if (isFileReadDenied(filename, appState.toolPermissionContext)) {
3043 return null
3044 }
3045
3046 // Check file size before attempting to read (skip for PDFs — they have their own size/page handling below)
3047 if (
3048 mode === 'at-mention' &&
3049 !isFileWithinReadSizeLimit(
3050 filename,
3051 getDefaultFileReadingLimits().maxSizeBytes,
3052 )
3053 ) {
3054 const ext = parse(filename).ext.toLowerCase()
3055 if (!isPDFExtension(ext)) {
3056 try {
3057 const stats = await getFsImplementation().stat(filename)
3058 logEvent('tengu_attachment_file_too_large', {
3059 size_bytes: stats.size,
3060 mode,
3061 } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
3062 return null
3063 } catch {
3064 // If we can't stat the file, proceed with normal reading (will fail later if file doesn't exist)
3065 }
3066 }
3067 }
3068
3069 // For large PDFs on @ mention, return a lightweight reference instead of inlining
3070 if (mode === 'at-mention') {
3071 const pdfRef = await tryGetPDFReference(filename)
3072 if (pdfRef) {
3073 return pdfRef
3074 }
3075 }
3076
3077 // Check if file is already in context with latest version
3078 const existingFileState = toolUseContext.readFileState.get(filename)

Callers 2

processAtMentionedFilesFunction · 0.85

Calls 12

isFileReadDeniedFunction · 0.85
parseFunction · 0.85
isPDFExtensionFunction · 0.85
getFsImplementationFunction · 0.85
logEventFunction · 0.85
tryGetPDFReferenceFunction · 0.85
getCwdFunction · 0.85
countCharInStringFunction · 0.85
readTruncatedFileFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected