MCPcopy Index your code
hub / github.com/Noumena-Network/code / generateFileAttachment

Function generateFileAttachment

src/utils/attachments.ts:3027–3206  ·  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

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

Callers 2

processAtMentionedFilesFunction · 0.85

Calls 12

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

Tested by

no test coverage detected