#!/usr/bin/env ruby

require 'open3'
require 'date'

word = "beach" # TODO randomize

prompt = File.open('prompt') { |f| f.read.gsub('{WORD}', word) }
File.open('prompt.tmp', 'w') { |f| f.write(prompt) } # easier than escaping in bash command lol


cmd = %{llama-cli -m DeepSeek*.gguf -ngl 15000 -cnv --grammar-file grammar -f prompt.tmp --repeat-penalty 1.1 --repeat-last-n 20 -c 32768 --no-display-prompt}

stdin, stdout, _ = Open3.popen2e(cmd)

file = File.open("log/#{DateTime.now.to_s}.txt", 'w')
file.write("Word: #{word}\n")

Thread.new do
  cur_line = ''
  is_thinking = false
  while c = stdout.getc
    file.write(c)
    file.flush()
    if c == "\n"
      if cur_line.include?('<think>')
        is_thinking = true
      elsif cur_line.include?('</think>')
        is_thinking = false
        puts "</think>" # now we have 2 newlines
      end

      cur_line = ''
    else
      cur_line += c
    end

    if is_thinking
      if c == ' '
        print 'think '
      elsif c == "\n"
        puts
      end
    else
      print c
    end
  end
end

loop do
  line = gets
  stdin.write(line)
  file.write(line)
  file.flush()
  stdin.flush()
end