sdsforge 0.5.0

CLI and desktop app for generating, converting, validating, and rendering chemical safety data sheets in MHLW/JIS Z 7253 structured formats.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! Shared CLI/GUI implementation for the `sdsforge` binary.
//!
//! Exposes [`run_cli_from`] and [`run_gui`] so the deprecated `sdsconv` compat
//! binary (see `../sdsconv`) can execute the exact same command parsing and
//! task implementations instead of duplicating them — the old binary forwards
//! its argv into [`run_cli_from`] rather than reimplementing anything.

mod app;
mod config;
mod evidence;
mod tasks;

use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use clap::{Parser, Subcommand, ValueEnum};
use futures::stream::{self, StreamExt};
use indicatif::{ProgressBar, ProgressStyle};
use sdsforge_core::{Language, SourceCountry, SdsRoot};

use tasks::{
    AssistParams, EvalCorpusParams, LogFn, Provider, Quality, RenderFormat, RenderParams,
    ToDocxParams, ToHtmlParams, ToJsonParams, ToPdfParams,
    check_json_file_size, collect_files,
};

// ---------------------------------------------------------------------------
// CLI-specific enums (thin wrappers around tasks::{Provider, Quality, ...})
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, ValueEnum, Default)]
enum CliQuality {
    Low,
    #[default]
    Medium,
    High,
    /// Maximum output tokens (65 536) for very long SDS documents.
    Max,
}

impl From<CliQuality> for Quality {
    fn from(q: CliQuality) -> Self {
        match q {
            CliQuality::Low    => Quality::Low,
            CliQuality::Medium => Quality::Medium,
            CliQuality::High   => Quality::High,
            CliQuality::Max    => Quality::Max,
        }
    }
}

#[derive(Clone, Copy, ValueEnum)]
enum CliLanguage {
    Ja,
    En,
    ZhCn,
    ZhTw,
}

impl From<CliLanguage> for Language {
    fn from(l: CliLanguage) -> Self {
        match l {
            CliLanguage::Ja   => Language::Japanese,
            CliLanguage::En   => Language::English,
            CliLanguage::ZhCn => Language::ChineseSimplified,
            CliLanguage::ZhTw => Language::ChineseTraditional,
        }
    }
}

#[derive(Clone, Copy, ValueEnum)]
enum CliCountry {
    /// Japan (JIS Z 7253 / MHLW)
    Jp,
    /// China (GB/T 16483)
    Cn,
    /// Taiwan (CNS 15030)
    Tw,
    /// Korea (K-GHS Rev.6)
    Kr,
}

impl From<CliCountry> for SourceCountry {
    fn from(c: CliCountry) -> Self {
        match c {
            CliCountry::Jp => SourceCountry::Japan,
            CliCountry::Cn => SourceCountry::China,
            CliCountry::Tw => SourceCountry::Taiwan,
            CliCountry::Kr => SourceCountry::Korea,
        }
    }
}

#[derive(Clone, Copy, ValueEnum, Default)]
enum CliProvider {
    #[default]
    Anthropic,
    Openai,
    Gemini,
    Mistral,
    Groq,
    Cohere,
    Local,
}

impl From<CliProvider> for Provider {
    fn from(p: CliProvider) -> Self {
        match p {
            CliProvider::Anthropic => Provider::Anthropic,
            CliProvider::Openai    => Provider::Openai,
            CliProvider::Gemini    => Provider::Gemini,
            CliProvider::Mistral   => Provider::Mistral,
            CliProvider::Groq      => Provider::Groq,
            CliProvider::Cohere    => Provider::Cohere,
            CliProvider::Local     => Provider::Local,
        }
    }
}

/// `--to` value for `sdsforge render`. Kebab-case values (`docx`/`html`/`pdf`)
/// come from clap's default `ValueEnum` naming.
#[derive(Clone, Copy, ValueEnum)]
enum CliRenderFormat {
    Docx,
    Html,
    Pdf,
}

impl From<CliRenderFormat> for RenderFormat {
    fn from(f: CliRenderFormat) -> Self {
        match f {
            CliRenderFormat::Docx => RenderFormat::Docx,
            CliRenderFormat::Html => RenderFormat::Html,
            CliRenderFormat::Pdf  => RenderFormat::Pdf,
        }
    }
}

