yuno-proofreader 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/lib/yuno/proofreader/analysis/adjustments.rb +30 -32
  4. data/lib/yuno/proofreader/analysis/crawler.rb +71 -73
  5. data/lib/yuno/proofreader/analysis/entries.rb +52 -54
  6. data/lib/yuno/proofreader/analysis/extractor.rb +162 -164
  7. data/lib/yuno/proofreader/analysis/graph.rb +86 -88
  8. data/lib/yuno/proofreader/analysis/judge.rb +80 -82
  9. data/lib/yuno/proofreader/analysis/judge_prompt.rb +50 -52
  10. data/lib/yuno/proofreader/analysis/judgments.rb +50 -52
  11. data/lib/yuno/proofreader/analysis/links.rb +44 -46
  12. data/lib/yuno/proofreader/analysis/list_order.rb +20 -22
  13. data/lib/yuno/proofreader/analysis/reasons.rb +44 -43
  14. data/lib/yuno/proofreader/analysis/report.rb +79 -81
  15. data/lib/yuno/proofreader/analysis/runner.rb +133 -135
  16. data/lib/yuno/proofreader/analysis/scorer.rb +97 -99
  17. data/lib/yuno/proofreader/analysis/settings.rb +65 -67
  18. data/lib/yuno/proofreader/analysis/signals.rb +77 -79
  19. data/lib/yuno/proofreader/analysis/structured_data.rb +40 -42
  20. data/lib/yuno/proofreader/analysis/url_tree.rb +34 -36
  21. data/lib/yuno/proofreader/analysis/weights.rb +67 -69
  22. data/lib/yuno/proofreader/audit/agenda.rb +63 -65
  23. data/lib/yuno/proofreader/audit/agenda_report.rb +44 -46
  24. data/lib/yuno/proofreader/audit/answers.rb +53 -55
  25. data/lib/yuno/proofreader/audit/attempt.rb +50 -52
  26. data/lib/yuno/proofreader/audit/batch.rb +49 -49
  27. data/lib/yuno/proofreader/audit/batch_report.rb +62 -64
  28. data/lib/yuno/proofreader/audit/cadence.rb +47 -49
  29. data/lib/yuno/proofreader/audit/instructions.rb +15 -17
  30. data/lib/yuno/proofreader/audit/log_fields.rb +16 -18
  31. data/lib/yuno/proofreader/audit/prompt.rb +118 -120
  32. data/lib/yuno/proofreader/audit/report.rb +116 -118
  33. data/lib/yuno/proofreader/audit/runner.rb +150 -152
  34. data/lib/yuno/proofreader/audit/schemas.rb +49 -0
  35. data/lib/yuno/proofreader/audit/steps.rb +46 -48
  36. data/lib/yuno/proofreader/cli/analysis_commands.rb +36 -38
  37. data/lib/yuno/proofreader/cli/audit_commands.rb +89 -91
  38. data/lib/yuno/proofreader/cli/command_log.rb +34 -36
  39. data/lib/yuno/proofreader/cli/flags.rb +33 -35
  40. data/lib/yuno/proofreader/cli/generate_commands.rb +99 -101
  41. data/lib/yuno/proofreader/cli/init_command.rb +123 -125
  42. data/lib/yuno/proofreader/cli/model_commands.rb +35 -37
  43. data/lib/yuno/proofreader/cli/queue_commands.rb +79 -81
  44. data/lib/yuno/proofreader/cli/sheet_commands.rb +42 -44
  45. data/lib/yuno/proofreader/cli.rb +197 -199
  46. data/lib/yuno/proofreader/config.rb +13 -17
  47. data/lib/yuno/proofreader/context.rb +45 -50
  48. data/lib/yuno/proofreader/env_file.rb +52 -56
  49. data/lib/yuno/proofreader/errors.rb +7 -9
  50. data/lib/yuno/proofreader/findings/finding.rb +73 -75
  51. data/lib/yuno/proofreader/findings/matching.rb +21 -23
  52. data/lib/yuno/proofreader/findings/merger.rb +35 -37
  53. data/lib/yuno/proofreader/findings/outline.rb +50 -52
  54. data/lib/yuno/proofreader/findings/plan.rb +14 -16
  55. data/lib/yuno/proofreader/findings/reconciler.rb +170 -172
  56. data/lib/yuno/proofreader/findings/source.rb +54 -56
  57. data/lib/yuno/proofreader/findings/text.rb +24 -26
  58. data/lib/yuno/proofreader/generate/cache.rb +54 -56
  59. data/lib/yuno/proofreader/generate/collector.rb +98 -100
  60. data/lib/yuno/proofreader/generate/describe.rb +18 -20
  61. data/lib/yuno/proofreader/generate/llm/prompt.rb +50 -52
  62. data/lib/yuno/proofreader/generate/llm/reader.rb +84 -86
  63. data/lib/yuno/proofreader/generate/llm/wording.rb +12 -14
  64. data/lib/yuno/proofreader/generate/llms_builder.rb +36 -38
  65. data/lib/yuno/proofreader/generate/llms_report.rb +82 -84
  66. data/lib/yuno/proofreader/generate/llms_txt.rb +99 -101
  67. data/lib/yuno/proofreader/generate/llms_writer.rb +43 -45
  68. data/lib/yuno/proofreader/generate/outline.rb +67 -69
  69. data/lib/yuno/proofreader/generate/passage.rb +41 -43
  70. data/lib/yuno/proofreader/generate/saved_answer.rb +42 -44
  71. data/lib/yuno/proofreader/http/client.rb +94 -96
  72. data/lib/yuno/proofreader/http/net_http_transport.rb +35 -37
  73. data/lib/yuno/proofreader/http/robots.rb +51 -53
  74. data/lib/yuno/proofreader/init/ask.rb +45 -47
  75. data/lib/yuno/proofreader/init/detector.rb +75 -77
  76. data/lib/yuno/proofreader/init/interview.rb +155 -157
  77. data/lib/yuno/proofreader/init/parsing.rb +37 -39
  78. data/lib/yuno/proofreader/init/web.rb +22 -24
  79. data/lib/yuno/proofreader/llm/client.rb +118 -113
  80. data/lib/yuno/proofreader/llm/defaults.rb +10 -12
  81. data/lib/yuno/proofreader/llm/errors.rb +18 -18
  82. data/lib/yuno/proofreader/llm/factory.rb +34 -36
  83. data/lib/yuno/proofreader/llm/handoff.rb +86 -88
  84. data/lib/yuno/proofreader/llm/http_transport.rb +25 -27
  85. data/lib/yuno/proofreader/llm/json_text.rb +12 -14
  86. data/lib/yuno/proofreader/llm/provider.rb +49 -51
  87. data/lib/yuno/proofreader/llm/providers/gemini.rb +111 -111
  88. data/lib/yuno/proofreader/llm/providers/manual.rb +34 -36
  89. data/lib/yuno/proofreader/llm/providers.rb +17 -19
  90. data/lib/yuno/proofreader/llm/types.rb +6 -8
  91. data/lib/yuno/proofreader/llm/usage.rb +8 -10
  92. data/lib/yuno/proofreader/log.rb +88 -96
  93. data/lib/yuno/proofreader/pages/fetcher.rb +53 -55
  94. data/lib/yuno/proofreader/pages/report.rb +37 -39
  95. data/lib/yuno/proofreader/pages/snapshot.rb +32 -34
  96. data/lib/yuno/proofreader/pages/trimmer.rb +23 -25
  97. data/lib/yuno/proofreader/settings_file.rb +35 -38
  98. data/lib/yuno/proofreader/site.rb +52 -54
  99. data/lib/yuno/proofreader/site_reader.rb +122 -124
  100. data/lib/yuno/proofreader/sitemap/aligner.rb +58 -60
  101. data/lib/yuno/proofreader/sitemap/importer.rb +19 -21
  102. data/lib/yuno/proofreader/sitemap/loader.rb +95 -97
  103. data/lib/yuno/proofreader/sitemap/mismatch.rb +53 -55
  104. data/lib/yuno/proofreader/sitemap/pairer.rb +171 -173
  105. data/lib/yuno/proofreader/sitemap/parser.rb +50 -52
  106. data/lib/yuno/proofreader/sitemap/report.rb +72 -74
  107. data/lib/yuno/proofreader/store/cells.rb +55 -49
  108. data/lib/yuno/proofreader/store/gateways/csv.rb +53 -56
  109. data/lib/yuno/proofreader/store/gateways/google.rb +152 -158
  110. data/lib/yuno/proofreader/store/memory.rb +60 -64
  111. data/lib/yuno/proofreader/store/records.rb +18 -20
  112. data/lib/yuno/proofreader/store/schema.rb +85 -89
  113. data/lib/yuno/proofreader/store/sheets.rb +132 -145
  114. data/lib/yuno/proofreader/store/tab_setup.rb +51 -54
  115. data/lib/yuno/proofreader/version.rb +1 -1
  116. data/lib/yuno/proofreader.rb +3 -4
  117. data/proofreader.example.yml +15 -10
  118. metadata +3 -2
