-
Notifications
You must be signed in to change notification settings - Fork 545
feat(experimental): Postgres session providers with Hyperdrive support #1297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattzcarey
wants to merge
4
commits into
main
Choose a base branch
from
feat/postgres-session-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6f3caa
feat(experimental): PlanetScale SessionProvider + async interface
7bbde2d
Merge branch 'main' into feat/postgres-session-provider
mattzcarey 7f24de2
fix(session): address PR #1297 review feedback
mattzcarey 941fc10
chore(session-planetscale): align kumo + ai dep versions with workspace
mattzcarey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
|
devin-ai-integration[bot] marked this conversation as resolved.
|
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Postgres Session Example | ||
|
|
||
| Agent with session history stored in an external Postgres database via [Cloudflare Hyperdrive](https://developers.cloudflare.com/hyperdrive/) instead of Durable Object SQLite. | ||
|
|
||
| ## Why external Postgres? | ||
|
|
||
| DO SQLite is great for per-user state, but sessions live and die with the DO. An external database gives you: | ||
|
|
||
| - **Cross-DO queries** — search across all conversations from any Worker | ||
| - **Analytics** — run SQL against your conversation data directly | ||
| - **Decoupled lifecycle** — session data survives DO eviction, migration, and resets | ||
| - **Shared state** — multiple DOs or services can read/write the same session tables | ||
|
|
||
| ## Setup | ||
|
|
||
| ### 1. Create a Postgres database | ||
|
|
||
| Use any Postgres provider (Neon, Supabase, PlanetScale, etc.) and copy the connection string. | ||
|
|
||
| ### 2. Create a Hyperdrive config | ||
|
|
||
| ```bash | ||
| npx wrangler hyperdrive create my-session-db \ | ||
| --connection-string="postgresql://user:password@host:port/dbname" | ||
| ``` | ||
|
|
||
| Update `wrangler.jsonc` with the returned Hyperdrive ID. | ||
|
|
||
| ### 3. Create the tables | ||
|
|
||
| Run the migration SQL from [docs/sessions.md](../../docs/sessions.md#3-create-the-tables) in your database console. The providers do not auto-create tables — migrations are managed by you. | ||
|
|
||
| ### 4. Deploy | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm run deploy | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| The key difference from the standard `session-memory` example: | ||
|
|
||
| ```ts | ||
| // Standard: auto-wires to DO SQLite | ||
| const session = Session.create(this) | ||
| .withContext("memory", { maxTokens: 1100 }) | ||
| .withCachedPrompt(); | ||
|
|
||
| // Postgres: pass providers explicitly | ||
| const conn = wrapPgClient(pgClient); | ||
|
|
||
| const session = Session.create(new PostgresSessionProvider(conn, sessionId)) | ||
| .withContext("memory", { | ||
| maxTokens: 1100, | ||
| provider: new PostgresContextProvider(conn, `memory_${sessionId}`) | ||
| }) | ||
| .withContext("knowledge", { | ||
| provider: new PostgresSearchProvider(conn) | ||
| }) | ||
| .withCachedPrompt(new PostgresContextProvider(conn, `_prompt_${sessionId}`)); | ||
| ``` | ||
|
|
||
| When `Session.create()` receives a `SessionProvider` (not a `SqlProvider`), it skips all SQLite auto-wiring. Context blocks and the prompt cache need explicit providers since there's no DO storage to fall back to. | ||
|
|
||
| ## Connection interface | ||
|
|
||
| The providers use `?` placeholders internally. This example wraps the `pg` driver to convert them to `$1, $2, ...`: | ||
|
|
||
| ```ts | ||
| interface PostgresConnection { | ||
| execute( | ||
| query: string, | ||
| args?: (string | number | boolean | null)[] | ||
| ): Promise<{ rows: Record<string, unknown>[] }>; | ||
| } | ||
| ``` | ||
|
|
||
| Any Postgres driver with a compatible `execute()` method works. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| declare namespace Cloudflare { | ||
| interface Env { | ||
| AI: Ai; | ||
| HYPERDRIVE: Hyperdrive; | ||
| } | ||
| } | ||
| interface Env extends Cloudflare.Env {} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should use session_id as part of the primary key.
Since this table is shared across sessions, id by itself makes message ids global to the whole db. If two sessions ever use the same message id, one of them gets silently skipped.
Could we make this use
PRIMARY KEY (session_id, id)and update the insert conflict to match