Skip to content

Commit 782a5b0

Browse files
committed
clean up
1 parent 979ad67 commit 782a5b0

File tree

14 files changed

+148
-143
lines changed

14 files changed

+148
-143
lines changed

blueprints/fields/og-group.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ fields:
2323
ogDescription:
2424
label: seo.fields.ogDescription.label
2525
type: seo-writer
26-
inline: true
27-
marks: false
28-
nodes: false
26+
ai: og-description
2927
placeholder: "{{ page.metadata.ogDescription }}"
3028
ogImage:
3129
label: seo.fields.ogImage.label

blueprints/site.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ columns:
1616
metaDescription:
1717
label: seo.fields.metaDescription.label
1818
type: seo-writer
19-
inline: true
20-
marks: false
21-
nodes: false
19+
ai: site-description
2220
help: seo.fields.metaDescription.help
2321
_seoLine1:
2422
type: line
@@ -36,9 +34,7 @@ columns:
3634
ogDescription:
3735
label: seo.fields.ogDescription.label
3836
type: seo-writer
39-
inline: true
40-
marks: false
41-
nodes: false
37+
ai: site-og-description
4238
placeholder: "{{ site.metaDescription }}"
4339
ogSiteName:
4440
label: seo.fields.ogSiteName.label

classes/Ai.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace tobimori\Seo;
44

55
use Generator;
6-
use Kirby\Cms\App;
7-
use Kirby\Cms\Site;
86
use Kirby\Exception\Exception as KirbyException;
97
use tobimori\Seo\Ai\Driver;
108

@@ -25,7 +23,7 @@ public static function enabled(): bool
2523
*/
2624
public static function provider(string|null $providerId = null): Driver
2725
{
28-
$providerId ??= Seo::option('ai.defaultProvider');
26+
$providerId ??= Seo::option('ai.provider');
2927

3028
if (isset(self::$providers[$providerId])) {
3129
return self::$providers[$providerId];
@@ -48,33 +46,15 @@ public static function provider(string|null $providerId = null): Driver
4846
return self::$providers[$providerId] = new $driver($config['config'] ?? []);
4947
}
5048

51-
public static function streamTask(string $taskId, array $variables = [], array $context = []): Generator
52-
{
53-
$prompt = self::renderPrompt($taskId, $variables);
54-
$providerId = $task['provider'] ?? null;
55-
56-
return self::provider($providerId)->stream($prompt);
57-
}
58-
59-
private static function renderPrompt(string $taskId, $model, ?string $lang): string
49+
public static function streamTask(string $taskId, array $variables = []): Generator
6050
{
6151
$snippet = "seo/prompts/tasks/{$taskId}";
62-
63-
$page = $model instanceof Site ? $model->homePage() : $model;
64-
App::instance()->site()->visit($page, $lang);
65-
App::instance()->data = [
66-
'page' => $page,
67-
'site' => App::instance()->site(),
68-
'kirby' => App::instance(),
69-
];
70-
$content = snippet($snippet, [], return: true);
71-
72-
$content = trim($content ?? '');
73-
74-
if ($content !== '') {
75-
return $content;
52+
$instructions = trim(snippet("seo/prompts/system", $variables, return: true));
53+
$prompt = trim(snippet($snippet, $variables, return: true));
54+
if ($instructions === '' || $prompt === '') {
55+
throw new KirbyException("AI prompt snippet \"{$snippet}\" is missing or empty.");
7656
}
7757

78-
throw new KirbyException("AI prompt snippet \"{$snippet}\" is missing or empty.");
58+
return self::provider()->stream($prompt, $instructions, /* todo custom model here */);
7959
}
8060
}

classes/Ai/Drivers/OpenAi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function stream(
2626
'Accept: text/event-stream',
2727
"Authorization: Bearer {$apiKey}",
2828
];
29-
3029
if ($organization = $this->config('organization')) {
3130
$headers[] = "OpenAI-Organization: {$organization}";
3231
}

config/fields.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Kirby\Cms\App;
4+
use Kirby\Cms\Page;
35
use Kirby\Http\Response;
46
use tobimori\Seo\Ai;
57

@@ -15,6 +17,11 @@
1517
return false;
1618
}
1719

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+
1825
return $ai;
1926
},
2027

@@ -29,45 +36,41 @@
2936
'pattern' => 'ai/stream',
3037
'method' => 'POST',
3138
'action' => function () {
39+
$kirby = $this->kirby();
40+
3241
if (!Ai::enabled()) {
3342
return Response::json([
3443
'status' => 'error',
35-
'message' => t('seo.ai.error.disabled', 'AI features are disabled.')
44+
'message' => t('seo.ai.error.disabled')
3645
], 404);
3746
}
3847

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) {
4649
return Response::json([
4750
'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);
5753
}
5854

59-
if (!is_array($context)) {
60-
$context = [];
61-
}
55+
$data = $kirby->request()->body()->data();
56+
$lang = $data['lang'];
6257

58+
// for site, use homepage
59+
$model = $this->field()->model();
60+
$page = $model instanceof Page ? $model : $model->homePage();
61+
$kirby->site()->visit($page, $lang);
6362
if ($lang) {
64-
kirby()->setCurrentLanguage($lang);
63+
$kirby->setCurrentLanguage($lang);
6564
}
6665

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+
];
7072

73+
// begin streaming thingy
7174
ignore_user_abort(true);
7275
@set_time_limit(0);
7376

@@ -97,10 +100,9 @@
97100

98101
try {
99102
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
104106
]) as $chunk
105107
) {
106108
$send([
@@ -109,8 +111,6 @@
109111
'payload' => $chunk->payload,
110112
]);
111113
}
112-
113-
$send(['type' => 'stream_end']);
114114
} catch (\Throwable $exception) {
115115
$send([
116116
'type' => 'error',

config/options/ai.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
// TODO: custom provider per task
66
return [
77
'enabled' => true,
8-
9-
'defaultProvider' => 'openai',
10-
8+
'provider' => 'openai',
119
'providers' => [
1210
'openai' => [
1311
'driver' => OpenAi::class,

0 commit comments

Comments
 (0)