<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ineersa / symfony-ai-openai-codex-platform example snippets
ymfony\AI\Platform\Bridge\OpenAICodex\Auth\CodexAuthCommand;
use Symfony\AI\Platform\Bridge\OpenAICodex\Auth\CodexAuthFileStore;
use Symfony\AI\Platform\Bridge\OpenAICodex\Auth\CodexOAuthConfig;
use Symfony\AI\Platform\Bridge\OpenAICodex\Auth\CodexOAuthService;
use Symfony\AI\Platform\Bridge\OpenAICodex\Auth\CodexTokenRefresher;
use Symfony\AI\Platform\Bridge\OpenAICodex\CodexModel;
use Symfony\AI\Platform\Bridge\OpenAICodex\CodexTransportEnum;
use Symfony\AI\Platform\Bridge\OpenAICodex\Factory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\Component\Console\Application;
// Use your application's name instead of 'my-cli'. Share this identity
// between OAuth login, token refresh, and API requests.
$config = new CodexOAuthConfig(
originator: 'my-cli',
displayName: 'OpenAI Codex',
commandName: 'auth:codex',
);
$refresher = new CodexTokenRefresher(config: $config);
// Choose where your application stores credentials. The store refreshes
// expired tokens under a file lock when loadCredentials() is called.
$store = new CodexAuthFileStore(__DIR__.'/var/codex/auth.json', tokenRefresher: $refresher);
$service = new CodexOAuthService($store, $refresher, $config);
// Register the login command. An existing Console app can register it too.
if ('ask' !== ($argv[1] ?? null)) {
$application = new Application();
$application->addCommand(new CodexAuthCommand($service, $config));
exit($application->run());
}
$credentials = $store->loadCredentials();
if (null === $credentials) {
throw new RuntimeException('Run php codex.php auth:codex first.');
}
$provider = Factory::createProvider(
accessToken: $credentials->access,
accountId: $credentials->accountId,
originator: $config->originator,
userAgent: 'my-cli/1.0', // Replace with your application's name and version.
transport: CodexTransportEnum::Websocket,
// Retry once with a fresh token if the provider rejects the current one.
accessTokenRefresher: static fn (): string => $service->refreshCredentials()->access,
);
$result = $provider->invoke(
new CodexModel('gpt-5.6'),
new MessageBag(Message::ofUser('Hello')),
);
foreach ($result->asTextStream() as $text) {
echo $text;
}
echo PHP_EOL;
sh
php codex.php auth:codex
php codex.php ask
sh
php codex.php auth:codex --port=1456 --timeout=120 --no-browser
sh
php codex.php auth:codex --refresh
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.