@@ -1,133 +1,131 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- # The commands that write files from what the site already says: generate llms.
6
- module GenerateCommands
7
- GENERATORS = { "llms" => :generate_llms }.freeze
8
- DEFAULT_PAGES = 50
9
- PAGES_FILE = "llms-pages.json"
10
-
11
- private
12
-
13
- def generate(args)
14
- kind, *rest = args
15
- action = GENERATORS[kind]
16
- return usage_error("Unknown generate command: #{kind.inspect}. Try: generate llms") unless action
17
-
18
- send(action, rest)
19
- end
3
+ module Yuno::Proofreader
4
+ # The commands that write files from what the site already says: generate llms.
5
+ module GenerateCommands
6
+ GENERATORS = { "llms" => :generate_llms }.freeze
7
+ DEFAULT_PAGES = 50
8
+ PAGES_FILE = "llms-pages.json"
20
9
 
21
- def generate_llms(args)
22
- count, remaining, problem = take_count(args)
23
- return usage_error(problem) if problem
10
+ private
24
11
 
25
- options, rest, unknown = split_args(remaining, %w[all no-ai language full-text resume])
26
- problem = generate_args_problem(options, rest, unknown, count)
27
- return usage_error(problem) if problem
12
+ def generate(args)
13
+ kind, *rest = args
14
+ action = GENERATORS[kind]
15
+ return usage_error("Unknown generate command: #{kind.inspect}. Try: generate llms") unless action
28
16
 
