harn-stdlib 0.10.18

Embedded Harn standard library source catalog
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
pub import "std/agent/action_graph"
pub import "std/agent/result_text"
import "std/collections"
import "std/context"
import { llm_call_options } from "std/llm/options"

/**
 * workflow_model_spec.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_model_spec(config) {
  const target = config?.model ?? config?.model_alias ?? config?.model_tier ?? config?.tier
  if target == nil || target == "" {
    return nil
  }
  return llm_pick_model(target, filter_nil({provider: config?.provider}))
}

/**
 * workflow_options.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_options(config) {
  const spec = workflow_model_spec(config)
  const option_keys = [
    "max_tokens",
    "temperature",
    "top_p",
    "top_k",
    "stop",
    "seed",
    "frequency_penalty",
    "presence_penalty",
    "response_format",
    "schema",
    "thinking",
    "reasoning_effort",
    "reasoning_policy",
    "thinking_policy",
    "reasoning_scale",
    "problem_scale",
    "reasoning_task",
    "tools",
    "tool_choice",
    "cache",
    "prompt_cache_ttl",
    "timeout",
    "structural_experiment",
    "loop_until_done",
    "max_iterations",
    "max_nudges",
    "nudge",
    "turn_policy",
    "stop_after_successful_tools",
    "require_successful_tools",
    "tool_retries",
    "tool_backoff_ms",
    "tool_format",
    "native_tool_fallback",
    "transcript",
  ]
  return filter_nil(
    pick_keys(config ?? {}, option_keys)
      + {
      provider: config?.provider ?? spec?.provider,
      model: config?.model ?? spec?.id,
      context_callback: config?.context_callback ?? config?.context_filter,
    },
  )
}

/**
 * stage_config.
 *
 * @effects: []
 * @errors: []
 */
pub fn stage_config(flow, overrides, stage_name) {
  const combined = (flow ?? {}) + (overrides ?? {})
  const stage = combined[stage_name] ?? {}
  const merged = combined + stage
  return filter_nil(merged + {_stage: stage_name})
}

/**
 * stage_prompt.
 *
 * @effects: []
 * @errors: []
 */
pub fn stage_prompt(task, config) {
  const ctx = config?.context
  return prompt_compose(
    task,
    ctx,
    filter_nil(
      {
        system: config?.system,
        task_label: config?.task_label,
        separator: config?.context_separator,
        max_chars: config?.context_max_chars,
        section_max_chars: config?.context_section_max_chars,
      },
    ),
  )
}

/**
 * run_stage.
 *
 * @effects: [llm.call]
 * @errors: []
 */
pub fn run_stage(task, config) {
  const prompt = stage_prompt(task, config)
  const opts = workflow_options(config)
  if config?.mode == "completion" {
    return llm_completion(prompt?.prompt, config?.suffix, prompt?.system, llm_call_options(opts))
  }
  if config?.mode == "llm" || !config?.loop_until_done {
    return llm_call(prompt?.prompt, prompt?.system, llm_call_options(opts))
  }
  return agent_loop(
    prompt?.prompt,
    prompt?.system,
    opts + {loop_until_done: config?.loop_until_done ?? true},
  )
}

/**
 * verify_command.
 *
 * @effects: [host]
 * @errors: []
 */
pub fn verify_command(config) {
  if config?.command == nil || config?.command == "" {
    return nil
  }
  const shell = host_call("process.get_default_shell", {})
  const output = host_call("process.exec", {mode: "shell", command: config?.command, shell_id: shell?.id})
  let ok = output?.success
  if config?.expect_status != nil {
    const actual_status = output?.exit_code ?? output?.legacy_status ?? output?.status
    ok = actual_status == config.expect_status
  }
  if config?.expect_text != nil {
    ok = ok && contains(output?.combined ?? "", config?.expect_text)
  }
  return {kind: "command", ok: ok, output: output}
}

/**
 * verify_result.
 *
 * @effects: []
 * @errors: []
 */
