11<?php
22
3+ use dokuwiki \ErrorHandler ;
34use dokuwiki \Extension \ActionPlugin ;
4- use dokuwiki \Extension \EventHandler ;
55use dokuwiki \Extension \Event ;
6+ use dokuwiki \Extension \EventHandler ;
67use dokuwiki \Logger ;
7- use dokuwiki \ErrorHandler ;
88use dokuwiki \plugin \aichat \Chunk ;
99
1010/**
@@ -41,7 +41,7 @@ public function handleQuestion(Event $event, mixed $param)
4141 $ helper = plugin_load ('helper ' , 'aichat ' );
4242
4343 $ question = $ INPUT ->post ->str ('question ' );
44- $ history = json_decode ((string ) $ INPUT ->post ->str ('history ' ), null , 512 , JSON_THROW_ON_ERROR );
44+ $ history = json_decode ((string )$ INPUT ->post ->str ('history ' ), null , 512 , JSON_THROW_ON_ERROR );
4545 header ('Content-Type: application/json ' );
4646
4747 if (!$ helper ->userMayAccess ()) {
@@ -58,15 +58,21 @@ public function handleQuestion(Event $event, mixed $param)
5858 $ sources = [];
5959 foreach ($ result ['sources ' ] as $ source ) {
6060 /** @var Chunk $source */
61- $ sources [wl ($ source ->getPage ())] = p_get_first_heading ($ source ->getPage ()) ?: $ source ->getPage ();
61+ if (isset ($ sources [$ source ->getPage ()])) continue ; // only show the first occurrence per page
62+ $ sources [$ source ->getPage ()] = [
63+ 'page ' => $ source ->getPage (),
64+ 'url ' => wl ($ source ->getPage ()),
65+ 'title ' => p_get_first_heading ($ source ->getPage ()) ?: $ source ->getPage (),
66+ 'score ' => sprintf ("%.2f%% " , $ source ->getScore ()*100 ),
67+ ];
6268 }
6369 $ parseDown = new Parsedown ();
6470 $ parseDown ->setSafeMode (true );
6571
6672 echo json_encode ([
6773 'question ' => $ result ['question ' ],
6874 'answer ' => $ parseDown ->text ($ result ['answer ' ]),
69- 'sources ' => $ sources ,
75+ 'sources ' => array_values ( $ sources) ,
7076 ], JSON_THROW_ON_ERROR );
7177
7278 if ($ this ->getConf ('logging ' )) {
0 commit comments