SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
4.8 KB · 106 lines shellscript
Raw Blame History
1#!/usr/bin/env bash2# Shared helpers for live API probing. Source this file: `source scripts/lib.sh`3# - Loads .env (never echoes keys)4# - oai / ant : curl wrappers (auth headers injected, never printed)5# - log_request : appends a sanitized record to reports/live-requests.jsonl6set -o pipefail78# Works under bash (BASH_SOURCE) and zsh (%x); falls back to PWD.9_lib_src="${BASH_SOURCE[0]:-}"10if [ -z "$_lib_src" ] && [ -n "${ZSH_VERSION:-}" ]; then _lib_src="${(%):-%x}"; fi11if [ -n "$_lib_src" ]; then ATLAS_ROOT="$(cd "$(dirname "$_lib_src")/.." && pwd)"; else ATLAS_ROOT="$PWD"; fi12export ATLAS_ROOT1314if [ -f "$ATLAS_ROOT/.env" ]; then15  set -a16  # shellcheck disable=SC109117  . "$ATLAS_ROOT/.env"18  set +a19fi2021: "${OPENAI_API_KEY:?OPENAI_API_KEY missing (see .env.example)}"22: "${ANTHROPIC_API_KEY:?ANTHROPIC_API_KEY missing (see .env.example)}"23# xAI / Gemini are optional (warn only)24[ -n "${XAI_API_KEY:-}" ] || echo "warning: XAI_API_KEY not set" >&225[ -n "${GEMINI_API_KEY:-}" ] || echo "warning: GEMINI_API_KEY not set" >&226export GEMINI_API_VERSION="${GEMINI_API_VERSION:-v1beta}"27export ANTHROPIC_VERSION="${ANTHROPIC_VERSION:-2023-06-01}"2829mkdir -p "$ATLAS_ROOT/tmp-live" "$ATLAS_ROOT/reports"30LIVE_LOG="$ATLAS_ROOT/reports/live-requests.jsonl"3132# Mask anything that looks like a key in arbitrary text (defense in depth for logs).33mask() { sed -E 's/sk-(ant-)?[A-Za-z0-9_-]{8,}/sk-***REDACTED***/g; s/xai-[A-Za-z0-9_-]{16,}/xai-***REDACTED***/g; s/AIza[A-Za-z0-9_-]{20,}/AIza***REDACTED***/g; s/AQ\.[A-Za-z0-9_-]{20,}/AQ.***REDACTED***/g; s/(Bearer|x-api-key:?|x-goog-api-key:?) *[A-Za-z0-9._-]{8,}/\1 ***REDACTED***/gI; s/([?&]key=)[A-Za-z0-9._-]{8,}/\1***REDACTED***/g'; }3435# log_request provider method path http_status est_cost_usd note36log_request() {37  local ts; ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"38  printf '{"ts":"%s","provider":"%s","method":"%s","path":"%s","status":%s,"est_cost_usd":%s,"note":"%s"}\n' \39    "$ts" "$1" "$2" "$3" "${4:-0}" "${5:-0}" "$(printf '%s' "${6:-}" | mask | sed 's/"/\\"/g')" >> "$LIVE_LOG"40}4142# oai METHOD PATH [curl args...]   e.g. oai GET /v1/models ; oai POST /v1/responses -d @body.json43# Writes body to stdout, sets OAI_STATUS.44oai() {45  local method="$1" _p="$2"; shift 246  local out; out="$(mktemp)"47  OAI_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://api.openai.com$_p" \48    -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" "$@")"49  cat "$out"; rm -f "$out"50  export OAI_STATUS51}5253# ant METHOD PATH [curl args...]   Adds x-api-key + anthropic-version. Pass -H 'anthropic-beta: ...' as needed.54ant() {55  local method="$1" _p="$2"; shift 256  local out; out="$(mktemp)"57  ANT_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://api.anthropic.com$_p" \58    -H "x-api-key: $ANTHROPIC_API_KEY" -H "anthropic-version: $ANTHROPIC_VERSION" -H "Content-Type: application/json" "$@")"59  cat "$out"; rm -f "$out"60  export ANT_STATUS61}6263# xai METHOD PATH [curl args...]   (OpenAI-compatible base https://api.x.ai)64xai() {65  local method="$1" _p="$2"; shift 266  local out; out="$(mktemp)"67  XAI_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://api.x.ai$_p" \68    -H "Authorization: Bearer $XAI_API_KEY" -H "Content-Type: application/json" "$@")"69  cat "$out"; rm -f "$out"70  export XAI_STATUS71}72# gemini METHOD PATH [curl args...]   PATH like /v1beta/models/gemini-2.5-flash:generateContent (key sent as header, never in URL)73gemini() {74  local method="$1" _p="$2"; shift 275  local out; out="$(mktemp)"76  GEMINI_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://generativelanguage.googleapis.com$_p" \77    -H "x-goog-api-key: $GEMINI_API_KEY" -H "Content-Type: application/json" "$@")"78  cat "$out"; rm -f "$out"79  export GEMINI_STATUS80}8182# Multipart variants (no forced Content-Type; pass -F fields). Note: OAI_STATUS/ANT_STATUS are set in the83# CURRENT shell only when the function is not run inside $(...) or a pipeline — redirect to a file instead:84#   oai_form POST /v1/files -F purpose=user_data -F file=@x.txt > out.json; echo "$OAI_STATUS"85oai_form() {86  local method="$1" _p="$2"; shift 287  local out; out="$(mktemp)"88  OAI_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://api.openai.com$_p" \89    -H "Authorization: Bearer $OPENAI_API_KEY" "$@")"90  cat "$out"; rm -f "$out"91  export OAI_STATUS92}93ant_form() {94  local method="$1" _p="$2"; shift 295  local out; out="$(mktemp)"96  ANT_STATUS="$(curl -sS -o "$out" -w '%{http_code}' -X "$method" "https://api.anthropic.com$_p" \97    -H "x-api-key: $ANTHROPIC_API_KEY" -H "anthropic-version: $ANTHROPIC_VERSION" "$@")"98  cat "$out"; rm -f "$out"99  export ANT_STATUS100}101102# Save a sanitized JSON/text payload to a file under sources/ or tmp-live/103save_sanitized() { # save_sanitized <dest-file>  (reads stdin)104  mkdir -p "$(dirname "$1")"; mask > "$1"105}106