pub fn verify_result(task, result, config) {
  if config == nil {
    return nil
  }
  if config?.command != nil {
    return verify_command(config)
  }
  if config?.assert_text != nil {
    return {kind: "text", ok: contains(result?.text ?? "", config?.assert_text), output: result?.text ?? ""}
  }
  return nil
}

/**
 * repair_task.
 *
 * @effects: []
 * @errors: []
 */
pub fn repair_task(original_task, verification, config) {
  if config?.prompt != nil {
    return config?.prompt
  }
  const summary = json_stringify(verification)
  return "The previous attempt did not pass verification.\n\nVerification:\n" + summary
    + "\n\nContinue working on the original task and fix the problem.\n\nOriginal task:\n"
    + original_task
}

/**
 * workflow.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow(config) {
  return workflow_graph(config ?? {})
}

/**
 * Executes a task through the supplied workflow graph and summarizes its stages.
 *
 * @effects: []
 * @errors: []
 */
pub fn task_run(task, flow, overrides) {
  const graph = workflow_graph(flow ?? {})
  const runtime = workflow_execute(task, graph, overrides?.artifacts ?? [], overrides)
  const stages = runtime?.run?.stages ?? []
  let act = nil
  let verify = nil
  let repair = nil
  const artifacts = runtime?.artifacts ?? []
  const latest_text = if len(artifacts) > 0 {
    artifacts[len(artifacts) - 1]?.text
  } else {
    act?.visible_text
  }
  for stage in stages {
    if act == nil {
      act = stage
      continue
    }
    if verify == nil && (stage?.node_id == "verify" || stage?.kind == "verify") {
      verify = stage
      continue
    }
    if repair == nil && stage?.node_id == "repair" {
      repair = stage
    }
  }
  const status = if verify?.status == "failed" || repair != nil {
    "needs_attention"
  } else {
    runtime?.status ?? "completed"
  }
  return {
    task: task,
    workflow: graph?.name ?? "workflow",
    status: status,
    text: latest_text,
    result: runtime,
    act: act,
    repair: repair,
    verification: verify,
    final_verification: verify,
    transcript: runtime?.transcript,
    run: runtime?.run,
    artifacts: artifacts,
  }
}

