Skip to content

feat(storage): add Cloudflare D1 conversation storage driver - #646

Merged
edenreich merged 1 commit into
mainfrom
feat/storage-d1-driver
Jun 23, 2026
Merged

edenreich merged 1 commit into
mainfrom
feat/storage-d1-driver

Conversation

@edenreich

@edenreich edenreich commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a d1 conversation storage backend that persists conversations and the channel session-group index to Cloudflare D1 over its HTTP query API (POST /accounts/{account}/d1/database/{database}/query).

D1 is SQLite under the hood, so the driver mirrors the existing SQLite backend method-for-method and reuses the SQLite migrations verbatim - only the transport (HTTP instead of a local file handle) differs. This unblocks headless infer on ephemeral GitHub Actions runners: sqlite/jsonl/memory live on the runner's disk (wiped on recycle, unreadable externally) and postgres/redis speak wire protocols D1 doesn't, whereas D1 is external to the runner and the control plane can read it back via its native binding.

Closes #645

What changed

File Change
internal/infra/storage/d1.go (new) D1Storage implementing ConversationStorage + SessionGroupStorage; thin do/exec/queryRows HTTP layer over the D1 /query envelope; param encoder + decode helpers
internal/infra/storage/d1_test.go (new) httptest mock backed by a real in-process SQLite DB so round-trips exercise genuine SQL (ordering, not-found, JSON round-trips); mirrors the SQLite tests + a ListConversationsNeedingTitles test, a request-shape/auth test, and a gated live test
config/config.go, internal/infra/storage/interfaces.go, factory.go StorageTypeD1, D1StorageConfig/D1Config, defaults, and the two factory cases
cmd/migrate.go report D1 as auto-migrated (its schema is created on connect) instead of erroring
.infer/config.yaml regenerated d1 block (init --overwrite; agents.yaml diff discarded per CLAUDE.md)

Configuration

storage:
  enabled: true
  type: d1
  d1:
    account_id: "<cloudflare-account-id>"
    database_id: "<d1-database-id>"
    api_token: "<api-token-with-d1-edit>"   # inject via INFER_STORAGE_D1_API_TOKEN
    base_url: "https://api.cloudflare.com/client/v4"  # optional

Env overrides auto-derive: INFER_STORAGE_D1_ACCOUNT_ID, _DATABASE_ID, _API_TOKEN, _BASE_URL.

Design notes

  • Schema parity: the driver runs migrations.GetSQLiteMigrations() over HTTP, so conversations / session_groups stay byte-for-byte compatible with the SQLite backend - either side may initialise the database.
  • Timestamps: stored as UTC RFC3339 for stable lexical ORDER BY updated_at DESC across runners in any timezone and unambiguous external reads.
  • Two mappers preserved: ListConversations stays lean while ListConversationsNeedingTitles carries Model/Tags/TitleGenerated/TitleInvalidated/TitleGenerationTime (the title-generation batch path needs them) - covered by a dedicated test.
  • Secrets: api_token follows the existing plaintext-config + env-override convention (like the Postgres password) and is never logged.

Testing

  • go build ./... ✅
  • go test ./... ✅ (exit 0, 0 failures)
  • task lint ✅ (0 issues)
  • D1 suite: 8 pass, live test skipped (runs against real D1 when INFER_TEST_D1_LIVE + INFER_STORAGE_D1_* are set)

Cross-repo impact

Docs-only. No gateway/SDK/operator changes - the gateway already reads D1 via its native binding; this adds the CLI write path. A [DOCS] follow-up for inference-gateway/docs → cli.md (add Cloudflare D1 to the storage-backends sections + config keys/env vars) should accompany this PR - happy to file it.

Add a `d1` storage backend that persists conversations and the channel
session-group index to Cloudflare D1 over its HTTP query API. D1 is SQLite
under the hood, so the driver mirrors the SQLite backend method-for-method
and reuses the SQLite migrations verbatim; only the transport (HTTP instead
of a local file handle) differs. This lets headless `infer` on ephemeral CI
runners write to an external store that the control plane can read.