29
- site = Site.load(rest.first || CLI::DEFAULT_SITE_FILE)
30
- language = options["language"]
31
- if language && !site.languages.include?(language)
32
- return usage_error("Unknown language for --language: #{language}")
33
- end
17
+ send(action, rest)
18
+ end
34
19
 
35
- run_generate_llms(site, options, count, language)
36
- end
20
+ def generate_llms(args)
21
+ count, remaining, problem = take_count(args)
22
+ return usage_error(problem) if problem
37
23
 
38
- def generate_args_problem(options, rest, unknown, count)
39
- return "Unknown option: #{unknown.first}" unless unknown.empty?
40
- return "--language needs a language, for example --language=de" if options["language"] == true
24
+ options, rest, unknown = split_args(remaining, %w[all no-ai language full-text resume])
25
+ problem = generate_args_problem(options, rest, unknown, count)
26
+ return usage_error(problem) if problem
41
27
 
42
- combination_problem(options, count) || ("Too many arguments for generate llms" if rest.size > 1)
28
+ site = Site.load(rest.first || CLI::DEFAULT_SITE_FILE)
29
+ language = options["language"]
30
+ if language && !site.languages.include?(language)
31
+ return usage_error("Unknown language for --language: #{language}")
43
32
  end
44
33
 
45
- def combination_problem(options, count)
46
- return resume_problem if options.key?("resume") && fresh_option?(options, count)
34
+ run_generate_llms(site, options, count, language)
35
+ end
47
36
 
48
- "--count and --all cannot be used together" if count && options.key?("all")
49
- end
37
+ def generate_args_problem(options, rest, unknown, count)
38
+ return "Unknown option: #{unknown.first}" unless unknown.empty?
39
+ return "--language needs a language, for example --language=de" if options["language"] == true
50
40
 
51
- def fresh_option?(options, count) = count || options.key?("all") || options.key?("full-text")
41
+ combination_problem(options, count) || ("Too many arguments for generate llms" if rest.size > 1)
42
+ end
52
43
 
53
- def resume_problem
54
- "--resume reuses the pages saved by the last run, so --count, --all and --full-text cannot be used with it"
55
- end
44
+ def combination_problem(options, count)
45
+ return resume_problem if options.key?("resume") && fresh_option?(options, count)
56
46
 
