nika-init 0.64.0

Nika project scaffolding — course generator, workflow templates, showcase
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
//! Showcase Fetch — 15 workflows demonstrating ALL 9 extract modes + 3 response modes
//!
//! Complete coverage of the `fetch:` verb's extraction and response capabilities:
//!
//! **Extract modes (9):**
//! - `markdown` — Full HTML to clean Markdown (htmd)
//! - `article` — Main article content only (Readability/dom_smoothie)
//! - `text` — Visible text, optionally filtered by `selector:`
//! - `selector` — Raw HTML matching a CSS selector
//! - `metadata` — OG tags, Twitter Cards, JSON-LD, SEO tags
//! - `links` — Classified link list (internal/external, nav/content/footer)
//! - `jsonpath` — JSONPath query on JSON API responses
//! - `feed` — RSS/Atom/JSON Feed parsing (feed-rs)
//! - `llm_txt` — AI-era content discovery (/.well-known/llm.txt, /llms.txt)
//!
//! **Response modes (3):**
//! - `full` — JSON envelope: { status, headers, body, url }
//! - `binary` — Store in CAS, return hash for media pipeline
//! - (default) — Raw body text
//!
//! Workflows 01-09: one extract mode each
//! Workflows 10-12: one response mode each
//! Workflows 13-15: combos (multi-extract, feed-to-newsletter, scrape+analyze)
//!
//! All use `{{PROVIDER}}`/`{{MODEL}}` for LLM tasks. Pure-fetch workflows set
//! `requires_llm: false` in comments.

use super::WorkflowTemplate;

/// Return all 15 showcase fetch workflows.
pub fn get_showcase_fetch_workflows() -> Vec<WorkflowTemplate> {
    vec![
        WorkflowTemplate {
            filename: "01-fetch-markdown.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_01_MARKDOWN,
        },
        WorkflowTemplate {
            filename: "02-fetch-article.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_02_ARTICLE,
        },
        WorkflowTemplate {
            filename: "03-fetch-text-selector.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_03_TEXT,
        },
        WorkflowTemplate {
            filename: "04-fetch-selector-html.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_04_SELECTOR,
        },
        WorkflowTemplate {
            filename: "05-fetch-metadata.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_05_METADATA,
        },
        WorkflowTemplate {
            filename: "06-fetch-links.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_06_LINKS,
        },
        WorkflowTemplate {
            filename: "07-fetch-jsonpath.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_07_JSONPATH,
        },
        WorkflowTemplate {
            filename: "08-fetch-feed.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_08_FEED,
        },
        WorkflowTemplate {
            filename: "09-fetch-llm-txt.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_09_LLM_TXT,
        },
        WorkflowTemplate {
            filename: "10-response-full.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_10_RESPONSE_FULL,
        },
        WorkflowTemplate {
            filename: "11-response-binary.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_11_RESPONSE_BINARY,
        },
        WorkflowTemplate {
            filename: "12-response-default.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_12_RESPONSE_DEFAULT,
        },
        WorkflowTemplate {
            filename: "13-multi-extract-comparison.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_13_MULTI_EXTRACT,
        },
        WorkflowTemplate {
            filename: "14-rss-to-newsletter.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_14_RSS_NEWSLETTER,
        },
        WorkflowTemplate {
            filename: "15-scrape-and-analyze.nika.yaml",
            tier_dir: "showcase-fetch",
            content: FETCH_15_SCRAPE_ANALYZE,
        },
    ]
}

// =============================================================================
// 01 — Markdown Extraction
// fetch: blog URL -> extract:markdown -> artifact
// =============================================================================

