Open History to see the last commit
1<?php
2
3declare(strict_types=1);
4
5const DEFAULT_OPENAI_MODEL = 'gpt-4o';
6const DEFAULT_ANTHROPIC_MODEL = 'claude-sonnet-4-6';
7
8function props_from_input(array $input): array
9{
10 if (!empty($input['forwardedProps']) && is_array($input['forwardedProps'])) {
11 return $input['forwardedProps'];
12 }
13
14 if (!empty($input['data']) && is_array($input['data'])) {
15 return $input['data'];
16 }
17
18 return [];
19}
20
21function provider_from_input(array $input): array
22{
23 $props = props_from_input($input);
24 $provider = is_string($props['provider'] ?? null) && $props['provider'] !== ''
25 ? $props['provider']
26 : 'openai';
27 $model = is_string($props['model'] ?? null) ? $props['model'] : '';
28
29 if ($provider === 'anthropic') {
30 return [
31 'provider' => 'anthropic',
32 'model' => $model !== '' ? $model : DEFAULT_ANTHROPIC_MODEL,
33 ];
34 }
35
36 return [
37 'provider' => 'openai',
38 'model' => $model !== '' ? $model : DEFAULT_OPENAI_MODEL,
39 ];
40}
41
42function sse_event(array $payload): string
43{
44 return 'data: ' . json_encode($payload, JSON_UNESCAPED_UNICODE) . "\n\n";
45}
46
47function sse_done(): string
48{
49 return "data: [DONE]\n\n";
50}
51
52function run_started(string $threadId, string $runId): string
53{
54 return sse_event([
55 'type' => 'RUN_STARTED',
56 'threadId' => $threadId,
57 'runId' => $runId,
58 ]);
59}
60
61function text_message_start(string $messageId): string
62{
63 return sse_event([
64 'type' => 'TEXT_MESSAGE_START',
65 'messageId' => $messageId,
66 'role' => 'assistant',
67 ]);
68}
69
70function text_message_content(string $messageId, string $delta): string
71{
72 return sse_event([
73 'type' => 'TEXT_MESSAGE_CONTENT',
74 'messageId' => $messageId,
75 'delta' => $delta,
76 ]);
77}
78
79function text_message_end(string $messageId): string
80{
81 return sse_event([
82 'type' => 'TEXT_MESSAGE_END',
83 'messageId' => $messageId,
84 ]);
85}
86
87function run_finished(string $threadId, string $runId): string
88{
89 return sse_event([
90 'type' => 'RUN_FINISHED',
91 'threadId' => $threadId,
92 'runId' => $runId,
93 'finishReason' => 'stop',
94 ]);
95}
96
97function run_error(string $threadId, string $runId, string $message): string
98{
99 return sse_event([
100 'type' => 'RUN_ERROR',
101 'threadId' => $threadId,
102 'runId' => $runId,
103 'error' => ['message' => $message],
104 ]);
105}
106
107function begin_sse(): void
108{
109 header('Content-Type: text/event-stream');
110 header('Cache-Control: no-cache');
111 header('Connection: keep-alive');
112 header('Access-Control-Allow-Origin: *');
113 http_response_code(200);
114}
115
116function write_sse(string $frame): void
117{
118 echo $frame;
119 if (ob_get_level() > 0) {
120 ob_flush();
121 }
122 flush();
123}
124
125function cors_preflight(): void
126{
127 header('Access-Control-Allow-Origin: *');
128 header('Access-Control-Allow-Methods: POST, OPTIONS');
129 header('Access-Control-Allow-Headers: Content-Type');
130 http_response_code(204);
131}
132
133function stream_text(
134 string $threadId,
135 string $runId,
136 callable $stream
137): void {
138 begin_sse();
139 $messageId = 'msg-' . $runId;
140
141 write_sse(run_started($threadId, $runId));
142 write_sse(text_message_start($messageId));
143
144 $emit = static function (string $delta) use ($messageId): void {
145 if ($delta === '') {
146 return;
147 }
148 write_sse(text_message_content($messageId, $delta));
149 };
150
151 try {
152 $stream($emit);
153 write_sse(text_message_end($messageId));
154 write_sse(run_finished($threadId, $runId));
155 write_sse(sse_done());
156 } catch (Throwable $error) {
157 error_log('[php] chat error: ' . $error->getMessage());
158 write_sse(run_error($threadId, $runId, $error->getMessage()));
159 write_sse(sse_done());
160 }
161}
162
163function stream_error_only(string $threadId, string $runId, string $message): void
164{
165 begin_sse();
166 write_sse(run_started($threadId, $runId));
167 write_sse(run_error($threadId, $runId, $message));
168 write_sse(sse_done());
169}