|
| 1 | +import logging |
| 2 | +import time |
| 3 | +from typing import NoReturn |
| 4 | + |
| 5 | +from google import genai |
| 6 | +from google.genai import errors, types |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +# Import into a File Search store is a long-running operation. Poll until it |
| 11 | +# reports done, bounded so a stuck operation surfaces as an error instead of |
| 12 | +# blocking the collection task indefinitely. |
| 13 | +IMPORT_POLL_INTERVAL_SECONDS = 5 |
| 14 | +IMPORT_POLL_TIMEOUT_SECONDS = 300 |
| 15 | + |
| 16 | + |
| 17 | +def _raise_genai_error( |
| 18 | + err: errors.APIError, |
| 19 | + *, |
| 20 | + log_prefix: str, |
| 21 | + resource: str, |
| 22 | +) -> NoReturn: |
| 23 | + """Translate a google-genai SDK error into a user-facing InterruptedError. |
| 24 | +
|
| 25 | + Mirrors the source-tagged, fault-based pattern in |
| 26 | + OpenAIVectorStoreCrud.update and GoogleAIProvider.execute: 4xx is the |
| 27 | + caller's fault (warning), 5xx is provider-side (error). |
| 28 | + """ |
| 29 | + if isinstance(err, errors.ClientError): |
| 30 | + code = err.code |
| 31 | + status = err.status or "" |
| 32 | + msg = err.message or str(err) |
| 33 | + if code == 429: |
| 34 | + error_message = ( |
| 35 | + f"[GEMINI] Rate limit / quota exceeded (code: 429 {status}): " |
| 36 | + f"{msg}. Wait at least 1 minute and retry; if the issue " |
| 37 | + f"persists, request a quota increase from Google or contact Kaapi." |
| 38 | + ) |
| 39 | + elif code == 403: |
| 40 | + error_message = ( |
| 41 | + f"[GEMINI] Authentication / permission denied (code: 403 " |
| 42 | + f"{status}): {msg}. Verify the Gemini API key is valid, not " |
| 43 | + f"expired, and has access to the File Search API for this project." |
| 44 | + ) |
| 45 | + elif code == 404: |
| 46 | + error_message = ( |
| 47 | + f"[GEMINI] Resource not found (code: 404 {status}): {msg}. " |
| 48 | + f"Verify the File Search store and file names exist and have " |
| 49 | + f"not expired." |
| 50 | + ) |
| 51 | + elif code == 400: |
| 52 | + error_message = ( |
| 53 | + f"[GEMINI] Bad request (code: 400 {status}): {msg}. Review the " |
| 54 | + f"File Search store configuration and file references — the " |
| 55 | + f"request shape may be invalid." |
| 56 | + ) |
| 57 | + else: |
| 58 | + error_message = ( |
| 59 | + f"[GEMINI] Client error (code: {code} {status}): {msg}. Review " |
| 60 | + f"the request configuration; if the issue persists, contact Kaapi." |
| 61 | + ) |
| 62 | + logger.warning( |
| 63 | + f"[{log_prefix}] {error_message} | resource={resource}", |
| 64 | + exc_info=True, |
| 65 | + ) |
| 66 | + else: |
| 67 | + status = getattr(err, "status", "") or "" |
| 68 | + code = getattr(err, "code", "unknown") |
| 69 | + msg = getattr(err, "message", None) or str(err) |
| 70 | + error_message = ( |
| 71 | + f"[GEMINI] Server error (code: {code} {status}): {msg}. This is " |
| 72 | + f"typically transient (Gemini overloaded or internal error) — retry " |
| 73 | + f"in a few seconds. If the issue persists, contact Kaapi." |
| 74 | + ) |
| 75 | + logger.error( |
| 76 | + f"[{log_prefix}] {error_message} | resource={resource}", |
| 77 | + exc_info=True, |
| 78 | + ) |
| 79 | + raise InterruptedError(error_message) |
| 80 | + |
| 81 | + |
| 82 | +class GeminiCrud: |
| 83 | + def __init__(self, client: genai.Client) -> None: |
| 84 | + if client is None: |
| 85 | + logger.error("[GeminiCrud] Gemini client is not configured") |
| 86 | + raise ValueError("Gemini client is not configured") |
| 87 | + |
| 88 | + self.client = client |
| 89 | + |
| 90 | + |
| 91 | +class GeminiFileSearchStoreCrud(GeminiCrud): |
| 92 | + """CRUD for Google AI Studio (Gemini) File Search stores.""" |
| 93 | + |
| 94 | + def create(self) -> str: |
| 95 | + logger.info("[GeminiFileSearchStoreCrud.create] Creating file search store") |
| 96 | + try: |
| 97 | + store = self.client.file_search_stores.create( |
| 98 | + config=types.CreateFileSearchStoreConfig() |
| 99 | + ) |
| 100 | + except (errors.ClientError, errors.ServerError) as err: |
| 101 | + _raise_genai_error( |
| 102 | + err, |
| 103 | + log_prefix="GeminiFileSearchStoreCrud.create", |
| 104 | + resource="<new store>", |
| 105 | + ) |
| 106 | + |
| 107 | + logger.info( |
| 108 | + f"[GeminiFileSearchStoreCrud.create] File search store created | " |
| 109 | + f"store_name={store.name}" |
| 110 | + ) |
| 111 | + return store.name |
| 112 | + |
| 113 | + def import_document(self, store_name: str, file_name: str) -> None: |
| 114 | + logger.info( |
| 115 | + f"[GeminiFileSearchStoreCrud.import_document] Importing file into store | " |
| 116 | + f"store_name={store_name}, file_name={file_name}" |
| 117 | + ) |
| 118 | + try: |
| 119 | + operation = self.client.file_search_stores.import_file( |
| 120 | + file_search_store_name=store_name, |
| 121 | + file_name=file_name, |
| 122 | + ) |
| 123 | + except (errors.ClientError, errors.ServerError) as err: |
| 124 | + _raise_genai_error( |
| 125 | + err, |
| 126 | + log_prefix="GeminiFileSearchStoreCrud.import_document", |
| 127 | + resource=store_name, |
| 128 | + ) |
| 129 | + |
| 130 | + deadline = time.monotonic() + IMPORT_POLL_TIMEOUT_SECONDS |
| 131 | + while not operation.done: |
| 132 | + if time.monotonic() > deadline: |
| 133 | + error_message = ( |
| 134 | + f"[KAAPI] Timed out (code: import-timeout) after " |
| 135 | + f"{IMPORT_POLL_TIMEOUT_SECONDS}s waiting for Gemini to import " |
| 136 | + f"'{file_name}' into store '{store_name}'. Retry the import, " |
| 137 | + f"or use fewer / smaller files per collection." |
| 138 | + ) |
| 139 | + logger.error( |
| 140 | + f"[GeminiFileSearchStoreCrud.import_document] {error_message}" |
| 141 | + ) |
| 142 | + raise InterruptedError(error_message) |
| 143 | + |
| 144 | + time.sleep(IMPORT_POLL_INTERVAL_SECONDS) |
| 145 | + try: |
| 146 | + operation = self.client.operations.get(operation) |
| 147 | + except (errors.ClientError, errors.ServerError) as err: |
| 148 | + _raise_genai_error( |
| 149 | + err, |
| 150 | + log_prefix="GeminiFileSearchStoreCrud.import_document", |
| 151 | + resource=store_name, |
| 152 | + ) |
| 153 | + |
| 154 | + if operation.error: |
| 155 | + error_message = ( |
| 156 | + f"[GEMINI] Import operation failed for '{file_name}' into store " |
| 157 | + f"'{store_name}': {operation.error}. Verify the file is a " |
| 158 | + f"supported format and has not expired, then retry." |
| 159 | + ) |
| 160 | + logger.error(f"[GeminiFileSearchStoreCrud.import_document] {error_message}") |
| 161 | + raise InterruptedError(error_message) |
| 162 | + |
| 163 | + logger.info( |
| 164 | + f"[GeminiFileSearchStoreCrud.import_document] File imported | " |
| 165 | + f"store_name={store_name}, file_name={file_name}" |
| 166 | + ) |
| 167 | + |
| 168 | + def delete(self, store_name: str) -> None: |
| 169 | + logger.info( |
| 170 | + f"[GeminiFileSearchStoreCrud.delete] Deleting file search store | " |
| 171 | + f"store_name={store_name}" |
| 172 | + ) |
| 173 | + try: |
| 174 | + self.client.file_search_stores.delete( |
| 175 | + name=store_name, |
| 176 | + config=types.DeleteFileSearchStoreConfig(force=True), |
| 177 | + ) |
| 178 | + except (errors.ClientError, errors.ServerError) as err: |
| 179 | + _raise_genai_error( |
| 180 | + err, |
| 181 | + log_prefix="GeminiFileSearchStoreCrud.delete", |
| 182 | + resource=store_name, |
| 183 | + ) |
| 184 | + |
| 185 | + logger.info( |
| 186 | + f"[GeminiFileSearchStoreCrud.delete] File search store deleted | " |
| 187 | + f"store_name={store_name}" |
| 188 | + ) |
0 commit comments