Skip to content

feat(storage): add Cloudflare D1 conversation storage driver #645

Description

Summary

Add a d1 conversation storage driver to infer so it can persist conversation history and the channel session-group index to a Cloudflare D1 database over D1's HTTP query API.

Why

infer is driven headless on ephemeral GitHub Actions runners (control plane dispatches it). The existing backends do not fit that model:

  • sqlite / jsonl / memory live on the runner's ephemeral disk - wiped on every recycle, and unreachable by an external reader.
  • postgres / redis need a wire protocol Cloudflare D1 does not speak.

The infer can only read from D1 (native binding). So infer needs to write to D1 over its HTTP query API (POST /accounts/{account_id}/d1/database/{database_id}/query), which none of the current backends do. Because D1 is SQLite under the hood, the SQL is identical to the existing sqlite.go; only the transport (HTTP instead of a local file handle) differs.

Scope (in inference-gateway/cli)

  • internal/infra/storage/d1.go - new D1Storage implementing ConversationStorage + SessionGroupStorage over the D1 HTTP query API.
  • config/config.go - register StorageTypeD1 ("d1") and add D1StorageConfig + the StorageConfig.D1 field.
  • internal/infra/storage/interfaces.go - add D1Config and the StorageConfig.D1 field.
  • internal/infra/storage/factory.go - add the d1 case to NewStorageFromConfig and NewStorage.
  • internal/infra/storage/d1_test.go - tests against an httptest mock of the D1 /query envelope, plus an optional gated live test.

The full driver source and the exact config.go / interfaces.go / factory.go edits are in the reference doc linked above.

Config

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

Schema compatibility

D1 is SQLite, so the schema is infer's existing SQLite schema verbatim and the driver creates tables idempotently (CREATE TABLE IF NOT EXISTS). It MUST stay byte-for-byte compatible with internal/infra/storage/migrations/sqlite_migrations.go (conversations, session_groups) so either side may initialise the database.

Tests

Mirror sqlite_test.go, but stand up an httptest.Server that speaks the D1 /query envelope ({"success":true,"result":[{"results":[...],"meta":{"changes":N}}]}) and point D1Config.BaseURL at it. Assert:

  • SaveConversation then LoadConversation round-trips entries and metadata.
  • ListConversations orders by updated_at DESC and honours limit/offset.
  • DeleteConversation / UpdateConversationMetadata return "not found" when no rows change.
  • PutSessionGroup / GetSessionGroup / ListSessionGroups round-trip the index.
  • ListConversationsNeedingTitles carries Model / Tags / TitleGenerated / TitleInvalidated / TitleGenerationTime (mirror sqlite.go exactly - do not route those rows through the lean summary mapper, or the title-generation batch path loses those fields).
  • Optional: an end-to-end test against a real D1, gated behind an env var.

Run go build ./... and go test ./internal/infra/storage/... in the cli module.

Acceptance criteria

  • storage.type: d1 writes conversations and session groups to D1 over the HTTP API.
  • A later infer agent run reads back the same rows; an external D1 reader sees them too.
  • No data loss across a container / runner recycle (D1 is external to the runner).
  • Tests pass against the httptest mock.

Activity

  1. added
    enhancement
    on Jun 23, 2026
  2. moved this to Todo in 2026on Jun 23, 2026
  3. moved this from Todo to Done in 2026on Jun 24, 2026
  4. inference-gateway-releaser commented on Jun 24, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.123.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions