harn-cli 0.7.38

CLI for the Harn programming language — run, test, REPL, format, and lint
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
use std::fs;
use std::path::{Path, PathBuf};
use std::process;

use crate::cli::{NewArgs, ProjectTemplate};

pub(crate) fn resolve_new_args(
    args: &NewArgs,
) -> Result<(Option<String>, ProjectTemplate), String> {
    let template = args.template.unwrap_or(ProjectTemplate::Basic);
    match (args.first.as_deref(), args.second.as_deref()) {
        (Some("package"), Some(name)) => Ok((Some(name.to_string()), ProjectTemplate::Package)),
        (Some("connector"), Some(name)) => Ok((Some(name.to_string()), ProjectTemplate::Connector)),
        (Some(kind @ ("package" | "connector")), None) => Err(format!(
            "`harn new {kind}` requires a package name, for example `harn new {kind} my-{kind}`"
        )),
        (Some(name), None) => Ok((Some(name.to_string()), template)),
        (None, None) => Ok((None, template)),
        (Some(_), Some(_)) => Err(
            "unexpected second positional argument; use `harn new package NAME` or `harn new NAME --template package`"
                .to_string(),
        ),
        (None, Some(_)) => unreachable!("clap cannot fill second positional without first"),
    }
}

pub(crate) fn init_project(name: Option<&str>, template: ProjectTemplate) {
    let dir = match name {
        Some(n) => {
            let dir = PathBuf::from(n);
            if dir.exists() {
                eprintln!("Directory '{}' already exists", n);
                process::exit(1);
            }
            fs::create_dir_all(&dir).unwrap_or_else(|e| {
                eprintln!("Failed to create directory: {e}");
                process::exit(1);
            });
            println!("Creating project '{}'...", n);
            dir
        }
        None => {
            println!("Initializing harn project in current directory...");
            PathBuf::from(".")
        }
    };

    let project_name = name
        .and_then(|value| Path::new(value).file_name().and_then(|name| name.to_str()))
        .unwrap_or("my-project");
    for (relative_path, content) in template_files(project_name, template) {
        write_if_new(&dir.join(relative_path), &content);
    }

    println!();
    if let Some(n) = name {
        println!("  cd {}", n);
    }
    match template {
        ProjectTemplate::Basic
        | ProjectTemplate::Agent
        | ProjectTemplate::Eval
        | ProjectTemplate::Package
        | ProjectTemplate::Connector => {
            println!("  harn run main.harn       # run the program");
            println!("  harn test tests/         # run the tests");
        }
        ProjectTemplate::McpServer => {
            println!("  harn mcp-serve main.harn # expose the starter MCP server");
        }
        ProjectTemplate::PipelineLab => {
            println!("  harn playground --task \"Explain this repo\"    # run the lab");
            println!("  harn playground --watch --task \"Refine the prompt\"  # live iteration");
        }
    }
    println!("  harn fmt main.harn       # format code");
    println!("  harn lint main.harn      # lint code");
    if matches!(
        template,
        ProjectTemplate::Package | ProjectTemplate::Connector
    ) {
        println!("  harn package check       # validate publish readiness");
        println!("  harn package docs        # generate API docs");
        println!("  harn package pack        # build an inspectable artifact");
    }
    println!("  harn doctor              # verify the local environment");
}

fn write_if_new(path: &Path, content: &str) {
    if let Some(parent) = path.parent() {
        if let Err(e) = fs::create_dir_all(parent) {
            eprintln!("Failed to create {}: {e}", parent.display());
            return;
        }
    }
    if path.exists() {
        println!("  skip  {} (already exists)", path.display());
    } else {
        fs::write(path, content).unwrap_or_else(|e| {
            eprintln!("Failed to write {}: {e}", path.display());
        });
        println!("  create  {}", path.display());
    }
}