// ---------------------------------------------------------------------------
// Clap CLI definition
// ---------------------------------------------------------------------------

#[derive(Parser)]
#[command(name = "sdsforge", about = "Generate, convert, translate, and validate SDS documents against MHLW standard JSON")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

/// `--source-kind` value for `sdsforge assist`. Assist v1 only accepts
/// supplier SDS documents — a typed, required (no default) enum so the
/// caller states out loud what kind of document `--source` is, since that
/// becomes `AssistRun::source_evidence_level`.
#[derive(Clone, Copy, ValueEnum)]
enum CliSourceKind {
    SupplierSds,
}

/// `--profile` value for `sdsforge generate`. Only `mhlw-v1` exists today —
/// a typed enum (rather than a free-form string) so requesting an
/// unsupported profile fails clearly through clap rather than silently
/// falling back to MHLW.
#[derive(Clone, Copy, ValueEnum, Default)]
enum CliGenerationProfile {
    #[default]
    MhlwV1,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate an SDS draft (official JSON + generation report + review report) from a product formulation
    Generate {
        /// Product formulation input (.json, .yaml, or .yml)
        #[arg(short, long)]
        input: PathBuf,
        /// Directory to write official_sds.json / generation_report.json / review_report.md
        #[arg(short, long)]
        output_dir: PathBuf,
        #[arg(long, value_enum, default_value = "mhlw-v1")]
        profile: CliGenerationProfile,
        /// Resolve CAS numbers through PubChem and normalize returned structures.
        /// Only CAS numbers are sent -- product name, concentrations, supplier, and
        /// evidence data never leave the machine.
        #[arg(long)]
        enrich: bool,
        /// Exit with a non-zero status if the generated draft is Blocked (artifacts are still written).
        #[arg(long)]
        strict: bool,
        /// Permit replacing existing generation artifacts in --output-dir.
        #[arg(long)]
        force: bool,
    },

    /// Convert a PDF, Word document, HTML file, or URL to MHLW standard JSON
    ToJson {
        #[arg(short, long, conflicts_with = "input_dir")]
        input: Option<String>,
        #[arg(long, conflicts_with = "input")]
        input_dir: Option<PathBuf>,
        #[arg(short, long, conflicts_with = "output_dir")]
        output: Option<PathBuf>,
        #[arg(long, conflicts_with = "output")]
        output_dir: Option<PathBuf>,
        /// API key (or set ANTHROPIC_API_KEY / OPENAI_API_KEY env var)
        #[arg(long, hide_env_values = true)]
        api_key: Option<String>,
        #[arg(long, value_enum)]
        lang: Option<CliLanguage>,
        /// Target country for country-specific extraction rules, validation, and compliance report.
        /// Inferred from --lang when omitted (zh-cn → cn, zh-tw → tw, ja → jp).
        #[arg(long, value_enum)]
        country: Option<CliCountry>,
        #[arg(long)]
        model: Option<String>,
        #[arg(long, value_enum, default_value = "anthropic")]
        provider: CliProvider,
        #[arg(long)]
        base_url: Option<String>,
        #[arg(long, value_enum, default_value = "medium")]
        quality: CliQuality,
        #[arg(long, default_value = "4")]
        concurrency: usize,
        #[arg(long)]
        enrich: bool,
        /// Run the validation-driven correction pass after primary extraction.
        ///
        /// Fixes invalid GHS H/P-codes via a targeted second LLM call and
        /// corrects CAS check-digit errors deterministically.
        #[arg(long)]
        correct: bool,
        /// Use the MHLW-recommended filename: SDS_<date>_<product_code>.json
        #[arg(long)]
        suggested_name: bool,
    },

    /// Propose Section 4 (first-aid measures) candidate values from a supplier SDS,
    /// for a human to review. Writes exactly one JSON proposal file — never touches
    /// official_sds.json, generation artifacts, or any ProductInput file.
    Assist {
        /// Supplier SDS document (PDF/DOCX/XLSX/HTML/TXT) — assist v1 accepts exactly one.
        #[arg(long)]
        source: PathBuf,
        /// What kind of document `--source` is. Assist v1 only accepts supplier-sds.
        #[arg(long, value_enum)]
        source_kind: CliSourceKind,
        /// SDS section to extract candidates for. Assist v1 only supports 4 (first-aid measures).
        #[arg(long)]
        section: u32,
        #[arg(long)]
        output: PathBuf,
        /// API key (or set ANTHROPIC_API_KEY / OPENAI_API_KEY env var)
        #[arg(long, hide_env_values = true)]
        api_key: Option<String>,
        #[arg(long)]
        model: Option<String>,
        #[arg(long, value_enum, default_value = "anthropic")]
        provider: CliProvider,
        #[arg(long)]
        base_url: Option<String>,
    },

