diff --git a/good-fences/public/index.html b/good-fences/public/index.html new file mode 100644 index 0000000..6787406 --- /dev/null +++ b/good-fences/public/index.html @@ -0,0 +1,185 @@ + + + + + +Lend the Mower + + + +
+

Lend the Mower

+ yard · new yard · connecting… +
+
+
+
+
+ + +
+
+
+ +
+
+
+ + + diff --git a/good-fences/src/mower.ts b/good-fences/src/mower.ts index 424c1e3..c70d167 100644 --- a/good-fences/src/mower.ts +++ b/good-fences/src/mower.ts @@ -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(); - 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], diff --git a/good-fences/src/neighbor.ts b/good-fences/src/neighbor.ts index 81496d9..6120688 100644 --- a/good-fences/src/neighbor.ts +++ b/good-fences/src/neighbor.ts @@ -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 { - #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 { + 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(); } } diff --git a/good-fences/wrangler.jsonc b/good-fences/wrangler.jsonc index 4c2db04..17d806c 100644 --- a/good-fences/wrangler.jsonc +++ b/good-fences/wrangler.jsonc @@ -4,6 +4,8 @@ "main": "./src/worker.ts", "compatibility_date": "2026-10-01", "compatibility_flags": ["nodejs_compat"], + // the chat page; other paths fall through to the Worker + "assets": { "directory": "./public" }, "ai": { "binding": "AI" }, // Dynamic Workers: deploying this binding takes the Workers Paid plan "worker_loaders": [{ "binding": "LOADER" }],