@assistant-ui/store 0.2.11 → 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/dist/AuiIf.d.ts +19 -15
- package/dist/AuiIf.d.ts.map +1 -1
- package/dist/AuiIf.js +27 -24
- package/dist/AuiIf.js.map +1 -1
- package/dist/Derived.d.ts +15 -11
- package/dist/Derived.d.ts.map +1 -1
- package/dist/Derived.js +24 -20
- package/dist/Derived.js.map +1 -1
- package/dist/RenderChildrenWithAccessor.d.ts +13 -6
- package/dist/RenderChildrenWithAccessor.d.ts.map +1 -1
- package/dist/RenderChildrenWithAccessor.js +42 -48
- package/dist/RenderChildrenWithAccessor.js.map +1 -1
- package/dist/attachTransformScopes.d.ts +11 -10
- package/dist/attachTransformScopes.d.ts.map +1 -1
- package/dist/attachTransformScopes.js +20 -23
- package/dist/attachTransformScopes.js.map +1 -1
- package/dist/index.d.ts +15 -16
- package/dist/index.js +14 -18
- package/dist/tapClientList.d.ts +26 -23
- package/dist/tapClientList.d.ts.map +1 -1
- package/dist/tapClientList.js +66 -63
- package/dist/tapClientList.js.map +1 -1
- package/dist/tapClientLookup.d.ts +14 -11
- package/dist/tapClientLookup.d.ts.map +1 -1
- package/dist/tapClientLookup.js +31 -37
- package/dist/tapClientLookup.js.map +1 -1
- package/dist/tapClientResource.d.ts +12 -9
- package/dist/tapClientResource.d.ts.map +1 -1
- package/dist/tapClientResource.js +92 -104
- package/dist/tapClientResource.js.map +1 -1
- package/dist/types/client.d.ts +50 -42
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/client.js +0 -2
- package/dist/types/events.d.ts +23 -24
- package/dist/types/events.d.ts.map +1 -1
- package/dist/types/events.js +13 -6
- package/dist/types/events.js.map +1 -1
- package/dist/useAui.d.ts +17 -15
- package/dist/useAui.d.ts.map +1 -1
- package/dist/useAui.js +176 -191
- package/dist/useAui.js.map +1 -1
- package/dist/useAuiEvent.d.ts +6 -2
- package/dist/useAuiEvent.d.ts.map +1 -1
- package/dist/useAuiEvent.js +59 -47
- package/dist/useAuiEvent.js.map +1 -1
- package/dist/useAuiState.d.ts +6 -2
- package/dist/useAuiState.d.ts.map +1 -1
- package/dist/useAuiState.js +44 -42
- package/dist/useAuiState.js.map +1 -1
- package/dist/utils/BaseProxyHandler.d.ts +19 -16
- package/dist/utils/BaseProxyHandler.d.ts.map +1 -1
- package/dist/utils/BaseProxyHandler.js +46 -43
- package/dist/utils/BaseProxyHandler.js.map +1 -1
- package/dist/utils/NotificationManager.d.ts +13 -9
- package/dist/utils/NotificationManager.d.ts.map +1 -1
- package/dist/utils/NotificationManager.js +69 -82
- package/dist/utils/NotificationManager.js.map +1 -1
- package/dist/utils/proxied-assistant-state.d.ts +8 -4
- package/dist/utils/proxied-assistant-state.d.ts.map +1 -1
- package/dist/utils/proxied-assistant-state.js +28 -26
- package/dist/utils/proxied-assistant-state.js.map +1 -1
- package/dist/utils/react-assistant-context.d.ts +15 -10
- package/dist/utils/react-assistant-context.d.ts.map +1 -1
- package/dist/utils/react-assistant-context.js +69 -67
- package/dist/utils/react-assistant-context.js.map +1 -1
- package/dist/utils/splitClients.d.ts +12 -8
- package/dist/utils/splitClients.d.ts.map +1 -1
- package/dist/utils/splitClients.js +39 -43
- package/dist/utils/splitClients.js.map +1 -1
- package/dist/utils/tap-assistant-context.d.ts +17 -14
- package/dist/utils/tap-assistant-context.d.ts.map +1 -1
- package/dist/utils/tap-assistant-context.js +18 -15
- package/dist/utils/tap-assistant-context.js.map +1 -1
- package/dist/utils/tap-client-stack-context.d.ts +10 -6
- package/dist/utils/tap-client-stack-context.d.ts.map +1 -1
- package/dist/utils/tap-client-stack-context.js +22 -19
- package/dist/utils/tap-client-stack-context.js.map +1 -1
- package/dist/wrapperResource.d.ts +6 -2
- package/dist/wrapperResource.d.ts.map +1 -1
- package/dist/wrapperResource.js +12 -9
- package/dist/wrapperResource.js.map +1 -1
- package/package.json +7 -7
- package/src/types/client.ts +1 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/types/client.js.map +0 -1
package/dist/useAui.js
CHANGED
|
@@ -1,210 +1,195 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
2
|
+
import { getClientIndex } from "./utils/tap-client-stack-context.js";
|
|
3
|
+
import { tapClientResource } from "./tapClientResource.js";
|
|
4
|
+
import { PROXIED_ASSISTANT_STATE_SYMBOL, createProxiedAssistantState } from "./utils/proxied-assistant-state.js";
|
|
5
|
+
import { DefaultAssistantClient, createRootAssistantClient, useAssistantContextValue } from "./utils/react-assistant-context.js";
|
|
6
|
+
import { tapSplitClients } from "./utils/splitClients.js";
|
|
7
|
+
import { normalizeEventSelector } from "./types/events.js";
|
|
7
8
|
import { NotificationManager } from "./utils/NotificationManager.js";
|
|
8
9
|
import { withAssistantTapContextProvider } from "./utils/tap-assistant-context.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import { useResource } from "@assistant-ui/tap/react";
|
|
11
|
+
import { resource, tapEffect, tapMemo, tapRef, tapResource, tapResourceRoot, tapResources, withKey } from "@assistant-ui/tap";
|
|
12
|
+
//#region src/useAui.ts
|
|
12
13
|
const tapShallowMemoArray = (array) => {
|
|
13
|
-
|
|
14
|
-
return tapMemo(() => array, array);
|
|
14
|
+
return tapMemo(() => array, array);
|
|
15
15
|
};
|
|
16
|
-
const RootClientResource = resource(({ element, emit, clientRef
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const RootClientResource = resource(({ element, emit, clientRef }) => {
|
|
17
|
+
const { methods, state } = withAssistantTapContextProvider({
|
|
18
|
+
clientRef,
|
|
19
|
+
emit
|
|
20
|
+
}, () => tapClientResource(element));
|
|
21
|
+
return tapMemo(() => ({
|
|
22
|
+
state,
|
|
23
|
+
methods
|
|
24
|
+
}), [methods, state]);
|
|
19
25
|
});
|
|
20
|
-
const RootClientAccessorResource = resource(({ element, notifications, clientRef, name
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
26
|
+
const RootClientAccessorResource = resource(({ element, notifications, clientRef, name }) => {
|
|
27
|
+
const store = tapResourceRoot(RootClientResource({
|
|
28
|
+
element,
|
|
29
|
+
emit: notifications.emit,
|
|
30
|
+
clientRef
|
|
31
|
+
}));
|
|
32
|
+
tapEffect(() => {
|
|
33
|
+
return store.subscribe(notifications.notifySubscribers);
|
|
34
|
+
}, [store, notifications]);
|
|
35
|
+
return tapMemo(() => {
|
|
36
|
+
const clientFunction = () => store.getValue().methods;
|
|
37
|
+
Object.defineProperties(clientFunction, {
|
|
38
|
+
source: {
|
|
39
|
+
value: "root",
|
|
40
|
+
writable: false
|
|
41
|
+
},
|
|
42
|
+
query: {
|
|
43
|
+
value: {},
|
|
44
|
+
writable: false
|
|
45
|
+
},
|
|
46
|
+
name: {
|
|
47
|
+
value: name,
|
|
48
|
+
configurable: true
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return clientFunction;
|
|
52
|
+
}, [store, name]);
|
|
43
53
|
});
|
|
44
54
|
const NoOpRootClientsAccessorsResource = resource(() => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
return tapMemo(() => ({
|
|
56
|
+
clients: [],
|
|
57
|
+
subscribe: void 0,
|
|
58
|
+
on: void 0
|
|
59
|
+
}), []);
|
|
50
60
|
});
|
|
51
|
-
const RootClientsAccessorsResource = resource(({ clients: inputClients, clientRef
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
96
|
-
}, [results, notifications, clientRef]);
|
|
61
|
+
const RootClientsAccessorsResource = resource(({ clients: inputClients, clientRef }) => {
|
|
62
|
+
const notifications = tapResource(NotificationManager());
|
|
63
|
+
tapEffect(() => clientRef.parent.subscribe(notifications.notifySubscribers), [clientRef, notifications]);
|
|
64
|
+
const results = tapShallowMemoArray(tapResources(() => Object.keys(inputClients).map((key) => withKey(key, RootClientAccessorResource({
|
|
65
|
+
element: inputClients[key],
|
|
66
|
+
notifications,
|
|
67
|
+
clientRef,
|
|
68
|
+
name: key
|
|
69
|
+
}))), [
|
|
70
|
+
inputClients,
|
|
71
|
+
notifications,
|
|
72
|
+
clientRef
|
|
73
|
+
]));
|
|
74
|
+
return tapMemo(() => {
|
|
75
|
+
return {
|
|
76
|
+
clients: results,
|
|
77
|
+
subscribe: notifications.subscribe,
|
|
78
|
+
on: function(selector, callback) {
|
|
79
|
+
if (!this) throw new Error("const { on } = useAui() is not supported. Use aui.on() instead.");
|
|
80
|
+
const { scope, event } = normalizeEventSelector(selector);
|
|
81
|
+
if (scope !== "*") {
|
|
82
|
+
if (this[scope].source === null) throw new Error(`Scope "${scope}" is not available. Use { scope: "*", event: "${event}" } to listen globally.`);
|
|
83
|
+
}
|
|
84
|
+
const localUnsub = notifications.on(event, (payload, clientStack) => {
|
|
85
|
+
if (scope === "*") {
|
|
86
|
+
callback(payload);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const scopeClient = this[scope]();
|
|
90
|
+
if (scopeClient === clientStack[getClientIndex(scopeClient)]) callback(payload);
|
|
91
|
+
});
|
|
92
|
+
if (scope !== "*" && clientRef.parent[scope].source === null) return localUnsub;
|
|
93
|
+
const parentUnsub = clientRef.parent.on(selector, callback);
|
|
94
|
+
return () => {
|
|
95
|
+
localUnsub();
|
|
96
|
+
parentUnsub();
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, [
|
|
101
|
+
results,
|
|
102
|
+
notifications,
|
|
103
|
+
clientRef
|
|
104
|
+
]);
|
|
97
105
|
});
|
|
98
|
-
const DerivedClientAccessorResource = resource(({ element, clientRef, name
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
},
|
|
114
|
-
query: {
|
|
115
|
-
value: propsRef.current.query,
|
|
116
|
-
},
|
|
117
|
-
name: {
|
|
118
|
-
value: name,
|
|
119
|
-
configurable: true,
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
return clientFunction;
|
|
123
|
-
}, [clientRef, name]);
|
|
106
|
+
const DerivedClientAccessorResource = resource(({ element, clientRef, name }) => {
|
|
107
|
+
const propsRef = tapRef(element.props);
|
|
108
|
+
propsRef.current = element.props;
|
|
109
|
+
return tapMemo(() => {
|
|
110
|
+
const clientFunction = () => propsRef.current.get(clientRef.current);
|
|
111
|
+
Object.defineProperties(clientFunction, {
|
|
112
|
+
source: { value: propsRef.current.source },
|
|
113
|
+
query: { value: propsRef.current.query },
|
|
114
|
+
name: {
|
|
115
|
+
value: name,
|
|
116
|
+
configurable: true
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return clientFunction;
|
|
120
|
+
}, [clientRef, name]);
|
|
124
121
|
});
|
|
125
122
|
const serializeMeta = (name, meta) => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
queryKey = JSON.stringify(sorted);
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
queryKey = String(meta.query);
|
|
139
|
-
}
|
|
140
|
-
return `${name}::${meta.source}::${queryKey}`;
|
|
123
|
+
let queryKey;
|
|
124
|
+
try {
|
|
125
|
+
const sorted = {};
|
|
126
|
+
for (const k of Object.keys(meta.query).sort()) sorted[k] = meta.query[k];
|
|
127
|
+
queryKey = JSON.stringify(sorted);
|
|
128
|
+
} catch {
|
|
129
|
+
queryKey = String(meta.query);
|
|
130
|
+
}
|
|
131
|
+
return `${name}::${meta.source}::${queryKey}`;
|
|
141
132
|
};
|
|
142
|
-
const DerivedClientsAccessorsResource = resource(({ clients, clientRef
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
133
|
+
const DerivedClientsAccessorsResource = resource(({ clients, clientRef }) => {
|
|
134
|
+
return tapShallowMemoArray(tapResources(() => Object.keys(clients).map((key) => {
|
|
135
|
+
const name = key;
|
|
136
|
+
const element = clients[name];
|
|
137
|
+
return withKey(serializeMeta(name, element.props), DerivedClientAccessorResource({
|
|
138
|
+
element,
|
|
139
|
+
clientRef,
|
|
140
|
+
name
|
|
141
|
+
}));
|
|
142
|
+
}), [clients, clientRef]));
|
|
152
143
|
});
|
|
153
144
|
/**
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
clientRef.current = client;
|
|
192
|
-
}
|
|
193
|
-
return client;
|
|
145
|
+
* Resource that creates an extended AssistantClient.
|
|
146
|
+
*/
|
|
147
|
+
const AssistantClientResource = resource(({ parent, clients }) => {
|
|
148
|
+
const { rootClients, derivedClients } = tapSplitClients(clients, parent);
|
|
149
|
+
const clientRef = tapRef({
|
|
150
|
+
parent,
|
|
151
|
+
current: null
|
|
152
|
+
}).current;
|
|
153
|
+
tapEffect(() => {
|
|
154
|
+
clientRef.current = client;
|
|
155
|
+
});
|
|
156
|
+
const rootFields = tapResource(Object.keys(rootClients).length > 0 ? RootClientsAccessorsResource({
|
|
157
|
+
clients: rootClients,
|
|
158
|
+
clientRef
|
|
159
|
+
}) : NoOpRootClientsAccessorsResource());
|
|
160
|
+
const derivedFields = tapResource(DerivedClientsAccessorsResource({
|
|
161
|
+
clients: derivedClients,
|
|
162
|
+
clientRef
|
|
163
|
+
}));
|
|
164
|
+
const client = tapMemo(() => {
|
|
165
|
+
const proto = parent === DefaultAssistantClient ? createRootAssistantClient() : parent;
|
|
166
|
+
const client = Object.create(proto);
|
|
167
|
+
Object.assign(client, {
|
|
168
|
+
subscribe: rootFields.subscribe ?? parent.subscribe,
|
|
169
|
+
on: rootFields.on ?? parent.on,
|
|
170
|
+
[PROXIED_ASSISTANT_STATE_SYMBOL]: createProxiedAssistantState(client)
|
|
171
|
+
});
|
|
172
|
+
for (const field of rootFields.clients) client[field.name] = field;
|
|
173
|
+
for (const field of derivedFields) client[field.name] = field;
|
|
174
|
+
return client;
|
|
175
|
+
}, [
|
|
176
|
+
parent,
|
|
177
|
+
rootFields,
|
|
178
|
+
derivedFields
|
|
179
|
+
]);
|
|
180
|
+
if (clientRef.current === null) clientRef.current = client;
|
|
181
|
+
return client;
|
|
194
182
|
});
|
|
195
183
|
/** @deprecated This API is highly experimental and may be changed in a minor release */
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
clients,
|
|
204
|
-
}));
|
|
205
|
-
}
|
|
206
|
-
if (parent === null)
|
|
207
|
-
throw new Error("received null parent, this usage is not allowed");
|
|
208
|
-
return parent;
|
|
184
|
+
function useAui(clients, { parent } = { parent: useAssistantContextValue() }) {
|
|
185
|
+
if (clients) return useResource(AssistantClientResource({
|
|
186
|
+
parent: parent ?? DefaultAssistantClient,
|
|
187
|
+
clients
|
|
188
|
+
}));
|
|
189
|
+
if (parent === null) throw new Error("received null parent, this usage is not allowed");
|
|
190
|
+
return parent;
|
|
209
191
|
}
|
|
192
|
+
//#endregion
|
|
193
|
+
export { AssistantClientResource, useAui };
|
|
194
|
+
|
|
210
195
|
//# sourceMappingURL=useAui.js.map
|
package/dist/useAui.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAui.js","sourceRoot":"","sources":["../src/useAui.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EACL,QAAQ,EACR,OAAO,EACP,YAAY,EACZ,SAAS,EACT,MAAM,EACN,WAAW,EACX,OAAO,EACP,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAS3B,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,yBAAyB,GAC1B,2CAAwC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EAAE;YAC9B,CAAC,8BAA8B,CAAC,EAAE,2BAA2B,CAAC,MAAM,CAAC;SACtE,CAAC,CAAC;QAEH,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACtC,MAAc,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QACtC,CAAC;QACD,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE,CAAC;YACjC,MAAc,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,CAAC;IAExC,IAAI,SAAS,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;QAC/B,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC;IAC7B,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CACF,CAAC;AAyEF,wFAAwF;AACxF,MAAM,UAAU,MAAM,CACpB,OAAsB,EACtB,EAAE,MAAM,KAAyC;IAC/C,MAAM,EAAE,wBAAwB,EAAE;CACnC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,6FAA6F;QAC7F,OAAO,WAAW,CAChB,uBAAuB,CAAC;YACtB,MAAM,EAAE,MAAM,IAAI,sBAAsB;YACxC,OAAO;SACR,CAAC,CACH,CAAC;IACJ,CAAC;IACD,IAAI,MAAM,KAAK,IAAI;QACjB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
1
|
+
{"version":3,"file":"useAui.js","names":[],"sources":["../src/useAui.ts"],"sourcesContent":["\"use client\";\n\nimport { useResource } from \"@assistant-ui/tap/react\";\nimport {\n resource,\n tapMemo,\n tapResources,\n tapEffect,\n tapRef,\n tapResource,\n withKey,\n tapResourceRoot,\n} from \"@assistant-ui/tap\";\nimport type {\n AssistantClient,\n AssistantClientAccessor,\n ClientNames,\n ClientElement,\n ClientMeta,\n} from \"./types/client\";\nimport type { DerivedElement } from \"./Derived\";\nimport {\n useAssistantContextValue,\n DefaultAssistantClient,\n createRootAssistantClient,\n} from \"./utils/react-assistant-context\";\nimport {\n type DerivedClients,\n type RootClients,\n tapSplitClients,\n} from \"./utils/splitClients\";\nimport {\n normalizeEventSelector,\n type AssistantEventName,\n type AssistantEventCallback,\n type AssistantEventSelector,\n} from \"./types/events\";\nimport { NotificationManager } from \"./utils/NotificationManager\";\nimport { withAssistantTapContextProvider } from \"./utils/tap-assistant-context\";\nimport { tapClientResource } from \"./tapClientResource\";\nimport { getClientIndex } from \"./utils/tap-client-stack-context\";\nimport {\n PROXIED_ASSISTANT_STATE_SYMBOL,\n createProxiedAssistantState,\n} from \"./utils/proxied-assistant-state\";\n\nconst tapShallowMemoArray = <T>(array: readonly T[]) => {\n // biome-ignore lint/correctness/useExhaustiveDependencies: shallow memo\n return tapMemo(() => array, array);\n};\n\nconst RootClientResource = resource(\n <K extends ClientNames>({\n element,\n emit,\n clientRef,\n }: {\n element: ClientElement<K>;\n emit: NotificationManager[\"emit\"];\n clientRef: { parent: AssistantClient; current: AssistantClient | null };\n }) => {\n const { methods, state } = withAssistantTapContextProvider(\n { clientRef, emit },\n () => tapClientResource(element),\n );\n return tapMemo(() => ({ state, methods }), [methods, state]);\n },\n);\n\nconst RootClientAccessorResource = resource(\n <K extends ClientNames>({\n element,\n notifications,\n clientRef,\n name,\n }: {\n element: ClientElement<K>;\n notifications: NotificationManager;\n clientRef: { parent: AssistantClient; current: AssistantClient | null };\n name: K;\n }): AssistantClientAccessor<K> => {\n const store = tapResourceRoot(\n RootClientResource({ element, emit: notifications.emit, clientRef }),\n );\n\n tapEffect(() => {\n return store.subscribe(notifications.notifySubscribers);\n }, [store, notifications]);\n\n return tapMemo(() => {\n const clientFunction = () => store.getValue().methods;\n Object.defineProperties(clientFunction, {\n source: {\n value: \"root\" as const,\n writable: false,\n },\n query: {\n value: {} as Record<string, never>,\n writable: false,\n },\n name: {\n value: name,\n configurable: true,\n },\n });\n return clientFunction as AssistantClientAccessor<K>;\n }, [store, name]);\n },\n);\n\nconst NoOpRootClientsAccessorsResource = resource(() => {\n return tapMemo(\n () => ({\n clients: [] as AssistantClientAccessor<ClientNames>[],\n subscribe: undefined,\n on: undefined,\n }),\n [],\n );\n});\n\nconst RootClientsAccessorsResource = resource(\n ({\n clients: inputClients,\n clientRef,\n }: {\n clients: RootClients;\n clientRef: { parent: AssistantClient; current: AssistantClient | null };\n }) => {\n const notifications = tapResource(NotificationManager());\n\n tapEffect(\n () => clientRef.parent.subscribe(notifications.notifySubscribers),\n [clientRef, notifications],\n );\n\n const results = tapShallowMemoArray(\n tapResources(\n () =>\n Object.keys(inputClients).map((key) =>\n withKey(\n key,\n RootClientAccessorResource({\n element: inputClients[key as keyof typeof inputClients]!,\n notifications,\n clientRef,\n name: key as keyof typeof inputClients,\n }),\n ),\n ),\n [inputClients, notifications, clientRef],\n ),\n );\n\n return tapMemo(() => {\n return {\n clients: results,\n subscribe: notifications.subscribe,\n on: function <TEvent extends AssistantEventName>(\n this: AssistantClient,\n selector: AssistantEventSelector<TEvent>,\n callback: AssistantEventCallback<TEvent>,\n ) {\n if (!this) {\n throw new Error(\n \"const { on } = useAui() is not supported. Use aui.on() instead.\",\n );\n }\n\n const { scope, event } = normalizeEventSelector(selector);\n\n if (scope !== \"*\") {\n const source = this[scope as ClientNames].source;\n if (source === null) {\n throw new Error(\n `Scope \"${scope}\" is not available. Use { scope: \"*\", event: \"${event}\" } to listen globally.`,\n );\n }\n }\n\n const localUnsub = notifications.on(event, (payload, clientStack) => {\n if (scope === \"*\") {\n callback(payload);\n return;\n }\n\n const scopeClient = this[scope as ClientNames]();\n const index = getClientIndex(scopeClient);\n if (scopeClient === clientStack[index]) {\n callback(payload);\n }\n });\n if (\n scope !== \"*\" &&\n clientRef.parent[scope as ClientNames].source === null\n )\n return localUnsub;\n\n const parentUnsub = clientRef.parent.on(selector, callback);\n\n return () => {\n localUnsub();\n parentUnsub();\n };\n },\n };\n }, [results, notifications, clientRef]);\n },\n);\n\nconst DerivedClientAccessorResource = resource(\n <K extends ClientNames>({\n element,\n clientRef,\n name,\n }: {\n element: DerivedElement<K>;\n clientRef: { parent: AssistantClient; current: AssistantClient | null };\n name: K;\n }) => {\n // Track the latest props on a ref updated in render. The fiber is\n // keyed on the scope's meta by DerivedClientsAccessorsResource, so\n // source/query are stable for this fiber's lifetime and the only\n // value that can change between renders for the same fiber is the\n // identity of the `get` closure. Routing reads through the ref\n // avoids the one-commit lag that the previous `tapEffectEvent`\n // path imposed.\n const propsRef = tapRef(element.props);\n propsRef.current = element.props;\n\n return tapMemo(() => {\n const clientFunction = () => propsRef.current.get(clientRef.current!);\n Object.defineProperties(clientFunction, {\n source: {\n value: propsRef.current.source,\n },\n query: {\n value: propsRef.current.query,\n },\n name: {\n value: name,\n configurable: true,\n },\n });\n return clientFunction as AssistantClientAccessor<K>;\n }, [clientRef, name]);\n },\n);\n\nconst serializeMeta = <K extends ClientNames>(\n name: K,\n meta: ClientMeta<K>,\n): string => {\n // Sort top-level keys so {a, b} and {b, a} hash to the same fiber\n // identity, and guard JSON.stringify against unusual values (BigInt,\n // circular refs) so render never throws here.\n let queryKey: string;\n try {\n const sorted: Record<string, unknown> = {};\n for (const k of Object.keys(meta.query as object).sort()) {\n sorted[k] = (meta.query as Record<string, unknown>)[k];\n }\n queryKey = JSON.stringify(sorted);\n } catch {\n queryKey = String(meta.query);\n }\n return `${name}::${meta.source}::${queryKey}`;\n};\n\nconst DerivedClientsAccessorsResource = resource(\n ({\n clients,\n clientRef,\n }: {\n clients: DerivedClients;\n clientRef: { parent: AssistantClient; current: AssistantClient | null };\n }) => {\n return tapShallowMemoArray(\n tapResources(\n () =>\n Object.keys(clients).map((key) => {\n const name = key as keyof typeof clients;\n const element = clients[name]!;\n return withKey(\n serializeMeta(name, element.props),\n DerivedClientAccessorResource({\n element,\n clientRef,\n name,\n }),\n );\n }),\n [clients, clientRef],\n ),\n );\n },\n);\n\n/**\n * Resource that creates an extended AssistantClient.\n */\nexport const AssistantClientResource = resource(\n ({\n parent,\n clients,\n }: {\n parent: AssistantClient;\n clients: useAui.Props;\n }): AssistantClient => {\n const { rootClients, derivedClients } = tapSplitClients(clients, parent);\n\n const clientRef = tapRef({\n parent: parent,\n current: null as AssistantClient | null,\n }).current;\n\n tapEffect(() => {\n // if (clientRef.current && clientRef.current !== client)\n // throw new Error(\"clientRef.current !== client\");\n\n clientRef.current = client;\n });\n\n const rootFields = tapResource(\n Object.keys(rootClients).length > 0\n ? RootClientsAccessorsResource({ clients: rootClients, clientRef })\n : NoOpRootClientsAccessorsResource(),\n );\n\n const derivedFields = tapResource(\n DerivedClientsAccessorsResource({ clients: derivedClients, clientRef }),\n );\n\n const client = tapMemo(() => {\n // Swap DefaultAssistantClient -> createRootAssistantClient at root to change error message\n const proto =\n parent === DefaultAssistantClient\n ? createRootAssistantClient()\n : parent;\n\n const client = Object.create(proto) as AssistantClient;\n Object.assign(client, {\n subscribe: rootFields.subscribe ?? parent.subscribe,\n on: rootFields.on ?? parent.on,\n [PROXIED_ASSISTANT_STATE_SYMBOL]: createProxiedAssistantState(client),\n });\n\n for (const field of rootFields.clients) {\n (client as any)[field.name] = field;\n }\n for (const field of derivedFields) {\n (client as any)[field.name] = field;\n }\n\n return client;\n }, [parent, rootFields, derivedFields]);\n\n if (clientRef.current === null) {\n clientRef.current = client;\n }\n\n return client;\n },\n);\n\nexport namespace useAui {\n export type Props = {\n [K in ClientNames]?: ClientElement<K> | DerivedElement<K>;\n };\n}\n\n/**\n * Returns the current `AssistantClient` from context.\n *\n * Read the client supplied by the nearest {@link AuiProvider} or\n * {@link AssistantRuntimeProvider}, then access a scope on it —\n * `aui.thread()`, `aui.composer()`, `aui.message()`, and so on. Pair\n * with {@link useAuiState} to read reactive state and {@link useAuiEvent}\n * to subscribe to events. The returned client also exposes lower-level\n * methods such as `aui.on(...)` and `aui.subscribe(...)`; prefer\n * `useAuiEvent` for React event subscriptions.\n *\n * Rendered outside a provider, the returned client's scope accessors\n * throw a descriptive error whenever they are called.\n *\n * @example\n * ```tsx\n * const aui = useAui();\n *\n * const onSend = () => aui.composer().send();\n * const onCancel = () => aui.thread().cancelRun();\n * ```\n *\n * @example\n * ```tsx\n * // Combine with useAuiState to drive disabled state.\n * const aui = useAui();\n * const isRunning = useAuiState((s) => s.thread.isRunning);\n *\n * return (\n * <button disabled={isRunning} onClick={() => aui.composer().send()}>\n * Send\n * </button>\n * );\n * ```\n */\nexport function useAui(): AssistantClient;\n/**\n * Extends the parent `AssistantClient` with additional scopes.\n *\n * Advanced overload used when building primitives or providers — for example,\n * when a custom provider needs to register a `message`, `part`, or other scope\n * onto the client visible to its descendants. Application code rarely reaches\n * for this; use {@link useAui} with no arguments to read the existing client.\n *\n * @example\n * ```tsx\n * const aui = useAui({\n * message: Derived({\n * source: \"thread\",\n * query: { index: 0 },\n * get: (aui) => aui.thread().message({ index: 0 }),\n * }),\n * });\n *\n * const role = useAuiState((s) => s.message.role);\n * ```\n */\nexport function useAui(clients: useAui.Props): AssistantClient;\n/**\n * Extends an explicit parent `AssistantClient` with additional scopes.\n */\nexport function useAui(\n clients: useAui.Props,\n config: { parent: null | AssistantClient },\n): AssistantClient;\n/** @deprecated This API is highly experimental and may be changed in a minor release */\nexport function useAui(\n clients?: useAui.Props,\n { parent }: { parent: null | AssistantClient } = {\n parent: useAssistantContextValue(),\n },\n): AssistantClient {\n if (clients) {\n // biome-ignore lint/correctness/useHookAtTopLevel: intentional conditional/nested hook usage\n return useResource(\n AssistantClientResource({\n parent: parent ?? DefaultAssistantClient,\n clients,\n }),\n );\n }\n if (parent === null)\n throw new Error(\"received null parent, this usage is not allowed\");\n return parent;\n}\n"],"mappings":";;;;;;;;;;;;AA8CA,MAAM,uBAA0B,UAAwB;CAEtD,OAAO,cAAc,OAAO,KAAK;AACnC;AAEA,MAAM,qBAAqB,UACD,EACtB,SACA,MACA,gBAKI;CAC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,SAAgB,OACd,SACA,EAAE,WAA+C,EAC/C,QAAQ,yBAAyB,EACnC,GACiB;CACjB,IAAI,SAEF,OAAO,YACL,wBAAwB;EACtB,QAAQ,UAAU;EAClB;CACF,CAAC,CACH;CAEF,IAAI,WAAW,MACb,MAAM,IAAI,MAAM,iDAAiD;CACnE,OAAO;AACT"}
|
package/dist/useAuiEvent.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AssistantEventCallback, AssistantEventName, AssistantEventSelector } from "./types/events.js";
|
|
2
|
+
|
|
3
|
+
//#region src/useAuiEvent.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Subscribes to an assistant event for the lifetime of the component.
|
|
4
6
|
*
|
|
@@ -40,5 +42,7 @@ import type { AssistantEventName, AssistantEventCallback, AssistantEventSelector
|
|
|
40
42
|
* });
|
|
41
43
|
* ```
|
|
42
44
|
*/
|
|
43
|
-
|
|
45
|
+
declare const useAuiEvent: <TEvent extends AssistantEventName>(selector: AssistantEventSelector<TEvent>, callback: AssistantEventCallback<TEvent>) => void;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { useAuiEvent };
|
|
44
48
|
//# sourceMappingURL=useAuiEvent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuiEvent.d.ts","
|
|
1
|
+
{"version":3,"file":"useAuiEvent.d.ts","names":[],"sources":["../src/useAuiEvent.ts"],"mappings":";;;;;AAmDA;;;;;;;;;;;;;;;;;;;AAE0C;;;;;;;;;;;;;;;;;;;;cAF7B,WAAA,kBAA8B,kBAAA,EACzC,QAAA,EAAU,sBAAA,CAAuB,MAAA,GACjC,QAAA,EAAU,sBAAA,CAAuB,MAAA"}
|
package/dist/useAuiEvent.js
CHANGED
|
@@ -1,52 +1,64 @@
|
|
|
1
|
+
import { normalizeEventSelector } from "./types/events.js";
|
|
2
|
+
import { useAui } from "./useAui.js";
|
|
1
3
|
import { useEffect } from "react";
|
|
2
4
|
import { useEffectEvent } from "use-effect-event";
|
|
3
|
-
|
|
4
|
-
import { normalizeEventSelector } from "./types/events.js";
|
|
5
|
+
//#region src/useAuiEvent.ts
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
7
|
+
* Subscribes to an assistant event for the lifetime of the component.
|
|
8
|
+
*
|
|
9
|
+
* The subscription is established on mount and re-established whenever the
|
|
10
|
+
* scope or event name changes. The `callback` is wrapped in an effect-event
|
|
11
|
+
* shim, so the latest closure is invoked on each emission — you do not
|
|
12
|
+
* need to memoize it.
|
|
13
|
+
*
|
|
14
|
+
* @param selector - Either a dotted event name like
|
|
15
|
+
* `"thread.modelContextUpdate"` or an object `{ scope, event }`. Use
|
|
16
|
+
* `scope: "*"` to subscribe at the root client and receive emissions
|
|
17
|
+
* from any descendant scope, regardless of which one is in React
|
|
18
|
+
* context.
|
|
19
|
+
* @param callback - Invoked with the event payload. The most recent
|
|
20
|
+
* reference is always called. Return values are ignored, async callbacks
|
|
21
|
+
* are not awaited, and the callback cannot be called during render.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* // React to transient model-context changes.
|
|
26
|
+
* useAuiEvent("thread.modelContextUpdate", ({ threadId }) => {
|
|
27
|
+
* analytics.track("model_context_update", { threadId });
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* // React to thread switches.
|
|
34
|
+
* useAuiEvent("threadListItem.switchedTo", () => {
|
|
35
|
+
* resetLocalState();
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Listen from the root client rather than the current React context.
|
|
42
|
+
* useAuiEvent({ scope: "*", event: "thread.modelContextUpdate" }, (payload) => {
|
|
43
|
+
* analytics.track("model_context_update", payload);
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
const useAuiEvent = (selector, callback) => {
|
|
48
|
+
const aui = useAui();
|
|
49
|
+
const callbackRef = useEffectEvent(callback);
|
|
50
|
+
const { scope, event } = normalizeEventSelector(selector);
|
|
51
|
+
useEffect(() => aui.on({
|
|
52
|
+
scope,
|
|
53
|
+
event
|
|
54
|
+
}, callbackRef), [
|
|
55
|
+
aui,
|
|
56
|
+
scope,
|
|
57
|
+
event,
|
|
58
|
+
callbackRef
|
|
59
|
+
]);
|
|
51
60
|
};
|
|
61
|
+
//#endregion
|
|
62
|
+
export { useAuiEvent };
|
|
63
|
+
|
|
52
64
|
//# sourceMappingURL=useAuiEvent.js.map
|
package/dist/useAuiEvent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuiEvent.js","
|
|
1
|
+
{"version":3,"file":"useAuiEvent.js","names":[],"sources":["../src/useAuiEvent.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport { useEffectEvent } from \"use-effect-event\";\nimport { useAui } from \"./useAui\";\nimport type {\n AssistantEventName,\n AssistantEventCallback,\n AssistantEventSelector,\n} from \"./types/events\";\nimport { normalizeEventSelector } from \"./types/events\";\n\n/**\n * Subscribes to an assistant event for the lifetime of the component.\n *\n * The subscription is established on mount and re-established whenever the\n * scope or event name changes. The `callback` is wrapped in an effect-event\n * shim, so the latest closure is invoked on each emission — you do not\n * need to memoize it.\n *\n * @param selector - Either a dotted event name like\n * `\"thread.modelContextUpdate\"` or an object `{ scope, event }`. Use\n * `scope: \"*\"` to subscribe at the root client and receive emissions\n * from any descendant scope, regardless of which one is in React\n * context.\n * @param callback - Invoked with the event payload. The most recent\n * reference is always called. Return values are ignored, async callbacks\n * are not awaited, and the callback cannot be called during render.\n *\n * @example\n * ```tsx\n * // React to transient model-context changes.\n * useAuiEvent(\"thread.modelContextUpdate\", ({ threadId }) => {\n * analytics.track(\"model_context_update\", { threadId });\n * });\n * ```\n *\n * @example\n * ```tsx\n * // React to thread switches.\n * useAuiEvent(\"threadListItem.switchedTo\", () => {\n * resetLocalState();\n * });\n * ```\n *\n * @example\n * ```tsx\n * // Listen from the root client rather than the current React context.\n * useAuiEvent({ scope: \"*\", event: \"thread.modelContextUpdate\" }, (payload) => {\n * analytics.track(\"model_context_update\", payload);\n * });\n * ```\n */\nexport const useAuiEvent = <TEvent extends AssistantEventName>(\n selector: AssistantEventSelector<TEvent>,\n callback: AssistantEventCallback<TEvent>,\n) => {\n const aui = useAui();\n const callbackRef = useEffectEvent(callback);\n\n const { scope, event } = normalizeEventSelector(selector);\n useEffect(\n () => aui.on({ scope, event }, callbackRef),\n [aui, scope, event, callbackRef],\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,MAAa,eACX,UACA,aACG;CACH,MAAM,MAAM,OAAO;CACnB,MAAM,cAAc,eAAe,QAAQ;CAE3C,MAAM,EAAE,OAAO,UAAU,uBAAuB,QAAQ;CACxD,gBACQ,IAAI,GAAG;EAAE;EAAO;CAAM,GAAG,WAAW,GAC1C;EAAC;EAAK;EAAO;EAAO;CAAW,CACjC;AACF"}
|
package/dist/useAuiState.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AssistantState } from "./types/client.js";
|
|
2
|
+
|
|
3
|
+
//#region src/useAuiState.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Subscribes to a slice of {@link AssistantState} and re-renders the
|
|
4
6
|
* component whenever that slice changes.
|
|
@@ -31,5 +33,7 @@ import type { AssistantState } from "./types/client.js";
|
|
|
31
33
|
* const canSend = useAuiState((s) => s.composer.canSend);
|
|
32
34
|
* ```
|
|
33
35
|
*/
|
|
34
|
-
|
|
36
|
+
declare const useAuiState: <T>(selector: (state: AssistantState) => T) => T;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useAuiState };
|
|
35
39
|
//# sourceMappingURL=useAuiState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuiState.d.ts","
|
|
1
|
+
{"version":3,"file":"useAuiState.d.ts","names":[],"sources":["../src/useAuiState.ts"],"mappings":";;;;;AAqCA;;;;;;;;;;;;;;;AAmBC;;;;;;;;;;;;;;;cAnBY,WAAA,MAAkB,QAAA,GAAW,KAAA,EAAO,cAAA,KAAmB,CAAA,KAAI,CAAA"}
|