    /// Render a structured SDS/JSON document as DOCX, HTML, or PDF
    Render {
        #[arg(short, long, conflicts_with = "input_dir")]
        input: Option<PathBuf>,
        #[arg(long, conflicts_with = "input")]
        input_dir: Option<PathBuf>,
        #[arg(short, long, conflicts_with = "output_dir")]
        output: Option<PathBuf>,
        #[arg(long, conflicts_with = "output")]
        output_dir: Option<PathBuf>,
        /// Output format
        #[arg(long, value_enum)]
        to: CliRenderFormat,
        #[arg(long, value_enum, default_value = "ja")]
        lang: CliLanguage,
        /// Word template (.docx) — only used with `--to docx`
        #[arg(long)]
        template: Option<PathBuf>,
    },

    /// (deprecated — use `render --to docx`) Convert MHLW standard JSON to a Word document
    ToDocx {
        #[arg(short, long, conflicts_with = "input_dir")]
        input: Option<PathBuf>,
        #[arg(long, conflicts_with = "input")]
        input_dir: Option<PathBuf>,
        #[arg(short, long, conflicts_with = "output_dir")]
        output: Option<PathBuf>,
        #[arg(long, conflicts_with = "output")]
        output_dir: Option<PathBuf>,
        #[arg(long, value_enum, default_value = "ja")]
        lang: CliLanguage,
        #[arg(long)]
        template: Option<PathBuf>,
    },

    /// Validate a MHLW standard JSON file and report structural issues
    Validate {
        #[arg(short, long)]
        input: PathBuf,
        /// Output warnings as a JSON array (useful for CI)
        #[arg(long)]
        json: bool,
        /// Strict MHLW mode: exit 1 if any HIGH or CRIT finding is present
        #[arg(long)]
        strict_mhlw: bool,
    },

    /// (deprecated — use `render --to html`) Convert MHLW standard JSON to an HTML document
    ToHtml {
        #[arg(short, long, conflicts_with = "input_dir")]
        input: Option<PathBuf>,
        #[arg(long, conflicts_with = "input")]
        input_dir: Option<PathBuf>,
        #[arg(short, long, conflicts_with = "output_dir")]
        output: Option<PathBuf>,
        #[arg(long, conflicts_with = "output")]
        output_dir: Option<PathBuf>,
        #[arg(long, value_enum, default_value = "ja")]
        lang: CliLanguage,
    },

    /// (deprecated — use `render --to pdf`) Convert MHLW standard JSON to a PDF document
    ToPdf {
        #[arg(short, long, conflicts_with = "input_dir")]
        input: Option<PathBuf>,
        #[arg(long, conflicts_with = "input")]
        input_dir: Option<PathBuf>,
        #[arg(short, long, conflicts_with = "output_dir")]
        output: Option<PathBuf>,
        #[arg(long, conflicts_with = "output")]
        output_dir: Option<PathBuf>,
        #[arg(long, value_enum, default_value = "ja")]
        lang: CliLanguage,
    },