fn template_files(project_name: &str, template: ProjectTemplate) -> Vec<(&'static str, String)> {
    let manifest = format!(
        r#"[package]
name = "{project_name}"
version = "0.1.0"

[dependencies]
"#
    );

    match template {
        ProjectTemplate::Basic => vec![
            ("harn.toml", manifest),
            (
                "main.harn",
                r#"import "lib/helpers"

pipeline default(task) {
  let greeting = greet("world")
  log(greeting)
}
"#
                .to_string(),
            ),
            (
                "lib/helpers.harn",
                r#"fn greet(name) {
  return "Hello, " + name + "!"
}

fn add(a, b) {
  return a + b
}
"#
                .to_string(),
            ),
            (
                "tests/test_main.harn",
                r#"import "../lib/helpers"

pipeline test_greet(task) {
  assert_eq(greet("world"), "Hello, world!")
  assert_eq(greet("Harn"), "Hello, Harn!")
}

pipeline test_add(task) {
  assert_eq(add(2, 3), 5)
  assert_eq(add(-1, 1), 0)
  assert_eq(add(0, 0), 0)
}
"#
                .to_string(),
            ),
        ],
        ProjectTemplate::Agent => vec![
            ("harn.toml", manifest),
            (
                "main.harn",
                r#"pipeline default(task) {
  var tools = tool_registry()
  tools = tool_define(tools, "read_repo_file", "Read a file from the current repository", {
    parameters: {
      type: "object",
      properties: {
        path: {type: "string"}
      },
      required: ["path"]
    },
    returns: {type: "string"},
    handler: fn(args) {
      return read_file(args.path)
    }
  })

  let result = agent_loop(task, "You are a helpful agent. Read the repository before proposing changes.", {
    persistent: true,
    max_nudges: 3,
    tools: tools
  })

  println(result.text)
}
"#
                .to_string(),
            ),
            (
                "tests/test_agent.harn",
                r###"pipeline test_agent_smoke(task) {
  llm_mock({text: "##DONE##\nRepository looks healthy."})
  let result = agent_loop("Review the repository", "You are a code review agent.", {
    max_nudges: 1
  })

  assert_eq(result.status, "completed")
}
"###
                .to_string(),
            ),
        ],
        ProjectTemplate::McpServer => vec![
            ("harn.toml", manifest),
            (
                "main.harn",
                r#"pipeline default(task) {
  var tools = tool_registry()
  tools = tool_define(tools, "ping", "Return a pong response", {
    parameters: {
      type: "object",
      properties: {
        message: {type: "string"}
      },
      required: ["message"]
    },
    returns: {
      type: "object",
      properties: {
        message: {type: "string"},
        echoed: {type: "string"}
      }
    },
    handler: fn(args) {
      return {
        message: "pong",
        echoed: args.message
      }
    }
  })

  mcp_tools(tools)
  mcp_resource({
    uri: "info://server",
    name: "server-info",
    text: "Harn MCP starter server"
  })
}
"#
                .to_string(),
            ),
        ],
        ProjectTemplate::Eval => vec![
            ("harn.toml", manifest),
            (
                "main.harn",
                r#"pipeline default(task) {
  let input = "hello world"
  let output = input.upper()
  let passed = output == "HELLO WORLD"

  eval_metric("passed", passed)
  eval_metric("output_length", len(output))

  println(json_stringify({
    input: input,
    output: output,
    passed: passed
  }))
}
"#
                .to_string(),
            ),
            (
                "tests/test_eval.harn",
                r#"pipeline test_eval_metrics(task) {
  eval_metric("accuracy", 1.0, {suite: "smoke"})
  let metrics = eval_metrics()

  assert_eq(len(metrics), 1)
  assert_eq(metrics[0].name, "accuracy")
}
"#
                .to_string(),
            ),
            (
                "eval-suite.json",
                r#"{
  "_type": "eval_suite_manifest",
  "id": "sample-suite",
  "name": "Sample Eval Suite",
  "base_dir": ".",
  "cases": [
    {
      "label": "replace-with-a-run-record",
      "run_path": ".harn-runs/sample-run.json"
    }
  ]
}
"#
                .to_string(),
            ),
        ],
        ProjectTemplate::PipelineLab => vec![
            ("harn.toml", manifest),
            (
                "host.harn",
                r#"pub fn build_context(task) {
  return {
    task: task,
    cwd: cwd(),
  }
}

pub fn request_permission(tool_name, request_args) -> bool {
  return true
}
"#
                .to_string(),
            ),
            (
                "pipeline.harn",
                r#"pipeline default(task) {
  let context = build_context(env_or("HARN_TASK", ""))
  let result = llm_call(
    "Task: " + context.task + "\nWorkspace: " + context.cwd,
    "You are a concise coding assistant. Reply in 3 bullets max.",
  )
  println(result.text)
}
"#
                .to_string(),
            ),
            (
                "README.md",
                r#"# Pipeline Lab

Use this project to iterate on a Harn workflow against a local Harn-native host module.

## Run

```bash
harn playground --task "Explain this repository"
```

## Watch mode

```bash
harn playground --watch --task "Tighten the workflow prompt"
```

Edit `host.harn` or `pipeline.harn` and the playground will re-run automatically.
"#
                .to_string(),
            ),
        ],
        ProjectTemplate::Package => vec![
            (
                "harn.toml",
                format!(
                    r#"[package]
name = "{project_name}"
version = "0.1.0"
description = "Reusable Harn package."
license = "MIT OR Apache-2.0"
repository = "https://github.com/OWNER/{project_name}"
harn = ">=0.7,<0.8"
docs_url = "docs/api.md"

[exports]
lib = "lib/main.harn"

[dependencies]
"#
                ),
            ),
            (
                "lib/main.harn",
                r#"/// Return a greeting for `name`.
pub fn greet(name: string) -> string {
  return "Hello, " + name + "!"
}
"#
                .to_string(),
            ),
            (
                "main.harn",
                r#"import "lib/main"

pipeline default(task) {
  println(greet("world"))
}
"#
                .to_string(),
            ),
            (
                "tests/test_main.harn",
                r#"import "../lib/main"

pipeline test_greet(task) {
  assert_eq(greet("Harn"), "Hello, Harn!")
}
"#
                .to_string(),
            ),
            (
                ".github/workflows/harn-package.yml",
                r#"name: Harn package

on:
  pull_request:
  push:
    branches: [main]

jobs:
  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: taiki-e/install-action@cargo-binstall
      - run: cargo binstall harn-cli --no-confirm
      - run: harn install --locked --offline || harn install
      - run: harn test tests/
      - run: harn package check
      - run: harn package docs --check
      - run: harn package pack --dry-run
"#
                .to_string(),
            ),
            (
                "README.md",
                format!(
                    r#"# {project_name}

Reusable Harn package.

## Quickstart

```bash
harn add ../{project_name}
harn test tests/
harn package check
harn package docs
harn package pack
```

Consumers import stable modules through the `[exports]` entries in `harn.toml`.
"#
                ),
            ),
            (
                "LICENSE",
                "MIT OR Apache-2.0\n".to_string(),
            ),
            (
                "docs/api.md",
                format!(
                    r#"# API Reference: {project_name}

Generated by `harn package docs`.

Version: `0.1.0`

## Export `lib`

`lib/main.harn`

### fn `greet`

Return a greeting for `name`.

```harn
pub fn greet(name: string) -> string
```
"#
                ),
            ),
        ],
        ProjectTemplate::Connector => vec![
            (
                "harn.toml",
                format!(
                    r#"[package]
name = "{project_name}"
version = "0.1.0"
description = "Pure-Harn connector package."
license = "MIT OR Apache-2.0"
repository = "https://github.com/OWNER/{project_name}"
harn = ">=0.7,<0.8"
docs_url = "docs/api.md"

[exports]
connector = "connectors/echo.harn"

[[providers]]
id = "echo"
connector = {{ harn = "connectors/echo" }}

[connector_contract]
version = 1

[[connector_contract.fixtures]]
provider = "echo"
name = "message"
kind = "webhook"
body_json = {{ message = "hello" }}
expect_type = "event"
expect_kind = "webhook"
expect_event_count = 1

[dependencies]
"#
                ),
            ),
            (
                "connectors/echo.harn",
                r#"/// Connector provider id.
pub fn provider_id() {
  return "echo"
}

/// Trigger kinds emitted by this connector.
pub fn kinds() {
  return ["webhook"]
}

/// JSON payload schema for normalized inbound events.
pub fn payload_schema() {
  return {
    harn_schema_name: "EchoEventPayload",
    json_schema: {
      type: "object",
      additionalProperties: true,
    },
  }
}

/// Convert one inbound request into Harn trigger events.
pub fn normalize_inbound(raw) {
  let body = raw.body_json ?? json_parse(raw.body_text)
  return {
    type: "event",
    event: {
      kind: "webhook",
      dedupe_key: "echo:" + (body.message ?? "message"),
      payload: body,
    },
  }
}
"#
                .to_string(),
            ),
            (
                "main.harn",
                r#"import "connectors/echo"

pipeline default(task) {
  println(provider_id())
}
"#
                .to_string(),
            ),
            (
                "tests/test_connector.harn",
                r#"import "../connectors/echo"

pipeline test_provider_id(task) {
  assert_eq(provider_id(), "echo")
  assert_eq(kinds(), ["webhook"])
}
"#
                .to_string(),
            ),
            (
                ".github/workflows/harn-package.yml",
                r#"name: Harn connector package

on:
  pull_request:
  push:
    branches: [main]

jobs:
  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: taiki-e/install-action@cargo-binstall
      - run: cargo binstall harn-cli --no-confirm
      - run: harn install --locked --offline || harn install
      - run: harn test tests/
      - run: harn connector check .
      - run: harn package check
      - run: harn package docs --check
      - run: harn package pack --dry-run
"#
                .to_string(),
            ),
            (
                "README.md",
                format!(
                    r#"# {project_name}

Pure-Harn connector package.

## Quickstart

```bash
harn connector check .
harn test tests/
harn package check
harn package docs
harn package pack
```

Consumers import the connector through the stable `[exports]` entry in `harn.toml`.
"#
                ),
            ),
            ("LICENSE", "MIT OR Apache-2.0\n".to_string()),
            (
                "docs/api.md",
                format!(
                    r#"# API Reference: {project_name}

Generated by `harn package docs`.

Version: `0.1.0`

## Export `connector`

`connectors/echo.harn`

### fn `provider_id`

Connector provider id.

```harn
pub fn provider_id()
```

### fn `kinds`

Trigger kinds emitted by this connector.

```harn
pub fn kinds()
```

### fn `payload_schema`

JSON payload schema for normalized inbound events.

```harn
pub fn payload_schema()
```

### fn `normalize_inbound`

Convert one inbound request into Harn trigger events.

```harn
pub fn normalize_inbound(raw)
```
"#
                ),
            ),
        ],
    }
}

