2025-09-21 11:02:31 -07:00
|
|
|
import { memo } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import { useArtifact } from "@/hooks/use-artifact";
|
|
|
|
|
import type { ArtifactKind } from "./artifact";
|
|
|
|
|
import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from "./icons";
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2024-11-19 15:46:09 +03:00
|
|
|
const getActionText = (
|
2025-09-21 11:02:31 -07:00
|
|
|
type: "create" | "update" | "request-suggestions",
|
|
|
|
|
tense: "present" | "past"
|
2024-11-19 15:46:09 +03:00
|
|
|
) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
switch (type) {
|
2025-09-21 11:02:31 -07:00
|
|
|
case "create":
|
|
|
|
|
return tense === "present" ? "Creating" : "Created";
|
|
|
|
|
case "update":
|
|
|
|
|
return tense === "present" ? "Updating" : "Updated";
|
|
|
|
|
case "request-suggestions":
|
|
|
|
|
return tense === "present"
|
|
|
|
|
? "Adding suggestions"
|
|
|
|
|
: "Added suggestions to";
|
2024-10-30 16:01:24 +05:30
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
type DocumentToolResultProps = {
|
|
|
|
|
type: "create" | "update" | "request-suggestions";
|
2025-02-13 08:25:57 -08:00
|
|
|
result: { id: string; title: string; kind: ArtifactKind };
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2025-09-21 11:02:31 -07:00
|
|
|
};
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
function PureDocumentToolResult({
|
2024-10-30 16:01:24 +05:30
|
|
|
type,
|
|
|
|
|
result,
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly,
|
2024-10-30 16:01:24 +05:30
|
|
|
}: DocumentToolResultProps) {
|
2025-02-13 08:25:57 -08:00
|
|
|
const { setArtifact } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
2024-11-15 13:00:15 -05:00
|
|
|
<button
|
2025-09-09 15:44:07 -04:00
|
|
|
className="flex w-fit cursor-pointer flex-row items-start gap-3 rounded-xl border bg-background px-3 py-2"
|
2024-10-30 16:01:24 +05:30
|
|
|
onClick={(event) => {
|
2024-12-06 13:36:56 +03:00
|
|
|
if (isReadonly) {
|
|
|
|
|
toast.error(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Viewing files in shared chats is currently not supported."
|
2024-12-06 13:36:56 +03:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
const rect = event.currentTarget.getBoundingClientRect();
|
|
|
|
|
|
|
|
|
|
const boundingBox = {
|
|
|
|
|
top: rect.top,
|
|
|
|
|
left: rect.left,
|
|
|
|
|
width: rect.width,
|
|
|
|
|
height: rect.height,
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-01 07:11:22 +07:00
|
|
|
setArtifact((currentArtifact) => ({
|
2024-11-01 15:31:54 +05:30
|
|
|
documentId: result.id,
|
2024-12-16 18:14:40 +05:30
|
|
|
kind: result.kind,
|
2025-11-01 07:11:22 +07:00
|
|
|
content: currentArtifact.content,
|
2024-11-01 15:31:54 +05:30
|
|
|
title: result.title,
|
|
|
|
|
isVisible: true,
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "idle",
|
2024-11-01 15:31:54 +05:30
|
|
|
boundingBox,
|
2025-11-01 07:11:22 +07:00
|
|
|
}));
|
2024-10-30 16:01:24 +05:30
|
|
|
}}
|
2025-09-21 11:02:31 -07:00
|
|
|
type="button"
|
2024-10-30 16:01:24 +05:30
|
|
|
>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="mt-1 text-muted-foreground">
|
2025-09-21 11:02:31 -07:00
|
|
|
{type === "create" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<FileIcon />
|
2025-09-21 11:02:31 -07:00
|
|
|
) : type === "update" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<PencilEditIcon />
|
2025-09-21 11:02:31 -07:00
|
|
|
) : type === "request-suggestions" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<MessageIcon />
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2024-11-19 15:46:09 +03:00
|
|
|
<div className="text-left">
|
2025-09-21 11:02:31 -07:00
|
|
|
{`${getActionText(type, "past")} "${result.title}"`}
|
2024-10-30 16:01:24 +05:30
|
|
|
</div>
|
2024-11-15 13:00:15 -05:00
|
|
|
</button>
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
export const DocumentToolResult = memo(PureDocumentToolResult, () => true);
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
type DocumentToolCallProps = {
|
|
|
|
|
type: "create" | "update" | "request-suggestions";
|
2025-07-03 02:26:34 -07:00
|
|
|
args:
|
|
|
|
|
| { title: string; kind: ArtifactKind } // for create
|
|
|
|
|
| { id: string; description: string } // for update
|
|
|
|
|
| { documentId: string }; // for request-suggestions
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2025-09-21 11:02:31 -07:00
|
|
|
};
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2024-12-06 13:36:56 +03:00
|
|
|
function PureDocumentToolCall({
|
|
|
|
|
type,
|
|
|
|
|
args,
|
|
|
|
|
isReadonly,
|
|
|
|
|
}: DocumentToolCallProps) {
|
2025-02-13 08:25:57 -08:00
|
|
|
const { setArtifact } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
2024-11-19 15:46:09 +03:00
|
|
|
<button
|
2025-09-09 15:44:07 -04:00
|
|
|
className="cursor pointer flex w-fit flex-row items-start justify-between gap-3 rounded-xl border px-3 py-2"
|
2024-11-19 15:46:09 +03:00
|
|
|
onClick={(event) => {
|
2024-12-06 13:36:56 +03:00
|
|
|
if (isReadonly) {
|
|
|
|
|
toast.error(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Viewing files in shared chats is currently not supported."
|
2024-12-06 13:36:56 +03:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 15:46:09 +03:00
|
|
|
const rect = event.currentTarget.getBoundingClientRect();
|
|
|
|
|
|
|
|
|
|
const boundingBox = {
|
|
|
|
|
top: rect.top,
|
|
|
|
|
left: rect.left,
|
|
|
|
|
width: rect.width,
|
|
|
|
|
height: rect.height,
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((currentArtifact) => ({
|
|
|
|
|
...currentArtifact,
|
2024-11-19 15:46:09 +03:00
|
|
|
isVisible: true,
|
|
|
|
|
boundingBox,
|
|
|
|
|
}));
|
|
|
|
|
}}
|
2025-09-21 11:02:31 -07:00
|
|
|
type="button"
|
2024-11-19 15:46:09 +03:00
|
|
|
>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex flex-row items-start gap-3">
|
|
|
|
|
<div className="mt-1 text-zinc-500">
|
2025-09-21 11:02:31 -07:00
|
|
|
{type === "create" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<FileIcon />
|
2025-09-21 11:02:31 -07:00
|
|
|
) : type === "update" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<PencilEditIcon />
|
2025-09-21 11:02:31 -07:00
|
|
|
) : type === "request-suggestions" ? (
|
2024-10-30 16:01:24 +05:30
|
|
|
<MessageIcon />
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-11-19 15:46:09 +03:00
|
|
|
<div className="text-left">
|
2025-09-21 11:02:31 -07:00
|
|
|
{`${getActionText(type, "present")} ${
|
|
|
|
|
type === "create" && "title" in args && args.title
|
2025-07-03 02:26:34 -07:00
|
|
|
? `"${args.title}"`
|
2025-09-21 11:02:31 -07:00
|
|
|
: type === "update" && "description" in args
|
2025-07-03 02:26:34 -07:00
|
|
|
? `"${args.description}"`
|
2025-09-21 11:02:31 -07:00
|
|
|
: type === "request-suggestions"
|
|
|
|
|
? "for document"
|
|
|
|
|
: ""
|
2025-07-03 02:26:34 -07:00
|
|
|
}`}
|
2024-10-30 16:01:24 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="mt-1 animate-spin">{<LoaderIcon />}</div>
|
2024-11-19 15:46:09 +03:00
|
|
|
</button>
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
}
|
2024-12-03 17:49:38 +03:00
|
|
|
|
|
|
|
|
export const DocumentToolCall = memo(PureDocumentToolCall, () => true);
|