Make document call component clickable (#558)

This commit is contained in:
Jeremy 2024-11-19 15:46:09 +03:00 committed by GitHub
parent f360ff96cc
commit 9dcdb7b50b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 15 deletions

View file

@ -1,4 +1,3 @@
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';

View file

@ -3,14 +3,19 @@ import type { SetStateAction } from 'react';
import type { UIBlock } from './block'; import type { UIBlock } from './block';
import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from './icons'; import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from './icons';
const getActionText = (type: 'create' | 'update' | 'request-suggestions') => { const getActionText = (
type: 'create' | 'update' | 'request-suggestions',
tense: 'present' | 'past',
) => {
switch (type) { switch (type) {
case 'create': case 'create':
return 'Creating'; return tense === 'present' ? 'Creating' : 'Created';
case 'update': case 'update':
return 'Updating'; return tense === 'present' ? 'Updating' : 'Updated';
case 'request-suggestions': case 'request-suggestions':
return 'Adding suggestions'; return tense === 'present'
? 'Adding suggestions'
: 'Added suggestions to';
default: default:
return null; return null;
} }
@ -26,7 +31,6 @@ interface DocumentToolResultProps {
export function DocumentToolResult({ export function DocumentToolResult({
type, type,
result, result,
block,
setBlock, setBlock,
}: DocumentToolResultProps) { }: DocumentToolResultProps) {
return ( return (
@ -62,8 +66,8 @@ export function DocumentToolResult({
<MessageIcon /> <MessageIcon />
) : null} ) : null}
</div> </div>
<div className=""> <div className="text-left">
{getActionText(type)} {result.title} {`${getActionText(type, 'past')} "${result.title}"`}
</div> </div>
</button> </button>
); );
@ -72,11 +76,35 @@ export function DocumentToolResult({
interface DocumentToolCallProps { interface DocumentToolCallProps {
type: 'create' | 'update' | 'request-suggestions'; type: 'create' | 'update' | 'request-suggestions';
args: { title: string }; args: { title: string };
setBlock: (value: SetStateAction<UIBlock>) => void;
} }
export function DocumentToolCall({ type, args }: DocumentToolCallProps) { export function DocumentToolCall({
type,
args,
setBlock,
}: DocumentToolCallProps) {
return ( return (
<div className="w-fit border py-2 px-3 rounded-xl flex flex-row items-start justify-between gap-3"> <button
type="button"
className="cursor pointer w-fit border py-2 px-3 rounded-xl flex flex-row items-start justify-between gap-3"
onClick={(event) => {
const rect = event.currentTarget.getBoundingClientRect();
const boundingBox = {
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height,
};
setBlock((currentBlock) => ({
...currentBlock,
isVisible: true,
boundingBox,
}));
}}
>
<div className="flex flex-row gap-3 items-start"> <div className="flex flex-row gap-3 items-start">
<div className="text-zinc-500 mt-1"> <div className="text-zinc-500 mt-1">
{type === 'create' ? ( {type === 'create' ? (
@ -88,12 +116,12 @@ export function DocumentToolCall({ type, args }: DocumentToolCallProps) {
) : null} ) : null}
</div> </div>
<div className=""> <div className="text-left">
{getActionText(type)} {args.title} {`${getActionText(type, 'present')} ${args.title ? `"${args.title}"` : ''}`}
</div> </div>
</div> </div>
<div className="animate-spin mt-1">{<LoaderIcon />}</div> <div className="animate-spin mt-1">{<LoaderIcon />}</div>
</div> </button>
); );
} }

View file

@ -104,13 +104,22 @@ export const PreviewMessage = ({
{toolName === 'getWeather' ? ( {toolName === 'getWeather' ? (
<Weather /> <Weather />
) : toolName === 'createDocument' ? ( ) : toolName === 'createDocument' ? (
<DocumentToolCall type="create" args={args} /> <DocumentToolCall
type="create"
args={args}
setBlock={setBlock}
/>
) : toolName === 'updateDocument' ? ( ) : toolName === 'updateDocument' ? (
<DocumentToolCall type="update" args={args} /> <DocumentToolCall
type="update"
args={args}
setBlock={setBlock}
/>
) : toolName === 'requestSuggestions' ? ( ) : toolName === 'requestSuggestions' ? (
<DocumentToolCall <DocumentToolCall
type="request-suggestions" type="request-suggestions"
args={args} args={args}
setBlock={setBlock}
/> />
) : null} ) : null}
</div> </div>