Skip to content

Commit 87201da

Browse files
authored
Merge pull request #4 from ayacoo/v2
V2
2 parents ff40774 + b737dd3 commit 87201da

5 files changed

Lines changed: 38 additions & 57 deletions

File tree

Classes/Controller/SummarizeController.php

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
namespace Ayacoo\NewsTldr\Controller;
44

55
use Ayacoo\NewsTldr\Event\ModifyChatGptContentEvent;
6+
use OpenAI;
67
use Psr\EventDispatcher\EventDispatcherInterface;
78
use TYPO3\CMS\Backend\Utility\BackendUtility;
89
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
910
use TYPO3\CMS\Core\Http\JsonResponse;
10-
use TYPO3\CMS\Core\Http\RequestFactory;
1111
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
1212

1313
class SummarizeController
1414
{
1515
private const EXTENSION_KEY = 'news_tldr';
1616

1717
public function __construct(
18-
private readonly RequestFactory $requestFactory,
1918
private readonly ExtensionConfiguration $extensionConfiguration,
2019
private readonly EventDispatcherInterface $eventDispatcher
2120
)
@@ -46,49 +45,21 @@ public function updateTeaserAction(): JsonResponse
4645
new ModifyChatGptContentEvent($row, $content)
4746
);
4847

49-
$payload = [];
50-
$payload['model'] = $extConf['model'] ?? '';
51-
$messages = new \stdClass();
52-
$messages->role = 'user';
53-
$messages->content = $modifyChatGptContentEvent->getContent();
54-
$payload['messages'] = [$messages];
55-
56-
$additionalOptions = [
57-
'body' => json_encode($payload),
58-
'headers' => [
59-
'Authorization' => 'Bearer ' . $token,
60-
'Cache-Control' => 'no-cache',
61-
'Content-Type' => 'application/json'
48+
$client = OpenAI::client($token);
49+
$response = $client->chat()->create([
50+
'model' => $extConf['model'],
51+
'messages' => [
52+
[
53+
'role' => 'user',
54+
'content' => $modifyChatGptContentEvent->getContent()
55+
],
6256
],
63-
];
64-
$response = $this->requestFactory->request(
65-
'https://api.openai.com/v1/chat/completions',
66-
'POST',
67-
$additionalOptions
68-
);
57+
]);
6958

70-
if ($response->getStatusCode() !== 200) {
71-
throw new \RuntimeException(
72-
LocalizationUtility::translate('status_code_is', self::EXTENSION_KEY) . $response->getStatusCode()
73-
);
74-
}
75-
if ($response->getHeaderLine('Content-Type') !== 'application/json') {
76-
throw new \RuntimeException(
77-
LocalizationUtility::translate('no_valid_json', self::EXTENSION_KEY)
78-
);
79-
}
80-
$content = $response->getBody()->getContents();
81-
try {
82-
$result = json_decode($content, true, flags: JSON_THROW_ON_ERROR);
83-
return new JsonResponse([
84-
'text' => (string)$result['choices'][0]['message']['content'] ?? '',
85-
'success' => true
86-
]);
87-
} catch (\JsonException) {
88-
return new JsonResponse([
89-
'text' => LocalizationUtility::translate('no_valid_token', self::EXTENSION_KEY),
90-
'success' => false
91-
]);
92-
}
59+
$result = $response->toArray();
60+
return new JsonResponse([
61+
'text' => (string)$result['choices'][0]['message']['content'] ?? '',
62+
'success' => true
63+
]);
9364
}
9465
}

Classes/Form/Element/SummarizeFieldElement.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
namespace Ayacoo\NewsTldr\Form\Element;
55

66
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
7-
use TYPO3\CMS\Core\Imaging\Icon;
7+
use TYPO3\CMS\Backend\Form\NodeFactory;
8+
use TYPO3\CMS\Core\Imaging\IconFactory;
9+
use TYPO3\CMS\Core\Imaging\IconSize;
810
use TYPO3\CMS\Core\Page\PageRenderer;
911
use TYPO3\CMS\Core\Utility\GeneralUtility;
12+
use TYPO3\CMS\Core\Utility\StringUtility;
1013

1114
class SummarizeFieldElement extends AbstractFormElement
1215
{
@@ -22,18 +25,24 @@ public function render(): array
2225

2326
$row = $this->data['databaseRow'];
2427

28+
$nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
29+
$this->injectNodeFactory($nodeFactory);
2530
$fieldInformationResult = $this->renderFieldInformation();
2631
$resultArray = $this->mergeChildReturnIntoExistingResult($this->initializeResultArray(), $fieldInformationResult, false);
2732

33+
$fieldId = StringUtility::getUniqueId('formengine-textarea-');
34+
2835
$html = [];
36+
$html[] = $this->renderLabel($fieldId);
2937
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
3038
$html[] = '<div class="form-wizards-wrap">';
3139
$html[] = '<div class="form-wizards-element">';
3240
$html[] = '<div class="form-control-wrap">';
3341

34-
$icon = $this->iconFactory->getIcon(
42+
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
43+
$icon = $iconFactory->getIcon(
3544
'actions-dice',
36-
Icon::SIZE_SMALL,
45+
IconSize::SMALL,
3746
'overlay-identifier'
3847
);
3948

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ The extension makes it possible to create short news summaries using ChatGPT.
1717

1818
To use this extension, you need the following requirements:
1919

20-
- PHP version 8.1 or higher
21-
- TYPO3 version 12
20+
- PHP version 8.2 or higher
21+
- TYPO3 version 13
2222
- [News][3] Extension 11 or higher
2323
- [ChatGPT API Token][2] (Please note the number of tokens and costs)
2424

@@ -68,7 +68,7 @@ class ContentListener
6868
$row = $event->getRow()
6969
$text = strip_tags($row['bodytext'] ?? '');
7070
71-
$content = 'Fasse mir diesen Text in 100 Zeichen zusammen: ' . $text;
71+
$content = 'Summarise this text for me in 100 characters: ' . $text;
7272
$event->setContent($content);
7373
7474
return $event;

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ayacoo/news-tldr",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"type": "typo3-cms-extension",
55
"description": "Creates a short summary for news via ChatGPT",
66
"homepage": "https://www.ayacoo.de",
@@ -14,9 +14,10 @@
1414
"GPL-2.0-or-later"
1515
],
1616
"require": {
17-
"php": ">=8.1",
18-
"typo3/cms-core": "^12.4",
19-
"georgringer/news": "^11.0"
17+
"php": ">=8.2 < 8.4",
18+
"typo3/cms-core": "^13.4",
19+
"georgringer/news": "^12.0",
20+
"openai-php/client": "*"
2021
},
2122
"autoload": {
2223
"psr-4": {

ext_emconf.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
'category' => 'plugin',
77
'constraints' => [
88
'depends' => [
9-
'typo3' => '12.3.0-12.9.99',
10-
'news' => '11.0.0-11.9.99',
9+
'typo3' => '13.0.0-13.4.99',
10+
'news' => '12.0.0-12.9.99',
1111
],
1212
'conflicts' => [],
1313
'suggests' => [],
@@ -17,5 +17,5 @@
1717
'author' => 'Guido Schmechel',
1818
'author_email' => 'info@ayacoo.de',
1919
'author_company' => 'ayacoo',
20-
'version' => '1.0.1',
20+
'version' => '2.0.0',
2121
];

0 commit comments

Comments
 (0)