Skip to content

Commit 787dfc3

Browse files
comynliComyn LI 李学明haydenbleasel
authored
feat(reasoning): ReasoningTrigger allows custom getThinkingMessage function (#255)
* feat(reasoning): ReasoningTrigger allows custom getThinkingMessage function * Update reasoning.mdx * Create giant-streets-swim.md --------- Co-authored-by: Comyn LI 李学明 <[email protected]> Co-authored-by: Hayden Bleasel <[email protected]>
1 parent 68bc484 commit 787dfc3

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.changeset/giant-streets-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ai-elements": patch
3+
---
4+
5+
ReasoningTrigger allows custom getThinkingMessage function

apps/docs/content/docs/components/(chatbot)/reasoning.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ export async function POST(req: Request) {
175175
type: 'string',
176176
default: '"Reasoning"',
177177
},
178+
getThinkingMessage: {
179+
description: 'Optional function to customize the thinking message. Receives isStreaming and duration parameters.',
180+
type: '(isStreaming: boolean, duration?: number) => ReactNode',
181+
},
178182
'...props': {
179183
description: 'Any other props are spread to the underlying CollapsibleTrigger component.',
180184
type: 'React.ComponentProps<typeof CollapsibleTrigger>',

packages/elements/src/reasoning.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@repo/shadcn-ui/components/ui/collapsible";
99
import { cn } from "@repo/shadcn-ui/lib/utils";
1010
import { BrainIcon, ChevronDownIcon } from "lucide-react";
11-
import type { ComponentProps } from "react";
11+
import type { ComponentProps, ReactNode } from "react";
1212
import { createContext, memo, useContext, useEffect, useState } from "react";
1313
import { Streamdown } from "streamdown";
1414
import { Shimmer } from "./shimmer";
@@ -22,7 +22,7 @@ type ReasoningContextValue = {
2222

2323
const ReasoningContext = createContext<ReasoningContextValue | null>(null);
2424

25-
const useReasoning = () => {
25+
export const useReasoning = () => {
2626
const context = useContext(ReasoningContext);
2727
if (!context) {
2828
throw new Error("Reasoning components must be used within Reasoning");
@@ -111,9 +111,11 @@ export const Reasoning = memo(
111111
}
112112
);
113113

114-
export type ReasoningTriggerProps = ComponentProps<typeof CollapsibleTrigger>;
114+
export type ReasoningTriggerProps = ComponentProps<typeof CollapsibleTrigger> & {
115+
getThinkingMessage?: (isStreaming: boolean, duration?: number) => ReactNode;
116+
};
115117

116-
const getThinkingMessage = (isStreaming: boolean, duration?: number) => {
118+
const defaultGetThinkingMessage = (isStreaming: boolean, duration?: number) => {
117119
if (isStreaming || duration === 0) {
118120
return <Shimmer duration={1}>Thinking...</Shimmer>;
119121
}
@@ -124,7 +126,7 @@ const getThinkingMessage = (isStreaming: boolean, duration?: number) => {
124126
};
125127

126128
export const ReasoningTrigger = memo(
127-
({ className, children, ...props }: ReasoningTriggerProps) => {
129+
({ className, children, getThinkingMessage = defaultGetThinkingMessage ...props }: ReasoningTriggerProps) => {
128130
const { isStreaming, isOpen, duration } = useReasoning();
129131

130132
return (

0 commit comments

Comments
 (0)