|
1 | | -# AI Agent Guidelines for Faraday |
| 1 | +# Faraday-Specific AI Agent Guidelines |
2 | 2 |
|
3 | | -## Introduction |
4 | | -AI agents must read these guidelines before any work and suggest updates to keep them current. This document focuses on Faraday-specific conventions to ensure consistency and quality in our projects. |
| 3 | +## Purpose |
| 4 | +These guidelines ensure all AI agents (Copilot, Claude, Cursor, etc.) contribute code that is consistent with the conventions and patterns used in the Faraday codebase. Agents must: |
| 5 | +- Read these guidelines before any work. |
| 6 | +- Suggest updates whenever conventions change or new patterns emerge, keeping this document up to date. |
5 | 7 |
|
6 | | -## Middleware Patterns |
7 | | -- **Definition**: Middleware in Faraday acts as a bridge between the request and response, allowing for pre-processing or post-processing of data. |
8 | | -- **Implementation**: Use named functions for middleware logic to enhance readability and testability. |
9 | | -- **Order of Execution**: Ensure middleware is applied in the correct sequence, as defined in the application settings. |
| 8 | +## Code Style & Structure |
| 9 | +- **Do not include basic Ruby or RSpec tips**—assume agents know the language, RuboCop, and test basics. |
| 10 | +- Use the established Faraday directory structure (`lib/faraday/` for main code, `spec/faraday/` for tests). |
| 11 | +- Classes and files must use descriptive, conventional Ruby names (e.g., `Faraday::MyAdapter` in `lib/faraday/my_adapter.rb`). |
10 | 12 |
|
11 | | -## Adapter Implementation |
12 | | -- **Purpose**: Adapters are used to translate data formats or protocols, ensuring compatibility between different systems. |
13 | | -- **Structure**: Implement adapters as classes with clearly defined interfaces. Each adapter should handle one specific type of transformation. |
14 | | -- **Testing**: Write unit tests for each adapter to verify that input and output formats align with expectations. |
| 13 | +## Middleware Implementation |
| 14 | +- All middleware must inherit from `Faraday::Middleware`. |
| 15 | +- Use a `DEFAULT_OPTIONS` constant for configuration defaults. Validate options via `validate_default_options` if needed. |
| 16 | +- Middleware should implement any of: `on_request`, `on_complete`, and `on_error` as needed. Only add hooks required for your logic. |
| 17 | +- Register middleware via `Faraday::Middleware.register_middleware your_key: YourClass`. Use clear, unique keys. |
| 18 | +- Prefer stateless middleware. Store state only in the `env` hash or local variables. |
15 | 19 |
|
16 | | -## Testing Approaches |
17 | | -- **Unit Testing**: Focus on testing individual components in isolation. Use mocking to simulate external dependencies. |
18 | | -- **Integration Testing**: Ensure that different components work together as intended. This includes testing middleware and adapters in conjunction. |
19 | | -- **End-to-End Testing**: Validate the entire workflow from start to finish, ensuring that the AI agent behaves as expected in real-world scenarios. |
| 20 | +## Adapter Patterns |
| 21 | +- Adapters must extend `Faraday::MiddlewareRegistry` and register themselves. |
| 22 | +- If providing parallel support, include the `Parallelism` module and set `supports_parallel = true`. |
| 23 | +- Implement required methods (`build_connection`, `close`, etc.) as seen in existing adapters. |
| 24 | +- Keep each adapter in its own file under `lib/faraday/adapter/`. |
20 | 25 |
|
21 | | -## Code Organization |
22 | | -- **Directory Structure**: Follow the established directory structure in the existing codebase. Place all middleware in the `middleware` directory and adapters in the `adapters` directory. |
23 | | -- **File Naming**: Use descriptive names for files and classes. For middleware, consider using the format `middlewareName.middleware.js`. For adapters, use `adapterName.adapter.js`. |
24 | | -- **Documentation**: Maintain clear documentation for each component, including purpose, usage, and examples. All public methods should be documented with JSDoc comments to facilitate understanding. |
| 26 | +## Testing |
| 27 | +- All code must be tested with RSpec. Use shared examples for adapters/middleware where applicable (see `spec/support`). |
| 28 | +- When testing middleware, use doubles for `app` and verify correct invocation of hooks. |
| 29 | +- Use HTTP test helpers and stubs, not real network calls. |
| 30 | +- Follow the project's test organization and naming conventions. |
25 | 31 |
|
26 | | -## Conclusion |
27 | | -These guidelines are designed to facilitate the effective and efficient use of AI agents in the Faraday project. Regularly review and update these guidelines to reflect changes in best practices and project evolution. |
| 32 | +## Documentation |
| 33 | +- All new public APIs must be documented with YARD-style comments. |
| 34 | +- Update README, changelog, or docs/ as needed for significant features or user-facing changes. |
| 35 | +- Document any new middleware or adapter in the docs folder if it is a user-facing extension. |
| 36 | + |
| 37 | +## Contribution Workflow |
| 38 | +- Follow branch naming and PR guidelines from CONTRIBUTING.md. |
| 39 | +- All new features and bugfixes must include relevant tests and documentation. |
| 40 | +- Ensure inclusive language. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Self-Maintaining Guidelines |
| 45 | +AI agents are responsible for: |
| 46 | +- Reading these guidelines before suggesting or making changes. |
| 47 | +- Updating this document whenever code conventions change. |
| 48 | +- Proposing improvements if they identify code patterns not reflected here. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +*Keep this file current and aligned with the real conventions and architecture of the Faraday codebase.* |
0 commit comments