Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions scripts/generate_types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,101 @@
# Treat unset variables as an error.
set -euo pipefail

REMOTE_URL="https://raw.githubusercontent.com/a2aproject/A2A/refs/heads/main/specification/json/a2a.json"
# A2A specification version to use
# Can be overridden via environment variable: A2A_SPEC_VERSION=v1.2.0 ./generate_types.sh
# Or via command-line flag: ./generate_types.sh --version v1.2.0 output.py
# Use a specific git tag, branch name, or commit SHA
# Examples: "v1.0.0", "v1.2.0", "main", "abc123def"
A2A_SPEC_VERSION="${A2A_SPEC_VERSION:-v0.3.0}"

# Build URL based on version format
# Tags use /refs/tags/, branches use /refs/heads/, commits use direct ref
build_remote_url() {
local version="$1"
local base_url="https://raw.githubusercontent.com/a2aproject/A2A"

if [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]; then
# Looks like a version tag (v1.0.0, v1.2.3)
echo "${base_url}/refs/tags/${version}/specification/json/a2a.json"
elif [[ "$version" =~ ^[0-9a-f]{7,40}$ ]]; then
# Looks like a commit SHA (7+ hex chars)
echo "${base_url}/${version}/specification/json/a2a.json"
else
# Assume it's a branch name (main, develop, etc.)
echo "${base_url}/refs/heads/${version}/specification/json/a2a.json"
fi
}

REMOTE_URL=$(build_remote_url "$A2A_SPEC_VERSION")

GENERATED_FILE=""
INPUT_FILE=""

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--input-file)
INPUT_FILE="$2"
shift 2
;;
*)
GENERATED_FILE="$1"
shift 1
;;
--input-file)
INPUT_FILE="$2"
shift 2
;;
--version)
A2A_SPEC_VERSION="$2"
REMOTE_URL=$(build_remote_url "$A2A_SPEC_VERSION")
shift 2
;;
*)
GENERATED_FILE="$1"
shift 1
;;
esac
done

if [ -z "$GENERATED_FILE" ]; then
echo "Error: Output file path must be provided." >&2
echo "Usage: $0 [--input-file <path>] <output-file-path>"
echo "Usage: $0 [--input-file <path>] [--version <version>] <output-file-path>"
echo ""
echo "Options:"
echo " --input-file <path> Use a local JSON schema file instead of fetching from remote"
echo " --version <version> Specify A2A spec version (default: v0.3.0)"
echo " Can be a git tag (v1.0.0), branch (main), or commit SHA"
echo ""
echo "Environment variables:"
echo " A2A_SPEC_VERSION Override default spec version"
echo ""
echo "Examples:"
echo " $0 src/a2a/types.py"
echo " $0 --version v1.2.0 src/a2a/types.py"
echo " $0 --input-file local/a2a.json src/a2a/types.py"
echo " A2A_SPEC_VERSION=main $0 src/a2a/types.py"
exit 1
fi

echo "Running datamodel-codegen..."
declare -a source_args
if [ -n "$INPUT_FILE" ]; then
echo " - Source File: $INPUT_FILE"
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: Input file does not exist: $INPUT_FILE" >&2
exit 1
fi
source_args=("--input" "$INPUT_FILE")
else
echo " - A2A Spec Version: $A2A_SPEC_VERSION"
echo " - Source URL: $REMOTE_URL"

# Validate that the remote URL is accessible
echo " - Validating remote URL..."
if ! curl --fail --silent --head "$REMOTE_URL" >/dev/null 2>&1; then
echo "" >&2
echo "Error: Unable to access A2A specification at version '$A2A_SPEC_VERSION'" >&2
echo "URL: $REMOTE_URL" >&2
echo "" >&2
echo "The version may not exist. Available versions can be found at:" >&2
echo " https://github.com/a2aproject/A2A/tags" >&2
echo "" >&2
exit 1
fi

source_args=("--url" "$REMOTE_URL")
fi
echo " - Output File: $GENERATED_FILE"
Expand Down
Loading