xref: /plugin/aichat/helper.php (revision dce0dee5ef27bcbbc5570fc278f3e75f426c19c5)
10337f47fSAndreas Gohr<?php
20337f47fSAndreas Gohr
33379af09SAndreas Gohruse dokuwiki\Extension\CLIPlugin;
45e6dd16eSAndreas Gohruse dokuwiki\Extension\Plugin;
5e33a1d7aSAndreas Gohruse dokuwiki\plugin\aichat\AIChat;
6f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Chunk;
70337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings;
8294a9eafSAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface;
9294a9eafSAndreas Gohruse dokuwiki\plugin\aichat\Model\EmbeddingInterface;
1034a1c478SAndreas Gohruse dokuwiki\plugin\aichat\Model\OpenAI\Embedding3Small;
1101f06932SAndreas Gohruse dokuwiki\plugin\aichat\Storage\AbstractStorage;
125e6dd16eSAndreas Gohruse dokuwiki\plugin\aichat\Storage\ChromaStorage;
1313dbfc23SAndreas Gohruse dokuwiki\plugin\aichat\Storage\PineconeStorage;
144c0099a8SAndreas Gohruse dokuwiki\plugin\aichat\Storage\QdrantStorage;
15f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\SQLiteStorage;
160337f47fSAndreas Gohr
170337f47fSAndreas Gohr/**
180337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component)
190337f47fSAndreas Gohr *
200337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
210337f47fSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
220337f47fSAndreas Gohr */
237ebc7895Ssplitbrainclass helper_plugin_aichat extends Plugin
240337f47fSAndreas Gohr{
253379af09SAndreas Gohr    /** @var CLIPlugin $logger */
263379af09SAndreas Gohr    protected $logger;
27294a9eafSAndreas Gohr    /** @var ChatInterface */
286a18e0f4SAndreas Gohr    protected $chatModel;
29294a9eafSAndreas Gohr    /** @var EmbeddingInterface */
306a18e0f4SAndreas Gohr    protected $embedModel;
310337f47fSAndreas Gohr    /** @var Embeddings */
320337f47fSAndreas Gohr    protected $embeddings;
3301f06932SAndreas Gohr    /** @var AbstractStorage */
3401f06932SAndreas Gohr    protected $storage;
350337f47fSAndreas Gohr
36e75dc39fSAndreas Gohr    /** @var array where to store meta data on the last run */
37e75dc39fSAndreas Gohr    protected $runDataFile;
38e75dc39fSAndreas Gohr
390337f47fSAndreas Gohr    /**
40f8d5ae01SAndreas Gohr     * Constructor. Initializes vendor autoloader
41f8d5ae01SAndreas Gohr     */
42f8d5ae01SAndreas Gohr    public function __construct()
43f8d5ae01SAndreas Gohr    {
44e75dc39fSAndreas Gohr        require_once __DIR__ . '/vendor/autoload.php'; // FIXME obsolete from Kaos onwards
45e75dc39fSAndreas Gohr        global $conf;
46e75dc39fSAndreas Gohr        $this->runDataFile = $conf['metadir'] . '/aichat__run.json';
47d02b7935SAndreas Gohr        $this->loadConfig();
48f8d5ae01SAndreas Gohr    }
49f8d5ae01SAndreas Gohr
50f8d5ae01SAndreas Gohr    /**
513379af09SAndreas Gohr     * Use the given CLI plugin for logging
523379af09SAndreas Gohr     *
533379af09SAndreas Gohr     * @param CLIPlugin $logger
543379af09SAndreas Gohr     * @return void
553379af09SAndreas Gohr     */
568285fff9SAndreas Gohr    public function setLogger($logger)
578285fff9SAndreas Gohr    {
583379af09SAndreas Gohr        $this->logger = $logger;
593379af09SAndreas Gohr    }
603379af09SAndreas Gohr
613379af09SAndreas Gohr    /**
62c4127b8eSAndreas Gohr     * Check if the current user is allowed to use the plugin (if it has been restricted)
63c4127b8eSAndreas Gohr     *
64c4127b8eSAndreas Gohr     * @return bool
65c4127b8eSAndreas Gohr     */
66c4127b8eSAndreas Gohr    public function userMayAccess()
67c4127b8eSAndreas Gohr    {
68c4127b8eSAndreas Gohr        global $auth;
69c4127b8eSAndreas Gohr        global $USERINFO;
70c4127b8eSAndreas Gohr        global $INPUT;
71c4127b8eSAndreas Gohr
72c4127b8eSAndreas Gohr        if (!$auth) return true;
73c4127b8eSAndreas Gohr        if (!$this->getConf('restrict')) return true;
74c4127b8eSAndreas Gohr        if (!isset($USERINFO)) return false;
75c4127b8eSAndreas Gohr
76c4127b8eSAndreas Gohr        return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']);
77c4127b8eSAndreas Gohr    }
78c4127b8eSAndreas Gohr
79c4127b8eSAndreas Gohr    /**
806a18e0f4SAndreas Gohr     * Access the Chat Model
810337f47fSAndreas Gohr     *
82294a9eafSAndreas Gohr     * @return ChatInterface
830337f47fSAndreas Gohr     */
846a18e0f4SAndreas Gohr    public function getChatModel()
850337f47fSAndreas Gohr    {
86294a9eafSAndreas Gohr        if ($this->chatModel instanceof ChatInterface) {
876a18e0f4SAndreas Gohr            return $this->chatModel;
886a18e0f4SAndreas Gohr        }
896a18e0f4SAndreas Gohr
90*dce0dee5SAndreas Gohr        [$namespace, $name] = sexplode(' ', $this->getConf('chatmodel'), 2);
91*dce0dee5SAndreas Gohr        $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $namespace . '\\ChatModel';
92d02b7935SAndreas Gohr
939f6b34c4SAndreas Gohr        if (!class_exists($class)) {
94*dce0dee5SAndreas Gohr            throw new \RuntimeException('No ChatModel found for ' . $namespace);
959f6b34c4SAndreas Gohr        }
96d02b7935SAndreas Gohr
97*dce0dee5SAndreas Gohr        $this->chatModel = new $class($name, $this->conf);
986a18e0f4SAndreas Gohr        return $this->chatModel;
999f6b34c4SAndreas Gohr    }
1009f6b34c4SAndreas Gohr
1016a18e0f4SAndreas Gohr    /**
1026a18e0f4SAndreas Gohr     * Access the Embedding Model
1036a18e0f4SAndreas Gohr     *
104294a9eafSAndreas Gohr     * @return EmbeddingInterface
1056a18e0f4SAndreas Gohr     */
1066a18e0f4SAndreas Gohr    public function getEmbedModel()
1076a18e0f4SAndreas Gohr    {
108294a9eafSAndreas Gohr        if ($this->embedModel instanceof EmbeddingInterface) {
1096a18e0f4SAndreas Gohr            return $this->embedModel;
1100337f47fSAndreas Gohr        }
1110337f47fSAndreas Gohr
112*dce0dee5SAndreas Gohr        [$namespace, $name] = sexplode(' ', $this->getConf('embedmodel'), 2);
113*dce0dee5SAndreas Gohr        $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $namespace . '\\EmbeddingModel';
1146a18e0f4SAndreas Gohr
115*dce0dee5SAndreas Gohr        if (!class_exists($class)) {
116*dce0dee5SAndreas Gohr            throw new \RuntimeException('No EmbeddingModel found for ' . $namespace);
117*dce0dee5SAndreas Gohr        }
118*dce0dee5SAndreas Gohr
119*dce0dee5SAndreas Gohr        $this->embedModel = new $class($name, $this->conf);
1206a18e0f4SAndreas Gohr        return $this->embedModel;
1216a18e0f4SAndreas Gohr    }
1226a18e0f4SAndreas Gohr
1236a18e0f4SAndreas Gohr
1240337f47fSAndreas Gohr    /**
1250337f47fSAndreas Gohr     * Access the Embeddings interface
1260337f47fSAndreas Gohr     *
1270337f47fSAndreas Gohr     * @return Embeddings
1280337f47fSAndreas Gohr     */
1290337f47fSAndreas Gohr    public function getEmbeddings()
1300337f47fSAndreas Gohr    {
1316a18e0f4SAndreas Gohr        if ($this->embeddings instanceof Embeddings) {
1326a18e0f4SAndreas Gohr            return $this->embeddings;
1336a18e0f4SAndreas Gohr        }
1346a18e0f4SAndreas Gohr
13534a1c478SAndreas Gohr        $this->embeddings = new Embeddings(
13634a1c478SAndreas Gohr            $this->getChatModel(),
13734a1c478SAndreas Gohr            $this->getEmbedModel(),
13834a1c478SAndreas Gohr            $this->getStorage(),
13934a1c478SAndreas Gohr            $this->conf
14034a1c478SAndreas Gohr        );
1413379af09SAndreas Gohr        if ($this->logger) {
1423379af09SAndreas Gohr            $this->embeddings->setLogger($this->logger);
1433379af09SAndreas Gohr        }
1449f6b34c4SAndreas Gohr
1450337f47fSAndreas Gohr        return $this->embeddings;
1460337f47fSAndreas Gohr    }
1470337f47fSAndreas Gohr
1480337f47fSAndreas Gohr    /**
14901f06932SAndreas Gohr     * Access the Storage interface
15001f06932SAndreas Gohr     *
15101f06932SAndreas Gohr     * @return AbstractStorage
15201f06932SAndreas Gohr     */
15301f06932SAndreas Gohr    public function getStorage()
15401f06932SAndreas Gohr    {
1556a18e0f4SAndreas Gohr        if ($this->storage instanceof AbstractStorage) {
1566a18e0f4SAndreas Gohr            return $this->storage;
1576a18e0f4SAndreas Gohr        }
1586a18e0f4SAndreas Gohr
15913dbfc23SAndreas Gohr        if ($this->getConf('pinecone_apikey')) {
16013dbfc23SAndreas Gohr            $this->storage = new PineconeStorage();
1615e6dd16eSAndreas Gohr        } elseif ($this->getConf('chroma_baseurl')) {
1625e6dd16eSAndreas Gohr            $this->storage = new ChromaStorage();
1634c0099a8SAndreas Gohr        } elseif ($this->getConf('qdrant_baseurl')) {
1644c0099a8SAndreas Gohr            $this->storage = new QdrantStorage();
16513dbfc23SAndreas Gohr        } else {
16601f06932SAndreas Gohr            $this->storage = new SQLiteStorage();
16768b6fa79SAndreas Gohr        }
1688285fff9SAndreas Gohr
1693379af09SAndreas Gohr        if ($this->logger) {
1703379af09SAndreas Gohr            $this->storage->setLogger($this->logger);
1713379af09SAndreas Gohr        }
17201f06932SAndreas Gohr
17301f06932SAndreas Gohr        return $this->storage;
17401f06932SAndreas Gohr    }
17501f06932SAndreas Gohr
17601f06932SAndreas Gohr    /**
1770337f47fSAndreas Gohr     * Ask a question with a chat history
1780337f47fSAndreas Gohr     *
1790337f47fSAndreas Gohr     * @param string $question
1800337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
1810337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
1820337f47fSAndreas Gohr     * @throws Exception
1830337f47fSAndreas Gohr     */
1840337f47fSAndreas Gohr    public function askChatQuestion($question, $history = [])
1850337f47fSAndreas Gohr    {
1860337f47fSAndreas Gohr        if ($history) {
1870337f47fSAndreas Gohr            $standaloneQuestion = $this->rephraseChatQuestion($question, $history);
1880337f47fSAndreas Gohr        } else {
1890337f47fSAndreas Gohr            $standaloneQuestion = $question;
1900337f47fSAndreas Gohr        }
19134a1c478SAndreas Gohr        return $this->askQuestion($standaloneQuestion, $history);
1920337f47fSAndreas Gohr    }
1930337f47fSAndreas Gohr
1940337f47fSAndreas Gohr    /**
1950337f47fSAndreas Gohr     * Ask a single standalone question
1960337f47fSAndreas Gohr     *
1970337f47fSAndreas Gohr     * @param string $question
19834a1c478SAndreas Gohr     * @param array $history [user, ai] of the previous question
1990337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
2000337f47fSAndreas Gohr     * @throws Exception
2010337f47fSAndreas Gohr     */
20234a1c478SAndreas Gohr    public function askQuestion($question, $history = [])
2030337f47fSAndreas Gohr    {
204e33a1d7aSAndreas Gohr        $similar = $this->getEmbeddings()->getSimilarChunks($question, $this->getLanguageLimit());
2059e81bea7SAndreas Gohr        if ($similar) {
206441edf84SAndreas Gohr            $context = implode(
207441edf84SAndreas Gohr                "\n",
208441edf84SAndreas Gohr                array_map(static fn(Chunk $chunk) => "\n```\n" . $chunk->getText() . "\n```\n", $similar)
209441edf84SAndreas Gohr            );
210219268b1SAndreas Gohr            $prompt = $this->getPrompt('question', [
211219268b1SAndreas Gohr                'context' => $context,
212219268b1SAndreas Gohr            ]);
2139e81bea7SAndreas Gohr        } else {
21434a1c478SAndreas Gohr            $prompt = $this->getPrompt('noanswer');
21534a1c478SAndreas Gohr            $history = [];
2169e81bea7SAndreas Gohr        }
21768908844SAndreas Gohr
21834a1c478SAndreas Gohr        $messages = $this->prepareMessages($prompt, $question, $history);
2196a18e0f4SAndreas Gohr        $answer = $this->getChatModel()->getAnswer($messages);
2200337f47fSAndreas Gohr
2210337f47fSAndreas Gohr        return [
2220337f47fSAndreas Gohr            'question' => $question,
2230337f47fSAndreas Gohr            'answer' => $answer,
2240337f47fSAndreas Gohr            'sources' => $similar,
2250337f47fSAndreas Gohr        ];
2260337f47fSAndreas Gohr    }
2270337f47fSAndreas Gohr
2280337f47fSAndreas Gohr    /**
2290337f47fSAndreas Gohr     * Rephrase a question into a standalone question based on the chat history
2300337f47fSAndreas Gohr     *
2310337f47fSAndreas Gohr     * @param string $question The original user question
2320337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
2330337f47fSAndreas Gohr     * @return string The rephrased question
2340337f47fSAndreas Gohr     * @throws Exception
2350337f47fSAndreas Gohr     */
2360337f47fSAndreas Gohr    public function rephraseChatQuestion($question, $history)
2370337f47fSAndreas Gohr    {
23834a1c478SAndreas Gohr        $prompt = $this->getPrompt('rephrase');
23934a1c478SAndreas Gohr        $messages = $this->prepareMessages($prompt, $question, $history);
24034a1c478SAndreas Gohr        return $this->getChatModel()->getAnswer($messages);
24134a1c478SAndreas Gohr    }
24234a1c478SAndreas Gohr
24334a1c478SAndreas Gohr    /**
24434a1c478SAndreas Gohr     * Prepare the messages for the AI
24534a1c478SAndreas Gohr     *
24634a1c478SAndreas Gohr     * @param string $prompt The fully prepared system prompt
24734a1c478SAndreas Gohr     * @param string $question The user question
24834a1c478SAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
24934a1c478SAndreas Gohr     * @return array An OpenAI compatible array of messages
25034a1c478SAndreas Gohr     */
25134a1c478SAndreas Gohr    protected function prepareMessages($prompt, $question, $history)
25234a1c478SAndreas Gohr    {
25334a1c478SAndreas Gohr        // calculate the space for context
25434a1c478SAndreas Gohr        $remainingContext = $this->getChatModel()->getMaxInputTokenLength();
25534a1c478SAndreas Gohr        $remainingContext -= $this->countTokens($prompt);
25634a1c478SAndreas Gohr        $remainingContext -= $this->countTokens($question);
25734a1c478SAndreas Gohr        $safetyMargin = $remainingContext * 0.05; // 5% safety margin
25834a1c478SAndreas Gohr        $remainingContext -= $safetyMargin;
25934a1c478SAndreas Gohr        // FIXME we may want to also have an upper limit for the history and not always use the full context
26034a1c478SAndreas Gohr
26134a1c478SAndreas Gohr        $messages = $this->historyMessages($history, $remainingContext);
26234a1c478SAndreas Gohr        $messages[] = [
26334a1c478SAndreas Gohr            'role' => 'system',
26434a1c478SAndreas Gohr            'content' => $prompt
26534a1c478SAndreas Gohr        ];
26634a1c478SAndreas Gohr        $messages[] = [
26734a1c478SAndreas Gohr            'role' => 'user',
26834a1c478SAndreas Gohr            'content' => $question
26934a1c478SAndreas Gohr        ];
27034a1c478SAndreas Gohr        return $messages;
27134a1c478SAndreas Gohr    }
27234a1c478SAndreas Gohr
27334a1c478SAndreas Gohr    /**
27434a1c478SAndreas Gohr     * Create an array of OpenAI compatible messages from the given history
27534a1c478SAndreas Gohr     *
27634a1c478SAndreas Gohr     * Only as many messages are used as fit into the token limit
27734a1c478SAndreas Gohr     *
27834a1c478SAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
27934a1c478SAndreas Gohr     * @param int $tokenLimit
28034a1c478SAndreas Gohr     * @return array
28134a1c478SAndreas Gohr     */
28234a1c478SAndreas Gohr    protected function historyMessages($history, $tokenLimit)
28334a1c478SAndreas Gohr    {
28434a1c478SAndreas Gohr        $remainingContext = $tokenLimit;
28534a1c478SAndreas Gohr
28634a1c478SAndreas Gohr        $messages = [];
2870337f47fSAndreas Gohr        $history = array_reverse($history);
2880337f47fSAndreas Gohr        foreach ($history as $row) {
28934a1c478SAndreas Gohr            $length = $this->countTokens($row[0] . $row[1]);
29034a1c478SAndreas Gohr            if ($length > $remainingContext) {
2910337f47fSAndreas Gohr                break;
2920337f47fSAndreas Gohr            }
29334a1c478SAndreas Gohr            $remainingContext -= $length;
2940337f47fSAndreas Gohr
29534a1c478SAndreas Gohr            $messages[] = [
29634a1c478SAndreas Gohr                'role' => 'assistant',
29734a1c478SAndreas Gohr                'content' => $row[1]
29834a1c478SAndreas Gohr            ];
29934a1c478SAndreas Gohr            $messages[] = [
30034a1c478SAndreas Gohr                'role' => 'user',
30134a1c478SAndreas Gohr                'content' => $row[0]
30234a1c478SAndreas Gohr            ];
30334a1c478SAndreas Gohr        }
30434a1c478SAndreas Gohr        return array_reverse($messages);
3050337f47fSAndreas Gohr    }
3060337f47fSAndreas Gohr
30734a1c478SAndreas Gohr    /**
30834a1c478SAndreas Gohr     * Get an aproximation of the token count for the given text
30934a1c478SAndreas Gohr     *
31034a1c478SAndreas Gohr     * @param $text
31134a1c478SAndreas Gohr     * @return int
31234a1c478SAndreas Gohr     */
31334a1c478SAndreas Gohr    protected function countTokens($text)
31434a1c478SAndreas Gohr    {
31534a1c478SAndreas Gohr        return count($this->getEmbeddings()->getTokenEncoder()->encode($text));
3160337f47fSAndreas Gohr    }
3170337f47fSAndreas Gohr
3180337f47fSAndreas Gohr    /**
3190337f47fSAndreas Gohr     * Load the given prompt template and fill in the variables
3200337f47fSAndreas Gohr     *
3210337f47fSAndreas Gohr     * @param string $type
3220337f47fSAndreas Gohr     * @param string[] $vars
3230337f47fSAndreas Gohr     * @return string
3240337f47fSAndreas Gohr     */
3250337f47fSAndreas Gohr    protected function getPrompt($type, $vars = [])
3260337f47fSAndreas Gohr    {
3270337f47fSAndreas Gohr        $template = file_get_contents($this->localFN('prompt_' . $type));
32834a1c478SAndreas Gohr        $vars['language'] = $this->getLanguagePrompt();
3290337f47fSAndreas Gohr
3307ebc7895Ssplitbrain        $replace = [];
3310337f47fSAndreas Gohr        foreach ($vars as $key => $val) {
3320337f47fSAndreas Gohr            $replace['{{' . strtoupper($key) . '}}'] = $val;
3330337f47fSAndreas Gohr        }
3340337f47fSAndreas Gohr
3350337f47fSAndreas Gohr        return strtr($template, $replace);
3360337f47fSAndreas Gohr    }
337219268b1SAndreas Gohr
338219268b1SAndreas Gohr    /**
339219268b1SAndreas Gohr     * Construct the prompt to define the answer language
340219268b1SAndreas Gohr     *
341219268b1SAndreas Gohr     * @return string
342219268b1SAndreas Gohr     */
343219268b1SAndreas Gohr    protected function getLanguagePrompt()
344219268b1SAndreas Gohr    {
345219268b1SAndreas Gohr        global $conf;
346cfaf6b32SAndreas Gohr        $isoLangnames = include(__DIR__ . '/lang/languages.php');
347cfaf6b32SAndreas Gohr
348cfaf6b32SAndreas Gohr        $currentLang = $isoLangnames[$conf['lang']] ?? 'English';
349219268b1SAndreas Gohr
350e33a1d7aSAndreas Gohr        if ($this->getConf('preferUIlanguage') > AIChat::LANG_AUTO_ALL) {
351219268b1SAndreas Gohr            if (isset($isoLangnames[$conf['lang']])) {
352219268b1SAndreas Gohr                $languagePrompt = 'Always answer in ' . $isoLangnames[$conf['lang']] . '.';
353219268b1SAndreas Gohr                return $languagePrompt;
354219268b1SAndreas Gohr            }
355219268b1SAndreas Gohr        }
356219268b1SAndreas Gohr
357cfaf6b32SAndreas Gohr        $languagePrompt = 'Always answer in the user\'s language. ' .
358cfaf6b32SAndreas Gohr            "If you are unsure about the language, speak $currentLang.";
359219268b1SAndreas Gohr        return $languagePrompt;
360219268b1SAndreas Gohr    }
361e33a1d7aSAndreas Gohr
362e33a1d7aSAndreas Gohr    /**
363e33a1d7aSAndreas Gohr     * Should sources be limited to current language?
364e33a1d7aSAndreas Gohr     *
365e33a1d7aSAndreas Gohr     * @return string The current language code or empty string
366e33a1d7aSAndreas Gohr     */
367e33a1d7aSAndreas Gohr    public function getLanguageLimit()
368e33a1d7aSAndreas Gohr    {
369e33a1d7aSAndreas Gohr        if ($this->getConf('preferUIlanguage') >= AIChat::LANG_UI_LIMITED) {
370e33a1d7aSAndreas Gohr            global $conf;
371e33a1d7aSAndreas Gohr            return $conf['lang'];
372e33a1d7aSAndreas Gohr        } else {
373e33a1d7aSAndreas Gohr            return '';
374e33a1d7aSAndreas Gohr        }
375e33a1d7aSAndreas Gohr    }
376e75dc39fSAndreas Gohr
377e75dc39fSAndreas Gohr    /**
378e75dc39fSAndreas Gohr     * Store info about the last run
379e75dc39fSAndreas Gohr     *
380e75dc39fSAndreas Gohr     * @param array $data
381e75dc39fSAndreas Gohr     * @return void
382e75dc39fSAndreas Gohr     */
383e75dc39fSAndreas Gohr    public function setRunData(array $data)
384e75dc39fSAndreas Gohr    {
385e75dc39fSAndreas Gohr        file_put_contents($this->runDataFile, json_encode($data, JSON_PRETTY_PRINT));
386e75dc39fSAndreas Gohr    }
387e75dc39fSAndreas Gohr
388e75dc39fSAndreas Gohr    /**
389e75dc39fSAndreas Gohr     * Get info about the last run
390e75dc39fSAndreas Gohr     *
391e75dc39fSAndreas Gohr     * @return array
392e75dc39fSAndreas Gohr     */
393e75dc39fSAndreas Gohr    public function getRunData()
394e75dc39fSAndreas Gohr    {
395e75dc39fSAndreas Gohr        if (!file_exists($this->runDataFile)) {
396e75dc39fSAndreas Gohr            return [];
397e75dc39fSAndreas Gohr        }
398e75dc39fSAndreas Gohr        return json_decode(file_get_contents($this->runDataFile), true);
399e75dc39fSAndreas Gohr    }
4000337f47fSAndreas Gohr}
401