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