docmachine-utils/src/write/run.cr

180 lines
5.1 KiB
Crystal
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Core
require "file_utils"
require "path"
# Internal
require "./config"
require "./nodes/root_node"
# Shards
require "term-prompt"
require "crinja"
module DocMachine::Write
class Run
private property config : DocMachine::Write::Config
property root = Nodes::RootNode.new
def initialize(@config)
end
def validate_build_dir()
if ! File.directory? @config.target_directory
Log.error { "Target must be a directory" }
exit 1
end
Log.info { "Target directory: #{@config.target_directory}" }
end
def ask_confirmation
# if !@config.force
# prompt = Term::Prompt.new
# confirm = prompt.no?("Are you sure you want to proceed?")
# exit 1 if !confirm
# end
end
def load_templates
pp @config
context_file = Path[@config.target_directory] / "CONTEXT.tpl"
if ! File.exists? context_file
raise "Context file #{context_file} is missing"
end
@root.context = File.read(context_file)
audience_file = Path[@config.target_directory] / "AUDIENCE.tpl"
if ! File.exists? audience_file
raise "Audience file #{audience_file} is missing"
end
@root.audience = File.read(audience_file)
goals_file = Path[@config.target_directory] / "GOALS.tpl"
if ! File.exists? goals_file
raise "Audience file #{goals_file} is missing"
end
@root.goals = File.read(goals_file)
constraints_file = Path[@config.target_directory] / "CONSTRAINTS.tpl"
if ! File.exists? constraints_file
raise "Audience file #{constraints_file} is missing"
end
@root.constraints = File.read(constraints_file)
end
# Verify parameters
def prepare()
validate_build_dir()
ask_confirmation()
load_templates()
Log.debug { "done" }
end
##
## ContentNode
## type: ...
## title: ...
## keywords: ...
## content: ...
##
def start()
@root.chapters = root.build_chapters()
@root.chapters.each do |chapter|
chapter.sections = chapter.build_sections()
chapter.sections.each do |section|
section.subsections = section.build_subsections()
# FIXME(later): section.exercises = section.build_exercises()
section.subsections.each do |subsection|
subsection.content = subsection.build_content()
subsection.content = subsection.fix_content()
# FIXME(later): subsection.exercises = subsection.build_exercises()
end
end
end
end
## Level 0 - each topic : build TOC (chapter list)
def from_topic_build_chapters
chapter_build_toc_template = FileStorage.get("./../data/prompts/01-each-chapter--build-toc.tpl")
chapters = [{ "title": "Terraform on Azure" }]
end
## Level 1 - each chapter : build TOC (section list)
# 1. build chat
# - (system) quality & style guidance
# - (user) context
# - (user) audience
# - (user) objectives
# - (user) main toc (chapters)
def from_chapter_build_sections()
delimiter = "34e127df" # FIXME: random 8 bytes hex string
chapters.each do |chapter|
template_vars = {
delimiter: delimiter,
chapter: chapter
}
render = Crinja.render(chapter_build_toc_template, template_vars)
puts render
end
end
## Level 2 - each section : build TOC (subsection list)
# 1. build chat
# - (system) quality & style guidance
# - (user) context
# - (user) audience
# - (user) objectives
# - (user) main toc (chapters)
# - (user) chapter toc (sections)
# 2. make openai request
# 3. validate result structure
# 4. create section objects in memory
def from_section_build_subsections()
section_build_toc_tpl = File.read(DOCMACHINE_DATA_PATH / "prompts" / "02-each-section--build-toc.tpl")
end
## Level 2 - each section : build EXERCISES
# 1. build chat
# - (system) quality & style guidance
# - (user) context
# - (user) audience
# - (user) objectives
# - (user) main toc (chapters)
# - (user) chapter toc (sections)
# 2. make openai request
# 3. validate result structure
# 4. create exercises objects in memory
def from_section_build_exercises()
section_build_toc_tpl = File.read(DOCMACHINE_DATA_PATH / "prompts" / "02-each-section--build-exercises.tpl")
end
def from_subsection_build_content()
## Level 3 - each subsection : build CONTENT
section_build_toc_tpl = File.read(DOCMACHINE_DATA_PATH / "prompts" / "02-each-subsection--build-content.tpl")
end
def from_subsection_fix_content()
## Level 4 - each subsection : build FIXED CONTENT
section_build_toc_tpl = File.read(DOCMACHINE_DATA_PATH / "prompts" / "02-each-subsection--fix-content.tpl")
end
def from_subsection_build_exercises()
## Level 1 - each subsection EXERCICES
section_build_toc_tpl = File.read(DOCMACHINE_DATA_PATH / "prompts" / "02-each-subsection--build-exercises.tpl")
end
def wait()
end
end
end