const FETCH_01_MARKDOWN: &str = r##"# =============================================================================
# SHOWCASE FETCH 01 — Markdown Extraction
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-markdown
#
# Fetches the Rust Blog and converts the entire HTML page to clean Markdown.
# The htmd library strips navigation, scripts, styles, and produces
# LLM-ready content. Artifact saves the result to disk.
#
# Run: nika run workflows/showcase-fetch/01-fetch-markdown.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-markdown-showcase
description: "Convert a blog homepage to clean Markdown via extract: markdown"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: fetch_blog
    description: "Fetch Rust Blog and convert to Markdown"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: markdown
      timeout: 20
    artifact:
      path: rust-blog-markdown.md

  - id: log_size
    depends_on: [fetch_blog]
    with:
      content: $fetch_blog
    exec:
      command: |
        echo "Markdown extraction complete. Content length: $(echo '{{with.content}}' | wc -c | tr -d ' ') bytes"
      shell: true
"##;

// =============================================================================
// 02 — Article Extraction
// fetch: news URL -> extract:article -> artifact
// =============================================================================

const FETCH_02_ARTICLE: &str = r##"# =============================================================================
# SHOWCASE FETCH 02 — Article Extraction (Readability)
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-article
#
# Extracts only the main article content from a webpage using the
# Readability algorithm (dom_smoothie). Strips navigation, ads, sidebars,
# cookie banners — leaving just the primary reading content.
#
# Run: nika run workflows/showcase-fetch/02-fetch-article.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-article-showcase
description: "Extract main article content with Readability via extract: article"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: fetch_article
    description: "Extract article content from Rust Blog"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: article
      timeout: 20
    artifact:
      path: rust-blog-article.md

  - id: log_result
    depends_on: [fetch_article]
    with:
      article: $fetch_article
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Article extraction complete — content ready for LLM consumption"
"##;

// =============================================================================
// 03 — Text Extraction with CSS Selector
// fetch: URL -> extract:text -> selector: "article p"
// =============================================================================

const FETCH_03_TEXT: &str = r##"# =============================================================================
# SHOWCASE FETCH 03 — Text Extraction with CSS Selector
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-html
#
# Extracts visible text from a webpage. When combined with selector:,
# only text from matching CSS elements is returned. Without selector:,
# returns all visible text (no HTML tags).
#
# Run: nika run workflows/showcase-fetch/03-fetch-text-selector.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-text-selector-showcase
description: "Extract visible text filtered by CSS selector via extract: text"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Text from specific elements only
  - id: fetch_paragraphs
    description: "Extract paragraph text from httpbin HTML page"
    fetch:
      url: "https://httpbin.org/html"
      extract: text
      selector: "p"
      timeout: 15
    artifact:
      path: httpbin-paragraphs.txt

  # All visible text (no selector)
  - id: fetch_all_text
    description: "Extract all visible text from httpbin HTML page"
    fetch:
      url: "https://httpbin.org/html"
      extract: text
      timeout: 15
    artifact:
      path: httpbin-all-text.txt

  - id: compare_sizes
    depends_on: [fetch_paragraphs, fetch_all_text]
    with:
      filtered: $fetch_paragraphs
      full: $fetch_all_text
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Filtered paragraphs vs full text extracted successfully"
"##;

// =============================================================================
// 04 — Raw HTML Selector
// fetch: URL -> extract:selector -> selector: "h1, h2, h3"
// =============================================================================

const FETCH_04_SELECTOR: &str = r##"# =============================================================================
# SHOWCASE FETCH 04 — Raw HTML Selector Extraction
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-html
#
# Returns the raw HTML of elements matching a CSS selector. Unlike
# extract: text (which strips tags), this preserves the HTML structure.
# Useful for scraping specific DOM fragments.
#
# Run: nika run workflows/showcase-fetch/04-fetch-selector-html.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-selector-html-showcase
description: "Extract raw HTML matching CSS selectors via extract: selector"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: fetch_headings
    description: "Extract all heading elements from httpbin HTML"
    fetch:
      url: "https://httpbin.org/html"
      extract: selector
      selector: "h1"
      timeout: 15
    artifact:
      path: httpbin-headings.html

  - id: fetch_paragraphs_html
    description: "Extract paragraph HTML from httpbin"
    fetch:
      url: "https://httpbin.org/html"
      extract: selector
      selector: "p"
      timeout: 15
    artifact:
      path: httpbin-paragraphs.html

  - id: log_done
    depends_on: [fetch_headings, fetch_paragraphs_html]
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Raw HTML selector extraction complete — headings and paragraphs captured"
"##;

