-
Notifications
You must be signed in to change notification settings - Fork 43
Add AI_Service layer for centralized AI operations #101
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
Add AI_Service layer for centralized AI operations #101
Conversation
Implements a service layer that provides a consistent interface for experimental features to communicate with AI providers through the WP AI Client SDK. New files: - includes/Services/AI_Service.php - Singleton service class - includes/Services/Contracts/AI_Service_Interface.php - Interface contract - tests/Integration/Includes/Services/AI_ServiceTest.php - Integration tests Features: - Centralized AI provider configuration and request handling - Model preference caching with clear_model_cache() method - Graceful handling when no provider is configured - Helper function get_ai_service() for easy access Hooks and filters: - ai_service_available: Override provider availability detection - ai_service_initialized: Action when service initializes - ai_service_prompt_builder: Filter prompt builder before generation Closes WordPress#26
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @iTsphillgood. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #101 +/- ##
=============================================
+ Coverage 50.33% 50.91% +0.57%
- Complexity 366 375 +9
=============================================
Files 26 27 +1
Lines 1951 1974 +23
=============================================
+ Hits 982 1005 +23
Misses 969 969
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@caseymanos thanks for the PR! I see you note this closes #26, but looking to double confirm that the various goals in that issue description are all covered by your work here; correct? |
felixarntz
left a comment
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.
While overall I agree that a more opinionated AI service makes sense in the context of this plugin, I think it does too many things. Specifically, it obscures some of the flexibility that the underlying WordPress AI Client APIs provide, for no good reason as far as I can tell.
I think we should simplify the implementation to focus on creating a prompt builder with plugin specific defaults applied, and leave the rest to the regular prompt builder APIs.
Have you tried Google Jules agent or Gemini 3 on cli? |
Address @felixarntz review comments: - Remove AI_Service_Interface (premature abstraction) - Remove DEFAULT_TEMPERATURE constant (no forced defaults) - Remove generate_text/generate_texts methods (redundant wrappers) - Make create_prompt() the centerpiece with optional $options array - Use existing get_preferred_models() helper instead of duplicating - Simplify from 311 to 181 lines The new API follows Felix's suggested pattern: $text = $ai_service->create_prompt($prompt, $options)->generate_text();
b3bfd72 to
8e0a728
Compare
|
@felixarntz @jeffpaul
Additional improvements:
Updated API// Simple usage - model preferences auto-applied
$text = get_ai_service()->create_prompt('Summarize this...')->generate_text();
// With options array
$text = get_ai_service()->create_prompt('Translate to French', [
'system_instruction' => 'You are a translator.',
'temperature' => 0.3,
])->generate_text();
// Full SDK access via chaining
$titles = get_ai_service()->create_prompt('Generate titles')
->using_candidate_count(5)
->generate_texts();
Hooks Available
| Hook | Type | Description |
|------------------------|--------|------------------------------------------|
| ai_service_available | Filter | Override provider availability detection |
| ai_service_initialized | Action | Fires when AI service initializes |
| ai_preferred_models | Filter | Customize default model preferences |
Note: Removed ai_service_prompt_builder filter as it's no longer needed - create_prompt() returns the builder directly so consumers can modify it with the SDK |
felixarntz
left a comment
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.
@caseymanos Almost LGTM, just one remaining concern.
|
Related: @dkotter @caseymanos I'm not sure about the |
Address Felix's review feedback: - Remove is_available() method from AI_Service (SDK provides is_supported_for_*() methods on Prompt_Builder for capability checking) - Rename get_preferred_models() to get_preferred_models_for_text_generation() to clarify the function returns text-generation-specific models - Rename filter from ai_preferred_models to ai_preferred_models_for_text_generation - Update docstring example to use SDK's is_supported_for_text_generation()
|
@felixarntz Also renamed get_preferred_models() to get_preferred_models_for_text_generation() (filter renamed too). |
felixarntz
left a comment
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.
@caseymanos Thanks, LGTM!
Note there are some merge conflicts though that need to be resolved.
Address dkotter and felixarntz review feedback: - Rename create_prompt() to create_textgen_prompt() to clarify this method is specifically for text generation - Update @SInCE tags from 0.1.0 to x.x.x for release process - Rename hook from ai_service_initialized to ai_experiments_service_initialized to use correct prefix
|
|
@caseymanos Thanks for the work here. We've merged in a few other PRs that used the |
…ls_for_text_generation
includes/bootstrap.php
Outdated
| // Initialize the WP AI Client. | ||
| AI_Client::init(); |
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.
Looking at this again, we already run this exact same code a few lines above (currently line 206). Is there a reason to initialize it again here? Or can this be removed?
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.
Note I removed that here 9a4845d but let me know if that is wrong
includes/bootstrap.php
Outdated
| $ai_service = AI_Service::get_instance(); | ||
| $ai_service->init(); |
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.
I guess question on this as well looking at it again. It seems the init method we are calling here doesn't actually do anything (beyond just running a hook). Do we need this init method and the loading of this class here? Or can we remove this and anyone that wants to use this class can be done via the get_ai_service helper function?
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.
Note I removed these lines and the init method here: 9a4845d. But let me know if that will cause problems
…ed the function and filter names
What?
Closes #26
Implements a centralized AI service layer (
AI_Service) that provides a consistent interface for experimental features to communicate with AI providers through the WP AI Client SDK.Why?
The plugin needs a unified service layer so that experimental features can communicate with AI providers without duplicating logic or directly coupling to the
AI_ClientSDK. This provides:How?
New files:
includes/Services/AI_Service.php- Singleton service class withgenerate_text(),generate_texts(),create_prompt(),is_available()methodsincludes/Services/Contracts/AI_Service_Interface.php- Interface contract ensuring consistent implementationtests/Integration/Includes/Services/AI_ServiceTest.php- Integration tests (26 test cases)Modified files:
includes/bootstrap.php- Initializes AI_Service afterAI_Client::init()includes/helpers.php- Addsget_ai_service()helper functionHooks and filters:
ai_service_availableai_service_initializedai_service_prompt_builderTesting Instructions
npm run test:phpis_available()should returnfalseTesting Instructions for Keyboard
Not applicable - this PR adds backend service infrastructure only with no UI changes.
Screenshots or screencast
Not applicable - this PR adds backend service infrastructure only with no UI changes.