π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
Legacy Slices (actions flattened into state)
Most stores still compose action classes into the Zustand state with flattenActions. Follow this layout only when editing one of them. New stores, and stores moving their data onto local-replica, use the state-only layout in SKILL.md. Reference store: src/store/eval (benchmark slice).
Layout
src/store/<domain>/
βββ initialState.ts # intersects every slice state, spreads every slice initial value
βββ store.ts # XStore = XState & XAction; createStore + flattenActions
βββ selectors.ts # re-exports slice selectors
βββ slices/<slice>/
βββ action.ts # XActionImpl class + create*Slice helper
βββ initialState.ts
βββ reducer.ts # optional: detail-map reducer
βββ selectors.ts # xxxSelectors aggregate
Large slices split actions into an actions/ directory (see src/store/chat/slices/agentRun/actions/).
Action Classes
type Setter = StoreSetter<EvalStore>;
export const createBenchmarkSlice = (set: Setter, get: () => EvalStore, _api?: unknown) =>
new BenchmarkActionImpl(set, get, _api);
export class BenchmarkActionImpl {
readonly #get: () => EvalStore;
readonly #set: Setter;
constructor(set: Setter, get: () => EvalStore, _api?: unknown) {
void _api;
this.#set = set;
this.#get = get;
}
refreshBenchmarks = async (): Promise<void> => {
await mutate(evalKeys.benchmarks());
};
}
export type BenchmarkAction = Pick<BenchmarkActionImpl, keyof BenchmarkActionImpl>;
- Keep the
(set, get, api)constructor shape. Drop#set(andsetNamespace) when the class never writes state; mark the param_setwithvoid _set. - Calls into another class go through
this.#get().otherAction(). When the callee is not on the store type the class sees, widengetwith a local augmentation type, e.g.ChatGroupStoreWithInternalinsrc/store/agentGroup/slices/curd.ts.
Composition
const createStore: StateCreator<EvalStore, [['zustand/devtools', never]]> = (...parameters) => ({
...initialState,
...flattenActions<EvalStoreAction>([
createBenchmarkSlice(...parameters),
createDatasetSlice(...parameters),
new EvalStoreResetAction(...parameters),
]),
});
- Merge class instances with
flattenActions; spreading an instance drops prototype methods. On a key collision the first instance wins. - A slice made of several classes composes them the same way in its own entry and types the result as
PublicActions<A & B & β¦>(src/store/agentGroup/action.ts) so#privatefields stay out of the store type. - Do not keep an old
StateCreatorobject slice and its class replacement active at the same time.
Action Types
| Kind | Naming | Role |
|---|---|---|
| Public | verb (updateBenchmark, useFetchX) |
what components call; orchestration |
| Internal | internal_ prefix |
shared logic other classes call through get(); not called from UI |
| Dispatch | internal_dispatch<Entity> |
runs a reducer on a map and sets the result only when it changed |
The internal_* names are a convention only: they are still public members of the store.
Reads (SWR β store)
useFetchBenchmarkDetail = (id?: string): SWRResponse =>
useClientDataSWR(
id ? evalKeys.benchmarkDetail(id) : null,
() => agentEvalService.getBenchmark(id!),
{
onSuccess: (data) => {
this.#get().internal_dispatchBenchmarkDetail({
id: id!,
type: 'setBenchmarkDetail',
value: data,
});
},
},
);
Return the SWR response so components can render error. Invalidate with refreshX = () => mutate(key). Request lifecycle and error rendering belong to the data-fetching-architecture skill.
Writes
- Reducer vs
set: run maps and lists through a reducer (reducer.ts, Immerproduce, discriminated-union payloads; see reducer). Toggle booleans and set scalar fields with a plain labelledset. - Optimistic create/update: dispatch the change, call the service, then refresh the affected keys. Track per-row pending ids (
loadingXxxDetailIds) and clear them infinally. A failure must refresh or restore β never leave a successful-looking edit. - Delete: call the service first and remove the row after it succeeds. Hand-written reducers have no rollback base, so an optimistic delete cannot be undone reliably. (Replica stores do delete optimistically; the engine rolls back.)
updateBenchmark = async (params: UpdateParams): Promise<void> => {
this.#get().internal_dispatchBenchmarkDetail({
id: params.id,
type: 'updateBenchmarkDetail',
value: params,
});
this.#get().internal_updateBenchmarkDetailLoading(params.id, true);
try {
await agentEvalService.updateBenchmark(params);
await this.#get().refreshBenchmarks();
await this.#get().refreshBenchmarkDetail(params.id);
} finally {
this.#get().internal_updateBenchmarkDetailLoading(params.id, false);
}
};
State Naming
- Detail maps:
xxxDetailMap; query-keyed maps:xxxMap/xxxMaps - Per-row pending:
loadingXxxIds,xxxEditingIds - Active selection:
activeXxxId - First successful load:
xxxInit