Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

Agui.php

169 lines4.0 KB

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 [];
20​
21function provider_from_input(array $input): array
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 ];
41​
42function sse_event(array $payload): string
44 return 'data: ' . json_encode($payload, JSON_UNESCAPED_UNICODE) . "\n\n";
46​
47function sse_done(): string
49 return "data: [DONE]\n\n";
51​
52function run_started(string $threadId, string $runId): string
54 return sse_event([
55 'type' => 'RUN_STARTED',
56 'threadId' => $threadId,
57 'runId' => $runId,
58 ]);
60​
61function text_message_start(string $messageId): string
63 return sse_event([
64 'type' => 'TEXT_MESSAGE_START',
65 'messageId' => $messageId,
66 'role' => 'assistant',
67 ]);
69​
70function text_message_content(string $messageId, string $delta): string
72 return sse_event([
73 'type' => 'TEXT_MESSAGE_CONTENT',
74 'messageId' => $messageId,
75 'delta' => $delta,
76 ]);
78​
79function text_message_end(string $messageId): string
81 return sse_event([
82 'type' => 'TEXT_MESSAGE_END',
83 'messageId' => $messageId,
84 ]);
86​
87function run_finished(string $threadId, string $runId): string
89 return sse_event([
90 'type' => 'RUN_FINISHED',
91 'threadId' => $threadId,
92 'runId' => $runId,
93 'finishReason' => 'stop',
94 ]);
96​
97function run_error(string $threadId, string $runId, string $message): string
99 return sse_event([
100 'type' => 'RUN_ERROR',
101 'threadId' => $threadId,
102 'runId' => $runId,
103 'error' => ['message' => $message],
104 ]);
106​
107function begin_sse(): void
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);
115​
116function write_sse(string $frame): void
118 echo $frame;
119 if (ob_get_level() > 0) {
120 ob_flush();
121 }
122 flush();
124​
125function cors_preflight(): void
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);
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 }
162​
163function stream_error_only(string $threadId, string $runId, string $message): void
165 begin_sse();
166 write_sse(run_started($threadId, $runId));
167 write_sse(run_error($threadId, $runId, $message));
168 write_sse(sse_done());