Skip to content

Commit

Permalink
feat: ContentPartComponent types (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 20, 2024
1 parent 4d42eb9 commit a6769d5
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 55 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-feet-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@assistant-ui/react-hook-form": patch
"@assistant-ui/react": patch
---

feat: ContentPartComponent types
4 changes: 2 additions & 2 deletions apps/www/pages/reference/integrations/react-hook-form.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Drop-in replacement hook for `useForm` that adds support for `@assistant-ui/reac
parameters: [
{
name: "renderer",
type: "ToolContentPartRenderer<{ name: string; value: string; }, {}>",
type: "ToolCallContentPartComponent<{ name: string; value: string; }, {}>",
description:
"The component to render when set_form_field is called.",
},
Expand All @@ -67,7 +67,7 @@ Drop-in replacement hook for `useForm` that adds support for `@assistant-ui/reac
parameters: [
{
name: "renderer",
type: "ToolContentPartRenderer<{}, {}>",
type: "ToolCallContentPartComponent<{}, {}>",
description:
"The component to render when submit_form is called.",
},
Expand Down
2 changes: 1 addition & 1 deletion apps/www/pages/reference/runtime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MyApp = () => {
};
```

### Properties
#### Properties

<ParametersTable
parameters={[
Expand Down
6 changes: 3 additions & 3 deletions packages/react-hook-form/src/useAssistantForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {
type ModelConfig,
type ToolCallContentPartComponent,
useAssistantContext,
useAssistantToolRenderer,
} from "@assistant-ui/react/experimental";
Expand All @@ -13,7 +14,6 @@ import {
useForm,
} from "react-hook-form";
import type { z } from "zod";
import type { ToolRenderComponent } from "../../react/src/model-config/ToolRenderComponent";
import { formTools } from "./formTools";

type UseAssistantFormProps<
Expand All @@ -23,13 +23,13 @@ type UseAssistantFormProps<
assistant?: {
tools?: {
set_form_field?: {
render?: ToolRenderComponent<
render?: ToolCallContentPartComponent<
z.ZodType<typeof formTools.set_form_field>,
unknown
>;
};
submit_form?: {
render?: ToolRenderComponent<
render?: ToolCallContentPartComponent<
z.ZodType<typeof formTools.submit_form>,
unknown
>;
Expand Down
11 changes: 4 additions & 7 deletions packages/react/src/context/stores/AssistantToolRenderers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
"use client";

import { create } from "zustand";
import type { ToolRenderComponent } from "../../model-config/ToolRenderComponent";
import type { ToolCallContentPartComponent } from "../../primitives/message/ContentPartComponentTypes";

export type AssistantToolRenderersState = {
// biome-ignore lint/suspicious/noExplicitAny: intentional any
getToolRenderer: (name: string) => ToolRenderComponent<any, any> | null;
getToolRenderer: (name: string) => ToolCallContentPartComponent | null;
setToolRenderer: (
name: string,
// biome-ignore lint/suspicious/noExplicitAny: intentional any
render: ToolRenderComponent<any, any>,
render: ToolCallContentPartComponent,
) => () => void;
};

export const makeAssistantToolRenderersStore = () =>
create<AssistantToolRenderersState>((set) => {
// biome-ignore lint/suspicious/noExplicitAny: intentional any
const renderers = new Map<string, ToolRenderComponent<any, any>[]>();
const renderers = new Map<string, ToolCallContentPartComponent[]>();

return {
getToolRenderer: (name) => {
Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export type {
ModelConfig,
} from "./utils/ModelConfigTypes";

export type {
TextContentPartProps,
TextContentPartComponent,
ImageContentPartProps,
ImageContentPartComponent,
UIContentPartProps,
UIContentPartComponent,
ToolCallContentPartProps,
ToolCallContentPartComponent,
} from "./primitives/message/ContentPartComponentTypes";

export * from "./context";
export { useAssistantInstructions } from "./model-config/useAssistantInstructions";
export { useAssistantTool } from "./model-config/useAssistantTool";
Expand Down
8 changes: 0 additions & 8 deletions packages/react/src/model-config/ToolRenderComponent.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/react/src/model-config/useAssistantTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import { useEffect } from "react";
import { useAssistantContext } from "../context/AssistantContext";
import type { ToolCallContentPartComponent } from "../primitives/message/ContentPartComponentTypes";
import type { Tool } from "../utils/ModelConfigTypes";
import type { ToolRenderComponent } from "./ToolRenderComponent";

export type UseAssistantTool<TArgs, TResult> = Tool<TArgs, TResult> & {
name: string;
render?: ToolRenderComponent<TArgs, TResult>;
render?: ToolCallContentPartComponent<TArgs, TResult>;
};

export const useAssistantTool = <TArgs, TResult>(
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/model-config/useAssistantToolRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use client";
import { useEffect } from "react";
import { useAssistantContext } from "../context/AssistantContext";
import type { ToolRenderComponent } from "./ToolRenderComponent";
import type { ToolCallContentPartComponent } from "../primitives/message/ContentPartComponentTypes";

type UseAssistantToolRenderer<TArgs, TResult> = {
name: string;
render: ToolRenderComponent<TArgs, TResult>;
render: ToolCallContentPartComponent<TArgs, TResult>;
};

export const useAssistantToolRenderer = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use client";
import type { ComponentType } from "react";
import type {
ImageContentPart,
TextContentPart,
ToolCallContentPart,
UIContentPart,
} from "../../utils/AssistantTypes";

type ContentPartStatus = "done" | "in_progress" | "error";

export type TextContentPartProps = {
part: TextContentPart;
status: ContentPartStatus;
};
export type TextContentPartComponent = ComponentType<TextContentPartProps>;

export type ImageContentPartProps = {
part: ImageContentPart;
status: ContentPartStatus;
};
export type ImageContentPartComponent = ComponentType<ImageContentPartProps>;

export type UIContentPartProps = {
part: UIContentPart;
status: ContentPartStatus;
};
export type UIContentPartComponent = ComponentType<UIContentPartProps>;

// biome-ignore lint/suspicious/noExplicitAny: intentional any
export type ToolCallContentPartProps<TArgs = any, TResult = any> = {
part: ToolCallContentPart<TArgs, TResult>;
status: ContentPartStatus;
};

export type ToolCallContentPartComponent<
// biome-ignore lint/suspicious/noExplicitAny: intentional any
TArgs = any,
// biome-ignore lint/suspicious/noExplicitAny: intentional any
TResult = any,
> = ComponentType<ToolCallContentPartProps<TArgs, TResult>>;
43 changes: 13 additions & 30 deletions packages/react/src/primitives/message/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,25 @@ import { type ComponentType, type FC, memo } from "react";
import { useAssistantContext, useContentPartContext } from "../../context";
import { useMessageContext } from "../../context/MessageContext";
import { ContentPartProvider } from "../../context/providers/ContentPartProvider";
import type {
ImageContentPart,
TextContentPart,
ToolCallContentPart,
UIContentPart,
} from "../../utils/AssistantTypes";
import { ContentPartDisplay } from "../contentPart/ContentPartDisplay";
import { ContentPartInProgressIndicator } from "../contentPart/ContentPartInProgressIndicator";
import { ContentPartText } from "../contentPart/ContentPartText";
import type {
ImageContentPartComponent,
TextContentPartComponent,
ToolCallContentPartComponent,
ToolCallContentPartProps,
UIContentPartComponent,
} from "./ContentPartComponentTypes";

type MessageContentProps = {
export type MessageContentProps = {
components?: {
Text?: ComponentType<{
part: TextContentPart;
status: "done" | "in_progress" | "error";
}>;
Image?: ComponentType<{
part: ImageContentPart;
status: "done" | "in_progress" | "error";
}>;
UI?: ComponentType<{
part: UIContentPart;
status: "done" | "in_progress" | "error";
}>;
Text?: TextContentPartComponent;
Image?: ImageContentPartComponent;
UI?: UIContentPartComponent;
tools?: {
by_name?: Record<
string,
ComponentType<{
part: ToolCallContentPart;
status: "done" | "in_progress" | "error";
}>
>;
Fallback?: ComponentType<{
part: ToolCallContentPart;
status: "done" | "in_progress" | "error";
}>;
by_name?: Record<string, ToolCallContentPartComponent>;
Fallback?: ComponentType<ToolCallContentPartProps>;
};
};
};
Expand Down

0 comments on commit a6769d5

Please sign in to comment.