feat(cloudflare): AI Gateway LanguageModel + DO-backed chat persistence#293
Open
sam-goodwin wants to merge 7 commits into
Open
feat(cloudflare): AI Gateway LanguageModel + DO-backed chat persistence#293sam-goodwin wants to merge 7 commits into
sam-goodwin wants to merge 7 commits into
Conversation
Contributor
Website Preview DeployedURL: https://alchemyeffectwebsite-worker-pr-293-24sz5eo6hk5t6245.testing-2b2.workers.dev Built from commit This comment updates automatically with each push. |
Contributor
|
Install the packages built from this commit: alchemy bun add alchemy@https://pkg.ing/alchemy/dd39a24@alchemy.run/better-auth bun add @alchemy.run/better-auth@https://pkg.ing/@alchemy.run/better-auth/dd39a24@alchemy.run/pr-package bun add @alchemy.run/pr-package@https://pkg.ing/@alchemy.run/pr-package/dd39a24 |
dd39a24 to
fc8b229
Compare
- AiGatewayLanguageModel: Workers AI LanguageModel provider over the AI Gateway (generateText + streamText with full SSE pipeline) - DurableObjectBackingPersistence: Effect BackingPersistence backed by state.storage; pairs with Chat.layerPersisted for cross-invocation chat history - Slim Agent: expose toolkit and handlerLayer as static fields, drop the chat/coding/lsp/fs subtrees in favor of composing Effect's AI primitives directly - Tutorial: tutorial/cloudflare/agent walks through wrapping the gateway as a LanguageModel and persisting turns inside a Durable Object Co-authored-by: Cursor <cursoragent@cursor.com>
Adds a minimal chat-bot stack (Worker + DO-backed Agent + Vite SPA) that exercises the AI Gateway LanguageModel binding and DurableObject chat persistence. Drags along supporting tweaks to Vite env injection and related docs/tooling. Co-authored-by: Cursor <cursoragent@cursor.com>
- examples/cloudflare-chat-bot: switchable LanguageModel layer (Kimi via
AI Gateway, OpenAI gpt-5.4-nano, Anthropic Haiku) wired from a UI
dropdown; API keys flow through Alchemy.Secret accessors.
- website/guides/effect-ai.mdx: new generic Effect AI guide showing how
to provide a LanguageModel layer to Cloudflare Worker / AWS Lambda
platforms, plus Chat persistence and provider links.
- website/tutorial/cloudflare/{ai-gateway,agent}.mdx: rewritten around
LanguageModel.generateText/streamText and Layer.mergeAll +
Layer.provideMerge composition.
- Collapse all double Effect.provide chains in docs/examples into single
Effect.provide(Layer.mergeAll(...)) calls.
Co-authored-by: Cursor <cursoragent@cursor.com>
0c71dec to
dee0ae4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Wire Workers AI through the AI Gateway as an Effect
LanguageModel, with a Durable Object–backedBackingPersistenceso chat history lives instate.storageand survives across DO invocations.A
POST /api/chaton the Worker that owns this DO returns the assistant reply and re-reads its history on subsequent turns — the samegetByName(id)instance keeps itsChatstate across separate HTTP requests, because the underlyingBackingPersistencewrites tostate.storage.What's in here
Cloudflare.AiGatewayLanguageModel—LanguageModel.LanguageModelprovider for Workers AI via the AI Gateway. SupportsgenerateTextandstreamText(full SSE pipeline, text + tool-call deltas).Cloudflare.DurableObjectChatPersistence— EffectBackingPersistenceLayer backed by a DO'sstate.storage. Drop in underChat.layerPersisted({ storeId })for fully-persistent chat sessions; nothing else to wire.Agent—toolkitandhandlerLayerare now static class fields. The old chat / coding / lsp / fs subtrees are removed; you compose Effect's AI primitives directly.generateText, (b)streamTextover real SSE, and (c) a two-turn conversation where turn 2 recalls a fact from turn 1 across separate HTTP requests (different DO invocations).Example app
examples/cloudflare-chat-bot/— full stack: Worker + DOChatAgent(this PR) + Vite React SPA. Routes:POST /api/chat?id=:sessionId— send a prompt, get the assistant reply + full conversationGET /api/messages?id=:sessionId— fetch existing threadPOST /api/reset?id=:sessionId— clear threadOne
getByName(sessionId)per session = one persisted chat thread per user. Tools (get_current_time,calculate,roll_dice) demonstrateToolkit.toLayerend-to-end.Docs
tutorial/cloudflare/agentwalks from "bind the gateway" → "wrap asLanguageModel" → "persist in a DO" step by step.