/**
 * workflow_continue.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_continue(prev, task, flow, overrides) {
  const transcript = prev?.transcript ?? prev
  const merged = (overrides ?? {}) + {transcript: transcript}
  return task_run(task, flow, merged)
}

fn __workflow_existing_transcript(result) {
  return result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript
}

fn __workflow_build_transcript(task, workflow_name, text: string, metadata) {
  const base_metadata = metadata ?? {}
  const transcript_metadata = base_metadata + {workflow: workflow_name}
  let built = transcript(transcript_metadata)
  if task {
    built = add_user(built, [{type: "text", text: task, visibility: "public"}])
  }
  if text {
    built = add_assistant(built, [{type: "output_text", text: text, visibility: "public"}])
  }
  return built
}

fn __workflow_result_artifacts(existing, text: string, workflow_name) {
  let run_artifacts = existing ?? []
  let has_visible_result_artifact = false
  if text {
    for item in run_artifacts {
      if item?.text == text {
        has_visible_result_artifact = true
      }
    }
    if !has_visible_result_artifact {
      run_artifacts = run_artifacts
        + [
        artifact(
          {
            kind: "summary",
            title: "Visible result",
            text: text,
            source: workflow_name,
            freshness: "fresh",
            relevance: 1.0,
            metadata: {visibility: "public"},
          },
        ),
      ]
    }
  }
  return run_artifacts
}

fn __workflow_result_metadata(base_run, options) {
  const base_metadata = base_run?.metadata ?? {}
  const option_metadata = options?.metadata ?? {}
  return base_metadata + option_metadata
}

/**
 * workflow_result_transcript.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_result_transcript(task, workflow_name, result, metadata) {
  const existing = __workflow_existing_transcript(result)
  if existing {
    return existing
  }
  const text = workflow_result_text(result)
  return __workflow_build_transcript(task, workflow_name, text, metadata)
}

/**
 * workflow_result_run.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_result_run(task, workflow_name, result, artifacts, options) {
  const base_run = if result?._type == "run_record" {
    result
  } else {
    result?.run ?? {}
  }
  const text = workflow_result_text(result)
  const metadata = __workflow_result_metadata(base_run, options)
  const transcript = if __workflow_existing_transcript(result) {
    __workflow_existing_transcript(result)
  } else {
    __workflow_build_transcript(task, workflow_name, text, metadata)
  }
  const run_artifacts = __workflow_result_artifacts(
    base_run?.artifacts ?? result?.artifacts ?? artifacts,
    text,
    workflow_name,
  )
  return run_record(
    base_run
      + {
      workflow_id: base_run?.workflow_id ?? workflow_name,
      workflow_name: base_run?.workflow_name ?? workflow_name,
      task: task,
      status: options?.status ?? base_run?.status ?? "completed",
      transcript: transcript,
      artifacts: run_artifacts,
      trace_spans: trace_spans(),
      metadata: metadata,
    },
  )
}

/**
 * workflow_result_persist.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_result_persist(task, workflow_name, result, artifacts, options) {
  const base_run = if result?._type == "run_record" {
    result
  } else {
    result?.run ?? {}
  }
  const text = workflow_result_text(result)
  const metadata = __workflow_result_metadata(base_run, options)
  const transcript = if __workflow_existing_transcript(result) {
    __workflow_existing_transcript(result)
  } else {
    __workflow_build_transcript(task, workflow_name, text, metadata)
  }
  const run_artifacts = __workflow_result_artifacts(
    base_run?.artifacts ?? result?.artifacts ?? artifacts,
    text,
    workflow_name,
  )
  const run = run_record(
    base_run
      + {
      workflow_id: base_run?.workflow_id ?? workflow_name,
      workflow_name: base_run?.workflow_name ?? workflow_name,
      task: task,
      status: options?.status ?? base_run?.status ?? "completed",
      transcript: transcript,
      artifacts: run_artifacts,
      trace_spans: trace_spans(),
      metadata: metadata,
    },
  )
  const persisted = run_record_save(run, options?.path)
  return {
    path: persisted?.path,
    persisted_path: persisted?.path,
    run: persisted?.run,
    status: persisted?.run?.status ?? "completed",
    transcript: persisted?.run?.transcript,
    artifacts: persisted?.run?.artifacts ?? [],
    visible_text: text,
  }
}

/**
 * handoff_artifact.
 *
 * @effects: []
 * @errors: []
 */
pub fn handoff_artifact(value) {
  const item = handoff(value)
  const target = item?.target_persona_or_human ?? {}
  const label = target?.label ?? target?.id ?? "unknown"
  return artifact(
    {
      kind: "handoff",
      title: "Handoff to " + label,
      text: handoff_context(item),
      data: item,
      source: item?.source_persona,
      freshness: "fresh",
      relevance: item?.confidence ?? 1.0,
      metadata: {handoff_id: item?.id, target_kind: target?.kind, target_label: label},
    },
  )
}

