1f6ef2e50SAndreas Gohr<?php 2f6ef2e50SAndreas Gohr 3f6ef2e50SAndreas Gohrnamespace dokuwiki\plugin\aichat; 4f6ef2e50SAndreas Gohr 5*30b9cbc7Ssplitbrainclass Chunk implements \JsonSerializable, \Stringable 6f6ef2e50SAndreas Gohr{ 7f6ef2e50SAndreas Gohr /** @var int */ 8f6ef2e50SAndreas Gohr protected $created; 9e33a1d7aSAndreas Gohr /** @var string */ 10e33a1d7aSAndreas Gohr protected $language; 11f6ef2e50SAndreas Gohr 12f6ef2e50SAndreas Gohr /** 13f6ef2e50SAndreas Gohr * @param string $page 14f6ef2e50SAndreas Gohr * @param int $id 15f6ef2e50SAndreas Gohr * @param string $text 16f6ef2e50SAndreas Gohr * @param float[] $embedding 17f6ef2e50SAndreas Gohr * @param int $created 18*30b9cbc7Ssplitbrain * @param int $score 19f6ef2e50SAndreas Gohr */ 20*30b9cbc7Ssplitbrain public function __construct(protected $page, protected $id, protected $text, protected $embedding, $lang = '', $created = '', protected $score = 0) 21f6ef2e50SAndreas Gohr { 22e33a1d7aSAndreas Gohr $this->language = $lang ?: $this->determineLanguage(); 239b3d1b36SAndreas Gohr $this->created = $created ?: time(); 24f6ef2e50SAndreas Gohr } 25f6ef2e50SAndreas Gohr 26*30b9cbc7Ssplitbrain public function __toString(): string 2701f06932SAndreas Gohr { 2801f06932SAndreas Gohr return $this->page . '#' . $this->id; 2901f06932SAndreas Gohr } 3001f06932SAndreas Gohr 31f6ef2e50SAndreas Gohr /** 32f6ef2e50SAndreas Gohr * @return int 33f6ef2e50SAndreas Gohr */ 34f6ef2e50SAndreas Gohr public function getId() 35f6ef2e50SAndreas Gohr { 36f6ef2e50SAndreas Gohr return $this->id; 37f6ef2e50SAndreas Gohr } 38f6ef2e50SAndreas Gohr 39f6ef2e50SAndreas Gohr /** 40f6ef2e50SAndreas Gohr * @param int $id 41f6ef2e50SAndreas Gohr */ 42f6ef2e50SAndreas Gohr public function setId($id): void 43f6ef2e50SAndreas Gohr { 44f6ef2e50SAndreas Gohr $this->id = $id; 45f6ef2e50SAndreas Gohr } 46f6ef2e50SAndreas Gohr 47f6ef2e50SAndreas Gohr /** 48f6ef2e50SAndreas Gohr * @return string 49f6ef2e50SAndreas Gohr */ 50f6ef2e50SAndreas Gohr public function getPage() 51f6ef2e50SAndreas Gohr { 52f6ef2e50SAndreas Gohr return $this->page; 53f6ef2e50SAndreas Gohr } 54f6ef2e50SAndreas Gohr 55f6ef2e50SAndreas Gohr /** 56f6ef2e50SAndreas Gohr * @param string $page 57f6ef2e50SAndreas Gohr */ 58f6ef2e50SAndreas Gohr public function setPage($page): void 59f6ef2e50SAndreas Gohr { 60f6ef2e50SAndreas Gohr $this->page = $page; 61f6ef2e50SAndreas Gohr } 62f6ef2e50SAndreas Gohr 63f6ef2e50SAndreas Gohr /** 64f6ef2e50SAndreas Gohr * @return string 65f6ef2e50SAndreas Gohr */ 66f6ef2e50SAndreas Gohr public function getText() 67f6ef2e50SAndreas Gohr { 68f6ef2e50SAndreas Gohr return $this->text; 69f6ef2e50SAndreas Gohr } 70f6ef2e50SAndreas Gohr 71f6ef2e50SAndreas Gohr /** 72f6ef2e50SAndreas Gohr * @param string $text 73f6ef2e50SAndreas Gohr */ 74f6ef2e50SAndreas Gohr public function setText($text): void 75f6ef2e50SAndreas Gohr { 76f6ef2e50SAndreas Gohr $this->text = $text; 77f6ef2e50SAndreas Gohr } 78f6ef2e50SAndreas Gohr 79f6ef2e50SAndreas Gohr /** 80f6ef2e50SAndreas Gohr * @return float[] 81f6ef2e50SAndreas Gohr */ 82f6ef2e50SAndreas Gohr public function getEmbedding() 83f6ef2e50SAndreas Gohr { 84f6ef2e50SAndreas Gohr return $this->embedding; 85f6ef2e50SAndreas Gohr } 86f6ef2e50SAndreas Gohr 87f6ef2e50SAndreas Gohr /** 88f6ef2e50SAndreas Gohr * @param float[] $embedding 89f6ef2e50SAndreas Gohr */ 90f6ef2e50SAndreas Gohr public function setEmbedding($embedding): void 91f6ef2e50SAndreas Gohr { 92f6ef2e50SAndreas Gohr $this->embedding = $embedding; 93f6ef2e50SAndreas Gohr } 94f6ef2e50SAndreas Gohr 95f6ef2e50SAndreas Gohr /** 96f6ef2e50SAndreas Gohr * @return int 97f6ef2e50SAndreas Gohr */ 98f6ef2e50SAndreas Gohr public function getCreated() 99f6ef2e50SAndreas Gohr { 100f6ef2e50SAndreas Gohr return $this->created; 101f6ef2e50SAndreas Gohr } 102f6ef2e50SAndreas Gohr 103f6ef2e50SAndreas Gohr /** 104f6ef2e50SAndreas Gohr * @param int $created 105f6ef2e50SAndreas Gohr */ 106f6ef2e50SAndreas Gohr public function setCreated($created): void 107f6ef2e50SAndreas Gohr { 108f6ef2e50SAndreas Gohr $this->created = $created; 109f6ef2e50SAndreas Gohr } 110f6ef2e50SAndreas Gohr 111f6ef2e50SAndreas Gohr /** 1129b3d1b36SAndreas Gohr * @return int 1139b3d1b36SAndreas Gohr */ 1149b3d1b36SAndreas Gohr public function getScore() 1159b3d1b36SAndreas Gohr { 1169b3d1b36SAndreas Gohr return $this->score; 1179b3d1b36SAndreas Gohr } 1189b3d1b36SAndreas Gohr 1199b3d1b36SAndreas Gohr /** 1209b3d1b36SAndreas Gohr * @param int 1219b3d1b36SAndreas Gohr */ 1229b3d1b36SAndreas Gohr public function setScore($score): void 1239b3d1b36SAndreas Gohr { 1249b3d1b36SAndreas Gohr $this->score = $score; 1259b3d1b36SAndreas Gohr } 1269b3d1b36SAndreas Gohr 127e33a1d7aSAndreas Gohr public function getLanguage(): string 128e33a1d7aSAndreas Gohr { 129e33a1d7aSAndreas Gohr return $this->language; 130e33a1d7aSAndreas Gohr } 131e33a1d7aSAndreas Gohr 132e33a1d7aSAndreas Gohr /** 133e33a1d7aSAndreas Gohr * @param string $language 134e33a1d7aSAndreas Gohr */ 135e33a1d7aSAndreas Gohr public function setLanguage($language): void 136e33a1d7aSAndreas Gohr { 137e33a1d7aSAndreas Gohr $this->language = $language; 138e33a1d7aSAndreas Gohr } 139e33a1d7aSAndreas Gohr 140e33a1d7aSAndreas Gohr /** 141e33a1d7aSAndreas Gohr * Initialize the language of the chunk 142e33a1d7aSAndreas Gohr * 143e33a1d7aSAndreas Gohr * When the translation plugin is available it is used to determine the language, otherwise the default language 144e33a1d7aSAndreas Gohr * is used. 145e33a1d7aSAndreas Gohr * 146e33a1d7aSAndreas Gohr * @return string The lanuaage code 147e33a1d7aSAndreas Gohr */ 148e33a1d7aSAndreas Gohr protected function determineLanguage() 149e33a1d7aSAndreas Gohr { 150e33a1d7aSAndreas Gohr global $conf; 151e33a1d7aSAndreas Gohr /** @var \helper_plugin_translation $trans */ 152e33a1d7aSAndreas Gohr $trans = plugin_load('helper', 'translation'); 153e33a1d7aSAndreas Gohr if ($trans) { 154e33a1d7aSAndreas Gohr $lc = $trans->realLC($trans->getLangPart($this->page)); 155e33a1d7aSAndreas Gohr } else { 156e33a1d7aSAndreas Gohr $lc = $conf['lang']; 157e33a1d7aSAndreas Gohr } 158e33a1d7aSAndreas Gohr return $lc; 159e33a1d7aSAndreas Gohr } 1609b3d1b36SAndreas Gohr 1619b3d1b36SAndreas Gohr 1629b3d1b36SAndreas Gohr /** 163f6ef2e50SAndreas Gohr * Create a Chunk from a JSON string 164f6ef2e50SAndreas Gohr * 165f6ef2e50SAndreas Gohr * @param string $json 166f6ef2e50SAndreas Gohr * @return Chunk 167f6ef2e50SAndreas Gohr */ 1687ebc7895Ssplitbrain public static function fromJSON($json) 169f6ef2e50SAndreas Gohr { 170*30b9cbc7Ssplitbrain $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 171f6ef2e50SAndreas Gohr return new self( 172f6ef2e50SAndreas Gohr $data['page'], 173f6ef2e50SAndreas Gohr $data['id'], 174f6ef2e50SAndreas Gohr $data['text'], 175f6ef2e50SAndreas Gohr $data['embedding'], 176e33a1d7aSAndreas Gohr $data['language'] ?? '', 177f6ef2e50SAndreas Gohr $data['created'] 178f6ef2e50SAndreas Gohr ); 179f6ef2e50SAndreas Gohr } 180f6ef2e50SAndreas Gohr 181f6ef2e50SAndreas Gohr /** @inheritdoc */ 182f6ef2e50SAndreas Gohr public function jsonSerialize() 183f6ef2e50SAndreas Gohr { 184f6ef2e50SAndreas Gohr return [ 185f6ef2e50SAndreas Gohr 'page' => $this->page, 186f6ef2e50SAndreas Gohr 'id' => $this->id, 187f6ef2e50SAndreas Gohr 'text' => $this->text, 188f6ef2e50SAndreas Gohr 'embedding' => $this->embedding, 189e33a1d7aSAndreas Gohr 'language' => $this->language, 190f6ef2e50SAndreas Gohr 'created' => $this->created, 191f6ef2e50SAndreas Gohr ]; 192f6ef2e50SAndreas Gohr } 193f6ef2e50SAndreas Gohr} 194