Merge remote-tracking branch 'origin/main' into cloudflare

This commit is contained in:
javayhu 2025-09-02 00:18:06 +08:00
commit ad1cbedb56
6 changed files with 121 additions and 43 deletions

View File

@ -70,7 +70,7 @@ export default function ChatBot() {
}; };
return ( return (
<div className="mx-auto p-6 relative size-full h-screen rounded-lg bg-muted/50"> <div className="mx-auto p-6 relative size-full h-screen rounded-lg bg-muted">
<div className="flex flex-col h-full"> <div className="flex flex-col h-full">
<Conversation className="h-full"> <Conversation className="h-full">
<ConversationContent> <ConversationContent>

View File

@ -34,12 +34,13 @@ export const MessageContent = ({
className={cn( className={cn(
'flex flex-col gap-2 overflow-hidden rounded-lg px-4 py-3 text-foreground text-sm', 'flex flex-col gap-2 overflow-hidden rounded-lg px-4 py-3 text-foreground text-sm',
'group-[.is-user]:bg-primary group-[.is-user]:text-primary-foreground', 'group-[.is-user]:bg-primary group-[.is-user]:text-primary-foreground',
'group-[.is-assistant]:bg-secondary group-[.is-assistant]:text-foreground', 'group-[.is-assistant]:bg-card group-[.is-assistant]:text-card-foreground',
'is-user:dark',
className className
)} )}
{...props} {...props}
> >
<div className="is-user:dark">{children}</div> {children}
</div> </div>
); );
@ -54,10 +55,7 @@ export const MessageAvatar = ({
className, className,
...props ...props
}: MessageAvatarProps) => ( }: MessageAvatarProps) => (
<Avatar <Avatar className={cn('size-8 ring-1 ring-border', className)} {...props}>
className={cn('size-8 ring ring-1 ring-border', className)}
{...props}
>
<AvatarImage alt="" className="mt-0 mb-0" src={src} /> <AvatarImage alt="" className="mt-0 mb-0" src={src} />
<AvatarFallback>{name?.slice(0, 2) || 'ME'}</AvatarFallback> <AvatarFallback>{name?.slice(0, 2) || 'ME'}</AvatarFallback>
</Avatar> </Avatar>

View File

@ -38,13 +38,14 @@ export type ReasoningProps = ComponentProps<typeof Collapsible> & {
}; };
const AUTO_CLOSE_DELAY = 1000; const AUTO_CLOSE_DELAY = 1000;
const MS_IN_S = 1000;
export const Reasoning = memo( export const Reasoning = memo(
({ ({
className, className,
isStreaming = false, isStreaming = false,
open, open,
defaultOpen = false, defaultOpen = true,
onOpenChange, onOpenChange,
duration: durationProp, duration: durationProp,
children, children,
@ -70,23 +71,22 @@ export const Reasoning = memo(
setStartTime(Date.now()); setStartTime(Date.now());
} }
} else if (startTime !== null) { } else if (startTime !== null) {
setDuration(Math.round((Date.now() - startTime) / 1000)); setDuration(Math.round((Date.now() - startTime) / MS_IN_S));
setStartTime(null); setStartTime(null);
} }
}, [isStreaming, startTime, setDuration]); }, [isStreaming, startTime, setDuration]);
// Auto-open when streaming starts, auto-close when streaming ends (once only) // Auto-open when streaming starts, auto-close when streaming ends (once only)
useEffect(() => { useEffect(() => {
if (isStreaming && !isOpen) { if (defaultOpen && !isStreaming && isOpen && !hasAutoClosedRef) {
setIsOpen(true); // Add a small delay before closing to allow user to see the content
} else if (!isStreaming && isOpen && !defaultOpen && !hasAutoClosedRef) { const timer = setTimeout(() => {
// Add a small delay before closing to allow user to see the content setIsOpen(false);
const timer = setTimeout(() => { setHasAutoClosedRef(true);
setIsOpen(false); }, AUTO_CLOSE_DELAY);
setHasAutoClosedRef(true);
}, AUTO_CLOSE_DELAY); return () => clearTimeout(timer);
return () => clearTimeout(timer); }
}
}, [isStreaming, isOpen, defaultOpen, setIsOpen, hasAutoClosedRef]); }, [isStreaming, isOpen, defaultOpen, setIsOpen, hasAutoClosedRef]);
const handleOpenChange = (newOpen: boolean) => { const handleOpenChange = (newOpen: boolean) => {
@ -110,19 +110,10 @@ export const Reasoning = memo(
} }
); );
export type ReasoningTriggerProps = ComponentProps< export type ReasoningTriggerProps = ComponentProps<typeof CollapsibleTrigger>;
typeof CollapsibleTrigger
> & {
title?: string;
};
export const ReasoningTrigger = memo( export const ReasoningTrigger = memo(
({ ({ className, children, ...props }: ReasoningTriggerProps) => {
className,
title = 'Reasoning',
children,
...props
}: ReasoningTriggerProps) => {
const { isStreaming, isOpen, duration } = useReasoning(); const { isStreaming, isOpen, duration } = useReasoning();
return ( return (

View File

@ -0,0 +1,74 @@
'use client';
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from '@/components/ui/collapsible';
import { cn } from '@/lib/utils';
import { BookIcon, ChevronDownIcon } from 'lucide-react';
import type { ComponentProps } from 'react';
export type SourcesProps = ComponentProps<'div'>;
export const Sources = ({ className, ...props }: SourcesProps) => (
<Collapsible
className={cn('not-prose mb-4 text-primary text-xs', className)}
{...props}
/>
);
export type SourcesTriggerProps = ComponentProps<typeof CollapsibleTrigger> & {
count: number;
};
export const SourcesTrigger = ({
className,
count,
children,
...props
}: SourcesTriggerProps) => (
<CollapsibleTrigger className={cn("flex items-center gap-2", className)} {...props}>
{children ?? (
<>
<p className="font-medium">Used {count} sources</p>
<ChevronDownIcon className="h-4 w-4" />
</>
)}
</CollapsibleTrigger>
);
export type SourcesContentProps = ComponentProps<typeof CollapsibleContent>;
export const SourcesContent = ({
className,
...props
}: SourcesContentProps) => (
<CollapsibleContent
className={cn(
'mt-3 flex w-fit flex-col gap-2',
'data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 outline-none data-[state=closed]:animate-out data-[state=open]:animate-in',
className
)}
{...props}
/>
);
export type SourceProps = ComponentProps<'a'>;
export const Source = ({ href, title, children, ...props }: SourceProps) => (
<a
className="flex items-center gap-2"
href={href}
rel="noreferrer"
target="_blank"
{...props}
>
{children ?? (
<>
<BookIcon className="h-4 w-4" />
<span className="block font-medium">{title}</span>
</>
)}
</a>
);

View File

@ -50,7 +50,7 @@ const getStatusBadge = (status: ToolUIPart['state']) => {
} as const; } as const;
return ( return (
<Badge className="rounded-full text-xs" variant="secondary"> <Badge className="gap-1.5 rounded-full text-xs" variant="secondary">
{icons[status]} {icons[status]}
{labels[status]} {labels[status]}
</Badge> </Badge>

View File

@ -543,6 +543,9 @@ export class StripeProvider implements PaymentProvider {
return; return;
} }
const periodStart = this.getPeriodStart(stripeSubscription);
const periodEnd = this.getPeriodEnd(stripeSubscription);
// create fields // create fields
const createFields: any = { const createFields: any = {
id: randomUUID(), id: randomUUID(),
@ -555,12 +558,8 @@ export class StripeProvider implements PaymentProvider {
status: this.mapSubscriptionStatusToPaymentStatus( status: this.mapSubscriptionStatusToPaymentStatus(
stripeSubscription.status stripeSubscription.status
), ),
periodStart: stripeSubscription.current_period_start periodStart: periodStart,
? new Date(stripeSubscription.current_period_start * 1000) periodEnd: periodEnd,
: null,
periodEnd: stripeSubscription.current_period_end
? new Date(stripeSubscription.current_period_end * 1000)
: null,
cancelAtPeriodEnd: stripeSubscription.cancel_at_period_end, cancelAtPeriodEnd: stripeSubscription.cancel_at_period_end,
trialStart: stripeSubscription.trial_start trialStart: stripeSubscription.trial_start
? new Date(stripeSubscription.trial_start * 1000) ? new Date(stripeSubscription.trial_start * 1000)
@ -620,12 +619,8 @@ export class StripeProvider implements PaymentProvider {
.limit(1); .limit(1);
// get new period start and end // get new period start and end
const newPeriodStart = stripeSubscription.current_period_start const newPeriodStart = this.getPeriodStart(stripeSubscription);
? new Date(stripeSubscription.current_period_start * 1000) const newPeriodEnd = this.getPeriodEnd(stripeSubscription);
: undefined;
const newPeriodEnd = stripeSubscription.current_period_end
? new Date(stripeSubscription.current_period_end * 1000)
: undefined;
// Check if this is a renewal (period has changed and subscription is active) // Check if this is a renewal (period has changed and subscription is active)
const isRenewal = const isRenewal =
@ -978,4 +973,24 @@ export class StripeProvider implements PaymentProvider {
// Default to auto to let Stripe detect the language // Default to auto to let Stripe detect the language
return 'auto'; return 'auto';
} }
private getPeriodStart(subscription: Stripe.Subscription): Date | undefined {
const s: any = subscription as any;
const startUnix =
s.current_period_start ??
s?.items?.data?.[0]?.current_period_start ??
undefined;
return typeof startUnix === 'number'
? new Date(startUnix * 1000)
: undefined;
}
private getPeriodEnd(subscription: Stripe.Subscription): Date | undefined {
const s: any = subscription as any;
const endUnix =
s.current_period_end ??
s?.items?.data?.[0]?.current_period_end ??
undefined;
return typeof endUnix === 'number' ? new Date(endUnix * 1000) : undefined;
}
} }