harn-stdlib 0.10.0

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
/*
 * std/ui_resource - MCP Apps-compatible UI resource envelopes with text and
 * structured fallbacks.
 *
 * Harn workflows describe interactive UI resources without depending on a
 * browser runtime. Hosts that advertise MCP Apps capability receive the
 * `ui://` resource and a `_meta.ui` block matching the MCP Apps overview
 * (https://modelcontextprotocol.io/extensions/apps/overview). Hosts that do
 * not advertise the capability receive the same payload with the resource
 * stripped, leaving a text fallback and an optional structured fallback so
 * the tool result is still useful in plain chat or headless contexts.
 *
 * The resource HTML is validated through `std/artifact/web` so the same
 * network/host-bridge/secret rules used by safe artifact patching apply to
 * embedded UI payloads. Validation findings remain attached to the resource
 * envelope and to the tool-result wrapper, and `ui_tool_result_validate`
 * refuses to ship a resource whose HTML failed validation unless the caller
 * explicitly opts in to a degraded preview.
 */
import { web_artifact_text_fallback, web_artifact_validate } from "std/artifact/web"
import { filter_nil } from "std/collections"

type UiResourceVisibility = "app_only" | "model_visible" | "always_visible" | string

type UiResourceContentsEncoding = "utf8" | "base64"

type UiResourceCsp = {
  default_src: list<string>,
  script_src: list<string>,
  style_src: list<string>,
  img_src: list<string>,
  connect_src: list<string>,
  frame_ancestors: list<string>,
  sandbox: list<string>,
}

type UiResourceValidationSummary = {
  ok: bool,
  error_codes: list<string>,
  warning_codes: list<string>,
}

type UiResource = {
  schema: "harn.ui_resource.v1",
  uri: string,
  name: string,
  description?: string,
  mime_type: string,
  profile: string,
  contents: string,
  contents_encoding: "utf8" | "base64",
  content_sha256: string,
  size_bytes: int,
  version?: string,
  permissions: list<string>,
  capabilities: list<string>,
  csp: UiResourceCsp,
  validation: UiResourceValidationSummary,
  meta: dict,
}

type UiResourceOptions = {
  description?: string,
  mime_type?: string,
  profile?: string,
  contents_encoding?: UiResourceContentsEncoding,
  version?: string,
  permissions?: list<string>,
  capabilities?: list<string>,
  csp?: UiResourceCsp,
  validation?: dict,
  meta?: dict,
}?

type UiToolMetaUi = {
  resource_uri: string,
  resource_name: string,
  profile: string,
  visibility: UiResourceVisibility,
  initial_view?: dict,
  permissions: list<string>,
  capabilities: list<string>,
}

type UiToolMeta = {schema: "harn.ui_tool_meta.v1", ui: UiToolMetaUi}

type UiToolMetaOptions = {visibility?: UiResourceVisibility, initial_view?: dict}?

type UiTextFallback = {schema: "harn.ui_fallback.text.v1", content: string}

type UiStructuredFallback = {schema: "harn.ui_fallback.structured.v1", data: dict, text?: string}

type UiStructuredFallbackOptions = {text?: string}?

type UiDefaultTextFallbackOptions = {max_chars?: int}?

type UiHostCapabilities = {
  apps: bool,
  profiles: list<string>,
  permissions: list<string>,
  bridges: list<string>,
}

type UiHostCapabilityInput = dict?

type UiToolCallEnvelope = {
  schema: "harn.ui_tool_call.v1",
  jsonrpc: "2.0",
  id: string,
  method: "tools/call",
  params: dict,
}

type UiToolCallOptions = {id?: string, meta?: dict}?

type UiContextUpdateEnvelope = {
  schema: "harn.ui_context_update.v1",
  jsonrpc: "2.0",
  id: string,
  method: "context/update",
  params: dict,
}

type UiContextUpdateOptions = {id?: string, model_visible?: bool}?