// =============================================================================
// 05 — Metadata Extraction
// fetch: URL -> extract:metadata -> structured OG/Twitter/JSON-LD
// =============================================================================

const FETCH_05_METADATA: &str = r##"# =============================================================================
# SHOWCASE FETCH 05 — Metadata Extraction (OG / Twitter / JSON-LD / SEO)
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-html
#
# Extracts structured metadata from a webpage: Open Graph tags,
# Twitter Cards, JSON-LD structured data, and basic SEO tags
# (title, description, canonical URL, etc.). Returns JSON.
#
# Run: nika run workflows/showcase-fetch/05-fetch-metadata.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-metadata-showcase
description: "Extract OG, Twitter Cards, JSON-LD, and SEO metadata via extract: metadata"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: github_metadata
    description: "Extract metadata from GitHub homepage"
    fetch:
      url: "https://github.com"
      extract: metadata
      timeout: 15
    artifact:
      path: github-metadata.json
      format: json

  - id: rust_blog_metadata
    description: "Extract metadata from Rust Blog"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: metadata
      timeout: 15
    artifact:
      path: rust-blog-metadata.json
      format: json

  - id: log_metadata
    depends_on: [github_metadata, rust_blog_metadata]
    with:
      gh: $github_metadata
      rust: $rust_blog_metadata
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Metadata extraction complete for GitHub and Rust Blog"
"##;

// =============================================================================
// 06 — Link Extraction
// fetch: URL -> extract:links -> internal/external classification
// =============================================================================

const FETCH_06_LINKS: &str = r##"# =============================================================================
# SHOWCASE FETCH 06 — Link Extraction and Classification
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-html
#
# Extracts all links from a webpage and classifies them:
# - Internal vs external
# - Navigation vs content vs footer
# Returns structured JSON with URL, text, type, and zone.
#
# Run: nika run workflows/showcase-fetch/06-fetch-links.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-links-showcase
description: "Extract and classify links via extract: links"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: extract_links
    description: "Extract and classify all links from Hacker News"
    fetch:
      url: "https://news.ycombinator.com"
      extract: links
      timeout: 15
    artifact:
      path: hn-links.json
      format: json

  - id: log_links
    depends_on: [extract_links]
    with:
      links: $extract_links
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Link extraction complete — internal/external classification ready"
"##;

// =============================================================================
// 07 — JSONPath Extraction
// fetch: API -> extract:jsonpath -> selector: "$.data[*].name"
// =============================================================================

const FETCH_07_JSONPATH: &str = r##"# =============================================================================
# SHOWCASE FETCH 07 — JSONPath Extraction
# =============================================================================
# requires_llm: false
# category: fetch-extract
#
# Queries JSON APIs using JSONPath expressions. Zero external dependencies —
# JSONPath is always available. The selector: field holds the JSONPath query.
# Surgical extraction from massive JSON payloads.
#
# Run: nika run workflows/showcase-fetch/07-fetch-jsonpath.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-jsonpath-showcase
description: "Extract specific fields from JSON APIs via extract: jsonpath"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # JSONPath on httpbin structured JSON
  - id: slideshow_title
    description: "Extract slideshow title from httpbin JSON"
    fetch:
      url: "https://httpbin.org/json"
      extract: jsonpath
      selector: "$.slideshow.title"
      timeout: 10
    artifact:
      path: slideshow-title.json
      format: json

  # JSONPath on nested array
  - id: slide_titles
    description: "Extract all slide titles from httpbin JSON"
    fetch:
      url: "https://httpbin.org/json"
      extract: jsonpath
      selector: "$.slideshow.slides[*].title"
      timeout: 10
    artifact:
      path: slide-titles.json
      format: json

  # JSONPath on Hacker News Algolia API
  - id: hn_search
    description: "Search Hacker News and extract story titles"
    fetch:
      url: "https://hn.algolia.com/api/v1/search?query=rust&tags=story&hitsPerPage=5"
      extract: jsonpath
      selector: "$.hits[*].title"
      timeout: 15
    artifact:
      path: hn-rust-titles.json
      format: json

  - id: log_results
    depends_on: [slideshow_title, slide_titles, hn_search]
    with:
      title: $slideshow_title
      slides: $slide_titles
      hn: $hn_search
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "JSONPath extraction complete — 3 queries across 2 APIs"
"##;

