Skip to content

feat(examples): add alchemy-cloudflare chat session example - #48

Merged
K-Mistele merged 1 commit into
mainfrom
implement-chat-session-with-durable-objects-and-alchemy-tyntlz
Sep 30, 2026
Merged

K-Mistele merged 1 commit into
mainfrom
implement-chat-session-with-durable-objects-and-alchemy-tyntlz

Conversation

@K-Mistele

Copy link
Copy Markdown
Contributor

What

A new example, examples/alchemy-cloudflare: a fold chat host on Cloudflare, deployed with Alchemy v2.

  • ChatSession.ts: the Durable Object. Each fold session is one object, named by its SessionId.
    • The first send to a new ID starts the session; after that, the object resumes it from its log when it starts up.
    • send takes whenRunning: queue (the default), steer or interrupt.
  • DurableObjectEventLog.ts: fold's EventLogService stored in the object's SQLite.
  • Keepalive.ts: a 30s alarm heartbeat that keeps the object from being evicted while a turn runs.
  • Restarts: a turn cut off by a crash or deploy gets a "continue" nudge the next time the object starts up, at most 3 times per turn.
  • Api.ts / Worker.ts: HttpRouter routes over a ChatSessions service, which calls the Durable Objects.
    • POST /sessions/:sessionId/messages
    • GET /sessions/:sessionId
  • Model: OpenAI gpt-5.6-terra on medium reasoning, through openaiModel. The deploy reads OPENAI_API_KEY from your environment and stores it on the Worker as a secret.

Other changes

  • Root package.json: adds the examples/* workspace, and typecheck / test now also run over the examples.
  • Catalog: adds alchemy, @cloudflare/workers-types and @effect/platform-bun.
  • fold-core is unchanged.

Why not Workers AI

Alchemy's Workers AI LanguageModel repeats streamed text on some models and never emits streamed tool calls. fold always uses streamText, so both bugs hit it. Reported upstream: alchemy-run/alchemy#1907.

Testing

  • bun run typecheck, lint and the route tests (test/Api.test.ts, against an in-memory ChatSessions) pass.
  • Deployed and tested live on Workers AI: sessions start, resume and persist across requests.
  • The switch to OpenAI hasn't been deployed yet.

🤖 Generated with Claude Code

A fold chat host on Cloudflare deployed with Alchemy v2. Each session is a
Durable Object named by its SessionId, with the fold event log stored in the
object's SQLite. An alarm heartbeat keeps the object alive while a turn runs,
and a turn cut off by a restart is nudged to continue (at most 3 times).
HttpRouter routes reach the objects through a ChatSessions service. The model
is OpenAI gpt-5.6-terra on medium reasoning, with the API key bound from the
deploy environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HumanLayer-Session: https://app.dev.codelayer.gg/sessions/01a0ef41-7f9a-7c78-83e6-b468fe8c440e
@K-Mistele
K-Mistele merged commit 8a97886 into main Sep 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant