Applies ForEach results sequentially to build the final payload in index order.
OptionalclonePayloadFn: <T>(payload: T) => TApplies an action output mapping fold onto a cloned copy of basePayload.
OptionalclonePayloadFn: <T>(payload: T) => TApplies sub-agent input mapping to prepare initial payload for a related sub-agent.
OptionalgetPathFn: (obj: unknown, path: string) => unknownApplies sub-agent output mapping to update the parent payload.
OptionalgetPathFn: (obj: unknown, path: string) => unknownBuilds the user-role record of what the model asked to run on an Actions turn ([You invoked ...]).
Creates a non-terminating 'Failed' step preserving the previous decision's payload.
AST-reduce a large code-string action-result value when the agent opted into a code
language (via crushCodeLang or a subclass override) and the value clears the size
threshold. Returns reduced code plus a one-line legend, or null to keep the string
verbatim (crushing disabled, too small, or no net saving).
token optimization via @memberjunction/context-crush (CodeCompressor-inspired)
Optionalconfig: ActionResultCrushConfigStructurally compress a JSON action-result value when crushing is enabled for the run
and the value clears the size threshold. Accepts either the JSON.stringify of an
object/array param, or a raw string param that itself contains JSON (many actions
stringify their payloads, e.g. run-adhoc-query's Results). Returns crushed text
plus a one-line legend, or null when crushing is disabled, the value is too small, the
value isn't valid JSON, or compression wouldn't actually save characters — so callers
fall back to verbatim (and, for strings, to code crushing) behavior.
token optimization via @memberjunction/context-crush (SmartCrusher-inspired)
Optionalconfig: ActionResultCrushConfigExtracts the output object from the first action result summary for loop output folding.
Formats an array of action result summaries as compact markdown instead of
pretty-printed JSON. This typically saves 60-70% of the tokens that the
old JSON.stringify(actionSummaries, null, 2) approach consumed, while
remaining equally parseable by LLMs.
Format per action:
## ActionName ✓ (or ✗ for failure)
**Result:** RESULT_CODE — human message
**Output:**
• `ParamName`: value
AI directives are intentionally omitted here because they are surfaced as a separate high-priority message (see the caller).
OptionalcrushConfig: ActionResultCrushConfigFormats all loop errors as newline-separated text for a step's ErrorMessage.
Formats loop iteration results as markdown. Handles two distinct result shapes depending on whether the loop body executed actions or sub-agents:
Action iterations: priorStepResult is an ActionResultSummary[], set by
executeActionsStep(). These are formatted via FormatActionResultsAsMarkdown()
(or formatActionResultsFn when supplied by BaseAgent).
Sub-agent iterations: priorStepResult is a pre-formatted markdown string,
set by the loop handlers (executeSingleForEachIteration / executeSingleWhileIteration)
which call formatSubAgentResultAsMarkdown() and assign the result to priorStepResult.
Since the markdown is already rendered upstream, this method simply includes it as-is.
This two-path design means sub-agent formatting happens at assignment time (in the loop handler), while action formatting happens at render time (here). Both produce the same markdown style used by non-loop results, ensuring consistency across the codebase.
OptionalcrushConfig: ActionResultCrushConfigOptionalformatActionResultsFn: (summaries: ActionResultSummary[]) => stringFormats a parameter value for display in action execution messages.
Formats a single output parameter value for inclusion in action result markdown. Scalars are shown inline with backtick formatting; objects and arrays use compact (single-line) JSON to avoid the indentation overhead of pretty-printed JSON.
By default, values are not truncated — the expiration/compaction lifecycle handles context window management. Truncating at formatting time permanently discards data before the LLM ever sees it. Pass an explicit maxLength only when you have a specific reason to limit output size.
OptionalcrushConfig: ActionResultCrushConfigFormats an ExecuteAgentResult as compact markdown for inclusion in
conversation messages. Extracts only the fields that are meaningful
to the calling agent (success, status, error, and payload) and
presents them in a concise format.
Extracts an array collection from a payload path or a static:[...] literal.
Strips "payload." prefix if present (for LLM convenience).
OptionalgetPathFn: (obj: unknown, path: string) => unknownPrepares a context message containing parent payload data or conversation messages for a related sub-agent.
Optionalparams: ExecuteAgentParamsOptionalgetPathFn: (obj: unknown, path: string) => unknownResolves the per-run action-result compression config from the agent-type prompt params.
Resolves multiple {{expression}} placeholders embedded within a literal string.
Unlike the single-variable path (which can return objects/numbers), this always returns a string because the resolved values are interpolated back into surrounding text.
OptionalresolveValue: (val: string, ctx: Record<string, unknown>, itemVar: string) => unknownResolves {{variable}} templates recursively across an object's string and array properties.
resolveValue resolves each string; resolveNested resolves each nested object. BaseAgent binds
both to its protected resolveValueFromContext / resolveTemplates, so a subclass override of
either still runs at every depth. Without resolveNested, nested objects recurse here with the same
resolveValue.
OptionalresolveValue: (val: string, ctx: Record<string, unknown>, itemVar: string) => unknownOptionalresolveNested: (Resolves a single string value or {{expression}} reference from a loop iteration context.
Handles both scalar and complex object iterations in ForEach / While loops:
{{expr}} can return non-string values (objects, numbers, arrays).{{expr}} delegate to ResolveInlineTemplateExpressions
and return an interpolated string.itemVariable match ({{user}} or {{entityName}}) returns context.item directly.Optionaloptions: {Validates a ForEachOperation header (itemVariable and collectionPath).
Validates a WhileOperation header and delegates body validation.
Collaborator extracted from
BaseAgent(WS-4.5b) responsible for step and loop dispatching utilities: action/sub-agent invocation & result markdown formatting, structural JSON/code context-crush compression, sub-agent payload cloning and input/output path mapping, ForEach/While batching & sequential result folding, and loop template interpolation ({{item.field}}).