MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / SummarizeFile

Function SummarizeFile

pkg/aiusechat/google/google-summarize.go:166–283  ·  view source on GitHub ↗

SummarizeFile reads a file and generates a summary using Google's Generative AI. It supports images, PDFs, and text files based on the limits defined in wshcmd-ai.go. Returns the summary text, usage information, and any error encountered.

(ctx context.Context, filename string, opts SummarizeOpts)

Source from the content-addressed store, hash-verified

164// It supports images, PDFs, and text files based on the limits defined in wshcmd-ai.go.
165// Returns the summary text, usage information, and any error encountered.
166func SummarizeFile(ctx context.Context, filename string, opts SummarizeOpts) (string, *GoogleUsage, error) {
167 if opts.Mode == "" {
168 return "", nil, fmt.Errorf("mode is required")
169 }
170
171 // Read the file
172 data, err := os.ReadFile(filename)
173 if err != nil {
174 return "", nil, fmt.Errorf("reading file: %w", err)
175 }
176
177 // Detect MIME type
178 mimeType := detectMimeType(data)
179
180 isPDF := mimeType == "application/pdf"
181 isImage := strings.HasPrefix(mimeType, "image/")
182
183 if !isPDF && !isImage {
184 mimeType = "text/plain"
185 if utilfn.ContainsBinaryData(data) {
186 return "", nil, fmt.Errorf("file contains binary data and cannot be summarized")
187 }
188 }
189
190 // Validate file size
191 maxSize, sizeStr := getMaxFileSize(mimeType, opts.Mode)
192 if len(data) > maxSize {
193 return "", nil, fmt.Errorf("file exceeds maximum size of %s for %s files", sizeStr, mimeType)
194 }
195
196 // Create client
197 client, err := genai.NewClient(ctx, option.WithAPIKey(opts.APIKey))
198 if err != nil {
199 return "", nil, fmt.Errorf("creating Google AI client: %w", err)
200 }
201 defer client.Close()
202
203 // Create model
204 model := client.GenerativeModel(SummarizeModel)
205
206 // Select prompt based on mode
207 var prompt string
208 switch opts.Mode {
209 case ModeQuickSummary:
210 prompt = QuickSummaryPrompt
211 case ModeUseful:
212 prompt = UsefulSummaryPrompt
213 case ModePublicCode:
214 prompt = PublicCodeSummaryPrompt
215 case ModeHTMLContent:
216 prompt = HTMLContentPrompt
217 case ModeHTMLFull:
218 prompt = HTMLFullPrompt
219 default:
220 prompt = SummarizePrompt
221 }
222
223 // Prepare the content parts

Callers 4

mainFunction · 0.92

Calls 7

ContainsBinaryDataFunction · 0.92
ReadFileMethod · 0.80
WriteStringMethod · 0.80
detectMimeTypeFunction · 0.70
getMaxFileSizeFunction · 0.70
CloseMethod · 0.65
StringMethod · 0.45

Tested by 3