type UiToolResult = {
  schema: "harn.ui_tool_result.v1",
  ui_resource?: UiResource,
  meta: UiToolMeta,
  text_fallback: UiTextFallback,
  structured_fallback?: UiStructuredFallback,
  validation: UiResourceValidationSummary,
  selected: string,
}

type UiToolResultOptions = {
  tool_meta?: UiToolMeta,
  tool_meta_options?: UiToolMetaOptions,
  text_fallback?: UiTextFallback | string,
  structured_fallback?: UiStructuredFallback,
  allow_invalid_resource?: bool,
  fallback?: UiDefaultTextFallbackOptions,
}?

const UI_RESOURCE_SCHEMA = "harn.ui_resource.v1"

const UI_TOOL_META_SCHEMA = "harn.ui_tool_meta.v1"

const UI_TEXT_FALLBACK_SCHEMA = "harn.ui_fallback.text.v1"

const UI_STRUCTURED_FALLBACK_SCHEMA = "harn.ui_fallback.structured.v1"

const UI_TOOL_RESULT_SCHEMA = "harn.ui_tool_result.v1"

const UI_TOOL_CALL_SCHEMA = "harn.ui_tool_call.v1"

const UI_CONTEXT_UPDATE_SCHEMA = "harn.ui_context_update.v1"

const UI_MCP_APP_PROFILE = "mcp-app"

const UI_MCP_APP_MIME_TYPE = "text/html;profile=mcp-app"

const UI_DEFAULT_CSP = {
  default_src: ["'self'"],
  script_src: ["'self'", "'unsafe-inline'"],
  style_src: ["'self'", "'unsafe-inline'"],
  img_src: ["'self'", "data:"],
  connect_src: ["'none'"],
  frame_ancestors: ["'none'"],
  sandbox: ["allow-scripts", "allow-same-origin"],
}

fn __ui_text(value) {
  if value == nil {
    return ""
  }
  return trim(to_string(value))
}

fn __ui_first(values) {
  for value in values {
    const text = __ui_text(value)
    if text != "" {
      return text
    }
  }
  return nil
}

fn __ui_str_list(values) {
  let out = []
  for value in values ?? [] {
    const text = __ui_text(value)
    if text != "" && !out.contains(text) {
      out = out.push(text)
    }
  }
  return out
}

fn __ui_validate_uri(uri) {
  const text = __ui_text(uri)
  if text == "" {
    throw "std/ui_resource: uri is required"
  }
  if !text.starts_with("ui://") {
    throw "std/ui_resource: uri must use the ui:// scheme, got " + text
  }
  return text
}

fn __ui_validation_summary(validation) {
  return {
    ok: validation?.ok ?? false,
    error_codes: validation?.error_codes ?? [],
    warning_codes: validation?.warning_codes ?? [],
  }
}

fn __ui_csp(options) {
  const base = options?.csp ?? UI_DEFAULT_CSP
  return {
    default_src: __ui_str_list(base.default_src ?? UI_DEFAULT_CSP.default_src),
    script_src: __ui_str_list(base.script_src ?? UI_DEFAULT_CSP.script_src),
    style_src: __ui_str_list(base.style_src ?? UI_DEFAULT_CSP.style_src),
    img_src: __ui_str_list(base.img_src ?? UI_DEFAULT_CSP.img_src),
    connect_src: __ui_str_list(base.connect_src ?? UI_DEFAULT_CSP.connect_src),
    frame_ancestors: __ui_str_list(base.frame_ancestors ?? UI_DEFAULT_CSP.frame_ancestors),
    sandbox: __ui_str_list(base.sandbox ?? UI_DEFAULT_CSP.sandbox),
  }
}

