@assistant-ui/store 0.2.0 → 0.2.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/dist/AuiIf.d.ts +1 -1
- package/dist/AuiIf.d.ts.map +1 -1
- package/dist/Derived.d.ts +34 -0
- package/dist/Derived.d.ts.map +1 -0
- package/dist/Derived.js +24 -0
- package/dist/Derived.js.map +1 -0
- package/dist/attachTransformScopes.d.ts +11 -0
- package/dist/attachTransformScopes.d.ts.map +1 -0
- package/dist/attachTransformScopes.js +12 -0
- package/dist/attachTransformScopes.js.map +1 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -4
- package/dist/index.js.map +1 -1
- package/dist/tapClientList.d.ts +28 -0
- package/dist/tapClientList.d.ts.map +1 -0
- package/dist/tapClientList.js +68 -0
- package/dist/tapClientList.js.map +1 -0
- package/dist/tapClientLookup.d.ts +15 -0
- package/dist/tapClientLookup.d.ts.map +1 -0
- package/dist/tapClientLookup.js +42 -0
- package/dist/tapClientLookup.js.map +1 -0
- package/dist/tapClientResource.d.ts +13 -0
- package/dist/tapClientResource.d.ts.map +1 -0
- package/dist/tapClientResource.js +114 -0
- package/dist/tapClientResource.js.map +1 -0
- package/dist/types/client.d.ts +115 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +2 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/events.d.ts +33 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +8 -0
- package/dist/types/events.js.map +1 -0
- package/dist/useAui.d.ts +5 -3
- package/dist/useAui.d.ts.map +1 -1
- package/dist/useAui.js +7 -2
- package/dist/useAui.js.map +1 -1
- package/dist/useAuiEvent.d.ts +1 -1
- package/dist/useAuiEvent.d.ts.map +1 -1
- package/dist/useAuiEvent.js +1 -1
- package/dist/useAuiEvent.js.map +1 -1
- package/dist/useAuiState.d.ts +2 -2
- package/dist/useAuiState.d.ts.map +1 -1
- package/dist/useAuiState.js +2 -2
- package/dist/useAuiState.js.map +1 -1
- package/dist/utils/BaseProxyHandler.d.ts +23 -0
- package/dist/utils/BaseProxyHandler.d.ts.map +1 -0
- package/dist/utils/BaseProxyHandler.js +46 -0
- package/dist/utils/BaseProxyHandler.js.map +1 -0
- package/dist/utils/NotificationManager.d.ts +11 -0
- package/dist/utils/NotificationManager.d.ts.map +1 -0
- package/dist/utils/NotificationManager.js +84 -0
- package/dist/utils/NotificationManager.js.map +1 -0
- package/dist/utils/proxied-assistant-state.d.ts +8 -0
- package/dist/utils/proxied-assistant-state.d.ts.map +1 -0
- package/dist/utils/proxied-assistant-state.js +34 -0
- package/dist/utils/proxied-assistant-state.js.map +1 -0
- package/dist/utils/react-assistant-context.d.ts +1 -1
- package/dist/utils/react-assistant-context.d.ts.map +1 -1
- package/dist/utils/react-assistant-context.js +2 -1
- package/dist/utils/react-assistant-context.js.map +1 -1
- package/dist/utils/splitClients.d.ts +10 -0
- package/dist/utils/splitClients.d.ts.map +1 -0
- package/dist/utils/splitClients.js +53 -0
- package/dist/utils/splitClients.js.map +1 -0
- package/dist/utils/tap-assistant-context.d.ts +19 -0
- package/dist/utils/tap-assistant-context.d.ts.map +1 -0
- package/dist/utils/tap-assistant-context.js +23 -0
- package/dist/utils/tap-assistant-context.js.map +1 -0
- package/dist/utils/tap-client-stack-context.d.ts +23 -0
- package/dist/utils/tap-client-stack-context.d.ts.map +1 -0
- package/dist/utils/tap-client-stack-context.js +28 -0
- package/dist/utils/tap-client-stack-context.js.map +1 -0
- package/dist/wrapperResource.d.ts +3 -0
- package/dist/wrapperResource.d.ts.map +1 -0
- package/dist/wrapperResource.js +11 -0
- package/dist/wrapperResource.js.map +1 -0
- package/package.json +5 -5
- package/src/AuiIf.ts +1 -1
- package/src/Derived.ts +46 -0
- package/src/__tests__/hooks.test.tsx +1 -1
- package/src/attachTransformScopes.ts +38 -0
- package/src/index.ts +37 -21
- package/src/tapClientList.ts +121 -0
- package/src/tapClientLookup.ts +79 -0
- package/src/tapClientResource.ts +187 -0
- package/src/types/client.ts +180 -0
- package/src/types/events.ts +77 -0
- package/src/useAui.ts +21 -19
- package/src/useAuiEvent.ts +2 -2
- package/src/useAuiState.ts +3 -3
- package/src/utils/BaseProxyHandler.ts +50 -0
- package/src/utils/NotificationManager.ts +114 -0
- package/src/utils/proxied-assistant-state.ts +53 -0
- package/src/utils/react-assistant-context.tsx +3 -7
- package/src/utils/splitClients.ts +80 -0
- package/src/utils/tap-assistant-context.ts +58 -0
- package/src/utils/tap-client-stack-context.ts +51 -0
- package/src/wrapperResource.ts +17 -0
- package/dist/scope-registry-forward.d.ts +0 -6
- package/dist/scope-registry-forward.d.ts.map +0 -1
- package/dist/scope-registry-forward.js +0 -2
- package/dist/scope-registry-forward.js.map +0 -1
- package/dist/scope-registry.d.ts +0 -17
- package/dist/scope-registry.d.ts.map +0 -1
- package/dist/scope-registry.js +0 -2
- package/dist/scope-registry.js.map +0 -1
- package/src/scope-registry-forward.ts +0 -6
- package/src/scope-registry.ts +0 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tap-assistant-context.js","sourceRoot":"","sources":["../../src/utils/tap-assistant-context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,GAAG,EACH,mBAAmB,EACnB,cAAc,GACf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,cAAc,EAAoB,sCAAmC;AAa9E,MAAM,mBAAmB,GACvB,qBAAqB,CAAkC,IAAI,CAAC,CAAC;AAE/D,MAAM,CAAC,MAAM,+BAA+B,GAAG,CAC7C,KAA+B,EAC/B,EAAiB,EACjB,EAAE;IACF,OAAO,mBAAmB,CAAC,mBAAmB,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;AAC7D,CAAC,CAAC;AAEF,MAAM,sBAAsB,GAAG,GAAG,EAAE;IAClC,MAAM,GAAG,GAAG,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACrC,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAElE,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,EAAE;IACxC,OAAO,sBAAsB,EAAE,CAAC,SAAS,CAAC;AAC5C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,MAAM,EAAE,IAAI,EAAE,GAAG,sBAAsB,EAAE,CAAC;IAC1C,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IAErC,OAAO,cAAc,CACnB,CACE,KAAa,EACb,OAAsC,EACtC,EAAE;QACF,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;IACpC,CAAC,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ClientMethods } from "../types/client.js";
|
|
2
|
+
/**
|
|
3
|
+
* Symbol used to get the client index from a ClientProxy.
|
|
4
|
+
*/
|
|
5
|
+
export declare const SYMBOL_CLIENT_INDEX: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* Get the index of a client (its position in the client stack when created).
|
|
8
|
+
*/
|
|
9
|
+
export declare const getClientIndex: (client: ClientMethods) => number;
|
|
10
|
+
/**
|
|
11
|
+
* The client stack - an array of clients representing the current hierarchy.
|
|
12
|
+
*/
|
|
13
|
+
export type ClientStack = readonly ClientMethods[];
|
|
14
|
+
/**
|
|
15
|
+
* Get the current client stack inside a tap resource.
|
|
16
|
+
*/
|
|
17
|
+
export declare const tapClientStack: () => ClientStack;
|
|
18
|
+
/**
|
|
19
|
+
* Execute a callback with a client pushed onto the stack.
|
|
20
|
+
* The stack is duplicated, not mutated.
|
|
21
|
+
*/
|
|
22
|
+
export declare const tapWithClientStack: <T>(client: ClientMethods, callback: () => T) => T;
|
|
23
|
+
//# sourceMappingURL=tap-client-stack-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tap-client-stack-context.d.ts","sourceRoot":"","sources":["../../src/utils/tap-client-stack-context.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,2BAAwB;AAErD;;GAEG;AACH,eAAO,MAAM,mBAAmB,eAA2C,CAAC;AAE5E;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,aAAa,KAAG,MAItD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,SAAS,aAAa,EAAE,CAAC;AAInD;;GAEG;AACH,eAAO,MAAM,cAAc,QAAO,WAEjC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAClC,QAAQ,aAAa,EACrB,UAAU,MAAM,CAAC,KAChB,CAOF,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createResourceContext, tap, withContextProvider, tapMemo, } from "@assistant-ui/tap";
|
|
2
|
+
/**
|
|
3
|
+
* Symbol used to get the client index from a ClientProxy.
|
|
4
|
+
*/
|
|
5
|
+
export const SYMBOL_CLIENT_INDEX = Symbol("assistant-ui.store.clientIndex");
|
|
6
|
+
/**
|
|
7
|
+
* Get the index of a client (its position in the client stack when created).
|
|
8
|
+
*/
|
|
9
|
+
export const getClientIndex = (client) => {
|
|
10
|
+
return client[SYMBOL_CLIENT_INDEX];
|
|
11
|
+
};
|
|
12
|
+
const ClientStackContext = createResourceContext([]);
|
|
13
|
+
/**
|
|
14
|
+
* Get the current client stack inside a tap resource.
|
|
15
|
+
*/
|
|
16
|
+
export const tapClientStack = () => {
|
|
17
|
+
return tap(ClientStackContext);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Execute a callback with a client pushed onto the stack.
|
|
21
|
+
* The stack is duplicated, not mutated.
|
|
22
|
+
*/
|
|
23
|
+
export const tapWithClientStack = (client, callback) => {
|
|
24
|
+
const currentStack = tapClientStack();
|
|
25
|
+
const newStack = tapMemo(() => [...currentStack, client], [currentStack, client]);
|
|
26
|
+
return withContextProvider(ClientStackContext, newStack, callback);
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=tap-client-stack-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tap-client-stack-context.js","sourceRoot":"","sources":["../../src/utils/tap-client-stack-context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,GAAG,EACH,mBAAmB,EACnB,OAAO,GACR,MAAM,mBAAmB,CAAC;AAG3B;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,gCAAgC,CAAC,CAAC;AAE5E;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,MAAqB,EAAU,EAAE;IAC9D,OAAQ,MAAuD,CAC7D,mBAAmB,CACpB,CAAC;AACJ,CAAC,CAAC;AAOF,MAAM,kBAAkB,GAAG,qBAAqB,CAAc,EAAE,CAAC,CAAC;AAElE;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAgB,EAAE;IAC9C,OAAO,GAAG,CAAC,kBAAkB,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,MAAqB,EACrB,QAAiB,EACd,EAAE;IACL,MAAM,YAAY,GAAG,cAAc,EAAE,CAAC;IACtC,MAAM,QAAQ,GAAG,OAAO,CACtB,GAAG,EAAE,CAAC,CAAC,GAAG,YAAY,EAAE,MAAM,CAAC,EAC/B,CAAC,YAAY,EAAE,MAAM,CAAC,CACvB,CAAC;IACF,OAAO,mBAAmB,CAAC,kBAAkB,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACrE,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapperResource.d.ts","sourceRoot":"","sources":["../src/wrapperResource.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,eAAe,EACpB,QAAQ,EAGT,MAAM,mBAAmB,CAAC;AAE3B,eAAO,MAAM,eAAe,GAAI,CAAC,EAAE,CAAC,EAClC,IAAI,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,CAAC,KACnC,QAAQ,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,CAOhC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { resource, withKey, } from "@assistant-ui/tap";
|
|
2
|
+
export const wrapperResource = (fn) => {
|
|
3
|
+
const res = resource(fn);
|
|
4
|
+
return (props) => {
|
|
5
|
+
const el = res(props);
|
|
6
|
+
if (props.key === undefined)
|
|
7
|
+
return el;
|
|
8
|
+
return withKey(props.key, el);
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=wrapperResource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapperResource.js","sourceRoot":"","sources":["../src/wrapperResource.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,QAAQ,EACR,OAAO,GACR,MAAM,mBAAmB,CAAC;AAE3B,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,EAAoC,EACH,EAAE;IACnC,MAAM,GAAG,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IACzB,OAAO,CAAC,KAAyB,EAAE,EAAE;QACnC,MAAM,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;QACtB,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS;YAAE,OAAO,EAAE,CAAC;QACvC,OAAO,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@assistant-ui/store",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Tap-based state management for @assistant-ui",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state-management",
|
|
@@ -28,11 +28,10 @@
|
|
|
28
28
|
],
|
|
29
29
|
"sideEffects": false,
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@assistant-ui/core": "^0.1.0",
|
|
32
|
-
"@assistant-ui/tap": "^0.5.0",
|
|
33
31
|
"use-effect-event": "^2.0.3"
|
|
34
32
|
},
|
|
35
33
|
"peerDependencies": {
|
|
34
|
+
"@assistant-ui/tap": "^0.5.1",
|
|
36
35
|
"@types/react": "*",
|
|
37
36
|
"react": "^18 || ^19"
|
|
38
37
|
},
|
|
@@ -43,11 +42,12 @@
|
|
|
43
42
|
},
|
|
44
43
|
"devDependencies": {
|
|
45
44
|
"@testing-library/react": "^16.3.2",
|
|
46
|
-
"@types/react": "^19.2.
|
|
45
|
+
"@types/react": "^19.2.14",
|
|
47
46
|
"@types/react-dom": "^19.2.3",
|
|
48
|
-
"jsdom": "^28.
|
|
47
|
+
"jsdom": "^28.1.0",
|
|
49
48
|
"vitest": "^4.0.18",
|
|
50
49
|
"react": "^19.2.4",
|
|
50
|
+
"@assistant-ui/tap": "0.5.1",
|
|
51
51
|
"@assistant-ui/x-buildutils": "0.0.1"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
package/src/AuiIf.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type { FC, PropsWithChildren } from "react";
|
|
4
4
|
import { useAuiState } from "./useAuiState";
|
|
5
|
-
import type { AssistantState } from "
|
|
5
|
+
import type { AssistantState } from "./types/client";
|
|
6
6
|
|
|
7
7
|
export namespace AuiIf {
|
|
8
8
|
export type Props = PropsWithChildren<{ condition: AuiIf.Condition }>;
|
package/src/Derived.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { resource, ResourceElement } from "@assistant-ui/tap";
|
|
2
|
+
import type {
|
|
3
|
+
AssistantClient,
|
|
4
|
+
ClientNames,
|
|
5
|
+
AssistantClientAccessor,
|
|
6
|
+
ClientMeta,
|
|
7
|
+
} from "./types/client";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a derived client field that references a client from a parent scope.
|
|
11
|
+
* The get callback always calls the most recent version (useEffectEvent pattern).
|
|
12
|
+
*
|
|
13
|
+
* IMPORTANT: The `get` callback must return a client that was created via
|
|
14
|
+
* `tapClientResource` (or `tapClientLookup`/`tapClientList` which use it internally).
|
|
15
|
+
* This is required for event scoping to work correctly.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const aui = useAui({
|
|
20
|
+
* message: Derived({
|
|
21
|
+
* source: "thread",
|
|
22
|
+
* query: { index: 0 },
|
|
23
|
+
* get: (aui) => aui.thread().message({ index: 0 }),
|
|
24
|
+
* }),
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const Derived = resource(
|
|
29
|
+
<K extends ClientNames>(_config: Derived.Props<K>): null => {
|
|
30
|
+
return null;
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export type DerivedElement<K extends ClientNames> = ResourceElement<
|
|
35
|
+
null,
|
|
36
|
+
Derived.Props<K>
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
export namespace Derived {
|
|
40
|
+
/**
|
|
41
|
+
* Props passed to a derived client resource element.
|
|
42
|
+
*/
|
|
43
|
+
export type Props<K extends ClientNames> = {
|
|
44
|
+
get: (client: AssistantClient) => ReturnType<AssistantClientAccessor<K>>;
|
|
45
|
+
} & (ClientMeta<K> | { getMeta: (client: AssistantClient) => ClientMeta<K> });
|
|
46
|
+
}
|
|
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
6
6
|
import { AuiProvider } from "../utils/react-assistant-context";
|
|
7
7
|
import { useAuiState } from "../useAuiState";
|
|
8
8
|
import { useAuiEvent } from "../useAuiEvent";
|
|
9
|
-
import { PROXIED_ASSISTANT_STATE_SYMBOL } from "
|
|
9
|
+
import { PROXIED_ASSISTANT_STATE_SYMBOL } from "../utils/proxied-assistant-state";
|
|
10
10
|
|
|
11
11
|
afterEach(() => {
|
|
12
12
|
vi.restoreAllMocks();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ResourceElement } from "@assistant-ui/tap";
|
|
2
|
+
import type {
|
|
3
|
+
AssistantClient,
|
|
4
|
+
ClientElement,
|
|
5
|
+
ClientNames,
|
|
6
|
+
} from "./types/client";
|
|
7
|
+
import type { DerivedElement } from "./Derived";
|
|
8
|
+
|
|
9
|
+
const TRANSFORM_SCOPES = Symbol("assistant-ui.transform-scopes");
|
|
10
|
+
|
|
11
|
+
export type ScopesConfig = {
|
|
12
|
+
[K in ClientNames]?: ClientElement<K> | DerivedElement<K>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type TransformScopesFn = (
|
|
16
|
+
scopes: ScopesConfig,
|
|
17
|
+
parent: AssistantClient,
|
|
18
|
+
) => ScopesConfig;
|
|
19
|
+
|
|
20
|
+
type ResourceWithTransformScopes = {
|
|
21
|
+
[TRANSFORM_SCOPES]?: TransformScopesFn;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function attachTransformScopes<
|
|
25
|
+
T extends (...args: any[]) => ResourceElement<any>,
|
|
26
|
+
>(resource: T, transform: TransformScopesFn): void {
|
|
27
|
+
const r = resource as T & ResourceWithTransformScopes;
|
|
28
|
+
if (r[TRANSFORM_SCOPES]) {
|
|
29
|
+
throw new Error("transformScopes is already attached to this resource");
|
|
30
|
+
}
|
|
31
|
+
r[TRANSFORM_SCOPES] = transform;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getTransformScopes<
|
|
35
|
+
T extends (...args: any[]) => ResourceElement<any>,
|
|
36
|
+
>(resource: T): TransformScopesFn | undefined {
|
|
37
|
+
return (resource as T & ResourceWithTransformScopes)[TRANSFORM_SCOPES];
|
|
38
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,30 +1,46 @@
|
|
|
1
|
-
//
|
|
2
|
-
export
|
|
3
|
-
|
|
1
|
+
// hooks
|
|
2
|
+
export { useAui } from "./useAui";
|
|
3
|
+
export { useAuiState } from "./useAuiState";
|
|
4
|
+
export { useAuiEvent } from "./useAuiEvent";
|
|
5
|
+
|
|
6
|
+
// components
|
|
7
|
+
export { AuiIf } from "./AuiIf";
|
|
8
|
+
export { AuiProvider } from "./utils/react-assistant-context";
|
|
4
9
|
|
|
5
|
-
//
|
|
10
|
+
// resources
|
|
11
|
+
export { Derived } from "./Derived";
|
|
12
|
+
export { attachTransformScopes } from "./attachTransformScopes";
|
|
13
|
+
export type { ScopesConfig } from "./attachTransformScopes";
|
|
14
|
+
|
|
15
|
+
// tap hooks
|
|
6
16
|
export {
|
|
7
|
-
Derived,
|
|
8
|
-
attachTransformScopes,
|
|
9
|
-
type ScopesConfig,
|
|
10
17
|
tapAssistantClientRef,
|
|
11
18
|
tapAssistantEmit,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
} from "./utils/tap-assistant-context";
|
|
20
|
+
export { tapClientResource } from "./tapClientResource";
|
|
21
|
+
export { tapClientLookup } from "./tapClientLookup";
|
|
22
|
+
export { tapClientList } from "./tapClientList";
|
|
23
|
+
|
|
24
|
+
// types
|
|
25
|
+
export type {
|
|
26
|
+
ScopeRegistry,
|
|
27
|
+
ClientOutput,
|
|
28
|
+
ClientMethods,
|
|
29
|
+
ClientSchema,
|
|
30
|
+
ClientNames,
|
|
31
|
+
ClientEvents,
|
|
32
|
+
ClientMeta,
|
|
33
|
+
ClientElement,
|
|
34
|
+
Unsubscribe,
|
|
35
|
+
AssistantClientAccessor,
|
|
36
|
+
AssistantClient,
|
|
37
|
+
AssistantState,
|
|
38
|
+
} from "./types/client";
|
|
39
|
+
export {
|
|
40
|
+
normalizeEventSelector,
|
|
18
41
|
type AssistantEventName,
|
|
19
42
|
type AssistantEventCallback,
|
|
20
43
|
type AssistantEventPayload,
|
|
21
44
|
type AssistantEventSelector,
|
|
22
45
|
type AssistantEventScope,
|
|
23
|
-
} from "
|
|
24
|
-
|
|
25
|
-
// React-specific (stay in store)
|
|
26
|
-
export { useAui } from "./useAui";
|
|
27
|
-
export { useAuiState } from "./useAuiState";
|
|
28
|
-
export { useAuiEvent } from "./useAuiEvent";
|
|
29
|
-
export { AuiIf } from "./AuiIf";
|
|
30
|
-
export { AuiProvider } from "./utils/react-assistant-context";
|
|
46
|
+
} from "./types/events";
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { tapConst, tapState, withKey } from "@assistant-ui/tap";
|
|
2
|
+
import type { ContravariantResource } from "@assistant-ui/tap";
|
|
3
|
+
import { tapClientLookup } from "./tapClientLookup";
|
|
4
|
+
import type { ClientMethods } from "./types/client";
|
|
5
|
+
|
|
6
|
+
type InferClientState<TMethods> = TMethods extends {
|
|
7
|
+
getState: () => infer S;
|
|
8
|
+
}
|
|
9
|
+
? S
|
|
10
|
+
: unknown;
|
|
11
|
+
|
|
12
|
+
type DataHandle<TData> = { data: TData | undefined; hasData: boolean };
|
|
13
|
+
|
|
14
|
+
const createProps = <TData>(
|
|
15
|
+
key: string,
|
|
16
|
+
data: DataHandle<TData>,
|
|
17
|
+
remove: () => void,
|
|
18
|
+
): tapClientList.ResourceProps<TData> => {
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
getInitialData: () => {
|
|
22
|
+
if (!data.hasData) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
"getInitialData may only be called during initial render",
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return data.data!;
|
|
28
|
+
},
|
|
29
|
+
remove,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const tapClientList = <TData, TMethods extends ClientMethods>(
|
|
34
|
+
props: tapClientList.Props<TData, TMethods>,
|
|
35
|
+
): {
|
|
36
|
+
state: InferClientState<TMethods>[];
|
|
37
|
+
get: (lookup: { index: number } | { key: string }) => TMethods;
|
|
38
|
+
add: (initialData: TData) => void;
|
|
39
|
+
} => {
|
|
40
|
+
const { initialValues, getKey, resource: Resource } = props;
|
|
41
|
+
|
|
42
|
+
type Props = tapClientList.ResourceProps<TData>;
|
|
43
|
+
|
|
44
|
+
const initialDataHandles: DataHandle<TData>[] = tapConst(() => [], []);
|
|
45
|
+
|
|
46
|
+
const [items, setItems] = tapState<Record<string, Props>>(() => {
|
|
47
|
+
const entries: [string, Props][] = [];
|
|
48
|
+
for (const data of initialValues) {
|
|
49
|
+
const key = getKey(data);
|
|
50
|
+
const handle = { data, hasData: true };
|
|
51
|
+
entries.push([
|
|
52
|
+
key,
|
|
53
|
+
createProps(key, handle, () => {
|
|
54
|
+
setItems((items) => {
|
|
55
|
+
const newItems = { ...items };
|
|
56
|
+
delete newItems[key];
|
|
57
|
+
return newItems;
|
|
58
|
+
});
|
|
59
|
+
}),
|
|
60
|
+
]);
|
|
61
|
+
initialDataHandles.push(handle);
|
|
62
|
+
}
|
|
63
|
+
return Object.fromEntries(entries);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const lookup = tapClientLookup<TMethods>(
|
|
67
|
+
() =>
|
|
68
|
+
Object.values(items).map((props) => withKey(props.key, Resource(props))),
|
|
69
|
+
[items, Resource],
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
initialDataHandles.forEach((handle) => {
|
|
73
|
+
handle.data = undefined;
|
|
74
|
+
handle.hasData = false;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const add = (data: TData) => {
|
|
78
|
+
const key = getKey(data);
|
|
79
|
+
setItems((items) => {
|
|
80
|
+
if (key in items) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Tried to add item with a key ${key} that already exists`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const handle = { data, hasData: true };
|
|
87
|
+
initialDataHandles.push(handle);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
...items,
|
|
91
|
+
[key]: createProps(key, handle, () => {
|
|
92
|
+
setItems((items) => {
|
|
93
|
+
const newItems = { ...items };
|
|
94
|
+
delete newItems[key];
|
|
95
|
+
return newItems;
|
|
96
|
+
});
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
state: lookup.state,
|
|
104
|
+
get: lookup.get,
|
|
105
|
+
add,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export namespace tapClientList {
|
|
110
|
+
export type ResourceProps<TData> = {
|
|
111
|
+
key: string;
|
|
112
|
+
getInitialData: () => TData;
|
|
113
|
+
remove: () => void;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export type Props<TData, TMethods extends ClientMethods> = {
|
|
117
|
+
initialValues: TData[];
|
|
118
|
+
getKey: (data: TData) => string;
|
|
119
|
+
resource: ContravariantResource<TMethods, ResourceProps<TData>>;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
tapMemo,
|
|
3
|
+
tapResource,
|
|
4
|
+
tapResources,
|
|
5
|
+
type ResourceElement,
|
|
6
|
+
} from "@assistant-ui/tap";
|
|
7
|
+
import type { ClientMethods } from "./types/client";
|
|
8
|
+
import { ClientResource } from "./tapClientResource";
|
|
9
|
+
import { wrapperResource } from "./wrapperResource";
|
|
10
|
+
|
|
11
|
+
type InferClientState<TMethods> = TMethods extends {
|
|
12
|
+
getState: () => infer S;
|
|
13
|
+
}
|
|
14
|
+
? S
|
|
15
|
+
: unknown;
|
|
16
|
+
|
|
17
|
+
const ClientResourceWithKey = wrapperResource(
|
|
18
|
+
<TMethods extends ClientMethods>(el: ResourceElement<TMethods>) => {
|
|
19
|
+
if (el.key === undefined) {
|
|
20
|
+
throw new Error("tapClientResource: Element has no key");
|
|
21
|
+
}
|
|
22
|
+
return tapResource(ClientResource(el)) as {
|
|
23
|
+
methods: TMethods;
|
|
24
|
+
state: InferClientState<TMethods>;
|
|
25
|
+
key: string | number;
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export function tapClientLookup<TMethods extends ClientMethods>(
|
|
31
|
+
getElements: () => readonly ResourceElement<TMethods>[],
|
|
32
|
+
getElementsDeps: readonly unknown[],
|
|
33
|
+
): {
|
|
34
|
+
state: InferClientState<TMethods>[];
|
|
35
|
+
get: (lookup: { index: number } | { key: string }) => TMethods;
|
|
36
|
+
} {
|
|
37
|
+
const resources = tapResources(
|
|
38
|
+
() => getElements().map((el) => ClientResourceWithKey(el)),
|
|
39
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: getElementsDeps is passed through from caller
|
|
40
|
+
getElementsDeps,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const keys = tapMemo(() => Object.keys(resources), [resources]);
|
|
44
|
+
|
|
45
|
+
// For arrays, track element key -> index mapping
|
|
46
|
+
const keyToIndex = tapMemo(() => {
|
|
47
|
+
return resources.reduce(
|
|
48
|
+
(acc, resource, index) => {
|
|
49
|
+
acc[resource.key] = index;
|
|
50
|
+
return acc;
|
|
51
|
+
},
|
|
52
|
+
{} as Record<string, number>,
|
|
53
|
+
);
|
|
54
|
+
}, [resources]);
|
|
55
|
+
|
|
56
|
+
const state = tapMemo(() => {
|
|
57
|
+
return resources.map((r) => r.state);
|
|
58
|
+
}, [resources]);
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
state,
|
|
62
|
+
get: (lookup: { index: number } | { key: string }) => {
|
|
63
|
+
if ("index" in lookup) {
|
|
64
|
+
if (lookup.index < 0 || lookup.index >= keys.length) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`tapClientLookup: Index ${lookup.index} out of bounds (length: ${keys.length})`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return resources[lookup.index]!.methods;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const index = keyToIndex[lookup.key];
|
|
73
|
+
if (index === undefined) {
|
|
74
|
+
throw new Error(`tapClientLookup: Key "${lookup.key}" not found`);
|
|
75
|
+
}
|
|
76
|
+
return resources[index]!.methods;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|