    /// Extract raw text from a document or URL (no LLM)
    ExtractText {
        #[arg(short, long)]
        input: String,
        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    /// Detect the language of an SDS document or URL
    DetectLang {
        #[arg(short, long)]
        input: String,
    },

    /// Evaluate a corpus of SDS files and produce quality scores + causasv features
    EvalCorpus {
        /// Directory containing SDS files (PDF/DOCX/XLSX/HTML/TXT)
        #[arg(long)]
        input_dir: PathBuf,
        /// Output directory for all reports and generated JSON
        #[arg(long)]
        output_dir: PathBuf,
        /// Number of parallel workers
        #[arg(long, default_value = "8")]
        jobs: usize,
        /// Exit 1 if any HIGH or CRIT finding exists across the corpus
        #[arg(long)]
        strict_mhlw: bool,
        /// Run validation-driven correction pass (extra LLM call)
        #[arg(long)]
        correct: bool,
        /// Enrich CAS numbers via PubChem
        #[arg(long)]
        enrich: bool,
        /// Source language (omit for auto-detect)
        #[arg(long, value_enum)]
        lang: Option<CliLanguage>,
        #[arg(long, value_enum)]
        country: Option<CliCountry>,
        #[arg(long, value_enum, default_value = "anthropic")]
        provider: CliProvider,
        #[arg(long, env = "ANTHROPIC_API_KEY")]
        api_key: Option<String>,
        #[arg(long, default_value = "claude-sonnet-4-5")]
        model: String,
        #[arg(long, value_enum, default_value = "medium")]
        quality: CliQuality,
        #[arg(long)]
        base_url: Option<String>,
        /// Limit number of files processed (useful for smoke tests)
        #[arg(long)]
        max_files: Option<usize>,
        /// Path to quality_check.py (default: tools/quality_check.py)
        #[arg(long)]
        qc_script: Option<PathBuf>,
    },
}

// ---------------------------------------------------------------------------
// Process-wide setup, shared by both the `sdsforge` binary and the
// deprecated `sdsconv` compat binary.
// ---------------------------------------------------------------------------

/// Install the pdf-extract panic filter and initialise tracing. Call once,
/// before branching into CLI or GUI mode.
pub fn init_process() {
    // Suppress the noisy panic backtrace that Rust's default panic hook emits for
    // pdf-extract crate panics.  Those panics are always caught by std::panic::catch_unwind
    // in sdsforge_core::converter::extractor and do not represent real failures —
    // they happen when pdf-extract encounters Shift-JIS / CID-font encoded PDFs, after
    // which the code falls back to pdftotext / OCR automatically.
    //
    // We install a custom hook that forwards everything to the original hook *except*
    // panics originating from files inside the pdf-extract crate.
    {
        let default_hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(move |info| {
            let file = info.location().map(|l| l.file()).unwrap_or("");
            if file.contains("pdf-extract") || file.contains("pdf_extract") {
                // Panic is caught upstream by catch_unwind — silently discard.
                return;
            }
            default_hook(info);
        }));
    }

    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")),
        )
        .with_writer(std::io::stderr)
        .init();
}

/// Launch the GUI. Thin re-export so both binaries share one call site.
pub fn run_gui() -> anyhow::Result<()> {
    app::run_gui()
}

/// Parse `args` as an `sdsforge` command line and execute it.
///
/// Used by the real `sdsforge` binary (with `std::env::args_os()`) and by the
/// deprecated `sdsconv` compat binary (forwarding its own argv), so both share
/// identical parsing and command execution — no duplicated CLI logic.
///
/// `args[0]` is conventionally the program name; clap only uses it as a
/// placeholder since `#[command(name = "sdsforge")]` fixes the displayed name
/// regardless of which binary actually launched the process.
pub async fn run_cli_from<I, T>(args: I) -> anyhow::Result<()>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let cli = Cli::parse_from(args);
    run_cli(cli).await
}

// ---------------------------------------------------------------------------
// CLI handler
// ---------------------------------------------------------------------------