/**
 * workflow_session.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session(prev) {
  const transcript = prev?.transcript ?? prev?.result?.transcript ?? prev
  const run = prev?.run ?? prev?.result?.run
  const persisted_path = prev?.persisted_path ?? prev?.path ?? run?.persisted_path
  const workflow_id = prev?.workflow_id ?? run?.workflow_id ?? prev?.workflow ?? run?.workflow_name
  return {
    _type: "workflow_session",
    workflow_id: workflow_id,
    transcript: transcript,
    run: run,
    persisted_path: persisted_path,
    status: prev?.status ?? run?.status ?? transcript?.state ?? "active",
    workflow: prev?.workflow ?? run?.workflow?.name,
    task: prev?.task ?? run?.task,
    summary: transcript_summary(transcript),
    transcript_state: transcript?.state ?? "active",
    message_count: len(transcript_messages(transcript)),
    event_count: len(transcript_events(transcript)),
    asset_count: len(transcript_assets(transcript)),
    usage: run?.usage ?? prev?.usage,
  }
}

/**
 * workflow_session_new.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_new(metadata) {
  const raw = metadata ?? {}
  const workflow_id = raw?.workflow_id ?? raw?.workflow
  return workflow_session({status: "active", workflow_id: workflow_id, transcript: transcript(raw)})
}

/**
 * workflow_session_restore.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_restore(source) {
  const run = if type_of(source) == "string" {
    run_record_load(source)
  } else {
    source?.run ?? source
  }
  return workflow_session(
    {
      status: run?.status ?? "restored",
      workflow_id: run?.workflow_id,
      run: run,
      persisted_path: run?.persisted_path,
      transcript: run?.transcript ?? transcript(),
    },
  )
}

/**
 * workflow_session_fork.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_fork(prev) {
  const session = workflow_session(prev)
  return workflow_session(session + {transcript: transcript_fork(session.transcript)})
}

/**
 * workflow_session_archive.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_archive(prev) {
  const session = workflow_session(prev)
  return workflow_session(session + {status: "archived", transcript: transcript_archive(session.transcript)})
}

/**
 * workflow_session_resume.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_resume(prev) {
  const session = workflow_session(prev)
  return workflow_session(session + {status: "active", transcript: transcript_resume(session.transcript)})
}

/**
 * workflow_compact.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_compact(prev, options) {
  const transcript = prev?.transcript ?? prev
  return transcript_summarize(transcript, options)
}

/**
 * workflow_session_compact.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_compact(prev, options) {
  const session = workflow_session(prev)
  return workflow_session(session + {transcript: transcript_summarize(session.transcript, options)})
}

/**
 * workflow_reset.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_reset(prev, carry_summary) {
  const transcript = prev?.transcript ?? prev
  if carry_summary && transcript_summary(transcript) {
    return transcript_compact(transcript, {keep_last: 0})
  }
  return transcript()
}

/**
 * workflow_session_reset.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_reset(prev, carry_summary) {
  const session = workflow_session(prev)
  return workflow_session(
    {
      workflow_id: session.workflow_id,
      workflow: session.workflow,
      status: "active",
      transcript: workflow_reset(session.transcript, carry_summary),
    },
  )
}

/**
 * continue_as_new.
 *
 * @effects: []
 * @errors: []
 */
pub fn continue_as_new(prev, options = nil) {
  const session = workflow_session(prev)
  const carry_summary = options?.carry_summary ?? true
  const _ = workflow.continue_as_new(session)
  return workflow_session(
    {
      workflow_id: session.workflow_id,
      workflow: session.workflow,
      status: "active",
      persisted_path: session.persisted_path,
      run: session.run,
      transcript: workflow_reset(session.transcript, carry_summary),
    },
  )
}

/**
 * workflow_session_persist.
 *
 * @effects: []
 * @errors: []
 */
pub fn workflow_session_persist(prev, path) {
  const session = workflow_session(prev)
  if session.run == nil {
    return session
  }
  const persisted = run_record_save(session.run, path)
  return workflow_session(session + {run: persisted?.run, persisted_path: persisted?.path})
}

/**
 * worker_request.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_request(worker) {
  return worker?.request ?? worker?.worker?.request ?? worker?.data?.request
}

/**
 * worker_result.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_result(worker) {
  return worker?.result ?? worker?.data?.payload ?? worker?.worker?.result
}

/**
 * worker_provenance.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_provenance(worker) {
  return worker?.provenance ?? worker?.worker?.provenance ?? worker?.data?.provenance
}

/**
 * worker_research_questions.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_research_questions(worker) {
  return worker_request(worker)?.research_questions ?? []
}

/**
 * worker_action_items.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_action_items(worker) {
  return worker_request(worker)?.action_items ?? []
}

/**
 * worker_workflow_stages.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_workflow_stages(worker) {
  return worker_request(worker)?.workflow_stages ?? []
}

/**
 * worker_verification_steps.
 *
 * @effects: []
 * @errors: []
 */
pub fn worker_verification_steps(worker) {
  return worker_request(worker)?.verification_steps ?? []
}