xref: /plugin/aichat/action.php (revision 30b9cbc7090f3bc7eca85292f4ff98a1cf513f8f)
1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\Logger;
7use dokuwiki\ErrorHandler;
8use dokuwiki\plugin\aichat\Chunk;
9
10/**
11 * DokuWiki Plugin aichat (Action Component)
12 *
13 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
14 * @author  Andreas Gohr <gohr@cosmocode.de>
15 */
16class action_plugin_aichat extends ActionPlugin
17{
18    /** @inheritDoc */
19    public function register(EventHandler $controller)
20    {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleQuestion');
22    }
23
24
25    /**
26     * Event handler for AJAX_CALL_UNKNOWN event
27     *
28     * @see https://www.dokuwiki.org/devel:events:ajax_call_unknown
29     * @param Doku_Event $event Event object
30     * @param mixed $param optional parameter passed when event was registered
31     * @return void
32     */
33    public function handleQuestion(Event $event, mixed $param)
34    {
35        if ($event->data !== 'aichat') return;
36        $event->preventDefault();
37        $event->stopPropagation();
38        global $INPUT;
39
40        /** @var helper_plugin_aichat $helper */
41        $helper = plugin_load('helper', 'aichat');
42
43        $question = $INPUT->post->str('question');
44        $history = json_decode((string) $INPUT->post->str('history'), null, 512, JSON_THROW_ON_ERROR);
45        header('Content-Type: application/json');
46
47        if (!$helper->userMayAccess()) {
48            echo json_encode([
49                'question' => $question,
50                'answer' => $this->getLang('restricted'),
51                'sources' => [],
52            ], JSON_THROW_ON_ERROR);
53            return;
54        }
55
56        try {
57            $result = $helper->askChatQuestion($question, $history);
58            $sources = [];
59            foreach ($result['sources'] as $source) {
60                /** @var Chunk $source */
61                $sources[wl($source->getPage())] = p_get_first_heading($source->getPage()) ?: $source->getPage();
62            }
63            $parseDown = new Parsedown();
64            $parseDown->setSafeMode(true);
65
66            echo json_encode([
67                'question' => $result['question'],
68                'answer' => $parseDown->text($result['answer']),
69                'sources' => $sources,
70            ], JSON_THROW_ON_ERROR);
71
72            if ($this->getConf('logging')) {
73                Logger::getInstance('aichat')->log(
74                    $question,
75                    [
76                        'interpretation' => $result['question'],
77                        'answer' => $result['answer'],
78                        'sources' => $sources,
79                        'ip' => $INPUT->server->str('REMOTE_ADDR'),
80                        'user' => $INPUT->server->str('REMOTE_USER'),
81                        'stats' => $helper->getModel()->getUsageStats()
82                    ]
83                );
84            }
85        } catch (\Exception $e) {
86            ErrorHandler::logException($e);
87            echo json_encode([
88                'question' => $question,
89                'answer' => 'An error occurred. More info may be available in the error log. ' . $e->getMessage(),
90                'sources' => [],
91            ], JSON_THROW_ON_ERROR);
92        }
93    }
94}
95