@collective-intelligence/extensions-sdk-frontend-react 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +29 -10
- package/README.md +20 -21
- package/dist/effect/effect-runtime/query-cache.d.ts +3 -146
- package/dist/effect.d.ts +1 -1
- package/dist/effect.js +1 -1
- package/dist/record-layer/collection-ids.d.ts +8 -0
- package/dist/record-layer/collections/id-reads.d.ts +26 -0
- package/dist/record-layer/collections/query.d.ts +55 -0
- package/dist/record-layer/collections/realtime.d.ts +124 -0
- package/dist/record-layer/errors.d.ts +64 -0
- package/dist/record-layer/identity.d.ts +57 -0
- package/dist/record-layer/index.d.ts +23 -0
- package/dist/record-layer/layer.d.ts +11 -0
- package/dist/record-layer/machines.d.ts +45 -0
- package/dist/record-layer/outbox.d.ts +141 -0
- package/dist/record-layer/outbox.test-support.d.ts +182 -0
- package/dist/record-layer/ports.d.ts +141 -0
- package/dist/record-layer/position.d.ts +12 -0
- package/dist/record-layer/seed/client.d.ts +38 -0
- package/dist/record-layer/seed/freshness.d.ts +19 -0
- package/dist/record-layer/seed/server.d.ts +77 -0
- package/dist/record-layer/server.d.ts +6 -0
- package/dist/record-layer/service.d.ts +48 -0
- package/dist/record-layer/targets.d.ts +50 -0
- package/dist/record-layer/write-lock.d.ts +23 -0
- package/dist/record-layer/writes.d.ts +85 -0
- package/dist/records/adapters.d.ts +37 -0
- package/dist/records/runtime-provider.d.ts +22 -0
- package/dist/records.d.ts +12 -0
- package/dist/records.js +2 -0
- package/dist/records.server.d.ts +4 -0
- package/dist/records.server.js +1 -0
- package/dist/studio-api/@tanstack/react-query.gen.d.ts +14 -14
- package/dist/workflow.js +2 -1
- package/dist/workflows/default-template-ui/inputs/default-input-details.d.ts +9 -0
- package/dist/workflows/default-template-ui/inputs/translations.d.ts +5 -0
- package/dist/workflows/field-editor/authored-value.d.ts +4 -0
- package/dist/workflows/workflow-runtime/read-only-saved-value-field.d.ts +10 -0
- package/dist/workflows/workflow-runtime/translations.d.ts +1 -0
- package/docs/effect.md +8 -90
- package/docs/guide/ai/index.md +1 -1
- package/docs/guide/develop/sdks.md +10 -9
- package/docs/record-collections.md +147 -0
- package/docs/tanstack-skills.md +74 -0
- package/package.json +29 -7
- package/skills/extensions-sdk-frontend-react-move-to-record-collections/SKILL.md +54 -0
- package/skills/extensions-sdk-frontend-react-record-collections/SKILL.md +48 -0
- package/skills/extensions-sdk-frontend-react-tanstack-db-ssr/SKILL.md +34 -0
package/docs/effect.md
CHANGED
|
@@ -103,96 +103,14 @@ export const RowsWidget = (props: Readonly<{ name: string }>) => (
|
|
|
103
103
|
|
|
104
104
|
### Optimistic write
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
edit until a fetch gives it one.
|
|
115
|
-
- **Order.** The writes to one key go to the Studio API one at a time, in
|
|
116
|
-
call order. When the Studio API gets a write, the running fetch of the key
|
|
117
|
-
is cancelled.
|
|
118
|
-
- **Fetches.** A fetch that ends while writes are pending, for example after
|
|
119
|
-
another widget refreshes the key or a window focus, updates the confirmed
|
|
120
|
-
value, and the layers show over it again. Thus an edit must give the same
|
|
121
|
-
value for a value that already holds it: set fields, or replace or remove a
|
|
122
|
-
row by its id. To add a row, skip the add when the row is there.
|
|
123
|
-
- **Send-time payload.** Give `write` as a function to build the payload when
|
|
124
|
-
the Studio API gets the write. It gets `{ confirmed, optimistic }`: the
|
|
125
|
-
confirmed value with the answers of the writes before it, and that value
|
|
126
|
-
with this edit only. A refused write before it is not in either value.
|
|
127
|
-
- **Answer.** When the Studio API confirms the write, its layer goes, and the
|
|
128
|
-
confirmed value becomes `options.confirm(result, confirmed)`, for example
|
|
129
|
-
the stored row with its new revision. Without `confirm`, or when it gives
|
|
130
|
-
`undefined`, the confirmed value gets the edit. When `write` fails, is
|
|
131
|
-
interrupted or dies, only its own layer goes; the other layers show over
|
|
132
|
-
the confirmed value. When `confirm` throws, the write dies with its error,
|
|
133
|
-
the confirmed value stays, and its layer still goes. When the write ends,
|
|
134
|
-
the key is invalidated, so views read the stored value.
|
|
135
|
-
- **Other members.** A value that `setData`, `update` or `remove` puts while
|
|
136
|
-
writes are pending stays. The edits before it no longer show or roll back,
|
|
137
|
-
and their `confirm` does not run, so an older answer never replaces the
|
|
138
|
-
newer value.
|
|
139
|
-
- **Runs.** Each run of the effect that `optimisticUpdate` returns is one
|
|
140
|
-
write with its own layer, so an actor can run one save effect twice.
|
|
141
|
-
- **Pending.** `pending(queryKey)` gives the number of optimistic writes of
|
|
142
|
-
the key that wait for the Studio API or run, for example to show
|
|
143
|
-
"Saving…".
|
|
144
|
-
|
|
145
|
-
The writes of one key share their layers across every `QueryCache` over the
|
|
146
|
-
same `QueryClient`. The `QueryClient` holds them, so the writes of two
|
|
147
|
-
widgets to one key also show together and go one at a time, though each
|
|
148
|
-
widget bundles its own copy of `/effect`. Copies of releases with another
|
|
149
|
-
layout of these writes keep their own. The key is the entry that `getQueryData`
|
|
150
|
-
finds, also with a `queryKeyHashFn` of the Host's `QueryClient`. A write that
|
|
151
|
-
changes several keys does the steps itself: `cancel` the keys, `update` them,
|
|
152
|
-
run the write, and `invalidate` the keys when the actor ends.
|
|
153
|
-
|
|
154
|
-
```ts
|
|
155
|
-
type Row = Readonly<{ id: string; name: string; revision: string }>
|
|
156
|
-
type RenameInput = Readonly<{
|
|
157
|
-
id: string
|
|
158
|
-
name: string
|
|
159
|
-
runtime: EffectRunner<QueryCache>
|
|
160
|
-
}>
|
|
161
|
-
|
|
162
|
-
const rename = fromEffect((input: RenameInput) =>
|
|
163
|
-
QueryCache.use((cache) =>
|
|
164
|
-
cache.optimisticUpdate(
|
|
165
|
-
rowsKey,
|
|
166
|
-
// Replaces the row by its id, so a fetch under the layer keeps it.
|
|
167
|
-
(rows: ReadonlyArray<Row>) =>
|
|
168
|
-
rows.map((row) =>
|
|
169
|
-
row.id === input.id ? { ...row, name: input.name } : row
|
|
170
|
-
),
|
|
171
|
-
// Built when the Studio API gets the save: the revision of the save
|
|
172
|
-
// before it, and no change of a refused save.
|
|
173
|
-
({ optimistic }) => {
|
|
174
|
-
const row = optimistic?.find(({ id }) => id === input.id)
|
|
175
|
-
return row === undefined
|
|
176
|
-
? Effect.die("no row")
|
|
177
|
-
: writeName(input.id, { name: row.name, revision: row.revision })
|
|
178
|
-
},
|
|
179
|
-
// `writeName` answers the stored row with its new revision.
|
|
180
|
-
{
|
|
181
|
-
confirm: (stored: Row, confirmed) =>
|
|
182
|
-
confirmed?.map((row) => (row.id === stored.id ? stored : row))
|
|
183
|
-
}
|
|
184
|
-
)
|
|
185
|
-
)
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
const savingRows = fromEffect(
|
|
189
|
-
(input: Readonly<{ runtime: EffectRunner<QueryCache> }>) =>
|
|
190
|
-
QueryCache.use((cache) => cache.pending(rowsKey))
|
|
191
|
-
)
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
A `write` given as an Effect works as before: it is the same for each
|
|
195
|
-
confirmed value.
|
|
106
|
+
`QueryCache` has no optimistic write. An optimistic write of a record is a
|
|
107
|
+
Record Collection transaction: update a view data item with `RecordWrites`
|
|
108
|
+
and `studioViewWriteTarget` of `/records`; for an optimistic create or
|
|
109
|
+
delete, file an SDK gap ([record-collections.md](record-collections.md)).
|
|
110
|
+
A write that changes keys
|
|
111
|
+
of `QueryCache` does the steps itself:
|
|
112
|
+
`cancel` the keys, `update` them, run the write, and `invalidate` the keys
|
|
113
|
+
when the actor ends.
|
|
196
114
|
|
|
197
115
|
## Rules of the write parts
|
|
198
116
|
|
package/docs/guide/ai/index.md
CHANGED
|
@@ -226,7 +226,7 @@ Your server invokes an Agent with `agents.invoke({ agentId, input })`, which ret
|
|
|
226
226
|
|
|
227
227
|
For a multi-turn chat, use the shared chat. Start a Conversation with the Agent with `ci.chat.createConversation({ organizationId, payload: { agentId } })`, send each message with `createChatClient(ci, { organizationId }).send({ conversationId, message })` from `@collective-intelligence/external-apps-server-sdk/chat`, and read earlier turns with `ci.chat.getTranscript`. Each turn is an ordinary Agent run, and the earlier successful turns, with their saved Agent Output, become the model's conversation history. A message sent while a turn still runs steers that turn and starts no second turn, and a resend with the same message `id` adds nothing.
|
|
228
228
|
|
|
229
|
-
`send` resolves `{ ok: true, value, runId }`: `runId` is the id of the turn's run, which the answer message also carries as `metadata.invocationId`, and `turnRunId(message)` reads it from any answer message. `runs.get`, `runs.sources` and `runs.feedback` accept that id like the `runId` of `agents.invoke`. They answer only the caller that owns the Conversation (the member, the app server or the visitor) through the application where the Conversation started, and answer `not_found` to every other caller. As for any run, only a signed-in member reads `runs.sources` and gives `runs.feedback`. `runs.get` gives the status and `result: { message, output? }`, where `output` is the Agent Output that the turn saved. `runs.feedback` also rates a succeeded turn whose answer is only its Agent Output, with the Output's JSON text as the rated answer; a turn with neither text nor Output has no answer to rate and gets `not_found`. The member can also rate and clear the answer with `ci.chat.putAnswerFeedback` and `ci.chat.clearAnswerFeedback` (`params: { invocationId: runId }`); it is the same rating. Each completed Agent turn with an answer to rate (its text or its Agent Output) of `ci.chat.getTranscript` gives that member `feedbackTargetId` (the turn's run id) and `currentUserFeedback`, so the app shows a saved rating again after a reload. `runs.sources` gives the searched Document Versions with their pages and a download link. `runs.stream`
|
|
229
|
+
`send` resolves `{ ok: true, value, runId }`: `runId` is the id of the turn's run, which the answer message also carries as `metadata.invocationId`, and `turnRunId(message)` reads it from any answer message. `runs.get`, `runs.stream`, `runs.cancel`, `runs.sources` and `runs.feedback` accept that id like the `runId` of `agents.invoke`. They answer only the caller that owns the Conversation (the member, the app server or the visitor) through the application where the Conversation started, and answer `not_found` to every other caller. As for any run, only a signed-in member reads `runs.sources` and gives `runs.feedback`. `runs.get` gives the status and `result: { message, output? }`, where `output` is the Agent Output that the turn saved. `runs.feedback` also rates a succeeded turn whose answer is only its Agent Output, with the Output's JSON text as the rated answer; a turn with neither text nor Output has no answer to rate and gets `not_found`. The member can also rate and clear the answer with `ci.chat.putAnswerFeedback` and `ci.chat.clearAnswerFeedback` (`params: { invocationId: runId }`); it is the same rating. Each completed Agent turn with an answer to rate (its text or its Agent Output) of `ci.chat.getTranscript` gives that member `feedbackTargetId` (the turn's run id) and `currentUserFeedback`, so the app shows a saved rating again after a reload. `runs.sources` gives the searched Document Versions with their pages and a download link. `runs.stream` pages the turn's events as for `agents.invoke`: `accepted`, the `agent.*` events and the application events that its tools publish, then one terminal event with the `runs.get` result. `runs.cancel` stops the turn. The answer itself streams from `GET /runs/:runId/stream` of the chat mount.
|
|
230
230
|
|
|
231
231
|
Anonymous visitors can chat with an Agent only when its audience has `anonymous: [app]` for that application and the call passes `caller: { kind: "anonymous_visitor", visitor: { issuer, subjectId } }`. Their turns run with the Agent's own bindings, never with the application's grants, and visitors never see each other's conversations.
|
|
232
232
|
|
|
@@ -197,15 +197,16 @@ At the time of writing, the template catalog pins these versions:
|
|
|
197
197
|
> **Note:** The release workflow bumps the template pins after each npm release. Copy the
|
|
198
198
|
> catalog from the current template instead of this table.
|
|
199
199
|
|
|
200
|
-
Some names from older examples no longer exist. An old server SDK subpath fails with `ERR_PACKAGE_PATH_NOT_EXPORTED
|
|
201
|
-
|
|
202
|
-
| Old import | Use instead
|
|
203
|
-
| -------------------------------------------------------- |
|
|
204
|
-
| `@collective-intelligence/extensions-sdk-backend-effect` | `@collective-intelligence/extensions-sdk-backend/effect`, same export names
|
|
205
|
-
| `@collective-intelligence/extensions-sdk-backend-zod` | Effect Schema through `effectSchema` from `extensions-sdk-backend/effect`
|
|
206
|
-
| `external-apps-server-sdk/runtime/effect` | `external-apps-server-sdk/effect`
|
|
207
|
-
| `external-apps-server-sdk/persistence/drizzle/effect` | `external-apps-server-sdk/effect` plus `/effect/node-postgres`
|
|
208
|
-
| `external-apps-server-sdk/runtime/next` | `external-apps-server-sdk/next`
|
|
200
|
+
Some names from older examples no longer exist. An old server SDK subpath fails with `ERR_PACKAGE_PATH_NOT_EXPORTED`. The lint rule `effect-subpath` reports the old Effect paths, and `package-entrypoint` reports any subpath that the package does not export.
|
|
201
|
+
|
|
202
|
+
| Old import | Use instead |
|
|
203
|
+
| -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
204
|
+
| `@collective-intelligence/extensions-sdk-backend-effect` | `@collective-intelligence/extensions-sdk-backend/effect`, same export names |
|
|
205
|
+
| `@collective-intelligence/extensions-sdk-backend-zod` | Effect Schema through `effectSchema` from `extensions-sdk-backend/effect` |
|
|
206
|
+
| `external-apps-server-sdk/runtime/effect` | `external-apps-server-sdk/effect` |
|
|
207
|
+
| `external-apps-server-sdk/persistence/drizzle/effect` | `external-apps-server-sdk/effect` plus `/effect/node-postgres` |
|
|
208
|
+
| `external-apps-server-sdk/runtime/next` | `external-apps-server-sdk/next` |
|
|
209
|
+
| `external-apps-server-sdk/knowledge-sources/payload` | `ciPayloadPlugin` from `external-apps-server-sdk/payload`, which serves the site's collections as a Gateway |
|
|
209
210
|
|
|
210
211
|
The two removed packages stopped at version 0.2.0, and npm marks them deprecated.
|
|
211
212
|
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Record Collections
|
|
2
|
+
|
|
3
|
+
`./records` holds the record layer: Record Collections, `RecordWrites` and the
|
|
4
|
+
bridge to Business Logic Machines. A Record Collection is a TanStack DB
|
|
5
|
+
collection of CI records of one query. In a Studio Widget, the runtime of
|
|
6
|
+
`RecordRuntimeProvider` (`./records`) provides `QueryCache`,
|
|
7
|
+
`RecordCollections` and `RecordWrites`, over the Studio Host's `QueryClient`
|
|
8
|
+
and the Studio API client that the Host binds. A widget that uses Record
|
|
9
|
+
Collections mounts it at its root in place of `EffectRuntimeProvider` of
|
|
10
|
+
`/effect`, which provides only `QueryCache` and loads without TanStack DB.
|
|
11
|
+
|
|
12
|
+
Install the TanStack peers with the exact versions in the package's
|
|
13
|
+
`peerDependencies`: `@tanstack/db` and `@tanstack/react-db` `1.0.0-rc.1.5`,
|
|
14
|
+
`@tanstack/query-db-collection` `1.5.2`, `@tanstack/offline-transactions`
|
|
15
|
+
`1.2.1`, and `@tanstack/query-core`. Load the TanStack DB skills that
|
|
16
|
+
[tanstack-skills.md](tanstack-skills.md) names before you write a live query
|
|
17
|
+
or a collection.
|
|
18
|
+
|
|
19
|
+
## Which kind to use
|
|
20
|
+
|
|
21
|
+
| Data | Use |
|
|
22
|
+
| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
|
|
23
|
+
| Records of one Studio API query that the widget shows as a list or follows across views | Query Record Collection |
|
|
24
|
+
| A one-off read that is not a set of records you show or follow: a count, a search, a report | Plain TanStack Query (`useQuery`, `QueryCache.fetch`) |
|
|
25
|
+
| Records that other users change while the widget is open | A Query Record Collection, read again on a user action; Studio has no realtime source |
|
|
26
|
+
|
|
27
|
+
- **Query Record Collection**: `RecordCollections.query(options)`, with
|
|
28
|
+
`options` `{ queryKey, schema, load, target? }`. `load` is an Effect that returns every record of the query as
|
|
29
|
+
`{ id, revision, value }`. Its rows stay on the Studio Host's `QueryClient`,
|
|
30
|
+
which the Host and all widgets share, so start each `queryKey` with the
|
|
31
|
+
package name. Pass `target: studioViewWriteTarget(view)` when the query
|
|
32
|
+
holds a view's data items: an update of an item then shows in every
|
|
33
|
+
collection of that view that holds it.
|
|
34
|
+
- **Realtime Record Collection**: a realtime collection needs a source of
|
|
35
|
+
Change Hints, and the Studio Host gives none. `RecordCollections.realtime`
|
|
36
|
+
fails its sync in Studio with `RecordSyncFailed` (`retrying: false`).
|
|
37
|
+
- `schema` is an Effect `Schema` of the record value. A record that does not
|
|
38
|
+
decode fails the collection's sync with `RecordSyncFailed`.
|
|
39
|
+
- Provide the services that `load` needs where you call `query`, for example
|
|
40
|
+
`Effect.provide(layerStudioApi())`: the widget runtime provides only
|
|
41
|
+
`QueryCache`, `RecordCollections` and `RecordWrites`.
|
|
42
|
+
|
|
43
|
+
## Business Logic Machines
|
|
44
|
+
|
|
45
|
+
A Business Logic Machine reads records through a live query and keeps ids or
|
|
46
|
+
counts in its context, never rows. Pass `useRecordRuntime()` as `runtime` in
|
|
47
|
+
the machine input.
|
|
48
|
+
|
|
49
|
+
- `fromLiveQuery((input) => effect)` follows a Record Collection or a live
|
|
50
|
+
query collection over Record Collections. It sends the parent
|
|
51
|
+
`{ type: "records.rows", rows }` when the rows are ready and after each
|
|
52
|
+
change, and `{ type: "records.failed", error }` when the query or its first
|
|
53
|
+
load fails. A stop ends the subscription.
|
|
54
|
+
- Each row is `{ id, revision, value }`.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { layerStudioApi } from "@collective-intelligence/extensions-sdk-frontend-react/effect"
|
|
58
|
+
import {
|
|
59
|
+
fromLiveQuery,
|
|
60
|
+
RecordCollections,
|
|
61
|
+
useRecordRuntime
|
|
62
|
+
} from "@collective-intelligence/extensions-sdk-frontend-react/records"
|
|
63
|
+
import * as Effect from "effect/Effect"
|
|
64
|
+
|
|
65
|
+
const watchItems = fromLiveQuery(
|
|
66
|
+
(input: Readonly<{ runtime: ReturnType<typeof useRecordRuntime> }>) =>
|
|
67
|
+
RecordCollections.use((collections) =>
|
|
68
|
+
collections.query({
|
|
69
|
+
load: readViewItems, // an Effect on StudioApi that returns the records
|
|
70
|
+
queryKey: ["com.acme.crm", "view-items"],
|
|
71
|
+
schema: Item
|
|
72
|
+
})
|
|
73
|
+
).pipe(Effect.provide(layerStudioApi()))
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Writes
|
|
78
|
+
|
|
79
|
+
An update of a view data item is a Record Collection transaction:
|
|
80
|
+
`RecordWrites.update` (or `fromTransaction`) with
|
|
81
|
+
`studioViewWriteTarget({ organizationId, slug, viewId })`, `RecordId` and
|
|
82
|
+
`IdempotencyKey` of `./records`, on the widget's own Query Record
|
|
83
|
+
Collection. The item shows the change at once in every collection of the
|
|
84
|
+
view that holds it, CI's answer replaces it before the write settles, and a
|
|
85
|
+
refusal rolls back only that change.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import {
|
|
89
|
+
IdempotencyKey,
|
|
90
|
+
RecordId,
|
|
91
|
+
RecordWrites,
|
|
92
|
+
studioViewWriteTarget
|
|
93
|
+
} from "@collective-intelligence/extensions-sdk-frontend-react/records"
|
|
94
|
+
|
|
95
|
+
const markLost = (items: QueryRecordCollection<RecordRow<Item>>, id: string) =>
|
|
96
|
+
RecordWrites.use((writes) =>
|
|
97
|
+
writes.update({
|
|
98
|
+
change: { stage: "lost" },
|
|
99
|
+
collection: items,
|
|
100
|
+
id: RecordId.make(id),
|
|
101
|
+
idempotencyKey: IdempotencyKey.make(crypto.randomUUID()),
|
|
102
|
+
target: studioViewWriteTarget(view)
|
|
103
|
+
})
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The Studio API write path covers updates of view data items only:
|
|
108
|
+
`RecordWrites.create` and `delete` are refused. For those writes:
|
|
109
|
+
|
|
110
|
+
- Run each write in a `fromEffect` actor through `StudioApi`, then refresh
|
|
111
|
+
the reads: `invalidate` the collection's `queryKey` with `QueryCache`.
|
|
112
|
+
- For an optimistic create or delete, file an SDK gap (see
|
|
113
|
+
[File an SDK gap](#file-an-sdk-gap)) and write no optimistic value, record
|
|
114
|
+
cache or write queue in the widget.
|
|
115
|
+
|
|
116
|
+
Writes to one record run one at a time across every widget bundle, through
|
|
117
|
+
one lock that each copy of the record layer shares. Studio starts no outbox:
|
|
118
|
+
a write still pending at a reload is not sent again.
|
|
119
|
+
|
|
120
|
+
## Server rendering
|
|
121
|
+
|
|
122
|
+
The Studio Host renders widgets in the browser. `seedRecords` and
|
|
123
|
+
`getRequestClients` exist in the `react-server` build of `./records` for
|
|
124
|
+
parity with the external-apps SDK; a widget does not use them. See the
|
|
125
|
+
[extensions-sdk-frontend-react-tanstack-db-ssr](../skills/extensions-sdk-frontend-react-tanstack-db-ssr/SKILL.md)
|
|
126
|
+
skill.
|
|
127
|
+
|
|
128
|
+
## Errors
|
|
129
|
+
|
|
130
|
+
| Error | Meaning |
|
|
131
|
+
| --------------------------- | ---------------------------------------------------------------------------- |
|
|
132
|
+
| `RecordSyncFailed` | A collection could not load or apply records; `retrying` tells if it retries |
|
|
133
|
+
| `RecordWriteRefused` | The Studio API refused the write; only that write rolls back |
|
|
134
|
+
| `RecordWriteConflict` | The record changed since the revision the write sent (Studio API 409) |
|
|
135
|
+
| `RecordWriteOutcomeUnknown` | No answer arrived within the 60-second resend window |
|
|
136
|
+
|
|
137
|
+
## File an SDK gap
|
|
138
|
+
|
|
139
|
+
When the record layer cannot express what a widget needs, stop and report it;
|
|
140
|
+
write no local optimistic code, record cache or write queue.
|
|
141
|
+
|
|
142
|
+
1. Write down the flow, the record type, and the `./records` call that is
|
|
143
|
+
missing.
|
|
144
|
+
2. Open an issue for `@collective-intelligence/extensions-sdk-frontend-react`
|
|
145
|
+
with that note, or tell the user that the SDK lacks it.
|
|
146
|
+
3. Until the SDK has it, keep the flow on `fromEffect` writes and
|
|
147
|
+
`QueryCache` refreshes, or leave it unbuilt.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# TanStack skills
|
|
2
|
+
|
|
3
|
+
`@tanstack/db` and `@tanstack/react-db` ship their own agent skills, which
|
|
4
|
+
TanStack Intent loads by name. This SDK names the ones to load and adds the
|
|
5
|
+
rules of its record layer; it copies none of them.
|
|
6
|
+
|
|
7
|
+
## Allowlist the packages
|
|
8
|
+
|
|
9
|
+
Permit both packages in the extension's `package.json`, then run
|
|
10
|
+
`intent install` (`@tanstack/intent` as a devDependency) and commit what it
|
|
11
|
+
writes:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"intent": {
|
|
16
|
+
"skills": ["@tanstack/db", "@tanstack/react-db"],
|
|
17
|
+
"exclude": ["@tanstack/db#meta-framework"]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`intent list` shows the permitted skills; `intent load <package>#<skill>`
|
|
23
|
+
prints one.
|
|
24
|
+
|
|
25
|
+
## Skills to load
|
|
26
|
+
|
|
27
|
+
| Task | Load |
|
|
28
|
+
| ------------------------------------------------------------------------------- | ------------------------------------------- |
|
|
29
|
+
| Any work on Record Collections: concepts, and the entry to the other skills | `@tanstack/db#db-core` |
|
|
30
|
+
| A live query: `from`, `where`, `join`, `select`, `orderBy`, `$hasPendingWrites` | `@tanstack/db#db-core/live-queries` |
|
|
31
|
+
| `useLiveQuery` and the other React hooks | `@tanstack/react-db#react-db` |
|
|
32
|
+
| What a pending write shows, and the transaction states | `@tanstack/db#db-core/mutations-optimistic` |
|
|
33
|
+
| The states of a collection (`loading`, `ready`, `error`) | `@tanstack/db#db-core/collection-setup` |
|
|
34
|
+
|
|
35
|
+
These skills teach TanStack DB itself. Where one of them makes a collection
|
|
36
|
+
(`createCollection`, `queryCollectionOptions`) or writes
|
|
37
|
+
(`collection.insert`, `collection.update`, `collection.delete`,
|
|
38
|
+
`createOptimisticAction`, `createTransaction`), use the SDK instead: open
|
|
39
|
+
collections with `RecordCollections`, and write as
|
|
40
|
+
[record-collections.md](record-collections.md) says.
|
|
41
|
+
|
|
42
|
+
The SDK owns two topics, so skip their skills:
|
|
43
|
+
`@tanstack/db#db-core/custom-adapter` (the SDK adapts the Studio API) and
|
|
44
|
+
`@tanstack/db#db-core/persistence` (a widget keeps no records across
|
|
45
|
+
reloads).
|
|
46
|
+
|
|
47
|
+
## The shipped server-rendering skill is out of date
|
|
48
|
+
|
|
49
|
+
`@tanstack/db#meta-framework` was written for TanStack DB 0.6 and says that
|
|
50
|
+
TanStack DB has no server rendering. TanStack DB `1.0.0-rc.1.5` has it, but a
|
|
51
|
+
Studio Widget renders in the browser, so neither applies to a widget. Run the
|
|
52
|
+
[extensions-sdk-frontend-react-tanstack-db-ssr](../skills/extensions-sdk-frontend-react-tanstack-db-ssr/SKILL.md)
|
|
53
|
+
skill in its place; the allowlist above excludes it.
|
|
54
|
+
|
|
55
|
+
## TanStack Query rules
|
|
56
|
+
|
|
57
|
+
Apply these rules on top of the repository `tanstack-query` skill; where they
|
|
58
|
+
differ, these win.
|
|
59
|
+
|
|
60
|
+
- Write in a `fromEffect` actor of `/effect`, through `StudioApi`, never with
|
|
61
|
+
`useMutation` or its "Optimistic Updates" patterns (`onMutate`,
|
|
62
|
+
`setQueryData` and a rollback). The `ci-solution/effect-writes` lint rule
|
|
63
|
+
reports `useMutation`. After the write, refresh the reads with
|
|
64
|
+
`QueryCache` (`invalidate`, `setData`, `update`, `remove`).
|
|
65
|
+
- An optimistic write of a CI record belongs to the record layer: file an SDK
|
|
66
|
+
gap when `./records` cannot make it, as
|
|
67
|
+
[record-collections.md](record-collections.md) says.
|
|
68
|
+
- Read one-off data with `useQuery`, or with `QueryCache.fetch` in a machine.
|
|
69
|
+
Records that the widget shows as a list or follows go in a Query Record
|
|
70
|
+
Collection.
|
|
71
|
+
- Start each query key with the package name: the Studio Host and all widgets
|
|
72
|
+
share one `QueryClient`.
|
|
73
|
+
- Skip the "SSR & Hydration" steps of the `tanstack-query` skill: the Studio
|
|
74
|
+
Host renders widgets in the browser.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@collective-intelligence/extensions-sdk-frontend-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Public hosted React contribution contracts for Collective Intelligence extensions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -61,6 +61,18 @@
|
|
|
61
61
|
"types": "./dist/effect.d.ts",
|
|
62
62
|
"import": "./dist/effect.js",
|
|
63
63
|
"default": "./dist/effect.js"
|
|
64
|
+
},
|
|
65
|
+
"./records": {
|
|
66
|
+
"react-server": {
|
|
67
|
+
"ci-source": "./src/records.server.ts",
|
|
68
|
+
"types": "./dist/records.server.d.ts",
|
|
69
|
+
"import": "./dist/records.server.js",
|
|
70
|
+
"default": "./dist/records.server.js"
|
|
71
|
+
},
|
|
72
|
+
"ci-source": "./src/records.ts",
|
|
73
|
+
"types": "./dist/records.d.ts",
|
|
74
|
+
"import": "./dist/records.js",
|
|
75
|
+
"default": "./dist/records.js"
|
|
64
76
|
}
|
|
65
77
|
},
|
|
66
78
|
"scripts": {
|
|
@@ -80,12 +92,17 @@
|
|
|
80
92
|
"type-check": "bun check --no-script -p tsconfig.json && bun check --no-script -p tsconfig.type-tests.json"
|
|
81
93
|
},
|
|
82
94
|
"dependencies": {
|
|
83
|
-
"@collective-intelligence/effect-xstate": "0.
|
|
84
|
-
"@collective-intelligence/extensions-sdk": "0.
|
|
85
|
-
"@collective-intelligence/extensions-sdk-frontend": "0.
|
|
95
|
+
"@collective-intelligence/effect-xstate": "0.12.0",
|
|
96
|
+
"@collective-intelligence/extensions-sdk": "0.12.0",
|
|
97
|
+
"@collective-intelligence/extensions-sdk-frontend": "0.12.0"
|
|
86
98
|
},
|
|
87
99
|
"peerDependencies": {
|
|
88
|
-
"@collective-intelligence/ui-react": "0.
|
|
100
|
+
"@collective-intelligence/ui-react": "0.12.0",
|
|
101
|
+
"@tanstack/db": "1.0.0-rc.1.5",
|
|
102
|
+
"@tanstack/offline-transactions": "1.2.1",
|
|
103
|
+
"@tanstack/query-core": ">=5 <6",
|
|
104
|
+
"@tanstack/query-db-collection": "1.5.2",
|
|
105
|
+
"@tanstack/react-db": "1.0.0-rc.1.5",
|
|
89
106
|
"@tanstack/react-query": ">=5 <6",
|
|
90
107
|
"@xyflow/react": ">=12 <13",
|
|
91
108
|
"effect": "^4.1.0",
|
|
@@ -102,12 +119,17 @@
|
|
|
102
119
|
"access": "public"
|
|
103
120
|
},
|
|
104
121
|
"devDependencies": {
|
|
105
|
-
"@collective-intelligence/ui-react": "0.
|
|
122
|
+
"@collective-intelligence/ui-react": "0.12.0",
|
|
106
123
|
"@effect/eslint-plugin": "0.3.2",
|
|
107
124
|
"@effect/vitest": "4.1.4",
|
|
108
125
|
"@hey-api/openapi-ts": "^0.96.0",
|
|
109
126
|
"@oxlint/plugins": "1.94.0",
|
|
110
127
|
"@punks/lints": "0.2.0",
|
|
128
|
+
"@tanstack/db": "1.0.0-rc.1.5",
|
|
129
|
+
"@tanstack/offline-transactions": "1.2.1",
|
|
130
|
+
"@tanstack/query-core": "5.101.4",
|
|
131
|
+
"@tanstack/query-db-collection": "1.5.2",
|
|
132
|
+
"@tanstack/react-db": "1.0.0-rc.1.5",
|
|
111
133
|
"@tanstack/react-query": "5.101.4",
|
|
112
134
|
"@testing-library/react": "^16.3.2",
|
|
113
135
|
"@types/bun": "1.4.2",
|
|
@@ -130,5 +152,5 @@
|
|
|
130
152
|
"vitest": "5.0.2",
|
|
131
153
|
"xstate": "5.33.3"
|
|
132
154
|
},
|
|
133
|
-
"gitHead": "
|
|
155
|
+
"gitHead": "995aa5542f6f440c5cebe836b57bc9bae57ae7e8"
|
|
134
156
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: extensions-sdk-frontend-react-move-to-record-collections
|
|
3
|
+
description: Move the optimistic writes of a Studio Widget on `@collective-intelligence/extensions-sdk-frontend-react` off `QueryCache.optimisticUpdate` and `optimistic-write` modules, toward the record layer of `./records`. Use when an SDK upgrade fails the type-check on `optimisticUpdate`, `pending`, `QueryCacheWriteValues` or `QueryCacheOptimisticOptions`, or code imports an `entities/optimistic-write` module.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Move optimistic writes to the record layer
|
|
7
|
+
|
|
8
|
+
Reference: [record-collections.md](../../docs/record-collections.md),
|
|
9
|
+
[effect.md](../../docs/effect.md).
|
|
10
|
+
|
|
11
|
+
The SDK release with `./records` removed `QueryCache.optimisticUpdate`,
|
|
12
|
+
`QueryCache.pending`, the types `QueryCacheWriteValues` and
|
|
13
|
+
`QueryCacheOptimisticOptions`, and the `optimistic-write` engine, with no
|
|
14
|
+
wrapper. Every optimistic write of a CI record is a Record Collection
|
|
15
|
+
transaction. A widget stays on the old SDK until this skill has moved every
|
|
16
|
+
call site.
|
|
17
|
+
|
|
18
|
+
## Steps
|
|
19
|
+
|
|
20
|
+
1. **Inventory.** Upgrade the SDK and run the type-check. Also search for
|
|
21
|
+
`optimisticUpdate(`, `.pending(`, `QueryCacheWriteValues`,
|
|
22
|
+
`QueryCacheOptimisticOptions`, `optimistic-write`, and `useMutation` with
|
|
23
|
+
`onMutate`. Done when you have a list of call sites, each with what it
|
|
24
|
+
writes.
|
|
25
|
+
2. **Sort each call site**: case **A** writes a CI record or a view data
|
|
26
|
+
item; case **B** edits a cache value that is not a CI record, such as a
|
|
27
|
+
preference or a computed list. Done when each call site has its case.
|
|
28
|
+
3. **Move view data item updates of case A to `RecordWrites`.** Run
|
|
29
|
+
`RecordWrites.update` or `fromTransaction` with
|
|
30
|
+
`studioViewWriteTarget(view)` on the widget's Query Record Collection, and
|
|
31
|
+
delete the old optimistic call. A create or a delete of case A has no
|
|
32
|
+
Studio write path: follow "File an SDK gap" in `record-collections.md`,
|
|
33
|
+
once per record type, and move it with step 4. Done when each update of
|
|
34
|
+
case A runs through `RecordWrites` and each other write of case A has a
|
|
35
|
+
filed gap.
|
|
36
|
+
4. **Move each call site to the `QueryCache` steps.** In its `fromEffect`
|
|
37
|
+
actor: `cancel` the keys, `update` them, run the write through
|
|
38
|
+
`StudioApi`, and `invalidate` the keys when the actor ends. Delete the
|
|
39
|
+
`confirm` option and the `{ confirmed, optimistic }` write function. Done
|
|
40
|
+
when no call site calls `optimisticUpdate` or an `optimistic-write`
|
|
41
|
+
module.
|
|
42
|
+
5. **Move lists to Query Record Collections** where the widget shows or
|
|
43
|
+
follows records, as the `extensions-sdk-frontend-react-record-collections`
|
|
44
|
+
skill says. Done when each such list reads through a live query.
|
|
45
|
+
6. **Replace the pending reads.** `pending(queryKey)` becomes the state of
|
|
46
|
+
the machine that runs the write, or `$hasPendingWrites` in a live query
|
|
47
|
+
over a Record Collection. Done when the search of step 1 finds nothing.
|
|
48
|
+
7. **Fix the tests.** Remove `optimisticUpdate` and `pending` from
|
|
49
|
+
`makeTestQueryCache` overrides and from `calls` assertions, and assert on
|
|
50
|
+
the `cancel`, `update` and `invalidate` calls and entries. Done when the
|
|
51
|
+
tests pass.
|
|
52
|
+
8. **Verify.** Run the type-check, the tests and oxlint with the
|
|
53
|
+
`recommended` config of `@collective-intelligence/lint`. Done when all
|
|
54
|
+
pass with no `ci-solution/effect-writes` finding.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: extensions-sdk-frontend-react-record-collections
|
|
3
|
+
description: Build Studio Widgets and their Business Logic Machines on the Record Collections of `@collective-intelligence/extensions-sdk-frontend-react/records`. Use when a widget shows or follows a list of records from the Studio API, code imports `@tanstack/db` or `@tanstack/react-db`, or lint reports `ci-solution/effect-writes`.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Build widgets on Record Collections
|
|
7
|
+
|
|
8
|
+
Reference: [record-collections.md](../../docs/record-collections.md),
|
|
9
|
+
[tanstack-skills.md](../../docs/tanstack-skills.md),
|
|
10
|
+
[effect.md](../../docs/effect.md).
|
|
11
|
+
|
|
12
|
+
## Steps
|
|
13
|
+
|
|
14
|
+
1. **Load the TanStack DB skills.** Check that the extension's
|
|
15
|
+
`package.json` permits `@tanstack/db` and `@tanstack/react-db` in
|
|
16
|
+
`intent.skills`, then load each skill that `tanstack-skills.md` names for
|
|
17
|
+
the task. Done when `intent list` shows them and you have read
|
|
18
|
+
`@tanstack/db#db-core`.
|
|
19
|
+
2. **Choose the kind** for each data set with the table "Which kind to use"
|
|
20
|
+
in `record-collections.md`: a Query Record Collection for lists the widget
|
|
21
|
+
shows or follows, plain TanStack Query for one-off reads. Studio has no
|
|
22
|
+
Realtime Record Collection. Done when each data set has one kind.
|
|
23
|
+
3. **Mount the runtime.** Put `RecordRuntimeProvider` of `/records` at the
|
|
24
|
+
root of each widget, in place of `EffectRuntimeProvider` of `/effect`, and
|
|
25
|
+
pass `useRecordRuntime()` to each machine in its input. Its runtime also
|
|
26
|
+
has `QueryCache`. Done when every machine that reads records gets that
|
|
27
|
+
runtime.
|
|
28
|
+
4. **Open each collection** with `RecordCollections.query` (`queryKey`,
|
|
29
|
+
`schema`, `load`) in a `fromLiveQuery` actor, with the services of `load`
|
|
30
|
+
provided at the call. Start each `queryKey` with the package name, and keep the
|
|
31
|
+
keys and schemas in one module. Done when no module calls
|
|
32
|
+
`createCollection` or `queryCollectionOptions` itself.
|
|
33
|
+
5. **Keep rows out of state.** The machine takes `records.rows` events and
|
|
34
|
+
keeps ids or counts; views read rows with `useLiveQuery` or from the
|
|
35
|
+
machine's events. Done when no React state or machine context holds a
|
|
36
|
+
copy of a row.
|
|
37
|
+
6. **Write through the record layer.** Update a view data item with
|
|
38
|
+
`RecordWrites.update` or `fromTransaction` and
|
|
39
|
+
`studioViewWriteTarget(view)` on the widget's Query Record Collection.
|
|
40
|
+
Run a create or a delete in a `fromEffect` actor on `StudioApi`, then
|
|
41
|
+
`invalidate` the collection's key with `QueryCache`; for an optimistic
|
|
42
|
+
create or delete, follow "File an SDK gap" in `record-collections.md`.
|
|
43
|
+
Done when the widget has no `useMutation`, no hand-written record cache
|
|
44
|
+
and no write queue.
|
|
45
|
+
7. **Verify.** Run the type-check, the machine tests (with
|
|
46
|
+
`makeTestQueryCache()`) and oxlint with the `recommended` config of
|
|
47
|
+
`@collective-intelligence/lint`. Done when all pass with no
|
|
48
|
+
`ci-solution/effect-writes` finding.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: extensions-sdk-frontend-react-tanstack-db-ssr
|
|
3
|
+
description: Decide how a Studio Widget on `@collective-intelligence/extensions-sdk-frontend-react/records` shows Record Collection rows on first render, given that the Studio Host renders widgets in the browser. Use when a widget asks for server rendering, prefetch or hydration of Record Collections, imports `seedRecords` or `getRequestClients`, or the `@tanstack/db#meta-framework` skill would apply.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# First render of Record Collections in a widget
|
|
7
|
+
|
|
8
|
+
Reference: [record-collections.md](../../docs/record-collections.md),
|
|
9
|
+
[tanstack-skills.md](../../docs/tanstack-skills.md).
|
|
10
|
+
|
|
11
|
+
The Studio Host renders widgets in the browser and has no React Server
|
|
12
|
+
Components. The shipped `@tanstack/db#meta-framework` skill does not apply.
|
|
13
|
+
|
|
14
|
+
## Steps
|
|
15
|
+
|
|
16
|
+
1. **Check where the code renders.** A Studio or Dashboard contribution
|
|
17
|
+
renders in the Host's browser page. Code that also runs in a Next.js app
|
|
18
|
+
on `@collective-intelligence/external-apps-client-sdk-react` follows that
|
|
19
|
+
SDK's `client-sdk-react-tanstack-db-ssr` skill there. Done when you know
|
|
20
|
+
which host renders each component.
|
|
21
|
+
2. **Drop the server seed from widgets.** Remove `seedRecords`,
|
|
22
|
+
`getRequestClients` and `<RecordSeedBoundary>` from widget code: the
|
|
23
|
+
Host makes no server render, so nothing seeds them. Done when no widget
|
|
24
|
+
module imports them.
|
|
25
|
+
3. **Load in the browser.** Open the Query Record Collection in a
|
|
26
|
+
`fromLiveQuery` actor as soon as the widget mounts. Until the first
|
|
27
|
+
`records.rows` event, the machine is in a loading state and the view shows
|
|
28
|
+
it. Done when the widget renders a loading state, then the rows.
|
|
29
|
+
4. **Reuse the Host's cache.** Give the collection the same `queryKey` in
|
|
30
|
+
every widget that reads the same records: the Host and all widgets share
|
|
31
|
+
one `QueryClient`, so those widgets share one cache entry. Done when
|
|
32
|
+
widgets that read the same records share one key.
|
|
33
|
+
5. **Verify.** Open the widget in Studio and check the loading state, then the
|
|
34
|
+
rows, with no error in the console. Done when both show.
|