Repository Public
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : API Atlas8# File : examples/gemini/file-search/file_search_lifecycle.sh9# Module : examples10# Repository : https://git.spboucher.ai/doc-api11# Created : 2026-09-1912# Last Modified : 2026-09-1913#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940set -euo pipefail41cd "$(dirname "$0")/../../.."42source scripts/lib.sh43MODEL="${MODEL:-gemini-3.5-flash-lite}"44# File Search: create store, resumable upload, poll, query with fileSearch tool, delete (force).45# STATUS: LIVE_VERIFIED 2026-09-18 (same requests as tmp-live/gemini-tools/g*.json via the Python runner; script itself not re-executed).46STORE=$(gemini POST /v1beta/fileSearchStores -d '{"displayName":"atlas-sh"}' | jq -r .name)47log_request gemini POST /v1beta/fileSearchStores "${GEMINI_STATUS:-200}" 0 "file_search_lifecycle.sh create"48printf 'The secret fruit is kumquat.' > /tmp/atlas-probe.txt; N=$(wc -c < /tmp/atlas-probe.txt | tr -d ' ')49HDR=$(mktemp)50curl -sS -D "$HDR" -o /dev/null "https://generativelanguage.googleapis.com/upload/v1beta/$STORE:uploadToFileSearchStore" \51 -H "x-goog-api-key: $GEMINI_API_KEY" -H "X-Goog-Upload-Protocol: resumable" -H "X-Goog-Upload-Command: start" \52 -H "X-Goog-Upload-Header-Content-Length: $N" -H "X-Goog-Upload-Header-Content-Type: text/plain" -H "Content-Type: application/json" -d '{"displayName":"probe.txt"}'53UPLOAD_URL=$(grep -i '^x-goog-upload-url:' "$HDR" | cut -d' ' -f2 | tr -d '\r'); rm -f "$HDR"54log_request gemini POST "/upload/v1beta/fileSearchStores/{store}:uploadToFileSearchStore" 200 0 "file_search_lifecycle.sh upload start"55OP=$(curl -sS "$UPLOAD_URL" -H "x-goog-api-key: $GEMINI_API_KEY" -H "X-Goog-Upload-Offset: 0" -H "X-Goog-Upload-Command: upload, finalize" --data-binary @/tmp/atlas-probe.txt | jq -r .name)56log_request gemini POST "/upload/v1beta/fileSearchStores/{store}:uploadToFileSearchStore" 200 0 "file_search_lifecycle.sh upload finalize"57until gemini GET "/v1beta/$OP" | jq -e '.done == true' > /dev/null; do sleep 2; done58DOC=$(gemini GET "/v1beta/$OP" | jq -r .response.documentName); echo "document: $DOC"59gemini POST "/v1beta/models/$MODEL:generateContent" -d "$(jq -n --arg s "$STORE" '{contents:[{parts:[{text:"What is the secret fruit? 3 words."}]}],tools:[{fileSearch:{fileSearchStoreNames:[$s]}}],generationConfig:{maxOutputTokens:32}}')" | jq -c '.candidates[0].content.parts[]|.text // empty, .usageMetadata'60log_request gemini POST "/v1beta/models/{model}:generateContent" "${GEMINI_STATUS:-200}" 0.0001 "file_search_lifecycle.sh fileSearch query"61gemini DELETE "/v1beta/$DOC?force=true" > /dev/null; echo "delete doc: ${GEMINI_STATUS:-200}"62gemini DELETE "/v1beta/$STORE?force=true" > /dev/null; echo "delete store: ${GEMINI_STATUS:-200}"63