57
- # The model client is built first, so a missing API key stops the command before any page is fetched.
58
- def run_generate_llms(site, options, count, only)
59
- client = wording_client(site, options)
60
- saved = saved_pages(site, options)
61
- collected = saved ? saved.collected : fresh_pages(site, options, count)
62
- languages = languages_to_write(site, client, only)
63
- files = write_llms_files(site, collected, client, languages)
64
- show_llms_report(collected, languages, files, saved)
65
- files.any? { |file| file.built.failure } ? 1 : 0
66
- end
47
+ "--count and --all cannot be used together" if count && options.key?("all")
48
+ end
67
49
 
68
- def saved_pages(site, options)
69
- return unless options.key?("resume")
50
+ def fresh_option?(options, count) = count || options.key?("all") || options.key?("full-text")
70
51
 
71
- Generate::Cache.load(pages_path(site), website: site.website)
72
- end
52
+ def resume_problem
53
+ "--resume reuses the pages saved by the last run, so --count, --all and --full-text cannot be used with it"
54
+ end
73
55
 
74
- # With --resume and a provider that needs a person, the answer is already in the answer file, so nothing is
75
- # asked: it is waited for. With an API provider, the model is simply asked again.
76
- def wording_client(site, options)
77
- return if options.key?("no-ai")
56
+ # The model client is built first, so a missing API key stops the command before any page is fetched.
57
+ def run_generate_llms(site, options, count, only)
58
+ client = wording_client(site, options)
59
+ saved = saved_pages(site, options)
60
+ collected = saved ? saved.collected : fresh_pages(site, options, count)
61
+ languages = languages_to_write(site, client, only)
62
+ files = write_llms_files(site, collected, client, languages)
63
+ show_llms_report(collected, languages, files, saved)
64
+ files.any? { |file| file.built.failure } ? 1 : 0
65
+ end
78
66
 
79
- provider = Llm::Providers.resolve(site.provider)
80
- if options.key?("resume") && provider.interactive?
81
- handoff = Llm::Handoff.new(input: @input, output: @out)
82
- return Generate::SavedAnswer.new(handoff:, model: Llm.models_for(site, provider).names.first,
83
- say: ->(message) { @out.puts message })
84
- end
67
+ def saved_pages(site, options)
68
+ return unless options.key?("resume")
85
69
 
86
- Llm.client_for(site, env: @env, transport: @llm_transport, log:)
87
- end
70
+ Generate::Cache.load(pages_path(site), website: site.website)
71
+ end
88
72
 
89
- # The language of the prompt that is waiting in the prompt file goes first, so its answer is used.
90
- def languages_to_write(site, client, only)
91
- languages = only ? [only] : site.languages
92
- client.is_a?(Generate::SavedAnswer) ? client.order(languages) : languages
93
- end
73
+ # With --resume and a provider that needs a person, the answer is already in the answer file, so nothing is
74
+ # asked: it is waited for. With an API provider, the model is simply asked again.
75
+ def wording_client(site, options)
76
+ return if options.key?("no-ai")
94
77
 
95
- def write_llms_files(site, collected, client, languages)
96
- builder = Generate::LlmsBuilder.new(client:, progress: ->(message) { @out.puts message }, log:)
97
- Generate::LlmsWriter.new(site:, builder:).call(collected, languages)
78
+ provider = Llm::Providers.resolve(site.provider)
79
+ if options.key?("resume") && provider.interactive?
80
+ handoff = Llm::Handoff.new(input: @input, output: @out)
81
+ return Generate::SavedAnswer.new(handoff:, model: Llm.models_for(site, provider).names.first,
82
+ say: ->(message) { @out.puts message })
98
83
  end
99
84
 
100
- def show_llms_report(collected, languages, files, saved)
101
- report = Generate::LlmsReport.new(collected:, requested: languages, files:, saved_at: saved&.saved_at)
102
- report.lines.each { |line| @out.puts line }
103
- end
85
+ Llm.client_for(site, env: @env, transport: @llm_transport, log:)
86
+ end
104
87
 
105
- def fresh_pages(site, options, count)
106
- limit = options.key?("all") ? nil : (count || DEFAULT_PAGES)
107
- collected = collect_pages(site, limit, full_text: options.key?("full-text"))
108
- Generate::Cache.save(pages_path(site), collected, website: site.website)
109
- @out.puts "Saved what was read to #{pages_path(site)}: if a later step is interrupted, " \
110
- "generate llms --resume continues from it without fetching again."
111
- collected
112
- end
88
+ # The language of the prompt that is waiting in the prompt file goes first, so its answer is used.
89
+ def languages_to_write(site, client, only)
90
+ languages = only ? [only] : site.languages
91
+ client.is_a?(Generate::SavedAnswer) ? client.order(languages) : languages
92
+ end
93
+
94
+ def write_llms_files(site, collected, client, languages)
95
+ builder = Generate::LlmsBuilder.new(client:, progress: ->(message) { @out.puts message }, log:)
96
+ Generate::LlmsWriter.new(site:, builder:).call(collected, languages)
97
+ end
113
98
 
