~firefoxphpai-chat
17 items • Download ./*

..
ajax.php
ana-ai-chat.png
atom-one-dark.min.css
convert-databases-v1.0.2-to-v1.0.3.php
db-browser.php
emoji.json
favicon.ico
highlight.min.js
index.php
note.txt
pair-all-chat-times.php
readme.txt
rebuild-chats-json.php
recalculate-chat-count.php
setup.php
setup_and_launch.py
worker.php


ai-chatpair-all-chat-times.php
4 KB• 26•  1 week ago•  DownloadRawClose
1 week ago•  26

{}
<?php
    error_reporting(0);
    //error_reporting(E_ALL & ~E_NOTICE);
    date_default_timezone_set("Europe/Oslo");

    // for fixing chat log time stamps from before around v1.0.3 or 4.
    // chats before had mismatch user/assistant timestamp, while match
    // is utilized for fetcing prompt in img2img insert-
    //
    // this script goes through all chat logs, from oldest to latest
    // loops the array of messages, if i=user and i+1=assistant, user
    // time = assistant time, and i+=2 skips to next entry, +=1 if 
    // non match, ie one was deleted. (backups current/original as .log_copyN)

    $files=ListFilesInFolder("./chats");
    $arr=[];
    for($i=0;$i<count($files);$i++) {
        $ext=substr($files[$i],strrpos($files[$i],".")+1);
        if(strtolower($ext)=="log") {
            $arr[]=$files[$i];
        }
    }

    $chats=[];
    for($i=0;$i<count($arr);$i++) {
        $chatId=substr($arr[$i],0,strrpos($arr[$i],".")); // id

        $chatfile="./chats/".$arr[$i];
        $lastmtime=filemtime($chatfile); // time

        $fdata=file_get_contents($chatfile);
        $flist=explode("\n",$fdata);
        $newstr="";
        $edited=false;
        for($j=0;$j<count($flist);$j++) {
            if(IsJsonString($flist[$j])) {
                $j2=$j+1;
                $msg=json_decode($flist[$j],true);
                $msgN=array("role"=>"none");
                if($j2<count($flist)) {
                    if(IsJsonString($flist[$j2])) {
                        $msgN=json_decode($flist[($j2)],true);
                    }
                }
                if($msg['role']=="user" && $msgN['role']=="assistant") {
                    $msg['time']=$msgN['time']; // copy ai time/chat write
                    $newstr.=json_encode($msg, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)."\n";
                    $newstr.=$flist[$j2]."\n";
                    $edited=true;
                    // fix time jump 2
                    $j+=1; // one extra
                } else {
                    $newstr.=$flist[$j]."\n";
                    // skip fix jump 1?
                }
            }
        }

        if($edited) {
            echo "changes found for chatfile: ".$chatfile."<br>";
        }

        $write=true;
        if($write && $edited) { 
            if(file_exists($chatfile)) {
                $num=1;
                while(file_exists($chatfile."_copy".$num)) {
                    $num++;
                }
                rename($chatfile,$chatfile."_copy".$num);
            }
        
            file_put_contents($chatfile, $newstr, FILE_APPEND | LOCK_EX);
            
            if($edited) {
                echo "changes applied to chatfile: ".$chatfile."<br>";
                echo "and backup made to: ".$chatfile."_copy".$num."<br>";
            }
        }
    } 
    exit;

    function IsJsonString($string) {
        if(substr($string,0,1)=="{" || substr($string,0,1)=="[") {
            return true;
        } else {
            return false;
        }
    }

    function ListFoldersInFolder($dir="./chats") {
        $filesAndFolders = scandir($dir);
        $folders = [];

        foreach ($filesAndFolders as $item) {
            // Exclude relative pointers and check if it's a directory
            if ($item !== '.' && $item !== '..' && is_dir($dir . '/' . $item)) {
                $folders[] = $item;
            }
        }

        return $folders;
    }

    function ListFilesInFolder($dir="./chats") {
        $filesAndFolders = scandir($dir);
        $files = [];

        foreach ($filesAndFolders as $item) {
            // Exclude relative pointers and check if it's a directory
            if ($item !== '.' && $item !== '..' && is_file($dir . '/' . $item)) {
                $files[] = $item;
            }
        }

        return $files;
    }
?>

Top
©twily.info 2013 - 2026
twily at twily dot info



3 946 547 visits
... ^ v