From 73fa17fa70722f62530971f15f1cd81a820bb68a Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Fri, 4 Dec 2026 03:53:47 -0800 Subject: [PATCH] feat(cli): add Office and OpenDocument MIME type detection and hints - Pin Office (.doc, .docx, .dotx, .ppt, .pptx, .xls, .xlsx) and OpenDocument (.odt, .odp, .ods) extensions in mimeByExtension so detection does not depend on the host MIME database. - Update analyze help text, contentClassOf comments, and local/remote MIME rejection hints to name Office and OpenDocument formats. - Add unit tests for Office/OpenDocument MIME detection, classification, and remote MIME hints. PiperOrigin-RevId: 1036198912 --- internal/cli/custom/analyze.go | 2 +- internal/cli/custom/custom_test.go | 26 ++++++++++++++++++++++++++ internal/cli/custom/media.go | 21 +++++++++++++++++---- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/internal/cli/custom/analyze.go b/internal/cli/custom/analyze.go index 9f90f249..a9612640 100644 --- a/internal/cli/custom/analyze.go +++ b/internal/cli/custom/analyze.go @@ -35,7 +35,7 @@ const defaultAnalyzeQuestion = "Describe this file in detail." func attachAnalyze(cmd *cobra.Command) { cmd.Use = "analyze [question]" cmd.Short = "Ask questions about images, audio, video, documents, text files, or YouTube URLs (" + defaultTextModel + ")" - cmd.Long = "Ask a question about one or more images, audio files, videos, PDFs, CSV or text\nfiles, or YouTube URLs.\n\nPass each media source separately with --input. files/ references use the\nFiles API, and YouTube URLs are passed by URI. The optional question applies to\nall inputs; its default is \"" + defaultAnalyzeQuestion + "\"\nBy default, stdout prints only the model's answer. The request is not stored\nserver-side.\n\n" + inlineLimitNote + cmd.Long = "Ask a question about one or more images, audio files, videos, documents or text\nfiles, or YouTube URLs.\n\nPass each media source separately with --input. files/ references use the\nFiles API, and YouTube URLs are passed by URI. The optional question applies to\nall inputs; its default is \"" + defaultAnalyzeQuestion + "\"\nBy default, stdout prints only the model's answer. The request is not stored\nserver-side.\n\n" + inlineLimitNote cmd.Example = " gemini-api analyze -i report.pdf \"Summarize the key findings\"\n" + " gemini-api analyze -i photo.jpg\n" + " gemini-api analyze -i files/abc123 \"List every speaker with timestamps\"\n" + diff --git a/internal/cli/custom/custom_test.go b/internal/cli/custom/custom_test.go index afa3e842..32f695b5 100644 --- a/internal/cli/custom/custom_test.go +++ b/internal/cli/custom/custom_test.go @@ -358,6 +358,17 @@ func TestDetectMIME(t *testing.T) { {"clip.mp3", "Audio/WAV", "audio/wav"}, {"scan.TIFF", "", "image/tiff"}, {"clip.mkv", "", "video/x-matroska"}, + // Office and OpenDocument types resolve from the built-in table, not the host's mime database. + {"memo.doc", "", "application/msword"}, + {"memo.DOCX", "", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {"memo.dotx", "", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, + {"memo.odt", "", "application/vnd.oasis.opendocument.text"}, + {"deck.ppt", "", "application/vnd.ms-powerpoint"}, + {"deck.pptx", "", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + {"deck.odp", "", "application/vnd.oasis.opendocument.presentation"}, + {"sheet.xls", "", "application/vnd.ms-excel"}, + {"sheet.xlsx", "", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {"sheet.ods", "", "application/vnd.oasis.opendocument.spreadsheet"}, {"noext", "", ""}, } for _, tt := range tests { @@ -383,6 +394,8 @@ func TestContentClassOf(t *testing.T) { {"application/pdf", contentDocument, true}, // CSV is a document by enum even though it is textual. {"text/csv", contentDocument, true}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", contentDocument, true}, + {"application/vnd.oasis.opendocument.spreadsheet", contentDocument, true}, {"text/plain", contentText, true}, {"text/x-go", contentText, true}, {"application/json", contentText, true}, @@ -3238,3 +3251,16 @@ func TestWrapVersionOutputSubstitutesReleaseVersion(t *testing.T) { }) } } + +// TestRemoteMIMEHintListsOfficeTypes pins that the hint for an uploaded file +// of an unsupported type names every document family the API accepts. +func TestRemoteMIMEHintListsOfficeTypes(t *testing.T) { + err := checkRemoteMIME("text/plain", "input", analyzePolicy) + pe, ok := err.(porcelainError) + if !ok { + t.Fatalf("checkRemoteMIME = %v, want a usage error", err) + } + if hints := strings.Join(pe.hints, "\n"); !strings.Contains(hints, "Office and OpenDocument") { + t.Errorf("hints = %q, want Office and OpenDocument named", hints) + } +} diff --git a/internal/cli/custom/media.go b/internal/cli/custom/media.go index c699edd7..7fcabfce 100644 --- a/internal/cli/custom/media.go +++ b/internal/cli/custom/media.go @@ -85,7 +85,19 @@ var mimeByExtension = map[string]string{ ".mpeg": "video/mpeg", ".mpg": "video/mpeg", ".flv": "video/x-flv", ".wmv": "video/wmv", ".3gp": "video/3gpp", ".pdf": "application/pdf", ".csv": "text/csv", - ".txt": "text/plain", ".md": "text/markdown", + // Office and OpenDocument files, pinned here so detection does not depend + // on the host's mime database (absent in minimal containers). + ".doc": "application/msword", + ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".dotx": "application/vnd.openxmlformats-officedocument.wordprocessingml.template", + ".odt": "application/vnd.oasis.opendocument.text", + ".ppt": "application/vnd.ms-powerpoint", + ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".odp": "application/vnd.oasis.opendocument.presentation", + ".xls": "application/vnd.ms-excel", + ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".ods": "application/vnd.oasis.opendocument.spreadsheet", + ".txt": "text/plain", ".md": "text/markdown", ".html": "text/html", ".htm": "text/html", ".xml": "text/xml", ".json": "application/json", ".js": "text/javascript", ".ts": "text/x-typescript", ".py": "text/x-python", ".go": "text/x-go", ".css": "text/css", ".yaml": "text/yaml", ".yml": "text/yaml", @@ -142,7 +154,8 @@ var textApplicationMIME = map[string]bool{ // contentClassOf picks the content block for a MIME type and reports whether // the API accepts that exact type there. The image, audio, video, and document -// blocks each publish a closed mime_type enum (documents are only PDF and CSV); +// blocks each publish a closed mime_type enum (documents are PDF, CSV, and the +// Office and OpenDocument formats); // every other textual type is sent as a text block, which carries no MIME type. // Anything else falls to the document block with sendable=false. func contentClassOf(mimeType string) (class contentClass, sendable bool) { @@ -423,7 +436,7 @@ func checkLocalFile(path, mimeOverride, ref string, policy mediaPolicy) (int64, class, sendable := contentClassOf(mimeType) if !sendable { return 0, "", usageError(fmt.Sprintf("%s: MIME type %s is not accepted by the Interactions API", ref, mimeType), - "Supported inputs: images, audio, video, PDF, CSV, and text files; convert the file, or pass --mime-type if the detection is wrong") + "Supported inputs: images, audio, video, PDF, CSV, Office and OpenDocument files, and text files; convert the file, or pass --mime-type if the detection is wrong") } if err := policy.checkClass(mimeType, ref); err != nil { return 0, "", err @@ -625,7 +638,7 @@ func checkRemoteMIME(mimeType, ref string, policy mediaPolicy) error { } if class, sendable := contentClassOf(mimeType); !sendable || class == contentText { return usageError(fmt.Sprintf("%s: MIME type %s cannot be referenced by URI in the Interactions API", ref, mimeType), - "Supported uploaded inputs: images, audio, video, PDF, and CSV; pass text files as local paths, or --mime-type if the stored type is wrong") + "Supported uploaded inputs: images, audio, video, PDF, CSV, and Office and OpenDocument files; pass text files as local paths, or --mime-type if the stored type is wrong") } return policy.checkClass(mimeType, ref) }