Compare commits
5 commits
3f985f2751
...
a2272230e2
Author | SHA1 | Date | |
---|---|---|---|
a2272230e2 | |||
615c377623 | |||
5689929c83 | |||
0832e4c877 | |||
8ae9599d0f |
16 changed files with 329 additions and 63 deletions
24
data/prompts/01-each-chapter--build-toc.tpl
Normal file
24
data/prompts/01-each-chapter--build-toc.tpl
Normal file
|
@ -0,0 +1,24 @@
|
|||
{# GOAL: Build sections object containing subsections #}
|
||||
|
||||
Merci.
|
||||
|
||||
Suit la structure du PROGRAMME DE FORMATION.
|
||||
|
||||
Concentre toi sur le chapitre « {{chapter.title}} ».
|
||||
|
||||
Commence par écrire "boundary: {{ delimiter }}".
|
||||
Ensuite rédige la table des matière détaillée de ce chapitre en respectant la structure suivante:
|
||||
|
||||
«
|
||||
{
|
||||
"type": "section",
|
||||
"title": ""
|
||||
"subsections": [
|
||||
{ "type": "subsection", "title": "...", "keywords": ["...", "..."] },
|
||||
{ "type": "subsection", "title": "...", "keywords": ["...", "..."] },
|
||||
{ "type": "subsection", "title": "...", "keywords": ["...", "..."] }
|
||||
]
|
||||
}
|
||||
»
|
||||
|
||||
Termine en écrivant "boundary: {{ delimiter }}".
|
|
@ -1,18 +0,0 @@
|
|||
GOAL: Build sections object containing subsections
|
||||
|
||||
PROMPT:
|
||||
|
||||
Merci.
|
||||
|
||||
Suit la structure du PROGRAMME DE FORMATION.
|
||||
|
||||
Concentre toi sur le chapitre « {{this.title}} ».
|
||||
|
||||
Focalise toi plus spécifiquement sur les sections suivantes «
|
||||
{% for child in this.children %}
|
||||
* {{ child }}
|
||||
{% endfor %}
|
||||
» de ce chapitre sur lequel on se concentre.
|
||||
|
||||
Rédige la table des matière détaillée de ces différentes sections.
|
||||
Ajoute également à la fin les différents travaux pratiques possibles pour chaque section.
|
|
@ -1,5 +1,9 @@
|
|||
version: 2.0
|
||||
shards:
|
||||
baked_file_system:
|
||||
git: https://github.com/schovi/baked_file_system.git
|
||||
version: 0.10.0
|
||||
|
||||
cor:
|
||||
git: https://github.com/watzon/cor.git
|
||||
version: 0.1.0+git.commit.9c9e51ac6168f3bd4fdc51d679b65de09ef76cac
|
||||
|
|
|
@ -17,6 +17,9 @@ dependencies:
|
|||
github: crystal-term/prompt
|
||||
crinja:
|
||||
github: straight-shoota/crinja
|
||||
baked_file_system:
|
||||
github: schovi/baked_file_system
|
||||
version: 0.10.0
|
||||
|
||||
# FIXME: for prompts rendering
|
||||
|
||||
|
|
|
@ -55,23 +55,35 @@ module DocMachine::Build
|
|||
Process.run("docker", ["pull", @docker_image], output: STDOUT)
|
||||
Log.info { "Building cache for image (#{data_cache_dir})" }
|
||||
FileUtils.mkdir_p(data_cache_dir)
|
||||
Process.run(
|
||||
status = Process.run(
|
||||
"docker",
|
||||
["image", "save", @docker_image, "-o", data_cache_file.to_s],
|
||||
output: STDOUT
|
||||
)
|
||||
Log.info { "done" }
|
||||
if status.success?
|
||||
Log.info { "done" }
|
||||
else
|
||||
Log.error { "Unable to save cache image" }
|
||||
exit 1
|
||||
end
|
||||
|
||||
else
|
||||
Log.info { "Cache already exist. Skipping." }
|
||||
end
|
||||
|
||||
Log.info { "Loading #{@docker_image} image from cache..." }
|
||||
docker_image_loaded = false
|
||||
Process.run(
|
||||
status = Process.run(
|
||||
"docker",
|
||||
["image", "load", @docker_image, "-i", data_cache_file.to_s],
|
||||
["image", "load", "-i", data_cache_file.to_s],
|
||||
output: STDOUT
|
||||
)
|
||||
if status.success?
|
||||
Log.info { "done" }
|
||||
else
|
||||
Log.error { "Unable to load cache image" }
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
def start()
|
||||
|
|
21
src/cli.cr
21
src/cli.cr
|
@ -2,6 +2,7 @@ require "option_parser"
|
|||
require "digest/sha256"
|
||||
require "colorize"
|
||||
|
||||
require "./log"
|
||||
require "./config"
|
||||
require "./build/cli"
|
||||
require "./build/run"
|
||||
|
@ -14,6 +15,8 @@ require "./write/run"
|
|||
|
||||
module DocMachine
|
||||
class Cli
|
||||
Log = DocMachine::Log.for("cli")
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
||||
|
@ -28,12 +31,18 @@ module DocMachine
|
|||
"Main options:"
|
||||
].join("\n")
|
||||
|
||||
opts.on("-v", "--verbose", "Enable verbosity") do |verbose|
|
||||
config.verbose = true
|
||||
opts.on("-v", "--verbosity LEVEL", "Change verbosity level to LEVEL (0..3)") do |verbose|
|
||||
verbose_i = verbose.to_i
|
||||
verbose_i = 0 if verbose.to_i < 0
|
||||
verbose_i = 3 if verbose.to_i > 3
|
||||
config.verbosity = ::Log::Severity.from_value(3 - verbose_i)
|
||||
rescue ex: ArgumentError
|
||||
Log.error { "Wrong value for parameter --verbosity" }
|
||||
exit 1
|
||||
end
|
||||
|
||||
opts.on("-h", "--help", "Show this help") do
|
||||
Log.info { opts }
|
||||
Log.notice { opts }
|
||||
exit
|
||||
end
|
||||
|
||||
|
@ -47,15 +56,17 @@ module DocMachine
|
|||
end
|
||||
|
||||
parser.parse(args)
|
||||
::Log.setup(config.verbosity, ::Log::IOBackend.new(formatter: BaseFormat))
|
||||
Log.notice { "verbosity level = #{config.verbosity}" }
|
||||
Log.debug { "commands = #{commands}" }
|
||||
|
||||
if commands.size < 1
|
||||
Log.error { parser.to_s }
|
||||
Log.error { "ERROR: no command defined" }
|
||||
Log.error { "No command defined" }
|
||||
end
|
||||
|
||||
commands.each do |command|
|
||||
# Log.info { "== Running #{command}" }
|
||||
Log.debug { "== Running #{command}" }
|
||||
command.call()
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ require "./module"
|
|||
module DocMachine
|
||||
class Config
|
||||
|
||||
property verbose : Bool = false
|
||||
property verbosity = ::Log::Severity::Notice
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
|
38
src/log.cr
Normal file
38
src/log.cr
Normal file
|
@ -0,0 +1,38 @@
|
|||
|
||||
require "log"
|
||||
require "colorize"
|
||||
|
||||
struct DebugFormat < Log::StaticFormatter
|
||||
def run
|
||||
string @entry.severity.label[0].downcase
|
||||
string ": "
|
||||
source
|
||||
string ": "
|
||||
message
|
||||
end
|
||||
end
|
||||
|
||||
struct BaseFormat < Log::StaticFormatter
|
||||
def run
|
||||
io = ::IO::Memory.new
|
||||
|
||||
color = case @entry.severity
|
||||
when ::Log::Severity::Error
|
||||
Colorize.colorize.red.bold
|
||||
when ::Log::Severity::Warn
|
||||
Colorize.colorize.red.yellow
|
||||
when ::Log::Severity::Notice
|
||||
Colorize.colorize.bold
|
||||
else
|
||||
Colorize.colorize
|
||||
end
|
||||
|
||||
color.surround(io) do
|
||||
io << @entry.message
|
||||
end
|
||||
|
||||
string io.to_s
|
||||
end
|
||||
end
|
||||
|
||||
# Log.define_formatter BaseFormat, "#{severity.to_s.lstrip}(#{source}): #{message}"
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
require "./cli"
|
||||
|
||||
Log.define_formatter BaseFormat, "#{message}"
|
||||
|
||||
require "./log"
|
||||
|
||||
::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat))
|
||||
::Log.progname = "(root)"
|
||||
app = DocMachine::Cli.new
|
||||
app.start(ARGV)
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
|
||||
require "./config"
|
||||
require "./run"
|
||||
require "./module"
|
||||
|
||||
module DocMachine::Write
|
||||
Log = DocMachine::Log.for("write")
|
||||
|
||||
class Cli
|
||||
Log = DocMachine::Write::Log.for("cli")
|
||||
|
||||
|
@ -11,22 +11,31 @@ module DocMachine::Write
|
|||
config = Config.new(parent_config)
|
||||
|
||||
opts.on("write", "Write content target for plan (beta)") do
|
||||
opts.banner = "Usage: #{PROGRAM_NAME} scaffold [options] TARGET"
|
||||
opts.banner = "Usage: #{PROGRAM_NAME} write [options] TARGET"
|
||||
|
||||
opts.on("-f", "--force", "Don't ask for confirmation") do
|
||||
config.force = true
|
||||
end
|
||||
|
||||
opts.on("-t", "--template TEMPLATE", "Use given template") do |template_name|
|
||||
config.template_name = template_name
|
||||
end
|
||||
|
||||
commands << ->() : Nil do
|
||||
Log.debug { "before any" }
|
||||
if args.size < 1
|
||||
Log.error { "ERROR: No target given!" }
|
||||
Log.error { "No target given!" }
|
||||
exit 1
|
||||
end
|
||||
config.target_directory = args[0]
|
||||
|
||||
Log.debug { "before new" }
|
||||
app = DocMachine::Write::Run.new(config)
|
||||
Log.debug { "before prepare" }
|
||||
app.prepare
|
||||
Log.debug { "before start" }
|
||||
app.start
|
||||
Log.debug { "before wait" }
|
||||
app.wait
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,7 @@ module DocMachine::Write
|
|||
|
||||
property target_directory : String = "."
|
||||
property force : Bool = false
|
||||
property template_name : String = ""
|
||||
|
||||
def initialize(@parent : DocMachine::Config)
|
||||
end
|
||||
|
|
7
src/write/file_storage.cr
Normal file
7
src/write/file_storage.cr
Normal file
|
@ -0,0 +1,7 @@
|
|||
require "baked_file_system"
|
||||
|
||||
class FileStorage
|
||||
extend BakedFileSystem
|
||||
|
||||
bake_folder "../../data/prompts"
|
||||
end
|
6
src/write/module.cr
Normal file
6
src/write/module.cr
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
require "../module"
|
||||
|
||||
module DocMachine::Write
|
||||
Log = DocMachine::Log.for("write")
|
||||
end
|
6
src/write/nodes/module.cr
Normal file
6
src/write/nodes/module.cr
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
require "../module"
|
||||
|
||||
module DocMachine::Write::Nodes
|
||||
Log = DocMachine::Write::Log.for("nodes")
|
||||
end
|
46
src/write/nodes/root_node.cr
Normal file
46
src/write/nodes/root_node.cr
Normal file
|
@ -0,0 +1,46 @@
|
|||
|
||||
require "./module"
|
||||
|
||||
module DocMachine::Write::Nodes
|
||||
class RootNode
|
||||
property context : String = ""
|
||||
property audience : String = ""
|
||||
property goals : String = ""
|
||||
property constraints : String = ""
|
||||
property chapters = [] of ChapterNode
|
||||
|
||||
def build_chapters()
|
||||
[] of ChapterNode
|
||||
end
|
||||
end
|
||||
|
||||
class ChapterNode
|
||||
property sections = [] of SectionNode
|
||||
|
||||
def build_sections()
|
||||
[] of SectionNode
|
||||
end
|
||||
end
|
||||
|
||||
class SectionNode
|
||||
property subsections = [] of SubsectionNode
|
||||
|
||||
def build_subsections()
|
||||
[] of SubsectionNode
|
||||
end
|
||||
end
|
||||
|
||||
class SubsectionNode
|
||||
property content = [] of String
|
||||
|
||||
def build_content()
|
||||
[] of String
|
||||
end
|
||||
|
||||
def fix_content()
|
||||
[] of String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
174
src/write/run.cr
174
src/write/run.cr
|
@ -1,60 +1,178 @@
|
|||
|
||||
# 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
|
||||
|
||||
# Verify parameters
|
||||
def prepare()
|
||||
def validate_build_dir()
|
||||
if ! File.directory? @config.target_directory
|
||||
Log.error { "ERROR: target must be a directory" }
|
||||
Log.error { "Target must be a directory" }
|
||||
exit 1
|
||||
end
|
||||
|
||||
Log.info { "Target directory: #{@config.target_directory}" }
|
||||
|
||||
if !@config.force
|
||||
prompt = Term::Prompt.new
|
||||
confirm = prompt.no?("Are you sure you want to proceed?")
|
||||
exit 1 if !confirm
|
||||
end
|
||||
end
|
||||
|
||||
def start()
|
||||
Log.info { "== Writeing #{@config.target_directory}" }
|
||||
p = Path.new(@config.target_directory)
|
||||
cwd = Dir.current
|
||||
["docs", "slides", "images"].each do |dir|
|
||||
p_sub = p.join(dir)
|
||||
Log.info { "-- creating #{p_sub}" }
|
||||
FileUtils.mkdir_p(p_sub)
|
||||
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
|
||||
["docs", "slides"].each do |dir|
|
||||
p_sub = p.join(dir)
|
||||
FileUtils.cd(p_sub)
|
||||
Log.info { "-- creating link to images in #{p_sub}" }
|
||||
if File.symlink? "images"
|
||||
FileUtils.rm "images"
|
||||
end
|
||||
FileUtils.ln_sf(Path.new("..","images"), Path.new("images"))
|
||||
FileUtils.cd(cwd)
|
||||
@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
|
||||
Log.info { "-- creating README.md" }
|
||||
FileUtils.touch("README.md")
|
||||
@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
|
||||
|
|
Loading…
Add table
Reference in a new issue