;;; llm-common.scm - Shared utilities for LLM providers ;;; ;;; BSD-3-Clause License ;;; Copyright (c) 2025, Rolando Abarca (module llm-common (detect-mime-type image-mime-type? pdf-mime-type? vision-mime-type? read-file-base64 read-text-file kebab->snake) (import scheme chicken.base chicken.string chicken.pathname chicken.io srfi-13 ;; strings base64) ;; base64 encoding ;;; ================================================================ ;;; File Handling Helpers ;;; ================================================================ ;; Detect MIME type from file path extension (define (detect-mime-type path) (let* ((pn (make-pathname #f path)) (ext (pathname-extension pn))) (cond ((not ext) "application/octet-stream") ((member ext '("jpg" "jpeg")) "image/jpeg") ((equal? ext "png") "image/png") ((equal? ext "gif") "image/gif") ((equal? ext "webp") "image/webp") ((equal? ext "pdf") "application/pdf") ((member ext '("txt" "md" "scm" "lisp" "c" "h" "js" "py" "rb")) "text/plain") (else "application/octet-stream")))) ;; Check if MIME type is an image (define (image-mime-type? mime) (string-prefix? "image/" mime)) ;; Check if MIME type is PDF (define (pdf-mime-type? mime) (equal? mime "application/pdf")) ;; Check if MIME type is vision-compatible (images + PDFs) ;; Vision-capable models can read both via image_url content type (define (vision-mime-type? mime) (or (image-mime-type? mime) (pdf-mime-type? mime))) ;; Read file as binary and encode to base64 (define (read-file-base64 path) (call-with-input-file path (lambda (port) (let* ((contents (read-string #f port)) (encoded (base64-encode contents))) encoded)) #:binary)) ;; Read text file content (define (read-text-file path) (call-with-input-file path (lambda (port) (read-string #f port)))) ;;; ================================================================ ;;; String Utilities ;;; ================================================================ ;; Helper: Convert kebab-case symbol to snake_case symbol ;; 'get-current-time -> 'get_current_time (define (kebab->snake sym) (string->symbol (string-translate (symbol->string sym) #\- #\_))) ) ;; end module