@assistant-ui/core 0.1.9 → 0.1.11
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/adapters/index.d.ts +3 -0
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +1 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/voice.d.ts +49 -0
- package/dist/adapters/voice.d.ts.map +1 -0
- package/dist/adapters/voice.js +109 -0
- package/dist/adapters/voice.js.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/model-context/types.d.ts +4 -0
- package/dist/model-context/types.d.ts.map +1 -1
- package/dist/model-context/types.js.map +1 -1
- package/dist/react/client/Interactables.d.ts.map +1 -1
- package/dist/react/client/Interactables.js +155 -65
- package/dist/react/client/Interactables.js.map +1 -1
- package/dist/react/client/interactable-model-context.d.ts +8 -0
- package/dist/react/client/interactable-model-context.d.ts.map +1 -0
- package/dist/react/client/interactable-model-context.js +62 -0
- package/dist/react/client/interactable-model-context.js.map +1 -0
- package/dist/react/index.d.ts +6 -3
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +5 -2
- package/dist/react/index.js.map +1 -1
- package/dist/react/model-context/useAssistantContext.d.ts +4 -0
- package/dist/react/model-context/useAssistantContext.d.ts.map +1 -0
- package/dist/react/model-context/useAssistantContext.js +18 -0
- package/dist/react/model-context/useAssistantContext.js.map +1 -0
- package/dist/react/model-context/useAssistantInteractable.d.ts +18 -0
- package/dist/react/model-context/useAssistantInteractable.d.ts.map +1 -0
- package/dist/react/model-context/useAssistantInteractable.js +31 -0
- package/dist/react/model-context/useAssistantInteractable.js.map +1 -0
- package/dist/react/model-context/useInteractableState.d.ts +15 -0
- package/dist/react/model-context/useInteractableState.d.ts.map +1 -0
- package/dist/react/model-context/useInteractableState.js +36 -0
- package/dist/react/model-context/useInteractableState.js.map +1 -0
- package/dist/react/model-context/useToolArgsStatus.d.ts +8 -0
- package/dist/react/model-context/useToolArgsStatus.d.ts.map +1 -0
- package/dist/react/model-context/useToolArgsStatus.js +31 -0
- package/dist/react/model-context/useToolArgsStatus.js.map +1 -0
- package/dist/react/primitive-hooks/useVoice.d.ts +10 -0
- package/dist/react/primitive-hooks/useVoice.d.ts.map +1 -0
- package/dist/react/primitive-hooks/useVoice.js +28 -0
- package/dist/react/primitive-hooks/useVoice.js.map +1 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +14 -0
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +14 -0
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +16 -2
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -1
- package/dist/react/runtimes/useLocalRuntime.d.ts +1 -0
- package/dist/react/runtimes/useLocalRuntime.d.ts.map +1 -1
- package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts.map +1 -1
- package/dist/react/runtimes/useRemoteThreadListRuntime.js +17 -1
- package/dist/react/runtimes/useRemoteThreadListRuntime.js.map +1 -1
- package/dist/react/runtimes/useToolInvocations.d.ts.map +1 -1
- package/dist/react/runtimes/useToolInvocations.js +8 -0
- package/dist/react/runtimes/useToolInvocations.js.map +1 -1
- package/dist/react/types/scopes/interactables.d.ts +17 -0
- package/dist/react/types/scopes/interactables.d.ts.map +1 -1
- package/dist/runtime/api/thread-list-runtime.d.ts +2 -0
- package/dist/runtime/api/thread-list-runtime.d.ts.map +1 -1
- package/dist/runtime/api/thread-list-runtime.js +4 -0
- package/dist/runtime/api/thread-list-runtime.js.map +1 -1
- package/dist/runtime/api/thread-runtime.d.ts +21 -1
- package/dist/runtime/api/thread-runtime.d.ts.map +1 -1
- package/dist/runtime/api/thread-runtime.js +25 -0
- package/dist/runtime/api/thread-runtime.js.map +1 -1
- package/dist/runtime/base/base-thread-runtime-core.d.ts +24 -1
- package/dist/runtime/base/base-thread-runtime-core.d.ts.map +1 -1
- package/dist/runtime/base/base-thread-runtime-core.js +205 -1
- package/dist/runtime/base/base-thread-runtime-core.js.map +1 -1
- package/dist/runtime/interfaces/thread-runtime-core.d.ts +14 -0
- package/dist/runtime/interfaces/thread-runtime-core.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-store-adapter.d.ts +2 -0
- package/dist/runtimes/external-store/external-store-adapter.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts +2 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.js +3 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.js.map +1 -1
- package/dist/runtimes/local/local-runtime-options.d.ts +2 -0
- package/dist/runtimes/local/local-runtime-options.d.ts.map +1 -1
- package/dist/runtimes/local/local-thread-runtime-core.d.ts +2 -0
- package/dist/runtimes/local/local-thread-runtime-core.d.ts.map +1 -1
- package/dist/runtimes/local/local-thread-runtime-core.js +6 -0
- package/dist/runtimes/local/local-thread-runtime-core.js.map +1 -1
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.d.ts +8 -0
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.js +15 -6
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.js.map +1 -1
- package/dist/runtimes/remote-thread-list/empty-thread-core.d.ts.map +1 -1
- package/dist/runtimes/remote-thread-list/empty-thread-core.js +18 -2
- package/dist/runtimes/remote-thread-list/empty-thread-core.js.map +1 -1
- package/dist/runtimes/remote-thread-list/types.d.ts +14 -0
- package/dist/runtimes/remote-thread-list/types.d.ts.map +1 -1
- package/dist/store/runtime-clients/thread-list-runtime-client.d.ts.map +1 -1
- package/dist/store/runtime-clients/thread-list-runtime-client.js +1 -0
- package/dist/store/runtime-clients/thread-list-runtime-client.js.map +1 -1
- package/dist/store/runtime-clients/thread-runtime-client.d.ts.map +1 -1
- package/dist/store/runtime-clients/thread-runtime-client.js +7 -8
- package/dist/store/runtime-clients/thread-runtime-client.js.map +1 -1
- package/dist/store/scopes/thread.d.ts +9 -9
- package/dist/store/scopes/thread.d.ts.map +1 -1
- package/dist/store/scopes/threads.d.ts +1 -0
- package/dist/store/scopes/threads.d.ts.map +1 -1
- package/dist/types/message.d.ts +1 -0
- package/dist/types/message.d.ts.map +1 -1
- package/package.json +9 -15
- package/src/adapters/index.ts +5 -0
- package/src/adapters/voice.ts +166 -0
- package/src/index.ts +10 -0
- package/src/model-context/types.ts +5 -0
- package/src/react/client/Interactables.ts +221 -122
- package/src/react/client/interactable-model-context.ts +83 -0
- package/src/react/index.ts +19 -8
- package/src/react/model-context/useAssistantContext.ts +22 -0
- package/src/react/model-context/useAssistantInteractable.ts +47 -0
- package/src/react/model-context/useInteractableState.ts +63 -0
- package/src/react/model-context/useToolArgsStatus.ts +51 -0
- package/src/react/primitive-hooks/useVoice.ts +41 -0
- package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +15 -2
- package/src/react/runtimes/useRemoteThreadListRuntime.ts +19 -1
- package/src/react/runtimes/useToolInvocations.ts +9 -0
- package/src/react/types/scopes/interactables.ts +22 -0
- package/src/runtime/api/thread-list-runtime.ts +7 -0
- package/src/runtime/api/thread-runtime.ts +41 -0
- package/src/runtime/base/base-thread-runtime-core.ts +243 -2
- package/src/runtime/interfaces/thread-runtime-core.ts +17 -0
- package/src/runtimes/external-store/external-store-adapter.ts +2 -0
- package/src/runtimes/external-store/external-store-thread-runtime-core.ts +3 -1
- package/src/runtimes/local/local-runtime-options.ts +2 -0
- package/src/runtimes/local/local-thread-runtime-core.ts +7 -0
- package/src/runtimes/readonly/ReadonlyThreadRuntimeCore.ts +20 -6
- package/src/runtimes/remote-thread-list/empty-thread-core.ts +23 -2
- package/src/runtimes/remote-thread-list/types.ts +16 -0
- package/src/store/runtime-clients/thread-list-runtime-client.ts +1 -0
- package/src/store/runtime-clients/thread-runtime-client.ts +7 -8
- package/src/store/scopes/thread.ts +9 -8
- package/src/store/scopes/threads.ts +1 -0
- package/src/tests/empty-thread-core.test.ts +33 -0
- package/src/tests/remote-thread-list-isLoading.test.ts +192 -0
- package/src/tests/remote-thread-list-reactive-threadId.test.ts +93 -0
- package/src/tests/thread-list-runtime-getLoadThreadsPromise.test.ts +75 -0
- package/src/types/message.ts +1 -0
- package/dist/react/model-context/makeInteractable.d.ts +0 -10
- package/dist/react/model-context/makeInteractable.d.ts.map +0 -1
- package/dist/react/model-context/makeInteractable.js +0 -10
- package/dist/react/model-context/makeInteractable.js.map +0 -1
- package/dist/react/model-context/useInteractable.d.ts +0 -16
- package/dist/react/model-context/useInteractable.d.ts.map +0 -1
- package/dist/react/model-context/useInteractable.js +0 -36
- package/dist/react/model-context/useInteractable.js.map +0 -1
- package/src/react/model-context/makeInteractable.ts +0 -21
- package/src/react/model-context/useInteractable.ts +0 -73
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tests for the reactive threadId useEffect logic in useRemoteThreadListRuntime.
|
|
5
|
+
*
|
|
6
|
+
* The useEffect compares options.threadId against a prevThreadIdRef and calls
|
|
7
|
+
* switchToThread or switchToNewThread when it changes. We test this logic
|
|
8
|
+
* by simulating the ref + comparison pattern.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Mirrors the useEffect in useRemoteThreadListRuntimeImpl.
|
|
12
|
+
// Keep in sync if that implementation changes.
|
|
13
|
+
function simulateThreadIdEffect(
|
|
14
|
+
prevRef: { current: string | undefined },
|
|
15
|
+
threadId: string | undefined,
|
|
16
|
+
switchToThread: (id: string) => void,
|
|
17
|
+
switchToNewThread: () => void,
|
|
18
|
+
) {
|
|
19
|
+
if (threadId === prevRef.current) return;
|
|
20
|
+
prevRef.current = threadId;
|
|
21
|
+
if (threadId) {
|
|
22
|
+
switchToThread(threadId);
|
|
23
|
+
} else {
|
|
24
|
+
switchToNewThread();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("threadId reactive effect", () => {
|
|
29
|
+
it("does nothing when threadId stays the same", () => {
|
|
30
|
+
const ref = { current: "thread-1" };
|
|
31
|
+
const switchToThread = vi.fn();
|
|
32
|
+
const switchToNewThread = vi.fn();
|
|
33
|
+
|
|
34
|
+
simulateThreadIdEffect(ref, "thread-1", switchToThread, switchToNewThread);
|
|
35
|
+
|
|
36
|
+
expect(switchToThread).not.toHaveBeenCalled();
|
|
37
|
+
expect(switchToNewThread).not.toHaveBeenCalled();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("calls switchToThread when threadId changes", () => {
|
|
41
|
+
const ref = { current: "thread-1" };
|
|
42
|
+
const switchToThread = vi.fn();
|
|
43
|
+
const switchToNewThread = vi.fn();
|
|
44
|
+
|
|
45
|
+
simulateThreadIdEffect(ref, "thread-2", switchToThread, switchToNewThread);
|
|
46
|
+
|
|
47
|
+
expect(switchToThread).toHaveBeenCalledWith("thread-2");
|
|
48
|
+
expect(ref.current).toBe("thread-2");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("calls switchToNewThread when threadId becomes undefined", () => {
|
|
52
|
+
const ref = { current: "thread-1" };
|
|
53
|
+
const switchToThread = vi.fn();
|
|
54
|
+
const switchToNewThread = vi.fn();
|
|
55
|
+
|
|
56
|
+
simulateThreadIdEffect(ref, undefined, switchToThread, switchToNewThread);
|
|
57
|
+
|
|
58
|
+
expect(switchToNewThread).toHaveBeenCalledOnce();
|
|
59
|
+
expect(ref.current).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("skips switchToNewThread on first render when threadId is already undefined", () => {
|
|
63
|
+
const ref = { current: undefined as string | undefined };
|
|
64
|
+
const switchToThread = vi.fn();
|
|
65
|
+
const switchToNewThread = vi.fn();
|
|
66
|
+
|
|
67
|
+
// First render: undefined === undefined → skipped
|
|
68
|
+
simulateThreadIdEffect(ref, undefined, switchToThread, switchToNewThread);
|
|
69
|
+
expect(switchToNewThread).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("handles full navigation cycle", () => {
|
|
73
|
+
const ref = { current: undefined as string | undefined };
|
|
74
|
+
const switchToThread = vi.fn();
|
|
75
|
+
const switchToNewThread = vi.fn();
|
|
76
|
+
|
|
77
|
+
// Mount with thread-1
|
|
78
|
+
simulateThreadIdEffect(ref, "thread-1", switchToThread, switchToNewThread);
|
|
79
|
+
expect(switchToThread).toHaveBeenCalledWith("thread-1");
|
|
80
|
+
|
|
81
|
+
// Navigate to thread-2
|
|
82
|
+
simulateThreadIdEffect(ref, "thread-2", switchToThread, switchToNewThread);
|
|
83
|
+
expect(switchToThread).toHaveBeenCalledWith("thread-2");
|
|
84
|
+
|
|
85
|
+
// Navigate to new thread
|
|
86
|
+
simulateThreadIdEffect(ref, undefined, switchToThread, switchToNewThread);
|
|
87
|
+
expect(switchToNewThread).toHaveBeenCalledOnce();
|
|
88
|
+
|
|
89
|
+
// Navigate back to thread-1
|
|
90
|
+
simulateThreadIdEffect(ref, "thread-1", switchToThread, switchToNewThread);
|
|
91
|
+
expect(switchToThread).toHaveBeenCalledTimes(3);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { ThreadListRuntimeImpl } from "../runtime/api/thread-list-runtime";
|
|
3
|
+
import type { ThreadListRuntimeCore } from "../runtime/interfaces/thread-list-runtime-core";
|
|
4
|
+
import { EMPTY_THREAD_CORE } from "../runtimes/remote-thread-list/empty-thread-core";
|
|
5
|
+
|
|
6
|
+
const MAIN_THREAD_ID = "main-thread";
|
|
7
|
+
|
|
8
|
+
const createMockCore = (
|
|
9
|
+
loadPromise: Promise<void> = Promise.resolve(),
|
|
10
|
+
): ThreadListRuntimeCore => ({
|
|
11
|
+
isLoading: false,
|
|
12
|
+
mainThreadId: MAIN_THREAD_ID,
|
|
13
|
+
newThreadId: undefined,
|
|
14
|
+
threadIds: [MAIN_THREAD_ID],
|
|
15
|
+
archivedThreadIds: [],
|
|
16
|
+
threadItems: {
|
|
17
|
+
[MAIN_THREAD_ID]: {
|
|
18
|
+
id: MAIN_THREAD_ID,
|
|
19
|
+
remoteId: MAIN_THREAD_ID,
|
|
20
|
+
externalId: undefined,
|
|
21
|
+
status: "regular",
|
|
22
|
+
title: undefined,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
getMainThreadRuntimeCore: () => EMPTY_THREAD_CORE,
|
|
26
|
+
getThreadRuntimeCore: () => EMPTY_THREAD_CORE,
|
|
27
|
+
getItemById: (id) => {
|
|
28
|
+
if (id === MAIN_THREAD_ID) {
|
|
29
|
+
return {
|
|
30
|
+
id: MAIN_THREAD_ID,
|
|
31
|
+
remoteId: MAIN_THREAD_ID,
|
|
32
|
+
externalId: undefined,
|
|
33
|
+
status: "regular",
|
|
34
|
+
title: undefined,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
},
|
|
39
|
+
switchToThread: () => Promise.resolve(),
|
|
40
|
+
switchToNewThread: () => Promise.resolve(),
|
|
41
|
+
getLoadThreadsPromise: () => loadPromise,
|
|
42
|
+
detach: () => Promise.resolve(),
|
|
43
|
+
rename: () => Promise.resolve(),
|
|
44
|
+
archive: () => Promise.resolve(),
|
|
45
|
+
unarchive: () => Promise.resolve(),
|
|
46
|
+
delete: () => Promise.resolve(),
|
|
47
|
+
initialize: () => Promise.resolve({ remoteId: "r", externalId: undefined }),
|
|
48
|
+
generateTitle: () => Promise.resolve(),
|
|
49
|
+
subscribe: () => () => {},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("ThreadListRuntime.getLoadThreadsPromise", () => {
|
|
53
|
+
it("returns a Promise that resolves", async () => {
|
|
54
|
+
const runtime = new ThreadListRuntimeImpl(createMockCore());
|
|
55
|
+
const result = runtime.getLoadThreadsPromise();
|
|
56
|
+
expect(result).toBeInstanceOf(Promise);
|
|
57
|
+
await expect(result).resolves.toBeUndefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("delegates to the core implementation", async () => {
|
|
61
|
+
let resolved = false;
|
|
62
|
+
const loadPromise = new Promise<void>((resolve) => {
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
resolved = true;
|
|
65
|
+
resolve();
|
|
66
|
+
}, 10);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const runtime = new ThreadListRuntimeImpl(createMockCore(loadPromise));
|
|
70
|
+
|
|
71
|
+
expect(resolved).toBe(false);
|
|
72
|
+
await runtime.getLoadThreadsPromise();
|
|
73
|
+
expect(resolved).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
package/src/types/message.ts
CHANGED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { FC } from "react";
|
|
2
|
-
import { type UseInteractableConfig } from "./useInteractable.js";
|
|
3
|
-
export type InteractableConfig<TState> = UseInteractableConfig<TState> & {
|
|
4
|
-
name: string;
|
|
5
|
-
};
|
|
6
|
-
export type AssistantInteractable = FC & {
|
|
7
|
-
unstable_interactable: InteractableConfig<any>;
|
|
8
|
-
};
|
|
9
|
-
export declare const makeInteractable: <TState>(config: InteractableConfig<TState>) => AssistantInteractable;
|
|
10
|
-
//# sourceMappingURL=makeInteractable.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"makeInteractable.d.ts","sourceRoot":"","sources":["../../../src/react/model-context/makeInteractable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC;AAC3B,OAAO,EAAmB,KAAK,qBAAqB,EAAE,6BAA0B;AAEhF,MAAM,MAAM,kBAAkB,CAAC,MAAM,IAAI,qBAAqB,CAAC,MAAM,CAAC,GAAG;IACvE,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,EAAE,GAAG;IACvC,qBAAqB,EAAE,kBAAkB,CAAC,GAAG,CAAC,CAAC;CAChD,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,MAAM,EACrC,QAAQ,kBAAkB,CAAC,MAAM,CAAC,0BAQnC,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { useInteractable } from "./useInteractable.js";
|
|
2
|
-
export const makeInteractable = (config) => {
|
|
3
|
-
const Interactable = () => {
|
|
4
|
-
useInteractable(config.name, config);
|
|
5
|
-
return null;
|
|
6
|
-
};
|
|
7
|
-
Interactable.unstable_interactable = config;
|
|
8
|
-
return Interactable;
|
|
9
|
-
};
|
|
10
|
-
//# sourceMappingURL=makeInteractable.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"makeInteractable.js","sourceRoot":"","sources":["../../../src/react/model-context/makeInteractable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAA8B,6BAA0B;AAUhF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,MAAkC,EAClC,EAAE;IACF,MAAM,YAAY,GAA0B,GAAG,EAAE;QAC/C,eAAe,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IACF,YAAY,CAAC,qBAAqB,GAAG,MAAM,CAAC;IAC5C,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { InteractableStateSchema } from "../types/scopes/interactables.js";
|
|
2
|
-
export type UseInteractableConfig<TState> = {
|
|
3
|
-
description: string;
|
|
4
|
-
stateSchema: InteractableStateSchema;
|
|
5
|
-
initialState: TState;
|
|
6
|
-
id?: string;
|
|
7
|
-
selected?: boolean;
|
|
8
|
-
};
|
|
9
|
-
type StateUpdater<TState> = TState | ((prev: TState) => TState);
|
|
10
|
-
export type UseInteractableMetadata = {
|
|
11
|
-
id: string;
|
|
12
|
-
setSelected: (selected: boolean) => void;
|
|
13
|
-
};
|
|
14
|
-
export declare const useInteractable: <TState>(name: string, config: UseInteractableConfig<TState>) => [TState, (updater: StateUpdater<TState>) => void, UseInteractableMetadata];
|
|
15
|
-
export {};
|
|
16
|
-
//# sourceMappingURL=useInteractable.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useInteractable.d.ts","sourceRoot":"","sources":["../../../src/react/model-context/useInteractable.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,uBAAuB,EAAE,yCAAsC;AAE7E,MAAM,MAAM,qBAAqB,CAAC,MAAM,IAAI;IAC1C,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,uBAAuB,CAAC;IACrC,YAAY,EAAE,MAAM,CAAC;IACrB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,KAAK,YAAY,CAAC,MAAM,IAAI,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;AAEhE,MAAM,MAAM,uBAAuB,GAAG;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAC;CAC1C,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,MAAM,EACpC,MAAM,MAAM,EACZ,QAAQ,qBAAqB,CAAC,MAAM,CAAC,KACpC,CACD,MAAM,EACN,CAAC,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,KAAK,IAAI,EACvC,uBAAuB,CA+CxB,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { useEffect, useCallback, useId, useRef } from "react";
|
|
2
|
-
import { useAui, useAuiState } from "@assistant-ui/store";
|
|
3
|
-
export const useInteractable = (name, config) => {
|
|
4
|
-
const aui = useAui();
|
|
5
|
-
const autoId = useId().replace(/[^a-zA-Z0-9]/g, "");
|
|
6
|
-
const id = config.id ?? autoId;
|
|
7
|
-
const stateSchemaRef = useRef(config.stateSchema);
|
|
8
|
-
stateSchemaRef.current = config.stateSchema;
|
|
9
|
-
const initialStateRef = useRef(config.initialState);
|
|
10
|
-
initialStateRef.current = config.initialState;
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
return aui.interactables().register({
|
|
13
|
-
id,
|
|
14
|
-
name,
|
|
15
|
-
description: config.description,
|
|
16
|
-
stateSchema: stateSchemaRef.current,
|
|
17
|
-
initialState: initialStateRef.current,
|
|
18
|
-
selected: config.selected,
|
|
19
|
-
});
|
|
20
|
-
}, [aui, id, name, config.description, config.selected]);
|
|
21
|
-
const state = useAuiState((s) => s.interactables.definitions[id]?.state) ??
|
|
22
|
-
config.initialState;
|
|
23
|
-
const setState = useCallback((updater) => {
|
|
24
|
-
aui.interactables().setState(id, (prev) => {
|
|
25
|
-
if (typeof updater === "function") {
|
|
26
|
-
return updater(prev);
|
|
27
|
-
}
|
|
28
|
-
return updater;
|
|
29
|
-
});
|
|
30
|
-
}, [aui, id]);
|
|
31
|
-
const setSelected = useCallback((selected) => {
|
|
32
|
-
aui.interactables().setSelected(id, selected);
|
|
33
|
-
}, [aui, id]);
|
|
34
|
-
return [state, setState, { id, setSelected }];
|
|
35
|
-
};
|
|
36
|
-
//# sourceMappingURL=useInteractable.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useInteractable.js","sourceRoot":"","sources":["../../../src/react/model-context/useInteractable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAkB1D,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,IAAY,EACZ,MAAqC,EAKrC,EAAE;IACF,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;IAErB,MAAM,MAAM,GAAG,KAAK,EAAE,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;IACpD,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,IAAI,MAAM,CAAC;IAE/B,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAClD,cAAc,CAAC,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC;IAC5C,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IACpD,eAAe,CAAC,OAAO,GAAG,MAAM,CAAC,YAAY,CAAC;IAE9C,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,CAAC,aAAa,EAAE,CAAC,QAAQ,CAAC;YAClC,EAAE;YACF,IAAI;YACJ,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,WAAW,EAAE,cAAc,CAAC,OAAO;YACnC,YAAY,EAAE,eAAe,CAAC,OAAO;YACrC,QAAQ,EAAE,MAAM,CAAC,QAAQ;SAC1B,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEzD,MAAM,KAAK,GACR,WAAW,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,KAAK,CAAY;QACtE,MAAM,CAAC,YAAY,CAAC;IAEtB,MAAM,QAAQ,GAAG,WAAW,CAC1B,CAAC,OAA6B,EAAE,EAAE;QAChC,GAAG,CAAC,aAAa,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE;YACxC,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;gBAClC,OAAQ,OAAoC,CAAC,IAAc,CAAC,CAAC;YAC/D,CAAC;YACD,OAAO,OAAO,CAAC;QACjB,CAAC,CAAC,CAAC;IACL,CAAC,EACD,CAAC,GAAG,EAAE,EAAE,CAAC,CACV,CAAC;IAEF,MAAM,WAAW,GAAG,WAAW,CAC7B,CAAC,QAAiB,EAAE,EAAE;QACpB,GAAG,CAAC,aAAa,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IAChD,CAAC,EACD,CAAC,GAAG,EAAE,EAAE,CAAC,CACV,CAAC;IAEF,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;AAChD,CAAC,CAAC"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { FC } from "react";
|
|
2
|
-
import { useInteractable, type UseInteractableConfig } from "./useInteractable";
|
|
3
|
-
|
|
4
|
-
export type InteractableConfig<TState> = UseInteractableConfig<TState> & {
|
|
5
|
-
name: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type AssistantInteractable = FC & {
|
|
9
|
-
unstable_interactable: InteractableConfig<any>;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const makeInteractable = <TState>(
|
|
13
|
-
config: InteractableConfig<TState>,
|
|
14
|
-
) => {
|
|
15
|
-
const Interactable: AssistantInteractable = () => {
|
|
16
|
-
useInteractable(config.name, config);
|
|
17
|
-
return null;
|
|
18
|
-
};
|
|
19
|
-
Interactable.unstable_interactable = config;
|
|
20
|
-
return Interactable;
|
|
21
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { useEffect, useCallback, useId, useRef } from "react";
|
|
2
|
-
import { useAui, useAuiState } from "@assistant-ui/store";
|
|
3
|
-
import type { InteractableStateSchema } from "../types/scopes/interactables";
|
|
4
|
-
|
|
5
|
-
export type UseInteractableConfig<TState> = {
|
|
6
|
-
description: string;
|
|
7
|
-
stateSchema: InteractableStateSchema;
|
|
8
|
-
initialState: TState;
|
|
9
|
-
id?: string;
|
|
10
|
-
selected?: boolean;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
type StateUpdater<TState> = TState | ((prev: TState) => TState);
|
|
14
|
-
|
|
15
|
-
export type UseInteractableMetadata = {
|
|
16
|
-
id: string;
|
|
17
|
-
setSelected: (selected: boolean) => void;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const useInteractable = <TState>(
|
|
21
|
-
name: string,
|
|
22
|
-
config: UseInteractableConfig<TState>,
|
|
23
|
-
): [
|
|
24
|
-
TState,
|
|
25
|
-
(updater: StateUpdater<TState>) => void,
|
|
26
|
-
UseInteractableMetadata,
|
|
27
|
-
] => {
|
|
28
|
-
const aui = useAui();
|
|
29
|
-
|
|
30
|
-
const autoId = useId().replace(/[^a-zA-Z0-9]/g, "");
|
|
31
|
-
const id = config.id ?? autoId;
|
|
32
|
-
|
|
33
|
-
const stateSchemaRef = useRef(config.stateSchema);
|
|
34
|
-
stateSchemaRef.current = config.stateSchema;
|
|
35
|
-
const initialStateRef = useRef(config.initialState);
|
|
36
|
-
initialStateRef.current = config.initialState;
|
|
37
|
-
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
return aui.interactables().register({
|
|
40
|
-
id,
|
|
41
|
-
name,
|
|
42
|
-
description: config.description,
|
|
43
|
-
stateSchema: stateSchemaRef.current,
|
|
44
|
-
initialState: initialStateRef.current,
|
|
45
|
-
selected: config.selected,
|
|
46
|
-
});
|
|
47
|
-
}, [aui, id, name, config.description, config.selected]);
|
|
48
|
-
|
|
49
|
-
const state =
|
|
50
|
-
(useAuiState((s) => s.interactables.definitions[id]?.state) as TState) ??
|
|
51
|
-
config.initialState;
|
|
52
|
-
|
|
53
|
-
const setState = useCallback(
|
|
54
|
-
(updater: StateUpdater<TState>) => {
|
|
55
|
-
aui.interactables().setState(id, (prev) => {
|
|
56
|
-
if (typeof updater === "function") {
|
|
57
|
-
return (updater as (prev: TState) => TState)(prev as TState);
|
|
58
|
-
}
|
|
59
|
-
return updater;
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
[aui, id],
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
const setSelected = useCallback(
|
|
66
|
-
(selected: boolean) => {
|
|
67
|
-
aui.interactables().setSelected(id, selected);
|
|
68
|
-
},
|
|
69
|
-
[aui, id],
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
return [state, setState, { id, setSelected }];
|
|
73
|
-
};
|