Current File : //safe-wp/wp-includes/php-ai-client/src/AiClient.php
<?php
declare (strict_types=1);
namespace WordPress\AiClient;
use WordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface;
use WordPress\AiClientDependencies\Psr\SimpleCache\CacheInterface;
use WordPress\AiClient\Builders\PromptBuilder;
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface;
use WordPress\AiClient\Providers\Contracts\ProviderInterface;
use WordPress\AiClient\Providers\Models\Contracts\ModelInterface;
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
use WordPress\AiClient\Providers\ProviderRegistry;
use WordPress\AiClient\Results\DTO\GenerativeAiResult;
/**
* Main AI Client class providing both fluent and traditional APIs for AI operations.
*
* This class serves as the primary entry point for AI operations, offering:
* - Fluent API for easy-to-read chained method calls
* - Traditional API for array-based configuration (WordPress style)
* - Integration with provider registry for model discovery
* - Support for three model specification approaches
*
* All model requirements analysis and capability matching is handled
* automatically by the PromptBuilder, which provides intelligent model
* discovery based on prompt content and configuration.
*
* ## Model Specification Approaches
*
* ### 1. Specific Model Instance
* Use a specific ModelInterface instance when you know exactly which model to use:
* ```php
* $model = $registry->getProvider('openai')->getModel('gpt-4');
* $result = AiClient::generateTextResult('What is PHP?', $model);
* ```
*
* ### 2. ModelConfig for Auto-Discovery
* Use ModelConfig to specify requirements and let the system discover the best model:
* ```php
* $config = new ModelConfig();
* $config->setTemperature(0.7);
* $config->setMaxTokens(150);
*
* $result = AiClient::generateTextResult('What is PHP?', $config);
* ```
*
* ### 3. Automatic Discovery (Default)
* Pass null or omit the parameter for intelligent model discovery based on prompt content:
* ```php
* // System analyzes prompt and selects appropriate model automatically
* $result = AiClient::generateTextResult('What is PHP?');
* $imageResult = AiClient::generateImageResult('A sunset over mountains');
* ```
*
* ## Fluent API Examples
* ```php
* // Fluent API with automatic model discovery
* $result = AiClient::prompt('Generate an image of a sunset')
* ->usingTemperature(0.7)
* ->generateImageResult();
*
* // Fluent API with specific model
* $result = AiClient::prompt('What is PHP?')
* ->usingModel($specificModel)
* ->usingTemperature(0.5)
* ->generateTextResult();
*
* // Fluent API with model configuration
* $result = AiClient::prompt('Explain quantum physics')
* ->usingModelConfig($config)
* ->generateTextResult();
* ```
*
* @since 0.1.0
*
* @phpstan-import-type Prompt from PromptBuilder
*
* phpcs:ignore Generic.Files.LineLength.TooLong
*/
class AiClient
{
/**
* @var string The version of the AI Client.
*/
public const VERSION = '1.3.1';
/**
* @var ProviderRegistry|null The default provider registry instance.
*/
private static ?ProviderRegistry $defaultRegistry = null;
/**
* @var EventDispatcherInterface|null The event dispatcher for prompt lifecycle events.
*/
private static ?EventDispatcherInterface $eventDispatcher = null;
/**
* @var CacheInterface|null The PSR-16 cache for storing and retrieving cached data.
*/
private static ?CacheInterface $cache = null;
/**
* Gets the default provider registry instance.
*
* @since 0.1.0
*
* @return ProviderRegistry The default provider registry.
*/
public static function defaultRegistry(): ProviderRegistry
{
if (self::$defaultRegistry === null) {
self::$defaultRegistry = new ProviderRegistry();
}
return self::$defaultRegistry;
}
/**
* Sets the event dispatcher for prompt lifecycle events.
*
* The event dispatcher will be used to dispatch BeforeGenerateResultEvent and
* AfterGenerateResultEvent during prompt generation.
*
* @since 0.4.0
*
* @param EventDispatcherInterface|null $dispatcher The event dispatcher, or null to disable.
* @return void
*/
public static function setEventDispatcher(?EventDispatcherInterface $dispatcher): void
{
self::$eventDispatcher = $dispatcher;
}
/**
* Gets the event dispatcher for prompt lifecycle events.
*
* @since 0.4.0
*
* @return EventDispatcherInterface|null The event dispatcher, or null if not set.
*/
public static function getEventDispatcher(): ?EventDispatcherInterface
{
return self::$eventDispatcher;
}
/**
* Sets the PSR-16 cache for storing and retrieving cached data.
*
* The cache can be used to store AI responses and other data to avoid
* redundant API calls and improve performance.
*
* @since 0.4.0
*
* @param CacheInterface|null $cache The PSR-16 cache instance, or null to disable caching.
* @return void
*/
public static function setCache(?CacheInterface $cache): void
{
self::$cache = $cache;
}
/**
* Gets the PSR-16 cache instance.
*
* @since 0.4.0
*
* @return CacheInterface|null The cache instance, or null if not set.
*/
public static function getCache(): ?CacheInterface
{
return self::$cache;
}
/**
* Checks if a provider is configured and available for use.
*
* Supports multiple input formats for developer convenience:
* - ProviderAvailabilityInterface: Direct availability check
* - string (provider ID): e.g., AiClient::isConfigured('openai')
* - string (class name): e.g., AiClient::isConfigured(OpenAiProvider::class)
*
* When using string input, this method leverages the ProviderRegistry's centralized
* dependency management, ensuring HttpTransporter and authentication are properly
* injected into availability instances.
*
* @since 0.1.0
* @since 0.2.0 Now supports being passed a provider ID or class name.
*
* @param ProviderAvailabilityInterface|string|class-string<ProviderInterface> $availabilityOrIdOrClassName
* The provider availability instance, provider ID, or provider class name.
* @return bool True if the provider is configured and available, false otherwise.
*/
public static function isConfigured($availabilityOrIdOrClassName): bool
{
// Handle direct ProviderAvailabilityInterface (backward compatibility)
if ($availabilityOrIdOrClassName instanceof ProviderAvailabilityInterface) {
return $availabilityOrIdOrClassName->isConfigured();
}
// Handle string input (provider ID or class name) via registry
if (is_string($availabilityOrIdOrClassName)) {
return self::defaultRegistry()->isProviderConfigured($availabilityOrIdOrClassName);
}
throw new \InvalidArgumentException('Parameter must be a ProviderAvailabilityInterface instance, provider ID string, or provider class name. ' . sprintf('Received: %s', is_object($availabilityOrIdOrClassName) ? get_class($availabilityOrIdOrClassName) : gettype($availabilityOrIdOrClassName)));
}
/**
* Creates a new prompt builder for fluent API usage.
*
* Returns a PromptBuilder instance configured with the specified or default registry.
* The traditional API methods in this class delegate to PromptBuilder
* for all generation logic.
*
* @since 0.1.0
*
* @param Prompt $prompt Optional initial prompt content.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return PromptBuilder The prompt builder instance.
*/
public static function prompt($prompt = null, ?ProviderRegistry $registry = null): PromptBuilder
{
return new PromptBuilder($registry ?? self::defaultRegistry(), $prompt, self::$eventDispatcher);
}
/**
* Generates content using a unified API that automatically detects model capabilities.
*
* When no model is provided, this method delegates to PromptBuilder for intelligent
* model discovery based on prompt content and configuration. When a model is provided,
* it infers the capability from the model's interfaces and delegates to the capability-based method.
*
* @since 0.1.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig $modelOrConfig Specific model to use, or model configuration
* for auto-discovery.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the provided model doesn't support any known generation type.
* @throws \RuntimeException If no suitable model can be found for the prompt.
*/
public static function generateResult($prompt, $modelOrConfig, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateResult();
}
/**
* Generates text using the traditional API approach.
*
* @since 0.1.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
* or model configuration for auto-discovery,
* or null for defaults.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the prompt format is invalid.
* @throws \RuntimeException If no suitable model is found.
*/
public static function generateTextResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateTextResult();
}
/**
* Generates an image using the traditional API approach.
*
* @since 0.1.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
* or model configuration for auto-discovery,
* or null for defaults.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the prompt format is invalid.
* @throws \RuntimeException If no suitable model is found.
*/
public static function generateImageResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateImageResult();
}
/**
* Converts text to speech using the traditional API approach.
*
* @since 0.1.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
* or model configuration for auto-discovery,
* or null for defaults.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the prompt format is invalid.
* @throws \RuntimeException If no suitable model is found.
*/
public static function convertTextToSpeechResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->convertTextToSpeechResult();
}
/**
* Generates speech using the traditional API approach.
*
* @since 0.1.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
* or model configuration for auto-discovery,
* or null for defaults.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the prompt format is invalid.
* @throws \RuntimeException If no suitable model is found.
*/
public static function generateSpeechResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateSpeechResult();
}
/**
* Generates a video using the traditional API approach.
*
* @since 1.3.0
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig Optional specific model to use,
* or model configuration for auto-discovery,
* or null for defaults.
* @param ProviderRegistry|null $registry Optional custom registry. If null, uses default.
* @return GenerativeAiResult The generation result.
*
* @throws \InvalidArgumentException If the prompt format is invalid.
* @throws \RuntimeException If no suitable model is found.
*/
public static function generateVideoResult($prompt, $modelOrConfig = null, ?ProviderRegistry $registry = null): GenerativeAiResult
{
self::validateModelOrConfigParameter($modelOrConfig);
return self::getConfiguredPromptBuilder($prompt, $modelOrConfig, $registry)->generateVideoResult();
}
/**
* Creates a new message builder for fluent API usage.
*
* This method will be implemented once MessageBuilder is available.
* MessageBuilder will provide a fluent interface for constructing complex
* messages with multiple parts, attachments, and metadata.
*
* @since 0.1.0
*
* @param string|null $text Optional initial message text.
* @return object MessageBuilder instance (type will be updated when MessageBuilder is available).
*
* @throws \RuntimeException When MessageBuilder is not yet available.
*/
public static function message(?string $text = null)
{
throw new RuntimeException('MessageBuilder is not yet available. This method depends on builder infrastructure. ' . 'Use direct generation methods (generateTextResult, generateImageResult, etc.) for now.');
}
/**
* Validates that parameter is ModelInterface, ModelConfig, or null.
*
* @param mixed $modelOrConfig The parameter to validate.
* @return void
* @throws \InvalidArgumentException If parameter is invalid type.
*/
private static function validateModelOrConfigParameter($modelOrConfig): void
{
if ($modelOrConfig !== null && !$modelOrConfig instanceof ModelInterface && !$modelOrConfig instanceof ModelConfig) {
throw new InvalidArgumentException('Parameter must be a ModelInterface instance (specific model), ' . 'ModelConfig instance (for auto-discovery), or null (default auto-discovery). ' . sprintf('Received: %s', is_object($modelOrConfig) ? get_class($modelOrConfig) : gettype($modelOrConfig)));
}
}
/**
* Configures PromptBuilder based on model/config parameter type.
*
* @param Prompt $prompt The prompt content.
* @param ModelInterface|ModelConfig|null $modelOrConfig The model or config parameter.
* @param ProviderRegistry|null $registry Optional custom registry to use.
* @return PromptBuilder Configured prompt builder.
*/
private static function getConfiguredPromptBuilder($prompt, $modelOrConfig, ?ProviderRegistry $registry = null): PromptBuilder
{
$builder = self::prompt($prompt, $registry);
if ($modelOrConfig instanceof ModelInterface) {
$builder->usingModel($modelOrConfig);
} elseif ($modelOrConfig instanceof ModelConfig) {
$builder->usingModelConfig($modelOrConfig);
}
// null case: use default model discovery
return $builder;
}
}
Zerion Mini Shell 1.0
Home - Professional Genealogists and Historians - LineagesSkip to content
Our talented staff of professional researchers can help you, whether you have hit a roadblock in your research or simply lack the time or training to tackle it yourself. Plus, our rates are 40% lower than many of our competitors! Check out our sample projects and then pick the research package that best fits your needs!
ENSLAVED PEOPLE & ENSLAVERS
Identifying the men and women who owned your ancestors during slavery requires finely honed skills and years of experience. Our expertise comes from having traced hundreds of African-American family trees. Choose the research package that best fits your needs, and we will start tracing your ancestors back to the slave period and look for their owners.
Genealogy Research that's Insightful, Thorough & Personal
Our staff of amazinggenealogistsand family historians cannot wait to start documenting your ancestry. They will work with you to plan and achieve your research goals.
We will need your help in bringing your ancestors back to life by supplying items not available in public records or online, such as photos, passages from diaries, family stories, unsolved mysteries, and other interesting details.
The end result will be a fun trip down several memory lanes, and you will want to share it with family and friends.
“When Alex Haley prodded me to have my ancestry traced, I reached out to Johni Cerny and she has been my genealogist for 34 years—tracing my African-American roots and the ancestry of many of my close friends. She eternally reminds all of us how we got to this planet. Johni also keeps my soul eternally smiling and nobody does it better!”
Quincy D. Jones
“Thank you, thank you, thank you…for the amazing work you did to identify my biological father. The DNA results were gibberish until you explained it in layman’s terms for me and then used it to find him. Now it is time for you to trace his ancestry. I can’t wait to see the results of that adventure!”
Janet Williamson
“Wow, the research package arrived a few days ago and I can hardly put it down. The report and all those wonderful documents tell an interesting story about my mother’s Graupman ancestors. We will treasure this forever! Let me know what else you can do to trace the Graupman line in German records.”
K. Meyer
“My sister always felt like the odd person out in our family. DNA testing confirmed we were half-siblings. In her late 60s, we didn’t hold out much hope for identifying her biological father. With over 1,000 matches, we turned to Lineages’ staff expert for help. A few months and a lot of hard work, they solved the mystery. Lineages gave my sister the gift of a lifetime. Thank you, Lineages!”
How Your Family Tree Could Make You a Citizen Somewhere in many American family trees is a name followed by a Canadian birthplace: a fishing village in Nova Scotia, a farm township in Ontario, a parish along the St. Lawrence River. Maybe it belongs to a great-grandmother who crossed into Michigan as a young woman…
Imagine sitting down with a photograph of a document that has stumped your family for three generations. It is a baptismal register from a village church, the ink faded to a whisper, the words looping across the page in a script that seems indecipherable. Previously, you could hire a specialist, spend months teaching yourself the…
What an immigrant’s disappearance taught me about the limits of records, the value of living relatives, and why some answers only exist in family memory. The ancestor in this story happened to be Swedish; the story itself is one that almost every immigrant family carries. Öckerö parish husförhörslängd AIIa:5 (1916–1938), Image 1490, Page 140 —…
For half a century, Price Genealogy has done more than just look at census records and birth certificates; we’ve listened to the echoes of the past to tell the stories that define your present As we celebrate our golden anniversary, we look back at five decades of unmatched expertise, and more importantly, look forward to…
Description: French soldier in service uniform Date: 1914 Library of Congress, public domain. If you have recent French ancestors, there’s a good chance they appear in one of the most useful record sets you may not be using: the Registres de Matricule, or Military Conscription Registers. Starting in the late 19th century, every young man…
Insights from 50 Years of Professional Genealogy Your ancestors are from a country outside of the United States – now what? If your ancestors are from European countries such as the United Kingdom, Scandinavia, Germany, France, Switzerland, Spain, Italy, or even Latin American countries such as Mexico and Brazil, you may be able to find…
What better way to honor the 250th birthday of the United States of America than by discovering and recognizing your family’s contributions to the foundation of the United States. Lineage societies offer an interesting opportunity to recognize those past contributions and to participate in activities today that will give our descendants reason to celebrate in…
For fifty years, Price Genealogy has had the privilege of walking alongside families as they uncover their roots. While our company name appears on every report, it’s our researchers who bring each story to life. Behind every carefully crafted report, every breakthrough discovery, and every long-awaited answer is a dedicated researcher who truly cares about…
The journey of a master genealogist often begins not in a grand archive, but in the quiet corners of a family home. For Richard Price, that journey began in 1962. Like many young teenagers, Richard was naturally curious, but the discovery of an old file drawer of family memorabilia in his grandfather’s garage, transformed a…
Understanding Pedigree Collapse A typical family tree looks like this, with you at the bottom and spreading out vertically as your ancestors multiply, from 2 parents, to 4 grandparents, 8 great grandparents, 16 great grandparents, and so on. But probably at some point for all of us a portion of our tree resembles more of…