Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions console/src/pages/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
IAgentScopeRuntimeWebUIOptions,
} from "@agentscope-ai/chat";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Button, Modal, Result } from "antd";
import { Button, Modal, Result, message } from "antd";
import { ExclamationCircleOutlined, SettingOutlined } from "@ant-design/icons";
import { SparkCopyLine } from "@agentscope-ai/icons";
import { useTranslation } from "react-i18next";
import { useLocation, useNavigate } from "react-router-dom";
import sessionApi from "./sessionApi";
Expand All @@ -13,6 +14,20 @@ import Weather from "./Weather";
import { getApiToken, getApiUrl } from "../../api/config";
import { providerApi } from "../../api/modules/provider";
import ModelSelector from "./ModelSelector";
import "./index.module.less";
Comment thread
lllcy marked this conversation as resolved.
Outdated

type CopyableContent =
| { type?: string; text?: string }
| { type?: string; refusal?: string };
Comment thread
lllcy marked this conversation as resolved.
Outdated

type CopyableMessage = {
role?: string;
content?: CopyableContent[];
};

type CopyableResponse = {
output?: CopyableMessage[];
};

interface CustomWindow extends Window {
currentSessionId?: string;
Expand All @@ -22,6 +37,57 @@ interface CustomWindow extends Window {

declare const window: CustomWindow;

function extractCopyableText(response: CopyableResponse): string {
const collectText = (assistantOnly: boolean) => {
const chunks = (response.output || []).flatMap((item: CopyableMessage) => {
if (assistantOnly && item.role !== "assistant") return [];

return (item.content || []).flatMap((content: CopyableContent) => {
if (content.type === "text" && typeof content.text === "string") {
return [content.text];
}

if (content.type === "refusal" && typeof content.refusal === "string") {
return [content.refusal];
}

return [];
});
});
Comment thread
lllcy marked this conversation as resolved.

return chunks.filter(Boolean).join("\n\n").trim();
};

return collectText(true) || collectText(false) || JSON.stringify(response);
Comment thread
lllcy marked this conversation as resolved.
Outdated
}

async function copyText(text: string) {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return;
}

const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "absolute";
textarea.style.left = "-9999px";
document.body.appendChild(textarea);

let copied = false;
try {
textarea.focus();
textarea.select();
copied = document.execCommand("copy");
} finally {
document.body.removeChild(textarea);
}

if (!copied) {
throw new Error("Failed to copy text");
}
}

function buildModelError(): Response {
return new Response(
JSON.stringify({
Expand Down Expand Up @@ -176,6 +242,18 @@ export default function ChatPage() {
[],
);

const copyResponse = useCallback(
async (response: CopyableResponse) => {
try {
await copyText(extractCopyableText(response));
message.success(t("common.copied"));
} catch {
Comment thread
lllcy marked this conversation as resolved.
message.error(t("common.copyFailed"));
}
},
[t],
);

const customFetch = useCallback(
async (data: {
input: any[];
Expand Down Expand Up @@ -250,11 +328,26 @@ export default function ChatPage() {
console.log(data);
},
},
actions: {
list: [
{
icon: (
<span title={t("common.copy")}>
<SparkCopyLine />
</span>
),
onClick: ({ data }: { data: CopyableResponse }) => {
void copyResponse(data);
},
},
],
replace: true,
Comment thread
lllcy marked this conversation as resolved.
},
Comment thread
lllcy marked this conversation as resolved.
Comment thread
lllcy marked this conversation as resolved.
customToolRenderConfig: {
"weather search mock": Weather,
},
} as unknown as IAgentScopeRuntimeWebUIOptions;
}, [wrappedSessionApi, customFetch, t]);
}, [wrappedSessionApi, customFetch, copyResponse, t]);

return (
<div style={{ height: "100%", width: "100%" }}>
Expand Down
Loading