@collective-intelligence/extensions-sdk-frontend-react 0.2.10 → 0.2.12
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 +3 -2
- package/README.md +7 -4
- package/dist/effect/effect-runtime/query-cache.d.ts +48 -3
- package/dist/effect.js +1 -1
- package/docs/effect.md +18 -17
- package/package.json +11 -8
package/AGENTS.md
CHANGED
|
@@ -14,8 +14,9 @@ the full API.
|
|
|
14
14
|
peer. `react`, `react-dom` and `xstate` come from the host.
|
|
15
15
|
- Write in a `fromEffect` actor, never with `useMutation` or `fromPromise`;
|
|
16
16
|
after the write, refresh the reads through `QueryCache`. An optimistic
|
|
17
|
-
write
|
|
18
|
-
`
|
|
17
|
+
write of one key uses `QueryCache.optimisticUpdate`; a write of several
|
|
18
|
+
keys calls `QueryCache.cancel` on them before `update`, and `invalidate`
|
|
19
|
+
when the write settles. Mount
|
|
19
20
|
`EffectRuntimeProvider` at the root of each widget.
|
|
20
21
|
- Test machines with `makeTestQueryCache()` of `/effect`, never with a double
|
|
21
22
|
made with `QueryCache.of`. Seed its `queryClient`, then assert on its
|
package/README.md
CHANGED
|
@@ -304,15 +304,18 @@ and after the write the actor refreshes the reads through `QueryCache`.
|
|
|
304
304
|
- `fromEffect(run)` makes XState actor logic that runs the effect of `run` on
|
|
305
305
|
`input.runtime`. A stop of the actor interrupts the fiber; after a stop, no
|
|
306
306
|
result of that run gets to the actor, also in a React Strict Mode remount.
|
|
307
|
-
- `QueryCache` has `fetch`, `invalidate`, `cancel`, `setData`, `update
|
|
308
|
-
`remove`. `invalidate`, `cancel`, `update` and `remove` apply to the key and
|
|
307
|
+
- `QueryCache` has `fetch`, `invalidate`, `cancel`, `setData`, `update`,
|
|
308
|
+
`remove` and `optimisticUpdate`. `invalidate`, `cancel`, `update` and `remove` apply to the key and
|
|
309
309
|
to each key below it. `fetch` runs its query once, without a retry, and fails
|
|
310
310
|
with `StudioApiError` or `StudioApiTransportError`; another failure is a
|
|
311
311
|
Defect. A cancel of the key interrupts the query; the waiting `fetch` then
|
|
312
312
|
returns the cached value, or is interrupted when the key has no value.
|
|
313
313
|
`cancel` cancels the running fetches, so they write no value; it keeps the
|
|
314
|
-
cached values and starts no fetch.
|
|
315
|
-
|
|
314
|
+
cached values and starts no fetch. `optimisticUpdate(key, edit, write)` is
|
|
315
|
+
an optimistic write of one key: it cancels the running fetch, caches
|
|
316
|
+
`edit` of the cached value, runs `write`, puts back the value from before
|
|
317
|
+
the edit when `write` fails while the entry still holds the edit, and
|
|
318
|
+
invalidates the key when `write` ends. The
|
|
316
319
|
Host and all widgets share one `QueryClient`, so start each key with the
|
|
317
320
|
package name.
|
|
318
321
|
- `makeTestQueryCache(options?)` gives a machine test the `QueryCache` over a
|
|
@@ -27,7 +27,8 @@ export type QueryCacheValue = Readonly<{
|
|
|
27
27
|
* cached values and starts no fetch, so a view of a key whose first fetch
|
|
28
28
|
* it cancels shows no value until the next fetch. Call it before an
|
|
29
29
|
* optimistic `update`, so that a fetch that ends later does not replace the
|
|
30
|
-
* new value, and `invalidate` when the write ends
|
|
30
|
+
* new value, and `invalidate` when the write ends; `optimisticUpdate` does
|
|
31
|
+
* these steps for one key.
|
|
31
32
|
*/
|
|
32
33
|
cancel: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
33
34
|
/** Caches `data` as the value of `queryKey`; views show it at once. */
|
|
@@ -49,6 +50,20 @@ export type QueryCacheValue = Readonly<{
|
|
|
49
50
|
* keeps its last result until it renders again.
|
|
50
51
|
*/
|
|
51
52
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
55
|
+
* `queryKey`, and gives its result. It cancels the running fetch of the key
|
|
56
|
+
* and caches `edit` of the cached value, so views show the edit at once; an
|
|
57
|
+
* entry without a value stays without one. When `write` fails, is
|
|
58
|
+
* interrupted or dies, the value from before the edit comes back, but only
|
|
59
|
+
* while the entry still holds the edit: a value that a fetch or another
|
|
60
|
+
* member put after it stays. When `write` ends, the key is invalidated, and
|
|
61
|
+
* its views read it again. The optimistic writes to one key, of every
|
|
62
|
+
* `QueryCache` over the same `QueryClient`, run one at a time in call
|
|
63
|
+
* order, so each edit starts from the value of the write before it. Writes
|
|
64
|
+
* to other keys, also keys below `queryKey`, do not wait.
|
|
65
|
+
*/
|
|
66
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
52
67
|
}>;
|
|
53
68
|
declare const QueryCache_base: Context.ServiceClass<QueryCache, "@collective-intelligence/extensions-sdk-frontend-react/QueryCache", Readonly<{
|
|
54
69
|
/**
|
|
@@ -67,7 +82,8 @@ declare const QueryCache_base: Context.ServiceClass<QueryCache, "@collective-int
|
|
|
67
82
|
* cached values and starts no fetch, so a view of a key whose first fetch
|
|
68
83
|
* it cancels shows no value until the next fetch. Call it before an
|
|
69
84
|
* optimistic `update`, so that a fetch that ends later does not replace the
|
|
70
|
-
* new value, and `invalidate` when the write ends
|
|
85
|
+
* new value, and `invalidate` when the write ends; `optimisticUpdate` does
|
|
86
|
+
* these steps for one key.
|
|
71
87
|
*/
|
|
72
88
|
cancel: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
73
89
|
/** Caches `data` as the value of `queryKey`; views show it at once. */
|
|
@@ -89,6 +105,20 @@ declare const QueryCache_base: Context.ServiceClass<QueryCache, "@collective-int
|
|
|
89
105
|
* keeps its last result until it renders again.
|
|
90
106
|
*/
|
|
91
107
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
110
|
+
* `queryKey`, and gives its result. It cancels the running fetch of the key
|
|
111
|
+
* and caches `edit` of the cached value, so views show the edit at once; an
|
|
112
|
+
* entry without a value stays without one. When `write` fails, is
|
|
113
|
+
* interrupted or dies, the value from before the edit comes back, but only
|
|
114
|
+
* while the entry still holds the edit: a value that a fetch or another
|
|
115
|
+
* member put after it stays. When `write` ends, the key is invalidated, and
|
|
116
|
+
* its views read it again. The optimistic writes to one key, of every
|
|
117
|
+
* `QueryCache` over the same `QueryClient`, run one at a time in call
|
|
118
|
+
* order, so each edit starts from the value of the write before it. Writes
|
|
119
|
+
* to other keys, also keys below `queryKey`, do not wait.
|
|
120
|
+
*/
|
|
121
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
92
122
|
}>>;
|
|
93
123
|
/**
|
|
94
124
|
* The remote data of the Studio Host's `QueryClient`, as the machine actors of
|
|
@@ -116,7 +146,8 @@ export declare const make: (queryClient: QueryClient) => Readonly<{
|
|
|
116
146
|
* cached values and starts no fetch, so a view of a key whose first fetch
|
|
117
147
|
* it cancels shows no value until the next fetch. Call it before an
|
|
118
148
|
* optimistic `update`, so that a fetch that ends later does not replace the
|
|
119
|
-
* new value, and `invalidate` when the write ends
|
|
149
|
+
* new value, and `invalidate` when the write ends; `optimisticUpdate` does
|
|
150
|
+
* these steps for one key.
|
|
120
151
|
*/
|
|
121
152
|
cancel: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
122
153
|
/** Caches `data` as the value of `queryKey`; views show it at once. */
|
|
@@ -138,6 +169,20 @@ export declare const make: (queryClient: QueryClient) => Readonly<{
|
|
|
138
169
|
* keeps its last result until it renders again.
|
|
139
170
|
*/
|
|
140
171
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
174
|
+
* `queryKey`, and gives its result. It cancels the running fetch of the key
|
|
175
|
+
* and caches `edit` of the cached value, so views show the edit at once; an
|
|
176
|
+
* entry without a value stays without one. When `write` fails, is
|
|
177
|
+
* interrupted or dies, the value from before the edit comes back, but only
|
|
178
|
+
* while the entry still holds the edit: a value that a fetch or another
|
|
179
|
+
* member put after it stays. When `write` ends, the key is invalidated, and
|
|
180
|
+
* its views read it again. The optimistic writes to one key, of every
|
|
181
|
+
* `QueryCache` over the same `QueryClient`, run one at a time in call
|
|
182
|
+
* order, so each edit starts from the value of the write before it. Writes
|
|
183
|
+
* to other keys, also keys below `queryKey`, do not wait.
|
|
184
|
+
*/
|
|
185
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
141
186
|
}>;
|
|
142
187
|
/** A layer of the `QueryCache` over `queryClient`. */
|
|
143
188
|
export declare const layer: (queryClient: QueryClient) => Layer.Layer<QueryCache, never, never>;
|
package/dist/effect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import*as f from"effect/Schema";class S extends f.TaggedError()("StudioApiError",{body:f.Unknown,status:f.Int}){}class d extends f.TaggedError()("StudioApiTransportError",{cause:f.Defect()}){}import{fromPromise as N}from"xstate";var A=()=>Promise.race([]),T=(t)=>N(async({input:s,signal:a})=>{try{let e=await s.runtime.runPromise(t(s),{signal:a});return a.aborted?A():e}catch(e){if(a.aborted)return A();throw e}});import{hashKey as M,isCancelledError as K,QueryObserver as q}from"@tanstack/react-query";import*as Q from"effect/Brand";import*as x from"effect/Context";import*as i from"effect/Effect";import*as c from"effect/Exit";import{constVoid as P}from"effect/Function";import*as g from"effect/Layer";import*as C from"effect/Semaphore";class p extends x.Service()("@collective-intelligence/extensions-sdk-frontend-react/QueryCache"){}var w=new WeakMap,F=(t,s)=>(a)=>{let e=M(s),r=w.get(t)??new Map;return w.set(t,r),i.acquireUseRelease(i.sync(()=>{let n=r.get(e)??{semaphore:C.makeUnsafe(1),users:0};return n.users+=1,r.set(e,n),n}),(n)=>n.semaphore.withPermits(1)(a),(n)=>i.sync(()=>{if(n.users-=1,n.users===0)r.delete(e)}))},H=Q.nominal(),B=(t,s,a)=>{let e=t.getQueryData(s);if(e===void 0)return;t.setQueryData(s,a(e));let r=t.getQueryCache().find({exact:!0,queryKey:s});return r===void 0?void 0:H({dataUpdateCount:r.state.dataUpdateCount,query:r,snapshot:e})},j=(t,s)=>{let{query:a}=s;if(t.getQueryCache().get(a.queryHash)===a&&a.state.dataUpdateCount===s.dataUpdateCount)t.setQueryData(a.queryKey,s.snapshot)},D=(t)=>{let s=async({query:e,...r},n)=>{try{return c.succeed(await t.fetchQuery({...r,queryFn:({signal:o})=>i.runPromiseWith(n)(e,{signal:o}),retry:!1}))}catch(o){if(K(o))return c.interrupt();return o instanceof S||o instanceof d?c.fail(o):c.die(o)}},a=(e)=>i.sync(()=>new q(t,{enabled:!1,queryKey:e}).subscribe(P));return p.of({cancel:(e)=>i.promise(()=>t.cancelQueries({queryKey:e})),fetch:(e)=>i.flatMap(i.context(),(r)=>i.acquireUseRelease(a(e.queryKey),()=>i.flatten(i.promise(()=>s(e,r))),(n)=>i.sync(n))),invalidate:(e)=>i.promise(()=>t.invalidateQueries({queryKey:e})),optimisticUpdate:(e,r,n)=>F(t,e)(i.acquireUseRelease(i.promise(()=>t.cancelQueries({exact:!0,queryKey:e})).pipe(i.andThen(i.sync(()=>B(t,e,r)))),()=>n,(o,u)=>i.sync(()=>{if(o!==void 0&&c.isFailure(u))j(t,o);t.invalidateQueries({exact:!0,queryKey:e})}))),remove:(e)=>i.sync(()=>{t.removeQueries({queryKey:e})}),setData:(e,r)=>i.sync(()=>{t.setQueryData(e,r)}),update:(e,r)=>i.sync(()=>{t.setQueriesData({queryKey:e},r)})})},k=(t)=>g.succeed(p,D(t));import{QueryClient as z}from"@tanstack/react-query";import*as V from"effect/Effect";import*as v from"effect/Layer";var J=(t={})=>{let s=t.queryClient??new z({defaultOptions:{queries:{gcTime:1/0}}});for(let[o,u]of t.seed??[])s.setQueryData(o,u);let a=D(s),e=t.overrides??{},r=[],n=(o,u,m)=>V.suspend(()=>(r.push({member:o,queryKey:u}),m()));return{calls:r,layer:v.succeed(p,p.of({cancel:(o)=>n("cancel",o,()=>(e.cancel??a.cancel)(o)),fetch:(o)=>n("fetch",o.queryKey,()=>(e.fetch??a.fetch)(o)),invalidate:(o)=>n("invalidate",o,()=>(e.invalidate??a.invalidate)(o)),optimisticUpdate:(o,u,m)=>n("optimisticUpdate",o,()=>(e.optimisticUpdate??a.optimisticUpdate)(o,u,m)),remove:(o)=>n("remove",o,()=>(e.remove??a.remove)(o)),setData:(o,u)=>n("setData",o,()=>(e.setData??a.setData)(o,u)),update:(o,u)=>n("update",o,()=>(e.update??a.update)(o,u))})),queryClient:s}};import{useQueryClient as X}from"@tanstack/react-query";import*as L from"effect/ManagedRuntime";import{createContext as Y,useContext as Z,useMemo as _}from"react";import{jsx as re}from"react/jsx-runtime";var G=Y(null),ee=({children:t})=>{let s=X(),a=_(()=>L.make(k(s)),[s]);return re(G.Provider,{value:a,children:t})},te=()=>{let t=Z(G);if(t===null)throw Error("useEffectRuntime requires an EffectRuntimeProvider");return t};import{client as oe,studioAgentRunsGet as ae,studioAgentRunsList as ie,studioViewDataGetItem as se,studioViewDataSearch as ne,studioViewDataSearchMembers as ue,studioViewDataUpdateItem as ce,studioWorkflowRunsGet as fe,studioWorkflowRunsList as de}from"@collective-intelligence/extensions-sdk-frontend-react/studio-api";import*as O from"effect/Config";import*as b from"effect/Context";import*as E from"effect/Effect";import*as R from"effect/Layer";class h extends b.Service()("@collective-intelligence/extensions-sdk-frontend-react/StudioApi"){}var pe=(t={})=>{let s=t.baseUrl===void 0?{}:{baseUrl:t.baseUrl},a=(e,r)=>E.fn(`StudioApi.${e}`)(function*(o){let u=!1,m=oe.getConfig().fetch??globalThis.fetch,W=Object.assign(async(l,I)=>(u=!0,await m(l,I)),m),y=yield*E.tryPromise({catch:(l)=>{if(u)return new d({cause:l});throw l},try:(l)=>r(o,{...s,fetch:W,responseStyle:"fields",signal:l,throwOnError:!1})});if(y.response===void 0)return yield*new d({cause:y.error});if(!y.response.ok)return yield*new S({body:y.error,status:y.response.status});return y.data});return h.of({studioAgentRunsGet:a("studioAgentRunsGet",(e,r)=>ae({...e,...r})),studioAgentRunsList:a("studioAgentRunsList",(e,r)=>ie({...e,...r})),studioViewDataGetItem:a("studioViewDataGetItem",(e,r)=>se({...e,...r})),studioViewDataSearch:a("studioViewDataSearch",(e,r)=>ne({...e,...r})),studioViewDataSearchMembers:a("studioViewDataSearchMembers",(e,r)=>ue({...e,...r})),studioViewDataUpdateItem:a("studioViewDataUpdateItem",(e,r)=>ce({...e,...r})),studioWorkflowRunsGet:a("studioWorkflowRunsGet",(e,r)=>fe({...e,...r})),studioWorkflowRunsList:a("studioWorkflowRunsList",(e,r)=>de({...e,...r}))})},U=(t={})=>R.sync(h,()=>pe(t)),me=(t)=>R.unwrap(E.map(O.unwrap(t),U));export{ee as EffectRuntimeProvider,p as QueryCache,h as StudioApi,S as StudioApiError,d as StudioApiTransportError,T as fromEffect,U as layerStudioApi,me as layerStudioApiConfig,J as makeTestQueryCache,te as useEffectRuntime};
|
package/docs/effect.md
CHANGED
|
@@ -103,11 +103,16 @@ export const RowsWidget = (props: Readonly<{ name: string }>) => (
|
|
|
103
103
|
|
|
104
104
|
### Optimistic write
|
|
105
105
|
|
|
106
|
-
An optimistic write shows its value before the Studio API answers.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
the stored
|
|
106
|
+
An optimistic write shows its value before the Studio API answers. For one
|
|
107
|
+
key, use `QueryCache.optimisticUpdate(queryKey, edit, write)`: it cancels the
|
|
108
|
+
running fetch of the key, so that a fetch that ends later does not replace the
|
|
109
|
+
new value; caches `edit` of the cached value; runs `write` and gives its
|
|
110
|
+
result; and invalidates the key when `write` ends, so views read the stored
|
|
111
|
+
value. When `write` fails, is interrupted or dies, the value from before the
|
|
112
|
+
edit comes back while the entry still holds the edit. The optimistic writes to
|
|
113
|
+
one key run one at a time in call order. A write that changes several keys does
|
|
114
|
+
these steps itself: `cancel` the keys, `update` them, run the write, and
|
|
115
|
+
`invalidate` the keys when the actor ends.
|
|
111
116
|
|
|
112
117
|
```ts
|
|
113
118
|
type RenameInput = Readonly<{
|
|
@@ -118,19 +123,15 @@ type RenameInput = Readonly<{
|
|
|
118
123
|
|
|
119
124
|
const rename = fromEffect((input: RenameInput) =>
|
|
120
125
|
QueryCache.use((cache) =>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
row.id === input.id ? { ...row, name: input.name } : row
|
|
128
|
-
)
|
|
129
|
-
)
|
|
126
|
+
// An entry without a value stays without one: `edit` gets the cached rows.
|
|
127
|
+
cache.optimisticUpdate(
|
|
128
|
+
rowsKey,
|
|
129
|
+
(rows: ReadonlyArray<Row>) =>
|
|
130
|
+
rows.map((row) =>
|
|
131
|
+
row.id === input.id ? { ...row, name: input.name } : row
|
|
130
132
|
),
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
)
|
|
133
|
+
writeName(input.id, input.name)
|
|
134
|
+
)
|
|
134
135
|
)
|
|
135
136
|
)
|
|
136
137
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@collective-intelligence/extensions-sdk-frontend-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "Public hosted React contribution contracts for Collective Intelligence extensions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -80,11 +80,11 @@
|
|
|
80
80
|
"type-check": "bun check && bun check -p tsconfig.type-tests.json"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@collective-intelligence/extensions-sdk": "0.2.
|
|
84
|
-
"@collective-intelligence/extensions-sdk-frontend": "0.2.
|
|
83
|
+
"@collective-intelligence/extensions-sdk": "0.2.6",
|
|
84
|
+
"@collective-intelligence/extensions-sdk-frontend": "0.2.6"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
87
|
-
"@collective-intelligence/ui-react": "0.2.
|
|
87
|
+
"@collective-intelligence/ui-react": "0.2.3",
|
|
88
88
|
"@tanstack/react-query": ">=5 <6",
|
|
89
89
|
"@xyflow/react": ">=12 <13",
|
|
90
90
|
"effect": "^4.1.0",
|
|
@@ -101,7 +101,8 @@
|
|
|
101
101
|
"access": "public"
|
|
102
102
|
},
|
|
103
103
|
"devDependencies": {
|
|
104
|
-
"@collective-intelligence/ui-react": "0.2.
|
|
104
|
+
"@collective-intelligence/ui-react": "0.2.3",
|
|
105
|
+
"@effect/eslint-plugin": "0.3.2",
|
|
105
106
|
"@effect/vitest": "4.1.0",
|
|
106
107
|
"@hey-api/openapi-ts": "^0.96.0",
|
|
107
108
|
"@oxlint/plugins": "1.90.0",
|
|
@@ -110,8 +111,12 @@
|
|
|
110
111
|
"@types/bun": "1.4.2",
|
|
111
112
|
"@types/react": "^19.2.14",
|
|
112
113
|
"@types/react-dom": "^19.2.3",
|
|
114
|
+
"@typescript-eslint/utils": "8.22.0",
|
|
115
|
+
"@xstate/react": "6.1.0",
|
|
113
116
|
"@xyflow/react": "^12.10.2",
|
|
114
117
|
"effect": "4.1.0",
|
|
118
|
+
"es-module-lexer": "2.3.2",
|
|
119
|
+
"eslint": "9.39.5",
|
|
115
120
|
"eslint-plugin-react-you-might-not-need-an-effect": "latest",
|
|
116
121
|
"jsdom": "^28.1.0",
|
|
117
122
|
"oxfmt": "0.66.0",
|
|
@@ -121,9 +126,7 @@
|
|
|
121
126
|
"typescript": "7.0.2",
|
|
122
127
|
"ultracite": "7.10.7",
|
|
123
128
|
"vitest": "5.0.2",
|
|
124
|
-
"es-module-lexer": "2.3.2",
|
|
125
|
-
"@xstate/react": "6.1.0",
|
|
126
129
|
"xstate": "5.33.3"
|
|
127
130
|
},
|
|
128
|
-
"gitHead": "
|
|
131
|
+
"gitHead": "c417a35ab790e3f72c3e32651face1cc3de00eb9"
|
|
129
132
|
}
|