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 run_started(string $threadId, string $runId): string
48{
49 return sse_event([
50 'type' => 'RUN_STARTED',
51 'threadId' => $threadId,
52 'runId' => $runId,
53 ]);
54}
55
56function text_message_start(string $messageId): string
57{
58 return sse_event([
59 'type' => 'TEXT_MESSAGE_START',
60 'messageId' => $messageId,
61 'role' => 'assistant',
62 ]);
63}
64
65function text_message_content(string $messageId, string $delta): string
66{
67 return sse_event([
68 'type' => 'TEXT_MESSAGE_CONTENT',
69 'messageId' => $messageId,
70 'delta' => $delta,
71 ]);
72}
73
74function text_message_end(string $messageId): string
75{
76 return sse_event([
77 'type' => 'TEXT_MESSAGE_END',
78 'messageId' => $messageId,
79 ]);
80}
81
82function run_finished(string $threadId, string $runId): string
83{
84 return sse_event([
85 'type' => 'RUN_FINISHED',
86 'threadId' => $threadId,
87 'runId' => $runId,
88 'finishReason' => 'stop',
89 ]);
90}
91
92function run_error(string $threadId, string $runId, string $message): string
93{
94 return sse_event([
95 'type' => 'RUN_ERROR',
96 'threadId' => $threadId,
97 'runId' => $runId,
98 'error' => ['message' => $message],
99 ]);
100}
101
102function begin_sse(): void
103{
104 header('Content-Type: text/event-stream');
105 header('Cache-Control: no-cache');
106 header('Connection: keep-alive');
107 header('Access-Control-Allow-Origin: *');
108 http_response_code(200);
109}
110
111function write_sse(string $frame): void
112{
113 echo $frame;
114 if (ob_get_level() > 0) {
115 ob_flush();
116 }
117 flush();
118}
119
120function cors_preflight(): void
121{
122 header('Access-Control-Allow-Origin: *');
123 header('Access-Control-Allow-Methods: POST, OPTIONS');
124 header('Access-Control-Allow-Headers: Content-Type');
125 http_response_code(204);
126}
127
128function stream_text(
129 string $threadId,
130 string $runId,
131 callable $stream
132): void {
133 begin_sse();
134 $messageId = 'msg-' . $runId;
135
136 write_sse(run_started($threadId, $runId));
137 write_sse(text_message_start($messageId));
138
139 $emit = static function (string $delta) use ($messageId): void {
140 if ($delta === '') {
141 return;
142 }
143 write_sse(text_message_content($messageId, $delta));
144 };
145
146 try {
147 $stream($emit);
148 write_sse(text_message_end($messageId));
149 write_sse(run_finished($threadId, $runId));
150 } catch (Throwable $error) {
151 error_log('[php] chat error: ' . $error->getMessage());
152 write_sse(run_error($threadId, $runId, $error->getMessage()));
153 }
154}
155
156function stream_error_only(string $threadId, string $runId, string $message): void
157{
158 begin_sse();
159 write_sse(run_started($threadId, $runId));
160 write_sse(run_error($threadId, $runId, $message));
161}