Open History to see the last commit
1<?php
2
3declare(strict_types=1);
4
5/**
6 * PHP Slim Framework server example for TanStack AI
7 * Streams OpenAI API events in SSE format compatible with TanStack AI client
8 */
9
10require __DIR__ . '/../vendor/autoload.php';
11
12use Psr\Http\Message\ResponseInterface as Response;
13use Psr\Http\Message\ServerRequestInterface as Request;
14use Slim\Factory\AppFactory;
15use Dotenv\Dotenv;
16use TanStack\AI\StreamChunkConverter;
17use TanStack\AI\MessageFormatters;
18use TanStack\AI\SSEFormatter;
19use OpenAI\Client as OpenAIClient;
20use OpenAI\Factory;
21use Monolog\Logger;
22use Monolog\Handler\StreamHandler;
23use Slim\Psr7\Response as SlimResponse;
24
25// Load environment variables
26$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
27$dotenv->load();
28
29// Configure logging
30$logger = new Logger('tanstack-ai');
31$logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
32
33// Initialize Slim app
34$app = AppFactory::create();
35
36// CORS middleware - wide open for development
37$app->add(function (Request $request, $handler) {
38 // Handle preflight OPTIONS request
39 if ($request->getMethod() === 'OPTIONS') {
40 $response = new SlimResponse();
41 return $response
42 ->withHeader('Access-Control-Allow-Origin', '*')
43 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
44 ->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
45 ->withHeader('Access-Control-Max-Age', '86400')
46 ->withStatus(204);
47 }
49 $response = $handler->handle($request);
50 return $response
51 ->withHeader('Access-Control-Allow-Origin', '*')
52 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
53 ->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
54});
55
56// Initialize OpenAI client
57$openaiApiKey = $_ENV['OPENAI_API_KEY'] ?? null;
58if (!$openaiApiKey) {
59 throw new RuntimeException(
60 "OPENAI_API_KEY environment variable is required. " .
61 "Please set it in your .env file or environment."
62 );
63}
64
65// Validate API key format
66if (str_starts_with($openaiApiKey, 'op://')) {
67 throw new RuntimeException(
68 "⚠️ ERROR: API key appears to be a 1Password reference (op://...).\n" .
69 "You need to use the actual API key value, not the 1Password reference.\n" .
70 "Please copy the actual key from 1Password (starts with 'sk-') and update your .env file."
71 );
72}
73
74if (!str_starts_with($openaiApiKey, 'sk-')) {
75 $logger->warning("API key doesn't start with 'sk-'. This may not be a valid OpenAI API key.");
76 $logger->warning("Key starts with: " . substr($openaiApiKey, 0, 10) . "...");
77}
78
79if (strlen($openaiApiKey) < 40) {
80 $logger->warning("API key seems too short (" . strlen($openaiApiKey) . " chars). OpenAI keys are typically 50+ characters.");
81}
82
83// Display API key info on startup (masked for security)
84function maskApiKey(string $key): string
85{
86 if (strlen($key) <= 11) {
87 return str_repeat('*', strlen($key));
88 }
89 return substr($key, 0, 7) . '...' . substr($key, -4);
90}
91
92echo "\n" . str_repeat('=', 60) . "\n";
93echo "🚀 TanStack AI Slim Server Starting (OpenAI)...\n";
94echo str_repeat('=', 60) . "\n";
95echo "✅ OPENAI_API_KEY loaded: " . maskApiKey($openaiApiKey) . "\n";
96echo " Key length: " . strlen($openaiApiKey) . " characters\n";
97echo "🌐 Server will start on: http://0.0.0.0:8001\n";
98echo " (Note: Run with: php -S 0.0.0.0:8001 -t public public/openai-server.php)\n";
99echo str_repeat('=', 60) . "\n\n";
100
101$client = (new Factory())->withApiKey($openaiApiKey)->make();
102
103// Explicit OPTIONS route for CORS preflight
104$app->options('/chat', function (Request $request, Response $response) {
105 return $response
106 ->withHeader('Access-Control-Allow-Origin', '*')
107 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
108 ->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
109 ->withHeader('Access-Control-Max-Age', '86400')
110 ->withStatus(204);
111});
112
113// Chat endpoint
114$app->post('/chat', function (Request $request, Response $response) use ($client, $logger) {
115 try {
116 $body = json_decode($request->getBody()->getContents(), true);
117 $messages = $body['messages'] ?? [];
118 $data = $body['data'] ?? [];
119
120 $logger->info("📥 POST /chat received - " . count($messages) . " messages");
121
122 // Convert messages to OpenAI format
123 $openaiMessages = MessageFormatters::formatMessagesForOpenAI($messages);
124 $logger->info("✅ Converted " . count($openaiMessages) . " messages to OpenAI format");
125
126 // Default model
127 $model = $data['model'] ?? 'gpt-4o';
128 $logger->info("🤖 Using model: " . $model);
129
130 // Initialize converter
131 $converter = new StreamChunkConverter(model: $model, provider: 'openai');
132
133 // Send headers immediately for streaming (must be before any output)
134 // Disable output buffering and send headers
135 if (ob_get_level() > 0) {
136 ob_end_clean();
137 }
139 header('Content-Type: text/event-stream');
140 header('Cache-Control: no-cache');
141 header('Connection: keep-alive');
142 header('X-Accel-Buffering: no');
143 header('Access-Control-Allow-Origin: *');
144 header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
145 header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
147 // Flush headers immediately
148 flush();
149
150 // Stream response with CORS headers (for Slim response object)
151 $response = $response
152 ->withHeader('Content-Type', 'text/event-stream')
153 ->withHeader('Cache-Control', 'no-cache')
154 ->withHeader('Connection', 'keep-alive')
155 ->withHeader('X-Accel-Buffering', 'no')
156 ->withHeader('Access-Control-Allow-Origin', '*')
157 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
158 ->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
159
160 // Note: We're using direct output (echo) for streaming, so we don't need the response body
161
162 $logger->info("🚀 Starting OpenAI stream for model: {$model}");
163
164 $eventCount = 0;
165 $chunkCount = 0;
166
167 try {
168 // Stream from OpenAI
169 $stream = $client->chat()->createStreamed([
170 'model' => $model,
171 'messages' => $openaiMessages,
172 'max_tokens' => 1024,
173 'temperature' => 0.7,
174 ]);
175
176 $logger->info("✅ OpenAI stream created, starting to receive events...");
177
178 foreach ($stream as $event) {
179 $eventCount++;
180 // Convert event to array if it's an object
181 $eventData = is_array($event) ? $event : (array)$event;
182 $logger->debug("📨 Received OpenAI event #{$eventCount}: " . (is_array($event) ? 'array' : get_class($event)));
183
184 // Convert OpenAI event to StreamChunk format
185 $chunks = $converter->convertEvent($event);
186
187 foreach ($chunks as $chunk) {
188 $chunkCount++;
189 $chunkType = $chunk['type'] ?? 'unknown';
190 $logger->debug("📤 Sending chunk #{$chunkCount} (type: {$chunkType})");
191 $chunkData = SSEFormatter::formatChunk($chunk);
192 // Output directly for streaming
193 echo $chunkData;
194 if (ob_get_level() > 0) {
195 ob_flush();
196 }
197 flush();
198 }
199 }
200
201 $logger->info("✅ Stream complete - {$eventCount} events, {$chunkCount} chunks sent");
202
203 // Send completion marker
204 $logger->info("📤 Sending [DONE] marker");
205 $doneMarker = SSEFormatter::formatDone();
206 echo $doneMarker;
207 flush();
208
209 } catch (\Throwable $e) {
210 $logger->error("❌ Error in stream: " . get_class($e) . ": " . $e->getMessage());
211 // Send error chunk
212 $errorChunk = $converter->convertError($e);
213 $errorData = SSEFormatter::formatChunk($errorChunk);
214 echo $errorData;
215 flush();
216 exit;
217 }
218
219 // Should not reach here, but just in case
220 exit;
221
222 } catch (\Throwable $e) {
223 $logger->error("❌ Error in chat_endpoint: " . get_class($e) . ": " . $e->getMessage());
224 $response->getBody()->write(json_encode(['error' => $e->getMessage()]));
225 return $response->withStatus(500)->withHeader('Content-Type', 'application/json');
226 }
227});
228
229// Health check endpoint
230$app->get('/health', function (Request $request, Response $response) {
231 $response->getBody()->write(json_encode([
232 'status' => 'ok',
233 'service' => 'tanstack-ai-slim-openai'
234 ]));
235 return $response->withHeader('Content-Type', 'application/json');
236});
237
238$app->run();
239