114
- def pages_path(site) = File.join(File.expand_path(site.data_dir), PAGES_FILE)
99
+ def show_llms_report(collected, languages, files, saved)
100
+ report = Generate::LlmsReport.new(collected:, requested: languages, files:, saved_at: saved&.saved_at)
101
+ report.lines.each { |line| @out.puts line }
102
+ end
115
103
 
116
- def collect_pages(site, limit, full_text:)
117
- records = store_for(site).pages
118
- raise Error, "The Pages tab is empty. Run: yuno-proofreader pages import" if records.empty?
104
+ def fresh_pages(site, options, count)
105
+ limit = options.key?("all") ? nil : (count || DEFAULT_PAGES)
106
+ collected = collect_pages(site, limit, full_text: options.key?("full-text"))
107
+ Generate::Cache.save(pages_path(site), collected, website: site.website)
108
+ @out.puts "Saved what was read to #{pages_path(site)}: if a later step is interrupted, " \
109
+ "generate llms --resume continues from it without fetching again."
110
+ collected
111
+ end
119
112
 
120
- collector = Generate::Collector.new(site:, fetcher: Pages::Fetcher.new(site, http_client), full_text:,
121
- progress: ->(message) { @out.puts message })
122
- collected = collector.call(records, limit:)
123
- raise Error, nothing_read_message(collected) if collected.empty?
113
+ def pages_path(site) = File.join(File.expand_path(site.data_dir), PAGES_FILE)
124
114
 
125
- collected
126
- end
115
+ def collect_pages(site, limit, full_text:)
116
+ records = store_for(site).pages
117
+ raise Error, "The Pages tab is empty. Run: yuno-proofreader pages import" if records.empty?
127
118
 
128
- def nothing_read_message(collected)
129
- "None of the pages could be read, so nothing was written (#{collected.problems.first(3).join('; ')})."
130
- end
119
+ collector = Generate::Collector.new(site:, fetcher: Pages::Fetcher.new(site, http_client), full_text:,
120
+ progress: ->(message) { @out.puts message })
121
+ collected = collector.call(records, limit:)
122
+ raise Error, nothing_read_message(collected) if collected.empty?
123
+
124
+ collected
125
+ end
126
+
127
+ def nothing_read_message(collected)
128
+ "None of the pages could be read, so nothing was written (#{collected.problems.first(3).join('; ')})."
131
129
  end
132
130
  end
133
131
  end
