formatters.ts
This file contains helper functions that translate the agent's internal data (like your action definitions, context state, and logs) into the structured XML format the AI model expects to see in its prompt. It also helps format Zod schemas into JSON schemas for the prompt.
How it Affects You
You don't need to call functions like formatAction
or formatContextState
directly. The framework uses them automatically when preparing the prompt for
the AI model during each step of an agent.run
. For example:
- When you define an
action
with a description and schema,formatAction
converts that definition into the<action>
XML block seen in the prompt. - When you define a
render
function for yourcontext
, the output of your function is placed inside the<state>
tag within the<context>
XML block generated byformatContextState
. - The
formatSchema
function ensures the Zod schemas you define for actions, outputs, etc., are translated into a format the AI model can understand within the prompt's<schema>
tags.
Benefit
These formatters ensure that all the information the agent needs to give the AI model (available tools, current state, recent history) is presented in a consistent, structured way (XML) that the model is trained to understand. This standardization makes the communication between your agent's code and the AI model reliable. You don't have to worry about manually creating complex XML prompts.
Anticipated Questions
- "Do I need to write XML?" No. You define your components using
JavaScript/TypeScript objects (via helpers like
action
,context
, etc.). These formatters handle the conversion to XML automatically before sending the prompt to the AI. - "Why does Daydreams use XML in prompts?" XML provides a clear way to structure complex information (like nested states, lists of tools with descriptions and schemas) for the AI model, making it easier for the model to parse and understand the different parts of the prompt.
- "What is the
render
function in this file used for?" It's primarily used internally by the framework to assemble the main prompt template by inserting the formatted XML blocks (like actions, contexts, logs) into the correct placeholders.