-
Notifications
You must be signed in to change notification settings - Fork 440
feat: add run.sh for human-present x402 scenario #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chopmob-cloud
wants to merge
4
commits into
google-agentic-commerce:main
Choose a base branch
from
chopmob-cloud:feat/x402-human-present-run-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−1
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2552994
feat: add run.sh for human-present x402 scenario
chopmob-cloud 6d07838
fix: set executable bit on run.sh
chopmob-cloud c46a19f
fix(x402-run): use rm -rf + mkdir for robust log directory cleanup
chopmob-cloud 852c1d3
Merge branch 'main' into feat/x402-human-present-run-script
chopmob-cloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
code/samples/python/scenarios/a2a/human-present/x402/run.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #!/bin/bash | ||
|
|
||
| # A script to automate the execution of the x402 (human-present) example. | ||
| # It starts all necessary servers and agents in the background. | ||
|
|
||
| # Exit immediately if any command exits with a non-zero status. | ||
| set -e | ||
|
|
||
| export PAYMENT_METHOD="x402" | ||
|
|
||
| # The directory containing the agents. | ||
| AGENTS_DIR="code/samples/python/src/roles" | ||
| # A directory to store logs. | ||
| LOG_DIR=".logs" | ||
|
|
||
| if [ ! -d "$AGENTS_DIR" ]; then | ||
| echo "Error: Directory '$AGENTS_DIR' not found." | ||
| echo "Please run this script from the root of the repository." | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ -f .env ]; then | ||
| set -a | ||
| source .env | ||
| set +a | ||
| fi | ||
|
|
||
| USE_VERTEXAI=$(printf "%s" "${GOOGLE_GENAI_USE_VERTEXAI}" | tr '[:upper:]' '[:lower:]') | ||
| if [ -z "${GOOGLE_API_KEY}" ] && [ "${USE_VERTEXAI}" != "true" ]; then | ||
| echo "Please set your GOOGLE_API_KEY environment variable before running." | ||
| echo "Alternatively, set GOOGLE_GENAI_USE_VERTEXAI=true to use Vertex AI with ADC." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Set up and activate a virtual environment. | ||
| echo "Setting up the Python virtual environment..." | ||
|
|
||
| if [ ! -d ".venv" ]; then | ||
| uv venv | ||
| fi | ||
|
|
||
| # Detect the correct activation script path based on the operating system | ||
| case "$OSTYPE" in | ||
| msys* | cygwin*) | ||
| # Windows (Git Bash, MSYS2, or Cygwin) | ||
| source .venv/Scripts/activate | ||
| ;; | ||
| *) | ||
| # Unix/Linux/macOS | ||
| source .venv/bin/activate | ||
| ;; | ||
| esac | ||
| echo "Virtual environment activated." | ||
|
|
||
| # Create a directory for log files. | ||
| mkdir -p "$LOG_DIR" | ||
|
|
||
| # This function is called automatically when the script exits (for any reason) | ||
| # to ensure all background processes are terminated. | ||
| cleanup() { | ||
| echo "" | ||
| echo "Shutting down background processes..." | ||
| if [ ${#pids[@]} -ne 0 ]; then | ||
| kill "${pids[@]}" 2>/dev/null | ||
| wait "${pids[@]}" 2>/dev/null | ||
| fi | ||
| echo "Cleanup complete." | ||
| } | ||
|
|
||
| # Trap the EXIT signal to call the cleanup function. | ||
| trap cleanup EXIT | ||
|
|
||
| # Explicitly sync to ensure the virtual environment is up to date. | ||
| echo "Syncing virtual environment with uv sync..." | ||
| if uv sync --package ap2-samples; then | ||
| echo "Virtual environment synced successfully." | ||
| else | ||
| echo "Error: uv sync failed. Aborting." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Clear old logs. | ||
| echo "Clearing the logs directory..." | ||
| if [ -d "$LOG_DIR" ]; then | ||
| rm -f "$LOG_DIR"/* | ||
| fi | ||
|
|
||
| # Shared temp directory for keys and state across all agents. | ||
| export TEMP_DB_DIR="${TEMP_DB_DIR:-$(pwd)/.temp-db}" | ||
| echo "Clearing temp-db directory..." | ||
| rm -rf "$TEMP_DB_DIR" | ||
| mkdir -p "$TEMP_DB_DIR" | ||
| export AGENT_PROVIDER_PUBLIC_KEY_PATH="$TEMP_DB_DIR/agent_provider_signing_key.pub" | ||
|
|
||
| # Start all the remote agents & servers. | ||
| pids=() | ||
|
|
||
| echo "" | ||
| echo "Starting remote servers and agents as background processes..." | ||
|
|
||
| # uv sync is explicitly run before starting any agents. | ||
| # Prevent servers starting in parallel from colliding by trying to sync again. | ||
| UV_RUN_CMD="uv run --no-sync" | ||
|
|
||
| if [ -f ".env" ]; then | ||
| UV_RUN_CMD="$UV_RUN_CMD --env-file .env" | ||
| fi | ||
|
|
||
| echo "-> Starting the Merchant Agent (port:8001 log:$LOG_DIR/merchant_agent.log)..." | ||
| $UV_RUN_CMD --package ap2-samples python -m roles.merchant_agent >"$LOG_DIR/merchant_agent.log" 2>&1 & | ||
| pids+=($!) | ||
|
|
||
| echo "-> Starting the Credentials Provider (port:8002 log:$LOG_DIR/credentials_provider_agent.log)..." | ||
| $UV_RUN_CMD --package ap2-samples python -m roles.credentials_provider_agent >"$LOG_DIR/credentials_provider_agent.log" 2>&1 & | ||
| pids+=($!) | ||
|
|
||
| echo "-> Starting the Merchant Payment Processor Agent (port:8003 log:$LOG_DIR/mpp_agent.log)..." | ||
| $UV_RUN_CMD --package ap2-samples python -m roles.merchant_payment_processor_agent >"$LOG_DIR/mpp_agent.log" 2>&1 & | ||
| pids+=($!) | ||
|
|
||
| echo "" | ||
| echo "All remote servers are starting." | ||
|
|
||
| echo "Starting the Shopping Agent..." | ||
| $UV_RUN_CMD --package ap2-samples adk web --host 0.0.0.0 $AGENTS_DIR | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
rm -f "$LOG_DIR"/*can be problematic if the directory is empty, as the glob pattern*might not expand (depending on shell settings), leading to an attempt to delete a literal file named*. A more robust way to clear the directory is to remove and recreate it, or usefindto delete its contents.