// =============================================================================
// 08 — RSS/Atom Feed Parsing
// fetch: RSS URL -> extract:feed -> structured entries
// =============================================================================

const FETCH_08_FEED: &str = r##"# =============================================================================
# SHOWCASE FETCH 08 — RSS/Atom Feed Parsing
# =============================================================================
# requires_llm: false
# category: fetch-extract
# features: fetch-feed
#
# Parses RSS, Atom, and JSON Feed formats using the feed-rs library.
# Returns structured JSON with title, entries, dates, authors, and links.
# Works with any standard syndication feed.
#
# Run: nika run workflows/showcase-fetch/08-fetch-feed.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-feed-showcase
description: "Parse RSS/Atom feeds into structured JSON via extract: feed"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: rust_feed
    description: "Parse the Rust Blog Atom feed"
    fetch:
      url: "https://blog.rust-lang.org/feed.xml"
      extract: feed
      timeout: 15
    artifact:
      path: rust-feed.json
      format: json

  - id: log_feed
    depends_on: [rust_feed]
    with:
      feed: $rust_feed
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "RSS feed parsed — entries extracted and structured as JSON"
"##;

// =============================================================================
// 09 — LLM.txt Discovery
// fetch: URL -> extract:llm_txt -> display content
// =============================================================================

const FETCH_09_LLM_TXT: &str = r##"# =============================================================================
# SHOWCASE FETCH 09 — LLM.txt Content Discovery
# =============================================================================
# requires_llm: false
# category: fetch-extract
#
# AI-era content discovery. Checks for /.well-known/llm.txt and /llms.txt
# files that websites publish to help LLMs understand their content.
# Part of the llms.txt standard for AI-friendly web content.
#
# Run: nika run workflows/showcase-fetch/09-fetch-llm-txt.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-llm-txt-showcase
description: "Discover AI content via extract: llm_txt"

artifacts:
  dir: .output/showcase-fetch

tasks:
  - id: check_anthropic
    description: "Check Anthropic docs for llm.txt"
    fetch:
      url: "https://docs.anthropic.com"
      extract: llm_txt
      timeout: 15
    artifact:
      path: anthropic-llm-txt.md

  - id: log_discovery
    depends_on: [check_anthropic]
    with:
      result: $check_anthropic
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "LLM.txt discovery complete"
"##;

// =============================================================================
// 10 — Full Response Envelope
// fetch: URL -> response:full -> status + headers + body + url
// =============================================================================

const FETCH_10_RESPONSE_FULL: &str = r##"# =============================================================================
# SHOWCASE FETCH 10 — Full Response Envelope
# =============================================================================
# requires_llm: false
# category: fetch-response
#
# Returns the complete HTTP response as a JSON envelope containing:
# - status: HTTP status code
# - headers: all response headers
# - body: response body text
# - url: after redirect resolution
#
# Perfect for debugging redirects, checking security headers, API monitoring.
#
# Run: nika run workflows/showcase-fetch/10-response-full.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-response-full-showcase
description: "Inspect complete HTTP response via response: full"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Full response from a simple GET
  - id: get_full
    description: "Fetch httpbin GET with full response envelope"
    fetch:
      url: "https://httpbin.org/get"
      response: full
      timeout: 10
    artifact:
      path: httpbin-full-response.json
      format: json

  # Full response showing headers
  - id: inspect_headers
    description: "Fetch httpbin headers with full envelope"
    fetch:
      url: "https://httpbin.org/headers"
      response: full
      timeout: 10
    artifact:
      path: httpbin-headers-full.json
      format: json

  - id: log_responses
    depends_on: [get_full, inspect_headers]
    with:
      get: $get_full
      headers: $inspect_headers
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Full response envelopes captured — status, headers, body, and url available"
"##;

