historyData = $history; } public function LogIsEmpty() { return $this->historyData == null || !isset($this->historyData->log) || count($this->historyData->log) == 0; } public function GetLog() { if($this->LogIsEmpty()) return []; return $this->historyData->log; } public function SetLog($log) { $history = $this->GetOrCreateHistory(); $history->log = $log; } public function GetOrCreateHistory() { if($this->historyData == null) $this->historyData = new \stdClass(); return $this->historyData; } public function RecordHistoryLog($message, $role = 'assistant') { $history = $this->GetOrCreateHistory(); if(!isset($history->log)) $history->log = []; $history->log[] = (object) [ 'role' => $role, 'content' => $message ]; return $history; } public function GetHistory() { return $this->historyData; } public function HasSystemMessage() { return $this->historyData != null && isset($this->historyData->log) && count($this->historyData->log) > 0 && $this->historyData->log[0]->role == 'system'; } public function GetAllMessagesOfRole($role = 'user') { return ArrayUtils::Filter($this->GetLog(), function ($item) use ($role) { return $item->role == $role; }); } }