/**
 * Build a Content-Security-Policy header value from a CSP dict.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_resource_csp_header(csp: UiResourceCsp) -> string {
  let directives = []
  const entries = [
    {name: "default-src", values: csp.default_src},
    {name: "script-src", values: csp.script_src},
    {name: "style-src", values: csp.style_src},
    {name: "img-src", values: csp.img_src},
    {name: "connect-src", values: csp.connect_src},
    {name: "frame-ancestors", values: csp.frame_ancestors},
  ]
  for entry in entries {
    if len(entry.values) > 0 {
      directives = directives.push(entry.name + " " + join(entry.values, " "))
    }
  }
  return join(directives, "; ")
}

/**
 * Build the `sandbox` attribute value for the host iframe.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_resource_sandbox_attr(csp: UiResourceCsp) -> string {
  return join(csp.sandbox, " ")
}

/**
 * Build a `harn.ui_resource.v1` envelope from HTML source.
 *
 * `options.contents_encoding` selects between inline UTF-8 (default) and
 * base64. Validation always runs through `std/artifact/web` so resource
 * envelopes pin the same network/host-bridge/secret guarantees the rest of
 * Harn's artifact pipeline uses.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_resource(uri: string, name: string, html: string, options: UiResourceOptions = nil) -> UiResource {
  const normalized_uri = __ui_validate_uri(uri)
  const normalized_name = __ui_text(name)
  if normalized_name == "" {
    throw "std/ui_resource: name is required"
  }
  const source = html ?? ""
  // MCP Apps resources communicate with the host through postMessage by
  // contract, so we default to allowing the artifact-web host-bridge surface.
  // Callers can pin a tighter policy via `options.validation`.
  const validation_options = {allow_host_bridge: true}.merge(options?.validation ?? {})
  const validation = web_artifact_validate(source, validation_options)
  const encoding = options?.contents_encoding ?? "utf8"
  const contents = if encoding == "base64" {
    bytes_to_base64(bytes_from_string(source))
  } else if encoding == "utf8" {
    source
  } else {
    throw "std/ui_resource: contents_encoding must be utf8 or base64, got " + __ui_text(encoding)
  }
  const envelope = {
    schema: UI_RESOURCE_SCHEMA,
    uri: normalized_uri,
    name: normalized_name,
    description: __ui_first([options?.description]),
    mime_type: options?.mime_type ?? UI_MCP_APP_MIME_TYPE,
    profile: options?.profile ?? UI_MCP_APP_PROFILE,
    contents: contents,
    contents_encoding: encoding,
    content_sha256: "sha256:" + sha256(source),
    size_bytes: len(source),
    version: __ui_first([options?.version]),
    permissions: __ui_str_list(options?.permissions ?? []),
    capabilities: __ui_str_list(options?.capabilities ?? []),
    csp: __ui_csp(options),
    validation: __ui_validation_summary(validation),
    meta: options?.meta ?? {},
  }
  return filter_nil(envelope)
}

/**
 * Build a `_meta.ui` tool-declaration block describing a UI resource.
 *
 * MCP Apps hosts read `_meta.ui.resourceUri` from tool descriptions, so the
 * returned envelope mirrors the host-facing keys via `to_mcp_meta` for direct
 * inclusion in MCP `tools/list` payloads.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_tool_meta(resource: UiResource, options: UiToolMetaOptions = nil) -> UiToolMeta {
  const visibility = options?.visibility ?? "app_only"
  if !["app_only", "model_visible", "always_visible"].contains(visibility) {
    throw "std/ui_resource: visibility must be app_only, model_visible, or always_visible"
  }
  const ui = filter_nil(
    {
      resource_uri: resource.uri,
      resource_name: resource.name,
      profile: resource.profile ?? UI_MCP_APP_PROFILE,
      visibility: visibility,
      initial_view: options?.initial_view,
      permissions: resource.permissions,
      capabilities: resource.capabilities,
    },
  )
  return {schema: UI_TOOL_META_SCHEMA, ui: ui}
}

/**
 * Serialize `ui_tool_meta` into the MCP Apps `_meta.ui` dict shape.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_tool_meta_to_mcp(meta: UiToolMeta) -> dict {
  const ui = meta.ui
  return {
    ui: filter_nil(
      {
        resourceUri: ui.resource_uri,
        resourceName: ui.resource_name,
        profile: ui.profile,
        visibility: ui.visibility,
        initialView: ui.initial_view,
        permissions: ui.permissions,
        capabilities: ui.capabilities,
      },
    ),
  }
}

/**
 * Build a text fallback envelope for hosts without embedded UI support.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_text_fallback(content: string) -> UiTextFallback {
  const text = __ui_text(content)
  if text == "" {
    throw "std/ui_resource: text fallback content must not be empty"
  }
  return {schema: UI_TEXT_FALLBACK_SCHEMA, content: text}
}

/**
 * Build a structured fallback envelope for hosts that prefer JSON results.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_structured_fallback(data: dict, options: UiStructuredFallbackOptions = nil) -> UiStructuredFallback {
  if data == nil {
    throw "std/ui_resource: structured fallback data is required"
  }
  return filter_nil({schema: UI_STRUCTURED_FALLBACK_SCHEMA, data: data, text: __ui_first([options?.text])})
}

fn __ui_default_text_fallback(resource: UiResource, fallback_options: UiDefaultTextFallbackOptions) {
  const html = if resource.contents_encoding == "base64" {
    bytes_to_string(bytes_from_base64(resource.contents))
  } else {
    resource.contents
  }
  const max_chars = fallback_options?.max_chars ?? 2000
  return web_artifact_text_fallback(html, {max_chars: max_chars})
}

/**
 * Wrap a UI resource and matching fallbacks into a single tool-result envelope.
 *
 * Hosts that advertise MCP Apps support read `ui_resource` and the
 * accompanying `_meta.ui`. Hosts without that capability still render either
 * the text fallback or the optional structured fallback. The text fallback is
 * mandatory so every host has a non-empty rendering path.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_tool_result(resource: UiResource, options: UiToolResultOptions = nil) -> UiToolResult {
  const meta = options?.tool_meta ?? ui_tool_meta(resource, options?.tool_meta_options)
  const text_fallback = if options?.text_fallback != nil {
    if options?.text_fallback?.schema == UI_TEXT_FALLBACK_SCHEMA {
      options?.text_fallback
    } else {
      ui_text_fallback(to_string(options?.text_fallback))
    }
  } else {
    ui_text_fallback(__ui_default_text_fallback(resource, options?.fallback))
  }
  const structured = options?.structured_fallback
  const allow_invalid = options?.allow_invalid_resource ?? false
  const include_resource = resource.validation.ok || allow_invalid
  const envelope = {
    schema: UI_TOOL_RESULT_SCHEMA,
    ui_resource: include_resource ? resource : nil,
    meta: meta,
    text_fallback: text_fallback,
    structured_fallback: structured,
    validation: __ui_validation_summary(resource.validation),
    selected: "ui_resource",
  }
  return filter_nil(envelope)
}

/**
 * Normalize a host-advertised capabilities dict into a `UiHostCapabilities`.
 *
 * MCP Apps (`capabilities.apps = {enabled, profiles, ...}`), the OpenAI Apps
 * SDK (`ui.apps = true` + `ui.profiles`), and bare `{apps: true}` flag shapes
 * all funnel through the same returned surface so downstream selection logic
 * does not need host-specific branches.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_host_capabilities(input: UiHostCapabilityInput = nil) -> UiHostCapabilities {
  const value = input ?? {}
  const apps_flag = value?.apps?.enabled ?? value?.ui?.apps ?? value?.apps ?? false
  const apps = type_of(apps_flag) == "bool" ? apps_flag : false
  const declared_profiles = __ui_str_list(value?.apps?.profiles ?? value?.ui?.profiles ?? value?.profiles ?? [])
  const profiles = if len(declared_profiles) > 0 {
    declared_profiles
  } else {
    apps ? [UI_MCP_APP_PROFILE] : []
  }
  return {
    apps: apps || len(declared_profiles) > 0,
    profiles: profiles,
    permissions: __ui_str_list(value?.apps?.permissions ?? value?.ui?.permissions ?? value?.permissions),
    bridges: __ui_str_list(value?.apps?.bridges ?? value?.ui?.bridges ?? value?.bridges),
  }
}

/**
 * Return true if the host capabilities advertise MCP Apps support.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_host_supports_apps(capabilities: UiHostCapabilityInput = nil) -> bool {
  const caps = ui_host_capabilities(capabilities)
  if !caps.apps {
    return false
  }
  if len(caps.profiles) == 0 {
    return true
  }
  return caps.profiles.contains(UI_MCP_APP_PROFILE)
}

/**
 * Choose the best representation for a host.
 *
 * Returns a copy of `result` with `selected` set to `"ui_resource"`,
 * `"structured_fallback"`, or `"text_fallback"`. Hosts without UI support
 * receive the same envelope minus `ui_resource` so they still see the
 * fallbacks and provenance metadata.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_select_for_host(result: UiToolResult, capabilities: UiHostCapabilityInput = nil) -> UiToolResult {
  const supports_apps = ui_host_supports_apps(capabilities)
  const resource = result.ui_resource
  const resource_ok = resource != nil && resource.validation.ok
  if supports_apps && resource_ok {
    return result.merge({selected: "ui_resource"})
  }
  const selected = if result.structured_fallback != nil {
    "structured_fallback"
  } else {
    "text_fallback"
  }
  return result.merge({ui_resource: nil, selected: selected})
}

/**
 * Build the host->guest JSON-RPC envelope a sandboxed UI sees over postMessage.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_tool_call_envelope(name: string, params: dict? = nil, options: UiToolCallOptions = nil) -> UiToolCallEnvelope {
  const tool_name = __ui_text(name)
  if tool_name == "" {
    throw "std/ui_resource: tool name is required"
  }
  const id = (options?.id ?? "ui_call_") + substring(sha256(tool_name + json_stringify(params ?? {})), 0, 16)
  return {
    schema: UI_TOOL_CALL_SCHEMA,
    jsonrpc: "2.0",
    id: id,
    method: "tools/call",
    params: filter_nil({name: tool_name, arguments: params ?? {}, _meta: options?.meta}),
  }
}

/**
 * Build the guest->host JSON-RPC envelope used to update model-visible context.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_context_update_envelope(key: string, value, options: UiContextUpdateOptions = nil) -> UiContextUpdateEnvelope {
  const context_key = __ui_text(key)
  if context_key == "" {
    throw "std/ui_resource: context update key is required"
  }
  const id = (options?.id ?? "ui_ctx_") + substring(sha256(context_key + json_stringify(value ?? nil)), 0, 16)
  return {
    schema: UI_CONTEXT_UPDATE_SCHEMA,
    jsonrpc: "2.0",
    id: id,
    method: "context/update",
    params: filter_nil({key: context_key, value: value, model_visible: options?.model_visible ?? true}),
  }
}

/**
 * Validate a tool-result envelope and its inner shapes.
 *
 * @effects: []
 * @errors: []
 */