// =============================================================================
// 11 — Binary Response (CAS storage)
// fetch: image URL -> response:binary -> nika:import -> nika:dimensions
// =============================================================================

const FETCH_11_RESPONSE_BINARY: &str = r##"# =============================================================================
# SHOWCASE FETCH 11 — Binary Response + Media Pipeline
# =============================================================================
# requires_llm: false
# category: fetch-response
#
# Downloads a binary file (image) into content-addressable storage (CAS).
# The task output is the CAS hash, which can be piped into media tools
# like nika:dimensions and nika:thumbhash for further processing.
#
# Run: nika run workflows/showcase-fetch/11-response-binary.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-response-binary-showcase
description: "Download binary into CAS and extract dimensions via response: binary"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Download a PNG image into CAS
  - id: download_image
    description: "Download a PNG image into content-addressable storage"
    fetch:
      url: "https://httpbin.org/image/png"
      response: binary
      timeout: 15
    artifact:
      path: downloaded-image.png
      format: binary

  # Extract dimensions from the downloaded image
  - id: get_dimensions
    depends_on: [download_image]
    with:
      img: $download_image
    invoke:
      tool: "nika:dimensions"
      params:
        hash: "{{with.img.hash}}"

  # Generate a thumbhash placeholder
  - id: get_thumbhash
    depends_on: [download_image]
    with:
      img: $download_image
    invoke:
      tool: "nika:thumbhash"
      params:
        hash: "{{with.img.hash}}"

  - id: log_media
    depends_on: [get_dimensions, get_thumbhash]
    with:
      dims: $get_dimensions
      hash: $get_thumbhash
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Binary download + media pipeline complete — dimensions and thumbhash extracted"
"##;

// =============================================================================
// 12 — Default Text Response
// fetch: URL -> (no response: field) -> display raw body
// =============================================================================

const FETCH_12_RESPONSE_DEFAULT: &str = r##"# =============================================================================
# SHOWCASE FETCH 12 — Default Text Response
# =============================================================================
# requires_llm: false
# category: fetch-response
#
# When no response: field is specified, fetch returns the raw body text.
# This is the simplest mode — no JSON envelope, no CAS storage.
# Just the HTTP response body as a string.
#
# Run: nika run workflows/showcase-fetch/12-response-default.nika.yaml

schema: "nika/workflow@0.12"
workflow: fetch-response-default-showcase
description: "Fetch raw body text with default response mode (no response: field)"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Default response — just the body text
  - id: fetch_ip
    description: "Fetch public IP as raw JSON text"
    fetch:
      url: "https://httpbin.org/ip"
      timeout: 10
    artifact:
      path: public-ip.txt

  # Another default fetch — UUID
  - id: fetch_uuid
    description: "Fetch a random UUID as raw text"
    fetch:
      url: "https://httpbin.org/uuid"
      timeout: 10
    artifact:
      path: random-uuid.txt

  # Default fetch from a JSON API
  - id: fetch_json_raw
    description: "Fetch httpbin JSON as raw text (no extraction)"
    fetch:
      url: "https://httpbin.org/json"
      timeout: 10
    artifact:
      path: raw-json-body.txt

  - id: log_defaults
    depends_on: [fetch_ip, fetch_uuid, fetch_json_raw]
    with:
      ip: $fetch_ip
      uuid: $fetch_uuid
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Default response mode — raw body text captured for all 3 endpoints"
"##;

// =============================================================================
// 13 — Multi-Extract Comparison
// fetch: same URL -> markdown vs article vs text -> LLM compare
// =============================================================================

const FETCH_13_MULTI_EXTRACT: &str = r##"# =============================================================================
# SHOWCASE FETCH 13 — Multi-Extract Comparison
# =============================================================================
# requires_llm: true
# category: fetch-combo
# features: fetch-markdown, fetch-article, fetch-html
#
# Fetches the SAME URL with 3 different extract modes (markdown, article,
# text) and asks an LLM to compare the results. Shows how each mode
# produces different output from identical source HTML.
#
# Run: nika run workflows/showcase-fetch/13-multi-extract-comparison.nika.yaml

