-
Notifications
You must be signed in to change notification settings - Fork 344
feat: Micropayment channel extension for AP2 #83
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
base: main
Are you sure you want to change the base?
feat: Micropayment channel extension for AP2 #83
Conversation
This enhancement extends AP2 with a comprehensive micropayment channel framework that enables high-frequency, sub-cent transactions between agents through state channels, supporting Kite's vision of pay-per-use and streaming payment models. Key Features: - Payment channel infrastructure with state management - Streaming payment primitives for real-time micropayments - Multi-currency support including stablecoins (USDC, PYUSD) - Channel security framework with dispute resolution - Pay-per-token, pay-per-second, and pay-per-API-call models - A2A extension integration for agent capability advertising Components Added: - PaymentChannel with participant management and policies - StreamingPaymentSession for continuous payment flows - ChannelManager for lifecycle and security operations - CryptoPaymentAmount for blockchain-native currencies - AI inference service sample with micropayment integration - Extended A2A roles: micropayment-provider, streaming-payment-consumer This enables use cases like: - Pay-per-API-call for AI inference services - Streaming payments for real-time data feeds - Usage-based pricing for computational resources - Micro-subscriptions with automatic scaling - Cross-agent value transfer in multi-step workflows Transforms AP2 from traditional e-commerce into a comprehensive framework for the agent economy, enabling ultra-high-frequency programmable payment flows while maintaining security and interoperability.
Summary of ChangesHello @jaytoday, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances AP2 by integrating a robust micropayment channel extension. This new capability facilitates ultra-high-frequency, low-value transactions, transforming AP2 into a comprehensive platform for the agent economy. It enables flexible payment models like streaming and pay-per-use, and includes a sample AI inference service to demonstrate its practical application, alongside necessary updates to documentation and core payment types. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a comprehensive micropayment channel framework, a significant enhancement for AP2. The new modules for channel management and streaming payments are well-structured and the sample AI inference service is a great addition for demonstrating the new capabilities. The code quality is high, and the widespread adoption of modern Python styling and type hints across the codebase is a welcome improvement for maintainability. I have a few minor suggestions to enhance code clarity and robustness.
| state_data = f'{channel_id}' | ||
| for participant in participants: | ||
| state_data += f'{participant.participant_id}:{participant.current_balance.value}' | ||
|
|
||
| return hashlib.sha256(state_data.encode()).hexdigest() |
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.
The current state hash calculation is dependent on the order of participants in the list. This could lead to different hashes for the same state if the participant order changes for any reason. To make this more robust, I recommend sorting the participants by a unique and consistent key (e.g., participant_id) before generating the hash string.
| state_data = f'{channel_id}' | |
| for participant in participants: | |
| state_data += f'{participant.participant_id}:{participant.current_balance.value}' | |
| return hashlib.sha256(state_data.encode()).hexdigest() | |
| state_data = f'{channel_id}' | |
| sorted_participants = sorted(participants, key=lambda p: p.participant_id) | |
| for participant in sorted_participants: | |
| state_data += f'{participant.participant_id}:{participant.current_balance.value}' | |
| return hashlib.sha256(state_data.encode()).hexdigest() |
| 'I appreciate your question. Let me break this down into manageable components for you.', | ||
| ] | ||
|
|
||
| import random |
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.
| inactive_clients = [] | ||
| for client_did, channel_id in self.active_clients.items(): | ||
| if channel_id not in self.channel_manager.active_channels: | ||
| inactive_clients.append(client_did) | ||
|
|
||
| for client_did in inactive_clients: | ||
| del self.active_clients[client_did] |
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.
This logic for removing inactive clients can be made more concise and Pythonic by using a dictionary comprehension. This improves readability by expressing the intent in a single statement.
| inactive_clients = [] | |
| for client_did, channel_id in self.active_clients.items(): | |
| if channel_id not in self.channel_manager.active_channels: | |
| inactive_clients.append(client_did) | |
| for client_did in inactive_clients: | |
| del self.active_clients[client_did] | |
| self.active_clients = { | |
| client_did: channel_id | |
| for client_did, channel_id in self.active_clients.items() | |
| if channel_id in self.channel_manager.active_channels | |
| } |
| def is_expired(self, current_time: datetime | None = None) -> bool: | ||
| """Check if the channel has expired.""" | ||
| if current_time is None: | ||
| current_time = datetime.now(UTC) | ||
|
|
||
| expires_at = datetime.fromisoformat( | ||
| self.expires_at.replace('Z', '+00:00') | ||
| ) | ||
| return current_time > expires_at |
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.
This is a minor point for future maintainability. The use of .replace('Z', '+00:00') is necessary for Python versions before 3.11. However, datetime.fromisoformat in Python 3.11+ can handle the 'Z' (Zulu time) suffix directly. Since the codebase is adopting other modern Python features, you might consider removing this replace call if you are targeting Python 3.11 or newer.
This enhancement extends AP2 with a comprehensive micropayment channel framework that enables high-frequency, sub-cent transactions between agents through state channels, supporting Kite's vision of pay-per-use and streaming payment models.
Key Features:
Components Added:
This enables use cases like:
Transforms AP2 from traditional e-commerce into a comprehensive framework for the agent economy, enabling ultra-high-frequency programmable payment flows while maintaining security and interoperability.
Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
CONTRIBUTINGGuide.