Skip to content

Conversation

@johnlk
Copy link
Contributor

@johnlk johnlk commented Jun 22, 2025

Description

This change is a move to make the env vars and API calls agnostic to provider. For now, we're just adding support for Claude but more can be added in the future by extending the ai.sh definition.

Changes Made

  • introduced new AI_... env vars
  • defined discrete API call methods for openai and claude
  • updated readme

Additional Notes

Existing users will not be affected as this is backward compatible

@johnlk johnlk self-assigned this Jun 22, 2025
@github-actions
Copy link

Score: 88

Improvements:

  • Consolidate duplicated curl invocation logic: extract common headers and error-handling to a shared helper to avoid code drift between ai::prompt_openai and ai::prompt_claude.
  • Validate that required inputs (AI_PROVIDER, AI_API_KEY, etc.) are non-empty early in main() instead of relying on docpars or utils::verify_required_env_vars.
  • Normalize AI_PROVIDER casing (e.g. tr '[:upper:]' '[:lower:]') to prevent misconfiguration like OpenAI vs openai.
  • Externalize magic defaults (model names, API URLs, versions) as constants at the top of ai.sh or in a config file for easier updates.
  • Add configurable timeouts/retries for curl calls to guard against transient network failures.
  • Quote all variable expansions (e.g. "${git_diff}") to avoid word-splitting in diff content containing spaces.
  • Remove legacy parameters entirely once clients have migrated; deprecation messages could be in release notes rather than runtime logs.
  • Introduce unit tests or integration mocks for both providers to validate request payloads and parsing logic.
  • Handle JSON parsing errors (e.g. non-JSON response or missing .choices) to avoid silent failures or empty output.
  • Consider using a more structured data format (e.g. YAML) for GitHub Action inputs to improve readability and maintainability.
# Example refactor: single prompt helper with provider-specific hooks
ai::prompt_model() {
  local git_diff="$1"
  local payload headers url parser
  case "${AI_PROVIDER,,}" in
    openai)
      url="https://api.openai.com/v1/chat/completions"
      headers=( -H "Authorization: Bearer $AI_API_KEY" )
      payload=$(ai::build_openai_payload "$git_diff")
      parser="jq -r '.choices[0].message.content'"
      ;;
    claude)
      url="https://api.anthropic.com/v1/messages"
      headers=( -H "x-api-key: $AI_API_KEY" -H "anthropic-version: 2023-06-01" )
      payload=$(ai::build_claude_payload "$git_diff")
      parser="jq -r '.content[0].text'"
      ;;
    *)
      utils::log_error "Unsupported AI_PROVIDER: $AI_PROVIDER"
      return 1
      ;;
  esac

  local response
  response=$(curl --fail --connect-timeout 5 --max-time 30 -sSL "${headers[@]}" \
    -H "Content-Type: application/json" -d "$payload" "$url") \
    || { utils::log_error "Network error talking to $AI_PROVIDER"; return 1; }

  echo "$response" | jq -e . > /dev/null \
    || { utils::log_error "Invalid JSON from $AI_PROVIDER"; return 1; }

  echo "$response" | eval "$parser"
}

Update to on-push for testing

Update action for testing

Reset testing
@johnlk johnlk force-pushed the john/support-claude branch from ee4051c to 76da9bf Compare June 22, 2025 18:26
@johnlk johnlk merged commit 4bcdc37 into main Jun 22, 2025
2 checks passed
@johnlk johnlk deleted the john/support-claude branch June 22, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants