;
}) {
const { select, setOpen } = useArtifacts();
if (name === "web_search") {
let label: React.ReactNode = "Search for related information";
if (typeof args.query === "string") {
label = (
Search on the web for{" "}
"{args.query}"
);
}
return (
{Array.isArray(result) && (
{result.map((item) => (
{item.title}
))}
)}
);
} else if (name === "web_fetch") {
const url = (args as { url: string })?.url;
let title = url;
if (typeof result === "string") {
const potentialTitle = extractTitleFromMarkdown(result);
if (potentialTitle && potentialTitle.toLowerCase() !== "untitled") {
title = potentialTitle;
}
}
return (
{
window.open(url, "_blank");
}}
>
{url && (
{title}
)}
);
} else if (name === "ls") {
let description: string | undefined = (args as { description: string })
?.description;
if (!description) {
description = "List folder";
}
const path: string | undefined = (args as { path: string })?.path;
return (
{path && (
{path}
)}
);
} else if (name === "read_file") {
let description: string | undefined = (args as { description: string })
?.description;
if (!description) {
description = "Read file";
}
const path: string | undefined = (args as { path: string })?.path;
return (
{path && (
{path}
)}
);
} else if (name === "write_file" || name === "str_replace") {
let description: string | undefined = (args as { description: string })
?.description;
if (!description) {
description = "Write file";
}
const path: string | undefined = (args as { path: string })?.path;
return (
{
select(
new URL(
`write-file:${path}?message_id=${messageId}&tool_call_id=${id}`,
).toString(),
);
setOpen(true);
}}
>
{path && (
{path}
)}
);
} else if (name === "bash") {
const description: string | undefined = (args as { description: string })
?.description;
if (!description) {
return "Execute command";
}
const command: string | undefined = (args as { command: string })?.command;
return (
{command && (
{command}
)}
);
} else if (name === "present_files") {
return (
{Array.isArray((args as { filepaths: string[] }).filepaths) &&
(args as { filepaths: string[] }).filepaths.map(
(filepath: string) => (
{filepath}
),
)}
);
} else {
const description: string | undefined = (args as { description: string })
?.description;
return (
Use "{name}" tool
)
}
icon={WrenchIcon}
>
);
}
}
interface GenericCoTStep {
id?: string;
messageId?: string;
type: T;
}
interface CoTReasoningStep extends GenericCoTStep<"reasoning"> {
reasoning: string | null;
}
interface CoTToolCallStep extends GenericCoTStep<"toolCall"> {
name: string;
args: Record;
result?: string;
}
type CoTStep = CoTReasoningStep | CoTToolCallStep;
function convertToSteps(messages: Message[]): CoTStep[] {
const steps: CoTStep[] = [];
for (const message of messages) {
if (message.type === "ai") {
const reasoning = extractReasoningContentFromMessage(message);
if (reasoning) {
const step: CoTReasoningStep = {
id: message.id,
messageId: message.id,
type: "reasoning",
reasoning: extractReasoningContentFromMessage(message),
};
steps.push(step);
}
for (const tool_call of message.tool_calls ?? []) {
const step: CoTToolCallStep = {
id: tool_call.id,
messageId: message.id,
type: "toolCall",
name: tool_call.name,
args: tool_call.args,
};
const toolCallId = tool_call.id;
if (toolCallId) {
const toolCallResult = findToolCallResult(toolCallId, messages);
if (toolCallResult) {
try {
const json = JSON.parse(toolCallResult);
step.result = json;
} catch {
step.result = toolCallResult;
}
}
}
steps.push(step);
}
}
}
return steps;
}