#!/usr/bin/env

cmd = 'llama-cli -m /DeepSeek-R1-Distill-Qwen-32B-Q6_K_L.gguf -ngl 15000 -cnv --grammar-file grammar -f prompt --repeat-penalty 1.1 --repeat-last-n 20 -c 32768 --no-display-prompt'

require 'open3'
require 'date'

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

file = File.open("log/#{DateTime.now.to_s}.txt", 'w')

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