#[cfg(test)]
mod tests {
    use super::{resolve_new_args, template_files};
    use crate::cli::{NewArgs, ProjectTemplate};

    #[test]
    fn basic_template_keeps_library_layout() {
        let files = template_files("sample", ProjectTemplate::Basic);
        let paths: Vec<&str> = files.iter().map(|(path, _)| *path).collect();
        assert!(paths.contains(&"lib/helpers.harn"));
        assert!(paths.contains(&"tests/test_main.harn"));
    }

    #[test]
    fn new_templates_include_expected_entrypoints() {
        let agent = template_files("sample", ProjectTemplate::Agent);
        assert!(agent.iter().any(|(path, _)| *path == "main.harn"));
        assert!(agent
            .iter()
            .any(|(path, _)| *path == "tests/test_agent.harn"));

        let mcp = template_files("sample", ProjectTemplate::McpServer);
        assert!(mcp.iter().any(|(path, _)| *path == "main.harn"));

        let eval = template_files("sample", ProjectTemplate::Eval);
        assert!(eval.iter().any(|(path, _)| *path == "eval-suite.json"));

        let pipeline_lab = template_files("sample", ProjectTemplate::PipelineLab);
        assert!(pipeline_lab.iter().any(|(path, _)| *path == "host.harn"));
        assert!(pipeline_lab
            .iter()
            .any(|(path, _)| *path == "pipeline.harn"));

        let package = template_files("sample", ProjectTemplate::Package);
        assert!(package.iter().any(|(path, _)| *path == "lib/main.harn"));
        assert!(package
            .iter()
            .any(|(path, _)| *path == ".github/workflows/harn-package.yml"));

        let connector = template_files("sample", ProjectTemplate::Connector);
        assert!(connector
            .iter()
            .any(|(path, _)| *path == "connectors/echo.harn"));
    }

    #[test]
    fn new_package_kind_resolves_to_package_template() {
        let args = NewArgs {
            first: Some("package".to_string()),
            second: Some("sample".to_string()),
            template: None,
        };
        let (name, template) = resolve_new_args(&args).unwrap();
        assert_eq!(name.as_deref(), Some("sample"));
        assert_eq!(template, ProjectTemplate::Package);
    }
}