feat(good-fences): a chat page that shows the yard as it's mowed

The page talks to a Neighbor over the agent WebSocket: it sends chat
requests, shows the code the model runs and what came of it, and draws
the yard from the agent's state.

The yard is the Neighbor's state, so the page hears each move and a
yard survives eviction: the mower picks up where the saved yard left
off. Moves go out at most every 200ms, so a long run of pushes is not a
write per cell. Only the server may change that state; Agent state
otherwise accepts updates from any connected client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dckcandClaude Opus 5.5 committed 2026-11-03 23:29:21 -06:00
1 parent 76ec2c0019
commit 3c4915f5ad
4 files changed
+242 -12

No files matched your search

+22 -7
View File
@@ -21,13 +21,27 @@ const headings = [
[-1, 0], // west
] as const;
/** a mower on a `width` × `height` yard, starting at `at`; pushing past an
* edge wraps to the other side (6d) */
export const makeMower = ({ width, height, at }: { width: number; height: number; at: { x: number; y: number } }) => {
let { x, y } = at;
let heading = 0;
const cut = new Set<string>();
const cutHere = () => cut.add(`${x},${y}`);
/** where a mower is and what it has cut, as `"x,y"` cells; `heading` indexes north, east, south, west */
export type Yard = Readonly<{
width: number;
height: number;
x: number;
y: number;
heading: number;
cut: readonly string[];
}>;
/** a mower on a yard, picking up where `yard` left off; pushing past an
* edge wraps to the other side (6d). `onMove` hears each move. */
export const makeMower = (yard: Yard, onMove: (yard: Yard) => void = () => {}) => {
const { width, height } = yard;
let { x, y, heading } = yard;
const cut = new Set(yard.cut);
const view = (): Yard => ({ width, height, x, y, heading, cut: [...cut] });
const cutHere = () => {
cut.add(`${x},${y}`);
onMove(view());
};
return {
start() {
heading = 0;
@@ -41,6 +55,7 @@ export const makeMower = ({ width, height, at }: { width: number; height: number
},
turn() {
heading = (heading + 1) % headings.length;
onMove(view());
},
stop() {},
cutCells: () => [...cut],
+33 -5
View File
@@ -1,16 +1,44 @@
/** @file The neighbor's harness as a Project Think agent, lent a mower. */
import { Think } from "@cloudflare/think";
import type { Connection } from "agents";
import { createExecuteTool } from "@cloudflare/think/tools/execute";
import { createWorkersAI } from "workers-ai-provider";
import { MAX_TOKENS, MODEL } from "./model.ts";
import { MowerConnector } from "./mower-connector.ts";
import { makeMower } from "./mower.ts";
import { makeMower, type Mower, type Yard } from "./mower.ts";
type Env = { AI: Ai; LOADER: WorkerLoader };
export class Neighbor extends Think<Env> {
#mower = makeMower({ width: 40, height: 40, at: { x: 20, y: 20 } });
/** how often, at most, a mowing yard goes out to the page */
const PUBLISH_MS = 200;
export class Neighbor extends Think<Env, Yard> {
initialState: Yard = { width: 40, height: 40, x: 20, y: 20, heading: 0, cut: [] };
#mower: Mower | undefined;
#pending: Yard | undefined;
/** the mower, back where the saved yard left it; its moves are saved and
* broadcast at most every PUBLISH_MS */
get #lent(): Mower {
this.#mower ??= makeMower(this.state, (yard) => {
const idle = this.#pending === undefined;
this.#pending = yard;
if (!idle) return;
setTimeout(() => {
const latest = this.#pending;
this.#pending = undefined;
if (latest) this.setState(latest);
}, PUBLISH_MS);
});
return this.#mower;
}
/** the yard is the mower's to change; pages only watch */
validateStateChange(_next: Yard, source: Connection | "server") {
if (source !== "server") throw Error("the yard changes only by mowing");
}
getModel() {
return createWorkersAI({ binding: this.env.AI })(MODEL);
@@ -32,13 +60,13 @@ lend you something, put it to use, then tell me what you did.`;
getTools() {
return {
execute: createExecuteTool(this, {
connectors: [new MowerConnector(this.ctx, this.env, this.#mower)],
connectors: [new MowerConnector(this.ctx, this.env, this.#lent)],
}),
};
}
/** the cells the mower has cut, as `"x,y"` */
cutCells() {
return this.#mower.cutCells();
return this.#lent.cutCells();
}
}