@collective-intelligence/extensions-sdk-frontend-react 0.8.0 → 0.9.1
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 +6 -3
- package/README.md +21 -8
- package/dist/effect/effect-runtime/query-cache.d.ts +131 -36
- package/dist/effect.d.ts +1 -1
- package/dist/effect.js +1 -1
- package/docs/effect.md +69 -11
- package/docs/guide/ai/index.md +4 -2
- package/docs/guide/ai/studio.md +1 -1
- package/package.json +7 -7
package/AGENTS.md
CHANGED
|
@@ -14,9 +14,12 @@ 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 of one key uses `QueryCache.optimisticUpdate`;
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
write of one key uses `QueryCache.optimisticUpdate`; build its payload in a
|
|
18
|
+
`write` function, which gets the confirmed value when the Studio API gets
|
|
19
|
+
the write, and give the Studio API's answer to `confirm`. Its edit must
|
|
20
|
+
give the same value for a value that already holds it, as a fetch can end
|
|
21
|
+
under it. A write of several keys calls `QueryCache.cancel` on them before
|
|
22
|
+
`update`, and `invalidate` when the write settles. Mount
|
|
20
23
|
`EffectRuntimeProvider` at the root of each widget.
|
|
21
24
|
- Test machines with `makeTestQueryCache()` of `/effect`, never with a double
|
|
22
25
|
made with `QueryCache.of`. Seed its `queryClient`, then assert on its
|
package/README.md
CHANGED
|
@@ -334,19 +334,32 @@ and after the write the actor refreshes the reads through `QueryCache`.
|
|
|
334
334
|
It is the `fromEffect` of `@collective-intelligence/effect-xstate/actors`,
|
|
335
335
|
which the package depends on at the release version.
|
|
336
336
|
- `QueryCache` has `fetch`, `invalidate`, `cancel`, `setData`, `update`,
|
|
337
|
-
`remove` and `
|
|
337
|
+
`remove`, `optimisticUpdate` and `pending`. `invalidate`, `cancel`, `update` and `remove` apply to the key and
|
|
338
338
|
to each key below it. `fetch` runs its query once, without a retry, and fails
|
|
339
339
|
with `StudioApiError` or `StudioApiTransportError`; another failure is a
|
|
340
340
|
Defect. A cancel of the key interrupts the query; the waiting `fetch` then
|
|
341
341
|
returns the cached value, or is interrupted when the key has no value.
|
|
342
342
|
`cancel` cancels the running fetches, so they write no value; it keeps the
|
|
343
|
-
cached values and starts no fetch. `optimisticUpdate(key, edit, write
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
343
|
+
cached values and starts no fetch. `optimisticUpdate(key, edit, write,
|
|
344
|
+
options?)` is an optimistic write of one key: it runs `write` and gives its
|
|
345
|
+
result. Each pending write of the key is a layer of its `edit` over the
|
|
346
|
+
confirmed value, and views show the layers in call order at once. The
|
|
347
|
+
writes to one key, of every `QueryCache` over the same `QueryClient`, go to
|
|
348
|
+
the Studio API one at a time in call order; when the Studio API gets one,
|
|
349
|
+
the running fetch of the key is cancelled, and a `write` function gets
|
|
350
|
+
`{ confirmed, optimistic }` of that moment, so a refused write before it is
|
|
351
|
+
not in its payload. A fetch that ends while writes are pending updates the
|
|
352
|
+
confirmed value, and the layers show over it again. When the Studio API
|
|
353
|
+
confirms the write, the confirmed value becomes
|
|
354
|
+
`options.confirm(result, confirmed)` or gets the edit; when `write` fails,
|
|
355
|
+
is interrupted or dies, only its own layer goes. A value that `setData`,
|
|
356
|
+
`update` or `remove` puts meanwhile stays, and the `confirm` of the writes
|
|
357
|
+
before it does not run. Each run of the effect is one write. The key is
|
|
358
|
+
invalidated when `write` ends, also when `confirm` throws. The
|
|
359
|
+
`QueryClient` holds the writes, so widgets that each bundle `/effect`
|
|
360
|
+
share them. `pending(key)` gives the number of
|
|
361
|
+
optimistic writes of the key that wait or run. The Host and all widgets
|
|
362
|
+
share one `QueryClient`, so start each key with the package name.
|
|
350
363
|
- `makeTestQueryCache(options?)` gives a machine test the `QueryCache` over a
|
|
351
364
|
real `QueryClient` of its own: `layer` for `ManagedRuntime.make`,
|
|
352
365
|
`queryClient` to seed and to assert on, and `calls`, one
|
|
@@ -10,6 +10,32 @@ export type QueryCacheFetchOptions<Data> = Readonly<{
|
|
|
10
10
|
/** Milliseconds for which a cached value stays fresh; without it, the QueryClient default applies. */
|
|
11
11
|
staleTime?: number;
|
|
12
12
|
}>;
|
|
13
|
+
/** The values of the entry of a key when CI gets its optimistic write. */
|
|
14
|
+
export type QueryCacheWriteValues<Data> = Readonly<{
|
|
15
|
+
/**
|
|
16
|
+
* CI's value of the key: the value of the last fetch with the answers of
|
|
17
|
+
* the writes before this one, without any pending edit. `undefined` when
|
|
18
|
+
* the entry has no value.
|
|
19
|
+
*/
|
|
20
|
+
confirmed: Data | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* `confirmed` with the edit of this write only, so a refused write before
|
|
23
|
+
* it is not in it. `undefined` when the entry has no value.
|
|
24
|
+
*/
|
|
25
|
+
optimistic: Data | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
export type QueryCacheOptimisticOptions<Data, A> = Readonly<{
|
|
28
|
+
/**
|
|
29
|
+
* The confirmed value after CI confirms the write with `result`, for
|
|
30
|
+
* example CI's row with its new revision in `confirmed`. Without it, or
|
|
31
|
+
* when it gives `undefined`, the confirmed value gets the edit. It does not
|
|
32
|
+
* run when `setData`, `update` or `remove` replaced the value of the key
|
|
33
|
+
* after the edit: the newer value stays. When it throws, the write dies
|
|
34
|
+
* with its error, and the key still shows its other layers and is
|
|
35
|
+
* invalidated.
|
|
36
|
+
*/
|
|
37
|
+
confirm?: (result: A, confirmed: Data | undefined) => Data | undefined;
|
|
38
|
+
}>;
|
|
13
39
|
export type QueryCacheValue = Readonly<{
|
|
14
40
|
/**
|
|
15
41
|
* The fresh cached value of `queryKey`, or the value of one run of `query`,
|
|
@@ -52,18 +78,41 @@ export type QueryCacheValue = Readonly<{
|
|
|
52
78
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
53
79
|
/**
|
|
54
80
|
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
55
|
-
* `queryKey`, and gives its result.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
81
|
+
* `queryKey`, and gives its result. Each pending write of a key is a layer
|
|
82
|
+
* of its edit over the confirmed value, CI's value of the key, and views
|
|
83
|
+
* show the layers in call order at once; an entry without a value shows no
|
|
84
|
+
* edit until a fetch gives it one. The writes to one key, of every
|
|
85
|
+
* `QueryCache` over the same `QueryClient`, go to CI one at a time in call
|
|
86
|
+
* order; writes to other keys, also keys below `queryKey`, do not wait.
|
|
87
|
+
* The `QueryClient` holds these writes, so each bundled copy of the SDK
|
|
88
|
+
* over it shares them. Each run of the returned effect is one write, so an
|
|
89
|
+
* effect that runs twice writes twice.
|
|
90
|
+
*
|
|
91
|
+
* When CI gets the write, the running fetch of the key is cancelled, and a
|
|
92
|
+
* `write` function is called with the `QueryCacheWriteValues` of that
|
|
93
|
+
* moment: the confirmed value with the answers of the writes before it,
|
|
94
|
+
* and that value with this edit only. A fetch that ends while writes are
|
|
95
|
+
* pending updates the confirmed value, and the edits show over it again,
|
|
96
|
+
* so each edit must give the same value for a value that already holds
|
|
97
|
+
* it, for example set fields or replace a row by its id.
|
|
98
|
+
*
|
|
99
|
+
* When CI confirms the write, its layer goes, and the confirmed value
|
|
100
|
+
* becomes `options.confirm(result, confirmed)` or gets the edit. When
|
|
101
|
+
* `write` fails, is interrupted or dies, only its own layer goes: the
|
|
102
|
+
* other layers show over the confirmed value. A value that `setData`,
|
|
103
|
+
* `update` or `remove` puts while writes are pending stays: the edits
|
|
104
|
+
* before it no longer show or roll back, and CI's answer to them does not
|
|
105
|
+
* change the value. When the write ends, the key is invalidated, and its
|
|
106
|
+
* views read it again. The key is the entry that `getQueryData` finds,
|
|
107
|
+
* also with a `queryKeyHashFn` of the `QueryClient`.
|
|
108
|
+
*/
|
|
109
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R> | ((values: QueryCacheWriteValues<Data>) => Effect.Effect<A, E, R>), options?: QueryCacheOptimisticOptions<Data, A>) => Effect.Effect<A, E, R>;
|
|
110
|
+
/**
|
|
111
|
+
* The number of optimistic writes of `queryKey` that wait for CI or run,
|
|
112
|
+
* of every `QueryCache` over the same `QueryClient`, also of another
|
|
113
|
+
* bundled copy of the SDK. Keys below `queryKey` do not count.
|
|
114
|
+
*/
|
|
115
|
+
pending: (queryKey: QueryKey) => Effect.Effect<number>;
|
|
67
116
|
}>;
|
|
68
117
|
declare const QueryCache_base: Context.ServiceClass<QueryCache, "@collective-intelligence/extensions-sdk-frontend-react/QueryCache", Readonly<{
|
|
69
118
|
/**
|
|
@@ -107,18 +156,41 @@ declare const QueryCache_base: Context.ServiceClass<QueryCache, "@collective-int
|
|
|
107
156
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
108
157
|
/**
|
|
109
158
|
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
110
|
-
* `queryKey`, and gives its result.
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
|
|
121
|
-
|
|
159
|
+
* `queryKey`, and gives its result. Each pending write of a key is a layer
|
|
160
|
+
* of its edit over the confirmed value, CI's value of the key, and views
|
|
161
|
+
* show the layers in call order at once; an entry without a value shows no
|
|
162
|
+
* edit until a fetch gives it one. The writes to one key, of every
|
|
163
|
+
* `QueryCache` over the same `QueryClient`, go to CI one at a time in call
|
|
164
|
+
* order; writes to other keys, also keys below `queryKey`, do not wait.
|
|
165
|
+
* The `QueryClient` holds these writes, so each bundled copy of the SDK
|
|
166
|
+
* over it shares them. Each run of the returned effect is one write, so an
|
|
167
|
+
* effect that runs twice writes twice.
|
|
168
|
+
*
|
|
169
|
+
* When CI gets the write, the running fetch of the key is cancelled, and a
|
|
170
|
+
* `write` function is called with the `QueryCacheWriteValues` of that
|
|
171
|
+
* moment: the confirmed value with the answers of the writes before it,
|
|
172
|
+
* and that value with this edit only. A fetch that ends while writes are
|
|
173
|
+
* pending updates the confirmed value, and the edits show over it again,
|
|
174
|
+
* so each edit must give the same value for a value that already holds
|
|
175
|
+
* it, for example set fields or replace a row by its id.
|
|
176
|
+
*
|
|
177
|
+
* When CI confirms the write, its layer goes, and the confirmed value
|
|
178
|
+
* becomes `options.confirm(result, confirmed)` or gets the edit. When
|
|
179
|
+
* `write` fails, is interrupted or dies, only its own layer goes: the
|
|
180
|
+
* other layers show over the confirmed value. A value that `setData`,
|
|
181
|
+
* `update` or `remove` puts while writes are pending stays: the edits
|
|
182
|
+
* before it no longer show or roll back, and CI's answer to them does not
|
|
183
|
+
* change the value. When the write ends, the key is invalidated, and its
|
|
184
|
+
* views read it again. The key is the entry that `getQueryData` finds,
|
|
185
|
+
* also with a `queryKeyHashFn` of the `QueryClient`.
|
|
186
|
+
*/
|
|
187
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R> | ((values: QueryCacheWriteValues<Data>) => Effect.Effect<A, E, R>), options?: QueryCacheOptimisticOptions<Data, A>) => Effect.Effect<A, E, R>;
|
|
188
|
+
/**
|
|
189
|
+
* The number of optimistic writes of `queryKey` that wait for CI or run,
|
|
190
|
+
* of every `QueryCache` over the same `QueryClient`, also of another
|
|
191
|
+
* bundled copy of the SDK. Keys below `queryKey` do not count.
|
|
192
|
+
*/
|
|
193
|
+
pending: (queryKey: QueryKey) => Effect.Effect<number>;
|
|
122
194
|
}>>;
|
|
123
195
|
/**
|
|
124
196
|
* The remote data of the Studio Host's `QueryClient`, as the machine actors of
|
|
@@ -171,18 +243,41 @@ export declare const make: (queryClient: QueryClient) => Readonly<{
|
|
|
171
243
|
remove: (queryKey: QueryKey) => Effect.Effect<void>;
|
|
172
244
|
/**
|
|
173
245
|
* Runs `write`, the remote write of an optimistic edit of the entry of
|
|
174
|
-
* `queryKey`, and gives its result.
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
|
|
185
|
-
|
|
246
|
+
* `queryKey`, and gives its result. Each pending write of a key is a layer
|
|
247
|
+
* of its edit over the confirmed value, CI's value of the key, and views
|
|
248
|
+
* show the layers in call order at once; an entry without a value shows no
|
|
249
|
+
* edit until a fetch gives it one. The writes to one key, of every
|
|
250
|
+
* `QueryCache` over the same `QueryClient`, go to CI one at a time in call
|
|
251
|
+
* order; writes to other keys, also keys below `queryKey`, do not wait.
|
|
252
|
+
* The `QueryClient` holds these writes, so each bundled copy of the SDK
|
|
253
|
+
* over it shares them. Each run of the returned effect is one write, so an
|
|
254
|
+
* effect that runs twice writes twice.
|
|
255
|
+
*
|
|
256
|
+
* When CI gets the write, the running fetch of the key is cancelled, and a
|
|
257
|
+
* `write` function is called with the `QueryCacheWriteValues` of that
|
|
258
|
+
* moment: the confirmed value with the answers of the writes before it,
|
|
259
|
+
* and that value with this edit only. A fetch that ends while writes are
|
|
260
|
+
* pending updates the confirmed value, and the edits show over it again,
|
|
261
|
+
* so each edit must give the same value for a value that already holds
|
|
262
|
+
* it, for example set fields or replace a row by its id.
|
|
263
|
+
*
|
|
264
|
+
* When CI confirms the write, its layer goes, and the confirmed value
|
|
265
|
+
* becomes `options.confirm(result, confirmed)` or gets the edit. When
|
|
266
|
+
* `write` fails, is interrupted or dies, only its own layer goes: the
|
|
267
|
+
* other layers show over the confirmed value. A value that `setData`,
|
|
268
|
+
* `update` or `remove` puts while writes are pending stays: the edits
|
|
269
|
+
* before it no longer show or roll back, and CI's answer to them does not
|
|
270
|
+
* change the value. When the write ends, the key is invalidated, and its
|
|
271
|
+
* views read it again. The key is the entry that `getQueryData` finds,
|
|
272
|
+
* also with a `queryKeyHashFn` of the `QueryClient`.
|
|
273
|
+
*/
|
|
274
|
+
optimisticUpdate: <Data, A, E, R>(queryKey: QueryKey, edit: (current: Data) => Data, write: Effect.Effect<A, E, R> | ((values: QueryCacheWriteValues<Data>) => Effect.Effect<A, E, R>), options?: QueryCacheOptimisticOptions<Data, A>) => Effect.Effect<A, E, R>;
|
|
275
|
+
/**
|
|
276
|
+
* The number of optimistic writes of `queryKey` that wait for CI or run,
|
|
277
|
+
* of every `QueryCache` over the same `QueryClient`, also of another
|
|
278
|
+
* bundled copy of the SDK. Keys below `queryKey` do not count.
|
|
279
|
+
*/
|
|
280
|
+
pending: (queryKey: QueryKey) => Effect.Effect<number>;
|
|
186
281
|
}>;
|
|
187
282
|
/** A layer of the `QueryCache` over `queryClient`. */
|
|
188
283
|
export declare const layer: (queryClient: QueryClient) => Layer.Layer<QueryCache, never, never>;
|
package/dist/effect.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ export { makeTestQueryCache } from "./effect/effect-runtime/query-cache-testing.
|
|
|
5
5
|
export { EffectRuntimeProvider, useEffectRuntime } from "./effect/effect-runtime/runtime-provider.js";
|
|
6
6
|
export { StudioApi, layer as layerStudioApi, layerConfig as layerStudioApiConfig } from "./effect/studio-api/service.js";
|
|
7
7
|
export type { EffectRunner } from "@collective-intelligence/effect-xstate/actors";
|
|
8
|
-
export type { QueryCacheFetchOptions } from "./effect/effect-runtime/query-cache.js";
|
|
8
|
+
export type { QueryCacheFetchOptions, QueryCacheOptimisticOptions, QueryCacheWriteValues } from "./effect/effect-runtime/query-cache.js";
|
|
9
9
|
export type { QueryCacheCall, TestQueryCache, TestQueryCacheOptions } from "./effect/effect-runtime/query-cache-testing.js";
|
|
10
10
|
export type { StudioApiOperation, StudioApiOperationName, StudioApiOptions, StudioApiRequest, StudioApiSuccess, StudioApiValue } from "./effect/studio-api/service.js";
|
package/dist/effect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{fromEffect as
|
|
1
|
+
import{fromEffect as Ne}from"@collective-intelligence/effect-xstate/actors";import*as y from"effect/Schema";class S extends y.TaggedError()("StudioApiError",{body:y.Unknown,status:y.Int}){}class l extends y.TaggedError()("StudioApiTransportError",{cause:y.Defect()}){}import{isCancelledError as q,QueryObserver as P}from"@tanstack/react-query";import*as v from"effect/Context";import*as a from"effect/Effect";import*as p from"effect/Exit";import{constVoid as F}from"effect/Function";import*as k from"effect/Layer";import*as g from"effect/Schema";class m extends v.Service()("@collective-intelligence/extensions-sdk-frontend-react/QueryCache"){}var C=Symbol.for("@collective-intelligence/query-cache/optimistic-writes/v1"),O=(t)=>t[C],L=(t,i)=>t.defaultQueryOptions({queryKey:i}).queryHash,Q=(t)=>{for(let i of t.layers)i.shown=!1},w=(t,i)=>{if(i.confirmed===void 0)return;let r=i.confirmed;for(let e of i.layers)if(e.shown)r=e.apply(r);i.showing=!0;try{t.setQueryData(i.queryKey,r)}finally{i.showing=!1}},B=(t,i)=>(r)=>{let e=i.get(r.query.queryHash);if(e===void 0||e.showing)return;if(r.type==="removed"){e.confirmed=void 0,Q(e);return}if(r.type!=="updated")return;let{action:s}=r;if(s.type==="success")if(e.confirmed=r.query.state.data,s.manual===!0)Q(e);else w(t,e);else if(s.type==="setState")if(r.query.state.data===void 0)e.confirmed=void 0,Q(e);else w(t,e)},H=(t)=>{let i=new Map,r={keys:i,stop:t.getQueryCache().subscribe(B(t,i))};return Object.defineProperty(t,C,{configurable:!0,value:r}),r},x=(t,i)=>{let r=t.layers.indexOf(i);if(r===-1)return!1;return t.layers.splice(r,1),!0},j=(t)=>{t.layers[0]?.wake?.()},z=(t,i)=>a.callback((r)=>{if(t.layers[0]===i){r(a.undefined);return}return i.wake=()=>{i.wake=void 0,r(a.undefined)},a.sync(()=>{i.wake=void 0})}),W=(t,i)=>{let r=O(t);if(i.layers.length>0||r?.keys.get(i.hash)!==i)return;if(r.keys.delete(i.hash),r.keys.size===0)r.stop(),Reflect.deleteProperty(t,C)},J=(t,i,r)=>{let e=O(t)??H(t),s=L(t,i),u=e.keys.get(s)??{confirmed:t.getQueryData(i),hash:s,layers:[],queryKey:i,showing:!1};e.keys.set(s,u),u.layers.push(r);try{w(t,u)}catch(o){throw x(u,r),W(t,u),o}return u},V=(t)=>{let i=async({query:e,...s},u)=>{try{return p.succeed(await t.fetchQuery({...s,queryFn:({signal:o})=>a.runPromiseWith(u)(e,{signal:o}),retry:!1}))}catch(o){if(q(o))return p.interrupt();return g.is(S)(o)||g.is(l)(o)?p.fail(o):p.die(o)}},r=(e)=>a.sync(()=>new P(t,{enabled:!1,queryKey:e}).subscribe(F));return m.of({cancel:(e)=>a.promise(()=>t.cancelQueries({queryKey:e})),fetch:(e)=>a.flatMap(a.context(),(s)=>a.acquireUseRelease(r(e.queryKey),()=>a.flatten(a.promise(()=>i(e,s))),(u)=>a.sync(u))),invalidate:(e)=>a.promise(()=>t.invalidateQueries({queryKey:e})),optimisticUpdate:(e,s,u,o)=>a.suspend(()=>{let c={apply:s,confirm:(n,d)=>{if(!c.shown)return n;let E=o?.confirm?.(d,n);if(E!==void 0)return E;return n===void 0?void 0:s(n)},send:(n)=>a.isEffect(u)?u:u({confirmed:n,optimistic:n===void 0?void 0:s(n)}),shown:!0,wake:void 0},f=c,R=(n,d)=>a.sync(()=>{x(n,f);let E=n.confirmed;try{if(p.isSuccess(d))n.confirmed=f.confirm(n.confirmed,d.value)}finally{if(c.shown||n.confirmed!==E)w(t,n);t.invalidateQueries({exact:!0,queryKey:e})}});return a.acquireUseRelease(a.sync(()=>J(t,e,f)),(n)=>z(n,f).pipe(a.andThen(a.promise(()=>t.cancelQueries({exact:!0,queryKey:e})).pipe(a.andThen(a.suspend(()=>f.send(n.confirmed))),a.onExit((d)=>R(n,d))))),(n)=>a.sync(()=>{if(x(n,f)&&c.shown)w(t,n);j(n),W(t,n)}))}),pending:(e)=>a.sync(()=>O(t)?.keys.get(L(t,e))?.layers.length??0),remove:(e)=>a.sync(()=>{t.removeQueries({queryKey:e})}),setData:(e,s)=>a.sync(()=>{t.setQueryData(e,s)}),update:(e,s)=>a.sync(()=>{t.setQueriesData({queryKey:e},s)})})},K=(t)=>k.succeed(m,V(t));import{QueryClient as X}from"@tanstack/react-query";import*as b from"effect/Effect";import*as G from"effect/Layer";var Y=(t={})=>{let i=t.queryClient??new X({defaultOptions:{queries:{gcTime:1/0}}});for(let[o,c]of t.seed??[])i.setQueryData(o,c);let r=V(i),e=t.overrides??{},s=[],u=(o,c,f)=>b.suspend(()=>(s.push({member:o,queryKey:c}),f()));return{calls:s,layer:G.succeed(m,m.of({cancel:(o)=>u("cancel",o,()=>(e.cancel??r.cancel)(o)),fetch:(o)=>u("fetch",o.queryKey,()=>(e.fetch??r.fetch)(o)),invalidate:(o)=>u("invalidate",o,()=>(e.invalidate??r.invalidate)(o)),optimisticUpdate:(o,c,f,R)=>u("optimisticUpdate",o,()=>(e.optimisticUpdate??r.optimisticUpdate)(o,c,f,R)),pending:(o)=>u("pending",o,()=>(e.pending??r.pending)(o)),remove:(o)=>u("remove",o,()=>(e.remove??r.remove)(o)),setData:(o,c)=>u("setData",o,()=>(e.setData??r.setData)(o,c)),update:(o,c)=>u("update",o,()=>(e.update??r.update)(o,c))})),queryClient:i}};import{EffectRuntimeProvider as Z,makeEffectRuntimeContext as _,useEffectRuntime as ee}from"@collective-intelligence/effect-xstate/react";import{useQueryClient as te}from"@tanstack/react-query";import*as N from"effect/ManagedRuntime";import{useMemo as re}from"react";import{jsx as oe}from"react/jsx-runtime";var I=_(),ie=({children:t})=>{let i=te(),r=re(()=>N.make(K(i)),[i]);return oe(Z,{context:I,runtime:r,children:t})},ae=()=>ee(I);import{client as se,studioAgentRunsGet as ne,studioAgentRunsList as ue,studioViewDataGetItem as ce,studioViewDataSearch as fe,studioViewDataSearchMembers as de,studioViewDataUpdateItem as pe,studioWorkflowRunsGet as ye,studioWorkflowRunsList as le}from"@collective-intelligence/extensions-sdk-frontend-react/studio-api";import*as T from"effect/Config";import*as U from"effect/Context";import*as h from"effect/Effect";import*as D from"effect/Layer";class A extends U.Service()("@collective-intelligence/extensions-sdk-frontend-react/StudioApi"){}var me=(t={})=>{let i=t.baseUrl===void 0?{}:{baseUrl:t.baseUrl},r=(e,s)=>h.fn(`StudioApi.${e}`)(function*(o){let c=!1,f=se.getConfig().fetch??globalThis.fetch,R=Object.assign(async(d,E)=>(c=!0,await f(d,E)),f),n=yield*h.tryPromise({catch:(d)=>{if(c)return new l({cause:d});throw d},try:(d)=>s(o,{...i,fetch:R,responseStyle:"fields",signal:d,throwOnError:!1})});if(n.response===void 0)return yield*new l({cause:n.error});if(!n.response.ok)return yield*new S({body:n.error,status:n.response.status});return n.data});return A.of({studioAgentRunsGet:r("studioAgentRunsGet",(e,s)=>ne({...e,...s})),studioAgentRunsList:r("studioAgentRunsList",(e,s)=>ue({...e,...s})),studioViewDataGetItem:r("studioViewDataGetItem",(e,s)=>ce({...e,...s})),studioViewDataSearch:r("studioViewDataSearch",(e,s)=>fe({...e,...s})),studioViewDataSearchMembers:r("studioViewDataSearchMembers",(e,s)=>de({...e,...s})),studioViewDataUpdateItem:r("studioViewDataUpdateItem",(e,s)=>pe({...e,...s})),studioWorkflowRunsGet:r("studioWorkflowRunsGet",(e,s)=>ye({...e,...s})),studioWorkflowRunsList:r("studioWorkflowRunsList",(e,s)=>le({...e,...s}))})},M=(t={})=>D.sync(A,()=>me(t)),Ee=(t)=>D.unwrap(h.map(T.unwrap(t),M));export{ie as EffectRuntimeProvider,m as QueryCache,A as StudioApi,S as StudioApiError,l as StudioApiTransportError,Ne as fromEffect,M as layerStudioApi,Ee as layerStudioApiConfig,Y as makeTestQueryCache,ae as useEffectRuntime};
|
package/docs/effect.md
CHANGED
|
@@ -104,17 +104,55 @@ export const RowsWidget = (props: Readonly<{ name: string }>) => (
|
|
|
104
104
|
### Optimistic write
|
|
105
105
|
|
|
106
106
|
An optimistic write shows its value before the Studio API answers. For one
|
|
107
|
-
key, use `QueryCache.optimisticUpdate(queryKey, edit, write)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
value
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
key, use `QueryCache.optimisticUpdate(queryKey, edit, write, options?)`. It
|
|
108
|
+
runs `write` and gives its result.
|
|
109
|
+
|
|
110
|
+
- **Layers.** Each pending write of a key is a layer of its `edit` over the
|
|
111
|
+
confirmed value, which is the Studio API's value of the key. Views show the
|
|
112
|
+
layers in call order at once, so a second quick write shows while the
|
|
113
|
+
Studio API has not answered the first. An entry without a value shows no
|
|
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.
|
|
116
153
|
|
|
117
154
|
```ts
|
|
155
|
+
type Row = Readonly<{ id: string; name: string; revision: string }>
|
|
118
156
|
type RenameInput = Readonly<{
|
|
119
157
|
id: string
|
|
120
158
|
name: string
|
|
@@ -123,19 +161,39 @@ type RenameInput = Readonly<{
|
|
|
123
161
|
|
|
124
162
|
const rename = fromEffect((input: RenameInput) =>
|
|
125
163
|
QueryCache.use((cache) =>
|
|
126
|
-
// An entry without a value stays without one: `edit` gets the cached rows.
|
|
127
164
|
cache.optimisticUpdate(
|
|
128
165
|
rowsKey,
|
|
166
|
+
// Replaces the row by its id, so a fetch under the layer keeps it.
|
|
129
167
|
(rows: ReadonlyArray<Row>) =>
|
|
130
168
|
rows.map((row) =>
|
|
131
169
|
row.id === input.id ? { ...row, name: input.name } : row
|
|
132
170
|
),
|
|
133
|
-
|
|
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
|
+
}
|
|
134
184
|
)
|
|
135
185
|
)
|
|
136
186
|
)
|
|
187
|
+
|
|
188
|
+
const savingRows = fromEffect(
|
|
189
|
+
(input: Readonly<{ runtime: EffectRunner<QueryCache> }>) =>
|
|
190
|
+
QueryCache.use((cache) => cache.pending(rowsKey))
|
|
191
|
+
)
|
|
137
192
|
```
|
|
138
193
|
|
|
194
|
+
A `write` given as an Effect works as before: it is the same for each
|
|
195
|
+
confirmed value.
|
|
196
|
+
|
|
139
197
|
## Rules of the write parts
|
|
140
198
|
|
|
141
199
|
- Mount `EffectRuntimeProvider` at the root of each widget. It uses the
|
package/docs/guide/ai/index.md
CHANGED
|
@@ -224,9 +224,11 @@ Your server invokes an Agent with `agents.invoke({ agentId, input })`, which ret
|
|
|
224
224
|
|
|
225
225
|
`runs.stream` returns three kinds of event. `agent.tool` gives the tool name and its state (running, completed or failed) without arguments or results. `agent.reasoning` gives the provider's reasoning-summary text in batches, and `agent.context` gives input and output token counts for each model step. A page holds at most 100 events, so keep polling with the last cursor. Filter the reasoning text on your server before you show it to visitors.
|
|
226
226
|
|
|
227
|
-
For a multi-turn chat,
|
|
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
|
-
|
|
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`. `runs.sources` gives the searched Document Versions with their pages and a download link. `runs.stream` and `runs.cancel` read only runs that `agents.invoke` started: follow a turn with `GET /runs/:runId/stream` of the chat mount, and stop it with `ci.chat.cancelTurn`.
|
|
230
|
+
|
|
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.
|
|
230
232
|
|
|
231
233
|
## Workflows
|
|
232
234
|
|
package/docs/guide/ai/studio.md
CHANGED
|
@@ -228,7 +228,7 @@ Workflows and agents can start from either side:
|
|
|
228
228
|
| Primitive | From the web app | From Studio |
|
|
229
229
|
| --------- | ----------------------------------------------------------------------------- | ------------------------------------------- |
|
|
230
230
|
| Workflow | An External application trigger, started by the server's Fixed workflow route | An On-demand trigger with a trigger form |
|
|
231
|
-
| Agent | `agents.invoke` or
|
|
231
|
+
| Agent | `agents.invoke` or a shared-chat Conversation from the app server | The Agents and Chat pages, or an agent form |
|
|
232
232
|
| Audience | `applications: [application]` | `groups`, `profiles` or `organization` |
|
|
233
233
|
|
|
234
234
|
A trigger form needs a trigger that people start, so an External application trigger cannot back it. One workflow can declare both triggers when the same work must start from both sides. The template's convert-lead workflow has only the External application trigger.
|
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.9.1",
|
|
4
4
|
"description": "Public hosted React contribution contracts for Collective Intelligence extensions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -80,12 +80,12 @@
|
|
|
80
80
|
"type-check": "bun check --no-script -p tsconfig.json && bun check --no-script -p tsconfig.type-tests.json"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@collective-intelligence/effect-xstate": "0.
|
|
84
|
-
"@collective-intelligence/extensions-sdk": "0.
|
|
85
|
-
"@collective-intelligence/extensions-sdk-frontend": "0.
|
|
83
|
+
"@collective-intelligence/effect-xstate": "0.9.1",
|
|
84
|
+
"@collective-intelligence/extensions-sdk": "0.9.1",
|
|
85
|
+
"@collective-intelligence/extensions-sdk-frontend": "0.9.1"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
|
-
"@collective-intelligence/ui-react": "0.
|
|
88
|
+
"@collective-intelligence/ui-react": "0.9.1",
|
|
89
89
|
"@tanstack/react-query": ">=5 <6",
|
|
90
90
|
"@xyflow/react": ">=12 <13",
|
|
91
91
|
"effect": "^4.1.0",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"access": "public"
|
|
103
103
|
},
|
|
104
104
|
"devDependencies": {
|
|
105
|
-
"@collective-intelligence/ui-react": "0.
|
|
105
|
+
"@collective-intelligence/ui-react": "0.9.1",
|
|
106
106
|
"@effect/eslint-plugin": "0.3.2",
|
|
107
107
|
"@effect/vitest": "4.1.4",
|
|
108
108
|
"@hey-api/openapi-ts": "^0.96.0",
|
|
@@ -130,5 +130,5 @@
|
|
|
130
130
|
"vitest": "5.0.2",
|
|
131
131
|
"xstate": "5.33.3"
|
|
132
132
|
},
|
|
133
|
-
"gitHead": "
|
|
133
|
+
"gitHead": "31d4f895460d3b7dbc111d97034124364d78d900"
|
|
134
134
|
}
|