Your team asks you to organise an offsite in Lisbon. There are dates to agree on,
places to find, and a group to keep happy. Let’s build an assistant to help:
FieldTrip.
Right now, your app can only say pong. In this step, you give FieldTrip a job
description and connect it to an AI model so it can reply in the chat. Each
conversation keeps its own message history, so your next question can build on
the last one.
What you’ll add: instructions for the planner, a model to generate replies,
and a connection between the chat page and the agent.
Under the hood: how Flue runs the agent
An agent is an exported, capitalized function whose file starts with 'use agent'.
Flue re-runs that function before every model call. useModel selects the model.
The function name FieldTrip generates FlueFieldTripAgent and the
FLUE_FIELD_TRIP_AGENT binding. Each conversation ID selects a separate Durable
Object: Cloudflare storage that keeps that conversation’s history.
Run this command in your terminal to remove the starter’s placeholder file:
Terminal window
rmsrc/agents/.gitkeep
Then switch to your code editor, such as VS Code. In its file sidebar, open
src → agents and create a new file named field-trip.ts. Paste the
Complete file code below into that file’s editing pane and save it.
src
agents
field-trip.ts
Create this file. Create the parent folders if needed. 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.
return`You are FieldTrip, a helpful team-offsite planner. You help groups plan memorable offsites by understanding their destination, dates, headcount, budget, and interests.`;
}
New file from main → cp/1-hello-agent
+8−0
Use this comparison to review what's changing. For code to paste into your editor, use Complete file.
+ return `You are FieldTrip, a helpful team-offsite planner. You help groups plan memorable offsites by understanding their destination, dates, headcount, budget, and interests.`;
+}
What changes: a new file defines the planner’s identity and selects the model
that will generate its replies. The starter had a chat page, but no agent to
answer its messages.
Walk through the change
Mark the module as an agent.'use agent' tells Flue’s build tooling to
recognise the exported, capitalized FieldTrip function. Its name becomes
FlueFieldTripAgent, the generated Cloudflare Durable Object class.
Choose the model.useModel(...) selects the Gemma model through the
cloudflare provider. That provider uses the Worker’s AI binding; the model
name is not an API key.
Give the model its job description. The returned string becomes the
instructions for the planner. It tells the model which trip details to ask
about; it is not a fixed response sent to every user.
Let the runtime repeat the setup. Flue re-runs this function before each
model call. The function declares the current model and instructions; Flue
manages the conversation history and the loop that produces the answer.
Follow it in the workshop: introduce yourself, then ask what you said first
using the same conversation ID. The instructions explain who replies; the
conversation history explains how it can recall the earlier message.
Connect the chat page to FieldTrip in src/app.ts:
src
app.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.
What changes: the HTTP route map now imports FieldTrip and mounts its agent
router. This connects the existing chat page to the new planner.
Walk through the change
Import the two pieces.FieldTrip is the agent definition;
createAgentRouter supplies Flue’s HTTP endpoints for submitting messages and
reading the conversation. Importing the agent alone would not expose it.
Mount the router at a stable address.app.route('/agents/field-trip', createAgentRouter(FieldTrip)) delegates that
part of the URL space to Flue. The chat UI and smoke script use this route
with a conversation ID to reach the right conversation.
Keep one Worker entry point.export default app lets Hono receive HTTP
requests and choose a route. / serves the chat HTML, /api/ping still
returns pong, and the agent route handles the conversation protocol.
Follow it in the workshop: open the chat and send a message. A working ping
only proves the Worker can answer HTTP requests; a FieldTrip reply proves the
agent router is connected too.
Give Cloudflare a place to store each conversation by adding the agent’s first
storage setup entry (a migration) in wrangler.jsonc:
wrangler.jsonc
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.
// Durable Object migrations. Flue generates one DO class per agent
// (FieldTrip -> FlueFieldTripAgent), but YOU own the migration history.
// Checkpoint 1 adds the first entry here. Append; never rewrite deployed entries.
- "migrations": []
+ "migrations": [
+ {
+ "tag": "v1",
+ "new_sqlite_classes": ["FlueFieldTripAgent"]
+ }
+ ]
}
What changes: the first Durable Object migration introduces
FlueFieldTripAgent, giving the generated agent a SQLite-backed home for its
conversation data.
Walk through the change
Connect the source name to the class name. Flue generates
FlueFieldTripAgent from the FieldTrip function. The migration must use the
generated class name exactly so Cloudflare can create its storage namespace.
Record the first storage change.tag: "v1" identifies this migration;
new_sqlite_classes declares the new class with SQLite-backed storage. This
creates the class’s namespace, not one shared chat for every user.
Select an object for each conversation. Flue routes each conversation ID
to its own object in that namespace. Later turns can retrieve its history
instead of relying on a variable in the Worker’s memory.
Keep the migration history. This workshop uses the migrations array.
After deployment, retain v1 and append future entries, such as the Sandbox
migration in checkpoint 5. Rewriting an applied entry is not an upgrade path.
Follow it in the workshop: deployment should list FlueFieldTripAgent.
Continue the same conversation to test memory, then use another ID to start a
separate history.
Account selection comes from CLOUDFLARE_ACCOUNT_ID, set in your terminal during
Session setup. Use the temporary account you claimed,
and keep its account ID out of wrangler.jsonc.
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp1-live"Hi, who are you? Two sentences."
npmrunsmoke--https://field-trip-agent.<subdomain>.workers.devcp1-live"What was the first thing I asked you? One sentence."
Use the exact URL printed by the deploy command. The deployment should list the
AI binding and FlueFieldTripAgent Durable Object. The first command should end
with ✔ completed; the second should recall your introduction question.