Skip to content

Commit e5d5dfa

Browse files
committed
wip
1 parent 06d5bf4 commit e5d5dfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+123
-158
lines changed

apps/docs/components/assistant-ui/attachment.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ComposerPrimitive,
88
MessagePrimitive,
99
useAssistantState,
10-
useAssistantStoreApi,
10+
useAssistantApi,
1111
} from "@assistant-ui/react";
1212
import { useShallow } from "zustand/shallow";
1313
import {
@@ -94,8 +94,8 @@ const AttachmentPreviewDialog: FC<PropsWithChildren> = ({ children }) => {
9494
};
9595

9696
const ComposerAttachmentUI: FC = () => {
97-
const storeApi = useAssistantStoreApi();
98-
const canRemove = storeApi.meta.attachment?.source !== "message";
97+
const { meta } = useAssistantApi();
98+
const canRemove = meta.attachment?.source !== "message";
9999

100100
const name = useAssistantState(({ attachment }) => attachment.name);
101101

packages/react-hook-form/src/useAssistantForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ModelContext,
55
tool,
66
type ToolCallMessagePartComponent,
7-
useAssistantActions,
7+
useAssistantApi,
88
useAssistantToolUI,
99
} from "@assistant-ui/react";
1010
import { useEffect } from "react";
@@ -66,7 +66,7 @@ export const useAssistantForm = <
6666
const form = useForm<TFieldValues, TContext, TTransformedValues>(props);
6767
const { control, getValues, setValue } = form;
6868

69-
const actions = useAssistantActions();
69+
const { actions } = useAssistantApi();
7070
useEffect(() => {
7171
const value: ModelContext = {
7272
system: `Form State:\n${JSON.stringify(getValues())}`,

packages/react-langgraph/src/useLangGraphRuntime.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "./types";
1010
import {
1111
useAssistantState,
12-
useAssistantStoreApi,
12+
useAssistantApi,
1313
useExternalMessageConverter,
1414
useExternalStoreRuntime,
1515
} from "@assistant-ui/react";
@@ -231,11 +231,11 @@ export const useLangGraphRuntime = ({
231231
};
232232

233233
const loadingRef = useRef(false);
234-
const storeApi = useAssistantStoreApi();
234+
const { getState } = useAssistantApi();
235235
useEffect(() => {
236-
if (!storeApi || !switchToThread || loadingRef.current) return;
236+
if (!switchToThread || loadingRef.current) return;
237237

238-
const externalId = storeApi.getState().threadListItem.externalId;
238+
const externalId = getState().threadListItem.externalId;
239239
if (externalId) {
240240
loadingRef.current = true;
241241
switchToThread(externalId).finally(() => {

packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
import { GenericThreadHistoryAdapter } from "../runtimes/adapters/thread-history/ThreadHistoryAdapter";
1313
import { ReadonlyJSONObject } from "assistant-stream/utils";
1414
import {
15-
AssistantStoreApi,
16-
useAssistantStoreApi,
15+
AssistantApi,
16+
useAssistantApi,
1717
} from "../context/react/AssistantContext";
1818

1919
class FormattedThreadHistoryAdapter<TMessage, TStorageFormat>
@@ -51,7 +51,7 @@ class FormattedThreadHistoryAdapter<TMessage, TStorageFormat>
5151
class AssistantCloudThreadHistoryAdapter implements ThreadHistoryAdapter {
5252
constructor(
5353
private cloudRef: RefObject<AssistantCloud>,
54-
private store: AssistantStoreApi,
54+
private store: AssistantApi,
5555
) {}
5656

5757
private _getIdForLocalId: Record<string, string | Promise<string>> = {};
@@ -164,7 +164,7 @@ class AssistantCloudThreadHistoryAdapter implements ThreadHistoryAdapter {
164164
export const useAssistantCloudThreadHistoryAdapter = (
165165
cloudRef: RefObject<AssistantCloud>,
166166
): ThreadHistoryAdapter => {
167-
const store = useAssistantStoreApi();
167+
const store = useAssistantApi();
168168
const [adapter] = useState(
169169
() => new AssistantCloudThreadHistoryAdapter(cloudRef, store),
170170
);

packages/react/src/context/providers/MessageProvider.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { useMemo, type FC, type PropsWithChildren } from "react";
44
import { useAssistantStoreWithSelector } from "../react/utils/createAssistantStoreWithSelector";
55
import {
66
AssistantStoreContext,
7-
useAssistantActions,
8-
useAssistantStoreApi,
7+
useAssistantApi,
98
} from "../react/AssistantContext";
109
import { MessageClientActions } from "../../client/MessageClient";
1110
import { ComposerClientActions } from "../../client/ComposerClient";
@@ -15,13 +14,12 @@ export const MessageByIndexProvider: FC<
1514
index: number;
1615
}>
1716
> = ({ index, children }) => {
18-
const actions = useAssistantActions();
19-
const storeApi = useAssistantStoreApi();
17+
const { actions, getState } = useAssistantApi();
2018
const messageActions = useMemo(() => {
2119
const composerProxy = new Proxy({} as ComposerClientActions, {
2220
get(_, prop: keyof ComposerClientActions) {
2321
return (...args: unknown[]) => {
24-
const id = storeApi.getState().thread.messages[index]!.id;
22+
const id = getState().thread.messages[index]!.id;
2523
return (
2624
actions.thread.message(id).composer[prop] as (
2725
...args: any[]
@@ -38,14 +36,14 @@ export const MessageByIndexProvider: FC<
3836
return composerProxy;
3937
}
4038

41-
const id = storeApi.getState().thread.messages[index]!.id;
39+
const id = getState().thread.messages[index]!.id;
4240
return (
4341
actions.thread.message(id)[prop] as (...args: any[]) => unknown
4442
)(...args);
4543
};
4644
},
4745
});
48-
}, [actions, index, storeApi]);
46+
}, [actions, index, getState]);
4947

5048
const client = useAssistantStoreWithSelector({
5149
message: {

packages/react/src/context/providers/ThreadListItemProvider.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import { useMemo, type FC, type PropsWithChildren } from "react";
44
import { AssistantClientState } from "../../client/AssistantClient";
55
import { useAssistantStoreWithSelector } from "../react/utils/createAssistantStoreWithSelector";
66
import {
7-
AssistantActions,
7+
AssistantApi,
88
AssistantStoreContext,
9-
useAssistantActions,
10-
useAssistantStoreApi,
9+
useAssistantApi,
1110
} from "../react/AssistantContext";
1211
import { ThreadListItemClientActions } from "../../client/ThreadListItemClient";
1312

1413
export namespace ThreadListItemProvider {
1514
export type Props = PropsWithChildren<{
1615
id: string | ((state: AssistantClientState) => string);
17-
meta: NonNullable<AssistantActions["meta"]["threadListItem"]>;
16+
meta: NonNullable<AssistantApi["meta"]["threadListItem"]>;
1817
}>;
1918
}
2019

@@ -23,8 +22,7 @@ const ThreadListItemProvider: FC<ThreadListItemProvider.Props> = ({
2322
children,
2423
meta,
2524
}) => {
26-
const actions = useAssistantActions();
27-
const storeApi = useAssistantStoreApi();
25+
const { actions, getState } = useAssistantApi();
2826

2927
const threadListItemActions = useMemo(() => {
3028
if (typeof idOrSelector === "string") {
@@ -34,14 +32,14 @@ const ThreadListItemProvider: FC<ThreadListItemProvider.Props> = ({
3432
return new Proxy({} as ThreadListItemClientActions, {
3533
get(_, prop: keyof ThreadListItemClientActions) {
3634
return (...args: unknown[]) => {
37-
const id = idOrSelector(storeApi.getState());
35+
const id = idOrSelector(getState());
3836
return (
3937
actions.threads.item(id)[prop] as (...args: any[]) => unknown
4038
)(...args);
4139
};
4240
},
4341
});
44-
}, [actions, idOrSelector, storeApi]);
42+
}, [actions, idOrSelector, getState]);
4543

4644
const client = useAssistantStoreWithSelector({
4745
threadListItem: {

packages/react/src/context/react/AssistantContext.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type AssistantActions = AssistantClientActions & {
5353
attachment: AttachmentClientActions;
5454
};
5555

56-
export type AssistantStoreApi = Store<AssistantState, AssistantActions> & {
56+
export type AssistantApi = Store<AssistantState, AssistantActions> & {
5757
meta: {
5858
threadListItem: {
5959
query:
@@ -98,24 +98,22 @@ export type AssistantStoreApi = Store<AssistantState, AssistantActions> & {
9898
};
9999
};
100100

101-
export const AssistantStoreContext = createContext<
102-
AssistantStoreApi | undefined
103-
>(undefined);
101+
export const AssistantStoreContext = createContext<AssistantApi | undefined>(
102+
undefined,
103+
);
104104

105-
export const useAssistantStoreApi = (): AssistantStoreApi => {
105+
export const useAssistantApi = (): AssistantApi => {
106106
const context = useContext(AssistantStoreContext);
107107
if (!context)
108-
throw new Error(
109-
"useAssistantStoreApi must be used within AssistantProvider",
110-
);
108+
throw new Error("useAssistantApi must be used within AssistantProvider");
111109

112110
return context;
113111
};
114112

115113
export const useAssistantState = <T>(
116114
selector: (state: AssistantState) => T,
117115
): T => {
118-
const store = useAssistantStoreApi();
116+
const store = useAssistantApi();
119117
const slice = useSyncExternalStore(
120118
store.subscribe,
121119
() => selector(store.getState()),
@@ -125,8 +123,3 @@ export const useAssistantState = <T>(
125123

126124
return slice;
127125
};
128-
129-
export const useAssistantActions = () => {
130-
const store = useAssistantStoreApi();
131-
return store.actions;
132-
};

packages/react/src/context/react/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
// TODO createContextStoreHook does not work well with server-side nextjs bundler
33
// use client necessary here for now
44

5-
export {
6-
useAssistantState,
7-
useAssistantActions,
8-
useAssistantStoreApi,
9-
} from "./AssistantContext";
5+
export { useAssistantState, useAssistantApi } from "./AssistantContext";
106

117
export {
128
useThreadViewport,

packages/react/src/context/react/utils/createAssistantStoreWithSelector.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use client";
22
import {
33
AssistantState,
4-
AssistantStoreApi,
4+
AssistantApi,
55
AssistantActions,
6-
useAssistantStoreApi,
6+
useAssistantApi,
77
} from "../AssistantContext";
88
import { useMemo } from "react";
99
import { Store } from "../../../utils/tap-store";
@@ -18,12 +18,12 @@ type SelectorConfig<K extends SelectorKey> = {
1818
};
1919

2020
type Selectors = { [K in SelectorKey]?: SelectorConfig<K> } & {
21-
meta: Partial<AssistantStoreApi["meta"]>;
21+
meta: Partial<AssistantApi["meta"]>;
2222
};
2323

2424
class ProxiedAssistantState implements AssistantState {
2525
constructor(
26-
public readonly store: AssistantStoreApi,
26+
public readonly store: AssistantApi,
2727
selectors: Omit<Selectors, "meta">,
2828
) {
2929
Object.entries(selectors).forEach(([propName, selector]) => {
@@ -77,11 +77,11 @@ export const createAssistantStoreWithSelector = <T extends Selectors>(
7777
store: Store<
7878
Omit<AssistantState, keyof T>,
7979
Omit<AssistantActions, keyof T>
80-
> & { meta?: AssistantStoreApi["meta"] },
80+
> & { meta?: AssistantApi["meta"] },
8181
{ meta, ...selectors }: T,
82-
): AssistantStoreApi => {
82+
): AssistantApi => {
8383
const stateProxy = new ProxiedAssistantState(
84-
store as AssistantStoreApi,
84+
store as AssistantApi,
8585
selectors,
8686
);
8787
const getState = () => stateProxy;
@@ -110,7 +110,7 @@ export const createAssistantStoreWithSelector = <T extends Selectors>(
110110
export const useAssistantStoreWithSelector = <T extends Selectors>(
111111
selectors: T,
112112
) => {
113-
const store = useAssistantStoreApi();
113+
const store = useAssistantApi();
114114

115115
return useMemo(
116116
() => createAssistantStoreWithSelector(store, selectors),

packages/react/src/model-context/makeAssistantVisible.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
useContext,
1313
} from "react";
1414
import { z } from "zod";
15-
import { useAssistantActions } from "../context/react/AssistantContext";
15+
import { useAssistantApi } from "../context/react/AssistantContext";
1616
import { useComposedRefs } from "@radix-ui/react-compose-refs";
1717
import { tool } from "./tool";
1818

@@ -72,7 +72,7 @@ export const makeAssistantVisible = <T extends ComponentType<any>>(
7272
const clickId = useId();
7373
const componentRef = useRef<HTMLElement>(null);
7474

75-
const actions = useAssistantActions();
75+
const { actions } = useAssistantApi();
7676

7777
const { clickable, editable } = config ?? {};
7878
useEffect(() => {

0 commit comments

Comments
 (0)