10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 3bddd899cSAndreas Gohruse dokuwiki\ErrorHandler; 4bddd899cSAndreas Gohruse dokuwiki\plugin\aichat\backend\Chunk; 5bddd899cSAndreas Gohr 60337f47fSAndreas Gohr/** 70337f47fSAndreas Gohr * DokuWiki Plugin aichat (Action Component) 80337f47fSAndreas Gohr * 90337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 100337f47fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 110337f47fSAndreas Gohr */ 120337f47fSAndreas Gohrclass action_plugin_aichat extends \dokuwiki\Extension\ActionPlugin 130337f47fSAndreas Gohr{ 140337f47fSAndreas Gohr 150337f47fSAndreas Gohr /** @inheritDoc */ 160337f47fSAndreas Gohr public function register(Doku_Event_Handler $controller) 170337f47fSAndreas Gohr { 180337f47fSAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleQuestion'); 190337f47fSAndreas Gohr 200337f47fSAndreas Gohr } 210337f47fSAndreas Gohr 220337f47fSAndreas Gohr 230337f47fSAndreas Gohr /** 240337f47fSAndreas Gohr * Event handler for AJAX_CALL_UNKNOWN event 250337f47fSAndreas Gohr * 260337f47fSAndreas Gohr * @see https://www.dokuwiki.org/devel:events:ajax_call_unknown 270337f47fSAndreas Gohr * @param Doku_Event $event Event object 280337f47fSAndreas Gohr * @param mixed $param optional parameter passed when event was registered 290337f47fSAndreas Gohr * @return void 300337f47fSAndreas Gohr */ 310337f47fSAndreas Gohr public function handleQuestion(Doku_Event $event, $param) 320337f47fSAndreas Gohr { 330337f47fSAndreas Gohr if ($event->data !== 'aichat') return; 340337f47fSAndreas Gohr $event->preventDefault(); 350337f47fSAndreas Gohr $event->stopPropagation(); 360337f47fSAndreas Gohr global $INPUT; 370337f47fSAndreas Gohr 380337f47fSAndreas Gohr $question = $INPUT->post->str('question'); 390337f47fSAndreas Gohr $history = json_decode($INPUT->post->str('history')); 40bddd899cSAndreas Gohr header('Content-Type: application/json'); 410337f47fSAndreas Gohr 42bddd899cSAndreas Gohr try { 430337f47fSAndreas Gohr /** @var helper_plugin_aichat $helper */ 440337f47fSAndreas Gohr $helper = plugin_load('helper', 'aichat'); 450337f47fSAndreas Gohr $result = $helper->askChatQuestion($question, $history); 460337f47fSAndreas Gohr $sources = []; 470337f47fSAndreas Gohr foreach ($result['sources'] as $source) { 48bddd899cSAndreas Gohr /** @var Chunk $source */ 49bddd899cSAndreas Gohr $sources[wl($source->getPage())] = p_get_first_heading($source->getPage()) ?: $source->getPage(); 500337f47fSAndreas Gohr } 510337f47fSAndreas Gohr echo json_encode([ 520337f47fSAndreas Gohr 'question' => $result['question'], 530337f47fSAndreas Gohr 'answer' => $result['answer'], 540337f47fSAndreas Gohr 'sources' => $sources, 550337f47fSAndreas Gohr ]); 56*82d5855eSAndreas Gohr 57*82d5855eSAndreas Gohr if ($this->getConf('logging')) { 58*82d5855eSAndreas Gohr \dokuwiki\Logger::getInstance('aichat')->log( 59*82d5855eSAndreas Gohr $question, 60*82d5855eSAndreas Gohr [ 61*82d5855eSAndreas Gohr 'interpretation' => $result['question'], 62*82d5855eSAndreas Gohr 'answer' => $result['answer'], 63*82d5855eSAndreas Gohr 'sources' => $sources, 64*82d5855eSAndreas Gohr 'ip' => $INPUT->server->str('REMOTE_ADDR'), 65*82d5855eSAndreas Gohr 'user' => $INPUT->server->str('REMOTE_USER'), 66*82d5855eSAndreas Gohr 'stats' => $helper->getOpenAI()->getUsageStats() 67*82d5855eSAndreas Gohr ] 68*82d5855eSAndreas Gohr ); 69*82d5855eSAndreas Gohr } 70bddd899cSAndreas Gohr } catch (\Exception $e) { 71bddd899cSAndreas Gohr ErrorHandler::logException($e); 72bddd899cSAndreas Gohr echo json_encode([ 73bddd899cSAndreas Gohr 'question' => $question, 74bddd899cSAndreas Gohr 'answer' => 'An error occurred. More info may be available in the error log. ' . $e->getMessage(), 75bddd899cSAndreas Gohr 'sources' => [], 76bddd899cSAndreas Gohr ]); 77bddd899cSAndreas Gohr } 780337f47fSAndreas Gohr } 790337f47fSAndreas Gohr 800337f47fSAndreas Gohr} 810337f47fSAndreas Gohr 82