- internal/infra/storage/d1.go: D1Storage implementing ConversationStorage
  and SessionGroupStorage over POST /accounts/{a}/d1/database/{d}/query
- config + factory wiring (StorageTypeD1, D1StorageConfig, D1Config, defaults)
- cmd/migrate.go: report D1 as auto-migrated (schema created on connect)
- d1_test.go: httptest mock backed by a real in-process SQLite DB so
  round-trips exercise real SQL; plus a gated live test

Timestamps are stored as UTC RFC3339 for stable ORDER BY across runners and
unambiguous external reads.

Closes #645
@edenreich
edenreich requested a review from a team as a code owner June 23, 2026 18:04
@edenreich

Copy link
Copy Markdown
Contributor Author

@claude Can you create the follow-up for the docs repo?

@inference-gateway-maintainer

inference-gateway-maintainer Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @edenreich's task in 3m 38s —— View job


Docs follow-up filed ✅

Created the [DOCS] follow-up in inference-gateway/docs:

inference-gateway/docs#273 - [DOCS] Add Cloudflare D1 conversation storage backend to cli.md
inference-gateway/docs#273 (labels: documentation, bot)

  • Gather context: review the D1 storage changes (config keys, env vars, changed files)
  • Inspect the inference-gateway/docs repo structure (cli.md storage sections)
  • Check for an existing follow-up issue to avoid duplicates (none found)
  • Create the [DOCS] follow-up issue in inference-gateway/docs
  • Report the created issue link

The issue is scoped against the actual cli.md content and the changes in this PR. It asks the docs to:

  1. Add Cloudflare D1 to both storage-backends lists (the Configuration overview and the Conversation Management section).
  2. Extend the backend-agnostic inspection note (jsonl, sqlite, postgres, redis, memory) to include d1.
  3. Document the storage.d1 config block (account_id, database_id, api_token, base_url).
  4. Document the env overrides: INFER_STORAGE_D1_ACCOUNT_ID, INFER_STORAGE_D1_DATABASE_ID, INFER_STORAGE_D1_API_TOKEN (secret), INFER_STORAGE_D1_BASE_URL.
  5. Note that infer migrate reports D1 as auto-migrated (schema created on connect).

It also captures the design notes worth surfacing in docs (SQLite schema parity, UTC RFC3339 timestamps, api_token secret-injection convention) and cross-links this PR (#646) and the original issue (#645).

Note: I filed this as a tracking issue rather than opening a docs PR, since the request and the PR's "Cross-repo impact" section both describe it as a follow-up to accompany this PR. If you'd prefer I open the actual docs PR against inference-gateway/docs instead, let me know and I can draft the cli.md edits directly.
· Branch feat/storage-d1-driver

@edenreich
edenreich merged commit b6785ae into main Jun 23, 2026
1 check passed
@edenreich
edenreich deleted the feat/storage-d1-driver branch June 23, 2026 18:18
inference-gateway-releaser Bot added a commit that referenced this pull request Jun 24, 2026
## [0.123.0](v0.122.2...v0.123.0) (2026-06-24)

### 🚀 Features

* **storage:** add Cloudflare D1 conversation storage driver ([#646](#646)) ([b6785ae](b6785ae)), closes [#645](#645)
* **claude-code:** surface token usage and cost in headless mode ([#650](#650)) ([4e4f846](4e4f846)), closes [#649](#649)

### 🐛 Bug Fixes

* set git identity for semantic release ([a03ed0b](a03ed0b))

### ♻️ Code Refactoring

* tighten inline diff context to 2 lines ([adcc17b](adcc17b))

### 🔧 Build System

* **deps:** bump the github-actions group with 3 updates ([#648](#648)) ([92784cd](92784cd))
* **deps:** bump the gomod group with 3 updates ([#647](#647)) ([a319eeb](a319eeb))

### 🧹 Maintenance

* **deps:** bump infer CLI v0.121.1 -> v0.122.2, infer-action v0.15.1 -> v0.15.4 ([#644](#644)) ([6befa6f](6befa6f))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.123.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(storage): add Cloudflare D1 conversation storage driver

1 participant