schema: "nika/workflow@0.12"
workflow: multi-extract-comparison
description: "Compare markdown vs article vs text extraction on the same URL"
provider: "{{PROVIDER}}"
model: "{{MODEL}}"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Same URL, three extraction modes
  - id: as_markdown
    description: "Full Markdown extraction"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: markdown
      timeout: 20
    artifact:
      path: comparison-markdown.md

  - id: as_article
    description: "Article-only extraction (Readability)"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: article
      timeout: 20
    artifact:
      path: comparison-article.md

  - id: as_text
    description: "Plain text extraction"
    fetch:
      url: "https://blog.rust-lang.org/"
      extract: text
      timeout: 20
    artifact:
      path: comparison-text.txt

  # LLM compares all three outputs
  - id: compare
    description: "LLM analysis of extraction mode differences"
    depends_on: [as_markdown, as_article, as_text]
    with:
      md: $as_markdown
      article: $as_article
      text: $as_text
    infer:
      prompt: |
        Compare these 3 extraction modes applied to the same URL (blog.rust-lang.org):

        ## 1. extract: markdown (first 1500 chars)
        {{with.md | first(1500)}}

        ## 2. extract: article (first 1500 chars)
        {{with.article | first(1500)}}

        ## 3. extract: text (first 1500 chars)
        {{with.text | first(1500)}}

        Analyze:
        1. What does each mode preserve vs strip?
        2. Which is best for LLM summarization?
        3. Which is best for data extraction?
        4. Which is best for human reading?
        5. When would you pick each one?

        Be specific about the structural differences you observe.
      max_tokens: 600
    artifact:
      path: extraction-comparison-report.md
      template: |
        # Multi-Extract Comparison Report

        {{output}}
"##;

// =============================================================================
// 14 — RSS to Newsletter
// fetch: 3 feeds -> extract:feed -> for_each entries -> infer summarize
// =============================================================================

const FETCH_14_RSS_NEWSLETTER: &str = r##"# =============================================================================
# SHOWCASE FETCH 14 — RSS Feed to Newsletter Pipeline
# =============================================================================
# requires_llm: true
# category: fetch-combo
# features: fetch-feed
#
# Fetches an RSS feed, then uses an LLM to summarize the entries into
# a newsletter-style digest. Demonstrates extract: feed piped into
# infer: for AI-powered content curation.
#
# Run: nika run workflows/showcase-fetch/14-rss-to-newsletter.nika.yaml

schema: "nika/workflow@0.12"
workflow: rss-to-newsletter
description: "Fetch RSS feed and generate an AI-curated newsletter digest"
provider: "{{PROVIDER}}"
model: "{{MODEL}}"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Phase 1: Fetch the Rust Blog feed
  - id: rust_feed
    description: "Parse Rust Blog Atom feed"
    fetch:
      url: "https://blog.rust-lang.org/feed.xml"
      extract: feed
      timeout: 15
    artifact:
      path: newsletter-rust-feed.json
      format: json

  # Phase 2: Summarize into a newsletter digest
  - id: create_digest
    description: "Generate newsletter digest from feed entries"
    depends_on: [rust_feed]
    with:
      feed: $rust_feed
    infer:
      prompt: |
        You are a tech newsletter curator. Create a concise weekly digest
        from this RSS feed data.

        FEED DATA:
        {{with.feed}}

        Format as a newsletter with:
        1. A catchy header with the feed name
        2. Top 5 most recent entries, each with:
           - Title (as a heading)
           - 2-sentence summary of what the post covers
           - Why it matters for Rust developers
        3. A "Quick Links" section with remaining entry titles
        4. A brief editorial closing paragraph

        Write in an engaging but professional tone.
      max_tokens: 800
    artifact:
      path: rust-newsletter-digest.md
      template: |
        {{output}}

  - id: log_done
    depends_on: [create_digest]
    invoke:
      tool: "nika:log"
      params:
        level: "info"
        message: "Newsletter digest generated from RSS feed"
