Skip to main content

@flow-state-dev/core/prompt

Interfaces​

RenderTaggedContextOptions​

Options that adjust pretty-printing and escaping.

Properties​

escape?​
escape?: boolean;

Escape <, >, & in string leaves. Default: true.

indent?​
indent?: string;

Indentation per nesting level. Default: two spaces. Pass "" for compact output.


TagAccumulator​

Aggregated tag tree built by aggregateContextEntries. Keys are the canonical (kebab-case-normalized) tag names. Values are either string-leaf arrays or nested accumulators.

Insertion order is not preserved by this map alone — the runtime tracks authoring order separately in a parallel order: string[] array.

Indexable​

[key: string]: TagAccumulatorValue

Type Aliases​

MaybeString​

type MaybeString = string | undefined | null | false;

A value that may be a string or falsy (undefined, null, empty string, false).


SectionOptions​

type SectionOptions = object;

Options form for section. level (1–6) sets the heading depth so a section can nest under another (e.g. level: 3 → ###).

Properties​

level?​
level?: number;
title​
title: string;

TagAccumulatorValue​

type TagAccumulatorValue = string[] | TagAccumulator;

A node inside an aggregated tag tree.

  • string[] — leaf content. Multiple values are joined by newline.
  • TagAccumulator — nested tags. Recurses through renderTaggedContext.

Variables​

RESERVED_TAG_NAMES​

const RESERVED_TAG_NAMES: ReadonlySet<string>;

Names that may not be used as user-authored context tag keys.

Functions​

codeBlock()​

function codeBlock(code, language?): string;

Wraps text in a fenced code block with optional language tag.

Parameters​

code​

string

language?​

string

Returns​

string

Example​

codeBlock("const x = 1;", "ts")
// => "```ts\nconst x = 1;\n```"

codeBlock("plain text")
// => "```\nplain text\n```"

entries()​

function entries<T>(record, formatter): string;

Iterates over a record, applying a formatter to each entry. Useful for rendering typed collections where each entry needs custom formatting.

Type Parameters​

T​

T

Parameters​

record​

Record<string, T>

formatter​

(key, value) => MaybeString

Returns​

string

Example​

const artifacts = { doc1: { title: "Intro" }, doc2: { title: "Conclusion" } };
entries(artifacts, (id, art) => `[${id}] ${art.title}`)
// => "[doc1] Intro\n[doc2] Conclusion"

join()​

function join(...parts): string;

Joins multiple string fragments with double newlines. Falsy values are filtered out, making conditional assembly trivial.

Parameters​

parts​

...MaybeString[]

Returns​

string

Example​

join(
section("Intro", "Hello"),
when(showDetails, () => section("Details", detailText)),
section("End", "Goodbye")
)

keyValues()​

function keyValues(data): string;

Formats a record as key-value pairs, one per line. Null and undefined values are filtered out.

Parameters​

data​

Record<string, string | number | boolean | null | undefined>

Returns​

string

Example​

keyValues({ name: "Alice", role: "admin", score: 42 })
// => "name: Alice\nrole: admin\nscore: 42"

keyValues({ a: "yes", b: null, c: "maybe" })
// => "a: yes\nc: maybe"

list()​

function list(items, options?): string;

Formats an array as a bulleted or numbered list. Falsy items are filtered out.

Parameters​

items​

MaybeString[]

options?​
bullet?​

string

ordered?​

boolean

Returns​

string

Example​

list(["apples", "bananas", "cherries"])
// => "- apples\n- bananas\n- cherries"

list(["first", "second"], { ordered: true })
// => "1. first\n2. second"

list(["custom"], { bullet: "*" })
// => "* custom"

renderTaggedContext()​

function renderTaggedContext(
tagged,
order,
options?
): string;

Render an aggregated tag tree (produced by aggregateContextEntries) into a single XML string. Keys with no contributed content are omitted.

Parameters​

tagged​

TagAccumulator

Aggregated tag tree keyed by canonical tag name.

order​

string[]

Authoring order for top-level keys. Keys not in order are appended in Object.keys(tagged) order.

options?​

RenderTaggedContextOptions

Pretty-printing and escaping controls.

Returns​

string

Example​

renderTaggedContext(
{ documents: ["doc body"], memory: { recent: ["item"] } },
["documents", "memory"]
)
// "<documents>\n doc body\n</documents>\n<memory>\n <recent>\n item\n </recent>\n</memory>"

section()​

function section(title, ...content): string;

Creates a titled section with content underneath. Falsy content items are filtered out.

Pass a string title for a level-2 (##) heading, or the options form { title, level } to nest under another section. level is clamped to 1–6.

Parameters​

title​

string | SectionOptions

content​

...MaybeString[]

Returns​

string

Example​

section("Research Topics", list(topics))
// => "## Research Topics\ntopic1\ntopic2"

section({ title: "Subsection", level: 3 }, "body")
// => "### Subsection\nbody"

table()​

function table(rows, options?): string;

Renders an array of records as a GitHub-flavored Markdown table.

Columns default to the union of keys across all rows in first-seen order; pass columns to fix the set and order. Missing cells render empty, values are stringified, and | / newlines in cells are escaped so they don't break the table. Empty input (or no columns) returns an empty string.

Parameters​

rows​

Record<string, unknown>[]

options?​
columns?​

string[]

Returns​

string

Example​

table([{ ticker: "AAPL", qty: 10 }, { ticker: "JPM", qty: 5 }])
// => "| ticker | qty |\n| --- | --- |\n| AAPL | 10 |\n| JPM | 5 |"

validateTagName()​

function validateTagName(normalizedName, source?): void;

Validate a normalized (kebab-case) tag name. Throws with a descriptive message if the name is malformed or reserved.

Parameters​

normalizedName​

string

The post-normalization tag name to check.

source?​

string

Optional human-readable source (e.g. "user config", capability name) for the error message.

Returns​

void

Example​

validateTagName("documents") // ok
validateTagName("tool-use") // throws: "tool-use" is reserved
validateTagName("1st") // throws: invalid tag name

when()​

function when(condition, content): string | undefined;

Conditionally includes content based on a boolean condition. Returns the content string if condition is true, undefined if false. Content can be a string or a lazy function (evaluated only when true).

Parameters​

condition​

boolean

content​

string | (() => string)

Returns​

string | undefined

Example​

when(topics.length > 0, () => section("Topics", list(topics)))
// => section string if topics exist, undefined otherwise

when(isVerbose, "Include extra context here")
// => "Include extra context here" or undefined

xmlTag()​

function xmlTag(name, content): string;

Build a single XML tag with content. Empty content (after recursion) suppresses the tag entirely so that placeholder slots nobody filled don't leak empty <documents></documents> into the prompt.

xmlTag does not escape its content — callers must pass already-escaped strings if escaping is desired. The default rendering pipeline (renderTaggedContext) escapes string leaves before they reach this helper.

Parameters​

name​

string

content​

string | string[] | null | undefined

Returns​

string

Example​

xmlTag("documents", "doc body")
// "<documents>\n doc body\n</documents>"

xmlTag("documents", null) // ""