@@ -1,130 +1,128 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- # The command that starts the settings file of a site: init.
6
- module InitCommand
7
- EXAMPLE = File.expand_path("../../../../proofreader.example.yml", __dir__)
8
- SWITCHES = %w[template yes offline].freeze
9
- VALUES = %w[website business audience tone sitemap].freeze
10
- LISTS = %w[language note skip-path rule ignore-term difference].freeze
11
- HEADER = ["Settings of yuno-proofreader, written by `yuno-proofreader init`.",
12
- "Every setting is explained in the template: run `yuno-proofreader init --template`.",
13
- "`yuno-proofreader settings` shows what an audit still needs."].freeze
14
-
15
- private
16
-
17
- def init(args)
18
- flags = Flags.parse(args, switches: SWITCHES, values: VALUES, lists: LISTS)
19
- problem = init_problem(flags)
20
- return usage_error(problem) if problem
21
-
22
- init_flow(flags)
23
- 0
24
- end
25
-
26
- def init_problem(flags) = flags.problem || ("Too many arguments for init" if flags.rest.size > 1)
27
-
28
- # Without --template the interview runs. With --template and no file name the template is only shown, and
29
- # with a file name it is written there.
30
- def init_flow(flags)
31
- path = flags.rest.first
32
- return init_interview(path || CLI::DEFAULT_SITE_FILE, flags) unless flags.switches.key?("template")
33
-
34
- path ? init_template(path) : print_template
35
- end
36
-
37
- # With no file name the template is only shown, so it can be read, or saved with a redirect.
38
- def print_template = @out.write(example_text)
39
-
40
- def init_template(path)
41
- create_file(path, example_text)
42
- @out.puts "Wrote #{path}. It is a template: every value that starts with [FILL IN] is for you to replace.",
43
- "Then run: yuno-proofreader settings #{path}"
44
- end
45
-
46
- def init_interview(path, flags)
47
- ensure_free(path)
48
- interview = Init::Interview.new(flags:, ask: Init::Ask.new(input: @input, out: @out),
49
- web: Init::Web.new(http: http_client), out: @out,
50
- offline: flags.switches.key?("offline"), yes: flags.switches.key?("yes"))
51
- settings = interview.call
52
- site = Site.from_hash(settings)
53
- create_file(path, SettingsFile.render(settings, header: HEADER))
54
- init_lines(path, site).each { |line| @out.puts line }
55
- end
56
-
57
- def ensure_free(path) = (raise ConfigError, taken_message(path) if File.exist?(path))
58
-
59
- def taken_message(path)
60
- "#{path} already exists, so it was left as it is. Delete it first, or choose another name: " \
61
- "yuno-proofreader init NAME.yml"
62
- end
63
-
64
- # Writes the file, and never over one that is already there.
65
- def create_file(path, text)
66
- File.open(path, File::WRONLY | File::CREAT | File::EXCL) { |file| file.write(text) }
67
- rescue Errno::EEXIST
68
- raise ConfigError, taken_message(path)
69
- rescue SystemCallError => e
70
- raise ConfigError, "Could not write #{path}: #{e.message}"
71
- end
72
-
73
- def example_text
74
- File.read(EXAMPLE)
75
- rescue SystemCallError
76
- raise Error, "The template settings file is missing from this installation (#{EXAMPLE})."
77
- end
78
-
79
- # ---- what is printed afterwards ----
80
-
81
- def init_lines(path, site)
82
- problems = Array(site.audit_problems)
83
- suffix = path == CLI::DEFAULT_SITE_FILE ? "" : " #{path}"
84
- ["", "Wrote #{path}.", *readiness(problems), "",
85
- "Outside this file you also need:", *environment_lines(site), "",
86
- "Next:", *next_steps(suffix)]
87
- end
88
-
89
- def readiness(problems)
90
- return ["It is ready for an audit."] if problems.empty?
91
-
92
- ["Before an audit can run, this is still missing:", *problems.map { |problem| " - #{problem}" }]
93
- end
94
-
95
- def environment_lines(site)
96
- provider = Llm::Providers.resolve(site.provider)
97
- rows = []
98
- rows << ["SPREADSHEET_ID", "the id of your Google Sheet"] if site.store == "sheets"
99
- rows << [provider.api_key_env, "the API key of #{provider.id}"] if provider.api_key_env
100
- [*rows.map { |name, meaning| environment_line(name, meaning) }, *provider_hint(provider),
101
- " Put them in a .env file in this folder, or export them.", *google_hint(site)]
102
- end
103
-
104
- def environment_line(name, meaning)
105
- state = @env[name].to_s.strip.empty? ? "not set" : "set"
106
- " #{name.ljust(18)} #{state.ljust(8)} (#{meaning})"
107
- end
108
-
109
- def next_steps(suffix)
110
- [" yuno-proofreader prepare#{suffix} creates the tabs (CSV files, or a Google Sheet)",
111
- " yuno-proofreader pages import#{suffix} adds your pages",
112
- " yuno-proofreader analyze#{suffix} ranks the pages by importance",
113
- " yuno-proofreader audit --next#{suffix} audits the most important page that is due"]
114
- end
115
-
116
- def provider_hint(provider)
117
- return [] if provider.api_key_env
118
-
119
- [" No model account is needed: each prompt is saved to tmp/handoff/prompt.md for you to paste into a chat.",
120
- " To call a model automatically, set provider: gemini in the settings file and export GEMINI_API_KEY."]
121
- end
122
-
123
- def google_hint(site)
124
- return [] unless site.store == "sheets"
125
-
126
- [" Google credentials: see the README, section \"Connecting to Google\""]
127
- end
3
+ module Yuno::Proofreader
4
+ # The command that starts the settings file of a site: init.
5
+ module InitCommand
6
+ EXAMPLE = File.expand_path("../../../../proofreader.example.yml", __dir__)
7
+ SWITCHES = %w[template yes offline].freeze
8
+ VALUES = %w[website business audience tone sitemap].freeze
9
+ LISTS = %w[language note skip-path rule ignore-term difference].freeze
10
+ HEADER = ["Settings of yuno-proofreader, written by `yuno-proofreader init`.",
11
+ "Every setting is explained in the template: run `yuno-proofreader init --template`.",
12
+ "`yuno-proofreader settings` shows what an audit still needs."].freeze
13
+
14
+ private
15
+
16
+ def init(args)
17
+ flags = Flags.parse(args, switches: SWITCHES, values: VALUES, lists: LISTS)
18
+ problem = init_problem(flags)
19
+ return usage_error(problem) if problem
20
+
21
+ init_flow(flags)
22
+ 0
23
+ end
24
+
25
+ def init_problem(flags) = flags.problem || ("Too many arguments for init" if flags.rest.size > 1)
26
+
27
+ # Without --template the interview runs. With --template and no file name the template is only shown, and
28
+ # with a file name it is written there.
29
+ def init_flow(flags)
30
+ path = flags.rest.first
31
+ return init_interview(path || CLI::DEFAULT_SITE_FILE, flags) unless flags.switches.key?("template")
32
+
33
+ path ? init_template(path) : print_template
34
+ end
35
+
36
+ # With no file name the template is only shown, so it can be read, or saved with a redirect.
37
+ def print_template = @out.write(example_text)
38
+
39
+ def init_template(path)
40
+ create_file(path, example_text)
41
+ @out.puts "Wrote #{path}. It is a template: every value that starts with [FILL IN] is for you to replace.",
42
+ "Then run: yuno-proofreader settings #{path}"
43
+ end
44
+
45
+ def init_interview(path, flags)
46
+ ensure_free(path)
47
+ interview = Init::Interview.new(flags:, ask: Init::Ask.new(input: @input, out: @out),
48
+ web: Init::Web.new(http: http_client), out: @out,
49
+ offline: flags.switches.key?("offline"), yes: flags.switches.key?("yes"))
50
+ settings = interview.call
51
+ site = Site.from_hash(settings)
52
+ create_file(path, SettingsFile.render(settings, header: HEADER))
53
+ init_lines(path, site).each { |line| @out.puts line }
54
+ end
55
+
56
+ def ensure_free(path) = (raise ConfigError, taken_message(path) if File.exist?(path))
57
+
58
+ def taken_message(path)
59
+ "#{path} already exists, so it was left as it is. Delete it first, or choose another name: " \
60
+ "yuno-proofreader init NAME.yml"
61
+ end
62
+
63
+ # Writes the file, and never over one that is already there.
64
+ def create_file(path, text)
65
+ File.open(path, File::WRONLY | File::CREAT | File::EXCL) { |file| file.write(text) }
66
+ rescue Errno::EEXIST
67
+ raise ConfigError, taken_message(path)
68
+ rescue SystemCallError => e
69
+ raise ConfigError, "Could not write #{path}: #{e.message}"
70
+ end
71
+
72
+ def example_text
73
+ File.read(EXAMPLE)
74
+ rescue SystemCallError
75
+ raise Error, "The template settings file is missing from this installation (#{EXAMPLE})."
76
+ end
77
+
78
+ # ---- what is printed afterwards ----
79
+
80
+ def init_lines(path, site)
81
+ problems = Array(site.audit_problems)
82
+ suffix = path == CLI::DEFAULT_SITE_FILE ? "" : " #{path}"
83
+ ["", "Wrote #{path}.", *readiness(problems), "",
84
+ "Outside this file you also need:", *environment_lines(site), "",
85
+ "Next:", *next_steps(suffix)]
86
+ end
87
+
88
+ def readiness(problems)
89
+ return ["It is ready for an audit."] if problems.empty?
90
+
91
+ ["Before an audit can run, this is still missing:", *problems.map { |problem| " - #{problem}" }]
92
+ end
93
+
94
+ def environment_lines(site)
95
+ provider = Llm::Providers.resolve(site.provider)
96
+ rows = []
97
+ rows << ["SPREADSHEET_ID", "the id of your Google Sheet"] if site.store == "sheets"
98
+ rows << [provider.api_key_env, "the API key of #{provider.id}"] if provider.api_key_env
99
+ [*rows.map { |name, meaning| environment_line(name, meaning) }, *provider_hint(provider),
100
+ " Put them in a .env file in this folder, or export them.", *google_hint(site)]
101
+ end
102
+
103
+ def environment_line(name, meaning)
104
+ state = @env[name].to_s.strip.empty? ? "not set" : "set"
105
+ " #{name.ljust(18)} #{state.ljust(8)} (#{meaning})"
106
+ end
107
+
108
+ def next_steps(suffix)
109
+ [" yuno-proofreader prepare#{suffix} creates the tabs (CSV files, or a Google Sheet)",
110
+ " yuno-proofreader pages import#{suffix} adds your pages",
111
+ " yuno-proofreader analyze#{suffix} ranks the pages by importance",
112
+ " yuno-proofreader audit --next#{suffix} audits the most important page that is due"]
113
+ end
114
+
115
+ def provider_hint(provider)
116
+ return [] if provider.api_key_env
117
+
118
+ [" No model account is needed: each prompt is saved to tmp/handoff/prompt.md for you to paste into a chat.",
119
+ " To call a model automatically, set provider: gemini in the settings file and export GEMINI_API_KEY."]
120
+ end
121
+
122
+ def google_hint(site)
123
+ return [] unless site.store == "sheets"
124
+
125
+ [" Google credentials: see the README, section \"Connecting to Google\""]
128
126
  end