"##;

// =============================================================================
// 15 — Scrape + Analyze
// fetch: metadata -> infer: SEO analysis -> structured report -> artifact
// =============================================================================

const FETCH_15_SCRAPE_ANALYZE: &str = r##"# =============================================================================
# SHOWCASE FETCH 15 — Scrape + SEO Analysis Pipeline
# =============================================================================
# requires_llm: true
# category: fetch-combo
# features: fetch-html
#
# Fetches metadata and links from a website, then uses an LLM to produce
# a structured SEO analysis report. Combines extract: metadata and
# extract: links with structured output and artifact generation.
#
# Run: nika run workflows/showcase-fetch/15-scrape-and-analyze.nika.yaml

schema: "nika/workflow@0.12"
workflow: scrape-and-analyze
description: "Scrape metadata + links, then generate a structured SEO report"
provider: "{{PROVIDER}}"
model: "{{MODEL}}"

artifacts:
  dir: .output/showcase-fetch

tasks:
  # Phase 1: Extract metadata
  - id: scrape_metadata
    description: "Extract OG, Twitter Cards, JSON-LD, and SEO tags"
    fetch:
      url: "https://github.com"
      extract: metadata
      timeout: 15
    artifact:
      path: seo-metadata.json
      format: json

  # Phase 2: Extract and classify links
  - id: scrape_links
    description: "Extract and classify all links"
    fetch:
      url: "https://github.com"
      extract: links
      timeout: 15
    artifact:
      path: seo-links.json
      format: json

  # Phase 3: Fetch full response for header analysis
  - id: check_headers
    description: "Inspect HTTP response headers for security and caching"
    fetch:
      url: "https://github.com"
      response: full
      timeout: 15

  # Phase 4: LLM analyzes everything
  - id: seo_analysis
    description: "AI-powered SEO analysis from scraped data"
    depends_on: [scrape_metadata, scrape_links, check_headers]
    with:
      metadata: $scrape_metadata
      links: $scrape_links
      resp_status: $check_headers.status
      resp_headers: $check_headers.headers
    infer:
      prompt: |
        You are an SEO expert. Analyze this website's SEO posture from the scraped data.

        ## Metadata (OG, Twitter Cards, JSON-LD, SEO tags)
        {{with.metadata}}

        ## Link Classification (internal/external, nav/content/footer)
        {{with.links}}

        ## HTTP Headers (security, caching, performance)
        Status: {{with.resp_status}}
        {{with.resp_headers | to_json}}

        Produce a structured SEO report with:
        1. Overall SEO Score (0-100)
        2. Metadata Quality: title, description, OG completeness, Twitter Cards
        3. Link Health: internal/external ratio, broken-link risk areas
        4. Security Headers: CSP, HSTS, X-Frame-Options presence
        5. Top 5 Issues (ranked by impact)
        6. Top 5 Quick Wins (easy to fix, high impact)

        Return as JSON with fields: score, metadata_quality, link_health,
        security_headers, top_issues (array), quick_wins (array).
      max_tokens: 800
      temperature: 0.2
    structured:
      schema:
        type: object
        properties:
          score:
            type: integer
            description: "Overall SEO score 0-100"
          metadata_quality:
            type: object
            properties:
              title_present:
                type: boolean
              description_present:
                type: boolean
              og_completeness:
                type: string
              twitter_cards:
                type: string
          link_health:
            type: object
            properties:
              internal_count:
                type: integer
              external_count:
                type: integer
              assessment:
                type: string
          security_headers:
            type: object
            properties:
              csp:
                type: boolean
              hsts:
                type: boolean
              x_frame_options:
                type: boolean
          top_issues:
            type: array
            items:
              type: string
          quick_wins:
            type: array
            items:
              type: string
        required: [score, top_issues, quick_wins]
    artifact:
      path: seo-analysis-report.json
      format: json

  # Phase 5: Generate human-readable report
  - id: final_report
    description: "Generate formatted SEO report from structured analysis"
    depends_on: [seo_analysis]
    with:
      analysis: $seo_analysis
    infer:
      prompt: |
        Convert this structured SEO analysis into a professional Markdown report.

        ANALYSIS DATA:
        {{with.analysis}}

        Include:
        - Executive summary with the overall score
        - Detailed breakdown of each category
        - Prioritized action items
        - A summary table of findings

        Format as clean, well-structured Markdown.
      max_tokens: 600
    artifact:
      path: seo-report-final.md
      template: |
        # SEO Analysis Report — github.com

        {{output}}
