1f6ef2e50SAndreas Gohr<?php 2f6ef2e50SAndreas Gohr 3f6ef2e50SAndreas Gohrnamespace dokuwiki\plugin\aichat; 4f6ef2e50SAndreas Gohr 530b9cbc7Ssplitbrainclass 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 1830b9cbc7Ssplitbrain * @param int $score 19f6ef2e50SAndreas Gohr */ 20441edf84SAndreas Gohr public function __construct( 21441edf84SAndreas Gohr protected $page, 22441edf84SAndreas Gohr protected $id, 23441edf84SAndreas Gohr protected $text, 24441edf84SAndreas Gohr protected $embedding, 25441edf84SAndreas Gohr $lang = '', 26441edf84SAndreas Gohr $created = '', 27441edf84SAndreas Gohr protected $score = 0 28*962f9fb0Ssplitbrain ) { 29e33a1d7aSAndreas Gohr $this->language = $lang ?: $this->determineLanguage(); 309b3d1b36SAndreas Gohr $this->created = $created ?: time(); 31f6ef2e50SAndreas Gohr } 32f6ef2e50SAndreas Gohr 3330b9cbc7Ssplitbrain public function __toString(): string 3401f06932SAndreas Gohr { 3501f06932SAndreas Gohr return $this->page . '#' . $this->id; 3601f06932SAndreas Gohr } 3701f06932SAndreas Gohr 38f6ef2e50SAndreas Gohr /** 39f6ef2e50SAndreas Gohr * @return int 40f6ef2e50SAndreas Gohr */ 41f6ef2e50SAndreas Gohr public function getId() 42f6ef2e50SAndreas Gohr { 43f6ef2e50SAndreas Gohr return $this->id; 44f6ef2e50SAndreas Gohr } 45f6ef2e50SAndreas Gohr 46f6ef2e50SAndreas Gohr /** 47f6ef2e50SAndreas Gohr * @param int $id 48f6ef2e50SAndreas Gohr */ 49f6ef2e50SAndreas Gohr public function setId($id): void 50f6ef2e50SAndreas Gohr { 51f6ef2e50SAndreas Gohr $this->id = $id; 52f6ef2e50SAndreas Gohr } 53f6ef2e50SAndreas Gohr 54f6ef2e50SAndreas Gohr /** 55f6ef2e50SAndreas Gohr * @return string 56f6ef2e50SAndreas Gohr */ 57f6ef2e50SAndreas Gohr public function getPage() 58f6ef2e50SAndreas Gohr { 59f6ef2e50SAndreas Gohr return $this->page; 60f6ef2e50SAndreas Gohr } 61f6ef2e50SAndreas Gohr 62f6ef2e50SAndreas Gohr /** 63f6ef2e50SAndreas Gohr * @param string $page 64f6ef2e50SAndreas Gohr */ 65f6ef2e50SAndreas Gohr public function setPage($page): void 66f6ef2e50SAndreas Gohr { 67f6ef2e50SAndreas Gohr $this->page = $page; 68f6ef2e50SAndreas Gohr } 69f6ef2e50SAndreas Gohr 70f6ef2e50SAndreas Gohr /** 71f6ef2e50SAndreas Gohr * @return string 72f6ef2e50SAndreas Gohr */ 73f6ef2e50SAndreas Gohr public function getText() 74f6ef2e50SAndreas Gohr { 75f6ef2e50SAndreas Gohr return $this->text; 76f6ef2e50SAndreas Gohr } 77f6ef2e50SAndreas Gohr 78f6ef2e50SAndreas Gohr /** 79f6ef2e50SAndreas Gohr * @param string $text 80f6ef2e50SAndreas Gohr */ 81f6ef2e50SAndreas Gohr public function setText($text): void 82f6ef2e50SAndreas Gohr { 83f6ef2e50SAndreas Gohr $this->text = $text; 84f6ef2e50SAndreas Gohr } 85f6ef2e50SAndreas Gohr 86f6ef2e50SAndreas Gohr /** 87f6ef2e50SAndreas Gohr * @return float[] 88f6ef2e50SAndreas Gohr */ 89f6ef2e50SAndreas Gohr public function getEmbedding() 90f6ef2e50SAndreas Gohr { 91f6ef2e50SAndreas Gohr return $this->embedding; 92f6ef2e50SAndreas Gohr } 93f6ef2e50SAndreas Gohr 94f6ef2e50SAndreas Gohr /** 95f6ef2e50SAndreas Gohr * @param float[] $embedding 96f6ef2e50SAndreas Gohr */ 97f6ef2e50SAndreas Gohr public function setEmbedding($embedding): void 98f6ef2e50SAndreas Gohr { 99f6ef2e50SAndreas Gohr $this->embedding = $embedding; 100f6ef2e50SAndreas Gohr } 101f6ef2e50SAndreas Gohr 102f6ef2e50SAndreas Gohr /** 103f6ef2e50SAndreas Gohr * @return int 104f6ef2e50SAndreas Gohr */ 105f6ef2e50SAndreas Gohr public function getCreated() 106f6ef2e50SAndreas Gohr { 107f6ef2e50SAndreas Gohr return $this->created; 108f6ef2e50SAndreas Gohr } 109f6ef2e50SAndreas Gohr 110f6ef2e50SAndreas Gohr /** 111f6ef2e50SAndreas Gohr * @param int $created 112f6ef2e50SAndreas Gohr */ 113f6ef2e50SAndreas Gohr public function setCreated($created): void 114f6ef2e50SAndreas Gohr { 115f6ef2e50SAndreas Gohr $this->created = $created; 116f6ef2e50SAndreas Gohr } 117f6ef2e50SAndreas Gohr 118f6ef2e50SAndreas Gohr /** 1199b3d1b36SAndreas Gohr * @return int 1209b3d1b36SAndreas Gohr */ 1219b3d1b36SAndreas Gohr public function getScore() 1229b3d1b36SAndreas Gohr { 1239b3d1b36SAndreas Gohr return $this->score; 1249b3d1b36SAndreas Gohr } 1259b3d1b36SAndreas Gohr 1269b3d1b36SAndreas Gohr /** 1279b3d1b36SAndreas Gohr * @param int 1289b3d1b36SAndreas Gohr */ 1299b3d1b36SAndreas Gohr public function setScore($score): void 1309b3d1b36SAndreas Gohr { 1319b3d1b36SAndreas Gohr $this->score = $score; 1329b3d1b36SAndreas Gohr } 1339b3d1b36SAndreas Gohr 134e33a1d7aSAndreas Gohr public function getLanguage(): string 135e33a1d7aSAndreas Gohr { 136e33a1d7aSAndreas Gohr return $this->language; 137e33a1d7aSAndreas Gohr } 138e33a1d7aSAndreas Gohr 139e33a1d7aSAndreas Gohr /** 140e33a1d7aSAndreas Gohr * @param string $language 141e33a1d7aSAndreas Gohr */ 142e33a1d7aSAndreas Gohr public function setLanguage($language): void 143e33a1d7aSAndreas Gohr { 144e33a1d7aSAndreas Gohr $this->language = $language; 145e33a1d7aSAndreas Gohr } 146e33a1d7aSAndreas Gohr 147e33a1d7aSAndreas Gohr /** 148e33a1d7aSAndreas Gohr * Initialize the language of the chunk 149e33a1d7aSAndreas Gohr * 150e33a1d7aSAndreas Gohr * When the translation plugin is available it is used to determine the language, otherwise the default language 151e33a1d7aSAndreas Gohr * is used. 152e33a1d7aSAndreas Gohr * 153e33a1d7aSAndreas Gohr * @return string The lanuaage code 154e33a1d7aSAndreas Gohr */ 155e33a1d7aSAndreas Gohr protected function determineLanguage() 156e33a1d7aSAndreas Gohr { 157e33a1d7aSAndreas Gohr global $conf; 158e33a1d7aSAndreas Gohr /** @var \helper_plugin_translation $trans */ 159e33a1d7aSAndreas Gohr $trans = plugin_load('helper', 'translation'); 160e33a1d7aSAndreas Gohr if ($trans) { 161e33a1d7aSAndreas Gohr $lc = $trans->realLC($trans->getLangPart($this->page)); 162e33a1d7aSAndreas Gohr } else { 163e33a1d7aSAndreas Gohr $lc = $conf['lang']; 164e33a1d7aSAndreas Gohr } 165e33a1d7aSAndreas Gohr return $lc; 166e33a1d7aSAndreas Gohr } 1679b3d1b36SAndreas Gohr 1689b3d1b36SAndreas Gohr 1699b3d1b36SAndreas Gohr /** 170f6ef2e50SAndreas Gohr * Create a Chunk from a JSON string 171f6ef2e50SAndreas Gohr * 172f6ef2e50SAndreas Gohr * @param string $json 173f6ef2e50SAndreas Gohr * @return Chunk 174f6ef2e50SAndreas Gohr */ 1757ebc7895Ssplitbrain public static function fromJSON($json) 176f6ef2e50SAndreas Gohr { 17730b9cbc7Ssplitbrain $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 178f6ef2e50SAndreas Gohr return new self( 179f6ef2e50SAndreas Gohr $data['page'], 180f6ef2e50SAndreas Gohr $data['id'], 181f6ef2e50SAndreas Gohr $data['text'], 182f6ef2e50SAndreas Gohr $data['embedding'], 183e33a1d7aSAndreas Gohr $data['language'] ?? '', 184f6ef2e50SAndreas Gohr $data['created'] 185f6ef2e50SAndreas Gohr ); 186f6ef2e50SAndreas Gohr } 187f6ef2e50SAndreas Gohr 188f6ef2e50SAndreas Gohr /** @inheritdoc */ 189f6ef2e50SAndreas Gohr public function jsonSerialize() 190f6ef2e50SAndreas Gohr { 191f6ef2e50SAndreas Gohr return [ 192f6ef2e50SAndreas Gohr 'page' => $this->page, 193f6ef2e50SAndreas Gohr 'id' => $this->id, 194f6ef2e50SAndreas Gohr 'text' => $this->text, 195f6ef2e50SAndreas Gohr 'embedding' => $this->embedding, 196e33a1d7aSAndreas Gohr 'language' => $this->language, 197f6ef2e50SAndreas Gohr 'created' => $this->created, 198f6ef2e50SAndreas Gohr ]; 199f6ef2e50SAndreas Gohr } 200f6ef2e50SAndreas Gohr} 201