Files
aitbc/cli/handlers/ai.py
aitbc e60cc3226c
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 9s
Blockchain Synchronization Verification / sync-verification (push) Failing after 1s
CLI Tests / test-cli (push) Failing after 3s
Documentation Validation / validate-docs (push) Successful in 6s
Documentation Validation / validate-policies-strict (push) Successful in 2s
Integration Tests / test-service-integration (push) Successful in 40s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 1s
P2P Network Verification / p2p-verification (push) Successful in 2s
Production Tests / Production Integration Tests (push) Successful in 21s
Python Tests / test-python (push) Successful in 13s
Security Scanning / security-scan (push) Failing after 46s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Successful in 17s
Smart Contract Tests / lint-solidity (push) Successful in 10s
Add sys import to test files and remove obsolete integration tests
- Add sys import to 29 test files across agent-coordinator, blockchain-event-bridge, blockchain-node, and coordinator-api
- Remove apps/blockchain-event-bridge/tests/test_integration.py (obsolete bridge integration tests)
- Remove apps/coordinator-api/tests/test_integration.py (obsolete API integration tests)
- Implement GPU registration in marketplace_gpu.py with GPURegistry model persistence
2026-04-23 16:43:17 +02:00

219 lines
7.3 KiB
Python

"""AI job submission and management handlers."""
import json
import sys
import requests
def handle_ai_submit(args, default_rpc_url, first, read_password, render_mapping):
"""Handle AI job submission."""
rpc_url = args.rpc_url or default_rpc_url
chain_id = getattr(args, "chain_id", None)
wallet = first(getattr(args, "wallet_name", None), getattr(args, "wallet", None))
model = first(getattr(args, "job_type_arg", None), getattr(args, "job_type", None))
prompt = first(getattr(args, "prompt_arg", None), getattr(args, "prompt", None))
payment = first(getattr(args, "payment_arg", None), getattr(args, "payment", None))
if not wallet or not model or not prompt:
print("Error: --wallet, --type, and --prompt are required")
sys.exit(1)
# Get auth headers
password = read_password(args)
from keystore_auth import get_auth_headers
headers = get_auth_headers(wallet, password, args.password_file)
job_data = {
"wallet": wallet,
"model": model,
"prompt": prompt,
}
if payment:
job_data["payment"] = payment
if chain_id:
job_data["chain_id"] = chain_id
print(f"Submitting AI job to {rpc_url}...")
try:
response = requests.post(f"{rpc_url}/rpc/ai/submit", json=job_data, headers=headers, timeout=30)
if response.status_code == 200:
result = response.json()
print("AI job submitted successfully")
render_mapping("Job:", result)
else:
print(f"Submission failed: {response.status_code}")
print(f"Error: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error submitting AI job: {e}")
sys.exit(1)
def handle_ai_jobs(args, default_rpc_url, output_format, render_mapping):
"""Handle AI jobs list query."""
rpc_url = args.rpc_url or default_rpc_url
chain_id = getattr(args, "chain_id", None)
print(f"Getting AI jobs from {rpc_url}...")
try:
params = {}
if chain_id:
params["chain_id"] = chain_id
if args.limit:
params["limit"] = args.limit
response = requests.get(f"{rpc_url}/rpc/ai/jobs", params=params, timeout=10)
if response.status_code == 200:
jobs = response.json()
if output_format(args) == "json":
print(json.dumps(jobs, indent=2))
else:
print("AI jobs:")
if isinstance(jobs, list):
for job in jobs:
print(f" Job ID: {job.get('job_id', 'N/A')}, Model: {job.get('model', 'N/A')}, Status: {job.get('status', 'N/A')}")
else:
render_mapping("Jobs:", jobs)
else:
print(f"Query failed: {response.status_code}")
print(f"Error: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error getting AI jobs: {e}")
sys.exit(1)
def handle_ai_job(args, default_rpc_url, output_format, render_mapping, first):
"""Handle AI job details query."""
rpc_url = args.rpc_url or default_rpc_url
chain_id = getattr(args, "chain_id", None)
job_id = first(getattr(args, "job_id_arg", None), getattr(args, "job_id", None))
if not job_id:
print("Error: --job-id is required")
sys.exit(1)
print(f"Getting AI job {job_id} from {rpc_url}...")
try:
params = {}
if chain_id:
params["chain_id"] = chain_id
response = requests.get(f"{rpc_url}/rpc/ai/job/{job_id}", params=params, timeout=10)
if response.status_code == 200:
job = response.json()
if output_format(args) == "json":
print(json.dumps(job, indent=2))
else:
render_mapping(f"Job {job_id}:", job)
else:
print(f"Query failed: {response.status_code}")
print(f"Error: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error getting AI job: {e}")
sys.exit(1)
def handle_ai_cancel(args, default_rpc_url, read_password, render_mapping, first):
"""Handle AI job cancellation."""
rpc_url = args.rpc_url or default_rpc_url
chain_id = getattr(args, "chain_id", None)
job_id = first(getattr(args, "job_id_arg", None), getattr(args, "job_id", None))
wallet = getattr(args, "wallet", None)
if not job_id or not wallet:
print("Error: --job-id and --wallet are required")
sys.exit(1)
# Get auth headers
password = read_password(args)
from keystore_auth import get_auth_headers
headers = get_auth_headers(wallet, password, args.password_file)
cancel_data = {
"job_id": job_id,
"wallet": wallet,
}
if chain_id:
cancel_data["chain_id"] = chain_id
print(f"Cancelling AI job {job_id} on {rpc_url}...")
try:
response = requests.post(f"{rpc_url}/rpc/ai/job/{job_id}/cancel", json=cancel_data, headers=headers, timeout=30)
if response.status_code == 200:
result = response.json()
print("AI job cancelled successfully")
render_mapping("Cancel result:", result)
else:
print(f"Cancellation failed: {response.status_code}")
print(f"Error: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error cancelling AI job: {e}")
sys.exit(1)
def handle_ai_stats(args, default_rpc_url, output_format, render_mapping):
"""Handle AI service statistics query."""
rpc_url = args.rpc_url or default_rpc_url
chain_id = getattr(args, "chain_id", None)
print(f"Getting AI service statistics from {rpc_url}...")
try:
params = {}
if chain_id:
params["chain_id"] = chain_id
response = requests.get(f"{rpc_url}/rpc/ai/stats", params=params, timeout=10)
if response.status_code == 200:
stats = response.json()
if output_format(args) == "json":
print(json.dumps(stats, indent=2))
else:
render_mapping("AI service statistics:", stats)
else:
print(f"Query failed: {response.status_code}")
print(f"Error: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error getting AI stats: {e}")
sys.exit(1)
def handle_ai_service_list(args, ai_operations, render_mapping):
"""Handle AI service list command."""
result = ai_operations("service_list")
if result:
render_mapping("AI Services:", result)
else:
sys.exit(1)
def handle_ai_service_status(args, ai_operations, render_mapping):
"""Handle AI service status command."""
kwargs = {}
if hasattr(args, "name") and args.name:
kwargs["name"] = args.name
result = ai_operations("service_status", **kwargs)
if result:
render_mapping("Service Status:", result)
else:
sys.exit(1)
def handle_ai_service_test(args, ai_operations, render_mapping):
"""Handle AI service test command."""
kwargs = {}
if hasattr(args, "name") and args.name:
kwargs["name"] = args.name
result = ai_operations("service_test", **kwargs)
if result:
render_mapping("Service Test:", result)
else:
sys.exit(1)