async fn run_cli(cli: Cli) -> anyhow::Result<()> {
    let cfg = config::AppConfig::load();
    let log: LogFn = tasks::stdout_log();

    match cli.command {
        Commands::Generate {
            input, output_dir, profile: CliGenerationProfile::MhlwV1,
            enrich, strict, force,
        } => {
            let outcome = tasks::run_generate(
                tasks::GenerateParams { input, output_dir, enrich, force },
                Arc::clone(&log),
            ).await?;

            if outcome.release_status == sdsforge_core::ReleaseStatus::Blocked {
                eprintln!(
                    "Release status: BLOCKED ({} blocking finding(s), {} unresolved field(s)) — see review_report.md",
                    outcome.blocking_findings_count, outcome.unresolved_count
                );
            }
            if strict && outcome.release_status == sdsforge_core::ReleaseStatus::Blocked {
                std::process::exit(1);
            }
        }

        Commands::Assist {
            source, source_kind: CliSourceKind::SupplierSds, section, output,
            api_key, model, provider, base_url,
        } => {
            if section != 4 {
                anyhow::bail!("assist v1 only supports --section 4 (first-aid measures)");
            }
            let provider = Provider::from(provider);
            let api_key  = resolve_api_key(api_key, provider, &cfg)?;
            let model    = model.unwrap_or_else(|| provider.default_model(Quality::Medium).to_string());

            tasks::run_assist(
                AssistParams { source, output, provider, api_key, model, base_url },
                Arc::clone(&log),
            ).await?;
        }

        Commands::ToJson {
            input, input_dir, output, output_dir,
            api_key, lang, country, model, provider, base_url, quality, concurrency,
            enrich, correct, suggested_name,
        } => {
            let provider = Provider::from(provider);
            let quality  = Quality::from(quality);
            let api_key  = resolve_api_key(api_key, provider, &cfg)?;
            let model    = model.unwrap_or_else(|| provider.default_model(quality).to_string());
            let country  = country.map(SourceCountry::from);

            eprintln!(
                "Quality: {} (max_chars={}, max_tokens={}, model={})",
                quality.label(), quality.max_chars(), quality.max_tokens(), model
            );

            match (input, input_dir) {
                (Some(input), None) => {
                    let output = output.ok_or_else(|| anyhow::anyhow!("--output required"))?;
                    tasks::run_to_json(ToJsonParams {
                        input, output, provider, api_key, model, quality,
                        lang: lang.map(Language::from), country, base_url, enrich, correct,
                        use_suggested_filename: suggested_name,
                    }, Arc::clone(&log)).await?;
                }
                (None, Some(dir)) => {
                    let out_dir = output_dir.ok_or_else(|| anyhow::anyhow!("--output-dir required"))?;
                    std::fs::create_dir_all(&out_dir)?;
                    batch_to_json(
                        &dir, &out_dir, provider, api_key, model, quality,
                        lang.map(Language::from), country, base_url, concurrency, enrich, correct,
                        suggested_name,
                    ).await;
                }
                _ => anyhow::bail!("Specify either --input or --input-dir"),
            }
        }

        Commands::Render { input, input_dir, output, output_dir, to, lang, template } => {
            let lang = Language::from(lang);
            let format = RenderFormat::from(to);
            match (input, input_dir) {
                (Some(input), None) => {
                    let output = output.ok_or_else(|| anyhow::anyhow!("--output required"))?;
                    tasks::run_render(
                        RenderParams { input, output, lang, format, template },
                        Arc::clone(&log),
                    ).await?;
                }
                (None, Some(dir)) => {
                    let out_dir = output_dir.ok_or_else(|| anyhow::anyhow!("--output-dir required"))?;
                    std::fs::create_dir_all(&out_dir)?;
                    match format {
                        RenderFormat::Docx => batch_to_docx(&dir, &out_dir, lang, template.as_deref())?,
                        RenderFormat::Html => batch_to_html(&dir, &out_dir, lang)?,
                        RenderFormat::Pdf  => batch_to_pdf(&dir, &out_dir, lang)?,
                    }
                }
                _ => anyhow::bail!("Specify either --input or --input-dir"),
            }
        }

        Commands::ToDocx { input, input_dir, output, output_dir, lang, template } => {
            eprintln!("warning: `sdsforge to-docx` is deprecated; use `sdsforge render --to docx`");
            match (input, input_dir) {
                (Some(input), None) => {
                    let output = output.ok_or_else(|| anyhow::anyhow!("--output required"))?;
                    tasks::run_to_docx(
                        ToDocxParams { input, output, lang: Language::from(lang), template },
                        Arc::clone(&log),
                    ).await?;
                }
                (None, Some(dir)) => {
                    let out_dir = output_dir.ok_or_else(|| anyhow::anyhow!("--output-dir required"))?;
                    std::fs::create_dir_all(&out_dir)?;
                    batch_to_docx(&dir, &out_dir, Language::from(lang), template.as_deref())?;
                }
                _ => anyhow::bail!("Specify either --input or --input-dir"),
            }
        }

        Commands::Validate { input, json, strict_mhlw } => {
            let findings = tasks::run_validate(input, Arc::clone(&log)).await?;
            if json {
                println!("{}", serde_json::to_string_pretty(&findings)?);
            }
            if strict_mhlw {
                let has_high_or_crit = findings
                    .iter()
                    .any(|f| f.level == "HIGH" || f.level == "CRIT");
                if has_high_or_crit {
                    eprintln!("strict-mhlw: HIGH/CRIT findings present — see warnings above.");
                    std::process::exit(1);
                }
            } else if !findings.is_empty() && !json {
                std::process::exit(1);
            }
        }

        Commands::ToHtml { input, input_dir, output, output_dir, lang } => {
            eprintln!("warning: `sdsforge to-html` is deprecated; use `sdsforge render --to html`");
            match (input, input_dir) {
                (Some(input), None) => {
                    let output = output.ok_or_else(|| anyhow::anyhow!("--output required"))?;
                    tasks::run_to_html(
                        ToHtmlParams { input, output, lang: Language::from(lang) },
                        Arc::clone(&log),
                    ).await?;
                }
                (None, Some(dir)) => {
                    let out_dir = output_dir.ok_or_else(|| anyhow::anyhow!("--output-dir required"))?;
                    std::fs::create_dir_all(&out_dir)?;
                    batch_to_html(&dir, &out_dir, Language::from(lang))?;
                }
                _ => anyhow::bail!("Specify either --input or --input-dir"),
            }
        }

        Commands::ToPdf { input, input_dir, output, output_dir, lang } => {
            eprintln!("warning: `sdsforge to-pdf` is deprecated; use `sdsforge render --to pdf`");
            match (input, input_dir) {
                (Some(input), None) => {
                    let output = output.ok_or_else(|| anyhow::anyhow!("--output required"))?;
                    tasks::run_to_pdf(
                        ToPdfParams { input, output, lang: Language::from(lang) },
                        Arc::clone(&log),
                    ).await?;
                }
                (None, Some(dir)) => {
                    let out_dir = output_dir.ok_or_else(|| anyhow::anyhow!("--output-dir required"))?;
                    std::fs::create_dir_all(&out_dir)?;
                    batch_to_pdf(&dir, &out_dir, Language::from(lang))?;
                }
                _ => anyhow::bail!("Specify either --input or --input-dir"),
            }
        }

        Commands::ExtractText { input, output } => {
            use anyhow::Context as _;
            use sdsforge_core::{extract_text, extract_text_from_url};
            let is_url = input.starts_with("http://") || input.starts_with("https://");
            let text = if is_url {
                extract_text_from_url(&input).await?
            } else {
                extract_text(Path::new(&input)).await?
            };
            match output {
                Some(path) => {
                    std::fs::write(&path, &text)
                        .with_context(|| format!("writing {}", path.display()))?;
                    eprintln!("Saved text to {}", path.display());
                }
                None => print!("{text}"),
            }
        }

        Commands::DetectLang { input } => {
            use sdsforge_core::{detect_language_from_file, detect_language_from_url};
            let is_url = input.starts_with("http://") || input.starts_with("https://");
            let lang = if is_url {
                detect_language_from_url(&input).await?
            } else {
                detect_language_from_file(Path::new(&input)).await?
            };
            println!("{} ({})", lang.name_en(), lang.bcp47());
        }

        Commands::EvalCorpus {
            input_dir, output_dir, jobs, strict_mhlw, correct, enrich,
            lang, country, provider, api_key, model, quality, base_url,
            max_files, qc_script,
        } => {
            let provider = Provider::from(provider);
            let api_key  = resolve_api_key(api_key, provider, &cfg)?;
            let lang     = lang.map(Language::from);
            let country  = country.map(SourceCountry::from);
            // Default qc_script: tools/quality_check.py relative to current dir
            let qc_script = qc_script.unwrap_or_else(|| PathBuf::from("tools/quality_check.py"));
            std::fs::create_dir_all(&output_dir)?;
            tasks::run_eval_corpus(EvalCorpusParams {
                input_dir, output_dir, provider, api_key,
                model, quality: Quality::from(quality),
                lang, country, base_url, jobs, correct, enrich, strict_mhlw,
                max_files, qc_script,
            }, log).await?;
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// API key resolution
// ---------------------------------------------------------------------------

fn resolve_api_key(
    cli_key: Option<String>,
    provider: Provider,
    cfg: &config::AppConfig,
) -> anyhow::Result<String> {
    if let Some(k) = cli_key {
        eprintln!(
            "Warning: passing --api-key on the command line may expose it to other local \
             processes. Prefer setting the environment variable instead."
        );
        return Ok(k);
    }
    let env_var = provider.api_key_env();
    if let Ok(k) = std::env::var(env_var) {
        return Ok(k);
    }
    if provider == Provider::Local {
        return Ok("ollama".to_string());
    }
    // Fall back to config file if provider matches
    if !cfg.api_key.is_empty() && Provider::from_str(&cfg.provider) == provider {
        return Ok(cfg.api_key.clone());
    }
    anyhow::bail!("API key not provided. Set --api-key or {env_var}")
}

// ---------------------------------------------------------------------------
// Batch helpers — CLI only (with indicatif progress bars)
// The per-file work delegates to the shared tasks:: runners.
// ---------------------------------------------------------------------------

fn make_pb(total: u64) -> Arc<ProgressBar> {
    let pb = ProgressBar::new(total);
    pb.set_style(
        ProgressStyle::with_template("[{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} {msg}")
            .unwrap_or_else(|_| ProgressStyle::default_bar()),
    );
    Arc::new(pb)
}

#[allow(clippy::too_many_arguments)]
async fn batch_to_json(
    input_dir: &Path,
    output_dir: &Path,
    provider: Provider,
    api_key: String,
    model: String,
    quality: Quality,
    lang: Option<Language>,
    country: Option<SourceCountry>,
    base_url: Option<String>,
    concurrency: usize,
    enrich: bool,
    correct: bool,
    use_suggested_filename: bool,
) {
    let files = collect_files(input_dir, &["pdf", "docx", "xlsx", "xls"]);
    let total = files.len();
    if total == 0 {
        eprintln!("No files found in {}", input_dir.display());
        return;
    }
    let pb = make_pb(total as u64);
    let output_dir = Arc::new(output_dir.to_path_buf());
    let ok     = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let failed = Arc::new(std::sync::atomic::AtomicUsize::new(0));

    stream::iter(files)
        .map(|path| {
            let pb         = Arc::clone(&pb);
            let output_dir = Arc::clone(&output_dir);
            let ok         = Arc::clone(&ok);
            let failed     = Arc::clone(&failed);
            let api_key    = api_key.clone();
            let model      = model.clone();
            let base_url   = base_url.clone();
            async move {
                let stem = match path.file_stem().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
                    Some(s) => s.to_string(),
                    None => {
                        pb.println(format!("[SKIP] {}: no file stem", path.display()));
                        failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        pb.inc(1);
                        return;
                    }
                };
                let out_path = output_dir.join(format!("{stem}.json"));
                pb.set_message(stem.clone());
                let pb2 = Arc::clone(&pb);
                let log: LogFn = Arc::new(move |msg| { pb2.println(msg); });
                let result = tasks::run_to_json(ToJsonParams {
                    input: path.to_string_lossy().into_owned(),
                    output: out_path,
                    provider, api_key, model, quality, lang, country, base_url, enrich, correct,
                    use_suggested_filename,
                }, log).await;
                match result {
                    Ok(_)  => { ok.fetch_add(1, std::sync::atomic::Ordering::Relaxed); }
                    Err(e) => {
                        pb.println(format!("[ERROR] {}: {e}", path.display()));
                        failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    }
                }
                pb.inc(1);
            }
        })
        .buffer_unordered(concurrency.max(1))
        .collect::<Vec<_>>()
        .await;

    let ok     = ok.load(std::sync::atomic::Ordering::Relaxed);
    let failed = failed.load(std::sync::atomic::Ordering::Relaxed);
    pb.finish_and_clear();
    eprintln!("Done: {ok} ok, {failed} failed");
}

fn batch_to_docx(
    input_dir: &Path,
    output_dir: &Path,
    lang: Language,
    template: Option<&Path>,
) -> anyhow::Result<()> {
    use sdsforge_core::{convert_from_json, convert_from_template, ConvertConfig};
    let files = collect_files(input_dir, &["json"]);
    let total = files.len();
    if total == 0 { eprintln!("No .json files found"); return Ok(()); }
    let pb = make_pb(total as u64);
    let (mut ok, mut failed) = (0usize, 0usize);
    for path in &files {
        let stem = match path.file_stem().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
            Some(s) => s.to_string(),
            None => { failed += 1; pb.inc(1); continue; }
        };
        pb.set_message(stem.clone());
        let out_path = output_dir.join(format!("{stem}.docx"));
        let result = check_json_file_size(path)
            .and_then(|_| std::fs::read_to_string(path).map_err(anyhow::Error::from))
            .and_then(|raw| {
                // Auto-detect lang per file when the default (Japanese) was not explicitly set.
                let effective_lang = if lang == Language::default() {
                    sdsforge_core::detect_language(&raw)
                } else {
                    lang
                };
                let file_config = ConvertConfig {
                    source_language: None,
                    output_language: effective_lang,
                    ..Default::default()
                };
                serde_json::from_str::<SdsRoot>(&raw)
                    .map_err(anyhow::Error::from)
                    .and_then(|sds| {
                        if let Some(tmpl) = template {
                            convert_from_template(&sds, tmpl, &out_path).map_err(anyhow::Error::from)
                        } else {
                            convert_from_json(&sds, &out_path, &file_config).map_err(anyhow::Error::from)
                        }
                    })
            });
        match result {
            Ok(_)  => ok += 1,
            Err(e) => { pb.println(format!("[ERROR] {}: {e}", path.display())); failed += 1; }
        }
        pb.inc(1);
    }
    pb.finish_and_clear();
    eprintln!("Done: {ok} ok, {failed} failed");
    Ok(())
}

fn batch_to_html(input_dir: &Path, output_dir: &Path, lang: Language) -> anyhow::Result<()> {
    use sdsforge_core::{converter::html::render_html, ConvertConfig};
    let files = collect_files(input_dir, &["json"]);
    let total = files.len();
    if total == 0 { eprintln!("No .json files found"); return Ok(()); }
    let pb = make_pb(total as u64);
    let config = ConvertConfig { source_language: None, output_language: lang, ..Default::default() };
    let (mut ok, mut failed) = (0usize, 0usize);
    for path in &files {
        let stem = match path.file_stem().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
            Some(s) => s.to_string(),
            None => { failed += 1; pb.inc(1); continue; }
        };
        pb.set_message(stem.clone());
        let out_path = output_dir.join(format!("{stem}.html"));
        let result = check_json_file_size(path)
            .and_then(|_| std::fs::read_to_string(path).map_err(anyhow::Error::from))
            .and_then(|raw| serde_json::from_str::<SdsRoot>(&raw).map_err(anyhow::Error::from))
            .and_then(|sds| render_html(&sds, config.output_language).map_err(anyhow::Error::from))
            .and_then(|html| std::fs::write(&out_path, html).map_err(anyhow::Error::from));
        match result {
            Ok(_)  => ok += 1,
            Err(e) => { pb.println(format!("[ERROR] {}: {e}", path.display())); failed += 1; }
        }
        pb.inc(1);
    }
    pb.finish_and_clear();
    eprintln!("Done: {ok} ok, {failed} failed");
    Ok(())
}

