xref: /plugin/aichat/action.php (revision 0337f47f2dd6cc31bdbfd81a2499e687ce633b47)
1*0337f47fSAndreas Gohr<?php
2*0337f47fSAndreas Gohr
3*0337f47fSAndreas Gohr/**
4*0337f47fSAndreas Gohr * DokuWiki Plugin aichat (Action Component)
5*0337f47fSAndreas Gohr *
6*0337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7*0337f47fSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
8*0337f47fSAndreas Gohr */
9*0337f47fSAndreas Gohrclass action_plugin_aichat extends \dokuwiki\Extension\ActionPlugin
10*0337f47fSAndreas Gohr{
11*0337f47fSAndreas Gohr
12*0337f47fSAndreas Gohr    /** @inheritDoc */
13*0337f47fSAndreas Gohr    public function register(Doku_Event_Handler $controller)
14*0337f47fSAndreas Gohr    {
15*0337f47fSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleQuestion');
16*0337f47fSAndreas Gohr
17*0337f47fSAndreas Gohr    }
18*0337f47fSAndreas Gohr
19*0337f47fSAndreas Gohr
20*0337f47fSAndreas Gohr    /**
21*0337f47fSAndreas Gohr     * Event handler for AJAX_CALL_UNKNOWN event
22*0337f47fSAndreas Gohr     *
23*0337f47fSAndreas Gohr     * @see https://www.dokuwiki.org/devel:events:ajax_call_unknown
24*0337f47fSAndreas Gohr     * @param Doku_Event $event Event object
25*0337f47fSAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
26*0337f47fSAndreas Gohr     * @return void
27*0337f47fSAndreas Gohr     */
28*0337f47fSAndreas Gohr    public function handleQuestion(Doku_Event $event, $param)
29*0337f47fSAndreas Gohr    {
30*0337f47fSAndreas Gohr        if ($event->data !== 'aichat') return;
31*0337f47fSAndreas Gohr        $event->preventDefault();
32*0337f47fSAndreas Gohr        $event->stopPropagation();
33*0337f47fSAndreas Gohr        global $INPUT;
34*0337f47fSAndreas Gohr
35*0337f47fSAndreas Gohr        $question = $INPUT->post->str('question');
36*0337f47fSAndreas Gohr        $history = json_decode($INPUT->post->str('history'));
37*0337f47fSAndreas Gohr
38*0337f47fSAndreas Gohr        /** @var helper_plugin_aichat $helper */
39*0337f47fSAndreas Gohr        $helper = plugin_load('helper', 'aichat');
40*0337f47fSAndreas Gohr
41*0337f47fSAndreas Gohr        $result = $helper->askChatQuestion($question, $history);
42*0337f47fSAndreas Gohr
43*0337f47fSAndreas Gohr        $sources = [];
44*0337f47fSAndreas Gohr        foreach ($result['sources'] as $source) {
45*0337f47fSAndreas Gohr            $sources[wl($source['meta']['pageid'])] = p_get_first_heading($source['meta']['pageid']) ?:
46*0337f47fSAndreas Gohr                $source['meta']['pageid'];
47*0337f47fSAndreas Gohr        }
48*0337f47fSAndreas Gohr
49*0337f47fSAndreas Gohr        header('Content-Type: application/json');
50*0337f47fSAndreas Gohr        echo json_encode([
51*0337f47fSAndreas Gohr            'question' => $result['question'],
52*0337f47fSAndreas Gohr            'answer' => $result['answer'],
53*0337f47fSAndreas Gohr            'sources' => $sources,
54*0337f47fSAndreas Gohr        ]);
55*0337f47fSAndreas Gohr    }
56*0337f47fSAndreas Gohr
57*0337f47fSAndreas Gohr}
58*0337f47fSAndreas Gohr
59