@assistant-ui/core 0.2.2 → 0.2.3
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/model-context/tool.d.ts +25 -0
- package/dist/model-context/tool.d.ts.map +1 -1
- package/dist/model-context/tool.js +25 -0
- package/dist/model-context/tool.js.map +1 -1
- package/dist/react/AssistantRuntimeProvider.d.ts +33 -0
- package/dist/react/AssistantRuntimeProvider.d.ts.map +1 -1
- package/dist/react/AssistantRuntimeProvider.js +22 -0
- package/dist/react/AssistantRuntimeProvider.js.map +1 -1
- package/dist/react/client/DataRenderers.d.ts +7 -0
- package/dist/react/client/DataRenderers.d.ts.map +1 -1
- package/dist/react/client/DataRenderers.js +7 -0
- package/dist/react/client/DataRenderers.js.map +1 -1
- package/dist/react/client/Tools.d.ts +12 -0
- package/dist/react/client/Tools.d.ts.map +1 -1
- package/dist/react/client/Tools.js +8 -0
- package/dist/react/client/Tools.js.map +1 -1
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/model-context/makeAssistantDataUI.d.ts +13 -0
- package/dist/react/model-context/makeAssistantDataUI.d.ts.map +1 -1
- package/dist/react/model-context/makeAssistantDataUI.js +6 -0
- package/dist/react/model-context/makeAssistantDataUI.js.map +1 -1
- package/dist/react/model-context/makeAssistantTool.d.ts +15 -0
- package/dist/react/model-context/makeAssistantTool.d.ts.map +1 -1
- package/dist/react/model-context/makeAssistantTool.js +8 -0
- package/dist/react/model-context/makeAssistantTool.js.map +1 -1
- package/dist/react/model-context/makeAssistantToolUI.d.ts +15 -0
- package/dist/react/model-context/makeAssistantToolUI.d.ts.map +1 -1
- package/dist/react/model-context/makeAssistantToolUI.js +8 -0
- package/dist/react/model-context/makeAssistantToolUI.js.map +1 -1
- package/dist/react/model-context/toolbox.d.ts +29 -0
- package/dist/react/model-context/toolbox.d.ts.map +1 -1
- package/dist/react/model-context/useAssistantDataUI.d.ts +9 -0
- package/dist/react/model-context/useAssistantDataUI.d.ts.map +1 -1
- package/dist/react/model-context/useAssistantDataUI.js +6 -0
- package/dist/react/model-context/useAssistantDataUI.js.map +1 -1
- package/dist/react/model-context/useAssistantTool.d.ts +34 -0
- package/dist/react/model-context/useAssistantTool.d.ts.map +1 -1
- package/dist/react/model-context/useAssistantTool.js +30 -0
- package/dist/react/model-context/useAssistantTool.js.map +1 -1
- package/dist/react/model-context/useAssistantToolUI.d.ts +12 -0
- package/dist/react/model-context/useAssistantToolUI.d.ts.map +1 -1
- package/dist/react/model-context/useAssistantToolUI.js +9 -0
- package/dist/react/model-context/useAssistantToolUI.js.map +1 -1
- package/dist/react/model-context/useToolArgsStatus.d.ts +29 -0
- package/dist/react/model-context/useToolArgsStatus.d.ts.map +1 -1
- package/dist/react/model-context/useToolArgsStatus.js +24 -0
- package/dist/react/model-context/useToolArgsStatus.js.map +1 -1
- package/dist/react/primitive-hooks/useActionBarCopy.d.ts.map +1 -1
- package/dist/react/primitive-hooks/useActionBarCopy.js +4 -3
- package/dist/react/primitive-hooks/useActionBarCopy.js.map +1 -1
- package/dist/react/primitives/messagePart/MessagePartInProgress.d.ts +6 -0
- package/dist/react/primitives/messagePart/MessagePartInProgress.d.ts.map +1 -0
- package/dist/react/primitives/messagePart/MessagePartInProgress.js +7 -0
- package/dist/react/primitives/messagePart/MessagePartInProgress.js.map +1 -0
- package/dist/react/runtimes/useToolInvocations.d.ts +9 -0
- package/dist/react/runtimes/useToolInvocations.d.ts.map +1 -1
- package/dist/react/runtimes/useToolInvocations.js +318 -264
- package/dist/react/runtimes/useToolInvocations.js.map +1 -1
- package/dist/react/types/MessagePartComponentTypes.d.ts +11 -0
- package/dist/react/types/MessagePartComponentTypes.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts +1 -0
- 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 +11 -0
- package/dist/runtimes/external-store/external-store-thread-runtime-core.js.map +1 -1
- package/dist/store/clients/model-context-client.d.ts.map +1 -1
- package/dist/store/clients/model-context-client.js +24 -4
- package/dist/store/clients/model-context-client.js.map +1 -1
- package/dist/store/scopes/model-context.d.ts +4 -1
- package/dist/store/scopes/model-context.d.ts.map +1 -1
- package/dist/types/message.d.ts +22 -0
- package/dist/types/message.d.ts.map +1 -1
- package/package.json +10 -9
- package/src/model-context/tool.ts +25 -0
- package/src/react/AssistantRuntimeProvider.tsx +33 -0
- package/src/react/client/DataRenderers.ts +7 -0
- package/src/react/client/Tools.ts +10 -0
- package/src/react/index.ts +1 -0
- package/src/react/model-context/makeAssistantDataUI.ts +13 -0
- package/src/react/model-context/makeAssistantTool.ts +15 -0
- package/src/react/model-context/makeAssistantToolUI.ts +15 -0
- package/src/react/model-context/toolbox.ts +32 -1
- package/src/react/model-context/useAssistantDataUI.ts +9 -0
- package/src/react/model-context/useAssistantTool.ts +34 -0
- package/src/react/model-context/useAssistantToolUI.ts +12 -0
- package/src/react/model-context/useToolArgsStatus.ts +29 -0
- package/src/react/primitive-hooks/useActionBarCopy.ts +9 -5
- package/src/react/primitives/messagePart/MessagePartInProgress.ts +15 -0
- package/src/react/runtimes/useToolInvocations.ts +410 -341
- package/src/react/types/MessagePartComponentTypes.ts +11 -0
- package/src/runtimes/external-store/external-store-thread-runtime-core.ts +11 -0
- package/src/store/clients/model-context-client.test.ts +108 -0
- package/src/store/clients/model-context-client.ts +36 -6
- package/src/store/scopes/model-context.ts +4 -1
- package/src/tests/external-store-thread-runtime-core.test.ts +113 -0
- package/src/types/message.ts +22 -0
|
@@ -4,10 +4,25 @@ import {
|
|
|
4
4
|
useAssistantToolUI,
|
|
5
5
|
} from "./useAssistantToolUI";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Component returned by {@link makeAssistantToolUI}.
|
|
9
|
+
*
|
|
10
|
+
* Rendering the component registers a renderer for matching tool-call message
|
|
11
|
+
* parts.
|
|
12
|
+
*/
|
|
7
13
|
export type AssistantToolUI = FC & {
|
|
14
|
+
/** Tool renderer registered by this component. */
|
|
8
15
|
unstable_tool: AssistantToolUIProps<any, any>;
|
|
9
16
|
};
|
|
10
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Creates a React component that registers a tool-call renderer when rendered.
|
|
20
|
+
*
|
|
21
|
+
* Use this to package reusable display components for tools whose definitions
|
|
22
|
+
* are registered elsewhere.
|
|
23
|
+
*
|
|
24
|
+
* @param tool - Tool renderer registration.
|
|
25
|
+
*/
|
|
11
26
|
export const makeAssistantToolUI = <TArgs, TResult>(
|
|
12
27
|
tool: AssistantToolUIProps<TArgs, TResult>,
|
|
13
28
|
) => {
|
|
@@ -5,15 +5,46 @@ type WithRender<T, TArgs extends Record<string, unknown>, TResult> = T extends {
|
|
|
5
5
|
type: "frontend" | "human";
|
|
6
6
|
}
|
|
7
7
|
? T & { render: ToolCallMessagePartComponent<TArgs, TResult> }
|
|
8
|
-
: T & {
|
|
8
|
+
: T & {
|
|
9
|
+
render?: ToolCallMessagePartComponent<TArgs, TResult> | undefined;
|
|
10
|
+
};
|
|
9
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Tool definition accepted by the React tool registry.
|
|
14
|
+
*
|
|
15
|
+
* Extends the core tool contract with a render component. Human tools rely on
|
|
16
|
+
* the renderer to collect input from the user. Frontend tools execute in the
|
|
17
|
+
* browser and require a UI surface for their progress and result. Backend
|
|
18
|
+
* tools execute server-side and may omit a renderer. The `render` component is
|
|
19
|
+
* required for frontend and human tools and optional for backend tools.
|
|
20
|
+
*/
|
|
10
21
|
export type ToolDefinition<
|
|
11
22
|
TArgs extends Record<string, unknown>,
|
|
12
23
|
TResult,
|
|
13
24
|
> = WithRender<Tool<TArgs, TResult>, TArgs, TResult>;
|
|
14
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Named collection of tools exposed to the assistant model.
|
|
28
|
+
*
|
|
29
|
+
* Keys are the tool names the model receives and uses in tool calls.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const toolkit = {
|
|
34
|
+
* get_weather: {
|
|
35
|
+
* type: "frontend",
|
|
36
|
+
* description: "Get the weather for a city.",
|
|
37
|
+
* parameters: weatherSchema,
|
|
38
|
+
* execute: async ({ city }: { city: string }) => fetchWeather(city),
|
|
39
|
+
* render: WeatherToolUI,
|
|
40
|
+
* },
|
|
41
|
+
* } satisfies Toolkit;
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
15
44
|
export type Toolkit = Record<string, ToolDefinition<any, any>>;
|
|
16
45
|
|
|
46
|
+
/** Configuration for the {@link Tools} resource. */
|
|
17
47
|
export type ToolsConfig = {
|
|
48
|
+
/** Tools to register with model context and, when provided, message renderers. */
|
|
18
49
|
toolkit: Toolkit;
|
|
19
50
|
};
|
|
@@ -2,11 +2,20 @@ import { useEffect } from "react";
|
|
|
2
2
|
import { useAui } from "@assistant-ui/store";
|
|
3
3
|
import type { DataMessagePartComponent } from "../types/MessagePartComponentTypes";
|
|
4
4
|
|
|
5
|
+
/** Props used to register a renderer for `data` message parts. */
|
|
5
6
|
export type AssistantDataUIProps<T = any> = {
|
|
7
|
+
/** Data part name this renderer handles. */
|
|
6
8
|
name: string;
|
|
9
|
+
/** Component rendered for matching data message parts. */
|
|
7
10
|
render: DataMessagePartComponent<T>;
|
|
8
11
|
};
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Registers a renderer for named `data` message parts while the component is
|
|
15
|
+
* mounted.
|
|
16
|
+
*
|
|
17
|
+
* @param dataUI - Data renderer registration, or `null` to skip registration.
|
|
18
|
+
*/
|
|
10
19
|
export const useAssistantDataUI = (dataUI: AssistantDataUIProps | null) => {
|
|
11
20
|
const aui = useAui();
|
|
12
21
|
useEffect(() => {
|
|
@@ -3,13 +3,47 @@ import { useAui } from "@assistant-ui/store";
|
|
|
3
3
|
import type { ToolCallMessagePartComponent } from "../types/MessagePartComponentTypes";
|
|
4
4
|
import type { AssistantToolProps as CoreAssistantToolProps } from "../..";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Props used to register a tool from React.
|
|
8
|
+
*/
|
|
6
9
|
export type AssistantToolProps<
|
|
7
10
|
TArgs extends Record<string, unknown>,
|
|
8
11
|
TResult,
|
|
9
12
|
> = CoreAssistantToolProps<TArgs, TResult> & {
|
|
13
|
+
/** Component used to render calls to this tool in assistant messages. */
|
|
10
14
|
render?: ToolCallMessagePartComponent<TArgs, TResult> | undefined;
|
|
11
15
|
};
|
|
12
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Registers a tool with the assistant model context while the component is
|
|
19
|
+
* mounted.
|
|
20
|
+
*
|
|
21
|
+
* If `render` is provided, it is also installed as the renderer for matching
|
|
22
|
+
* tool-call message parts. The registration is removed automatically when the
|
|
23
|
+
* component unmounts or the tool definition changes.
|
|
24
|
+
*
|
|
25
|
+
* Pass a referentially stable tool object, such as one declared at module
|
|
26
|
+
* scope or memoized with `useMemo`, to avoid re-registering on every render.
|
|
27
|
+
*
|
|
28
|
+
* @param tool - Tool definition and name to register.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* const weatherTool = {
|
|
33
|
+
* toolName: "get_weather",
|
|
34
|
+
* type: "frontend",
|
|
35
|
+
* description: "Get the weather for a city.",
|
|
36
|
+
* parameters: weatherSchema,
|
|
37
|
+
* execute: async ({ city }: { city: string }) => fetchWeather(city),
|
|
38
|
+
* render: WeatherToolUI,
|
|
39
|
+
* } satisfies AssistantToolProps<{ city: string }, Weather>;
|
|
40
|
+
*
|
|
41
|
+
* function WeatherToolRegistration() {
|
|
42
|
+
* useAssistantTool(weatherTool);
|
|
43
|
+
* return null;
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
13
47
|
export const useAssistantTool = <
|
|
14
48
|
TArgs extends Record<string, unknown>,
|
|
15
49
|
TResult,
|
|
@@ -2,11 +2,23 @@ import { useEffect } from "react";
|
|
|
2
2
|
import { useAui } from "@assistant-ui/store";
|
|
3
3
|
import type { ToolCallMessagePartComponent } from "../types/MessagePartComponentTypes";
|
|
4
4
|
|
|
5
|
+
/** Props used to register a renderer for tool-call message parts. */
|
|
5
6
|
export type AssistantToolUIProps<TArgs, TResult> = {
|
|
7
|
+
/** Name of the tool whose calls should use this renderer. */
|
|
6
8
|
toolName: string;
|
|
9
|
+
/** Component rendered for matching tool-call message parts. */
|
|
7
10
|
render: ToolCallMessagePartComponent<TArgs, TResult>;
|
|
8
11
|
};
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Registers a tool-call renderer while the component is mounted.
|
|
15
|
+
*
|
|
16
|
+
* This only affects rendering. Pair it with {@link useAssistantTool},
|
|
17
|
+
* {@link Tools}, or a backend tool registry to expose the actual tool
|
|
18
|
+
* definition to the model.
|
|
19
|
+
*
|
|
20
|
+
* @param tool - Tool renderer registration, or `null` to skip registration.
|
|
21
|
+
*/
|
|
10
22
|
export const useAssistantToolUI = (
|
|
11
23
|
tool: AssistantToolUIProps<any, any> | null,
|
|
12
24
|
) => {
|
|
@@ -7,13 +7,42 @@ import {
|
|
|
7
7
|
|
|
8
8
|
type PropFieldStatus = "streaming" | "complete";
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Streaming completion status for the arguments of the current tool call.
|
|
12
|
+
*/
|
|
10
13
|
export type ToolArgsStatus<
|
|
11
14
|
TArgs extends Record<string, unknown> = Record<string, unknown>,
|
|
12
15
|
> = {
|
|
16
|
+
/** Overall lifecycle state of the tool-call part. */
|
|
13
17
|
status: "running" | "complete" | "incomplete" | "requires-action";
|
|
18
|
+
/** Per-argument status keyed by argument name. */
|
|
14
19
|
propStatus: Partial<Record<keyof TArgs, PropFieldStatus>>;
|
|
15
20
|
};
|
|
16
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Reads whether each argument field for the current tool-call message part is
|
|
24
|
+
* still streaming or complete.
|
|
25
|
+
*
|
|
26
|
+
* Use inside a tool-call renderer to avoid showing incomplete argument values
|
|
27
|
+
* as final.
|
|
28
|
+
*
|
|
29
|
+
* @throws If called outside a tool-call message part.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* function WeatherToolUI({
|
|
34
|
+
* args,
|
|
35
|
+
* }: ToolCallMessagePartProps<{ city: string }>) {
|
|
36
|
+
* const { propStatus } = useToolArgsStatus<{ city: string }>();
|
|
37
|
+
*
|
|
38
|
+
* return (
|
|
39
|
+
* <span>
|
|
40
|
+
* {propStatus.city === "streaming" ? "Reading city..." : args.city}
|
|
41
|
+
* </span>
|
|
42
|
+
* );
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
17
46
|
export const useToolArgsStatus = <
|
|
18
47
|
TArgs extends Record<string, unknown> = Record<string, unknown>,
|
|
19
48
|
>(): ToolArgsStatus<TArgs> => {
|
|
@@ -27,11 +27,15 @@ export const useActionBarCopy = ({
|
|
|
27
27
|
if (!valueToCopy) return;
|
|
28
28
|
|
|
29
29
|
const write = copyToClipboard ?? (() => {});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
// The rejection handler swallows clipboard write failures (permission denied,
|
|
31
|
+
// API unavailable) so they don't surface as unhandled promise rejections.
|
|
32
|
+
Promise.resolve(write(valueToCopy)).then(
|
|
33
|
+
() => {
|
|
34
|
+
aui.message().setIsCopied(true);
|
|
35
|
+
setTimeout(() => aui.message().setIsCopied(false), copiedDuration);
|
|
36
|
+
},
|
|
37
|
+
() => {},
|
|
38
|
+
);
|
|
35
39
|
}, [aui, isEditing, composerValue, copiedDuration, copyToClipboard]);
|
|
36
40
|
|
|
37
41
|
return { copy, disabled, isCopied };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FC, PropsWithChildren } from "react";
|
|
2
|
+
import { useAuiState } from "@assistant-ui/store";
|
|
3
|
+
|
|
4
|
+
export namespace MessagePartPrimitiveInProgress {
|
|
5
|
+
export type Props = PropsWithChildren;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const MessagePartPrimitiveInProgress: FC<
|
|
9
|
+
MessagePartPrimitiveInProgress.Props
|
|
10
|
+
> = ({ children }) => {
|
|
11
|
+
const isInProgress = useAuiState((s) => s.part.status.type === "running");
|
|
12
|
+
return isInProgress ? children : null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
MessagePartPrimitiveInProgress.displayName = "MessagePartPrimitive.InProgress";
|