Rabby create store

From RabbyHub/Rabby · View interactive version

Rabby create store is an AI agent skill from the GitHub repository RabbyHub/Rabby. Create or migrate Rabby UI state stores with Zustand createRabbyStore, including wallet-backed background persistence, partial synchronization, revision handling, Zod-validated service schemas, hydration, rollback, and tests. Use when adding a persisted Rabby UI store, migrating a Rematch model to Z

How to use this skill

Skill file

Create Rabby Store

Create persisted UI stores with the background service as the source of truth. Preserve existing service APIs and migrate only the requested domain.

Inspect the Existing Domain

  1. Read these infrastructure files before changing code:
  1. Read src/ui/state/swap.ts and src/background/service/swap.ts as the reference implementation.
  2. Locate the current Rematch model, all UI call sites, initialization order, background service fields, and legacy controller methods for the requested domain.
  3. Check the worktree and preserve unrelated user changes.
  4. Use rg to find consumers before renaming, deleting, or changing a public API.

Decide State Ownership

Implement the Background Source of Truth

Define the persisted shape once with Zod in the domain service:

const featureStoreSchema = z.object({
  enabled: z.boolean().default(false),
  selectedItem: itemSchema.optional(),
});

export type FeatureServiceStore = z.output<typeof featureStoreSchema>;

const createFeatureStoreTemplate = (): FeatureServiceStore =>
  featureStoreSchema.parse({});

Then initialize and patch the service through the shared persistence helpers:

this.store = await createPersistStore({
  name: 'feature',
  template: createFeatureStoreTemplate(),
  schema: featureStoreSchema,
});

patchStore(partials: Partial<FeatureServiceStore>) {
  patchPersistStore(this.store, partials);
}

Follow these rules:

Register the Generic Background Bridge

  1. Add the service store type to PersistedStoreMap in src/types/persistedStore.ts.
  2. Add the new key to the routing in src/background/controller/wallet.ts for snapshot reads and partial writes.
  3. Delegate writes to the domain service's patchStore(partials) method. Do not assign an incoming full UI state directly to the service store.
  4. Return the service snapshot with the current background origin and revision so the UI can reject stale updates without confusing a Service Worker restart for an old event.
  5. Keep the generic API shaped like storage: getStorageSnapshot(key) and setStorageItem(key, partials).

Implement the UI Store

Build the domain store with createRabbyStore and createExtensionStoreOptions:

type FeatureState = FeatureServiceStore & {
  transientResult: Result | null;
};

type FeatureActions = {
  setEnabled: (enabled: boolean) => void;
};

export type FeatureStore = FeatureState & FeatureActions;

export const useFeatureStore = createRabbyStore<FeatureStore>(
  (set) => ({
    enabled: false,
    selectedItem: undefined,
    transientResult: null,
    setEnabled(enabled) {
      set({ enabled });
    },
  }),
  createExtensionStoreOptions<FeatureStore, 'feature'>({
    storageKey: 'feature',
    autoHydrate: false,
    partialize(state) {
      const persistedState: Partial<FeatureStore> = {};
      Object.entries(state).forEach(([key, value]) => {
        if (key !== 'transientResult' && typeof value !== 'function') {
          (persistedState as Record<string, unknown>)[key] = value;
        }
      });
      return persistedState;
    },
    onError(error) {
      console.error('[featureStore]', error);
    },
  })
);

Preserve Synchronization Invariants

Test the Migration

Add or update focused tests alongside the existing store tests. Cover the behaviors relevant to the domain:

Run the targeted tests first, then run yarn check. If the user asks for a commit, also load and follow skills/rabby-yarn-v4-commit-check/SKILL.md before committing.

Guardrails

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.