fn batch_to_pdf(input_dir: &Path, output_dir: &Path, lang: Language) -> anyhow::Result<()> {
    let files = collect_files(input_dir, &["json"]);
    let total = files.len();
    if total == 0 { eprintln!("No .json files found"); return Ok(()); }
    let pb = make_pb(total as u64);
    let (mut ok, mut failed) = (0usize, 0usize);
    for path in &files {
        let stem = match path.file_stem().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
            Some(s) => s.to_string(),
            None => { failed += 1; pb.inc(1); continue; }
        };
        pb.set_message(stem.clone());
        let out_path = output_dir.join(format!("{stem}.pdf"));
        let result = check_json_file_size(path)
            .and_then(|_| std::fs::read_to_string(path).map_err(anyhow::Error::from))
            .and_then(|raw| serde_json::from_str::<SdsRoot>(&raw).map_err(anyhow::Error::from))
            .and_then(|sds| {
                sdsforge_core::converter::render_pdf(&sds, lang)
                    .map_err(anyhow::Error::from)
                    .and_then(|bytes| std::fs::write(&out_path, bytes).map_err(anyhow::Error::from))
            });
        match result {
            Ok(_)  => ok += 1,
            Err(e) => { pb.println(format!("[ERROR] {}: {e}", path.display())); failed += 1; }
        }
        pb.inc(1);
    }
    pb.finish_and_clear();
    eprintln!("Done: {ok} ok, {failed} failed");
    Ok(())
}