"##;

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_showcase_fetch_workflow_count() {
        let workflows = get_showcase_fetch_workflows();
        assert_eq!(
            workflows.len(),
            15,
            "Should have exactly 15 showcase fetch workflows"
        );
    }

    #[test]
    fn test_showcase_fetch_filenames_unique() {
        let workflows = get_showcase_fetch_workflows();
        let mut names: Vec<&str> = workflows.iter().map(|w| w.filename).collect();
        let len = names.len();
        names.sort();
        names.dedup();
        assert_eq!(names.len(), len, "All filenames must be unique");
    }

    #[test]
    fn test_showcase_fetch_all_have_schema() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.content.contains("schema: \"nika/workflow@0.12\""),
                "Workflow {} must declare schema",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_all_have_workflow_name() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.content.contains("workflow:"),
                "Workflow {} must have workflow: declaration",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_all_have_tasks() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.content.contains("tasks:"),
                "Workflow {} must have tasks section",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_all_nika_yaml_extension() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.filename.ends_with(".nika.yaml"),
                "Workflow {} must end with .nika.yaml",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_all_in_showcase_fetch_dir() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert_eq!(
                w.tier_dir, "showcase-fetch",
                "Workflow {} must be in showcase-fetch directory",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_valid_yaml() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            // Skip YAML validation for templates with placeholders
            if w.content.contains("{{PROVIDER}}") || w.content.contains("{{MODEL}}") {
                continue;
            }
            let parsed: Result<serde_json::Value, _> = serde_saphyr::from_str(w.content);
            assert!(
                parsed.is_ok(),
                "Workflow {} should be valid YAML: {:?}",
                w.filename,
                parsed.err()
            );
        }
    }

    #[test]
    fn test_showcase_fetch_all_use_fetch_verb() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.content.contains("fetch:"),
                "Workflow {} must use the fetch: verb (it's a fetch showcase)",
                w.filename
            );
        }
    }

    #[test]
    fn test_showcase_fetch_extract_modes_coverage() {
        let all_content: String = get_showcase_fetch_workflows()
            .iter()
            .map(|w| w.content)
            .collect::<Vec<_>>()
            .join("\n");

        let modes = [
            "extract: markdown",
            "extract: article",
            "extract: text",
            "extract: selector",
            "extract: metadata",
            "extract: links",
            "extract: jsonpath",
            "extract: feed",
            "extract: llm_txt",
        ];

        for mode in &modes {
            assert!(all_content.contains(mode), "Missing extract mode: {}", mode);
        }
    }

    #[test]
    fn test_showcase_fetch_response_modes_coverage() {
        let all_content: String = get_showcase_fetch_workflows()
            .iter()
            .map(|w| w.content)
            .collect::<Vec<_>>()
            .join("\n");

        assert!(
            all_content.contains("response: full"),
            "Missing response mode: full"
        );
        assert!(
            all_content.contains("response: binary"),
            "Missing response mode: binary"
        );
        // Default mode = no response: field, verified by workflow 12 existing
        // and NOT having response: in its main fetch tasks
    }

    #[test]
    fn test_showcase_fetch_all_have_artifacts_dir() {
        let workflows = get_showcase_fetch_workflows();
        for w in &workflows {
            assert!(
                w.content.contains("artifacts:") || w.content.contains("artifact:"),
                "Workflow {} should produce artifacts",
                w.filename
            );
        }
    }
}