129
127
  end
130
128
  end
@@ -1,42 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- # The command that works with the model provider: models.
6
- module ModelCommands
7
- private
8
-
9
- def models(args)
10
- file, problem = single_file(args, "models")
11
- return usage_error(problem) if problem
12
-
13
- site = Site.load(file)
14
- provider = Llm.provider_for(site, env: @env, transport: @llm_transport)
15
- selection = Llm.models_for(site, provider.class)
16
- used = selection.names.map { |name| provider.normalize(name) }
17
- print_models(provider, provider.models, used, selection.source)
18
- 0
19
- end
20
-
21
- def print_models(provider, available, used, source)
22
- return print_manual_models(used, source) unless provider.class.api_key_env
23
-
24
- @out.puts "Models your #{provider.class.id} key can use for text generation:"
25
- available.each { |name| @out.puts " #{name}#{' (in use)' if used.include?(name)}" }
26
- @out.puts "", "In use (#{origin_of(source)}): #{used.join(', ')}"
27
- missing = used - available
28
- return if missing.empty?
29
-
30
- @out.puts "Not available to this key: #{missing.join(', ')}. A model that fails is skipped, " \
31
- "so set models in proofreader.yml to the ones you want."
32
- end
33
-
34
- def print_manual_models(used, source)
35
- @out.puts "The manual provider has no list of models: you pick the model in the chat yourself.",
36
- "Label shown with each prompt (#{origin_of(source)}): #{used.join(', ')}"
37
- end
38
-
39
- def origin_of(source) = source == :settings ? "from your settings" : "built-in default"
3
+ module Yuno::Proofreader
4
+ # The command that works with the model provider: models.
5
+ module ModelCommands
6
+ private
7
+
8
+ def models(args)
9
+ file, problem = single_file(args, "models")
10
+ return usage_error(problem) if problem
11
+
12
+ site = Site.load(file)
13
+ provider = Llm.provider_for(site, env: @env, transport: @llm_transport)
14
+ selection = Llm.models_for(site, provider.class)
15
+ used = selection.names.map { |name| provider.normalize(name) }
16
+ print_models(provider, provider.models, used, selection.source)
17
+ 0
40
18
  end
19
+
20
+ def print_models(provider, available, used, source)
21
+ return print_manual_models(used, source) unless provider.class.api_key_env
22
+
23
+ @out.puts "Models your #{provider.class.id} key can use for text generation:"
24
+ available.each { |name| @out.puts " #{name}#{' (in use)' if used.include?(name)}" }
25
+ @out.puts "", "In use (#{origin_of(source)}): #{used.join(', ')}"
26
+ missing = used - available
27
+ return if missing.empty?
28
+
29
+ @out.puts "Not available to this key: #{missing.join(', ')}. A model that fails is skipped, " \
30
+ "so set models in proofreader.yml to the ones you want."
31
+ end
32
+
33
+ def print_manual_models(used, source)
34
+ @out.puts "The manual provider has no list of models: you pick the model in the chat yourself.",
35
+ "Label shown with each prompt (#{origin_of(source)}): #{used.join(', ')}"
36
+ end
37
+
38
+ def origin_of(source) = source == :settings ? "from your settings" : "built-in default"
41
39
  end
42
40
  end