|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Kirby\Cms\App; |
| 4 | +use Kirby\Cms\Page; |
3 | 5 | use Kirby\Http\Response; |
4 | 6 | use tobimori\Seo\Ai; |
5 | 7 |
|
|
15 | 17 | return false; |
16 | 18 | } |
17 | 19 |
|
| 20 | + // check ai permission @see index.php L31 |
| 21 | + if (App::instance()->user()->role()->permissions()->for('tobimori.seo', 'ai') === false) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
18 | 25 | return $ai; |
19 | 26 | }, |
20 | 27 |
|
|
29 | 36 | 'pattern' => 'ai/stream', |
30 | 37 | 'method' => 'POST', |
31 | 38 | 'action' => function () { |
| 39 | + $kirby = $this->kirby(); |
| 40 | + |
32 | 41 | if (!Ai::enabled()) { |
33 | 42 | return Response::json([ |
34 | 43 | 'status' => 'error', |
35 | | - 'message' => t('seo.ai.error.disabled', 'AI features are disabled.') |
| 44 | + 'message' => t('seo.ai.error.disabled') |
36 | 45 | ], 404); |
37 | 46 | } |
38 | 47 |
|
39 | | - $field = $this->field(); |
40 | | - $model = $field->model(); |
41 | | - $lang = $model?->kirby()->language()?->code(); |
42 | | - $data = kirby()->request()->body()->data(); |
43 | | - $taskId = $data['task'] ?? null; |
44 | | - |
45 | | - if (!is_string($taskId) || trim($taskId) === '') { |
| 48 | + if ($kirby->user()->role()->permissions()->for('tobimori.seo', 'ai') === false) { |
46 | 49 | return Response::json([ |
47 | 50 | 'status' => 'error', |
48 | | - 'message' => 'Missing AI task identifier.' |
49 | | - ], 400); |
50 | | - } |
51 | | - |
52 | | - $variables = $data['variables'] ?? []; |
53 | | - $context = $data['context'] ?? []; |
54 | | - |
55 | | - if (!is_array($variables)) { |
56 | | - $variables = []; |
| 51 | + 'message' => t('seo.ai.error.permission') |
| 52 | + ], 404); |
57 | 53 | } |
58 | 54 |
|
59 | | - if (!is_array($context)) { |
60 | | - $context = []; |
61 | | - } |
| 55 | + $data = $kirby->request()->body()->data(); |
| 56 | + $lang = $data['lang']; |
62 | 57 |
|
| 58 | + // for site, use homepage |
| 59 | + $model = $this->field()->model(); |
| 60 | + $page = $model instanceof Page ? $model : $model->homePage(); |
| 61 | + $kirby->site()->visit($page, $lang); |
63 | 62 | if ($lang) { |
64 | | - kirby()->setCurrentLanguage($lang); |
| 63 | + $kirby->setCurrentLanguage($lang); |
65 | 64 | } |
66 | 65 |
|
67 | | - if ($model) { |
68 | | - kirby()->site()->visit($model); |
69 | | - } |
| 66 | + // inject data in snippets / rendering process |
| 67 | + $kirby->data = [ // TODO: check if we want to access the draft / edited version for $page |
| 68 | + 'page' => $page, |
| 69 | + 'site' => $kirby->site(), |
| 70 | + 'kirby' => $kirby |
| 71 | + ]; |
70 | 72 |
|
| 73 | + // begin streaming thingy |
71 | 74 | ignore_user_abort(true); |
72 | 75 | @set_time_limit(0); |
73 | 76 |
|
|
97 | 100 |
|
98 | 101 | try { |
99 | 102 | foreach ( |
100 | | - Ai::streamTask($taskId, $variables, [ |
101 | | - ...$context, |
102 | | - 'model' => $model, |
103 | | - 'language' => $lang, |
| 103 | + Ai::streamTask($this->field()->ai(), [ |
| 104 | + 'instructions' => $data['instructions'] ?? null, |
| 105 | + 'edit' => $data['edit'] ?? null |
104 | 106 | ]) as $chunk |
105 | 107 | ) { |
106 | 108 | $send([ |
|
109 | 111 | 'payload' => $chunk->payload, |
110 | 112 | ]); |
111 | 113 | } |
112 | | - |
113 | | - $send(['type' => 'stream_end']); |
114 | 114 | } catch (\Throwable $exception) { |
115 | 115 | $send([ |
116 | 116 | 'type' => 'error', |
|
0 commit comments