pub fn ui_tool_result_validate(result: UiToolResult) -> UiToolResult {
  if result.schema != UI_TOOL_RESULT_SCHEMA {
    throw "std/ui_resource: unsupported tool result schema " + __ui_text(result.schema)
  }
  if result.text_fallback.schema != UI_TEXT_FALLBACK_SCHEMA {
    throw "std/ui_resource: text_fallback is required"
  }
  if __ui_text(result.text_fallback.content) == "" {
    throw "std/ui_resource: text_fallback content must not be empty"
  }
  if result.meta.schema != UI_TOOL_META_SCHEMA {
    throw "std/ui_resource: meta block is required"
  }
  if result.ui_resource != nil {
    const resource = result.ui_resource
    if resource.schema != UI_RESOURCE_SCHEMA {
      throw "std/ui_resource: ui_resource has unsupported schema " + __ui_text(resource.schema)
    }
    if !resource.validation.ok {
      throw "std/ui_resource: ui_resource failed validation (codes: "
        + join(resource.validation.error_codes, ",")
        + ")"
    }
  }
  if result.structured_fallback != nil {
    const structured = result.structured_fallback
    if structured.schema != UI_STRUCTURED_FALLBACK_SCHEMA {
      throw "std/ui_resource: structured_fallback has unsupported schema " + __ui_text(structured.schema)
    }
  }
  return result
}