Skip to content

Commit 9f01754

Browse files
committed
feat: Internalize useCallbackRef and composeEventHandlers, introduce ThreadRoot primitive, and update component imports to use runtime components.
1 parent 6a512fc commit 9f01754

37 files changed

Lines changed: 807 additions & 75 deletions

packages-test-3/ai-chat/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"./primitives/thread-list-item-more": "./src/primitives/thread-list-item-more/index.ts",
2222
"./runtime": "./src/runtime.tsx",
2323
"./component-types": "./src/component-types.ts",
24+
"./component-type-check": "./src/component-type-check.ts",
2425
"./hook-types": "./src/hook-types.ts",
2526
"./utils": "./src/utils/index.ts",
2627
"./hooks": "./src/hooks/index.ts",

packages-test-3/ai-chat/src/adapters/local-storage-adapter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
22

3-
import { CustomUIDataTypes } from "../primitives/thread/thread-root";
43
import type { ThreadAdapter } from "../types/adapters";
54
import type { Thread, Threads } from "../types/entities";
65
import type { UIMessage } from "ai";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type {RuntimeComponents} from './component-types'

packages-test-3/ai-chat/src/component-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Event = {
2424
export interface RuntimeComponents {
2525
Box: ComponentType<BaseComponentPropsWithChildren>
2626
Text: ComponentType<BaseComponentPropsWithChildren>
27+
Form: ComponentType<BaseComponentPropsWithChildren & { onSubmit?: (e: Event) => void; }>
2728
Button: ButtonComponent
2829
ScrollArea: ComponentType<BaseComponentPropsWithChildren>
2930
Input: ComponentType<BaseComponentProps & { value?: string; onChange?: (e: Event) => void; placeholder?: string }>

packages-test-3/ai-chat/src/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from './use-escape-keydown'
22
export * from './use-managed-ref'
33
export * from './use-on-resize-content'
44
export * from './use-on-scroll-to-bottom'
5-
export * from './use-size-handle'
5+
export * from './use-size-handle'
6+
export * from './use-callback-ref'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useCallback, useEffect, useRef } from 'react'
2+
3+
/**
4+
* A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a
5+
* prop or avoid re-executing effects when passed as a dependency
6+
*/
7+
export function useCallbackRef<T extends (...args: any[]) => any>(
8+
callback: T | undefined
9+
): T {
10+
const callbackRef = useRef(callback)
11+
12+
useEffect(() => {
13+
callbackRef.current = callback
14+
})
15+
16+
// Return a memoized callback that will always call the latest callback
17+
return useCallback(((...args) => callbackRef.current?.(...args)) as T, [])
18+
}

packages-test-3/ai-chat/src/hooks/use-on-resize-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
21
import { useCallback } from "react";
32
import { useManagedRef } from "./use-managed-ref";
3+
import { useCallbackRef } from "./use-callback-ref";
44

55
export const useOnResizeContent = (callback: () => void) => {
66
const callbackRef = useCallbackRef(callback);

packages-test-3/ai-chat/src/hooks/use-on-scroll-to-bottom.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use client";
22

3-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
43
import { useEffect } from "react";
54
import { useThreadViewport } from "../primitives/thread/thread-viewport-context";
5+
import { useCallbackRef } from "./use-callback-ref";
66

77
export const useOnScrollToBottom = (
88
callback: (config: { behavior: ScrollBehavior }) => void,

packages-test-3/ai-chat/src/model-context/use-assistant-tool-ui.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useEffect } from "react";
4-
import { useThread } from "../primitives/thread";
4+
import { useThread } from "@creatorem/ai-chat/primitives/thread";
55
import type { ToolCallMessagePartComponent } from "../types/message-part-component-types";
66

77
export type AssistantToolUIProps<TArgs, TResult> = {

packages-test-3/ai-chat/src/model-context/use-assistant-tool.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useEffect } from "react";
4-
import { useThread } from "../primitives/thread";
4+
import { useThread } from "@creatorem/ai-chat/primitives/thread";
55
import type { ToolCallMessagePartComponent } from "../types/message-part-component-types";
66
import type { Tool } from "@creatorem/stream";
77

0 commit comments

Comments
 (0)