You tell FieldTrip: “Lisbon, 14 people, 400 EUR each.” A few messages later, two
more colleagues join. The planner needs to update 14 → 16 while keeping the
city and budget.
Think of this step as giving FieldTrip a trip notebook. The chat history
records what was said; the notebook holds the current facts in named fields.
FieldTrip updates those fields when plans change and reads them before replying.
That saved notebook is called persistent state: it stays available after an
app restart or redeployment.
What you’ll add: a saved trip brief and a way for FieldTrip to update it
when you tell it your plans have changed.
Under the hood: where the trip brief lives
usePersistentState('brief', {}) stores the brief in the current conversation’s
Durable Object. The updater merges new fields into the saved value. Before each
model call, Flue re-runs the agent function and includes the latest brief in its
instructions, so the model can use it in its next reply.
Update src/agents/field-trip.ts to give FieldTrip its notebook, a way to save
changes, and instructions to use the saved facts when answering:
src
agents
field-trip.ts
Replace the entire contents of this file. In your code editor (for example, VS Code), open the file named above. Copy the code in Complete file, paste it into that file's editing pane, and save.
// Tools can write state. The write commits together with the tool call.
useTool({
name: 'save_trip_brief',
description:
'Save or update the offsite trip brief. Call this whenever the user states or changes the city, dates, headcount, budget, or interests. Only include fields the user mentioned; they are merged into the saved brief.',
// The agent re-renders before every model call, so these instructions
// always reflect the latest saved brief.
const hasBrief = Object.keys(brief).length > 0;
return`You are FieldTrip, a helpful team-offsite planner. You help groups plan memorable offsites by understanding their destination, dates, headcount, budget, and interests.
Rules:
1. If the user's message contains ANY trip detail (city, dates, headcount, budget, interests), your FIRST action is to call \`save_trip_brief\` with those fields. Do this before writing any reply.
2. Answer questions about the trip from the saved brief below. If a detail is missing, ask for it.
3. Keep replies short: at most 120 words unless the user asks for more detail.
- return `You are FieldTrip, a helpful team-offsite planner. You help groups plan memorable offsites by understanding their destination, dates, headcount, budget, and interests.`;
+
+ // Durable, per-conversation state (stored in this conversation's Durable Object).
+ // Shaped like React's useState, but it survives restarts and redeploys.
+ // Tools can write state. The write commits together with the tool call.
+ useTool({
+ name: 'save_trip_brief',
+ description:
+ 'Save or update the offsite trip brief. Call this whenever the user states or changes the city, dates, headcount, budget, or interests. Only include fields the user mentioned; they are merged into the saved brief.',
+ // The agent re-renders before every model call, so these instructions
+ // always reflect the latest saved brief.
+ const hasBrief = Object.keys(brief).length > 0;
+ return `You are FieldTrip, a helpful team-offsite planner. You help groups plan memorable offsites by understanding their destination, dates, headcount, budget, and interests.
+
+Rules:
+1. If the user's message contains ANY trip detail (city, dates, headcount, budget, interests), your FIRST action is to call \`save_trip_brief\` with those fields. Do this before writing any reply.
+2. Answer questions about the trip from the saved brief below. If a detail is missing, ask for it.
+3. Keep replies short: at most 120 words unless the user asks for more detail.
What changes: FieldTrip gains a structured trip brief, a tool for updating
it, and instructions that include its latest value. Chat history records the
conversation; the brief records the current agreed facts.
Walk through the change
Describe the notebook.TripBrief names optional fields such as city
and headcount. It helps TypeScript check your code. The Valibot input
schema separately validates values supplied by the model at runtime.
Open durable state.usePersistentState<TripBrief>('brief', {}) returns
the saved value and its setter. 'brief' is the storage key, and {} is the
initial value for this conversation. A normal local variable would not provide
this persistence.
Save only the fields mentioned.save_trip_brief filters out undefined
values, then setBrief((previous) => ({ ...previous, ...updates })) merges
the update into the latest saved value. Updating just the headcount therefore
keeps the city and budget. The updater reads the latest value rather than an
earlier render’s snapshot.
Make the saved facts visible to the model. The returned instructions
include JSON.stringify(brief, null, 2). After the tool writes state, the next
agent render includes the updated notebook before the model answers.
Follow it in the workshop: save Lisbon, 14 people, and a budget, then change
only the headcount to 16. Inspect the second tool call: it should update
headcount without resending or erasing the other facts.
No route, config, or migration changes are needed. State uses the existing
FlueFieldTripAgent Durable Object.
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp2-live"We're planning an offsite in Lisbon from <START> to <END> for 14 people, budget around 400 EUR per person. We like food, history and something outdoorsy."
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp2-live"Update: two more people are joining, so 16 now."
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp2-live"What's my headcount, city and budget? One line."
Look for a full save_trip_brief call, then a second call containing only
{"headcount":16}. The final answer should retain Lisbon and the budget.
Optional depth check: prove isolation with a different conversation:
Terminal window
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp2-live-other"What's my saved headcount?"
The fresh conversation must not inherit cp2-live state.
This opens the same conversation as the smoke test. If you repeat this checkpoint, before copying its commands.
Start npm run dev in Terminal A and wait for VITE ... ready. Run in Terminal B:
Terminal window
npmrunsmoke--http://localhost:5173cp2-local"We're planning an offsite in Lisbon from <START> to <END> for 14 people, budget around 400 EUR per person. We like food, history and something outdoorsy."
npmrunsmoke--http://localhost:5173cp2-local"Update: two more people are joining, so 16 now."
npmrunsmoke--http://localhost:5173cp2-local"What's my headcount, city and budget? One line."
Look for a full save_trip_brief call, then a second call containing only
{"headcount":16}. The final answer should retain Lisbon and the budget.
Optional depth check: prove isolation with a different conversation:
Terminal window
npmrunsmoke--http://localhost:5173cp2-local-other"What's my saved headcount?"
The fresh conversation must not inherit cp2-local state.
This opens the same conversation as the smoke test. If you repeat this checkpoint, before copying its commands.
Verification gate
Prove it works
FieldTrip saves the initial trip details using save_trip_brief.
When two people join, it updates the headcount to 16 and keeps the city and budget.
When asked, it answers: 16 people, Lisbon, and 400 EUR per person.
Optional: a new conversation starts with an empty trip notebook.
Optional mini quizCheck your understanding: the trip notebook3 questions / instant feedback
Recovery lane
Get back on track in under a minute
If a dev server is running, stop it first. Uncommitted work is stashed and the current commit gets a backup branch before anything moves.
Option 01
Restart this exercise
Return to cp/1-hello-agent, rebuild this exercise, then use its Live deploy verification tab. Stop any running dev server with Ctrl+C before switching.